@linxin666/dsh-liangshen 0.3.24 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +2 -2
- package/README.md +24 -17
- package/README.zh.md +25 -18
- package/cordis.patch.yml +3 -2
- package/icon.svg +3 -0
- package/lib/client.js +381 -77
- package/lib/client.js.map +1 -1
- package/lib/index.js +594 -428
- package/lib/types/announce.test.d.ts +10 -0
- package/lib/types/client/LiangShenSettingsCard.d.ts +27 -10
- package/lib/types/client/PluginSettingsCard.d.ts +4 -0
- package/lib/types/client/index.d.ts +32 -13
- package/lib/types/client/lever-controller.d.ts +16 -2
- package/lib/types/client/locales.d.ts +13 -0
- package/lib/types/client/main-session.d.ts +32 -0
- package/lib/types/client/plugin-card-seat.d.ts +23 -16
- package/lib/types/client/settings-form.d.ts +17 -17
- package/lib/types/composition.d.ts +116 -0
- package/lib/types/composition.test.d.ts +7 -0
- package/lib/types/index.d.ts +146 -31
- package/lib/types/index.test.d.ts +5 -0
- package/package.json +25 -20
- package/presets/liangshen/agent.cordis.yml +44 -10
- package/presets/liangshen/fact-ledger.mjs +141 -0
- package/presets/liangshen/guard.mjs +426 -0
- package/presets/liangshen/minimal-prompt.mjs +2 -2
- package/presets/liangshen/tool-catalog.mjs +24 -20
- package/presets/liangshen/working-context.mjs +10 -2
- package/src/announce.test.ts +248 -55
- package/src/client/LiangShenLever.module.css +2 -0
- package/src/client/LiangShenLever.tsx +37 -9
- package/src/client/LiangShenSettingsCard.tsx +96 -12
- package/src/client/PluginSettingsCard.tsx +7 -2
- package/src/client/index.ts +61 -23
- package/src/client/lever-controller.ts +76 -9
- package/src/client/locales.ts +30 -4
- package/src/client/main-session.ts +43 -0
- package/src/client/plugin-card-seat.ts +24 -17
- package/src/client/settings-form.ts +34 -31
- package/src/composition.test.ts +294 -0
- package/src/composition.ts +554 -0
- package/src/index.test.ts +68 -1
- package/src/index.ts +278 -107
- package/tools/benchmark-live-run.mjs +171 -32
- package/lib/types/schema.d.ts +0 -18
- package/lib/types/schema.test.d.ts +0 -1
- package/lib/types/sync.d.ts +0 -74
- package/lib/types/sync.test.d.ts +0 -1
- package/src/schema.test.ts +0 -64
- package/src/schema.ts +0 -110
- package/src/sync.test.ts +0 -303
- package/src/sync.ts +0 -324
package/src/sync.test.ts
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { existsSync, mkdirSync, mkdtempSync, rmSync, statSync, utimesSync, writeFileSync, readFileSync } from 'node:fs'
|
|
3
|
-
import { tmpdir } from 'node:os'
|
|
4
|
-
import { join } from 'node:path'
|
|
5
|
-
import { renderPresetOverrides, syncOnePreset, syncPresetTrees } from './sync.ts'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Minimal structurally valid agent.cordis.yml used by the sync fixtures. It
|
|
9
|
-
* carries the two rows the settings overlay writes into, because the overlay
|
|
10
|
-
* only rewrites keys that already exist in the source — a fixture without them
|
|
11
|
-
* could not tell "left alone" from "wrote nothing".
|
|
12
|
-
*/
|
|
13
|
-
const VALID_AGENT_YAML = [
|
|
14
|
-
"- id: persona",
|
|
15
|
-
" name: '@deepseek-ai/dsh-persona'",
|
|
16
|
-
"",
|
|
17
|
-
"- id: tool-catalog",
|
|
18
|
-
" name: ./tool-catalog.mjs",
|
|
19
|
-
" config:",
|
|
20
|
-
" presentation: 'ptc'",
|
|
21
|
-
"",
|
|
22
|
-
].join('\n')
|
|
23
|
-
|
|
24
|
-
function fixture(): { source: string; target: string; dispose: () => void } {
|
|
25
|
-
const base = mkdtempSync(join(tmpdir(), 'dsh-liangshen-'))
|
|
26
|
-
const source = join(base, 'presets')
|
|
27
|
-
const target = join(base, 'agent-presets')
|
|
28
|
-
mkdirSync(join(source, 'liangshen'), { recursive: true })
|
|
29
|
-
writeFileSync(join(source, 'liangshen', 'agent.cordis.yml'), VALID_AGENT_YAML)
|
|
30
|
-
writeFileSync(join(source, 'liangshen', 'tool-catalog.mjs'), 'export const name = "x"\n')
|
|
31
|
-
writeFileSync(join(source, 'liangshen', 'preset.yml'), 'name: 梁神模式\n')
|
|
32
|
-
return { source, target, dispose: () => rmSync(base, { recursive: true, force: true }) }
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
describe('renderPresetOverrides', () => {
|
|
36
|
-
const SOURCE = [
|
|
37
|
-
"# a comment that names presentation: 'ptc'",
|
|
38
|
-
"- id: tool-catalog",
|
|
39
|
-
" name: ./tool-catalog.mjs",
|
|
40
|
-
" config:",
|
|
41
|
-
" descriptionMaxLength: 200",
|
|
42
|
-
" presentation: 'ptc'",
|
|
43
|
-
" pagedToolPatterns: ['mcp__*']",
|
|
44
|
-
"",
|
|
45
|
-
].join('\n')
|
|
46
|
-
|
|
47
|
-
it('rewrites exactly the targeted keys and nothing else', () => {
|
|
48
|
-
const out = renderPresetOverrides(SOURCE, {
|
|
49
|
-
presentation: 'native',
|
|
50
|
-
})
|
|
51
|
-
expect(out).toContain("presentation: 'native'")
|
|
52
|
-
// Untouched values and the surrounding rows survive verbatim.
|
|
53
|
-
expect(out).toContain('descriptionMaxLength: 200')
|
|
54
|
-
expect(out).toContain("pagedToolPatterns: ['mcp__*']")
|
|
55
|
-
// The comment that mentions the key is not a config line and stays put.
|
|
56
|
-
expect(out).toContain("# a comment that names presentation: 'ptc'")
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
it('leaves absent settings and unknown rows alone', () => {
|
|
60
|
-
expect(renderPresetOverrides(SOURCE, {})).toBe(SOURCE)
|
|
61
|
-
// A row the composition does not mount is never invented.
|
|
62
|
-
const out = renderPresetOverrides("- id: persona\n name: x\n", { presentation: 'native' })
|
|
63
|
-
expect(out).toBe("- id: persona\n name: x\n")
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
it('does not write a key the target row does not carry', () => {
|
|
67
|
-
const noPresentation = "- id: persona\n name: x\n config:\n name: 'test'\n"
|
|
68
|
-
const out = renderPresetOverrides(noPresentation, { presentation: 'native' })
|
|
69
|
-
expect(out).toBe(noPresentation)
|
|
70
|
-
})
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
describe('syncPresetTrees', () => {
|
|
74
|
-
it('copies the bundled preset tree into the target root', () => {
|
|
75
|
-
const f = fixture()
|
|
76
|
-
try {
|
|
77
|
-
const result = syncPresetTrees(f.source, f.target)
|
|
78
|
-
expect(result.synced).toEqual(['liangshen'])
|
|
79
|
-
expect(result.current).toEqual([])
|
|
80
|
-
expect(result.failed).toEqual([])
|
|
81
|
-
expect(readFileSync(join(f.target, 'liangshen', 'preset.yml'), 'utf8')).toContain('梁神模式')
|
|
82
|
-
expect(readFileSync(join(f.target, 'liangshen', 'tool-catalog.mjs'), 'utf8')).toContain('x')
|
|
83
|
-
} finally { f.dispose() }
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
it('is idempotent — a second run copies nothing', () => {
|
|
87
|
-
const f = fixture()
|
|
88
|
-
try {
|
|
89
|
-
syncPresetTrees(f.source, f.target)
|
|
90
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
91
|
-
expect(second.synced).toEqual([])
|
|
92
|
-
expect(second.current).toEqual(['liangshen'])
|
|
93
|
-
} finally { f.dispose() }
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
it('rewrites the tree when a file changed', () => {
|
|
97
|
-
const f = fixture()
|
|
98
|
-
try {
|
|
99
|
-
syncPresetTrees(f.source, f.target)
|
|
100
|
-
writeFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'changed\n')
|
|
101
|
-
const third = syncPresetTrees(f.source, f.target)
|
|
102
|
-
expect(third.synced).toEqual(['liangshen'])
|
|
103
|
-
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
104
|
-
} finally { f.dispose() }
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
it('copies a preset whose source path contains non-ASCII characters', () => {
|
|
108
|
-
const f = fixture()
|
|
109
|
-
try {
|
|
110
|
-
// Regression test for nodejs/node#54476: on Node 22 for Windows,
|
|
111
|
-
// fs.cpSync({ recursive: true }) aborts the process (0xC0000409) when
|
|
112
|
-
// the source path contains CJK characters, e.g. a CJK home directory.
|
|
113
|
-
// sync.ts must not use fs.cpSync; a CJK user profile must still boot.
|
|
114
|
-
const chinese = join(f.source, '梁神')
|
|
115
|
-
mkdirSync(chinese, { recursive: true })
|
|
116
|
-
writeFileSync(join(chinese, 'agent.cordis.yml'), VALID_AGENT_YAML)
|
|
117
|
-
writeFileSync(join(chinese, 'preset.yml'), 'name: 梁神模式\n')
|
|
118
|
-
const result = syncPresetTrees(f.source, f.target)
|
|
119
|
-
expect(result.synced).toContain('梁神')
|
|
120
|
-
expect(readFileSync(join(f.target, '梁神', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
121
|
-
} finally { f.dispose() }
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
it('retires a previously bundled preset directory removed from the source', () => {
|
|
125
|
-
const f = fixture()
|
|
126
|
-
try {
|
|
127
|
-
syncPresetTrees(f.source, f.target)
|
|
128
|
-
mkdirSync(join(f.target, 'liangshen-exact'), { recursive: true })
|
|
129
|
-
writeFileSync(join(f.target, 'liangshen-exact', 'agent.cordis.yml'), VALID_AGENT_YAML)
|
|
130
|
-
const result = syncPresetTrees(f.source, f.target, ['liangshen-exact'])
|
|
131
|
-
expect(result.retired).toEqual(['liangshen-exact'])
|
|
132
|
-
expect(existsSync(join(f.target, 'liangshen-exact'))).toBe(false)
|
|
133
|
-
expect(existsSync(join(f.target, 'liangshen'))).toBe(true)
|
|
134
|
-
} finally { f.dispose() }
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
it('never touches directories it does not own', () => {
|
|
138
|
-
const f = fixture()
|
|
139
|
-
try {
|
|
140
|
-
syncPresetTrees(f.source, f.target)
|
|
141
|
-
mkdirSync(join(f.target, 'user-authored'), { recursive: true })
|
|
142
|
-
writeFileSync(join(f.target, 'user-authored', 'x.txt'), 'mine\n')
|
|
143
|
-
const result = syncPresetTrees(f.source, f.target)
|
|
144
|
-
expect(result.synced).toEqual([])
|
|
145
|
-
expect(readFileSync(join(f.target, 'user-authored', 'x.txt'), 'utf8')).toBe('mine\n')
|
|
146
|
-
} finally { f.dispose() }
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
it('removes target files whose source file was deleted', () => {
|
|
150
|
-
const f = fixture()
|
|
151
|
-
try {
|
|
152
|
-
syncPresetTrees(f.source, f.target)
|
|
153
|
-
rmSync(join(f.source, 'liangshen', 'tool-catalog.mjs'))
|
|
154
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
155
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
156
|
-
expect(existsSync(join(f.target, 'liangshen', 'tool-catalog.mjs'))).toBe(false)
|
|
157
|
-
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
158
|
-
} finally { f.dispose() }
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
it('rewrites a same-size, same-mtime file whose bytes differ', () => {
|
|
162
|
-
const f = fixture()
|
|
163
|
-
try {
|
|
164
|
-
syncPresetTrees(f.source, f.target)
|
|
165
|
-
const source = join(f.source, 'liangshen', 'agent.cordis.yml')
|
|
166
|
-
const dest = join(f.target, 'liangshen', 'agent.cordis.yml')
|
|
167
|
-
// Same byte length, different content, so only the byte compare catches it.
|
|
168
|
-
const sourceText = readFileSync(source, 'utf8')
|
|
169
|
-
writeFileSync(dest, sourceText.replace('persona', 'parsena'))
|
|
170
|
-
const stat = statSync(source)
|
|
171
|
-
utimesSync(dest, stat.atime, stat.mtime)
|
|
172
|
-
expect(statSync(dest).size).toBe(stat.size)
|
|
173
|
-
expect(Math.abs(statSync(dest).mtimeMs - stat.mtimeMs)).toBeLessThan(1)
|
|
174
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
175
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
176
|
-
expect(readFileSync(dest, 'utf8')).toBe(VALID_AGENT_YAML)
|
|
177
|
-
} finally { f.dispose() }
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
it('removes extra nested files and the directories they leave empty', () => {
|
|
181
|
-
const f = fixture()
|
|
182
|
-
try {
|
|
183
|
-
syncPresetTrees(f.source, f.target)
|
|
184
|
-
mkdirSync(join(f.target, 'liangshen', 'extra', 'nested'), { recursive: true })
|
|
185
|
-
writeFileSync(join(f.target, 'liangshen', 'extra', 'nested', 'leftover.txt'), 'x\n')
|
|
186
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
187
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
188
|
-
expect(existsSync(join(f.target, 'liangshen', 'extra'))).toBe(false)
|
|
189
|
-
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
190
|
-
} finally { f.dispose() }
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
it('replaces a regular file at the target preset path with a directory', () => {
|
|
194
|
-
const f = fixture()
|
|
195
|
-
try {
|
|
196
|
-
mkdirSync(f.target, { recursive: true })
|
|
197
|
-
writeFileSync(join(f.target, 'liangshen'), 'not a directory\n')
|
|
198
|
-
const result = syncPresetTrees(f.source, f.target)
|
|
199
|
-
expect(result.synced).toEqual(['liangshen'])
|
|
200
|
-
expect(statSync(join(f.target, 'liangshen')).isDirectory()).toBe(true)
|
|
201
|
-
expect(readFileSync(join(f.target, 'liangshen', 'preset.yml'), 'utf8')).toContain('梁神模式')
|
|
202
|
-
} finally { f.dispose() }
|
|
203
|
-
})
|
|
204
|
-
|
|
205
|
-
it('reports a missing source root as an empty run', () => {
|
|
206
|
-
const f = fixture()
|
|
207
|
-
try {
|
|
208
|
-
const result = syncPresetTrees(join(f.source, 'nope'), f.target)
|
|
209
|
-
expect(result).toEqual({ synced: [], current: [], failed: [], retired: [] })
|
|
210
|
-
} finally { f.dispose() }
|
|
211
|
-
})
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
describe('mtime fast path', () => {
|
|
215
|
-
it('re-syncs an ordinary file whose mtime drifted beyond tolerance', () => {
|
|
216
|
-
// Plain copies keep the size-and-mtime fast path: a gap beyond tolerance
|
|
217
|
-
// proves the pair cannot be byte-identical, so the read is skipped and the
|
|
218
|
-
// tree counts as moved.
|
|
219
|
-
const f = fixture()
|
|
220
|
-
try {
|
|
221
|
-
syncPresetTrees(f.source, f.target)
|
|
222
|
-
const dest = join(f.target, 'liangshen', 'preset.yml')
|
|
223
|
-
const stat = statSync(dest)
|
|
224
|
-
utimesSync(dest, stat.atime, new Date(stat.mtimeMs + 86400_000))
|
|
225
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
226
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
227
|
-
expect(second.current).toEqual([])
|
|
228
|
-
} finally { f.dispose() }
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
it('treats the composition as current when its bytes match the overlaid source', () => {
|
|
232
|
-
// The composition is the one file the overlay may legitimately make differ
|
|
233
|
-
// from the raw source, so it is compared by content: an unchanged tree must
|
|
234
|
-
// report `current`, never a rewrite on every start.
|
|
235
|
-
const f = fixture()
|
|
236
|
-
try {
|
|
237
|
-
syncPresetTrees(f.source, f.target, [], { presentation: 'native' })
|
|
238
|
-
const dest = join(f.target, 'liangshen', 'agent.cordis.yml')
|
|
239
|
-
expect(readFileSync(dest, 'utf8')).toContain("presentation: 'native'")
|
|
240
|
-
const second = syncPresetTrees(f.source, f.target, [], { presentation: 'native' })
|
|
241
|
-
expect(second.current).toEqual(['liangshen'])
|
|
242
|
-
expect(second.synced).toEqual([])
|
|
243
|
-
} finally { f.dispose() }
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
it('rewrites the composition when the settings change', () => {
|
|
247
|
-
const f = fixture()
|
|
248
|
-
try {
|
|
249
|
-
syncPresetTrees(f.source, f.target, [], { presentation: 'native' })
|
|
250
|
-
const dest = join(f.target, 'liangshen', 'agent.cordis.yml')
|
|
251
|
-
const second = syncPresetTrees(f.source, f.target, [], { presentation: 'both' })
|
|
252
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
253
|
-
expect(readFileSync(dest, 'utf8')).toContain("presentation: 'both'")
|
|
254
|
-
} finally { f.dispose() }
|
|
255
|
-
})
|
|
256
|
-
|
|
257
|
-
it('re-syncs a target file with a different size without reading it twice', () => {
|
|
258
|
-
const f = fixture()
|
|
259
|
-
try {
|
|
260
|
-
syncPresetTrees(f.source, f.target)
|
|
261
|
-
writeFileSync(join(f.target, 'liangshen', 'preset.yml'), 'name: different\n')
|
|
262
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
263
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
264
|
-
} finally { f.dispose() }
|
|
265
|
-
})
|
|
266
|
-
})
|
|
267
|
-
|
|
268
|
-
describe('agent.cordis.yml validation after sync', () => {
|
|
269
|
-
it('reports an invalid bundled preset as failed instead of synced', () => {
|
|
270
|
-
const f = fixture()
|
|
271
|
-
try {
|
|
272
|
-
writeFileSync(join(f.source, 'liangshen', 'agent.cordis.yml'), 'rows: []\n')
|
|
273
|
-
const result = syncPresetTrees(f.source, f.target)
|
|
274
|
-
expect(result.synced).toEqual([])
|
|
275
|
-
expect(result.current).toEqual([])
|
|
276
|
-
expect(result.failed).toHaveLength(1)
|
|
277
|
-
expect(result.failed[0].id).toBe('liangshen')
|
|
278
|
-
expect(result.failed[0].error).toContain('failed validation')
|
|
279
|
-
} finally { f.dispose() }
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
it('validates a preset again on an idempotent run and still reports current', () => {
|
|
283
|
-
const f = fixture()
|
|
284
|
-
try {
|
|
285
|
-
syncPresetTrees(f.source, f.target)
|
|
286
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
287
|
-
expect(second.current).toEqual(['liangshen'])
|
|
288
|
-
expect(second.failed).toEqual([])
|
|
289
|
-
} finally { f.dispose() }
|
|
290
|
-
})
|
|
291
|
-
})
|
|
292
|
-
|
|
293
|
-
describe('syncOnePreset', () => {
|
|
294
|
-
it('copies once and stays current on identical bytes', () => {
|
|
295
|
-
const f = fixture()
|
|
296
|
-
try {
|
|
297
|
-
const source = join(f.source, 'liangshen')
|
|
298
|
-
const target = join(f.target, 'liangshen')
|
|
299
|
-
expect(syncOnePreset(source, target)).toBe('synced')
|
|
300
|
-
expect(syncOnePreset(source, target)).toBe('current')
|
|
301
|
-
} finally { f.dispose() }
|
|
302
|
-
})
|
|
303
|
-
})
|
package/src/sync.ts
DELETED
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sync every preset directory under `sourceRoot` into `targetRoot` — the
|
|
3
|
-
* dsh agent-presets discovery root (harness-home `.agent-presets`).
|
|
4
|
-
*
|
|
5
|
-
* A preset is a directory holding `agent.cordis.yml`; the directory name is
|
|
6
|
-
* the preset id. Copy is per-directory and idempotent: a preset whose target
|
|
7
|
-
* tree is byte-identical to the source tree is skipped, otherwise the source
|
|
8
|
-
* tree is copied and any target files the source does not contain are removed.
|
|
9
|
-
* Directories the plugin does not own (other presets the user authored) are
|
|
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.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync } from 'node:fs'
|
|
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
|
|
29
|
-
|
|
30
|
-
/** One sync run's outcome, grouped for diagnostics. */
|
|
31
|
-
export interface SyncResult {
|
|
32
|
-
/** Preset ids whose tree was (re)written this run. */
|
|
33
|
-
synced: string[]
|
|
34
|
-
/** Preset ids already current — nothing copied. */
|
|
35
|
-
current: string[]
|
|
36
|
-
/** Preset ids that failed, with the underlying error message. */
|
|
37
|
-
failed: { id: string; error: string }[]
|
|
38
|
-
/** Previously bundled preset ids removed from the target root this run. */
|
|
39
|
-
retired: string[]
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function filesUnder(root: string): string[] {
|
|
43
|
-
const out: string[] = []
|
|
44
|
-
const walk = (dir: string): void => {
|
|
45
|
-
for (const entry of readdirSync(dir)) {
|
|
46
|
-
const path = join(dir, entry)
|
|
47
|
-
if (statSync(path).isDirectory()) walk(path)
|
|
48
|
-
else out.push(path)
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
walk(root)
|
|
52
|
-
return out
|
|
53
|
-
}
|
|
54
|
-
|
|
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
|
-
*/
|
|
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
|
|
66
|
-
return readFileSync(a).equals(readFileSync(b))
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Remove files not in `keep` (relative paths), then remove only the
|
|
71
|
-
* directories those removals left empty — still strictly inside `root`, so
|
|
72
|
-
* sibling presets are never touched.
|
|
73
|
-
*/
|
|
74
|
-
function pruneExtras(root: string, keep: ReadonlySet<string>): void {
|
|
75
|
-
const parents = new Set<string>()
|
|
76
|
-
for (const file of filesUnder(root)) {
|
|
77
|
-
if (!keep.has(relative(root, file))) {
|
|
78
|
-
parents.add(dirname(file))
|
|
79
|
-
rmSync(file, { force: true })
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
for (const start of parents) {
|
|
83
|
-
let dir: string | undefined = start
|
|
84
|
-
while (dir !== undefined && relative(root, dir) !== '') {
|
|
85
|
-
if (existsSync(dir) && readdirSync(dir).length === 0) {
|
|
86
|
-
rmSync(dir, { recursive: true, force: true })
|
|
87
|
-
dir = dirname(dir)
|
|
88
|
-
} else {
|
|
89
|
-
dir = undefined
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
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
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Copy the whole tree under `sourceDir` into `targetDir`, creating the target
|
|
104
|
-
* directory as needed. Intentionally not `fs.cpSync` (recursive): on Node 22
|
|
105
|
-
* for Windows, `fs.cpSync` with `recursive: true` crashes the process with a
|
|
106
|
-
* fatal error (STATUS_STACK_BUFFER_OVERRUN / 0xC0000409, no JS exception is
|
|
107
|
-
* thrown) whenever the source path contains non-ASCII characters such as a
|
|
108
|
-
* CJK home directory (nodejs/node#54476, regression from nodejs/node#53614).
|
|
109
|
-
* Since `engines` supports Node ^22.19.0, this must work on Node 22, so the
|
|
110
|
-
* copy is done with the same per-entry primitives the rest of the module
|
|
111
|
-
* already uses. Source mtimes are preserved to keep the `preserveTimestamps`
|
|
112
|
-
* contract of the previous `cpSync` call.
|
|
113
|
-
*/
|
|
114
|
-
function copyTreeSync(sourceDir: string, targetDir: string): void {
|
|
115
|
-
mkdirSync(targetDir, { recursive: true })
|
|
116
|
-
for (const entry of readdirSync(sourceDir)) {
|
|
117
|
-
const source = join(sourceDir, entry)
|
|
118
|
-
const target = join(targetDir, entry)
|
|
119
|
-
const stat = statSync(source)
|
|
120
|
-
if (stat.isDirectory()) {
|
|
121
|
-
copyTreeSync(source, target)
|
|
122
|
-
} else {
|
|
123
|
-
copyFileSync(source, target)
|
|
124
|
-
utimesSync(target, stat.atime, stat.mtime)
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* The preset-row settings the plugin exposes in the web settings surface, as the
|
|
131
|
-
* overrides to write into the synced `agent.cordis.yml`.
|
|
132
|
-
*
|
|
133
|
-
* The settings surface edits the PLUGIN's namespace, while the values that shape
|
|
134
|
-
* a session live in the preset's rows. Writing the chosen values into the synced
|
|
135
|
-
* tree is what connects the two: the bundled source stays the shipped default,
|
|
136
|
-
* and the operator's choice rides on top of it. Every field is optional — an
|
|
137
|
-
* absent value leaves the shipped row exactly as it is.
|
|
138
|
-
*/
|
|
139
|
-
export interface PresetOverrides {
|
|
140
|
-
/** The `tool-catalog` row's `presentation` value. */
|
|
141
|
-
presentation?: string
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Replace one indented `key: <value>` line inside the block of `- id: <row>`.
|
|
146
|
-
*
|
|
147
|
-
* Strings render single-quoted and booleans render bare: YAML reads an unquoted
|
|
148
|
-
* `false` as a boolean, while `'false'` would be a truthy string and silently
|
|
149
|
-
* invert the switch.
|
|
150
|
-
*/
|
|
151
|
-
function setRowValue(text: string, rowId: string, key: string, value: string | boolean | undefined): string {
|
|
152
|
-
if (value === undefined) return text
|
|
153
|
-
const literal = typeof value === 'boolean' ? String(value) : `'${value}'`
|
|
154
|
-
const rowStart = text.indexOf(`- id: ${rowId}
|
|
155
|
-
`)
|
|
156
|
-
if (rowStart < 0) return text
|
|
157
|
-
const afterRow = rowStart + `- id: ${rowId}
|
|
158
|
-
`.length
|
|
159
|
-
// The row body ends at the first line that is not indented (the next top-level
|
|
160
|
-
// entry or a comment block).
|
|
161
|
-
const rest = text.slice(afterRow)
|
|
162
|
-
const boundary = rest.search(/^\S/m)
|
|
163
|
-
const body = boundary < 0 ? rest : rest.slice(0, boundary)
|
|
164
|
-
const tail = boundary < 0 ? '' : rest.slice(boundary)
|
|
165
|
-
const pattern = new RegExp(`^(\\s*)${key}:\\s*.*$`, 'm')
|
|
166
|
-
const rewritten = pattern.test(body)
|
|
167
|
-
? body.replace(pattern, `$1${key}: ${literal}`)
|
|
168
|
-
: body
|
|
169
|
-
return text.slice(0, afterRow) + rewritten + tail
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Render `agent.cordis.yml` with the settings overrides applied.
|
|
174
|
-
*
|
|
175
|
-
* Rows are matched by their `- id:` marker, so the transform does not depend on
|
|
176
|
-
* line numbers or on the values the shipped file happens to carry. A row or key
|
|
177
|
-
* the source does not contain is left alone rather than invented: the overlay
|
|
178
|
-
* narrows the shipped configuration, it never adds a mount the preset did not
|
|
179
|
-
* have.
|
|
180
|
-
* @param text - the bundled preset composition.
|
|
181
|
-
* @param overrides - the operator's settings-surface choices.
|
|
182
|
-
* @returns the composition to write into the synced tree.
|
|
183
|
-
*/
|
|
184
|
-
export function renderPresetOverrides(text: string, overrides: PresetOverrides): string {
|
|
185
|
-
let out = text
|
|
186
|
-
out = setRowValue(out, 'tool-catalog', 'presentation', overrides.presentation)
|
|
187
|
-
return out
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/** Copy `sourceRoot/<id>` into `targetRoot/<id>`, idempotently. */
|
|
191
|
-
export function syncOnePreset(sourceDir: string, targetDir: string, overrides: PresetOverrides = {}): 'synced' | 'current' {
|
|
192
|
-
const sourceFiles = filesUnder(sourceDir)
|
|
193
|
-
const sourceSet = new Set(sourceFiles.map(file => relative(sourceDir, file)))
|
|
194
|
-
|
|
195
|
-
if (existsSync(targetDir) && !statSync(targetDir).isDirectory()) {
|
|
196
|
-
rmSync(targetDir, { recursive: true, force: true })
|
|
197
|
-
}
|
|
198
|
-
const applyOverrides = (): void => {
|
|
199
|
-
const agentFile = join(targetDir, 'agent.cordis.yml')
|
|
200
|
-
if (!existsSync(agentFile)) return
|
|
201
|
-
const rendered = renderPresetOverrides(readFileSync(agentFile, 'utf8'), overrides)
|
|
202
|
-
if (rendered !== readFileSync(agentFile, 'utf8')) writeFileSync(agentFile, rendered)
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (!existsSync(targetDir)) {
|
|
206
|
-
copyTreeSync(sourceDir, targetDir)
|
|
207
|
-
pruneExtras(targetDir, sourceSet)
|
|
208
|
-
applyOverrides()
|
|
209
|
-
return 'synced'
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// The composition is compared against the SOURCE AFTER the overlay, because the
|
|
213
|
-
// target legitimately carries the operator's settings there. Every other file
|
|
214
|
-
// is a plain copy, so it keeps the original size-and-mtime fast path — the
|
|
215
|
-
// overlay is the one thing that can make target bytes differ from source bytes
|
|
216
|
-
// on purpose.
|
|
217
|
-
const agentEntry = join(sourceDir, 'agent.cordis.yml')
|
|
218
|
-
const expectedAgent = existsSync(agentEntry)
|
|
219
|
-
? Buffer.from(renderPresetOverrides(readFileSync(agentEntry, 'utf8'), overrides))
|
|
220
|
-
: undefined
|
|
221
|
-
|
|
222
|
-
let dirty = false
|
|
223
|
-
for (const file of sourceFiles) {
|
|
224
|
-
const dest = join(targetDir, relative(sourceDir, file))
|
|
225
|
-
if (!existsSync(dest)) {
|
|
226
|
-
dirty = true
|
|
227
|
-
break
|
|
228
|
-
}
|
|
229
|
-
if (file === agentEntry) {
|
|
230
|
-
if (expectedAgent === undefined || !expectedAgent.equals(readFileSync(dest))) {
|
|
231
|
-
dirty = true
|
|
232
|
-
break
|
|
233
|
-
}
|
|
234
|
-
continue
|
|
235
|
-
}
|
|
236
|
-
if (!sameFile(file, dest)) {
|
|
237
|
-
dirty = true
|
|
238
|
-
break
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
if (!dirty) {
|
|
242
|
-
for (const file of filesUnder(targetDir)) {
|
|
243
|
-
if (!sourceSet.has(relative(targetDir, file))) {
|
|
244
|
-
dirty = true
|
|
245
|
-
break
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
if (!dirty) {
|
|
250
|
-
// Content matches, but a previous run may have written the tree before the
|
|
251
|
-
// overlay existed (or under different settings). Re-apply so an upgrade and
|
|
252
|
-
// a settings change both converge without a spurious "synced" report.
|
|
253
|
-
const before = readFileSync(join(targetDir, 'agent.cordis.yml'), 'utf8')
|
|
254
|
-
const after = renderPresetOverrides(before, overrides)
|
|
255
|
-
if (after !== before) {
|
|
256
|
-
writeFileSync(join(targetDir, 'agent.cordis.yml'), after)
|
|
257
|
-
return 'synced'
|
|
258
|
-
}
|
|
259
|
-
return 'current'
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// Drop target-only entries first so file/dir type clashes never reach the
|
|
263
|
-
// copy, then copy and prune again per the post-copy contract.
|
|
264
|
-
pruneExtras(targetDir, sourceSet)
|
|
265
|
-
copyTreeSync(sourceDir, targetDir)
|
|
266
|
-
pruneExtras(targetDir, sourceSet)
|
|
267
|
-
applyOverrides()
|
|
268
|
-
return 'synced'
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Sync every preset under `sourceRoot` into `targetRoot`, then remove
|
|
273
|
-
* target directories named in `retire` that the bundle no longer ships —
|
|
274
|
-
* preset ids the plugin once owned and later dropped. Only those exact ids
|
|
275
|
-
* are removed; every other target directory is left untouched.
|
|
276
|
-
*
|
|
277
|
-
* Each synced (or already-current) preset is validated against the structural
|
|
278
|
-
* `agent.cordis.yml` schema; a validation failure lands in `failed` so the
|
|
279
|
-
* caller can surface a broken preset as a first-class result instead of a
|
|
280
|
-
* warn-only log line.
|
|
281
|
-
* @param sourceRoot - plugin-owned preset tree (bundled in the package).
|
|
282
|
-
* @param targetRoot - dsh agent-presets discovery root (e.g. <home>/.dsh/.agent-presets).
|
|
283
|
-
* @param retire - previously bundled preset ids to remove when absent from the source.
|
|
284
|
-
*/
|
|
285
|
-
export function syncPresetTrees(sourceRoot: string, targetRoot: string, retire: string[] = [], overrides: PresetOverrides = {}): SyncResult {
|
|
286
|
-
const result: SyncResult = { synced: [], current: [], failed: [], retired: [] }
|
|
287
|
-
mkdirSync(targetRoot, { recursive: true })
|
|
288
|
-
if (existsSync(sourceRoot)) {
|
|
289
|
-
for (const entry of readdirSync(sourceRoot)) {
|
|
290
|
-
const source = join(sourceRoot, entry)
|
|
291
|
-
if (!statSync(source).isDirectory()) continue
|
|
292
|
-
const id = basename(source)
|
|
293
|
-
const targetDir = join(targetRoot, id)
|
|
294
|
-
let outcome: 'synced' | 'current'
|
|
295
|
-
try {
|
|
296
|
-
outcome = syncOnePreset(source, targetDir, overrides)
|
|
297
|
-
} catch (error) {
|
|
298
|
-
result.failed.push({ id, error: error instanceof Error ? error.message : String(error) })
|
|
299
|
-
continue
|
|
300
|
-
}
|
|
301
|
-
try {
|
|
302
|
-
const problems = validatePresetAgentFile(targetDir)
|
|
303
|
-
if (problems.length > 0) {
|
|
304
|
-
result.failed.push({ id, error: `agent.cordis.yml failed validation: ${problems.join('; ')}` })
|
|
305
|
-
} else if (outcome === 'synced') {
|
|
306
|
-
result.synced.push(id)
|
|
307
|
-
} else {
|
|
308
|
-
result.current.push(id)
|
|
309
|
-
}
|
|
310
|
-
} catch (error) {
|
|
311
|
-
result.failed.push({ id, error: error instanceof Error ? error.message : String(error) })
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
for (const id of retire) {
|
|
316
|
-
if (existsSync(join(sourceRoot, id))) continue
|
|
317
|
-
const stale = join(targetRoot, id)
|
|
318
|
-
if (existsSync(stale) && statSync(stale).isDirectory()) {
|
|
319
|
-
rmSync(stale, { recursive: true, force: true })
|
|
320
|
-
result.retired.push(id)
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
return result
|
|
324
|
-
}
|