Skip to content

fix(agent-service): reject prompts sent while a run is in flight - #8753

Merged
bobbai00 merged 4 commits into
apache:mainfrom
CaroFernando:feat/warehouse-run-race
Nov 24, 2026
Merged

bobbai00 merged 4 commits into
apache:mainfrom
CaroFernando:feat/warehouse-run-race

Conversation

@CaroFernando

@CaroFernando CaroFernando commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this PR?

A prompt arriving while an agent run is already in flight could
re-point that run's warehouse mid-execution, and race on shared agent
state.

Before: prompt A starts run (GENERATING) -> prompt B arrives mid-run
-> setDelegateWarehouse() applies B's warehouse unconditionally
-> A's still-running execution tools read the new warehouse
After: prompt A starts run -> prompt B arrives mid-run -> rejected
with a busy error, A's warehouse/state untouched

Root cause, two parts:

  1. server.ts's WsClientPromptCommand handler called
    agent.setDelegateWarehouse(...) unconditionally on every prompt,
    with no check for whether a run was already in progress.
  2. TexeraAgent.sendMessage didn't flip state to GENERATING until
    after its first await (refreshWorkflowFromBackend()). Since two
    message handler invocations can interleave across that await, a
    second prompt could see stale state and proceed concurrently,
    racing on head, abortController, and currentMessageId as well.

Fix:

agent-service

  • sendMessage now flips state (and resets abortController)
    synchronously before any await, and throws if already GENERATING.
    refreshWorkflowFromBackend() runs inside the try, so its finally
    always resets state.
  • The WebSocket handler checks agent.getState() and rejects a busy
    agent with a WsServerErrorEvent before it ever calls
    setDelegateWarehouse, so a rejected prompt's delegate config is left
    untouched.
  • stop() is now a no-op unless the agent is GENERATING. Before, an
    idle stop set STOPPING with nothing to reset it, so the new busy
    guard rejected every later prompt.
  • Both stop paths report the resulting state instead of assuming a stop
    happened: the WebSocket stop handler broadcasts agent.getState(),
    and POST /agents/:id/stop returns { status: agent.getState() }
    (STOPPING mid-run, AVAILABLE when idle) instead of a fixed
    "stopping". Nothing in the repo reads that response body.

frontend

  • The operator-feedback panel (agent-interaction.component.ts) only
    sends when the selected agent is AVAILABLE, matching the chat
    panel. canSend() disables the Send button mid-run, and
    sendFeedbackToAgent() checks too, since Ctrl+Enter bypasses the
    disabled button. A busy agent gets a warning toast, no success toast,
    and the textarea keeps its text.
  • New AgentService.getAgentStateSnapshot() reads an agent's latest
    state synchronously without creating tracking for an unknown id.
    getAgentState() now wraps it, so the lookup lives in one place.

UI change, operator-feedback panel during an agent run:

image image

Before: Send is enabled mid-run. Sending shows "Message sent to agent
successfully", clears the input, and the feedback lands as a second
prompt while the first run is still thinking. After: Send is disabled
mid-run. Ctrl+Enter shows only "Agent is busy. Please wait for the
current run to finish.", nothing is sent, and the text is kept.

Any related issues, documentation, discussions?

Closes #8711

How was this PR tested?

Regression tests, each verified red without its fix and green with it:

  • agent-service/src/agent/texera-agent.spec.ts: gates a mock LLM call
    open, asserts a concurrent sendMessage() call throws "busy" instead
    of interleaving. With no sleep before the second call, it also fails
    if the refresh await moves back above the state flip.
  • agent-service/src/agent/texera-agent.spec.ts: an idle stop() is a
    no-op, a stop() while generating still moves to STOPPING, and
    sendMessage succeeds after an idle stop.
  • agent-service/src/server.ws.spec.ts: the busy scenario over the real
    WebSocket protocol; also pins an initial warehouseId and asserts a
    busy-rejected prompt carrying a different warehouseId never reaches
    setDelegateWarehouse. An idle WS stop reports AVAILABLE.
  • agent-service/src/server.spec.ts: POST /agents/:id/stop returns
    STOPPING mid-run and AVAILABLE when idle.
  • agent-interaction.component.spec.ts: Send is disabled and
    Ctrl+Enter is rejected while the agent is busy, with the input kept.

Run with:
cd agent-service && bun run format:check && bun run typecheck && bun test
348 tests pass (0 fail).
cd frontend && yarn format:ci && yarn lint && yarn test --watch=false
254 test files, 7295 tests pass (1 skipped, 0 fail).

Additionally verified manually against the real running stack (real
signed JWT, real WebSocket, and an actual LLM API key generating a real
response) rather than a stubbed model: sent two prompts back-to-back
with different warehouseIds, confirmed the second is rejected with
"Agent is busy processing another message" while the first's run — and
its warehouseId — is unaffected. Also checked that POST /stop on an
idle agent followed by a prompt runs normally, and that in the feedback
panel Send is disabled mid-run and Ctrl+Enter shows only the busy
warning with the input kept.

UI click path for the screenshots (Chromium, 1280x800 viewport, local-dev
stack, qwen/qwen4-max agent): open a workflow with a CSV File Scan
operator, create an agent in the AI Agents panel, click the operator's
chat button to open the feedback popover and select the agent, send a
multi-step prompt in the agent chat, then while it shows "Thinking..."
type feedback in the popover and click Send (before) or press Ctrl+Enter
(after).

Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude

@Yicong-Huang Yicong-Huang added release/v1.3 back porting to release/v1.3 release/v1.2 back porting to release/v1.2 labels Sep 29, 2026
@github-actions
github-actions Bot requested review from mengw15 and xuang7 September 29, 2026 05:44
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Automated Reviewer Suggestions

Based on the git blame history of the changed files, we recommend the following reviewers:

  • Contributors with relevant context: @bobbai00, @Ma77Ball
    You can notify them by mentioning @bobbai00, @Ma77Ball in a comment.

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Backport auto-label report

This fix: PR was checked against each actively-supported release branch. A release/* label nominates a backport target; the branch's release manager approving this PR is what sends the fix there. The required Backport Approvals check stays red until every label below is approved, so each manager either approves or removes their own label — which is why the labels left on a merged PR are exactly the branches it reached.

Release branch Analysis
🚫 release/v1.3 Label was removed earlier (opt-out); not re-added. Re-add it by hand if this fix should be backported here after all — the branch's release manager then has to approve before this PR can merge.
🚫 release/v1.2 Label was removed earlier (opt-out); not re-added. Re-add it by hand if this fix should be backported here after all — the branch's release manager then has to approve before this PR can merge.

Auto-label run.

@codecov-commenter

codecov-commenter commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.83333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.68%. Comparing base (cb1d619) to head (66e6a67).

Files with missing lines Patch % Lines
...t/agent-interaction/agent-interaction.component.ts 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##               main    #8753   +/-   ##
=========================================
  Coverage     91.68%   91.68%           
  Complexity     5132     5132           
=========================================
  Files          1305     1305           
  Lines         58229    58242   +13     
  Branches       7600     7602    +2     
=========================================
+ Hits          53385    53398   +13     
  Misses         2937     2937           
  Partials       1907     1907           
Flag Coverage Δ *Carryforward flag
access-control-service 77.44% <ø> (ø) Carriedforward from cb1d619
agent-service 99.56% <100.00%> (+<0.01%) ⬆️
amber 85.77% <ø> (ø) Carriedforward from cb1d619
computing-unit-managing-service 63.83% <ø> (ø) Carriedforward from cb1d619
config-service 87.50% <ø> (ø) Carriedforward from cb1d619
file-service 82.16% <ø> (ø) Carriedforward from cb1d619
frontend 96.43% <88.88%> (+<0.01%) ⬆️
notebook-migration-service 83.77% <ø> (ø) Carriedforward from cb1d619
pyamber 98.38% <ø> (ø) Carriedforward from cb1d619
result-service 86.48% <ø> (ø) Carriedforward from cb1d619
workflow-compiling-service 80.90% <ø> (ø) Carriedforward from cb1d619

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@chenlica
chenlica requested review from bobbai00 and removed request for xuang7 September 29, 2026 06:10
@chenlica

Copy link
Copy Markdown
Contributor

@bobbai00 Please take a look.

@bobbai00 bobbai00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔴 3 must-fix · 2 advisory · 0 polish: the guard is correct and race-free, but it makes an idle stop permanent and rejects the feedback path #8711 names.

Design & architecture (1)

  • server.ts:501: feedback panel still sends mid-run, then shows success and clears its input (must-fix, see inline)

Correctness (2)

  • texera-agent.ts:505: an idle stop() now wedges the agent in STOPPING (must-fix, see inline)
  • texera-agent.ts:516: refresh await is outside the try that resets state (advisory, see inline)

Simplifications (1)

  • texera-agent.spec.ts:837: the sleep lets the test pass with the old ordering (advisory, see inline)

Conventions (1)

  • server.ws.spec.ts:318: fails format:check, failing build / agent-service (must-fix, see inline)
Verification trace

Traced the check-then-act: nothing between server.ts:500 and server.ts:520 awaits, and sendMessage flips to GENERATING before its first await (texera-agent.ts:505-509), so two prompts cannot both pass. Locally at the head, bun test is 312 pass. A scratch test confirmed the idle-stop wedge. Moving the refresh await back above the flip keeps the new unit test green with the sleep and red without it.

Comment thread agent-service/src/server.ts Resolved
Comment thread agent-service/src/agent/texera-agent.ts Resolved
Comment thread agent-service/src/agent/texera-agent.ts Resolved
Comment thread agent-service/src/server.ws.spec.ts Resolved

@bobbai00 bobbai00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Left some comments

CaroFernando added a commit to CaroFernando/texera that referenced this pull request Oct 22, 2026
Address review on apache#8753:
- stop() is a no-op unless a run is GENERATING; an idle stop left the
  agent in STOPPING with no finally to reset it, so every later prompt
  was rejected as busy. The WS stop handler now broadcasts the agent's
  actual state instead of always STOPPING.
- Move refreshWorkflowFromBackend inside the try so the finally always
  resets state.
- Gate the operator feedback panel on the agent being AVAILABLE (like
  the chat panel); a mid-run Ctrl+Enter now warns and keeps the input
  instead of showing a false success toast and clearing it.
- Drop the sleep from the busy unit test so it pins the ordering fix.
- Prettier fix in server.ws.spec.ts.
@github-actions github-actions Bot added the frontend Changes related to the frontend GUI label Oct 22, 2026
@CaroFernando
CaroFernando requested a review from bobbai00 October 23, 2026 04:27

@bobbai00 bobbai00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 5 resolved · 0 open · 3 new (3 new = 3 newly introduced · 0 late catches)

All five round-1 findings are fixed and pinned by tests. Three advisories remain.

Correctness (1)

  • server.ts:486: REST /stop still answers "stopping" when the new guard makes it a no-op (advisory, see inline)

Simplifications (1)

  • agent.service.ts:966: repeats getAgentState's lookup (advisory, see inline)

Conventions (1)

  • agent-interaction.component.ts:189: PR description predates the round-2 changes (advisory, see inline)
Verification trace

Checked each round-1 fix at b503a23. Removing the stop() guard fails the three new idle-stop tests, and moving the refresh await back above the state flip fails the #8711 concurrency test. Locally bun test is 315 pass. The red build / frontend (ubuntu-latest) is the runner's playwright install step ("does not support chromium on ubuntu26.04-x64"), after all 6652 tests passed.

Comment thread agent-service/src/server.ts Resolved
@bobbai00

Copy link
Copy Markdown
Contributor

@CaroFernando can you check new comments and resolve them soon ?

@xuang7 xuang7 removed the release/v1.2 back porting to release/v1.2 label Nov 16, 2026
@xuang7

xuang7 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Removing release/v1.2. The warehouse re-pointing that #8711 describes depends on setDelegateWarehouse and the warehouseId carried by WsClientPromptCommand, neither of which exists on release/v1.2, and the patch does not apply to that branch.

@CaroFernando
CaroFernando force-pushed the feat/warehouse-run-race branch from b503a23 to 37e4a3a Compare November 16, 2026 19:42
CaroFernando added a commit to CaroFernando/texera that referenced this pull request Nov 16, 2026
Address review on apache#8753:
- stop() is a no-op unless a run is GENERATING; an idle stop left the
  agent in STOPPING with no finally to reset it, so every later prompt
  was rejected as busy. The WS stop handler now broadcasts the agent's
  actual state instead of always STOPPING.
- Move refreshWorkflowFromBackend inside the try so the finally always
  resets state.
- Gate the operator feedback panel on the agent being AVAILABLE (like
  the chat panel); a mid-run Ctrl+Enter now warns and keeps the input
  instead of showing a false success toast and clearing it.
- Drop the sleep from the busy unit test so it pins the ordering fix.
- Prettier fix in server.ws.spec.ts.
CaroFernando added a commit to CaroFernando/texera that referenced this pull request Nov 16, 2026
Address round-2 review on apache#8753:
- POST /agents/:id/stop returned "stopping" even when the idle-stop
  no-op left the agent AVAILABLE. Return agent.getState(), matching
  the WS stop handler, and split the spec into mid-run and idle cases.
- getAgentState now wraps getAgentStateSnapshot so the lookup and its
  UNAVAILABLE fallback live in one place.
@CaroFernando

Copy link
Copy Markdown
Contributor Author

@bobbai00 Addressed the round-2 comments in 37e4a3a and rebased onto main. The description now has before/after screenshots of the feedback panel. Ready for another look.

@bobbai00 bobbai00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 3 resolved · 0 open · 0 new (0 new = 0 newly introduced · 0 late catches)

All three round-2 advisories are fixed, and I found nothing new after the rebase onto main. LGTM.

Verification trace

Checked at 37e4a3a. POST /agents/:id/stop now returns agent.getState() (server.ts:307), and reverting it to the old literal fails both new server.spec.ts stop tests. getAgentState delegates to getAgentStateSnapshot with the same result for both callers. After the rebase, sendMessage still has one caller (server.ts:513) and texera-agent.ts still writes state in exactly three places, so the busy guard and the idle-stop no-op hold. Locally bun test is 348 pass, and CI is green.

WsClientPromptCommand re-pointed the agent's warehouse via
setDelegateWarehouse unconditionally, and TexeraAgent.sendMessage did
not flip state to GENERATING until after an await, letting a second
prompt slip through the same race window. A prompt/feedback message
arriving mid-run could therefore re-point an in-flight run's warehouse
mid-execution, and interleave with head/abortController/currentMessageId.

sendMessage now flips state synchronously before its first await and
rejects a concurrent call; the WebSocket handler rejects a busy agent
before touching delegate config.

Closes apache#8711
Address review on apache#8753:
- stop() is a no-op unless a run is GENERATING; an idle stop left the
  agent in STOPPING with no finally to reset it, so every later prompt
  was rejected as busy. The WS stop handler now broadcasts the agent's
  actual state instead of always STOPPING.
- Move refreshWorkflowFromBackend inside the try so the finally always
  resets state.
- Gate the operator feedback panel on the agent being AVAILABLE (like
  the chat panel); a mid-run Ctrl+Enter now warns and keeps the input
  instead of showing a false success toast and clearing it.
- Drop the sleep from the busy unit test so it pins the ordering fix.
- Prettier fix in server.ws.spec.ts.
…test

A bare "AVAILABLE" literal fails tsc --noEmit (the Typecheck CI step).
Address round-2 review on apache#8753:
- POST /agents/:id/stop returned "stopping" even when the idle-stop
  no-op left the agent AVAILABLE. Return agent.getState(), matching
  the WS stop handler, and split the spec into mid-run and idle cases.
- getAgentState now wraps getAgentStateSnapshot so the lookup and its
  UNAVAILABLE fallback live in one place.
@CaroFernando
CaroFernando force-pushed the feat/warehouse-run-race branch from 37e4a3a to 66e6a67 Compare November 24, 2026 21:07
@bobbai00
bobbai00 added this pull request to the merge queue Nov 24, 2026
Merged via the queue into apache:main with commit de3f021 Nov 24, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-service feature frontend Changes related to the frontend GUI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BYO-S3] An agent's warehouse can be re-pointed while one of its runs is still in flight

7 participants