@osolmaz/pi-workflows 0.13.4 → 0.15.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 +146 -160
- package/dist/builtins/autoimplement.workflow.js +3 -1
- package/dist/builtins/autoimplement.workflow.js.map +1 -1
- package/dist/builtins/change-verification.workflow.js +7 -2
- package/dist/builtins/change-verification.workflow.js.map +1 -1
- package/dist/builtins/metadata.d.ts +22 -0
- package/dist/builtins/metadata.js +10 -0
- package/dist/builtins/metadata.js.map +1 -0
- package/dist/builtins/monitor.workflow.js +4 -1
- package/dist/builtins/monitor.workflow.js.map +1 -1
- package/dist/builtins/sanity-check.workflow.js +4 -1
- package/dist/builtins/sanity-check.workflow.js.map +1 -1
- package/dist/builtins/workspace-preparation.workflow.js +3 -1
- package/dist/builtins/workspace-preparation.workflow.js.map +1 -1
- package/dist/controllers/index.d.ts +1 -2
- package/dist/controllers/index.js +0 -1
- package/dist/controllers/index.js.map +1 -1
- package/dist/controllers/sqlite.d.ts +85 -31
- package/dist/controllers/sqlite.js +541 -126
- package/dist/controllers/sqlite.js.map +1 -1
- package/dist/controllers/store.d.ts +1 -0
- package/dist/extension/controller-command.d.ts +22 -0
- package/dist/extension/controller-command.js +32 -0
- package/dist/extension/controller-command.js.map +1 -0
- package/dist/extension/index.d.ts +2 -9
- package/dist/extension/index.js +876 -2928
- package/dist/extension/index.js.map +1 -1
- package/dist/extension/message-card.d.ts +13 -0
- package/dist/extension/message-card.js +46 -0
- package/dist/extension/message-card.js.map +1 -0
- package/dist/extension/step-message.d.ts +5 -6
- package/dist/extension/step-message.js +41 -52
- package/dist/extension/step-message.js.map +1 -1
- package/dist/host/child-worker-supervisor.d.ts +50 -0
- package/dist/host/child-worker-supervisor.js +159 -0
- package/dist/host/child-worker-supervisor.js.map +1 -0
- package/dist/host/client.d.ts +48 -0
- package/dist/host/client.js +216 -0
- package/dist/host/client.js.map +1 -0
- package/dist/host/controller-worker-entry.d.ts +2 -0
- package/dist/host/controller-worker-entry.js +244 -0
- package/dist/host/controller-worker-entry.js.map +1 -0
- package/dist/host/controller-worker-protocol.d.ts +36 -0
- package/dist/host/controller-worker-protocol.js +49 -0
- package/dist/host/controller-worker-protocol.js.map +1 -0
- package/dist/host/controller-worker-supervisor.d.ts +21 -0
- package/dist/host/controller-worker-supervisor.js +54 -0
- package/dist/host/controller-worker-supervisor.js.map +1 -0
- package/dist/host/host-entry.d.ts +2 -0
- package/dist/host/host-entry.js +23 -0
- package/dist/host/host-entry.js.map +1 -0
- package/dist/host/processes.d.ts +17 -12
- package/dist/host/processes.js +154 -50
- package/dist/host/processes.js.map +1 -1
- package/dist/host/protocol.d.ts +38 -0
- package/dist/host/protocol.js +156 -0
- package/dist/host/protocol.js.map +1 -0
- package/dist/host/resolver-entry.d.ts +49 -0
- package/dist/host/resolver-entry.js +149 -0
- package/dist/host/resolver-entry.js.map +1 -0
- package/dist/host/rpc-executor.d.ts +14 -3
- package/dist/host/rpc-executor.js +63 -30
- package/dist/host/rpc-executor.js.map +1 -1
- package/dist/host/runner.d.ts +95 -29
- package/dist/host/runner.js +2311 -348
- package/dist/host/runner.js.map +1 -1
- package/dist/host/state.d.ts +174 -0
- package/dist/host/state.js +689 -0
- package/dist/host/state.js.map +1 -0
- package/dist/host/worker-entry.d.ts +10 -0
- package/dist/host/worker-entry.js +446 -0
- package/dist/host/worker-entry.js.map +1 -0
- package/dist/host/worker-protocol.d.ts +31 -0
- package/dist/host/worker-protocol.js +122 -0
- package/dist/host/worker-protocol.js.map +1 -0
- package/dist/host/worker-store.d.ts +80 -0
- package/dist/host/worker-store.js +91 -0
- package/dist/host/worker-store.js.map +1 -0
- package/dist/host/worker-supervisor.d.ts +22 -0
- package/dist/host/worker-supervisor.js +54 -0
- package/dist/host/worker-supervisor.js.map +1 -0
- package/dist/render/canvas.d.ts +2 -1
- package/dist/render/canvas.js +40 -15
- package/dist/render/canvas.js.map +1 -1
- package/dist/render/graph-render.d.ts +7 -3
- package/dist/render/graph-render.js +125 -74
- package/dist/render/graph-render.js.map +1 -1
- package/dist/state/database.js +3 -1
- package/dist/state/database.js.map +1 -1
- package/dist/state/index.d.ts +1 -0
- package/dist/state/index.js +1 -0
- package/dist/state/index.js.map +1 -1
- package/dist/state/prune.js +36 -10
- package/dist/state/prune.js.map +1 -1
- package/dist/state/schema.d.ts +1 -1
- package/dist/state/schema.js +171 -3
- package/dist/state/schema.js.map +1 -1
- package/dist/state/viewer.d.ts +46 -0
- package/dist/state/viewer.js +249 -0
- package/dist/state/viewer.js.map +1 -0
- package/dist/viewer/cli.d.ts +1 -1
- package/dist/viewer/cli.js +47 -18
- package/dist/viewer/cli.js.map +1 -1
- package/dist/workflows/composition.js +25 -2
- package/dist/workflows/composition.js.map +1 -1
- package/dist/workflows/definition.d.ts +3 -1
- package/dist/workflows/definition.js +25 -0
- package/dist/workflows/definition.js.map +1 -1
- package/dist/workflows/engine.d.ts +1 -0
- package/dist/workflows/engine.js +157 -42
- package/dist/workflows/engine.js.map +1 -1
- package/dist/workflows/errors.d.ts +3 -1
- package/dist/workflows/errors.js +4 -7
- package/dist/workflows/errors.js.map +1 -1
- package/dist/workflows/human-decision.d.ts +3 -0
- package/dist/workflows/human-decision.js +31 -0
- package/dist/workflows/human-decision.js.map +1 -1
- package/dist/workflows/index.d.ts +1 -1
- package/dist/workflows/index.js +1 -1
- package/dist/workflows/index.js.map +1 -1
- package/dist/workflows/schema.js +17 -6
- package/dist/workflows/schema.js.map +1 -1
- package/dist/{viewer → workflows}/session-reducer.d.ts +3 -1
- package/dist/{viewer → workflows}/session-reducer.js +4 -0
- package/dist/workflows/session-reducer.js.map +1 -0
- package/dist/workflows/store.d.ts +87 -1
- package/dist/workflows/store.js +892 -93
- package/dist/workflows/store.js.map +1 -1
- package/dist/workflows/tool-input.d.ts +0 -22
- package/dist/workflows/tool-input.js +1 -44
- package/dist/workflows/tool-input.js.map +1 -1
- package/dist/workflows/types.d.ts +36 -4
- package/docs/2026-08-25-workflow-follow-ups.md +8 -6
- package/docs/2026-08-30-out-of-process-workflow-host-plan.md +353 -0
- package/docs/CONTROLLERS.md +11 -11
- package/docs/DEFERRED_TURNS.md +61 -27
- package/docs/HUMAN_DECISIONS.md +12 -4
- package/docs/SQLITE_STATE.md +42 -8
- package/docs/WORKFLOW_HOST.md +452 -0
- package/docs/development.md +46 -30
- package/docs/live-replay-protocol.md +129 -100
- package/docs/plans/2026-08-19-human-decision-gates-plan.md +34 -8
- package/docs/plans/2026-08-27-workflow-terminal-restart-plan.md +399 -0
- package/docs/plans/2026-08-28-piw-incremental-viewer-plan.md +293 -0
- package/docs/plans/piw-viewer-experience-implementation-plan.md +11 -1
- package/docs/plans/tui-viewer-implementation-plan.md +5 -0
- package/docs/tui-viewer.md +18 -3
- package/docs/workflows.md +164 -156
- package/examples/workflows/command-batch.workflow.ts +2 -0
- package/examples/workflows/shell.workflow.ts +2 -1
- package/herdr-plugin.toml +1 -1
- package/package.json +1 -1
- package/skills/autodoc/SKILL.md +1 -1
- package/skills/autoimplement/SKILL.md +1 -1
- package/skills/autoplan/SKILL.md +1 -1
- package/skills/pi-workflows/SKILL.md +2 -0
- package/src/builtins/autoimplement.workflow.ts +3 -0
- package/src/builtins/change-verification.workflow.ts +7 -2
- package/src/builtins/metadata.ts +9 -0
- package/src/builtins/monitor.workflow.ts +4 -0
- package/src/builtins/sanity-check.workflow.ts +4 -0
- package/src/builtins/workspace-preparation.workflow.ts +3 -1
- package/src/controllers/index.ts +3 -5
- package/src/controllers/sqlite.ts +929 -225
- package/src/controllers/store.ts +1 -0
- package/src/extension/controller-command.ts +45 -0
- package/src/extension/index.ts +948 -3506
- package/src/extension/message-card.ts +61 -0
- package/src/extension/step-message.ts +58 -63
- package/src/host/child-worker-supervisor.ts +183 -0
- package/src/host/client.ts +293 -0
- package/src/host/controller-worker-entry.ts +311 -0
- package/src/host/controller-worker-protocol.ts +104 -0
- package/src/host/controller-worker-supervisor.ts +79 -0
- package/src/host/host-entry.ts +23 -0
- package/src/host/processes.ts +171 -54
- package/src/host/protocol.ts +196 -0
- package/src/host/resolver-entry.ts +241 -0
- package/src/host/rpc-executor.ts +76 -34
- package/src/host/runner.ts +2813 -422
- package/src/host/state.ts +1160 -0
- package/src/host/worker-entry.ts +533 -0
- package/src/host/worker-protocol.ts +165 -0
- package/src/host/worker-store.ts +229 -0
- package/src/host/worker-supervisor.ts +74 -0
- package/src/render/canvas.ts +44 -10
- package/src/render/graph-render.ts +145 -90
- package/src/state/database.ts +2 -1
- package/src/state/index.ts +14 -0
- package/src/state/prune.ts +35 -9
- package/src/state/schema.ts +171 -3
- package/src/state/viewer.ts +356 -0
- package/src/viewer/cli.ts +49 -17
- package/src/workflows/composition.ts +36 -2
- package/src/workflows/definition.ts +32 -0
- package/src/workflows/engine.ts +157 -54
- package/src/workflows/errors.ts +11 -2
- package/src/workflows/human-decision.ts +49 -0
- package/src/workflows/index.ts +2 -0
- package/src/workflows/schema.ts +19 -6
- package/src/{viewer → workflows}/session-reducer.ts +11 -1
- package/src/workflows/store.ts +1316 -108
- package/src/workflows/tool-input.ts +2 -60
- package/src/workflows/types.ts +32 -4
- package/dist/controllers/workflow-engine-scheduler.d.ts +0 -29
- package/dist/controllers/workflow-engine-scheduler.js +0 -175
- package/dist/controllers/workflow-engine-scheduler.js.map +0 -1
- package/dist/extension/controller-host.d.ts +0 -48
- package/dist/extension/controller-host.js +0 -110
- package/dist/extension/controller-host.js.map +0 -1
- package/dist/extension/deferred-turn-coordinator.d.ts +0 -32
- package/dist/extension/deferred-turn-coordinator.js +0 -143
- package/dist/extension/deferred-turn-coordinator.js.map +0 -1
- package/dist/extension/deferred-turn.d.ts +0 -44
- package/dist/extension/deferred-turn.js +0 -110
- package/dist/extension/deferred-turn.js.map +0 -1
- package/dist/extension/executor.d.ts +0 -86
- package/dist/extension/executor.js +0 -311
- package/dist/extension/executor.js.map +0 -1
- package/dist/extension/follow-up-coordinator.d.ts +0 -27
- package/dist/extension/follow-up-coordinator.js +0 -131
- package/dist/extension/follow-up-coordinator.js.map +0 -1
- package/dist/extension/recorder.d.ts +0 -84
- package/dist/extension/recorder.js +0 -528
- package/dist/extension/recorder.js.map +0 -1
- package/dist/extension/session-events.d.ts +0 -133
- package/dist/extension/session-events.js +0 -61
- package/dist/extension/session-events.js.map +0 -1
- package/dist/viewer/session-reducer.js.map +0 -1
- package/src/controllers/workflow-engine-scheduler.ts +0 -246
- package/src/extension/controller-host.ts +0 -167
- package/src/extension/deferred-turn-coordinator.ts +0 -171
- package/src/extension/deferred-turn.ts +0 -166
- package/src/extension/executor.ts +0 -411
- package/src/extension/follow-up-coordinator.ts +0 -151
- package/src/extension/recorder.ts +0 -655
- package/src/extension/session-events.ts +0 -121
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import type {
|
|
3
|
-
SqliteControllerStore,
|
|
4
|
-
WorkflowTurnIntentRecord,
|
|
5
|
-
WorkflowTurnIntentResolution,
|
|
6
|
-
} from "../controllers/sqlite.js";
|
|
7
|
-
import { deferredTurnMessageId } from "./deferred-turn.js";
|
|
8
|
-
|
|
9
|
-
export type BranchIntentResolution = {
|
|
10
|
-
resolution: WorkflowTurnIntentResolution;
|
|
11
|
-
messageId: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
type NaturalDelivery = {
|
|
15
|
-
runId: string;
|
|
16
|
-
targetSessionId: string;
|
|
17
|
-
resolution: Exclude<WorkflowTurnIntentResolution, "fallback">;
|
|
18
|
-
send: (turnIntentId?: string) => void;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
type FallbackDelivery = {
|
|
22
|
-
targetSessionId: string;
|
|
23
|
-
send: (intent: WorkflowTurnIntentRecord) => void;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export class DeferredTurnCoordinator {
|
|
27
|
-
private readonly store: () => SqliteControllerStore | null;
|
|
28
|
-
private readonly branchResolution: (intentId: string) => BranchIntentResolution | null;
|
|
29
|
-
private readonly leaseMs: number;
|
|
30
|
-
private readonly deferredNatural = new Map<string, NaturalDelivery>();
|
|
31
|
-
|
|
32
|
-
constructor(options: {
|
|
33
|
-
store: () => SqliteControllerStore | null;
|
|
34
|
-
branchResolution: (intentId: string) => BranchIntentResolution | null;
|
|
35
|
-
leaseMs: number;
|
|
36
|
-
}) {
|
|
37
|
-
this.store = options.store;
|
|
38
|
-
this.branchResolution = options.branchResolution;
|
|
39
|
-
this.leaseMs = options.leaseMs;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
sendNatural(delivery: NaturalDelivery, idle: boolean): "sent" | "deferred" | "suppressed" {
|
|
43
|
-
const store = this.store();
|
|
44
|
-
const intent = store?.findPendingWorkflowTurnIntent({
|
|
45
|
-
runId: delivery.runId,
|
|
46
|
-
targetSessionId: delivery.targetSessionId,
|
|
47
|
-
});
|
|
48
|
-
if (store === null || store === undefined || intent === undefined) {
|
|
49
|
-
delivery.send();
|
|
50
|
-
return "sent";
|
|
51
|
-
}
|
|
52
|
-
if (!idle) {
|
|
53
|
-
this.deferredNatural.set(intent.intentId, delivery);
|
|
54
|
-
return "deferred";
|
|
55
|
-
}
|
|
56
|
-
const result = this.deliverNatural(intent.intentId, delivery);
|
|
57
|
-
if (result === "deferred") this.deferredNatural.set(intent.intentId, delivery);
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
flushNatural(idle: boolean): number {
|
|
62
|
-
if (!idle) return 0;
|
|
63
|
-
let sent = 0;
|
|
64
|
-
for (const [intentId, delivery] of this.deferredNatural) {
|
|
65
|
-
const result = this.deliverNatural(intentId, delivery);
|
|
66
|
-
if (result !== "deferred") {
|
|
67
|
-
this.deferredNatural.delete(intentId);
|
|
68
|
-
}
|
|
69
|
-
if (result === "sent") sent += 1;
|
|
70
|
-
}
|
|
71
|
-
return sent;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
deliverFallbacks(delivery: FallbackDelivery, idle: boolean): number {
|
|
75
|
-
const store = this.store();
|
|
76
|
-
if (!idle || store === null) return 0;
|
|
77
|
-
const claimToken = randomUUID();
|
|
78
|
-
const intents = store.claimEligibleWorkflowTurnIntents({
|
|
79
|
-
targetSessionId: delivery.targetSessionId,
|
|
80
|
-
claimToken,
|
|
81
|
-
leaseMs: this.leaseMs,
|
|
82
|
-
});
|
|
83
|
-
let sent = 0;
|
|
84
|
-
for (const [index, intent] of intents.entries()) {
|
|
85
|
-
const branch = this.branchResolution(intent.intentId);
|
|
86
|
-
if (branch !== null) {
|
|
87
|
-
store.resolveWorkflowTurnIntent({
|
|
88
|
-
intentId: intent.intentId,
|
|
89
|
-
targetSessionId: delivery.targetSessionId,
|
|
90
|
-
claimToken,
|
|
91
|
-
resolution: branch.resolution,
|
|
92
|
-
messageId: branch.messageId,
|
|
93
|
-
});
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
try {
|
|
97
|
-
delivery.send(intent);
|
|
98
|
-
store.resolveWorkflowTurnIntent({
|
|
99
|
-
intentId: intent.intentId,
|
|
100
|
-
targetSessionId: delivery.targetSessionId,
|
|
101
|
-
claimToken,
|
|
102
|
-
resolution: "fallback",
|
|
103
|
-
messageId: deferredTurnMessageId(intent.intentId, "fallback"),
|
|
104
|
-
});
|
|
105
|
-
sent += 1;
|
|
106
|
-
} catch (error) {
|
|
107
|
-
for (const pending of intents.slice(index)) {
|
|
108
|
-
store.releaseWorkflowTurnIntentClaim({
|
|
109
|
-
intentId: pending.intentId,
|
|
110
|
-
targetSessionId: delivery.targetSessionId,
|
|
111
|
-
claimToken,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
throw error;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return sent;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
clearDeferred(): void {
|
|
121
|
-
this.deferredNatural.clear();
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
private deliverNatural(
|
|
125
|
-
intentId: string,
|
|
126
|
-
delivery: NaturalDelivery,
|
|
127
|
-
): "sent" | "deferred" | "suppressed" {
|
|
128
|
-
const store = this.store();
|
|
129
|
-
if (store === null) return "deferred";
|
|
130
|
-
const claimToken = randomUUID();
|
|
131
|
-
const intent = store.claimWorkflowTurnIntent({
|
|
132
|
-
intentId,
|
|
133
|
-
targetSessionId: delivery.targetSessionId,
|
|
134
|
-
claimToken,
|
|
135
|
-
leaseMs: this.leaseMs,
|
|
136
|
-
});
|
|
137
|
-
if (intent === undefined) {
|
|
138
|
-
const current = store.getWorkflowTurnIntent(intentId);
|
|
139
|
-
return current?.resolvedAt === null ? "deferred" : "suppressed";
|
|
140
|
-
}
|
|
141
|
-
const branch = this.branchResolution(intent.intentId);
|
|
142
|
-
if (branch !== null) {
|
|
143
|
-
store.resolveWorkflowTurnIntent({
|
|
144
|
-
intentId: intent.intentId,
|
|
145
|
-
targetSessionId: delivery.targetSessionId,
|
|
146
|
-
claimToken,
|
|
147
|
-
resolution: branch.resolution,
|
|
148
|
-
messageId: branch.messageId,
|
|
149
|
-
});
|
|
150
|
-
return "suppressed";
|
|
151
|
-
}
|
|
152
|
-
try {
|
|
153
|
-
delivery.send(intent.intentId);
|
|
154
|
-
store.resolveWorkflowTurnIntent({
|
|
155
|
-
intentId: intent.intentId,
|
|
156
|
-
targetSessionId: delivery.targetSessionId,
|
|
157
|
-
claimToken,
|
|
158
|
-
resolution: delivery.resolution,
|
|
159
|
-
messageId: deferredTurnMessageId(intent.intentId, delivery.resolution),
|
|
160
|
-
});
|
|
161
|
-
return "sent";
|
|
162
|
-
} catch (error) {
|
|
163
|
-
store.releaseWorkflowTurnIntentClaim({
|
|
164
|
-
intentId: intent.intentId,
|
|
165
|
-
targetSessionId: delivery.targetSessionId,
|
|
166
|
-
claimToken,
|
|
167
|
-
});
|
|
168
|
-
throw error;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import type {
|
|
3
|
-
WorkflowTurnIntentCause,
|
|
4
|
-
WorkflowTurnIntentFacts,
|
|
5
|
-
WorkflowTurnIntentRecord,
|
|
6
|
-
WorkflowTurnIntentResolution,
|
|
7
|
-
} from "../controllers/sqlite.js";
|
|
8
|
-
|
|
9
|
-
export const DEFERRED_TURN_MESSAGE_TYPE = "pi-workflows-deferred-turn";
|
|
10
|
-
export const DEFERRED_TURN_MESSAGE_SCHEMA = "pi-workflows.deferred-turn-message.v1";
|
|
11
|
-
const FACTS_SCHEMA = "pi-workflows.deferred-turn-facts.v1";
|
|
12
|
-
const MAX_REASON_CHARS = 8_192;
|
|
13
|
-
|
|
14
|
-
export type DeferredTurnSource = {
|
|
15
|
-
runId: string;
|
|
16
|
-
workflowName: string;
|
|
17
|
-
targetSessionId: string;
|
|
18
|
-
cause: WorkflowTurnIntentCause;
|
|
19
|
-
sourceEventId: string;
|
|
20
|
-
observedState: string;
|
|
21
|
-
nodeId?: string | null;
|
|
22
|
-
attemptId?: string | null;
|
|
23
|
-
reason?: string | null;
|
|
24
|
-
handoff?: boolean;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export type DeferredTurnDescriptor = {
|
|
28
|
-
intentId: string;
|
|
29
|
-
sourceEventId: string;
|
|
30
|
-
runId: string;
|
|
31
|
-
workflowRef: string;
|
|
32
|
-
targetSessionId: string;
|
|
33
|
-
cause: WorkflowTurnIntentCause;
|
|
34
|
-
nodeId: string | null;
|
|
35
|
-
attemptId: string | null;
|
|
36
|
-
fallbackFacts: WorkflowTurnIntentFacts;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export type DeferredTurnMessageDetails = {
|
|
40
|
-
schema: typeof DEFERRED_TURN_MESSAGE_SCHEMA;
|
|
41
|
-
turnIntentId: string;
|
|
42
|
-
runId: string;
|
|
43
|
-
cause: WorkflowTurnIntentCause;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export function deferredTurnSourceEventId(options: {
|
|
47
|
-
runId: string;
|
|
48
|
-
cause: WorkflowTurnIntentCause;
|
|
49
|
-
nodeId?: string | null;
|
|
50
|
-
attemptId?: string | null;
|
|
51
|
-
source?: string;
|
|
52
|
-
}): string {
|
|
53
|
-
const parts = [
|
|
54
|
-
options.runId,
|
|
55
|
-
options.source ?? "run",
|
|
56
|
-
options.nodeId ?? "$run",
|
|
57
|
-
options.attemptId ?? "$none",
|
|
58
|
-
options.cause,
|
|
59
|
-
];
|
|
60
|
-
return `turn-source:${digest(parts)}`;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function createDeferredTurnDescriptor(source: DeferredTurnSource): DeferredTurnDescriptor {
|
|
64
|
-
const nodeId = source.nodeId ?? null;
|
|
65
|
-
const attemptId = source.attemptId ?? null;
|
|
66
|
-
const fallbackFacts = createDeferredTurnFacts(source);
|
|
67
|
-
return {
|
|
68
|
-
intentId: `deferred-turn:${digest([
|
|
69
|
-
source.targetSessionId,
|
|
70
|
-
source.runId,
|
|
71
|
-
source.sourceEventId,
|
|
72
|
-
nodeId ?? "$run",
|
|
73
|
-
attemptId ?? "$none",
|
|
74
|
-
source.cause,
|
|
75
|
-
])}`,
|
|
76
|
-
sourceEventId: source.sourceEventId,
|
|
77
|
-
runId: source.runId,
|
|
78
|
-
workflowRef: source.workflowName,
|
|
79
|
-
targetSessionId: source.targetSessionId,
|
|
80
|
-
cause: source.cause,
|
|
81
|
-
nodeId,
|
|
82
|
-
attemptId,
|
|
83
|
-
fallbackFacts,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function createDeferredTurnFacts(source: DeferredTurnSource): WorkflowTurnIntentFacts {
|
|
88
|
-
return {
|
|
89
|
-
schema: FACTS_SCHEMA,
|
|
90
|
-
workflowName: source.workflowName,
|
|
91
|
-
runId: source.runId,
|
|
92
|
-
observedState: source.observedState,
|
|
93
|
-
cause: source.cause,
|
|
94
|
-
nodeId: source.nodeId ?? null,
|
|
95
|
-
attemptId: source.attemptId ?? null,
|
|
96
|
-
reason: boundedReason(source.reason),
|
|
97
|
-
handoff: source.handoff ?? false,
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function deferredTurnMessageId(
|
|
102
|
-
intentId: string,
|
|
103
|
-
resolution: WorkflowTurnIntentResolution,
|
|
104
|
-
): string {
|
|
105
|
-
return `turn-message:${digest([intentId, resolution])}`;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function deferredTurnMessageDetails(
|
|
109
|
-
intent: WorkflowTurnIntentRecord,
|
|
110
|
-
): DeferredTurnMessageDetails {
|
|
111
|
-
return {
|
|
112
|
-
schema: DEFERRED_TURN_MESSAGE_SCHEMA,
|
|
113
|
-
turnIntentId: intent.intentId,
|
|
114
|
-
runId: intent.runId,
|
|
115
|
-
cause: intent.cause,
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export function buildDeferredTurnContent(intent: WorkflowTurnIntentRecord): string {
|
|
120
|
-
const facts = intent.fallbackFacts;
|
|
121
|
-
const source = [
|
|
122
|
-
facts.nodeId === null ? null : `node ${facts.nodeId}`,
|
|
123
|
-
facts.attemptId === null ? null : `attempt ${facts.attemptId}`,
|
|
124
|
-
]
|
|
125
|
-
.filter((part): part is string => part !== null)
|
|
126
|
-
.join(", ");
|
|
127
|
-
const reason =
|
|
128
|
-
facts.reason === null
|
|
129
|
-
? ""
|
|
130
|
-
: ` Reason: ${facts.reason}${/[.!?]$/.test(facts.reason) ? "" : "."}`;
|
|
131
|
-
const sourceText = source.length === 0 ? "" : ` Source: ${source}.`;
|
|
132
|
-
if (facts.cause === "launchFailed") {
|
|
133
|
-
return [
|
|
134
|
-
`Workflow ${facts.workflowName} failed to start (run ${facts.runId}).`,
|
|
135
|
-
`${sourceText}${reason}`.trim(),
|
|
136
|
-
"Inspect the durable workflow state before you explain the outcome or start a corrected workflow.",
|
|
137
|
-
]
|
|
138
|
-
.filter((part) => part.length > 0)
|
|
139
|
-
.join(" ");
|
|
140
|
-
}
|
|
141
|
-
if (facts.handoff) {
|
|
142
|
-
return [
|
|
143
|
-
`Workflow ${facts.workflowName} was handed to another runner (run ${facts.runId}).`,
|
|
144
|
-
`Observed state: ${facts.observedState}.${sourceText}${reason}`,
|
|
145
|
-
"Inspect the durable workflow state before you explain the outcome or take any authorized corrective action.",
|
|
146
|
-
].join(" ");
|
|
147
|
-
}
|
|
148
|
-
return [
|
|
149
|
-
`Workflow ${facts.workflowName} ended with state ${facts.observedState} (run ${facts.runId}).`,
|
|
150
|
-
`${sourceText}${reason}`.trim(),
|
|
151
|
-
"Inspect the durable workflow state before you explain the outcome or take any authorized corrective action.",
|
|
152
|
-
]
|
|
153
|
-
.filter((part) => part.length > 0)
|
|
154
|
-
.join(" ");
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function boundedReason(value: string | null | undefined): string | null {
|
|
158
|
-
if (value === null || value === undefined) return null;
|
|
159
|
-
const trimmed = value.trim();
|
|
160
|
-
if (trimmed.length === 0) return null;
|
|
161
|
-
return trimmed.length <= MAX_REASON_CHARS ? trimmed : trimmed.slice(0, MAX_REASON_CHARS);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function digest(parts: readonly string[]): string {
|
|
165
|
-
return createHash("sha256").update(JSON.stringify(parts)).digest("hex");
|
|
166
|
-
}
|