The GitHub Actions job "Required Checks" on 
texera.git/gh-readonly-queue/main/pr-8753-41e25f41076398617bc75cadb2be236a2e8a2a7c
 has succeeded.
Run started by GitHub user bobbai00 (triggered by bobbai00).

Head commit for run:
de3f021deceb3e25529e36441f53f2cf4007b303 / Luis Fernando Caro Reyna 
<[email protected]>
fix(agent-service): reject prompts sent while a run is in flight (#8753)

### 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:

<img width="1644" height="610" alt="image"
src="https://github.com/user-attachments/assets/3206edf7-a9f1-49e8-81bc-522bf7d41b6c";
/>

<img width="1644" height="610" alt="image"
src="https://github.com/user-attachments/assets/2164743a-5fbb-4433-b189-7f0d63e3e9c9";
/>


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 `warehouseId`s, 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

Report URL: https://github.com/apache/texera/actions/runs/45501022337

With regards,
GitHub Actions via GitBox

Reply via email to