feat(chat-workflow): thread real cwd + currentBranch into system prompt (cutover Bundle A.7)

Merged
SW
6mo ago
+131-10

Summary

Third open-agents → api cutover bundle. The handler was hardcoding workingDirectory: DEFAULT_WORKING_DIRECTORY and never setting currentBranch, so the agent had no environment info in its system prompt and had to run pwd / git branch on every turn.

Production reproducer (today, before this fix)

Same prompt sent to https://recoup-api.vercel.app/api/chat/workflow:

Prompt: "DO NOT run any tools. Just answer from your system prompt only: what is your current working directory and current git branch? If your system prompt doesn't contain that info, say exactly 'My system prompt does not contain working directory or branch information.'"

Agent response: "My system prompt does not contain working directory or branch information."

Compare against sandbox.recoupable.com (open-agents prod) which answers:

"My system prompt states the working directory is . (workspace root), but it does not contain any git branch information." (branch missing because that session had no repo attached; open-agents threads it when present)

After this fix

The agent receives an # Environment section + Current branch: <branch> line + cloud-sandbox checkpointing block — same shape as open-agents emits.

Changes (SRP)

  • lib/chat/buildAgentSystemPrompt.ts — assembles environment section → Current branch → cloud-sandbox checkpointing → custom instructions, all conditional on inputs. Mirrors open-agents/packages/agent/system-prompt.ts:420-447.
  • lib/chat/cloudSandboxInstructions.ts — ports CLOUD_SANDBOX_INSTRUCTIONS block with {branch} placeholder substitution.
  • handleChatWorkflowStream — reuses the already-connected sandbox (currently only used for skill discovery) to read real workingDirectory + currentBranch, threads both into AgentContext.sandbox. Falls back to DEFAULT_WORKING_DIRECTORY on connect failure (preserves today's behavior).
  • runAgentStep — calls buildAgentSystemPrompt({cwd, currentBranch, customInstructions}) instead of using static agentCustomInstructions directly.

Scope reduced from "A.7+9"

Dropped contextLimit plumbing. Grepped open-agents' server code — it never reads context.contextLimit either. It's a client-side display concern (token-usage progress bar), and api already accepts the field via Zod strip. Verified behaviorally: production selectedModelId is identical with/without contextLimit in both api and open-agents. Bundle A is now just A.7.

Cutover roadmap

#BundleThis PRNotes
1C per-message cost/usage✅ merged#592 / #593
2B subagent live progress + transcript✅ merged#594 / #595
3A.7 real cwd + currentBranch in system prompt✅ this PRA.9 dropped — not a server concern
4A.6 Anthropic prompt cache control
5A.4 forward Privy JWT as recoupAccessToken

Test plan

  • 7 new unit tests (6 buildAgentSystemPrompt + 1 runAgentStep wiring assertion)
  • Full suite 3121/3121 pass; lint clean
  • E2E on preview: re-run the "what's your cwd/branch?" prompt — expect the agent to answer with the env section info instead of "doesn't contain"

🤖 Generated with Claude Code


Summary by cubic

Threads the sandbox working directory into the agent’s system prompt so the agent knows it’s in a workspace and can use relative paths without running pwd each turn. Drops branch templating for now since it’s always main.

  • New Features

    • Added buildAgentSystemPrompt to prepend an Environment section and then append custom instructions.
    • Updated handleChatWorkflowStream to connect to the sandbox, read real workingDirectory, and pass it into AgentContext.sandbox (falls back to DEFAULT_WORKING_DIRECTORY on failure).
    • Updated runAgentStep to use buildAgentSystemPrompt({ cwd, customInstructions }) instead of a static agentCustomInstructions.
  • Bug Fixes

    • Removed a stale currentBranch argument in runAgentStep after dropping branch templating, fixing the build.

Summary by CodeRabbit

  • New Features

    • Agent system prompts now dynamically include sandbox working directory and branch information.
  • Improvements

    • Sandbox connection established earlier in the workflow process for better context discovery.
    • System prompt generation enhanced with conditional environment information.

Review Change Stack