runs: reconcile uncertain browser launches with a persistent intent key

This commit is contained in:
2026-10-20 15:40:43 +01:00
parent f06281db08
commit 44834e91f0
8 changed files with 146 additions and 9 deletions
+11
View File
@@ -42,6 +42,17 @@ can use the host user's permissions, credentials, and network. Use a dedicated
unprivileged account, VM, or container with an explicit workspace mount for unprivileged account, VM, or container with an explicit workspace mount for
untrusted definitions. Do not advertise prompt instructions as sandboxing. untrusted definitions. Do not advertise prompt instructions as sandboxing.
## Lost run-start replies
The web Run dialog saves one immutable launch payload and request key in tab
storage before sending it. An uncertain launch is not automatically replayed.
`Check / retry` first uses the read-only `get_run` command with `request_key`;
if admitted, it opens that exact run, even if it already finished. Otherwise it
resends the original intent with the same key. Changing the task requires first
resolving admission; an explicit refusal is editable only after that lookup
confirms no run exists. A page reload and reopening the same pipeline/version
recovers the pending intent on the same endpoint.
## Delivery discipline ## Delivery discipline
Checks and browser probes use scratch databases and loopback ports. Installing Checks and browser probes use scratch databases and loopback ports. Installing
+8
View File
@@ -76,6 +76,14 @@ func TestRunWebSocketRemainsResponsiveAndAuditable(t *testing.T) {
t.Fatal("started run omitted pinned graph") t.Fatal("started run omitted pinned graph")
} }
<-entered <-entered
lookup := request(protocol.Message{Type: protocol.GetRun, ID: "reconcile", RequestKey: "ws-once"})
if lookup.Type != protocol.Run || lookup.RunDetail == nil || lookup.RunDetail.ID != started.RunDetail.ID {
t.Fatalf("read-only admission lookup failed: %+v", lookup)
}
absent := request(protocol.Message{Type: protocol.GetRun, ID: "absent", RequestKey: "not-admitted"})
if absent.Type != protocol.Run || absent.RunDetail != nil {
t.Fatalf("absent lookup must not invent a run: %+v", absent)
}
if response := request(protocol.Message{Type: protocol.Ping, ID: "ping"}); response.Type != protocol.Pong { if response := request(protocol.Message{Type: protocol.Ping, ID: "ping"}); response.Type != protocol.Pong {
t.Fatal("active run blocked ping") t.Fatal("active run blocked ping")
} }
+9 -1
View File
@@ -302,7 +302,15 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
} }
case protocol.GetRun: case protocol.GetRun:
response.Type = protocol.Run response.Type = protocol.Run
response.RunDetail, err = runs.getRun(request.RunID) if request.RunID == 0 && request.RequestKey != "" {
var id int64
id, err = stores[0].RunByKey(connCtx, request.RequestKey)
if err == nil && id != 0 {
response.RunDetail, err = runs.getRun(id)
}
} else {
response.RunDetail, err = runs.getRun(request.RunID)
}
case protocol.CancelRun: case protocol.CancelRun:
response.Type = protocol.Run response.Type = protocol.Run
err = runs.cancel(request.RunID) err = runs.cancel(request.RunID)
+9 -2
View File
@@ -185,12 +185,12 @@ export class PipelinesRepository {
* Starts a fresh run of one pipeline version. `notifyMode` is auto, on or off; * Starts a fresh run of one pipeline version. `notifyMode` is auto, on or off;
* auto omits the field so the pipeline definition applies. * auto omits the field so the pipeline definition applies.
*/ */
async runPipeline({ pipelineId, version, prompt, workspace, notifyMode = "auto", title = "" } = {}) { async runPipeline({ pipelineId, version, prompt, workspace, notifyMode = "auto", title = "", requestKey = newChatOperationId() } = {}) {
const request = { const request = {
type: Protocol.RUN_PIPELINE, type: Protocol.RUN_PIPELINE,
pipeline_id: Number(pipelineId), pipeline_id: Number(pipelineId),
version: Number(version), version: Number(version),
request_key: newChatOperationId(), request_key: requestKey,
workspace: String(workspace ?? "").trim(), workspace: String(workspace ?? "").trim(),
input: { prompt: String(prompt ?? "") }, input: { prompt: String(prompt ?? "") },
}; };
@@ -204,6 +204,13 @@ export class PipelinesRepository {
return response.run_detail; return response.run_detail;
} }
/** Read-only admission reconciliation after a lost launch response. */
async runByRequestKey(requestKey) {
if (!String(requestKey ?? "").trim()) throw new ProtocolError("A launch request key is required.");
const response = await this.#client.call({ type: Protocol.GET_RUN, request_key: requestKey });
return response.run_detail ?? null;
}
async cancelRun(id) { async cancelRun(id) {
const response = await this.#client.call({ type: Protocol.CANCEL_RUN, run_id: id }); const response = await this.#client.call({ type: Protocol.CANCEL_RUN, run_id: id });
if (!response.run_detail) { if (!response.run_detail) {
+34
View File
@@ -0,0 +1,34 @@
// A launch intent survives a lost response and a page reload. It is never
// replayed automatically; the operator explicitly checks/retries the same key.
import { newChatOperationId } from "./repository.js";
function storageKey(endpoint, pipelineId, version) {
return `pipelines.web.pendingRun.v1:${endpoint}:${pipelineId}:${version}`;
}
export function pendingRun(storage, endpoint, pipelineId, version) {
try {
const item = JSON.parse(storage?.getItem(storageKey(endpoint, pipelineId, version)) ?? "null");
if (!item || typeof item.requestKey !== "string" || typeof item.prompt !== "string" || typeof item.workspace !== "string") return null;
if (item.pipelineId !== pipelineId || item.version !== version) return null;
return item;
} catch {
return null;
}
}
export function createRunIntent(payload) {
return { ...payload, requestKey: newChatOperationId() };
}
export function saveRunIntent(storage, endpoint, intent) {
try {
storage?.setItem(storageKey(endpoint, intent.pipelineId, intent.version), JSON.stringify(intent));
} catch { /* In-memory identity still protects retries when storage is unavailable. */ }
}
export function clearRunIntent(storage, endpoint, intent) {
try {
storage?.removeItem(storageKey(endpoint, intent.pipelineId, intent.version));
} catch { /* Storage failure must not turn a completed launch into another run. */ }
}
+39 -6
View File
@@ -27,6 +27,8 @@ import {
import { PIPELINE_GRAPH_EMPTY, layoutGraph, mount as mountGraph } from "../graph.js"; import { PIPELINE_GRAPH_EMPTY, layoutGraph, mount as mountGraph } from "../graph.js";
import { statusIcon } from "../statusIcon.js"; import { statusIcon } from "../statusIcon.js";
import { writeGraphExpanded } from "../graphPrefs.js"; import { writeGraphExpanded } from "../graphPrefs.js";
import { pendingRun, createRunIntent, saveRunIntent, clearRunIntent } from "../runIntent.js";
import { ProtocolError } from "../client.js";
export function createPipelinesView(rt) { export function createPipelinesView(rt) {
const { state, repo, modal } = rt; const { state, repo, modal } = rt;
@@ -391,6 +393,13 @@ async function openRunDialog(pipeline) {
sending: false, sending: false,
error: "", error: "",
}; };
start.intent = pendingRun(globalThis.sessionStorage, state.url, start.pipelineId, start.version);
if (start.intent) {
start.prompt = start.intent.prompt;
start.workspace = start.intent.workspace;
start.notifyMode = start.intent.notifyMode;
start.error = "A previous launch has an unknown result. Check/retry reconciles that same request; it will not start a duplicate.";
}
state.runStart = start; state.runStart = start;
renderContent(); renderContent();
try { try {
@@ -428,9 +437,9 @@ async function startRun() {
} }
start.sending = true; start.sending = true;
start.error = ""; start.error = "";
renderContent(); const recovering = start.intent != null;
try { if (!start.intent) {
const run = await repo.runPipeline({ start.intent = createRunIntent({
pipelineId: start.pipelineId, pipelineId: start.pipelineId,
version: start.version, version: start.version,
prompt, prompt,
@@ -438,12 +447,34 @@ async function startRun() {
notifyMode: start.notifyMode, notifyMode: start.notifyMode,
title: "", title: "",
}); });
}
saveRunIntent(globalThis.sessionStorage, state.url, start.intent);
renderContent();
try {
const existing = recovering ? await repo.runByRequestKey(start.intent.requestKey) : null;
const run = existing ?? await repo.runPipeline(start.intent);
clearRunIntent(globalThis.sessionStorage, state.url, start.intent);
state.runStart = null; state.runStart = null;
await applyRoute({ kind: "run", id: run.id }); await applyRoute({ kind: "run", id: run.id });
} catch (error) { } catch (error) {
if (state.runStart !== start) return; if (state.runStart !== start) return;
// A confirmed refusal can be corrected, but only after a read proves no
// run was admitted. A generic transport failure remains uncertain.
if (error instanceof ProtocolError) {
try {
const existing = await repo.runByRequestKey(start.intent.requestKey);
if (existing) {
clearRunIntent(globalThis.sessionStorage, state.url, start.intent);
state.runStart = null;
await applyRoute({ kind: "run", id: existing.id });
return;
}
clearRunIntent(globalThis.sessionStorage, state.url, start.intent);
start.intent = null;
} catch { /* Keep the launch immutable until admission can be checked. */ }
}
start.sending = false; start.sending = false;
start.error = describeError(error); start.error = describeError(error) + (start.intent ? ". Admission may be unknown. Check/retry uses the same request key." : "");
renderContent(); renderContent();
} }
} }
@@ -470,6 +501,7 @@ function renderRunStartDialog() {
rows: "5", rows: "5",
placeholder: "What should this run do?", placeholder: "What should this run do?",
spellcheck: "false", spellcheck: "false",
disabled: start.intent != null,
oninput: (event) => { start.prompt = event.target.value; }, oninput: (event) => { start.prompt = event.target.value; },
}); });
prompt.value = start.prompt; prompt.value = start.prompt;
@@ -486,6 +518,7 @@ function renderRunStartDialog() {
type: "text", type: "text",
list: listId, list: listId,
value: start.workspace, value: start.workspace,
disabled: start.intent != null,
spellcheck: "false", spellcheck: "false",
autocomplete: "off", autocomplete: "off",
placeholder: "/absolute/path/on/the/server", placeholder: "/absolute/path/on/the/server",
@@ -497,7 +530,7 @@ function renderRunStartDialog() {
const notifyField = h("label", { class: "run-start-field", for: "runNotify" }); const notifyField = h("label", { class: "run-start-field", for: "runNotify" });
notifyField.append(h("span", { class: "run-start-label", text: "Notifications for this run" })); notifyField.append(h("span", { class: "run-start-label", text: "Notifications for this run" }));
const notify = h("select", { id: "runNotify", onchange: (event) => { start.notifyMode = event.target.value; } }); const notify = h("select", { id: "runNotify", disabled: start.intent != null, onchange: (event) => { start.notifyMode = event.target.value; } });
for (const [value, label] of [ for (const [value, label] of [
["auto", "Pipeline default"], ["auto", "Pipeline default"],
["on", "Notify me"], ["on", "Notify me"],
@@ -525,7 +558,7 @@ function renderRunStartDialog() {
h("button", { h("button", {
class: "primary run-start-confirm", class: "primary run-start-confirm",
type: "button", type: "button",
text: start.sending ? "Starting…" : "Run", text: start.sending ? "Starting…" : start.intent ? "Check / retry" : "Run",
disabled: start.sending, disabled: start.sending,
onclick: startRun, onclick: startRun,
}), }),
+14
View File
@@ -332,6 +332,20 @@ test("a run action throws when the reply has no run detail", async () => {
// ---- The composer's own commands (C8/C9) ---------------------------------- // ---- The composer's own commands (C8/C9) ----------------------------------
test("run retries preserve one admission key and can reconcile without execution", async () => {
const { repo, ws } = connected();
ws.reply = (frame) => ({ id: frame.id, type: "run", run_detail: { id: 42 } });
const intent = { pipelineId: 7, version: 1, prompt: "task", workspace: "/work", requestKey: "same-intent" };
await repo.runPipeline(intent);
await repo.runPipeline(intent);
assert.deepEqual(ws.sent.map((frame) => frame.request_key), ["same-intent", "same-intent"]);
assert.equal((await repo.runByRequestKey("same-intent")).id, 42);
assert.equal(lastFrame(ws).type, "get_run");
assert.equal(lastFrame(ws).request_key, "same-intent");
ws.reply = (frame) => ({ id: frame.id, type: "run" });
assert.equal(await repo.runByRequestKey("not-admitted"), null);
});
test("chatPrompt trims the text, sends the operation and native message ids, and omits files when empty", async () => { test("chatPrompt trims the text, sends the operation and native message ids, and omits files when empty", async () => {
const { repo, ws } = connected(); const { repo, ws } = connected();
ws.reply = (frame) => ({ id: frame.id, type: "chat_prompt" }); ws.reply = (frame) => ({ id: frame.id, type: "chat_prompt" });
+22
View File
@@ -0,0 +1,22 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { createRunIntent, saveRunIntent, pendingRun, clearRunIntent } from "../src/runIntent.js";
test("pending launch identity and payload survive reload and stay endpoint-scoped", () => {
const values = new Map();
const storage = { getItem: (key) => values.get(key), setItem: (key, value) => values.set(key, value), removeItem: (key) => values.delete(key) };
const payload = { pipelineId: 7, version: 3, prompt: "report", workspace: "/work", notifyMode: "off" };
const intent = createRunIntent(payload);
assert.ok(intent.requestKey);
saveRunIntent(storage, "ws://server/connect", intent);
assert.deepEqual(pendingRun(storage, "ws://server/connect", 7, 3), intent);
assert.equal(pendingRun(storage, "ws://other/connect", 7, 3), null);
assert.equal(pendingRun(storage, "ws://server/connect", 7, 4), null);
clearRunIntent(storage, "ws://server/connect", intent);
assert.equal(pendingRun(storage, "ws://server/connect", 7, 3), null);
});
test("malformed or unavailable pending-run storage never invents an intent", () => {
assert.equal(pendingRun({ getItem: () => "not json" }, "ws://s", 1, 1), null);
assert.equal(pendingRun({ getItem: () => { throw Error("disabled"); } }, "ws://s", 1, 1), null);
});