conductor-remote 1.117.0 → 1.118.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -8
- package/dist/assets/{PierrePatch-D3_YBIAr.js → PierrePatch-Dh-08CLI.js} +1 -1
- package/dist/assets/index-BRzsSW8g.css +1 -0
- package/dist/assets/index-BpY3Jhxk.js +65 -0
- package/dist/index.html +2 -2
- package/dist/push-sw.js +2 -2
- package/dist/sw.js +1 -1
- package/dist-node/scripts/nosleep-setup.js +2 -2
- package/dist-node/scripts/nosleep.js +3 -3
- package/dist-node/scripts/service/commands.js +32 -0
- package/dist-node/scripts/service/configuration.js +205 -0
- package/dist-node/scripts/service/environment.js +59 -0
- package/dist-node/scripts/service/flags.js +45 -0
- package/dist-node/scripts/service/installation.js +91 -0
- package/dist-node/scripts/service/launch-agent.js +134 -0
- package/dist-node/scripts/service/network.js +244 -0
- package/dist-node/scripts/service/presentation.js +101 -0
- package/dist-node/scripts/service/voice.js +113 -0
- package/dist-node/scripts/service.js +5 -980
- package/dist-node/src/{agent-config.js → agents/agent-config.js} +1 -1
- package/dist-node/src/{model-cache.js → agents/model-cache.js} +1 -1
- package/dist-node/src/{roles.js → agents/roles.js} +3 -3
- package/dist-node/src/db.js +1 -1
- package/dist-node/src/{parked.js → delivery/parked.js} +1 -1
- package/dist-node/src/{dev-server.js → dev-server/controller.js} +8 -291
- package/dist-node/src/dev-server/ports.js +63 -0
- package/dist-node/src/dev-server/processes.js +73 -0
- package/dist-node/src/dev-server/proxy.js +158 -0
- package/dist-node/src/{run-activity.js → dev-server/run-activity.js} +2 -2
- package/dist-node/src/dev-server/types.js +1 -0
- package/dist-node/src/{file-preview.js → files/file-preview.js} +1 -1
- package/dist-node/src/{change-stats.js → git/change-stats.js} +1 -1
- package/dist-node/src/{git.js → git/diff.js} +43 -18
- package/dist-node/src/{autoupdate.js → host/autoupdate.js} +1 -1
- package/dist-node/src/{funnel-watchdog.js → host/funnel-watchdog.js} +4 -4
- package/dist-node/src/{logbuf.js → host/logbuf.js} +1 -1
- package/dist-node/src/{nosleep-helper.js → host/nosleep-helper.js} +1 -1
- package/dist-node/src/{tailscale.js → host/tailscale.js} +1 -1
- package/dist-node/src/http/router-types.js +2 -0
- package/dist-node/src/http/router.js +78 -0
- package/dist-node/src/http/routes/create-workspace.js +90 -0
- package/dist-node/src/http/routes/files.js +53 -0
- package/dist-node/src/http/routes/prompts.js +363 -0
- package/dist-node/src/http/routes/sessions.js +279 -0
- package/dist-node/src/http/routes/state.js +186 -0
- package/dist-node/src/http/routes/system.js +220 -0
- package/dist-node/src/http/routes/voice.js +117 -0
- package/dist-node/src/http/routes/workflows.js +248 -0
- package/dist-node/src/http/routes/workspaces.js +267 -0
- package/dist-node/src/http/services/base.js +252 -0
- package/dist-node/src/http/services/delegations.js +301 -0
- package/dist-node/src/http/services/delivery.js +433 -0
- package/dist-node/src/http/services/files.js +199 -0
- package/dist-node/src/http/services/mcp.js +94 -0
- package/dist-node/src/http/services/responses.js +120 -0
- package/dist-node/src/http/services/voice.js +147 -0
- package/dist-node/src/http/services/workflow-probes.js +215 -0
- package/dist-node/src/http/services/workflow-state.js +195 -0
- package/dist-node/src/http/services/workflow.js +366 -0
- package/dist-node/src/http/services.js +35 -0
- package/dist-node/src/mcp/arguments.js +14 -0
- package/dist-node/src/mcp/dispatcher.js +56 -0
- package/dist-node/src/mcp/formatters.js +110 -0
- package/dist-node/src/mcp/protocol.js +7 -0
- package/dist-node/src/mcp/registry.js +66 -0
- package/dist-node/src/mcp/tools/agent-options.js +122 -0
- package/dist-node/src/mcp/tools/chat-actions.js +194 -0
- package/dist-node/src/mcp/tools/chats.js +194 -0
- package/dist-node/src/mcp/tools/delegation.js +135 -0
- package/dist-node/src/mcp/tools/system.js +135 -0
- package/dist-node/src/mcp/tools/voice.js +136 -0
- package/dist-node/src/mcp/tools/workspaces.js +259 -0
- package/dist-node/src/mcp/types.js +1 -0
- package/dist-node/src/mcp.js +6 -4
- package/dist-node/src/{notify.js → notifications/notify.js} +5 -5
- package/dist-node/src/{webpush.js → notifications/webpush.js} +1 -1
- package/dist-node/src/orchestration/delegation/codec.js +201 -0
- package/dist-node/src/{delegation-intake.js → orchestration/delegation/intake.js} +2 -2
- package/dist-node/src/{delegation-prompt.js → orchestration/delegation/prompt.js} +1 -1
- package/dist-node/src/orchestration/delegation/queue.js +255 -0
- package/dist-node/src/orchestration/delegation/store.js +111 -0
- package/dist-node/src/orchestration/delegation/types.js +2 -0
- package/dist-node/src/orchestration/persistence/capabilities.js +109 -0
- package/dist-node/src/orchestration/persistence/codecs.js +148 -0
- package/dist-node/src/orchestration/persistence/connection.js +205 -0
- package/dist-node/src/orchestration/persistence/db.js +101 -0
- package/dist-node/src/orchestration/persistence/effect-recovery.js +227 -0
- package/dist-node/src/orchestration/persistence/effect-transitions.js +157 -0
- package/dist-node/src/orchestration/persistence/effects.js +109 -0
- package/dist-node/src/orchestration/persistence/errors.js +39 -0
- package/dist-node/src/orchestration/persistence/events.js +29 -0
- package/dist-node/src/orchestration/persistence/idempotency.js +82 -0
- package/dist-node/src/orchestration/persistence/job-attempts.js +114 -0
- package/dist-node/src/orchestration/persistence/jobs.js +159 -0
- package/dist-node/src/orchestration/persistence/projections.js +190 -0
- package/dist-node/src/orchestration/persistence/quarantine.js +46 -0
- package/dist-node/src/orchestration/persistence/records.js +115 -0
- package/dist-node/src/orchestration/persistence/relays.js +78 -0
- package/dist-node/src/orchestration/persistence/runs.js +156 -0
- package/dist-node/src/{orchestration-schema.js → orchestration/persistence/schema.js} +1 -1
- package/dist-node/src/orchestration/persistence/types.js +1 -0
- package/dist-node/src/orchestration/persistence/ui-lease.js +197 -0
- package/dist-node/src/orchestration/persistence/values.js +114 -0
- package/dist-node/src/orchestration/workflow/commands.js +309 -0
- package/dist-node/src/orchestration/workflow/coordinator.js +28 -0
- package/dist-node/src/orchestration/workflow/effect-recovery.js +125 -0
- package/dist-node/src/orchestration/workflow/effects.js +307 -0
- package/dist-node/src/orchestration/workflow/errors.js +13 -0
- package/dist-node/src/orchestration/workflow/helpers.js +154 -0
- package/dist-node/src/orchestration/workflow/job-dispatch.js +312 -0
- package/dist-node/src/orchestration/workflow/job-results.js +280 -0
- package/dist-node/src/{workflow.js → orchestration/workflow/prompts.js} +2 -2
- package/dist-node/src/orchestration/workflow/root.js +293 -0
- package/dist-node/src/orchestration/workflow/start.js +99 -0
- package/dist-node/src/orchestration/workflow/state.js +105 -0
- package/dist-node/src/orchestration/workflow/types.js +1 -0
- package/dist-node/src/orchestration/workflow/wake.js +164 -0
- package/dist-node/src/prefs.js +1 -1
- package/dist-node/src/{background-tasks.js → reads/background-tasks.js} +1 -1
- package/dist-node/src/reads/messages.js +362 -0
- package/dist-node/src/reads/repository.js +87 -0
- package/dist-node/src/reads/sessions.js +165 -0
- package/dist-node/src/reads/types.js +1 -0
- package/dist-node/src/reads/workspaces.js +230 -0
- package/dist-node/src/reads/worktrees.js +45 -0
- package/dist-node/src/routes.js +5 -5
- package/dist-node/src/{search.js → search/coordinator.js} +4 -4
- package/dist-node/src/{search-worker.js → search/worker.js} +3 -3
- package/dist-node/src/server.js +14 -3930
- package/dist-node/src/settings.js +1 -1
- package/dist-node/src/shared.js +10 -10
- package/dist-node/src/{transcript.js → transcript/parser.js} +4 -4
- package/dist-node/src/{tool-usage-worker.js → usage/tool-usage-worker.js} +1 -1
- package/dist-node/src/{tool-usage.js → usage/tool-usage.js} +1 -1
- package/dist-node/src/voice/brief.js +22 -50
- package/dist-node/src/voice/context.js +1 -1
- package/dist-node/src/voice/dates.js +38 -0
- package/dist-node/src/voice/history.js +14 -5
- package/dist-node/src/voice/prompt.js +10 -9
- package/dist-node/src/voice/recall.js +115 -0
- package/dist-node/src/{speech.js → voice/speech.js} +1 -1
- package/dist-node/src/voice/tools.js +92 -3
- package/dist-node/src/voice/webrtc.js +1 -1
- package/dist-node/src/wire.js +2 -2
- package/dist-node/src/writes/actuator.js +124 -0
- package/dist-node/src/writes/agent-options.js +151 -0
- package/dist-node/src/writes/applescript/agent-options.applescript +334 -0
- package/dist-node/src/writes/applescript/chat-targeting.applescript +149 -0
- package/dist-node/src/writes/applescript/composer.applescript +195 -0
- package/dist-node/src/writes/applescript/hotspot.applescript +155 -0
- package/dist-node/src/writes/applescript/lifecycle.applescript +266 -0
- package/dist-node/src/writes/applescript/run-tasks.applescript +208 -0
- package/dist-node/src/writes/applescript/source.js +52 -0
- package/dist-node/src/writes/applescript/window.applescript +436 -0
- package/dist-node/src/writes/applescript/workspace-status.applescript +177 -0
- package/dist-node/src/writes/applescript/workspace-targeting.applescript +359 -0
- package/dist-node/src/writes/chats.js +160 -0
- package/dist-node/src/writes/guards.js +77 -0
- package/dist-node/src/writes/runner.js +67 -0
- package/dist-node/src/{sidecar.js → writes/sidecar.js} +1 -1
- package/dist-node/src/writes/system.js +81 -0
- package/dist-node/src/writes/targeting.js +121 -0
- package/dist-node/src/writes/types.js +1 -0
- package/dist-node/src/writes/ui-lock.js +229 -0
- package/dist-node/src/writes/workspaces.js +233 -0
- package/docs/voice-setup.md +34 -12
- package/package.json +2 -2
- package/dist/assets/index-ByTwVInL.css +0 -1
- package/dist/assets/index-LAm9X7wv.js +0 -65
- package/dist-node/src/conductor.applescript +0 -2279
- package/dist-node/src/delegations.js +0 -557
- package/dist-node/src/mcp-tools.js +0 -1319
- package/dist-node/src/orchestration-db.js +0 -2186
- package/dist-node/src/reads.js +0 -787
- package/dist-node/src/workflow-coordinator.js +0 -2119
- package/dist-node/src/writes.js +0 -1223
- /package/dist-node/src/{conductor-settings.js → agents/conductor-settings.js} +0 -0
- /package/dist-node/src/{firstprompt.js → delivery/firstprompt.js} +0 -0
- /package/dist-node/src/{sendonce.js → delivery/sendonce.js} +0 -0
- /package/dist-node/src/{preview-urls.js → dev-server/preview-urls.js} +0 -0
- /package/dist-node/src/{run-configs.js → dev-server/run-configs.js} +0 -0
- /package/dist-node/src/{attachments.js → files/attachments.js} +0 -0
- /package/dist-node/src/{icons.js → files/icons.js} +0 -0
- /package/dist-node/src/{staged-attachments.js → files/staged-attachments.js} +0 -0
- /package/dist-node/src/{fork-workspace.js → git/fork-workspace.js} +0 -0
- /package/dist-node/src/{merge.js → git/merge.js} +0 -0
- /package/dist-node/src/{pr.js → git/pr.js} +0 -0
- /package/dist-node/src/{nosleep.js → host/nosleep.js} +0 -0
- /package/dist-node/src/{relay-processes.js → host/relay-processes.js} +0 -0
- /package/dist-node/src/{ui-lease-watchdog.js → host/ui-lease-watchdog.js} +0 -0
- /package/dist-node/src/{wifi.js → host/wifi.js} +0 -0
- /package/dist-node/src/{orchestration-schema.generated.js → orchestration/persistence/schema.generated.js} +0 -0
- /package/dist-node/src/{workflow-effect-runner.js → orchestration/workflow/effect-runner.js} +0 -0
- /package/dist-node/src/{workflow-http.js → orchestration/workflow/http.js} +0 -0
- /package/dist-node/src/{workflow-machine.js → orchestration/workflow/machine.js} +0 -0
- /package/dist-node/src/{session-poller.js → reads/session-poller.js} +0 -0
- /package/dist-node/src/{context-breakdown.js → transcript/context-breakdown.js} +0 -0
- /package/dist-node/src/{chat-cursor.js → transcript/cursor.js} +0 -0
- /package/dist-node/src/{plan-usage.js → usage/plan-usage.js} +0 -0
- /package/dist-node/src/{tool-usage-service.js → usage/tool-usage-service.js} +0 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { mkdirSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
4
|
+
import { drizzle } from 'drizzle-orm/node-sqlite';
|
|
5
|
+
import { OrchestrationError, UnsupportedOrchestrationSchemaError } from "./errors.js";
|
|
6
|
+
import { ORCHESTRATION_BOOTSTRAP_SQL, ORCHESTRATION_SCHEMA_VERSION, ORCHESTRATION_TABLE_NAMES } from "./schema.js";
|
|
7
|
+
import { defaultScrub, isPromiseLike } from "./values.js";
|
|
8
|
+
const migrations = [ORCHESTRATION_BOOTSTRAP_SQL];
|
|
9
|
+
const sqliteRetryWait = new Int32Array(new SharedArrayBuffer(4));
|
|
10
|
+
/**
|
|
11
|
+
* Durable, relay-owned Workflow coordinator state. Every mutating method is a
|
|
12
|
+
* short synchronous `BEGIN IMMEDIATE` transaction. `idempotentMutation` callbacks
|
|
13
|
+
* must therefore be synchronous; domain methods called inside one join its transaction.
|
|
14
|
+
*/
|
|
15
|
+
export class PersistenceConnection {
|
|
16
|
+
db;
|
|
17
|
+
orm;
|
|
18
|
+
now;
|
|
19
|
+
processProbe;
|
|
20
|
+
scrubPublicText;
|
|
21
|
+
busyTimeoutMs;
|
|
22
|
+
transactionDepth = 0;
|
|
23
|
+
schemaVersion;
|
|
24
|
+
writable;
|
|
25
|
+
schemaWarning;
|
|
26
|
+
constructor(file, options = {}) {
|
|
27
|
+
if (file !== ':memory:')
|
|
28
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
29
|
+
this.now = options.now ?? Date.now;
|
|
30
|
+
// Without an exact process-identity probe, failure to prove death must fail closed.
|
|
31
|
+
this.processProbe = options.processProbe ?? (() => true);
|
|
32
|
+
this.scrubPublicText = options.scrubPublicText ?? defaultScrub;
|
|
33
|
+
this.busyTimeoutMs = Math.max(0, Math.floor(options.busyTimeoutMs ?? 5000));
|
|
34
|
+
this.db = new DatabaseSync(file);
|
|
35
|
+
this.orm = drizzle({ client: this.db });
|
|
36
|
+
this.db.exec(`PRAGMA busy_timeout = ${this.busyTimeoutMs}`);
|
|
37
|
+
this.db.exec('PRAGMA foreign_keys = ON');
|
|
38
|
+
let found;
|
|
39
|
+
try {
|
|
40
|
+
found = this.detectSchemaVersion();
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
this.reopenReadOnly(file);
|
|
44
|
+
this.schemaVersion = -1;
|
|
45
|
+
this.writable = false;
|
|
46
|
+
this.schemaWarning = error instanceof Error ? error.message : 'orchestration schema metadata is corrupt';
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (found > ORCHESTRATION_SCHEMA_VERSION) {
|
|
50
|
+
this.reopenReadOnly(file);
|
|
51
|
+
this.schemaVersion = found;
|
|
52
|
+
this.writable = false;
|
|
53
|
+
this.schemaWarning = `orchestration schema ${found} requires a newer relay`;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
// `busy_timeout` does not reliably wait for a concurrent journal-mode change.
|
|
57
|
+
// Relay processes can cold-start together, so retry only these initialization
|
|
58
|
+
// pragmas within the same bounded budget used by SQLite statements.
|
|
59
|
+
this.execInitializationPragma('PRAGMA journal_mode = WAL');
|
|
60
|
+
this.execInitializationPragma('PRAGMA synchronous = NORMAL');
|
|
61
|
+
for (let next = found + 1; next <= ORCHESTRATION_SCHEMA_VERSION; next++)
|
|
62
|
+
this.applyMigration(next);
|
|
63
|
+
let schemaProblem;
|
|
64
|
+
try {
|
|
65
|
+
schemaProblem = this.currentSchemaProblem();
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
69
|
+
schemaProblem = `orchestration schema could not be validated: ${detail}`;
|
|
70
|
+
}
|
|
71
|
+
if (schemaProblem) {
|
|
72
|
+
this.reopenReadOnly(file);
|
|
73
|
+
this.schemaVersion = ORCHESTRATION_SCHEMA_VERSION;
|
|
74
|
+
this.writable = false;
|
|
75
|
+
this.schemaWarning = schemaProblem;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this.schemaVersion = ORCHESTRATION_SCHEMA_VERSION;
|
|
79
|
+
this.writable = true;
|
|
80
|
+
this.schemaWarning = undefined;
|
|
81
|
+
}
|
|
82
|
+
close() {
|
|
83
|
+
this.db.close();
|
|
84
|
+
}
|
|
85
|
+
reopenReadOnly(file) {
|
|
86
|
+
if (file === ':memory:')
|
|
87
|
+
return;
|
|
88
|
+
this.db.close();
|
|
89
|
+
this.db = new DatabaseSync(file, { readOnly: true });
|
|
90
|
+
this.orm = drizzle({ client: this.db });
|
|
91
|
+
this.db.exec(`PRAGMA busy_timeout = ${this.busyTimeoutMs}`);
|
|
92
|
+
this.db.exec('PRAGMA foreign_keys = ON');
|
|
93
|
+
}
|
|
94
|
+
currentSchemaProblem() {
|
|
95
|
+
const rows = this.db
|
|
96
|
+
.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name IN (${ORCHESTRATION_TABLE_NAMES.map(() => '?').join(',')})`)
|
|
97
|
+
.all(...ORCHESTRATION_TABLE_NAMES);
|
|
98
|
+
const present = new Set(rows.map(row => row.name));
|
|
99
|
+
const missing = ORCHESTRATION_TABLE_NAMES.filter(table => !present.has(table));
|
|
100
|
+
if (missing.length > 0)
|
|
101
|
+
return `orchestration schema is missing: ${missing.join(', ')}`;
|
|
102
|
+
const mutex = this.db.prepare('SELECT COUNT(*) count FROM ui_mutex WHERE id = 1').get();
|
|
103
|
+
const quarantine = this.db.prepare('SELECT COUNT(*) count FROM ui_quarantine WHERE id = 1').get();
|
|
104
|
+
if (Number(mutex.count) !== 1 || Number(quarantine.count) !== 1) {
|
|
105
|
+
return 'orchestration schema is missing its singleton UI coordination rows';
|
|
106
|
+
}
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
execInitializationPragma(sql) {
|
|
110
|
+
const deadline = Date.now() + this.busyTimeoutMs;
|
|
111
|
+
for (;;) {
|
|
112
|
+
try {
|
|
113
|
+
this.db.exec(sql);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
const busy = error instanceof Error &&
|
|
118
|
+
(error.errcode === 5 || /database is (?:locked|busy)/i.test(error.message));
|
|
119
|
+
const remaining = deadline - Date.now();
|
|
120
|
+
if (!busy || remaining <= 0)
|
|
121
|
+
throw error;
|
|
122
|
+
Atomics.wait(sqliteRetryWait, 0, 0, Math.min(25, remaining));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
detectSchemaVersion() {
|
|
127
|
+
const table = this.db
|
|
128
|
+
.prepare("SELECT 1 present FROM sqlite_master WHERE type = 'table' AND name = 'orchestration_meta'")
|
|
129
|
+
.get();
|
|
130
|
+
if (!table) {
|
|
131
|
+
const stateTables = ORCHESTRATION_TABLE_NAMES.filter(name => name !== 'orchestration_meta');
|
|
132
|
+
const existing = this.db
|
|
133
|
+
.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name IN (${stateTables.map(() => '?').join(',')}) ORDER BY name`)
|
|
134
|
+
.all(...stateTables);
|
|
135
|
+
if (existing.length > 0) {
|
|
136
|
+
throw new OrchestrationError(`orchestration schema version metadata is missing (found ${existing.map(row => row.name).join(', ')})`);
|
|
137
|
+
}
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
const row = this.db.prepare("SELECT value FROM orchestration_meta WHERE key = 'schema_version'").get();
|
|
141
|
+
if (!row)
|
|
142
|
+
throw new OrchestrationError('orchestration schema version metadata is missing');
|
|
143
|
+
const version = Number(row.value);
|
|
144
|
+
if (!Number.isSafeInteger(version) || version < 0)
|
|
145
|
+
throw new OrchestrationError('invalid orchestration schema version');
|
|
146
|
+
return version;
|
|
147
|
+
}
|
|
148
|
+
applyMigration(version) {
|
|
149
|
+
const migration = migrations[version - 1];
|
|
150
|
+
if (!migration)
|
|
151
|
+
throw new OrchestrationError(`missing orchestration migration ${version}`);
|
|
152
|
+
this.db.exec('BEGIN IMMEDIATE');
|
|
153
|
+
try {
|
|
154
|
+
// Another relay may have migrated after this connection's initial read but
|
|
155
|
+
// before it acquired the write lock. Recheck under the lock and join it.
|
|
156
|
+
const current = this.detectSchemaVersion();
|
|
157
|
+
if (current >= version) {
|
|
158
|
+
this.db.exec('COMMIT');
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (current !== version - 1) {
|
|
162
|
+
throw new OrchestrationError(`cannot migrate orchestration schema ${current} to ${version}`);
|
|
163
|
+
}
|
|
164
|
+
this.db.exec(migration);
|
|
165
|
+
this.db
|
|
166
|
+
.prepare('INSERT OR REPLACE INTO orchestration_meta(key, value) VALUES (?, ?)')
|
|
167
|
+
.run('schema_version', String(version));
|
|
168
|
+
this.db.exec('COMMIT');
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
this.db.exec('ROLLBACK');
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
assertWritable() {
|
|
176
|
+
if (!this.writable && this.schemaVersion > ORCHESTRATION_SCHEMA_VERSION) {
|
|
177
|
+
throw new UnsupportedOrchestrationSchemaError(this.schemaVersion);
|
|
178
|
+
}
|
|
179
|
+
if (this.schemaWarning)
|
|
180
|
+
throw new OrchestrationError(this.schemaWarning);
|
|
181
|
+
if (!this.writable)
|
|
182
|
+
throw new OrchestrationError('orchestration database is read-only');
|
|
183
|
+
}
|
|
184
|
+
immediate(operation) {
|
|
185
|
+
this.assertWritable();
|
|
186
|
+
if (this.transactionDepth > 0)
|
|
187
|
+
return operation();
|
|
188
|
+
this.db.exec('BEGIN IMMEDIATE');
|
|
189
|
+
this.transactionDepth++;
|
|
190
|
+
try {
|
|
191
|
+
const result = operation();
|
|
192
|
+
if (isPromiseLike(result))
|
|
193
|
+
throw new OrchestrationError('orchestration transactions must be synchronous');
|
|
194
|
+
this.db.exec('COMMIT');
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
this.db.exec('ROLLBACK');
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
this.transactionDepth--;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { consumeWorkflowCapability, getWorkflowCapability, issueWorkflowCapability, revokeWorkflowCapabilities } from "./capabilities.js";
|
|
2
|
+
import { PersistenceConnection } from "./connection.js";
|
|
3
|
+
import { markWorkflowEffectReceiptLost, reconcileAbandonedWorkflowEffect, recordLateWorkflowEffect, replayAmbiguousWorkflowEffect, retryWorkflowEffect } from "./effect-recovery.js";
|
|
4
|
+
import { markWorkflowEffectAmbiguous, markWorkflowEffectCancelled, markWorkflowEffectCommitted, markWorkflowEffectDispatched, markWorkflowEffectFailed, markWorkflowEffectFailedBeforeMayExecute, markWorkflowEffectMayExecute } from "./effect-transitions.js";
|
|
5
|
+
import { claimPreparedWorkflowEffect, markWorkflowEffectSatisfiedWithoutDispatch, prepareWorkflowEffect } from "./effects.js";
|
|
6
|
+
import { listWorkflowEvents } from "./events.js";
|
|
7
|
+
import { getIdempotentMutation, idempotentMutation } from "./idempotency.js";
|
|
8
|
+
import { createWorkflowJobAttempt, recordLateWorkflowChildResult, updateWorkflowJobAttempt } from "./job-attempts.js";
|
|
9
|
+
import { activateWorkflowJob, claimNextWorkflowJob, createWorkflowJob, reconcileAbandonedWorkflowJobClaim, updateWorkflowJob } from "./jobs.js";
|
|
10
|
+
import { compactTerminalRuns, getWorkflowProjection, listLatestWorkflowProjectionsForWorkspaces, listWorkflowProjections, listWorkflowRunIdsNeedingWake } from "./projections.js";
|
|
11
|
+
import { activateUiQuarantine, clearUiQuarantine, getUiQuarantine } from "./quarantine.js";
|
|
12
|
+
import { findActiveWorkflowByRoot, getWorkflowEffect, getWorkflowJob, getWorkflowRun, listWorkflowEffectAttempts, listWorkflowEffects, listWorkflowJobAttempts, listWorkflowJobs } from "./records.js";
|
|
13
|
+
import { findIncompatibleRelayInstances, heartbeatRelayInstance, listRelayInstances, registerRelayInstance } from "./relays.js";
|
|
14
|
+
import { cancelWorkflowRun, createWorkflowRun, transitionWorkflowRun } from "./runs.js";
|
|
15
|
+
import { acquireUiLease, createSharedUiLeaseProvider, getUiLeaseOwner, markUiLeaseMayExecute, releaseUiLease, renewUiLease } from "./ui-lease.js";
|
|
16
|
+
export * from "./errors.js";
|
|
17
|
+
export { canonicalRequestHash, canonicalRequestJson, hashCapabilityToken } from "./idempotency.js";
|
|
18
|
+
export { ORCHESTRATION_SCHEMA_VERSION } from "./schema.js";
|
|
19
|
+
export * from "./types.js";
|
|
20
|
+
/** One connection owns every nested transaction; domain operations never open a second handle. */
|
|
21
|
+
export class OrchestrationDb {
|
|
22
|
+
connection;
|
|
23
|
+
constructor(file, options = {}) {
|
|
24
|
+
this.connection = new PersistenceConnection(file, options);
|
|
25
|
+
}
|
|
26
|
+
get schemaVersion() {
|
|
27
|
+
return this.connection.schemaVersion;
|
|
28
|
+
}
|
|
29
|
+
get writable() {
|
|
30
|
+
return this.connection.writable;
|
|
31
|
+
}
|
|
32
|
+
get schemaWarning() {
|
|
33
|
+
return this.connection.schemaWarning;
|
|
34
|
+
}
|
|
35
|
+
close() {
|
|
36
|
+
this.connection.close();
|
|
37
|
+
}
|
|
38
|
+
idempotentMutation(operation, clientId, request, mutate, link = {}) {
|
|
39
|
+
return idempotentMutation(this.connection, operation, clientId, request, mutate, link);
|
|
40
|
+
}
|
|
41
|
+
getIdempotentMutation(operation, clientId, request) {
|
|
42
|
+
return getIdempotentMutation(this.connection, operation, clientId, request);
|
|
43
|
+
}
|
|
44
|
+
createWorkflowRun = (...args) => createWorkflowRun(this.connection, ...args);
|
|
45
|
+
getWorkflowRun = (...args) => getWorkflowRun(this.connection, ...args);
|
|
46
|
+
findActiveWorkflowByRoot = (...args) => findActiveWorkflowByRoot(this.connection, ...args);
|
|
47
|
+
transitionWorkflowRun = (...args) => transitionWorkflowRun(this.connection, ...args);
|
|
48
|
+
cancelWorkflowRun = (...args) => cancelWorkflowRun(this.connection, ...args);
|
|
49
|
+
createWorkflowJob = (...args) => createWorkflowJob(this.connection, ...args);
|
|
50
|
+
activateWorkflowJob = (...args) => activateWorkflowJob(this.connection, ...args);
|
|
51
|
+
getWorkflowJob = (...args) => getWorkflowJob(this.connection, ...args);
|
|
52
|
+
listWorkflowJobs = (...args) => listWorkflowJobs(this.connection, ...args);
|
|
53
|
+
claimNextWorkflowJob = (...args) => claimNextWorkflowJob(this.connection, ...args);
|
|
54
|
+
reconcileAbandonedWorkflowJobClaim = (...args) => reconcileAbandonedWorkflowJobClaim(this.connection, ...args);
|
|
55
|
+
updateWorkflowJob = (...args) => updateWorkflowJob(this.connection, ...args);
|
|
56
|
+
createWorkflowJobAttempt = (...args) => createWorkflowJobAttempt(this.connection, ...args);
|
|
57
|
+
listWorkflowJobAttempts = (...args) => listWorkflowJobAttempts(this.connection, ...args);
|
|
58
|
+
updateWorkflowJobAttempt = (...args) => updateWorkflowJobAttempt(this.connection, ...args);
|
|
59
|
+
recordLateWorkflowChildResult = (...args) => recordLateWorkflowChildResult(this.connection, ...args);
|
|
60
|
+
prepareWorkflowEffect = (...args) => prepareWorkflowEffect(this.connection, ...args);
|
|
61
|
+
getWorkflowEffect = (...args) => getWorkflowEffect(this.connection, ...args);
|
|
62
|
+
listWorkflowEffects = (...args) => listWorkflowEffects(this.connection, ...args);
|
|
63
|
+
markWorkflowEffectSatisfiedWithoutDispatch = (...args) => markWorkflowEffectSatisfiedWithoutDispatch(this.connection, ...args);
|
|
64
|
+
claimPreparedWorkflowEffect = (...args) => claimPreparedWorkflowEffect(this.connection, ...args);
|
|
65
|
+
markWorkflowEffectDispatched = (...args) => markWorkflowEffectDispatched(this.connection, ...args);
|
|
66
|
+
markWorkflowEffectMayExecute = (...args) => markWorkflowEffectMayExecute(this.connection, ...args);
|
|
67
|
+
markWorkflowEffectCommitted = (...args) => markWorkflowEffectCommitted(this.connection, ...args);
|
|
68
|
+
markWorkflowEffectFailed = (...args) => markWorkflowEffectFailed(this.connection, ...args);
|
|
69
|
+
markWorkflowEffectFailedBeforeMayExecute = (...args) => markWorkflowEffectFailedBeforeMayExecute(this.connection, ...args);
|
|
70
|
+
markWorkflowEffectAmbiguous = (...args) => markWorkflowEffectAmbiguous(this.connection, ...args);
|
|
71
|
+
markWorkflowEffectReceiptLost = (...args) => markWorkflowEffectReceiptLost(this.connection, ...args);
|
|
72
|
+
recordLateWorkflowEffect = (...args) => recordLateWorkflowEffect(this.connection, ...args);
|
|
73
|
+
markWorkflowEffectCancelled = (...args) => markWorkflowEffectCancelled(this.connection, ...args);
|
|
74
|
+
reconcileAbandonedWorkflowEffect = (...args) => reconcileAbandonedWorkflowEffect(this.connection, ...args);
|
|
75
|
+
retryWorkflowEffect = (...args) => retryWorkflowEffect(this.connection, ...args);
|
|
76
|
+
replayAmbiguousWorkflowEffect = (...args) => replayAmbiguousWorkflowEffect(this.connection, ...args);
|
|
77
|
+
listWorkflowEffectAttempts = (...args) => listWorkflowEffectAttempts(this.connection, ...args);
|
|
78
|
+
getWorkflowCapability = (...args) => getWorkflowCapability(this.connection, ...args);
|
|
79
|
+
issueWorkflowCapability = (...args) => issueWorkflowCapability(this.connection, ...args);
|
|
80
|
+
consumeWorkflowCapability = (...args) => consumeWorkflowCapability(this.connection, ...args);
|
|
81
|
+
revokeWorkflowCapabilities = (...args) => revokeWorkflowCapabilities(this.connection, ...args);
|
|
82
|
+
listWorkflowEvents = (...args) => listWorkflowEvents(this.connection, ...args);
|
|
83
|
+
registerRelayInstance = (...args) => registerRelayInstance(this.connection, ...args);
|
|
84
|
+
heartbeatRelayInstance = (...args) => heartbeatRelayInstance(this.connection, ...args);
|
|
85
|
+
listRelayInstances = (...args) => listRelayInstances(this.connection, ...args);
|
|
86
|
+
findIncompatibleRelayInstances = (...args) => findIncompatibleRelayInstances(this.connection, ...args);
|
|
87
|
+
acquireUiLease = (...args) => acquireUiLease(this.connection, ...args);
|
|
88
|
+
markUiLeaseMayExecute = (...args) => markUiLeaseMayExecute(this.connection, ...args);
|
|
89
|
+
renewUiLease = (...args) => renewUiLease(this.connection, ...args);
|
|
90
|
+
releaseUiLease = (...args) => releaseUiLease(this.connection, ...args);
|
|
91
|
+
getUiLeaseOwner = (...args) => getUiLeaseOwner(this.connection, ...args);
|
|
92
|
+
createSharedUiLeaseProvider = (...args) => createSharedUiLeaseProvider(this.connection, ...args);
|
|
93
|
+
activateUiQuarantine = (...args) => activateUiQuarantine(this.connection, ...args);
|
|
94
|
+
clearUiQuarantine = (...args) => clearUiQuarantine(this.connection, ...args);
|
|
95
|
+
getUiQuarantine = (...args) => getUiQuarantine(this.connection, ...args);
|
|
96
|
+
getWorkflowProjection = (...args) => getWorkflowProjection(this.connection, ...args);
|
|
97
|
+
listWorkflowProjections = (...args) => listWorkflowProjections(this.connection, ...args);
|
|
98
|
+
listLatestWorkflowProjectionsForWorkspaces = (...args) => listLatestWorkflowProjectionsForWorkspaces(this.connection, ...args);
|
|
99
|
+
listWorkflowRunIdsNeedingWake = (...args) => listWorkflowRunIdsNeedingWake(this.connection, ...args);
|
|
100
|
+
compactTerminalRuns = (...args) => compactTerminalRuns(this.connection, ...args);
|
|
101
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { isTerminalWorkflowPhase } from "../workflow/machine.js";
|
|
2
|
+
import { WorkflowTransitionError } from "./errors.js";
|
|
3
|
+
import { appendEvent, findEvent, touchRun } from "./events.js";
|
|
4
|
+
import { canonicalOptional } from "./idempotency.js";
|
|
5
|
+
import { requireEffect, requireEffectByAction, requireRun } from "./records.js";
|
|
6
|
+
import { asObject, json, optionalJson, probeAlive, sameOptionalProcess, sameOwner, TERMINAL_EFFECT_STATES } from "./values.js";
|
|
7
|
+
export function markWorkflowEffectReceiptLost(context, input) {
|
|
8
|
+
return context.immediate(() => {
|
|
9
|
+
const effect = requireEffectByAction(context, input.runId, input.actionId);
|
|
10
|
+
const receipt = asObject(input.expectedReceipt);
|
|
11
|
+
if (receipt.kind !== 'outbox' || typeof receipt.id !== 'string') {
|
|
12
|
+
throw new WorkflowTransitionError('only a tagged outbox receipt can become lost');
|
|
13
|
+
}
|
|
14
|
+
if (canonicalOptional(effect.receipt) !== canonicalOptional(input.expectedReceipt)) {
|
|
15
|
+
throw new WorkflowTransitionError(`effect ${input.actionId} receipt changed before loss reconciliation`);
|
|
16
|
+
}
|
|
17
|
+
if (effect.state === 'ambiguous' && effect.errorCode === input.errorCode)
|
|
18
|
+
return effect;
|
|
19
|
+
if (effect.state !== 'committed') {
|
|
20
|
+
throw new WorkflowTransitionError(`effect ${input.actionId} is ${effect.state}, expected committed`);
|
|
21
|
+
}
|
|
22
|
+
const at = context.now();
|
|
23
|
+
context.db
|
|
24
|
+
.prepare(`UPDATE workflow_effects SET state = 'ambiguous', error_code = ?, error_message = ?,
|
|
25
|
+
updated_at = ?, terminal_at = ? WHERE id = ? AND state = 'committed'`)
|
|
26
|
+
.run(input.errorCode, input.errorMessage, at, at, effect.id);
|
|
27
|
+
if (effect.attemptCount > 0) {
|
|
28
|
+
context.db
|
|
29
|
+
.prepare(`UPDATE workflow_effect_attempts SET state = 'ambiguous', evidence_json = ?, error_code = ?,
|
|
30
|
+
error_message = ?, updated_at = ?, terminal_at = ? WHERE effect_id = ? AND attempt_number = ?`)
|
|
31
|
+
.run(optionalJson(input.evidence), input.errorCode, input.errorMessage, at, at, effect.id, effect.attemptCount);
|
|
32
|
+
}
|
|
33
|
+
touchRun(context, input.runId, at);
|
|
34
|
+
appendEvent(context, input.runId, input.eventKey ?? `effect_receipt_lost:${input.actionId}:${String(receipt.id)}`, 'workflow_effect_receipt_lost', { effectId: effect.id, actionId: input.actionId, errorCode: input.errorCode });
|
|
35
|
+
return requireEffect(context, effect.id);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/** Record a post-cancellation receipt without reopening the run or effect. */
|
|
39
|
+
export function recordLateWorkflowEffect(context, input) {
|
|
40
|
+
return context.immediate(() => {
|
|
41
|
+
const run = requireRun(context, input.runId);
|
|
42
|
+
if (run.phase !== 'cancelled') {
|
|
43
|
+
throw new WorkflowTransitionError(`Workflow ${run.id} is not cancelled`);
|
|
44
|
+
}
|
|
45
|
+
const effect = requireEffectByAction(context, run.id, input.actionId);
|
|
46
|
+
const prior = findEvent(context, run.id, input.eventKey);
|
|
47
|
+
if (prior) {
|
|
48
|
+
if (prior.type !== 'late_effect' || canonicalOptional(effect.receipt) !== canonicalOptional(input.receipt)) {
|
|
49
|
+
throw new WorkflowTransitionError(`late effect event ${input.eventKey} conflicts with existing evidence`);
|
|
50
|
+
}
|
|
51
|
+
return effect;
|
|
52
|
+
}
|
|
53
|
+
const at = context.now();
|
|
54
|
+
const settled = effect.state === 'dispatched' || effect.state === 'ambiguous';
|
|
55
|
+
context.db
|
|
56
|
+
.prepare(`UPDATE workflow_effects SET state = ?, receipt_json = ?, error_code = ?, error_message = ?,
|
|
57
|
+
updated_at = ?, terminal_at = COALESCE(terminal_at, ?) WHERE id = ?`)
|
|
58
|
+
.run(settled ? 'committed' : effect.state, json(input.receipt), settled ? null : (effect.errorCode ?? null), settled ? null : (effect.errorMessage ?? null), at, settled ? at : (effect.terminalAt ?? null), effect.id);
|
|
59
|
+
if (effect.attemptCount > 0) {
|
|
60
|
+
context.db
|
|
61
|
+
.prepare(`UPDATE workflow_effect_attempts SET state = CASE WHEN state IN ('dispatched', 'ambiguous') THEN 'committed' ELSE state END,
|
|
62
|
+
receipt_json = ?, error_code = CASE WHEN state IN ('dispatched', 'ambiguous') THEN NULL ELSE error_code END,
|
|
63
|
+
error_message = CASE WHEN state IN ('dispatched', 'ambiguous') THEN NULL ELSE error_message END,
|
|
64
|
+
updated_at = ?, terminal_at = CASE WHEN state IN ('dispatched', 'ambiguous') THEN COALESCE(terminal_at, ?) ELSE terminal_at END
|
|
65
|
+
WHERE effect_id = ? AND attempt_number = ?`)
|
|
66
|
+
.run(json(input.receipt), at, at, effect.id, effect.attemptCount);
|
|
67
|
+
}
|
|
68
|
+
const receipt = asObject(input.receipt);
|
|
69
|
+
const deliveredMessage = receipt.kind === 'message' &&
|
|
70
|
+
typeof receipt.id === 'string' &&
|
|
71
|
+
Number.isSafeInteger(receipt.rowid) &&
|
|
72
|
+
(receipt.turnId === null || typeof receipt.turnId === 'string');
|
|
73
|
+
if (effect.jobId && deliveredMessage && (effect.kind === 'send_task' || effect.kind === 'return_baton')) {
|
|
74
|
+
const receiptColumn = effect.kind === 'send_task' ? 'task_receipt_json' : 'baton_receipt_json';
|
|
75
|
+
context.db
|
|
76
|
+
.prepare(`UPDATE workflow_jobs SET ${receiptColumn} = ?, updated_at = ? WHERE id = ? AND run_id = ?`)
|
|
77
|
+
.run(json(input.receipt), at, effect.jobId, run.id);
|
|
78
|
+
}
|
|
79
|
+
// A positive receipt is stronger than an earlier ambiguity. Clear only the
|
|
80
|
+
// hold for this exact effect inside the same transaction; a newer unrelated
|
|
81
|
+
// quarantine can never be erased by a late callback.
|
|
82
|
+
context.db
|
|
83
|
+
.prepare(`UPDATE ui_quarantine SET active = 0, cleared_at = ?, cleared_by = ?
|
|
84
|
+
WHERE id = 1 AND active = 1 AND (
|
|
85
|
+
effect_id = ? OR (effect_id IS NULL AND action_id IN (?, ?))
|
|
86
|
+
)`)
|
|
87
|
+
.run(at, `late-effect:${input.eventKey}`, effect.id, effect.id, effect.actionId);
|
|
88
|
+
touchRun(context, run.id, at);
|
|
89
|
+
appendEvent(context, run.id, input.eventKey, 'late_effect', {
|
|
90
|
+
effectId: effect.id,
|
|
91
|
+
actionId: effect.actionId,
|
|
92
|
+
receipt: input.receipt
|
|
93
|
+
});
|
|
94
|
+
return requireEffect(context, effect.id);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Reconcile an orphan only after the caller checked for a positive receipt.
|
|
99
|
+
* A dead owner before `mayExecute` is safely reset; every later boundary is ambiguous.
|
|
100
|
+
*/
|
|
101
|
+
export function reconcileAbandonedWorkflowEffect(context, input) {
|
|
102
|
+
const observed = requireEffectByAction(context, input.runId, input.actionId);
|
|
103
|
+
if (!observed.owner)
|
|
104
|
+
return { status: 'unowned', effect: observed };
|
|
105
|
+
if (TERMINAL_EFFECT_STATES.includes(observed.state))
|
|
106
|
+
return { status: 'terminal', effect: observed };
|
|
107
|
+
const probe = input.processProbe ?? context.processProbe;
|
|
108
|
+
if (probeAlive(observed.owner, probe))
|
|
109
|
+
return { status: 'owner_alive', effect: observed };
|
|
110
|
+
if (observed.externalProcess && probeAlive(observed.externalProcess, probe)) {
|
|
111
|
+
return { status: 'external_process_alive', effect: observed };
|
|
112
|
+
}
|
|
113
|
+
return context.immediate(() => {
|
|
114
|
+
const current = requireEffectByAction(context, input.runId, input.actionId);
|
|
115
|
+
const run = requireRun(context, input.runId);
|
|
116
|
+
if (!current.owner ||
|
|
117
|
+
!sameOwner(current.owner, observed.owner) ||
|
|
118
|
+
current.state !== observed.state ||
|
|
119
|
+
current.attemptCount !== observed.attemptCount ||
|
|
120
|
+
current.mayExecute !== observed.mayExecute ||
|
|
121
|
+
!sameOptionalProcess(current.externalProcess, observed.externalProcess)) {
|
|
122
|
+
return { status: 'changed', effect: current };
|
|
123
|
+
}
|
|
124
|
+
const safelyPrepared = current.state === 'prepared' || (current.state === 'dispatched' && !current.mayExecute);
|
|
125
|
+
const terminalRun = isTerminalWorkflowPhase(run.phase);
|
|
126
|
+
const at = context.now();
|
|
127
|
+
if (safelyPrepared) {
|
|
128
|
+
context.db
|
|
129
|
+
.prepare(`UPDATE workflow_effects SET state = ?, owner_instance_id = NULL, owner_pid = NULL,
|
|
130
|
+
owner_process_started_at = NULL, owner_protocol_version = NULL, launch_nonce = NULL,
|
|
131
|
+
external_pid = NULL, external_process_started_at = NULL, external_process_group = NULL,
|
|
132
|
+
may_execute = 0, error_code = NULL, error_message = NULL, updated_at = ?, terminal_at = ?
|
|
133
|
+
WHERE id = ?`)
|
|
134
|
+
.run(terminalRun ? 'cancelled' : 'prepared', at, terminalRun ? at : null, current.id);
|
|
135
|
+
if (current.attemptCount > 0) {
|
|
136
|
+
context.db
|
|
137
|
+
.prepare(`UPDATE workflow_effect_attempts SET state = 'cancelled', evidence_json = ?, updated_at = ?, terminal_at = ?
|
|
138
|
+
WHERE effect_id = ? AND attempt_number = ?`)
|
|
139
|
+
.run(json({ reason: 'owner_died_before_may_execute' }), at, at, current.id, current.attemptCount);
|
|
140
|
+
}
|
|
141
|
+
touchRun(context, current.runId, at);
|
|
142
|
+
appendEvent(context, current.runId, input.eventKey, terminalRun ? 'workflow_effect_cancelled_after_owner_exit' : 'workflow_effect_safely_recovered', {
|
|
143
|
+
effectId: current.id,
|
|
144
|
+
actionId: current.actionId,
|
|
145
|
+
attemptNumber: current.attemptCount
|
|
146
|
+
});
|
|
147
|
+
return {
|
|
148
|
+
status: terminalRun ? 'terminal' : 'safely_prepared',
|
|
149
|
+
effect: requireEffect(context, current.id)
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
context.db
|
|
153
|
+
.prepare(`UPDATE workflow_effects SET state = 'ambiguous', error_code = 'ambiguous_effect',
|
|
154
|
+
error_message = 'The UI action may have executed before its relay owner exited.',
|
|
155
|
+
updated_at = ?, terminal_at = ? WHERE id = ?`)
|
|
156
|
+
.run(at, at, current.id);
|
|
157
|
+
if (current.attemptCount > 0) {
|
|
158
|
+
context.db
|
|
159
|
+
.prepare(`UPDATE workflow_effect_attempts SET state = 'ambiguous', error_code = 'ambiguous_effect',
|
|
160
|
+
error_message = 'The UI action may have executed before its relay owner exited.',
|
|
161
|
+
updated_at = ?, terminal_at = ? WHERE effect_id = ? AND attempt_number = ?`)
|
|
162
|
+
.run(at, at, current.id, current.attemptCount);
|
|
163
|
+
}
|
|
164
|
+
touchRun(context, current.runId, at);
|
|
165
|
+
appendEvent(context, current.runId, input.eventKey, 'workflow_effect_abandoned_ambiguous', {
|
|
166
|
+
effectId: current.id,
|
|
167
|
+
actionId: current.actionId,
|
|
168
|
+
attemptNumber: current.attemptCount
|
|
169
|
+
});
|
|
170
|
+
return { status: 'ambiguous', effect: requireEffect(context, current.id) };
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
export function retryWorkflowEffect(context, runId, actionId, eventKey) {
|
|
174
|
+
return context.immediate(() => {
|
|
175
|
+
const run = requireRun(context, runId);
|
|
176
|
+
if (isTerminalWorkflowPhase(run.phase)) {
|
|
177
|
+
throw new WorkflowTransitionError(`terminal Workflow ${run.id} cannot retry effects`);
|
|
178
|
+
}
|
|
179
|
+
const effect = requireEffectByAction(context, runId, actionId);
|
|
180
|
+
if (effect.state !== 'failed')
|
|
181
|
+
throw new WorkflowTransitionError(`effect ${actionId} is not deterministically failed`);
|
|
182
|
+
const at = context.now();
|
|
183
|
+
context.db
|
|
184
|
+
.prepare(`UPDATE workflow_effects SET state = 'prepared', owner_instance_id = NULL, owner_pid = NULL,
|
|
185
|
+
owner_process_started_at = NULL, owner_protocol_version = NULL, launch_nonce = NULL,
|
|
186
|
+
external_pid = NULL, external_process_started_at = NULL, external_process_group = NULL,
|
|
187
|
+
may_execute = 0, error_code = NULL, error_message = NULL, updated_at = ?, terminal_at = NULL
|
|
188
|
+
WHERE id = ?`)
|
|
189
|
+
.run(at, effect.id);
|
|
190
|
+
touchRun(context, runId, at);
|
|
191
|
+
appendEvent(context, runId, eventKey, 'workflow_effect_retry_prepared', {
|
|
192
|
+
effectId: effect.id,
|
|
193
|
+
actionId,
|
|
194
|
+
nextAttempt: effect.attemptCount + 1
|
|
195
|
+
});
|
|
196
|
+
return requireEffect(context, effect.id);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/** Used only behind the explicit, idempotent phone confirmation for duplicate risk. */
|
|
200
|
+
export function replayAmbiguousWorkflowEffect(context, runId, actionId, eventKey) {
|
|
201
|
+
return context.immediate(() => {
|
|
202
|
+
const run = requireRun(context, runId);
|
|
203
|
+
if (isTerminalWorkflowPhase(run.phase)) {
|
|
204
|
+
throw new WorkflowTransitionError(`terminal Workflow ${run.id} cannot replay effects`);
|
|
205
|
+
}
|
|
206
|
+
const effect = requireEffectByAction(context, runId, actionId);
|
|
207
|
+
if (effect.state !== 'ambiguous') {
|
|
208
|
+
throw new WorkflowTransitionError(`effect ${actionId} is not ambiguous`);
|
|
209
|
+
}
|
|
210
|
+
const at = context.now();
|
|
211
|
+
context.db
|
|
212
|
+
.prepare(`UPDATE workflow_effects SET state = 'prepared', owner_instance_id = NULL, owner_pid = NULL,
|
|
213
|
+
owner_process_started_at = NULL, owner_protocol_version = NULL, launch_nonce = NULL,
|
|
214
|
+
external_pid = NULL, external_process_started_at = NULL, external_process_group = NULL,
|
|
215
|
+
may_execute = 0, receipt_json = NULL, error_code = NULL, error_message = NULL,
|
|
216
|
+
updated_at = ?, terminal_at = NULL WHERE id = ?`)
|
|
217
|
+
.run(at, effect.id);
|
|
218
|
+
touchRun(context, runId, at);
|
|
219
|
+
appendEvent(context, runId, eventKey, 'workflow_effect_risky_replay_prepared', {
|
|
220
|
+
effectId: effect.id,
|
|
221
|
+
actionId,
|
|
222
|
+
previousAttempt: effect.attemptCount,
|
|
223
|
+
nextAttempt: effect.attemptCount + 1
|
|
224
|
+
});
|
|
225
|
+
return requireEffect(context, effect.id);
|
|
226
|
+
});
|
|
227
|
+
}
|