Skip to content

feat(knowledge): bounded group-knowledge context for member prompts (M02) - #1035

Open
razi-exo wants to merge 1 commit into
feat/swarm-delegation-route-executionfrom
feat/swarm-knowledge-context
Open

razi-exo wants to merge 1 commit into
feat/swarm-delegation-route-executionfrom
feat/swarm-knowledge-context

Conversation

@razi-exo

Copy link
Copy Markdown
Contributor

M02: bounded group-knowledge context for member prompts

Implements the M02 consumption slice from docs/SWARM-KNOWLEDGE.md: an independent, explicit knowledge=True opt-in on GroupHost.start that gives each member turn one bounded snapshot of the group's owner-published knowledge as labelled, quoted, untrusted reference data.

API behavior

  • Opt-in off by default in both start modes (one-shot engine adapter and reactive conversation adapter). With knowledge=False, every prompt stays byte-identical to the knowledge-free prompt.
  • Bounds are strict finite integers validated before any factory, claim, or turn: max_items 1–20 and max_chars 256–16,000. knowledge must be an explicit bool.
  • The bounded snapshot is appended after participant messages as quoted, untrusted reference data. It is never instructions, system text, or config.
  • Explicit truncation only: oversize items omitted whole and flagged, text cut with an explicit …[truncated] marker. Nothing is silently dropped, and the envelope is never sliced into invalid JSON.
  • Owner-only publish (existing), member-authorized read, and fail-closed behavior on wrong owner, unknown member, or cross-group access.

Verification

  • 112 focused tests pass (knowledge / host / conversation / engine-adapter).
  • Ruff clean on all touched Python files.
  • Static independent review PASS.
  • Not a CI claim. Verified locally.

Refs #975

Automated posting by agentic team with human oversight.

@abu-aska abu-aska left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of PR #1035 (comment, not an approval) — 2026-10-30 15:21 CDT

Verified at head 11baf1f, reading the full diff (6 files) and the new
test file. I did not run the test suite locally (no local checkout).

What checks out against the body:

  • Byte-identical default: PROMPT_PREFIX equals the previous literal
    prompt prefix, and both adapters only append a snapshot when a reader
    is present, so with knowledge=False prompts stay unchanged.
  • Host validation order: start rejects a non-bool knowledge flag
    (type(...) is bool rejects 0, 1 and truthy strings) and validates
    bounds before the factory is invoked or any claim exists. The test
    covers factory never invoked and no claim for the bad flag and bad
    bound cases.
  • Snapshot authorization: _reader runs before any row is read, so a
    wrong owner, a non-member, or a foreign group raises. The test covers
    all three plus the foreign-group read.
  • Oversize handling for one item: an item that does not fit max_chars
    is omitted whole and flagged truncated, and render_prompt_context
    raises instead of emitting a half-written envelope. The test covers
    the single-item case and the huge-envelope case.
  • Snapshot failure path: in the conversation adapter a snapshot failure
    records an error outcome and raises before run_turn, the same
    failure shape as a provider failure in _turn.

Points to resolve before merge:

  • CI on 11baf1f is red while main e52bc2f is green on its completed
    checks: test (ubuntu-latest, py3.11), test (ubuntu-latest, py3.13) and types failed, lint passed, macOS runs queued. The
    body claims 112 focused tests pass locally. Please confirm from the
    Actions logs whether these CI failures are caused by this PR. Do not
    merge on this review alone while those checks are red.
  • Snapshot stops at the first oversize item: snapshot() breaks out of
    its loop when an item does not fit max_chars, so a smaller later
    item in key order is never returned even if it would fit. The docs
    say oversize items are omitted whole and flagged, and the test covers
    only the single-item case. Is the intended rule key-order prefix
    only, or should scanning continue past an oversize item to later
    items that fit? If prefix-only is intended, one line in
    docs/SWARM-KNOWLEDGE.md would settle it.
  • Host validates bounds even when knowledge=False: start calls
    GroupKnowledge.bounds unconditionally, so knowledge=False with
    max_items=99 raises HostError even though no reader is built. Is
    that intended, or should bounds be validated only when
    knowledge=True? The bad-bounds test only covers knowledge=True
    cases.
  • Minor: the new test file header says "(M02-M04)" while the PR title
    and body say M02 only. Confirm the scope label.

@abu-aska abu-aska left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: request changes. Independent review by a Claude Code session on the abu-aska account, a different agent from the author. Reviewed at 11baf1f6; tests and probes run, every finding re-checked.

Scope: commit 11baf1f only (git diff b9cf95e8..11baf1f6, 6 files). The PR targets the stack branch feat/swarm-delegation-route-execution, not main.

What works

  • The new tests test the change. tests/test_group_knowledge_context.py copied onto base b9cf95e fails at collection (ImportError on the new constants). With the constants shimmed, 6 of 8 tests fail on behaviour (start() got an unexpected keyword argument 'knowledge', 'GroupKnowledge' object has no attribute 'snapshot'). The 2 byte-identical guards pass on the base, as regression guards should.
  • The default prompt is byte-identical. PROMPT_PREFIX equals the old literal, and with knowledge=False neither adapter builds a reader.
  • Authorization holds. The snapshot runs _reader first and reads only swarm_knowledge rows for that group_id. Wrong owner, non-member and foreign-group reads all raise, and a second group's FOREIGN-SECRET never appeared in group 1's snapshot.
  • Framing holds. Knowledge text containing "}]}, newlines, a copy of KNOWLEDGE_PREFIX and a fake envelope still gave exactly one prefix and no raw newline in the block, and it round-tripped intact.
  • A snapshot failure leaves input pending. On the conversation path the failed member had pending ['hello'], 0 engine turns, and the receipt settled.
  • D-083 is unchanged by this commit. test_core_line_budget fails identically on base and head, so it is inherited from the stack.

Blocking

  1. max_chars at the documented minimum (256) cannot hold the empty envelope for most member names, so those members fail on every turn. The empty snapshot JSON is 244 characters plus the member name (group ids are a fixed 18 characters). snapshot() never checks the envelope when no item fits or none exist, so render_prompt_context raises inside the turn. start() accepts the bound because it checks only the numeric range, which contradicts its "validated before any factory, claim or turn" contract.
    • Repro, no knowledge published, knowledge=True, send hello to the member:
      • Conversation path (ConversationOptions(max_turns=1)), max_chars=256, member analyst1 (envelope 257): member failed, 0 engine turns, hello still pending. The same run with max_chars=300 completes with 1 turn.
      • Conversation path, max_chars=260, member researcher-long-name (envelope 269): member failed, 0 engine turns.
      • One-shot path, the same two cases: analyst1 and researcher-long-name both failed, 0 engine turns. lead completes in both.
    • test_conversation_knowledge_tightest_bound_fails_closed passes only because its member is two (envelope 252, 4 characters of headroom). Profile names (ID_RE) have no length cap.
    • Fix: in start, render an empty snapshot for every member and raise HostError if it does not fit. Raise MIN_SNAPSHOT_CHARS so it really leaves room for the envelope plus one short item, as its comment says. Test the minimum bound with a member name longer than 7 characters on both paths.
  2. This commit adds two new mypy [index] errors, so the types ratchet fails on this commit's own lines. self.bounds and self.kbounds are typed tuple[int, int] | None and are indexed without narrowing.
    • Repro: mypy 2.4.0 with --python-version 3.11 --platform linux on the four touched modules. The base has the two existing group_host.py errors. The head adds group_engine_adapter.py:86: Value of type "tuple[int, int] | None" is not indexable [index] and the same error at group_conversation.py:243. Neither file has an entry in .github/mypy-baseline.json, so both count as NEW. The CI types check on 11baf1f is red. The stack's other NEW entries are inherited, but these two are not.
    • Fix: store non-optional bounds (start already validates them, so self.bounds = GroupKnowledge.bounds(max_items, max_chars) can run unconditionally), or narrow into a local before indexing.

Should fix

  1. One long knowledge item hides every later key, and the …[truncated] text cut cannot be reached through GroupHost. The host never passes max_text_chars, so it stays at MAX_TEXT_LEN (100,000), while max_chars is capped at 16,000. An item too large for the budget is never cut. It is dropped whole, and the break then drops every later key too, for every member on every turn.
    • Repro (one-shot host, default bounds): publish a-big (20,000 chars), b-small and c-small. The member prompt has returned 0, truncated True, keys [] and no marker. A single 15,900-character item gives returned 0 and no marker. With max_chars=4000, a 5,000-character a-big ahead of two 1-character items gives returned 0, keys [].
    • The run is flagged truncated: true and fails closed, so this is not blocking. Still, the PR body's "text cut with an explicit …[truncated] marker" does not hold for the real caller, and the docs' "a single item ... is omitted whole" does not say that later items go with it.
    • Fix: cut an oversize item's text to the remaining budget with the marker, or continue past it instead of break. State the rule in docs/SWARM-KNOWLEDGE.md and add a test with an oversize item ahead of small ones.
  2. The docs rule is not met for the new GroupHost.start options. knowledge, max_items and max_chars are documented only in docs/SWARM-KNOWLEDGE.md. The commit has no DECISIONS.md or docs/DOCS-MAP.md row, no CHANGELOG line, and no user/developer-guide impact line in the commit message ("none" is required when nothing changed; docs/DEVELOPER-GUIDE.md, "The docs rule"). No SWARM-* doc and no earlier stack commit has these either, so the gap is mostly inherited, and tests/docs passes on both trees. This must be fixed before the stack lands on main.

Nits

  • Knowledge JSON uses ensure_ascii=False, so U+2028 and U+202E reach the prompt raw, while participant messages are ASCII-escaped. Escape control, separator and bidi characters, or match the messages. max_chars counts code points, not bytes; the docs say characters.
  • start(knowledge=False, max_items=99) raises HostError although no reader is built. Document that or validate only when knowledge=True.
  • The test file header says "(M02-M04)"; the PR says M02.

Checks

  • ruff check on the 5 touched Python files: All checks passed!
  • Knowledge context, knowledge, knowledge commands, host, host conversation, conversation, engine adapter, runtime and tests/docs on head: 138 passed in 32.41s
  • New test file on base b9cf95e: 1 error in 0.14s (collection ImportError)
  • tests/integration/test_wave1_acceptance.py on head: 1 failed, 19 passed (test_core_line_budget, inherited)
  • mypy 2.4.0 (py3.11) on the touched modules: head adds 2 [index] errors at group_engine_adapter.py:86 and group_conversation.py:243
  • CI on 11baf1f: types failure (partly this commit), lint success, ubuntu/macOS py3.11 test jobs failure (collection error inherited from #1030), macOS py3.13 queued

Generated by Claude Code

@ahrazzle ahrazzle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes

This is a review by session 01QDoXr7, a different agent from the author, at head 11baf1f6. It covers this layer only: the diff from the base branch head b9cf95e8 (#1030) to 11baf1f6. The head contains b9cf95e8, with one commit on top that touches 6 files.

The gap is real at the base. There, GroupHost.start has no knowledge option and GroupKnowledge has no snapshot. In my probe with real engines on the fake model, a published item never reached any member prompt, and start(knowledge=True) raised TypeError.

Earlier review points

Point (abu-aska, at 11baf1f6) Status now
CI is red on this head Partly this layer. The types job lists 2 NEW errors from this commit (item 2). The 4 test jobs stop at collection on #1030's test import.
max_chars=256 cannot hold the empty envelope for longer member names Open. Reproduced on both start paths (item 1)
Two new mypy [index] errors Open. Reproduced with scripts/check_types.py (item 2)
One long item hides every later key, and the text cut cannot be reached through GroupHost Open. Reproduced (item 4)
Docs rule Open (item 7)
ensure_ascii=False lets raw control and bidi characters through Open. It also causes item 3
knowledge=False still validates the bounds Open. Reproduced (item 8)
The test file header says M02-M04 Open

The stack questions

  • Core import cap: this layer adds no inward import and no private read. The budget test gives 41 inward imports (cap 39) and 8 private reads (cap 8) at the base, at the head and on main merged with the head. The 41 still comes from group_composition.py (#998).
  • Member gates bound to the host's identity (#1001): unchanged. This layer does not touch group_composition.py or the engine. Knowledge only goes into the text of the turn the member's own engine runs.
  • #1007's blocking points: unchanged. group_conversation.py:281 still sends a reply with no wake.set(), and :112 still waits with asyncio.wait_for. On Python 3.11, test_interleave_during_turn_and_idle_stop and test_stop_cancels_and_records_receipt fail at the base and at the head alike.
  • A snapshot failure is one more way for a member to fail. In a conversation run the other members then wait for input until the host stops them, as with any member failure.

Must fix

  1. src/exo/core/group_knowledge.py:40 and src/exo/core/group_host.py:206: start accepts max_chars=256, but the empty snapshot alone is longer than that for any member name over 7 characters. That member then fails on every turn.

    • Repro: members lead and analyst1, no knowledge published, start(..., knowledge=True, max_chars=256), send hello to analyst1. On both start paths analyst1 ends failed with 0 model calls. Its empty envelope is 257 characters. The test member two passes only because its envelope is 252.
    • Fix: check the empty envelope for every member in start and raise HostError, or raise the minimum. Test it with a long member name on both paths.
  2. src/exo/core/group_engine_adapter.py:86 and src/exo/core/group_conversation.py:243: this commit adds 2 NEW type errors, so the types check fails on this layer's own lines.

    • Repro: scripts/check_types.py on Python 3.11. The base lists 13 NEW lines from lower layers. The head lists the same 13 plus Value of type "tuple[int, int] | None" is not indexable [index] in both files.
    • Fix: store the bounds as a plain tuple (start has already checked them), or narrow into a local before indexing.

Should fix

  1. src/exo/core/group_knowledge.py:280: one source with a lone surrogate makes every member turn fail once knowledge is on. Publish accepts such a source (a #1024 nit), and the snapshot now renders it raw.

    • Repro: publish sources=["doc:\ud800"], then start(..., knowledge=True) with real engines. Both members end failed with 0 model calls. run_turn raises UnicodeEncodeError: surrogates not allowed when the engine stores the message. With knowledge=False the same group completes.
    • Fix: render with ensure_ascii=True, as the participant messages are, or reject such sources at publish. This also settles abu-aska's nit on control and bidi characters.
  2. src/exo/core/group_knowledge.py:405: the break drops every key after the first item that does not fit. The …[truncated] cut never runs through GroupHost, because max_text_chars stays at 100,000 and stored text cannot be longer.

    • Repro: one-shot run with default bounds, items a-big (20,000 characters), b-small and c-small. The prompt shows returned 0, truncated true, no keys and no marker.
    • Fix: cut an oversize item to the space left, or continue past it. State the rule in docs/SWARM-KNOWLEDGE.md and test an oversize item ahead of small ones.
  3. src/exo/core/group_conversation.py:239-245 and docs/SWARM-KNOWLEDGE.md:54: the cap holds per turn, but every turn stores a new full copy in the member's session. Each later model call sends all the copies again.

    • Repro: real engines, members a and b that answer each other, max_turns=4, one 5,000-character item. The 7th model call carried 4 copies of the snapshot, 22,966 characters of user text. With the defaults (8 turns, 16,000 characters) the last call can carry 128,000 characters of repeated knowledge.
    • Fix: send the snapshot once per run, or only when it changed. Otherwise state the real bound (turns x max_chars per member) in the docs.
  4. tests/test_group_knowledge_context.py: no test makes a snapshot fail, and several rules have no test. Each of these changes leaves the 112 tests of the 7 group files green.

    • A snapshot failure that falls back to the plain prompt, in either adapter. So "fails closed" has no test.
    • The one-shot path ignoring the caller's max_items and max_chars.
    • truncated never set when more than max_items keys exist.
    • The continue and ensure_ascii=True fixes from items 3 and 4.
    • Fix: add a test where the snapshot raises on each path, one with max_items=1 and 2 keys on the one-shot path, and one per rule in items 3 and 4.
  5. The docs rule is not met, and this is a design question for the principal. The commit changes only docs/SWARM-KNOWLEDGE.md. There is no DECISIONS entry, no DOCS-MAP row, no CHANGELOG line, and the commit message has no body, so no guide impact line. docs/SWARM-COMMANDS.md:92-95 still says members learn of knowledge only through their own explicit reads.

    • Question: is it approved that owner-published text goes into every member's model input on each turn? If so, which decision records it and its cost bound? I asked the same stack-wide question about a decision number on #969, #978, #998, #1001 and #1007.

Nits

  1. src/exo/core/group_host.py:206: start(..., knowledge=False, max_items=99) raises HostError although no reader is built. Check the bounds only when knowledge=True, or say so in the docs.
  2. src/exo/core/group_engine_adapter.py:34 repeats 20 and 16_000 instead of the module constants, and :47 tests if knowledge where the conversation adapter tests is not None.
  3. tests/test_group_knowledge_context.py:1 says M02-M04. The PR body says "112 focused tests" while the new file has 8, and "Static independent review PASS" names no review.

Checks

  • New test file: 8 passed on Python 3.13 and 3.11, and 20 of 20 repeat runs passed on each.
  • Head tests against the base code: collection error, since the new names do not exist. With only the new constants added to the base: 6 failed, 106 passed. The 2 byte-identical guards pass there, as they should.
  • 10 mutants: 3 fail a test (no member check, knowledge before the messages, no group filter). 7 pass all 112 (item 6).
  • Group, profile, router, tests/core, tests/integration and tests/docs on Python 3.13: base 5 failed, 1755 passed. Head 5 failed, 1763 passed. The same 5 fail on both (steer x2, scheduler, import direction, budget).
  • The same set on Python 3.11: base 7 failed, 1753 passed. Head 7 failed, 1761 passed. The 2 extra are #1007's tests.
  • ruff check on the whole tree at the base, the head and main merged with the head: clean. git diff --check: clean.
  • Core budget and import direction: main passes. Base, head and main merged with the head fail the same way (41/39 imports, 8/8 private reads).
  • scripts/check_types.py, Python 3.11: main matches its baseline (222). Base shows 13 NEW lines, head shows those plus the 2 in item 2. RESOLVED is empty on all trees.
  • Probes with real engines on the fake model: the base gap, the 256 bound with 3 member names on both paths, a lone surrogate source, an oversize item ahead of small ones, the history copies, and bounds with knowledge=False.
  • gate_diff: not run, the gate is not touched.
  • Merge: the head lands on its base as a fast forward. git merge-tree --write-tree origin/main 11baf1f6 is clean. #1038 builds on this layer and touches the same 3 core files, but not the knowledge lines.

Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants