fix(agent-history): ignore sessions untouched before agent first seen - #3
Open
dorokuma wants to merge 1 commit into
Open
fix(agent-history): ignore sessions untouched before agent first seen#3dorokuma wants to merge 1 commit into
dorokuma wants to merge 1 commit into
Conversation
When a pane agent is first observed, its authoritative session ref (herdr detection or shepherd-pi registration) may not have landed yet, so history resolution falls back to cwd-based discovery. Discovery scans fixed roots (~/.pi/agent/sessions and peers) and ranks by cwd match and mtime, which can select an unrelated stale session that happens to share the cwd. That session's content is then persisted into status events and delivered to the orchestrator's wake messages, misattributing another run's output. A session file untouched since before the agent's first_seen_at cannot be that agent's live session, so discovery now drops candidates older than first_seen_at (10 min grace) and reports no history rather than a guessed one.
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.
What & why
When a pane agent is first observed, its authoritative session ref (herdr detection or shepherd-pi registration) may not have landed yet, so history resolution falls back to
discoverAgentHistory. Discovery scans fixed roots (~/.pi/agent/sessionsand peers) and ranks candidates by cwd match plus mtime. If an unrelated, stale session file happens to share the pane's cwd, it gets picked as the "best" candidate; its content is then persisted into the agent's status events and delivered to the orchestrator's wake messages — misattributing another run's output to the new pane agent.Observed signature: the first status events for a pane carry
historyRef.kind === "discovered_file"whose content comes from an unrelated run; once the real registration lands, subsequent events usehistoryRef.kind === "agent_session"and are clean.Fix principle
A session file untouched since before the agent's
first_seen_atcannot be that agent's live session. Better to report no history than wrong history. Discovery now drops candidates whose mtime is older thanfirstSeenAtMs - DISCOVERY_RECENCY_GRACE_MS.agentSession.kind === "path"branch is untouched — it is not a cwd-based guess.firstSeenAtMsis optional. When omitted, behaviour is exactly as before, so cwd-guessing agents that don't pass it (claude / codex / gemini / …) are unaffected.Changes
src/agent-history/discovery.ts: add optionalfirstSeenAtMsto the lookup input, exportDISCOVERY_RECENCY_GRACE_MS, and filter stale candidates before ranking.src/observability/agent-context-service.tsandsrc/daemon/observability-server.ts: passagent.firstSeenAtinto the history lookup.test/unit/agent-history-discovery.test.ts, plus aresolveCompactHistorypassthrough test intest/unit/agent-history-service.test.ts.Validation
pnpm typecheck✓pnpm lint/pnpm format:check✓pnpm test: all tests pass except one pre-existing, unrelated failure intest/unit/shepherd-pi-extension.test.ts("does not connect outside a complete Herdr environment"), which also fails on pristineorigin/mainin this environment.