eugenegujing opened a new pull request, #9423:
URL: https://github.com/apache/texera/pull/9423
### What changes were proposed in this PR?
The agent only knew about operators it executed itself through
`executeOperator`. When the user clicked Run, the agent learned nothing about
that run, so it could not answer "why did this fail?" or "why is my result
empty?". This PR sends a report on the user's last run with each prompt.
How the report travels
```mermaid
sequenceDiagram
actor U as User
participant B as Browser
participant CU as Computing unit
participant A as Agent service
participant M as LLM
U->>B: Click Run
B->>CU: Execute request
CU-->>B: Initializing
Note over B: trackRunOrigin marks the run as the user's
CU-->>B: State, errors, row counts
U->>B: "Why did this fail?"
Note over B: buildLastRunReport
rect rgba(46, 160, 67, 0.15)
B->>A: Prompt + lastRun
Note over A: parseLastRun validates the report
A->>M: Context with the Last Run lines
end
M-->>A: Explanation
A-->>B: Answer
```
In the UI. A three-operator workflow whose last operator sleeps two seconds
per row. The user ran it, stopped it, and asked the agent about the run.
| `main` | This PR |
|---|---|
| <img width="1568" height="755" alt="A2-main-slow-run-2-answer"
src="https://github.com/user-attachments/assets/55000174-a8f5-4178-8556-d3d752da7625"
/> | <img width="1568" height="755" alt="A2-branchA-slow-run-2-answer"
src="https://github.com/user-attachments/assets/e131303e-dd5e-404a-a30e-be5c99fd3f4f"
/> |
| The agent reports `Score orders` as not executed, 0 rows. The canvas shows
18 rows in and 17 out. | The agent reports 12 rows in and 11 out for `Score
orders`, the numbers on the canvas. |
Size and reading order
491 lines of code in 11 files, and 2,257 lines of tests in 8 spec files. The
code reads best in the order the report travels.
| Order | Files | Lines | What they hold |
|---|---|---|---|
| 1 | `agent-types.ts` (frontend), `types/execution.ts`,
`types/ws/client.ts` | +65 | The shape of the report on both sides of the
websocket |
| 2 | `execute-workflow.service.ts` | +100 | Run-origin tracking
(`trackRunOrigin`) |
| 3 | `agent.service.ts`, `workflow-console.service.ts` | +143 | Building
the report when a prompt is sent |
| 4 | `last-run.ts`, `server.ts` | +73 | Validating the report on arrival |
| 5 | `texera-agent.ts`, `context-utils.ts` | +108 | Holding the report,
dropping it inside a turn, rendering it |
| 6 | `prompts.ts` | +2 | The two prompt rules |
The agent now sees, under each operator in the dataflow section of its
context
```
Last Run (from the user's run): Completed, input rows 5000, output rows 0
Last Run Error (from the user's run): ValueError: bad row ...
```
and after the operators a `## Last Run` block with the run state, the errors
that belong to no operator, and the errors of operators the agent's copy of the
workflow does not have.
When the frontend attaches a report
| The user's last run | Report |
|---|---|
| Completed, Failed, Killed or Terminated | attached |
| In flight and an operator has logged an error (a Python UDF that raises
pauses and leaves the run Running) | attached, state shown as `Running (not
finished)` |
| In flight with no error, or not started yet | none |
| Not requested from this browser | none |
The same rules, and what happens to the report afterwards
```mermaid
flowchart TB
ask[The user sends a prompt] --> mine{"Was the run on screen<br/>requested
from this browser?"}
mine -- no --> none[No report]
mine -- yes --> state{Run state}
state -- "Completed, Failed,<br/>Killed, Terminated" --> attach[Report
attached]
state -- "in flight, an operator<br/>has logged an error" --> attach
state -- "in flight with no error,<br/>or not started" --> none
attach --> ctx["assembleContext renders it<br/>under each operator and in
Last Run"]
ctx --> own{"Does the agent start<br/>a run of its own this turn?"}
own -- no --> keep[Report stays in the context]
own -- yes --> drop[Dropped for the rest of the turn]
```
In the report an operator's entry is its fatal error, or else its last ERROR
console message, the way the AI Fix tab reads it. Fatal errors the backend
files under `"unknown operator"` (`ErrorUtils.getOperatorFromActorIdOpt(None)`
in the run's error handler, setup failures in `WorkflowWebsocketResource`, a
lost cluster node) go to a workflow-level list. A long message keeps its first
1500 and last 500 characters. The row counts were suggested by @zyratlo in
#9377.
How the frontend knows the run is the user's
The browser shows the latest execution on the workflow whoever started it,
including the agent's own runs. `ExecuteWorkflowService` now tracks the origin.
| Event | Effect |
|---|---|
| Run request sent on a connected socket | a local run is pending |
| `Initializing` while a local run is pending | the run on screen is the
user's |
| One repeated `Initializing` for that run | ignored (the backend sends it
twice) |
| Any other `Initializing`, or `Uninitialized` (socket reopened) | not the
user's |
| Error event with no run in flight, unless it appends to the user's own
failed run | not the user's |
| Error event right after the ack that carries an older run's errors | not
the user's |
| A live agent step that called `executeOperator`, `addOperator` or
`modifyOperator` | not the user's |
When the frontend cannot tell, it omits the report and the agent behaves as
it does today. The cases it gets wrong are under Known limits.
Agent service
- `last-run.ts` validates the report with a zod schema. A malformed report
is logged and treated as absent, and the prompt is still processed.
- `assembleContext` renders it, marks an operator with a run error as failed
and leaves out the agent's older result for that operator. A report is only
rendered while it is newer than every result the agent has. The frontend sends
none after an agent run, and `TexeraAgent` drops it for the rest of a turn once
the agent calls `executeOperator`, or an `addOperator` or `modifyOperator` the
tool accepted, because those start a run of the agent's own.
- Two prompt rules are added before "Refine or fix operator in place". One
covers a run error (explain it, name the operator, ask before fixing, say so
when the error belongs to an operator the agent cannot see, and do not run, add
or modify operators on an unfinished run until the user agrees). The other
tells the model to read the row counts when a result is empty. Existing rules
are unchanged, and `serializeTask` is untouched because #9244 edits it.
Why from the frontend. Fatal errors live in the computing unit's memory and
are not persisted, and no endpoint reads console messages back. Row counts are
served by `GET /api/executions/{wid}/stats/{eid}`, but the browser already
holds them for the same run next to the errors. So this needs no backend
change, and the source can move to REST later without changing the agent side.
Known limits
- After its first turn the agent works on its own copy of the workflow and
does not see operators or edits the user made by hand since. Errors for
operators it does not have are listed by id and message in the `Last Run`
block, and row counts for them are not shown.
- The report describes the last run. If the user edits an operator and asks
before running again, the error may no longer match the operator. The prompt
rule tells the model to say so.
- An operator's last ERROR console message stays in the report after the
user retries or skips the failing tuple.
- `getErrorMessages()` is empty unless the state is Failed, so every other
state carries console errors and row counts only.
- A page reload or a websocket reconnect drops the report until the user
runs again.
- Once the report is dropped inside a turn, nothing in the context repeats
the user's error. If the model edits in its first step without explaining, its
final answer cannot quote it.
- Execution events carry no execution id or requester, so three cases are
attributed wrongly. Another session's run that fails during setup while the
user's run is in flight is taken as the user's. A foreign run that starts
between the user's ack and its repeated `Initializing` is taken as that repeat.
Another session's refused request is appended by the backend to the user's
failed run as one extra error line.
- Asking before applying a fix is a prompt rule, not a guarantee. With
`claude-haiku-4.5` the model asked first for a UDF runtime error and for an
empty result, and fixed a UDF syntax error without asking. It may also act on a
reported error during a later, unrelated prompt.
- The error text goes to the LLM endpoint configured for the agent service,
as the AI Fix tab from #8637 already does.
Not in this PR. A run in progress that has logged no error, since partial
counts alone mislead. And a run the agent starts (`executeOperator`, or an
accepted `addOperator` or `modifyOperator`) shutting down a run the user has in
flight, which is existing behavior and deserves its own issue.
### Any related issues, documentation, discussions?
Closes #9420. Proposed in #9377 (change 1), following up on #9246. Lands
after #9244, which edits the same context serialization file.
Note for reviewers: this branch and the docs-tools branch from #9377 both
add to `prompts.ts`, `texera-agent.ts` and `context-utils.ts` (and their specs)
in neighbouring places, so the two do not apply on top of each other as they
are. Whichever lands second will be rebased.
### How was this PR tested?
- agent-service, `cd agent-service && bun test`: 403 pass, 0 fail across 20
files (326 before this PR), plus `bun run typecheck` and `prettier --check`.
Covers the schema with its limits written as literals against what the frontend
sends, the rendering for known, unknown and no operators and for final and
in-flight states, the in-turn drop for each tool and for rejected calls, the
prompt wording, and the malformed-report warning.
- frontend, `cd frontend && NODE_OPTIONS="--no-experimental-webstorage" npx
ng test --watch=false --include <spec>`: 390 passed across
`agent.service.spec.ts` (134), `execute-workflow.service.spec.ts` (101),
`workflow-console.service.spec.ts` (6) and `workflow-editor.component.spec.ts`
(149), plus `prettier --check` and `eslint`. Covers every attach rule and cap,
and each run-origin rule with the ordering that would break it. One test drives
the real execution, console and status services through the websocket event
stream to show that a previous run's error and counts do not reach the next run.
- Mutation checks on the new logic. Every mutant was killed.
On a local stack running this branch, with three Text Input to Python UDF
workflows, the prompt frames read from the browser, and `claude-haiku-4.5`
| Case | Result |
|---|---|
| No run yet, or a run in flight with no error | no report, then a Completed
report once the run ended |
| UDF yields nothing, run Completed | Completed with both operators' row
counts |
| UDF has a syntax error, run Failed | Failed with the error at workflow
level (it arrives under `"unknown operator"`) |
| UDF raises, user asks without killing | Running with one operator error
(934 characters ending in the `ValueError` line) and the row counts (Text Input
Completed 0 to 1, UDF Paused 1 to 0). The context showed `State: Running (not
finished)`. The model explained the error with no tool call and, asked
directly, said the run had not finished |
| Pause, then Resume on that run | Paused and Running reports |
| Kill | Killed report with the traceback and both row counts |
| Page reloaded after a finished run | no report until the next run |
| Agent ran an operator with `executeOperator` | no report on the next
prompt, back after the user's next run |
| Agent edits the failing operator inside a turn (UDF raises in flight, and
the syntax-error run) | the first step's context had the report, the step after
`modifyOperator` had none, and the model stopped after one edit. Before the
in-turn drop it edited the same operator three times in one turn |
### Was this PR authored or co-authored using generative AI tooling?
Yes. Generated-by: Claude Code (claude-fable-5-2). Co-authored with Claude;
the author reviewed the change before submission.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]