ralphflow-dsh 0.1.1 → 2.0.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.md +220 -167
- package/cordis.patch.yml +11 -0
- package/lib/create.d.ts +7 -11
- package/lib/create.js +132 -83
- package/lib/engine.d.ts +667 -262
- package/lib/engine.js +3583 -2579
- package/lib/index.d.ts +3 -4
- package/lib/index.js +222 -163
- package/lib/message-source.d.ts +25 -0
- package/lib/message-source.js +2 -0
- package/lib/reset.d.ts +40 -0
- package/lib/reset.js +147 -0
- package/lib/tools.d.ts +31 -14
- package/lib/tools.js +407 -684
- package/lib/verify.d.ts +57 -0
- package/lib/verify.js +326 -0
- package/lib/voting.d.ts +63 -0
- package/lib/voting.js +131 -0
- package/package.json +25 -44
- package/workflows/loop.yaml +21 -6
- package/workflows/spec.yaml +51 -46
- package/lib/actions.d.ts +0 -37
- package/lib/actions.js +0 -55
- package/lib/check-voting.d.ts +0 -54
- package/lib/check-voting.js +0 -289
- package/lib/check.d.ts +0 -64
- package/lib/check.js +0 -329
- package/lib/client/CommandCard.d.ts +0 -24
- package/lib/client/CommandCard.js +0 -69
- package/lib/client/HeaderAction.d.ts +0 -39
- package/lib/client/HeaderAction.js +0 -509
- package/lib/client/RunCard.d.ts +0 -27
- package/lib/client/RunCard.js +0 -375
- package/lib/client/client.d.ts +0 -3
- package/lib/client/client.js +0 -275
- package/lib/client/definition.d.ts +0 -126
- package/lib/client/definition.js +0 -259
- package/lib/client/hidden-runs.d.ts +0 -6
- package/lib/client/hidden-runs.js +0 -52
- package/lib/client/http-state.d.ts +0 -67
- package/lib/client/http-state.js +0 -81
- package/lib/client/markdown.d.ts +0 -26
- package/lib/client/markdown.js +0 -174
- package/lib/client/notify-watch.d.ts +0 -38
- package/lib/client/notify-watch.js +0 -98
- package/lib/client/notify.d.ts +0 -21
- package/lib/client/notify.js +0 -168
- package/lib/client/snapshots.d.ts +0 -8
- package/lib/client/snapshots.js +0 -101
- package/lib/client.js +0 -2361
- package/lib/commands.d.ts +0 -41
- package/lib/commands.js +0 -223
- package/lib/deps.d.ts +0 -17
- package/lib/deps.js +0 -19
- package/lib/done-detect.d.ts +0 -2
- package/lib/done-detect.js +0 -28
- package/lib/driver.d.ts +0 -32
- package/lib/driver.js +0 -248
- package/lib/events.d.ts +0 -35
- package/lib/events.js +0 -52
- package/lib/jobs.d.ts +0 -56
- package/lib/jobs.js +0 -355
- package/lib/mutex.d.ts +0 -9
- package/lib/mutex.js +0 -21
- package/lib/session-hygiene.d.ts +0 -25
- package/lib/session-hygiene.js +0 -205
- package/lib/voting-progress.d.ts +0 -37
- package/lib/voting-progress.js +0 -89
package/lib/engine.d.ts
CHANGED
|
@@ -1,306 +1,711 @@
|
|
|
1
|
+
import { MAX_VOTERS, formatVotingFailureReason, formatVotingInfraReason, formatVotingPassReason, voterProgressLine, voterStatusLabel } from "./voting.js";
|
|
2
|
+
export { MAX_VOTERS };
|
|
3
|
+
export { formatVotingFailureReason, formatVotingPassReason, formatVotingInfraReason, voterProgressLine, voterStatusLabel };
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
5
|
+
* 工作区内的运行时根目录。**工作区 dot-dir**(§1.8):与 opencode `.opencode/ralph-flow/`、
|
|
6
|
+
* claude `.claude/ralph-flow/` 形状一致;全局命名空间 `~/.dsh/ralph-flow/` 由
|
|
7
|
+
* {@link RALPH_FLOW_NAME} 拼出,两个作用域里的插件命名空间都叫 `ralph-flow`。
|
|
3
8
|
*
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
9
|
+
* 注意:这里是**工作区相对路径**(含 `/`),`path.join(workspace, RALPH_FLOW_DIR)` 仍成立。
|
|
10
|
+
*/
|
|
11
|
+
export declare const RALPH_FLOW_DIR = ".dsh/ralph-flow";
|
|
12
|
+
/** 插件命名空间名(全局 `~/.dsh/<name>/...`)——与工作区内 `.dsh/<name>/` 对称 */
|
|
13
|
+
export declare const RALPH_FLOW_NAME = "ralph-flow";
|
|
14
|
+
/**
|
|
15
|
+
* 执行日志(JSONL)轮转参数 —— 照 opencode:单文件上限 **10 MB**、保留 **3** 份
|
|
16
|
+
* (`execution.log.1` / `.2` / `.3`,最旧的删除)。见 docs/v2/execution-log-brief.md §3.3。
|
|
17
|
+
*
|
|
18
|
+
* 上限可注入(`EnginePorts.logMaxBytes` 或环境变量 `RALPHFLOW_LOG_MAX_BYTES`):
|
|
19
|
+
* 轮转只有把阈值压到 1 KB 才可能在测试里跑出来,硬编码常量等于这条验收不可测。
|
|
20
|
+
*/
|
|
21
|
+
export declare const MAX_LOG_SIZE_BYTES: number;
|
|
22
|
+
export declare const MAX_LOG_ROTATIONS = 3;
|
|
23
|
+
/**
|
|
24
|
+
* 验证模型的引用形态(与 opencode/claude 完全一致):
|
|
25
|
+
* - 字符串 `"provider/model"`(如 `deepseek/deepseek-chat`)
|
|
26
|
+
* - 对象 `{ providerID, modelID }`(两者都必须是非空字符串)
|
|
27
|
+
* **裸模型名**(如 `"sonnet"`)无法解析到 provider → 回退到默认模型(doctor 告警)。
|
|
28
|
+
*/
|
|
29
|
+
export type ModelRef = string | {
|
|
30
|
+
providerID?: string;
|
|
31
|
+
modelID?: string;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* 归一化模型引用(照抄 opencode `resolveCheckModel` 的语义,保证三端同一份资产同解):
|
|
35
|
+
* - 对象:`providerID` 与 `modelID` 都是非空字符串才有效,否则 `undefined`(回退)
|
|
36
|
+
* - 字符串:必须在第一个 `/` 处切开且 provider 非空;**裸名 → `undefined`**
|
|
37
|
+
*/
|
|
38
|
+
export declare function resolveCheckModel(model: ModelRef | undefined | null): {
|
|
39
|
+
providerID: string;
|
|
40
|
+
modelID: string;
|
|
41
|
+
} | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* 六个**必填**步骤字段(与 opencode / claude 的加载期校验**代码**逐字同严)。
|
|
7
44
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* and was actively harmful here:
|
|
12
|
-
* - No shared mutable "bound instance": every instance-scoped function takes an
|
|
13
|
-
* explicit `instId` (like the original opencode plugin passed `directory`),
|
|
14
|
-
* so there is nothing to serialize and no in-memory state lock.
|
|
15
|
-
* - No cross-process file lock: a single process needs none; operations on one
|
|
16
|
-
* instance are naturally serialized by opencode's per-session turn model.
|
|
17
|
-
* - Ownership is just `session_id` stored in the instance's state.json — no
|
|
18
|
-
* owner-session file, no pid/ppid liveness inference.
|
|
45
|
+
* 那边缺一个就 `skipStep` —— 该步被**静默丢弃**(作者的资产在别端会丢步,或整份定义因
|
|
46
|
+
* 「没有任何有效步骤」被拒收)。本仓库对「会让资产不再表示它所说的话」的配置一律**硬错误**
|
|
47
|
+
* (悬空 `on_pass`、成环、步骤级 `manual_step` 都是同一口径),所以这里不再静默丢步。
|
|
19
48
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
49
|
+
* 两个容易踩的点:
|
|
50
|
+
* · **空串算缺** —— `desc: ""` 与没写没有区别(opencode 的 `!step.desc` 同样把空串当缺);
|
|
51
|
+
* · **子工作流调用点同样必填这六个** —— 在 opencode/claude 的加载器里这六项排在 `workflow`
|
|
52
|
+
* 分支**之前**,调用点一个都少不了。
|
|
23
53
|
*/
|
|
24
|
-
export
|
|
25
|
-
|
|
26
|
-
model?: string | {
|
|
27
|
-
providerID?: string;
|
|
28
|
-
modelID?: string;
|
|
29
|
-
};
|
|
30
|
-
timeout_ms?: number;
|
|
31
|
-
system_prompt?: string;
|
|
32
|
-
}
|
|
33
|
-
export interface NormalStepDef {
|
|
54
|
+
export declare const REQUIRED_STEP_FIELDS: readonly ["desc", "input", "output", "on_pass", "on_fail", "max_fail_count"];
|
|
55
|
+
export interface StepDef {
|
|
34
56
|
id: string;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
57
|
+
/**
|
|
58
|
+
* 一句话步骤说明(会进 DO/CHECK 提示词)。**加载期必填**(`REQUIRED_STEP_FIELDS`):
|
|
59
|
+
* 缺失/非字符串/空串 = 整份定义被拒(见 `validateRequiredStepFields`)。
|
|
60
|
+
*/
|
|
61
|
+
desc?: string;
|
|
62
|
+
do?: string;
|
|
63
|
+
/**
|
|
64
|
+
* **本步生效的任务**(DO/CHECK 提示词「## 任务」段的内容)。
|
|
65
|
+
*
|
|
66
|
+
* 普通步骤不写它 → 提示词回落 `InstanceState.user_task`(发起会话的任务描述),行为与
|
|
67
|
+
* 改造前逐字一致。它只在**子工作流调用点带 `do`** 时被填上:调用点的 `do` 语义是
|
|
68
|
+
* 「这段子工作流要做什么」,**加载期静态展开**时下沉到该调用点展开出的每个子步骤
|
|
69
|
+
* (见 `prefixSubWorkflowSteps` / `loadWorkflow`)。嵌套按**最内层优先、外层继承**组合:
|
|
70
|
+
* 内层调用点写了 `do` 就覆盖外层,没写就继承外层下沉下来的值 —— 与 `adversarial_check.model`
|
|
71
|
+
* 的逐层继承形态一致。
|
|
72
|
+
*
|
|
73
|
+
* **定义期字段,不落状态**(宪法 §10.4):它是工作流定义的属性,由 `loadWorkflow` 现算,
|
|
74
|
+
* `InstanceState` 里没有对应字段;`StepDef` 本身也只活在内存里(state 只记 `workflow_name`)。
|
|
75
|
+
*
|
|
76
|
+
* **内部字段**(作者不可写:`KNOWN_STEP_KEYS` 不含它,加载解析也不从 YAML 读它)——
|
|
77
|
+
* 与 {@link StepDef.reset_from_auto} 同一口径:载体是「调用点上的 `do`」,不是步骤上的 `task`。
|
|
78
|
+
*/
|
|
79
|
+
task?: string;
|
|
80
|
+
/**
|
|
81
|
+
* **子工作流调用点**:用 `workflow:` 代替 `do:`,整步委托给另一个工作流(对齐 opencode 的
|
|
82
|
+
* 「子工作流步骤」)。本实现的落地方式是**加载期静态展开**(见 {@link MAX_EXPANDED_STEPS} 与
|
|
83
|
+
* `loadWorkflow`):调用点被就地替换成子工作流的步骤,每个子步骤 id 加 `调用点id/` 前缀,
|
|
84
|
+
* 子工作流的出口接到调用点的 `on_pass`。
|
|
85
|
+
*
|
|
86
|
+
* 这是**加载期字段**:`loadWorkflow` 返回的 `steps` 里绝不会留下它(调用点已被展开掉)。
|
|
87
|
+
*
|
|
88
|
+
* 调用点**接受并校验**的键 = `id` / `desc` / `do` / `input` / `output` / `workflow` / `on_pass` /
|
|
89
|
+
* `on_fail` / `max_fail_count` / `reset`(外加工作流级 `manual_step` 列表里的调用点 id =
|
|
90
|
+
* 整段子工作流跑完后停门)。其中六个必填字段与普通步骤**同一条硬规则** ——
|
|
91
|
+
* opencode/claude 的加载器把这六项排在 `workflow` 分支之前,调用点一个都少不了。
|
|
92
|
+
* `reset: true` 在调用点上**生效**:进入子工作流 = 首个展开后子步骤的重置(下沉时打
|
|
93
|
+
* {@link StepDef.reset_from_call} 来源标记)。调用点上的 `do` 也**生效**:下沉到子步骤的
|
|
94
|
+
* {@link StepDef.task}(这段子工作流要做什么),不写就继承父级任务。
|
|
95
|
+
* 其余键(`check`/`check_voting`/`check_model`/`inputs`/未识别键)一律**加载期告警并指路**。
|
|
96
|
+
*/
|
|
97
|
+
workflow?: string;
|
|
39
98
|
check?: string;
|
|
40
|
-
/**
|
|
41
|
-
|
|
99
|
+
/**
|
|
100
|
+
* 多验证者投票(对齐 opencode 2.8.0 的 `check_voting`):1–5 个验证者**并行**检查,
|
|
101
|
+
* 每票只查自己那一条检查依据,**全过才放行**。与 `check` **互斥**(同写 = 加载期硬错误);
|
|
102
|
+
* 两者都不写 = 本步免验证(跳过对抗性验证)。
|
|
103
|
+
*
|
|
104
|
+
* 条目只有两个字段被兑现:`check`(必填)与 `model`(可选,该票专用模型)。
|
|
105
|
+
* `timeout_ms` / `system_prompt` 与 `adversarial_check` 下同名键同一口径:
|
|
106
|
+
* 本版本从公开契约中删除/未支持,加载期告警并忽略(见 `loadWorkflow`)。
|
|
107
|
+
*/
|
|
42
108
|
check_voting?: CheckVotingEntry[];
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
109
|
+
/** 输入说明(进 CHECK 提示词)。**加载期必填**(缺失/非字符串/空串 = 整份拒收)。 */
|
|
110
|
+
input?: string;
|
|
111
|
+
/** 交付物说明(进 DO 的「交付物」与 CHECK)。**加载期必填**。 */
|
|
112
|
+
output?: string;
|
|
113
|
+
/** 通过后去哪(步骤 id 或 `done`)。**加载期必填**(不再有「缺省 = 顺序下一步」)。 */
|
|
114
|
+
on_pass?: string;
|
|
115
|
+
/** 失败重试去哪(步骤 id,不允许 `done`)。**加载期必填**(不再有「缺省 = 自身」)。 */
|
|
116
|
+
on_fail?: string;
|
|
117
|
+
/** 失败预算(≥1 的整数)。**加载期必填**(不再有「缺省 3」)。 */
|
|
118
|
+
max_fail_count?: number;
|
|
119
|
+
/**
|
|
120
|
+
* 步骤级验证模型覆盖(对齐 opencode/claude 2.8.0)。
|
|
121
|
+
* **仅单 `check` 场景生效**;与 `check_voting` 同写、或没有 `check` → 加载期硬错误。
|
|
122
|
+
*/
|
|
123
|
+
check_model?: ModelRef;
|
|
124
|
+
/**
|
|
125
|
+
* **重置门**(`reset: true`,对齐 opencode 方言):进入本步前,把属主会话可见面的
|
|
126
|
+
* `nodes[1]` 到末尾整段替换成一条 ralphflow 自己写的**交接稿**,使模型收到的
|
|
127
|
+
* messages = 系统提示 + 交接稿 + 本步 DO。
|
|
128
|
+
*
|
|
129
|
+
* 载体与硬约束见 `src/reset.ts` 与 `docs/v2/reset-feasibility.md`。要点:
|
|
130
|
+
* · 替换只能发生在**步骤边界的空闲窗口**(`agent.runMaintenance`,phase ≠ idle 即放弃);
|
|
131
|
+
* · **工作流首步无法重置**:首步 DO 是 `ralphflow_start` 工具的返回值,替换会落在工具
|
|
132
|
+
* 调用内部 → 孤儿 tool/result → 静默损坏会话。首步标了 `reset: true` 时启动回执会
|
|
133
|
+
* **如实说明**,重置从第二步起生效(本字段仍是合法键,不是告警忽略)。
|
|
134
|
+
* · 这只是「换入干净上下文」的载体;`/ralphflow-reset` 命令是**同一根接线的手动入口**
|
|
135
|
+
* (`resetCurrent`,只换上下文、不赦免失败),两者不是一回事。
|
|
136
|
+
*/
|
|
50
137
|
reset?: boolean;
|
|
138
|
+
/**
|
|
139
|
+
* **内部来源标记**(作者不可写:`KNOWN_STEP_KEYS` 不含它,加载解析也不从 YAML 读它):
|
|
140
|
+
* 本步的 `reset: true` 不是作者在这一步上手写的,而是**子工作流的 `auto_reset: true`**
|
|
141
|
+
* 在加载期静态展开时下沉而来的(见 `prefixSubWorkflowSteps`)。
|
|
142
|
+
*
|
|
143
|
+
* 为什么必须有它:展开后「子工作流」这个对象已经不存在了,`resetCauseOf` 只看得到合成出来的
|
|
144
|
+
* `reset: true`,会把 `auto_reset` 带出的重置**说成**「本步标了 `reset: true`」——正是任务书
|
|
145
|
+
* 点名禁止的错话(作者没标)。这个标记把来源保留下来,让 `resetCauseOf` 回到 `"auto"` 支。
|
|
146
|
+
*
|
|
147
|
+
* 纯措辞用途:对行为零影响(`stepWantsReset` 仍为真,替换照做)。
|
|
148
|
+
*/
|
|
149
|
+
reset_from_auto?: true;
|
|
150
|
+
/**
|
|
151
|
+
* **内部来源标记**(作者不可写,同 {@link StepDef.reset_from_auto}):本步的 `reset: true`
|
|
152
|
+
* 来自**子工作流调用点上的 `reset: true`**(加载期静态展开时下沉到**首个展开后子步骤**)。
|
|
153
|
+
*
|
|
154
|
+
* 值是**调用点的 id** —— 措辞要能说出「哪个调用点标的」(`resetSourceText` 的 `"call"` 支),
|
|
155
|
+
* 而不是含糊地说「本步标了 `reset: true`」(作者没在这一步上标)。
|
|
156
|
+
*
|
|
157
|
+
* 纯措辞用途:对行为零影响(`stepWantsReset` 仍为真,替换照做)。
|
|
158
|
+
*/
|
|
159
|
+
reset_from_call?: string;
|
|
51
160
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
max_fail_count: number;
|
|
62
|
-
reset?: boolean;
|
|
161
|
+
/** 投票条目(`check_voting[i]`):一条检查依据 +(可选)该票专用模型 */
|
|
162
|
+
export interface CheckVotingEntry {
|
|
163
|
+
/** 该验证者**专属**的检查依据(只查这一条;非空字符串,加载期硬校验) */
|
|
164
|
+
check: string;
|
|
165
|
+
/**
|
|
166
|
+
* 该票专用模型;不填 → 继承全局 `adversarial_check.model`,再往后是「发起会话当前模型」。
|
|
167
|
+
* 形态与 {@link ModelRef} 相同:`"provider/model"` 字符串或 `{providerID, modelID}` 对象。
|
|
168
|
+
*/
|
|
169
|
+
model?: ModelRef;
|
|
63
170
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
171
|
+
/**
|
|
172
|
+
* `adversarial_check` 是**可选**对象,**唯一允许的字段是 `model`**。
|
|
173
|
+
*
|
|
174
|
+
* 验证者身份与职责是 Ralphflow 的**内部定义**(`verify.ts` 的 `VERIFIER_PERSONA`),
|
|
175
|
+
* 不是工作流资产的一项配置:同一个职责不该在「Agent 名称 / 工作流提示词 / 步骤检查依据」
|
|
176
|
+
* 三处重复表达。因此 `agent` / `system_prompt` / `timeout_ms` 已从公开契约中删除——
|
|
177
|
+
* 它们出现在 YAML 里时 **加载期与 doctor 都告警并忽略**(warn+ignore,与未知键同一条口径,
|
|
178
|
+
* 见 design §8 Q13),绝不拒收、不静默、不改作别的含义。
|
|
179
|
+
*/
|
|
180
|
+
export interface AdversarialConfig {
|
|
181
|
+
/**
|
|
182
|
+
* 可选:验证模型。不写 → 验证者沿用**发起会话的当前模型**(宿主
|
|
183
|
+
* `resolveChildAgentOptions` 继承父级 provider/model);写了 → 用该模型。
|
|
184
|
+
* 两形态:`"provider/model"` 字符串或 `{providerID, modelID}` 对象。
|
|
185
|
+
*/
|
|
186
|
+
model?: ModelRef;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* 本步是否配置了**单** `check` —— **纯函数谓词,只读 `StepDef`**(design §12.1 精修)。
|
|
190
|
+
*
|
|
191
|
+
* 「本步是否需要独立验证」的判据是 {@link stepHasVerification}(`check` **或** `check_voting`):
|
|
192
|
+
* 判据来自**工作流定义**(作者所有),执行者在运行期无法影响它。因此
|
|
193
|
+
* 「无对抗性检查的步骤跳过验证」是**作者声明的机械推进**,不是执行者绕过裁判。
|
|
194
|
+
*
|
|
195
|
+
* **零新状态字段**(宪法 §10.4):某步有没有检查是工作流定义的属性、不是运行事实,
|
|
196
|
+
* 需要时现算即可 —— 绝不往 `InstanceState` 里加 `skipped_steps[]` 之类的派生量。
|
|
197
|
+
*/
|
|
198
|
+
export declare function stepHasCheck(step: Pick<StepDef, "check">): boolean;
|
|
199
|
+
/**
|
|
200
|
+
* 本步是否配置了**任何**对抗性检查:单 `check` 或多验证者投票(`check_voting`)。
|
|
201
|
+
*
|
|
202
|
+
* 这是「本步是否需要独立验证」的**唯一判据**(取代原先只看 `check` 的写法):
|
|
203
|
+
* 两者都不写 → 跳过对抗性验证(manual 步骤 = 纯人工审查)。
|
|
204
|
+
*/
|
|
205
|
+
export declare function stepHasVerification(step: Pick<StepDef, "check" | "check_voting">): boolean;
|
|
206
|
+
/** 校验通过的投票票数(0 = 不是投票步);上限 {@link MAX_VOTERS} 照抄 opencode */
|
|
207
|
+
export declare function voterCountOf(step: Pick<StepDef, "check_voting">): number;
|
|
208
|
+
/** 本步预期落地几张判定票:投票步 = 票数;单 `check` 步 = 1;免验证步 = 0 */
|
|
209
|
+
export declare function expectedVerdicts(step: Pick<StepDef, "check" | "check_voting">): number;
|
|
210
|
+
/**
|
|
211
|
+
* 重置门的**触发来源**(纯函数,只读工作流定义 + 步骤定义):
|
|
212
|
+
* · `"step"` —— 作者在**这一步**上显式写了 `reset: true`;
|
|
213
|
+
* · `"auto"` —— 工作流级 `auto_reset: true`(等价于每一步都标 reset,作者并没有标这一步);
|
|
214
|
+
* · `"call"` —— 子工作流**调用点**上写了 `reset: true`(进入子工作流 = 首个展开后子步骤的重置)。
|
|
215
|
+
*
|
|
216
|
+
* 三者对引擎的行为**完全一致**(都是「进入本步前换干净上下文」),分开只为**措辞**:
|
|
217
|
+
* 把 `auto`/`call` 说成「本步标了 `reset: true`」是**说错话**(作者没在这一步上标),
|
|
218
|
+
* 启动回执 / 播报 / 推进回执一律按来源分述(见各处 `resetSourceText` / `describeResetCause`)。
|
|
219
|
+
*
|
|
220
|
+
* 同时标了步骤级 `reset: true` 与工作流级 `auto_reset: true` 时按 `"step"` 计:作者确实
|
|
221
|
+
* 在这一步上标了,那么这句话本身是真的。
|
|
222
|
+
*
|
|
223
|
+
* **子工作流**的 `auto_reset` 在加载期静态展开时下沉为子步骤的 `reset: true`(见
|
|
224
|
+
* `prefixSubWorkflowSteps`),但会同时打上 {@link StepDef.reset_from_auto} 标记 ——
|
|
225
|
+
* 于是这里的来源仍是 `"auto"`,不会被误判成作者标了这一步。调用点上的 `reset: true`
|
|
226
|
+
* 同理下沉到**首个展开后子步骤**并打 {@link StepDef.reset_from_call} 标记(记着调用点 id)。
|
|
227
|
+
*/
|
|
228
|
+
export type ResetCause = "step" | "auto" | "call";
|
|
229
|
+
/** 本步进入前是否要重置上下文,以及**为什么**(`undefined` = 不重置)。 */
|
|
230
|
+
export declare function resetCauseOf(wf: Pick<WorkflowDef, "auto_reset">, step: Pick<StepDef, "reset" | "reset_from_auto" | "reset_from_call">): ResetCause | undefined;
|
|
231
|
+
/**
|
|
232
|
+
* 本步进入前是否要重置上下文(步骤级 `reset` ∨ 工作流级 `auto_reset`)。
|
|
233
|
+
*
|
|
234
|
+
* **唯一判据**:所有投递点(advance / 返工 / 手动 `/ralphflow-reset`)都走这一个谓词,
|
|
235
|
+
* 不再有「这里看 reset、那里看 auto_reset」的分叉 —— 那正是两种豁免规则走样的来源。
|
|
236
|
+
*/
|
|
237
|
+
export declare function stepWantsReset(wf: Pick<WorkflowDef, "auto_reset">, step: Pick<StepDef, "reset">): boolean;
|
|
238
|
+
/**
|
|
239
|
+
* 本步**生效的任务**(DO 与 CHECK 提示词「## 任务」段的内容)—— 纯函数,只读 `StepDef` 与任务描述。
|
|
240
|
+
*
|
|
241
|
+
* 判据:{@link StepDef.task}(子工作流调用点的 `do` 在加载期下沉而来)优先;
|
|
242
|
+
* 没有(普通步骤、或调用点没写 `do`)就回落 `InstanceState.user_task`(发起会话的任务描述)。
|
|
243
|
+
*
|
|
244
|
+
* 为什么 DO 与 CHECK 必须用**同一个**函数:两边提示词的「## 任务」在改造前是同一句
|
|
245
|
+
* `user_task`,验证者据此核对产出。若只给 DO 换上调用点的 `do`、CHECK 仍拿父级任务,执行者
|
|
246
|
+
* 与验证者就会对着**两个不同的任务**干活 —— 那正是「资产不再表示它所说的话」。所以下沉出来的
|
|
247
|
+
* `task` 在**两个消费点**上一起生效,而不是只改 DO。
|
|
248
|
+
*
|
|
249
|
+
* **零新状态字段**(宪法 §10.4):`task` 是工作流定义的属性,现算即可,不落 `InstanceState`。
|
|
250
|
+
*/
|
|
251
|
+
export declare function effectiveTaskOf(step: Pick<StepDef, "task">, userTask: string): string;
|
|
252
|
+
/**
|
|
253
|
+
* 子工作流展开后的步骤 id 分隔符(`<调用点 id>/<子步骤 id>`,多层继续叠加)。
|
|
254
|
+
*
|
|
255
|
+
* 之所以是硬约束:调用点 id 与子工作流里的步骤 id **都不允许含**这个字符 —— 否则
|
|
256
|
+
* `a` + `b/c` 与 `a/b` + `c` 会展开出同一个 id,展开就不再是一一对应。两处都由
|
|
257
|
+
* `loadWorkflow` 在**加载期硬错误**(绝不等到运行期才炸)。
|
|
258
|
+
*/
|
|
259
|
+
export declare const SUBWORKFLOW_ID_SEP = "/";
|
|
260
|
+
/**
|
|
261
|
+
* 这个 id 是**子工作流调用点**(而不是展开后的步骤)吗?
|
|
262
|
+
*
|
|
263
|
+
* 加载期静态展开把调用点**就地替换**成 `调用点id/子步骤id`,调用点自身不再是步骤;而步骤 id
|
|
264
|
+
* 含 {@link SUBWORKFLOW_ID_SEP} 是加载期硬错误。所以「某个步骤 id 以 `id/` 开头」就精确说明
|
|
265
|
+
* `id` 是调用点 —— 据此给 `/ralphflow-rewind` 一句**准确**的拒绝理由(说得出它被展开成了什么),
|
|
266
|
+
* 而不是笼统地说「没有这个步骤」。
|
|
267
|
+
*/
|
|
268
|
+
export declare function isSubWorkflowCallId(wf: Pick<WorkflowDef, "steps">, id: string): boolean;
|
|
269
|
+
/**
|
|
270
|
+
* 展开后步骤总数上限(**加载期**计数,超了立刻中止展开)。
|
|
271
|
+
*
|
|
272
|
+
* ralphflow 面向长程工作流:2000 是「够长程」与「一次误配不会把引擎拖死」之间的取舍。
|
|
273
|
+
* 计数贯穿整条调用链(子工作流被多个调用点引用时**每个调用点各算一次**,因为它会被
|
|
274
|
+
* 就地展开成多份步骤)。
|
|
275
|
+
*
|
|
276
|
+
* 它**只管步数、不管深度**:一条每层只有调用点(自身零步骤)的链,步数可以是 1 而层数上千
|
|
277
|
+
* —— 那种情况由 {@link MAX_SUBWORKFLOW_DEPTH} 拦(展开器是递归的,步数上限兜不住调用深度)。
|
|
278
|
+
*/
|
|
279
|
+
export declare const MAX_EXPANDED_STEPS = 2000;
|
|
280
|
+
/**
|
|
281
|
+
* 子工作流**嵌套深度**上限(调用链层数,含最外层工作流;**加载期硬错误**)。
|
|
282
|
+
*
|
|
283
|
+
* 为什么必须有它:展开器是**递归**的(每层 `loadWorkflow` 一帧),而 {@link MAX_EXPANDED_STEPS}
|
|
284
|
+
* 只数步骤 —— 实测「1900 个互相串联的工作流文件、每个自身零步骤」时步数只有 1,步数上限
|
|
285
|
+
* 完全不触发,递归却深到把宿主 JS 调用栈打爆(`RangeError: Maximum call stack size exceeded`
|
|
286
|
+
* 冒泡出 `loadWorkflow`,调用方拿到的是崩溃而不是硬错误)。本上限在**展开任何一层之前**就
|
|
287
|
+
* 拒绝过深的链,保证加载器只会返回「说人话的硬错误」。
|
|
288
|
+
*
|
|
289
|
+
* 取值 32:opencode 的运行时深度上限是 5 层,这里比它宽松得多;而真实复用链(2–3 层)
|
|
290
|
+
* 离 32 极远,所以既不会误伤正常组合,又远低于任何宿主栈上限。
|
|
291
|
+
*/
|
|
292
|
+
export declare const MAX_SUBWORKFLOW_DEPTH = 32;
|
|
293
|
+
/**
|
|
294
|
+
* 子工作流**加载期静态展开**的递归上下文(相对 opencode 的运行时状态栈方案,本实现把
|
|
295
|
+
* 嵌套彻底消化在 `loadWorkflow` 里:展开后的 `steps` 就是实例看到的全部步骤,
|
|
296
|
+
* `current_step` 仍是单字符串 —— **零新增 InstanceState 字段**)。
|
|
297
|
+
*/
|
|
298
|
+
export interface SubWorkflowLoadCtx {
|
|
299
|
+
/** 从最外层到**当前**工作流(含)的名字链:成环检测 + 报错里说清调用路径 */
|
|
300
|
+
chain: string[];
|
|
301
|
+
/** 调用链上生效的验证模型(`adversarial_check.model` 逐字段继承的父级值) */
|
|
302
|
+
inheritedModel?: ModelRef;
|
|
303
|
+
/**
|
|
304
|
+
* 调用链上生效的**任务**(调用点 `do` 逐层继承的父级值):外层调用点写了 `do`,内层调用点
|
|
305
|
+
* 没写时,子步骤的 {@link StepDef.task} 就继承它。形态与 {@link SubWorkflowLoadCtx.inheritedModel}
|
|
306
|
+
* 一致 —— **最内层优先、外层继承**。
|
|
307
|
+
*/
|
|
308
|
+
inheritedTask?: string;
|
|
309
|
+
/** 展开计数器(整条链共享一个对象,任一层的超限都会被立刻读到) */
|
|
310
|
+
counter: {
|
|
311
|
+
count: number;
|
|
69
312
|
};
|
|
70
|
-
agent?: string;
|
|
71
|
-
system_prompt?: string;
|
|
72
|
-
timeout_ms?: number;
|
|
73
313
|
}
|
|
74
314
|
export interface WorkflowDef {
|
|
75
315
|
name: string;
|
|
76
|
-
description
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
316
|
+
description?: string;
|
|
317
|
+
/**
|
|
318
|
+
* 审查门步骤(方言形态:**顶层** id 列表,与 `steps` 同级)。
|
|
319
|
+
*
|
|
320
|
+
* 这是审查门的**唯一**写法:步骤级 `manual_step` 键已从方言中删除,
|
|
321
|
+
* 出现即加载期硬错误(`loadWorkflow`;opencode 只认列表,步骤级写法在那边
|
|
322
|
+
* 会被当作「不认识的步骤键」忽略 → 人工审查门静默消失)。
|
|
323
|
+
*/
|
|
324
|
+
manual_step?: string[];
|
|
325
|
+
adversarial_check?: AdversarialConfig;
|
|
326
|
+
/**
|
|
327
|
+
* 工作流级重置门(`auto_reset: true`,对齐 opencode 方言):等价于给**所有**步骤标
|
|
328
|
+
* `reset: true` —— 任何方式进入都触发(含 on_fail 回本步的失败重试),不设第二种豁免。
|
|
329
|
+
*
|
|
330
|
+
* 缺省 false。与步骤级 `reset` 的语义**完全一致**,区别只在**措辞来源**:
|
|
331
|
+
* `auto_reset` 带出的重置绝不能被说成「本步标了 `reset: true`」(作者没标)。
|
|
332
|
+
* 靠 {@link resetCauseOf} 区分来源,凡是要对人说的话都按来源分两支。
|
|
333
|
+
*
|
|
334
|
+
* 子工作流的 `auto_reset` 在**加载期静态展开**时会下沉成子步骤的 `reset: true`
|
|
335
|
+
* (展开后只剩步骤,没有「子工作流」这个对象了;见 `prefixSubWorkflowSteps`),
|
|
336
|
+
* 并同时打上来源标记 `reset_from_auto`,以免被措辞误判成作者标了那一步的 `reset`。
|
|
337
|
+
*/
|
|
80
338
|
auto_reset?: boolean;
|
|
339
|
+
steps: StepDef[];
|
|
340
|
+
warnings: string[];
|
|
341
|
+
}
|
|
342
|
+
export type VerdictStatus = "passed" | "failed" | "infra";
|
|
343
|
+
export interface Verdict {
|
|
344
|
+
check_index: number;
|
|
345
|
+
status: VerdictStatus;
|
|
346
|
+
reason: string;
|
|
347
|
+
agent_id?: string;
|
|
348
|
+
step_id: string;
|
|
349
|
+
ts: string;
|
|
350
|
+
}
|
|
351
|
+
export interface Delegation {
|
|
352
|
+
run_id: string;
|
|
353
|
+
agent_id?: string;
|
|
354
|
+
check_index: number;
|
|
355
|
+
/**
|
|
356
|
+
* 本笔委派属于第几轮投票(`1` = 首轮;`2` = 某票 infra 后的**自动重试轮**)。
|
|
357
|
+
*
|
|
358
|
+
* 这是**原始事实**(这一笔委派到底是第几次跑),不是派生量:聚合时要靠它区分
|
|
359
|
+
* 「首轮 infra → 自动重试一次」与「重试仍 infra → 暂停」(照抄 opencode 的重试预算,
|
|
360
|
+
* 见 `check-voting.ts` §4.4)。单 `check` 步骤不写该字段(省略 = 首轮)。
|
|
361
|
+
*/
|
|
362
|
+
attempt?: number;
|
|
363
|
+
ts: string;
|
|
364
|
+
/**
|
|
365
|
+
* 属主运行时 id(`<pid>-<装载时刻>-<rand>`):**只作诊断**,不参与判活。
|
|
366
|
+
*
|
|
367
|
+
* 为什么不用 pid 判活(第一版就是这么写的,被实测打回):本部署里各 agent 进程跑在
|
|
368
|
+
* 各自的 sandbox 里,有**独立的 PID namespace 与 /proc 视图** —— 实测宿主进程
|
|
369
|
+
* `pid 919449` 明明活着,另一个进程(验证者的沙箱)`kill(919449, 0)` 拿到 `ESRCH`、
|
|
370
|
+
* `/proc/uptime` 也对不上,于是把活着的属主判成死的,照样把在飞验证清掉。
|
|
371
|
+
* 跨沙箱唯一可靠共享的是**文件系统**,所以判活改用心跳(见下)。
|
|
372
|
+
*/
|
|
373
|
+
owner_runtime?: string;
|
|
374
|
+
/**
|
|
375
|
+
* 属主心跳时刻(epoch ms)—— **判孤儿的唯一依据**:属主在等判定期间每
|
|
376
|
+
* {@link DELEGATION_HEARTBEAT_REFRESH_MS} 刷一次;超过 {@link DELEGATION_HEARTBEAT_TTL_MS}
|
|
377
|
+
* 没刷 = 属主已经不在了(进程崩溃/被重启),判定永远回不来。
|
|
378
|
+
*
|
|
379
|
+
* 缺这个字段 = 来源不可考的老 `state.json`(或旧版插件落的账)→ 按孤儿兜底。
|
|
380
|
+
*/
|
|
381
|
+
heartbeat_at?: number;
|
|
81
382
|
}
|
|
82
|
-
|
|
383
|
+
/** 在飞委派的心跳周期:属主每隔这么久把 `heartbeat_at` 刷新一次 */
|
|
384
|
+
export declare const DELEGATION_HEARTBEAT_REFRESH_MS = 5000;
|
|
385
|
+
/**
|
|
386
|
+
* 心跳多久没刷就算属主失联(= 真孤儿)。
|
|
387
|
+
*
|
|
388
|
+
* 取 12 倍刷新周期:既容忍属主事件循环偶发卡顿(一次两次没刷上不算死),又让崩溃留下的
|
|
389
|
+
* 孤儿在一个 TTL 之内被兜住 —— 崩溃恢复没有缩水,只是判据从「pid 还在吗」换成「心跳还在吗」。
|
|
390
|
+
*/
|
|
391
|
+
export declare const DELEGATION_HEARTBEAT_TTL_MS = 60000;
|
|
392
|
+
export declare function runtimeId(): string;
|
|
393
|
+
/** 一笔委派是否仍有存活属主 —— `restore()` 的唯一判据(导出供测试直接取证)。 */
|
|
394
|
+
export declare function delegationOwnerAlive(d: Delegation): boolean;
|
|
395
|
+
export interface HistoryEntry {
|
|
396
|
+
ts: string;
|
|
397
|
+
step?: string;
|
|
398
|
+
event: string;
|
|
399
|
+
detail?: string;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* §1.4 每步耗时与重试次数:**全部从 `history` 的 `ts` 与 `fail_counts` 派生**
|
|
403
|
+
* (不新增落盘字段)。纯函数,便于用合成 history 直接验证。
|
|
404
|
+
*
|
|
405
|
+
* 耗时区间 = 属于某步的**第一条**历史事件 → 下一条属于其它步骤的事件(或 `endTs`);
|
|
406
|
+
* 同一被反复进入的步骤(返工/回退)累计总时长。
|
|
407
|
+
*
|
|
408
|
+
* ⚠️ 这里曾经有一个只在「步骤切换」时才结算的实现:最后一步的耗时被算成
|
|
409
|
+
* `endTs - 该步最后一条事件`,而最后一条事件(`complete`)与 `endTs` 只差几毫秒
|
|
410
|
+
* —— 于是**每一步都显示 `耗时 0s`**(实测:6m22s 的一轮报告成 0s,历史上每一份
|
|
411
|
+
* 归档报告都是 0s)。区间起点必须是该步的**首条**事件,不是末条。
|
|
412
|
+
*
|
|
413
|
+
* 重试次数 = `max(fail_counts[step], 该步 verdict_failed 条数)`:
|
|
414
|
+
* 通过时 `clearFailCount` 会把该步计数清零、恢复暂停时也会清零,所以单看
|
|
415
|
+
* `fail_counts` 会把「先失败几次再通过」记成 0 轮 —— 必须同时从 history 兜底。
|
|
416
|
+
*/
|
|
417
|
+
export declare function stepStats(history: HistoryEntry[], failCounts: Record<string, number> | undefined, endTs: number): Array<{
|
|
418
|
+
step: string;
|
|
419
|
+
ms: number;
|
|
420
|
+
retries: number;
|
|
421
|
+
}>;
|
|
422
|
+
export interface InstanceState {
|
|
83
423
|
active: boolean;
|
|
84
424
|
workflow_name: string;
|
|
85
425
|
current_step: string;
|
|
86
|
-
current_phase: string;
|
|
87
|
-
fail_count: number;
|
|
88
426
|
user_task: string;
|
|
427
|
+
/**
|
|
428
|
+
* **每步**失败轮数(原始事实,落盘)。
|
|
429
|
+
* 为什么必须按步记:`max_fail_count` 是**步骤级**属性,而 `on_fail` 可回退到更早步骤
|
|
430
|
+
* (内置 spec.yaml 的 verify→implement 就是)。单一标量会让被回退到的步骤继承前一步的
|
|
431
|
+
* 失败数 → 它自己第一次失败就触发上限(提前暂停);反过来若回退时清零,成环的 on_fail
|
|
432
|
+
* 又永远累积不到上限 → 无限 ping-pong。按步记同时解决两者。
|
|
433
|
+
*/
|
|
434
|
+
fail_counts: Record<string, number>;
|
|
435
|
+
/**
|
|
436
|
+
* 当前步的失败轮数。**派生量,不落盘**(宪法 §10.4):
|
|
437
|
+
* 由 `readState` 从 `fail_counts[current_step]` 算出,仅作读取便利;`writeState` 会剔除它。
|
|
438
|
+
*/
|
|
439
|
+
fail_count: number;
|
|
89
440
|
paused: boolean;
|
|
90
|
-
pause_reason?:
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
export interface StepExecutionRecord {
|
|
97
|
-
stepId: string;
|
|
98
|
-
phase: string;
|
|
99
|
-
status: "passed" | "failed";
|
|
100
|
-
failCount: number;
|
|
101
|
-
startTime: string;
|
|
102
|
-
endTime?: string;
|
|
103
|
-
reason?: string;
|
|
441
|
+
pause_reason?: "max_failures" | "check_infra" | "user_cancelled" | "no_submit";
|
|
442
|
+
/** 本轮 DO 是否已交卷(引擎观测到的事实) */
|
|
443
|
+
do_submitted: boolean;
|
|
444
|
+
owner_session?: string;
|
|
445
|
+
/** 交卷时的助手回复摘要(验证者 prompt 用) */
|
|
446
|
+
last_submit_summary?: string;
|
|
104
447
|
/**
|
|
105
|
-
*
|
|
106
|
-
*
|
|
448
|
+
* 本实例产出目录的**目录名**(`artifacts/<artifacts_dir_name>/`)。
|
|
449
|
+
*
|
|
450
|
+
* **不是派生量**(宪法 §10.4 的例外有据):将来子工作流会改写 `user_task`,
|
|
451
|
+
* 名字一旦落盘就无法事后重算;且它必须与实例目录同生共死地隔离。
|
|
452
|
+
* 启动时由 {@link makeArtifactsDirName} 固定。
|
|
453
|
+
*
|
|
454
|
+
* 缺该字段(老 `state.json`)→ 读取时回退 `instId`(向后兼容,老产出目录名不变)。
|
|
107
455
|
*/
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
456
|
+
artifacts_dir_name?: string;
|
|
457
|
+
delegations: Delegation[];
|
|
458
|
+
verdicts: Verdict[];
|
|
459
|
+
history: HistoryEntry[];
|
|
460
|
+
started_at: string;
|
|
461
|
+
updated_at: string;
|
|
114
462
|
}
|
|
115
463
|
export interface InstanceInfo {
|
|
116
464
|
id: string;
|
|
117
|
-
state:
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
465
|
+
state: InstanceState;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* 一条**已归档运行**的历史条目(从 `reports/*.md` 的头部字段解析而来)。
|
|
469
|
+
*
|
|
470
|
+
* 解析失败的报告**也列出来**(`parsed: false`、`statusLabel: "无法解析"`),
|
|
471
|
+
* 绝不静默丢弃——用户至少能看到「这里有个报告,我没读懂」。
|
|
472
|
+
*/
|
|
473
|
+
export interface HistoryInfo {
|
|
474
|
+
id: string;
|
|
475
|
+
parsed: boolean;
|
|
476
|
+
statusLabel: string;
|
|
477
|
+
task?: string;
|
|
478
|
+
startedAt?: string;
|
|
479
|
+
endedAt?: string;
|
|
480
|
+
/** 工作区相对报告路径(正斜杠) */
|
|
481
|
+
relPath: string;
|
|
482
|
+
/** 报告里登记的产出目录名(用于「孤儿产出」诊断;老报告可能没有) */
|
|
483
|
+
artifactsDirName?: string;
|
|
484
|
+
/** 报告标题里的工作流名(可选,仅用于展示) */
|
|
485
|
+
workflow?: string;
|
|
122
486
|
}
|
|
123
|
-
export interface
|
|
487
|
+
export interface WorkflowEntry {
|
|
488
|
+
name: string;
|
|
489
|
+
desc: string;
|
|
490
|
+
invalid: boolean;
|
|
491
|
+
problems: string[];
|
|
492
|
+
warnings: string[];
|
|
493
|
+
}
|
|
494
|
+
export interface ToolResult {
|
|
495
|
+
ok: boolean;
|
|
124
496
|
text: string;
|
|
125
|
-
|
|
126
|
-
|
|
497
|
+
}
|
|
498
|
+
/** reset 门的投递物:交接稿(模型可见)+ 给用户看的可见告知(随后被遮蔽,只留给 Chat 视图)。 */
|
|
499
|
+
export interface ResetRequest {
|
|
500
|
+
/** 交接稿正文:替换后**系统提示之外唯一**的模型可见内容。 */
|
|
501
|
+
handoff: string;
|
|
502
|
+
/** 可见告知:append 来源(Chat 显示),随后被同一次替换遮蔽(模型看不到)。 */
|
|
503
|
+
notice: {
|
|
504
|
+
summary: string;
|
|
505
|
+
text: string;
|
|
506
|
+
};
|
|
127
507
|
/**
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
508
|
+
* 动手前的**最后一道复查**(在 maintenance job 内、紧邻 append 之前同步调用):
|
|
509
|
+
* 实例在「等空闲窗口」这段时间里可能已被取消/收摊,那就**不做替换**。
|
|
510
|
+
*
|
|
511
|
+
* 必须是「重新读盘」的实现,不能闭包一个内存快照 —— 取消/完成走的是另一条 `readState`
|
|
512
|
+
* (每次 JSON.parse 新对象)并会销毁实例目录,快照永远看不到(实测踩过:护栏成了空操作)。
|
|
133
513
|
*/
|
|
134
|
-
|
|
514
|
+
canProceed?: () => boolean;
|
|
135
515
|
}
|
|
136
516
|
/**
|
|
137
|
-
*
|
|
517
|
+
* reset 门的执行结果。**失败绝不抛**:引擎据此如实播报并**照常投递 DO**——
|
|
518
|
+
* 「没换成」不能演变成「本步无法执行」。
|
|
138
519
|
*/
|
|
139
|
-
export interface
|
|
140
|
-
|
|
141
|
-
|
|
520
|
+
export interface ResetOutcome {
|
|
521
|
+
ok: boolean;
|
|
522
|
+
/** 失败原因(ok=false 时给出;引擎翻成人话写进播报,绝不静默) */
|
|
523
|
+
reason?: "no_session" | "no_maintenance" | "surface_too_short" | "unbalanced" | "balance_error" | "aborted" | "not_idle" | "instance_gone" | "maintenance_failed";
|
|
524
|
+
/** 诊断补充(宿主异常原文) */
|
|
525
|
+
detail?: string;
|
|
526
|
+
/** 被遮蔽的节点数(ok=true 时给出) */
|
|
527
|
+
shadowed?: number;
|
|
528
|
+
/** 交接稿节点 seq(ok=true 时给出) */
|
|
529
|
+
handoffSeq?: number;
|
|
530
|
+
/** 可见告知节点 seq(ok=true 时给出) */
|
|
531
|
+
noticeSeq?: number;
|
|
142
532
|
}
|
|
143
|
-
/**
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
533
|
+
/** 引擎对外的两个端口:向属主会话投递指令 / 委派独立验证者(T1 的唯一入口) */
|
|
534
|
+
export interface EnginePorts {
|
|
535
|
+
/**
|
|
536
|
+
* 把指令投递给主会话(插件消息 + 唤醒)。
|
|
537
|
+
* `summary` 是给**用户看**的一行摘要:dsh 客户端按 `source.form === "notice"` + `summary`
|
|
538
|
+
* 渲染成不展开就能读的 notice 行;省略则退化为不显眼的 opaque 注入行(用户看不到)。
|
|
539
|
+
* 凡是用户应当知道的播报都必须传 summary。
|
|
540
|
+
*
|
|
541
|
+
* `opts.dedupe === false` 表示**引擎自驱的投递**(DO / 推进 / 重置告知等)不去重:
|
|
542
|
+
* 去重本是给「用户连按命令」的护栏,用在状态迁移上会把真实迁移吞掉(实测:手动 reset
|
|
543
|
+
* 重投的 DO 与几秒前同一步的 DO 逐字相同 → 被去重 → 模型拿不到任何指令)。
|
|
544
|
+
* 引擎的一律传 `{dedupe:false}`。
|
|
545
|
+
*/
|
|
546
|
+
deliver: (sessionId: string, text: string, summary?: string, opts?: {
|
|
547
|
+
dedupe?: boolean;
|
|
548
|
+
}) => boolean;
|
|
549
|
+
/**
|
|
550
|
+
* 重置门(步骤级 `reset: true` / 工作流级 `auto_reset: true` / 手动 `/ralphflow-reset`)的载体:
|
|
551
|
+
* 把属主会话可见面 `nodes[1]` 到末尾整段替换成 `req.handoff`。**必须**在步骤边界的空闲窗口内
|
|
552
|
+
* 完成(宿主侧用 `agent.runMaintenance` 钉死),失败就如实返回 `ok:false`(引擎照常投递 DO)。
|
|
553
|
+
*
|
|
554
|
+
* 缺省(未装配)时要重置的步骤**不重置**,但启动/推进回执会如实标注——绝不假装做过。
|
|
555
|
+
*/
|
|
556
|
+
resetSurface?: (sessionId: string, req: ResetRequest) => Promise<ResetOutcome>;
|
|
557
|
+
/** 委派独立验证者,返回判定(绝不接受主会话提供的判定) */
|
|
558
|
+
verify: (req: VerifyRequest) => Promise<Verdict>;
|
|
559
|
+
log?: (level: "info" | "warn" | "error", event: string, data?: unknown) => void;
|
|
560
|
+
/**
|
|
561
|
+
* 执行日志(JSONL)单文件上限(字节)。**测试注入口**(轮转只有注入小阈值才可验证);
|
|
562
|
+
* 缺省 10 MB,也可用环境变量 `RALPHFLOW_LOG_MAX_BYTES`(正整数)覆盖。
|
|
563
|
+
* 它不是状态、不落盘到 `state.json`——只影响 append 前的轮转判据。
|
|
564
|
+
*/
|
|
565
|
+
logMaxBytes?: number;
|
|
165
566
|
}
|
|
166
|
-
export
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
readExtraDirs: (instId: string) => string[];
|
|
185
|
-
readOwnerSession: (instId: string) => string | null;
|
|
186
|
-
claimOwnership: (instId: string, sessionId: string | null) => void;
|
|
187
|
-
listInstances: () => InstanceInfo[];
|
|
188
|
-
resolveInstance: (explicitId: string | null | undefined, sessionId: string | null) => {
|
|
189
|
-
ok: true;
|
|
190
|
-
id: string;
|
|
191
|
-
attached: boolean;
|
|
192
|
-
} | {
|
|
193
|
-
ok: false;
|
|
194
|
-
text: string;
|
|
567
|
+
export interface VerifyRequest {
|
|
568
|
+
instId: string;
|
|
569
|
+
step: StepDef;
|
|
570
|
+
workflow: WorkflowDef;
|
|
571
|
+
userTask: string;
|
|
572
|
+
ownerSession?: string;
|
|
573
|
+
checkIndex: number;
|
|
574
|
+
/**
|
|
575
|
+
* 多验证者投票(`check_voting`):本票的**专属检查依据**与序号。
|
|
576
|
+
*
|
|
577
|
+
* 有它 ⇒ 验证者提示词走**投票变体**(共享上下文 + 该票依据 + 「你是 N 个之一,只查自己
|
|
578
|
+
* 这一条」约束,照抄 opencode `buildVotingCheckPrompt`);没有它 ⇒ 单 `check` 步骤,
|
|
579
|
+
* 检查依据取 `step.check`。**执行的判定只由 ports.verify 的返回决定**(T1),这里只传事实。
|
|
580
|
+
*/
|
|
581
|
+
voter?: {
|
|
582
|
+
index: number;
|
|
583
|
+
count: number;
|
|
584
|
+
check: string;
|
|
195
585
|
};
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
readReinjectCount: (instId: string, stepId?: string, phase?: string) => number;
|
|
212
|
-
clearDoPromptCache: (instId: string) => void;
|
|
213
|
-
clearDoneTagDetected: (instId: string) => void;
|
|
214
|
-
writeDoPromptCache: (prompt: string, instId: string) => void;
|
|
215
|
-
readDoPromptCache: (instId: string) => string;
|
|
216
|
-
buildDoNudge: (instId: string, stepId: string) => string;
|
|
217
|
-
markPromptDelivered: (stepId: string, instId: string) => void;
|
|
218
|
-
writeAdversarialSession: (checkSessionId: string, instId: string) => void;
|
|
219
|
-
clearAdversarialSession: (instId: string) => void;
|
|
220
|
-
readAdversarialSession: (instId: string) => string | null;
|
|
221
|
-
readAdversarialSessions: (instId: string) => string[];
|
|
222
|
-
removeAdversarialSession: (checkSessionId: string, instId: string) => void;
|
|
223
|
-
pruneStaleAdversarialSessions: (instId: string, graceMs?: number) => number;
|
|
224
|
-
parseWorkflowFile: (filePath: string, workflowName: string, problems?: string[]) => WorkflowDef | null;
|
|
225
|
-
loadWorkflow: (workflowName: string, problems?: string[]) => WorkflowDef | null;
|
|
226
|
-
listWorkflows: () => Array<{
|
|
227
|
-
name: string;
|
|
228
|
-
desc: string;
|
|
229
|
-
invalid?: boolean;
|
|
230
|
-
}>;
|
|
231
|
-
lintWorkflow: (wf: WorkflowDef, rawParsed: any, fromBuiltin?: boolean) => string[];
|
|
232
|
-
buildDoctorReport: () => string;
|
|
233
|
-
getStep: (workflow: WorkflowDef, stepId: string) => StepDef | null;
|
|
234
|
-
buildDoPrompt: (instId: string, step: NormalStepDef, userTask?: string, retryContext?: string, retryCount?: number) => string;
|
|
235
|
-
buildCheckPrompt: (instId: string, step: NormalStepDef, userTask?: string) => string;
|
|
236
|
-
buildVotingCheckPrompt: (instId: string, step: NormalStepDef, userTask: string | undefined, entry: CheckVotingEntry, index: number, count: number) => string;
|
|
237
|
-
buildSubWorkflowUserTask: (instId: string, step: SubWorkflowStepDef, parentUserTask: string) => string;
|
|
238
|
-
resolveSubWorkflowEntry: (instId: string, subWorkflowName: string, parentUserTask: string, parentStep: SubWorkflowStepDef, maxDepth?: number, retryContext?: string, retryCount?: number) => {
|
|
239
|
-
text: string;
|
|
240
|
-
error?: boolean;
|
|
586
|
+
/**
|
|
587
|
+
* 本实例产出目录的**工作区相对路径**(§1.7,正斜杠)。
|
|
588
|
+
* 验证者继承父会话工作区,因此用它就能读到 DO 的产出;CHECK 提示词据此注入「产出目录」。
|
|
589
|
+
*/
|
|
590
|
+
artifactsRelDir: string;
|
|
591
|
+
/**
|
|
592
|
+
* 已归一化的验证模型(优先级:步骤 `check_model` > 全局 `adversarial_check.model`)。
|
|
593
|
+
* 归一化在**引擎**里做(`resolveCheckModel`),验证者只消费结果——与 opencode 的
|
|
594
|
+
* `resolveVerifierModel` 同构,保证三端同一份资产解析出同一个模型。
|
|
595
|
+
* `undefined` = **不传 `agentOptions`**,由宿主 `resolveChildAgentOptions` 让子代理
|
|
596
|
+
* 继承**父级** provider/model——即发起会话的当前模型(不是 provider/部署默认)。
|
|
597
|
+
*/
|
|
598
|
+
model?: {
|
|
599
|
+
providerID: string;
|
|
600
|
+
modelID: string;
|
|
241
601
|
};
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
archiveReport: (instId: string, workflowName: string, status: string, records: StepExecutionRecord[]) => string | null;
|
|
263
|
-
getReportPath: (instId: string) => string;
|
|
264
|
-
reportExists: (instId: string) => boolean;
|
|
265
|
-
matchCheckTag: (responseText: string) => RegExpMatchArray | null;
|
|
266
|
-
parseCheckResult: (responseText: string) => boolean;
|
|
267
|
-
getAdversarialCheckReason: (responseText: string) => string;
|
|
268
|
-
handleCheckPassed: (instId: string, state: RalphFlowState, workflow: WorkflowDef, step: StepDef, checkResult: {
|
|
269
|
-
reason?: string;
|
|
270
|
-
}, skipped?: boolean) => TransitionResult;
|
|
271
|
-
handleCheckFailed: (instId: string, state: RalphFlowState, workflow: WorkflowDef, step: StepDef, checkResult: {
|
|
272
|
-
reason?: string;
|
|
273
|
-
}) => TransitionResult;
|
|
274
|
-
shouldResetOnTransition: typeof shouldResetOnTransition;
|
|
275
|
-
migrateLegacyInstance: () => void;
|
|
276
|
-
ensureProjectWorkflows: () => void;
|
|
277
|
-
};
|
|
278
|
-
export declare function isSubWorkflowStep(step: StepDef): step is SubWorkflowStepDef;
|
|
602
|
+
/**
|
|
603
|
+
* 取消句柄(dsh 委派契约要求的 "caller's cancellation";不是超时预算)
|
|
604
|
+
*
|
|
605
|
+
* 注意这里**没有** `submitSummary`:执行者的交卷自述**不进验证者视野**(T1)。
|
|
606
|
+
* opencode 与 claude 版都从不把自述传给验证者,并在提示词里明令"不要依赖任何外部
|
|
607
|
+
* 提供的实现总结"。验证者的职责是**只看结果是否满足检查依据,不管执行者怎么做的**。
|
|
608
|
+
* 交卷自述仍留在 `state.last_submit_summary`,但只服务于审查门改稿重交去重
|
|
609
|
+
* (见 `onSubmit`),不再流向验证者。
|
|
610
|
+
*/
|
|
611
|
+
signal: AbortSignal;
|
|
612
|
+
/**
|
|
613
|
+
* 执行日志(JSONL)写入端口 —— **验证者取证证据的入口**(§3.2):
|
|
614
|
+
* `verify.ts` 用它记下**发给验证者的提示词原文**与**验证者返回的原始输出**,
|
|
615
|
+
* 好让「验证者为什么判错」在事后复盘得动(截断的日志排查时等于没有)。
|
|
616
|
+
*
|
|
617
|
+
* 由引擎提供,实例 id 已绑定;**写失败绝不影响验证**(引擎侧只告警不抛,这里调用方也
|
|
618
|
+
* 各自兜底)。它不携带任何判定语义 —— 判定仍只走 `ports.verify` 的返回值(T1)。
|
|
619
|
+
*/
|
|
620
|
+
logEvent?: (level: "info" | "warn" | "error", event: string, extra?: Record<string, unknown>) => void;
|
|
621
|
+
}
|
|
279
622
|
/**
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
623
|
+
* §1.2 doctor lint:对**已通过硬校验**的工作流做可达性 / 收尾 / 模板 / 无检查告警
|
|
624
|
+
* (对齐 opencode 的 `lintWorkflow`,但按我们的推进规则计算:`on_pass` 缺省 = 顺序下一步)。
|
|
625
|
+
*
|
|
626
|
+
* 这些是「引擎只在运行时才暴露、甚至永远不暴露」的问题:
|
|
627
|
+
* · 不可达步骤静默不执行;
|
|
628
|
+
* · 没有可达步骤能到 `done` → 工作流永远不完成(旧版 doctor 还报 ✅,运行时无限循环);
|
|
629
|
+
* · 未解析的 `{{...}}` 原样进入提示词;
|
|
630
|
+
* · 非 manual 步没写 check → 该步**跳过对抗性验证**、直接推进(必须让作者知道)。
|
|
283
631
|
*/
|
|
284
|
-
export declare function
|
|
632
|
+
export declare function lintWorkflow(steps: StepDef[], manual: Set<string>, autoReset?: boolean): string[];
|
|
633
|
+
/** 人类可读时长(报告用):`45s` / `3m12s` / `1h05m` */
|
|
634
|
+
export declare function formatDuration(ms: number): string;
|
|
285
635
|
/**
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
636
|
+
* 产出目录名 = 任务摘要 slug + 实例 id 尾段(照 opencode `makeArtifactsDirName` 语义)。
|
|
637
|
+
*
|
|
638
|
+
* 三条不可退让的细节:
|
|
639
|
+
* 1. **按码点截断**(`Array.from`):普通 `slice()` 会把代理对切成半个 UTF-16 单元,
|
|
640
|
+
* 落成目录名时变成 U+FFFD —— 提示词里的名字与磁盘上的真实目录就指到两个地方。
|
|
641
|
+
* 先截断再 trim `-`:截断本身可能暴露一个尾随 `-`。
|
|
642
|
+
* 2. **剥掉路径分隔符与 `.`**:slug 可能来自任意用户任务,绝不能让 `..`、`/`、`\`
|
|
643
|
+
* 把产出目录带出 `artifacts/`。
|
|
644
|
+
* 3. 尾段只取 instId 最后一段:同一任务并发跑时 slug 相同、尾段不同 → 不互相覆盖。
|
|
645
|
+
*
|
|
646
|
+
* slug 为空(任务全是空白/被剥字符)→ 直接回退整个 `instId`(保证目录名非空且唯一)。
|
|
290
647
|
*/
|
|
291
|
-
export declare function
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
648
|
+
export declare function makeArtifactsDirName(task: string, instId: string): string;
|
|
649
|
+
/** 内置工作流文件(不落盘,只存在于插件目录;lib/engine.js → ../workflows/<name>.yaml) */
|
|
650
|
+
export declare function builtinWorkflowPath(name: string): string | undefined;
|
|
651
|
+
/** 全局工作流目录:`<DSH_HOME|~/.dsh>/ralph-flow/workflows` */
|
|
652
|
+
export declare function globalWorkflowsDirOf(): string;
|
|
295
653
|
/**
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
654
|
+
* 只读某个工作区(+ 全局目录 + 内置)里**全部工作流的名字与描述** —— 不建引擎、不建任何目录。
|
|
655
|
+
*
|
|
656
|
+
* 动态快捷命令 `/ralphflow-<名字>` 在插件加载时登记,而引擎是按工作区**惰性**创建的
|
|
657
|
+
* (首个工具调用才建)。只靠引擎的话,用户在新工作区里第一次打开会话时,自己建的工作流
|
|
658
|
+
* 还没有快捷命令。这个函数让登记可以在 `session/created` 时廉价完成(纯读目录)。
|
|
659
|
+
*
|
|
660
|
+
* **必须包含内置工作流**(`loop`/`spec`)——它们不落盘,只存在于插件目录,漏掉就会让
|
|
661
|
+
* `/ralphflow-loop`、`/ralphflow-spec` 消失。工作区里的同名文件遮蔽内置(有意行为)。
|
|
299
662
|
*/
|
|
300
|
-
export declare function
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
export declare
|
|
305
|
-
|
|
306
|
-
|
|
663
|
+
export declare function listWorkflowsIn(workspace: string): Array<{
|
|
664
|
+
name: string;
|
|
665
|
+
desc: string;
|
|
666
|
+
}>;
|
|
667
|
+
export declare function createEngine(projectDir: string, ports: EnginePorts): {
|
|
668
|
+
root: string;
|
|
669
|
+
instancesDir: string;
|
|
670
|
+
workflowsDir: string;
|
|
671
|
+
reportsDir: string;
|
|
672
|
+
projectDir: string;
|
|
673
|
+
ensureLayout: () => void;
|
|
674
|
+
listWorkflows: () => WorkflowEntry[];
|
|
675
|
+
loadWorkflow: (name: string, ctx?: SubWorkflowLoadCtx) => {
|
|
676
|
+
def: WorkflowDef | null;
|
|
677
|
+
problems: string[];
|
|
678
|
+
warnings: string[];
|
|
679
|
+
};
|
|
680
|
+
readState: (instId: string) => InstanceState | null;
|
|
681
|
+
listInstances: () => InstanceInfo[];
|
|
682
|
+
listHistory: () => HistoryInfo[];
|
|
683
|
+
instanceDir: (instId: string) => string;
|
|
684
|
+
artifactsDirOf: (instId: string) => string;
|
|
685
|
+
artifactsRelDirOf: (instId: string) => string;
|
|
686
|
+
reportRelPathOf: (instId: string) => string;
|
|
687
|
+
destroyInstance: (instId: string, status: "done" | "cancelled", state?: InstanceState) => {
|
|
688
|
+
reportPath: string;
|
|
689
|
+
instanceDirRemoved: boolean;
|
|
690
|
+
instanceDir: string;
|
|
691
|
+
} | null;
|
|
692
|
+
start: (workflowName: string, task: string, sessionId: string) => ToolResult;
|
|
693
|
+
onSubmit: (sessionId: string, summary?: string) => ToolResult;
|
|
694
|
+
noteAssistantText: (sessionId: string, text: string) => void;
|
|
695
|
+
remindToSubmit: (sessionId: string) => {
|
|
696
|
+
remind: boolean;
|
|
697
|
+
message?: string;
|
|
698
|
+
summary?: string;
|
|
699
|
+
};
|
|
700
|
+
continueInstance: (sessionId: string, instanceRef?: string) => ToolResult;
|
|
701
|
+
resetCurrent: (sessionId: string) => ToolResult;
|
|
702
|
+
rewindTo: (sessionId: string, targetRef: string, reason?: string) => ToolResult;
|
|
703
|
+
cancelInstance: (sessionId: string, instanceRef?: string, reason?: string) => ToolResult;
|
|
704
|
+
statusOf: (sessionId: string, instanceRef?: string) => ToolResult;
|
|
705
|
+
listAll: () => ToolResult;
|
|
706
|
+
restore: () => void;
|
|
707
|
+
diagnose: () => ToolResult;
|
|
708
|
+
activeInstanceOfSession: (sessionId: string) => InstanceInfo | undefined;
|
|
709
|
+
};
|
|
710
|
+
export type Engine = ReturnType<typeof createEngine>;
|
|
711
|
+
export declare const BUILTIN_WORKFLOWS: string[];
|