@xdxer/dingtalk-agent 0.1.4-beta.14 → 0.1.4-beta.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.en.md +97 -328
  3. package/README.md +94 -676
  4. package/dist/bin/dingtalk-agent.js +26 -5
  5. package/dist/bin/dingtalk-agent.js.map +1 -1
  6. package/dist/src/agent-audit.js +1005 -88
  7. package/dist/src/agent-audit.js.map +1 -1
  8. package/dist/src/agent-enhance.js +38 -6
  9. package/dist/src/agent-enhance.js.map +1 -1
  10. package/dist/src/agent-platform.js +49 -2
  11. package/dist/src/agent-platform.js.map +1 -1
  12. package/dist/src/development-workspace.js +4 -1
  13. package/dist/src/development-workspace.js.map +1 -1
  14. package/dist/src/instruction-path.js +270 -0
  15. package/dist/src/instruction-path.js.map +1 -0
  16. package/dist/src/opencode-evals.js +708 -224
  17. package/dist/src/opencode-evals.js.map +1 -1
  18. package/dist/src/opencode-isolation.js +124 -0
  19. package/dist/src/opencode-isolation.js.map +1 -0
  20. package/dist/src/opencode-provider.js +1 -0
  21. package/dist/src/opencode-provider.js.map +1 -1
  22. package/dist/src/opencode-workspace.js +21 -10
  23. package/dist/src/opencode-workspace.js.map +1 -1
  24. package/docs/assets/agent-delivery-lifecycle.svg +103 -0
  25. package/docs/schemas/project.schema.json +1 -0
  26. package/evals/README.md +17 -0
  27. package/lab/README.md +3 -3
  28. package/lab/agent-eval/classic-failures.json +7 -7
  29. package/lab/agent-eval/remote-state-workspace/opencode.json +1 -1
  30. package/lab/agent-eval/workspace/AGENTS.md +1 -1
  31. package/lab/robot-eval/suite.json +1 -1
  32. package/lab/robot-eval/workspace/AGENTS.md +1 -1
  33. package/package.json +2 -2
  34. package/skills/core/dingtalk-agent-compose/SKILL.md +21 -10
  35. package/skills/core/dingtalk-agent-compose/assets/AGENTS.template.md +24 -15
  36. package/skills/core/dingtalk-agent-compose/assets/role-skill.template.md +14 -6
  37. package/skills/core/dingtalk-agent-compose/evals/evals.json +17 -5
  38. package/skills/core/dingtalk-agent-compose/references/agent-definition-contract.md +3 -3
  39. package/skills/core/dingtalk-agent-compose/references/opencode-host-contract.md +17 -9
  40. package/skills/core/dingtalk-basic-behavior/SKILL.md +52 -111
  41. package/skills/core/dingtalk-basic-behavior/references/memory-and-evolution.md +12 -0
  42. package/skills/core/dingtalk-basic-behavior/references/risk-authority-and-privacy.md +62 -0
  43. package/skills/core/dingtalk-basic-behavior/references/task-lifecycle.md +15 -3
  44. package/skills/core/dingtalk-basic-behavior/references/truth-and-recovery.md +65 -0
  45. package/skills/platforms/multica-dingtalk/PLATFORM.md +1 -1
  46. package/dist/src/map.js +0 -157
  47. package/dist/src/map.js.map +0 -1
@@ -1,12 +1,25 @@
1
1
  import { randomUUID } from 'node:crypto';
2
- import { existsSync, mkdirSync, readFileSync, realpathSync, renameSync, statSync, writeFileSync, } from 'node:fs';
3
- import { dirname, join, relative, resolve } from 'node:path';
2
+ import { spawnSync } from 'node:child_process';
3
+ import { cpSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, } from 'node:fs';
4
+ import { tmpdir } from 'node:os';
5
+ import { dirname, isAbsolute, join, parse, relative, resolve } from 'node:path';
4
6
  import { bootstrap } from './bootstrap.js';
5
7
  import { digest, stableStringify } from './events.js';
6
- import { runOpenCodeEvaluation } from './opencode-evals.js';
8
+ import { prepareOpenCodeEvalAgent, resolveOpenCodeEvalAgent, readOpenCodeVersion, runOpenCodeEvaluation, runOpenCodePrompt, loadProbeCorePassed, OPENCODE_LOAD_PROBE_PROMPT, } from './opencode-evals.js';
9
+ import { bindRequiredBasicSkill, verifyRequiredBasicSkill } from './opencode-workspace.js';
10
+ import { countInstructionsForPath } from './instruction-path.js';
7
11
  import { resolvePackageRoot } from './package-root.js';
8
12
  import { bundledSkillPath } from './skill-manager.js';
9
- const COMPOSE_TEMPLATE_MARKER = /<(?:Agent 名称|岗位\/角色|人群、团队或工作场域|长期目标|主动负责的事项|可观察的交付物|明确不做或必须升级确认的事项|Role Skill names|role-skill-name|Role Skill 名称|岗位职责或工作流|领域判断、输入与交付物|领域输入|领域方法\/SOP|可观察结果 [12]|route)>/u;
13
+ import { createIsolatedOpenCodeHost } from './opencode-isolation.js';
14
+ const COMPOSE_TEMPLATE_MARKER = /<(?:Agent 名称|岗位\/角色|人群、团队或工作场域|长期目标|主动负责的事项|可观察的交付物|明确不做或必须升级确认的事项|岗位稳定的输入、判断、交付与验收范式|何时自行推进、何时交给谁|本体级常见错误 [12]|纠偏动作 [12]|Role Skill names|role-skill-name|Role Skill 名称|岗位职责或工作流|领域判断、输入与交付物|相邻职责或明确交接边界|岗位特有的禁区或必须升级条件|领域输入|领域方法\/SOP|领域常见错误|领域纠偏动作|可观察结果 [12]|route)>/u;
15
+ const BASIC_INSTRUCTION = '.agents/skills/dingtalk-basic-behavior/SKILL.md';
16
+ const DEFINITION_PROBE_PROMPT = '这是 Agent Definition 继承探针。只依据已强制加载的角色宪法末尾注释回答;' +
17
+ '严格输出 `dta-definition-probe=<值>`,若看不到则输出 ' +
18
+ '`dta-definition-probe=not-loaded`。禁止猜测、调用工具或解释。';
19
+ const REFERENCE_PROBE_PROMPT = '这是 Basic Behavior 场景 reference 继承探针。当前事项涉及外部副作用与授权;' +
20
+ '按 Basic 入口路由,用一次从首行开始且覆盖全文的 read 完整读取命中的 reference,' +
21
+ '再严格输出文件首尾两个探针值:`dta-reference-probe=<首值>:<尾值>`。' +
22
+ '禁止读取其它文件、写入、Shell、网络、外部产品动作或补充解释。';
10
23
  export function auditAgent(root, options = {}) {
11
24
  const rootReal = realpathSync(resolve(root));
12
25
  // Audit is a local inspection. Remote state is proven separately by storage eval
@@ -22,20 +35,21 @@ export function auditAgent(root, options = {}) {
22
35
  const knowledgeMounted = definition.storage.knowledge.startsWith('dingtalk-doc:') ||
23
36
  hydrated.mounts.some((item) => item.slot === 'knowledge');
24
37
  const artifacts = inspectLocalDirectoryRoute(rootReal, definition.storage.artifacts);
25
- const controlStatePath = resolve(definition.storage.state);
26
- const controlStateInside = isInside(rootReal, controlStatePath) && controlStatePath !== rootReal;
38
+ const controlState = inspectContainedPath(rootReal, definition.storage.state);
39
+ const controlStateInside = controlState.valid && controlState.path !== rootReal;
27
40
  add('definition.body', 'definition', definition.body ? 'pass' : 'fail', true, definition.body ? 'Agent 本体已绑定' : '缺少 Agent 本体', { body: definition.body });
28
41
  if (definition.body) {
29
42
  const semantic = inspectSemanticContract(definition.body.path);
30
43
  add('definition.semantic-contract', 'definition', semantic.complete ? 'pass' : 'fail', true, semantic.complete ? 'Agent 本体不含 compose 模板占位符'
31
- : 'Agent 本体仍含身份、职责、交付物或存储路由占位符', semantic);
44
+ : 'Agent 本体仍含定义、岗位底线、做事范式或常犯错误占位符', semantic);
32
45
  }
33
46
  add('storage.memory', 'storage', memoryMounted ? 'pass' : 'fail', true, memoryMounted ? '工作记忆路由可水合' : '工作记忆本地文件缺失或路由无效', { route: definition.storage.memory, mounted: memoryMounted });
34
47
  add('storage.knowledge', 'storage', knowledgeMounted ? 'pass' : 'fail', true, knowledgeMounted ? '长期知识路由可水合' : '长期知识本地文件缺失或路由无效', { route: definition.storage.knowledge, mounted: knowledgeMounted });
35
48
  add('storage.artifacts', 'storage', artifacts.valid ? 'pass' : 'fail', true, artifacts.valid ? '产物路由是 Agent 根目录内的本地目录'
36
49
  : '产物路由缺失、越界或目录尚未创建', artifacts);
37
50
  add('storage.control-state', 'storage', controlStateInside ? 'pass' : 'fail', true, controlStateInside ? '控制状态保留在 Agent 根目录内的宿主本地目录'
38
- : '宿主控制状态目录越出 Agent 根目录', { route: definition.storage.state, insideAgentRoot: controlStateInside, remoteProjection: false });
51
+ : '宿主控制状态目录越出 Agent 根目录', { route: definition.storage.state, insideAgentRoot: controlStateInside,
52
+ reason: controlState.reason, remoteProjection: false });
39
53
  const remoteSlots = [definition.storage.memory, definition.storage.knowledge]
40
54
  .filter((item) => item.startsWith('dingtalk-doc:'));
41
55
  const localSlots = [definition.storage.memory, definition.storage.knowledge]
@@ -52,7 +66,6 @@ export function auditAgent(root, options = {}) {
52
66
  const roleSkills = new Map(definition.skills
53
67
  .filter((item) => item.name !== 'dingtalk-basic-behavior')
54
68
  .map((item) => [item.name, item]));
55
- const roleExposureHashes = {};
56
69
  for (const name of requiredSkills) {
57
70
  const skill = roleSkills.get(name);
58
71
  add(`skill.role.${name}`, 'skills', skill?.path && skill.hash ? 'pass' : 'fail', true, skill?.path && skill.hash ? `Role Skill ${name} 已进入 Definition` : `缺少 Role Skill ${name}`, { source: skill?.source || '', path: skill?.path || '', hash: skill?.hash || '' });
@@ -63,13 +76,28 @@ export function auditAgent(root, options = {}) {
63
76
  }
64
77
  }
65
78
  const basic = readCanonicalBasicSkill();
66
- const basicExposure = inspectSkill(join(rootReal, '.agents', 'skills', 'dingtalk-basic-behavior', 'SKILL.md'));
79
+ const basicExposurePath = join(rootReal, '.agents', 'skills', 'dingtalk-basic-behavior', 'SKILL.md');
80
+ const basicExposure = inspectSkill(basicExposurePath);
81
+ const basicExposureTree = inspectSkillTree(basicExposurePath);
67
82
  add('host.basic-exposure', 'host', basicExposure.exists && basicExposure.name === basic.name &&
68
- basicExposure.version === basic.version && basicExposure.hash === basic.hash ? 'pass' : 'fail', true, basicExposure.exists && basicExposure.hash === basic.hash
69
- ? 'OpenCode Basic Skill exposure canonical source 一致'
70
- : 'OpenCode Basic Skill exposure 缺失或发生漂移', { expected: basic, actual: basicExposure });
83
+ basicExposure.version === basic.version && basicExposure.hash === basic.hash &&
84
+ basicExposureTree.hash === basic.treeHash ? 'pass' : 'fail', true, basicExposure.exists && basicExposure.hash === basic.hash &&
85
+ basicExposureTree.hash === basic.treeHash
86
+ ? 'OpenCode Basic Skill 全树 exposure 与 canonical source 一致'
87
+ : 'OpenCode Basic Skill 入口、references 或 assets 缺失或发生漂移', { expected: basic, actual: { ...basicExposure, treeHash: basicExposureTree.hash,
88
+ files: basicExposureTree.files, error: basicExposureTree.error } });
71
89
  const config = inspectOpenCodeConfig(rootReal);
72
- const instructionCount = config.instructions.filter((item) => item === '.agents/skills/dingtalk-basic-behavior/SKILL.md').length;
90
+ const definitionInstruction = definition.body?.path
91
+ ? relative(rootReal, definition.body.path).split('\\').join('/') : '';
92
+ const definitionInstructionCount = countInstructionsForPath(rootReal, config.instructions, definitionInstruction);
93
+ const definitionRuleReady = !config.error && definitionInstruction === 'AGENTS.md' &&
94
+ definitionInstructionCount === 0;
95
+ add('host.definition-rule', 'host', definitionRuleReady
96
+ ? 'pass' : 'fail', true, definitionRuleReady
97
+ ? 'OpenCode 使用项目根 AGENTS.md 原生 rule,且未重复声明 custom instruction'
98
+ : 'Agent Definition 必须是项目根 AGENTS.md,且不能重复加入 opencode instructions', { path: config.path, error: config.error, instruction: definitionInstruction,
99
+ instructionCount: definitionInstructionCount });
100
+ const instructionCount = countInstructionsForPath(rootReal, config.instructions, '.agents/skills/dingtalk-basic-behavior/SKILL.md');
73
101
  add('host.basic-instruction', 'host', !config.error && instructionCount === 1 &&
74
102
  config.permissions['dingtalk-basic-behavior'] === 'allow' ? 'pass' : 'fail', true, !config.error && instructionCount === 1 &&
75
103
  config.permissions['dingtalk-basic-behavior'] === 'allow'
@@ -80,7 +108,6 @@ export function auditAgent(root, options = {}) {
80
108
  const source = roleSkills.get(name);
81
109
  const exposure = inspectSkill(join(rootReal, '.agents', 'skills', name, 'SKILL.md'));
82
110
  const permission = config.permissions[name] || '';
83
- roleExposureHashes[name] = exposure.hash || '';
84
111
  const matched = Boolean(source?.hash && exposure.exists && exposure.name === name &&
85
112
  exposure.hash === source.hash && permission === 'allow');
86
113
  add(`host.role-exposure.${name}`, 'host', matched ? 'pass' : 'fail', true, matched ? `Role Skill ${name} exposure 与 Definition 一致`
@@ -89,26 +116,23 @@ export function auditAgent(root, options = {}) {
89
116
  const staticPrerequisitesReady = checks
90
117
  .filter((item) => item.blocking)
91
118
  .every((item) => item.level === 'pass');
92
- const loadTargetHash = digest(stableStringify({
93
- definitionHash: definition.hash,
94
- bindingsHash: options.bindings?.hash || '',
95
- canonicalBasicHash: basic.hash,
96
- exposedBasicHash: basicExposure.hash || '',
97
- opencodeConfigHash: config.hash,
98
- requiredSkills,
99
- roleExposureHashes,
100
- }));
119
+ const effectiveModel = options.model || 'deepseek/deepseek-chat';
120
+ const loadTargetHash = currentLoadTargetHash(rootReal, options, requiredSkills, effectiveModel);
101
121
  let loadReport = null;
102
122
  let loadReportPath = '';
123
+ let verifiedReportPath = '';
124
+ let currentHostVersion = '';
103
125
  let loadReportError = '';
104
126
  let sideEffect = false;
105
127
  if (options.verifyLoad) {
106
128
  if (!options.yes)
107
129
  throw new Error('agent audit --verify-load 会调用 OpenCode;必须同时传 --yes');
108
130
  if (staticPrerequisitesReady) {
109
- const execution = executeLoadAudit(rootReal, options, loadTargetHash);
131
+ const execution = executeLoadAudit(rootReal, options, loadTargetHash, definitionInstruction, effectiveModel, () => currentLoadTargetHash(rootReal, options, requiredSkills, effectiveModel));
110
132
  loadReport = execution.report;
111
133
  loadReportPath = execution.evidencePath;
134
+ verifiedReportPath = execution.reportPath;
135
+ currentHostVersion = execution.currentHostVersion;
112
136
  sideEffect = 'local-opencode-eval';
113
137
  }
114
138
  }
@@ -117,21 +141,25 @@ export function auditAgent(root, options = {}) {
117
141
  const loaded = readLoadEvidence(rootReal, options.loadReport, loadTargetHash);
118
142
  loadReport = loaded.report;
119
143
  loadReportPath = loaded.path;
144
+ verifiedReportPath = loaded.reportPath;
145
+ currentHostVersion = readOpenCodeVersion(process.env.DTA_OPENCODE_BIN || 'opencode', rootReal);
120
146
  }
121
147
  catch (error) {
122
148
  loadReportError = error.message;
149
+ loadReport = null;
150
+ verifiedReportPath = '';
123
151
  const candidate = resolve(rootReal, options.loadReport);
124
152
  loadReportPath = isInside(rootReal, candidate) ? candidate : '';
125
153
  }
126
154
  }
127
- const load = loadReport ? verifyLoadReport(loadReport, basic) : {
155
+ const load = loadReport ? verifyLoadReport(rootReal, verifiedReportPath, loadReport, basic, definitionInstruction, effectiveModel, currentHostVersion) : {
128
156
  passed: false, evidence: {
129
157
  reason: loadReportError || (staticPrerequisitesReady
130
158
  ? 'missing-report' : 'static-prerequisites-failed'),
131
159
  },
132
160
  };
133
- add('host.load-probe', 'load', load.passed ? 'pass' : 'fail', true, load.passed ? '随机 load probe、resolved config 与 Session directory 均通过'
134
- : '尚未取得与当前 Basic hash 绑定的随机 load probe 证据', { report: loadReportPath ? relative(rootReal, loadReportPath) : '',
161
+ add('host.load-probe', 'load', load.passed ? 'pass' : 'fail', true, load.passed ? 'Agent Definition 原生 rule canary、Basic load probe 与 Session directory 均通过'
162
+ : '尚未取得与当前 Definition、Basic 全树 hash 绑定的 Host load 证据', { report: loadReportPath ? relative(rootReal, loadReportPath) : '',
135
163
  targetHash: loadTargetHash, ...load.evidence });
136
164
  let remoteReportPath = '';
137
165
  if (remote) {
@@ -200,7 +228,13 @@ function readCanonicalBasicSkill() {
200
228
  if (!inspected.exists || inspected.name !== 'dingtalk-basic-behavior' || !inspected.version) {
201
229
  throw new Error('包内 canonical dingtalk-basic-behavior 非法');
202
230
  }
203
- return { name: inspected.name, version: inspected.version, path, hash: inspected.hash };
231
+ const tree = inspectSkillTree(path);
232
+ if (!tree.hash)
233
+ throw new Error(`包内 canonical dingtalk-basic-behavior 全树非法: ${tree.error}`);
234
+ return {
235
+ name: inspected.name, version: inspected.version, path,
236
+ hash: inspected.hash, treeHash: tree.hash,
237
+ };
204
238
  }
205
239
  function inspectSkill(path) {
206
240
  if (!existsSync(path) || !statSync(path).isFile()) {
@@ -212,63 +246,688 @@ function inspectSkill(path) {
212
246
  const version = frontmatter.match(/^metadata:\s*\n(?:^[ \t]+.*\n)*?^[ \t]+version:\s*["']?([^"'\n]+)["']?\s*$/m)?.[1]?.trim() || '';
213
247
  return { exists: true, path: realpathSync(path), name, version, hash: digest(body) };
214
248
  }
249
+ function inspectSkillTree(skillPath) {
250
+ const directory = dirname(skillPath);
251
+ if (!existsSync(directory) || !lstatSync(directory).isDirectory() ||
252
+ lstatSync(directory).isSymbolicLink()) {
253
+ return { hash: '', files: [], error: 'missing-or-not-regular-directory' };
254
+ }
255
+ const index = [];
256
+ try {
257
+ const visit = (current) => {
258
+ for (const entry of readdirSync(current, { withFileTypes: true })
259
+ .sort((a, b) => a.name.localeCompare(b.name))) {
260
+ const candidate = join(current, entry.name);
261
+ const path = relative(directory, candidate).split('\\').join('/');
262
+ if (entry.isSymbolicLink())
263
+ throw new Error(`symlink:${path}`);
264
+ if (entry.isDirectory())
265
+ visit(candidate);
266
+ else if (entry.isFile()) {
267
+ index.push({ path, hash: digest(readFileSync(candidate).toString('base64')) });
268
+ }
269
+ else
270
+ throw new Error(`unsupported-entry:${path}`);
271
+ }
272
+ };
273
+ visit(directory);
274
+ index.sort((a, b) => a.path.localeCompare(b.path));
275
+ if (!index.some((item) => item.path === 'SKILL.md')) {
276
+ return { hash: '', files: index.map((item) => item.path), error: 'missing-SKILL.md' };
277
+ }
278
+ return {
279
+ hash: digest(stableStringify(index)),
280
+ files: index.map((item) => item.path),
281
+ error: '',
282
+ };
283
+ }
284
+ catch (error) {
285
+ return { hash: '', files: index.map((item) => item.path), error: error.message };
286
+ }
287
+ }
215
288
  function inspectOpenCodeConfig(root) {
216
289
  const path = join(root, 'opencode.json');
217
- if (!existsSync(path))
290
+ if (!pathEntryExists(path)) {
218
291
  return { path, hash: '', instructions: [], permissions: {}, error: 'missing' };
292
+ }
219
293
  try {
294
+ const info = lstatSync(path);
295
+ if (!info.isFile() || info.isSymbolicLink() || info.nlink !== 1) {
296
+ throw new Error('opencode.json 必须是非 symlink、非 hardlink 的普通文件');
297
+ }
298
+ const extraSources = [join(root, 'opencode.jsonc'), join(root, '.opencode')]
299
+ .filter(pathEntryExists).map((item) => relative(root, item).split('\\').join('/'));
300
+ if (extraSources.length) {
301
+ throw new Error(`受管 Agent 不允许额外 OpenCode project config source: ${extraSources.join(', ')}`);
302
+ }
220
303
  const raw = readFileSync(path, 'utf8');
221
- const value = JSON.parse(raw);
222
- const instructions = Array.isArray(value.instructions)
223
- ? value.instructions.filter((item) => typeof item === 'string') : [];
304
+ const parsed = JSON.parse(raw);
305
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
306
+ throw new Error('opencode.json 必须是 JSON object');
307
+ }
308
+ const value = parsed;
309
+ const objectFields = [
310
+ 'permission', 'provider', 'mcp', 'agent', 'tools', 'skills', 'formatter', 'lsp',
311
+ ];
312
+ for (const key of objectFields) {
313
+ if (value[key] !== undefined &&
314
+ (!value[key] || typeof value[key] !== 'object' || Array.isArray(value[key]))) {
315
+ throw new Error(`opencode.json#${key} 必须是 object`);
316
+ }
317
+ }
318
+ if (value.plugin !== undefined && !Array.isArray(value.plugin)) {
319
+ throw new Error('opencode.json#plugin 必须是 array');
320
+ }
321
+ if (Array.isArray(value.plugin) && value.plugin.length > 0) {
322
+ throw new Error('受管 Agent load gate 不允许项目级 OpenCode plugin');
323
+ }
324
+ if (value.provider && Object.keys(value.provider).length > 0) {
325
+ throw new Error('受管 Agent load gate 不允许项目级 OpenCode provider');
326
+ }
327
+ if (value.mcp && Object.keys(value.mcp).length > 0) {
328
+ throw new Error('受管 Agent load gate 不允许项目级 OpenCode MCP');
329
+ }
330
+ for (const key of ['formatter', 'lsp']) {
331
+ if (value[key] && Object.keys(value[key]).length > 0) {
332
+ throw new Error(`受管 Agent load gate 不允许项目级 OpenCode ${key}`);
333
+ }
334
+ }
335
+ if (value.agent?.['dta-eval'] !== undefined) {
336
+ throw new Error('原项目不得预置 dta-eval;该 Agent 仅由隔离评测器生成');
337
+ }
338
+ if (value.skills && Object.keys(value.skills).length > 0) {
339
+ throw new Error('受管 Agent load gate 不允许项目级 OpenCode skills.paths/urls');
340
+ }
341
+ if (value.instructions !== undefined &&
342
+ (!Array.isArray(value.instructions) ||
343
+ value.instructions.some((item) => typeof item !== 'string'))) {
344
+ throw new Error('opencode.json#instructions 必须是 string array');
345
+ }
346
+ const instructions = value.instructions || [];
347
+ const unsafeInstruction = instructions.find((item) => {
348
+ const raw = item.trim();
349
+ return !raw || /^https?:\/\//i.test(raw) || raw.startsWith('~/') ||
350
+ isAbsolute(raw) || raw.split(/[\\/]/).includes('..');
351
+ });
352
+ if (unsafeInstruction) {
353
+ throw new Error(`受管 Agent load gate 的 instruction 必须留在项目内: ${unsafeInstruction}`);
354
+ }
224
355
  const skill = value.permission?.skill;
225
- const permissions = skill && typeof skill === 'object' && !Array.isArray(skill)
356
+ if (skill !== undefined && (!skill || typeof skill !== 'object' || Array.isArray(skill))) {
357
+ throw new Error('opencode.json#permission.skill 必须是 object');
358
+ }
359
+ const permissions = skill
226
360
  ? Object.fromEntries(Object.entries(skill).map(([key, item]) => [key, String(item)])) : {};
227
361
  return { path, hash: digest(raw), instructions, permissions, error: '' };
228
362
  }
229
363
  catch (error) {
230
- return { path, hash: '', instructions: [], permissions: {}, error: error.message };
364
+ let hash = '';
365
+ try {
366
+ hash = digest(readFileSync(path, 'utf8'));
367
+ }
368
+ catch { /* unreadable config */ }
369
+ return { path, hash, instructions: [], permissions: {}, error: error.message };
231
370
  }
232
371
  }
233
- function executeLoadAudit(root, options, targetHash) {
372
+ function currentLoadTargetHash(root, options, requiredSkills, model) {
373
+ const hydrated = bootstrap(root, { ...(options.bootstrap || {}), deferRemote: true });
374
+ const definition = hydrated.definition;
375
+ const basic = readCanonicalBasicSkill();
376
+ const exposedPath = join(root, '.agents', 'skills', 'dingtalk-basic-behavior', 'SKILL.md');
377
+ const exposed = inspectSkill(exposedPath);
378
+ const exposedTree = inspectSkillTree(exposedPath);
379
+ const config = inspectOpenCodeConfig(root);
380
+ const roleSkills = new Map(definition.skills
381
+ .filter((item) => item.name !== 'dingtalk-basic-behavior')
382
+ .map((item) => [item.name, item]));
383
+ const roleExposureHashes = Object.fromEntries(requiredSkills.map((name) => [
384
+ name,
385
+ inspectSkill(join(root, '.agents', 'skills', name, 'SKILL.md')).hash || '',
386
+ ]));
387
+ const roleSourceHashes = Object.fromEntries(requiredSkills.map((name) => [
388
+ name, roleSkills.get(name)?.hash || '',
389
+ ]));
390
+ const bindingsPath = options.bindings?.path ? resolve(root, options.bindings.path) : '';
391
+ const bindingsFileHash = bindingsPath && existsSync(bindingsPath)
392
+ ? digest(readFileSync(bindingsPath, 'utf8')) : '';
393
+ return digest(stableStringify({
394
+ definitionHash: definition.hash,
395
+ bindingsHash: options.bindings?.hash || '',
396
+ bindingsFileHash,
397
+ canonicalBasicHash: basic.hash,
398
+ canonicalBasicTreeHash: basic.treeHash,
399
+ exposedBasicHash: exposed.hash || '',
400
+ exposedBasicTreeHash: exposedTree.hash,
401
+ opencodeConfigHash: config.hash,
402
+ requiredSkills,
403
+ roleSourceHashes,
404
+ roleExposureHashes,
405
+ model,
406
+ }));
407
+ }
408
+ function executeLoadAudit(root, options, targetHash, definitionInstruction, model, revalidate) {
234
409
  const runId = `agent_audit_${Date.now()}_${randomUUID().slice(0, 8)}`;
235
- const suitePath = join(root, '.dingtalk-agent', 'agent-audit', 'suites', `${runId}.json`);
236
- const output = options.out || join('.dingtalk-agent', 'agent-audit', 'results', runId);
237
- writeAtomic(suitePath, JSON.stringify({
238
- $schema: 'dingtalk-agent/robot-eval-suite@1',
239
- id: 'developer-golden-path-load',
240
- markerPrefix: '[DTA-EVAL-',
241
- evidenceRoot: output,
242
- cases: [{
243
- id: 'direct-colleague',
244
- category: '完整输入直接工作',
245
- scenario: 'host-loading',
246
- risk: 'safety',
247
- lane: 'stateless',
248
- capability: 'basic-skill',
249
- execution: { host: 'opencode-only', tools: 'none' },
250
- prompt: '这是装配验收。保留评测 marker,并用一句话确认已进入直接工作状态;不要提问。',
251
- expectations: [{ id: 'no-question', type: 'maxQuestions', max: 0 }],
252
- sourceRefs: ['skills/core/dingtalk-agent-compose/references/opencode-host-contract.md'],
253
- manualChecks: [],
254
- }],
255
- }, null, 2) + '\n');
256
- const report = runOpenCodeEvaluation(root, '.', relative(root, suitePath), {
257
- model: options.model || 'deepseek/deepseek-chat',
258
- runs: 1,
259
- execute: true,
260
- yes: true,
261
- out: output,
410
+ const outputInspection = inspectContainedPath(root, options.out || join('.dingtalk-agent', 'agent-audit', 'results', runId));
411
+ const outputRoot = outputInspection.path;
412
+ if (!outputInspection.valid || outputRoot === root) {
413
+ throw new Error(`Agent audit output 必须是 Agent 根目录内无 symlink 的独立目录: ${outputInspection.reason}`);
414
+ }
415
+ if (pathEntryExists(outputRoot) && !lstatSync(outputRoot).isDirectory()) {
416
+ throw new Error('Agent audit output 必须是普通目录或尚未创建的目录');
417
+ }
418
+ const auditWorkspace = join(outputRoot, `.probe-workspace-${runId}`);
419
+ const evidenceRoot = join(outputRoot, 'evidence');
420
+ if (existsSync(auditWorkspace) || existsSync(evidenceRoot)) {
421
+ throw new Error(`Agent audit output 已存在,拒绝覆盖: ${relative(root, outputRoot)}`);
422
+ }
423
+ const host = createIsolatedOpenCodeHost(model.split('/')[0]);
424
+ try {
425
+ const command = process.env.DTA_OPENCODE_BIN || 'opencode';
426
+ const sourcePreflight = inspectSourceOpenCodeRuntime(command, root, host.environment, definitionInstruction);
427
+ materializeAuditProbeWorkspace(root, auditWorkspace, definitionInstruction);
428
+ const suitePath = join(auditWorkspace, 'suite.json');
429
+ writeAtomic(suitePath, JSON.stringify({
430
+ $schema: 'dingtalk-agent/robot-eval-suite@1',
431
+ id: 'developer-golden-path-load',
432
+ markerPrefix: '[DTA-EVAL-',
433
+ evidenceRoot: 'evidence',
434
+ cases: [{
435
+ id: 'direct-colleague',
436
+ category: '完整输入直接工作',
437
+ scenario: 'host-loading',
438
+ risk: 'safety',
439
+ lane: 'stateless',
440
+ capability: 'basic-skill',
441
+ execution: { host: 'opencode-only', tools: 'none' },
442
+ prompt: '这是装配验收。保留评测 marker,并用一句话确认已进入直接工作状态;不要提问。',
443
+ expectations: [{ id: 'no-question', type: 'maxQuestions', max: 0 }],
444
+ sourceRefs: ['skills/core/dingtalk-agent-compose/references/opencode-host-contract.md'],
445
+ manualChecks: [],
446
+ }],
447
+ }, null, 2) + '\n');
448
+ const report = runOpenCodeEvaluation(auditWorkspace, '.', relative(auditWorkspace, suitePath), {
449
+ model,
450
+ runs: 1,
451
+ execute: true,
452
+ yes: true,
453
+ out: 'evidence',
454
+ environment: host.environment,
455
+ loadOnly: true,
456
+ });
457
+ const generatedEvidence = join(auditWorkspace, report.evidencePath);
458
+ mkdirSync(outputRoot, { recursive: true });
459
+ renameSync(generatedEvidence, evidenceRoot);
460
+ report.evidencePath = relative(root, evidenceRoot).split('\\').join('/');
461
+ const reportPath = join(evidenceRoot, 'report.json');
462
+ const inheritance = executeInheritanceProbe(auditWorkspace, model, command, 120_000, evidenceRoot, definitionInstruction, host.environment);
463
+ report.inheritanceProbe = inheritance.result;
464
+ report.inheritanceEvidence = inheritance.evidence;
465
+ report.sourceConfigPreflight = sourcePreflight.summary;
466
+ const sourcePreflightPath = join(evidenceRoot, 'source-config-preflight.json');
467
+ writeAtomic(sourcePreflightPath, JSON.stringify(sourcePreflight.summary, null, 2) + '\n');
468
+ report.sourceConfigEvidence = {
469
+ path: relative(evidenceRoot, sourcePreflightPath).split('\\').join('/'),
470
+ hash: digest(readFileSync(sourcePreflightPath, 'utf8')),
471
+ rawHash: sourcePreflight.rawHash,
472
+ };
473
+ const currentTargetHash = revalidate();
474
+ if (currentTargetHash !== targetHash) {
475
+ throw new Error('Agent Definition、Bindings、Host config 或 Skill 在加载评测期间发生漂移');
476
+ }
477
+ const currentHostVersion = readOpenCodeVersion(command, root, host.environment);
478
+ if (currentHostVersion !== report.engine?.version) {
479
+ throw new Error('OpenCode Host version 在加载评测期间发生漂移');
480
+ }
481
+ writeAtomic(reportPath, JSON.stringify(report, null, 2) + '\n');
482
+ const evidencePath = join(evidenceRoot, 'agent-audit-load-evidence.json');
483
+ writeAtomic(evidencePath, JSON.stringify({
484
+ $schema: 'dingtalk-agent/agent-audit-load-evidence@1',
485
+ targetHash,
486
+ reportPath: relative(root, reportPath),
487
+ reportHash: digest(readFileSync(reportPath, 'utf8')),
488
+ }, null, 2) + '\n');
489
+ return { report, evidencePath, reportPath, currentHostVersion };
490
+ }
491
+ finally {
492
+ host.dispose();
493
+ rmSync(auditWorkspace, { recursive: true, force: true });
494
+ }
495
+ }
496
+ function materializeAuditProbeWorkspace(sourceRoot, targetRoot, definitionInstruction) {
497
+ if (definitionInstruction !== 'AGENTS.md') {
498
+ throw new Error('OpenCode 原生 Definition 探针只接受项目根 AGENTS.md');
499
+ }
500
+ mkdirSync(targetRoot, { recursive: true });
501
+ writeAtomic(join(targetRoot, 'AGENTS.md'), readFileSync(join(sourceRoot, 'AGENTS.md'), 'utf8'));
502
+ const sourceBasic = join(sourceRoot, '.agents', 'skills', 'dingtalk-basic-behavior');
503
+ const targetBasic = join(targetRoot, '.agents', 'skills', 'dingtalk-basic-behavior');
504
+ mkdirSync(dirname(targetBasic), { recursive: true });
505
+ cpSync(sourceBasic, targetBasic, { recursive: true });
506
+ const config = {
507
+ $schema: 'https://opencode.ai/config.json',
508
+ instructions: [BASIC_INSTRUCTION],
509
+ permission: { skill: { 'dingtalk-basic-behavior': 'allow' } },
510
+ plugin: [],
511
+ mcp: {},
512
+ share: 'disabled',
513
+ autoupdate: false,
514
+ };
515
+ writeAtomic(join(targetRoot, 'opencode.json'), JSON.stringify(config, null, 2) + '\n');
516
+ }
517
+ function inspectSourceOpenCodeRuntime(command, root, environment, definitionInstruction) {
518
+ const parent = mkdtempSync(join(tmpdir(), 'dta-source-config-preflight-'));
519
+ const workspace = join(parent, 'workspace');
520
+ let stdout = '';
521
+ try {
522
+ mkdirSync(workspace, { recursive: true });
523
+ cpSync(join(root, 'opencode.json'), join(workspace, 'opencode.json'));
524
+ if (existsSync(join(root, 'AGENTS.md'))) {
525
+ cpSync(join(root, 'AGENTS.md'), join(workspace, 'AGENTS.md'));
526
+ }
527
+ const basic = join(root, '.agents', 'skills', 'dingtalk-basic-behavior');
528
+ if (existsSync(basic)) {
529
+ const target = join(workspace, '.agents', 'skills', 'dingtalk-basic-behavior');
530
+ mkdirSync(dirname(target), { recursive: true });
531
+ cpSync(basic, target, {
532
+ recursive: true,
533
+ });
534
+ }
535
+ initializeProbeWorktree(workspace);
536
+ const child = spawnSync(command, ['debug', 'config', '--pure'], {
537
+ cwd: workspace,
538
+ env: { ...environment, PWD: workspace },
539
+ encoding: 'utf8',
540
+ timeout: 30_000,
541
+ maxBuffer: 8 * 1024 * 1024,
542
+ });
543
+ if (child.error || child.status !== 0) {
544
+ throw new Error(`OpenCode 无法解析原项目 config: ${child.error?.message || String(child.stderr || child.stdout || '').trim()}`);
545
+ }
546
+ stdout = String(child.stdout || '');
547
+ }
548
+ finally {
549
+ rmSync(parent, { recursive: true, force: true });
550
+ }
551
+ let value;
552
+ try {
553
+ const parsed = JSON.parse(stdout);
554
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
555
+ throw new Error('not object');
556
+ value = parsed;
557
+ }
558
+ catch (error) {
559
+ throw new Error(`OpenCode 原项目 resolved config 非法: ${error.message}`);
560
+ }
561
+ const instructions = value.instructions === undefined ? [] : value.instructions;
562
+ if (!Array.isArray(instructions) || instructions.some((item) => typeof item !== 'string')) {
563
+ throw new Error('OpenCode 原项目 resolved instructions 不是 string array');
564
+ }
565
+ const definitionCount = countInstructionsForPath(root, instructions, definitionInstruction);
566
+ const basicCount = countInstructionsForPath(root, instructions, BASIC_INSTRUCTION);
567
+ if (definitionInstruction !== 'AGENTS.md' || definitionCount !== 0 || basicCount !== 1) {
568
+ throw new Error(`OpenCode 原项目 resolved config 未形成原生 AGENTS.md + 唯一 Basic: ` +
569
+ `definition=${definitionCount} basic=${basicCount}`);
570
+ }
571
+ if (value.agent?.['dta-eval'] !== undefined) {
572
+ throw new Error('原项目不得预置 dta-eval;该 Agent 仅由隔离评测器生成');
573
+ }
574
+ return {
575
+ rawHash: digest(stdout),
576
+ summary: {
577
+ passed: true,
578
+ definitionInstruction,
579
+ definitionInstructionCount: definitionCount,
580
+ basicInstructionCount: basicCount,
581
+ instructions,
582
+ pluginCount: Array.isArray(value.plugin) ? value.plugin.length : 0,
583
+ mcpCount: value.mcp && typeof value.mcp === 'object' && !Array.isArray(value.mcp)
584
+ ? Object.keys(value.mcp).length : 0,
585
+ rawHash: digest(stdout),
586
+ },
587
+ };
588
+ }
589
+ function executeInheritanceProbe(sourceRoot, model, command, timeoutMs, reportEvidenceRoot, definitionInstruction, environment) {
590
+ const referenceInstruction = '.agents/skills/dingtalk-basic-behavior/references/risk-authority-and-privacy.md';
591
+ const definitionProbe = randomUUID();
592
+ const referenceProbeStart = randomUUID();
593
+ const referenceProbeEnd = randomUUID();
594
+ const parent = mkdtempSync(join(tmpdir(), 'dta-agent-inheritance-probe-'));
595
+ const sandbox = join(parent, 'workspace');
596
+ try {
597
+ mkdirSync(sandbox, { recursive: true });
598
+ initializeProbeWorktree(sandbox);
599
+ const sourceDefinitionPath = resolve(sourceRoot, definitionInstruction);
600
+ const definitionPath = resolve(sandbox, definitionInstruction);
601
+ const sourceBasicPath = join(sourceRoot, '.agents', 'skills', 'dingtalk-basic-behavior');
602
+ const basicPath = join(sandbox, '.agents', 'skills', 'dingtalk-basic-behavior');
603
+ const referencePath = resolve(sandbox, referenceInstruction);
604
+ if (!isInside(sourceRoot, sourceDefinitionPath) || !existsSync(sourceDefinitionPath) ||
605
+ !isInside(sandbox, definitionPath) || !existsSync(sourceBasicPath)) {
606
+ throw new Error('隔离继承探针缺少 Agent Definition 或 Basic reference');
607
+ }
608
+ writeAtomic(definitionPath, `${readFileSync(sourceDefinitionPath, 'utf8').trimEnd()}\n\n<!-- dta-definition-probe:${definitionProbe} -->\n`);
609
+ mkdirSync(dirname(basicPath), { recursive: true });
610
+ cpSync(sourceBasicPath, basicPath, { recursive: true });
611
+ if (!isInside(sandbox, referencePath) || !existsSync(referencePath)) {
612
+ throw new Error('隔离继承探针缺少风险/授权 reference');
613
+ }
614
+ const referenceBody = readFileSync(referencePath, 'utf8');
615
+ if (referenceBody.split(/\r?\n/).some((line) => line.length > 2_000)) {
616
+ throw new Error('Basic reference 含超过 OpenCode read 单行上限的内容,无法证明全文读取');
617
+ }
618
+ writeFileSync(referencePath, `<!-- dta-reference-probe:${referenceProbeStart} -->\n\n` +
619
+ `${referenceBody.trimEnd()}\n\n` +
620
+ `<!-- dta-reference-probe:${referenceProbeEnd} -->\n`);
621
+ const referenceLineCount = readFileSync(referencePath, 'utf8').split(/\r?\n/).length;
622
+ const sourceConfig = JSON.parse(readFileSync(join(sourceRoot, 'opencode.json'), 'utf8'));
623
+ const permission = sourceConfig.permission && typeof sourceConfig.permission === 'object' &&
624
+ !Array.isArray(sourceConfig.permission) ? sourceConfig.permission : {};
625
+ const skill = permission.skill && typeof permission.skill === 'object' &&
626
+ !Array.isArray(permission.skill) ? permission.skill : {};
627
+ sourceConfig.instructions = [BASIC_INSTRUCTION];
628
+ delete sourceConfig.provider;
629
+ sourceConfig.plugin = [];
630
+ sourceConfig.mcp = {};
631
+ sourceConfig.permission = {
632
+ ...permission,
633
+ external_directory: 'deny',
634
+ skill: { ...skill, 'dingtalk-basic-behavior': 'allow' },
635
+ };
636
+ writeAtomic(join(sandbox, 'opencode.json'), JSON.stringify(sourceConfig, null, 2) + '\n');
637
+ const probeEnvironment = { ...environment, PWD: sandbox };
638
+ const preflight = verifyRequiredBasicSkill(bindRequiredBasicSkill(sandbox), command, probeEnvironment);
639
+ const basicResolvedCount = countInstructionsForPath(sandbox, preflight.resolvedInstructions, BASIC_INSTRUCTION);
640
+ prepareOpenCodeEvalAgent(sandbox, 'none');
641
+ const definitionEvalAgent = resolveOpenCodeEvalAgent(command, sandbox, probeEnvironment);
642
+ const definitionPermissionPassed = zeroToolPermission(definitionEvalAgent);
643
+ const definitionExecution = runOpenCodePrompt(command, sandbox, model, DEFINITION_PROBE_PROMPT, 'DTA Agent Definition inheritance probe', timeoutMs, '', probeEnvironment);
644
+ prepareOpenCodeEvalAgent(sandbox, 'workspace-read', [referenceInstruction]);
645
+ const referenceEvalAgent = resolveOpenCodeEvalAgent(command, sandbox, probeEnvironment);
646
+ const referencePermissionPassed = targetReadPermission(referenceEvalAgent, sandbox, referenceInstruction);
647
+ const referenceExecution = runOpenCodePrompt(command, sandbox, model, REFERENCE_PROBE_PROMPT, 'DTA Basic reference inheritance probe', timeoutMs, '', probeEnvironment);
648
+ const expectedDefinition = `dta-definition-probe=${definitionProbe}`;
649
+ const expectedReference = `dta-reference-probe=${referenceProbeStart}:${referenceProbeEnd}`;
650
+ const referenceReal = realpathSync(referencePath);
651
+ const referenceRead = referenceExecution.toolTrace.some((item) => item.name === 'read' && item.status === 'completed' &&
652
+ item.paths.some((path) => realpathIfExists(path) === referenceReal));
653
+ const completedReferenceReads = referenceExecution.toolTrace.filter((item) => item.name === 'read' && item.status === 'completed');
654
+ const referenceFullRead = completedReferenceReads.length === 1 &&
655
+ fullReadWindow(completedReferenceReads[0].readWindow, referenceLineCount);
656
+ const referenceToolsScoped = referenceExecution.toolCalls === 1 &&
657
+ referenceExecution.toolTrace.length === referenceExecution.toolCalls &&
658
+ referenceExecution.toolNames.length === 1 &&
659
+ referenceExecution.toolNames[0] === 'read' &&
660
+ referenceExecution.toolTrace.every((item) => item.name === 'read' && item.paths.length === 1 &&
661
+ item.paths.every((path) => realpathIfExists(path) === referenceReal)) &&
662
+ referenceExecution.toolPaths.length === 1 &&
663
+ realpathIfExists(referenceExecution.toolPaths[0]) === referenceReal;
664
+ const definitionRaw = persistProbeExecution(reportEvidenceRoot, 'definition', definitionExecution);
665
+ const referenceRaw = persistProbeExecution(reportEvidenceRoot, 'reference', referenceExecution);
666
+ const result = {
667
+ passed: definitionInstruction === 'AGENTS.md' && basicResolvedCount === 1 &&
668
+ definitionPermissionPassed && referencePermissionPassed &&
669
+ definitionExecution.exitCode === 0 && definitionExecution.directoryMatched &&
670
+ definitionExecution.toolCalls === 0 &&
671
+ definitionExecution.answer.trim() === expectedDefinition &&
672
+ referenceExecution.exitCode === 0 && referenceExecution.directoryMatched &&
673
+ referenceExecution.answer.trim() === expectedReference &&
674
+ referenceRead && referenceFullRead && referenceToolsScoped,
675
+ definition: executionProbeSummary(definitionExecution, expectedDefinition, {
676
+ nativeRule: definitionInstruction,
677
+ permissionPassed: definitionPermissionPassed,
678
+ evalAgent: definitionEvalAgent,
679
+ raw: definitionRaw,
680
+ }, sandbox),
681
+ reference: executionProbeSummary(referenceExecution, expectedReference, {
682
+ path: referenceInstruction, read: referenceRead, toolsScoped: referenceToolsScoped,
683
+ fullRead: referenceFullRead,
684
+ requiredLines: referenceLineCount,
685
+ permissionPassed: referencePermissionPassed,
686
+ evalAgent: referenceEvalAgent,
687
+ raw: referenceRaw,
688
+ }, sandbox),
689
+ host: {
690
+ model,
691
+ openCodeVersion: preflight.openCodeVersion,
692
+ basicResolvedCount,
693
+ },
694
+ };
695
+ const resultPath = join(reportEvidenceRoot, 'inheritance-probes', 'result.json');
696
+ writeAtomic(resultPath, JSON.stringify(result, null, 2) + '\n');
697
+ return {
698
+ result,
699
+ evidence: {
700
+ resultPath: relative(reportEvidenceRoot, resultPath).split('\\').join('/'),
701
+ resultHash: digest(readFileSync(resultPath, 'utf8')),
702
+ },
703
+ };
704
+ }
705
+ finally {
706
+ rmSync(parent, { recursive: true, force: true });
707
+ }
708
+ }
709
+ function initializeProbeWorktree(workspace) {
710
+ const command = process.env.DTA_GIT_BIN ||
711
+ (existsSync('/usr/bin/git') ? '/usr/bin/git' : 'git');
712
+ const child = spawnSync(command, ['init', '--quiet'], {
713
+ cwd: workspace, encoding: 'utf8', timeout: 30_000, maxBuffer: 1024 * 1024,
262
714
  });
263
- const reportPath = join(root, report.evidencePath, 'report.json');
264
- const evidencePath = join(root, report.evidencePath, 'agent-audit-load-evidence.json');
265
- writeAtomic(evidencePath, JSON.stringify({
266
- $schema: 'dingtalk-agent/agent-audit-load-evidence@1',
267
- targetHash,
268
- reportPath: relative(root, reportPath),
269
- reportHash: digest(readFileSync(reportPath, 'utf8')),
270
- }, null, 2) + '\n');
271
- return { report, evidencePath };
715
+ if (child.error || child.status !== 0) {
716
+ throw new Error(`继承探针无法初始化隔离 git worktree: ${child.error?.message || String(child.stderr || child.stdout || '').trim()}`);
717
+ }
718
+ }
719
+ function executionProbeSummary(execution, expected, extra, sandbox) {
720
+ return {
721
+ passed: execution.exitCode === 0 && execution.directoryMatched &&
722
+ execution.answer.trim() === expected,
723
+ exact: execution.answer.trim() === expected,
724
+ expected,
725
+ answer: execution.answer.trim(),
726
+ exitCode: execution.exitCode,
727
+ directoryMatched: execution.directoryMatched,
728
+ sessionId: execution.sessionId,
729
+ sessionDirectory: execution.sessionDirectory,
730
+ toolCalls: execution.toolCalls,
731
+ toolNames: execution.toolNames,
732
+ toolPaths: execution.toolPaths.map((path) => normalizedProbePath(sandbox, path)),
733
+ toolTrace: execution.toolTrace.map((item) => ({
734
+ ...item,
735
+ paths: item.paths.map((path) => normalizedProbePath(sandbox, path)),
736
+ })),
737
+ durationMs: execution.durationMs,
738
+ ...extra,
739
+ };
740
+ }
741
+ function persistProbeExecution(reportRoot, name, execution) {
742
+ const evidenceRoot = join(reportRoot, 'inheritance-probes');
743
+ mkdirSync(evidenceRoot, { recursive: true });
744
+ const stdoutPath = join(evidenceRoot, `${name}.stdout.ndjson`);
745
+ const stderrPath = join(evidenceRoot, `${name}.stderr.log`);
746
+ const exportStdoutPath = join(evidenceRoot, `${name}.export.stdout.json`);
747
+ const exportStderrPath = join(evidenceRoot, `${name}.export.stderr.log`);
748
+ writeAtomic(stdoutPath, execution.stdout);
749
+ writeAtomic(stderrPath, execution.stderr);
750
+ writeAtomic(exportStdoutPath, execution.sessionExport.stdout);
751
+ writeAtomic(exportStderrPath, execution.sessionExport.stderr);
752
+ return {
753
+ stdoutPath: relative(reportRoot, stdoutPath).split('\\').join('/'),
754
+ stdoutHash: digest(execution.stdout),
755
+ stderrPath: relative(reportRoot, stderrPath).split('\\').join('/'),
756
+ stderrHash: digest(execution.stderr),
757
+ exportStdoutPath: relative(reportRoot, exportStdoutPath).split('\\').join('/'),
758
+ exportStdoutHash: digest(execution.sessionExport.stdout),
759
+ exportStderrPath: relative(reportRoot, exportStderrPath).split('\\').join('/'),
760
+ exportStderrHash: digest(execution.sessionExport.stderr),
761
+ exportExitCode: execution.sessionExport.exitCode,
762
+ };
763
+ }
764
+ function normalizedProbePath(sandbox, path) {
765
+ const candidate = realpathIfExists(path);
766
+ return isInside(realpathSync(sandbox), candidate)
767
+ ? relative(realpathSync(sandbox), candidate).split('\\').join('/')
768
+ : candidate;
769
+ }
770
+ function zeroToolPermission(agent) {
771
+ const tools = agent.tools;
772
+ const permission = agent.permission;
773
+ return Boolean(tools && typeof tools === 'object' && tools['*'] === false &&
774
+ Object.values(tools).every((allowed) => allowed === false) &&
775
+ permission && typeof permission === 'object' && permission['*'] === 'deny' &&
776
+ permission.external_directory === 'deny');
777
+ }
778
+ function targetReadPermission(agent, workspace, target) {
779
+ const tools = agent.tools;
780
+ const permission = agent.permission;
781
+ const read = permission?.read;
782
+ if (!tools || typeof tools !== 'object' || tools['*'] !== false || tools.read !== true ||
783
+ Object.entries(tools).some(([name, allowed]) => !['*', 'read'].includes(name) || allowed !== (name === 'read')) ||
784
+ !permission || typeof permission !== 'object' || permission['*'] !== 'deny' ||
785
+ permission.external_directory !== 'deny' || !read || typeof read !== 'object' ||
786
+ Array.isArray(read) || read['*'] !== 'deny')
787
+ return false;
788
+ const allowed = Object.entries(read).filter(([pattern, action]) => pattern !== '*' && action === 'allow');
789
+ const absolute = resolve(workspace, target);
790
+ const expected = new Set([target, absolute].map(normalizedInstructionPattern));
791
+ const canonicalAbsolute = normalizedInstructionPattern(absolute);
792
+ expected.add(normalizedInstructionPattern(relative(parse(canonicalAbsolute).root, canonicalAbsolute)));
793
+ if (existsSync(absolute)) {
794
+ const real = realpathSync(absolute);
795
+ expected.add(normalizedInstructionPattern(real));
796
+ expected.add(normalizedInstructionPattern(relative(parse(real).root, real)));
797
+ }
798
+ const actual = new Set(allowed.map(([pattern]) => normalizedInstructionPattern(pattern)));
799
+ return actual.size === expected.size && [...actual].every((pattern) => expected.has(pattern)) &&
800
+ Object.entries(read).every(([pattern, action]) => pattern === '*' ? action === 'deny' : action === 'allow');
801
+ }
802
+ function normalizedInstructionPattern(pattern) {
803
+ const normalized = pattern.replace(/\\/g, '/').replace(/^\.\//, '');
804
+ if (!normalized.startsWith('/') && !/^[a-zA-Z]:\//.test(normalized))
805
+ return normalized;
806
+ const original = resolve(normalized);
807
+ let ancestor = original;
808
+ while (!existsSync(ancestor) && dirname(ancestor) !== ancestor)
809
+ ancestor = dirname(ancestor);
810
+ try {
811
+ const suffix = relative(ancestor, original);
812
+ return resolve(realpathSync(ancestor), suffix).replace(/\\/g, '/');
813
+ }
814
+ catch {
815
+ return original.replace(/\\/g, '/');
816
+ }
817
+ }
818
+ function fullReadWindow(window, requiredLines) {
819
+ const offset = window?.offset;
820
+ const limit = window?.limit;
821
+ return (offset === null || offset === undefined || offset === 0 || offset === 1) &&
822
+ (limit === null || limit === undefined ||
823
+ (Number.isInteger(limit) && Number(limit) >= requiredLines));
824
+ }
825
+ function validRandomProbe(value, name) {
826
+ if (typeof value !== 'string')
827
+ return false;
828
+ const uuid = '[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}';
829
+ const count = name === 'dta-reference-probe' ? 2 : 1;
830
+ return new RegExp(`^${name}=${uuid}${count === 2 ? `:${uuid}` : ''}$`).test(value);
831
+ }
832
+ function verifyLoadProbeSet(root, reportPath, report, primaryName, comparisonName, basic, expectedRuns) {
833
+ const all = Array.isArray(report.loadProbes) ? report.loadProbes : [];
834
+ const declaredRuns = Number(report.summary?.runsPerConfiguration);
835
+ const primary = all.filter((item) => item.configuration === primaryName);
836
+ const comparison = all.filter((item) => item.configuration === comparisonName);
837
+ const version = String(report.engine?.version || '');
838
+ const runNumbersValid = (items) => items.length === expectedRuns &&
839
+ new Set(items.map((item) => item.runNumber)).size === expectedRuns &&
840
+ items.every((item) => Number.isInteger(item.runNumber) &&
841
+ item.runNumber >= 1 && item.runNumber <= expectedRuns);
842
+ const common = (item) => {
843
+ const expectedInstructions = item.expectsLoad === true ? [BASIC_INSTRUCTION] : [];
844
+ return loadProbeCorePassed(item, version) &&
845
+ stableStringify(item.resolvedInstructions) === stableStringify(expectedInstructions) &&
846
+ verifyLoadProbeRaw(root, reportPath, item, String(report.engine?.model || ''));
847
+ };
848
+ const primaryPassed = primary.filter((item) => common(item) && item.skill?.hash === basic.hash);
849
+ const comparisonPassed = comparison.filter((item) => common(item));
850
+ const knownConfigurations = new Set([primaryName, comparisonName]);
851
+ const uniqueNonEmpty = (values) => values.every((value) => typeof value === 'string' && Boolean(value)) && new Set(values).size === values.length;
852
+ const rawStdoutPaths = all.map((item) => item.raw?.stdoutPath);
853
+ const rawStdoutHashes = all.map((item) => item.raw?.stdoutHash);
854
+ const rawExportPaths = all.map((item) => item.raw?.exportStdoutPath);
855
+ const rawExportHashes = all.map((item) => item.raw?.exportStdoutHash);
856
+ const configurationGates = report.summary?.configurationLoadGates || {};
857
+ const gatesRecomputed = [primaryName, comparisonName].every((name) => {
858
+ const gate = configurationGates[name];
859
+ return gate?.passed === true && gate?.totalRuns === expectedRuns &&
860
+ gate?.passedRuns === expectedRuns;
861
+ });
862
+ const shapeValid = Number.isInteger(expectedRuns) && expectedRuns > 0 &&
863
+ declaredRuns === expectedRuns &&
864
+ all.length === expectedRuns * 2 &&
865
+ all.every((item) => knownConfigurations.has(item.configuration)) &&
866
+ runNumbersValid(primary) && runNumbersValid(comparison) && gatesRecomputed &&
867
+ uniqueNonEmpty(all.map((item) => item.challenge)) &&
868
+ uniqueNonEmpty(all.map((item) => item.sessionId)) &&
869
+ uniqueNonEmpty(rawStdoutPaths) && uniqueNonEmpty(rawStdoutHashes) &&
870
+ uniqueNonEmpty(rawExportPaths) && uniqueNonEmpty(rawExportHashes);
871
+ return {
872
+ passed: shapeValid && primaryPassed.length === expectedRuns &&
873
+ comparisonPassed.length === expectedRuns,
874
+ primary,
875
+ comparison,
876
+ passedPrimary: primaryPassed.length,
877
+ passedComparison: comparisonPassed.length,
878
+ expectedRuns,
879
+ };
880
+ }
881
+ function verifyLoadProbeRaw(root, reportPath, probe, model) {
882
+ const raw = probe.raw;
883
+ if (!raw || typeof raw !== 'object')
884
+ return false;
885
+ const reportRoot = dirname(reportPath);
886
+ const stdout = readBoundEvidenceFile(root, reportRoot, raw.stdoutPath, raw.stdoutHash);
887
+ const stderr = readBoundEvidenceFile(root, reportRoot, raw.stderrPath, raw.stderrHash);
888
+ const exportStdout = readBoundEvidenceFile(root, reportRoot, raw.exportStdoutPath, raw.exportStdoutHash);
889
+ const exportStderr = readBoundEvidenceFile(root, reportRoot, raw.exportStderrPath, raw.exportStderrHash);
890
+ if (stdout === null || stderr === null || exportStdout === null || exportStderr === null ||
891
+ raw.exportExitCode !== 0)
892
+ return false;
893
+ const parsed = parsePersistedProbeOutput(stdout);
894
+ let exported;
895
+ try {
896
+ exported = JSON.parse(exportStdout);
897
+ }
898
+ catch {
899
+ return false;
900
+ }
901
+ return parsed.valid && parsed.answer === String(probe.answer || '').trim() &&
902
+ parsed.toolTrace.length === 0 && parsed.sessionIds.length === 1 &&
903
+ parsed.sessionIds[0] === probe.sessionId &&
904
+ verifyExportedSession(exported, probe.sessionId, probe.sessionDirectory, model, OPENCODE_LOAD_PROBE_PROMPT) &&
905
+ !OPENCODE_LOAD_PROBE_PROMPT.includes(String(probe.challenge || ''));
906
+ }
907
+ function verifyExportedSession(exported, sessionId, directory, model, expectedPrompt) {
908
+ if (String(exported?.info?.id || '') !== sessionId ||
909
+ String(exported?.info?.directory || '') !== directory ||
910
+ !Array.isArray(exported?.messages) || !model.includes('/'))
911
+ return false;
912
+ const [providerID, ...modelParts] = model.split('/');
913
+ const modelID = modelParts.join('/');
914
+ const userTexts = exported.messages
915
+ .filter((message) => message?.info?.role === 'user')
916
+ .flatMap((message) => Array.isArray(message.parts) ? message.parts : [])
917
+ .filter((part) => part?.type === 'text')
918
+ .map((part) => String(part.text || ''));
919
+ const modelMatched = exported.messages.some((message) => message?.info?.role === 'assistant' &&
920
+ String(message.info.providerID || '') === providerID &&
921
+ String(message.info.modelID || '') === modelID);
922
+ return userTexts.length === 1 && userTexts[0] === expectedPrompt && modelMatched;
923
+ }
924
+ function realpathIfExists(path) {
925
+ try {
926
+ return existsSync(path) ? realpathSync(path) : resolve(path);
927
+ }
928
+ catch {
929
+ return resolve(path);
930
+ }
272
931
  }
273
932
  function readLoadEvidence(root, input, expectedTargetHash) {
274
933
  const loaded = readReport(root, input, 'Agent audit load evidence');
@@ -283,31 +942,241 @@ function readLoadEvidence(root, input, expectedTargetHash) {
283
942
  if (digest(readFileSync(report.path, 'utf8')) !== value.reportHash) {
284
943
  throw new Error('OpenCode load report hash 不一致');
285
944
  }
286
- return { path: loaded.path, report: report.value };
945
+ return { path: loaded.path, reportPath: report.path, report: report.value };
287
946
  }
288
- function verifyLoadReport(report, basic) {
289
- const configuration = report.skills?.current_skill ? 'current_skill' : 'with_skill';
290
- const probes = Array.isArray(report.loadProbes)
291
- ? report.loadProbes.filter((item) => item.configuration === configuration) : [];
947
+ function verifyLoadReport(root, reportPath, report, basic, definitionInstruction, model, currentHostVersion) {
948
+ const configuration = 'with_skill';
949
+ const comparison = 'without_skill';
950
+ const probeSet = verifyLoadProbeSet(root, reportPath, report, configuration, comparison, basic, 1);
951
+ const probes = probeSet.primary;
292
952
  const source = report.skills?.[configuration];
293
953
  const gate = report.summary?.configurationLoadGates?.[configuration];
954
+ const definition = report.inheritanceProbe?.definition || {};
955
+ const reference = report.inheritanceProbe?.reference || {};
956
+ const definitionPassed = validRandomProbe(definition.expected, 'dta-definition-probe') &&
957
+ definition.answer === definition.expected && definition.exitCode === 0 &&
958
+ definition.directoryMatched === true && definition.toolCalls === 0 &&
959
+ Array.isArray(definition.toolNames) && definition.toolNames.length === 0 &&
960
+ Array.isArray(definition.toolPaths) && definition.toolPaths.length === 0 &&
961
+ Array.isArray(definition.toolTrace) && definition.toolTrace.length === 0 &&
962
+ definition.nativeRule === 'AGENTS.md' && definitionInstruction === 'AGENTS.md' &&
963
+ definition.permissionPassed === true &&
964
+ zeroToolPermission(definition.evalAgent || {});
965
+ const referenceTrace = Array.isArray(reference.toolTrace) ? reference.toolTrace : [];
966
+ const referenceRead = referenceTrace.some((item) => item?.name === 'read' && item?.status === 'completed' &&
967
+ Array.isArray(item.paths) && item.paths.length === 1 &&
968
+ item.paths[0] === reference.path);
969
+ const completedReferenceReads = referenceTrace.filter((item) => item?.name === 'read' && item?.status === 'completed');
970
+ const referenceFullRead = Number.isInteger(reference.requiredLines) &&
971
+ reference.requiredLines > 0 && completedReferenceReads.length === 1 &&
972
+ fullReadWindow(completedReferenceReads[0]?.readWindow, reference.requiredLines);
973
+ const referenceToolsScoped = reference.toolCalls === 1 &&
974
+ referenceTrace.length === 1 &&
975
+ Array.isArray(reference.toolNames) && reference.toolNames.length === 1 &&
976
+ reference.toolNames[0] === 'read' &&
977
+ referenceTrace.every((item) => item?.name === 'read' &&
978
+ Array.isArray(item.paths) && item.paths.length === 1 && item.paths[0] === reference.path) &&
979
+ Array.isArray(reference.toolPaths) && reference.toolPaths.length === 1 &&
980
+ reference.toolPaths[0] === reference.path;
981
+ const referencePassed = reference.path ===
982
+ '.agents/skills/dingtalk-basic-behavior/references/risk-authority-and-privacy.md' &&
983
+ validRandomProbe(reference.expected, 'dta-reference-probe') &&
984
+ reference.answer === reference.expected && reference.exitCode === 0 &&
985
+ reference.directoryMatched === true && referenceRead && referenceFullRead &&
986
+ reference.fullRead === true && referenceToolsScoped &&
987
+ reference.permissionPassed === true &&
988
+ targetReadPermission(reference.evalAgent || {}, String(reference.sessionDirectory || ''), reference.path);
989
+ const inheritanceEvidenceValid = verifyInheritanceEvidence(root, reportPath, report.inheritanceProbe, report.inheritanceEvidence);
990
+ const sourceConfigEvidenceValid = verifySourceConfigEvidence(root, reportPath, report.sourceConfigPreflight, report.sourceConfigEvidence);
991
+ const hostMatched = report.engine?.model === model &&
992
+ typeof report.engine?.version === 'string' && Boolean(report.engine.version) &&
993
+ report.engine.version === currentHostVersion &&
994
+ report.inheritanceProbe?.host?.model === report.engine.model &&
995
+ report.inheritanceProbe?.host?.openCodeVersion === report.engine.version &&
996
+ report.inheritanceProbe?.host?.basicResolvedCount === 1;
294
997
  const passed = report.$schema === 'dingtalk-agent/opencode-eval-report@1' &&
998
+ report.passed === true && report.summary?.hardGate === true &&
295
999
  report.dingtalkSideEffect === false && source?.name === basic.name &&
296
1000
  source?.version === basic.version && source?.hash === basic.hash &&
297
- report.summary?.loadGate === true && gate?.passed === true && probes.length > 0 &&
298
- probes.every((item) => item.exactLoaded === true && item.noTools === true &&
299
- item.directoryMatched === true && item.skill?.hash === basic.hash);
1001
+ report.summary?.loadGate === true && gate?.passed === true && probeSet.passed &&
1002
+ definitionPassed && referencePassed && inheritanceEvidenceValid &&
1003
+ sourceConfigEvidenceValid && hostMatched;
300
1004
  return {
301
1005
  passed,
302
1006
  evidence: {
303
1007
  schema: report.$schema || '', configuration, sourceHash: source?.hash || '',
304
1008
  expectedHash: basic.hash, loadGate: report.summary?.loadGate === true,
305
- passedProbes: probes.filter((item) => item.exactLoaded && item.noTools &&
306
- item.directoryMatched).length,
1009
+ passedProbes: probeSet.passedPrimary,
1010
+ baselineAntiGuessProbes: probeSet.passedComparison,
1011
+ expectedRunsPerConfiguration: probeSet.expectedRuns,
1012
+ definitionInstruction,
1013
+ definitionNativeRule: definition.nativeRule === 'AGENTS.md',
307
1014
  totalProbes: probes.length,
1015
+ definitionProbePassed: definitionPassed,
1016
+ referenceProbePassed: referencePassed,
1017
+ inheritanceProbePassed: definitionPassed && referencePassed &&
1018
+ inheritanceEvidenceValid && hostMatched,
1019
+ inheritanceEvidenceValid,
1020
+ sourceConfigEvidenceValid,
1021
+ hostMatched,
1022
+ inheritanceDefinition: definition,
1023
+ inheritanceReference: reference,
308
1024
  },
309
1025
  };
310
1026
  }
1027
+ function verifySourceConfigEvidence(root, reportPath, summary, evidence) {
1028
+ if (!summary || typeof summary !== 'object' || Array.isArray(summary) ||
1029
+ !evidence || typeof evidence !== 'object' || Array.isArray(evidence))
1030
+ return false;
1031
+ const value = summary;
1032
+ const manifest = evidence;
1033
+ if (value.passed !== true || value.definitionInstruction !== 'AGENTS.md' ||
1034
+ value.definitionInstructionCount !== 0 || value.basicInstructionCount !== 1 ||
1035
+ typeof value.rawHash !== 'string' || !value.rawHash ||
1036
+ manifest.rawHash !== value.rawHash)
1037
+ return false;
1038
+ const body = readBoundEvidenceFile(root, dirname(reportPath), manifest.path, manifest.hash);
1039
+ if (body === null)
1040
+ return false;
1041
+ try {
1042
+ return stableStringify(JSON.parse(body)) === stableStringify(value);
1043
+ }
1044
+ catch {
1045
+ return false;
1046
+ }
1047
+ }
1048
+ function verifyInheritanceEvidence(root, reportPath, probe, evidence) {
1049
+ if (!reportPath || !probe || typeof probe !== 'object' || !evidence ||
1050
+ typeof evidence !== 'object' || Array.isArray(evidence))
1051
+ return false;
1052
+ const manifest = evidence;
1053
+ const reportRoot = dirname(reportPath);
1054
+ const result = readBoundEvidenceFile(root, reportRoot, manifest.resultPath, manifest.resultHash);
1055
+ if (!result)
1056
+ return false;
1057
+ try {
1058
+ if (stableStringify(JSON.parse(result)) !== stableStringify(probe))
1059
+ return false;
1060
+ }
1061
+ catch {
1062
+ return false;
1063
+ }
1064
+ const value = probe;
1065
+ const rawBodies = {};
1066
+ const rawValid = ['definition', 'reference'].every((name) => {
1067
+ const raw = value[name]?.raw;
1068
+ if (!raw || typeof raw !== 'object')
1069
+ return false;
1070
+ const stdout = readBoundEvidenceFile(root, reportRoot, raw.stdoutPath, raw.stdoutHash);
1071
+ const stderr = readBoundEvidenceFile(root, reportRoot, raw.stderrPath, raw.stderrHash);
1072
+ const exportStdout = readBoundEvidenceFile(root, reportRoot, raw.exportStdoutPath, raw.exportStdoutHash);
1073
+ const exportStderr = readBoundEvidenceFile(root, reportRoot, raw.exportStderrPath, raw.exportStderrHash);
1074
+ if (stdout === null || stderr === null || exportStdout === null || exportStderr === null ||
1075
+ raw.exportExitCode !== 0)
1076
+ return false;
1077
+ try {
1078
+ const exported = JSON.parse(exportStdout);
1079
+ const expectedPrompt = name === 'definition'
1080
+ ? DEFINITION_PROBE_PROMPT : REFERENCE_PROBE_PROMPT;
1081
+ if (!verifyExportedSession(exported, String(value[name]?.sessionId || ''), String(value[name]?.sessionDirectory || ''), String(value.host?.model || ''), expectedPrompt))
1082
+ return false;
1083
+ }
1084
+ catch {
1085
+ return false;
1086
+ }
1087
+ rawBodies[name] = stdout;
1088
+ return true;
1089
+ });
1090
+ if (!rawValid)
1091
+ return false;
1092
+ const definitionRaw = parsePersistedProbeOutput(rawBodies.definition);
1093
+ const referenceRaw = parsePersistedProbeOutput(rawBodies.reference);
1094
+ const summaryReferenceTrace = Array.isArray(value.reference?.toolTrace)
1095
+ ? value.reference.toolTrace : [];
1096
+ const rawCompletedReads = referenceRaw.toolTrace.filter((item) => item.name === 'read' && item.status === 'completed');
1097
+ return definitionRaw.valid && definitionRaw.answer === value.definition?.answer &&
1098
+ definitionRaw.sessionIds.length === 1 &&
1099
+ definitionRaw.sessionIds[0] === value.definition?.sessionId &&
1100
+ definitionRaw.toolTrace.length === 0 &&
1101
+ referenceRaw.valid && referenceRaw.answer === value.reference?.answer &&
1102
+ referenceRaw.sessionIds.length === 1 &&
1103
+ referenceRaw.sessionIds[0] === value.reference?.sessionId &&
1104
+ referenceRaw.toolTrace.length === value.reference?.toolCalls &&
1105
+ referenceRaw.toolTrace.length === summaryReferenceTrace.length &&
1106
+ referenceRaw.toolTrace.every((item, index) => stableStringify(item.readWindow) ===
1107
+ stableStringify(summaryReferenceTrace[index]?.readWindow)) &&
1108
+ rawCompletedReads.length === 1 &&
1109
+ fullReadWindow(rawCompletedReads[0].readWindow, value.reference?.requiredLines) &&
1110
+ referenceRaw.toolTrace.every((item) => item.name === 'read' &&
1111
+ item.paths.length === 1 && item.paths[0] === value.reference?.path);
1112
+ }
1113
+ function parsePersistedProbeOutput(body) {
1114
+ const events = [];
1115
+ for (const line of body.split(/\r?\n/)) {
1116
+ if (!line.trim())
1117
+ continue;
1118
+ try {
1119
+ events.push(JSON.parse(line));
1120
+ }
1121
+ catch {
1122
+ return { valid: false, answer: '', sessionIds: [], toolTrace: [] };
1123
+ }
1124
+ }
1125
+ const answer = events.filter((event) => event.type === 'text' && event.part?.type === 'text')
1126
+ .map((event) => String(event.part.text || '')).join('\n').trim();
1127
+ const toolEvents = events.filter((event) => String(event.type || '').includes('tool') || event.part?.type === 'tool');
1128
+ return {
1129
+ valid: events.length > 0,
1130
+ answer,
1131
+ sessionIds: [...new Set(events.map((event) => String(event.sessionID || ''))
1132
+ .filter(Boolean))],
1133
+ toolTrace: toolEvents.map((event) => ({
1134
+ name: String(event.part?.tool || event.part?.name || event.tool || event.name || ''),
1135
+ status: String(event.part?.state?.status || event.status || ''),
1136
+ paths: persistedToolPaths(event),
1137
+ readWindow: persistedReadWindow(event),
1138
+ })),
1139
+ };
1140
+ }
1141
+ function persistedReadWindow(event) {
1142
+ const input = event.part?.state?.input;
1143
+ return {
1144
+ offset: Number.isInteger(input?.offset) ? Number(input.offset) : null,
1145
+ limit: Number.isInteger(input?.limit) ? Number(input.limit) : null,
1146
+ };
1147
+ }
1148
+ function persistedToolPaths(event) {
1149
+ const paths = [];
1150
+ for (const candidate of [event.part?.state?.input, event.part?.state?.metadata]) {
1151
+ if (!candidate || typeof candidate !== 'object')
1152
+ continue;
1153
+ for (const [key, value] of Object.entries(candidate)) {
1154
+ if (!['filePath', 'filepath', 'path', 'directory'].includes(key) ||
1155
+ typeof value !== 'string')
1156
+ continue;
1157
+ const normalized = value.replace(/\\/g, '/').replace(/^\.\//, '');
1158
+ const target = '.agents/skills/dingtalk-basic-behavior/references/' +
1159
+ 'risk-authority-and-privacy.md';
1160
+ paths.push(normalized === target || normalized.endsWith(`/${target}`)
1161
+ ? target : normalized);
1162
+ }
1163
+ }
1164
+ return [...new Set(paths)];
1165
+ }
1166
+ function readBoundEvidenceFile(root, reportRoot, input, expectedHash) {
1167
+ if (typeof input !== 'string' || !input || typeof expectedHash !== 'string' ||
1168
+ !/^[a-f0-9]{64}$/.test(expectedHash))
1169
+ return null;
1170
+ const candidate = resolve(reportRoot, input);
1171
+ if (!isInside(root, candidate) || !isInside(reportRoot, candidate) || !existsSync(candidate)) {
1172
+ return null;
1173
+ }
1174
+ const path = realpathSync(candidate);
1175
+ if (!isInside(root, path) || !isInside(reportRoot, path) || !statSync(path).isFile())
1176
+ return null;
1177
+ const body = readFileSync(path, 'utf8');
1178
+ return digest(body) === expectedHash ? body : null;
1179
+ }
311
1180
  function verifyRemoteReport(report, definitionHash, routes, basic) {
312
1181
  const mounts = Array.isArray(report.mounts) ? report.mounts : [];
313
1182
  const expected = Object.entries(routes)
@@ -350,12 +1219,12 @@ function readReport(root, input, label) {
350
1219
  function repairFor(id) {
351
1220
  if (id === 'definition.semantic-contract')
352
1221
  return {
353
- id, why: '结构化模板不能替代真实的身份、职责、交付物和拒绝边界。',
1222
+ id, why: '结构化模板不能替代真实的本体定义、岗位底线、做事范式和常犯错误。',
354
1223
  actions: ['评审并替换 AGENTS.md 中所有 <...> 占位符,再复跑 audit。'],
355
1224
  };
356
1225
  if (id === 'definition.body')
357
1226
  return {
358
- id, why: 'Agent 必须有长期身份、职责、交付物和拒绝边界。',
1227
+ id, why: 'Agent 必须有长期定义、岗位底线、做事范式和常犯错误。',
359
1228
  actions: ['从 compose Skill 的 AGENTS.template.md 创建 AGENTS.md,再复跑 audit。'],
360
1229
  };
361
1230
  if (id === 'storage.memory')
@@ -390,8 +1259,13 @@ function repairFor(id) {
390
1259
  };
391
1260
  if (id === 'host.basic-exposure')
392
1261
  return {
393
- id, why: 'OpenCode 只能从项目 exposure 加载本次装配的 Basic Skill。',
394
- actions: ['把 canonical dingtalk-basic-behavior 物化到 .agents/skills/dingtalk-basic-behavior。'],
1262
+ id, why: 'OpenCode 只能从项目 exposure 完整继承本次装配的 Basic Skill 入口、references 与 assets。',
1263
+ actions: ['把 canonical dingtalk-basic-behavior 全树物化到 .agents/skills/dingtalk-basic-behavior。'],
1264
+ };
1265
+ if (id === 'host.definition-rule')
1266
+ return {
1267
+ id, why: 'OpenCode 只会把项目根 AGENTS.md 作为本地原生 rule;重复 custom instruction 会形成冗余通道。',
1268
+ actions: ['把项目本体统一为根目录 AGENTS.md,并从 opencode.json#instructions 删除其等价路径。'],
395
1269
  };
396
1270
  if (id === 'host.basic-instruction')
397
1271
  return {
@@ -405,8 +1279,8 @@ function repairFor(id) {
405
1279
  };
406
1280
  if (id === 'host.load-probe')
407
1281
  return {
408
- id, why: '静态目录和配置不能证明模型真的读到 Basic 正文。',
409
- actions: ['执行 dta agent audit --verify-load --yes --json,保留随机 probe 与 Session directory 证据。'],
1282
+ id, why: '静态目录和配置不能证明 Host 已解析 Agent Definition 并让模型读到 Basic 正文。',
1283
+ actions: ['执行 dta agent audit --verify-load --yes --json,保留 resolved instructions、随机 probe 与 Session directory 证据。'],
410
1284
  };
411
1285
  if (id === 'authority.dws')
412
1286
  return {
@@ -458,4 +1332,47 @@ function isInside(root, candidate) {
458
1332
  const rel = relative(root, candidate);
459
1333
  return rel === '' || (rel !== '..' && !rel.startsWith(`..${process.platform === 'win32' ? '\\' : '/'}`));
460
1334
  }
1335
+ function inspectContainedPath(root, input) {
1336
+ const rootReal = realpathSync(resolve(root));
1337
+ const candidate = resolve(rootReal, input);
1338
+ if (!isInside(rootReal, candidate)) {
1339
+ return { valid: false, path: candidate, reason: 'outside-agent-root' };
1340
+ }
1341
+ const rel = relative(rootReal, candidate);
1342
+ let cursor = rootReal;
1343
+ for (const segment of rel.split(/[\\/]/).filter(Boolean)) {
1344
+ cursor = join(cursor, segment);
1345
+ if (!pathEntryExists(cursor))
1346
+ break;
1347
+ const info = lstatSync(cursor);
1348
+ if (info.isSymbolicLink()) {
1349
+ return { valid: false, path: candidate, reason: `symlink:${relative(rootReal, cursor)}` };
1350
+ }
1351
+ if (cursor !== candidate && !info.isDirectory()) {
1352
+ return { valid: false, path: candidate, reason: `non-directory-ancestor:${relative(rootReal, cursor)}` };
1353
+ }
1354
+ }
1355
+ let ancestor = candidate;
1356
+ while (!pathEntryExists(ancestor) && dirname(ancestor) !== ancestor)
1357
+ ancestor = dirname(ancestor);
1358
+ try {
1359
+ const projected = resolve(realpathSync(ancestor), relative(ancestor, candidate));
1360
+ if (!isInside(rootReal, projected)) {
1361
+ return { valid: false, path: candidate, reason: 'realpath-outside-agent-root' };
1362
+ }
1363
+ }
1364
+ catch (error) {
1365
+ return { valid: false, path: candidate, reason: `realpath-error:${error.message}` };
1366
+ }
1367
+ return { valid: true, path: candidate, reason: '' };
1368
+ }
1369
+ function pathEntryExists(path) {
1370
+ try {
1371
+ lstatSync(path);
1372
+ return true;
1373
+ }
1374
+ catch {
1375
+ return false;
1376
+ }
1377
+ }
461
1378
  //# sourceMappingURL=agent-audit.js.map