Skip to content

fix(session): preserve active agent and model on prompt_async without explicit fields#21729

Closed
sjawhar wants to merge 1 commit into
anomalyco:devfrom
sjawhar:fix/prompt-async-agent-preserve
Closed

fix(session): preserve active agent and model on prompt_async without explicit fields#21729
sjawhar wants to merge 1 commit into
anomalyco:devfrom
sjawhar:fix/prompt-async-agent-preserve

Conversation

@sjawhar
Copy link
Copy Markdown

@sjawhar sjawhar commented Apr 9, 2026

Issue for this PR

Closes #21728

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When prompt_async (or command()) arrives without explicit agent or model fields, the session now preserves the active agent and model from the most recent user/assistant message instead of falling back to the default agent's configured defaults.

Before: Omitted agent/model on prompt_async defaults to config, silently overriding the session's active state. Envoy notifications could redirect a session's agent and model.

After: Omitted fields inherit from the current session prompt state. If the caller explicitly sets agent, model falls through to that agent's config (correct behavior). If both are omitted, both are preserved from the session's current state.

Changes:

  • Replaced the agent-only currentAgent() helper with a broader current() helper that returns both agent and model from the most recent message
  • createUserMessage() now uses cur?.agent and cur?.model as fallbacks before agent config defaults
  • Model preservation is scoped: only applies when agent is also omitted (if caller explicitly sets agent, model defaults to that agent's config)
  • Added regression test: "reuses the last model when model is omitted"

How did you verify your code works?

  • cd packages/opencode && bun test test/session/prompt.test.ts — 9 pass, 0 fail
  • cd packages/opencode && bun typecheck — pass
  • Live TUI smoke test: standalone session, changed model, sent prompt_async via direct HTTP — agent and model preserved
  • Live Envoy smoke test: sent envoy_send notification to same session — agent and model preserved

Notes

  • Pre-existing test failure in unknown command throws typed error with available names is unrelated — fails on dev as well
  • Envoy side verified innocent: notification delivery posts only { "parts": [...] }, no agent/model/variant fields

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@sjawhar
Copy link
Copy Markdown
Author

sjawhar commented Apr 9, 2026

CI Failure Analysis

The unit (linux) and unit (windows) failures are not caused by this PR. The error is:

TypeError: undefined is not an object (evaluating 'Provider.defaultLayer')
  at packages/opencode/src/agent/agent.ts:397:19

This occurs during @opencode-ai/sdk#build (SDK code generation), which imports opencode internals and hits a circular dependency / import ordering issue. This PR only touches packages/opencode/src/session/prompt.ts — no agent, provider, or SDK changes.

The same dev branch test workflow (run 24205574886) is still in progress, so we can confirm whether dev itself has the same failure shortly.

Re: Potential Duplicates

The bot flagged #18615 and #13456 as potential duplicates. These are related but not duplicates:

This PR fixes the server-side root cause that #18615 describes. #13456 is a separate client-side issue.

@sjawhar sjawhar force-pushed the fix/prompt-async-agent-preserve branch 2 times, most recently from 96358b9 to 3eb6178 Compare April 9, 2026 18:25
@sjawhar
Copy link
Copy Markdown
Author

sjawhar commented Apr 9, 2026

CI update after direct comparison with dev:

  • Latest completed dev test run (24206854383) has unit linux/windows green and e2e linux green / e2e windows red.
  • This means the Windows e2e failures are pre-existing on dev.
  • The current PR's Linux e2e failure is not present on dev, and the failing spec is session-model-persistence.spec.ts, which overlaps this PR's behavior area.

Because of that overlap, I am not documenting the Linux failure away as unrelated. I have re-run the failed e2e jobs to distinguish a flaky timeout from a real regression before changing code.

@sjawhar
Copy link
Copy Markdown
Author

sjawhar commented Apr 9, 2026

Additional investigation on the Linux e2e failure:

The failing spec is packages/app/e2e/session/session-model-persistence.spec.ts, but the relevant helper path does not exercise the fallback logic changed in this PR.

Spec helper evidence:

  • packages/app/e2e/fixtures.ts:399-405 calls sdk.session.prompt({ agent: state?.agent, model: state?.model, variant: state?.variant, noReply: true, ... })
  • So the test submits prompts with explicit agent + explicit model + explicit variant

PR behavior change:

  • This PR only changes packages/opencode/src/session/prompt.ts fallback behavior when agent / model are omitted (prompt_async / command injection / other no-field paths)
  • When input.model is present, createUserMessage() still uses the explicit input.model path first

Conclusion:

  • Even though the failing spec name overlaps “session model persistence,” its prompt submission path does not hit the changed fallback logic in this PR
  • Windows e2e failures are demonstrably pre-existing on dev
  • Linux e2e appears to be an unrelated/flaky timeout in a broad Playwright run rather than a semantic regression from this one-file fallback change

I don’t have permissions to rerun the failed jobs from this environment, so I can’t prove flakiness by re-run here. But the changed code path and the failing test input path do not intersect.

@sjawhar sjawhar force-pushed the fix/prompt-async-agent-preserve branch 2 times, most recently from 844b341 to 5ce8560 Compare April 15, 2026 01:14
@sjawhar sjawhar force-pushed the fix/prompt-async-agent-preserve branch from 5ce8560 to 3550fd9 Compare April 16, 2026 03:42
@sjawhar sjawhar closed this Apr 29, 2026
@sjawhar sjawhar deleted the fix/prompt-async-agent-preserve branch April 29, 2026 16:28
@sjawhar
Copy link
Copy Markdown
Author

sjawhar commented May 26, 2026

Rebased onto current dev. Branch fix/prompt-async-agent-preserve restored at the rebased head. The Linux e2e failure analyzed previously was unrelated to this PR.

@sjawhar
Copy link
Copy Markdown
Author

sjawhar commented May 26, 2026

Reopen was blocked by GitHub's force-push protection. Resubmitted as #29357.

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.

Bug: prompt_async notifications override session's active agent and model

1 participant