Skip to content

fix(agent-runtime): stop run after local prompt config errors - #1199

Open
mturac wants to merge 1 commit into
CodebuffAI:mainfrom
mturac:fix/issue-1155-prompt-error
Open

fix(agent-runtime): stop run after local prompt config errors#1199
mturac wants to merge 1 commit into
CodebuffAI:mainfrom
mturac:fix/issue-1155-prompt-error

Conversation

@mturac

@mturac mturac commented Sep 2, 2026

Copy link
Copy Markdown

When assembleLocalAgentTemplates returns validation errors, callMainPrompt now returns early with an error result and a prompt-response action, instead of continuing into mainPrompt. Before this the orphaned run kept executing after the prompt-error banner -- burning credits, executing tools, and mutating shared state while the UI already showed the error.

Also adds a regression test verifying early return on validation errors.

Fixes the "prompt-error orphans the run" sub-bug from #1155.

Tested with a new call-main-prompt.test.ts that verifies prompt-error and prompt-response are sent, no response-chunk events fire, and the result is an error. Existing main-prompt.test.ts tests still pass.

When assembleLocalAgentTemplates returns validation errors,
callMainPrompt now returns early with an error result instead of
continuing into mainPrompt. Previously the orphaned run kept
executing (burning credits, running tools) after the prompt-error
banner was already shown.

Closes CodebuffAI#1155 (sub-bug 1)
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-02T08:16:47.108700Z 3d14627 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3d14627eb4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +188 to +190
sendAction({
action: {
type: 'prompt-response',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Wait for the async error event before resolving the run

When local validation fails in CodebuffClient.run and the supported handleEvent callback returns a promise, the preceding prompt-error starts handlePromptResponse, which awaits that callback (sdk/src/run.ts lines 1243-1245), but sendAction does not await the handler. This immediately following prompt-response enters the synchronous response branch and resolves the run first (lines 1256-1287), so await client.run() can return while its error event is still being processed. Emit only one terminal action or serialize these dispatches so asynchronous error handling completes before the run resolves.

Useful? React with 👍 / 👎.

Comment on lines +43 to +48
it('returns early without calling mainPrompt when agent config validation fails', async () => {
const sentActions: Array<{ type: string }> = []
const sendAction = ({ action }: { action: { type: string } }) => {
sentActions.push(action)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Exercise validation with an invalid config fixture

This test replaces assembleLocalAgentTemplates through an ES-module spy even though the validation path can be exercised directly by placing an invalid template in mockFileContext.agentTemplates. That unnecessarily couples the regression test to module-binding behavior and conflicts with the repository convention to prefer dependency injection over module mocking; use a real invalid fixture here, or inject the assembler if isolation is required.

AGENTS.md reference: AGENTS.md:L28-L28

Useful? React with 👍 / 👎.

Comment on lines +183 to +185
const errorResult = {
sessionState: action.sessionState,
output: { type: 'error' as const, message: errorMessage },

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the rejected prompt in the returned session state

When validation fails before loopAgentSteps runs, the user's current prompt has not yet been inserted into messageHistory, but this result simply returns the unchanged incoming session. Consequently, an SDK caller that fixes the config and passes this failed result as previousRun loses the original request—for example, a follow-up such as “retry” has no request to retry. The SDK's thrown-error path explicitly adds the prompt when the runtime made no progress (sdk/src/run.ts lines 509-527); this early-return path should preserve it equivalently before sending the terminal state.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3d14627eb4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +188 to +190
sendAction({
action: {
type: 'prompt-response',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Wait for the async error event before resolving the run

When local validation fails in CodebuffClient.run and the supported handleEvent callback returns a promise, the preceding prompt-error starts handlePromptResponse, which awaits that callback (sdk/src/run.ts lines 1243-1245), but sendAction does not await the handler. This immediately following prompt-response enters the synchronous response branch and resolves the run first (lines 1256-1287), so await client.run() can return while its error event is still being processed. Emit only one terminal action or serialize these dispatches so asynchronous error handling completes before the run resolves.

Useful? React with 👍 / 👎.

Comment on lines +43 to +48
it('returns early without calling mainPrompt when agent config validation fails', async () => {
const sentActions: Array<{ type: string }> = []
const sendAction = ({ action }: { action: { type: string } }) => {
sentActions.push(action)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Exercise validation with an invalid config fixture

This test replaces assembleLocalAgentTemplates through an ES-module spy even though the validation path can be exercised directly by placing an invalid template in mockFileContext.agentTemplates. That unnecessarily couples the regression test to module-binding behavior and conflicts with the repository convention to prefer dependency injection over module mocking; use a real invalid fixture here, or inject the assembler if isolation is required.

AGENTS.md reference: AGENTS.md:L28-L28

Useful? React with 👍 / 👎.

Comment on lines +183 to +185
const errorResult = {
sessionState: action.sessionState,
output: { type: 'error' as const, message: errorMessage },

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the rejected prompt in the returned session state

When validation fails before loopAgentSteps runs, the user's current prompt has not yet been inserted into messageHistory, but this result simply returns the unchanged incoming session. Consequently, an SDK caller that fixes the config and passes this failed result as previousRun loses the original request—for example, a follow-up such as “retry” has no request to retry. The SDK's thrown-error path explicitly adds the prompt when the runtime made no progress (sdk/src/run.ts lines 509-527); this early-return path should preserve it equivalently before sending the terminal state.

Useful? React with 👍 / 👎.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch — continuing into mainPrompt after already having sent prompt-error is exactly the kind of orphaned-run bug that burns credits and mutates state after the UI has moved on. The fix in main-prompt.ts (lines ~171-202) is minimal and in the right place, and the new test in call-main-prompt.test.ts actually asserts the previously-missing behavior (no response-chunk, error result returned, prompt-response sent).

A few things worth checking before this is ported:

  1. The PR is currently listed as Mergeable: unstable. Please check whether this is a rebase conflict or an actual CI/type-check failure — the prompt-response action literal you construct (toolCalls: [], toolResults: [], output) needs to satisfy whatever the real discriminated-union type for that action requires. The test bypasses this with as any on the whole params object, so a type mismatch on the action itself wouldn't be caught there.

  2. Worth double-checking that errorResult.sessionState (passed through unchanged) is what downstream consumers of prompt-response expect on an error path — e.g., do other error-producing code paths bump a run counter, credits, or usage fields that this shortcut skips?

  3. Minor: the test only covers the validation-error branch; it'd strengthen confidence to also assert mainPrompt (or whatever the next call is) was not invoked, via a spy, rather than only checking for absence of response-chunk.

Overall the diagnosis and shape of the fix look right — just want the type-correctness and unstable-mergeable status resolved before this gets ported.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants