@h-rig/run-worker 0.0.6-alpha.157 → 0.0.6-alpha.159
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/dist/src/autohost.d.ts +8 -10
- package/dist/src/autohost.js +683 -95
- package/dist/src/constants.d.ts +0 -1
- package/dist/src/constants.js +0 -2
- package/dist/src/extension.js +683 -95
- package/dist/src/host-kernel.d.ts +22 -0
- package/dist/src/host-kernel.js +78 -0
- package/dist/src/host.d.ts +2 -0
- package/dist/src/host.js +419 -0
- package/dist/src/index.d.ts +0 -6
- package/dist/src/index.js +1913 -133
- package/dist/src/local-run-changes.d.ts +3 -0
- package/dist/src/local-run-changes.js +65 -0
- package/dist/src/notifications.js +13 -5
- package/dist/src/notify-cap.d.ts +11 -0
- package/dist/src/notify-cap.js +13 -0
- package/dist/src/panel-plugin.js +3 -7
- package/dist/src/plugin.d.ts +0 -11
- package/dist/src/plugin.js +1910 -101
- package/dist/src/{runs → read-model-backend}/control.d.ts +0 -1
- package/dist/src/{runs → read-model-backend}/control.js +361 -38
- package/dist/src/{runs → read-model-backend}/diagnostics.d.ts +0 -1
- package/dist/src/{runs → read-model-backend}/diagnostics.js +1 -3
- package/dist/src/{runs → read-model-backend}/guard.js +2 -3
- package/dist/src/{runs → read-model-backend}/inbox.js +366 -36
- package/dist/src/{runs → read-model-backend}/index.js +552 -223
- package/dist/src/{runs → read-model-backend}/inspect.d.ts +0 -1
- package/dist/src/{runs → read-model-backend}/inspect.js +350 -34
- package/dist/src/{runs → read-model-backend}/projection.d.ts +21 -7
- package/dist/src/{runs → read-model-backend}/projection.js +349 -31
- package/dist/src/{runs → read-model-backend}/run-status.d.ts +6 -3
- package/dist/src/{runs → read-model-backend}/run-status.js +53 -33
- package/dist/src/{runs → read-model-backend}/stats.d.ts +0 -1
- package/dist/src/{runs → read-model-backend}/stats.js +373 -58
- package/dist/src/read-model-service.d.ts +2 -0
- package/dist/src/read-model-service.js +1433 -0
- package/dist/src/session-journal.d.ts +60 -0
- package/dist/src/session-journal.js +471 -0
- package/dist/src/stall.d.ts +8 -3
- package/dist/src/stall.js +0 -1
- package/dist/src/utils.js +282 -3
- package/package.json +9 -12
- package/dist/src/journal.d.ts +0 -33
- package/dist/src/journal.js +0 -31
- /package/dist/src/{runs → read-model-backend}/guard.d.ts +0 -0
- /package/dist/src/{runs → read-model-backend}/inbox.d.ts +0 -0
- /package/dist/src/{runs → read-model-backend}/index.d.ts +0 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { JournalCapability, TransportCapability } from "@rig/contracts";
|
|
2
|
+
import type { CapabilityProviderPlugin } from "@rig/kernel-seed/plugin-abi";
|
|
3
|
+
import { type DefaultKernelBootRecord } from "@rig/kernel-seed/boot-default";
|
|
4
|
+
export type RunWorkerHydrationResult = {
|
|
5
|
+
readonly projectRoot: string;
|
|
6
|
+
readonly dotenvLoaded: boolean;
|
|
7
|
+
readonly configLoaded: boolean;
|
|
8
|
+
readonly errors: readonly string[];
|
|
9
|
+
};
|
|
10
|
+
export type RunWorkerHydrateProjectEnvOptions = {
|
|
11
|
+
readonly env?: NodeJS.ProcessEnv;
|
|
12
|
+
readonly dotenvPath?: string;
|
|
13
|
+
};
|
|
14
|
+
export type AdoptRunWorkerKernelOptions = RunWorkerHydrateProjectEnvOptions & {
|
|
15
|
+
readonly entrypoint?: string;
|
|
16
|
+
readonly journal?: JournalCapability;
|
|
17
|
+
readonly transport?: TransportCapability;
|
|
18
|
+
readonly hydrateEnv?: boolean;
|
|
19
|
+
};
|
|
20
|
+
export declare function createRunWorkerPlacementTransportPlugin(transport: TransportCapability): CapabilityProviderPlugin;
|
|
21
|
+
export declare function hydrateRunWorkerProjectEnv(projectRoot: string, options?: RunWorkerHydrateProjectEnvOptions): Promise<RunWorkerHydrationResult>;
|
|
22
|
+
export declare function adoptRunWorkerKernel(root?: string, options?: AdoptRunWorkerKernelOptions): Promise<DefaultKernelBootRecord>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/run-worker/src/host-kernel.ts
|
|
3
|
+
import { existsSync, readFileSync } from "fs";
|
|
4
|
+
import { resolve } from "path";
|
|
5
|
+
import { applyConfigEnv } from "@rig/core/config-env";
|
|
6
|
+
import { loadConfig } from "@rig/core/load-config";
|
|
7
|
+
import { bootDefaultKernelIntoProcess } from "@rig/kernel-seed/boot-default";
|
|
8
|
+
import { createPlacementTransportPlugin } from "@rig/kernel-seed/default-kernel";
|
|
9
|
+
function unquoteDotenvValue(value) {
|
|
10
|
+
const trimmed = value.trim();
|
|
11
|
+
if (trimmed.length >= 2 && (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
12
|
+
return trimmed.slice(1, -1);
|
|
13
|
+
}
|
|
14
|
+
return trimmed;
|
|
15
|
+
}
|
|
16
|
+
function hydrateDotenv(path, env) {
|
|
17
|
+
if (!existsSync(path))
|
|
18
|
+
return false;
|
|
19
|
+
const source = readFileSync(path, "utf8");
|
|
20
|
+
for (const rawLine of source.split(/\r?\n/)) {
|
|
21
|
+
const line = rawLine.trim();
|
|
22
|
+
if (!line || line.startsWith("#"))
|
|
23
|
+
continue;
|
|
24
|
+
const assignment = line.startsWith("export ") ? line.slice("export ".length).trim() : line;
|
|
25
|
+
const equals = assignment.indexOf("=");
|
|
26
|
+
if (equals <= 0)
|
|
27
|
+
continue;
|
|
28
|
+
const key = assignment.slice(0, equals).trim();
|
|
29
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key) || env[key])
|
|
30
|
+
continue;
|
|
31
|
+
env[key] = unquoteDotenvValue(assignment.slice(equals + 1));
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
function createRunWorkerPlacementTransportPlugin(transport) {
|
|
36
|
+
return createPlacementTransportPlugin(transport);
|
|
37
|
+
}
|
|
38
|
+
async function hydrateRunWorkerProjectEnv(projectRoot, options = {}) {
|
|
39
|
+
const normalizedRoot = resolve(projectRoot);
|
|
40
|
+
const env = options.env ?? process.env;
|
|
41
|
+
const errors = [];
|
|
42
|
+
let dotenvLoaded = false;
|
|
43
|
+
let configLoaded = false;
|
|
44
|
+
try {
|
|
45
|
+
dotenvLoaded = hydrateDotenv(resolve(normalizedRoot, options.dotenvPath ?? ".env"), env);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
errors.push(`dotenv: ${error instanceof Error ? error.message : String(error)}`);
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
applyConfigEnv(await loadConfig(normalizedRoot), env);
|
|
51
|
+
configLoaded = true;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
54
|
+
if (!message.includes("no rig.config"))
|
|
55
|
+
errors.push(`config: ${message}`);
|
|
56
|
+
}
|
|
57
|
+
return { projectRoot: normalizedRoot, dotenvLoaded, configLoaded, errors };
|
|
58
|
+
}
|
|
59
|
+
async function adoptRunWorkerKernel(root, options = {}) {
|
|
60
|
+
if (options.hydrateEnv === true && root !== undefined) {
|
|
61
|
+
await hydrateRunWorkerProjectEnv(root, options);
|
|
62
|
+
}
|
|
63
|
+
const bootInput = {
|
|
64
|
+
...options.entrypoint !== undefined ? { entrypoint: options.entrypoint } : {},
|
|
65
|
+
...options.journal !== undefined ? { journal: options.journal } : {}
|
|
66
|
+
};
|
|
67
|
+
if (options.transport === undefined)
|
|
68
|
+
return await bootDefaultKernelIntoProcess(bootInput);
|
|
69
|
+
return await bootDefaultKernelIntoProcess({
|
|
70
|
+
...bootInput,
|
|
71
|
+
extraPlugins: [createRunWorkerPlacementTransportPlugin(options.transport)]
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
export {
|
|
75
|
+
hydrateRunWorkerProjectEnv,
|
|
76
|
+
createRunWorkerPlacementTransportPlugin,
|
|
77
|
+
adoptRunWorkerKernel
|
|
78
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { adoptRunWorkerKernel, createRunWorkerPlacementTransportPlugin, hydrateRunWorkerProjectEnv, type AdoptRunWorkerKernelOptions, type RunWorkerHydrateProjectEnvOptions, type RunWorkerHydrationResult, } from "./host-kernel";
|
|
2
|
+
export { createRunJournal, sessionIdFromSessionFile, type JournalSessionProvider, type RunJournal, } from "./session-journal";
|
package/dist/src/host.js
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/run-worker/src/host-kernel.ts
|
|
3
|
+
import { existsSync, readFileSync } from "fs";
|
|
4
|
+
import { resolve } from "path";
|
|
5
|
+
import { applyConfigEnv } from "@rig/core/config-env";
|
|
6
|
+
import { loadConfig } from "@rig/core/load-config";
|
|
7
|
+
import { bootDefaultKernelIntoProcess } from "@rig/kernel-seed/boot-default";
|
|
8
|
+
import { createPlacementTransportPlugin } from "@rig/kernel-seed/default-kernel";
|
|
9
|
+
function unquoteDotenvValue(value) {
|
|
10
|
+
const trimmed = value.trim();
|
|
11
|
+
if (trimmed.length >= 2 && (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
12
|
+
return trimmed.slice(1, -1);
|
|
13
|
+
}
|
|
14
|
+
return trimmed;
|
|
15
|
+
}
|
|
16
|
+
function hydrateDotenv(path, env) {
|
|
17
|
+
if (!existsSync(path))
|
|
18
|
+
return false;
|
|
19
|
+
const source = readFileSync(path, "utf8");
|
|
20
|
+
for (const rawLine of source.split(/\r?\n/)) {
|
|
21
|
+
const line = rawLine.trim();
|
|
22
|
+
if (!line || line.startsWith("#"))
|
|
23
|
+
continue;
|
|
24
|
+
const assignment = line.startsWith("export ") ? line.slice("export ".length).trim() : line;
|
|
25
|
+
const equals = assignment.indexOf("=");
|
|
26
|
+
if (equals <= 0)
|
|
27
|
+
continue;
|
|
28
|
+
const key = assignment.slice(0, equals).trim();
|
|
29
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key) || env[key])
|
|
30
|
+
continue;
|
|
31
|
+
env[key] = unquoteDotenvValue(assignment.slice(equals + 1));
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
function createRunWorkerPlacementTransportPlugin(transport) {
|
|
36
|
+
return createPlacementTransportPlugin(transport);
|
|
37
|
+
}
|
|
38
|
+
async function hydrateRunWorkerProjectEnv(projectRoot, options = {}) {
|
|
39
|
+
const normalizedRoot = resolve(projectRoot);
|
|
40
|
+
const env = options.env ?? process.env;
|
|
41
|
+
const errors = [];
|
|
42
|
+
let dotenvLoaded = false;
|
|
43
|
+
let configLoaded = false;
|
|
44
|
+
try {
|
|
45
|
+
dotenvLoaded = hydrateDotenv(resolve(normalizedRoot, options.dotenvPath ?? ".env"), env);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
errors.push(`dotenv: ${error instanceof Error ? error.message : String(error)}`);
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
applyConfigEnv(await loadConfig(normalizedRoot), env);
|
|
51
|
+
configLoaded = true;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
54
|
+
if (!message.includes("no rig.config"))
|
|
55
|
+
errors.push(`config: ${message}`);
|
|
56
|
+
}
|
|
57
|
+
return { projectRoot: normalizedRoot, dotenvLoaded, configLoaded, errors };
|
|
58
|
+
}
|
|
59
|
+
async function adoptRunWorkerKernel(root, options = {}) {
|
|
60
|
+
if (options.hydrateEnv === true && root !== undefined) {
|
|
61
|
+
await hydrateRunWorkerProjectEnv(root, options);
|
|
62
|
+
}
|
|
63
|
+
const bootInput = {
|
|
64
|
+
...options.entrypoint !== undefined ? { entrypoint: options.entrypoint } : {},
|
|
65
|
+
...options.journal !== undefined ? { journal: options.journal } : {}
|
|
66
|
+
};
|
|
67
|
+
if (options.transport === undefined)
|
|
68
|
+
return await bootDefaultKernelIntoProcess(bootInput);
|
|
69
|
+
return await bootDefaultKernelIntoProcess({
|
|
70
|
+
...bootInput,
|
|
71
|
+
extraPlugins: [createRunWorkerPlacementTransportPlugin(options.transport)]
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
// packages/run-worker/src/session-journal.ts
|
|
75
|
+
import { Schema } from "effect";
|
|
76
|
+
import {
|
|
77
|
+
CUSTOM_TYPE_FOR,
|
|
78
|
+
RIG_CONTROL_SENTINEL_END,
|
|
79
|
+
RIG_INBOX_RESOLUTION_SENTINEL,
|
|
80
|
+
RIG_PAUSE_SENTINEL,
|
|
81
|
+
RIG_RESUME_SENTINEL,
|
|
82
|
+
RIG_STOP_SENTINEL,
|
|
83
|
+
RIG_STOP_SENTINEL_END,
|
|
84
|
+
RIG_WORKFLOW_STATUS_CHANGED,
|
|
85
|
+
RunJournalEvent,
|
|
86
|
+
TYPE_FOR_CUSTOM
|
|
87
|
+
} from "@rig/contracts";
|
|
88
|
+
var decodeRunJournalEvent = Schema.decodeUnknownSync(RunJournalEvent);
|
|
89
|
+
var RUN_STATUS_TRANSITIONS = {
|
|
90
|
+
created: ["queued", "preparing", "running", "failed", "stopped"],
|
|
91
|
+
queued: ["preparing", "running", "failed", "stopped"],
|
|
92
|
+
preparing: ["queued", "running", "needs-attention", "failed", "stopped"],
|
|
93
|
+
running: [
|
|
94
|
+
"queued",
|
|
95
|
+
"waiting-approval",
|
|
96
|
+
"waiting-user-input",
|
|
97
|
+
"paused",
|
|
98
|
+
"validating",
|
|
99
|
+
"reviewing",
|
|
100
|
+
"closing-out",
|
|
101
|
+
"needs-attention",
|
|
102
|
+
"completed",
|
|
103
|
+
"failed",
|
|
104
|
+
"stopped"
|
|
105
|
+
],
|
|
106
|
+
"waiting-approval": ["running", "needs-attention", "failed", "stopped"],
|
|
107
|
+
"waiting-user-input": ["running", "needs-attention", "failed", "stopped"],
|
|
108
|
+
paused: ["running", "failed", "stopped"],
|
|
109
|
+
validating: ["running", "reviewing", "closing-out", "needs-attention", "completed", "failed", "stopped"],
|
|
110
|
+
reviewing: ["running", "validating", "closing-out", "needs-attention", "completed", "failed", "stopped"],
|
|
111
|
+
"closing-out": ["running", "needs-attention", "completed", "failed", "stopped"],
|
|
112
|
+
"needs-attention": ["queued", "preparing", "running", "closing-out", "completed", "failed", "stopped"],
|
|
113
|
+
completed: [],
|
|
114
|
+
failed: ["queued", "preparing", "running", "closing-out"],
|
|
115
|
+
stopped: ["queued", "preparing", "running", "closing-out"]
|
|
116
|
+
};
|
|
117
|
+
var TERMINAL_RUN_STATUSES = ["completed", "failed", "stopped"];
|
|
118
|
+
function isTerminalRunStatus(status) {
|
|
119
|
+
return TERMINAL_RUN_STATUSES.includes(status);
|
|
120
|
+
}
|
|
121
|
+
function canTransitionRunStatus(from, to) {
|
|
122
|
+
if (from === null)
|
|
123
|
+
return true;
|
|
124
|
+
if (from === to)
|
|
125
|
+
return true;
|
|
126
|
+
return RUN_STATUS_TRANSITIONS[from].includes(to);
|
|
127
|
+
}
|
|
128
|
+
function assertRunStatusTransition(from, to) {
|
|
129
|
+
if (!canTransitionRunStatus(from, to)) {
|
|
130
|
+
throw new Error(`Illegal run status transition: ${from ?? "(none)"} -> ${to}. ` + `Allowed from ${from ?? "(none)"}: ${from ? RUN_STATUS_TRANSITIONS[from].join(", ") : "(any)"}.`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function reduceRunJournal(events, runId = null) {
|
|
134
|
+
let record = {};
|
|
135
|
+
let status = null;
|
|
136
|
+
const statusHistory = [];
|
|
137
|
+
const pendingApprovals = new Map;
|
|
138
|
+
const resolvedApprovals = [];
|
|
139
|
+
const pendingUserInputs = new Map;
|
|
140
|
+
const resolvedUserInputs = [];
|
|
141
|
+
const closeoutPhases = [];
|
|
142
|
+
let resolvedPipeline = null;
|
|
143
|
+
const stageOutcomes = [];
|
|
144
|
+
const anomalies = [];
|
|
145
|
+
let steeringCount = 0;
|
|
146
|
+
let stallCount = 0;
|
|
147
|
+
let lastSeq = 0;
|
|
148
|
+
let lastEventAt = null;
|
|
149
|
+
const projectedRunId = runId ?? events[0]?.runId ?? null;
|
|
150
|
+
for (const event of events) {
|
|
151
|
+
lastSeq = event.seq;
|
|
152
|
+
lastEventAt = event.at;
|
|
153
|
+
switch (event.type) {
|
|
154
|
+
case "status-changed": {
|
|
155
|
+
if (!canTransitionRunStatus(status, event.to)) {
|
|
156
|
+
anomalies.push({ seq: event.seq, kind: "illegal-transition", detail: `${status ?? "(none)"} -> ${event.to}` });
|
|
157
|
+
}
|
|
158
|
+
statusHistory.push({ seq: event.seq, at: event.at, from: status, to: event.to, reason: event.reason ?? null });
|
|
159
|
+
const wasTerminal = status !== null && isTerminalRunStatus(status);
|
|
160
|
+
status = event.to;
|
|
161
|
+
record = { ...record, updatedAt: event.at };
|
|
162
|
+
if (isTerminalRunStatus(event.to) && !record.completedAt)
|
|
163
|
+
record = { ...record, completedAt: event.at };
|
|
164
|
+
if (!isTerminalRunStatus(event.to) && wasTerminal)
|
|
165
|
+
record = { ...record, completedAt: null };
|
|
166
|
+
if (event.to === "running" && !record.startedAt)
|
|
167
|
+
record = { ...record, startedAt: event.at };
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case "record-patch": {
|
|
171
|
+
const next = { ...record };
|
|
172
|
+
for (const [key, value] of Object.entries(event.patch)) {
|
|
173
|
+
if (value !== undefined)
|
|
174
|
+
next[key] = value;
|
|
175
|
+
}
|
|
176
|
+
next.updatedAt = event.patch.updatedAt ?? event.at;
|
|
177
|
+
record = next;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
case "approval-requested": {
|
|
181
|
+
pendingApprovals.set(event.requestId, {
|
|
182
|
+
requestId: event.requestId,
|
|
183
|
+
requestKind: event.requestKind,
|
|
184
|
+
actionId: event.actionId ?? null,
|
|
185
|
+
payload: event.payload,
|
|
186
|
+
requestedAt: event.at
|
|
187
|
+
});
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
case "approval-resolved": {
|
|
191
|
+
const pending = pendingApprovals.get(event.requestId);
|
|
192
|
+
if (!pending) {
|
|
193
|
+
const alreadyResolved = resolvedApprovals.some((entry) => entry.requestId === event.requestId);
|
|
194
|
+
anomalies.push({ seq: event.seq, kind: alreadyResolved ? "duplicate-resolution" : "unknown-request", detail: `approval ${event.requestId}` });
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
pendingApprovals.delete(event.requestId);
|
|
198
|
+
resolvedApprovals.push({ ...pending, decision: event.decision, note: event.note ?? null, actor: event.actor, resolvedAt: event.at });
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
case "input-requested": {
|
|
202
|
+
pendingUserInputs.set(event.requestId, {
|
|
203
|
+
requestId: event.requestId,
|
|
204
|
+
requestKind: "user-input",
|
|
205
|
+
actionId: null,
|
|
206
|
+
payload: event.payload,
|
|
207
|
+
requestedAt: event.at
|
|
208
|
+
});
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
case "input-resolved": {
|
|
212
|
+
const pending = pendingUserInputs.get(event.requestId);
|
|
213
|
+
if (!pending) {
|
|
214
|
+
const alreadyResolved = resolvedUserInputs.some((entry) => entry.requestId === event.requestId);
|
|
215
|
+
anomalies.push({ seq: event.seq, kind: alreadyResolved ? "duplicate-resolution" : "unknown-request", detail: `user-input ${event.requestId}` });
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
pendingUserInputs.delete(event.requestId);
|
|
219
|
+
resolvedUserInputs.push({ ...pending, answers: event.answers, actor: event.actor, resolvedAt: event.at });
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
case "steering":
|
|
223
|
+
steeringCount += 1;
|
|
224
|
+
break;
|
|
225
|
+
case "adopted":
|
|
226
|
+
record = { ...record, pid: event.pid, updatedAt: event.at };
|
|
227
|
+
break;
|
|
228
|
+
case "stall-detected":
|
|
229
|
+
stallCount += 1;
|
|
230
|
+
break;
|
|
231
|
+
case "closeout-phase":
|
|
232
|
+
closeoutPhases.push({ seq: event.seq, at: event.at, phase: event.phase, outcome: event.outcome, detail: event.detail ?? null });
|
|
233
|
+
break;
|
|
234
|
+
case "pipeline-resolved":
|
|
235
|
+
resolvedPipeline = event.pipeline;
|
|
236
|
+
break;
|
|
237
|
+
case "stage-outcome":
|
|
238
|
+
stageOutcomes.push({ seq: event.seq, at: event.at, outcome: event.outcome });
|
|
239
|
+
break;
|
|
240
|
+
case "timeline-entry":
|
|
241
|
+
case "log-entry":
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
runId: projectedRunId,
|
|
247
|
+
record,
|
|
248
|
+
status,
|
|
249
|
+
statusHistory,
|
|
250
|
+
pendingApprovals: [...pendingApprovals.values()],
|
|
251
|
+
resolvedApprovals,
|
|
252
|
+
pendingUserInputs: [...pendingUserInputs.values()],
|
|
253
|
+
resolvedUserInputs,
|
|
254
|
+
steeringCount,
|
|
255
|
+
stallCount,
|
|
256
|
+
closeoutPhases,
|
|
257
|
+
resolvedPipeline,
|
|
258
|
+
stageOutcomes,
|
|
259
|
+
lastSeq,
|
|
260
|
+
lastEventAt,
|
|
261
|
+
anomalies
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function sessionIdFromSessionFile(sessionPath) {
|
|
265
|
+
if (!sessionPath)
|
|
266
|
+
return null;
|
|
267
|
+
const file = sessionPath.split(/[\\/]/).pop() ?? "";
|
|
268
|
+
const match = file.match(/_([0-9a-fA-F][0-9a-fA-F-]{7,})\.jsonl$/);
|
|
269
|
+
return match?.[1] ?? null;
|
|
270
|
+
}
|
|
271
|
+
function isRunSessionCustomType(customType) {
|
|
272
|
+
return customType !== undefined && Object.hasOwn(TYPE_FOR_CUSTOM, customType);
|
|
273
|
+
}
|
|
274
|
+
function foldRunSessionEntries(entries, runId) {
|
|
275
|
+
const events = [];
|
|
276
|
+
entries.forEach((entry, index) => {
|
|
277
|
+
if (entry.type !== "custom" || !isRunSessionCustomType(entry.customType))
|
|
278
|
+
return;
|
|
279
|
+
const data = entry.data !== null && typeof entry.data === "object" ? entry.data : {};
|
|
280
|
+
const stamped = {
|
|
281
|
+
v: 1,
|
|
282
|
+
seq: index + 1,
|
|
283
|
+
at: typeof data.at === "string" ? data.at : new Date(0).toISOString(),
|
|
284
|
+
runId,
|
|
285
|
+
...data,
|
|
286
|
+
type: TYPE_FOR_CUSTOM[entry.customType]
|
|
287
|
+
};
|
|
288
|
+
try {
|
|
289
|
+
events.push(decodeRunJournalEvent(stamped));
|
|
290
|
+
} catch {}
|
|
291
|
+
});
|
|
292
|
+
return reduceRunJournal(events, runId);
|
|
293
|
+
}
|
|
294
|
+
function asCustomEntries(entries) {
|
|
295
|
+
return entries.filter((entry) => entry.type === "custom");
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
class RunSessionJournal {
|
|
299
|
+
sm;
|
|
300
|
+
runId;
|
|
301
|
+
constructor(sm, runId) {
|
|
302
|
+
this.sm = sm;
|
|
303
|
+
this.runId = runId;
|
|
304
|
+
}
|
|
305
|
+
#append(init) {
|
|
306
|
+
this.sm.appendCustomEntry(CUSTOM_TYPE_FOR[init.type], { ...init, at: new Date().toISOString() });
|
|
307
|
+
}
|
|
308
|
+
appendStatus(to, opts = {}) {
|
|
309
|
+
const from = foldRunSessionEntries(asCustomEntries(this.sm.getEntries()), this.runId).status;
|
|
310
|
+
if (!opts.force)
|
|
311
|
+
assertRunStatusTransition(from, to);
|
|
312
|
+
if (opts.errorText !== undefined)
|
|
313
|
+
this.#append({ type: "record-patch", patch: { errorText: opts.errorText } });
|
|
314
|
+
this.#append({
|
|
315
|
+
type: "status-changed",
|
|
316
|
+
from,
|
|
317
|
+
to,
|
|
318
|
+
...opts.reason !== undefined ? { reason: opts.reason } : {},
|
|
319
|
+
...opts.actor !== undefined ? { actor: opts.actor } : {}
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
appendTimeline(payload) {
|
|
323
|
+
this.#append({ type: "timeline-entry", payload });
|
|
324
|
+
}
|
|
325
|
+
appendCloseoutPhase(input) {
|
|
326
|
+
this.#append({
|
|
327
|
+
type: "closeout-phase",
|
|
328
|
+
phase: input.phase,
|
|
329
|
+
outcome: input.outcome,
|
|
330
|
+
...input.detail !== undefined ? { detail: input.detail } : {}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
appendApprovalResolved(input) {
|
|
334
|
+
this.#append({
|
|
335
|
+
type: "approval-resolved",
|
|
336
|
+
requestId: input.requestId,
|
|
337
|
+
decision: input.decision,
|
|
338
|
+
actor: input.actor,
|
|
339
|
+
...input.note !== undefined ? { note: input.note } : {}
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
appendInputResolved(input) {
|
|
343
|
+
this.#append({ type: "input-resolved", requestId: input.requestId, answers: input.answers, actor: input.actor });
|
|
344
|
+
}
|
|
345
|
+
appendStall(input) {
|
|
346
|
+
this.#append({ type: "stall-detected", detail: input.detail });
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
function isRunJournalEventInit(event) {
|
|
350
|
+
if (event === null || typeof event !== "object")
|
|
351
|
+
return false;
|
|
352
|
+
const type = event.type;
|
|
353
|
+
return typeof type === "string" && Object.hasOwn(CUSTOM_TYPE_FOR, type);
|
|
354
|
+
}
|
|
355
|
+
function createJournalSessionProvider(options) {
|
|
356
|
+
const now = options.now ?? (() => new Date);
|
|
357
|
+
const appendRunEvent = async (event, runId = options.runId) => {
|
|
358
|
+
options.store.appendCustomEntry(CUSTOM_TYPE_FOR[event.type], { ...event, runId, at: now().toISOString() });
|
|
359
|
+
};
|
|
360
|
+
const readEntries = () => asCustomEntries(options.store.getEntries());
|
|
361
|
+
const entriesForRun = (runId) => readEntries().filter((entry) => {
|
|
362
|
+
const data = entry.data;
|
|
363
|
+
if (data === null || typeof data !== "object")
|
|
364
|
+
return runId === options.runId;
|
|
365
|
+
const candidate = data.runId;
|
|
366
|
+
return candidate === undefined ? runId === options.runId : candidate === runId;
|
|
367
|
+
});
|
|
368
|
+
return {
|
|
369
|
+
runId: options.runId,
|
|
370
|
+
async append(event) {
|
|
371
|
+
if (isRunJournalEventInit(event))
|
|
372
|
+
await appendRunEvent(event);
|
|
373
|
+
},
|
|
374
|
+
appendRunEvent,
|
|
375
|
+
async recordPipeline(runId, pipeline) {
|
|
376
|
+
await appendRunEvent({ type: "pipeline-resolved", pipeline }, runId);
|
|
377
|
+
},
|
|
378
|
+
async recordStageOutcome(runId, outcome) {
|
|
379
|
+
await appendRunEvent({ type: "stage-outcome", outcome }, runId);
|
|
380
|
+
},
|
|
381
|
+
async read(runId) {
|
|
382
|
+
return entriesForRun(runId);
|
|
383
|
+
},
|
|
384
|
+
readEntries,
|
|
385
|
+
readProjection: () => foldRunSessionEntries(entriesForRun(options.runId), options.runId)
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
async function createRunJournal(sessionManager, runId) {
|
|
389
|
+
try {
|
|
390
|
+
const writableSessionManager = sessionManager;
|
|
391
|
+
const journal = new RunSessionJournal(writableSessionManager, runId);
|
|
392
|
+
const kernel = createJournalSessionProvider({
|
|
393
|
+
runId,
|
|
394
|
+
store: {
|
|
395
|
+
appendCustomEntry: (customType, data) => writableSessionManager.appendCustomEntry(customType, data),
|
|
396
|
+
getEntries: () => sessionManager.getEntries?.() ?? sessionManager.getBranch?.() ?? []
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
return {
|
|
400
|
+
kernel,
|
|
401
|
+
appendStatus: journal.appendStatus.bind(journal),
|
|
402
|
+
appendTimeline: journal.appendTimeline.bind(journal),
|
|
403
|
+
appendCloseoutPhase: journal.appendCloseoutPhase.bind(journal),
|
|
404
|
+
appendApprovalResolved: journal.appendApprovalResolved.bind(journal),
|
|
405
|
+
appendInputResolved: journal.appendInputResolved.bind(journal),
|
|
406
|
+
appendStall: journal.appendStall.bind(journal)
|
|
407
|
+
};
|
|
408
|
+
} catch (error) {
|
|
409
|
+
console.warn(`[rig-run] RunSessionJournal unavailable; run-state arming deferred: ${error instanceof Error ? error.message : String(error)}`);
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
export {
|
|
414
|
+
sessionIdFromSessionFile,
|
|
415
|
+
hydrateRunWorkerProjectEnv,
|
|
416
|
+
createRunWorkerPlacementTransportPlugin,
|
|
417
|
+
createRunJournal,
|
|
418
|
+
adoptRunWorkerKernel
|
|
419
|
+
};
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
1
|
export { default, __rigExtensionTest, __rigRunWorkerTest } from "./extension";
|
|
2
2
|
export * from "./plugin";
|
|
3
|
-
export * from "./autohost";
|
|
4
|
-
export * from "./constants";
|
|
5
|
-
export * from "./journal";
|
|
6
|
-
export * from "./panel-plugin";
|
|
7
|
-
export * from "./notifications";
|
|
8
|
-
export * from "./stall";
|