reasonweave 0.0.0-stage → 1.3.2
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/AGENTS.md +101 -0
- package/INSTALL.md +258 -0
- package/README.md +280 -2
- package/bin/lab.mjs +888 -0
- package/constitution.md +55 -0
- package/docs/CONSUMER-PROMPTS.md +474 -0
- package/docs/backlogs/01-forward-wait-self-detach.md +18 -0
- package/docs/backlogs/02-decouple-wait-and-daemon-for-agent-hosts.md +35 -0
- package/docs/backlogs/03-baton-wait-multi-id-lightweight-pubsub.md +34 -0
- package/docs/backlogs/04-adr-cross-repo-automated-dispatch-boundary.md +19 -0
- package/docs/backlogs/05-feat-units-worktree-isolation-engine.md +23 -0
- package/docs/backlogs/06-feat-blind-audit-automated-clean-runner.md +22 -0
- package/docs/backlogs/07-feat-attention-throttle-silent-pipe.md +23 -0
- package/docs/backlogs/08-future-units-shadow-ledger.md +22 -0
- package/docs/backlogs/09-feat-agent-baton-core-convergence-and-resilience.md +42 -0
- package/docs/backlogs/10-feat-anti-orphan-deterministic-wiring-governance.md +35 -0
- package/docs/backlogs/11-perf-orphan-index-fs-read.md +16 -0
- package/docs/backlogs/12-feat-vibe-security-anti-suppression-and-orphan-deps.md +43 -0
- package/docs/backlogs/13-feat-sdd-reppit-tiered-guardrails.md +40 -0
- package/docs/backlogs/14-feat-universal-cli-contract-and-hermetic-test-fixtures.md +48 -0
- package/docs/backlogs/15-feat-reasonweave-rebranding-and-doc-code-synchronization-invariant.md +44 -0
- package/docs/backlogs/16-feat-micro-engineering-skills-and-sdd-baton-fusion.md +52 -0
- package/docs/codegraph-readiness-check.md +49 -0
- package/group/architecture/adr/0001-handoff-async-wait-and-reactive-wakeup.md +87 -0
- package/group/architecture/adr/0002-de-ritualize-handoff-prompt-for-intra-repo.md +53 -0
- package/group/architecture/adr/0003-attention-throttle-and-workspace-driver.md +65 -0
- package/group/architecture/adr/0004-spec-guard-and-drift-prevention.md +90 -0
- package/group/architecture/adr/0005-global-orphan-scavenger-and-read-only-invariants.md +94 -0
- package/group/architecture/adr/0006-triad-engineering-defense-and-mutation-sensitivity.md +68 -0
- package/group/architecture/adr/0007-agent-baton-state-machine-defense-and-workspace-jail-spec.md +86 -0
- package/group/architecture/adr/0008-agent-baton-core-convergence-and-heterogeneous-session-resilience.md +133 -0
- package/group/architecture/adr/0009-anti-orphan-deterministic-wiring-governance-and-federated-guardrails.md +108 -0
- package/group/architecture/adr/0010-agent-baton-identity-security-cryptographic-credentials-and-prompt-anti-confusion.md +119 -0
- package/group/architecture/adr/0011-vibe-security-anti-suppression-and-dependency-reachability.md +161 -0
- package/group/architecture/adr/0012-spec-driven-development-reppit-and-tiered-guardrails.md +130 -0
- package/group/architecture/adr/0013-universal-cli-contract-hermetic-testing-and-defensive-probes.md +110 -0
- package/group/architecture/adr/0014-reasonweave-rebranding-and-doc-code-synchronization-invariant.md +108 -0
- package/group/architecture/adr/0015-micro-engineering-skills-and-dual-core-gate-invariant.md +127 -0
- package/group/architecture/adr/INDEX.md +29 -0
- package/group/architecture/adr/templates/adr-template.md +33 -0
- package/group/architecture/dependency-graph.md +60 -0
- package/group/contracts/npm-packaging.md +69 -0
- package/group/product/intent.md +24 -0
- package/package.json +93 -4
- package/scripts/sync-all-consumers.mjs +174 -0
- package/specs/RFC-20261110-SPEC-GUARD-UNIT.md +353 -0
- package/specs/templates/01-research.md.template +23 -0
- package/specs/templates/02-proposals.md.template +22 -0
- package/specs/templates/03-spec.md.template +33 -0
- package/specs/templates/04-tasks.md.template +29 -0
- package/specs/templates/05-evidence.md.template +29 -0
- package/specs/templates/README.md +26 -0
- package/units/adr/AGENTS.md +85 -0
- package/units/adr/CHANGELOG.md +20 -0
- package/units/adr/README.md +88 -0
- package/units/adr/VERSION.md +10 -0
- package/units/adr/docs/existing-project-prompt.md +24 -0
- package/units/adr/docs/new-project-prompt.md +36 -0
- package/units/adr/scripts/adr-init.sh +101 -0
- package/units/adr/templates/INDEX.md +19 -0
- package/units/adr/templates/adr-frozen.md +33 -0
- package/units/adr/templates/adr-light.md +56 -0
- package/units/adr/templates/adr-standard.md +61 -0
- package/units/backlog/AGENTS.md +53 -0
- package/units/backlog/AUTONOMOUS-GOVERNANCE.md +98 -0
- package/units/backlog/CHANGELOG.md +37 -0
- package/units/backlog/README.md +124 -0
- package/units/backlog/VERSION.md +11 -0
- package/units/backlog/docs/existing-project-ironrules-prompt.md +31 -0
- package/units/backlog/docs/existing-project-prompt.md +24 -0
- package/units/backlog/docs/new-project-prompt.md +19 -0
- package/units/backlog/templates/entry.md +39 -0
- package/units/backlog/templates/project-ironrules.md +36 -0
- package/units/handoffs/AGENT-GUIDE.md +118 -0
- package/units/handoffs/AGENTS.md +28 -0
- package/units/handoffs/CHANGELOG.md +498 -0
- package/units/handoffs/CLAUDE.md +28 -0
- package/units/handoffs/COMMANDS.md +187 -0
- package/units/handoffs/DESIGN.md +74 -0
- package/units/handoffs/README.md +456 -0
- package/units/handoffs/RESPONSE-TEMPLATE.md +138 -0
- package/units/handoffs/VERSION.md +1 -0
- package/units/handoffs/handoff-status.mjs +1017 -0
- package/units/handoffs/handoff.mjs +5675 -0
- package/units/orphan/CHANGELOG.md +118 -0
- package/units/orphan/DESIGN.md +484 -0
- package/units/orphan/README.md +143 -0
- package/units/orphan/VERSION.md +1 -0
- package/units/orphan/adapters/android/index.mjs +140 -0
- package/units/orphan/adapters/python/index.mjs +232 -0
- package/units/orphan/adapters/web/index.mjs +280 -0
- package/units/orphan/bin/lab-orphan.mjs +298 -0
- package/units/orphan/core/blob-reader.mjs +142 -0
- package/units/orphan/core/classifier.mjs +261 -0
- package/units/orphan/core/cleaner.mjs +166 -0
- package/units/orphan/core/diff-checker.mjs +341 -0
- package/units/orphan/core/inverted-index.mjs +124 -0
- package/units/orphan/core/orphan-deps.mjs +192 -0
- package/units/orphan/core/protocol-s1-s3.mjs +194 -0
- package/units/orphan/core/solver.mjs +217 -0
- package/units/orphan/index.mjs +105 -0
- package/units/orphan/templates/ci/orphan-guard.yml +36 -0
- package/units/orphan/templates/pre-commit.d/30-orphan-guard +34 -0
- package/units/orphan/templates/wiring/README.md +51 -0
- package/units/orphan/templates/wiring/miniprogram-route-guard.js.template +80 -0
- package/units/orphan/templates/wiring/pre-commit-node.sh.template +73 -0
- package/units/orphan/templates/wiring/project-agents-gemini-templates.md +60 -0
- package/units/orphan/templates/wiring/python-parity-test.py.template +100 -0
- package/units/orphan/templates/wiring/rust-exhaustive-match.md +65 -0
- package/units/orphan/templates/wiring/swift-periphery.md +56 -0
- package/units/orphan/tests/adapters.test.mjs +243 -0
- package/units/orphan/tests/orphan-deps.test.mjs +131 -0
- package/units/orphan/tests/phase3-gates.test.mjs +135 -0
- package/units/playbook/README.md +106 -0
- package/units/playbook/ZSPEC-AGENT-GUIDE.md +99 -0
- package/units/playbook/docs/01-architecture.md +99 -0
- package/units/playbook/docs/02-bootstrap.md +126 -0
- package/units/playbook/docs/03-units.md +91 -0
- package/units/playbook/docs/04-constitution.md +113 -0
- package/units/playbook/docs/05-adr.md +33 -0
- package/units/playbook/docs/06-spec-kit.md +76 -0
- package/units/playbook/docs/07-backlog.md +30 -0
- package/units/playbook/docs/08-codegraph.md +79 -0
- package/units/playbook/docs/09-skills.md +101 -0
- package/units/playbook/docs/10-handoff.md +72 -0
- package/units/playbook/docs/11-evidence.md +163 -0
- package/units/playbook/docs/12-gates.md +124 -0
- package/units/playbook/docs/13-parallel.md +115 -0
- package/units/playbook/docs/14-dependency-graph.md +86 -0
- package/units/playbook/docs/15-memory.md +153 -0
- package/units/playbook/docs/16-change-control.md +121 -0
- package/units/playbook/docs/17-prompts.md +121 -0
- package/units/playbook/docs/18-shift-left.md +97 -0
- package/units/playbook/docs/19-anti-orphan-vertical-slicing.md +102 -0
- package/units/playbook/docs/20-vibe-security-and-anti-suppression.md +88 -0
- package/units/playbook/docs/21-spec-driven-reppit-closed-loop.md +88 -0
- package/units/playbook/examples/owisel.md +80 -0
- package/units/playbook/references/Agent-Engineering-Framework-AEF-v1.0.md +2033 -0
- package/units/playbook/references/links.md +103 -0
- package/units/playbook/templates/AGENTS.root.md +44 -0
- package/units/playbook/templates/AGENTS.unit.md +52 -0
- package/units/playbook/templates/adr/INDEX.md +17 -0
- package/units/playbook/templates/adr/NNNN-template.md +56 -0
- package/units/playbook/templates/backlogs/entry.md +30 -0
- package/units/playbook/templates/constitution.md +93 -0
- package/units/playbook/templates/evidence/README.md +36 -0
- package/units/playbook/templates/group/architecture/dependency-graph.md +48 -0
- package/units/playbook/templates/group/architecture/system.md +59 -0
- package/units/playbook/templates/group/contracts/api.md +67 -0
- package/units/playbook/templates/group/product/intent.md +30 -0
- package/units/playbook/templates/group/product/requirements.md +48 -0
- package/units/playbook/templates/unit-skeleton/README.md +35 -0
- package/units/preflight/AGENTS.md +45 -0
- package/units/preflight/CHANGELOG.md +55 -0
- package/units/preflight/INSTALL.md +65 -0
- package/units/preflight/README.md +104 -0
- package/units/preflight/VERSION.md +3 -0
- package/units/preflight/docs/audit-requirements.md +114 -0
- package/units/preflight/docs/existing-project-prompt.md +25 -0
- package/units/preflight/docs/new-project-prompt.md +25 -0
- package/units/preflight/docs/transfers/CONSUMER-INSTALL.md +40 -0
- package/units/preflight/docs/transfers/CONSUMER-SELFCHECK.md +51 -0
- package/units/preflight/docs/transfers/README.md +39 -0
- package/units/preflight/docs/transfers/prospero.md +26 -0
- package/units/preflight/templates/check.sh +43 -0
- package/units/preflight/templates/pre-commit +55 -0
- package/units/preflight/templates/pre-commit.d/20-preflight +15 -0
- package/units/preflight/templates/preflight.conf.example +21 -0
- package/units/preflight/templates/preflight.sh +189 -0
- package/units/preflight/templates/wrap-up.md +9 -0
- package/units/review-gate/AGENTS.md +42 -0
- package/units/review-gate/CHANGELOG.md +58 -0
- package/units/review-gate/README.md +118 -0
- package/units/review-gate/VERSION.md +3 -0
- package/units/review-gate/clean-runner.mjs +350 -0
- package/units/review-gate/docs/existing-project-prompt.md +28 -0
- package/units/review-gate/docs/new-project-prompt.md +38 -0
- package/units/review-gate/docs/transfers/iggle.md +34 -0
- package/units/review-gate/docs/transfers/prospero.md +35 -0
- package/units/review-gate/docs/transfers/swoop.md +36 -0
- package/units/review-gate/docs/transfers/yiqiyihuihua.md +35 -0
- package/units/review-gate/templates/.semgrepignore +37 -0
- package/units/review-gate/templates/pre-commit +130 -0
- package/units/review-gate/templates/scan.sh +158 -0
- package/units/skills/AGENTS.md +17 -0
- package/units/skills/CHANGELOG.md +18 -0
- package/units/skills/README.md +63 -0
- package/units/skills/VERSION.md +1 -0
- package/units/skills/bin/review-package.mjs +99 -0
- package/units/skills/bin/task-brief.mjs +102 -0
- package/units/skills/catalog/brainstorming/SKILL.md +75 -0
- package/units/skills/catalog/brainstorming/references/design-doc-template.md +28 -0
- package/units/skills/catalog/systematic-debugging/SKILL.md +101 -0
- package/units/skills/catalog/systematic-debugging/references/root-cause-tracing.md +23 -0
- package/units/skills/catalog/test-driven-development/SKILL.md +88 -0
- package/units/skills/catalog/test-driven-development/references/writing-good-tests.md +13 -0
- package/units/skills/catalog/verification-before-completion/SKILL.md +73 -0
- package/units/skills/skills.mjs +181 -0
- package/units/skills/tests/pipeline.test.mjs +131 -0
- package/units/skills/tests/skill-contract.test.mjs +67 -0
- package/units/skills/tests/skills-cli.test.mjs +107 -0
- package/units/spec-guard/AGENTS.md +43 -0
- package/units/spec-guard/CHANGELOG.md +88 -0
- package/units/spec-guard/README.md +148 -0
- package/units/spec-guard/VERSION.md +1 -0
- package/units/spec-guard/bin/spec-guard.mjs +431 -0
- package/units/spec-guard/core/adr-parser.mjs +540 -0
- package/units/spec-guard/core/matrix-checker.mjs +587 -0
- package/units/spec-guard/core/mutation-runner.mjs +267 -0
- package/units/spec-guard/core/scanner.mjs +333 -0
- package/units/spec-guard/core/telemetry_authenticator.mjs +484 -0
- package/units/spec-guard/core/wiring_tracer.mjs +306 -0
- package/units/spec-guard/templates/annotations/ContractOnly.kt +19 -0
- package/units/spec-guard/templates/annotations/Invariant.kt +16 -0
- package/units/spec-guard/templates/annotations/invariant.go +19 -0
- package/units/spec-guard/templates/annotations/invariant.py +38 -0
- package/units/spec-guard/templates/annotations/invariant.ts +39 -0
- package/units/spec-guard/templates/ci/spec-guard-audit.yml +23 -0
- package/units/spec-guard/templates/pre-commit.d/15-spec-guard +72 -0
- package/units/spec-guard/templates/spec-matrix.overlay.yml +70 -0
- package/units/spec-guard/tests/spec-guard.test.mjs +716 -0
- package/units/spec-guard/tests/telemetry_authenticator.test.mjs +633 -0
- package/units/worktree/AGENTS.md +22 -0
- package/units/worktree/CHANGELOG.md +14 -0
- package/units/worktree/README.md +65 -0
- package/units/worktree/VERSION.md +1 -0
- package/units/worktree/worktree.mjs +352 -0
- package//344/275/277/347/224/250/350/257/264/346/230/216.md +322 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 微信小程序轻量级路由与组件闭环探针 (Fast Gate Pre-commit Probe)
|
|
4
|
+
*
|
|
5
|
+
* 痛点:AI 经常在 pages/ 目录下生成了新页面或在 components/ 目录下编写了组件,
|
|
6
|
+
* 但忘记在 app.json 中声明页面,或者忘记在父页面的 *.json usingComponents 中引用。
|
|
7
|
+
*
|
|
8
|
+
* 设计特点:
|
|
9
|
+
* - 零重量级外部依赖(纯 Node 原生 fs/path 模块)。
|
|
10
|
+
* - 执行耗时 < 100ms,极度适合作为 git pre-commit 快速门禁。
|
|
11
|
+
* - 报错直接打印修复 JSON 代码段,方便 Agent 一键自愈。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
|
|
17
|
+
function checkMiniProgramWiring() {
|
|
18
|
+
const rootDir = process.cwd();
|
|
19
|
+
const appJsonPath = path.join(rootDir, 'app.json');
|
|
20
|
+
|
|
21
|
+
if (!fs.existsSync(appJsonPath)) {
|
|
22
|
+
// 若不是小程序根目录则静默跳过
|
|
23
|
+
process.exit(0);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let appConfig;
|
|
27
|
+
try {
|
|
28
|
+
appConfig = JSON.parse(fs.readFileSync(appJsonPath, 'utf8'));
|
|
29
|
+
} catch (err) {
|
|
30
|
+
console.error(`❌ [Anti-Orphan] app.json 解析失败: ${err.message}`);
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const registeredPages = new Set(appConfig.pages || []);
|
|
35
|
+
if (Array.isArray(appConfig.subpackages)) {
|
|
36
|
+
for (const sub of appConfig.subpackages) {
|
|
37
|
+
const root = sub.root.replace(/\/$/, '');
|
|
38
|
+
for (const p of sub.pages || []) {
|
|
39
|
+
registeredPages.add(`${root}/${p}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 1. 扫描 pages/ 目录下的所有实际页面目录
|
|
45
|
+
const pagesDir = path.join(rootDir, 'pages');
|
|
46
|
+
const missingInConfig = [];
|
|
47
|
+
|
|
48
|
+
if (fs.existsSync(pagesDir)) {
|
|
49
|
+
const pageDirs = fs.readdirSync(pagesDir, { withFileTypes: true })
|
|
50
|
+
.filter(dirent => dirent.isDirectory())
|
|
51
|
+
.map(dirent => dirent.name);
|
|
52
|
+
|
|
53
|
+
for (const p of pageDirs) {
|
|
54
|
+
const pageKey = `pages/${p}/${p}`;
|
|
55
|
+
const pageJson = path.join(pagesDir, p, `${p}.json`);
|
|
56
|
+
const pageWxml = path.join(pagesDir, p, `${p}.wxml`);
|
|
57
|
+
|
|
58
|
+
// 只要具备 wxml 或 json,即视为一个完整页面
|
|
59
|
+
if (fs.existsSync(pageJson) || fs.existsSync(pageWxml)) {
|
|
60
|
+
if (!registeredPages.has(pageKey)) {
|
|
61
|
+
missingInConfig.push(pageKey);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 2. 检查结果判定
|
|
68
|
+
if (missingInConfig.length > 0) {
|
|
69
|
+
console.error(`\n❌ [Anti-Orphan] 发现孤儿页面(已在 pages/ 创建但未在 app.json 声明):`);
|
|
70
|
+
for (const page of missingInConfig) {
|
|
71
|
+
console.error(` - ${page}`);
|
|
72
|
+
}
|
|
73
|
+
console.error(`\n👉 修复指引:请将上述页面路径追加到 app.json 的 "pages" 数组中完成接线闭环。\n`);
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
console.log(`✅ [Anti-Orphan] 小程序路由接线检查通过(已核验 ${registeredPages.size} 个页面)。`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
checkMiniProgramWiring();
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# ==============================================================================
|
|
3
|
+
# Pre-commit Hook: Node.js / ESM 栈反未接线快速门禁 (Fast Gate)
|
|
4
|
+
# 架构规范:遵循 ADR-0009 联邦门禁哲学,执行耗时预算 < 2s
|
|
5
|
+
# - L1 语法硬校验:node --check 逐个校验暂存区 JS/ESM 文件
|
|
6
|
+
# - L2 接线对账:调用 lab-orphan check-diff --staged(增量 O(Δ))
|
|
7
|
+
# ==============================================================================
|
|
8
|
+
set -euo pipefail
|
|
9
|
+
|
|
10
|
+
# 允许通过环境变量紧急豁免(用于紧急 hotfix,事后必须补接线审计)
|
|
11
|
+
if [[ "${SKIP_WIRING_GATE:-0}" == "1" ]]; then
|
|
12
|
+
echo "⚠️ [Anti-Orphan] 检测到 SKIP_WIRING_GATE=1,跳过 Node.js 接线门禁。"
|
|
13
|
+
echo " 提示:豁免提交需要在 PR/回执中补交 Wiring Proof。"
|
|
14
|
+
exit 0
|
|
15
|
+
fi
|
|
16
|
+
|
|
17
|
+
ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
|
|
18
|
+
cd "$ROOT"
|
|
19
|
+
|
|
20
|
+
echo "🔍 [Anti-Orphan] 正在执行 Node.js/ESM 增量接线门禁..."
|
|
21
|
+
|
|
22
|
+
# ---------- L1: 暂存区语法硬校验(毫秒级) ----------
|
|
23
|
+
SYNTAX_FAIL=0
|
|
24
|
+
while IFS= read -r file; do
|
|
25
|
+
[[ -z "$file" ]] && continue
|
|
26
|
+
case "$file" in */node_modules/*|node_modules/*) continue ;; esac
|
|
27
|
+
[[ -f "$file" ]] || continue
|
|
28
|
+
if ! node --check "$file" >/tmp/.precommit-node-check.$$ 2>&1; then
|
|
29
|
+
echo "❌ [Anti-Orphan] 语法校验失败: $file"
|
|
30
|
+
sed 's/^/ /' /tmp/.precommit-node-check.$$ || true
|
|
31
|
+
SYNTAX_FAIL=1
|
|
32
|
+
fi
|
|
33
|
+
done < <(git diff --cached --name-only --diff-filter=ACM -- '*.mjs' '*.js' '*.cjs' 2>/dev/null || true)
|
|
34
|
+
rm -f /tmp/.precommit-node-check.$$
|
|
35
|
+
|
|
36
|
+
if [[ "$SYNTAX_FAIL" -ne 0 ]]; then
|
|
37
|
+
echo "👉 修复指引:先修正上述语法错误,再重新 git add 并提交。"
|
|
38
|
+
exit 1
|
|
39
|
+
fi
|
|
40
|
+
|
|
41
|
+
# ---------- L2: 反未接线增量对账(Orphan Check-Diff) ----------
|
|
42
|
+
ORPHAN_ENGINE=""
|
|
43
|
+
if [[ -f "units/orphan/bin/lab-orphan.mjs" ]]; then
|
|
44
|
+
ORPHAN_ENGINE="node units/orphan/bin/lab-orphan.mjs"
|
|
45
|
+
elif [[ -f "node_modules/reasonweave/units/orphan/bin/lab-orphan.mjs" ]]; then
|
|
46
|
+
ORPHAN_ENGINE="node node_modules/reasonweave/units/orphan/bin/lab-orphan.mjs"
|
|
47
|
+
elif [[ -f "node_modules/@churzexo/reasonweave/units/orphan/bin/lab-orphan.mjs" ]]; then
|
|
48
|
+
ORPHAN_ENGINE="node node_modules/@churzexo/reasonweave/units/orphan/bin/lab-orphan.mjs"
|
|
49
|
+
elif [[ -f "node_modules/lab-agent-toolkit/units/orphan/bin/lab-orphan.mjs" ]]; then
|
|
50
|
+
ORPHAN_ENGINE="node node_modules/lab-agent-toolkit/units/orphan/bin/lab-orphan.mjs"
|
|
51
|
+
elif command -v lab-orphan >/dev/null 2>&1; then
|
|
52
|
+
ORPHAN_ENGINE="lab-orphan"
|
|
53
|
+
fi
|
|
54
|
+
|
|
55
|
+
if [[ -n "$ORPHAN_ENGINE" ]]; then
|
|
56
|
+
$ORPHAN_ENGINE check-diff --staged
|
|
57
|
+
else
|
|
58
|
+
echo "ℹ️ [Anti-Orphan] 未检测到 lab-orphan 引擎,已跳过接线对账(仅做语法校验)。"
|
|
59
|
+
echo " 挂载引擎:npm i -g reasonweave 或 npm i -D reasonweave"
|
|
60
|
+
echo " 参考配方:\$(lab path orphan)/templates/wiring/"
|
|
61
|
+
fi
|
|
62
|
+
|
|
63
|
+
# ---------- L3: 垂直切片自检提示(新增导出即需接线证明) ----------
|
|
64
|
+
NEW_EXPORTS="$(git diff --cached -U0 -- '*.mjs' '*.js' '*.ts' 2>/dev/null \
|
|
65
|
+
| grep -E '^\+.*export[[:space:]]+(async[[:space:]]+)?(function|const|class|default)' || true)"
|
|
66
|
+
if [[ -n "$NEW_EXPORTS" ]]; then
|
|
67
|
+
echo "🧭 [Vertical Slicing] 本次提交新增了导出符号,请确认调用链已闭环:"
|
|
68
|
+
echo "$NEW_EXPORTS" | sed 's/^+//' | sed 's/^/ /' | head -10
|
|
69
|
+
echo " 闭环要求:实现 -> 注册/声明 -> 主入口可见(详见 ADR-0009 / playbook 19)"
|
|
70
|
+
fi
|
|
71
|
+
|
|
72
|
+
echo "✅ [Anti-Orphan] Node.js 接线门禁检查通过。"
|
|
73
|
+
exit 0
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# 新项目 `AGENTS.md` / `GEMINI.md` 反假接线接入规范与模板
|
|
2
|
+
|
|
3
|
+
> **单一事实源(SSOT)**:本模板源自 [units/orphan/README.md](../../README.md) 与 [19-anti-orphan-vertical-slicing.md](../../../playbook/docs/19-anti-orphan-vertical-slicing.md)(沉淀自 ADR-075 实战)。
|
|
4
|
+
> **创建时机与职责**:本映射**仅在已有真实业务代码的下游消费方仓**(或新项目已建好首个真实入口与唯一运行时后)创建,一般由**消费方 Agent** 探测本仓真实路径后负责写入;全局安装/升级 `lab-agent-toolkit` 本身或 0 代码空目录**不创建**本映射(严禁残留 `<...>` 占位符)。
|
|
5
|
+
> **核心铁律**:业务仓库**不得复制**长篇方法论或三层架构正文,只放极简本地路由映射,并保留 `$(lab path orphan)/README.md` 指针。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 一、行数预算与 SSOT 约束
|
|
10
|
+
|
|
11
|
+
| 目标文件 | 接入段落 | 严格行数预算 | 职责边界 |
|
|
12
|
+
| :--- | :--- | :---: | :--- |
|
|
13
|
+
| `AGENTS.md` | 反假接线分级自查段 | **`<= 12` 行** | Tier 1 / Tier 2 本地目录与核心文件映射 + 自主呈批开单纪律 + SSOT 指针 |
|
|
14
|
+
| `GEMINI.md` | 反假接线执行守卫段 | **`<= 6` 行** | 唯一 `.venv` 运行时执行 L1/L2 + 禁 `SKIP_*` / `--no-verify` 绕过 |
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 二、标准复制模板
|
|
19
|
+
|
|
20
|
+
### 模板 A:`AGENTS.md` 分级自查段(`<= 12` 行,当前 6 行)
|
|
21
|
+
|
|
22
|
+
将以下片段复制到业务仓根目录 `AGENTS.md`,并将 `<...>` 替换为本项目真实路径:
|
|
23
|
+
|
|
24
|
+
```markdown
|
|
25
|
+
## 反假接线分级自查(SSOT: `$(lab path orphan)/README.md`)
|
|
26
|
+
- **Tier 1(轻量专项)**:参数全链路透传与单分支可达(本地链路:`<schema_or_registry>` → `<dispatcher>` → `<factory_or_handler>`)。
|
|
27
|
+
- **Tier 2(全局自查)**:注册表双向差集(含 `Tool(name=...)` 反向幽灵拦截)、`lab orphan scan`、动态加载盲区隔离(`<dynamic_dir>`)与跨端 SSOT 对账(`<ssot_targets>`)。
|
|
28
|
+
- **L2/L3 契约与存证**:L2 在唯一 `<.venv>` 跑全量路由非空产出 + 失败传播(`0`/`None`/异常)+ `--verify-mutations`;涉及真机/外部设备产出须过 L3 非对称 Ed25519 验签与新鲜度硬门限。
|
|
29
|
+
- **自主呈批开单**:本任务新引入断链当场修完复测;发现越界/历史/跨端假接线,禁静默跳过或越界硬改,须附 `文件:行号` + L1/L2 复现证据,主动询问人类是否 `baton new` 开单整改。
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 模板 B:`GEMINI.md` 执行守卫段(`<= 6` 行,当前 4 行)
|
|
33
|
+
|
|
34
|
+
将以下片段复制到业务仓根目录 `GEMINI.md`,并将 `<...>` 替换为本项目真实命令与虚拟环境路径:
|
|
35
|
+
|
|
36
|
+
```markdown
|
|
37
|
+
## 反假接线执行守卫(SSOT: `$(lab path orphan)/README.md`)
|
|
38
|
+
- **唯一运行时执行 L1/L2**:所有 L1 拓扑门禁与 L2 契约测试(含 `--verify-mutations`)一律使用项目唯一 `<.venv/bin/python>`(或唯一运行时)执行,严禁裂变环境。
|
|
39
|
+
- **严禁绕过**:提交前经 `preflight.conf` `RECONCILE_CMD` 与 `.githooks/pre-commit.d/`(含 OpenGrep SAST)联检;严禁使用 `SKIP_*` 环境变量或 `--no-verify` 绕过门禁,阻断即当场自愈。
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 模板 C:`preflight.conf` L1 + L2 串联参考片段
|
|
43
|
+
|
|
44
|
+
在业务仓根目录 `preflight.conf` 中配置 `RECONCILE_CMD`,确保本地 `preflight.sh` 与 CI 共同执行 L1 + L2 闭环:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# L1(静态拓扑零差集)+ L2(唯一 .venv 运行时失败传播与变异自证)
|
|
48
|
+
RECONCILE_CMD="<.venv/bin/python> scripts/audit_wiring.py --fast && <.venv/bin/python> tests/test_l2_failure_propagation_contract.py --verify-mutations"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## 三、4 项初始化验收清单(Initialization Acceptance Checklist)
|
|
54
|
+
|
|
55
|
+
新项目接入完成后,Agent 与人类须逐项核验以下 4 项硬指标(全绿方可视为初始化完成):
|
|
56
|
+
|
|
57
|
+
- [ ] **1. 行数预算达标**:`AGENTS.md` 分级自查段严格 `<= 12` 行、`GEMINI.md` 执行守卫段严格 `<= 6` 行;正文无方法论长篇复制,且保留 `$(lab path orphan)/README.md` 指针。
|
|
58
|
+
- [ ] **2. 路径真实性核验**:模板中填写的本地注册表、分发器、工厂、动态加载目录及唯一 `.venv` 路径在当前仓库**真实存在且可执行**(无占位符残留或悬空路径)。
|
|
59
|
+
- [ ] **3. `preflight.conf` L1+L2 串联**:`preflight.conf` 的 `RECONCILE_CMD` 已串联 L1(`--fast`)与 L2(含 `--verify-mutations`),运行 `bash scripts/preflight.sh` A5 对账全绿,且 CI 工作流已同步接入。
|
|
60
|
+
- [ ] **4. `pre-commit` SAST 挂载**:`.githooks/pre-commit.d/` 已挂载 OpenGrep SAST(如 `10-review-gate`)守卫反射与动态执行(`eval`/`exec`/`getattr`/`importlib`),且 L1/L2 门禁无任何 `SKIP_*` 绕过后门。
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Python 栈反未接线(Anti-Orphan)对等性测试模板
|
|
3
|
+
适用场景:AI Agent 框架、Planner-Executor 架构、CLI 原语系统、API 路由系统
|
|
4
|
+
设计原则:
|
|
5
|
+
1. 单一事实源(SSOT):通过装饰器反射生成元数据,避免维护多份 Schema。
|
|
6
|
+
2. 纯内存集合比对:执行时间 < 50ms,非常适合挂入本地 pytest 必跑项(Fast Gate)。
|
|
7
|
+
3. 确定性报错提示:断言失败时清晰输出修复指引,赋能 Agent 一键自愈。
|
|
8
|
+
4. 双运行器兼容:同时兼容 pytest 与 Python 原生 unittest。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import inspect
|
|
12
|
+
import unittest
|
|
13
|
+
from typing import Callable, Dict, Any, Set
|
|
14
|
+
|
|
15
|
+
# ==============================================================================
|
|
16
|
+
# 1. 业务框架模拟(请根据业务项目的真实注册机制进行替换)
|
|
17
|
+
# ==============================================================================
|
|
18
|
+
|
|
19
|
+
class PrimitiveRegistry:
|
|
20
|
+
"""单一事实源:底层原语注册中心"""
|
|
21
|
+
_primitives: Dict[str, Callable] = {}
|
|
22
|
+
|
|
23
|
+
@classmethod
|
|
24
|
+
def register(cls, name: str | None = None):
|
|
25
|
+
"""装饰器:标记函数为可用原语"""
|
|
26
|
+
def decorator(func: Callable):
|
|
27
|
+
fn_name = name or func.__name__
|
|
28
|
+
cls._primitives[fn_name] = func
|
|
29
|
+
return func
|
|
30
|
+
return decorator
|
|
31
|
+
|
|
32
|
+
@classmethod
|
|
33
|
+
def list_all(cls) -> Dict[str, Callable]:
|
|
34
|
+
return dict(cls._primitives)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class PlannerActionSpace:
|
|
38
|
+
"""上层规划器:LLM 或系统可调用的动作空间"""
|
|
39
|
+
_actions: Set[str] = set()
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def get_available_actions(cls) -> Set[str]:
|
|
43
|
+
# 实际项目中应返回向 LLM Prompt/Schema 实际暴露的动作空间
|
|
44
|
+
return set(cls._actions)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# ==============================================================================
|
|
48
|
+
# 2. 对等性测试与断言(兼容 pytest 与 unittest)
|
|
49
|
+
# ==============================================================================
|
|
50
|
+
|
|
51
|
+
class TestWiringParity(unittest.TestCase):
|
|
52
|
+
"""接线对等性与防孤儿断言套件"""
|
|
53
|
+
|
|
54
|
+
def test_planner_executor_wiring_parity(self):
|
|
55
|
+
"""
|
|
56
|
+
【对等断言】:确保所有通过 @register 声明的底层原语,
|
|
57
|
+
均已被上层 Planner 动作空间识别并暴露给调度系统。
|
|
58
|
+
"""
|
|
59
|
+
implemented = set(PrimitiveRegistry.list_all().keys())
|
|
60
|
+
planner_visible = PlannerActionSpace.get_available_actions()
|
|
61
|
+
|
|
62
|
+
# 1. 检查是否存在“已实现但未接线”的孤儿原语
|
|
63
|
+
orphaned_primitives = implemented - planner_visible
|
|
64
|
+
self.assertFalse(
|
|
65
|
+
orphaned_primitives,
|
|
66
|
+
f"❌ 发现未接线底层原语(已实现但上层 Planner 不可见):{sorted(orphaned_primitives)}\n"
|
|
67
|
+
f"👉 修复指引:请在上层规划器的注册列表或路由表中引入并注册该原语。"
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# 2. 检查是否存在“已声明但未实现”的虚幻原语
|
|
71
|
+
hallucinated_actions = planner_visible - implemented
|
|
72
|
+
self.assertFalse(
|
|
73
|
+
hallucinated_actions,
|
|
74
|
+
f"❌ 发现幻觉原语(上层 Planner 已暴露但底层未找到实现):{sorted(hallucinated_actions)}\n"
|
|
75
|
+
f"👉 修复指引:请在执行器层挂载 @register 补齐底层实现。"
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
def test_primitive_metadata_completeness(self):
|
|
79
|
+
"""
|
|
80
|
+
【语义完整性断言】:防止 AI 虽然接入了原语,但漏写了 Docstring 或类型注解,
|
|
81
|
+
导致 LLM 无法理解或无法正确提取 JSON Schema。
|
|
82
|
+
"""
|
|
83
|
+
for name, func in PrimitiveRegistry.list_all().items():
|
|
84
|
+
doc = inspect.getdoc(func)
|
|
85
|
+
self.assertTrue(
|
|
86
|
+
doc and len(doc.strip()) > 10,
|
|
87
|
+
f"❌ 原语 '{name}' 缺失有效的 Docstring 描述。\n"
|
|
88
|
+
f"👉 修复指引:为函数补充功能说明、输入参数与返回值说明,以便自动生成 Tool Schema。"
|
|
89
|
+
)
|
|
90
|
+
sig = inspect.signature(func)
|
|
91
|
+
self.assertIsNot(
|
|
92
|
+
sig.return_annotation,
|
|
93
|
+
inspect.Signature.empty,
|
|
94
|
+
f"❌ 原语 '{name}' 缺失返回类型注解。\n"
|
|
95
|
+
f"👉 修复指引:请显式声明返回值类型(如 `-> str`、`-> bool` 或 Pydantic 模型)。"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
if __name__ == '__main__':
|
|
100
|
+
unittest.main()
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Rust 栈反未接线(Anti-Orphan)最佳实践指南
|
|
2
|
+
|
|
3
|
+
> **适用场景**:高可靠核心引擎、状态机、协议分发、CLI 动作原语。
|
|
4
|
+
> **核心哲学**:将“未接线”缺陷转化为编译器无法通过的硬错误(Compile-time Errors)。
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 1. 机制 A:穷尽模式匹配(Exhaustive Enum Matching)
|
|
9
|
+
|
|
10
|
+
严禁在核心调度器、命令分发器或状态转移函数中使用 `_ => {}` 通配符!
|
|
11
|
+
|
|
12
|
+
### 反模式(容易发生静默未接线):
|
|
13
|
+
```rust
|
|
14
|
+
// ❌ 错误示范:通配符吞掉了新变体,导致新增原语未实际接线
|
|
15
|
+
match action {
|
|
16
|
+
ActionPrimitive::ReadFile(path) => handle_read(path),
|
|
17
|
+
ActionPrimitive::WriteFile(path, content) => handle_write(path, content),
|
|
18
|
+
_ => eprintln!("Unhandled or no-op"), // AI 新增变体后,这里静默忽略,编译完全通过!
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### 正确实践(穷尽匹配):
|
|
23
|
+
```rust
|
|
24
|
+
// ✅ 正确示范:强制显式穷尽所有变体
|
|
25
|
+
match action {
|
|
26
|
+
ActionPrimitive::ReadFile(path) => handle_read(path),
|
|
27
|
+
ActionPrimitive::WriteFile(path, content) => handle_write(path, content),
|
|
28
|
+
ActionPrimitive::DeleteFile(path) => handle_delete(path),
|
|
29
|
+
// 严禁写 _ => {} 分支!
|
|
30
|
+
// 只要在 ActionPrimitive 枚举中新增任何字段,此处立即报编译错误(E0004),
|
|
31
|
+
// 强迫 Agent 在同一次提交中把调度逻辑闭环。
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 2. 机制 B:编译级死代码熔断
|
|
38
|
+
|
|
39
|
+
在核心模块(`src/lib.rs` 或 `src/main.rs`)顶部明确声明门禁属性:
|
|
40
|
+
|
|
41
|
+
```rust
|
|
42
|
+
#![deny(dead_code)]
|
|
43
|
+
#![deny(unused_imports)]
|
|
44
|
+
#![deny(unused_variables)]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 避坑指南:
|
|
48
|
+
* **公共库(Library Crate)陷阱**:在 `lib.rs` 中使用 `pub fn` 或 `pub struct` 时,编译器默认认为它是对外 API,不会触发 `dead_code` 警告。
|
|
49
|
+
* **对策**:内部核心调度原语应放在私有模块或 internal crate 中,或者通过编写单元测试/集成测试来强制断言每个变体的覆盖。
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## 3. 机制 C:门禁分级落地建议
|
|
54
|
+
|
|
55
|
+
1. **Fast Gate(本地 Git Pre-commit)**:
|
|
56
|
+
```bash
|
|
57
|
+
# 仅检查语法与类型,耗时极短 (< 1.5s)
|
|
58
|
+
cargo check --tests
|
|
59
|
+
```
|
|
60
|
+
2. **Heavy Gate(Pre-push 或 CI)**:
|
|
61
|
+
```bash
|
|
62
|
+
# 深度静态扫描与全警告阻断
|
|
63
|
+
cargo clippy --all-targets -- -D warnings
|
|
64
|
+
cargo test
|
|
65
|
+
```
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# SwiftUI / iOS 栈反未接线(Anti-Orphan)实践指南
|
|
2
|
+
|
|
3
|
+
> **适用场景**:SwiftUI 客户端、iOS 原生应用架构。
|
|
4
|
+
> **痛点定位**:AI 极易生成漂亮的独立 `View` 或 `ViewModel`,但遗漏挂载到主 `NavigationStack`、`TabView` 或触发按钮上。
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 1. 核心工具选型:Periphery
|
|
9
|
+
|
|
10
|
+
[Periphery](https://github.com/peripheryapp/periphery) 是 Swift 生态中用于精确探测未引用代码(Unused Code / Dead Code)的工业级工具。它基于 Swift 编译器生成的真实索引图谱(IndexStore),而非粗糙的正规匹配。
|
|
11
|
+
|
|
12
|
+
### 常用命令:
|
|
13
|
+
```bash
|
|
14
|
+
# 全局快速扫描(依赖已构建好的 Index)
|
|
15
|
+
periphery scan --quiet
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## 2. 门禁分级落地原则(极其重要)
|
|
21
|
+
|
|
22
|
+
> ⚠️ **警告**:**严禁将 `periphery scan` 挂入本地 `pre-commit`!**
|
|
23
|
+
>
|
|
24
|
+
> Periphery 依赖 Xcode 编译生成的 IndexStore。在本地每次 `git commit` 时调用 Periphery 会消耗 15s 至 2 分钟不等,这会严重破坏开发者和 Agent 的本地心流,必然引发滥用 `--no-verify` 的反弹。
|
|
25
|
+
|
|
26
|
+
### 推荐分级架构:
|
|
27
|
+
|
|
28
|
+
1. **Fast Gate(本地 Pre-commit)**:
|
|
29
|
+
* 开启 Xcode 项目编译选项:`SWIFT_TREAT_WARNINGS_AS_ERRORS = YES`。
|
|
30
|
+
* 本地仅做快速 SwiftLint 语法格式与基础静态校验(耗时 < 1.5s)。
|
|
31
|
+
|
|
32
|
+
2. **Heavy Gate(Pre-push 钩子 或 GitHub Actions CI)**:
|
|
33
|
+
* 在代码推送到远端或提 PR 时执行 Periphery:
|
|
34
|
+
```yaml
|
|
35
|
+
# .github/workflows/periphery.yml
|
|
36
|
+
name: Periphery Unused Code Scan
|
|
37
|
+
on: [pull_request]
|
|
38
|
+
jobs:
|
|
39
|
+
scan:
|
|
40
|
+
runs-on: macos-latest
|
|
41
|
+
steps:
|
|
42
|
+
- uses: actions/checkout@v4
|
|
43
|
+
- name: Install Periphery
|
|
44
|
+
run: brew install peripheryapp/periphery/periphery
|
|
45
|
+
- name: Run Scan
|
|
46
|
+
run: periphery scan --strict
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## 3. SwiftUI 动态路由避坑指南
|
|
52
|
+
|
|
53
|
+
SwiftUI 采用强类型路由机制(如 `.navigationDestination(for: Route.self)`)时,由于是运行时类型分发,静态分析有时难以确认枚举变体是否全量被导航树消费。
|
|
54
|
+
|
|
55
|
+
**推荐配合穷尽枚举与状态机**:
|
|
56
|
+
与 Rust 类似,在 iOS 客户端中定义统一的 `AppRoute` 枚举,并在导航调度器中严禁使用 `default:` 分支,利用 Swift 编译器的穷尽检查(Exhaustive Switch)从源头上保证所有路由均有接线。
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit Tests for units/orphan Phase 2 Multi-stack Adapters (Python & Web/TS)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import test from 'node:test';
|
|
6
|
+
import assert from 'node:assert/strict';
|
|
7
|
+
import { Solver } from '../core/solver.mjs';
|
|
8
|
+
import { PythonAdapter } from '../adapters/python/index.mjs';
|
|
9
|
+
import { WebAdapter } from '../adapters/web/index.mjs';
|
|
10
|
+
import { SolverProtocol } from '../core/protocol-s1-s3.mjs';
|
|
11
|
+
|
|
12
|
+
// Mock Reader for isolated in-memory testing
|
|
13
|
+
function createMockReader(blobsMap) {
|
|
14
|
+
const blobs = Object.entries(blobsMap).map(([p, text]) => ({
|
|
15
|
+
path: p,
|
|
16
|
+
mode: '100644',
|
|
17
|
+
type: 'blob',
|
|
18
|
+
oid: 'mock-oid-' + p,
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
getHeadCommit: () => 'mock-commit-sha-40chars-hexadecimal1234567890',
|
|
23
|
+
listBlobs: (fn) => (fn ? blobs.filter(b => fn(b.path)) : blobs),
|
|
24
|
+
readBlobText: (p) => blobsMap[p] || '',
|
|
25
|
+
loadAllTextBlobs: (fn) => {
|
|
26
|
+
const map = new Map();
|
|
27
|
+
for (const [p, text] of Object.entries(blobsMap)) {
|
|
28
|
+
if (!fn || fn(p)) map.set(p, text);
|
|
29
|
+
}
|
|
30
|
+
return map;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test('PythonAdapter: __all__ export protection', () => {
|
|
36
|
+
const adapter = new PythonAdapter();
|
|
37
|
+
const mockReader = createMockReader({
|
|
38
|
+
'src/module.py': `
|
|
39
|
+
__all__ = ['exported_api', 'PublicClass']
|
|
40
|
+
|
|
41
|
+
def exported_api():
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
class PublicClass:
|
|
45
|
+
pass
|
|
46
|
+
|
|
47
|
+
def unexported_dead_func():
|
|
48
|
+
pass
|
|
49
|
+
`,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const candidates = adapter.extractCandidates(mockReader);
|
|
53
|
+
const exported = candidates.find(c => c.name === 'exported_api');
|
|
54
|
+
const pubClass = candidates.find(c => c.name === 'PublicClass');
|
|
55
|
+
const unexported = candidates.find(c => c.name === 'unexported_dead_func');
|
|
56
|
+
|
|
57
|
+
assert.ok(exported, 'exported_api should be extracted');
|
|
58
|
+
assert.equal(exported.isProtected, true, 'exported_api should be protected via __all__');
|
|
59
|
+
assert.ok(pubClass, 'PublicClass should be extracted');
|
|
60
|
+
assert.equal(pubClass.isProtected, true, 'PublicClass should be protected via __all__');
|
|
61
|
+
|
|
62
|
+
assert.ok(unexported, 'unexported_dead_func should be extracted');
|
|
63
|
+
assert.equal(unexported.isProtected, false, 'unexported_dead_func should not be protected');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('PythonAdapter: pytest functions, magic methods and route decorators protection', () => {
|
|
67
|
+
const adapter = new PythonAdapter();
|
|
68
|
+
const mockReader = createMockReader({
|
|
69
|
+
'tests/test_api.py': `
|
|
70
|
+
def test_user_login():
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
class TestUserModel:
|
|
74
|
+
pass
|
|
75
|
+
|
|
76
|
+
def helper_test_utility():
|
|
77
|
+
pass
|
|
78
|
+
`,
|
|
79
|
+
'src/app.py': `
|
|
80
|
+
@app.get('/health')
|
|
81
|
+
def health_check():
|
|
82
|
+
return {'status': 'ok'}
|
|
83
|
+
|
|
84
|
+
class Item:
|
|
85
|
+
def __init__(self):
|
|
86
|
+
pass
|
|
87
|
+
|
|
88
|
+
def __repr__(self):
|
|
89
|
+
return 'Item'
|
|
90
|
+
`,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const candidates = adapter.extractCandidates(mockReader);
|
|
94
|
+
|
|
95
|
+
const testFunc = candidates.find(c => c.name === 'test_user_login');
|
|
96
|
+
assert.ok(testFunc);
|
|
97
|
+
assert.equal(testFunc.isProtected, true, 'test_* functions should be protected');
|
|
98
|
+
|
|
99
|
+
const testClass = candidates.find(c => c.name === 'TestUserModel');
|
|
100
|
+
assert.ok(testClass);
|
|
101
|
+
assert.equal(testClass.isProtected, true, 'Test* classes should be protected');
|
|
102
|
+
|
|
103
|
+
const routeHandler = candidates.find(c => c.name === 'health_check');
|
|
104
|
+
assert.ok(routeHandler);
|
|
105
|
+
assert.equal(routeHandler.isProtected, true, '@app.get route handler should be protected');
|
|
106
|
+
|
|
107
|
+
const magicInit = candidates.find(c => c.name === '__init__');
|
|
108
|
+
if (magicInit) {
|
|
109
|
+
assert.equal(magicInit.isProtected, true, '__init__ magic method should be protected');
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('PythonAdapter: getattr/importlib dynamic reflection Fail-Closed downgrade', () => {
|
|
114
|
+
const adapter = new PythonAdapter();
|
|
115
|
+
const mockReaderWithReflection = createMockReader({
|
|
116
|
+
'src/dispatcher.py': `
|
|
117
|
+
import importlib
|
|
118
|
+
|
|
119
|
+
def dispatch(handler_name):
|
|
120
|
+
func = getattr(handlers, handler_name)
|
|
121
|
+
return func()
|
|
122
|
+
`,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
assert.equal(adapter.detectReflectionUsage(mockReaderWithReflection), true, 'Should detect getattr dynamic reflection');
|
|
126
|
+
|
|
127
|
+
const mockReaderClean = createMockReader({
|
|
128
|
+
'src/static_service.py': `
|
|
129
|
+
def direct_calc():
|
|
130
|
+
return 42
|
|
131
|
+
`,
|
|
132
|
+
});
|
|
133
|
+
assert.equal(adapter.detectReflectionUsage(mockReaderClean), false, 'Clean code should not flag reflection');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('WebAdapter: package.json entrypoints protection', () => {
|
|
137
|
+
const adapter = new WebAdapter();
|
|
138
|
+
const mockReader = createMockReader({
|
|
139
|
+
'package.json': JSON.stringify({
|
|
140
|
+
name: 'my-lib',
|
|
141
|
+
main: './dist/index.js',
|
|
142
|
+
module: './src/index.ts',
|
|
143
|
+
bin: {
|
|
144
|
+
'my-cli': './bin/cli.js',
|
|
145
|
+
},
|
|
146
|
+
exports: {
|
|
147
|
+
'.': './src/index.ts',
|
|
148
|
+
'./utils': './src/utils/index.ts',
|
|
149
|
+
},
|
|
150
|
+
}),
|
|
151
|
+
'src/index.ts': `
|
|
152
|
+
export function publicApi() {}
|
|
153
|
+
export const VERSION = '1.0.0';
|
|
154
|
+
`,
|
|
155
|
+
'bin/cli.js': `
|
|
156
|
+
export function runCli() {}
|
|
157
|
+
`,
|
|
158
|
+
'src/internal.ts': `
|
|
159
|
+
export function internalHelper() {}
|
|
160
|
+
export function unusedFunction() {}
|
|
161
|
+
`,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const candidates = adapter.extractCandidates(mockReader);
|
|
165
|
+
|
|
166
|
+
const publicApi = candidates.find(c => c.name === 'publicApi');
|
|
167
|
+
assert.ok(publicApi, 'publicApi should be extracted');
|
|
168
|
+
assert.equal(publicApi.isProtected, true, 'publicApi in src/index.ts should be protected by package.json exports');
|
|
169
|
+
|
|
170
|
+
const runCli = candidates.find(c => c.name === 'runCli');
|
|
171
|
+
assert.ok(runCli, 'runCli should be extracted');
|
|
172
|
+
assert.equal(runCli.isProtected, true, 'runCli in bin/cli.js should be protected by package.json bin');
|
|
173
|
+
|
|
174
|
+
const unusedFunc = candidates.find(c => c.name === 'unusedFunction');
|
|
175
|
+
assert.ok(unusedFunc, 'unusedFunction should be extracted');
|
|
176
|
+
assert.equal(unusedFunc.isProtected, false, 'Internal export should not be protected by default');
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('WebAdapter: unreferenced internal export detected as CONFIRMED_ORPHAN', () => {
|
|
180
|
+
const adapter = new WebAdapter();
|
|
181
|
+
const blobsMap = {
|
|
182
|
+
'package.json': JSON.stringify({
|
|
183
|
+
name: 'app',
|
|
184
|
+
main: './src/index.ts',
|
|
185
|
+
}),
|
|
186
|
+
'src/index.ts': `
|
|
187
|
+
import { usedHelper } from './internal.ts';
|
|
188
|
+
export function main() {
|
|
189
|
+
usedHelper();
|
|
190
|
+
}
|
|
191
|
+
`,
|
|
192
|
+
'src/internal.ts': `
|
|
193
|
+
export function usedHelper() { return 1; }
|
|
194
|
+
export function unreferencedDeadExport() { return 2; }
|
|
195
|
+
`,
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const solver = new Solver({
|
|
199
|
+
repoDir: process.cwd(),
|
|
200
|
+
ref: 'HEAD',
|
|
201
|
+
adapters: [adapter],
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// Inject mock blobs into solver
|
|
205
|
+
solver.reader = createMockReader(blobsMap);
|
|
206
|
+
|
|
207
|
+
const result = solver.solve({ includeAlive: false });
|
|
208
|
+
const deadExport = result.candidates.find(c => c.name === 'unreferencedDeadExport');
|
|
209
|
+
assert.ok(deadExport, 'unreferencedDeadExport should be in orphan candidates');
|
|
210
|
+
assert.equal(deadExport.status, 'CONFIRMED_ORPHAN', 'Unreferenced internal export should be CONFIRMED_ORPHAN');
|
|
211
|
+
|
|
212
|
+
const usedHelper = result.candidates.find(c => c.name === 'usedHelper');
|
|
213
|
+
assert.equal(usedHelper, undefined, 'usedHelper is imported in index.ts and should not be an orphan');
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('WebAdapter: dynamic import() expression Fail-Closed downgrade', () => {
|
|
217
|
+
const adapter = new WebAdapter();
|
|
218
|
+
const mockReaderWithDynamicImport = createMockReader({
|
|
219
|
+
'src/loader.ts': `
|
|
220
|
+
async function loadPlugin(pluginName: string) {
|
|
221
|
+
const mod = await import(\`./plugins/\${pluginName}\`);
|
|
222
|
+
return mod;
|
|
223
|
+
}
|
|
224
|
+
`,
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
assert.equal(adapter.detectReflectionUsage(mockReaderWithDynamicImport), true, 'Dynamic import expression should trigger reflection flag');
|
|
228
|
+
|
|
229
|
+
const mockReaderStatic = createMockReader({
|
|
230
|
+
'src/static.ts': `
|
|
231
|
+
import { a } from './a';
|
|
232
|
+
`,
|
|
233
|
+
});
|
|
234
|
+
assert.equal(adapter.detectReflectionUsage(mockReaderStatic), false, 'Static import should not trigger reflection');
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test('SolverProtocol S1-S3 suite remains 100% green', async () => {
|
|
238
|
+
const selfTest = await SolverProtocol.runSelfTest({ verbose: false });
|
|
239
|
+
assert.equal(selfTest.passed, true, 'S1-S3 protocol should pass cleanly');
|
|
240
|
+
assert.equal(selfTest.s1.passed, true, 'S1 injection test must pass');
|
|
241
|
+
assert.equal(selfTest.s2.passed, true, 'S2 in-use protection test must pass');
|
|
242
|
+
assert.equal(selfTest.s3.passed, true, 'S3 byte-level reproducibility test must pass');
|
|
243
|
+
});
|