@godv61/dsh-task-engine 0.24.0 → 0.25.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.
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Evidence kinds: a skill is judged against the proof it actually declares.
3
+ *
4
+ * Demanding a "real validation command" from every non-core skill forced an
5
+ * irrelevant command from a requirement or design skill, whose output is a
6
+ * document. The evidence existed — in another form.
7
+ */
8
+ import test from 'node:test'
9
+ import assert from 'node:assert/strict'
10
+ import { needsSkillReceipt, skillBlockers } from './lib/skill-audit.js'
11
+ import { adoptRecommendation, resolveFlow } from './lib/workflows.js'
12
+
13
+ /** A session that has loaded the named skills. */
14
+ function sessionWith(names) {
15
+ const events = []
16
+ for (const name of names) {
17
+ const callId = 'c' + events.length
18
+ events.push({ type: 'tool/call', data: { name: 'skill', callId, arguments: JSON.stringify({ name }) } })
19
+ events.push({ type: 'tool/result', data: { message: { content: [{ type: 'tool-result', toolCallId: callId, isError: false }] } } })
20
+ }
21
+ return { id: 's', snapshotEvents: () => events }
22
+ }
23
+
24
+ /** A standard-flow config with one extra skill on 开发 carrying the given evidence. */
25
+ function withEvidence(evidence) {
26
+ const adopted = adoptRecommendation('standard')
27
+ const skills = [...adopted.stage_bindings['开发'].skills, { skill: { source: 'project', name: 'doc-skill' }, rules: [], ...(evidence !== undefined ? { evidence } : {}) }]
28
+ return resolveFlow('standard', { ...adopted, stage_bindings: { ...adopted.stage_bindings, '开发': { skills } } }).config
29
+ }
30
+
31
+ /** A task at 开发 with everything else satisfied. */
32
+ function state(extra = {}) {
33
+ return {
34
+ schema: 1, id: 'E-1', title: 't', branch: 'main', work_size: 'standard', risk_level: 'standard',
35
+ stage: '开发', execution_version: 1,
36
+ requirement_confirmed: true, solution_confirmed: true,
37
+ items: [], artifacts: {}, files: [], commits: [],
38
+ verification: { passed: false, evidence: [] },
39
+ review: { outcome: 'pending' },
40
+ ...extra,
41
+ }
42
+ }
43
+
44
+ test('evidence: needsSkillReceipt honours a declared non-command kind', () => {
45
+ // Core skills keep their exemption; a declared kind overrides the default.
46
+ assert.equal(needsSkillReceipt('code-implement'), false, 'a core skill owes no separate receipt')
47
+ assert.equal(needsSkillReceipt('doc-skill'), true, 'an undeclared extra skill defaults to a command receipt')
48
+ assert.equal(needsSkillReceipt('doc-skill', 'command'), true)
49
+ assert.equal(needsSkillReceipt('doc-skill', 'artifact'), false)
50
+ assert.equal(needsSkillReceipt('doc-skill', 'review'), false)
51
+ assert.equal(needsSkillReceipt('doc-skill', 'manual'), false)
52
+ assert.equal(needsSkillReceipt('doc-skill', 'none'), false)
53
+ })
54
+
55
+ test('evidence: a document-producing skill is satisfied by a recorded artifact', () => {
56
+ // The blocker is the point: without an artifact it blocks, and with one it does
57
+ // not — so the declared kind is genuinely enforced rather than merely accepted.
58
+ //
59
+ // Mounted on 设计, which the standard flow declares an artifact for. A stage with
60
+ // no declared artifact has nothing to satisfy artifact evidence against, and the
61
+ // audit deliberately stays silent there rather than inventing a requirement.
62
+ const adopted = adoptRecommendation('standard')
63
+ const config = resolveFlow('standard', {
64
+ ...adopted,
65
+ stage_bindings: {
66
+ ...adopted.stage_bindings,
67
+ '设计': { skills: [{ skill: { source: 'project', name: 'doc-skill' }, rules: [], evidence: 'artifact' }] },
68
+ },
69
+ }).config
70
+ const session = sessionWith(['doc-skill'])
71
+ const atDesign = extra => ({ ...state({ stage: '设计', ...extra }) })
72
+
73
+ const missing = skillBlockers(atDesign(), config, session)
74
+ assert.ok(missing.some(b => b.includes('artifact evidence')),
75
+ `an artifact-evidence skill must ask for an artifact; got ${JSON.stringify(missing)}`)
76
+ assert.ok(!missing.some(b => b.includes('validation command')),
77
+ 'it must NOT ask for a shell command')
78
+
79
+ // Record what the stage declares and the blocker clears.
80
+ const declared = config.artifacts.find(artifact => artifact.stage === '设计')
81
+ assert.ok(declared !== undefined, 'the standard flow declares an artifact at 设计')
82
+ const cleared = skillBlockers(atDesign({ artifacts: { [declared.id]: { fields: { approach: 'x' } } } }), config, session)
83
+ assert.deepEqual(cleared.filter(b => b.includes('doc-skill')), [],
84
+ 'a recorded artifact satisfies artifact evidence')
85
+ })
86
+
87
+ test('evidence: a judging skill is satisfied by a passing review', () => {
88
+ const config = withEvidence('review')
89
+ const session = sessionWith(['code-implement', 'doc-skill'])
90
+ assert.ok(skillBlockers(state(), config, session).some(b => b.includes('review evidence')),
91
+ 'no review recorded means the review evidence is absent')
92
+ assert.deepEqual(
93
+ skillBlockers(state({ review: { outcome: 'pass' } }), config, session).filter(b => b.includes('doc-skill')), [],
94
+ 'a passing review satisfies it')
95
+ })
96
+
97
+ test('evidence: manual evidence needs an explicit statement, none needs nothing', () => {
98
+ const manual = withEvidence('manual')
99
+ const session = sessionWith(['code-implement', 'doc-skill'])
100
+ assert.ok(skillBlockers(state(), manual, session).some(b => b.includes('manual evidence')),
101
+ 'manual evidence is absent until something is recorded')
102
+ const recorded = state({ skill_results: { '开发': { 'doc-skill': { evidence: ['reviewed by the lead'] } } } })
103
+ assert.deepEqual(skillBlockers(recorded, manual, session).filter(b => b.includes('doc-skill')), [],
104
+ 'a recorded statement satisfies manual evidence')
105
+
106
+ const none = withEvidence('none')
107
+ assert.deepEqual(skillBlockers(state(), none, session).filter(b => b.includes('doc-skill')), [],
108
+ 'an advisory skill needs no proof beyond being loaded')
109
+ })
110
+
111
+ test('evidence: an undeclared extra skill still owes a command receipt', () => {
112
+ // The historical behaviour must survive: a config that says nothing keeps getting
113
+ // the command requirement, so existing setups are unaffected.
114
+ const config = withEvidence(undefined)
115
+ const session = sessionWith(['code-implement', 'doc-skill'])
116
+ const blockers = skillBlockers(state(), config, session)
117
+ assert.ok(blockers.some(b => b.includes('validation command')),
118
+ `an undeclared skill must still owe a command receipt; got ${JSON.stringify(blockers)}`)
119
+ })
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Catalog filtering: search and filters must narrow the list without ever hiding
3
+ * something that is currently in force.
4
+ *
5
+ * The panel shows the configuration; a filter that hid a bound skill would make the
6
+ * panel disagree with the config it is displaying, which is worse than a long list.
7
+ */
8
+ import test from 'node:test'
9
+ import assert from 'node:assert/strict'
10
+ import { bindingsForStage, formatResourceRef } from './lib/engine.js'
11
+ import { adoptRecommendation, resolveFlow } from './lib/workflows.js'
12
+
13
+ /**
14
+ * The filter the panel applies, lifted so it can be tested without a DOM.
15
+ *
16
+ * Mirrors `visibleSkills` in TaskEngineSection: an entry that is bound or shipped
17
+ * always matches; otherwise the query must appear in its name, description or layer.
18
+ */
19
+ function visibleSkills(catalog, binding, presetSkills, query, onlySelected) {
20
+ const boundKeys = new Set((binding?.skills ?? []).map(entry => formatResourceRef(entry.skill)))
21
+ const presetKeys = new Set(presetSkills.map(entry => formatResourceRef(entry.skill)))
22
+ const needle = query.trim().toLowerCase()
23
+ return catalog.filter(entry => {
24
+ const raw = formatResourceRef(entry.ref)
25
+ const inForce = boundKeys.has(raw) || presetKeys.has(raw)
26
+ if (onlySelected && !inForce) return false
27
+ if (needle === '') return true
28
+ return inForce
29
+ || entry.name.toLowerCase().includes(needle)
30
+ || entry.description.toLowerCase().includes(needle)
31
+ || entry.sourceLabel.toLowerCase().includes(needle)
32
+ })
33
+ }
34
+
35
+ const catalog = [
36
+ { ref: { source: 'bundled', name: 'code-implement' }, name: 'code-implement', description: '实现', sourceLabel: '内置' },
37
+ { ref: { source: 'bundled', name: 'code-review' }, name: 'code-review', description: '评审变更', sourceLabel: '内置' },
38
+ { ref: { source: 'project', name: 'api-audit' }, name: 'api-audit', description: '审计接口契约', sourceLabel: '项目' },
39
+ { ref: { source: 'user', name: 'doc-writer' }, name: 'doc-writer', description: '写文档', sourceLabel: '用户' },
40
+ ]
41
+ const binding = { skills: [{ skill: { source: 'project', name: 'api-audit' }, rules: [] }] }
42
+
43
+ test('filter: no query shows everything', () => {
44
+ assert.equal(visibleSkills(catalog, binding, [], '', false).length, 4)
45
+ })
46
+
47
+ test('filter: the query narrows by name, description and layer', () => {
48
+ // `api-audit` is bound in this fixture, so it stays visible whatever the query —
49
+ // that rule is asserted separately, and these expectations include it.
50
+ const names = (...args) => visibleSkills(...args).map(e => e.name).sort()
51
+ assert.deepEqual(names(catalog, binding, [], 'writer', false), ['api-audit', 'doc-writer'],
52
+ 'name matches')
53
+ assert.deepEqual(names(catalog, binding, [], '写文档', false), ['api-audit', 'doc-writer'],
54
+ 'description matches')
55
+ assert.deepEqual(names(catalog, binding, [], '用户', false), ['api-audit', 'doc-writer'],
56
+ 'the layer label matches, so a user can list one layer')
57
+ assert.deepEqual(names(catalog, binding, [], '审计', false), ['api-audit'],
58
+ 'a bound skill matching by its own description is shown')
59
+ assert.deepEqual(names(catalog, binding, [], 'nothing-matches-this', false), ['api-audit'],
60
+ 'a query matching nothing still shows the skill that is in force')
61
+ })
62
+
63
+ test('filter: a BOUND skill is never hidden by the query', () => {
64
+ // The panel shows the configuration. A search that hid a skill currently in force
65
+ // would make the panel disagree with the config it is displaying.
66
+ const shown = visibleSkills(catalog, binding, [], 'doc', false)
67
+ assert.ok(shown.some(e => e.name === 'api-audit'),
68
+ 'the bound skill stays visible even though it does not match the query')
69
+ assert.ok(shown.some(e => e.name === 'doc-writer'), 'and the matching one is shown too')
70
+ })
71
+
72
+ test('filter: only-selected keeps bound and preset skills, drops the rest', () => {
73
+ const shown = visibleSkills(catalog, binding, [{ skill: { source: 'bundled', name: 'code-review' } }], '', true)
74
+ const names = shown.map(e => e.name).sort()
75
+ assert.deepEqual(names, ['api-audit', 'code-review'],
76
+ 'a bound skill and a preset-shipped one are both in force; the other two are not')
77
+ })
78
+
79
+ test('filter: only-selected combined with a query still keeps what is in force', () => {
80
+ const shown = visibleSkills(catalog, binding, [], 'doc', true)
81
+ assert.ok(shown.some(e => e.name === 'api-audit'),
82
+ 'the filter must not drop a bound skill even when a query is active')
83
+ assert.ok(!shown.some(e => e.name === 'code-implement'), 'an unbound non-matching skill is dropped')
84
+ })
85
+
86
+ test('filter: the shipped catalog is small enough that the default view shows it all', () => {
87
+ // Sanity: the initial view of a fresh project is the shipped set, unfiltered.
88
+ const adopted = adoptRecommendation('standard')
89
+ const resolved = resolveFlow('standard', adopted).config
90
+ const binding = bindingsForStage('开发', resolved)
91
+ assert.ok(binding !== undefined, 'the adopted config binds 开发')
92
+ assert.ok((binding.skills ?? []).length > 0)
93
+ })
package/.freeze-test.mjs CHANGED
@@ -5,6 +5,7 @@
5
5
  * running task used silently changed what that task was doing, and deleting one
6
6
  * made a configured constraint disappear without the task recording anything.
7
7
  */
8
+ import { adoptRecommendation } from './lib/workflows.js'
8
9
  import test from 'node:test'
9
10
  import assert from 'node:assert/strict'
10
11
  import { join, resolve } from 'node:path'
@@ -15,7 +16,10 @@ import { hashText } from './lib/snapshot.js'
15
16
  async function created(flow) {
16
17
  const cwd = resolve('freeze-project')
17
18
  const records = new Map([
18
- [join(cwd, '.dsh/eng.json'), JSON.stringify({ flow })],
19
+ // A project that adopted the shipped recommendation, which is what gives it
20
+ // skills and rules to freeze. `{flow}` alone adopts nothing and therefore has
21
+ // nothing to freeze — a legitimate state, but not the one under test.
22
+ [join(cwd, '.dsh/eng.json'), JSON.stringify(adoptRecommendation(flow))],
19
23
  [join(cwd, 'a.js'), 'source'],
20
24
  ])
21
25
  const events = []
@@ -87,4 +91,109 @@ test('freeze: every preset freezes a complete, non-empty set', async () => {
87
91
  assert.ok((state.flow.resources ?? []).length >= 3,
88
92
  `${flow} must freeze its resources; saw ${(state.flow.resources ?? []).length}`)
89
93
  }
94
+ })
95
+
96
+ /**
97
+ * A task carrying one project rule on a skill, parked at the stage that uses it.
98
+ *
99
+ * Built from the adopted recommendation so the binding is one the resolver really
100
+ * accepts, then given a project rule of its own — exactly what the workbench
101
+ * writes when a user edits a skill's rule list.
102
+ */
103
+ async function atDevelopmentWithProjectRule() {
104
+ const cwd = resolve('freeze-live')
105
+ const adopted = adoptRecommendation('standard')
106
+ const skills = adopted.stage_bindings['开发'].skills.map(entry => ({
107
+ ...entry,
108
+ rules: [...entry.rules, { source: 'project', name: 'mine' }],
109
+ }))
110
+ const project = { ...adopted, stage_bindings: { ...adopted.stage_bindings, '开发': { skills } } }
111
+ const records = new Map([
112
+ [join(cwd, '.dsh/eng.json'), JSON.stringify(project)],
113
+ [join(cwd, '.dsh/rules/mine.md'), 'ORIGINAL RULE'],
114
+ [join(cwd, 'a.js'), 'source'],
115
+ ])
116
+ let execute
117
+ const key = (p, options) => join(options?.cwd ?? cwd, p)
118
+ const fs = {
119
+ resolve: async (p, options) => ({ targetKey: key(p, options) }),
120
+ readText: async target => records.get(target.targetKey),
121
+ listDir: async () => [],
122
+ lstat: async (p, options) => records.has(key(p, options)) ? { version: 'v' } : undefined,
123
+ writeText: async (target, content) => { records.set(target.targetKey, content) },
124
+ }
125
+ const ctx = {
126
+ fs,
127
+ tools: { register(tool) { execute = tool.execute; return () => {} } },
128
+ get: name => name === 'sandboxPolicy'
129
+ ? { resolve: () => ({ mode: 'workspace-write', workspaceRoot: cwd, sessionId: 'f' }) }
130
+ : undefined,
131
+ }
132
+ registerDevTask(ctx)
133
+ const exec = { agent: { session: { id: 'f', header: { cwd }, snapshotEvents: () => [] } }, signal: new AbortController().signal }
134
+ const call = async args => execute(JSON.parse(JSON.stringify({ task_id: 'FZ-2', ...args })), exec)
135
+ await call({ operation: 'create', title: 'freeze', branch: 'main', files: ['a.js'] })
136
+ const state = JSON.parse(records.get(join(cwd, '.dsh/task-FZ-2.json')))
137
+ state.stage = '开发'
138
+ state.requirement_confirmed = true
139
+ state.solution_confirmed = true
140
+ records.set(join(cwd, '.dsh/task-FZ-2.json'), JSON.stringify(state))
141
+ return {
142
+ call,
143
+ records,
144
+ cwd,
145
+ rule: () => join(cwd, '.dsh/rules/mine.md'),
146
+ disclosed: async () => {
147
+ const status = JSON.parse(await call({ operation: 'status' }))
148
+ return { status, mine: status.bindings.rules.find(r => r.name === 'mine') }
149
+ },
150
+ }
151
+ }
152
+
153
+ test('freeze: a running task follows the FROZEN body after its source is edited', async () => {
154
+ // Freezing that only archives the content is not freezing: status and the stage
155
+ // disclosure used to read the live files, so editing a rule silently changed what
156
+ // a running task was told to follow — the instability the snapshot exists to stop.
157
+ const f = await atDevelopmentWithProjectRule()
158
+ const before = await f.disclosed()
159
+ assert.equal(before.mine?.content, 'ORIGINAL RULE', 'the frozen body is what gets disclosed')
160
+
161
+ f.records.set(f.rule(), 'CHANGED RULE')
162
+ const after = await f.disclosed()
163
+ assert.equal(after.mine?.content, 'ORIGINAL RULE',
164
+ 'an edit to the source must not change what the running task is told to follow')
165
+ assert.ok((after.status.stale_source_rules ?? []).some(r => r.includes('edited')),
166
+ 'the drift is reported so it is visible rather than silently authoritative')
167
+ })
168
+
169
+ test('freeze: a running task survives its source rule being deleted', async () => {
170
+ // The frozen copy exists precisely so a deleted file is not a missing
171
+ // constraint. Reporting it as missing would be wrong: the task has the body.
172
+ const f = await atDevelopmentWithProjectRule()
173
+ f.records.delete(f.rule())
174
+ const { status, mine } = await f.disclosed()
175
+ assert.equal(mine?.content, 'ORIGINAL RULE', 'the frozen body is still in force')
176
+ assert.deepEqual(status.missing_rules, [],
177
+ 'a frozen rule is not missing; the task already holds its content')
178
+ assert.ok((status.stale_source_rules ?? []).some(r => r.includes('deleted')),
179
+ 'the deletion is reported as drift')
180
+ })
181
+
182
+ test('freeze: an unfrozen task falls back to live resolution and reports a missing file', async () => {
183
+ // Tasks created before freezing have no snapshot. They must keep working, and a
184
+ // deleted rule must be reported as missing rather than silently unbound — the
185
+ // two paths are distinguishable and neither silently degrades into the other.
186
+ const f = await atDevelopmentWithProjectRule()
187
+ const state = JSON.parse(f.records.get(join(f.cwd, '.dsh/task-FZ-2.json')))
188
+ delete state.flow.resources
189
+ f.records.set(join(f.cwd, '.dsh/task-FZ-2.json'), JSON.stringify(state))
190
+
191
+ const live = await f.disclosed()
192
+ assert.equal(live.mine?.content, 'ORIGINAL RULE', 'an unfrozen task reads the live file')
193
+
194
+ f.records.delete(f.rule())
195
+ const { status, mine } = await f.disclosed()
196
+ assert.equal(mine, undefined, 'with no snapshot and no file, the rule cannot resolve')
197
+ assert.ok((status.missing_rules ?? []).includes('project:mine'),
198
+ 'it is reported as missing so the gate can block on it')
90
199
  })
package/.hook-test.mjs CHANGED
@@ -100,10 +100,15 @@ function approvedTask(overrides = {}) {
100
100
  }
101
101
  }
102
102
 
103
- /** Resolve the standard preset's config the same way the hook does. */
103
+ /**
104
+ * Resolve the standard preset's config the way a real project has it: the skeleton
105
+ * plus the shipped recommendation, adopted. The hook reads the project's config
106
+ * from disk, so a project that adopted the recommendation is what these tests
107
+ * describe — the bare skeleton has no commit rule or artifacts to check.
108
+ */
104
109
  async function standardConfig() {
105
- const { resolveFlow } = await import('./lib/workflows.js')
106
- const resolved = resolveFlow('standard')
110
+ const { resolveFlow, adoptRecommendation } = await import('./lib/workflows.js')
111
+ const resolved = resolveFlow('standard', adoptRecommendation('standard'))
107
112
  assert.equal(resolved.ok, true, 'the built-in standard preset resolves')
108
113
  return resolved.config
109
114
  }