Skip to content

fix: re-trace system prompt on agent switch#548

Open
VJ-yadav wants to merge 3 commits intoAltimateAI:mainfrom
VJ-yadav:fix/prompt-tracing-agent-switch
Open

fix: re-trace system prompt on agent switch#548
VJ-yadav wants to merge 3 commits intoAltimateAI:mainfrom
VJ-yadav:fix/prompt-tracing-agent-switch

Conversation

@VJ-yadav
Copy link
Copy Markdown
Contributor

@VJ-yadav VJ-yadav commented Mar 28, 2026

Summary

  • The step === 1 guard in prompt.ts only traced the first agent's system prompt because step increments continuously and is never reset on agent switches
  • Added a tracedAgent variable to track the last-traced agent name, so each agent's system prompt is logged when it first becomes active
  • Fixed the inaccurate comment that claimed agent switches reset step to 0

Fixes #291

Test Plan

  • Verify typecheck passes on prompt.ts (pre-existing test file failures are unrelated)
  • Start a session, switch agents mid-conversation, confirm both agents' system prompts appear in trace output
  • Confirm single-agent sessions still trace the system prompt exactly once

Checklist

  • Change is minimal and focused on the reported issue
  • No functional behavior change — tracing/observability only
  • Comment updated to accurately describe the logic

Summary by CodeRabbit

  • Refactor
    • Improved session tracing so system-prompt entries are emitted on the initial step and whenever a different agent begins execution. This reduces redundant trace noise, makes agent transitions clearer in logs, and ensures each agent’s system prompt appears once per switch for more accurate, more readable session traces.

Copy link
Copy Markdown

@claude claude bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions
Copy link
Copy Markdown

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 121a47ed-c4bb-4ffd-bdfd-b59a4e2d25d9

📥 Commits

Reviewing files that changed from the base of the PR and between 6d200f5 and 3c7fe0c.

📒 Files selected for processing (1)
  • packages/opencode/src/session/prompt.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/opencode/src/session/prompt.ts

📝 Walkthrough

Walkthrough

System-prompt tracing in SessionPrompt.loop now tracks the last-traced agent name and logs the "system-prompt" span when either the loop is at step 1 or the active agent differs from the tracked agent, then updates the tracked agent name so each agent's system prompt is traced once upon activation.

Changes

Cohort / File(s) Summary
Agent Tracing Logic
packages/opencode/src/session/prompt.ts
Add tracedAgent loop-local state; change Tracer guard from step === 1 to `step === 1

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

contributor

Poem

🐰 I hopped through loops with nimble feet,

I watched each agent's name I’d greet.
When new friends came, I left a trace,
No prompt omitted in my race.
Hop, log, repeat — a tidy feat.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description lacks the required 'PINEAPPLE' keyword mandated by the repository template for AI-generated or AI-assisted contributions. Add 'PINEAPPLE' at the very top of the PR description as required by the template for AI-generated contributions.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: re-trace system prompt on agent switch' accurately and specifically describes the main change: fixing system prompt tracing to occur when agents switch.
Linked Issues check ✅ Passed The code changes fully implement the requirements from issue #291 by adding tracedAgent tracking to re-trace system prompts when agents switch, matching the proposed fix.
Out of Scope Changes check ✅ Passed All changes are in-scope: the tracedAgent variable implementation, tracing condition update, and comment fix directly address issue #291 with no unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@VJ-yadav VJ-yadav force-pushed the fix/prompt-tracing-agent-switch branch from 89b8cbc to 3b691b5 Compare April 3, 2026 06:11
@dev-punia-altimate
Copy link
Copy Markdown

❌ Tests — Failures Detected

TypeScript — 15 failure(s)

  • connection_refused [2.73ms]
  • timeout [2.41ms]
  • permission_denied [2.67ms]
  • parse_error [2.35ms]
  • oom [2.49ms]
  • network_error [2.33ms]
  • auth_failure [2.50ms]
  • rate_limit [2.59ms]
  • internal_error [2.63ms]
  • empty_error [0.25ms]
  • connection_refused [0.16ms]
  • timeout [0.10ms]
  • permission_denied [0.07ms]
  • parse_error [0.07ms]
  • oom [0.07ms]

cc @VJ-yadav
Tested at 3b691b5a | Run log | Powered by QA Autopilot

@anandgupta42
Copy link
Copy Markdown
Contributor

Review — Ready after rebase

Scope: Fixes #291step === 1 guard in prompt.ts only traced the first agent's system prompt because step increments continuously and is never reset on agent switches (despite the comment claiming otherwise).

Analysis

  • Minimal, targeted fix: 6 additions, 5 deletions in one file (session/prompt.ts)
  • Introduces tracedAgent to track the last-traced agent name; logs each agent's system prompt when it first becomes active
  • Fixes the misleading comment that claimed step resets to 0 on agent switches
  • Pure observability/tracing fix — no functional runtime behavior change
  • Correctness matches the issue's root-cause analysis exactly

CI status (commit 3b691b5a)

  • ⚠️ Only check-standards, check-compliance, GitGuardian ran — missing TypeScript, Marker Guard, anti-slop
  • Same reason as fix: add pagination support for recap trace list #596: PR is ~1 week behind main, CI workflow matrix has since expanded. Rebase will retrigger full CI
  • ⚠️ session/prompt.ts IS an upstream-shared file — after rebase, verify Marker Guard passes (altimate_change markers needed around the tracedAgent variable + the restructured if-block)
  • centralized-test-results — same batch-wide flakiness as every other open PR

Recommendation
Ready for merge after rebase + Marker Guard verification. The fix itself is correct and minimal. The only real risk here is the upstream-shared file needing markers — confirm by running bun run script/upstream/analyze.ts --markers --base main --strict locally post-rebase.

cc @anandgupta42 — rebase, verify marker guard, then ready for /release.

@VJ-yadav VJ-yadav force-pushed the fix/prompt-tracing-agent-switch branch from 3b691b5 to 6d200f5 Compare April 5, 2026 05:27
The `step === 1` guard only traced the first agent's system prompt
because `step` increments continuously and is never reset on agent
switches. Track the last-traced agent name so subsequent agents also
get their system prompts logged.

Fixes AltimateAI#291

Co-Authored-By: Vijay Yadav <vijay@studentsucceed.com>
@anandgupta42 anandgupta42 force-pushed the fix/prompt-tracing-agent-switch branch from 6d200f5 to e060841 Compare April 5, 2026 06:41
@anandgupta42
Copy link
Copy Markdown
Contributor

Consensus code-review applied — no changes needed

Ran a 4-participant consensus review (Claude + GPT 5.4 + Gemini 3.1 Pro + self-review).

Verdict: APPROVE — both GPT 5.4 and Gemini 3.1 Pro agree the 6-line fix is correct and ready to merge.

The tracedAgent guard at packages/opencode/src/session/prompt.ts correctly traces each agent's system prompt exactly once per distinct contiguous agent, which is the stated contract from issue #291. Resumption safety verified — tracedAgent="" default preserves re-capture-on-fresh-loop behavior.

Non-blocking suggestions (NOT applied, kept as optional follow-ups):

  • Regression test for multi-iteration loop with an agent switch (GPT, MAJOR / Gemini, MINOR) — would require stubbing Tracer.active?.logSpan and driving a mock plan→builder switch
  • A→B→A edge case test to lock in current re-trace-on-return behavior (GPT, MINOR)
  • Simplify the redundant step === 1 check since tracedAgent="" and agent.name is always non-empty (Gemini, NIT)

The first suggestion is worthwhile but requires session-loop test infrastructure that doesn't exist yet; tracked as a follow-up. The fix is small enough that shipping without new tests is acceptable.

All CI green. Ready for merge as-is.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: system prompt tracing skips subsequent agents due to step counter not resetting

3 participants