intentdna 1.4.8 → 1.4.9
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/dist/hooks/enforce.js
CHANGED
|
@@ -383,10 +383,10 @@ function checkPathAgainstScope(rolesScopeMap, input, filePath) {
|
|
|
383
383
|
continue;
|
|
384
384
|
const writeGlobs = entry.scope.write ?? [];
|
|
385
385
|
if (writeGlobs.length === 0) {
|
|
386
|
-
return
|
|
386
|
+
return allowOutput(`WARN [Intent DNA]: Role '${entry.role_name}' has no write permission (path: ${filePath}). Merge-time scope gate will filter.`);
|
|
387
387
|
}
|
|
388
388
|
if (!checkWriteAllowed(relativePath, writeGlobs)) {
|
|
389
|
-
return
|
|
389
|
+
return allowOutput(`WARN [Intent DNA]: Role '${entry.role_name}' cannot write to '${filePath}' (allowed: ${writeGlobs.join(", ")}). Merge-time scope gate will filter.`);
|
|
390
390
|
}
|
|
391
391
|
return null; // Role matched, write allowed
|
|
392
392
|
}
|
|
@@ -258,11 +258,13 @@ function generateGroupExecution(group, stepMap, options, plan) {
|
|
|
258
258
|
}
|
|
259
259
|
// Merge worktrees back (escalate on conflict)
|
|
260
260
|
lines.push("");
|
|
261
|
-
lines.push(" #
|
|
261
|
+
lines.push(" # Scope gate + merge worktrees back to main branch");
|
|
262
262
|
for (const id of group.step_ids) {
|
|
263
|
+
const step = stepMap.get(id);
|
|
263
264
|
const varId = sanitizeForBash(id);
|
|
264
265
|
const safeId = sanitizeForShell(id);
|
|
265
266
|
lines.push(` if [ "$STEP_${varId}_STATUS" -eq 0 ]; then`);
|
|
267
|
+
lines.push(` scope_gate "${safeId}" "dna-wt-${safeId}" "${step.role}" "${safeId}"`);
|
|
266
268
|
lines.push(` merge_worktree "dna-wt-${safeId}" "${safeId}"`);
|
|
267
269
|
lines.push(" fi");
|
|
268
270
|
}
|
|
@@ -336,6 +338,77 @@ function generateMergeWorktreeFn() {
|
|
|
336
338
|
"",
|
|
337
339
|
];
|
|
338
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* Generate the scope_gate bash helper function.
|
|
343
|
+
* Filters out-of-scope file changes from a worktree branch before merging.
|
|
344
|
+
* Reads role write scope from .dna/compiled/ir.json, reverts unauthorized files,
|
|
345
|
+
* and writes trace entries for each violation.
|
|
346
|
+
* Fail-open: never prevents merge from proceeding.
|
|
347
|
+
*/
|
|
348
|
+
function generateScopeGateFn() {
|
|
349
|
+
return [
|
|
350
|
+
"# Scope gate: filter out-of-scope changes before merge (three-layer defense L3)",
|
|
351
|
+
"scope_gate() {",
|
|
352
|
+
' local name="$1" branch="$2" role="$3" step_id="$4"',
|
|
353
|
+
' local ir_path="${PROJECT_ROOT}/.dna/compiled/ir.json"',
|
|
354
|
+
"",
|
|
355
|
+
' [ ! -f "$ir_path" ] && return 0',
|
|
356
|
+
"",
|
|
357
|
+
" # Read write scope globs for this role from IR",
|
|
358
|
+
" local allowed_globs",
|
|
359
|
+
" allowed_globs=$(jq -r --arg role \"$role\" \\",
|
|
360
|
+
" '.roles_scope_map[]? | select(.role_name == $role) | .scope.write[]?' \\",
|
|
361
|
+
' "$ir_path" 2>/dev/null)',
|
|
362
|
+
"",
|
|
363
|
+
" # No scope defined — allow all changes",
|
|
364
|
+
' [ -z "$allowed_globs" ] && return 0',
|
|
365
|
+
"",
|
|
366
|
+
" # Get files changed by this branch vs main",
|
|
367
|
+
" local changed",
|
|
368
|
+
' changed=$(git diff --name-only "$MAIN_BRANCH...$branch" 2>/dev/null)',
|
|
369
|
+
' [ -z "$changed" ] && return 0',
|
|
370
|
+
"",
|
|
371
|
+
" local rejected=()",
|
|
372
|
+
' while IFS= read -r file; do',
|
|
373
|
+
' [ -z "$file" ] && continue',
|
|
374
|
+
" local allowed=false",
|
|
375
|
+
' while IFS= read -r glob; do',
|
|
376
|
+
' [ -z "$glob" ] && continue',
|
|
377
|
+
" # Prefix matching (consistent with enforce.ts globToPrefix)",
|
|
378
|
+
' local prefix="${glob%%\\**}"',
|
|
379
|
+
' if [ -z "$prefix" ] || [[ "$file" == "${prefix}"* ]]; then',
|
|
380
|
+
" allowed=true",
|
|
381
|
+
" break",
|
|
382
|
+
" fi",
|
|
383
|
+
' done <<< "$allowed_globs"',
|
|
384
|
+
' if [ "$allowed" = false ]; then',
|
|
385
|
+
' rejected+=("$file")',
|
|
386
|
+
" fi",
|
|
387
|
+
' done <<< "$changed"',
|
|
388
|
+
"",
|
|
389
|
+
' [ ${#rejected[@]} -eq 0 ] && return 0',
|
|
390
|
+
"",
|
|
391
|
+
" echo \"[Intent DNA] Scope gate: ${#rejected[@]} file(s) outside role '${role}' scope, reverted:\"",
|
|
392
|
+
' printf " - %s\\n" "${rejected[@]}"',
|
|
393
|
+
"",
|
|
394
|
+
" # Revert out-of-scope files in worktree branch",
|
|
395
|
+
' for file in "${rejected[@]}"; do',
|
|
396
|
+
' (cd ".dna/worktrees/${name}" && git checkout "$MAIN_BRANCH" -- "$file") 2>/dev/null || true',
|
|
397
|
+
" done",
|
|
398
|
+
' (cd ".dna/worktrees/${name}" && git add -A && git commit --amend --no-edit) 2>/dev/null || true',
|
|
399
|
+
"",
|
|
400
|
+
" # Write trace entries for scope violations",
|
|
401
|
+
' local trace_dir="${PROJECT_ROOT}/.dna/state/trace"',
|
|
402
|
+
' mkdir -p "$trace_dir" 2>/dev/null || true',
|
|
403
|
+
' local trace_file="${trace_dir}/trace-$(date +%Y-%m-%d).jsonl"',
|
|
404
|
+
' for file in "${rejected[@]}"; do',
|
|
405
|
+
" printf '{\"trace_id\":\"%s\",\"event\":\"ScopeGate\",\"agent_type\":\"dna-%s\",\"step\":\"%s\",\"decision\":\"warn\",\"reason\":\"Merge-time scope gate reverted out-of-scope file\",\"target_path\":\"%s\",\"duration_ms\":0,\"timestamp\":\"%s\"}\\n' \\",
|
|
406
|
+
" \"$(uuidgen | tr '[:upper:]' '[:lower:]')\" \"$role\" \"$step_id\" \"$file\" \"$(date -u +%Y-%m-%dT%H:%M:%S.000Z)\" >> \"$trace_file\"",
|
|
407
|
+
" done",
|
|
408
|
+
"}",
|
|
409
|
+
"",
|
|
410
|
+
];
|
|
411
|
+
}
|
|
339
412
|
/**
|
|
340
413
|
* Generate transition check code after all groups execute.
|
|
341
414
|
*/
|
|
@@ -404,9 +477,10 @@ export function compileWorkflowToShell(plan, options) {
|
|
|
404
477
|
// run_agent function
|
|
405
478
|
lines.push(...generateRunAgentFn(opts));
|
|
406
479
|
lines.push("");
|
|
407
|
-
// merge_worktree
|
|
480
|
+
// merge_worktree + scope_gate functions (only if needed)
|
|
408
481
|
if (needsWorktreeSupport(plan)) {
|
|
409
482
|
lines.push(...generateMergeWorktreeFn());
|
|
483
|
+
lines.push(...generateScopeGateFn());
|
|
410
484
|
}
|
|
411
485
|
// Main execution
|
|
412
486
|
if (plan.retry.max_retries > 0) {
|
package/package.json
CHANGED
|
@@ -19,8 +19,31 @@ DNA 模板可声明并行执行策略和文件隔离策略,编译到 workflow-
|
|
|
19
19
|
| Schema 声明 | step 属性 + parallel block 双支持 | 灵活性 + 简洁性兼顾 |
|
|
20
20
|
| auto 模式 | 编译时 scope overlap 检测 | DNA 独有能力——Role scope 编译时已知 |
|
|
21
21
|
| merge 策略 | escalate only (MVP) | 冲突报告给用户,不自动解决 |
|
|
22
|
+
| scope.write 语义 | merge-time accept filter | 不是运行时 block rule,是合并时的产出过滤器 |
|
|
22
23
|
| 模型差异 | 暂不考虑 | 后续扩展 |
|
|
23
24
|
|
|
25
|
+
## 设计哲学:默会知识与三层防御
|
|
26
|
+
|
|
27
|
+
参考波兰尼(Michael Polanyi)默会知识理论:意图不能被规则穷尽,但可以通过适当的"身体"来实现。
|
|
28
|
+
|
|
29
|
+
**核心洞察**:`scope.write: ["test/**"]` 表达的是模板作者的**意图**("这个角色只该写测试文件"),不应该被实现为运行时工具拦截(显性规则,不完备),而应该被实现为物理隔离 + 出口把关(身体 + 审查)。
|
|
30
|
+
|
|
31
|
+
### 为什么工具拦截不可靠
|
|
32
|
+
|
|
33
|
+
路径级 scope enforce 通过拦截工具检查文件路径(Write/Edit/Bash/MCP...)。但写文件的工具不可枚举——每出现一个新工具就是一个绕过点。这是认识论问题,不是实现问题:显性规则永远追不上默会知识的丰富度。
|
|
34
|
+
|
|
35
|
+
### 三层对应
|
|
36
|
+
|
|
37
|
+
| 层 | 时机 | 机制 | 波兰尼对应 | 可靠度 |
|
|
38
|
+
|---|---|---|---|---|
|
|
39
|
+
| **工具控制** | PreToolUse | `tool_permissions.deny` — 禁止工具本身 | 显性知识(可完全条文化) | 高——工具名有限可枚举 |
|
|
40
|
+
| **过程提示** | PreToolUse | 当前 scope enforce(保留为软防线) | 辅助线索(from) | 低——最佳努力,不可依赖 |
|
|
41
|
+
| **出口把关** | Merge time | **scope gate 过滤 git diff** — 只接受 scope 内改动 | 焦点判断(to) | **确定性——基于实际产出** |
|
|
42
|
+
|
|
43
|
+
scope.write 的真正语义:**merge 时的 accept filter**,不是运行时的 block rule。
|
|
44
|
+
|
|
45
|
+
agent 在 worktree 内有完整读权限(理解上下文)和写自由(不被微管理),但只有符合角色意图的产出才能合并回主分支。如同管理者不盯每次敲键盘,而在 review 时评估产出。
|
|
46
|
+
|
|
24
47
|
## Schema 设计
|
|
25
48
|
|
|
26
49
|
### isolation 取值
|
|
@@ -152,6 +175,39 @@ git worktree remove .dna/worktrees/fix-auth 2>/dev/null
|
|
|
152
175
|
git worktree remove .dna/worktrees/fix-api 2>/dev/null
|
|
153
176
|
```
|
|
154
177
|
|
|
178
|
+
### Merge-time scope gate
|
|
179
|
+
|
|
180
|
+
合并前过滤越权改动(scope.write 的真正执行点):
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
# scope gate: 只接受 scope 内的改动
|
|
184
|
+
scope_gate() {
|
|
185
|
+
local branch=$1 role=$2
|
|
186
|
+
# 获取该 role 的 write scope globs (从 IR 读取)
|
|
187
|
+
local allowed_globs=$(read_scope_globs "$role")
|
|
188
|
+
|
|
189
|
+
# 检查所有改动文件
|
|
190
|
+
local changed=$(git diff --name-only HEAD...$branch)
|
|
191
|
+
local rejected=()
|
|
192
|
+
for file in $changed; do
|
|
193
|
+
if ! matches_any_glob "$file" $allowed_globs; then
|
|
194
|
+
rejected+=("$file")
|
|
195
|
+
# 在 worktree 中 revert 越权改动
|
|
196
|
+
(cd .dna/worktrees/$branch && git checkout HEAD -- "$file")
|
|
197
|
+
fi
|
|
198
|
+
done
|
|
199
|
+
|
|
200
|
+
if [ ${#rejected[@]} -gt 0 ]; then
|
|
201
|
+
echo "[Intent DNA] Scope gate: ${#rejected[@]} file(s) outside scope, reverted:"
|
|
202
|
+
printf " - %s\n" "${rejected[@]}"
|
|
203
|
+
# trace 记录越权尝试 → 表观遗传反馈
|
|
204
|
+
fi
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
scope_gate "fix-auth" "surgeon"
|
|
208
|
+
scope_gate "fix-api" "surgeon"
|
|
209
|
+
```
|
|
210
|
+
|
|
155
211
|
## Constraint IR 扩展
|
|
156
212
|
|
|
157
213
|
```typescript
|
|
@@ -175,11 +231,14 @@ interface WorkflowIR {
|
|
|
175
231
|
|
|
176
232
|
| DNA 概念 | 在并行隔离中的角色 |
|
|
177
233
|
|---|---|
|
|
234
|
+
| **Role scope.write** | **merge-time scope gate 的输入**——决定哪些改动可以合并回主分支 |
|
|
178
235
|
| **Role scope** | auto 模式的编译时 overlap 分析输入 |
|
|
236
|
+
| **tool_permissions.deny** | 工具级控制(可靠层)——reviewer 不能用 Edit |
|
|
179
237
|
| **Handoff** | 并行 step 的 artifact 传递(已实现) |
|
|
180
238
|
| **Threshold codon** | 可声明 `parallel_without_isolation == false` 硬约束 |
|
|
181
239
|
| **Attract codon** | `attract: worktree_per_task` 软提示(保留兼容) |
|
|
182
|
-
| **Hooks enforce** |
|
|
240
|
+
| **Hooks enforce (PreToolUse)** | 保留为过程提示(辅助软防线),不作为安全保障依赖 |
|
|
241
|
+
| **表观遗传** | scope gate 越权记录 → trace → epigenetic marker 反馈 |
|
|
183
242
|
|
|
184
243
|
## 不做
|
|
185
244
|
|
|
@@ -187,6 +246,7 @@ interface WorkflowIR {
|
|
|
187
246
|
- LLM 模型差异适配(暂不考虑)
|
|
188
247
|
- context budget 管理(已由独立进程 + Handoff 解决)
|
|
189
248
|
- container 级隔离(过度设计)
|
|
249
|
+
- sparse-checkout(限制读权限会破坏 agent 理解上下文的能力)
|
|
190
250
|
|
|
191
251
|
## Acceptance Criteria
|
|
192
252
|
|
|
@@ -197,9 +257,12 @@ interface WorkflowIR {
|
|
|
197
257
|
- [ ] Compiler: parallel block 展开为 step + depends_on
|
|
198
258
|
- [ ] workflow-runner: isolation=none 行为不变(向后兼容)
|
|
199
259
|
- [ ] workflow-runner: isolation=worktree 生成 git worktree 生命周期脚本
|
|
260
|
+
- [ ] workflow-runner: merge 前执行 scope gate(过滤越权改动)
|
|
200
261
|
- [ ] workflow-runner: merge 冲突时 escalate(报告 + 保留分支)
|
|
262
|
+
- [ ] scope gate: 越权改动被 revert + trace 记录
|
|
201
263
|
- [ ] E2E: 两个并行 step 写不同目录,auto → none,正确执行
|
|
202
264
|
- [ ] E2E: 两个并行 step 写相同目录,auto → worktree,隔离执行 + merge
|
|
265
|
+
- [ ] E2E: agent 写越权文件,scope gate 过滤 + trace 记录
|
|
203
266
|
- [ ] Dogfood: flutter-rewrite rescue workflow 可配置 isolation
|
|
204
267
|
- [ ] IR: ParallelGroup 包含 isolation + scope_overlap 信息
|
|
205
268
|
- [ ] 测试覆盖所有新增代码
|