@linxin666/dsh-liangshen 0.1.14 → 0.1.16
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 +2 -2
- package/README.md +7 -6
- package/README.zh.md +7 -6
- package/lib/index.js +179 -18
- package/lib/types/dsh-home.d.ts +16 -0
- package/lib/types/index.d.ts +2 -3
- package/lib/types/schema.d.ts +18 -0
- package/lib/types/sync.d.ts +10 -0
- package/package.json +1 -1
- package/presets/liangshen/agent.cordis.yml +10 -4
- package/presets/liangshen/preset.yml +1 -1
- package/presets/liangshen/tool-bootstrap.mjs +70 -3
- package/src/dsh-home.ts +36 -0
- package/src/index.test.ts +6 -0
- package/src/index.ts +8 -17
- package/src/schema.test.ts +64 -0
- package/src/schema.ts +110 -0
- package/src/sync.test.ts +77 -8
- package/src/sync.ts +52 -3
package/README.i18n.yaml
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
# side as of the last confirmed-consistent state. Both languages carry equal authority;
|
|
3
3
|
# after editing either side, bring the other along and re-record with:
|
|
4
4
|
# node scripts/verify-docs.mjs --write <dir>
|
|
5
|
-
README.md:
|
|
6
|
-
README.zh.md:
|
|
5
|
+
README.md: b7f13d1e7027a3f9fdf40f53d15bb96a736f8cf2
|
|
6
|
+
README.zh.md: 27da5569dd9068c83f7a64f8faa5f46683b8c3ad
|
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Ships the "Anchored Standard" preset as a one-command plugin of the dsh-web-ui f
|
|
|
8
8
|
|
|
9
9
|
DeepSeek V4 Pro conditions strongly on the API tool catalog visible in the FIRST request when choosing its execution trajectory. In the community eval ([xiaobright/modeltest](https://github.com/xiaobright/modeltest)), Standard / PTC scored 91/92 while Minimal reached 99/96 — but Minimal keeps only two tools. This two-phase approach separates the first-trajectory choice from full later capability:
|
|
10
10
|
|
|
11
|
-
1. The first model request exposes only the builtin Minimal preset's exact two tools (persistent `bash` plus `str_replace_editor`), keeps only the `persona` prompt section, empties runtime contexts, and passes only the user's own messages;
|
|
11
|
+
1. The first model request exposes only the builtin Minimal preset's exact two tools (persistent `bash` plus `str_replace_editor`), keeps only the `deployment:persona` prompt section, empties runtime contexts, and passes only the user's own messages;
|
|
12
12
|
2. After the session's first durable `tool/call`, promotion waits until the first reasoning block is minimal-like (contains `we` and no `let me`), with a four-step fallback; the wire then switches to Code Mode (PTC) — a single `run_code` tool backed by the full tool registry SDK — and every assembled prompt section plus the ordinary workspace-instruction, skill-catalog, and runtime-context injections return;
|
|
13
13
|
3. The phase derives from persisted session events, so resume / reload never lose state.
|
|
14
14
|
|
|
@@ -22,9 +22,10 @@ The preset ships with extra safeguards on top of the reference mechanism, all co
|
|
|
22
22
|
- `maxBootstrapSteps` — fallback promotion after N steps when no anchored block appeared;
|
|
23
23
|
- `promoteAfterFirstResponse` — a tool-less first response promotes once it has responded; an anchor-gated session also releases when its first turn ends (`turn/end`), so the next user turn already sees the promoted catalog;
|
|
24
24
|
- `promotedPresentation: code` — after promotion the wire is Code Mode (PTC): one `run_code` tool with the full registry available through the generated SDK, switched at the step boundary so the current step's native calls are never interrupted;
|
|
25
|
-
- `deferredSources` + `deferredGraceSteps` — workspace instructions and the skill catalog wait one extra step after promotion, so the tool-catalog switch and the injection shock do not land in the same step
|
|
25
|
+
- `deferredSources` + `deferredGraceSteps` — workspace instructions and the skill catalog wait one extra step after promotion, so the tool-catalog switch and the injection shock do not land in the same step;
|
|
26
|
+
- `bootstrapMaxTokens` — caps the phase-1 request output budget (community measurements put `max_tokens=1024` in the high-hit "We need" window, versus 0/5 at the 256k DSH default), and the cap is stripped again after promotion so `requestProposal` never solders 1024 into every later request.
|
|
26
27
|
|
|
27
|
-
Plan mode is supported: phase 1 filters the assembled prompt sections down to the one-line `persona`, and promotion restores all sections, so the plan-mode `plan:policy` section takes effect for every step after promotion.
|
|
28
|
+
Plan mode is supported: phase 1 filters the assembled prompt sections down to the one-line `deployment:persona`, and promotion restores all sections and appends the session's working directory to the persona, so the agent knows its workspace and the plan-mode `plan:policy` section takes effect for every step after promotion.
|
|
28
29
|
|
|
29
30
|
## Install
|
|
30
31
|
|
|
@@ -47,8 +48,8 @@ Fully restart `dsh web`, open a NEW empty session, and pick "梁神模式" as th
|
|
|
47
48
|
Export the session JSONL and inspect `request/header`:
|
|
48
49
|
|
|
49
50
|
- The first header should carry only `bash/str_replace_editor` (the persistent shell plus the sandboxed editor);
|
|
50
|
-
- The first turn should contain only the user's own messages — no workspace-instruction baseline, no runtime snapshot, no skill-catalog message — and only the `persona` prompt section;
|
|
51
|
-
- After the first tool call, the next changed header should carry exactly `run_code` (PTC); the runtime snapshot and all prompt sections (including plan mode's `plan:policy
|
|
51
|
+
- The first turn should contain only the user's own messages — no workspace-instruction baseline, no runtime snapshot, no skill-catalog message — and only the `deployment:persona` prompt section;
|
|
52
|
+
- After the first tool call, the next changed header should carry exactly `run_code` (PTC); the runtime snapshot and all prompt sections arrive with that step (including plan mode's `plan:policy`, and the persona now ends with the selected workspace path), and the workspace instructions and skill catalog arrive one step later;
|
|
52
53
|
- Phase-1 editor writes obey the host file sandbox policy — there is no bare local-filesystem bypass;
|
|
53
54
|
- Later requests keep `run_code`.
|
|
54
55
|
|
|
@@ -63,7 +64,7 @@ node tools/analyze-session.mjs ~/.dsh/sessions/<workspace>/<session>/session.jso
|
|
|
63
64
|
- A first model response that calls no tool promotes once it has responded; an anchor-gated session also releases when its first turn ends (`turn/end`). The release is decided during prompt assembly, so the new user turn already gets the promoted PTC catalog and its messages are not stripped;
|
|
64
65
|
- After the first tool call, promotion waits for the first minimal-like reasoning block or the `maxBootstrapSteps` fallback, whichever comes first;
|
|
65
66
|
- A tool call that fails still counts toward promotion as long as `tool/call` was persisted;
|
|
66
|
-
- Phase 1 keeps only the `persona` prompt section; promotion restores every assembled section, so plan mode's `plan:policy` is enforced after phase 1;
|
|
67
|
+
- Phase 1 keeps only the `deployment:persona` prompt section; promotion restores every assembled section and appends the session's working directory (`Your working directory is <cwd>.`) to the persona, so the agent works in the selected workspace and plan mode's `plan:policy` is enforced after phase 1;
|
|
67
68
|
- Workspace instructions, the skill catalog, and the runtime snapshot stay out of phase 1; the snapshot returns with the catalog and the other two arrive one step later;
|
|
68
69
|
- Phase-1 file tools inherit the host file sandbox (no bare `dsh-fs-local` filesystem);
|
|
69
70
|
- The phase-1 persistent `bash` replaces the Standard ephemeral shell for the whole session (both tools register the name `bash`);
|
package/README.zh.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
DeepSeek V4 Pro 会强烈依赖 API 中可见的**首轮工具目录**选择执行轨迹。社区评测([xiaobright/modeltest](https://github.com/xiaobright/modeltest))中,Standard / PTC 只有 91/92 分,Minimal 达到 99/96,但 Minimal 只有两个工具。两阶段方案把「首次轨迹选择」与「后续完整工具能力」拆开:
|
|
10
10
|
|
|
11
|
-
1. 首轮模型请求只暴露官方 Minimal 精确双工具(持久 `bash` 与 `str_replace_editor`),只保留 `persona` 一个 prompt section,清空运行时上下文,并且只放行用户自己的消息;
|
|
11
|
+
1. 首轮模型请求只暴露官方 Minimal 精确双工具(持久 `bash` 与 `str_replace_editor`),只保留 `deployment:persona` 一个 prompt section,清空运行时上下文,并且只放行用户自己的消息;
|
|
12
12
|
2. 会话出现首次持久 `tool/call` 后,晋升会等到首个 reasoning 块呈 minimal-like(包含 `we` 且无 `let me`)才发生,四步兜底;随后 wire 切换为 Code Mode(PTC)——只暴露一个 `run_code`,完整工具注册表通过生成的 SDK 调用——并恢复全部 prompt section(含 plan mode 的 `plan:policy`)以及 workspace 指令、skill 目录与运行时快照等常规注入;
|
|
13
13
|
3. 阶段从持久化 session events 推导,resume / reload 不丢失状态。
|
|
14
14
|
|
|
@@ -22,9 +22,10 @@ preset 在参考机制之上内置了额外保护,全部在 `agent.cordis.yml`
|
|
|
22
22
|
- `maxBootstrapSteps`:N 步后仍无锚定块时强制晋升;
|
|
23
23
|
- `promoteAfterFirstResponse`:首轮无工具调用的回答在响应后自动晋升;锚定门控中的会话也会在首轮结束时(`turn/end`)释放,因此新用户轮次一开始就拿到晋升后的目录;
|
|
24
24
|
- `promotedPresentation: code`:晋升后 wire 为 Code Mode(PTC)——一个 `run_code` 工具、完整注册表通过生成 SDK 调用;切换发生在 step 边界,不会打断当前步的原生工具调用;
|
|
25
|
-
- `deferredSources` + `deferredGraceSteps`:workspace 指令与 skill
|
|
25
|
+
- `deferredSources` + `deferredGraceSteps`:workspace 指令与 skill 目录在晋升后再等一步注入,工具目录切换和注入冲击不同时落地;
|
|
26
|
+
- `bootstrapMaxTokens`:phase 1 请求的输出预算封顶(社区实测 `max_tokens=1024` 是 "We need" 轨迹的高命中窗口,DSH 默认 256k 命中率为 0),晋升后自动剥离该封顶,避免 `requestProposal` 把 1024 焊进后续每个请求。
|
|
26
27
|
|
|
27
|
-
已支持 plan mode:phase 1 会把 prompt sections 过滤为仅剩一行 `persona`,晋升后恢复全部 sections
|
|
28
|
+
已支持 plan mode:phase 1 会把 prompt sections 过滤为仅剩一行 `deployment:persona`,晋升后恢复全部 sections 并在 persona 末尾追加所选工作区路径,因此 Agent 明确自己的工作目录,plan-mode 的 `plan:policy` 也在晋升后的每一步都生效。
|
|
28
29
|
|
|
29
30
|
## 安装
|
|
30
31
|
|
|
@@ -47,8 +48,8 @@ dsh plugin --profile web remove @linxin666/dsh-liangshen
|
|
|
47
48
|
导出 session JSONL,检查 `request/header`:
|
|
48
49
|
|
|
49
50
|
- 第一份 header 应只有 `bash/str_replace_editor`(持久 shell + 沙箱化编辑器);
|
|
50
|
-
- 第一轮应只包含用户自己的消息:没有 workspace 指令 baseline、没有运行时快照、没有 skill 目录消息,并且只有 `persona` 一个 prompt section;
|
|
51
|
-
- 首次工具调用后,下一份变更 header 应恰好为 `run_code`(PTC);运行时快照与全部 prompt section
|
|
51
|
+
- 第一轮应只包含用户自己的消息:没有 workspace 指令 baseline、没有运行时快照、没有 skill 目录消息,并且只有 `deployment:persona` 一个 prompt section;
|
|
52
|
+
- 首次工具调用后,下一份变更 header 应恰好为 `run_code`(PTC);运行时快照与全部 prompt section 随该步出现(含 plan mode 的 `plan:policy`,且 persona 末尾带有所选工作区路径),workspace 指令与 skill 目录再晚一步出现;
|
|
52
53
|
- phase 1 编辑器写入受宿主文件沙箱策略约束,不存在裸本地文件系统绕过;
|
|
53
54
|
- 此后的请求保持 `run_code`。
|
|
54
55
|
|
|
@@ -63,7 +64,7 @@ node tools/analyze-session.mjs <导出的 session.jsonl>
|
|
|
63
64
|
- 第一次模型响应如果没有调用工具,在响应后即自动晋升;锚定门控中的会话也会在首轮结束(`turn/end`)时释放。释放判定发生在 prompt assemble 阶段,所以新用户轮次一开始就拿到晋升后的 PTC 目录,其消息也不会再被剥离;
|
|
64
65
|
- 首次工具调用后,晋升等待首个 minimal-like reasoning 块或 `maxBootstrapSteps` 兜底,先到者生效;
|
|
65
66
|
- 工具执行即使失败,只要 `tool/call` 已持久化,仍计入晋升条件;
|
|
66
|
-
- phase 1 只保留 `persona` prompt section;晋升后恢复全部 assembled sections
|
|
67
|
+
- phase 1 只保留 `deployment:persona` prompt section;晋升后恢复全部 assembled sections 并在 persona 末尾追加所选工作区路径(`Your working directory is <cwd>.`),因此 Agent 会在选中的工作区工作,plan mode 的 `plan:policy` 也在晋升后生效;
|
|
67
68
|
- workspace 指令、skill 目录与运行时快照在首轮不注入;快照随 PTC 目录出现,前两者再晚一步出现;
|
|
68
69
|
- phase 1 文件工具继承宿主文件沙箱(不挂载裸 `dsh-fs-local`);
|
|
69
70
|
- phase 1 的持久 `bash` 会替代 Standard 的一次性 shell 直到会话结束(两个工具都注册 `bash` 名字);
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,133 @@
|
|
|
1
1
|
import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
2
|
-
import {
|
|
3
|
-
import { basename, dirname, join, relative } from "node:path";
|
|
2
|
+
import { basename, dirname, isAbsolute, join, relative } from "node:path";
|
|
4
3
|
import { fileURLToPath } from "node:url";
|
|
5
4
|
import z from "schemastery";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
//#region src/dsh-home.ts
|
|
7
|
+
/**
|
|
8
|
+
* DSH_HOME resolution shared by the plugin family's Host halves: the
|
|
9
|
+
* environment override wins, the platform home fallback follows. Mirrors
|
|
10
|
+
* what dsh-pet and dsh-liangshen each used to implement locally.
|
|
11
|
+
*/
|
|
12
|
+
/** Expand a leading ~ (or ~user) in a path, platform-style. */
|
|
13
|
+
function expandHome(path, home = homedir()) {
|
|
14
|
+
if (path === "~") return home;
|
|
15
|
+
if (path.startsWith("~/") || path.startsWith("~\\")) return join(home, path.slice(2));
|
|
16
|
+
return path;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Resolve the DSH home directory.
|
|
20
|
+
* @param env - process environment to read DSH_HOME from.
|
|
21
|
+
* @param home - platform home directory fallback (test seam).
|
|
22
|
+
* @returns the absolute DSH home path.
|
|
23
|
+
*/
|
|
24
|
+
function resolveDshHome(env = process.env, home = homedir()) {
|
|
25
|
+
const raw = env.DSH_HOME;
|
|
26
|
+
if (raw !== void 0 && raw.trim() !== "") {
|
|
27
|
+
const expanded = expandHome(raw.trim(), home);
|
|
28
|
+
return isAbsolute(expanded) ? expanded : join(process.cwd(), expanded);
|
|
29
|
+
}
|
|
30
|
+
return join(home, ".dsh");
|
|
31
|
+
}
|
|
32
|
+
/** Resolve the DSH home directory from the live environment. */
|
|
33
|
+
function dshHome() {
|
|
34
|
+
return resolveDshHome();
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/schema.ts
|
|
38
|
+
/**
|
|
39
|
+
* Structural validation for a bundled `agent.cordis.yml`.
|
|
40
|
+
*
|
|
41
|
+
* Deliberately dependency-free: it parses only the flat row metadata the sync
|
|
42
|
+
* and the dsh agent-presets loader rely on. Every top-level row is written as
|
|
43
|
+
* `- id: <id>` at column zero, with the `name`/`group`/`disabled` keys at two
|
|
44
|
+
* spaces of indentation. Nested `config:` and `isolate:` bodies are opaque to
|
|
45
|
+
* this validator — the dsh loader checks their semantics.
|
|
46
|
+
*
|
|
47
|
+
* Returns the list of problems found; an empty array means the document is
|
|
48
|
+
* structurally valid.
|
|
49
|
+
*/
|
|
50
|
+
/** A top-level row opener: `- id: <id>` (id may be blank for diagnostics). */
|
|
51
|
+
const ROW_RE = /^-\s+id:\s*(.*)$/;
|
|
52
|
+
/** Any top-level list item, for ids missing from a row opener. */
|
|
53
|
+
const ITEM_RE = /^-\s/;
|
|
54
|
+
/** A two-space-indented flat metadata key: ` name: <value>`. */
|
|
55
|
+
const META_RE = /^ {2}([A-Za-z][A-Za-z0-9_-]*):\s*(.*)$/;
|
|
56
|
+
/** The only `name` forms the dsh agent-presets loader mounts from a row. */
|
|
57
|
+
const NAME_PREFIX_RE = /^(\.\/|@|cordis:)/;
|
|
58
|
+
/** Strip one pair of surrounding single or double quotes from a scalar. */
|
|
59
|
+
function unquote(value) {
|
|
60
|
+
if (value.length >= 2) {
|
|
61
|
+
const first = value[0];
|
|
62
|
+
if ((first === "'" || first === "\"") && value.endsWith(first)) return value.slice(1, -1);
|
|
63
|
+
}
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Validate the structural contract of an `agent.cordis.yml` document.
|
|
68
|
+
* @param text - the raw YAML document text.
|
|
69
|
+
* @returns a list of human-readable problems; empty means valid.
|
|
70
|
+
*/
|
|
71
|
+
function validateAgentCordis(text) {
|
|
72
|
+
const errors = [];
|
|
73
|
+
const normalized = text.replace(/\r\n/g, "\n");
|
|
74
|
+
if (normalized.trim() === "") return ["document is empty"];
|
|
75
|
+
const seenIds = /* @__PURE__ */ new Set();
|
|
76
|
+
const current = {
|
|
77
|
+
id: null,
|
|
78
|
+
name: null,
|
|
79
|
+
group: null
|
|
80
|
+
};
|
|
81
|
+
const closeRow = () => {
|
|
82
|
+
if (current.id === null) return;
|
|
83
|
+
if (current.name === null) errors.push(`row "${current.id}": missing "name" key`);
|
|
84
|
+
else if (!NAME_PREFIX_RE.test(current.name)) errors.push(`row "${current.id}": name "${current.name}" must start with "./", "@" or "cordis:"`);
|
|
85
|
+
if (current.group === "true" && current.name !== "cordis:group") errors.push(`row "${current.id}": "group: true" requires name "cordis:group"`);
|
|
86
|
+
current.name = null;
|
|
87
|
+
current.group = null;
|
|
88
|
+
current.id = null;
|
|
89
|
+
};
|
|
90
|
+
const lines = normalized.split("\n");
|
|
91
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
92
|
+
const lineNo = index + 1;
|
|
93
|
+
const line = lines[index];
|
|
94
|
+
const trimmed = line.trim();
|
|
95
|
+
if (trimmed === "" || trimmed.startsWith("#")) continue;
|
|
96
|
+
const row = ROW_RE.exec(line);
|
|
97
|
+
if (row !== null) {
|
|
98
|
+
closeRow();
|
|
99
|
+
const id = row[1].trim();
|
|
100
|
+
if (id === "") {
|
|
101
|
+
errors.push(`line ${lineNo}: empty row id`);
|
|
102
|
+
current.id = null;
|
|
103
|
+
} else {
|
|
104
|
+
if (seenIds.has(id)) errors.push(`line ${lineNo}: duplicate row id "${id}"`);
|
|
105
|
+
seenIds.add(id);
|
|
106
|
+
current.id = id;
|
|
107
|
+
}
|
|
108
|
+
current.name = null;
|
|
109
|
+
current.group = null;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (current.id === null) {
|
|
113
|
+
if (ITEM_RE.test(line)) errors.push(`line ${lineNo}: list item does not declare an "id:"`);
|
|
114
|
+
else if (/^\S/.test(line)) errors.push(`line ${lineNo}: content outside a "- id:" row`);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const meta = META_RE.exec(line);
|
|
118
|
+
if (meta !== null) {
|
|
119
|
+
const value = unquote(meta[2].trim());
|
|
120
|
+
if (meta[1] === "name") current.name = value;
|
|
121
|
+
else if (meta[1] === "group") current.group = value;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (/^ {2}/.test(line)) continue;
|
|
125
|
+
errors.push(`line ${lineNo}: unexpected content in row "${current.id}"`);
|
|
126
|
+
}
|
|
127
|
+
closeRow();
|
|
128
|
+
return errors;
|
|
129
|
+
}
|
|
130
|
+
//#endregion
|
|
6
131
|
//#region src/sync.ts
|
|
7
132
|
/**
|
|
8
133
|
* Sync every preset directory under `sourceRoot` into `targetRoot` — the
|
|
@@ -14,7 +139,19 @@ import z from "schemastery";
|
|
|
14
139
|
* tree is copied and any target files the source does not contain are removed.
|
|
15
140
|
* Directories the plugin does not own (other presets the user authored) are
|
|
16
141
|
* never touched.
|
|
142
|
+
*
|
|
143
|
+
* After a preset is synced its `agent.cordis.yml` is validated against the
|
|
144
|
+
* structural preset schema; a validation failure is reported through the
|
|
145
|
+
* run's `failed` entries instead of being a warn-only side effect, so callers
|
|
146
|
+
* can observe (and surface) a broken preset rather than silently shipping it.
|
|
147
|
+
*/
|
|
148
|
+
/**
|
|
149
|
+
* Clock/coarse-grain tolerance for the mtime fast path. When a source and a
|
|
150
|
+
* target file share a size and a near-identical mtime we still fall through to
|
|
151
|
+
* a byte comparison; a mtime gap beyond this simply proves the pair cannot be
|
|
152
|
+
* byte-identical, so we skip the read.
|
|
17
153
|
*/
|
|
154
|
+
const MTIME_TOLERANCE_MS = 1e3;
|
|
18
155
|
function filesUnder(root) {
|
|
19
156
|
const out = [];
|
|
20
157
|
const walk = (dir) => {
|
|
@@ -27,8 +164,17 @@ function filesUnder(root) {
|
|
|
27
164
|
walk(root);
|
|
28
165
|
return out;
|
|
29
166
|
}
|
|
30
|
-
/**
|
|
167
|
+
/**
|
|
168
|
+
* File identity is bytes. Size and mtime are only a fast negative check: a
|
|
169
|
+
* size mismatch or a mtime gap beyond the tolerance proves the pair cannot be
|
|
170
|
+
* byte-identical without reading both, but an equal size and close mtime still
|
|
171
|
+
* fall through to a byte comparison so content differences are never missed.
|
|
172
|
+
*/
|
|
31
173
|
function sameFile(a, b) {
|
|
174
|
+
const sourceStat = statSync(a);
|
|
175
|
+
const targetStat = statSync(b);
|
|
176
|
+
if (sourceStat.size !== targetStat.size) return false;
|
|
177
|
+
if (Math.abs(sourceStat.mtimeMs - targetStat.mtimeMs) > MTIME_TOLERANCE_MS) return false;
|
|
32
178
|
return readFileSync(a).equals(readFileSync(b));
|
|
33
179
|
}
|
|
34
180
|
/**
|
|
@@ -53,6 +199,12 @@ function pruneExtras(root, keep) {
|
|
|
53
199
|
} else dir = void 0;
|
|
54
200
|
}
|
|
55
201
|
}
|
|
202
|
+
/** Validate the synced preset's `agent.cordis.yml` artifact on disk. */
|
|
203
|
+
function validatePresetAgentFile(presetDir) {
|
|
204
|
+
const agent = join(presetDir, "agent.cordis.yml");
|
|
205
|
+
if (!existsSync(agent)) return ["agent.cordis.yml is missing from the preset tree"];
|
|
206
|
+
return validateAgentCordis(readFileSync(agent, "utf8"));
|
|
207
|
+
}
|
|
56
208
|
/** Copy `sourceRoot/<id>` into `targetRoot/<id>`, idempotently. */
|
|
57
209
|
function syncOnePreset(sourceDir, targetDir) {
|
|
58
210
|
const sourceFiles = filesUnder(sourceDir);
|
|
@@ -97,6 +249,11 @@ function syncOnePreset(sourceDir, targetDir) {
|
|
|
97
249
|
* target directories named in `retire` that the bundle no longer ships —
|
|
98
250
|
* preset ids the plugin once owned and later dropped. Only those exact ids
|
|
99
251
|
* are removed; every other target directory is left untouched.
|
|
252
|
+
*
|
|
253
|
+
* Each synced (or already-current) preset is validated against the structural
|
|
254
|
+
* `agent.cordis.yml` schema; a validation failure lands in `failed` so the
|
|
255
|
+
* caller can surface a broken preset as a first-class result instead of a
|
|
256
|
+
* warn-only log line.
|
|
100
257
|
* @param sourceRoot - plugin-owned preset tree (bundled in the package).
|
|
101
258
|
* @param targetRoot - dsh agent-presets discovery root (e.g. <home>/.dsh/.agent-presets).
|
|
102
259
|
* @param retire - previously bundled preset ids to remove when absent from the source.
|
|
@@ -113,8 +270,25 @@ function syncPresetTrees(sourceRoot, targetRoot, retire = []) {
|
|
|
113
270
|
const source = join(sourceRoot, entry);
|
|
114
271
|
if (!statSync(source).isDirectory()) continue;
|
|
115
272
|
const id = basename(source);
|
|
273
|
+
const targetDir = join(targetRoot, id);
|
|
274
|
+
let outcome;
|
|
275
|
+
try {
|
|
276
|
+
outcome = syncOnePreset(source, targetDir);
|
|
277
|
+
} catch (error) {
|
|
278
|
+
result.failed.push({
|
|
279
|
+
id,
|
|
280
|
+
error: error instanceof Error ? error.message : String(error)
|
|
281
|
+
});
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
116
284
|
try {
|
|
117
|
-
|
|
285
|
+
const problems = validatePresetAgentFile(targetDir);
|
|
286
|
+
if (problems.length > 0) result.failed.push({
|
|
287
|
+
id,
|
|
288
|
+
error: `agent.cordis.yml failed validation: ${problems.join("; ")}`
|
|
289
|
+
});
|
|
290
|
+
else if (outcome === "synced") result.synced.push(id);
|
|
291
|
+
else result.current.push(id);
|
|
118
292
|
} catch (error) {
|
|
119
293
|
result.failed.push({
|
|
120
294
|
id,
|
|
@@ -165,20 +339,7 @@ const DEFAULT_ANNOUNCE = true;
|
|
|
165
339
|
/** Order of the announcement section within the tool-guidance band. */
|
|
166
340
|
const SECTION_ORDER = 150;
|
|
167
341
|
/** Model-facing announcement: plugin presence, principle, and limits. */
|
|
168
|
-
const LIANGSHEN_GUIDANCE = "本机已安装 dsh-liangshen 插件(梁神模式 agent preset):新建会话的预设选择器中可选「梁神模式」。原理:两阶段锚定——首轮模型请求仅暴露官方 Minimal 精确双工具(持久 bash 与 str_replace_editor,文件工具继承宿主沙箱),只保留一行 persona,清空运行时上下文并只放行用户的直接消息,锚定 Minimal 推理轨迹;晋升受首块锚定门控(首块包含 we 且无 let me,四步兜底),无工具首轮会在响应后自动晋升,晋升后 wire 切换为 Code Mode(PTC,单一 run_code
|
|
169
|
-
/** Expand a leading `~`, `~/` or `~\` to the current user's home directory. */
|
|
170
|
-
function expandTilde(path) {
|
|
171
|
-
if (path === "~") return homedir();
|
|
172
|
-
if (path.startsWith("~/") || path.startsWith("~\\")) return join(homedir(), path.slice(2));
|
|
173
|
-
return path;
|
|
174
|
-
}
|
|
175
|
-
/** Resolve the harness home (DSH_HOME overrides the conventional ~/.dsh). */
|
|
176
|
-
function dshHome() {
|
|
177
|
-
const override = process.env.DSH_HOME;
|
|
178
|
-
if (override === void 0) return join(homedir(), ".dsh");
|
|
179
|
-
const trimmed = override.trim();
|
|
180
|
-
return trimmed === "" ? join(homedir(), ".dsh") : expandTilde(trimmed);
|
|
181
|
-
}
|
|
342
|
+
const LIANGSHEN_GUIDANCE = "本机已安装 dsh-liangshen 插件(梁神模式 agent preset):新建会话的预设选择器中可选「梁神模式」。原理:两阶段锚定——首轮模型请求仅暴露官方 Minimal 精确双工具(持久 bash 与 str_replace_editor,文件工具继承宿主沙箱),只保留一行 persona,清空运行时上下文并只放行用户的直接消息,锚定 Minimal 推理轨迹;晋升受首块锚定门控(首块包含 we 且无 let me,四步兜底),无工具首轮会在响应后自动晋升,晋升后 wire 切换为 Code Mode(PTC,单一 run_code)并在 persona 追加所选工作区路径,workspace 指令与 skill 目录在晋升后再延迟一步注入。preset 文件由插件维护于 ~/.dsh/.agent-presets,升级插件时自动更新;默认预设由用户自行选择。用户提到「梁神模式 / 锚定模式 / anchored standard」时即指本插件,请据此协作。";
|
|
182
343
|
/** Absolute path of the bundled preset tree inside this package. */
|
|
183
344
|
function bundledPresetsRoot() {
|
|
184
345
|
return fileURLToPath(new URL("../presets/", import.meta.url));
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSH_HOME resolution shared by the plugin family's Host halves: the
|
|
3
|
+
* environment override wins, the platform home fallback follows. Mirrors
|
|
4
|
+
* what dsh-pet and dsh-liangshen each used to implement locally.
|
|
5
|
+
*/
|
|
6
|
+
/** Expand a leading ~ (or ~user) in a path, platform-style. */
|
|
7
|
+
export declare function expandHome(path: string, home?: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Resolve the DSH home directory.
|
|
10
|
+
* @param env - process environment to read DSH_HOME from.
|
|
11
|
+
* @param home - platform home directory fallback (test seam).
|
|
12
|
+
* @returns the absolute DSH home path.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveDshHome(env?: NodeJS.ProcessEnv, home?: string): string;
|
|
15
|
+
/** Resolve the DSH home directory from the live environment. */
|
|
16
|
+
export declare function dshHome(): string;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -28,9 +28,8 @@ export interface Config {
|
|
|
28
28
|
}
|
|
29
29
|
export declare const Config: z<Config>;
|
|
30
30
|
/** Model-facing announcement: plugin presence, principle, and limits. */
|
|
31
|
-
export declare const LIANGSHEN_GUIDANCE = "\u672C\u673A\u5DF2\u5B89\u88C5 dsh-liangshen \u63D2\u4EF6\uFF08\u6881\u795E\u6A21\u5F0F agent preset\uFF09\uFF1A\u65B0\u5EFA\u4F1A\u8BDD\u7684\u9884\u8BBE\u9009\u62E9\u5668\u4E2D\u53EF\u9009\u300C\u6881\u795E\u6A21\u5F0F\u300D\u3002\u539F\u7406\uFF1A\u4E24\u9636\u6BB5\u951A\u5B9A\u2014\u2014\u9996\u8F6E\u6A21\u578B\u8BF7\u6C42\u4EC5\u66B4\u9732\u5B98\u65B9 Minimal \u7CBE\u786E\u53CC\u5DE5\u5177\uFF08\u6301\u4E45 bash \u4E0E str_replace_editor\uFF0C\u6587\u4EF6\u5DE5\u5177\u7EE7\u627F\u5BBF\u4E3B\u6C99\u7BB1\uFF09\uFF0C\u53EA\u4FDD\u7559\u4E00\u884C persona\uFF0C\u6E05\u7A7A\u8FD0\u884C\u65F6\u4E0A\u4E0B\u6587\u5E76\u53EA\u653E\u884C\u7528\u6237\u7684\u76F4\u63A5\u6D88\u606F\uFF0C\u951A\u5B9A Minimal \u63A8\u7406\u8F68\u8FF9\uFF1B\u664B\u5347\u53D7\u9996\u5757\u951A\u5B9A\u95E8\u63A7\uFF08\u9996\u5757\u5305\u542B we \u4E14\u65E0 let me\uFF0C\u56DB\u6B65\u515C\u5E95\uFF09\uFF0C\u65E0\u5DE5\u5177\u9996\u8F6E\u4F1A\u5728\u54CD\u5E94\u540E\u81EA\u52A8\u664B\u5347\uFF0C\u664B\u5347\u540E wire \u5207\u6362\u4E3A Code Mode\uFF08PTC\uFF0C\u5355\u4E00 run_code\uFF09\uFF0Cworkspace \u6307\u4EE4\u4E0E skill \u76EE\u5F55\u5728\u664B\u5347\u540E\u518D\u5EF6\u8FDF\u4E00\u6B65\u6CE8\u5165\u3002preset \u6587\u4EF6\u7531\u63D2\u4EF6\u7EF4\u62A4\u4E8E ~/.dsh/.agent-presets\uFF0C\u5347\u7EA7\u63D2\u4EF6\u65F6\u81EA\u52A8\u66F4\u65B0\uFF1B\u9ED8\u8BA4\u9884\u8BBE\u7531\u7528\u6237\u81EA\u884C\u9009\u62E9\u3002\u7528\u6237\u63D0\u5230\u300C\u6881\u795E\u6A21\u5F0F / \u951A\u5B9A\u6A21\u5F0F / anchored standard\u300D\u65F6\u5373\u6307\u672C\u63D2\u4EF6\uFF0C\u8BF7\u636E\u6B64\u534F\u4F5C\u3002";
|
|
32
|
-
|
|
33
|
-
export declare function dshHome(): string;
|
|
31
|
+
export declare const LIANGSHEN_GUIDANCE = "\u672C\u673A\u5DF2\u5B89\u88C5 dsh-liangshen \u63D2\u4EF6\uFF08\u6881\u795E\u6A21\u5F0F agent preset\uFF09\uFF1A\u65B0\u5EFA\u4F1A\u8BDD\u7684\u9884\u8BBE\u9009\u62E9\u5668\u4E2D\u53EF\u9009\u300C\u6881\u795E\u6A21\u5F0F\u300D\u3002\u539F\u7406\uFF1A\u4E24\u9636\u6BB5\u951A\u5B9A\u2014\u2014\u9996\u8F6E\u6A21\u578B\u8BF7\u6C42\u4EC5\u66B4\u9732\u5B98\u65B9 Minimal \u7CBE\u786E\u53CC\u5DE5\u5177\uFF08\u6301\u4E45 bash \u4E0E str_replace_editor\uFF0C\u6587\u4EF6\u5DE5\u5177\u7EE7\u627F\u5BBF\u4E3B\u6C99\u7BB1\uFF09\uFF0C\u53EA\u4FDD\u7559\u4E00\u884C persona\uFF0C\u6E05\u7A7A\u8FD0\u884C\u65F6\u4E0A\u4E0B\u6587\u5E76\u53EA\u653E\u884C\u7528\u6237\u7684\u76F4\u63A5\u6D88\u606F\uFF0C\u951A\u5B9A Minimal \u63A8\u7406\u8F68\u8FF9\uFF1B\u664B\u5347\u53D7\u9996\u5757\u951A\u5B9A\u95E8\u63A7\uFF08\u9996\u5757\u5305\u542B we \u4E14\u65E0 let me\uFF0C\u56DB\u6B65\u515C\u5E95\uFF09\uFF0C\u65E0\u5DE5\u5177\u9996\u8F6E\u4F1A\u5728\u54CD\u5E94\u540E\u81EA\u52A8\u664B\u5347\uFF0C\u664B\u5347\u540E wire \u5207\u6362\u4E3A Code Mode\uFF08PTC\uFF0C\u5355\u4E00 run_code\uFF09\u5E76\u5728 persona \u8FFD\u52A0\u6240\u9009\u5DE5\u4F5C\u533A\u8DEF\u5F84\uFF0Cworkspace \u6307\u4EE4\u4E0E skill \u76EE\u5F55\u5728\u664B\u5347\u540E\u518D\u5EF6\u8FDF\u4E00\u6B65\u6CE8\u5165\u3002preset \u6587\u4EF6\u7531\u63D2\u4EF6\u7EF4\u62A4\u4E8E ~/.dsh/.agent-presets\uFF0C\u5347\u7EA7\u63D2\u4EF6\u65F6\u81EA\u52A8\u66F4\u65B0\uFF1B\u9ED8\u8BA4\u9884\u8BBE\u7531\u7528\u6237\u81EA\u884C\u9009\u62E9\u3002\u7528\u6237\u63D0\u5230\u300C\u6881\u795E\u6A21\u5F0F / \u951A\u5B9A\u6A21\u5F0F / anchored standard\u300D\u65F6\u5373\u6307\u672C\u63D2\u4EF6\uFF0C\u8BF7\u636E\u6B64\u534F\u4F5C\u3002";
|
|
32
|
+
export { dshHome } from './dsh-home.ts';
|
|
34
33
|
/** Absolute path of the bundled preset tree inside this package. */
|
|
35
34
|
export declare function bundledPresetsRoot(): string;
|
|
36
35
|
/**
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural validation for a bundled `agent.cordis.yml`.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately dependency-free: it parses only the flat row metadata the sync
|
|
5
|
+
* and the dsh agent-presets loader rely on. Every top-level row is written as
|
|
6
|
+
* `- id: <id>` at column zero, with the `name`/`group`/`disabled` keys at two
|
|
7
|
+
* spaces of indentation. Nested `config:` and `isolate:` bodies are opaque to
|
|
8
|
+
* this validator — the dsh loader checks their semantics.
|
|
9
|
+
*
|
|
10
|
+
* Returns the list of problems found; an empty array means the document is
|
|
11
|
+
* structurally valid.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Validate the structural contract of an `agent.cordis.yml` document.
|
|
15
|
+
* @param text - the raw YAML document text.
|
|
16
|
+
* @returns a list of human-readable problems; empty means valid.
|
|
17
|
+
*/
|
|
18
|
+
export declare function validateAgentCordis(text: string): string[];
|
package/lib/types/sync.d.ts
CHANGED
|
@@ -8,6 +8,11 @@
|
|
|
8
8
|
* tree is copied and any target files the source does not contain are removed.
|
|
9
9
|
* Directories the plugin does not own (other presets the user authored) are
|
|
10
10
|
* never touched.
|
|
11
|
+
*
|
|
12
|
+
* After a preset is synced its `agent.cordis.yml` is validated against the
|
|
13
|
+
* structural preset schema; a validation failure is reported through the
|
|
14
|
+
* run's `failed` entries instead of being a warn-only side effect, so callers
|
|
15
|
+
* can observe (and surface) a broken preset rather than silently shipping it.
|
|
11
16
|
*/
|
|
12
17
|
/** One sync run's outcome, grouped for diagnostics. */
|
|
13
18
|
export interface SyncResult {
|
|
@@ -30,6 +35,11 @@ export declare function syncOnePreset(sourceDir: string, targetDir: string): 'sy
|
|
|
30
35
|
* target directories named in `retire` that the bundle no longer ships —
|
|
31
36
|
* preset ids the plugin once owned and later dropped. Only those exact ids
|
|
32
37
|
* are removed; every other target directory is left untouched.
|
|
38
|
+
*
|
|
39
|
+
* Each synced (or already-current) preset is validated against the structural
|
|
40
|
+
* `agent.cordis.yml` schema; a validation failure lands in `failed` so the
|
|
41
|
+
* caller can surface a broken preset as a first-class result instead of a
|
|
42
|
+
* warn-only log line.
|
|
33
43
|
* @param sourceRoot - plugin-owned preset tree (bundled in the package).
|
|
34
44
|
* @param targetRoot - dsh agent-presets discovery root (e.g. <home>/.dsh/.agent-presets).
|
|
35
45
|
* @param retire - previously bundled preset ids to remove when absent from the source.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@linxin666/dsh-liangshen",
|
|
3
3
|
"description": "LiangShen (梁神) agent preset for the dsh web GUI: a two-phase anchored-standard preset — the first model request exposes only the builtin Minimal exact two-tool surface (persistent bash plus str_replace_editor) with no runtime contexts or injected instructions (Minimal-aligned trajectory anchor), promotion is gated on the first minimal-like reasoning block with a fallback, and the wire then switches to Code Mode (PTC, a single run_code tool over the full registry) with the ordinary injections returning afterwards. The preset files are synced into ~/.dsh/.agent-presets on host startup. Hot-pluggable — mounted via cordis.patch.yml + a profile node_modules symlink, no dsh source changes.",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.16",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": "^22.19.0 || >=24.0.0"
|
|
@@ -31,9 +31,12 @@
|
|
|
31
31
|
# ── identity ────────────────────────────────────────────────────────────────
|
|
32
32
|
|
|
33
33
|
# Phase 1 shows only this one-line persona: `tool-bootstrap` filters the
|
|
34
|
-
# assembled sections down to
|
|
35
|
-
#
|
|
36
|
-
#
|
|
34
|
+
# assembled sections down to the persona section (`deployment:persona`, the
|
|
35
|
+
# name `dsh-persona` registers under, plus the legacy `persona` name) during
|
|
36
|
+
# phase 1 and restores every section after promotion, appending the session's
|
|
37
|
+
# working directory to the persona — so the agent knows its workspace and
|
|
38
|
+
# plan-mode's `plan:policy` takes effect instead of being permanently
|
|
39
|
+
# shadowed by a `complete` persona.
|
|
37
40
|
- id: persona
|
|
38
41
|
name: '@deepseek-ai/dsh-persona'
|
|
39
42
|
config:
|
|
@@ -49,7 +52,9 @@
|
|
|
49
52
|
# first turn ends (`turn/end`). After promotion the catalog is presented as
|
|
50
53
|
# Code Mode (PTC): a single `run_code` tool on the wire, switched at the step
|
|
51
54
|
# boundary; workspace instructions and the skill catalog are deferred one
|
|
52
|
-
# step after promotion.
|
|
55
|
+
# step after promotion. `bootstrapMaxTokens` caps the phase-1 request output
|
|
56
|
+
# budget (community-observed "We need" trigger window, 1024) and is stripped
|
|
57
|
+
# again after promotion so the cap never solders into later requests.
|
|
53
58
|
- id: tool-bootstrap
|
|
54
59
|
name: ./tool-bootstrap.mjs
|
|
55
60
|
config:
|
|
@@ -59,6 +64,7 @@
|
|
|
59
64
|
anchorGate: true
|
|
60
65
|
maxBootstrapSteps: 4
|
|
61
66
|
promoteAfterFirstResponse: true
|
|
67
|
+
bootstrapMaxTokens: 1024
|
|
62
68
|
deferredSources: [agent-instructions, skill-catalog]
|
|
63
69
|
deferredGraceSteps: 1
|
|
64
70
|
promotedPresentation: code
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Phase 1 (no persisted `tool/call` yet):
|
|
6
6
|
* - tool catalog: one platform shell plus `commonTools`
|
|
7
|
-
* - prompt sections: only the
|
|
7
|
+
* - prompt sections: only the persona section (all other sections,
|
|
8
8
|
* including plan-mode's `plan:policy`, return after promotion)
|
|
9
9
|
* - runtime contexts: emptied (no sandbox/approval snapshot)
|
|
10
10
|
* - pre-step messages: only explicit user messages pass
|
|
@@ -32,6 +32,26 @@ export const name = 'anchored-tool-bootstrap'
|
|
|
32
32
|
/** Prompt assembly and the tool registry must exist before this filter runs. */
|
|
33
33
|
export const inject = ['systemPrompt', 'tools']
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Prompt section names that carry the preset persona. The `dsh-persona` row
|
|
37
|
+
* registers the preset persona as `deployment:persona` (the PERSONA_SECTION
|
|
38
|
+
* name of `@deepseek-ai/dsh-system-prompt`), shadowing the deployment
|
|
39
|
+
* default for the preset scope; `persona` is the legacy name kept for older
|
|
40
|
+
* harnesses that registered the persona section without the prefix.
|
|
41
|
+
*/
|
|
42
|
+
const PERSONA_SECTION_NAMES = new Set(['deployment:persona', 'persona'])
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Workspace line a promoted persona gains. Phase 1 keeps the exact one-line
|
|
46
|
+
* persona (the Minimal anchor); after promotion the model must also know the
|
|
47
|
+
* session's selected workspace, which the Standard persona carries through
|
|
48
|
+
* the `{{cwd}}` prompt variable. The literal cwd is read from the session
|
|
49
|
+
* header at assembly time instead, so the line stays correct after a
|
|
50
|
+
* workspace switch and a session without a selected workspace keeps the bare
|
|
51
|
+
* one-liner rather than failing prompt interpolation.
|
|
52
|
+
*/
|
|
53
|
+
const WORKSPACE_LINE_PREFIX = '\n\nYour working directory is '
|
|
54
|
+
|
|
35
55
|
/** Message-source kinds the model may see during phase 1. */
|
|
36
56
|
const DEFAULT_MESSAGE_SOURCES = ['user']
|
|
37
57
|
|
|
@@ -200,6 +220,29 @@ function refresh(agent, policy) {
|
|
|
200
220
|
return state
|
|
201
221
|
}
|
|
202
222
|
|
|
223
|
+
/**
|
|
224
|
+
* Append the session's working directory to the persona section of a promoted
|
|
225
|
+
* assembly. Returns the assembly unchanged when there is no persona section,
|
|
226
|
+
* no selected workspace, or the exact line is already present.
|
|
227
|
+
*/
|
|
228
|
+
function withWorkspaceLine(assembly, agent) {
|
|
229
|
+
const cwd = agent?.session?.header?.cwd
|
|
230
|
+
if (typeof cwd !== 'string' || cwd.length === 0) return assembly
|
|
231
|
+
if (!Array.isArray(assembly.sections)) return assembly
|
|
232
|
+
const line = `${WORKSPACE_LINE_PREFIX}${cwd}.`
|
|
233
|
+
const persona = assembly.sections.find(section =>
|
|
234
|
+
PERSONA_SECTION_NAMES.has(section?.name)
|
|
235
|
+
&& typeof section?.text === 'string'
|
|
236
|
+
&& !section.text.includes(line))
|
|
237
|
+
if (persona === undefined) return assembly
|
|
238
|
+
return {
|
|
239
|
+
...assembly,
|
|
240
|
+
sections: assembly.sections.map(section => section === persona
|
|
241
|
+
? { ...section, text: `${persona.text}${line}` }
|
|
242
|
+
: section),
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
203
246
|
/** Register the per-session bootstrap quarantine and promotion policy. */
|
|
204
247
|
export function apply(ctx, config) {
|
|
205
248
|
const commonTools = stringList(config.commonTools, 'commonTools')
|
|
@@ -210,12 +253,16 @@ export function apply(ctx, config) {
|
|
|
210
253
|
if (presentation !== 'native' && presentation !== 'code') {
|
|
211
254
|
throw new TypeError(`${name}: promotedPresentation must be "native" or "code"`)
|
|
212
255
|
}
|
|
256
|
+
const bootstrapMaxTokens = config.bootstrapMaxTokens === undefined
|
|
257
|
+
? undefined
|
|
258
|
+
: integerAtLeast(config.bootstrapMaxTokens, 'bootstrapMaxTokens', 1)
|
|
213
259
|
const policy = {
|
|
214
260
|
anchorGate: config.anchorGate === true,
|
|
215
261
|
promoteAfterFirstResponse: config.promoteAfterFirstResponse === true,
|
|
216
262
|
maxBootstrapSteps: integerAtLeast(config.maxBootstrapSteps ?? 4, 'maxBootstrapSteps', 1),
|
|
217
263
|
deferredGraceSteps: integerAtLeast(config.deferredGraceSteps ?? 0, 'deferredGraceSteps', 0),
|
|
218
264
|
promotedPresentation: presentation,
|
|
265
|
+
bootstrapMaxTokens,
|
|
219
266
|
}
|
|
220
267
|
|
|
221
268
|
// Promotion is applied at step/turn boundaries, never while a step is still
|
|
@@ -245,7 +292,7 @@ export function apply(ctx, config) {
|
|
|
245
292
|
const agent = context.agent
|
|
246
293
|
if (agent === undefined) return assembled
|
|
247
294
|
const state = refresh(agent, policy)
|
|
248
|
-
if (state.promoted) return assembled
|
|
295
|
+
if (state.promoted) return withWorkspaceLine(assembled, agent)
|
|
249
296
|
|
|
250
297
|
const available = new Set(assembled.tools.map(tool => tool.name))
|
|
251
298
|
const selectedShells = shellTools.filter(toolName => available.has(toolName))
|
|
@@ -263,7 +310,7 @@ export function apply(ctx, config) {
|
|
|
263
310
|
tools: assembled.tools.filter(tool => bootstrap.has(tool.name)),
|
|
264
311
|
contexts: [],
|
|
265
312
|
...(Array.isArray(assembled.sections)
|
|
266
|
-
? { sections: assembled.sections.filter(section => section?.name
|
|
313
|
+
? { sections: assembled.sections.filter(section => PERSONA_SECTION_NAMES.has(section?.name)) }
|
|
267
314
|
: {}),
|
|
268
315
|
}
|
|
269
316
|
}, { prepend: true })
|
|
@@ -290,4 +337,24 @@ export function apply(ctx, config) {
|
|
|
290
337
|
}
|
|
291
338
|
return decision
|
|
292
339
|
}, { prepend: true })
|
|
340
|
+
|
|
341
|
+
// Phase 1 caps the next request output budget to bootstrapMaxTokens, the
|
|
342
|
+
// community-observed We-need trigger window (dsh-anchored-standard issue 6),
|
|
343
|
+
// and strips the cap again after promotion. The strip is mandatory:
|
|
344
|
+
// requestProposal(persistedHeader) carries a plain maxTokens from the
|
|
345
|
+
// previous header into the next request unless the adapter marked it a
|
|
346
|
+
// default, so an un-stripped cap would be soldered into every request.
|
|
347
|
+
ctx.on('agent/request', async (payload, next) => {
|
|
348
|
+
const resolved = await next()
|
|
349
|
+
const agent = payload?.agent
|
|
350
|
+
if (agent === undefined || policy.bootstrapMaxTokens === undefined) return resolved
|
|
351
|
+
const state = refresh(agent, policy)
|
|
352
|
+
if (state.promoted) {
|
|
353
|
+
if (resolved.maxTokens !== policy.bootstrapMaxTokens) return resolved
|
|
354
|
+
const rest = { ...resolved }
|
|
355
|
+
delete rest.maxTokens
|
|
356
|
+
return rest
|
|
357
|
+
}
|
|
358
|
+
return { ...resolved, maxTokens: policy.bootstrapMaxTokens }
|
|
359
|
+
}, { prepend: true })
|
|
293
360
|
}
|
package/src/dsh-home.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Generated by scripts/sync-shared.mjs from shared/host/dsh-home.ts. Do not edit this copy; edit the shared source and run "node scripts/sync-shared.mjs".
|
|
2
|
+
/**
|
|
3
|
+
* DSH_HOME resolution shared by the plugin family's Host halves: the
|
|
4
|
+
* environment override wins, the platform home fallback follows. Mirrors
|
|
5
|
+
* what dsh-pet and dsh-liangshen each used to implement locally.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { homedir } from 'node:os'
|
|
9
|
+
import { isAbsolute, join } from 'node:path'
|
|
10
|
+
|
|
11
|
+
/** Expand a leading ~ (or ~user) in a path, platform-style. */
|
|
12
|
+
export function expandHome(path: string, home: string = homedir()): string {
|
|
13
|
+
if (path === '~') return home
|
|
14
|
+
if (path.startsWith('~/') || path.startsWith('~\\')) return join(home, path.slice(2))
|
|
15
|
+
return path
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolve the DSH home directory.
|
|
20
|
+
* @param env - process environment to read DSH_HOME from.
|
|
21
|
+
* @param home - platform home directory fallback (test seam).
|
|
22
|
+
* @returns the absolute DSH home path.
|
|
23
|
+
*/
|
|
24
|
+
export function resolveDshHome(env: NodeJS.ProcessEnv = process.env, home: string = homedir()): string {
|
|
25
|
+
const raw = env.DSH_HOME
|
|
26
|
+
if (raw !== undefined && raw.trim() !== '') {
|
|
27
|
+
const expanded = expandHome(raw.trim(), home)
|
|
28
|
+
return isAbsolute(expanded) ? expanded : join(process.cwd(), expanded)
|
|
29
|
+
}
|
|
30
|
+
return join(home, '.dsh')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Resolve the DSH home directory from the live environment. */
|
|
34
|
+
export function dshHome(): string {
|
|
35
|
+
return resolveDshHome()
|
|
36
|
+
}
|
package/src/index.test.ts
CHANGED
|
@@ -58,4 +58,10 @@ describe('dshHome', () => {
|
|
|
58
58
|
expect(dshHome()).toBe('/srv/dsh-home')
|
|
59
59
|
})
|
|
60
60
|
})
|
|
61
|
+
|
|
62
|
+
it('resolves a relative override against the process cwd (shared contract)', () => {
|
|
63
|
+
withEnv({ DSH_HOME: 'data/home' }, () => {
|
|
64
|
+
expect(dshHome()).toBe(join(process.cwd(), 'data', 'home'))
|
|
65
|
+
})
|
|
66
|
+
})
|
|
61
67
|
})
|
package/src/index.ts
CHANGED
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { existsSync, mkdirSync } from 'node:fs'
|
|
18
|
-
import { homedir } from 'node:os'
|
|
19
18
|
import { join } from 'node:path'
|
|
20
19
|
import { fileURLToPath } from 'node:url'
|
|
21
20
|
import type { Context } from '@deepseek-ai/cordis'
|
|
22
21
|
import type {} from '@deepseek-ai/dsh-system-prompt'
|
|
23
22
|
import z from 'schemastery'
|
|
23
|
+
import { dshHome } from './dsh-home.ts'
|
|
24
24
|
import { syncPresetTrees } from './sync.ts'
|
|
25
25
|
|
|
26
26
|
/** Stable cordis plugin name. */
|
|
@@ -49,22 +49,13 @@ const DEFAULT_ANNOUNCE = true
|
|
|
49
49
|
const SECTION_ORDER = 150
|
|
50
50
|
|
|
51
51
|
/** Model-facing announcement: plugin presence, principle, and limits. */
|
|
52
|
-
export const LIANGSHEN_GUIDANCE = '本机已安装 dsh-liangshen 插件(梁神模式 agent preset):新建会话的预设选择器中可选「梁神模式」。原理:两阶段锚定——首轮模型请求仅暴露官方 Minimal 精确双工具(持久 bash 与 str_replace_editor,文件工具继承宿主沙箱),只保留一行 persona,清空运行时上下文并只放行用户的直接消息,锚定 Minimal 推理轨迹;晋升受首块锚定门控(首块包含 we 且无 let me,四步兜底),无工具首轮会在响应后自动晋升,晋升后 wire 切换为 Code Mode(PTC,单一 run_code
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** Resolve the harness home (DSH_HOME overrides the conventional ~/.dsh). */
|
|
62
|
-
export function dshHome(): string {
|
|
63
|
-
const override = process.env.DSH_HOME
|
|
64
|
-
if (override === undefined) return join(homedir(), '.dsh')
|
|
65
|
-
const trimmed = override.trim()
|
|
66
|
-
return trimmed === '' ? join(homedir(), '.dsh') : expandTilde(trimmed)
|
|
67
|
-
}
|
|
52
|
+
export const LIANGSHEN_GUIDANCE = '本机已安装 dsh-liangshen 插件(梁神模式 agent preset):新建会话的预设选择器中可选「梁神模式」。原理:两阶段锚定——首轮模型请求仅暴露官方 Minimal 精确双工具(持久 bash 与 str_replace_editor,文件工具继承宿主沙箱),只保留一行 persona,清空运行时上下文并只放行用户的直接消息,锚定 Minimal 推理轨迹;晋升受首块锚定门控(首块包含 we 且无 let me,四步兜底),无工具首轮会在响应后自动晋升,晋升后 wire 切换为 Code Mode(PTC,单一 run_code)并在 persona 追加所选工作区路径,workspace 指令与 skill 目录在晋升后再延迟一步注入。preset 文件由插件维护于 ~/.dsh/.agent-presets,升级插件时自动更新;默认预设由用户自行选择。用户提到「梁神模式 / 锚定模式 / anchored standard」时即指本插件,请据此协作。'
|
|
53
|
+
// The harness-home resolution (DSH_HOME override with the platform-home
|
|
54
|
+
// fallback and ~ expansion) lives in the family-shared copy ./dsh-home.ts.
|
|
55
|
+
// Re-export it so the plugin surface stays stable while the implementation is
|
|
56
|
+
// shared across packages. A relative DSH_HOME resolves against the process CWD
|
|
57
|
+
// (absolute), which is the shared contract.
|
|
58
|
+
export { dshHome } from './dsh-home.ts'
|
|
68
59
|
|
|
69
60
|
/** Absolute path of the bundled preset tree inside this package. */
|
|
70
61
|
export function bundledPresetsRoot(): string {
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { validateAgentCordis } from './schema.ts'
|
|
3
|
+
|
|
4
|
+
const VALID = [
|
|
5
|
+
"- id: persona",
|
|
6
|
+
" name: '@deepseek-ai/dsh-persona'",
|
|
7
|
+
"- id: bootstrap",
|
|
8
|
+
" name: ./tool-bootstrap.mjs",
|
|
9
|
+
"- id: shell-group",
|
|
10
|
+
" name: cordis:group",
|
|
11
|
+
" group: true",
|
|
12
|
+
" isolate:",
|
|
13
|
+
" terminals: true",
|
|
14
|
+
" config:",
|
|
15
|
+
" - id: pty",
|
|
16
|
+
" name: '@deepseek-ai/dsh-terminal'",
|
|
17
|
+
].join('\n')
|
|
18
|
+
|
|
19
|
+
describe('validateAgentCordis', () => {
|
|
20
|
+
it('accepts a well-formed preset document', () => {
|
|
21
|
+
expect(validateAgentCordis(VALID)).toEqual([])
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('rejects an empty document', () => {
|
|
25
|
+
expect(validateAgentCordis('')).not.toEqual([])
|
|
26
|
+
expect(validateAgentCordis('\n \n')).not.toEqual([])
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('rejects a document that is not a list of rows', () => {
|
|
30
|
+
expect(validateAgentCordis('rows: []\n')).not.toEqual([])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('rejects a row without a name', () => {
|
|
34
|
+
const doc = "- id: persona\n group: true\n"
|
|
35
|
+
expect(validateAgentCordis(doc)).not.toEqual([])
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('rejects a row whose name starts with an unsupported prefix', () => {
|
|
39
|
+
const doc = "- id: bad\n name: 'plain-text'\n"
|
|
40
|
+
expect(validateAgentCordis(doc)).not.toEqual([])
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('rejects duplicate row ids', () => {
|
|
44
|
+
const doc = [
|
|
45
|
+
"- id: persona",
|
|
46
|
+
" name: '@deepseek-ai/dsh-persona'",
|
|
47
|
+
"- id: persona",
|
|
48
|
+
" name: '@deepseek-ai/dsh-tool-todo'",
|
|
49
|
+
].join('\n')
|
|
50
|
+
const errors = validateAgentCordis(doc)
|
|
51
|
+
expect(errors.some(error => error.includes('duplicate row id'))).toBe(true)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('rejects a group-true row that does not use cordis:group', () => {
|
|
55
|
+
const doc = "- id: shell-group\n name: '@deepseek-ai/dsh-persona'\n group: true\n"
|
|
56
|
+
expect(validateAgentCordis(doc)).not.toEqual([])
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('rejects a row with a blank id', () => {
|
|
60
|
+
const doc = "- id: \n"
|
|
61
|
+
const errors = validateAgentCordis(doc)
|
|
62
|
+
expect(errors.some(error => error.includes('empty row id'))).toBe(true)
|
|
63
|
+
})
|
|
64
|
+
})
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural validation for a bundled `agent.cordis.yml`.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately dependency-free: it parses only the flat row metadata the sync
|
|
5
|
+
* and the dsh agent-presets loader rely on. Every top-level row is written as
|
|
6
|
+
* `- id: <id>` at column zero, with the `name`/`group`/`disabled` keys at two
|
|
7
|
+
* spaces of indentation. Nested `config:` and `isolate:` bodies are opaque to
|
|
8
|
+
* this validator — the dsh loader checks their semantics.
|
|
9
|
+
*
|
|
10
|
+
* Returns the list of problems found; an empty array means the document is
|
|
11
|
+
* structurally valid.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** A top-level row opener: `- id: <id>` (id may be blank for diagnostics). */
|
|
15
|
+
const ROW_RE = /^-\s+id:\s*(.*)$/
|
|
16
|
+
/** Any top-level list item, for ids missing from a row opener. */
|
|
17
|
+
const ITEM_RE = /^-\s/
|
|
18
|
+
/** A two-space-indented flat metadata key: ` name: <value>`. */
|
|
19
|
+
const META_RE = /^ {2}([A-Za-z][A-Za-z0-9_-]*):\s*(.*)$/
|
|
20
|
+
/** The only `name` forms the dsh agent-presets loader mounts from a row. */
|
|
21
|
+
const NAME_PREFIX_RE = /^(\.\/|@|cordis:)/
|
|
22
|
+
|
|
23
|
+
/** Strip one pair of surrounding single or double quotes from a scalar. */
|
|
24
|
+
function unquote(value: string): string {
|
|
25
|
+
if (value.length >= 2) {
|
|
26
|
+
const first = value[0]
|
|
27
|
+
if ((first === "'" || first === '"') && value.endsWith(first)) {
|
|
28
|
+
return value.slice(1, -1)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return value
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Validate the structural contract of an `agent.cordis.yml` document.
|
|
36
|
+
* @param text - the raw YAML document text.
|
|
37
|
+
* @returns a list of human-readable problems; empty means valid.
|
|
38
|
+
*/
|
|
39
|
+
export function validateAgentCordis(text: string): string[] {
|
|
40
|
+
const errors: string[] = []
|
|
41
|
+
const normalized = text.replace(/\r\n/g, '\n')
|
|
42
|
+
if (normalized.trim() === '') {
|
|
43
|
+
return ['document is empty']
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const seenIds = new Set<string>()
|
|
47
|
+
const current = { id: null as string | null, name: null as string | null, group: null as string | null }
|
|
48
|
+
|
|
49
|
+
const closeRow = (): void => {
|
|
50
|
+
if (current.id === null) return
|
|
51
|
+
if (current.name === null) {
|
|
52
|
+
errors.push(`row "${current.id}": missing "name" key`)
|
|
53
|
+
} else if (!NAME_PREFIX_RE.test(current.name)) {
|
|
54
|
+
errors.push(`row "${current.id}": name "${current.name}" must start with "./", "@" or "cordis:"`)
|
|
55
|
+
}
|
|
56
|
+
if (current.group === 'true' && current.name !== 'cordis:group') {
|
|
57
|
+
errors.push(`row "${current.id}": "group: true" requires name "cordis:group"`)
|
|
58
|
+
}
|
|
59
|
+
current.name = null
|
|
60
|
+
current.group = null
|
|
61
|
+
current.id = null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const lines = normalized.split('\n')
|
|
65
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
66
|
+
const lineNo = index + 1
|
|
67
|
+
const line = lines[index]
|
|
68
|
+
const trimmed = line.trim()
|
|
69
|
+
if (trimmed === '' || trimmed.startsWith('#')) continue
|
|
70
|
+
|
|
71
|
+
const row = ROW_RE.exec(line)
|
|
72
|
+
if (row !== null) {
|
|
73
|
+
closeRow()
|
|
74
|
+
const id = row[1].trim()
|
|
75
|
+
if (id === '') {
|
|
76
|
+
errors.push(`line ${lineNo}: empty row id`)
|
|
77
|
+
current.id = null
|
|
78
|
+
} else {
|
|
79
|
+
if (seenIds.has(id)) errors.push(`line ${lineNo}: duplicate row id "${id}"`)
|
|
80
|
+
seenIds.add(id)
|
|
81
|
+
current.id = id
|
|
82
|
+
}
|
|
83
|
+
current.name = null
|
|
84
|
+
current.group = null
|
|
85
|
+
continue
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (current.id === null) {
|
|
89
|
+
if (ITEM_RE.test(line)) {
|
|
90
|
+
errors.push(`line ${lineNo}: list item does not declare an "id:"`)
|
|
91
|
+
} else if (/^\S/.test(line)) {
|
|
92
|
+
errors.push(`line ${lineNo}: content outside a "- id:" row`)
|
|
93
|
+
}
|
|
94
|
+
continue
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const meta = META_RE.exec(line)
|
|
98
|
+
if (meta !== null) {
|
|
99
|
+
const value = unquote(meta[2].trim())
|
|
100
|
+
if (meta[1] === 'name') current.name = value
|
|
101
|
+
else if (meta[1] === 'group') current.group = value
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
// Anything indented at least two spaces is a nested body, opaque here.
|
|
105
|
+
if (/^ {2}/.test(line)) continue
|
|
106
|
+
errors.push(`line ${lineNo}: unexpected content in row "${current.id}"`)
|
|
107
|
+
}
|
|
108
|
+
closeRow()
|
|
109
|
+
return errors
|
|
110
|
+
}
|
package/src/sync.test.ts
CHANGED
|
@@ -2,14 +2,17 @@ import { describe, expect, it } from 'vitest'
|
|
|
2
2
|
import { existsSync, mkdirSync, mkdtempSync, rmSync, statSync, utimesSync, writeFileSync, readFileSync } from 'node:fs'
|
|
3
3
|
import { tmpdir } from 'node:os'
|
|
4
4
|
import { join } from 'node:path'
|
|
5
|
-
import { syncPresetTrees } from './sync.ts'
|
|
5
|
+
import { syncOnePreset, syncPresetTrees } from './sync.ts'
|
|
6
|
+
|
|
7
|
+
/** Minimal structurally valid agent.cordis.yml used by the sync fixtures. */
|
|
8
|
+
const VALID_AGENT_YAML = "- id: persona\n name: '@deepseek-ai/dsh-persona'\n"
|
|
6
9
|
|
|
7
10
|
function fixture(): { source: string; target: string; dispose: () => void } {
|
|
8
11
|
const base = mkdtempSync(join(tmpdir(), 'dsh-liangshen-'))
|
|
9
12
|
const source = join(base, 'presets')
|
|
10
13
|
const target = join(base, 'agent-presets')
|
|
11
14
|
mkdirSync(join(source, 'liangshen'), { recursive: true })
|
|
12
|
-
writeFileSync(join(source, 'liangshen', 'agent.cordis.yml'),
|
|
15
|
+
writeFileSync(join(source, 'liangshen', 'agent.cordis.yml'), VALID_AGENT_YAML)
|
|
13
16
|
writeFileSync(join(source, 'liangshen', 'tool-bootstrap.mjs'), 'export const name = "x"\n')
|
|
14
17
|
writeFileSync(join(source, 'liangshen', 'preset.yml'), 'name: 梁神模式\n')
|
|
15
18
|
return { source, target, dispose: () => rmSync(base, { recursive: true, force: true }) }
|
|
@@ -45,7 +48,7 @@ describe('syncPresetTrees', () => {
|
|
|
45
48
|
writeFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'changed\n')
|
|
46
49
|
const third = syncPresetTrees(f.source, f.target)
|
|
47
50
|
expect(third.synced).toEqual(['liangshen'])
|
|
48
|
-
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(
|
|
51
|
+
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
49
52
|
} finally { f.dispose() }
|
|
50
53
|
})
|
|
51
54
|
|
|
@@ -54,7 +57,7 @@ describe('syncPresetTrees', () => {
|
|
|
54
57
|
try {
|
|
55
58
|
syncPresetTrees(f.source, f.target)
|
|
56
59
|
mkdirSync(join(f.target, 'liangshen-exact'), { recursive: true })
|
|
57
|
-
writeFileSync(join(f.target, 'liangshen-exact', 'agent.cordis.yml'),
|
|
60
|
+
writeFileSync(join(f.target, 'liangshen-exact', 'agent.cordis.yml'), VALID_AGENT_YAML)
|
|
58
61
|
const result = syncPresetTrees(f.source, f.target, ['liangshen-exact'])
|
|
59
62
|
expect(result.retired).toEqual(['liangshen-exact'])
|
|
60
63
|
expect(existsSync(join(f.target, 'liangshen-exact'))).toBe(false)
|
|
@@ -82,7 +85,7 @@ describe('syncPresetTrees', () => {
|
|
|
82
85
|
const second = syncPresetTrees(f.source, f.target)
|
|
83
86
|
expect(second.synced).toEqual(['liangshen'])
|
|
84
87
|
expect(existsSync(join(f.target, 'liangshen', 'tool-bootstrap.mjs'))).toBe(false)
|
|
85
|
-
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(
|
|
88
|
+
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
86
89
|
} finally { f.dispose() }
|
|
87
90
|
})
|
|
88
91
|
|
|
@@ -92,14 +95,16 @@ describe('syncPresetTrees', () => {
|
|
|
92
95
|
syncPresetTrees(f.source, f.target)
|
|
93
96
|
const source = join(f.source, 'liangshen', 'agent.cordis.yml')
|
|
94
97
|
const dest = join(f.target, 'liangshen', 'agent.cordis.yml')
|
|
95
|
-
|
|
98
|
+
// Same byte length, different content, so only the byte compare catches it.
|
|
99
|
+
const sourceText = readFileSync(source, 'utf8')
|
|
100
|
+
writeFileSync(dest, sourceText.replace('persona', 'parsena'))
|
|
96
101
|
const stat = statSync(source)
|
|
97
102
|
utimesSync(dest, stat.atime, stat.mtime)
|
|
98
103
|
expect(statSync(dest).size).toBe(stat.size)
|
|
99
104
|
expect(Math.abs(statSync(dest).mtimeMs - stat.mtimeMs)).toBeLessThan(1)
|
|
100
105
|
const second = syncPresetTrees(f.source, f.target)
|
|
101
106
|
expect(second.synced).toEqual(['liangshen'])
|
|
102
|
-
expect(readFileSync(dest, 'utf8')).toBe(
|
|
107
|
+
expect(readFileSync(dest, 'utf8')).toBe(VALID_AGENT_YAML)
|
|
103
108
|
} finally { f.dispose() }
|
|
104
109
|
})
|
|
105
110
|
|
|
@@ -112,7 +117,7 @@ describe('syncPresetTrees', () => {
|
|
|
112
117
|
const second = syncPresetTrees(f.source, f.target)
|
|
113
118
|
expect(second.synced).toEqual(['liangshen'])
|
|
114
119
|
expect(existsSync(join(f.target, 'liangshen', 'extra'))).toBe(false)
|
|
115
|
-
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(
|
|
120
|
+
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
116
121
|
} finally { f.dispose() }
|
|
117
122
|
})
|
|
118
123
|
|
|
@@ -136,3 +141,67 @@ describe('syncPresetTrees', () => {
|
|
|
136
141
|
} finally { f.dispose() }
|
|
137
142
|
})
|
|
138
143
|
})
|
|
144
|
+
|
|
145
|
+
describe('mtime fast path', () => {
|
|
146
|
+
it('re-syncs a byte-identical file whose mtime drifted beyond tolerance', () => {
|
|
147
|
+
const f = fixture()
|
|
148
|
+
try {
|
|
149
|
+
syncPresetTrees(f.source, f.target)
|
|
150
|
+
const dest = join(f.target, 'liangshen', 'agent.cordis.yml')
|
|
151
|
+
const stat = statSync(dest)
|
|
152
|
+
// Bump mtime a day into the future, keep the bytes identical.
|
|
153
|
+
utimesSync(dest, stat.atime, new Date(stat.mtimeMs + 86400_000))
|
|
154
|
+
const second = syncPresetTrees(f.source, f.target)
|
|
155
|
+
expect(second.synced).toEqual(['liangshen'])
|
|
156
|
+
expect(second.current).toEqual([])
|
|
157
|
+
expect(readFileSync(dest, 'utf8')).toBe(VALID_AGENT_YAML)
|
|
158
|
+
} finally { f.dispose() }
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('re-syncs a target file with a different size without reading it twice', () => {
|
|
162
|
+
const f = fixture()
|
|
163
|
+
try {
|
|
164
|
+
syncPresetTrees(f.source, f.target)
|
|
165
|
+
writeFileSync(join(f.target, 'liangshen', 'preset.yml'), 'name: different\n')
|
|
166
|
+
const second = syncPresetTrees(f.source, f.target)
|
|
167
|
+
expect(second.synced).toEqual(['liangshen'])
|
|
168
|
+
} finally { f.dispose() }
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
describe('agent.cordis.yml validation after sync', () => {
|
|
173
|
+
it('reports an invalid bundled preset as failed instead of synced', () => {
|
|
174
|
+
const f = fixture()
|
|
175
|
+
try {
|
|
176
|
+
writeFileSync(join(f.source, 'liangshen', 'agent.cordis.yml'), 'rows: []\n')
|
|
177
|
+
const result = syncPresetTrees(f.source, f.target)
|
|
178
|
+
expect(result.synced).toEqual([])
|
|
179
|
+
expect(result.current).toEqual([])
|
|
180
|
+
expect(result.failed).toHaveLength(1)
|
|
181
|
+
expect(result.failed[0].id).toBe('liangshen')
|
|
182
|
+
expect(result.failed[0].error).toContain('failed validation')
|
|
183
|
+
} finally { f.dispose() }
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it('validates a preset again on an idempotent run and still reports current', () => {
|
|
187
|
+
const f = fixture()
|
|
188
|
+
try {
|
|
189
|
+
syncPresetTrees(f.source, f.target)
|
|
190
|
+
const second = syncPresetTrees(f.source, f.target)
|
|
191
|
+
expect(second.current).toEqual(['liangshen'])
|
|
192
|
+
expect(second.failed).toEqual([])
|
|
193
|
+
} finally { f.dispose() }
|
|
194
|
+
})
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
describe('syncOnePreset', () => {
|
|
198
|
+
it('copies once and stays current on identical bytes', () => {
|
|
199
|
+
const f = fixture()
|
|
200
|
+
try {
|
|
201
|
+
const source = join(f.source, 'liangshen')
|
|
202
|
+
const target = join(f.target, 'liangshen')
|
|
203
|
+
expect(syncOnePreset(source, target)).toBe('synced')
|
|
204
|
+
expect(syncOnePreset(source, target)).toBe('current')
|
|
205
|
+
} finally { f.dispose() }
|
|
206
|
+
})
|
|
207
|
+
})
|
package/src/sync.ts
CHANGED
|
@@ -8,10 +8,24 @@
|
|
|
8
8
|
* tree is copied and any target files the source does not contain are removed.
|
|
9
9
|
* Directories the plugin does not own (other presets the user authored) are
|
|
10
10
|
* never touched.
|
|
11
|
+
*
|
|
12
|
+
* After a preset is synced its `agent.cordis.yml` is validated against the
|
|
13
|
+
* structural preset schema; a validation failure is reported through the
|
|
14
|
+
* run's `failed` entries instead of being a warn-only side effect, so callers
|
|
15
|
+
* can observe (and surface) a broken preset rather than silently shipping it.
|
|
11
16
|
*/
|
|
12
17
|
|
|
13
18
|
import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs'
|
|
14
19
|
import { basename, dirname, join, relative } from 'node:path'
|
|
20
|
+
import { validateAgentCordis } from './schema.ts'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Clock/coarse-grain tolerance for the mtime fast path. When a source and a
|
|
24
|
+
* target file share a size and a near-identical mtime we still fall through to
|
|
25
|
+
* a byte comparison; a mtime gap beyond this simply proves the pair cannot be
|
|
26
|
+
* byte-identical, so we skip the read.
|
|
27
|
+
*/
|
|
28
|
+
const MTIME_TOLERANCE_MS = 1000
|
|
15
29
|
|
|
16
30
|
/** One sync run's outcome, grouped for diagnostics. */
|
|
17
31
|
export interface SyncResult {
|
|
@@ -38,8 +52,17 @@ function filesUnder(root: string): string[] {
|
|
|
38
52
|
return out
|
|
39
53
|
}
|
|
40
54
|
|
|
41
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* File identity is bytes. Size and mtime are only a fast negative check: a
|
|
57
|
+
* size mismatch or a mtime gap beyond the tolerance proves the pair cannot be
|
|
58
|
+
* byte-identical without reading both, but an equal size and close mtime still
|
|
59
|
+
* fall through to a byte comparison so content differences are never missed.
|
|
60
|
+
*/
|
|
42
61
|
function sameFile(a: string, b: string): boolean {
|
|
62
|
+
const sourceStat = statSync(a)
|
|
63
|
+
const targetStat = statSync(b)
|
|
64
|
+
if (sourceStat.size !== targetStat.size) return false
|
|
65
|
+
if (Math.abs(sourceStat.mtimeMs - targetStat.mtimeMs) > MTIME_TOLERANCE_MS) return false
|
|
43
66
|
return readFileSync(a).equals(readFileSync(b))
|
|
44
67
|
}
|
|
45
68
|
|
|
@@ -69,6 +92,13 @@ function pruneExtras(root: string, keep: ReadonlySet<string>): void {
|
|
|
69
92
|
}
|
|
70
93
|
}
|
|
71
94
|
|
|
95
|
+
/** Validate the synced preset's `agent.cordis.yml` artifact on disk. */
|
|
96
|
+
function validatePresetAgentFile(presetDir: string): string[] {
|
|
97
|
+
const agent = join(presetDir, 'agent.cordis.yml')
|
|
98
|
+
if (!existsSync(agent)) return ['agent.cordis.yml is missing from the preset tree']
|
|
99
|
+
return validateAgentCordis(readFileSync(agent, 'utf8'))
|
|
100
|
+
}
|
|
101
|
+
|
|
72
102
|
/** Copy `sourceRoot/<id>` into `targetRoot/<id>`, idempotently. */
|
|
73
103
|
export function syncOnePreset(sourceDir: string, targetDir: string): 'synced' | 'current' {
|
|
74
104
|
const sourceFiles = filesUnder(sourceDir)
|
|
@@ -114,6 +144,11 @@ export function syncOnePreset(sourceDir: string, targetDir: string): 'synced' |
|
|
|
114
144
|
* target directories named in `retire` that the bundle no longer ships —
|
|
115
145
|
* preset ids the plugin once owned and later dropped. Only those exact ids
|
|
116
146
|
* are removed; every other target directory is left untouched.
|
|
147
|
+
*
|
|
148
|
+
* Each synced (or already-current) preset is validated against the structural
|
|
149
|
+
* `agent.cordis.yml` schema; a validation failure lands in `failed` so the
|
|
150
|
+
* caller can surface a broken preset as a first-class result instead of a
|
|
151
|
+
* warn-only log line.
|
|
117
152
|
* @param sourceRoot - plugin-owned preset tree (bundled in the package).
|
|
118
153
|
* @param targetRoot - dsh agent-presets discovery root (e.g. <home>/.dsh/.agent-presets).
|
|
119
154
|
* @param retire - previously bundled preset ids to remove when absent from the source.
|
|
@@ -126,9 +161,23 @@ export function syncPresetTrees(sourceRoot: string, targetRoot: string, retire:
|
|
|
126
161
|
const source = join(sourceRoot, entry)
|
|
127
162
|
if (!statSync(source).isDirectory()) continue
|
|
128
163
|
const id = basename(source)
|
|
164
|
+
const targetDir = join(targetRoot, id)
|
|
165
|
+
let outcome: 'synced' | 'current'
|
|
166
|
+
try {
|
|
167
|
+
outcome = syncOnePreset(source, targetDir)
|
|
168
|
+
} catch (error) {
|
|
169
|
+
result.failed.push({ id, error: error instanceof Error ? error.message : String(error) })
|
|
170
|
+
continue
|
|
171
|
+
}
|
|
129
172
|
try {
|
|
130
|
-
const
|
|
131
|
-
|
|
173
|
+
const problems = validatePresetAgentFile(targetDir)
|
|
174
|
+
if (problems.length > 0) {
|
|
175
|
+
result.failed.push({ id, error: `agent.cordis.yml failed validation: ${problems.join('; ')}` })
|
|
176
|
+
} else if (outcome === 'synced') {
|
|
177
|
+
result.synced.push(id)
|
|
178
|
+
} else {
|
|
179
|
+
result.current.push(id)
|
|
180
|
+
}
|
|
132
181
|
} catch (error) {
|
|
133
182
|
result.failed.push({ id, error: error instanceof Error ? error.message : String(error) })
|
|
134
183
|
}
|