ralphflow-dsh 0.1.1 → 2.0.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 +220 -167
- 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 +19 -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/reset.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { toolPairingBalancedAfter } from "@deepseek-ai/dsh-compaction";
|
|
2
|
+
import { boundContextSummary, createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
3
|
+
import { RALPHFLOW_SOURCE_KIND } from "./message-source.js";
|
|
4
|
+
const msgOf = (e) => (e instanceof Error ? e.message : String(e));
|
|
5
|
+
/**
|
|
6
|
+
* 造一个 `EnginePorts.resetSurface`。
|
|
7
|
+
*
|
|
8
|
+
* 返回值**永不抛**:把「没换成」如实回给引擎(引擎据此如实播报并照常投递 DO),
|
|
9
|
+
* 因为「重置失败」绝不能演变成「本步无法执行」。
|
|
10
|
+
*/
|
|
11
|
+
export function createResetSurface(ctx, log) {
|
|
12
|
+
const note = (level, event, data) => {
|
|
13
|
+
try {
|
|
14
|
+
log?.(level, event, data);
|
|
15
|
+
}
|
|
16
|
+
catch { }
|
|
17
|
+
};
|
|
18
|
+
const agentOf = (sessionId) => {
|
|
19
|
+
try {
|
|
20
|
+
const agents = ctx.agents;
|
|
21
|
+
const a = agents?.get?.(sessionId);
|
|
22
|
+
return a && typeof a === "object" ? a : undefined;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const sessionOf = (sessionId, agent) => {
|
|
29
|
+
if (agent?.session)
|
|
30
|
+
return agent.session;
|
|
31
|
+
try {
|
|
32
|
+
const sessions = ctx.sessions;
|
|
33
|
+
const s = sessions?.get?.(sessionId);
|
|
34
|
+
return s && typeof s === "object" ? s : undefined;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
return async function resetSurface(sessionId, req) {
|
|
41
|
+
const agent = agentOf(sessionId);
|
|
42
|
+
const session = sessionOf(sessionId, agent);
|
|
43
|
+
if (!session)
|
|
44
|
+
return { ok: false, reason: "no_session" };
|
|
45
|
+
if (typeof agent?.runMaintenance !== "function") {
|
|
46
|
+
// 没有宿主互斥入口就**不做**:直接 append 会与 agent 回合并发写同一份日志(1.3)
|
|
47
|
+
return { ok: false, reason: "no_maintenance" };
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
// ── 先等属主会话的驱动器**收工**(phase 回 idle),再做替换 ──────────────
|
|
51
|
+
//
|
|
52
|
+
// 为什么必须等:`reset: true` 的步骤有两种进入方式——
|
|
53
|
+
// · 验证回调推进 / 返工:调用发生在引擎的异步回调里,agent 本就空闲,
|
|
54
|
+
// `whenIdle()` 立刻兑现(与改造前等价);
|
|
55
|
+
// · **在工具调用内部推进**:审查门放行(模型调 `ralphflow_continue`)、
|
|
56
|
+
// 无 check 步骤交卷即推进。这时驱动器正忙(phase = running),
|
|
57
|
+
// `runMaintenance` 会同步抛错 ⇒ 替换**永远不生效**。
|
|
58
|
+
// ⚠️ 内置 `spec.yaml` 的 `implement` 恰恰**只能**经 `propose` 的审查门进入,
|
|
59
|
+
// 所以这不是边缘情况:不等的话第二行 `reset: true` 结构性失效
|
|
60
|
+
// (第一轮验证者 3/4 实测抓到过)。
|
|
61
|
+
//
|
|
62
|
+
// 这个等待不会死锁:调用方(引擎的 `deliverStepDo`)是**脱手**调用本函数的
|
|
63
|
+
// (`void ...then(...)`,绝不 await),所以工具调用照常返回 → 驱动器收工 →
|
|
64
|
+
// 这里兑现 → 替换落在**真正的空闲窗口**里 → 引擎随后才投递 DO。
|
|
65
|
+
// 硬约束 1.2 因此仍然成立:替换绝不发生在工具调用内部。
|
|
66
|
+
try {
|
|
67
|
+
await agent.whenIdle?.();
|
|
68
|
+
}
|
|
69
|
+
catch { /* 宿主没有 whenIdle(老运行时):退回直接尝试,靠 runMaintenance 的护栏兜底 */ }
|
|
70
|
+
return await agent.runMaintenance(async (signal) => {
|
|
71
|
+
// ── 1.1 合法范围 = nodes[1] … nodes[N](端点 inclusive;node0 是系统提示,永不覆盖)
|
|
72
|
+
const nodes = [...session.surface.nodes];
|
|
73
|
+
if (nodes.length < 2)
|
|
74
|
+
return { ok: false, reason: "surface_too_short" };
|
|
75
|
+
const startSeq = nodes[1];
|
|
76
|
+
const tailSeq = nodes[nodes.length - 1];
|
|
77
|
+
// ── 1.5 替换前自检平衡(`toolPairingBalancedAfter` 自己可能抛:seq 不在面上 /
|
|
78
|
+
// tool/result 没有对应 call → "corrupt surface")。不平衡就放弃本次。
|
|
79
|
+
let balanced = false;
|
|
80
|
+
try {
|
|
81
|
+
balanced = toolPairingBalancedAfter(session, tailSeq);
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
note("warn", "reset_balance_check_failed", { sessionId, error: msgOf(e) });
|
|
85
|
+
return { ok: false, reason: "balance_error" };
|
|
86
|
+
}
|
|
87
|
+
if (!balanced)
|
|
88
|
+
return { ok: false, reason: "unbalanced" };
|
|
89
|
+
if (signal?.aborted)
|
|
90
|
+
return { ok: false, reason: "aborted" };
|
|
91
|
+
// ── 最后一道复查:等空闲窗口这段时间里实例可能已被取消/结束。
|
|
92
|
+
// 它必须在 append **之前同步**做(同一次 maintenance job 内),否则取消后
|
|
93
|
+
// 照样会提交一次替换 —— 属主会话的旧对话被换成一条**已取消工作流**的交接稿。
|
|
94
|
+
if (typeof req.canProceed === "function") {
|
|
95
|
+
let ok = false;
|
|
96
|
+
try {
|
|
97
|
+
ok = req.canProceed() === true;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
ok = false;
|
|
101
|
+
}
|
|
102
|
+
if (!ok)
|
|
103
|
+
return { ok: false, reason: "instance_gone" };
|
|
104
|
+
}
|
|
105
|
+
// ── 决定②:另发一条**可见告知**(append 来源 → Chat 显示)。
|
|
106
|
+
// 它随后被本次替换一并遮蔽:用户看得到(append 来源的旧消息照常显示),
|
|
107
|
+
// 模型看不到(替换把 shadowed 节点整段移出可见面)—— 于是
|
|
108
|
+
// 模型上下文精确等于「系统提示 + 交接稿 + 本步 DO」。
|
|
109
|
+
//
|
|
110
|
+
// 为什么是**直接 append** 而不是走引擎的 `deliver`(`agent.steer`):
|
|
111
|
+
// steer 只是把消息塞进收件箱,驱动器要到**下一回合开始**才把它落成
|
|
112
|
+
// `user/message` —— 那时本次替换早已完成,告知会落在交接稿之后、
|
|
113
|
+
// 变成模型可见的第 3 条(违反完成判据)。要「可见但不进模型上下文」,
|
|
114
|
+
// 只能先让它成为 append 来源的节点、再让同一次替换把它遮蔽掉。
|
|
115
|
+
const notice = createUserMessage({
|
|
116
|
+
content: [{ type: "text", text: req.notice.text }],
|
|
117
|
+
source: { kind: RALPHFLOW_SOURCE_KIND, form: "notice", summary: boundContextSummary(req.notice.summary) },
|
|
118
|
+
});
|
|
119
|
+
const noticeSeq = session.append("user/message", notice, { surfaceOp: "append" }).seq;
|
|
120
|
+
// ── 整段替换:交接稿成为系统提示之外**唯一**的节点。
|
|
121
|
+
// 1.6 sourceEventSeqs 必须逐条列出每个被遮蔽节点(= nodes[1..N] + 告知),
|
|
122
|
+
// 缺一个即抛;1.4 用自有 plugin source,不发任何 compaction 事件。
|
|
123
|
+
const shadowed = [...nodes.slice(1), noticeSeq];
|
|
124
|
+
const handoff = createUserMessage({
|
|
125
|
+
content: [{ type: "text", text: req.handoff }],
|
|
126
|
+
source: { kind: RALPHFLOW_SOURCE_KIND },
|
|
127
|
+
});
|
|
128
|
+
const handoffSeq = session.append("user/message", handoff, {
|
|
129
|
+
surfaceOp: { op: "replace", startSeq, endSeq: noticeSeq },
|
|
130
|
+
sourceEventSeqs: shadowed,
|
|
131
|
+
}).seq;
|
|
132
|
+
note("info", "reset_surface_replaced", {
|
|
133
|
+
sessionId, shadowed: shadowed.length, handoffSeq, noticeSeq, startSeq, tailSeq,
|
|
134
|
+
});
|
|
135
|
+
return { ok: true, shadowed: shadowed.length, handoffSeq, noticeSeq };
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
// phase ≠ idle 时 runMaintenance **同步抛错** —— 这正是 1.2/1.3 的护栏:
|
|
140
|
+
// 在工具调用内部(例如工作流首步的 `ralphflow_start`)拿不到窗口,如实放弃。
|
|
141
|
+
const text = msgOf(e);
|
|
142
|
+
const notIdle = /already has active work/.test(text);
|
|
143
|
+
note("warn", notIdle ? "reset_surface_not_idle" : "reset_surface_failed", { sessionId, error: text });
|
|
144
|
+
return { ok: false, reason: notIdle ? "not_idle" : "maintenance_failed", detail: text };
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
package/lib/tools.d.ts
CHANGED
|
@@ -1,24 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Ralph Flow for dsh —
|
|
2
|
+
* Ralph Flow for dsh v2 — 工具 + 命令(命名与 opencode/claude 版一致)
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
4
|
+
* 命令语义 = 触发词:/ralphflow-* 注入指令给模型,由模型调用同名工具并自然回复。
|
|
5
|
+
* 已实现:start / list / status / continue / cancel / create / doctor + 工作流快捷命令(/ralphflow-<工作流名>);
|
|
6
|
+
* `/ralphflow-rewind` 与 `/ralphflow-reset` 是两条**机械命令**(见下)。
|
|
7
|
+
*
|
|
8
|
+
* **`/ralphflow-reset` 与 `/ralphflow-rewind` 是仅有的两条机械命令,但不是例外「命令面」**:
|
|
9
|
+
* 它们由命令处理器**直接**调用引擎完成机械动作 —— reset = `resetCurrent`(只换干净上下文
|
|
10
|
+
* + 重投当前步 DO,**不赦免失败**);rewind = `rewindTo`(回退到更早的步骤 + 清暂停与失败计数
|
|
11
|
+
* + 整段替换上下文 + 目标步 DO 带着原因重投)—— 再把结果交回模型自然语言回复。
|
|
12
|
+
* 为什么不做成 `ralphflow_reset` / `ralphflow_rewind` 工具:
|
|
13
|
+
* · 这两件事是**机械程序**的职责(reset 不赦免失败才守得住 `max_fail_count`;rewind 的
|
|
14
|
+
* 状态机倒退必须与机械动作原子发生),不该要求模型记得去调;
|
|
15
|
+
* · design §10.10 的边界 —— 不给模型任何「可调用的修复入口」(工具面保持固定)。
|
|
16
|
+
*
|
|
17
|
+
* 注意别把三者搞混:**步骤级 `reset: true` / 工作流级 `auto_reset: true`(重置门)**是工作流
|
|
18
|
+
* 作者写的键(载体见 `src/reset.ts`);`/ralphflow-reset`(重做当前步)与 `/ralphflow-rewind`
|
|
19
|
+
* (回退到更早步骤并换方向)是**用户**手里的两个手动入口 —— 三条自动重置路径(步骤级 /
|
|
20
|
+
* `auto_reset` / 调用点)与这两个手动入口全都走引擎的同一根接线(`deliverStepDo` 的
|
|
21
|
+
* `opts.manual` / `opts.rewind`)。
|
|
11
22
|
*/
|
|
12
23
|
import type { Context } from "@deepseek-ai/cordis";
|
|
13
24
|
import type { Agent } from "@deepseek-ai/dsh-agent";
|
|
14
25
|
import type { Engine } from "./engine.js";
|
|
15
|
-
import type { RalphJobManager } from "./jobs.js";
|
|
16
26
|
export interface ToolContext {
|
|
17
27
|
ctx: Context;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
28
|
+
/** 按工作区取引擎(**一个工作区一个引擎**,见 index.ts 的 engineFor);缺省用进程工作区 */
|
|
29
|
+
engineFor: (workspace?: string) => Engine;
|
|
30
|
+
deliver: (sessionId: string, text: string) => boolean;
|
|
31
|
+
/** 解析发起会话的工作区(实例资产落点) */
|
|
32
|
+
workspaceOfSession?: (sessionId: string) => string;
|
|
21
33
|
}
|
|
22
34
|
export type ToolHandler = (args: any, agent: Agent | undefined) => Promise<string> | string;
|
|
23
|
-
export
|
|
24
|
-
export declare function
|
|
35
|
+
export declare function registerTools(deps: ToolContext): Map<string, ToolHandler>;
|
|
36
|
+
export declare function registerCommands(deps: ToolContext & {
|
|
37
|
+
handlers: Map<string, ToolHandler>;
|
|
38
|
+
}): (workflows: Array<{
|
|
39
|
+
name: string;
|
|
40
|
+
desc: string;
|
|
41
|
+
}>) => void;
|