runs: reconcile uncertain browser launches with a persistent intent key
This commit is contained in:
@@ -185,12 +185,12 @@ export class PipelinesRepository {
|
||||
* Starts a fresh run of one pipeline version. `notifyMode` is auto, on or off;
|
||||
* 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 = {
|
||||
type: Protocol.RUN_PIPELINE,
|
||||
pipeline_id: Number(pipelineId),
|
||||
version: Number(version),
|
||||
request_key: newChatOperationId(),
|
||||
request_key: requestKey,
|
||||
workspace: String(workspace ?? "").trim(),
|
||||
input: { prompt: String(prompt ?? "") },
|
||||
};
|
||||
@@ -204,6 +204,13 @@ export class PipelinesRepository {
|
||||
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) {
|
||||
const response = await this.#client.call({ type: Protocol.CANCEL_RUN, run_id: id });
|
||||
if (!response.run_detail) {
|
||||
|
||||
@@ -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. */ }
|
||||
}
|
||||
@@ -27,6 +27,8 @@ import {
|
||||
import { PIPELINE_GRAPH_EMPTY, layoutGraph, mount as mountGraph } from "../graph.js";
|
||||
import { statusIcon } from "../statusIcon.js";
|
||||
import { writeGraphExpanded } from "../graphPrefs.js";
|
||||
import { pendingRun, createRunIntent, saveRunIntent, clearRunIntent } from "../runIntent.js";
|
||||
import { ProtocolError } from "../client.js";
|
||||
|
||||
export function createPipelinesView(rt) {
|
||||
const { state, repo, modal } = rt;
|
||||
@@ -391,6 +393,13 @@ async function openRunDialog(pipeline) {
|
||||
sending: false,
|
||||
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;
|
||||
renderContent();
|
||||
try {
|
||||
@@ -428,9 +437,9 @@ async function startRun() {
|
||||
}
|
||||
start.sending = true;
|
||||
start.error = "";
|
||||
renderContent();
|
||||
try {
|
||||
const run = await repo.runPipeline({
|
||||
const recovering = start.intent != null;
|
||||
if (!start.intent) {
|
||||
start.intent = createRunIntent({
|
||||
pipelineId: start.pipelineId,
|
||||
version: start.version,
|
||||
prompt,
|
||||
@@ -438,12 +447,34 @@ async function startRun() {
|
||||
notifyMode: start.notifyMode,
|
||||
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;
|
||||
await applyRoute({ kind: "run", id: run.id });
|
||||
} catch (error) {
|
||||
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.error = describeError(error);
|
||||
start.error = describeError(error) + (start.intent ? ". Admission may be unknown. Check/retry uses the same request key." : "");
|
||||
renderContent();
|
||||
}
|
||||
}
|
||||
@@ -470,6 +501,7 @@ function renderRunStartDialog() {
|
||||
rows: "5",
|
||||
placeholder: "What should this run do?",
|
||||
spellcheck: "false",
|
||||
disabled: start.intent != null,
|
||||
oninput: (event) => { start.prompt = event.target.value; },
|
||||
});
|
||||
prompt.value = start.prompt;
|
||||
@@ -486,6 +518,7 @@ function renderRunStartDialog() {
|
||||
type: "text",
|
||||
list: listId,
|
||||
value: start.workspace,
|
||||
disabled: start.intent != null,
|
||||
spellcheck: "false",
|
||||
autocomplete: "off",
|
||||
placeholder: "/absolute/path/on/the/server",
|
||||
@@ -497,7 +530,7 @@ function renderRunStartDialog() {
|
||||
|
||||
const notifyField = h("label", { class: "run-start-field", for: "runNotify" });
|
||||
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 [
|
||||
["auto", "Pipeline default"],
|
||||
["on", "Notify me"],
|
||||
@@ -525,7 +558,7 @@ function renderRunStartDialog() {
|
||||
h("button", {
|
||||
class: "primary run-start-confirm",
|
||||
type: "button",
|
||||
text: start.sending ? "Starting…" : "Run",
|
||||
text: start.sending ? "Starting…" : start.intent ? "Check / retry" : "Run",
|
||||
disabled: start.sending,
|
||||
onclick: startRun,
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user