Skip to content

fix(agent): wait for session file in agent send --wait - #401

Open
codeaholicguy wants to merge 1 commit into
mainfrom
fix/send-wait-session-race
Open

codeaholicguy wants to merge 1 commit into
mainfrom
fix/send-wait-session-race

Conversation

@codeaholicguy

Copy link
Copy Markdown
Owner

Closes #400

Repro

ai-devkit agent start --type pi --name test-agent --cwd /tmp/somewhere
ai-devkit agent send --id test-agent --wait --timeout 120000 --json "hello"
# -> ✖ No session file found for agent "test-agent"

Root cause

The agent registry entry is created at agent start, but the session file only materializes when the harness writes its first entry — which happens on the first message. sendToAgent resolved agent.sessionFilePath at dispatch time and threw No session file found immediately when it was absent, so send --wait right after start could never succeed.

Fix design

In sendToAgent (packages/cli/src/services/agent/agent.service.ts):

  • The prompt is sent first (unchanged delivery path via sendAgentPrompt). This matters: the send is often what triggers the harness to write its first session entry.
  • When agent.sessionFilePath is absent, the new waitForSessionFile polls manager.listAgents() every 500ms until the file appears, bounded by min(--timeout, 30s) default grace.
  • If the agent disappears from listAgents() while polling, a distinct error is raised: Agent "<name>" exited before creating a session file — separate from the generic response timeout.
  • If the grace elapses: Agent "<name>" did not create a session file within <N>ms.
  • Session-file appearance doubles as the harness readiness signal, covering the related "send into a still-booting TUI is silently dropped" symptom noted in the issue.
  • When sessionFilePath already exists, behavior is unchanged: the transcript baseline is still seeded before the send so no messages are skipped.

Validation

  • Unit: 3 new/updated cases in agent.test.ts — file appears after send → success; agent exits → distinct error; grace expiry → bounded error. packages/cli suite: 1341 tests pass.
  • e2e: new e2e/agent-send-wait.e2e.ts drives the compiled sendToAgent through a scripted listAgents() sequence reproducing start-then-send-wait. Trade-off: a real agent start boot needs tmux + a live harness binary, too heavy for CI — so process detection and tmux delivery are stubbed via the injected runtime/focusManager seams while the send → poll → wait path runs against the real built dist. Verified it catches the bug: all 3 tests fail against the pre-fix code.
  • npm run lint, npm test, npm run test:e2e all green (pre-commit hook + manual runs).

Generated with Devin

agent send --wait executed right after agent start failed with
"No session file found" because the registry entry exists at start while
the session file only materializes when the harness writes its first
entry (on the first message). The wait path resolved the session file at
dispatch time and failed immediately.

Send the prompt first, then poll listAgents() for the session file to
appear, bounded by the caller's --timeout or a 30s default grace period.
Session-file appearance doubles as the harness readiness signal, so
sends into a still-booting TUI are no longer silently dropped either.
A distinct error is raised when the agent process exits without ever
creating a session file.

Closes #400

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

fix(agent): agent send --wait immediately after agent start fails with 'No session file found'

1 participant