@juicesharp/rpiv-workflow 1.19.0 → 1.20.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 +81 -8
- package/api.ts +18 -703
- package/audit-rows.ts +136 -0
- package/audit.ts +139 -126
- package/built-ins.ts +20 -45
- package/chain-state.ts +169 -0
- package/command-run.ts +31 -3
- package/command.ts +116 -20
- package/docs/workflow-authoring.md +322 -61
- package/{lifecycle.ts → events.ts} +119 -35
- package/host.ts +30 -0
- package/index.ts +25 -81
- package/internal-utils.ts +92 -78
- package/internal.ts +21 -1
- package/judge.ts +229 -0
- package/layers.ts +10 -15
- package/load/alias.ts +6 -12
- package/load/cache.ts +9 -6
- package/load/index.ts +16 -109
- package/load/issues.ts +31 -0
- package/load/legacy.ts +102 -0
- package/load/merge.ts +2 -2
- package/load/shape-guards.test.ts +4 -1
- package/load/shape-guards.ts +3 -6
- package/load/skill-contract-phase.ts +94 -0
- package/loop-constructors.ts +623 -0
- package/loop-def.ts +240 -0
- package/loop-kinds.ts +493 -0
- package/loop.ts +304 -0
- package/messages.ts +225 -157
- package/outcomes/collectors/directory-path.ts +7 -3
- package/outcomes/collectors/require-opt.ts +11 -0
- package/outcomes/collectors/tool-call.test.ts +3 -1
- package/outcomes/collectors/tool-call.ts +8 -3
- package/outcomes/collectors/transcript-path.ts +2 -3
- package/outcomes/collectors/union.test.ts +54 -0
- package/outcomes/collectors/union.ts +28 -9
- package/outcomes/collectors/url.ts +7 -0
- package/outcomes/collectors/workspace-diff.test.ts +13 -0
- package/outcomes/collectors/workspace-diff.ts +21 -14
- package/outcomes/exec.ts +17 -0
- package/outcomes/git-commit.test.ts +76 -30
- package/outcomes/git-commit.ts +139 -100
- package/outcomes/index.ts +1 -0
- package/outcomes/parsers/json-body.ts +2 -2
- package/outcomes/side-effect.ts +3 -3
- package/output-spec.ts +36 -10
- package/output.ts +40 -20
- package/package.json +13 -7
- package/predicates.ts +11 -8
- package/preview.ts +77 -17
- package/registration.ts +109 -35
- package/routing-dsl.ts +310 -0
- package/routing.ts +2 -2
- package/runner/by-name.ts +2 -1
- package/runner/by-run-id.ts +2 -1
- package/runner/chain-advance.ts +59 -44
- package/runner/errors.ts +33 -0
- package/runner/failure.ts +109 -0
- package/runner/header-write-failure.test.ts +64 -0
- package/runner/index.ts +7 -13
- package/runner/input-validation.ts +11 -21
- package/runner/preflight.ts +173 -0
- package/runner/resolve-stage.ts +68 -0
- package/runner/resume-entry.ts +101 -0
- package/runner/resume-loop.test.ts +1410 -0
- package/runner/resume-loop.ts +110 -0
- package/runner/resume-session.test.ts +400 -0
- package/runner/resume.test.ts +881 -206
- package/runner/resume.ts +281 -261
- package/runner/run-context.ts +161 -0
- package/runner/run-stage.ts +456 -0
- package/runner/runner.ts +59 -400
- package/runner/script-stage.ts +103 -105
- package/schema-compat.ts +13 -1
- package/sessions/extraction.ts +53 -31
- package/sessions/index.ts +3 -1
- package/sessions/locate.test.ts +77 -0
- package/sessions/locate.ts +90 -0
- package/sessions/reattach.ts +63 -0
- package/sessions/sessions.ts +130 -127
- package/skill-contract.ts +22 -25
- package/skill-contracts/composition.ts +88 -37
- package/skill-contracts/{registries.ts → extension-points.ts} +45 -12
- package/skill-contracts/harvest.ts +2 -3
- package/skill-contracts/index.ts +15 -8
- package/skill-contracts/registry.ts +33 -50
- package/stage-def.ts +527 -0
- package/startup.ts +3 -1
- package/state/index.ts +8 -1
- package/state/names.ts +91 -65
- package/state/paths.ts +12 -0
- package/state/raw.ts +78 -0
- package/state/reads.ts +103 -85
- package/state/resolve.ts +73 -0
- package/state/state.ts +112 -15
- package/state/writes.ts +29 -12
- package/transcript.ts +24 -0
- package/triggers.ts +11 -3
- package/types.ts +202 -44
- package/validate/contract-compat.ts +193 -0
- package/validate/graph.ts +125 -0
- package/validate/issue.ts +314 -0
- package/validate/stage-rules.ts +420 -0
- package/validate-output.ts +69 -1
- package/validate-workflow.ts +55 -759
- package/control-flow.ts +0 -135
- package/fanout.ts +0 -102
- package/iterate.ts +0 -155
- package/runner/resume-fanout.test.ts +0 -331
- package/runner/resume-fanout.ts +0 -136
- package/runner/resume-iterate.test.ts +0 -444
- package/runner/resume-iterate.ts +0 -111
- package/runner/stage-lifecycle.ts +0 -500
package/audit-rows.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Audit-row persistence — the pure half of the audit layer. Owns the
|
|
3
|
+
* monotonic stage-number allocator, JSONL stage-row appends, the structured
|
|
4
|
+
* unit-row projection, and the shared success persistence/apply pair the
|
|
5
|
+
* skill path, the script path, and the resume fold all run through.
|
|
6
|
+
*
|
|
7
|
+
* Depends only on state, chain-state, and generic utils — NO UI, NO
|
|
8
|
+
* lifecycle dispatch. Terminal-outcome orchestration (notify + status-line
|
|
9
|
+
* + lifecycle events + `terminate`) lives in `audit.ts`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { StageDef } from "./api.js";
|
|
13
|
+
import { applyCompletedStage } from "./chain-state.js";
|
|
14
|
+
import { nowIso } from "./internal-utils.js";
|
|
15
|
+
import type { Output } from "./output.js";
|
|
16
|
+
import { appendStage, type SessionRef, type WorkflowStage } from "./state/index.js";
|
|
17
|
+
import type { RunState, UnitRef } from "./types.js";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Advance the monotonic stage-number allocator and return the assigned
|
|
21
|
+
* number. Call ONCE per stage activation, BEFORE the activation's output
|
|
22
|
+
* envelope is built — the envelope's `meta.stageNumber`, the JSONL row, and
|
|
23
|
+
* every lifecycle ref for the activation then share one explicit value
|
|
24
|
+
* instead of peeking `lastAllocatedStageNumber + 1` and relying on a
|
|
25
|
+
* "no record in between" convention.
|
|
26
|
+
*/
|
|
27
|
+
export function allocateStageNumber(state: RunState): number {
|
|
28
|
+
state.lastAllocatedStageNumber += 1;
|
|
29
|
+
return state.lastAllocatedStageNumber;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Attempts the append and returns the assigned `stageNumber` on success (or
|
|
34
|
+
* undefined on I/O failure). The number comes from `preAllocated` when the
|
|
35
|
+
* activation already ran `allocateStageNumber` (output-producing paths), or
|
|
36
|
+
* is allocated here (pre-output halts). Either way the allocator advances
|
|
37
|
+
* monotonically — once per activation — so a transient failure doesn't cause
|
|
38
|
+
* the next stage to reuse the lost row's number. Higher-level counters
|
|
39
|
+
* (e.g. `stagesCompleted`) gate on the returned value being defined.
|
|
40
|
+
*/
|
|
41
|
+
export function recordStage(
|
|
42
|
+
cwd: string,
|
|
43
|
+
runId: string,
|
|
44
|
+
stage: Omit<WorkflowStage, "stageNumber">,
|
|
45
|
+
state: RunState,
|
|
46
|
+
preAllocated?: number,
|
|
47
|
+
): number | undefined {
|
|
48
|
+
const stageNumber = preAllocated ?? allocateStageNumber(state);
|
|
49
|
+
return appendStage(cwd, runId, { stageNumber, ...stage }) ? stageNumber : undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* DISPLAY decoration for a loop-unit row's `stage` value —
|
|
54
|
+
* `"implement (phase-2)"`, `"breakdown (r0·judge)"`. Pure human label: the
|
|
55
|
+
* machine channel is the structured `parent`/`role`/`unitId`/`unitIndex`
|
|
56
|
+
* fields (`unitRowFields`); nothing may parse this string back. The driver
|
|
57
|
+
* builds the tag (`unit.id ?? unit.label` for fanout/iterate;
|
|
58
|
+
* `r{round}·{phase}` for assess) and decorates once at session construction.
|
|
59
|
+
*/
|
|
60
|
+
export const decorateStage = (parent: string, tag: string): string => `${parent} (${tag})`;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Project a session's unit identity into the structured row fields. Returns
|
|
64
|
+
* `{}` for single stages so call sites spread unconditionally —
|
|
65
|
+
* `JSON.stringify` drops nothing because nothing is added.
|
|
66
|
+
*/
|
|
67
|
+
export function unitRowFields(
|
|
68
|
+
unit: UnitRef | undefined,
|
|
69
|
+
): Pick<WorkflowStage, "parent" | "role" | "unitId" | "unitIndex"> {
|
|
70
|
+
if (!unit) return {};
|
|
71
|
+
return { parent: unit.parent, role: unit.role, unitId: unit.id, unitIndex: unit.index };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Apply a completed stage's effects to `RunState` — the I/O-free half of
|
|
76
|
+
* success persistence, and exactly what the resume fold replays per
|
|
77
|
+
* completed row: advance the completion counter, roll `state.output`, and
|
|
78
|
+
* advance the artifact slots via the `applyCompletedStage` authority.
|
|
79
|
+
* `output` may be absent (defensive — completed rows normally carry one);
|
|
80
|
+
* the counter still advances, matching the fold's semantics.
|
|
81
|
+
*/
|
|
82
|
+
export function applyStageSuccess(state: RunState, def: StageDef, stageName: string, output: Output | undefined): void {
|
|
83
|
+
state.stagesCompleted++;
|
|
84
|
+
if (!output) return;
|
|
85
|
+
state.output = output;
|
|
86
|
+
applyCompletedStage(state, def, stageName, output);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Persist a completed stage's success row, then apply its state effects —
|
|
91
|
+
* the ONE live-path success persistence (skill sessions + script stages).
|
|
92
|
+
* Returns `true` iff the JSONL row landed; on `false` the state is left at
|
|
93
|
+
* its prior values (output is set iff the row that carried it landed) and
|
|
94
|
+
* the CALLER owns the halt (notify + `terminate` — the wording differs per
|
|
95
|
+
* path). The resume fold runs `applyStageSuccess` directly (its rows are
|
|
96
|
+
* already on disk).
|
|
97
|
+
*/
|
|
98
|
+
export function persistStageSuccess(
|
|
99
|
+
state: RunState,
|
|
100
|
+
row: {
|
|
101
|
+
cwd: string;
|
|
102
|
+
runId: string;
|
|
103
|
+
stage: string;
|
|
104
|
+
/** Omitted on script-stage rows — JSON.stringify drops `undefined`. */
|
|
105
|
+
skill?: string;
|
|
106
|
+
output: Output;
|
|
107
|
+
/**
|
|
108
|
+
* REQUIRED: the Pi session that backed the activation, or `null` for
|
|
109
|
+
* sessionless paths (script stages) — the row serializes it verbatim.
|
|
110
|
+
*/
|
|
111
|
+
session: SessionRef | null;
|
|
112
|
+
unit?: UnitRef;
|
|
113
|
+
/** The activation's pre-allocated number (output-producing paths). */
|
|
114
|
+
preAllocated?: number;
|
|
115
|
+
},
|
|
116
|
+
def: StageDef,
|
|
117
|
+
): boolean {
|
|
118
|
+
const assigned = recordStage(
|
|
119
|
+
row.cwd,
|
|
120
|
+
row.runId,
|
|
121
|
+
{
|
|
122
|
+
stage: row.stage,
|
|
123
|
+
skill: row.skill,
|
|
124
|
+
status: "completed",
|
|
125
|
+
ts: nowIso(),
|
|
126
|
+
output: row.output,
|
|
127
|
+
session: row.session,
|
|
128
|
+
...unitRowFields(row.unit),
|
|
129
|
+
},
|
|
130
|
+
state,
|
|
131
|
+
row.preAllocated,
|
|
132
|
+
);
|
|
133
|
+
if (assigned === undefined) return false;
|
|
134
|
+
applyStageSuccess(state, def, row.stage, row.output);
|
|
135
|
+
return true;
|
|
136
|
+
}
|
package/audit.ts
CHANGED
|
@@ -1,48 +1,77 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Terminal-outcome orchestration — the impure half of the audit layer.
|
|
3
|
+
* Turns a halt reason into the full bundle a terminal outcome owes the
|
|
4
|
+
* user and the system: the JSONL failure row (via `audit-rows.ts`), the
|
|
5
|
+
* status-line clear, the notify toast, the `terminate()` state write, and
|
|
6
|
+
* the `onStageError` lifecycle fire.
|
|
7
|
+
*
|
|
8
|
+
* Depends on audit-rows + state + messages + events + handle. Shared by
|
|
9
|
+
* the runner + sessions; neither imports back. Pure row persistence (the
|
|
10
|
+
* allocator, `recordStage`, success persistence) lives in `audit-rows.ts`.
|
|
5
11
|
*/
|
|
6
12
|
|
|
13
|
+
import { recordStage, unitRowFields } from "./audit-rows.js";
|
|
14
|
+
import { lifecycleCtxFromSession, scriptStageRef, skillStageRef } from "./events.js";
|
|
7
15
|
import { handleToString } from "./handle.js";
|
|
8
|
-
import { assertNever } from "./internal-utils.js";
|
|
9
|
-
import { buildLifecycleContext, scriptStageRef, skillStageRef } from "./lifecycle.js";
|
|
16
|
+
import { assertNever, nowIso } from "./internal-utils.js";
|
|
10
17
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
18
|
+
FAIL_STAGE_ABORTED,
|
|
19
|
+
FAIL_STAGE_NO_RESPONSE,
|
|
20
|
+
FAIL_STAGE_TOOL_STALLED,
|
|
21
|
+
FAIL_STAGE_TRUNCATED,
|
|
22
|
+
type FailureText,
|
|
23
|
+
MSG_FAILURE_ROW_DROPPED,
|
|
15
24
|
MSG_PARTIAL_ARTIFACTS,
|
|
16
|
-
MSG_STAGE_ABORTED,
|
|
17
25
|
MSG_STAGE_FAILED,
|
|
18
|
-
MSG_STAGE_NO_RESPONSE,
|
|
19
|
-
MSG_STAGE_TOOL_STALLED,
|
|
20
|
-
MSG_STAGE_TRUNCATED,
|
|
21
26
|
MSG_WORKFLOW_CANCELLED,
|
|
22
27
|
STATUS_KEY,
|
|
23
28
|
} from "./messages.js";
|
|
24
|
-
import {
|
|
29
|
+
import { listArtifacts, type SessionRef } from "./state/index.js";
|
|
25
30
|
import type { StopSignal } from "./transcript.js";
|
|
26
|
-
import type {
|
|
31
|
+
import type { RunContext, RunState, RunTermination, SessionContext, UnitRef, WorkflowHostContext } from "./types.js";
|
|
32
|
+
|
|
33
|
+
// Re-export the persistence half so existing audit-layer consumers keep one
|
|
34
|
+
// import site; new code may import audit-rows.js directly.
|
|
35
|
+
export { allocateStageNumber, decorateStage, recordStage, unitRowFields } from "./audit-rows.js";
|
|
27
36
|
|
|
28
|
-
/**
|
|
29
|
-
|
|
37
|
+
/**
|
|
38
|
+
* THE one `state.termination` mutator. Every terminal path — completion
|
|
39
|
+
* (`finalizeWorkflow`), failure/abort (`recordTerminalFailure`), cancellation
|
|
40
|
+
* (`recordCancellation`), audit-write halts — lands its outcome through here,
|
|
41
|
+
* so the union can never be half-set and a new outcome variant has one
|
|
42
|
+
* write-site to thread through. Last write wins (a failure recorded after an
|
|
43
|
+
* earlier failure on the same unwind keeps today's semantics).
|
|
44
|
+
*/
|
|
45
|
+
export function terminate(state: RunState, outcome: Exclude<RunTermination, { status: "running" }>): void {
|
|
46
|
+
state.termination = outcome;
|
|
47
|
+
}
|
|
30
48
|
|
|
31
49
|
/**
|
|
32
50
|
* Minimal bookkeeping ctx. Structurally derived from `SessionContext` so any
|
|
33
51
|
* future field added to the base lands here too — no duplicate
|
|
34
|
-
* maintenance.
|
|
52
|
+
* maintenance. Every `StageSession` (single stage or loop unit) collapses to this.
|
|
35
53
|
*
|
|
36
54
|
* `isScript` toggles the `onStageError` ref construction in
|
|
37
55
|
* `recordTerminalFailure` from `skillStageRef` to `scriptStageRef` (the
|
|
38
56
|
* script branch carries no `skill` field). Defaulting to `undefined`
|
|
39
57
|
* preserves the skill-path behaviour for every existing caller.
|
|
58
|
+
*
|
|
59
|
+
* `unit` is present iff the failure/cancellation belongs to a loop unit — its
|
|
60
|
+
* identity is spread into the JSONL row so failed trailers carry the
|
|
61
|
+
* structured fields the resume guard consumes.
|
|
62
|
+
*
|
|
63
|
+
* `session` is REQUIRED (`null` = explicitly sessionless) — the compiler
|
|
64
|
+
* forces every audit-row writer to make the provenance decision; the value
|
|
65
|
+
* lands verbatim on the JSONL row (`WorkflowStage.session`), which is what
|
|
66
|
+
* session-backed resume dispatches on.
|
|
40
67
|
*/
|
|
41
68
|
export type AuditCtx = Pick<
|
|
42
69
|
SessionContext,
|
|
43
|
-
"cwd" | "runId" | "state" | "stageName" | "skill" | "lifecycle" | "runIdentity"
|
|
70
|
+
"cwd" | "runId" | "state" | "stageName" | "skill" | "lifecycle" | "runIdentity" | "allocatedStageNumber"
|
|
44
71
|
> & {
|
|
72
|
+
session: SessionRef | null;
|
|
45
73
|
isScript?: boolean;
|
|
74
|
+
unit?: UnitRef;
|
|
46
75
|
};
|
|
47
76
|
|
|
48
77
|
/**
|
|
@@ -61,12 +90,18 @@ export function runIdentityOf(run: RunContext): SessionContext["runIdentity"] {
|
|
|
61
90
|
* resume-time refusals). One source for the shape so every halt path records
|
|
62
91
|
* a uniform row. `isScript: true` drops the `skill` field from the JSONL row
|
|
63
92
|
* and switches `onStageError` to `scriptStageRef`.
|
|
93
|
+
*
|
|
94
|
+
* `session` is pinned to `null` here BY CONSTRUCTION: every caller of this
|
|
95
|
+
* builder records a failure that escaped (or never reached) a session —
|
|
96
|
+
* preflight halts, seam aborts, entry throws, routing errors, resume drift,
|
|
97
|
+
* script halts. In-session writers build their `AuditCtx` via `auditFor`
|
|
98
|
+
* (sessions/sessions.ts), which threads the captured `SessionRef`.
|
|
64
99
|
*/
|
|
65
100
|
export function auditCtxFor(
|
|
66
101
|
run: RunContext,
|
|
67
102
|
stageName: string,
|
|
68
103
|
skill: string,
|
|
69
|
-
opts?: { isScript?: boolean },
|
|
104
|
+
opts?: { isScript?: boolean; unit?: UnitRef; allocatedStageNumber?: number },
|
|
70
105
|
): AuditCtx {
|
|
71
106
|
return {
|
|
72
107
|
cwd: run.cwd,
|
|
@@ -74,54 +109,27 @@ export function auditCtxFor(
|
|
|
74
109
|
state: run.state,
|
|
75
110
|
stageName,
|
|
76
111
|
skill,
|
|
112
|
+
session: null,
|
|
77
113
|
lifecycle: run.lifecycle,
|
|
78
114
|
runIdentity: runIdentityOf(run),
|
|
79
115
|
...(opts?.isScript ? { isScript: true } : {}),
|
|
116
|
+
...(opts?.unit ? { unit: opts.unit } : {}),
|
|
117
|
+
...(opts?.allocatedStageNumber !== undefined ? { allocatedStageNumber: opts.allocatedStageNumber } : {}),
|
|
80
118
|
};
|
|
81
119
|
}
|
|
82
120
|
|
|
83
121
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* neutral about the wording.
|
|
91
|
-
*/
|
|
92
|
-
export const fanoutRowStage = (s: FanoutSession): string => `${s.stageName} (${s.id ?? s.label})`;
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* JSONL `WorkflowStage.stage` value for iterate-unit rows. Same projection as
|
|
96
|
-
* `fanoutRowStage` — the parent stage's record key suffixed with the unit's
|
|
97
|
-
* `id ?? label` (e.g. `"blueprint (phase-2)"`) so post-hoc readers can tell
|
|
98
|
-
* the accumulated units apart. Takes the decorated parts directly (rather than
|
|
99
|
-
* a session) because the iterate executor synthesizes the `StageSession` after
|
|
100
|
-
* building this label. Decoration is SAFE for named keying: iterate mandates
|
|
101
|
-
* `outcome.name`, so `resolvePublishName` ignores the decorated `stageName`
|
|
102
|
-
* and every unit publishes to the same `state.named` slot.
|
|
122
|
+
* Lifecycle ref for the CURRENT activation — ONE numbering base (the
|
|
123
|
+
* allocator value) for every event of one execution, so a listener can
|
|
124
|
+
* correlate a retry ref with the end/error ref it belongs to. Valid once the
|
|
125
|
+
* activation allocated its number (`allocatedStageNumber`); falls back to the
|
|
126
|
+
* last allocated number for record-time allocators (failure paths that never
|
|
127
|
+
* reached output production).
|
|
103
128
|
*/
|
|
104
|
-
export
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
* assigned number on success (or undefined on I/O failure). `lastAllocatedStageNumber`
|
|
109
|
-
* advances monotonically — once per call — so a transient failure doesn't
|
|
110
|
-
* cause the next stage to reuse the lost row's number. Higher-level counters
|
|
111
|
-
* (e.g. `stagesCompleted`) gate on the returned value being defined.
|
|
112
|
-
*
|
|
113
|
-
* `wrapOutput`'s `state.lastAllocatedStageNumber + 1` peek aligns with this allocation
|
|
114
|
-
* because the output is built BEFORE recordStage is called.
|
|
115
|
-
*/
|
|
116
|
-
export function recordStage(
|
|
117
|
-
cwd: string,
|
|
118
|
-
runId: string,
|
|
119
|
-
stage: Omit<WorkflowStage, "stageNumber">,
|
|
120
|
-
state: RunState,
|
|
121
|
-
): number | undefined {
|
|
122
|
-
state.lastAllocatedStageNumber += 1;
|
|
123
|
-
const stageNumber = state.lastAllocatedStageNumber;
|
|
124
|
-
return appendStage(cwd, runId, { stageNumber, ...stage }) ? stageNumber : undefined;
|
|
129
|
+
export function currentStageRef(
|
|
130
|
+
s: Pick<SessionContext, "stageName" | "skill" | "state" | "allocatedStageNumber">,
|
|
131
|
+
): ReturnType<typeof skillStageRef> {
|
|
132
|
+
return skillStageRef(s.stageName, s.allocatedStageNumber ?? s.state.lastAllocatedStageNumber, s.skill);
|
|
125
133
|
}
|
|
126
134
|
|
|
127
135
|
/** Surface every artifact recorded so far — recap on stage failure. */
|
|
@@ -132,18 +140,46 @@ export function notifyPartialArtifacts(ctx: WorkflowHostContext, cwd: string, ru
|
|
|
132
140
|
ctx.ui.notify(MSG_PARTIAL_ARTIFACTS(artifactList), "info");
|
|
133
141
|
}
|
|
134
142
|
|
|
143
|
+
/**
|
|
144
|
+
* The toast + JSONL halves of a terminal failure, paired by construction.
|
|
145
|
+
* Build via `failedArgs` / `abortedArgs` (or `stopFailureArgs`' switch) so a
|
|
146
|
+
* halt site can't mismatch status and notify level.
|
|
147
|
+
*/
|
|
148
|
+
export interface TerminalFailureArgs {
|
|
149
|
+
status: "failed" | "aborted";
|
|
150
|
+
notifyMsg: string;
|
|
151
|
+
notifyLevel: "warning" | "error";
|
|
152
|
+
errMsg: string;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Argument constructors for `recordTerminalFailure` — the
|
|
157
|
+
* `{status, notifyMsg, notifyLevel, errMsg}` quadruple every halt site used
|
|
158
|
+
* to spell by hand. One per terminal status: failures notify at `"error"`,
|
|
159
|
+
* aborts at `"warning"` (cooperative cancellation is expected, not
|
|
160
|
+
* exceptional).
|
|
161
|
+
*/
|
|
162
|
+
export function failedArgs(failure: FailureText): TerminalFailureArgs;
|
|
163
|
+
export function failedArgs(notifyMsg: string, errMsg: string): TerminalFailureArgs;
|
|
164
|
+
export function failedArgs(a: FailureText | string, b?: string): TerminalFailureArgs {
|
|
165
|
+
const f = typeof a === "string" ? { toast: a, error: b as string } : a;
|
|
166
|
+
return { status: "failed", notifyMsg: f.toast, notifyLevel: "error", errMsg: f.error };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function abortedArgs(failure: FailureText): TerminalFailureArgs;
|
|
170
|
+
export function abortedArgs(notifyMsg: string, errMsg: string): TerminalFailureArgs;
|
|
171
|
+
export function abortedArgs(a: FailureText | string, b?: string): TerminalFailureArgs {
|
|
172
|
+
const f = typeof a === "string" ? { toast: a, error: b as string } : a;
|
|
173
|
+
return { status: "aborted", notifyMsg: f.toast, notifyLevel: "warning", errMsg: f.error };
|
|
174
|
+
}
|
|
175
|
+
|
|
135
176
|
export async function recordTerminalFailure(
|
|
136
177
|
ctx: WorkflowHostContext,
|
|
137
178
|
audit: AuditCtx,
|
|
138
|
-
args:
|
|
139
|
-
status: "failed" | "aborted";
|
|
140
|
-
notifyMsg: string;
|
|
141
|
-
notifyLevel: "warning" | "error";
|
|
142
|
-
errMsg: string;
|
|
143
|
-
},
|
|
179
|
+
args: TerminalFailureArgs,
|
|
144
180
|
onFailure?: (ctx: WorkflowHostContext) => void,
|
|
145
181
|
): Promise<void> {
|
|
146
|
-
recordStage(
|
|
182
|
+
const written = recordStage(
|
|
147
183
|
audit.cwd,
|
|
148
184
|
audit.runId,
|
|
149
185
|
// Script-stage failure rows omit `skill` (the row split landed in A.0);
|
|
@@ -156,30 +192,31 @@ export async function recordTerminalFailure(
|
|
|
156
192
|
status: args.status,
|
|
157
193
|
ts: nowIso(),
|
|
158
194
|
errMsg: args.errMsg,
|
|
195
|
+
session: audit.session,
|
|
196
|
+
...unitRowFields(audit.unit),
|
|
159
197
|
},
|
|
160
198
|
audit.state,
|
|
199
|
+
// Reuse the activation's pre-allocated number when output production
|
|
200
|
+
// already burned one — the failure row carries the SAME stage number
|
|
201
|
+
// the activation's output/lifecycle refs used (no gap, no skew).
|
|
202
|
+
audit.allocatedStageNumber,
|
|
161
203
|
);
|
|
204
|
+
if (written === undefined) {
|
|
205
|
+
// A dropped FAILURE row corrupts resume: the trail's last row reads
|
|
206
|
+
// "completed" and a resume would route onward past this stage. The run
|
|
207
|
+
// is already halting — surface loudly + flag the envelope so callers
|
|
208
|
+
// know the trail is unsafe to resume from.
|
|
209
|
+
ctx.ui.notify(MSG_FAILURE_ROW_DROPPED(audit.stageName), "warning");
|
|
210
|
+
audit.state.telemetry.droppedFailureRows.push(audit.stageName);
|
|
211
|
+
}
|
|
162
212
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
163
213
|
ctx.ui.notify(args.notifyMsg, args.notifyLevel);
|
|
164
214
|
onFailure?.(ctx);
|
|
165
|
-
audit.state.
|
|
215
|
+
terminate(audit.state, { status: args.status, error: args.errMsg });
|
|
166
216
|
const ref = audit.isScript
|
|
167
217
|
? scriptStageRef(audit.stageName, audit.state.lastAllocatedStageNumber)
|
|
168
218
|
: skillStageRef(audit.stageName, audit.state.lastAllocatedStageNumber, audit.skill);
|
|
169
|
-
await audit.lifecycle.fire(
|
|
170
|
-
ctx,
|
|
171
|
-
"onStageError",
|
|
172
|
-
ref,
|
|
173
|
-
args.errMsg,
|
|
174
|
-
buildLifecycleContext({
|
|
175
|
-
cwd: audit.cwd,
|
|
176
|
-
runId: audit.runId,
|
|
177
|
-
workflow: audit.runIdentity.workflow,
|
|
178
|
-
totalStages: audit.runIdentity.totalStages,
|
|
179
|
-
trigger: audit.runIdentity.trigger,
|
|
180
|
-
state: audit.state,
|
|
181
|
-
}),
|
|
182
|
-
);
|
|
219
|
+
await audit.lifecycle.fire(ctx, "onStageError", ref, args.errMsg, lifecycleCtxFromSession(audit));
|
|
183
220
|
}
|
|
184
221
|
|
|
185
222
|
/**
|
|
@@ -198,67 +235,43 @@ export async function recordStopFailure(
|
|
|
198
235
|
await recordTerminalFailure(ctx, audit, stopFailureArgs(audit.skill, stop, errorMessage), onFailure);
|
|
199
236
|
}
|
|
200
237
|
|
|
201
|
-
function stopFailureArgs(
|
|
202
|
-
skill: string,
|
|
203
|
-
stop: Exclude<StopSignal, "stop">,
|
|
204
|
-
errorMessage: string,
|
|
205
|
-
): {
|
|
206
|
-
status: "failed" | "aborted";
|
|
207
|
-
notifyMsg: string;
|
|
208
|
-
notifyLevel: "warning" | "error";
|
|
209
|
-
errMsg: string;
|
|
210
|
-
} {
|
|
238
|
+
function stopFailureArgs(skill: string, stop: Exclude<StopSignal, "stop">, errorMessage: string): TerminalFailureArgs {
|
|
211
239
|
switch (stop) {
|
|
212
240
|
case "aborted":
|
|
213
|
-
return
|
|
214
|
-
status: "aborted",
|
|
215
|
-
notifyMsg: MSG_STAGE_ABORTED(skill),
|
|
216
|
-
notifyLevel: "warning",
|
|
217
|
-
errMsg: ERR_STAGE_ABORTED(skill),
|
|
218
|
-
};
|
|
241
|
+
return abortedArgs(FAIL_STAGE_ABORTED(skill));
|
|
219
242
|
case "length":
|
|
220
|
-
return
|
|
221
|
-
status: "failed",
|
|
222
|
-
notifyMsg: MSG_STAGE_TRUNCATED(skill),
|
|
223
|
-
notifyLevel: "error",
|
|
224
|
-
errMsg: ERR_STAGE_TRUNCATED(skill),
|
|
225
|
-
};
|
|
243
|
+
return failedArgs(FAIL_STAGE_TRUNCATED(skill));
|
|
226
244
|
case "toolUse":
|
|
227
|
-
return
|
|
228
|
-
status: "failed",
|
|
229
|
-
notifyMsg: MSG_STAGE_TOOL_STALLED(skill),
|
|
230
|
-
notifyLevel: "error",
|
|
231
|
-
errMsg: ERR_STAGE_TOOL_STALLED(skill),
|
|
232
|
-
};
|
|
245
|
+
return failedArgs(FAIL_STAGE_TOOL_STALLED(skill));
|
|
233
246
|
case "noResponse":
|
|
234
|
-
return
|
|
235
|
-
status: "failed",
|
|
236
|
-
notifyMsg: MSG_STAGE_NO_RESPONSE(skill),
|
|
237
|
-
notifyLevel: "error",
|
|
238
|
-
errMsg: ERR_STAGE_NO_RESPONSE(skill),
|
|
239
|
-
};
|
|
247
|
+
return failedArgs(FAIL_STAGE_NO_RESPONSE(skill));
|
|
240
248
|
case "error":
|
|
241
|
-
return
|
|
242
|
-
status: "failed",
|
|
243
|
-
notifyMsg: MSG_STAGE_FAILED(skill),
|
|
244
|
-
notifyLevel: "error",
|
|
245
|
-
errMsg: errorMessage,
|
|
246
|
-
};
|
|
249
|
+
return failedArgs(MSG_STAGE_FAILED(skill), errorMessage);
|
|
247
250
|
default:
|
|
248
251
|
return assertNever(stop);
|
|
249
252
|
}
|
|
250
253
|
}
|
|
251
254
|
|
|
252
255
|
export function recordCancellation(ctx: WorkflowHostContext, audit: AuditCtx): void {
|
|
256
|
+
// Cancellation is a first-class termination outcome (`status: "cancelled"`);
|
|
257
|
+
// `errMsg` is mirrored into the JSONL row so post-mortems work from the
|
|
258
|
+
// trail alone (same posture as `recordTerminalFailure`).
|
|
259
|
+
const errMsg = `${audit.skill} cancelled by user`;
|
|
253
260
|
recordStage(
|
|
254
261
|
audit.cwd,
|
|
255
262
|
audit.runId,
|
|
256
|
-
{
|
|
263
|
+
{
|
|
264
|
+
stage: audit.stageName,
|
|
265
|
+
skill: audit.skill,
|
|
266
|
+
status: "skipped",
|
|
267
|
+
ts: nowIso(),
|
|
268
|
+
errMsg,
|
|
269
|
+
session: audit.session,
|
|
270
|
+
...unitRowFields(audit.unit),
|
|
271
|
+
},
|
|
257
272
|
audit.state,
|
|
258
273
|
);
|
|
259
274
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
260
275
|
ctx.ui.notify(MSG_WORKFLOW_CANCELLED, "info");
|
|
261
|
-
|
|
262
|
-
// the error string is the signal.
|
|
263
|
-
audit.state.termination.error = `${audit.skill} cancelled by user`;
|
|
276
|
+
terminate(audit.state, { status: "cancelled", error: errMsg });
|
|
264
277
|
}
|
package/built-ins.ts
CHANGED
|
@@ -12,46 +12,28 @@
|
|
|
12
12
|
* `registerBuiltIns(...)` from their extension entry point with workflows
|
|
13
13
|
* that name their own bundled skills.
|
|
14
14
|
*
|
|
15
|
-
* The registry array is anchored on a `Symbol.for` slot on `globalThis
|
|
16
|
-
* Pi may load this module more than once — once for the
|
|
17
|
-
* extension itself, and once via the rpiv-pi
|
|
18
|
-
* from "@juicesharp/rpiv-workflow"`
|
|
19
|
-
* module-local state would be siloed between
|
|
20
|
-
* `globalThis[KEY]` is process-wide and survives the dup load.
|
|
15
|
+
* The registry array is anchored on a `Symbol.for` slot on `globalThis`
|
|
16
|
+
* (via `globalSlot`). Pi may load this module more than once — once for the
|
|
17
|
+
* rpiv-workflow extension itself, and once via the rpiv-pi
|
|
18
|
+
* `import { registerBuiltIns } from "@juicesharp/rpiv-workflow"`
|
|
19
|
+
* cross-package resolution — and module-local state would be siloed between
|
|
20
|
+
* those copies. `globalThis[KEY]` is process-wide and survives the dup load.
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
import type { Workflow } from "./api.js";
|
|
24
|
+
import { globalSlot, lazyProviderRegistry } from "./internal-utils.js";
|
|
24
25
|
|
|
25
26
|
const REGISTRY_KEY = Symbol.for("@juicesharp/rpiv-workflow:built-ins");
|
|
26
|
-
const PROVIDERS_KEY = Symbol.for("@juicesharp/rpiv-workflow:built-in-providers");
|
|
27
|
-
const FLUSH_KEY = Symbol.for("@juicesharp/rpiv-workflow:built-in-flush");
|
|
28
27
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
registry = [];
|
|
39
|
-
g[REGISTRY_KEY] = registry;
|
|
40
|
-
}
|
|
41
|
-
return registry;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Provider list + flush latch use the same `Symbol.for` global-slot strategy as
|
|
45
|
-
// the registry, so a duplicate module load shares one process-wide state.
|
|
46
|
-
function getProviders(): BuiltInsProvider[] {
|
|
47
|
-
const g = globalThis as unknown as Global;
|
|
48
|
-
let providers = g[PROVIDERS_KEY] as BuiltInsProvider[] | undefined;
|
|
49
|
-
if (!providers) {
|
|
50
|
-
providers = [];
|
|
51
|
-
g[PROVIDERS_KEY] = providers;
|
|
52
|
-
}
|
|
53
|
-
return providers;
|
|
54
|
-
}
|
|
28
|
+
const getRegistry = globalSlot(REGISTRY_KEY, () => [] as Workflow[]);
|
|
29
|
+
// Provider lifecycle (register / flush-on-demand; each provider runs once,
|
|
30
|
+
// post-flush registrations run on the next flush — `/reload` re-registration
|
|
31
|
+
// refreshes) via the shared `lazyProviderRegistry` helper — same global-slot
|
|
32
|
+
// strategy as the registry, so a duplicate module load shares one
|
|
33
|
+
// process-wide state. Safe to re-flush: `registerBuiltIns` replaces by name.
|
|
34
|
+
// No `onError`: built-in providers are trusted in-process code, a throw
|
|
35
|
+
// propagates to the awaiting `loadWorkflows` caller.
|
|
36
|
+
const providers = lazyProviderRegistry("@juicesharp/rpiv-workflow:built-in-providers");
|
|
55
37
|
|
|
56
38
|
/**
|
|
57
39
|
* Register one or more workflows into the `built-in` layer. Idempotent on
|
|
@@ -74,8 +56,8 @@ export function registerBuiltIns(workflows: readonly Workflow[]): void {
|
|
|
74
56
|
* defer constructing its workflow definitions off startup and onto first `/wf`.
|
|
75
57
|
* Register before the first read — `/wf` is the earliest reader.
|
|
76
58
|
*/
|
|
77
|
-
export function registerBuiltInsProvider(provider:
|
|
78
|
-
|
|
59
|
+
export function registerBuiltInsProvider(provider: () => void | Promise<void>): void {
|
|
60
|
+
providers.register(provider);
|
|
79
61
|
}
|
|
80
62
|
|
|
81
63
|
/**
|
|
@@ -84,13 +66,7 @@ export function registerBuiltInsProvider(provider: BuiltInsProvider): void {
|
|
|
84
66
|
* first flush won't run — acceptable, all register at extension load.
|
|
85
67
|
*/
|
|
86
68
|
export function flushBuiltInProviders(): Promise<void> {
|
|
87
|
-
|
|
88
|
-
const existing = g[FLUSH_KEY] as Promise<void> | undefined;
|
|
89
|
-
if (existing) return existing;
|
|
90
|
-
const pending = getProviders().splice(0);
|
|
91
|
-
const flush = Promise.all(pending.map((p) => p())).then(() => undefined);
|
|
92
|
-
g[FLUSH_KEY] = flush;
|
|
93
|
-
return flush;
|
|
69
|
+
return providers.flush();
|
|
94
70
|
}
|
|
95
71
|
|
|
96
72
|
/** Read-only view of the registry — consumed by `load.ts`. */
|
|
@@ -104,6 +80,5 @@ export function getBuiltIns(): readonly Workflow[] {
|
|
|
104
80
|
*/
|
|
105
81
|
export function __resetBuiltIns(): void {
|
|
106
82
|
getRegistry().length = 0;
|
|
107
|
-
|
|
108
|
-
(globalThis as unknown as Global)[FLUSH_KEY] = undefined;
|
|
83
|
+
providers.reset();
|
|
109
84
|
}
|