Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(cowork): a cancelled run starts no further phase

thinhlpg0f040052872e
diff --git a/web-app/src/lib/__tests__/coworkSubagent.test.ts b/web-app/src/lib/__tests__/coworkSubagent.test.ts
index f0fdede1a..f72b43556 100644
--- a/web-app/src/lib/__tests__/coworkSubagent.test.ts
+++ b/web-app/src/lib/__tests__/coworkSubagent.test.ts
@@ -784,6 +784,63 @@ describe('runDispatchPlan', () => {
expect(briefs.collector).toContain('beta findings')
})
+ // Mirrors the Rust `a_cancelled_run_starts_no_further_phase`: a run cancelled
+ // while a phase is running starts no phase after it. Before the fix the
+ // collector was dispatched anyway -- promoted in the UI, cancelled on arrival,
+ // and announced by a terminal notice for a plan nobody was waiting for.
+ it('starts no further phase once the run is aborted', async () => {
+ const controller = new AbortController()
+ const started: string[] = []
+ const plan: DispatchPlan = {
+ phases: [
+ { number: 0, subagents: [req('alpha')] },
+ { number: 1, subagents: [req('collector')] },
+ ],
+ }
+
+ const finalPhase = await runDispatchPlan(plan, 'c1', {
+ signal: controller.signal,
+ runOne: async (r) => {
+ started.push(r.name)
+ // Cancelled while phase 0 is still running.
+ if (r.name === 'alpha') controller.abort()
+ return okResult(`${r.name} out`)
+ },
+ writeBlackboard: async (name) => `/tmp/blackboard/${name}.md`,
+ onDispatch: () => {},
+ onComplete: () => {},
+ })
+
+ expect(started).toEqual(['alpha'])
+ // No final phase: the caller must not ring a doorbell for a plan that never
+ // finished, so the half-done phase 1 is not offered as one.
+ expect(finalPhase).toEqual([])
+ })
+
+ it('dispatches nothing when the run is already aborted', async () => {
+ const controller = new AbortController()
+ controller.abort()
+ const started: string[] = []
+
+ const finalPhase = await runDispatchPlan(
+ { phases: [{ number: 0, subagents: [req('alpha')] }] },
+ 'c1',
+ {
+ signal: controller.signal,
+ runOne: async (r) => {
+ started.push(r.name)
+ return okResult('out')
+ },
+ writeBlackboard: async (name) => `/tmp/blackboard/${name}.md`,
+ onDispatch: () => {},
+ onComplete: () => {},
+ }
+ )
+
+ expect(started).toEqual([])
+ expect(finalPhase).toEqual([])
+ })
+
it('keys each subagent `${callId}-${name}` and writes real answers to the blackboard', async () => {
const dispatched: string[] = []
const writes: string[] = []
diff --git a/web-app/src/lib/coworkSubagent.ts b/web-app/src/lib/coworkSubagent.ts
index f67570d1a..0c18a635e 100644
--- a/web-app/src/lib/coworkSubagent.ts
+++ b/web-app/src/lib/coworkSubagent.ts
@@ -847,6 +847,16 @@ export type DispatchPlanCallbacks = {
result: SubagentResult,
savedPath: string | null
) => void
+ /** The run's abort signal, if the caller has one.
+ *
+ * Consulted between phases: a cancelled run starts no further phase, because
+ * the children of the phase after this one would be dispatched only to be
+ * cancelled on arrival -- promoted in the UI as though they had worked, and
+ * then announced by a terminal notice for a plan nobody is waiting for. A
+ * phase already running is left alone; its children read the same signal
+ * themselves (`runSubagent`). Mirrors the Rust driver, which stops when the
+ * registry it dispatches into has been torn down. */
+ signal?: AbortSignal
}
/** One finished child of a phase: what `runDispatchPlan` returns for the final
@@ -874,6 +884,9 @@ function hasRealAnswer(result: SubagentResult): boolean {
* proceeds; this never throws (so the caller can release its plan-hold in a
* `finally`). Each subagent is keyed `${callId}-${name}` for the store.
*
+ * A cancelled run (`cb.signal`) stops the plan where it stands: no later phase is
+ * dispatched, no final phase is reported, and the caller rings no notice for it.
+ *
* Returns the final phase's outcomes so a multi-phase caller can ring the
* doorbell once with a consolidated notice (see `planCompletionNotice`).
*/
@@ -885,6 +898,11 @@ export async function runDispatchPlan(
let inputs: { name: string; output: string }[] = []
let finalPhase: PlanChildOutcome[] = []
for (const phase of plan.phases) {
+ // The run is gone: the phases still ahead are dead work, and there is no
+ // final phase to report. Returning nothing is what stops a caller from
+ // announcing a plan that never finished. Checked before the first phase too,
+ // so a plan dispatched into an already-cancelled run starts nothing at all.
+ if (cb.signal?.aborted) return []
const outcomes = await Promise.all(
phase.subagents.map(async (req): Promise<PlanChildOutcome> => {
const id = `${callId}-${req.name}`
diff --git a/web-app/src/routes/cowork.tsx b/web-app/src/routes/cowork.tsx
index c97431c34..84e982951 100644
--- a/web-app/src/routes/cowork.tsx
+++ b/web-app/src/routes/cowork.tsx
@@ -908,6 +908,9 @@ function CoworkPage() {
},
writeBlackboard: (name, content) =>
writeBlackboard(sid, name, content),
+ // A cancelled run dispatches no further phase; see
+ // `DispatchPlanCallbacks.signal`.
+ signal: controller.signal,
// begin at dispatch, finish at completion: with the plan hold
// above, inbox.pending() never reads false mid-plan.
onDispatch: () => inbox.begin(),
@@ -935,8 +938,9 @@ function CoworkPage() {
.then((finalPhase) => {
// Ring once for the whole plan, after its last phase. Queue
// the ping (note, not finish) since the plan-hold slot is
- // released by the finally below.
- if (multi) {
+ // released by the finally below. A cancelled run gets no
+ // notice: the plan did not finish, and nobody is waiting.
+ if (multi && !controller.signal.aborted) {
inbox.note(
planCompletionNotice({
phaseCount: plan.phases.length,