Repository navigation
fix(recall): read each agent's session variable so recall quality joins its session - #887
Merged
Merged
Conversation
…ns its session
`teamai recall` recorded recall quality under deriveSessionId({}), which
falls back from the payload to CLAUDE_SESSION_ID. Claude Code does not set
that variable (it sets CLAUDE_CODE_SESSION_ID), so recall used pid-<ppid>, a
new id per shell, and contribute-check never found it under the hook's
session_id: the knowledge-gap bonus never applied.
agentSessionIdFromEnv reads each agent's session variable (Claude Code,
Codex, CodeBuddy, Copilot CLI, Cursor, Pi, then CLAUDE_SESSION_ID). recall,
contribute and session save use it; they are CLI commands with no hook
payload. deriveSessionId is unchanged, so hooks keep their ids: a bridge
that sends no session_id (OpenCode, Pi, OMP) would otherwise inherit the
outer agent's variable when started from another agent's shell.
A vitest setup file clears those variables, so the suite does not depend on
the agent shell it runs in, and tests that do not isolate HOME cannot write
the live session's state into the real ~/.teamai.
Refs Tencent#883 (part 1)
|
Findings
The PR description includes a representative real-CLI verification, so its testing record is sufficient. |
Pi's hook bridge sends no session id, so Pi hooks record under the pid-<ppid>-<cwd> fallback. Reading PI_SESSION_ID made recall, contribute and session save use an id with no events: contribute-check never saw the recall quality or contributed state, and session save reported "No events found". It is no longer read. An agent started from another agent's shell inherits the outer agent's variable next to its own (Codex inside Claude Code has both CLAUDE_CODE_SESSION_ID and CODEX_SESSION_ID), and the fixed order picked the outer one. When several distinct ids are set, agentSessionIdFromEnv now picks the one whose session has the latest hook event, falling back to the variable order when none has events. The common single-variable case reads no events. Refs Tencent#883 (part 1)
|
Review Result
|
…rted session Pi and OpenCode export none of AGENT_SESSION_ENV, so started from a Claude Code or Codex shell they saw only the outer agent's inherited id. contribute then marked the outer session contributed, which suppressed its contribute-check hint; recall skewed the outer recall cache; and session save recorded the outer session. When a bridge marker is set (PI_SESSION_ID, set by Pi's bash tool, or OPENCODE, set by OpenCode), agentSessionIdFromEnv returns undefined, so each caller's fallback from main applies. An agent started from a Pi or OpenCode shell falls back too, as on main. OMP exports no marker. With several ids set, the id whose session started last now wins, instead of the id with the latest event. The inner agent always starts after the outer one, and a background `codex exec` or parallel subagents keep the outer session firing hooks. Tests now cover contribute marking the agent session, session save using the agent session and falling back to the most recent, and the nested Pi case through recall, contribute and session save. Refs Tencent#883 (part 1)
|
Findings
Review Status
|
…ed one wins agentSessionIdFromEnv compared each id's first event. `claude --resume` of yesterday's session, run from a new Codex session, then looked older than Codex, and recall and contribute state went to the outer Codex session. Each id now counts from its latest session_start event, falling back to its first event. Claude Code and Codex fire SessionStart on resume, and teamai registers it with matcher '*', so every resume records one. Refs Tencent#883 (part 1)
|
Findings
Review Status
|
jeff-r2026
approved these changes
Sep 29, 2026
This was referenced Sep 29, 2026
Walternater
pushed a commit
to Walternater/teamai-cli
that referenced
this pull request
Nov 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
teamai recallrecorded recall quality under an id that no hook ever uses, so contribute-check's knowledge-gap bonus never applied (#883, part 1). Recall now reads the agent's own session variable, which equals the session_id its hooks receive. Inside a Pi or OpenCode shell it reads none of them, because those bridges send hooks no session id and any variable they see is inherited from an outer agent. When one agent runs inside another, the inner agent's session wins.Type of Change
Test Plan
npx tsc --noEmitpassesnpm run lintpassesnpx vitest runpasses (run asenv -u CLAUDE_CONFIG_DIR npx vitest rununtil fix(tests): prevent model tests from overwriting Claude config (P1) #890 lands): 337 files, 5270 passed, 1 skippedenv -u CLAUDE_CONFIG_DIR npm run test:e2epasses: 59 files passed, 3 skipped; 368 tests passed, 26 skippedRegression test, driving
recall()(recall-scope-isolation.test.ts):session-id.test.tspins the variable order, each variable, thatPI_SESSION_IDorOPENCODEreturns undefined even with an inherited variable, the nested case (including when the outer session fires last, and a resumed inner session whose first event predates the outer one), CodeBuddy's own variable before itsCLAUDE_SESSION_IDalias, empty values, and that a hook with no session_id keeps itspid-<ppid>-<cwd>fallback even when it inheritsCLAUDE_CODE_SESSION_ID.Real CLI (
npm run build, sandboxHOMEwith one team doc,recall --dry-run "session"):contribute-edge-modes.test.tschecks thatcontribute()marks the agent's session contributed, and does not mark an outer Claude session from a Pi shell.save-session.test.tschecks thatsession saveuses the agent's session, falls back to the most recent session, and uses the most recent session from a Pi shell started by Claude Code.recall-scope-isolation.test.tschecks that recall from that Pi shell writes nothing under the Claude session.Nested agents, real CLI (
npm run build, tempHOME,CLAUDE_CONFIG_DIRand all agent variables unset),session save --dry-run:Before the fix, these cases fail. Against
main: the order list still containedPI_SESSION_ID,PI_SESSION_IDalone returnedpi-session, and the nested case returnedouter-claude. Against 4b3f325:CLAUDE_CODE_SESSION_IDwithPI_SESSION_IDorOPENCODEreturnedouter-claude, as did the nested case when the outer session fired last. Against fb97e0d: the resumed inner Claude session returnedouter-codex. From a Pi shell, recall wrote Claude's recall cache, contribute marked Claude's session contributed, andsession saverecorded the Claude session.Related Issues
Refs #883 (part 1). Part 2, adoption, is proposed in #884.
Notes for Reviewers
Door: two-way. It adds no stored format or migration: a revert only renames future cache files back to pid ids.
Blast Radius: small.
deriveSessionIdis the same as onmain. Putting the agent variables there would have moved the OpenCode, Pi and OMP bridges, which send no session_id, onto an outer agent's session when started from its shell. A test pins that.session saveinside an agent shell now records that agent's session instead of the most recent one, which is what its help already promised with$CLAUDE_SESSION_ID. If hooks never recorded that session it printsNo events found for session <id>.;--session-idstill overrides.contributenow marks the current session contributed on Claude Code, Codex and the others, so contribute-check stops hinting a session that already contributed.codex execstarted from a Claude Code shell recorded under the Claude session. When several different ids are set, the one whose current run started last wins, because the inner agent always starts after the outer one. This holds even when the outer agent keeps firing hooks, for example with a backgroundcodex execor parallel subagents. A run starts at the session's latestsession_startevent, or at its first event if it has none, so a resumed session (claude --resumeof yesterday's session from a new Codex session) counts from its resume. teamai registers SessionStart with matcher*(builtin-hooks.ts), so every SessionStart call recordssession_start. Claude Code and Codex fire SessionStart onstartup,resume,clearandcompact(Codex:codex-rs/hooks/src/events/session_start.rsand the hooks docs). I did not check CodeBuddy, Copilot or Cursor on resume; without a resume start event they fall back to the first event, as in fb97e0d. Limit: SessionStart also fires on compaction, so an outer agent that compacts while the inner one runs (possible only with a background or parallel call) counts as started later and wins. Stored events do not recordsource, so this cannot be told apart without a format change.events.jsonlis read only when more than one id is set. If the inner agent has no hooks installed, its session has no events and the outer one is picked; no hook would read the inner id anyway. Proposal: attribute recall and adoption to the agent session in every agent #884 still covers adoption throughPostToolUse.pid-<ppid>-<cwd>. Any id in their shell is inherited from an outer agent. Pi's bash tool setsPI_SESSION_ID(on by default) and OpenCode setsOPENCODE=1. When either is set,agentSessionIdFromEnvreturns undefined and each command falls back as onmain: recall usesderiveSessionId({}), contribute marks nothing, andsession saveuses the most recent session. The trade-off is reverse nesting: Claude Code or Codex started from a Pi or OpenCode shell also falls back, as onmain, so it does not join its own hook session. OMP exports no marker I could find, so OMP started from another agent's shell still sees the outer id. Forwarding the bridge's own session id in hook payloads would fix both, and is a separate change.deriveSessionId'sCLAUDE_SESSION_IDstep, used by hooks and by recall's fallback, has the same inherited-id problem when a bridge agent runs under CodeBuddy, which sets that alias.{"contributed": true}into the real~/.teamai.skill-commands-exist.test.tsfailed once under full parallel load (command.commandsundefined), then passed alone and in two full runs. It is unrelated to this change.