@deepseek-ai/dsh-tool-workflow 0.2.0-rc.2 → 0.2.1-alpha.1
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.i18n.yaml +6 -6
- package/README.md +2 -3
- package/README.zh.md +2 -3
- package/package.json +24 -31
- package/lib/invariant.js +0 -129
- package/lib/types/invariant.d.ts +0 -9
- package/lib/types/invariant.js +0 -152
package/README.i18n.yaml
CHANGED
|
@@ -42,17 +42,17 @@
|
|
|
42
42
|
en: 35950b2015800ec5
|
|
43
43
|
zh: 5ae6f5a5e53dc585
|
|
44
44
|
/deepseek-ai-dsh-tool-workflow/understand-the-implementation/durable-session-records:
|
|
45
|
-
en:
|
|
46
|
-
zh:
|
|
45
|
+
en: e5bbbb7788d6dcdf
|
|
46
|
+
zh: aa035f074f7ac021
|
|
47
47
|
/deepseek-ai-dsh-tool-workflow/understand-the-implementation/render-intent:
|
|
48
48
|
en: 389b3740ec1f5ac2
|
|
49
49
|
zh: 78430014f5e270e2
|
|
50
50
|
/deepseek-ai-dsh-tool-workflow/understand-the-implementation/source-map:
|
|
51
|
-
en:
|
|
52
|
-
zh:
|
|
51
|
+
en: c22defb9681d39c7
|
|
52
|
+
zh: 0856b88d731fb127
|
|
53
53
|
/deepseek-ai-dsh-tool-workflow/further-exploration:
|
|
54
|
-
en:
|
|
55
|
-
zh:
|
|
54
|
+
en: 7522ab6b5866e47b
|
|
55
|
+
zh: 0fdfb25026defbff
|
|
56
56
|
/deepseek-ai-dsh-tool-workflow/model-experience:
|
|
57
57
|
en: 215e7ba838619b7b
|
|
58
58
|
zh: a311da3843709f90
|
package/README.md
CHANGED
|
@@ -75,7 +75,7 @@ A background call registers the run through `jobs.start` inside the job starter,
|
|
|
75
75
|
|
|
76
76
|
### Durable session records
|
|
77
77
|
|
|
78
|
-
For a root transport execution (`exec.parent` absent), the tool projects the run into the calling Agent's Session with four log-only events: run-start after `start()` returns, member starts and endings filtered by `run.id`, then run-end only after the result is available and disposal reaches quiescence. Nested transport calls execute normally but write no record. The first failed Session append disables later recording for that run with one warning, leaving either no record or a legal continuous prefix without changing the tool result or cleanup.
|
|
78
|
+
For a root transport execution (`exec.parent` absent), the tool projects the run into the calling Agent's Session with four log-only events: run-start after `start()` returns, member starts and endings filtered by `run.id`, then run-end only after the result is available and disposal reaches quiescence. Nested transport calls execute normally but write no record. The first failed Session append disables later recording for that run with one warning, leaving either no record or a legal continuous prefix without changing the tool result or cleanup.
|
|
79
79
|
|
|
80
80
|
The engine's `workflow/phase` and `workflow/log` events have no per-line durable surface from this tool: the session log deliberately records run and member lifecycle only, and the Web transcript derives from those records. A background run's lines reach a human through the job observation record instead, which is transient by design.
|
|
81
81
|
|
|
@@ -90,7 +90,6 @@ Decided up front per the [render-intent Agent Note](../../../.agents/notes/imple
|
|
|
90
90
|
| [`src/index.ts`](src/index.ts) | Plugin entry: tool registration, run lifecycle, background job registration, recorder wiring |
|
|
91
91
|
| [`src/record.ts`](src/record.ts) | Background runs' live-progress mirror into the job's output ring |
|
|
92
92
|
| [`src/types.ts`](src/types.ts) | The four log-only record event payloads and their `SessionEventMap` declaration |
|
|
93
|
-
| [`src/invariant.ts`](src/invariant.ts) | Invariant companion: durable workflow-record protocol validation |
|
|
94
93
|
|
|
95
94
|
</details>
|
|
96
95
|
|
|
@@ -106,7 +105,7 @@ Read these pages when the tool-level contract is not enough. They move from the
|
|
|
106
105
|
- [PTC workflow engine](../workflow-ptc/README.md) — the engine that executes the scripts.
|
|
107
106
|
- [subagent tool](../../subagent/tool-subagent/README.md) — the plain-delegation alternative for one or two children.
|
|
108
107
|
- [Group map](../README.md) — the workflow capability family and its packages.
|
|
109
|
-
- [Dynamic workflows Agent Note](../../../.agents/notes/
|
|
108
|
+
- [historical Dynamic workflows Agent Note](../../../.agents/notes/archived/feature/2026-07-05-dynamic-workflows.md) — the seam design and its decisions.
|
|
110
109
|
|
|
111
110
|
-----
|
|
112
111
|
|
package/README.zh.md
CHANGED
|
@@ -75,7 +75,7 @@ kind: "package-reference"
|
|
|
75
75
|
|
|
76
76
|
### 持久会话记录
|
|
77
77
|
|
|
78
|
-
对于根 transport 执行(`exec.parent` 缺省),工具会用四个 log-only 事件把运行投影到调用方 agent 的会话:`start()` 返回后写 run-start,只记录 `run.id` 匹配的成员开始与结束,并且只在结果可用且 dispose 完全停稳后写 run-end。嵌套 transport
|
|
78
|
+
对于根 transport 执行(`exec.parent` 缺省),工具会用四个 log-only 事件把运行投影到调用方 agent 的会话:`start()` 返回后写 run-start,只记录 `run.id` 匹配的成员开始与结束,并且只在结果可用且 dispose 完全停稳后写 run-end。嵌套 transport 调用照常执行,但不写任何记录。会话追加操作首次失败后,本运行会停止后续记录并只告警一次,留下空记录或合法连续前缀,同时不改变工具结果和清理。
|
|
79
79
|
|
|
80
80
|
引擎的 `workflow/phase` 与 `workflow/log` 事件在本工具没有逐行的持久面:会话日志刻意只记录 run 与成员生命周期,Web transcript 由这些记录派生。后台运行的这些行改经任务观察 record 抵达人类,而 record 的瞬态是设计使然。
|
|
81
81
|
|
|
@@ -90,7 +90,6 @@ kind: "package-reference"
|
|
|
90
90
|
| [`src/index.ts`](src/index.ts) | 插件入口:工具注册、运行生命周期、后台任务注册、记录器接线 |
|
|
91
91
|
| [`src/record.ts`](src/record.ts) | 后台运行进任务输出环的实时进度镜像 |
|
|
92
92
|
| [`src/types.ts`](src/types.ts) | 四个 log-only 记录事件 payload 及其 `SessionEventMap` 声明 |
|
|
93
|
-
| [`src/invariant.ts`](src/invariant.ts) | 不变式配套入口:持久工作流记录协议校验 |
|
|
94
93
|
|
|
95
94
|
</details>
|
|
96
95
|
|
|
@@ -106,7 +105,7 @@ kind: "package-reference"
|
|
|
106
105
|
- [PTC 工作流引擎](../workflow-ptc/README.zh.md)——执行脚本的引擎。
|
|
107
106
|
- [subagent 工具](../../subagent/tool-subagent/README.zh.md)——一两项委派时的普通委派替代方案。
|
|
108
107
|
- [组地图](../README.zh.md)——工作流能力家族及其包。
|
|
109
|
-
- [
|
|
108
|
+
- [历史动态工作流 Agent Note](../../../.agents/notes/archived/feature/2026-07-05-dynamic-workflows.md)——seam 设计及其决策。
|
|
110
109
|
|
|
111
110
|
-----
|
|
112
111
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deepseek-ai/dsh-tool-workflow",
|
|
3
3
|
"description": "Model-facing workflow tool: run a JavaScript orchestration script over ctx.workflowEngine",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.1-alpha.1",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -18,10 +18,6 @@
|
|
|
18
18
|
"types": "./lib/types/index.d.ts",
|
|
19
19
|
"default": "./lib/index.js"
|
|
20
20
|
},
|
|
21
|
-
"./invariant": {
|
|
22
|
-
"types": "./lib/types/invariant.d.ts",
|
|
23
|
-
"default": "./lib/invariant.js"
|
|
24
|
-
},
|
|
25
21
|
"./types": {
|
|
26
22
|
"types": "./lib/types/types.d.ts",
|
|
27
23
|
"default": "./lib/types/types.js"
|
|
@@ -31,40 +27,37 @@
|
|
|
31
27
|
},
|
|
32
28
|
"files": [
|
|
33
29
|
"lib/index.js",
|
|
34
|
-
"lib/invariant.js",
|
|
35
30
|
"lib/types/**/*.js",
|
|
36
31
|
"lib/types/**/*.d.ts"
|
|
37
32
|
],
|
|
38
33
|
"license": "MIT",
|
|
39
34
|
"peerDependencies": {
|
|
40
|
-
"@deepseek-ai/cordis": "~4.0.
|
|
41
|
-
"@deepseek-ai/dsh-agent": "0.2.
|
|
42
|
-
"@deepseek-ai/dsh-
|
|
43
|
-
"@deepseek-ai/dsh-
|
|
44
|
-
"@deepseek-ai/dsh-
|
|
45
|
-
"@deepseek-ai/dsh-
|
|
46
|
-
"@deepseek-ai/dsh-
|
|
47
|
-
"@deepseek-ai/dsh-
|
|
48
|
-
"@deepseek-ai/dsh-tools": "0.2.0-rc.2"
|
|
35
|
+
"@deepseek-ai/cordis": "~4.0.5-alpha.1",
|
|
36
|
+
"@deepseek-ai/dsh-agent": "0.2.1-alpha.1",
|
|
37
|
+
"@deepseek-ai/dsh-jobs": "0.2.1-alpha.1",
|
|
38
|
+
"@deepseek-ai/dsh-session": "0.2.1-alpha.1",
|
|
39
|
+
"@deepseek-ai/dsh-llm": "0.2.1-alpha.1",
|
|
40
|
+
"@deepseek-ai/dsh-system-prompt": "0.2.1-alpha.1",
|
|
41
|
+
"@deepseek-ai/dsh-tools": "0.2.1-alpha.1",
|
|
42
|
+
"@deepseek-ai/dsh-workflow": "0.2.1-alpha.1"
|
|
49
43
|
},
|
|
50
44
|
"dependencies": {
|
|
51
|
-
"@deepseek-ai/schemastery": "~3.18.
|
|
45
|
+
"@deepseek-ai/schemastery": "~3.18.5-alpha.1"
|
|
52
46
|
},
|
|
53
47
|
"devDependencies": {
|
|
54
|
-
"@deepseek-ai/cordis": "~4.0.
|
|
55
|
-
"@deepseek-ai/dsh-agent": "0.2.
|
|
56
|
-
"@deepseek-ai/dsh-
|
|
57
|
-
"@deepseek-ai/dsh-
|
|
58
|
-
"@deepseek-ai/dsh-
|
|
59
|
-
"@deepseek-ai/dsh-
|
|
60
|
-
"@deepseek-ai/dsh-llm": "0.2.
|
|
61
|
-
"@deepseek-ai/dsh-session": "0.2.
|
|
62
|
-
"@deepseek-ai/dsh-
|
|
63
|
-
"@deepseek-ai/dsh-
|
|
64
|
-
"@deepseek-ai/dsh-
|
|
65
|
-
"@deepseek-ai/dsh-
|
|
66
|
-
"@deepseek-ai/dsh-workflow-ptc": "0.2.
|
|
67
|
-
"@deepseek-ai/dsh-
|
|
68
|
-
"@deepseek-ai/dsh-session-projection": "0.2.0-rc.2"
|
|
48
|
+
"@deepseek-ai/cordis": "~4.0.5-alpha.1",
|
|
49
|
+
"@deepseek-ai/dsh-agent-loop-testkit": "0.2.1-alpha.1",
|
|
50
|
+
"@deepseek-ai/dsh-jobs": "0.2.1-alpha.1",
|
|
51
|
+
"@deepseek-ai/dsh-jobs-local": "0.2.1-alpha.1",
|
|
52
|
+
"@deepseek-ai/dsh-agent": "0.2.1-alpha.1",
|
|
53
|
+
"@deepseek-ai/dsh-session": "0.2.1-alpha.1",
|
|
54
|
+
"@deepseek-ai/dsh-llm": "0.2.1-alpha.1",
|
|
55
|
+
"@deepseek-ai/dsh-session-projection": "0.2.1-alpha.1",
|
|
56
|
+
"@deepseek-ai/dsh-system-prompt": "0.2.1-alpha.1",
|
|
57
|
+
"@deepseek-ai/dsh-subagent": "0.2.1-alpha.1",
|
|
58
|
+
"@deepseek-ai/dsh-tool-jobs": "0.2.1-alpha.1",
|
|
59
|
+
"@deepseek-ai/dsh-workflow": "0.2.1-alpha.1",
|
|
60
|
+
"@deepseek-ai/dsh-workflow-ptc": "0.2.1-alpha.1",
|
|
61
|
+
"@deepseek-ai/dsh-tools": "0.2.1-alpha.1"
|
|
69
62
|
}
|
|
70
63
|
}
|
package/lib/invariant.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
//#region lib/types/invariant.js
|
|
2
|
-
/** Package-owned durable workflow-record invariants. @module @deepseek-ai/dsh-tool-workflow/invariant */
|
|
3
|
-
const PACKAGE_NAME = "@deepseek-ai/dsh-tool-workflow";
|
|
4
|
-
/** Cordis companion plugin name. */
|
|
5
|
-
const name = "tool-workflow-invariant";
|
|
6
|
-
/** Services required to validate existing and newly appended Session logs. */
|
|
7
|
-
const inject = ["invariants"];
|
|
8
|
-
/** Whether this package owns the candidate Session event. */
|
|
9
|
-
function isWorkflowRecordEvent(event) {
|
|
10
|
-
return event.type.startsWith("tool-workflow/");
|
|
11
|
-
}
|
|
12
|
-
/** Require a durable opaque identity to be a non-empty string. */
|
|
13
|
-
function stringId(value, label, fail) {
|
|
14
|
-
if (typeof value !== "string" || value.length === 0) fail(`${label} must be a non-empty string`);
|
|
15
|
-
return value;
|
|
16
|
-
}
|
|
17
|
-
/** Require one workflow member's 1-based sequence identity. */
|
|
18
|
-
function memberSeq(value, fail) {
|
|
19
|
-
if (!Number.isSafeInteger(value) || value < 1) fail("tool-workflow member seq must be a positive safe integer");
|
|
20
|
-
return value;
|
|
21
|
-
}
|
|
22
|
-
/** Read one plain payload field without trusting restored plugin data. */
|
|
23
|
-
function recordOf(event, fail) {
|
|
24
|
-
const data = event.data;
|
|
25
|
-
if (data === null || typeof data !== "object" || Array.isArray(data)) fail(`${event.type} data must be a JSON object`);
|
|
26
|
-
return data;
|
|
27
|
-
}
|
|
28
|
-
/** Copy only the run one candidate can mutate; other committed states stay shared. */
|
|
29
|
-
function cloneTraceForEvent(source, event, fail) {
|
|
30
|
-
const trace = new Map(source);
|
|
31
|
-
if (event.type === "tool-workflow/run-start") return trace;
|
|
32
|
-
const runId = stringId(recordOf(event, fail).runId, `${event.type} runId`, fail);
|
|
33
|
-
const run = source.get(runId);
|
|
34
|
-
if (run !== void 0) trace.set(runId, {
|
|
35
|
-
ended: run.ended,
|
|
36
|
-
members: new Map(run.members)
|
|
37
|
-
});
|
|
38
|
-
return trace;
|
|
39
|
-
}
|
|
40
|
-
/** Require the named run to exist and remain open. */
|
|
41
|
-
function openRun(trace, runId, eventType, fail) {
|
|
42
|
-
const run = trace.get(runId);
|
|
43
|
-
if (run === void 0) fail(`${eventType} has no matching tool-workflow/run-start for run ${runId}`);
|
|
44
|
-
if (run.ended) fail(`${eventType} appears after tool-workflow/run-end for run ${runId}`);
|
|
45
|
-
return run;
|
|
46
|
-
}
|
|
47
|
-
/** Advance the workflow-record fold with one relevant Session event. */
|
|
48
|
-
function applyEvent(trace, event, fail) {
|
|
49
|
-
const data = recordOf(event, fail);
|
|
50
|
-
const runId = stringId(data.runId, `${event.type} runId`, fail);
|
|
51
|
-
switch (event.type) {
|
|
52
|
-
case "tool-workflow/run-start":
|
|
53
|
-
if (typeof data.name !== "string" || data.name.length === 0) fail("tool-workflow/run-start name must be a non-empty string");
|
|
54
|
-
if (trace.has(runId)) fail(`tool-workflow/run-start repeats run ${runId}`);
|
|
55
|
-
trace.set(runId, {
|
|
56
|
-
ended: false,
|
|
57
|
-
members: /* @__PURE__ */ new Map()
|
|
58
|
-
});
|
|
59
|
-
return;
|
|
60
|
-
case "tool-workflow/agent-start": {
|
|
61
|
-
const run = openRun(trace, runId, event.type, fail);
|
|
62
|
-
const seq = memberSeq(data.seq, fail);
|
|
63
|
-
if (typeof data.label !== "string") fail("tool-workflow/agent-start label must be a string");
|
|
64
|
-
if (data.phase !== void 0 && typeof data.phase !== "string") fail("tool-workflow/agent-start phase must be a string when present");
|
|
65
|
-
stringId(data.childId, "tool-workflow/agent-start childId", fail);
|
|
66
|
-
if (run.members.has(seq)) fail(`tool-workflow/agent-start repeats member seq ${seq} in run ${runId}`);
|
|
67
|
-
run.members.set(seq, false);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
case "tool-workflow/agent-end": {
|
|
71
|
-
const run = openRun(trace, runId, event.type, fail);
|
|
72
|
-
const seq = memberSeq(data.seq, fail);
|
|
73
|
-
if (data.outcome !== "completed" && data.outcome !== "failed" && data.outcome !== "cancelled") fail(`tool-workflow/agent-end outcome ${String(data.outcome)} is invalid`);
|
|
74
|
-
const ended = run.members.get(seq);
|
|
75
|
-
if (ended === void 0) fail(`tool-workflow/agent-end has no matching member seq ${seq} in run ${runId}`);
|
|
76
|
-
if (ended) fail(`tool-workflow/agent-end repeats member seq ${seq} in run ${runId}`);
|
|
77
|
-
run.members.set(seq, true);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
case "tool-workflow/run-end": {
|
|
81
|
-
const run = openRun(trace, runId, event.type, fail);
|
|
82
|
-
if (data.stopReason !== "completed" && data.stopReason !== "cancelled" && data.stopReason !== "error") fail(`tool-workflow/run-end stopReason ${String(data.stopReason)} is invalid`);
|
|
83
|
-
const openMembers = [...run.members].filter(([, ended]) => !ended).map(([seq]) => seq);
|
|
84
|
-
if (openMembers.length > 0) fail(`tool-workflow/run-end leaves member seq ${openMembers.join(", ")} open in run ${runId}`);
|
|
85
|
-
run.ended = true;
|
|
86
|
-
run.members.clear();
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
default: fail(`unknown tool-workflow event type ${event.type}`);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/** Install an independent incremental fold over every attached Session. */
|
|
93
|
-
const install = Object.assign((ctx, fail) => {
|
|
94
|
-
const traces = /* @__PURE__ */ new WeakMap();
|
|
95
|
-
const staged = /* @__PURE__ */ new WeakMap();
|
|
96
|
-
const seed = (session) => {
|
|
97
|
-
const trace = /* @__PURE__ */ new Map();
|
|
98
|
-
for (const event of session.snapshotEvents().filter(isWorkflowRecordEvent)) applyEvent(trace, event, fail);
|
|
99
|
-
traces.set(session, trace);
|
|
100
|
-
return trace;
|
|
101
|
-
};
|
|
102
|
-
ctx.sessions.list().forEach(seed);
|
|
103
|
-
ctx.on("session/created", (session) => {
|
|
104
|
-
seed(session);
|
|
105
|
-
}, { global: true });
|
|
106
|
-
ctx.on("internal/dispatch", (_mode, eventName, args) => {
|
|
107
|
-
if (eventName !== "session/event") return;
|
|
108
|
-
const [session, event] = args;
|
|
109
|
-
if (!isWorkflowRecordEvent(event)) return;
|
|
110
|
-
const trace = cloneTraceForEvent(traces.get(session), event, fail);
|
|
111
|
-
applyEvent(trace, event, fail);
|
|
112
|
-
staged.set(event, {
|
|
113
|
-
session,
|
|
114
|
-
trace
|
|
115
|
-
});
|
|
116
|
-
}, { global: true });
|
|
117
|
-
ctx.on("session/event", (session, event) => {
|
|
118
|
-
if (!isWorkflowRecordEvent(event)) return;
|
|
119
|
-
const candidate = staged.get(event);
|
|
120
|
-
/* v8 ignore next 2 -- internal/dispatch stages the exact session/event callback arguments. */
|
|
121
|
-
if (candidate === void 0 || candidate.session !== session) return fail("session/event reached publication without matching workflow-record validation");
|
|
122
|
-
staged.delete(event);
|
|
123
|
-
traces.set(session, candidate.trace);
|
|
124
|
-
}, { global: true });
|
|
125
|
-
}, { inject: ["sessions"] });
|
|
126
|
-
/** Register this package's invariant companion. */
|
|
127
|
-
const apply = (ctx) => Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install));
|
|
128
|
-
//#endregion
|
|
129
|
-
export { apply, inject, name };
|
package/lib/types/invariant.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/** Package-owned durable workflow-record invariants. @module @deepseek-ai/dsh-tool-workflow/invariant */
|
|
2
|
-
import type { Context } from '@deepseek-ai/cordis';
|
|
3
|
-
/** Cordis companion plugin name. */
|
|
4
|
-
export declare const name = "tool-workflow-invariant";
|
|
5
|
-
/** Services required to validate existing and newly appended Session logs. */
|
|
6
|
-
export declare const inject: string[];
|
|
7
|
-
/** Register this package's invariant companion. */
|
|
8
|
-
export declare const apply: (ctx: Context) => Promise<() => void>;
|
|
9
|
-
//# sourceMappingURL=invariant.d.ts.map
|
package/lib/types/invariant.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
/** Package-owned durable workflow-record invariants. @module @deepseek-ai/dsh-tool-workflow/invariant */
|
|
2
|
-
const PACKAGE_NAME = '@deepseek-ai/dsh-tool-workflow';
|
|
3
|
-
/** Cordis companion plugin name. */
|
|
4
|
-
export const name = 'tool-workflow-invariant';
|
|
5
|
-
/** Services required to validate existing and newly appended Session logs. */
|
|
6
|
-
export const inject = ['invariants'];
|
|
7
|
-
/** Whether this package owns the candidate Session event. */
|
|
8
|
-
function isWorkflowRecordEvent(event) {
|
|
9
|
-
return event.type.startsWith('tool-workflow/');
|
|
10
|
-
}
|
|
11
|
-
/** Require a durable opaque identity to be a non-empty string. */
|
|
12
|
-
function stringId(value, label, fail) {
|
|
13
|
-
if (typeof value !== 'string' || value.length === 0)
|
|
14
|
-
fail(`${label} must be a non-empty string`);
|
|
15
|
-
return value;
|
|
16
|
-
}
|
|
17
|
-
/** Require one workflow member's 1-based sequence identity. */
|
|
18
|
-
function memberSeq(value, fail) {
|
|
19
|
-
if (!Number.isSafeInteger(value) || value < 1) {
|
|
20
|
-
fail('tool-workflow member seq must be a positive safe integer');
|
|
21
|
-
}
|
|
22
|
-
return value;
|
|
23
|
-
}
|
|
24
|
-
/** Read one plain payload field without trusting restored plugin data. */
|
|
25
|
-
function recordOf(event, fail) {
|
|
26
|
-
const data = event.data;
|
|
27
|
-
if (data === null || typeof data !== 'object' || Array.isArray(data)) {
|
|
28
|
-
fail(`${event.type} data must be a JSON object`);
|
|
29
|
-
}
|
|
30
|
-
return data;
|
|
31
|
-
}
|
|
32
|
-
/** Copy only the run one candidate can mutate; other committed states stay shared. */
|
|
33
|
-
function cloneTraceForEvent(source, event, fail) {
|
|
34
|
-
const trace = new Map(source);
|
|
35
|
-
if (event.type === 'tool-workflow/run-start')
|
|
36
|
-
return trace;
|
|
37
|
-
const data = recordOf(event, fail);
|
|
38
|
-
const runId = stringId(data.runId, `${event.type} runId`, fail);
|
|
39
|
-
const run = source.get(runId);
|
|
40
|
-
if (run !== undefined) {
|
|
41
|
-
trace.set(runId, { ended: run.ended, members: new Map(run.members) });
|
|
42
|
-
}
|
|
43
|
-
return trace;
|
|
44
|
-
}
|
|
45
|
-
/** Require the named run to exist and remain open. */
|
|
46
|
-
function openRun(trace, runId, eventType, fail) {
|
|
47
|
-
const run = trace.get(runId);
|
|
48
|
-
if (run === undefined)
|
|
49
|
-
fail(`${eventType} has no matching tool-workflow/run-start for run ${runId}`);
|
|
50
|
-
if (run.ended)
|
|
51
|
-
fail(`${eventType} appears after tool-workflow/run-end for run ${runId}`);
|
|
52
|
-
return run;
|
|
53
|
-
}
|
|
54
|
-
/** Advance the workflow-record fold with one relevant Session event. */
|
|
55
|
-
function applyEvent(trace, event, fail) {
|
|
56
|
-
const data = recordOf(event, fail);
|
|
57
|
-
const runId = stringId(data.runId, `${event.type} runId`, fail);
|
|
58
|
-
switch (event.type) {
|
|
59
|
-
case 'tool-workflow/run-start': {
|
|
60
|
-
if (typeof data.name !== 'string' || data.name.length === 0) {
|
|
61
|
-
fail('tool-workflow/run-start name must be a non-empty string');
|
|
62
|
-
}
|
|
63
|
-
if (trace.has(runId))
|
|
64
|
-
fail(`tool-workflow/run-start repeats run ${runId}`);
|
|
65
|
-
trace.set(runId, { ended: false, members: new Map() });
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
case 'tool-workflow/agent-start': {
|
|
69
|
-
const run = openRun(trace, runId, event.type, fail);
|
|
70
|
-
const seq = memberSeq(data.seq, fail);
|
|
71
|
-
if (typeof data.label !== 'string')
|
|
72
|
-
fail('tool-workflow/agent-start label must be a string');
|
|
73
|
-
if (data.phase !== undefined && typeof data.phase !== 'string') {
|
|
74
|
-
fail('tool-workflow/agent-start phase must be a string when present');
|
|
75
|
-
}
|
|
76
|
-
stringId(data.childId, 'tool-workflow/agent-start childId', fail);
|
|
77
|
-
if (run.members.has(seq))
|
|
78
|
-
fail(`tool-workflow/agent-start repeats member seq ${seq} in run ${runId}`);
|
|
79
|
-
run.members.set(seq, false);
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
case 'tool-workflow/agent-end': {
|
|
83
|
-
const run = openRun(trace, runId, event.type, fail);
|
|
84
|
-
const seq = memberSeq(data.seq, fail);
|
|
85
|
-
if (data.outcome !== 'completed' && data.outcome !== 'failed' && data.outcome !== 'cancelled') {
|
|
86
|
-
fail(`tool-workflow/agent-end outcome ${String(data.outcome)} is invalid`);
|
|
87
|
-
}
|
|
88
|
-
const ended = run.members.get(seq);
|
|
89
|
-
if (ended === undefined)
|
|
90
|
-
fail(`tool-workflow/agent-end has no matching member seq ${seq} in run ${runId}`);
|
|
91
|
-
if (ended)
|
|
92
|
-
fail(`tool-workflow/agent-end repeats member seq ${seq} in run ${runId}`);
|
|
93
|
-
run.members.set(seq, true);
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
case 'tool-workflow/run-end': {
|
|
97
|
-
const run = openRun(trace, runId, event.type, fail);
|
|
98
|
-
if (data.stopReason !== 'completed' && data.stopReason !== 'cancelled' && data.stopReason !== 'error') {
|
|
99
|
-
fail(`tool-workflow/run-end stopReason ${String(data.stopReason)} is invalid`);
|
|
100
|
-
}
|
|
101
|
-
const openMembers = [...run.members].filter(([, ended]) => !ended).map(([seq]) => seq);
|
|
102
|
-
if (openMembers.length > 0) {
|
|
103
|
-
fail(`tool-workflow/run-end leaves member seq ${openMembers.join(', ')} open in run ${runId}`);
|
|
104
|
-
}
|
|
105
|
-
run.ended = true;
|
|
106
|
-
run.members.clear();
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
default:
|
|
110
|
-
fail(`unknown tool-workflow event type ${event.type}`);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
/** Install an independent incremental fold over every attached Session. */
|
|
114
|
-
const install = Object.assign((ctx, fail) => {
|
|
115
|
-
const traces = new WeakMap();
|
|
116
|
-
const staged = new WeakMap();
|
|
117
|
-
const seed = (session) => {
|
|
118
|
-
const trace = new Map();
|
|
119
|
-
// oxlint-disable-next-line typescript/no-deprecated -- Existing Session history read; migration deferred.
|
|
120
|
-
for (const event of session.snapshotEvents().filter(isWorkflowRecordEvent))
|
|
121
|
-
applyEvent(trace, event, fail);
|
|
122
|
-
traces.set(session, trace);
|
|
123
|
-
return trace;
|
|
124
|
-
};
|
|
125
|
-
ctx.sessions.list().forEach(seed);
|
|
126
|
-
ctx.on('session/created', (session) => { seed(session); }, { global: true });
|
|
127
|
-
ctx.on('internal/dispatch', (_mode, eventName, args) => {
|
|
128
|
-
if (eventName !== 'session/event')
|
|
129
|
-
return;
|
|
130
|
-
const [session, event] = args;
|
|
131
|
-
if (!isWorkflowRecordEvent(event))
|
|
132
|
-
return;
|
|
133
|
-
// session/event dispatch follows list() or session/created seeding.
|
|
134
|
-
const trace = cloneTraceForEvent(traces.get(session), event, fail);
|
|
135
|
-
applyEvent(trace, event, fail);
|
|
136
|
-
staged.set(event, { session, trace });
|
|
137
|
-
}, { global: true });
|
|
138
|
-
ctx.on('session/event', (session, event) => {
|
|
139
|
-
if (!isWorkflowRecordEvent(event))
|
|
140
|
-
return;
|
|
141
|
-
const candidate = staged.get(event);
|
|
142
|
-
/* v8 ignore next 2 -- internal/dispatch stages the exact session/event callback arguments. */
|
|
143
|
-
if (candidate === undefined || candidate.session !== session) {
|
|
144
|
-
return fail('session/event reached publication without matching workflow-record validation');
|
|
145
|
-
}
|
|
146
|
-
staged.delete(event);
|
|
147
|
-
traces.set(session, candidate.trace);
|
|
148
|
-
}, { global: true });
|
|
149
|
-
}, { inject: ['sessions'] });
|
|
150
|
-
/** Register this package's invariant companion. */
|
|
151
|
-
export const apply = (ctx) => Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install));
|
|
152
|
-
//# sourceMappingURL=invariant.js.map
|