@linxin666/dsh-liangshen 0.3.23 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +2 -2
- package/README.md +55 -41
- package/README.zh.md +54 -40
- package/cordis.patch.yml +3 -2
- package/icon.svg +3 -0
- package/lib/client.js +1380 -29
- package/lib/client.js.map +1 -1
- package/lib/index.js +616 -367
- package/lib/types/announce.test.d.ts +10 -0
- package/lib/types/client/LiangShenSettingsCard.d.ts +78 -0
- package/lib/types/client/PluginSettingsCard.d.ts +137 -0
- package/lib/types/client/index.d.ts +57 -4
- package/lib/types/client/lever-controller.d.ts +16 -2
- package/lib/types/client/locales.d.ts +40 -1
- package/lib/types/client/main-session.d.ts +32 -0
- package/lib/types/client/plugin-card-seat.d.ts +103 -0
- package/lib/types/client/settings-form.d.ts +197 -0
- package/lib/types/composition.d.ts +116 -0
- package/lib/types/composition.test.d.ts +7 -0
- package/lib/types/core/lever.d.ts +6 -3
- package/lib/types/index.d.ts +160 -33
- package/lib/types/index.test.d.ts +5 -0
- package/package.json +27 -18
- package/presets/liangshen/NOTICE +4 -4
- package/presets/liangshen/agent.cordis.yml +168 -123
- package/presets/liangshen/fact-ledger.mjs +141 -0
- package/presets/liangshen/guard.mjs +426 -0
- package/presets/liangshen/minimal-prompt.mjs +73 -38
- package/presets/liangshen/paging.mjs +232 -0
- package/presets/liangshen/tool-activate.mjs +176 -0
- package/presets/liangshen/tool-catalog.mjs +615 -219
- package/presets/liangshen/working-context.mjs +222 -0
- package/src/announce.test.ts +248 -55
- package/src/client/LiangShenLever.module.css +2 -0
- package/src/client/LiangShenLever.tsx +41 -10
- package/src/client/LiangShenSettingsCard.tsx +233 -0
- package/src/client/PluginSettingsCard.tsx +555 -0
- package/src/client/index.ts +102 -5
- package/src/client/lever-controller.ts +76 -9
- package/src/client/locales.ts +79 -1
- package/src/client/main-session.ts +43 -0
- package/src/client/plugin-card-seat.ts +192 -0
- package/src/client/settings-card.module.css +418 -0
- package/src/client/settings-form.ts +442 -0
- package/src/composition.test.ts +294 -0
- package/src/composition.ts +554 -0
- package/src/core/lever.ts +7 -4
- package/src/index.test.ts +68 -1
- package/src/index.ts +293 -101
- package/tools/benchmark-live-run.mjs +193 -63
- package/lib/types/schema.d.ts +0 -18
- package/lib/types/schema.test.d.ts +0 -1
- package/lib/types/sync.d.ts +0 -47
- package/lib/types/sync.test.d.ts +0 -1
- package/presets/liangshen/custom-bash.mjs +0 -213
- package/src/schema.test.ts +0 -64
- package/src/schema.ts +0 -110
- package/src/sync.test.ts +0 -224
- package/src/sync.ts +0 -222
package/src/schema.test.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
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-catalog.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
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,224 +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 { 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"
|
|
9
|
-
|
|
10
|
-
function fixture(): { source: string; target: string; dispose: () => void } {
|
|
11
|
-
const base = mkdtempSync(join(tmpdir(), 'dsh-liangshen-'))
|
|
12
|
-
const source = join(base, 'presets')
|
|
13
|
-
const target = join(base, 'agent-presets')
|
|
14
|
-
mkdirSync(join(source, 'liangshen'), { recursive: true })
|
|
15
|
-
writeFileSync(join(source, 'liangshen', 'agent.cordis.yml'), VALID_AGENT_YAML)
|
|
16
|
-
writeFileSync(join(source, 'liangshen', 'tool-catalog.mjs'), 'export const name = "x"\n')
|
|
17
|
-
writeFileSync(join(source, 'liangshen', 'preset.yml'), 'name: 梁神模式\n')
|
|
18
|
-
return { source, target, dispose: () => rmSync(base, { recursive: true, force: true }) }
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
describe('syncPresetTrees', () => {
|
|
22
|
-
it('copies the bundled preset tree into the target root', () => {
|
|
23
|
-
const f = fixture()
|
|
24
|
-
try {
|
|
25
|
-
const result = syncPresetTrees(f.source, f.target)
|
|
26
|
-
expect(result.synced).toEqual(['liangshen'])
|
|
27
|
-
expect(result.current).toEqual([])
|
|
28
|
-
expect(result.failed).toEqual([])
|
|
29
|
-
expect(readFileSync(join(f.target, 'liangshen', 'preset.yml'), 'utf8')).toContain('梁神模式')
|
|
30
|
-
expect(readFileSync(join(f.target, 'liangshen', 'tool-catalog.mjs'), 'utf8')).toContain('x')
|
|
31
|
-
} finally { f.dispose() }
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it('is idempotent — a second run copies nothing', () => {
|
|
35
|
-
const f = fixture()
|
|
36
|
-
try {
|
|
37
|
-
syncPresetTrees(f.source, f.target)
|
|
38
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
39
|
-
expect(second.synced).toEqual([])
|
|
40
|
-
expect(second.current).toEqual(['liangshen'])
|
|
41
|
-
} finally { f.dispose() }
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('rewrites the tree when a file changed', () => {
|
|
45
|
-
const f = fixture()
|
|
46
|
-
try {
|
|
47
|
-
syncPresetTrees(f.source, f.target)
|
|
48
|
-
writeFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'changed\n')
|
|
49
|
-
const third = syncPresetTrees(f.source, f.target)
|
|
50
|
-
expect(third.synced).toEqual(['liangshen'])
|
|
51
|
-
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
52
|
-
} finally { f.dispose() }
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('copies a preset whose source path contains non-ASCII characters', () => {
|
|
56
|
-
const f = fixture()
|
|
57
|
-
try {
|
|
58
|
-
// Regression test for nodejs/node#54476: on Node 22 for Windows,
|
|
59
|
-
// fs.cpSync({ recursive: true }) aborts the process (0xC0000409) when
|
|
60
|
-
// the source path contains CJK characters, e.g. a CJK home directory.
|
|
61
|
-
// sync.ts must not use fs.cpSync; a CJK user profile must still boot.
|
|
62
|
-
const chinese = join(f.source, '梁神')
|
|
63
|
-
mkdirSync(chinese, { recursive: true })
|
|
64
|
-
writeFileSync(join(chinese, 'agent.cordis.yml'), VALID_AGENT_YAML)
|
|
65
|
-
writeFileSync(join(chinese, 'preset.yml'), 'name: 梁神模式\n')
|
|
66
|
-
const result = syncPresetTrees(f.source, f.target)
|
|
67
|
-
expect(result.synced).toContain('梁神')
|
|
68
|
-
expect(readFileSync(join(f.target, '梁神', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
69
|
-
} finally { f.dispose() }
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('retires a previously bundled preset directory removed from the source', () => {
|
|
73
|
-
const f = fixture()
|
|
74
|
-
try {
|
|
75
|
-
syncPresetTrees(f.source, f.target)
|
|
76
|
-
mkdirSync(join(f.target, 'liangshen-exact'), { recursive: true })
|
|
77
|
-
writeFileSync(join(f.target, 'liangshen-exact', 'agent.cordis.yml'), VALID_AGENT_YAML)
|
|
78
|
-
const result = syncPresetTrees(f.source, f.target, ['liangshen-exact'])
|
|
79
|
-
expect(result.retired).toEqual(['liangshen-exact'])
|
|
80
|
-
expect(existsSync(join(f.target, 'liangshen-exact'))).toBe(false)
|
|
81
|
-
expect(existsSync(join(f.target, 'liangshen'))).toBe(true)
|
|
82
|
-
} finally { f.dispose() }
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
it('never touches directories it does not own', () => {
|
|
86
|
-
const f = fixture()
|
|
87
|
-
try {
|
|
88
|
-
syncPresetTrees(f.source, f.target)
|
|
89
|
-
mkdirSync(join(f.target, 'user-authored'), { recursive: true })
|
|
90
|
-
writeFileSync(join(f.target, 'user-authored', 'x.txt'), 'mine\n')
|
|
91
|
-
const result = syncPresetTrees(f.source, f.target)
|
|
92
|
-
expect(result.synced).toEqual([])
|
|
93
|
-
expect(readFileSync(join(f.target, 'user-authored', 'x.txt'), 'utf8')).toBe('mine\n')
|
|
94
|
-
} finally { f.dispose() }
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
it('removes target files whose source file was deleted', () => {
|
|
98
|
-
const f = fixture()
|
|
99
|
-
try {
|
|
100
|
-
syncPresetTrees(f.source, f.target)
|
|
101
|
-
rmSync(join(f.source, 'liangshen', 'tool-catalog.mjs'))
|
|
102
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
103
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
104
|
-
expect(existsSync(join(f.target, 'liangshen', 'tool-catalog.mjs'))).toBe(false)
|
|
105
|
-
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
106
|
-
} finally { f.dispose() }
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
it('rewrites a same-size, same-mtime file whose bytes differ', () => {
|
|
110
|
-
const f = fixture()
|
|
111
|
-
try {
|
|
112
|
-
syncPresetTrees(f.source, f.target)
|
|
113
|
-
const source = join(f.source, 'liangshen', 'agent.cordis.yml')
|
|
114
|
-
const dest = join(f.target, 'liangshen', 'agent.cordis.yml')
|
|
115
|
-
// Same byte length, different content, so only the byte compare catches it.
|
|
116
|
-
const sourceText = readFileSync(source, 'utf8')
|
|
117
|
-
writeFileSync(dest, sourceText.replace('persona', 'parsena'))
|
|
118
|
-
const stat = statSync(source)
|
|
119
|
-
utimesSync(dest, stat.atime, stat.mtime)
|
|
120
|
-
expect(statSync(dest).size).toBe(stat.size)
|
|
121
|
-
expect(Math.abs(statSync(dest).mtimeMs - stat.mtimeMs)).toBeLessThan(1)
|
|
122
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
123
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
124
|
-
expect(readFileSync(dest, 'utf8')).toBe(VALID_AGENT_YAML)
|
|
125
|
-
} finally { f.dispose() }
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
it('removes extra nested files and the directories they leave empty', () => {
|
|
129
|
-
const f = fixture()
|
|
130
|
-
try {
|
|
131
|
-
syncPresetTrees(f.source, f.target)
|
|
132
|
-
mkdirSync(join(f.target, 'liangshen', 'extra', 'nested'), { recursive: true })
|
|
133
|
-
writeFileSync(join(f.target, 'liangshen', 'extra', 'nested', 'leftover.txt'), 'x\n')
|
|
134
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
135
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
136
|
-
expect(existsSync(join(f.target, 'liangshen', 'extra'))).toBe(false)
|
|
137
|
-
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe(VALID_AGENT_YAML)
|
|
138
|
-
} finally { f.dispose() }
|
|
139
|
-
})
|
|
140
|
-
|
|
141
|
-
it('replaces a regular file at the target preset path with a directory', () => {
|
|
142
|
-
const f = fixture()
|
|
143
|
-
try {
|
|
144
|
-
mkdirSync(f.target, { recursive: true })
|
|
145
|
-
writeFileSync(join(f.target, 'liangshen'), 'not a directory\n')
|
|
146
|
-
const result = syncPresetTrees(f.source, f.target)
|
|
147
|
-
expect(result.synced).toEqual(['liangshen'])
|
|
148
|
-
expect(statSync(join(f.target, 'liangshen')).isDirectory()).toBe(true)
|
|
149
|
-
expect(readFileSync(join(f.target, 'liangshen', 'preset.yml'), 'utf8')).toContain('梁神模式')
|
|
150
|
-
} finally { f.dispose() }
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
it('reports a missing source root as an empty run', () => {
|
|
154
|
-
const f = fixture()
|
|
155
|
-
try {
|
|
156
|
-
const result = syncPresetTrees(join(f.source, 'nope'), f.target)
|
|
157
|
-
expect(result).toEqual({ synced: [], current: [], failed: [], retired: [] })
|
|
158
|
-
} finally { f.dispose() }
|
|
159
|
-
})
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
describe('mtime fast path', () => {
|
|
163
|
-
it('re-syncs a byte-identical file whose mtime drifted beyond tolerance', () => {
|
|
164
|
-
const f = fixture()
|
|
165
|
-
try {
|
|
166
|
-
syncPresetTrees(f.source, f.target)
|
|
167
|
-
const dest = join(f.target, 'liangshen', 'agent.cordis.yml')
|
|
168
|
-
const stat = statSync(dest)
|
|
169
|
-
// Bump mtime a day into the future, keep the bytes identical.
|
|
170
|
-
utimesSync(dest, stat.atime, new Date(stat.mtimeMs + 86400_000))
|
|
171
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
172
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
173
|
-
expect(second.current).toEqual([])
|
|
174
|
-
expect(readFileSync(dest, 'utf8')).toBe(VALID_AGENT_YAML)
|
|
175
|
-
} finally { f.dispose() }
|
|
176
|
-
})
|
|
177
|
-
|
|
178
|
-
it('re-syncs a target file with a different size without reading it twice', () => {
|
|
179
|
-
const f = fixture()
|
|
180
|
-
try {
|
|
181
|
-
syncPresetTrees(f.source, f.target)
|
|
182
|
-
writeFileSync(join(f.target, 'liangshen', 'preset.yml'), 'name: different\n')
|
|
183
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
184
|
-
expect(second.synced).toEqual(['liangshen'])
|
|
185
|
-
} finally { f.dispose() }
|
|
186
|
-
})
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
describe('agent.cordis.yml validation after sync', () => {
|
|
190
|
-
it('reports an invalid bundled preset as failed instead of synced', () => {
|
|
191
|
-
const f = fixture()
|
|
192
|
-
try {
|
|
193
|
-
writeFileSync(join(f.source, 'liangshen', 'agent.cordis.yml'), 'rows: []\n')
|
|
194
|
-
const result = syncPresetTrees(f.source, f.target)
|
|
195
|
-
expect(result.synced).toEqual([])
|
|
196
|
-
expect(result.current).toEqual([])
|
|
197
|
-
expect(result.failed).toHaveLength(1)
|
|
198
|
-
expect(result.failed[0].id).toBe('liangshen')
|
|
199
|
-
expect(result.failed[0].error).toContain('failed validation')
|
|
200
|
-
} finally { f.dispose() }
|
|
201
|
-
})
|
|
202
|
-
|
|
203
|
-
it('validates a preset again on an idempotent run and still reports current', () => {
|
|
204
|
-
const f = fixture()
|
|
205
|
-
try {
|
|
206
|
-
syncPresetTrees(f.source, f.target)
|
|
207
|
-
const second = syncPresetTrees(f.source, f.target)
|
|
208
|
-
expect(second.current).toEqual(['liangshen'])
|
|
209
|
-
expect(second.failed).toEqual([])
|
|
210
|
-
} finally { f.dispose() }
|
|
211
|
-
})
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
describe('syncOnePreset', () => {
|
|
215
|
-
it('copies once and stays current on identical bytes', () => {
|
|
216
|
-
const f = fixture()
|
|
217
|
-
try {
|
|
218
|
-
const source = join(f.source, 'liangshen')
|
|
219
|
-
const target = join(f.target, 'liangshen')
|
|
220
|
-
expect(syncOnePreset(source, target)).toBe('synced')
|
|
221
|
-
expect(syncOnePreset(source, target)).toBe('current')
|
|
222
|
-
} finally { f.dispose() }
|
|
223
|
-
})
|
|
224
|
-
})
|
package/src/sync.ts
DELETED
|
@@ -1,222 +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 } 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
|
-
/** Copy `sourceRoot/<id>` into `targetRoot/<id>`, idempotently. */
|
|
130
|
-
export function syncOnePreset(sourceDir: string, targetDir: string): 'synced' | 'current' {
|
|
131
|
-
const sourceFiles = filesUnder(sourceDir)
|
|
132
|
-
const sourceSet = new Set(sourceFiles.map(file => relative(sourceDir, file)))
|
|
133
|
-
|
|
134
|
-
if (existsSync(targetDir) && !statSync(targetDir).isDirectory()) {
|
|
135
|
-
rmSync(targetDir, { recursive: true, force: true })
|
|
136
|
-
}
|
|
137
|
-
if (!existsSync(targetDir)) {
|
|
138
|
-
copyTreeSync(sourceDir, targetDir)
|
|
139
|
-
pruneExtras(targetDir, sourceSet)
|
|
140
|
-
return 'synced'
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
let dirty = false
|
|
144
|
-
for (const file of sourceFiles) {
|
|
145
|
-
const dest = join(targetDir, relative(sourceDir, file))
|
|
146
|
-
if (!existsSync(dest) || !sameFile(file, dest)) {
|
|
147
|
-
dirty = true
|
|
148
|
-
break
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
if (!dirty) {
|
|
152
|
-
for (const file of filesUnder(targetDir)) {
|
|
153
|
-
if (!sourceSet.has(relative(targetDir, file))) {
|
|
154
|
-
dirty = true
|
|
155
|
-
break
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
if (!dirty) return 'current'
|
|
160
|
-
|
|
161
|
-
// Drop target-only entries first so file/dir type clashes never reach the
|
|
162
|
-
// copy, then copy and prune again per the post-copy contract.
|
|
163
|
-
pruneExtras(targetDir, sourceSet)
|
|
164
|
-
copyTreeSync(sourceDir, targetDir)
|
|
165
|
-
pruneExtras(targetDir, sourceSet)
|
|
166
|
-
return 'synced'
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Sync every preset under `sourceRoot` into `targetRoot`, then remove
|
|
171
|
-
* target directories named in `retire` that the bundle no longer ships —
|
|
172
|
-
* preset ids the plugin once owned and later dropped. Only those exact ids
|
|
173
|
-
* are removed; every other target directory is left untouched.
|
|
174
|
-
*
|
|
175
|
-
* Each synced (or already-current) preset is validated against the structural
|
|
176
|
-
* `agent.cordis.yml` schema; a validation failure lands in `failed` so the
|
|
177
|
-
* caller can surface a broken preset as a first-class result instead of a
|
|
178
|
-
* warn-only log line.
|
|
179
|
-
* @param sourceRoot - plugin-owned preset tree (bundled in the package).
|
|
180
|
-
* @param targetRoot - dsh agent-presets discovery root (e.g. <home>/.dsh/.agent-presets).
|
|
181
|
-
* @param retire - previously bundled preset ids to remove when absent from the source.
|
|
182
|
-
*/
|
|
183
|
-
export function syncPresetTrees(sourceRoot: string, targetRoot: string, retire: string[] = []): SyncResult {
|
|
184
|
-
const result: SyncResult = { synced: [], current: [], failed: [], retired: [] }
|
|
185
|
-
mkdirSync(targetRoot, { recursive: true })
|
|
186
|
-
if (existsSync(sourceRoot)) {
|
|
187
|
-
for (const entry of readdirSync(sourceRoot)) {
|
|
188
|
-
const source = join(sourceRoot, entry)
|
|
189
|
-
if (!statSync(source).isDirectory()) continue
|
|
190
|
-
const id = basename(source)
|
|
191
|
-
const targetDir = join(targetRoot, id)
|
|
192
|
-
let outcome: 'synced' | 'current'
|
|
193
|
-
try {
|
|
194
|
-
outcome = syncOnePreset(source, targetDir)
|
|
195
|
-
} catch (error) {
|
|
196
|
-
result.failed.push({ id, error: error instanceof Error ? error.message : String(error) })
|
|
197
|
-
continue
|
|
198
|
-
}
|
|
199
|
-
try {
|
|
200
|
-
const problems = validatePresetAgentFile(targetDir)
|
|
201
|
-
if (problems.length > 0) {
|
|
202
|
-
result.failed.push({ id, error: `agent.cordis.yml failed validation: ${problems.join('; ')}` })
|
|
203
|
-
} else if (outcome === 'synced') {
|
|
204
|
-
result.synced.push(id)
|
|
205
|
-
} else {
|
|
206
|
-
result.current.push(id)
|
|
207
|
-
}
|
|
208
|
-
} catch (error) {
|
|
209
|
-
result.failed.push({ id, error: error instanceof Error ? error.message : String(error) })
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
for (const id of retire) {
|
|
214
|
-
if (existsSync(join(sourceRoot, id))) continue
|
|
215
|
-
const stale = join(targetRoot, id)
|
|
216
|
-
if (existsSync(stale) && statSync(stale).isDirectory()) {
|
|
217
|
-
rmSync(stale, { recursive: true, force: true })
|
|
218
|
-
result.retired.push(id)
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
return result
|
|
222
|
-
}
|