Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
host: Stop during agent start cancels the start instead of leaking a turn
Niko1221a24e78f6f1b8
diff --git a/apps/host/server/agent/agent-bridge.mjs b/apps/host/server/agent/agent-bridge.mjsindex 1a0740755..4049561f7 100644--- a/apps/host/server/agent/agent-bridge.mjs+++ b/apps/host/server/agent/agent-bridge.mjs@@ -173,9 +173,15 @@ export function createAgentBridge({ dataDir, componentsDir = null, localInstance* Run one agent turn for a Strata chat and stream it as frames.* `signal` means the client went away: the turn keeps running (that is not Stop).*/- async function runTurn({ chatId, message, workspacePath, choice = null, history = [], onFrame, requestId = `req-${randomUUID()}`, signal }) {- const c = chat(chatId);- if (c.turn) throw new AgentUnavailable('This chat is already running a turn.', 'turn-active');+ async function runTurn(options) {+ const c = chat(options.chatId);+ if (c.turn || c.starting) throw new AgentUnavailable('This chat is already running a turn.', 'turn-active');+ // From here until the Harness has the prompt, Stop has no session to cancel; it cancels the start.+ c.starting = true; c.cancelStart = false;+ try { return await runTurnInner(c, options); } finally { c.starting = false; }+ }++ async function runTurnInner(c, { chatId, message, workspacePath, choice = null, history = [], onFrame, requestId = `req-${randomUUID()}`, signal }) {let send = onFrame;signal?.addEventListener('abort', () => { send = () => {}; }, { once: true });const emit = frame => { try { send(frame); } catch { send = () => {}; } };@@ -183,6 +189,8 @@ export function createAgentBridge({ dataDir, componentsDir = null, localInstanceemit({ type: 'queued', requestId });const { rt: runtime, route } = await ensureRuntime(choice, signal);const { sessionId, fresh } = await sessionFor(c, workspacePath, runtime);+ const cancelled = () => { emit({ type: 'done', finishReason: 'aborted', cancelled: true, tokensIn: null, tokensOut: null, usageSource: 'unavailable', durationMs: 0, model: route.name, contextMax: route.contextWindow }); return { cancelled: true }; };+ if (c.cancelStart) return cancelled();if (c.route !== `${route.route}/${route.modelId}` || c.routeGeneration !== runtime.generation) {try {@@ -197,7 +205,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, localInstanceconst projector = new TurnProjector();const turn = {sessionId, projector, route, requestId, live: false, lastSeq: -1,- ended: deferred(), follow: null, snapshot: deferred(), finished: false,+ ended: deferred(), follow: null, snapshot: deferred(), finished: false, prompted: false, stopBeforePrompt: false,abortFromRuntime: reason => finishTurn(reason),};c.turn = turn;@@ -249,6 +257,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, localInstance.catch(e => { finishTurn(e.message); }).finally(() => clearTimeout(snapshotTimer.t));if (turn.finished) return { error: true };+ if (turn.stopBeforePrompt || c.cancelStart) { projector.ended = { kind: 'aborted', reason: { kind: 'user' } }; finishTurn(null); return { cancelled: true }; }let prompt = message;if (fresh && history.length) prompt = withEarlierConversation(history, message);@@ -258,6 +267,13 @@ export function createAgentBridge({ dataDir, componentsDir = null, localInstancefinishTurn(`The agent did not accept the message: ${e.message}`);return { error: true };}+ turn.prompted = true;+ if (turn.stopBeforePrompt) {+ // Stop landed in the instant between 'prompt sent' and 'prompt accepted'. A cancel before the turn+ // starts arms nothing, so wait for the turn to be ours, then cancel it.+ for (let i = 0; i < 100 && !turn.live && !turn.finished; i++) await new Promise(r => setTimeout(r, 50));+ if (!turn.finished) await runtime.gw.rpc('session/cancel', { request: { sessionId } }).catch(() => {});+ }// Wait for the turn, or for the client to leave. Leaving does not stop the turn.const gone = new Promise(r => signal?.addEventListener('abort', () => r('detached'), { once: true }));@@ -294,8 +310,15 @@ export function createAgentBridge({ dataDir, componentsDir = null, localInstanceasync function stop(chatId, { waitMs = 20_000 } = {}) {const c = chats.get(chatId);const turn = c?.turn;+ if (!turn && c?.starting) { c.cancelStart = true; return { stopped: true, ended: 'cancelled-before-start', ms: 0 }; }if (!turn || !rt) return { stopped: false, reason: 'no active turn' };const t0 = Date.now();+ if (!turn.prompted) {+ // The Harness has not been given the prompt yet, so there is nothing to cancel there.+ turn.stopBeforePrompt = true;+ const early = await Promise.race([turn.ended.promise, new Promise(r => setTimeout(() => r(null), 100))]);+ if (early) return { stopped: true, ended: early.reason?.kind ?? 'aborted', ms: Date.now() - t0 };+ }await rt.gw.rpc('session/cancel', { request: { sessionId: turn.sessionId } });let timer; const result = await Promise.race([turn.ended.promise, new Promise(r => { timer = after(waitMs, () => r(null)); })]);clearTimeout(timer);diff --git a/apps/host/tests/agent-bridge.test.mjs b/apps/host/tests/agent-bridge.test.mjsindex c9efb866b..e2c9605ab 100644--- a/apps/host/tests/agent-bridge.test.mjs+++ b/apps/host/tests/agent-bridge.test.mjs@@ -239,3 +239,18 @@ test('a missing or removed connection is a typed error, never another model', {await assert.rejects(r.bridge.runTurn({ chatId: 'chat-gone', message: 'SCRIPT:hello', workspacePath: r.ws, choice: { connectionId: 'nope', modelId: 'm' }, onFrame: () => {} }), err => err.code === 'model-unavailable');assert.equal(r.worker.requests.length, 0, 'nothing was sent to the local model instead');});++test('A05c Stop pressed while the agent is still starting cancels the start, and nothing reaches the model', { skip }, async t => {+ const r = await rig(t);+ const { frames, promise } = r.run('chat-a05c', 'SCRIPT:slow 200');+ await new Promise(x => setTimeout(x, 150)); // the Harness process takes about two seconds to boot+ const stopped = await r.bridge.stop('chat-a05c');+ assert.equal(stopped.stopped, true);+ const result = await promise;+ assert.equal(result.cancelled, true);+ assert.equal(frames.at(-1).finishReason, 'aborted');+ assert.equal(r.worker.requests.length, 0, 'the model was never asked');+ // The chat is usable afterwards.+ const again = r.run('chat-a05c', 'SCRIPT:echo fine'); await again.promise;+ assert.equal(text(again.frames), 'fine');+});