@juicesharp/rpiv-workflow 2.2.0 → 2.3.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/audit-ctx.ts +7 -7
- package/audit.ts +29 -29
- package/chain-state.ts +14 -3
- package/death-scene.ts +9 -9
- package/docs/workflow-authoring.md +2 -2
- package/events.ts +1 -1
- package/failure-memos.ts +4 -4
- package/handle.ts +16 -1
- package/host.ts +1 -1
- package/loop-def.ts +1 -1
- package/loop-kinds.ts +12 -12
- package/loop-parallel.ts +15 -15
- package/loop-waves.ts +3 -3
- package/loop.ts +32 -30
- package/messages.ts +30 -17
- package/outcomes/collectors/workspace-diff.ts +3 -3
- package/outcomes/git-commit.ts +11 -4
- package/output-spec.ts +7 -7
- package/package.json +2 -2
- package/preview.ts +8 -1
- package/registration.ts +3 -3
- package/routing.ts +1 -1
- package/runner/chain-advance.ts +49 -25
- package/runner/errors.ts +1 -1
- package/runner/failure.ts +22 -23
- package/runner/preflight.ts +3 -6
- package/runner/resolve-stage.ts +3 -3
- package/runner/resume-entry.ts +5 -5
- package/runner/resume-loop.ts +5 -5
- package/runner/resume.ts +1 -1
- package/runner/run-stage.ts +69 -64
- package/runner/runner.ts +4 -4
- package/runner/script-stage.ts +17 -17
- package/sessions/bash-strikes.ts +9 -9
- package/sessions/extraction.ts +19 -12
- package/sessions/halt-routing.ts +14 -14
- package/sessions/index.ts +1 -1
- package/sessions/reattach.ts +10 -10
- package/sessions/sessions.ts +29 -23
- package/sessions/spawn.ts +5 -5
- package/sessions/success-persist.ts +3 -3
- package/stage-def.ts +2 -2
- package/stage-errors.ts +1 -1
- package/state/index.ts +1 -1
- package/state/names.ts +1 -1
- package/state/reads.ts +1 -1
- package/state/state.ts +4 -4
- package/state/writes.ts +2 -2
- package/types.ts +16 -16
package/sessions/extraction.ts
CHANGED
|
@@ -28,10 +28,10 @@ import {
|
|
|
28
28
|
} from "../messages.js";
|
|
29
29
|
import { sideEffectOutcome } from "../outcomes/index.js";
|
|
30
30
|
import { finalizeOutput, type Output, outputMeta } from "../output.js";
|
|
31
|
-
import type {
|
|
31
|
+
import type { CollectContext, Outcome } from "../output-spec.js";
|
|
32
32
|
import { effectiveOutputSchemaOf } from "../stage-identity.js";
|
|
33
33
|
import { type BranchEntry, readBranch } from "../transcript.js";
|
|
34
|
-
import type {
|
|
34
|
+
import type { StageSessionContext, WorkflowSessionContext } from "../types.js";
|
|
35
35
|
import {
|
|
36
36
|
DEFAULT_VALIDATION_RETRIES,
|
|
37
37
|
DEFAULT_VALIDATION_RETRY_TIMEOUT_MS,
|
|
@@ -69,7 +69,7 @@ export type OutputProduction =
|
|
|
69
69
|
/** Retry loop re-produces against the latest branch after each fix request. */
|
|
70
70
|
export async function produceAndValidateOutput(
|
|
71
71
|
ctx: WorkflowSessionContext,
|
|
72
|
-
s:
|
|
72
|
+
s: StageSessionContext,
|
|
73
73
|
branch: BranchEntry[],
|
|
74
74
|
branchOffset: number | undefined,
|
|
75
75
|
): Promise<OutputProduction> {
|
|
@@ -107,7 +107,7 @@ function resolveOutcome(stage: StageDef, skill: string): Outcome {
|
|
|
107
107
|
return sideEffectOutcome;
|
|
108
108
|
case "produces":
|
|
109
109
|
throw new Error(
|
|
110
|
-
`
|
|
110
|
+
`dispatchStage: stage "${skill}" has kind "produces" but no \`outcome\` — ` +
|
|
111
111
|
"there is no framework default for produces stages (the `.rpiv/artifacts/` layout is " +
|
|
112
112
|
"an rpiv-pi convention). Either wire `outcome: rpivArtifactMdOutcome` (from @juicesharp/rpiv-pi) " +
|
|
113
113
|
"or supply your own `{ collector, parser? }`.",
|
|
@@ -124,7 +124,11 @@ function resolveOutcome(stage: StageDef, skill: string): Outcome {
|
|
|
124
124
|
* demand via the `branchOffset` field. Initial production and retry
|
|
125
125
|
* production use the same offset value.
|
|
126
126
|
*/
|
|
127
|
-
function buildCollectCtx(
|
|
127
|
+
function buildCollectCtx(
|
|
128
|
+
s: StageSessionContext,
|
|
129
|
+
branch: BranchEntry[],
|
|
130
|
+
branchOffset: number | undefined,
|
|
131
|
+
): CollectContext {
|
|
128
132
|
return {
|
|
129
133
|
cwd: s.cwd,
|
|
130
134
|
runId: s.runId,
|
|
@@ -137,7 +141,10 @@ function buildCollectCtx(s: StageSession, branch: BranchEntry[], branchOffset: n
|
|
|
137
141
|
};
|
|
138
142
|
}
|
|
139
143
|
|
|
140
|
-
function wrapOutput(
|
|
144
|
+
function wrapOutput(
|
|
145
|
+
s: StageSessionContext,
|
|
146
|
+
parts: { kind: string; artifacts: readonly Artifact[]; data: unknown },
|
|
147
|
+
): Output {
|
|
141
148
|
return finalizeOutput(
|
|
142
149
|
parts,
|
|
143
150
|
outputMeta({
|
|
@@ -167,7 +174,7 @@ type RunOutcomeResult = { kind: "ok"; output: Output } | Fatal;
|
|
|
167
174
|
*/
|
|
168
175
|
async function runOutcome(
|
|
169
176
|
outcome: Outcome,
|
|
170
|
-
ctx:
|
|
177
|
+
ctx: CollectContext,
|
|
171
178
|
finalize: (parts: { kind: string; artifacts: readonly Artifact[]; data: unknown }) => Output,
|
|
172
179
|
): Promise<RunOutcomeResult> {
|
|
173
180
|
let collected: Awaited<ReturnType<typeof outcome.collector.collect>>;
|
|
@@ -223,19 +230,19 @@ function enforceCompletionContract(
|
|
|
223
230
|
return { kind: "ok", output };
|
|
224
231
|
}
|
|
225
232
|
|
|
226
|
-
function shouldValidateOutput(s:
|
|
233
|
+
function shouldValidateOutput(s: StageSessionContext, output: Output): boolean {
|
|
227
234
|
return !!(effectiveOutputSchemaOf(s.stage, s.stageName, s.skillContracts) && output.data !== undefined);
|
|
228
235
|
}
|
|
229
236
|
|
|
230
237
|
interface RetryDeps {
|
|
231
238
|
outcome: Outcome;
|
|
232
|
-
collectCtx:
|
|
239
|
+
collectCtx: CollectContext;
|
|
233
240
|
finalize: (parts: { kind: string; artifacts: readonly Artifact[]; data: unknown }) => Output;
|
|
234
241
|
}
|
|
235
242
|
|
|
236
243
|
async function retryUntilValid(
|
|
237
244
|
ctx: WorkflowSessionContext,
|
|
238
|
-
s:
|
|
245
|
+
s: StageSessionContext,
|
|
239
246
|
deps: RetryDeps,
|
|
240
247
|
initial: Output,
|
|
241
248
|
): Promise<OutputProduction> {
|
|
@@ -279,7 +286,7 @@ async function retryUntilValid(
|
|
|
279
286
|
// no contract re-check (both ran before the loop opened).
|
|
280
287
|
if (attempt === 0) return { kind: "ok", value: initial };
|
|
281
288
|
const retryBranch = readBranch(ctx);
|
|
282
|
-
const retryCtx:
|
|
289
|
+
const retryCtx: CollectContext = { ...deps.collectCtx, branch: retryBranch };
|
|
283
290
|
const reRun = await runOutcome(deps.outcome, retryCtx, deps.finalize);
|
|
284
291
|
if (reRun.kind === "fatal") return { kind: "aborted", abort: reRun };
|
|
285
292
|
const contract = enforceCompletionContract(s.stage, s.skill, reRun.output);
|
|
@@ -372,7 +379,7 @@ async function validateOrFatal(
|
|
|
372
379
|
|
|
373
380
|
async function askAgentToFix(
|
|
374
381
|
ctx: WorkflowSessionContext,
|
|
375
|
-
s:
|
|
382
|
+
s: StageSessionContext,
|
|
376
383
|
attempt: number,
|
|
377
384
|
failures: SchemaValidationFailure[],
|
|
378
385
|
timeoutMs: number,
|
package/sessions/halt-routing.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* lifecycle signal borrows `unitEventOf` from here).
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import { type
|
|
18
|
+
import { type AuditContext, failedArgs, recordFatalFailure, recordStopFailure, recordUnitHalt } from "../audit.js";
|
|
19
19
|
import { allocateStageNumber } from "../audit-rows.js";
|
|
20
20
|
import { lifecycleCtxFromSession, skillStageRef } from "../events.js";
|
|
21
21
|
import { nowIso } from "../internal-utils.js";
|
|
@@ -23,7 +23,7 @@ import { FAIL_VALIDATION_EXHAUSTED, MSG_STAGE_FAILED } from "../messages.js";
|
|
|
23
23
|
import { failedOutput, type OutputMeta, outputMeta } from "../output.js";
|
|
24
24
|
import type { SessionRef } from "../state/index.js";
|
|
25
25
|
import type { StopSignal } from "../transcript.js";
|
|
26
|
-
import type {
|
|
26
|
+
import type { StageSessionContext, WorkflowHostContext } from "../types.js";
|
|
27
27
|
import { unitEventOf } from "./success-persist.js";
|
|
28
28
|
|
|
29
29
|
/**
|
|
@@ -48,7 +48,7 @@ type HaltReason =
|
|
|
48
48
|
*/
|
|
49
49
|
export async function haltStageOrSoftHalt(
|
|
50
50
|
ctx: WorkflowHostContext,
|
|
51
|
-
s:
|
|
51
|
+
s: StageSessionContext,
|
|
52
52
|
reason: HaltReason,
|
|
53
53
|
session: SessionRef | null,
|
|
54
54
|
): Promise<void> {
|
|
@@ -57,7 +57,7 @@ export async function haltStageOrSoftHalt(
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/** Collect-all reason text per arm — byte-identical to the prior inline strings. */
|
|
60
|
-
function softHaltReason(s:
|
|
60
|
+
function softHaltReason(s: StageSessionContext, reason: HaltReason): string {
|
|
61
61
|
switch (reason.kind) {
|
|
62
62
|
case "stop":
|
|
63
63
|
return `${s.skill} stopped (${reason.stop})`;
|
|
@@ -73,7 +73,7 @@ function softHaltReason(s: StageSession, reason: HaltReason): string {
|
|
|
73
73
|
/** Fail-fast terminal halt per arm — dispatches to the existing helpers, unchanged. */
|
|
74
74
|
function failFastHalt(
|
|
75
75
|
ctx: WorkflowHostContext,
|
|
76
|
-
s:
|
|
76
|
+
s: StageSessionContext,
|
|
77
77
|
reason: HaltReason,
|
|
78
78
|
session: SessionRef | null,
|
|
79
79
|
): Promise<void> {
|
|
@@ -93,7 +93,7 @@ function failFastHalt(
|
|
|
93
93
|
|
|
94
94
|
async function haltStage(
|
|
95
95
|
ctx: WorkflowHostContext,
|
|
96
|
-
s:
|
|
96
|
+
s: StageSessionContext,
|
|
97
97
|
stop: Exclude<StopSignal, "stop">,
|
|
98
98
|
session: SessionRef | null,
|
|
99
99
|
): Promise<void> {
|
|
@@ -102,11 +102,11 @@ async function haltStage(
|
|
|
102
102
|
|
|
103
103
|
async function haltStageWithExtractionError(
|
|
104
104
|
ctx: WorkflowHostContext,
|
|
105
|
-
s:
|
|
105
|
+
s: StageSessionContext,
|
|
106
106
|
message: string,
|
|
107
107
|
session: SessionRef | null,
|
|
108
108
|
): Promise<void> {
|
|
109
|
-
await
|
|
109
|
+
await recordFatalFailure(
|
|
110
110
|
ctx,
|
|
111
111
|
auditFor(s, session),
|
|
112
112
|
{ status: "failed", notifyMsg: MSG_STAGE_FAILED(s.skill), notifyLevel: "error", errMsg: message },
|
|
@@ -125,7 +125,7 @@ async function haltStageWithExtractionError(
|
|
|
125
125
|
*/
|
|
126
126
|
async function softHaltUnit(
|
|
127
127
|
ctx: WorkflowHostContext,
|
|
128
|
-
s:
|
|
128
|
+
s: StageSessionContext,
|
|
129
129
|
reason: string,
|
|
130
130
|
session: SessionRef | null,
|
|
131
131
|
): Promise<void> {
|
|
@@ -150,7 +150,7 @@ async function softHaltUnit(
|
|
|
150
150
|
|
|
151
151
|
/** OutputMeta for a sentinel — same stage number the failed row carries, so the
|
|
152
152
|
* live sentinel and the resume-rebuilt one are byte-identical. */
|
|
153
|
-
function outputMetaFor(s:
|
|
153
|
+
function outputMetaFor(s: StageSessionContext): OutputMeta {
|
|
154
154
|
return outputMeta({
|
|
155
155
|
stage: s.stageName,
|
|
156
156
|
skill: s.skill,
|
|
@@ -163,11 +163,11 @@ function outputMetaFor(s: StageSession): OutputMeta {
|
|
|
163
163
|
/** Exported to the `reattach.ts` companion — a promotion's validation-exhausted halt is identical to live. */
|
|
164
164
|
export async function haltStageWithValidationFailure(
|
|
165
165
|
ctx: WorkflowHostContext,
|
|
166
|
-
s:
|
|
166
|
+
s: StageSessionContext,
|
|
167
167
|
failureSummary: string,
|
|
168
168
|
session: SessionRef | null,
|
|
169
169
|
): Promise<void> {
|
|
170
|
-
await
|
|
170
|
+
await recordFatalFailure(
|
|
171
171
|
ctx,
|
|
172
172
|
auditFor(s, session),
|
|
173
173
|
failedArgs(FAIL_VALIDATION_EXHAUSTED(s.skill, failureSummary)),
|
|
@@ -175,7 +175,7 @@ export async function haltStageWithValidationFailure(
|
|
|
175
175
|
);
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
-
const auditFor = (s:
|
|
178
|
+
const auditFor = (s: StageSessionContext, session: SessionRef | null): AuditContext => ({
|
|
179
179
|
cwd: s.cwd,
|
|
180
180
|
runId: s.runId,
|
|
181
181
|
state: s.state,
|
|
@@ -190,7 +190,7 @@ const auditFor = (s: StageSession, session: SessionRef | null): AuditCtx => ({
|
|
|
190
190
|
// began) — a failure row reuses it instead of burning a second number.
|
|
191
191
|
allocatedStageNumber: s.allocatedStageNumber,
|
|
192
192
|
// Host-injected persisted-session branch reader — the death-scene artifact
|
|
193
|
-
// writer reads it off
|
|
193
|
+
// writer reads it off AuditContext. Absent for programmatic embedders / no
|
|
194
194
|
// provider (the writer degrades silently). Conditional spread keeps the
|
|
195
195
|
// common case (reader present) byte-clean and the no-provider case undefined.
|
|
196
196
|
...(s.readSessionBranch ? { readSessionBranch: s.readSessionBranch } : {}),
|
package/sessions/index.ts
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
|
|
7
7
|
export { locateSessionFile, pruneOrphanedChildSessions } from "./locate.js";
|
|
8
8
|
export { reattachStageSession } from "./reattach.js";
|
|
9
|
-
export { continueStageSession,
|
|
9
|
+
export { continueStageSession, executeStageSession } from "./sessions.js";
|
package/sessions/reattach.ts
CHANGED
|
@@ -29,21 +29,21 @@
|
|
|
29
29
|
|
|
30
30
|
import { MSG_RESUME_PROMOTED, MSG_RESUME_REATTACHED, REATTACH_PROMPT } from "../messages.js";
|
|
31
31
|
import { readBranch, readSessionRef } from "../transcript.js";
|
|
32
|
-
import type {
|
|
32
|
+
import type { StageSessionContext, WorkflowHostContext, WorkflowSessionContext } from "../types.js";
|
|
33
33
|
import { produceAndValidateOutput } from "./extraction.js";
|
|
34
34
|
import { haltStageWithValidationFailure } from "./halt-routing.js";
|
|
35
35
|
import { postStage } from "./sessions.js";
|
|
36
36
|
import { branchOffsetFor, resendIntoChild } from "./spawn.js";
|
|
37
37
|
import { recordStageSuccess } from "./success-persist.js";
|
|
38
38
|
|
|
39
|
-
// Same two-ctx split as `postStage`: `
|
|
39
|
+
// Same two-ctx split as `postStage`: `observerCtx` is the long-lived launcher/observer
|
|
40
40
|
// (user-facing notifications + record + the chain continuation that spawns the
|
|
41
41
|
// next stage); `child` is the reopened persisted session (branch reads + the
|
|
42
42
|
// reattach nudge).
|
|
43
43
|
export async function reattachStageSession(
|
|
44
|
-
|
|
44
|
+
observerCtx: WorkflowHostContext,
|
|
45
45
|
child: WorkflowSessionContext,
|
|
46
|
-
s:
|
|
46
|
+
s: StageSessionContext,
|
|
47
47
|
): Promise<void> {
|
|
48
48
|
// Promotion: extraction over the adopted branch, scoped by the SAME
|
|
49
49
|
// offset the interrupted activation ran under (persisted on its row and
|
|
@@ -53,19 +53,19 @@ export async function reattachStageSession(
|
|
|
53
53
|
const result = await produceAndValidateOutput(child, s, readBranch(child), offset);
|
|
54
54
|
|
|
55
55
|
if (result.kind === "ok") {
|
|
56
|
-
|
|
57
|
-
if (!(await recordStageSuccess(
|
|
58
|
-
await s.onSuccess(
|
|
56
|
+
observerCtx.ui.notify(MSG_RESUME_PROMOTED(s.skill), "info");
|
|
57
|
+
if (!(await recordStageSuccess(observerCtx, s, result.output, session))) return;
|
|
58
|
+
await s.onSuccess(observerCtx, result.output);
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
61
|
if (result.kind === "validation-exhausted") {
|
|
62
|
-
return haltStageWithValidationFailure(
|
|
62
|
+
return haltStageWithValidationFailure(observerCtx, s, result.failureSummary, session);
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// Promotion missed (collector-fatal) — reattach: nudge the session from
|
|
66
66
|
// its leaf, let the agent finish with full prior context, then run the
|
|
67
67
|
// standard post-session pipeline.
|
|
68
|
-
|
|
68
|
+
observerCtx.ui.notify(MSG_RESUME_REATTACHED(s.skill), "info");
|
|
69
69
|
await resendIntoChild(child, REATTACH_PROMPT(s.skill));
|
|
70
|
-
await postStage(
|
|
70
|
+
await postStage(observerCtx, child, s);
|
|
71
71
|
}
|
package/sessions/sessions.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Session execution — one Pi session per workflow stage / loop unit.
|
|
3
|
-
* `
|
|
4
|
-
* threading their identity via `
|
|
3
|
+
* `executeStageSession` is the only public entry (loop units run through it too,
|
|
4
|
+
* threading their identity via `StageSessionContext.unit`).
|
|
5
5
|
*
|
|
6
6
|
* Every stage runs in its own detached child session (`spawnChildAndRun`,
|
|
7
7
|
* spawn.ts); the only surviving policy divergence is the branch offset
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
|
|
26
26
|
import { WorkflowAbortError } from "../internal-utils.js";
|
|
27
27
|
import { type BranchEntry, classifyStop, readBranch, readSessionRef, type StopSignal } from "../transcript.js";
|
|
28
|
-
import type {
|
|
28
|
+
import type { StageSessionContext, WorkflowHostContext, WorkflowSessionContext } from "../types.js";
|
|
29
29
|
import { bashStrikesRemaining, bashTimeoutSteeringMessage, consumeBashStrike } from "./bash-strikes.js";
|
|
30
30
|
import { produceAndValidateOutput } from "./extraction.js";
|
|
31
31
|
import { haltStageOrSoftHalt } from "./halt-routing.js";
|
|
@@ -37,7 +37,7 @@ import { recordStageSuccess } from "./success-persist.js";
|
|
|
37
37
|
// ===========================================================================
|
|
38
38
|
|
|
39
39
|
/** Execute one DAG stage (or loop unit) in its own detached child session. */
|
|
40
|
-
export async function
|
|
40
|
+
export async function executeStageSession(ctx: WorkflowHostContext, s: StageSessionContext): Promise<void> {
|
|
41
41
|
await spawnChildAndRun(ctx, s, (child) => postStage(ctx, child, s));
|
|
42
42
|
}
|
|
43
43
|
|
|
@@ -56,13 +56,13 @@ export async function runStageSession(ctx: WorkflowHostContext, s: StageSession)
|
|
|
56
56
|
* branch ran under; resume re-applies that persisted value verbatim.
|
|
57
57
|
*/
|
|
58
58
|
export async function continueStageSession(
|
|
59
|
-
|
|
59
|
+
observerCtx: WorkflowHostContext,
|
|
60
60
|
child: WorkflowSessionContext,
|
|
61
|
-
s:
|
|
61
|
+
s: StageSessionContext,
|
|
62
62
|
): Promise<void> {
|
|
63
63
|
const offset = readBranch(child).length;
|
|
64
64
|
await resendIntoChild(child, s.prompt);
|
|
65
|
-
await postStage(
|
|
65
|
+
await postStage(observerCtx, child, s, offset);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
// ===========================================================================
|
|
@@ -74,7 +74,7 @@ export async function continueStageSession(
|
|
|
74
74
|
* persist → chain. Exported to the `reattach.ts` companion — a reattached
|
|
75
75
|
* session's continuation runs this exact pipeline, byte-identical to live.
|
|
76
76
|
*
|
|
77
|
-
* TWO ctxs (detachment): `
|
|
77
|
+
* TWO ctxs (detachment): `observerCtx` is the long-lived LAUNCHER/observer ctx the
|
|
78
78
|
* walk threads — it stays valid across every stage, so the user-facing recording
|
|
79
79
|
* (success/halt rows + notifications + lifecycle) AND the chain continuation
|
|
80
80
|
* (`onSuccess` → advance/step, which spawns the NEXT stage's child) all run on
|
|
@@ -82,7 +82,7 @@ export async function continueStageSession(
|
|
|
82
82
|
* background lane — and which is disposed when the stage ends). `child` is the
|
|
83
83
|
* in-session ctx: the agent transcript (`readBranch`/`readSessionRef`) and the
|
|
84
84
|
* validation-retry re-prompt (`produceAndValidateOutput` → `resendIntoChild`)
|
|
85
|
-
* read/write through it. Spawning the next stage off `
|
|
85
|
+
* read/write through it. Spawning the next stage off `observerCtx` is what keeps the
|
|
86
86
|
* launcher the single spawner (no nested-child chain).
|
|
87
87
|
*
|
|
88
88
|
* The backing `SessionRef` is captured ONCE at entry — every row this
|
|
@@ -90,9 +90,9 @@ export async function continueStageSession(
|
|
|
90
90
|
* carries the same provenance value.
|
|
91
91
|
*/
|
|
92
92
|
export async function postStage(
|
|
93
|
-
|
|
93
|
+
observerCtx: WorkflowHostContext,
|
|
94
94
|
child: WorkflowSessionContext,
|
|
95
|
-
s:
|
|
95
|
+
s: StageSessionContext,
|
|
96
96
|
// Defaults to the policy-derived offset (fresh ⇒ undefined; resume continue ⇒
|
|
97
97
|
// the persisted row's value). The live continue body passes the value it
|
|
98
98
|
// re-derived from the forked branch, which is authoritative there.
|
|
@@ -124,30 +124,36 @@ export async function postStage(
|
|
|
124
124
|
// verbatim); exhaustion falls through to the UNCHANGED soft-halt/terminal
|
|
125
125
|
// seam below, where the failure-row writers (memo + death-scene) fire for free.
|
|
126
126
|
if (consumeBashStrike(s, timeout.reason)) {
|
|
127
|
-
return retryStageAfterBashStrike(
|
|
127
|
+
return retryStageAfterBashStrike(observerCtx, child, s, offset, timeout.reason);
|
|
128
128
|
}
|
|
129
|
-
return haltStageOrSoftHalt(
|
|
129
|
+
return haltStageOrSoftHalt(observerCtx, s, { kind: "timeout", reason: timeout.reason }, session);
|
|
130
130
|
}
|
|
131
131
|
throw new WorkflowAbortError();
|
|
132
132
|
}
|
|
133
133
|
// Every halt below routes through the single `haltStageOrSoftHalt` gate: a
|
|
134
134
|
// fanout unit marked `collectAll` records a NON-terminal failed row + a sentinel
|
|
135
135
|
// slot instead of halting the run; everything else takes the arm's fail-fast
|
|
136
|
-
// halt. Recording + the continuation run on
|
|
136
|
+
// halt. Recording + the continuation run on observerCtx (the launcher) — the per-stage
|
|
137
137
|
// child is disposed when the stage ends.
|
|
138
|
-
if (outcome.stop !== "stop")
|
|
138
|
+
if (outcome.stop !== "stop")
|
|
139
|
+
return haltStageOrSoftHalt(observerCtx, s, { kind: "stop", stop: outcome.stop }, session);
|
|
139
140
|
|
|
140
141
|
const result = await produceAndValidateOutput(child, s, outcome.branch, offset);
|
|
141
142
|
if (result.kind === "fatal")
|
|
142
|
-
return haltStageOrSoftHalt(
|
|
143
|
+
return haltStageOrSoftHalt(observerCtx, s, { kind: "extraction", message: result.message }, session);
|
|
143
144
|
if (result.kind === "validation-exhausted")
|
|
144
|
-
return haltStageOrSoftHalt(
|
|
145
|
+
return haltStageOrSoftHalt(
|
|
146
|
+
observerCtx,
|
|
147
|
+
s,
|
|
148
|
+
{ kind: "validation", failureSummary: result.failureSummary },
|
|
149
|
+
session,
|
|
150
|
+
);
|
|
145
151
|
|
|
146
|
-
if (!(await recordStageSuccess(
|
|
152
|
+
if (!(await recordStageSuccess(observerCtx, s, result.output, session))) return;
|
|
147
153
|
// The validated Output goes to the continuation directly — loop drivers
|
|
148
154
|
// thread it into accumulated / feedForward without state back-reads. Runs on
|
|
149
|
-
//
|
|
150
|
-
await s.onSuccess(
|
|
155
|
+
// observerCtx so the next stage's child is spawned off the launcher.
|
|
156
|
+
await s.onSuccess(observerCtx, result.output);
|
|
151
157
|
}
|
|
152
158
|
|
|
153
159
|
// ===========================================================================
|
|
@@ -165,16 +171,16 @@ export async function postStage(
|
|
|
165
171
|
* failure-row writers (memo + death-scene artifact) fire for free.
|
|
166
172
|
*/
|
|
167
173
|
async function retryStageAfterBashStrike(
|
|
168
|
-
|
|
174
|
+
observerCtx: WorkflowHostContext,
|
|
169
175
|
child: WorkflowSessionContext,
|
|
170
|
-
s:
|
|
176
|
+
s: StageSessionContext,
|
|
171
177
|
offset: number | undefined,
|
|
172
178
|
reason: string,
|
|
173
179
|
): Promise<void> {
|
|
174
180
|
child.resetToolTimeout?.();
|
|
175
181
|
const remaining = bashStrikesRemaining(s);
|
|
176
182
|
await resendIntoChild(child, bashTimeoutSteeringMessage(reason, remaining, remaining === 0));
|
|
177
|
-
return postStage(
|
|
183
|
+
return postStage(observerCtx, child, s, offset);
|
|
178
184
|
}
|
|
179
185
|
|
|
180
186
|
// ===========================================================================
|
package/sessions/spawn.ts
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
21
|
import type { SessionPolicy } from "../api.js";
|
|
22
|
-
import type {
|
|
22
|
+
import type { StageSessionContext, WorkflowHostContext, WorkflowSessionContext } from "../types.js";
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* The per-policy branch offset — fresh ignores the stage-captured value;
|
|
@@ -54,7 +54,7 @@ type OpenMode = { reattach: { sessionFile: string } } | { fork: { sessionFile: s
|
|
|
54
54
|
*/
|
|
55
55
|
function openChild(
|
|
56
56
|
ctx: WorkflowHostContext,
|
|
57
|
-
s: Pick<
|
|
57
|
+
s: Pick<StageSessionContext, "prompt" | "model" | "signal" | "laneUnitIndex">,
|
|
58
58
|
body: (child: WorkflowSessionContext) => Promise<void>,
|
|
59
59
|
mode?: OpenMode,
|
|
60
60
|
): Promise<void> {
|
|
@@ -81,7 +81,7 @@ function openChild(
|
|
|
81
81
|
*/
|
|
82
82
|
export function spawnChildAndRun(
|
|
83
83
|
ctx: WorkflowHostContext,
|
|
84
|
-
s: Pick<
|
|
84
|
+
s: Pick<StageSessionContext, "prompt" | "model" | "signal" | "laneUnitIndex">,
|
|
85
85
|
body: (child: WorkflowSessionContext) => Promise<void>,
|
|
86
86
|
): Promise<void> {
|
|
87
87
|
return openChild(ctx, s, body);
|
|
@@ -102,7 +102,7 @@ export function spawnChildAndRun(
|
|
|
102
102
|
*/
|
|
103
103
|
export function reattachChildSession(
|
|
104
104
|
ctx: WorkflowHostContext,
|
|
105
|
-
s: Pick<
|
|
105
|
+
s: Pick<StageSessionContext, "prompt" | "model" | "signal" | "laneUnitIndex">,
|
|
106
106
|
sessionFile: string,
|
|
107
107
|
body: (child: WorkflowSessionContext) => Promise<void>,
|
|
108
108
|
): Promise<void> {
|
|
@@ -127,7 +127,7 @@ export function reattachChildSession(
|
|
|
127
127
|
*/
|
|
128
128
|
export function forkChildSession(
|
|
129
129
|
ctx: WorkflowHostContext,
|
|
130
|
-
s: Pick<
|
|
130
|
+
s: Pick<StageSessionContext, "prompt" | "model" | "signal" | "laneUnitIndex">,
|
|
131
131
|
sessionFile: string,
|
|
132
132
|
body: (child: WorkflowSessionContext) => Promise<void>,
|
|
133
133
|
): Promise<void> {
|
|
@@ -14,7 +14,7 @@ import { persistStageSuccess, rollLastSession } from "../audit-rows.js";
|
|
|
14
14
|
import { lifecycleCtxFromSession, skillStageRef, type UnitEvent } from "../events.js";
|
|
15
15
|
import type { Output } from "../output.js";
|
|
16
16
|
import type { SessionRef } from "../state/index.js";
|
|
17
|
-
import type {
|
|
17
|
+
import type { StageSessionContext, WorkflowHostContext } from "../types.js";
|
|
18
18
|
import { bashTimeoutStrikeHistory } from "./bash-strikes.js";
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -39,7 +39,7 @@ import { bashTimeoutStrikeHistory } from "./bash-strikes.js";
|
|
|
39
39
|
*/
|
|
40
40
|
export async function recordStageSuccess(
|
|
41
41
|
ctx: WorkflowHostContext,
|
|
42
|
-
s:
|
|
42
|
+
s: StageSessionContext,
|
|
43
43
|
output: Output,
|
|
44
44
|
session: SessionRef | null,
|
|
45
45
|
): Promise<boolean> {
|
|
@@ -89,7 +89,7 @@ export async function recordStageSuccess(
|
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
/** Public `UnitEvent` payload from the session's `UnitRef` + dispatched skill. */
|
|
92
|
-
export function unitEventOf(s:
|
|
92
|
+
export function unitEventOf(s: StageSessionContext): UnitEvent {
|
|
93
93
|
const u = s.unit!;
|
|
94
94
|
return { role: u.role, index: u.index, unitId: u.id, label: u.label, skill: s.skill };
|
|
95
95
|
}
|
package/stage-def.ts
CHANGED
|
@@ -515,7 +515,7 @@ function actsScript<TIn = unknown>(opts: ActsScriptOptions<TIn>): StageDef<TIn,
|
|
|
515
515
|
// / `{ kind: "stop" }` in routing.ts: no outgoing edge OR explicit `STOP`;
|
|
516
516
|
// orthogonal to this factory — a `terminal()` stage can route onward, and a
|
|
517
517
|
// plain stage can be a sink) and SENSE 3 (run-outcome prose — "terminal
|
|
518
|
-
// failure/outcome" = a halt that ends the run, e.g. audit.ts `
|
|
518
|
+
// failure/outcome" = a halt that ends the run, e.g. audit.ts `recordFailureRow`).
|
|
519
519
|
// See the `terminal` export doc below for the full glossary.
|
|
520
520
|
function terminalFn(overrides: Partial<StageDef> = {}): StageDef {
|
|
521
521
|
return withDefaults("side-effect", { ...overrides, inheritsArtifacts: false });
|
|
@@ -589,7 +589,7 @@ export const acts = Object.assign(actsFn, { script: actsScript, prompt: actsProm
|
|
|
589
589
|
* factory — a `terminal()` stage can route onward, a plain stage can be a
|
|
590
590
|
* sink.
|
|
591
591
|
* 3. RUN OUTCOME ("terminal failure/outcome" prose, e.g. audit.ts
|
|
592
|
-
* `
|
|
592
|
+
* `recordFailureRow` / `recordFatalFailure`): a failure/cancellation/
|
|
593
593
|
* abort that ends the run.
|
|
594
594
|
*/
|
|
595
595
|
export const terminal = Object.assign(terminalFn, { script: terminalScript });
|
package/stage-errors.ts
CHANGED
|
@@ -14,7 +14,7 @@ import type { FailureText } from "./messages.js";
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Thrown by a preflight check on failure; carries the recorded-row
|
|
17
|
-
* attribution + notify/err messages so `
|
|
17
|
+
* attribution + notify/err messages so `dispatchStageOrRecordFailure` can land
|
|
18
18
|
* a uniform JSONL row regardless of which slot tripped.
|
|
19
19
|
*
|
|
20
20
|
* `kind` annotates the violation class for diagnostics only — control
|
package/state/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ export type {
|
|
|
16
16
|
WorkflowStage,
|
|
17
17
|
} from "./state.js";
|
|
18
18
|
export {
|
|
19
|
+
appendHeader,
|
|
19
20
|
appendLoopCap,
|
|
20
21
|
appendRoutingDecision,
|
|
21
22
|
appendStage,
|
|
@@ -40,5 +41,4 @@ export {
|
|
|
40
41
|
STATE_SCHEMA_VERSION,
|
|
41
42
|
stateFilePath,
|
|
42
43
|
VALID_NAME,
|
|
43
|
-
writeHeader,
|
|
44
44
|
} from "./state.js";
|
package/state/names.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*
|
|
6
6
|
* Internal module — not re-exported from registration.ts. The runner reserves
|
|
7
7
|
* names through `claimName` (the single in-process door: validate →
|
|
8
|
-
* collision-check → persist) BEFORE `
|
|
8
|
+
* collision-check → persist) BEFORE `appendHeader`. External consumers resolve
|
|
9
9
|
* names through `resolveRun`.
|
|
10
10
|
*
|
|
11
11
|
* Durability: every write goes through `writeNamesIndex` — temp file +
|
package/state/reads.ts
CHANGED
|
@@ -27,7 +27,7 @@ import type { LoopCapRow, RoutingDecision, RunSummary, WorkflowHeader, WorkflowS
|
|
|
27
27
|
* Reads every line, filters by shape (not position). Header has no
|
|
28
28
|
* `stageNumber`; routing rows carry `type: "routing"`; stage rows have
|
|
29
29
|
* `stageNumber: number` and no `type`. Starting at line 0 keeps the first
|
|
30
|
-
* stage row recoverable even if a transient
|
|
30
|
+
* stage row recoverable even if a transient appendHeader failure left the
|
|
31
31
|
* file without its header.
|
|
32
32
|
*
|
|
33
33
|
* Each line's `JSON.parse` runs in its own try/catch — a truncated trailing
|
package/state/state.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*
|
|
13
13
|
* Internally split into three modules:
|
|
14
14
|
* - paths.ts — runsDir + stateFilePath + generateRunId
|
|
15
|
-
* - writes.ts — tryAppendJsonl +
|
|
15
|
+
* - writes.ts — tryAppendJsonl + appendHeader + appendStage +
|
|
16
16
|
* appendRoutingDecision
|
|
17
17
|
* - reads.ts — readLastStage + readAllStages + readRoutingDecisions +
|
|
18
18
|
* listArtifacts + readHeader + listRuns
|
|
@@ -103,7 +103,7 @@ export interface WorkflowStage {
|
|
|
103
103
|
output?: Output;
|
|
104
104
|
/**
|
|
105
105
|
* Reason a terminal-failure row was written — mirrors the
|
|
106
|
-
* `state.termination.error` set by `
|
|
106
|
+
* `state.termination.error` set by `recordFatalFailure`. Present
|
|
107
107
|
* only on `status: "failed" | "aborted"` rows; absent on completed /
|
|
108
108
|
* skipped rows. Persisting it here means post-mortems work from
|
|
109
109
|
* JSONL alone, without depending on a transient `ctx.ui.notify` toast.
|
|
@@ -136,7 +136,7 @@ export interface WorkflowStage {
|
|
|
136
136
|
/**
|
|
137
137
|
* Marks a NON-terminal collect-all fanout unit halt (`recordUnitHalt`):
|
|
138
138
|
* the unit failed but the run survives and a `failedOutput` sentinel fills its
|
|
139
|
-
* declared slot. Distinguishes a SOFT halt from a hard `
|
|
139
|
+
* declared slot. Distinguishes a SOFT halt from a hard `recordFatalFailure`
|
|
140
140
|
* row (byte-identical otherwise) so the resume fold rebuilds the
|
|
141
141
|
* sentinel by `unitIndex` rather than re-dispatching the unit. Absent on every
|
|
142
142
|
* other row (`undefined` is dropped by `JSON.stringify`).
|
|
@@ -258,4 +258,4 @@ export {
|
|
|
258
258
|
readRoutingDecisions,
|
|
259
259
|
} from "./reads.js";
|
|
260
260
|
export { resolveRun } from "./resolve.js";
|
|
261
|
-
export { appendLoopCap, appendRoutingDecision, appendStage
|
|
261
|
+
export { appendHeader, appendLoopCap, appendRoutingDecision, appendStage } from "./writes.js";
|
package/state/writes.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* swapping the backend means touching all of `state/` (a `RunStore` port
|
|
8
8
|
* is a known possible follow-up, deliberately deferred).
|
|
9
9
|
*
|
|
10
|
-
*
|
|
10
|
+
* appendHeader — boolean; the runner refuses the run start on failure.
|
|
11
11
|
* appendStage — boolean; allocator gates monotonic counters on it.
|
|
12
12
|
* appendRoutingDecision — boolean; telemetry-not-state, dropped rows surface up.
|
|
13
13
|
*/
|
|
@@ -42,7 +42,7 @@ function tryAppendJsonl(cwd: string, runId: string, row: unknown): boolean {
|
|
|
42
42
|
* while stage rows still land — so `runWorkflow` refuses the run start on
|
|
43
43
|
* false, before anything has executed.
|
|
44
44
|
*/
|
|
45
|
-
export function
|
|
45
|
+
export function appendHeader(cwd: string, header: WorkflowHeader): boolean {
|
|
46
46
|
return tryAppendJsonl(cwd, header.runId, header);
|
|
47
47
|
}
|
|
48
48
|
|