@juicesharp/rpiv-workflow 1.17.1 → 1.18.0
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 +56 -15
- package/api.ts +1 -1
- package/audit.ts +42 -7
- package/built-ins.ts +46 -2
- package/command-run.ts +132 -0
- package/command.ts +29 -100
- package/docs/workflow-authoring.md +23 -23
- package/extension.ts +18 -0
- package/fanout.ts +11 -5
- package/host.ts +26 -13
- package/index.ts +40 -134
- package/internal-utils.ts +35 -0
- package/internal.ts +1 -1
- package/iterate.ts +20 -8
- package/lifecycle.ts +1 -1
- package/load/index.ts +5 -1
- package/messages.ts +34 -0
- package/package.json +9 -3
- package/registration.ts +113 -0
- package/runner/by-name.test.ts +95 -0
- package/runner/by-name.ts +76 -0
- package/runner/by-run-id.test.ts +115 -0
- package/runner/by-run-id.ts +92 -0
- package/runner/chain-advance.ts +22 -42
- package/runner/index.ts +10 -1
- package/runner/resume-fanout.test.ts +331 -0
- package/runner/resume-fanout.ts +136 -0
- package/runner/resume-iterate.test.ts +444 -0
- package/runner/resume-iterate.ts +110 -0
- package/runner/resume.test.ts +1130 -0
- package/runner/resume.ts +333 -0
- package/runner/runner.ts +281 -91
- package/runner/script-stage.ts +18 -51
- package/runner/stage-lifecycle.ts +35 -27
- package/sessions/extraction.ts +4 -4
- package/sessions/sessions.ts +22 -40
- package/sessions/spawn.ts +15 -11
- package/startup.ts +9 -0
- package/state/index.ts +1 -0
- package/state/reads.ts +20 -0
- package/state/state.ts +1 -0
- package/types.ts +18 -13
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import type { PromptFn, StageDef } from "../api.js";
|
|
13
|
-
import { notifyPartialArtifacts, recordTerminalFailure } from "../audit.js";
|
|
13
|
+
import { auditCtxFor, notifyPartialArtifacts, recordTerminalFailure, runIdentityOf } from "../audit.js";
|
|
14
14
|
import { runFanout } from "../fanout.js";
|
|
15
15
|
import { handleToString } from "../handle.js";
|
|
16
16
|
import { currentPrimaryArtifact, withTimeout } from "../internal-utils.js";
|
|
@@ -34,7 +34,7 @@ import {
|
|
|
34
34
|
} from "../messages.js";
|
|
35
35
|
import { runFanoutSession, runStageSession } from "../sessions/index.js";
|
|
36
36
|
import { readBranch } from "../transcript.js";
|
|
37
|
-
import type { RunContext,
|
|
37
|
+
import type { RunContext, WorkflowHostContext } from "../types.js";
|
|
38
38
|
import {
|
|
39
39
|
DEFAULT_VALIDATION_RETRY_TIMEOUT_MS,
|
|
40
40
|
MAX_VALIDATION_RETRY_TIMEOUT_MS,
|
|
@@ -186,7 +186,12 @@ function formatNamedInputs(names: ReadonlyArray<string>, run: RunContext): strin
|
|
|
186
186
|
* Each `PreflightCheck` throws `StagePreflightError` on failure;
|
|
187
187
|
* `runStageOrRecordFailure` catches and records the JSONL row.
|
|
188
188
|
*/
|
|
189
|
-
export async function runStage(
|
|
189
|
+
export async function runStage(
|
|
190
|
+
curCtx: WorkflowHostContext,
|
|
191
|
+
currentName: string,
|
|
192
|
+
idx: number,
|
|
193
|
+
run: RunContext,
|
|
194
|
+
): Promise<void> {
|
|
190
195
|
const stage = resolveStage(currentName, idx, run);
|
|
191
196
|
|
|
192
197
|
if (await tryFanout(curCtx, stage, idx, run)) return;
|
|
@@ -238,7 +243,7 @@ export async function runStage(curCtx: RunnerCtx, currentName: string, idx: numb
|
|
|
238
243
|
stageName: stage.name,
|
|
239
244
|
skill: stage.skill,
|
|
240
245
|
lifecycle: run.lifecycle,
|
|
241
|
-
runIdentity:
|
|
246
|
+
runIdentity: runIdentityOf(run),
|
|
242
247
|
stage: stage.def,
|
|
243
248
|
stageIndex: idx,
|
|
244
249
|
snapshot,
|
|
@@ -268,7 +273,12 @@ function resolveStage(currentName: string, idx: number, run: RunContext): Resolv
|
|
|
268
273
|
* FanoutFn. Returns true iff fanout fired (i.e. at least one unit was
|
|
269
274
|
* returned) — caller then returns without running the single-stage path.
|
|
270
275
|
*/
|
|
271
|
-
async function tryFanout(
|
|
276
|
+
async function tryFanout(
|
|
277
|
+
curCtx: WorkflowHostContext,
|
|
278
|
+
stage: ResolvedStage,
|
|
279
|
+
idx: number,
|
|
280
|
+
run: RunContext,
|
|
281
|
+
): Promise<boolean> {
|
|
272
282
|
if (!stage.def.fanout) return false;
|
|
273
283
|
const primary = currentPrimaryArtifact(run.state);
|
|
274
284
|
const units = await stage.def.fanout({
|
|
@@ -306,7 +316,12 @@ async function tryFanout(curCtx: RunnerCtx, stage: ResolvedStage, idx: number, r
|
|
|
306
316
|
* stage-entry primary is frozen and threaded as `entryArtifact` so the
|
|
307
317
|
* generator keeps seeing its true source even as the rolling primary advances.
|
|
308
318
|
*/
|
|
309
|
-
async function tryIterate(
|
|
319
|
+
async function tryIterate(
|
|
320
|
+
curCtx: WorkflowHostContext,
|
|
321
|
+
stage: ResolvedStage,
|
|
322
|
+
idx: number,
|
|
323
|
+
run: RunContext,
|
|
324
|
+
): Promise<boolean> {
|
|
310
325
|
if (!stage.def.iterate) return false;
|
|
311
326
|
// Runtime mirror of the load-time invariant — defense-in-depth for embedders
|
|
312
327
|
// that bypass validateWorkflow. iterate dispatches via runStageSession, which
|
|
@@ -341,25 +356,18 @@ async function tryIterate(curCtx: RunnerCtx, stage: ResolvedStage, idx: number,
|
|
|
341
356
|
* run-wide `maxIterations` cap. Mirrors `checkBackwardJumpGuard`'s terminal
|
|
342
357
|
* path (chain-advance.ts): attribution targets the iterate node's real name.
|
|
343
358
|
*/
|
|
344
|
-
async function haltIterations(
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
{
|
|
357
|
-
status: "failed",
|
|
358
|
-
notifyMsg: MSG_ITERATIONS_EXHAUSTED(count, run.maxIterations),
|
|
359
|
-
notifyLevel: "error",
|
|
360
|
-
errMsg: ERR_ITERATIONS_EXHAUSTED(count, run.maxIterations),
|
|
361
|
-
},
|
|
362
|
-
);
|
|
359
|
+
export async function haltIterations(
|
|
360
|
+
curCtx: WorkflowHostContext,
|
|
361
|
+
run: RunContext,
|
|
362
|
+
stageName: string,
|
|
363
|
+
count: number,
|
|
364
|
+
): Promise<void> {
|
|
365
|
+
await recordTerminalFailure(curCtx, auditCtxFor(run, stageName, stageName), {
|
|
366
|
+
status: "failed",
|
|
367
|
+
notifyMsg: MSG_ITERATIONS_EXHAUSTED(count, run.maxIterations),
|
|
368
|
+
notifyLevel: "error",
|
|
369
|
+
errMsg: ERR_ITERATIONS_EXHAUSTED(count, run.maxIterations),
|
|
370
|
+
});
|
|
363
371
|
}
|
|
364
372
|
|
|
365
373
|
/**
|
|
@@ -466,7 +474,7 @@ function enforceSessionInvariants(stage: ResolvedStage, run: RunContext): void {
|
|
|
466
474
|
}
|
|
467
475
|
|
|
468
476
|
/** Entries before this index belong to prior stages; only meaningful for continue. */
|
|
469
|
-
function computeBranchOffset(curCtx:
|
|
477
|
+
function computeBranchOffset(curCtx: WorkflowHostContext, def: StageDef): number | undefined {
|
|
470
478
|
if (def.sessionPolicy !== "continue") return undefined;
|
|
471
479
|
return readBranch(curCtx).length;
|
|
472
480
|
}
|
|
@@ -524,7 +532,7 @@ function clampValidateTimeoutMs(raw: number | undefined): number {
|
|
|
524
532
|
);
|
|
525
533
|
}
|
|
526
534
|
|
|
527
|
-
async function captureStageSnapshot(def: StageDef, idx: number, run: RunContext): Promise<unknown> {
|
|
535
|
+
export async function captureStageSnapshot(def: StageDef, idx: number, run: RunContext): Promise<unknown> {
|
|
528
536
|
const snapshot = def.outcome?.collector.snapshot;
|
|
529
537
|
if (!snapshot) return undefined;
|
|
530
538
|
try {
|
package/sessions/extraction.ts
CHANGED
|
@@ -25,7 +25,7 @@ import { sideEffectOutcome } from "../outcomes/index.js";
|
|
|
25
25
|
import { finalizeOutput, type Output } from "../output.js";
|
|
26
26
|
import type { CollectCtx, OutputSpec } from "../output-spec.js";
|
|
27
27
|
import { type BranchEntry, readBranch } from "../transcript.js";
|
|
28
|
-
import type {
|
|
28
|
+
import type { StageSession, WorkflowHostContext } from "../types.js";
|
|
29
29
|
import {
|
|
30
30
|
DEFAULT_VALIDATION_RETRIES,
|
|
31
31
|
DEFAULT_VALIDATION_RETRY_TIMEOUT_MS,
|
|
@@ -46,7 +46,7 @@ export type OutputProduction =
|
|
|
46
46
|
|
|
47
47
|
/** Retry loop re-produces against the latest branch after each fix request. */
|
|
48
48
|
export async function produceAndValidateOutput(
|
|
49
|
-
ctx:
|
|
49
|
+
ctx: WorkflowHostContext,
|
|
50
50
|
s: StageSession,
|
|
51
51
|
branch: BranchEntry[],
|
|
52
52
|
branchOffset: number | undefined,
|
|
@@ -187,7 +187,7 @@ interface RetryDeps {
|
|
|
187
187
|
}
|
|
188
188
|
|
|
189
189
|
async function retryUntilValid(
|
|
190
|
-
ctx:
|
|
190
|
+
ctx: WorkflowHostContext,
|
|
191
191
|
s: StageSession,
|
|
192
192
|
deps: RetryDeps,
|
|
193
193
|
initial: Output,
|
|
@@ -276,7 +276,7 @@ async function validateOrFatal(
|
|
|
276
276
|
}
|
|
277
277
|
|
|
278
278
|
async function askAgentToFix(
|
|
279
|
-
ctx:
|
|
279
|
+
ctx: WorkflowHostContext,
|
|
280
280
|
s: StageSession,
|
|
281
281
|
attempt: number,
|
|
282
282
|
failures: SchemaValidationFailure[],
|
package/sessions/sessions.ts
CHANGED
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
recordStopFailure,
|
|
25
25
|
recordTerminalFailure,
|
|
26
26
|
} from "../audit.js";
|
|
27
|
-
import {
|
|
27
|
+
import { applyCompletedStage } from "../internal-utils.js";
|
|
28
28
|
import { buildLifecycleContext, skillStageRef } from "../lifecycle.js";
|
|
29
29
|
import {
|
|
30
30
|
ERR_AUDIT_WRITE_FAILED,
|
|
@@ -36,7 +36,7 @@ import {
|
|
|
36
36
|
} from "../messages.js";
|
|
37
37
|
import type { Output } from "../output.js";
|
|
38
38
|
import { type BranchEntry, classifyStop, readBranch, type StopSignal } from "../transcript.js";
|
|
39
|
-
import type { FanoutSession,
|
|
39
|
+
import type { FanoutSession, SessionContext, StageSession, WorkflowHostContext } from "../types.js";
|
|
40
40
|
import { produceAndValidateOutput } from "./extraction.js";
|
|
41
41
|
import { FRESH_HANDLER, handlerFor } from "./spawn.js";
|
|
42
42
|
|
|
@@ -45,14 +45,14 @@ import { FRESH_HANDLER, handlerFor } from "./spawn.js";
|
|
|
45
45
|
// ===========================================================================
|
|
46
46
|
|
|
47
47
|
/** Execute one DAG stage in its own session. */
|
|
48
|
-
export async function runStageSession(ctx:
|
|
48
|
+
export async function runStageSession(ctx: WorkflowHostContext, s: StageSession): Promise<void> {
|
|
49
49
|
const handler = handlerFor(s.stage.sessionPolicy);
|
|
50
50
|
const { cancelled } = await handler.spawn(ctx, s.prompt, (sessionCtx) => postStage(sessionCtx, s), s.continueHost);
|
|
51
51
|
if (cancelled) await recordCancellation(ctx, auditFor(s));
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
/** Execute one fanout-unit iteration. Always fresh. */
|
|
55
|
-
export async function runFanoutSession(ctx:
|
|
55
|
+
export async function runFanoutSession(ctx: WorkflowHostContext, s: FanoutSession): Promise<void> {
|
|
56
56
|
const { cancelled } = await FRESH_HANDLER.spawn(ctx, s.prompt, (sessionCtx) => postFanout(sessionCtx, s));
|
|
57
57
|
if (cancelled) await recordCancellation(ctx, auditFor(s));
|
|
58
58
|
}
|
|
@@ -62,7 +62,7 @@ export async function runFanoutSession(ctx: RunnerCtx, s: FanoutSession): Promis
|
|
|
62
62
|
// ===========================================================================
|
|
63
63
|
|
|
64
64
|
/** Stage post-processing: classify outcome → produce & validate output → persist → chain. */
|
|
65
|
-
async function postStage(ctx:
|
|
65
|
+
async function postStage(ctx: WorkflowHostContext, s: StageSession): Promise<void> {
|
|
66
66
|
const handler = handlerFor(s.stage.sessionPolicy);
|
|
67
67
|
const offset = handler.branchOffset(s.branchOffset);
|
|
68
68
|
const outcome = readSessionOutcome(ctx, offset);
|
|
@@ -77,7 +77,7 @@ async function postStage(ctx: RunnerCtx, s: StageSession): Promise<void> {
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
/** Fanout-unit post-processing: classify outcome → persist bare row → chain. */
|
|
80
|
-
async function postFanout(ctx:
|
|
80
|
+
async function postFanout(ctx: WorkflowHostContext, s: FanoutSession): Promise<void> {
|
|
81
81
|
const outcome = readSessionOutcome(ctx, undefined);
|
|
82
82
|
if (outcome.stop !== "stop") return haltFanout(ctx, s, outcome.stop);
|
|
83
83
|
|
|
@@ -89,11 +89,11 @@ async function postFanout(ctx: RunnerCtx, s: FanoutSession): Promise<void> {
|
|
|
89
89
|
// HALT HELPERS — turn a halt reason into the right audit-layer call
|
|
90
90
|
// ===========================================================================
|
|
91
91
|
|
|
92
|
-
async function haltStage(ctx:
|
|
92
|
+
async function haltStage(ctx: WorkflowHostContext, s: StageSession, stop: Exclude<StopSignal, "stop">): Promise<void> {
|
|
93
93
|
await recordStopFailure(ctx, auditFor(s), stop, `${s.skill} failed`, s.onFailure);
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
async function haltStageWithExtractionError(ctx:
|
|
96
|
+
async function haltStageWithExtractionError(ctx: WorkflowHostContext, s: StageSession, message: string): Promise<void> {
|
|
97
97
|
await recordTerminalFailure(
|
|
98
98
|
ctx,
|
|
99
99
|
auditFor(s),
|
|
@@ -102,7 +102,11 @@ async function haltStageWithExtractionError(ctx: RunnerCtx, s: StageSession, mes
|
|
|
102
102
|
);
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
async function haltStageWithValidationFailure(
|
|
105
|
+
async function haltStageWithValidationFailure(
|
|
106
|
+
ctx: WorkflowHostContext,
|
|
107
|
+
s: StageSession,
|
|
108
|
+
failureSummary: string,
|
|
109
|
+
): Promise<void> {
|
|
106
110
|
await recordTerminalFailure(
|
|
107
111
|
ctx,
|
|
108
112
|
auditFor(s),
|
|
@@ -116,7 +120,11 @@ async function haltStageWithValidationFailure(ctx: RunnerCtx, s: StageSession, f
|
|
|
116
120
|
);
|
|
117
121
|
}
|
|
118
122
|
|
|
119
|
-
async function haltFanout(
|
|
123
|
+
async function haltFanout(
|
|
124
|
+
ctx: WorkflowHostContext,
|
|
125
|
+
s: FanoutSession,
|
|
126
|
+
stop: Exclude<StopSignal, "stop">,
|
|
127
|
+
): Promise<void> {
|
|
120
128
|
await recordStopFailure(ctx, auditFor(s), stop, `${s.skill} unit ${s.unitIndex} (${s.label}) failed`);
|
|
121
129
|
}
|
|
122
130
|
|
|
@@ -149,41 +157,15 @@ function tryRecordStage(s: SessionContext, row: { stage: string; skill?: string;
|
|
|
149
157
|
return true;
|
|
150
158
|
}
|
|
151
159
|
|
|
152
|
-
/**
|
|
153
|
-
* Update the rolling chain-input slot. Three cases:
|
|
154
|
-
* 1. `produces` stages whose collector returned at least one artifact
|
|
155
|
-
* advance the primary (first artifact wins; `role` is user-facing
|
|
156
|
-
* metadata, not a framework gate).
|
|
157
|
-
* 2. `side-effect` stages with `inheritsArtifacts: false` (authored via
|
|
158
|
-
* `terminal()`) CLEAR the slot — they explicitly break the chain
|
|
159
|
-
* so anything after also starts without an inherited artifact.
|
|
160
|
-
* 3. Other `side-effect` stages (commit, implement) leave it in place
|
|
161
|
-
* so a stage after them inherits the upstream chain input.
|
|
162
|
-
*/
|
|
163
|
-
function maybeAdvancePrimary(s: StageSession, output: Output): void {
|
|
164
|
-
if (s.stage.kind === "produces") {
|
|
165
|
-
const next = output.artifacts[0];
|
|
166
|
-
if (next) s.state.primaryArtifact = next;
|
|
167
|
-
const key = resolvePublishName(s.stage, s.stageName);
|
|
168
|
-
const slot = s.state.named[key];
|
|
169
|
-
if (slot) slot.push(output);
|
|
170
|
-
else s.state.named[key] = [output];
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
if (s.stage.inheritsArtifacts === false) {
|
|
174
|
-
s.state.primaryArtifact = undefined;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
160
|
/**
|
|
179
161
|
* Returns true on successful write — caller gates `onSuccess` on this so the
|
|
180
162
|
* chain advances only when the audit row landed. On failure, leaves
|
|
181
163
|
* `state.output` / `state.primaryArtifact` at their prior values and sets
|
|
182
164
|
* `state.termination.error` to halt the run.
|
|
183
165
|
*/
|
|
184
|
-
async function recordStageSuccess(ctx:
|
|
166
|
+
async function recordStageSuccess(ctx: WorkflowHostContext, s: StageSession, output: Output): Promise<boolean> {
|
|
185
167
|
if (tryRecordStage(s, { stage: s.stageName, skill: s.skill, output })) {
|
|
186
|
-
|
|
168
|
+
applyCompletedStage(s.state, s.stage, s.stageName, output);
|
|
187
169
|
ctx.ui.notify(MSG_STAGE_COMPLETE(s.skill), "info");
|
|
188
170
|
await s.lifecycle.fire(
|
|
189
171
|
ctx,
|
|
@@ -199,7 +181,7 @@ async function recordStageSuccess(ctx: RunnerCtx, s: StageSession, output: Outpu
|
|
|
199
181
|
return false;
|
|
200
182
|
}
|
|
201
183
|
|
|
202
|
-
async function recordFanoutSuccess(ctx:
|
|
184
|
+
async function recordFanoutSuccess(ctx: WorkflowHostContext, s: FanoutSession): Promise<boolean> {
|
|
203
185
|
const stageLabel = fanoutRowStage(s);
|
|
204
186
|
if (tryRecordStage(s, { stage: stageLabel, skill: s.skill })) {
|
|
205
187
|
await s.lifecycle.fire(
|
|
@@ -246,7 +228,7 @@ interface SessionOutcome {
|
|
|
246
228
|
* No longer scans the transcript for an artifact path — discovery is
|
|
247
229
|
* the collector's job, not the runner's.
|
|
248
230
|
*/
|
|
249
|
-
function readSessionOutcome(ctx:
|
|
231
|
+
function readSessionOutcome(ctx: WorkflowHostContext, branchOffset: number | undefined): SessionOutcome {
|
|
250
232
|
const branch = readBranch(ctx);
|
|
251
233
|
return {
|
|
252
234
|
branch,
|
package/sessions/spawn.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import type { SessionPolicy } from "../api.js";
|
|
13
13
|
import type { WorkflowHost } from "../host.js";
|
|
14
|
-
import type {
|
|
14
|
+
import type { WorkflowHostContext } from "../types.js";
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Three policy-specific decisions that used to live as five ternaries
|
|
@@ -42,22 +42,21 @@ import type { RunnerCtx } from "../types.js";
|
|
|
42
42
|
export interface SessionPolicyHandler {
|
|
43
43
|
branchOffset(capturedOffset: number | undefined): number | undefined;
|
|
44
44
|
spawn(
|
|
45
|
-
ctx:
|
|
45
|
+
ctx: WorkflowHostContext,
|
|
46
46
|
prompt: string,
|
|
47
|
-
body: (sessionCtx:
|
|
47
|
+
body: (sessionCtx: WorkflowHostContext) => Promise<void>,
|
|
48
48
|
host?: WorkflowHost,
|
|
49
49
|
): Promise<{ cancelled: boolean }>;
|
|
50
|
-
send(ctx:
|
|
50
|
+
send(ctx: WorkflowHostContext, msg: string, host?: WorkflowHost): Promise<void>;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export const FRESH_HANDLER: SessionPolicyHandler = {
|
|
54
54
|
branchOffset: () => undefined,
|
|
55
55
|
async spawn(ctx, prompt, body) {
|
|
56
56
|
const { cancelled } = await ctx.newSession({
|
|
57
|
+
// `freshCtx` is a `WorkflowSessionContext` — the port guarantees
|
|
58
|
+
// `sendUserMessage` on the replacement ctx, so no runtime guard.
|
|
57
59
|
withSession: async (freshCtx) => {
|
|
58
|
-
if (!freshCtx.sendUserMessage) {
|
|
59
|
-
throw new Error("FRESH_HANDLER.spawn: replacement ctx missing sendUserMessage");
|
|
60
|
-
}
|
|
61
60
|
await freshCtx.sendUserMessage(prompt);
|
|
62
61
|
await body(freshCtx);
|
|
63
62
|
},
|
|
@@ -65,9 +64,10 @@ export const FRESH_HANDLER: SessionPolicyHandler = {
|
|
|
65
64
|
return { cancelled };
|
|
66
65
|
},
|
|
67
66
|
async send(ctx, msg) {
|
|
68
|
-
//
|
|
69
|
-
// `
|
|
70
|
-
//
|
|
67
|
+
// At runtime ctx is the in-session replacement (a
|
|
68
|
+
// `WorkflowSessionContext`), but the shared `SessionPolicyHandler`
|
|
69
|
+
// types it as the base `WorkflowHostContext` (continue's send may run
|
|
70
|
+
// on the senderless start ctx), so this seam keeps a defensive guard.
|
|
71
71
|
if (!ctx.sendUserMessage) {
|
|
72
72
|
throw new Error("FRESH_HANDLER.send: replacement ctx missing sendUserMessage");
|
|
73
73
|
}
|
|
@@ -116,7 +116,11 @@ export const CONTINUE_HANDLER: SessionPolicyHandler = {
|
|
|
116
116
|
* `enforceSessionInvariants` should have rejected the workflow before
|
|
117
117
|
* we land here.
|
|
118
118
|
*/
|
|
119
|
-
async function sendIntoExistingSession(
|
|
119
|
+
async function sendIntoExistingSession(
|
|
120
|
+
ctx: WorkflowHostContext,
|
|
121
|
+
host: WorkflowHost | undefined,
|
|
122
|
+
msg: string,
|
|
123
|
+
): Promise<void> {
|
|
120
124
|
if (ctx.sendUserMessage) {
|
|
121
125
|
await ctx.sendUserMessage(msg);
|
|
122
126
|
return;
|
package/startup.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* rpiv-workflow — ultra-thin startup-registration entry (~9ms): only the
|
|
3
|
+
* lifecycle + built-in registrars a sibling wires up at extension load, with no
|
|
4
|
+
* loader/DSL/runner graph. Pair `registerBuiltInsProvider` with a thunk that
|
|
5
|
+
* dynamically imports your definitions so they build on first `/wf`, not startup.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export { registerBuiltIns, registerBuiltInsProvider } from "./built-ins.js";
|
|
9
|
+
export { registerLifecycle } from "./lifecycle.js";
|
package/state/index.ts
CHANGED
package/state/reads.ts
CHANGED
|
@@ -133,6 +133,9 @@ export function listArtifacts(
|
|
|
133
133
|
* shape.
|
|
134
134
|
*
|
|
135
135
|
* Fail-soft like every other reader — never throws.
|
|
136
|
+
*
|
|
137
|
+
* Takes a concrete `runId`. For a user-supplied reference that may later need
|
|
138
|
+
* symbolic resolution (`@latest`, relative), call `resolveRun` instead.
|
|
136
139
|
*/
|
|
137
140
|
export function readHeader(cwd: string, runId: string): WorkflowHeader | undefined {
|
|
138
141
|
try {
|
|
@@ -149,6 +152,23 @@ export function readHeader(cwd: string, runId: string): WorkflowHeader | undefin
|
|
|
149
152
|
}
|
|
150
153
|
}
|
|
151
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Resolve a run *reference* to its header — the ref-resolution seam.
|
|
157
|
+
*
|
|
158
|
+
* Which to call: reach for `resolveRun` when the ref is **user-supplied** (the
|
|
159
|
+
* `/wf @<ref>` token, a CLI arg); reach for `readHeader` when you already hold
|
|
160
|
+
* a concrete `runId` (e.g. straight off `RunSummary.runId`). The split is
|
|
161
|
+
* intent, not behaviour: **today `resolveRun` is an exact alias of
|
|
162
|
+
* `readHeader`** (`ref === runId`). It exists as the single place a future
|
|
163
|
+
* symbolic resolver — `@latest`, relative refs, a friendly-name index
|
|
164
|
+
* (name → runId → readHeader) — slots in without touching any caller.
|
|
165
|
+
*
|
|
166
|
+
* Fail-soft like every reader — returns undefined when the ref doesn't resolve.
|
|
167
|
+
*/
|
|
168
|
+
export function resolveRun(cwd: string, ref: string): WorkflowHeader | undefined {
|
|
169
|
+
return readHeader(cwd, ref);
|
|
170
|
+
}
|
|
171
|
+
|
|
152
172
|
/**
|
|
153
173
|
* Enumerate every `<cwd>/.rpiv/workflows/runs/<run-id>.jsonl` and return its
|
|
154
174
|
* header projected as a `RunSummary`. Empty array when the runs
|
package/state/state.ts
CHANGED
package/types.ts
CHANGED
|
@@ -9,8 +9,9 @@
|
|
|
9
9
|
* layer. Always read the chain's primary artifact via
|
|
10
10
|
* `currentPrimaryArtifact(state)` (internal-utils.ts) — it prefers
|
|
11
11
|
* `output.artifacts[0]` and falls back to `fallbackPrimaryArtifact`.
|
|
12
|
-
* - `
|
|
13
|
-
* threaded from `withSession` callbacks down through stage/phase
|
|
12
|
+
* - `WorkflowHostContext` — the host port (defined in `host.js`, re-exported
|
|
13
|
+
* here) threaded from `withSession` callbacks down through stage/phase
|
|
14
|
+
* helpers, so the runtime layers import all three nouns from one module.
|
|
14
15
|
*
|
|
15
16
|
* Per-stage / per-phase sessions extend a shared `SessionContext` base
|
|
16
17
|
* (cwd, runId, state, prompt, skill). The audit layer pins its dependency
|
|
@@ -23,18 +24,14 @@
|
|
|
23
24
|
|
|
24
25
|
import type { StageDef, Workflow } from "./api.js";
|
|
25
26
|
import type { Artifact } from "./handle.js";
|
|
26
|
-
import type {
|
|
27
|
+
import type { WorkflowHost, WorkflowHostContext } from "./host.js";
|
|
27
28
|
import type { LifecycleDispatcher } from "./lifecycle.js";
|
|
28
29
|
import type { Output } from "./output.js";
|
|
29
30
|
import type { RunTrigger } from "./triggers.js";
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
* read stage/phase code without learning the port name. Identical
|
|
35
|
-
* shape; rename-only.
|
|
36
|
-
*/
|
|
37
|
-
export type RunnerCtx = WorkflowContext;
|
|
32
|
+
// Re-export the host port so runtime layers can pull `RunContext`,
|
|
33
|
+
// `RunState`, and the threaded ctx from this single runtime-types module.
|
|
34
|
+
export type { WorkflowHostContext } from "./host.js";
|
|
38
35
|
|
|
39
36
|
/** Mutable per-run bookkeeping threaded through the chain by reference. */
|
|
40
37
|
export interface RunState {
|
|
@@ -163,6 +160,14 @@ export interface RunContext {
|
|
|
163
160
|
trigger: RunTrigger;
|
|
164
161
|
/** Lifecycle event dispatcher — see `lifecycle.ts`. Threaded by reference. */
|
|
165
162
|
lifecycle: LifecycleDispatcher;
|
|
163
|
+
/**
|
|
164
|
+
* Optional cooperative-cancellation signal from `RunWorkflowOptions.signal`.
|
|
165
|
+
* Checked at the between-stage seam (top of `runStageOrRecordFailure`, before
|
|
166
|
+
* the start stage and before every routed next stage). An aborted signal
|
|
167
|
+
* records an `"aborted"` terminal row and unwinds — it does NOT interrupt a
|
|
168
|
+
* stage already streaming (Pi owns the live session).
|
|
169
|
+
*/
|
|
170
|
+
signal?: AbortSignal;
|
|
166
171
|
}
|
|
167
172
|
|
|
168
173
|
/**
|
|
@@ -218,8 +223,8 @@ export interface StageSession extends SessionContext {
|
|
|
218
223
|
continueHost?: WorkflowHost;
|
|
219
224
|
/** Only set for continue stages — branch slice offset. */
|
|
220
225
|
branchOffset?: number;
|
|
221
|
-
onFailure?: (ctx:
|
|
222
|
-
onSuccess: (ctx:
|
|
226
|
+
onFailure?: (ctx: WorkflowHostContext) => void;
|
|
227
|
+
onSuccess: (ctx: WorkflowHostContext, artifact: Artifact | undefined) => Promise<void>;
|
|
223
228
|
}
|
|
224
229
|
|
|
225
230
|
/**
|
|
@@ -241,5 +246,5 @@ export interface FanoutSession extends SessionContext {
|
|
|
241
246
|
id?: string;
|
|
242
247
|
/** Parent stage's 0-based index. */
|
|
243
248
|
stageIndex: number;
|
|
244
|
-
onSuccess: (ctx:
|
|
249
|
+
onSuccess: (ctx: WorkflowHostContext) => Promise<void>;
|
|
245
250
|
}
|