diff --git a/changelog.d/1546.fixed.md b/changelog.d/1546.fixed.md new file mode 100644 index 000000000..31ff2d84e --- /dev/null +++ b/changelog.d/1546.fixed.md @@ -0,0 +1 @@ +A step that only mentioned `needs-human` or `already-claimed` in its reply ended its own run, so quoting an issue title, writing `No needs-human label on this issue`, or reporting the label as absent stopped the pipeline. A run now ends early only on a structured signal: a declared output equal to the marker, or a reply line that is exactly `EARLY-END needs-human` / `EARLY-END already-claimed`. ([#1546](https://github.com/ThinkFlowLab/FlyBot/issues/1546)) diff --git a/docs/subsystems/workflow.md b/docs/subsystems/workflow.md index e4effd2a6..bf470176f 100644 --- a/docs/subsystems/workflow.md +++ b/docs/subsystems/workflow.md @@ -48,7 +48,7 @@ Everything in `plugins/workflow/`: the graph rules, the run store, the matcher, 1. Friends' Bots are not nodes. A `remote` Bot is refused by create, define and the matcher. 2. The canvas and the run share one graph: nodes, edges and positions. A drag commits `workflow/graph`, but only while **Edit** is on — a factory preset is copied first (#1378). Status highlighting reads that same graph. Replacing edges does not recompute who is already running. The Settings canvas paints that graph on the same 960px design plane, scaled to the width the settings page gives it, so the canvas is not a second scroll container inside the page's own scroll; its pointer deltas are divided by the scale, and its wires keep a stronger stroke than a plain divider so an edge can be followed at that size (#1483). -3. **A live run advances only when its ephemeral Bot replies, following the graph's edges in order.** That reply advances the run and may appear on the workflow stage log; it is not delivered into a persistent sidebar Bot conversation (for example Chief). The `workflow-step` send middleware denies those replies after `stepResult` runs. Nodes start only when their forward predecessors finish and edges reach them. **Early termination (#1537, #1538):** When a node completes **or fails** with `needs-human`, `already-claimed`, `tip-unchanged`, or `open-pr` detected in its outputs or reply text (see `checkEarlyEnd` in Key files), the run ends immediately with status `done`, sets `run.earlyEnd` to the condition name, and does not advance to successors, take a success-path `loop`, or burn `onFail` recovery attempts. Preferred signals are a declared OUTPUT equal to the marker, or a whole reply line `EARLY-END `; the engine also honors common phrases (`tip unchanged` / `unchanged tip`, `has open PR` / `already has open PR` / `found open PR`, plus the existing already-claimed heuristics) so a Bot that forgets the structured marker cannot soft-lock Claim↔Verify on the same tip. Factory prompt guidance lives in `EARLY_END_GUIDANCE` (`plugins/workflow/src/template.js`) for Settings graphs that wire Triage / Claim / Verify: Verify must escalate once on an unchanged tip, not re-run the full suite. There is no scripted or seeded run path: `workflow/state` exposes only `live === true` runs — the running ones, plus the last run when it is still `live` and ended `failed` — a failed sibling stays on the stage beside whatever is running now, with `run.failure` (`{ code, at, nodeName?, text? }`) naming what ended it, so the room does not have to open Runs to learn that a run failed (#1493, #1494). The failed sibling is left out when a running run carries the same task id: that is a retry, and the live run keeps the row (#1507). History entries carry the run `id` and the same `failure`, so a Runs row shows when the run ended and why, and a row for a run already on the stage is not listed twice. A run does not create a schedule. `scheduleLabel` is display text. When the host can create Bots, starting a live run (`assign`, `run_workflow`, `ingest`) spawns one ephemeral Bot per node from that node's prompt, role, model and tools, sends it the step from the Main Bot, and removes every instance when the run finishes, fails for good, or is reset. A `failOnce` retry keeps them. They are hidden and left out of the sidebar. Loading the plugin does not spawn Bots for the default workflows. Plugin unload (`dispose`) and the load-time `reap` both end a live run still marked `running`: in-flight nodes are failed, the run is archived as `failed`, and any ephemeral Bot ids are released. A run already written as `running` with no ephemeral map (an older unload) is healed the same way on the next load (#1414). Loading is also the one moment a run the host left behind is noticed when its ephemeral map is still present: `reap` releases those instances and records the run as `failed`. Nothing resumes it. A run that already ended keeps its own status (#1388). `workflow/reset` stopping one live run ends it the same way before its Bots are released: nodes still `running` become `failed`, so the strip cannot keep painting a run that is over (#1424). A graph edit that drops a node that is still running ends that run the way Stop does: the node is failed, the run is archived `failed`, its node Bots are released, and the queue drains (#1549). Every wait is bounded (#1530). A dispatch that does not land is not marked handed: the chat names the code and the attempt, and it is retried after a short backoff at most twice, never twice inside one hand-off, so a 429 is not hammered. When it still does not land, the step and the run fail with the reason written into the chat. Each running live step also holds a deadline — its workflow's `stepTimeoutMinutes`, `DEFAULT_STEP_TIMEOUT_MINUTES` (45 minutes) unless the record sets one, an integer from 1 to 120 given at create or through `workflow/step-timeout` (armed even when the host cannot spawn ephemeral Bots; #1530, #1580); when it expires the node and the run fail as `failed`, the run's ephemeral Bots are released, the chat says the step timed out, and `warn` names it once. The deadline is cleared as soon as the step moves, and by Pause, Stop, Reset, delete, `reap` and `dispose`; a paused run holds none, and Resume arms it again from that moment. A timer never outlives the run that started it. A reply that matches no running step — its run ended, or the node already moved — is reported through `warn` once per Bot and run instead of being dropped silently. A reply that says `could not answer your message` or `model call failed` fails that node and does not start its successor. A 429 or rate-limit mention in any other reply does not. Stop and Reset (`workflow/reset`) follow the same order as `dispose`: they release the run's Bots before the record forgets their ids, and an id whose release failed stays named on the still-live run, so the next Stop, Reset, dispose or load-time `reap` retries it (#1476). A run left that way is marked `paused` and `releasePending` and shows as Paused: it hands off no further node, `resume` is refused with an error, and Stop again is the retry. A whole-workflow Reset that could not release every Bot freezes every run it did not reset, because each of them has lost the ids whose release did go. The markers go away when the run ends: Stop again, `dispose`, `reap`, or its own completion. +3. **A live run advances only when its ephemeral Bot replies, following the graph's edges in order.** That reply advances the run and may appear on the workflow stage log; it is not delivered into a persistent sidebar Bot conversation (for example Chief). The `workflow-step` send middleware denies those replies after `stepResult` runs. Nodes start only when their forward predecessors finish and edges reach them. **Early termination (#1537, #1538, #1546, #1570, #1571):** When a node completes **or fails**, the run ends immediately with status `done`, sets `run.earlyEnd` to the condition name, and does not advance to successors, take a success-path `loop`, or burn `onFail` recovery attempts. The two markers that report the task itself end it only on a structured signal (#1546): `needs-human` and `already-claimed` must be a declared OUTPUT whose value **equals** the marker, or a whole reply line that is exactly `EARLY-END ` in any case. A step that quotes an issue title, negates the label or reports it absent keeps running. `tip-unchanged` (#1570) and `open-pr` (#1571) accept that same structured signal and also honor their prose fallbacks (`tip unchanged` / `unchanged tip`, `open PR already` / `already has an open PR` / `has an open PR` / `found an open PR`), so a Bot that forgets the structured marker cannot soft-lock Claim↔Verify on the same tip. See `checkEarlyEnd` in Key files. Factory prompt guidance lives in `EARLY_END_GUIDANCE` (`plugins/workflow/src/template.js`) for Settings graphs that wire Triage / Claim / Verify: Verify must escalate once on an unchanged tip, not re-run the full suite. There is no scripted or seeded run path: `workflow/state` exposes only `live === true` runs — the running ones, plus the last run when it is still `live` and ended `failed` — a failed sibling stays on the stage beside whatever is running now, with `run.failure` (`{ code, at, nodeName?, text? }`) naming what ended it, so the room does not have to open Runs to learn that a run failed (#1493, #1494). The failed sibling is left out when a running run carries the same task id: that is a retry, and the live run keeps the row (#1507). History entries carry the run `id` and the same `failure`, so a Runs row shows when the run ended and why, and a row for a run already on the stage is not listed twice. A run does not create a schedule. `scheduleLabel` is display text. When the host can create Bots, starting a live run (`assign`, `run_workflow`, `ingest`) spawns one ephemeral Bot per node from that node's prompt, role, model and tools, sends it the step from the Main Bot, and removes every instance when the run finishes, fails for good, or is reset. A `failOnce` retry keeps them. They are hidden and left out of the sidebar. Loading the plugin does not spawn Bots for the default workflows. Plugin unload (`dispose`) and the load-time `reap` both end a live run still marked `running`: in-flight nodes are failed, the run is archived as `failed`, and any ephemeral Bot ids are released. A run already written as `running` with no ephemeral map (an older unload) is healed the same way on the next load (#1414). Loading is also the one moment a run the host left behind is noticed when its ephemeral map is still present: `reap` releases those instances and records the run as `failed`. Nothing resumes it. A run that already ended keeps its own status (#1388). `workflow/reset` stopping one live run ends it the same way before its Bots are released: nodes still `running` become `failed`, so the strip cannot keep painting a run that is over (#1424). A graph edit that drops a node that is still running ends that run the way Stop does: the node is failed, the run is archived `failed`, its node Bots are released, and the queue drains (#1549). Every wait is bounded (#1530). A dispatch that does not land is not marked handed: the chat names the code and the attempt, and it is retried after a short backoff at most twice, never twice inside one hand-off, so a 429 is not hammered. When it still does not land, the step and the run fail with the reason written into the chat. Each running live step also holds a deadline — its workflow's `stepTimeoutMinutes`, `DEFAULT_STEP_TIMEOUT_MINUTES` (45 minutes) unless the record sets one, an integer from 1 to 120 given at create or through `workflow/step-timeout` (armed even when the host cannot spawn ephemeral Bots; #1530, #1580); when it expires the node and the run fail as `failed`, the run's ephemeral Bots are released, the chat says the step timed out, and `warn` names it once. The deadline is cleared as soon as the step moves, and by Pause, Stop, Reset, delete, `reap` and `dispose`; a paused run holds none, and Resume arms it again from that moment. A timer never outlives the run that started it. A reply that matches no running step — its run ended, or the node already moved — is reported through `warn` once per Bot and run instead of being dropped silently. A reply that says `could not answer your message` or `model call failed` fails that node and does not start its successor. A 429 or rate-limit mention in any other reply does not. Stop and Reset (`workflow/reset`) follow the same order as `dispose`: they release the run's Bots before the record forgets their ids, and an id whose release failed stays named on the still-live run, so the next Stop, Reset, dispose or load-time `reap` retries it (#1476). A run left that way is marked `paused` and `releasePending` and shows as Paused: it hands off no further node, `resume` is refused with an error, and Stop again is the retry. A whole-workflow Reset that could not release every Bot freezes every run it did not reset, because each of them has lost the ids whose release did go. The markers go away when the run ends: Stop again, `dispose`, `reap`, or its own completion. 4. Each workflow keeps a FIFO queue and up to `maxConcurrent` live runs (default 2, an integer from 1 to 8 on the record, via `workflow/concurrency`, or in Settings → Workflow graphs). A task that does not fit is enqueued. When a run finishes, every workflow with a free slot is drained, oldest enqueue first. A new assign on a workflow that already has a queue waits behind it. A task does not replace a running run or release that run's Bots. Migrating an old store keeps only runs that were still `running` with ephemeral Bot ids; other legacy scripted rows are dropped from active state (finished tasks may remain in lane history). History records a finished or failed live run. Lowering the cap does not stop a run that has already started. Assigning `{ botId }` claims the card for that local Bot and does not enqueue. A remote Bot is refused. `activeCount` is `runningOf(lane)` — live runs with `live === true` and `status === 'running'`. The stage aggregate chip and the Runs tab (运行记录) must agree: every such run appears as its own running row and progress card with its own `status` (never a hardcoded Running), the chip count does not exceed that list, and a failed sibling appears with its time and reason. 5. A node declares up to eight inputs and eight outputs. A required input is an output of the same name on an ancestor, or `task`, `title`, or `brief` on a root. A start with no provider is refused, and Bots spawned for that attempt are released. On a Bot-backed run a successor stays queued until its required inputs are present. If every forward predecessor is done, failed or skipped and an input is still missing, that node fails and the run stops. The dispatch text carries `Input : `. The Bot reports `OUTPUT : `; a single declared output may be the reply body. Nodes start in edge order. 6. Each node is a real ephemeral Bot for that run, then it is removed. A permanent sidebar Bot (`botId`) is optional and is not required for the step to run. **File and shell rights are the same as a normal sidebar Bot's:** every node Bot gets the `danger-full-access` preset, and `EPHEMERAL_DENIED_TOOLS` still refuses the team-admin tools at the guard and approval layers (#1466, 29 names). **`node.tools` selects connectors only:** `spawn` passes the node's cleaned list to `fb.bot.create`, the host records it as `stepTools`, and `connector-access.js` allows `mcp__github__*` for `github` and `mcp__composio__*` for `composio` — a connector the node did not name is refused, and a node that names none reaches none. The list is also stored in `fields.tools` for the UI. A hidden run Bot cannot show an approval card, so the approval seam answers the same way the guard does. The host stores this on the run Bot as `stepTools` and `permission`. diff --git a/plugins/workflow/src/graph.js b/plugins/workflow/src/graph.js index 215508ebd..24813f285 100644 --- a/plugins/workflow/src/graph.js +++ b/plugins/workflow/src/graph.js @@ -431,12 +431,16 @@ export function missingProviders(graph) { // Check if a step result indicates the workflow should end early. // Returns { shouldEnd: true, reason: string } if the run should stop, or { shouldEnd: false } to continue. // Reasons: needs-human (#1538), already-claimed (#1537), tip-unchanged, open-pr. -// Prefer structured OUTPUT values or a whole-line "EARLY-END " directive; also honor -// common completion phrases so a Bot that forgets the marker still cannot burn loop/onFail max. +// `needs-human` and `already-claimed` end a run only on a structured signal (#1546): a +// declared OUTPUT that equals the marker, or a whole reply line that is exactly +// "EARLY-END ". A step that quotes, negates or reports one of them is still working. +// `tip-unchanged` and `open-pr` keep that same structured signal plus their prose fallbacks, +// so a Bot that forgets the marker cannot burn a loop or an onFail attempt on the same tip. function checkEarlyEnd(outputs, text) { const lowerText = String(text ?? '').toLowerCase() const outputValues = Object.values(outputs ?? {}).map(v => String(v ?? '').toLowerCase().trim()) const reasons = ['needs-human', 'already-claimed', 'tip-unchanged', 'open-pr'] + const strict = new Set(['needs-human', 'already-claimed']) for (const line of lowerText.split(/\r?\n/)) { const match = line.trim().match(/^early-end\s+([a-z0-9_-]+)$/i) @@ -446,20 +450,12 @@ function checkEarlyEnd(outputs, text) { } for (const reason of reasons) { - if (outputValues.some(v => v === reason || v.includes(reason))) { + // A strict reason must be the whole value; a prose fallback may contain it. + if (outputValues.some(v => v === reason || (!strict.has(reason) && v.includes(reason)))) { return { shouldEnd: true, reason } } } - if (lowerText.includes('needs-human')) { - return { shouldEnd: true, reason: 'needs-human' } - } - - if (outputValues.some(v => v.includes('already-claimed')) || - (lowerText.includes('already claimed') && (lowerText.includes('agent-working') || lowerText.includes('claimed-by')))) { - return { shouldEnd: true, reason: 'already-claimed' } - } - if (/tip[\s_-]*unchanged|unchanged[\s_-]*tip/.test(lowerText)) { return { shouldEnd: true, reason: 'tip-unchanged' } } diff --git a/plugins/workflow/test/claim-and-needs-human.test.js b/plugins/workflow/test/claim-and-needs-human.test.js index d793dae8d..b335078c8 100644 --- a/plugins/workflow/test/claim-and-needs-human.test.js +++ b/plugins/workflow/test/claim-and-needs-human.test.js @@ -163,7 +163,7 @@ test('#1538: needs-human in any step ends the run', () => { assert.notEqual(run.nodeStatus.implement, 'running') }) -test('#1538: needs-human detection is case-insensitive and works in text body', () => { +test('#1538/#1546: the early-end directive is case-insensitive and line-owning', () => { const svc = service() const workflow = deliveryPipeline() const created = svc.create({ name: workflow.name, nodes: workflow.nodes, edges: workflow.edges }) @@ -171,12 +171,12 @@ test('#1538: needs-human detection is case-insensitive and works in text body', svc.assign({ workflowId, taskId: '#1541', title: 'Another needs-human case' }) - // Various ways needs-human might appear - in text body (not just outputs) + // The reply ends the run only through the directive line, in any case. const triageResult = svc.advance({ workflowId, nodeId: 'triage', outcome: 'done', - text: 'This issue NEEDS-HUMAN review before proceeding with implementation.', + text: 'Triage report follows.\n\nEarly-End NEEDS-HUMAN\n', outputs: { status: 'blocked' }, }) diff --git a/plugins/workflow/test/early-end-signal.test.js b/plugins/workflow/test/early-end-signal.test.js new file mode 100644 index 000000000..d7e56736e --- /dev/null +++ b/plugins/workflow/test/early-end-signal.test.js @@ -0,0 +1,139 @@ +// #1546: only a structured signal ends a run early. A step that mentions the +// markers while it works on them -- quoting an issue title, negating the label, +// reporting it absent -- must not stop its own run. + +import assert from 'node:assert/strict' +import { describe, it } from 'node:test' +import { createWorkflowService } from '../src/service.js' +import { normalizeGraph } from '../src/graph.js' + +const roster = [ + { id: 'bot-triage', name: 'Triage', role: 'Triage', remote: false }, + { id: 'bot-implement', name: 'Implement', role: 'Implement', remote: false }, +] + +// triage -> implement, so a run that must not end is still `running` with +// `implement` dispatched by the time `advance` answers. +function pipeline() { + return normalizeGraph({ + id: 'triage-then-implement', + name: 'Triage then implement', + nodes: [ + { id: 'triage', name: 'Triage', role: 'Triage', instructions: 'Triage the issue', outputs: [{ name: 'status', type: 'text' }] }, + { id: 'implement', name: 'Implement', role: 'Implement', instructions: 'Implement the fix', inputs: [{ name: 'status', type: 'text' }] }, + ], + edges: [{ from: 'triage', to: 'implement' }], + }) +} + +function started() { + const svc = createWorkflowService({ persist: false, now: () => 1_700_000_000_000, bots: () => roster }) + const graph = pipeline() + const created = svc.create({ name: graph.name, nodes: graph.nodes, edges: graph.edges }) + const workflowId = created.workflows.find(w => w.name === graph.name).id + svc.assign({ workflowId, taskId: '#1600', title: 'A task' }) + return { svc, workflowId } +} + +function advance(svc, workflowId, { text, outputs }) { + const state = svc.advance({ workflowId, nodeId: 'triage', outcome: 'done', text, outputs }) + return state.workflows.find(w => w.id === workflowId).run +} + +describe('Early end needs a structured signal', () => { + it('does not end the run when the reply quotes an issue title', () => { + const { svc, workflowId } = started() + const run = advance(svc, workflowId, { + text: 'Working on #1538: "runs now end when any step marks an issue as needs-human" — implementing the detector.', + outputs: { status: 'agent-ready' }, + }) + + assert.equal(run.status, 'running') + assert.equal(run.earlyEnd, undefined) + assert.equal(run.nodeStatus.implement, 'running') + }) + + it('does not end the run when the reply negates the label', () => { + const { svc, workflowId } = started() + const run = advance(svc, workflowId, { + text: 'No needs-human label on this issue; it is ready for implementation.', + outputs: { status: 'agent-ready' }, + }) + + assert.equal(run.status, 'running') + assert.equal(run.earlyEnd, undefined) + assert.equal(run.nodeStatus.implement, 'running') + }) + + it('does not end the run when the reply reports the label as absent', () => { + const { svc, workflowId } = started() + const run = advance(svc, workflowId, { + text: 'Checked labels: needs-human = absent. Proceeding.', + outputs: { status: 'agent-ready' }, + }) + + assert.equal(run.status, 'running') + assert.equal(run.earlyEnd, undefined) + assert.equal(run.nodeStatus.implement, 'running') + }) + + it('does not end the run when a sentence merely mentions the already-claimed rule', () => { + const { svc, workflowId } = started() + const run = advance(svc, workflowId, { + text: 'This issue is not already claimed: no agent-working label and no Claimed-by comment.', + outputs: { status: 'agent-ready' }, + }) + + assert.equal(run.status, 'running') + assert.equal(run.earlyEnd, undefined) + assert.equal(run.nodeStatus.implement, 'running') + }) + + it('ends the run when a declared output equals the marker', () => { + const { svc, workflowId } = started() + const run = advance(svc, workflowId, { + text: 'Triage complete.', + outputs: { status: 'Needs-Human' }, + }) + + assert.notEqual(run.status, 'running') + assert.equal(run.earlyEnd, 'needs-human') + assert.notEqual(run.nodeStatus.implement, 'running') + }) + + it('ends the run on a line that owns the directive', () => { + const { svc, workflowId } = started() + const run = advance(svc, workflowId, { + text: 'Triage complete.\n\nEARLY-END needs-human\n\nThe scope needs the owner.', + outputs: { status: 'blocked' }, + }) + + assert.notEqual(run.status, 'running') + assert.equal(run.earlyEnd, 'needs-human') + assert.notEqual(run.nodeStatus.implement, 'running') + }) + + it('ends the run on a line that owns the already-claimed directive', () => { + const { svc, workflowId } = started() + const run = advance(svc, workflowId, { + text: 'Claim check finished.\nearly-end ALREADY-CLAIMED\n', + outputs: { status: 'blocked' }, + }) + + assert.notEqual(run.status, 'running') + assert.equal(run.earlyEnd, 'already-claimed') + assert.notEqual(run.nodeStatus.implement, 'running') + }) + + it('does not end the run when the directive appears inside a sentence', () => { + const { svc, workflowId } = started() + const run = advance(svc, workflowId, { + text: 'The step documents the directive EARLY-END needs-human for later, and carries on.', + outputs: { status: 'agent-ready' }, + }) + + assert.equal(run.status, 'running') + assert.equal(run.earlyEnd, undefined) + assert.equal(run.nodeStatus.implement, 'running') + }) +})