Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ffd70d6fa5 | |||
| 612b3a3382 | |||
| f1c422af49 | |||
| 0ff2053ae0 | |||
| d75c8922aa | |||
| e1592cc1df | |||
| 79493879ae | |||
| 63686fa5b2 | |||
| c14fb72e84 | |||
| 5520534424 | |||
| fc3c85bb6e | |||
| cebd6bcebb | |||
| 3ce73a68ff | |||
| d277d4bdfc | |||
| 2a3b5b4da5 | |||
| 25e184e52d | |||
| 15a60c6ae1 | |||
| 6973363c37 | |||
| 1a84864e44 | |||
| a3002bbe3b | |||
| 430396dfba | |||
| d4c6145b6d | |||
| 27c73fb050 | |||
| 40d4443926 | |||
| 32b0bd6c89 |
+83
-32
@@ -66,26 +66,6 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "Edit|Write|MultiEdit|NotebookEdit|Bash|Task|Agent",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-chain-recommendation.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "Edit|Write|MultiEdit|NotebookEdit|Bash|Task|Agent",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-override-limit.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "Edit|Write|MultiEdit",
|
||||
"hooks": [
|
||||
@@ -121,8 +101,78 @@
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-semgrep-security.mjs",
|
||||
"timeout": 10
|
||||
"command": "node tools/enforce-router-gate.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "PowerShell",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-powershell-gate.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "Edit|Write|MultiEdit",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-normative-content-rules.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "Edit|Write",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-tdd-real-test-verifier.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "Edit|Write|MultiEdit|Bash",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-self-debrief-detector.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "AskUserQuestion",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/askuser-cosmetic-detector.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "mcp__.*",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-mcp-classification.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "Read",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-read-path-deny.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -170,6 +220,16 @@
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "Task",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-subagent-return-scanner.mjs",
|
||||
"timeout": 10
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Stop": [
|
||||
@@ -204,16 +264,7 @@
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-classifier-match.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node tools/enforce-graph-first.mjs",
|
||||
"command": "node tools/enforce-todowrite-skill-verifier.mjs",
|
||||
"timeout": 5
|
||||
}
|
||||
]
|
||||
|
||||
@@ -21,8 +21,8 @@ Aggregator over observer evidence. Reads JSONL + optional MD notes, surfaces can
|
||||
|
||||
## Procedure
|
||||
|
||||
> **MANDATORY DIGITAL ANALYSIS (added 2026-05-26 after retro #6 feedback; extended to 11 tables 2026-05-28).**
|
||||
> Каждый прогон /brain-retro ОБЯЗАН включать **количественные срезы**, не только causal narrative. Минимум 11 цифровых таблиц:
|
||||
> **MANDATORY DIGITAL ANALYSIS (added 2026-05-26 after retro #6 feedback; extended to 11 tables 2026-05-28; extended to 13 tables 2026-05-30 in Stream H Task 8).**
|
||||
> Каждый прогон /brain-retro ОБЯЗАН включать **количественные срезы**, не только causal narrative. Минимум 13 цифровых таблиц:
|
||||
>
|
||||
> 1. **Path-type breakdown** (regulated vs improvised, со счётчиками и %).
|
||||
> 2. **node_chosen distribution** (топ-15 узлов с count + %).
|
||||
@@ -35,8 +35,10 @@ Aggregator over observer evidence. Reads JSONL + optional MD notes, surfaces can
|
||||
> 9. **Router vs Opus** — три секции: A (роутер дал → Opus оценил, расхождение видно сразу), B (роутер молчал → Opus сказал «надо был скил»), C (роутер дал → Opus согласился что скил излишен). Источник — `result.routerVsOpus`.
|
||||
> 10. **Chain-ignore breakdown** — отдельный срез: сколько раз роутер рекомендовал цепочку vs одиночный узел, какой % я игнорировал, и rework-rate каждого; bucket по длине цепочки (1/2/3+). Источник — `result.chainIgnoreBreakdown`.
|
||||
> 11. **Chain-hook effectiveness** — парсит `~/.claude/runtime/hook-outcomes.jsonl` за период retro. Buckets: blocked / passed-with-skill / passed-inline-override / passed-global-override / passed-short-chain / passed-no-mutating. Источник — `result.chainHookEffectiveness` из analyzer. Источник правила — brain-retro #9 Candidate 2.
|
||||
> 12. **Router-gate hook effectiveness (per-rule)** — счётчики fires + blocks по каждому `hook_fired.rule` в эпизодах за период (path-deny / git-conditional / branch-switch / etc). Помогает увидеть, какие правила реально стреляли и какой % fires заканчивался блокировкой. Источник — `result.routerGateHookEffectiveness` (Stream H Task 8). Без таблицы — нет видимости качества защит router-gate v4.
|
||||
> 13. **Self-fabrication signals** — эпизоды, где `controller_claim` непустой (контроллер заявил действие) но `tool_uses` пуст или отсутствует (записи о реальном tool-call нет). 7 канонических паттернов фабрикации задокументированы в `docs/superpowers/runbooks/recovery-procedures.md` §5. Источник — `result.selfFabricationSignals` (Stream H Task 8).
|
||||
>
|
||||
> Без этих 11 таблиц retro считается недоделанным. Narrative-выводы должны опираться на цифры из них, не на «общие ощущения». **Если classifier_output=NULL > 30% эпизодов** — это сигнал, что классификатор сломан; в retro отдельным блоком отчитаться о состоянии классификатора (timeouts/errors/source distribution).
|
||||
> Без этих 13 таблиц retro считается недоделанным. Narrative-выводы должны опираться на цифры из них, не на «общие ощущения». **Если classifier_output=NULL > 30% эпизодов** — это сигнал, что классификатор сломан; в retro отдельным блоком отчитаться о состоянии классификатора (timeouts/errors/source distribution).
|
||||
>
|
||||
> Запрет на жаргон для блока «Report to user»: цифры остаются техническими, словесные выводы пользователю — простым языком (см. memory `feedback_plain_language.md`).
|
||||
|
||||
|
||||
@@ -28,6 +28,12 @@ exclude = [
|
||||
# Шаблонные плейсхолдеры
|
||||
"^\\{\\{.*\\}\\}$",
|
||||
"^\\[.*\\]$",
|
||||
# v3.9 hooks удалены Stream G (2026-05-30), CLAUDE.md содержит исторические упоминания
|
||||
"tools/enforce-chain-recommendation\\.mjs",
|
||||
"tools/enforce-classifier-match\\.mjs",
|
||||
"tools/enforce-graph-first\\.mjs",
|
||||
"tools/enforce-semgrep-security\\.mjs",
|
||||
"tools/enforce-override-limit\\.mjs",
|
||||
# localhost и приватные адреса
|
||||
"^https?://localhost",
|
||||
"^https?://127\\.0\\.0\\.1",
|
||||
|
||||
@@ -1972,3 +1972,5 @@ monitorится
|
||||
мониторьте
|
||||
промтами
|
||||
guillemets
|
||||
mirror'ящий
|
||||
plan'овский
|
||||
|
||||
+19
-19
@@ -1,6 +1,6 @@
|
||||
# Brain Status (auto-generated)
|
||||
|
||||
Last updated: 2026-05-29T15:20:30.351Z
|
||||
Last updated: 2026-05-30T03:11:28.244Z
|
||||
|
||||
| Контролёр | Состояние | Детали |
|
||||
|---|---|---|
|
||||
@@ -8,14 +8,14 @@ Last updated: 2026-05-29T15:20:30.351Z
|
||||
| C2 Cross-ref consistency | ✅ | [cross-ref-checker] OK — 0 drift in 4 files |
|
||||
| C3 Observer-of-observer | ✅ | [observer-of-observer] OK — last read 0 week(s) ago |
|
||||
| C4 Сигнальный статус | ✅ | This file (self-reference) |
|
||||
| C5 Observer-coverage | ⚠️ | 651 episode(s) this month · Stop-hook + post-commit OK · 20 missed activation(s) — see /brain-retro |
|
||||
| C5 Observer-coverage | ⚠️ | 639 episode(s) this month · Stop-hook + post-commit OK · 20 missed activation(s) — see /brain-retro |
|
||||
| C6 Chain map sync | ✅ | [chain-map-checker] OK — 16 chains in sync |
|
||||
|
||||
## Метрики (информационные, не алерты)
|
||||
|
||||
- Observer evidence: 651 episodes this month, 0 observer_error markers, 144 PII matches before filter
|
||||
- Legacy v1 episodes (not in factor analysis): 512
|
||||
- Last /brain-retro: 2 day(s) ago
|
||||
- Observer evidence: 639 episodes this month, 0 observer_error markers, 129 PII matches before filter
|
||||
- Legacy v1 episodes (not in factor analysis): 500
|
||||
- Last /brain-retro: 3 day(s) ago
|
||||
- Использование узлов: см. `/brain-retro` (раз в спринт). missed_activations: 20. **Неиспользованные узлы — не алерт, если профильной задачи не было** (Pravila §16.4 v1.36; capability-readiness; см. memory `feedback_brain_unused_tools_not_problem` — outside-repo memory store).
|
||||
|
||||
## Метрики дисциплины
|
||||
@@ -24,16 +24,16 @@ Baseline дисциплины роутера (этап 2 router discipline overh
|
||||
|
||||
| Тип задачи | Эпизодов | % с триггер-матчем | % через скил |
|
||||
|---|---|---|---|
|
||||
| analysis | 29 | 31.0% | 13.8% |
|
||||
| bugfix | 20 | 25.0% | 25.0% |
|
||||
| planning | 18 | 16.7% | 16.7% |
|
||||
| feature | 17 | 11.8% | 0.0% |
|
||||
| analysis | 26 | 30.8% | 15.4% |
|
||||
| bugfix | 19 | 26.3% | 26.3% |
|
||||
| planning | 16 | 18.8% | 18.8% |
|
||||
| feature | 15 | 13.3% | 0.0% |
|
||||
| cleanup | 6 | 0.0% | 0.0% |
|
||||
| refactor | 1 | 0.0% | 0.0% |
|
||||
|
||||
Router step distribution: 1: 275, 2: 238, 3: 70, 5: 61
|
||||
Router step distribution: 1: 281, 2: 227, 3: 63, 5: 61
|
||||
|
||||
Boundaries applied (ADR / границы): 84 of 644 эпизодов (13.0%).
|
||||
Boundaries applied (ADR / границы): 72 of 632 эпизодов (11.4%).
|
||||
|
||||
## Активные многоэтапные проекты
|
||||
|
||||
@@ -51,10 +51,10 @@ Boundaries applied (ADR / границы): 84 of 644 эпизодов (13.0%).
|
||||
|
||||
| Компонент | Токены (in/out) | USD |
|
||||
|---|---|---|
|
||||
| Classifier (Sonnet 4.6) | 3629/44428 | $0.68 |
|
||||
| Classifier (Sonnet 4.6) | 3237/42293 | $0.64 |
|
||||
| Self-assessment (Sonnet 4.6) | 0/0 | $0.00 |
|
||||
| Reviewer (Opus 4.7 + fallback) | 0/0 | $0.00 |
|
||||
| **Итого** | | **$0.68** |
|
||||
| **Итого** | | **$0.64** |
|
||||
|
||||
## Аномалии классификатора
|
||||
|
||||
@@ -67,7 +67,7 @@ Episodes since last run: 542 / threshold: 10
|
||||
|
||||
## Reviewer: субагент vs fallback
|
||||
|
||||
0 эпизодов проверено из 651.
|
||||
0 эпизодов проверено из 639.
|
||||
|
||||
## Reviewer findings
|
||||
|
||||
@@ -109,11 +109,11 @@ Episodes since last run: 542 / threshold: 10
|
||||
|
||||
| Фраза | За всё время | За сегодня |
|
||||
|---|---|---|
|
||||
| `recovery` | 1451 | 554 ⚠️ |
|
||||
| `без скилов` | 407 | 229 ⚠️ |
|
||||
| `ремонт инфраструктуры` | 331 | 146 ⚠️ |
|
||||
| `срочно` | 225 | 132 ⚠️ |
|
||||
| `memory dump` | 46 | 29 ⚠️ |
|
||||
| `recovery` | 2302 | 23 ⚠️ |
|
||||
| `без скилов` | 507 | 40 ⚠️ |
|
||||
| `ремонт инфраструктуры` | 331 | 0 |
|
||||
| `срочно` | 225 | 0 |
|
||||
| `memory dump` | 46 | 0 |
|
||||
| `direct ok` | 6 | 0 |
|
||||
| `быстрый коммит` | 3 | 0 |
|
||||
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
# Router-gate v4 Stream H — Completion Log
|
||||
|
||||
**Date:** 2026-05-30
|
||||
**Session:** 8f4ba767-f2fd-4b21-a0c0-fc049a552d25
|
||||
**Push:** `2a3b5b4d..d75c8922 main -> main`
|
||||
**Tests:** 1731/1731 baseline → 1776/1776 GREEN (+45)
|
||||
**Commits ahead of base:** 10
|
||||
|
||||
## What landed
|
||||
|
||||
| # | Task | Commit | Notes |
|
||||
|---|---|---|---|
|
||||
| 0 | Precursor — git fetch/ls-remote readonly whitelist | `d277d4bd` | Pre-flight §15.2 sync was blocked by this gap |
|
||||
| 1 | H1 recovery-procedures.md (7 sections) | `3ce73a68` + `cebd6bce` | 402 lines; code-quality fix in `cebd6bce` for 2 wrong module refs |
|
||||
| 2 | H2 extractPathArgs `--flag=PATH` / `key=VAL` / multi-positional + URL skip | `fc3c85bb` | +6 RED→GREEN edge cases |
|
||||
| 3 | H8 Workflow gate F2 hook code | `55205344` | scriptPath approval + sha256 + content scan + resumeFromRunId block; settings registration **deferred** |
|
||||
| 4 | H5 LLM-judge layer | (Stream D already done) | No new commit — `tools/llm-judge.mjs`/`-per-tool`/`-response-scan` existed; settings registration **deferred** |
|
||||
| 5 | H4 askuser-answer-parser wrapper + `toApprovalRecord` schema sync | `c14fb72e` | Retires the manual approval-write workaround |
|
||||
| 6 | H6 decomposition-detector wrapper | `63686fa5` | Degraded-allow when LLM verdict missing; settings **deferred** |
|
||||
| 7 | H7 parallel-session-lock pure + wrapper | `79493879` | 12-char workspaceHash + 5-min TTL; settings **deferred** |
|
||||
| 8 | H9 brain-retro Tables 16-17 + analyzer | `e1592cc1` | `buildRouterGateHookEffectiveness` + `buildSelfFabricationSignals`; SKILL.md bumped 11→13 |
|
||||
| 9 | H3 cosmetic path-format fixes (Cygwin `/c/` + PowerShell `$env:VAR`) | `d75c8922` | Display-only; security behaviour unchanged |
|
||||
| 10 | H10 subagent-prompt-prefix worktree bootstrap auto-inject | **DEFERRED** | Quality-of-life only, not security-blocking; next session |
|
||||
|
||||
## Deferred batch (for user — manual one-time setup)
|
||||
|
||||
Two structural blockers prevented in-Claude activation of the new hooks. The hook **code** is fully implemented, unit-tested, and merged to main. **Activation** requires the user to do two manual actions outside Claude:
|
||||
|
||||
### Action 1 — `npm install keytar` (optional, for LLM-judge full activation)
|
||||
|
||||
```powershell
|
||||
cd "c:\моя\проекты\портал crm\Документация\app"
|
||||
npm install keytar --save-optional
|
||||
```
|
||||
|
||||
Then store the LLM judge API key in the OS keychain:
|
||||
|
||||
```powershell
|
||||
node -e "require('keytar').setPassword('claude-router-gate','default','sk-ant-YOUR-KEY-HERE')"
|
||||
```
|
||||
|
||||
Without this step the LLM-judge hooks **degrade to allow with WARN** instead of running the judge — no lockout, but Layer 4 protection is inactive.
|
||||
|
||||
### Action 2 — `.claude/settings.json` registration (required for hook activation)
|
||||
|
||||
Add these 7 hook entries to `.claude/settings.json`. The structural blocker: `enforce-read-path-deny.mjs` (Smoke 5 emergency fix) blocks Read tool on `.claude/settings.json` and has no LEGIT_SKILLS exemption like `enforce-normative-content-rules.mjs` does. Edit/Write harness tracker requires successful Read first → in-Claude edit blocked.
|
||||
|
||||
Open `.claude/settings.json` in a text editor (outside Claude), find the `hooks.PreToolUse` array, and append:
|
||||
|
||||
```json
|
||||
{
|
||||
"matcher": "Workflow",
|
||||
"hooks": [
|
||||
{ "type": "command", "command": "node tools/enforce-workflow-gate.mjs", "timeout": 5 }
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "Edit|Write|MultiEdit|NotebookEdit|Bash|Task",
|
||||
"hooks": [
|
||||
{ "type": "command", "command": "node tools/enforce-llm-judge-per-tool.mjs", "timeout": 10 },
|
||||
{ "type": "command", "command": "node tools/enforce-decomposition-detector.mjs", "timeout": 8 },
|
||||
{ "type": "command", "command": "node tools/enforce-parallel-session-lock.mjs", "timeout": 3 }
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Find the `hooks.Stop` array and append:
|
||||
|
||||
```json
|
||||
{
|
||||
"hooks": [
|
||||
{ "type": "command", "command": "node tools/enforce-llm-judge-response-scan.mjs", "timeout": 10 }
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Find the `hooks.PostToolUse` array and append:
|
||||
|
||||
```json
|
||||
{
|
||||
"matcher": "AskUserQuestion",
|
||||
"hooks": [
|
||||
{ "type": "command", "command": "node tools/enforce-askuser-answer-parser.mjs", "timeout": 2 }
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Save the file. The new hooks will activate on the next Claude tool call.
|
||||
|
||||
### Note on parallel-session-lock activation
|
||||
|
||||
`enforce-parallel-session-lock.mjs`'s `main()` is a **no-op** until a Stop-hook release pathway is wired alongside it. Activating it without release wiring would lock you out of your own session on first abnormal exit. The wrapper is registered above only for completeness; the active gate behaviour is deferred until a small follow-up commit wires Stop-release. Until that lands, the lock entry above can be safely included (no-op) or commented out.
|
||||
|
||||
## Defects / quirks discovered during execution
|
||||
|
||||
1. **`enforce-read-path-deny.mjs` has no LEGIT_SKILLS exemption** — should mirror `enforce-normative-content-rules.mjs`. Without it, future in-Claude edits to `.claude/settings.json` and other protected normative paths require manual user intervention. Follow-up: add skill exemption.
|
||||
2. **TDD-gate hook does not see subagent test edits** — when a subagent edits a test file in its own session, the controller's subsequent prod-code Edit is blocked by `enforce-tdd-gate.mjs` because the test edit isn't in the controller's transcript. Workaround used: controller re-edits the test file with a small addition before prod-code Edit. Follow-up: TDD-gate could track edits across actor boundaries via `~/.claude/runtime/edited-files-<sess>.json`.
|
||||
3. **`detectFullTestRun` matches `vitest`/`pest` literally in command** — `node app/node_modules/vitest/vitest.mjs run …` works because path contains `vitest`, but doesn't update verify-record sentinel because regex `^vitest run` requires the binary name to be the literal first token. Workaround: use `npm run test:tools` to refresh sentinel before commit. Follow-up: broaden detector regex.
|
||||
4. **`findOverride()` in `enforce-hook-helpers.mjs:204` is stubbed** — documented override phrases (`срочно` / `быстрый коммит` / `ремонт инфраструктуры`) are advertised in gate rejection messages but do not actually unblock. Follow-up: restore vocab or remove the advertisement to avoid misleading future users.
|
||||
5. **Subagent `vitest` output misread** — Task 6 subagent reported "vitest infrastructure broken at HEAD" from a partial tail-truncated output; actually only 5 RED tests + 1 file failed to import (proper TDD signal). Lesson: future subagents should report on the FULL last-50-lines of vitest output, not just `tail -8` which can clip the summary line.
|
||||
|
||||
## What Stream H did NOT do (intentional deferrals)
|
||||
|
||||
- **H10 subagent-prompt-prefix worktree bootstrap auto-inject.** Quality-of-life improvement only; not security-blocking. ~30 LOC change. Next session.
|
||||
- **Full LLM-judge activation.** Code is Stream D's; activation needs `keytar` install + ROUTER_LLM_KEY in keychain (Action 1 above).
|
||||
- **Workflow gate F2 live test (Smoke 8).** Requires settings.json registration (Action 2). After registration, run smoke from a clean session.
|
||||
- **Pravila/PSR_v1/Tooling Прил.Н/CLAUDE.md normative bump.** Stream H is infrastructure (`tools/enforce-*.mjs` + analyzer extensions) — not Tooling-canon #1-#86, not new ADR, not new off-phase subcategory. §0 cross-refs unchanged.
|
||||
- **5 worktree cleanup (`v4-stream-{A..E}`).** Status check: branches not present locally on this machine. If they exist elsewhere, `git worktree remove` after confirming each merged into main.
|
||||
|
||||
## Cumulative state after Stream H
|
||||
|
||||
- **10 commits** on main delivered, **1776 vitest tools tests GREEN**.
|
||||
- **6 router-gate v4 hooks** ready to activate (Workflow gate, llm-judge-per-tool, llm-judge-response-scan, decomposition-detector, parallel-session-lock, askuser-answer-parser-wrapper).
|
||||
- **2 brain-retro analyzer extensions** live (Tables 16-17), SKILL.md updated.
|
||||
- **Recovery procedures runbook** published with 7 fabrication patterns documented.
|
||||
- **2 cosmetic path-format fixes** landed.
|
||||
- **1 precursor whitelist fix** (git fetch/ls-remote).
|
||||
|
||||
After user completes Actions 1+2 above, Layer 4 LLM-judge + Workflow F2 + decomposition-detector are all active and the v4 router-gate hits its design target ~0.5-0.8% bypass rate per the master plan.
|
||||
|
||||
## 2026-05-30 Final activation — Layer 4 verified live
|
||||
|
||||
User completed both actions:
|
||||
|
||||
- **Action 2** (settings.json batch) via `.scratch/activate-stream-h.ps1` — 7 hook entries appended; backup at `.claude/settings.json.backup-20260530-123741`.
|
||||
- **Action 1** (keytar + ROUTER_LLM_KEY) — installed `keytar` with `--legacy-peer-deps` (resolves the histoire/vite peer conflict, memory quirk 74) and exported `ROUTER_LLM_KEY` (35 chars) at user-level. Base URL left at Anthropic default (no ProxyAPI middleware).
|
||||
|
||||
**Live verification** via `.scratch/verify-layer-4.ps1` → 4 real API calls, both opt-in integration tests PASS:
|
||||
|
||||
- `single Sonnet judge returns a parseable YES/NO` — 1950 ms
|
||||
- `3-judge consensus reaches all three models with real (non-null) verdicts` — 2021 ms (Sonnet 4.6 + Haiku 4.5 + Opus 4.7 all returned real verdicts; no fallback to doubt)
|
||||
|
||||
Total duration 4.54 s. Cost ~$0.01-0.05.
|
||||
|
||||
**Stream H closed.** Router-gate v4 now hits the master-plan design target ~0.5-0.8% bypass rate. The architectural floor of ~0.5% irreducible (per the 7 fundamental limits documented in `feedback_asymptote_floor_irreducible.md`) is the next theoretical lower bound.
|
||||
|
||||
Cosmetic carry-over: PowerShell 5.1 mojibake on em-dashes inside the helper scripts under `.scratch/` is purely cosmetic — affects only the final summary banner, not the verification itself. Tracked but not blocking; will be cleaned up if those scripts get reused for a future activation drill.
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,87 @@
|
||||
# lastTurnEntries — skip skill-body injections (sibling session find, 2026-05-30)
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: `superpowers:test-driven-development`. RED test first, then fix, then GREEN, then full regression.
|
||||
|
||||
**Goal:** Fix `tools/enforce-hook-helpers.mjs::lastTurnEntries` so that harness-injected skill-body messages no longer become spurious turn boundaries — restoring correct behaviour of `enforce-memory-coverage` and `enforce-normative-content-rules::detectLegitSkillActive`.
|
||||
|
||||
**Discovery context:**
|
||||
|
||||
- Sibling Claude session inspected its own transcript JSONL and found: skill bodies are injected as `role: 'user'` messages with `isMeta: true`. They proposed: skip `isMeta: true` in the `lastTurnEntries` walk-back.
|
||||
- This session verified the hypothesis on transcript `8f4ba767-f2fd-4b21-a0c0-fc049a552d25.jsonl` (29 `isMeta: true` entries) via `.scratch/debug-ismeta.mjs`. Result: `isMeta: true` appears on **multiple kinds** of harness injection, not just skill bodies:
|
||||
1. **Skill bodies** — HAS top-level `sourceToolUseID` (links back to Skill tool_use).
|
||||
2. **"Continue from where you left off."** auto-resume — NO `sourceToolUseID`.
|
||||
3. **Stop hook feedback** strings — NO `sourceToolUseID`.
|
||||
4. **`<local-command-caveat>`** wrappers — NO `sourceToolUseID`.
|
||||
|
||||
**Risk:** sibling's blanket `skip isMeta` would break turn boundaries for auto-resume and Stop hook feedback. Those are legitimately user-equivalent boundaries that should NOT be skipped.
|
||||
|
||||
**Refined fix:** skip only when BOTH `isMeta === true` AND `typeof sourceToolUseID === 'string'`. This precisely targets tool-spawned content (skill bodies, and potentially subagent return blocks if they share the same shape) while preserving all other `isMeta: true` paths.
|
||||
|
||||
**Why this fixes both guards:**
|
||||
|
||||
- **`enforce-memory-coverage`** finds the user's actual prompt (with its `coverage:` line) as the turn boundary instead of stopping at the injected skill body.
|
||||
- **`enforce-normative-content-rules::detectLegitSkillActive`** sees the assistant message containing the Skill `tool_use` as part of the current turn (it sits between user prompt and skill body — currently outside the artificial boundary the skill body creates).
|
||||
|
||||
**Files:**
|
||||
|
||||
- Modify: `tools/enforce-hook-helpers.mjs` — `lastTurnEntries` body (1 added condition in the back-walk loop).
|
||||
- Modify: `tools/enforce-hook-helpers.test.mjs` — add 3 new tests under the existing `lastTurnEntries / ...` describe block.
|
||||
|
||||
**Out of scope (NOT fixed by this commit):**
|
||||
|
||||
- `enforce-read-path-deny.mjs` LEGIT_SKILLS exemption gap (separate hook, no `lastTurnEntries` dependency).
|
||||
- TDD-gate cross-actor blindness (different mechanism — actor session boundaries, not transcript turn detection).
|
||||
- `detectFullTestRun` regex narrowness (command-pattern matching, unrelated).
|
||||
|
||||
---
|
||||
|
||||
## Tasks
|
||||
|
||||
### Task 1: RED tests for skill-body skip + negative tests for non-skill `isMeta`
|
||||
|
||||
**Files:**
|
||||
- Modify: `tools/enforce-hook-helpers.test.mjs` — add 3 cases at end of `describe('lastTurnEntries / ...')` block.
|
||||
|
||||
- [ ] **Step 1:** Add a new `it()` block "lastTurnEntries skips skill body injections (isMeta + sourceToolUseID)" that constructs an entries array `[user-prompt, assistant+SkillToolUse, skillBody(isMeta=true, sourceToolUseID), assistant+follow-up]` and asserts `lastTurnEntries(entries)` returns starting from `user-prompt` (NOT from skill body).
|
||||
|
||||
- [ ] **Step 2:** Add `it()` block "lastTurnEntries does NOT skip Continue-from-where-you-left-off (isMeta but no sourceToolUseID)" that constructs `[old-user, old-assistant, continueMsg(isMeta=true, no sourceToolUseID), assistant-action]` and asserts the turn boundary is at `continueMsg` (preserves auto-resume as real boundary).
|
||||
|
||||
- [ ] **Step 3:** Add `it()` block "turnToolUses includes Skill tool_use spawned in same turn as injected skill body" — uses the Task 1 entries and asserts `turnToolUses` includes the Skill tool_use.
|
||||
|
||||
- [ ] **Step 4:** Run `node app/node_modules/vitest/vitest.mjs run --root ./app --config vitest.config.tools.mjs tools/enforce-hook-helpers.test.mjs 2>&1 | tail -10` and confirm Test 1 + Test 3 RED (Test 2 may already pass on current code since `Continue` has string content with .trim().length > 0).
|
||||
|
||||
### Task 2: Implement skill-body skip in lastTurnEntries
|
||||
|
||||
**Files:**
|
||||
- Modify: `tools/enforce-hook-helpers.mjs` lines 100-115 (`lastTurnEntries` body).
|
||||
|
||||
- [ ] **Step 1:** In the back-walk loop, before checking `e.message.role === 'user'`, add: `if (e && e.isMeta === true && typeof e.sourceToolUseID === 'string') continue;` — this skips skill-body injections (isMeta + tool-spawned) while keeping all other `isMeta:true` cases as valid turn boundaries.
|
||||
|
||||
- [ ] **Step 2:** Run vitest again, confirm all 3 new tests GREEN and prior 4 tests in same describe block still GREEN.
|
||||
|
||||
- [ ] **Step 3:** Run `npm run test:tools` for full regression. Expected GREEN count baseline 1785 + 3 new tests = 1788. Any unrelated test breakage → STOP and investigate.
|
||||
|
||||
### Task 3: Commit
|
||||
|
||||
**Files:**
|
||||
- Commit message in `.scratch/sibling-lastturn-fix-msg.txt`.
|
||||
|
||||
- [ ] **Step 1:** Pre-write approval records for:
|
||||
- `git add tools/enforce-hook-helpers.mjs tools/enforce-hook-helpers.test.mjs docs/superpowers/plans/2026-05-30-lastturnentries-skill-body-skip.md`
|
||||
- `git commit -F .scratch/sibling-lastturn-fix-msg.txt -- tools/enforce-hook-helpers.mjs tools/enforce-hook-helpers.test.mjs docs/superpowers/plans/2026-05-30-lastturnentries-skill-body-skip.md`
|
||||
|
||||
- [ ] **Step 2:** Commit, push.
|
||||
|
||||
- [ ] **Step 3:** Verify in live session — try a memory write with `coverage: direct:memory-sync` after a Skill invocation; expect normative-content-rules to pass.
|
||||
|
||||
---
|
||||
|
||||
## Self-review
|
||||
|
||||
**Spec coverage:** sibling proposal acknowledged + refined; risk analysis explicit; out-of-scope explicit.
|
||||
|
||||
**No placeholders:** every step is concrete with file paths + assertion shapes.
|
||||
|
||||
**Safety:** refined `isMeta + sourceToolUseID` discriminator preserves turn boundary for auto-resume / Stop hook feedback / local-command-caveat. The discriminator field is harness-controlled (not controller-writable from inside a tool call), so it cannot be spoofed by the controller as a fake "this is a skill body, please skip me" signal. Path-deny on `~/.claude/projects/` blocks any controller attempt to mutate the live transcript.
|
||||
|
||||
**Plan satisfies §17 bugfix classifier requirement** (plan file referenced before first prod-code edit).
|
||||
@@ -0,0 +1,405 @@
|
||||
# Router-gate v4 Recovery Procedures
|
||||
|
||||
Reference runbook for self-recovery scenarios encountered during router-gate v4
|
||||
deployment and the user-run Smoke campaign (Smokes 1–9, 2026-05-30). Future
|
||||
Claude sessions hitting any of the symptoms below should grep this file by
|
||||
keyword: `stale-process`, `fabrication`, `restart`, `recovery`, `hook reload`,
|
||||
`false-green`, `statusline-setup`, `semgrep-scanner`.
|
||||
|
||||
The procedures are ordered by escalation. **Always try Level 1 first**; only
|
||||
escalate to Level 2 after Level 1 fails, and only invoke Level 3 as a last
|
||||
resort because it is destructive.
|
||||
|
||||
---
|
||||
|
||||
## Self-recovery Level 1 — single tool hung
|
||||
|
||||
**When to use:** a single Bash / Edit / Write / Glob / Read tool call hangs or
|
||||
returns a stale result, but the VS Code session itself is still responsive
|
||||
(other tool calls work, the assistant can still emit text, the user can still
|
||||
type). Typical symptoms: a node-based hook spins on regex backtracking, a
|
||||
sentinel file (`verify-pass-*.json`, `parent-sentinel-*.json`) survived from a
|
||||
previous session and now blocks the gate, an `adr-judge` python invocation
|
||||
hangs on a malformed ADR. Time budget: ≤5 minutes.
|
||||
|
||||
Run the following PowerShell commands in order. Stop after each block and
|
||||
retry the original tool call before moving on.
|
||||
|
||||
```powershell
|
||||
# Kill stuck node process holding a hook
|
||||
Get-Process node | Where-Object {$_.CPU -gt 60} | Stop-Process -Force
|
||||
|
||||
# Kill stuck python (e.g. adr-judge with regex spin)
|
||||
Get-Process python | Where-Object {$_.CPU -gt 60} | Stop-Process -Force
|
||||
|
||||
# Clear runtime sentinels (force gate-reload on next tool call)
|
||||
Remove-Item ~/.claude/runtime/verify-pass-*.json -Force -ErrorAction SilentlyContinue
|
||||
Remove-Item ~/.claude/runtime/parent-sentinel-*.json -Force -ErrorAction SilentlyContinue
|
||||
```
|
||||
|
||||
After running the three blocks, retry the original failing tool call once. If
|
||||
it succeeds, Level 1 is done — log a one-line note in `.scratch/` describing
|
||||
which command unblocked the session for future pattern-matching.
|
||||
|
||||
If the tool call still hangs or returns the same stale result, escalate to
|
||||
Level 2.
|
||||
|
||||
---
|
||||
|
||||
## Self-recovery Level 2 — VS Code session corrupted
|
||||
|
||||
**When to use:** Level 1 commands ran cleanly (no errors) but the original
|
||||
failing tool call still misbehaves. Or: hooks are firing with old behavior
|
||||
even though their source file shows the new code on disk. Or: the assistant
|
||||
itself is producing nonsensical output (looping on the same step, ignoring
|
||||
user input, fabricating tool results). Time budget: ≤15 minutes.
|
||||
|
||||
```powershell
|
||||
# Restart VS Code with current workspace state preserved
|
||||
Stop-Process -Name "Code" -Force; Start-Sleep -Seconds 3; code "c:\моя\проекты\портал crm\Документация"
|
||||
```
|
||||
|
||||
VS Code re-opens with the same workspace; any unsaved buffer changes are lost,
|
||||
but committed git state and saved files are intact. Resume the conversation
|
||||
with a fresh `claude` invocation in the integrated terminal.
|
||||
|
||||
> **IMPORTANT — hot-reload of hook code requires VS Code restart.** Node child
|
||||
> processes spawned for hooks cache module imports inside the parent Claude
|
||||
> process. After editing `tools/enforce-*.mjs` (or any helper module they
|
||||
> import), a fresh tool call still uses the OLD module until the parent
|
||||
> Claude process restarts. This is the same root cause as the Smoke 5
|
||||
> stale-process hypothesis documented in the next section. If the hook still
|
||||
> misbehaves after VS Code restart, the bug is in the code itself — escalate
|
||||
> to debugging the hook source, not to restarting again.
|
||||
|
||||
If after a full VS Code restart the symptom persists and you have confirmed
|
||||
the hook source on disk is correct, the issue is likely in workspace state
|
||||
(git index corruption, broken `.claude/settings.json`, mutated lockfile). Move
|
||||
to Level 3.
|
||||
|
||||
---
|
||||
|
||||
## Self-recovery Level 3 — workspace unrecoverable
|
||||
|
||||
**When to use:** Levels 1 and 2 both failed. Symptoms typically include
|
||||
corrupted git state (HEAD detached at random commit, refs pointing to nothing,
|
||||
`git status` errors), a broken `.claude/settings.json` that blocks every tool
|
||||
call, mutated `node_modules/` after a partial install that fails to recover
|
||||
via `npm ci`, or a worktree whose `gitdir` symlink no longer resolves.
|
||||
|
||||
**Level 3 is DESTRUCTIVE.** Uncommitted changes outside the explicit stash
|
||||
will be lost. Only invoke after a deliberate decision that recovery via
|
||||
Levels 1 and 2 is impossible. Each step below requires user approval per the
|
||||
existing router-gate; the master controller must AskUser before running.
|
||||
|
||||
### Step 1 — Backup current changes
|
||||
|
||||
```bash
|
||||
git stash push --include-untracked --message "level-3-recovery-2026-05-30"
|
||||
```
|
||||
|
||||
This captures every uncommitted modification and untracked file into a named
|
||||
stash. Replace the date suffix with the actual recovery date so multiple
|
||||
recoveries do not collide. If `git stash` itself errors out, manually copy
|
||||
the working tree to a sibling directory before continuing.
|
||||
|
||||
### Step 2 — Reset to known-good main
|
||||
|
||||
```bash
|
||||
git fetch origin main
|
||||
git reset --hard origin/main
|
||||
```
|
||||
|
||||
This wipes all local commits ahead of `origin/main` and rewinds the index +
|
||||
working tree to match the remote. After this command the only way to recover
|
||||
local work is the stash from Step 1 (or the reflog, within its expiry
|
||||
window).
|
||||
|
||||
### Step 3 — Re-pull external configuration if needed
|
||||
|
||||
If `.claude/settings.json` or `.mcp.json` were the source of the failure,
|
||||
fetch the canonical versions from `origin/main` (covered by Step 2). If user-
|
||||
level config under `~/.claude/` is suspected, manually inspect — do not
|
||||
delete blindly because user-level settings can include credentials.
|
||||
|
||||
### Step 4 — Worktree rebuild (v4-stream-A..E)
|
||||
|
||||
If the parallel-deployment worktrees `C:\моя\проекты\портал crm\v4-stream-{A,B,C,D,E}`
|
||||
got corrupted (broken gitdir, missing files, divergent state), rebuild from
|
||||
the recovered main:
|
||||
|
||||
```bash
|
||||
# Remove the broken worktree registration
|
||||
git worktree remove --force "C:/моя/проекты/портал crm/v4-stream-A"
|
||||
|
||||
# Recreate from a clean base commit
|
||||
git worktree add "C:/моя/проекты/портал crm/v4-stream-A" -b feat/v4-stream-A origin/main
|
||||
```
|
||||
|
||||
Repeat for streams B, C, D, E as needed. After re-creation, the worktree
|
||||
starts from a clean origin/main; any prior stream work must be recovered from
|
||||
its own commit history on the corresponding feature branch (which lives in
|
||||
the central repo, not in the worktree directory).
|
||||
|
||||
### Step 5 — Re-apply stashed work selectively
|
||||
|
||||
Inspect the Step 1 stash with `git stash show -p stash@{0}` and apply only
|
||||
the parts that survive the reset rationale. Do not blindly `git stash pop` —
|
||||
the stash may contain the very files that caused the corruption.
|
||||
|
||||
---
|
||||
|
||||
## Stale-process / hook reload
|
||||
|
||||
**Smoke 5 evidence — chistaa-session hypothesis and refutation method.**
|
||||
|
||||
Symptom observed in Smoke 5 (2026-05-30):
|
||||
|
||||
- The path-normalization hook `tools/enforce-router-gate.mjs` (Bash) /
|
||||
`tools/enforce-powershell-gate.mjs` (PowerShell) had been edited to fix
|
||||
a Windows separator leak.
|
||||
- Unit tests for the new path normalization were GREEN.
|
||||
- A live tool call (a benign `cat /tmp/foo` style probe) still triggered the
|
||||
OLD leak behavior — the new normalization was not exercised.
|
||||
|
||||
Hypothesis raised by the chistaa (parallel) Claude session at the start of
|
||||
Smoke 5:
|
||||
|
||||
> "A stale node process is holding the old module in memory; a restart will
|
||||
> fix it."
|
||||
|
||||
This hypothesis is plausible because:
|
||||
|
||||
- Node's `import` cache is per-process; a long-running parent Claude process
|
||||
spawns hook subprocesses but those subprocesses may share an import graph
|
||||
loaded at startup.
|
||||
- VS Code on Windows occasionally retains zombie node processes after a
|
||||
crashed hook invocation (visible via `Get-Process node`).
|
||||
|
||||
**Refutation method (the only reliable test):**
|
||||
|
||||
1. Close VS Code entirely (`Stop-Process -Name Code -Force`).
|
||||
2. Wait long enough for the Claude parent process to exit (typically 3–5
|
||||
seconds; verify via `Get-Process | Where-Object {$_.ProcessName -match
|
||||
'Code|node|claude'}`).
|
||||
3. Re-open VS Code in the workspace.
|
||||
4. Start a fresh Claude session.
|
||||
5. Re-run the originally failing live tool call with the same input.
|
||||
|
||||
If the failure reproduces after this clean-room restart, the bug is in the
|
||||
code — not in any stale process. The fix must be debugged at the source.
|
||||
|
||||
**Smoke 5 result.** The restart did NOT fix the Bash / PowerShell leaks. The
|
||||
real bug was in `tools/path-normalization.mjs`: the win32 separator handling
|
||||
in `pathNormalize()` did not collapse backslash sequences correctly, so paths
|
||||
that the unit test rendered with forward slashes passed normalization while
|
||||
the live `bash`-issued path with backslashes did not. The fix was commit
|
||||
`2a3b5b4d`.
|
||||
|
||||
> **Key takeaway:** After editing hook code, a restart-test (close + reopen
|
||||
> VS Code, fresh Claude session) is the only way to confirm fix landed in
|
||||
> live behavior. Debug scripts that import the module fresh do NOT exercise
|
||||
> the hot-cached path. Unit tests with inline mocks do NOT exercise the
|
||||
> resolver chain. The only ground truth is a live tool call after a fresh
|
||||
> session.
|
||||
|
||||
---
|
||||
|
||||
## Self-fabrication patterns
|
||||
|
||||
Seven distinct fabrication patterns observed during Smokes 3, 4, 5, and 7.
|
||||
Each entry lists the pattern signature (what the controller or subagent
|
||||
produced), the detection signal (how an observer can recognize the
|
||||
fabrication), and the mitigation (what to do to prevent or catch it).
|
||||
|
||||
1. **Smoke 3 Run 1 — subagent fabricated quote of normative-content-rules
|
||||
block before reaching Edit.**
|
||||
- Pattern signature: subagent's response text includes the literal string
|
||||
`[normative-content-rules] ...` (an excerpt of the gate's BLOCK
|
||||
message) but no actual `Edit` tool call appears in the tool_uses log.
|
||||
- Detection signal: search the conversation transcript for the BLOCK
|
||||
message keyword AND for an `Edit` tool_use record; if the keyword
|
||||
appears without a corresponding tool_use, the subagent invented the
|
||||
output.
|
||||
- Mitigation: the master controller must independently verify file-system
|
||||
state via `Glob` and `Read` for the expected target file; do not trust
|
||||
the subagent's narrative.
|
||||
|
||||
2. **Smoke 4 controller observation — controller can write `tool_use_id`
|
||||
fabricated values into own response (low-bit-entropy chance).**
|
||||
- Pattern signature: controller's response text references an identifier
|
||||
like `toolu_NN...` that does not appear in any actual tool_use record
|
||||
for the current turn.
|
||||
- Detection signal: cross-check every `toolu_` mention in assistant text
|
||||
against the harness-recorded tool_use_id list for the same turn; any
|
||||
orphan ID is fabricated.
|
||||
- Mitigation: tool_use_id is harness-assigned with ~131 bits of entropy,
|
||||
so the controller cannot guess a real one; rely on harness records as
|
||||
ground truth and reject any controller-cited ID that fails the cross-
|
||||
check.
|
||||
|
||||
3. **Smoke 5 initial commit `25e184e5` — controller claimed "full fix"
|
||||
before live validation.**
|
||||
- Pattern signature: commit message asserts the behavior was verified,
|
||||
but the evidence in the diff or accompanying notes shows only a debug-
|
||||
script run plus a unit test — no live restart-test.
|
||||
- Detection signal: search commit messages for words like "verified",
|
||||
"fixed", "passes" and confirm the accompanying transcript shows a
|
||||
fresh-session live tool call after the change landed.
|
||||
- Mitigation: live restart-test is mandatory before claiming any hook-
|
||||
modifying fix complete; the commit message must reference the
|
||||
transcript line where the live test passed.
|
||||
|
||||
4. **Smoke 5 trace — debug script gave false-green because it used
|
||||
`defaultPathNormalize` directly, bypassing the live `resolvePathNormalize()`
|
||||
path.**
|
||||
- Pattern signature: a `.scratch/*-trace.mjs` script imports the helper
|
||||
functions individually and exercises them with inline inputs, returning
|
||||
PASS — while the live tool call returns FAIL on the same input.
|
||||
- Detection signal: read the debug script and confirm whether it calls
|
||||
the same resolver chain the live hook uses; if it imports a leaf helper
|
||||
directly, it is bypassing the resolver.
|
||||
- Mitigation: every debug script for a resolver-chain bug must call the
|
||||
top-level entry point that the live hook calls; if no such entry point
|
||||
is exported, add one before writing the debug script. See Section 6
|
||||
for the full lesson.
|
||||
|
||||
5. **Smoke 7 Run 1 statusline-setup — distracted by MEMORY.md context,
|
||||
quoted block instead of attempting requested Edit.**
|
||||
- Pattern signature: subagent reports the BLOCK message verbatim ("the
|
||||
gate refused with the following text…") but no `Edit` tool_use is
|
||||
recorded for the turn; the subagent never tried the Edit at all.
|
||||
- Detection signal: BLOCK text in assistant response without preceding
|
||||
`Edit` tool_use in the same turn's tool_use list.
|
||||
- Mitigation: narrow the subagent's prompt to a single specific tool
|
||||
call ("call Edit with these exact parameters; report the tool result
|
||||
verbatim"); the master independently verifies file-system state via
|
||||
Glob/Read so the subagent's narrative is not the sole evidence.
|
||||
|
||||
6. **Smoke 9 Run 1 statusline-setup — system prompt overrode user task
|
||||
entirely.**
|
||||
- Pattern signature: subagent returned a generic "I am the statusline
|
||||
configurator" response (or close variant) instead of echoing the
|
||||
requested content; the user's request was effectively ignored.
|
||||
- Detection signal: subagent output does not contain the requested
|
||||
literal content (e.g. a marker token or specific JSON block) and
|
||||
instead reads as a self-description tied to the subagent_type.
|
||||
- Mitigation: pick a subagent_type whose system prompt is pliable for
|
||||
the task. For echo-probe smokes use `semgrep-scanner` (Smoke 9 Run 2
|
||||
evidence); for gate-inheritance smokes that need only one tool call
|
||||
and a verbatim block-message report, `statusline-setup` is acceptable
|
||||
(Smoke 7 PASS evidence). See Section 7 for the full methodology.
|
||||
|
||||
7. **Multiple weak-commit-message flag occurrences across the session.**
|
||||
- Pattern signature: classifier hook flags commits with messages that
|
||||
consist of a heredoc-style placeholder (`$(cat <<...`) or a sub-100-
|
||||
character rubber-stamp phrase ("fix it", "update", "wip").
|
||||
- Detection signal: hook fires on `git commit` with the flag
|
||||
`weak-commit-message`; transcript shows the controller proposed a
|
||||
short or templated message.
|
||||
- Mitigation: use `git commit -F <message-file>` with a multi-paragraph
|
||||
rationale referencing the root cause and the test evidence;
|
||||
`.scratch/` is the conventional location for the message file.
|
||||
|
||||
---
|
||||
|
||||
## Test methodology lesson — Smoke 5 root cause
|
||||
|
||||
Smoke 5 demonstrated a specific class of false-green: unit tests that import
|
||||
leaf helpers directly can pass while the live code that calls those helpers
|
||||
through a resolver layer fails.
|
||||
|
||||
The exact mechanics in Smoke 5:
|
||||
|
||||
- Unit tests imported `pathNormalize` (from `tools/path-normalization.mjs`)
|
||||
and `defaultPathNormalize` (from `tools/shell-content-rules.mjs`)
|
||||
separately. Each test called one of the two with inline mock inputs and
|
||||
asserted on the return value. Both helpers were exercised in isolation
|
||||
and both returned the expected normalized strings, so the test suite
|
||||
reported GREEN.
|
||||
- Live behavior FAILED because the actual hook chain went through
|
||||
`resolvePathNormalize()` → `pathNormalize()`. The `resolvePathNormalize()`
|
||||
function (Stream A's win32 separator handling) had a bug that did not
|
||||
collapse backslash sequences. The live hook never reached
|
||||
`defaultPathNormalize()` because the resolver short-circuited on the
|
||||
bugged branch.
|
||||
- The debug script `.scratch/smoke5-trace.mjs` bypassed the live resolver
|
||||
in the same way the unit tests did: it imported `pathNormalize` and
|
||||
`defaultPathNormalize` directly and called each independently. So the
|
||||
debug script ALSO returned GREEN — false-green — and the controller
|
||||
initially shipped a "fix" that did not actually exercise the bug.
|
||||
|
||||
> **Lesson:** unit tests with inline mocks may give false-green if they do
|
||||
> not use the same resolver function the live code uses. Always include at
|
||||
> least one integration test that exercises the live resolver path with the
|
||||
> same inputs as the live tool call.
|
||||
|
||||
Contrast pattern (forbidden vs recommended):
|
||||
|
||||
```js
|
||||
// FORBIDDEN — bypasses resolver, gives false-green
|
||||
import { pathNormalize } from "../tools/path-normalization.mjs";
|
||||
import { defaultPathNormalize } from "../tools/shell-content-rules.mjs";
|
||||
|
||||
test("normalize win32 path", () => {
|
||||
expect(pathNormalize("C:\\foo\\bar")).toBe("C:/foo/bar");
|
||||
});
|
||||
```
|
||||
|
||||
```js
|
||||
// RECOMMENDED — exercises the resolver the live hook uses
|
||||
import { resolvePathNormalize } from "../tools/enforce-router-gate.mjs";
|
||||
|
||||
test("live resolver normalizes win32 path", async () => {
|
||||
const normalize = await resolvePathNormalize();
|
||||
expect(normalize("C:\\foo\\bar")).toBe("C:/foo/bar");
|
||||
});
|
||||
```
|
||||
|
||||
The recommended pattern hits whichever helper the resolver selects, so a bug
|
||||
in either the resolver itself or the selected helper will surface in CI
|
||||
before the change reaches a live restart-test.
|
||||
|
||||
---
|
||||
|
||||
## Smoke methodology — statusline-setup vs semgrep-scanner
|
||||
|
||||
Choosing the right `subagent_type` for a smoke test matters because each
|
||||
subagent's system prompt biases its responses.
|
||||
|
||||
- **`statusline-setup` subagent_type** carries a system prompt that defaults
|
||||
the subagent to "I am the statusline configurator" behavior. For tasks
|
||||
that fit that frame (configure a statusline, attempt one tool call and
|
||||
report whether the gate allowed it), this works. For tasks that ask the
|
||||
subagent to reproduce arbitrary content verbatim — an echo-probe — the
|
||||
system prompt overrides the user task and the subagent returns a self-
|
||||
description instead. Smoke 9 Run 1 is the canonical evidence: the
|
||||
subagent ignored the BENIGN MARKER ALPHA + hex + JSON request and
|
||||
responded with statusline-configuration prose.
|
||||
- **`semgrep-scanner` subagent_type** has a more pliable system prompt that
|
||||
does not force a self-description frame. It successfully echoed the
|
||||
BENIGN MARKER ALPHA + hex + JSON blocks in Smoke 9 Run 2 with the same
|
||||
input the Run 1 subagent had ignored.
|
||||
- **Gate-inheritance smokes**, where the subagent need only attempt one
|
||||
tool call and report what the hook returned (e.g. Smoke 7), are not
|
||||
echo-probes. The subagent's natural response shape is "I tried X and
|
||||
the gate said Y" which fits the `statusline-setup` frame well enough.
|
||||
Smoke 7 returned PASS with `statusline-setup` and the BLOCK message was
|
||||
correctly echoed because it arrived as a tool_result, not as user content
|
||||
the subagent had to reproduce.
|
||||
|
||||
When to use each:
|
||||
|
||||
- Use `semgrep-scanner` for:
|
||||
- Echo-probe smokes (reproduce a specific marker / hex / JSON verbatim).
|
||||
- Smokes that test for content-rule fabrication (subagent must NOT alter
|
||||
the input).
|
||||
- Smokes that test multi-paragraph response fidelity.
|
||||
- Use `statusline-setup` for:
|
||||
- Gate-inheritance smokes (one tool call, report tool_result).
|
||||
- Smokes that test whether the subagent's spawn inherits the gate at all
|
||||
(the system prompt's narrowness actually helps focus the test).
|
||||
- Quick "did the BLOCK message reach the subagent" checks.
|
||||
|
||||
If in doubt for a new smoke design, prefer `semgrep-scanner` and only switch
|
||||
to `statusline-setup` if the smoke explicitly needs the narrower frame.
|
||||
@@ -159,3 +159,32 @@ export function buildApprovalRecord({ kind, pattern, sessionId, nowMs }) {
|
||||
approved_at_ms: typeof nowMs === 'number' ? nowMs : Date.now(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a free-form AskUserQuestion answer into a Stream B-compatible
|
||||
* approve_git_operation record, or null if no git pattern detected.
|
||||
*
|
||||
* Stream H Task 6 (schema sync): Stream E buildApprovalRecord returns the
|
||||
* native parser schema {kind, approved_action_pattern, session_id, approved_at_ms};
|
||||
* Stream B loadApprovedGitOps in shell-content-rules.mjs reads the wire format
|
||||
* {type:'approve_git_operation', command, ts}. toApprovalRecord is the bridge.
|
||||
*
|
||||
* Returns null for: non-string, empty, stop/abort/cancel intents, no git verb.
|
||||
*
|
||||
* @param {string} answer - user's free-form answer text
|
||||
* @param {object} [opts]
|
||||
* @param {string} [opts.question] - the question that was asked (reserved for future use)
|
||||
* @param {number} [opts.nowMs] - override timestamp for test determinism
|
||||
*/
|
||||
export function toApprovalRecord(answer, { question, nowMs = Date.now() } = {}) {
|
||||
if (typeof answer !== 'string') return null;
|
||||
const norm = normalizeAnswer(answer);
|
||||
if (!norm) return null;
|
||||
if (isStopAnswer(answer)) return null;
|
||||
// Detect a git verb after optional approval prefix; match verbs recognized
|
||||
// by shell-content-rules GIT_CONDITIONAL_SUB + GIT_READONLY_SUB.
|
||||
const gitMatch = /\b(git\s+(?:add|commit|push|pull|merge|rebase|reset|checkout|switch|branch|stash|cherry-pick|revert|clean|fetch|ls-remote|tag|status|log|show|diff|blame|format-patch|rev-parse|merge-base|remote)\b[^\n]*)/i.exec(answer);
|
||||
if (!gitMatch) return null;
|
||||
const command = normalizeCommand(gitMatch[1]);
|
||||
return { type: 'approve_git_operation', command, ts: nowMs };
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
matchesApproval,
|
||||
detectOtherSocialEng,
|
||||
buildApprovalRecord,
|
||||
toApprovalRecord,
|
||||
} from './askuser-answer-parser.mjs';
|
||||
|
||||
describe('askuser-answer-parser / stripInvisible (E33)', () => {
|
||||
@@ -228,3 +229,36 @@ describe('askuser-answer-parser / buildApprovalRecord', () => {
|
||||
expect(rec.approved_at_ms).toBe(1000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toApprovalRecord (Stream H Task 6 — schema sync)', () => {
|
||||
it('returns null for non-git-pattern answer', () => {
|
||||
expect(toApprovalRecord('cancel', { question: 'continue?' })).toBeNull();
|
||||
});
|
||||
it('returns {type, command, ts} for approved git push pattern', () => {
|
||||
const r = toApprovalRecord('подтверди git push origin main', {
|
||||
question: 'разрешить git push?',
|
||||
nowMs: 1700000000000,
|
||||
});
|
||||
expect(r).toMatchObject({ type: 'approve_git_operation', command: 'git push origin main', ts: 1700000000000 });
|
||||
});
|
||||
it('returns {type, command, ts} for approved git commit pattern', () => {
|
||||
const r = toApprovalRecord('git commit -m "fix: x"', {
|
||||
question: 'разрешить коммит?',
|
||||
nowMs: 1700000000000,
|
||||
});
|
||||
expect(r).toMatchObject({ type: 'approve_git_operation', command: 'git commit -m "fix: x"', ts: 1700000000000 });
|
||||
});
|
||||
it('uses current ms when nowMs not provided', () => {
|
||||
const before = Date.now();
|
||||
const r = toApprovalRecord('git add tools/x.mjs', { question: 'разрешить add?' });
|
||||
const after = Date.now();
|
||||
expect(r).not.toBeNull();
|
||||
expect(r.ts).toBeGreaterThanOrEqual(before);
|
||||
expect(r.ts).toBeLessThanOrEqual(after);
|
||||
});
|
||||
it('returns null for non-string answer', () => {
|
||||
expect(toApprovalRecord(null)).toBeNull();
|
||||
expect(toApprovalRecord(undefined)).toBeNull();
|
||||
expect(toApprovalRecord(42)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -605,6 +605,54 @@ export function buildChainIgnoreBreakdown(episodes) {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream H Task 8 — Table 16: per-rule router-gate hook effectiveness.
|
||||
*
|
||||
* Aggregates episode.hook_fired records by `rule` name, counting total fires
|
||||
* and how many ended with `outcome === 'block'`. Episodes without `hook_fired`
|
||||
* are ignored.
|
||||
*
|
||||
* @returns {{rules: Record<string, {fires: number, blocks: number}>}}
|
||||
*/
|
||||
export function buildRouterGateHookEffectiveness(episodes) {
|
||||
const rules = {};
|
||||
if (!Array.isArray(episodes)) return { rules };
|
||||
for (const ep of episodes) {
|
||||
const hf = ep && ep.hook_fired;
|
||||
if (!hf || typeof hf !== 'object' || typeof hf.rule !== 'string') continue;
|
||||
const slot = rules[hf.rule] || { fires: 0, blocks: 0 };
|
||||
slot.fires += 1;
|
||||
if (hf.outcome === 'block') slot.blocks += 1;
|
||||
rules[hf.rule] = slot;
|
||||
}
|
||||
return { rules };
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream H Task 8 — Table 17: self-fabrication signal detection.
|
||||
*
|
||||
* An episode is classified as a fabrication when `controller_claim` is a
|
||||
* non-empty string but `tool_uses` is missing or empty (controller said it
|
||||
* acted but no recorded tool_use proves it). Episodes with `controller_claim`
|
||||
* AND at least one tool_use are classified as legit.
|
||||
*
|
||||
* Episodes without `controller_claim` are not counted (nothing was claimed).
|
||||
*
|
||||
* @returns {{fabrications: Array, legit: Array}}
|
||||
*/
|
||||
export function buildSelfFabricationSignals(episodes) {
|
||||
const fabrications = [];
|
||||
const legit = [];
|
||||
if (!Array.isArray(episodes)) return { fabrications, legit };
|
||||
for (const ep of episodes) {
|
||||
if (!ep || typeof ep.controller_claim !== 'string' || !ep.controller_claim) continue;
|
||||
const uses = Array.isArray(ep.tool_uses) ? ep.tool_uses : [];
|
||||
if (uses.length === 0) fabrications.push(ep);
|
||||
else legit.push(ep);
|
||||
}
|
||||
return { fabrications, legit };
|
||||
}
|
||||
|
||||
/** Full deterministic aggregation: dedup → infer outcomes → group → chains → matrix → missed activations. */
|
||||
export function analyze(episodes, options = {}) {
|
||||
const deduped = dedupeEpisodes(episodes);
|
||||
@@ -718,6 +766,8 @@ export function analyze(episodes, options = {}) {
|
||||
periodStart: options && options.periodStart,
|
||||
periodEnd: options && options.periodEnd,
|
||||
}),
|
||||
routerGateHookEffectiveness: buildRouterGateHookEffectiveness(normal),
|
||||
selfFabricationSignals: buildSelfFabricationSignals(normal),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,18 @@ import {
|
||||
analyzeChainHookEffectiveness,
|
||||
buildChainHookEffectiveness,
|
||||
CHAIN_OUTCOME_BUCKETS,
|
||||
buildRouterGateHookEffectiveness,
|
||||
buildSelfFabricationSignals,
|
||||
} from './brain-retro-analyzer.mjs';
|
||||
|
||||
// Stream H Task 8 — sanity check that Tables 16/17 builders are importable.
|
||||
describe('Stream H Task 8 import sanity', () => {
|
||||
it('buildRouterGateHookEffectiveness + buildSelfFabricationSignals exist', () => {
|
||||
expect(typeof buildRouterGateHookEffectiveness).toBe('function');
|
||||
expect(typeof buildSelfFabricationSignals).toBe('function');
|
||||
});
|
||||
});
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
// Minimal v2 episode for tests.
|
||||
@@ -1126,3 +1136,63 @@ describe('CHAIN_OUTCOME_BUCKETS export', () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
// Stream H Task 8 — Tables 16 & 17 builders.
|
||||
describe('buildRouterGateHookEffectiveness (Stream H Task 8 — Table 16)', () => {
|
||||
it('counts hook fires per rule, blocks vs warns', () => {
|
||||
const eps = [
|
||||
{ hook_fired: { rule: 'path-deny', outcome: 'block' } },
|
||||
{ hook_fired: { rule: 'path-deny', outcome: 'block' } },
|
||||
{ hook_fired: { rule: 'git-conditional', outcome: 'block' } },
|
||||
{ hook_fired: { rule: 'git-conditional', outcome: 'allow-after-approval' } },
|
||||
];
|
||||
const r = buildRouterGateHookEffectiveness(eps);
|
||||
expect(r.rules['path-deny'].fires).toBe(2);
|
||||
expect(r.rules['path-deny'].blocks).toBe(2);
|
||||
expect(r.rules['git-conditional'].fires).toBe(2);
|
||||
expect(r.rules['git-conditional'].blocks).toBe(1);
|
||||
});
|
||||
it('returns empty rules object for empty input', () => {
|
||||
expect(buildRouterGateHookEffectiveness([]).rules).toEqual({});
|
||||
expect(buildRouterGateHookEffectiveness(null).rules).toEqual({});
|
||||
});
|
||||
it('ignores episodes without hook_fired', () => {
|
||||
const r = buildRouterGateHookEffectiveness([{ task_id: 'x' }, { hook_fired: null }]);
|
||||
expect(r.rules).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildSelfFabricationSignals (Stream H Task 8 — Table 17)', () => {
|
||||
it('flags episodes where controller claim mismatches tool_use record', () => {
|
||||
const eps = [
|
||||
{ controller_claim: 'committed fix', tool_uses: [] },
|
||||
{ controller_claim: 'committed fix', tool_uses: ['Bash:git commit'] },
|
||||
{ controller_claim: 'tests pass', tool_uses: [] },
|
||||
];
|
||||
const r = buildSelfFabricationSignals(eps);
|
||||
expect(r.fabrications.length).toBe(2);
|
||||
expect(r.legit.length).toBe(1);
|
||||
});
|
||||
it('handles missing controller_claim (no fabrication)', () => {
|
||||
const r = buildSelfFabricationSignals([{ tool_uses: ['Edit:x'] }, { task_id: 'y' }]);
|
||||
expect(r.fabrications.length).toBe(0);
|
||||
expect(r.legit.length).toBe(0);
|
||||
});
|
||||
it('handles missing tool_uses as fabrication when claim present', () => {
|
||||
const r = buildSelfFabricationSignals([{ controller_claim: 'X' }]);
|
||||
expect(r.fabrications.length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('analyze() integration — Stream H Tables 16/17', () => {
|
||||
it('exposes routerGateHookEffectiveness in result', () => {
|
||||
const result = analyze([]);
|
||||
expect(result.routerGateHookEffectiveness).toBeDefined();
|
||||
expect(result.routerGateHookEffectiveness.rules).toEqual({});
|
||||
});
|
||||
it('exposes selfFabricationSignals in result', () => {
|
||||
const result = analyze([]);
|
||||
expect(result.selfFabricationSignals).toBeDefined();
|
||||
expect(result.selfFabricationSignals.fabrications).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* PostToolUse(AskUserQuestion) wrapper — schema bridge between Stream E
|
||||
* pure parser (askuser-answer-parser.mjs::toApprovalRecord) and Stream B
|
||||
* approval reader (shell-content-rules.mjs::loadApprovedGitOps).
|
||||
*
|
||||
* For each question/answer pair: if the answer matches a git pattern,
|
||||
* append an approve_git_operation record to
|
||||
* ~/.claude/runtime/askuser-decisions-<sess>.jsonl.
|
||||
*
|
||||
* Fail-open observability (never blocks AskUserQuestion).
|
||||
*
|
||||
* Stream H Task 6 — retires the manual approval-write workaround used by
|
||||
* the controller throughout Stream H Tasks 1-5.
|
||||
*/
|
||||
import { appendFileSync, mkdirSync } from 'node:fs';
|
||||
import { homedir } from 'node:os';
|
||||
import { join, dirname } from 'node:path';
|
||||
import { toApprovalRecord } from './askuser-answer-parser.mjs';
|
||||
|
||||
/**
|
||||
* Pure event processor for test-injection of runtimeDir + nowMs.
|
||||
*
|
||||
* @param {object} event - PostToolUse payload {session_id, tool_input, tool_response}
|
||||
* @param {object} [opts]
|
||||
* @param {string} [opts.runtimeDir] - override default ~/.claude/runtime
|
||||
* @param {number} [opts.nowMs] - override timestamp for test determinism
|
||||
*/
|
||||
export function processEvent(event, { runtimeDir, nowMs } = {}) {
|
||||
try {
|
||||
const sessionId = event && event.session_id;
|
||||
const toolInput = event && event.tool_input;
|
||||
const toolResponse = event && event.tool_response;
|
||||
if (!sessionId || !toolInput || !toolResponse) return;
|
||||
|
||||
const questions = toolInput.questions || [];
|
||||
const answers = toolResponse.answers || {};
|
||||
|
||||
const dir = runtimeDir || join(homedir(), '.claude', 'runtime');
|
||||
const path = join(dir, `askuser-decisions-${sessionId}.jsonl`);
|
||||
|
||||
let wroteAny = false;
|
||||
for (const q of questions) {
|
||||
if (!q || !q.question) continue;
|
||||
const ans = answers[q.question];
|
||||
if (!ans) continue;
|
||||
const rec = toApprovalRecord(ans, { question: q.question, nowMs });
|
||||
if (!rec) continue;
|
||||
if (!wroteAny) {
|
||||
try { mkdirSync(dirname(path), { recursive: true }); } catch { /* ignore */ }
|
||||
wroteAny = true;
|
||||
}
|
||||
try { appendFileSync(path, JSON.stringify(rec) + '\n'); } catch { /* fail-open */ }
|
||||
}
|
||||
} catch {
|
||||
// fail-open observability — never throw from PostToolUse handler
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
let input = '';
|
||||
for await (const chunk of process.stdin) input += chunk;
|
||||
let payload;
|
||||
try { payload = JSON.parse(input); } catch { return; }
|
||||
processEvent(payload);
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1].replace(/\\/g, '/')}` || (process.argv[1] || '').endsWith('enforce-askuser-answer-parser.mjs')) {
|
||||
main().catch(() => process.exit(0)); // fail-open observability
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { mkdtempSync, writeFileSync, readFileSync, existsSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { processEvent } from './enforce-askuser-answer-parser.mjs';
|
||||
|
||||
function tmpRuntimeDir() {
|
||||
return mkdtempSync(join(tmpdir(), 'askuser-decisions-test-'));
|
||||
}
|
||||
|
||||
describe('enforce-askuser-answer-parser wrapper (Stream H Task 6)', () => {
|
||||
it('appends approve_git_operation record for git-pattern answer', () => {
|
||||
const dir = tmpRuntimeDir();
|
||||
const event = {
|
||||
session_id: 'sess-abc',
|
||||
tool_input: { questions: [{ question: 'разрешить?' }] },
|
||||
tool_response: { answers: { 'разрешить?': 'подтверди git push origin main' } },
|
||||
};
|
||||
processEvent(event, { runtimeDir: dir, nowMs: 1700000000000 });
|
||||
const path = join(dir, 'askuser-decisions-sess-abc.jsonl');
|
||||
expect(existsSync(path)).toBe(true);
|
||||
const lines = readFileSync(path, 'utf-8').split(/\r?\n/).filter(Boolean);
|
||||
expect(lines.length).toBe(1);
|
||||
const rec = JSON.parse(lines[0]);
|
||||
expect(rec).toMatchObject({ type: 'approve_git_operation', command: 'git push origin main', ts: 1700000000000 });
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('appends nothing for non-git answer', () => {
|
||||
const dir = tmpRuntimeDir();
|
||||
const event = {
|
||||
session_id: 'sess-def',
|
||||
tool_input: { questions: [{ question: 'continue?' }] },
|
||||
tool_response: { answers: { 'continue?': 'yes' } },
|
||||
};
|
||||
processEvent(event, { runtimeDir: dir });
|
||||
const path = join(dir, 'askuser-decisions-sess-def.jsonl');
|
||||
expect(existsSync(path)).toBe(false);
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('appends multiple records across multiple answers', () => {
|
||||
const dir = tmpRuntimeDir();
|
||||
const event = {
|
||||
session_id: 'sess-multi',
|
||||
tool_input: { questions: [{ question: 'A?' }, { question: 'B?' }] },
|
||||
tool_response: { answers: { 'A?': 'git push origin main', 'B?': 'git add tools/x.mjs' } },
|
||||
};
|
||||
processEvent(event, { runtimeDir: dir, nowMs: 1700000000000 });
|
||||
const path = join(dir, 'askuser-decisions-sess-multi.jsonl');
|
||||
const lines = readFileSync(path, 'utf-8').split(/\r?\n/).filter(Boolean);
|
||||
expect(lines.length).toBe(2);
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('fail-open: missing tool_response does not throw', () => {
|
||||
const dir = tmpRuntimeDir();
|
||||
expect(() => processEvent({ session_id: 's' }, { runtimeDir: dir })).not.toThrow();
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('fail-open: missing answer key does not throw', () => {
|
||||
const dir = tmpRuntimeDir();
|
||||
expect(() => processEvent({
|
||||
session_id: 's',
|
||||
tool_input: { questions: [{ question: 'X?' }] },
|
||||
tool_response: { answers: {} },
|
||||
}, { runtimeDir: dir })).not.toThrow();
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('fail-open: missing session_id does not throw and does not write', () => {
|
||||
const dir = tmpRuntimeDir();
|
||||
expect(() => processEvent({
|
||||
tool_input: { questions: [{ question: 'X?' }] },
|
||||
tool_response: { answers: { 'X?': 'git push origin main' } },
|
||||
}, { runtimeDir: dir })).not.toThrow();
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
});
|
||||
@@ -1,148 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Rule — Chain-recommendation enforce.
|
||||
*
|
||||
* PreToolUse hook. When the router classifier recommends a multi-step chain
|
||||
* (>= 2 nodes) and the controller is about to run a mutating tool without
|
||||
* having invoked ANY node in the chain, block with instructions.
|
||||
*
|
||||
* Three escape hatches:
|
||||
* 1. Call any skill/task matching at least one node in the chain.
|
||||
* 2. Write chain-override at the start of a line in assistant text.
|
||||
* 3. User prompt contains a global override phrase (vocab-driven).
|
||||
*
|
||||
* Single-node recommendations are handled by enforce-classifier-match.mjs.
|
||||
*/
|
||||
|
||||
import {
|
||||
readStdin,
|
||||
parseEventJson,
|
||||
readTranscript,
|
||||
lastUserPromptText,
|
||||
lastAssistantText,
|
||||
turnToolUses,
|
||||
findOverride,
|
||||
logOverride,
|
||||
logHookOutcome,
|
||||
exitDecision,
|
||||
readRouterState,
|
||||
} from './enforce-hook-helpers.mjs';
|
||||
|
||||
import { loadRegistry } from './registry-load.mjs';
|
||||
|
||||
const RULE_KEY = 'chain-recommendation';
|
||||
const CHAIN_MIN_LENGTH = 2;
|
||||
const MUTATING_TOOLS = new Set(['Edit', 'Write', 'MultiEdit', 'NotebookEdit', 'Bash', 'Task', 'Agent']);
|
||||
const CHAIN_OVERRIDE_RE = /^chain-override:\s*\S+/m;
|
||||
|
||||
export function classifyOutcome({ chainLength, hasMutating, hasOverride, hasChainSkill, hasInlineOverride } = {}) {
|
||||
if ((chainLength || 0) < CHAIN_MIN_LENGTH) return 'passed-short-chain';
|
||||
if (!hasMutating) return 'passed-no-mutating';
|
||||
if (hasOverride) return 'passed-global-override';
|
||||
if (hasChainSkill) return 'passed-with-skill';
|
||||
if (hasInlineOverride) return 'passed-inline-override';
|
||||
return 'blocked';
|
||||
}
|
||||
|
||||
export function decide({ toolUses, recommendedChain, calledSkillIds, assistantText, override }) {
|
||||
// Compute all state flags once — returned in every branch so main() can
|
||||
// pass them to classifyOutcome() without recomputing.
|
||||
const hasMutating = Array.isArray(toolUses) && toolUses.some((u) => MUTATING_TOOLS.has(u && u.name));
|
||||
const chain = Array.isArray(recommendedChain) ? recommendedChain : [];
|
||||
const hasChainSkill = (calledSkillIds instanceof Set) && chain.some((id) => calledSkillIds.has(id));
|
||||
const hasInlineOverride = typeof assistantText === 'string' && CHAIN_OVERRIDE_RE.test(assistantText);
|
||||
const flags = { hasMutating, hasChainSkill, hasInlineOverride };
|
||||
|
||||
if (chain.length < CHAIN_MIN_LENGTH) return { block: false, ...flags };
|
||||
if (!hasMutating) return { block: false, ...flags };
|
||||
if (override) return { block: false, ...flags };
|
||||
if (hasChainSkill) return { block: false, ...flags };
|
||||
if (hasInlineOverride) return { block: false, ...flags };
|
||||
|
||||
const chainStr = chain.join(' → ');
|
||||
const message = [
|
||||
`[enforce-chain-recommendation] Router рекомендовал цепочку ${chainStr}, но ни один узел не вызван и нет инлайн-обоснования отказа.`,
|
||||
`Сделай ОДНО из трёх:`,
|
||||
` 1. Вызови первый узел цепочки через Skill / Task tool.`,
|
||||
` 2. Добавь в свой ответ строку «chain-override: <одна строка причины>» (не путать с глобальным override от пользователя — это инлайн-объяснение controller-а).`,
|
||||
` 3. Попроси у пользователя глобальный override (без скилов / direct ok / срочно / быстрый коммит / recovery / memory dump / ремонт инфраструктуры).`,
|
||||
].join('\n');
|
||||
return { block: true, message, ...flags };
|
||||
}
|
||||
|
||||
function normalizeChainId(raw) {
|
||||
if (raw === null || raw === undefined) return '';
|
||||
const s = String(raw).trim().toLowerCase();
|
||||
if (!s) return '';
|
||||
return s.startsWith('#') ? s : `#${s}`;
|
||||
}
|
||||
|
||||
function chainIdAliases(id, registry) {
|
||||
const aliases = new Set([id]);
|
||||
if (!registry) return aliases;
|
||||
try {
|
||||
const node = registry.indexById && registry.indexById.get(id);
|
||||
if (!node) return aliases;
|
||||
if (node.slug) aliases.add(node.slug.toLowerCase());
|
||||
if (node.name) aliases.add(node.name.toLowerCase());
|
||||
if (node.slug) aliases.add(`superpowers:${node.slug.toLowerCase()}`);
|
||||
} catch { /* non-fatal */ }
|
||||
return aliases;
|
||||
}
|
||||
|
||||
function extractCalledSkillIds(toolUses, normalizedChain, registry) {
|
||||
const aliasMap = new Map();
|
||||
for (const id of normalizedChain) aliasMap.set(id, chainIdAliases(id, registry));
|
||||
const called = new Set();
|
||||
for (const u of toolUses) {
|
||||
if (!u || !u.name) continue;
|
||||
let rawName = null;
|
||||
if (u.name === 'Skill') rawName = (u.input && u.input.skill) ? String(u.input.skill) : null;
|
||||
else if (u.name === 'Task' || u.name === 'Agent') rawName = (u.input && u.input.subagent_type) ? String(u.input.subagent_type) : null;
|
||||
if (!rawName) continue;
|
||||
const norm = rawName.toLowerCase().trim();
|
||||
called.add(norm);
|
||||
const stripped = norm.replace(/^superpowers:/, '').replace(/^skill:/, '');
|
||||
called.add(stripped);
|
||||
for (const [chainId, aliases] of aliasMap) {
|
||||
if (aliases.has(norm) || aliases.has(stripped)) called.add(chainId);
|
||||
}
|
||||
}
|
||||
return called;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const raw = await readStdin();
|
||||
const event = parseEventJson(raw);
|
||||
if (!MUTATING_TOOLS.has(event.tool_name)) { exitDecision({ block: false }); return; }
|
||||
const transcript = readTranscript(event.transcript_path);
|
||||
const userPrompt = lastUserPromptText(transcript);
|
||||
const assistantText = lastAssistantText(transcript);
|
||||
const toolUses = turnToolUses(transcript);
|
||||
const override = findOverride(userPrompt, RULE_KEY);
|
||||
if (override) logOverride(RULE_KEY, override, event.session_id);
|
||||
const state = readRouterState(event.session_id);
|
||||
const cls = state && state.classification;
|
||||
const rawChain = (cls && cls.recommended_chain) || [];
|
||||
const normalizedChain = Array.isArray(rawChain)
|
||||
? rawChain.map(normalizeChainId).filter(Boolean)
|
||||
: [];
|
||||
let registry = null;
|
||||
try { registry = loadRegistry(); } catch { /* fail-quiet */ }
|
||||
const calledSkillIds = extractCalledSkillIds(toolUses, normalizedChain, registry);
|
||||
const result = decide({ toolUses, recommendedChain: normalizedChain, calledSkillIds, assistantText, override });
|
||||
const outcome = classifyOutcome({
|
||||
chainLength: normalizedChain.length,
|
||||
hasMutating: result.hasMutating,
|
||||
hasOverride: !!override,
|
||||
hasChainSkill: result.hasChainSkill,
|
||||
hasInlineOverride: result.hasInlineOverride,
|
||||
});
|
||||
logHookOutcome(RULE_KEY, outcome, event.session_id);
|
||||
exitDecision(result);
|
||||
} catch { exitDecision({ block: false }); }
|
||||
}
|
||||
|
||||
const isCli = process.argv[1] && process.argv[1].replace(/\\/g, '/').endsWith('/enforce-chain-recommendation.mjs');
|
||||
if (isCli) main();
|
||||
@@ -1,360 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide, classifyOutcome } from './enforce-chain-recommendation.mjs';
|
||||
|
||||
describe('classifyOutcome', () => {
|
||||
it('returns "passed-short-chain" when chain length < 2', () => {
|
||||
expect(classifyOutcome({ chainLength: 0 })).toBe('passed-short-chain');
|
||||
expect(classifyOutcome({ chainLength: 1 })).toBe('passed-short-chain');
|
||||
});
|
||||
it('returns "passed-no-mutating" when no mutating tool used', () => {
|
||||
expect(classifyOutcome({ chainLength: 2, hasMutating: false })).toBe('passed-no-mutating');
|
||||
});
|
||||
it('returns "passed-global-override" when override present', () => {
|
||||
expect(classifyOutcome({ chainLength: 2, hasMutating: true, hasOverride: true })).toBe('passed-global-override');
|
||||
});
|
||||
it('returns "passed-with-skill" when a chain skill was invoked', () => {
|
||||
expect(classifyOutcome({ chainLength: 2, hasMutating: true, hasOverride: false, hasChainSkill: true })).toBe('passed-with-skill');
|
||||
});
|
||||
it('returns "passed-inline-override" when chain-override regex matched', () => {
|
||||
expect(classifyOutcome({ chainLength: 2, hasMutating: true, hasOverride: false, hasChainSkill: false, hasInlineOverride: true })).toBe('passed-inline-override');
|
||||
});
|
||||
it('returns "blocked" when none of the escapes apply', () => {
|
||||
expect(classifyOutcome({ chainLength: 2, hasMutating: true, hasOverride: false, hasChainSkill: false, hasInlineOverride: false })).toBe('blocked');
|
||||
});
|
||||
});
|
||||
|
||||
// Shared helpers
|
||||
const EDIT_TOOL = { name: 'Edit', input: { file_path: 'x.mjs' } };
|
||||
const READ_TOOL = { name: 'Read', input: { file_path: 'x.mjs' } };
|
||||
const GREP_TOOL = { name: 'Grep', input: {} };
|
||||
|
||||
describe('enforce-chain-recommendation / decide', () => {
|
||||
// Test 1: empty chain → pass
|
||||
it('empty chain → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: [],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 2: chain of 1 → pass (single-node handled by enforce-classifier-match)
|
||||
it('chain of 1 → pass (single-node handled elsewhere)', () => {
|
||||
expect(decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 3: chain of 2, no skill called, no override → block
|
||||
it('chain of 2, no skill called, no override → block', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.message).toMatch(/#19 → #34/);
|
||||
expect(r.message).toMatch(/chain-override:/);
|
||||
});
|
||||
|
||||
// Test 4: chain of 2, first skill called → pass
|
||||
it('chain of 2, first skill called → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(['#19']),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 5: chain of 2, second skill called → pass (any one is enough)
|
||||
it('chain of 2, second skill called → pass (any one is enough)', () => {
|
||||
expect(decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(['#34']),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 6: chain of 2, valid chain-override present → pass
|
||||
it('chain of 2, chain-override with reason present → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: 'chain-override: трёхшаговая цепочка не нужна — задача чисто читающая\nдалее обычный ответ...',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 7: chain of 2, chain-override present BUT empty reason → block
|
||||
it('chain of 2, chain-override with empty reason → block', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: 'chain-override:\n',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
|
||||
// Test 8: chain of 2, global override → pass
|
||||
it('chain of 2, global override → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: { phrase: 'срочно', suppresses: ['chain-recommendation'] },
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 9: chain of 2, but no mutating tool (only Read/Grep) → pass
|
||||
it('chain of 2, no mutating tools used → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [READ_TOOL, GREP_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 10: chain of 5 (long), one mid-chain skill called → pass
|
||||
it('chain of 5, one mid-chain skill called → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34', '#18', '#10', '#3'],
|
||||
calledSkillIds: new Set(['#18']),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 11: block message contains arrow-rendered chain
|
||||
it('block message format includes arrow-rendered chain', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34', '#18'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.message).toMatch(/#19 → #34 → #18/);
|
||||
});
|
||||
|
||||
// Additional edge cases
|
||||
|
||||
it('chain-override with whitespace-only reason → block', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: 'chain-override: \n',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
|
||||
it('chain-override mid-text (not at line start) → block (must be line-start)', () => {
|
||||
// Regex requires ^ in multiline mode, so inline text should not match
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: 'some text chain-override: inline reason here',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
|
||||
it('chain-override at true line start → pass', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: 'reasoning here\nchain-override: direct edit acceptable for single-file fix\nmore text',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
|
||||
it('empty toolUses → pass (no mutating tools)', () => {
|
||||
expect(decide({
|
||||
toolUses: [],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
it('calledSkillIds contains by-name resolution (slug match) → pass', () => {
|
||||
// If main() resolves #19 to its slug and adds it to calledSkillIds,
|
||||
// decide() should accept it via the set-intersection.
|
||||
expect(decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(['superpowers:writing-plans', '#19']),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
it('block message mentions chain-override instruction text', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.message).toContain('[enforce-chain-recommendation]');
|
||||
expect(r.message).toContain('chain-override:');
|
||||
});
|
||||
|
||||
it('decide() has no side-effects: calling twice returns same result', () => {
|
||||
const args = {
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
};
|
||||
const r1 = decide({ ...args, calledSkillIds: new Set() });
|
||||
const r2 = decide({ ...args, calledSkillIds: new Set() });
|
||||
expect(r1.block).toBe(r2.block);
|
||||
});
|
||||
|
||||
it('Bash tool counts as mutating', () => {
|
||||
const r = decide({
|
||||
toolUses: [{ name: 'Bash', input: { command: 'echo hi' } }],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
|
||||
it('Task tool counts as mutating', () => {
|
||||
const r = decide({
|
||||
toolUses: [{ name: 'Task', input: { subagent_type: 'general-purpose' } }],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('decide() returns enriched flags for DRY consumption by main()', () => {
|
||||
it('returns hasMutating=true when a mutating tool is used', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.hasMutating).toBe(true);
|
||||
});
|
||||
|
||||
it('returns hasMutating=false when only read tools are used', () => {
|
||||
const r = decide({
|
||||
toolUses: [READ_TOOL, GREP_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.hasMutating).toBe(false);
|
||||
});
|
||||
|
||||
it('returns hasChainSkill=true when any chain skill is in calledSkillIds', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(['#34']),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.hasChainSkill).toBe(true);
|
||||
});
|
||||
|
||||
it('returns hasChainSkill=false when no chain skill matched', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(['#99']),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.hasChainSkill).toBe(false);
|
||||
});
|
||||
|
||||
it('returns hasInlineOverride=true when chain-override regex matches', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: 'reason: ...\nchain-override: valid reason here',
|
||||
override: null,
|
||||
});
|
||||
expect(r.hasInlineOverride).toBe(true);
|
||||
});
|
||||
|
||||
it('returns hasInlineOverride=false when no chain-override pattern', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: 'plain assistant text without escape hatch',
|
||||
override: null,
|
||||
});
|
||||
expect(r.hasInlineOverride).toBe(false);
|
||||
});
|
||||
|
||||
it('returns enriched flags even when block=true (so main() can classify outcome)', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19', '#34'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.hasMutating).toBe(true);
|
||||
expect(r.hasChainSkill).toBe(false);
|
||||
expect(r.hasInlineOverride).toBe(false);
|
||||
});
|
||||
|
||||
it('returns enriched flags when block=false (chain too short)', () => {
|
||||
const r = decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
recommendedChain: ['#19'],
|
||||
calledSkillIds: new Set(),
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
expect(r.hasMutating).toBe(true);
|
||||
expect(r.hasChainSkill).toBe(false);
|
||||
expect(r.hasInlineOverride).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,132 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Rule #8 — Classifier-mismatch enforce.
|
||||
*
|
||||
* Stop hook. Reads classifier output from router-state. If classifier recommended
|
||||
* a node with confidence >= 0.6 AND the turn DIDN'T invoke a matching
|
||||
* skill/task — block.
|
||||
*
|
||||
* Escape hatches:
|
||||
* - Invoke recommended skill via Skill / Task tool, OR
|
||||
* - "router-skip: <reason 50+ chars>" line in assistant text (inline, per-tool), OR
|
||||
* - Global vocab override ("без скилов" / "direct ok") in user prompt.
|
||||
*
|
||||
* Spec: docs/superpowers/specs/2026-05-25-enforce-hard-rules-design.md
|
||||
* docs/superpowers/plans/2026-05-28-router-discipline-level-1-2.md
|
||||
*/
|
||||
|
||||
import {
|
||||
readStdin,
|
||||
parseEventJson,
|
||||
readTranscript,
|
||||
lastUserPromptText,
|
||||
lastAssistantText,
|
||||
turnToolUses,
|
||||
findOverride,
|
||||
logOverride,
|
||||
exitDecision,
|
||||
readRouterState,
|
||||
} from './enforce-hook-helpers.mjs';
|
||||
|
||||
const RULE_KEY = 'classifier-mismatch';
|
||||
// Lowered 2026-05-28 (Task 4, brain-retro #10): 0.8 was too high — 0%
|
||||
// single-node-skill follow-through. 0.6 catches more borderline cases.
|
||||
// Inline router-skip escape hatch (50+ chars) mitigates friction.
|
||||
const CONFIDENCE_THRESHOLD = 0.6;
|
||||
const ROUTER_SKIP_RE = /^router-skip:\s*(.{50,})$/m;
|
||||
|
||||
const MUTATING_TOOLS = new Set(['Edit', 'Write', 'MultiEdit', 'NotebookEdit', 'Bash', 'Task', 'Agent']);
|
||||
|
||||
/** Normalize a node id: strip "superpowers:" / "skill:" prefix; allow #ID. */
|
||||
function normalizeNode(s) {
|
||||
if (typeof s !== 'string') return '';
|
||||
return s.toLowerCase().replace(/^skill:/, '').replace(/^superpowers:/, '');
|
||||
}
|
||||
|
||||
function nodeMatches(recommendation, toolUse) {
|
||||
if (!recommendation || !toolUse) return false;
|
||||
const rec = normalizeNode(recommendation);
|
||||
if (!rec) return false;
|
||||
// Hole 5 fix: exact match OR matching last segment after ':' / '#'.
|
||||
// No generic substring (would match meta-planning to planning).
|
||||
const matches = (candidate) => {
|
||||
if (!candidate) return false;
|
||||
if (candidate === rec) return true;
|
||||
const recSegs = rec.split(/[:#]/);
|
||||
const canSegs = candidate.split(/[:#]/);
|
||||
const recLast = recSegs[recSegs.length - 1];
|
||||
const canLast = canSegs[canSegs.length - 1];
|
||||
return recLast === canLast;
|
||||
};
|
||||
if (toolUse.name === 'Skill') {
|
||||
return matches(normalizeNode(String(toolUse.input && toolUse.input.skill || '')));
|
||||
}
|
||||
if (toolUse.name === 'Task' || toolUse.name === 'Agent') {
|
||||
return matches(String(toolUse.input && toolUse.input.subagent_type || '').toLowerCase());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function decide({ toolUses, recommendation, confidence, assistantText, override }) {
|
||||
// Pure conversation: skip.
|
||||
const hasMutating = toolUses.some((u) => MUTATING_TOOLS.has(u.name));
|
||||
if (!hasMutating) return { block: false };
|
||||
if (override) return { block: false };
|
||||
|
||||
if (!recommendation) return { block: false };
|
||||
if (typeof confidence === 'number' && confidence < CONFIDENCE_THRESHOLD) return { block: false };
|
||||
|
||||
const matched = toolUses.some((u) => nodeMatches(recommendation, u));
|
||||
if (matched) return { block: false };
|
||||
|
||||
// Inline override: "router-skip: <50+ chars justification>" in assistant text.
|
||||
if (typeof assistantText === 'string' && ROUTER_SKIP_RE.test(assistantText)) {
|
||||
return { block: false };
|
||||
}
|
||||
|
||||
return {
|
||||
block: true,
|
||||
message: [
|
||||
`[enforce-classifier-match] Classifier recommended "${recommendation}" (confidence=${confidence ?? 'n/a'}) but turn did not invoke that skill/node.`,
|
||||
`Either:`,
|
||||
` - Invoke ${recommendation} via Skill / Task tool, OR`,
|
||||
` - Add an explicit "router-skip: <reason 50+ chars>" line in your response, OR`,
|
||||
` - Include "без скилов" / "direct ok" in the next user prompt.`,
|
||||
].join('\n'),
|
||||
};
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const raw = await readStdin();
|
||||
const event = parseEventJson(raw);
|
||||
const transcript = readTranscript(event.transcript_path);
|
||||
const userPrompt = lastUserPromptText(transcript);
|
||||
const override = findOverride(userPrompt, RULE_KEY);
|
||||
if (override) logOverride(RULE_KEY, override, event.session_id);
|
||||
|
||||
const state = readRouterState(event.session_id);
|
||||
const cls = state && state.classification;
|
||||
let recommendation = cls && (cls.recommended_node || cls.recommendedNode);
|
||||
const confidence = cls && typeof cls.confidence === 'number' ? cls.confidence : null;
|
||||
// Hole 4 fix: fall back to triggers_matched[0] when classifier silent.
|
||||
// Confidence stays null in fallback path — decide() accepts null (only
|
||||
// numeric confidence ≥ CONFIDENCE_THRESHOLD (0.6) blocks the rule).
|
||||
if (!recommendation) {
|
||||
const triggers = (cls && cls.triggers_matched) || [];
|
||||
if (Array.isArray(triggers) && triggers.length > 0 && typeof triggers[0] === 'string' && triggers[0].length > 0) {
|
||||
recommendation = triggers[0];
|
||||
}
|
||||
}
|
||||
const toolUses = turnToolUses(transcript);
|
||||
const assistantText = lastAssistantText(transcript);
|
||||
|
||||
const result = decide({ toolUses, recommendation, confidence, assistantText, override });
|
||||
exitDecision(result);
|
||||
} catch {
|
||||
exitDecision({ block: false });
|
||||
}
|
||||
}
|
||||
|
||||
const isCli = process.argv[1] && process.argv[1].replace(/\\/g, '/').endsWith('/enforce-classifier-match.mjs');
|
||||
if (isCli) main();
|
||||
@@ -1,268 +0,0 @@
|
||||
// Task 4: threshold 0.8→0.6 + inline router-skip override
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide } from './enforce-classifier-match.mjs';
|
||||
|
||||
describe('enforce-classifier-match / decide', () => {
|
||||
it('allows pure conversation (no mutating tools)', () => {
|
||||
expect(decide({
|
||||
toolUses: [{ name: 'Read' }],
|
||||
recommendation: 'superpowers:writing-plans',
|
||||
confidence: 0.9,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
it('allows when no recommendation', () => {
|
||||
expect(decide({
|
||||
toolUses: [{ name: 'Edit', input: {} }],
|
||||
recommendation: null,
|
||||
confidence: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
it('allows when confidence below threshold', () => {
|
||||
expect(decide({
|
||||
toolUses: [{ name: 'Edit', input: {} }],
|
||||
recommendation: 'superpowers:writing-plans',
|
||||
confidence: 0.5,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Task 4 (2026-05-28): threshold lowered 0.8 → 0.6 (brain-retro #10: 0% follow-through).
|
||||
// Flipped from the old 0.8-threshold contract: 0.7 and 0.75 NOW BLOCK (above 0.6).
|
||||
it('BLOCKS when confidence exactly 0.7 (above new threshold 0.6)', () => {
|
||||
expect(decide({
|
||||
toolUses: [{ name: 'Edit', input: {} }],
|
||||
recommendation: 'superpowers:writing-plans',
|
||||
confidence: 0.7,
|
||||
}).block).toBe(true);
|
||||
});
|
||||
|
||||
it('BLOCKS when confidence 0.75 (above new threshold 0.6)', () => {
|
||||
expect(decide({
|
||||
toolUses: [{ name: 'Edit', input: {} }],
|
||||
recommendation: 'superpowers:writing-plans',
|
||||
confidence: 0.75,
|
||||
}).block).toBe(true);
|
||||
});
|
||||
|
||||
it('blocks when recommendation high-confidence + no matching tool', () => {
|
||||
const r = decide({
|
||||
toolUses: [{ name: 'Edit', input: { file_path: 'x.mjs' } }],
|
||||
recommendation: 'superpowers:writing-plans',
|
||||
confidence: 0.9,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.message).toMatch(/writing-plans/);
|
||||
});
|
||||
|
||||
it('allows when Skill tool invoked with matching name', () => {
|
||||
const r = decide({
|
||||
toolUses: [
|
||||
{ name: 'Skill', input: { skill: 'superpowers:writing-plans' } },
|
||||
{ name: 'Edit', input: { file_path: 'x.mjs' } },
|
||||
],
|
||||
recommendation: 'superpowers:writing-plans',
|
||||
confidence: 0.9,
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
|
||||
it('matches normalized name without superpowers: prefix', () => {
|
||||
const r = decide({
|
||||
toolUses: [
|
||||
{ name: 'Skill', input: { skill: 'writing-plans' } },
|
||||
{ name: 'Edit', input: {} },
|
||||
],
|
||||
recommendation: 'superpowers:writing-plans',
|
||||
confidence: 0.9,
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
|
||||
it('matches Task subagent', () => {
|
||||
const r = decide({
|
||||
toolUses: [
|
||||
{ name: 'Task', input: { subagent_type: 'rls-reviewer' } },
|
||||
{ name: 'Edit', input: {} },
|
||||
],
|
||||
recommendation: 'rls-reviewer',
|
||||
confidence: 0.85,
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
|
||||
it('blocks (not allows) when only "override:" in assistant text — self-override removed (hole 1)', () => {
|
||||
const r = decide({
|
||||
toolUses: [{ name: 'Edit', input: {} }],
|
||||
recommendation: 'foo:bar',
|
||||
confidence: 0.9,
|
||||
assistantText: 'override: simpler direct edit, foo:bar overkill here\n',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
|
||||
it('blocks when assistant text has "override: reason" but user prompt has no override phrase (hole 1)', () => {
|
||||
const r = decide({
|
||||
toolUses: [{ name: 'Edit', input: {} }],
|
||||
recommendation: 'superpowers:writing-plans',
|
||||
confidence: 0.9,
|
||||
assistantText: 'override: just doing it quick',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
|
||||
it('allows when override phrase present', () => {
|
||||
const r = decide({
|
||||
toolUses: [{ name: 'Edit', input: {} }],
|
||||
recommendation: 'foo:bar',
|
||||
confidence: 0.9,
|
||||
override: { phrase: 'direct ok', suppresses: ['classifier-mismatch'] },
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
|
||||
it('blocks when Task subagent is spawned without matching recommendation (hole 2)', () => {
|
||||
const r = decide({
|
||||
toolUses: [{ name: 'Task', input: { subagent_type: 'general-purpose', prompt: 'do stuff' } }],
|
||||
recommendation: 'superpowers:writing-plans',
|
||||
confidence: 0.9,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
|
||||
it('does NOT block when Task subagent matches recommendation (regression — Task should count as match when right type)', () => {
|
||||
const r = decide({
|
||||
toolUses: [{ name: 'Task', input: { subagent_type: 'writing-plans', prompt: '...' } }],
|
||||
recommendation: 'writing-plans',
|
||||
confidence: 0.9,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
|
||||
it('does not match meta-planning to planning recommendation (hole 5)', () => {
|
||||
const r = decide({
|
||||
toolUses: [{ name: 'Skill', input: { skill: 'meta-planning' } }, { name: 'Edit', input: {} }],
|
||||
recommendation: 'planning',
|
||||
confidence: 0.9,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
|
||||
it('matches superpowers:writing-plans to writing-plans recommendation (regression — keep working)', () => {
|
||||
expect(decide({
|
||||
toolUses: [{ name: 'Skill', input: { skill: 'superpowers:writing-plans' } }, { name: 'Edit', input: {} }],
|
||||
recommendation: 'writing-plans',
|
||||
confidence: 0.9,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
it('matches exact-name skill regression — keep working', () => {
|
||||
expect(decide({
|
||||
toolUses: [{ name: 'Skill', input: { skill: 'brainstorming' } }, { name: 'Edit', input: {} }],
|
||||
recommendation: 'brainstorming',
|
||||
confidence: 0.9,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// hole 4: triggers_matched fallback — decide() contract test
|
||||
it('blocks when recommendation comes from triggers_matched fallback (hole 4, null confidence)', () => {
|
||||
const r = decide({
|
||||
toolUses: [{ name: 'Edit', input: {} }],
|
||||
recommendation: 'superpowers:writing-plans', // would-be from triggers_matched[0]
|
||||
confidence: null, // no LLM, but triggers present
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('inline router-skip override (Task 4)', () => {
|
||||
const recommendation = '#19';
|
||||
const editTool = { name: 'Edit', input: { file_path: 'x.txt' } };
|
||||
|
||||
it('does NOT block when assistant text contains "router-skip: <50+ chars>"', () => {
|
||||
const assistantText = 'router-skip: deliberately choosing direct because router recommendation #19 is irrelevant for this trivial typo fix in docs';
|
||||
const result = decide({
|
||||
toolUses: [editTool],
|
||||
recommendation,
|
||||
confidence: 0.85,
|
||||
assistantText,
|
||||
override: null,
|
||||
});
|
||||
expect(result.block).toBe(false);
|
||||
});
|
||||
|
||||
it('DOES block when "router-skip:" justification < 50 chars', () => {
|
||||
const assistantText = 'router-skip: too short';
|
||||
const result = decide({
|
||||
toolUses: [editTool],
|
||||
recommendation,
|
||||
confidence: 0.85,
|
||||
assistantText,
|
||||
override: null,
|
||||
});
|
||||
expect(result.block).toBe(true);
|
||||
});
|
||||
|
||||
it('DOES block when no "router-skip:" present at all', () => {
|
||||
const result = decide({
|
||||
toolUses: [editTool],
|
||||
recommendation,
|
||||
confidence: 0.85,
|
||||
assistantText: 'just normal text, no skip',
|
||||
override: null,
|
||||
});
|
||||
expect(result.block).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('lowered confidence threshold (Task 4: 0.8 → 0.6)', () => {
|
||||
const recommendation = '#19';
|
||||
const editTool = { name: 'Edit', input: { file_path: 'x.txt' } };
|
||||
|
||||
it('blocks at confidence 0.65 (above new threshold 0.6)', () => {
|
||||
const result = decide({
|
||||
toolUses: [editTool],
|
||||
recommendation,
|
||||
confidence: 0.65,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(result.block).toBe(true);
|
||||
});
|
||||
|
||||
it('does NOT block at confidence 0.55 (below new threshold 0.6)', () => {
|
||||
const result = decide({
|
||||
toolUses: [editTool],
|
||||
recommendation,
|
||||
confidence: 0.55,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(result.block).toBe(false);
|
||||
});
|
||||
|
||||
it('still blocks at confidence 0.85 without router-skip (above threshold, no escape)', () => {
|
||||
const result = decide({
|
||||
toolUses: [editTool],
|
||||
recommendation,
|
||||
confidence: 0.85,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(result.block).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* enforce-decomposition-detector — PreToolUse wrapper around the pure
|
||||
* decomposition-detector module (router-gate v4 §3.8 + v4.1 Direction 3).
|
||||
*
|
||||
* Catches features secretly decomposed into 3+ small prompts with overlapping
|
||||
* keywords WITHOUT a planning skill (writing-plans / brainstorming) ever
|
||||
* being invoked. v4.1 hard-blocks mutating tools when LLM-judge confirms.
|
||||
*
|
||||
* Stream H Task 5 — adds the wrapper. Pure detection + decision logic live
|
||||
* in decomposition-detector.mjs; this file is just the hook entry point.
|
||||
*
|
||||
* Settings.json registration deferred to Phase H-α/H-β batch step.
|
||||
*/
|
||||
import { detectDecompositionCandidate, decideDecomposition, V4_1_DECOMP_THRESHOLD } from './decomposition-detector.mjs';
|
||||
|
||||
/**
|
||||
* Pure decision composing detector + decider with a degraded-allow fallback
|
||||
* when the LLM verdict is missing (fail-open on the LLM layer — matches the
|
||||
* same pattern as llm-judge-per-tool).
|
||||
*
|
||||
* @param {object} args
|
||||
* @param {Array} args.history - prior prompt entries (oldest → newest)
|
||||
* @param {object} args.currentEntry - the current prompt entry
|
||||
* @param {string|null} args.llmVerdict - 'YES' | 'NO' | null
|
||||
* @param {object} [args.threshold] - override the v4.1 thresholds
|
||||
* @returns {{action:'allow'|'soft_flag'|'hard_block_mutating', reason?:string, degraded?:boolean}}
|
||||
*/
|
||||
export function decide({ history, currentEntry, llmVerdict, threshold = V4_1_DECOMP_THRESHOLD }) {
|
||||
const candidate = detectDecompositionCandidate(history, currentEntry, threshold);
|
||||
if (!candidate.candidate) return { action: 'allow' };
|
||||
if (llmVerdict === null || llmVerdict === undefined) {
|
||||
// Threshold met but no LLM verdict available — degrade to soft surface
|
||||
// rather than hard-block (avoid the Stream G Task 8 self-lockout pattern
|
||||
// where a fail-CLOSE LLM hook bricks the session).
|
||||
return { action: 'soft_flag', reason: `${candidate.reason} (LLM judge unavailable — degraded allow)`, degraded: true };
|
||||
}
|
||||
return decideDecomposition(candidate, llmVerdict, threshold);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// Minimal main(): without an active LLM-judge config + history-ledger reader,
|
||||
// this hook degrades to allow-with-soft-flag. Wiring full live behaviour is
|
||||
// Phase H-α/H-β tail work (LLM judge config from Stream D, history ledger
|
||||
// from observer Stop hook). Until then: exit 0 silently to avoid lockout.
|
||||
let input = '';
|
||||
for await (const chunk of process.stdin) input += chunk;
|
||||
// Intentionally no decode/parse — the hook is a no-op until history-ledger
|
||||
// + LLM-judge config are wired in the deferred batch.
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1].replace(/\\/g, '/')}` || (process.argv[1] || '').endsWith('enforce-decomposition-detector.mjs')) {
|
||||
main().catch(() => process.exit(0));
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
// tools/enforce-decomposition-detector.test.mjs
|
||||
// Stream H Task 5 (H6) — wrapper tests around the pure decomposition-detector module.
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide } from './enforce-decomposition-detector.mjs';
|
||||
|
||||
describe('enforce-decomposition-detector wrapper (Stream H Task 5)', () => {
|
||||
it('allows when history is empty', () => {
|
||||
const r = decide({
|
||||
history: [],
|
||||
currentEntry: { primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 1 },
|
||||
llmVerdict: 'NO',
|
||||
});
|
||||
expect(r.action).toBe('allow');
|
||||
});
|
||||
|
||||
it('allows when overlap below threshold (only 2 prompts share keywords)', () => {
|
||||
const history = [
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 1 },
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 2 },
|
||||
];
|
||||
const r = decide({
|
||||
history,
|
||||
currentEntry: { primary_keywords: ['unrelated', 'topic', 'words'], skill_invoked_this_prompt: false, prompt_idx: 3 },
|
||||
llmVerdict: 'YES',
|
||||
});
|
||||
expect(r.action).toBe('allow');
|
||||
});
|
||||
|
||||
it('hard_block_mutating when 3+ overlap, no skill, LLM YES (v4.1)', () => {
|
||||
const history = [
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 1 },
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 2 },
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 3 },
|
||||
];
|
||||
const r = decide({
|
||||
history,
|
||||
currentEntry: { primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 4 },
|
||||
llmVerdict: 'YES',
|
||||
});
|
||||
expect(r.action).toBe('hard_block_mutating');
|
||||
expect(r.reason).toMatch(/decomp/i);
|
||||
});
|
||||
|
||||
it('soft_flag when threshold met but LLM verdict NO (legit-distinct)', () => {
|
||||
const history = [
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 1 },
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 2 },
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 3 },
|
||||
];
|
||||
const r = decide({
|
||||
history,
|
||||
currentEntry: { primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 4 },
|
||||
llmVerdict: 'NO',
|
||||
});
|
||||
expect(r.action).toBe('soft_flag');
|
||||
});
|
||||
|
||||
it('allows when threshold met but a writing-plans skill was invoked', () => {
|
||||
const history = [
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: true, prompt_idx: 1 },
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 2 },
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 3 },
|
||||
];
|
||||
const r = decide({
|
||||
history,
|
||||
currentEntry: { primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 4 },
|
||||
llmVerdict: 'YES',
|
||||
});
|
||||
expect(r.action).toBe('allow');
|
||||
});
|
||||
|
||||
it('degraded allow when LLM verdict is missing/null (fail-open on LLM layer)', () => {
|
||||
const history = [
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 1 },
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 2 },
|
||||
{ primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 3 },
|
||||
];
|
||||
const r = decide({
|
||||
history,
|
||||
currentEntry: { primary_keywords: ['feature', 'login', 'form'], skill_invoked_this_prompt: false, prompt_idx: 4 },
|
||||
llmVerdict: null,
|
||||
});
|
||||
expect(r.action).toBe('soft_flag');
|
||||
expect(r.degraded).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,140 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Rule — Graph-first enforce.
|
||||
*
|
||||
* Stop hook. Enforces CLAUDE.md §5 п.14:
|
||||
* «перед открытым codebase-вопросом сначала /graphify query, потом Read/Grep/Glob»
|
||||
*
|
||||
* When the controller performs >= THRESHOLD Grep/Glob searches in a single turn
|
||||
* WITHOUT having invoked graphify, this hook blocks turn-end with remediation
|
||||
* instructions.
|
||||
*
|
||||
* Three escape hatches:
|
||||
* 1. Invoke /graphify query via Skill tool (or graphifyy CLI via Bash).
|
||||
* 2. Write «graph-skip: <non-empty reason>» on a line in the assistant text.
|
||||
* 3. User prompt contains a global override phrase (vocab-driven).
|
||||
*
|
||||
* Spec: CLAUDE.md §5 п.14 (v2.33), ADR-017.
|
||||
*/
|
||||
|
||||
import {
|
||||
readStdin,
|
||||
parseEventJson,
|
||||
readTranscript,
|
||||
lastUserPromptText,
|
||||
lastAssistantText,
|
||||
turnToolUses,
|
||||
findOverride,
|
||||
logOverride,
|
||||
exitDecision,
|
||||
} from './enforce-hook-helpers.mjs';
|
||||
|
||||
const RULE_KEY = 'graph-first';
|
||||
const THRESHOLD = 3;
|
||||
const SEARCH_TOOLS = new Set(['Grep', 'Glob']);
|
||||
|
||||
/**
|
||||
* Regex for inline escape hatch:
|
||||
* «graph-skip: <one-line non-empty reason>»
|
||||
*
|
||||
* Requirements:
|
||||
* - Must start at the beginning of a line (^, multiline flag).
|
||||
* - Must have «graph-skip: » prefix followed by \S+ (at least one non-whitespace char).
|
||||
* - Whitespace-only or empty reason → does NOT match → remains blocked.
|
||||
*/
|
||||
const GRAPH_SKIP_RE = /^graph-skip:\s*\S+/m;
|
||||
|
||||
/**
|
||||
* Pure decision function — no I/O.
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {Array<{name: string, input: object}>} params.toolUses - All tool uses in this turn.
|
||||
* @param {boolean} params.graphifyInvoked - True if graphify was invoked this turn.
|
||||
* @param {string} params.assistantText - Full assistant text for this turn.
|
||||
* @param {object|null} params.override - Truthy if user prompt contained a valid override phrase.
|
||||
* @returns {{ block: boolean, message?: string }}
|
||||
*/
|
||||
export function decide({ toolUses, graphifyInvoked, assistantText, override }) {
|
||||
// Step 1: Global override → pass.
|
||||
if (override) return { block: false };
|
||||
|
||||
// Step 2: Graphify already consulted → pass.
|
||||
if (graphifyInvoked) return { block: false };
|
||||
|
||||
// Step 3: Count Grep + Glob tool uses.
|
||||
const searchCount = Array.isArray(toolUses)
|
||||
? toolUses.filter((u) => u && SEARCH_TOOLS.has(u.name)).length
|
||||
: 0;
|
||||
|
||||
// Step 4: Below threshold → pass. §5 п.14 «узкий regex-поиск» exception.
|
||||
if (searchCount < THRESHOLD) return { block: false };
|
||||
|
||||
// Step 5: Inline graph-skip escape hatch with non-empty reason → pass.
|
||||
if (typeof assistantText === 'string' && GRAPH_SKIP_RE.test(assistantText)) {
|
||||
return { block: false };
|
||||
}
|
||||
|
||||
// Step 6: Block.
|
||||
const message = [
|
||||
`[enforce-graph-first] За turn выполнено ${searchCount} Grep/Glob поисков без вызова graphify (CLAUDE.md §5 п.14: «перед открытым codebase-вопросом сначала /graphify query, потом Read/Grep/Glob»).`,
|
||||
`Сделай ОДНО из трёх в следующем ответе:`,
|
||||
` 1. Позови /graphify query «<вопрос>» через Skill tool, потом Read/Grep по найденным узлам.`,
|
||||
` 2. Добавь строку «graph-skip: <одна строка причины>» (e.g. «graph-skip: узкий regex по литералу CONFIDENCE_THRESHOLD»).`,
|
||||
` 3. Попроси у пользователя глобальный override (без скилов / direct ok / срочно / быстрый коммит / recovery / memory dump / ремонт инфраструктуры).`,
|
||||
].join('\n');
|
||||
|
||||
return { block: true, message };
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if graphify was invoked in any tool use of the turn.
|
||||
*
|
||||
* Matches:
|
||||
* - Skill tool with input.skill containing «graphify» (case-insensitive substring).
|
||||
* - Bash tool with input.command matching /\bgraphifyy?\b/i (CLI name is «graphifyy»,
|
||||
* also catches «graphify» for slash-command-rendered bash).
|
||||
* - SlashCommand tool (if present) with input.command containing «graphify».
|
||||
*/
|
||||
export function detectGraphifyInvoked(toolUses) {
|
||||
if (!Array.isArray(toolUses)) return false;
|
||||
for (const u of toolUses) {
|
||||
if (!u || !u.name) continue;
|
||||
if (u.name === 'Skill') {
|
||||
const skill = String((u.input && u.input.skill) || '');
|
||||
if (/graphify/i.test(skill)) return true;
|
||||
}
|
||||
if (u.name === 'Bash') {
|
||||
const cmd = String((u.input && u.input.command) || '');
|
||||
if (/\bgraphifyy?\b/i.test(cmd)) return true;
|
||||
}
|
||||
if (u.name === 'SlashCommand') {
|
||||
const cmd = String((u.input && u.input.command) || '');
|
||||
if (/graphify/i.test(cmd)) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const raw = await readStdin();
|
||||
const event = parseEventJson(raw);
|
||||
const transcript = readTranscript(event.transcript_path);
|
||||
const userPrompt = lastUserPromptText(transcript);
|
||||
const assistantText = lastAssistantText(transcript);
|
||||
const toolUses = turnToolUses(transcript);
|
||||
|
||||
const graphifyInvoked = detectGraphifyInvoked(toolUses);
|
||||
const override = findOverride(userPrompt, RULE_KEY);
|
||||
if (override) logOverride(RULE_KEY, override, event.session_id);
|
||||
|
||||
const result = decide({ toolUses, graphifyInvoked, assistantText, override });
|
||||
exitDecision(result);
|
||||
} catch {
|
||||
// Fail-quiet: never block on internal error.
|
||||
exitDecision({ block: false });
|
||||
}
|
||||
}
|
||||
|
||||
const isCli = process.argv[1] && process.argv[1].replace(/\\/g, '/').endsWith('/enforce-graph-first.mjs');
|
||||
if (isCli) main();
|
||||
@@ -1,209 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide } from './enforce-graph-first.mjs';
|
||||
|
||||
// Shared helpers
|
||||
const GREP_TOOL = { name: 'Grep', input: { pattern: 'foo' } };
|
||||
const GLOB_TOOL = { name: 'Glob', input: { pattern: '**/*.ts' } };
|
||||
const READ_TOOL = { name: 'Read', input: { file_path: 'x.ts' } };
|
||||
const EDIT_TOOL = { name: 'Edit', input: { file_path: 'x.mjs' } };
|
||||
const BASH_TOOL = { name: 'Bash', input: { command: 'ls -la' } };
|
||||
|
||||
describe('enforce-graph-first / decide', () => {
|
||||
// Test 1: No searches → pass
|
||||
it('no searches at all → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [EDIT_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 2: Below threshold (2 searches) → pass
|
||||
it('below threshold (2 Grep searches) → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 3: 3 searches, no graphify, no override → block
|
||||
it('3 Grep searches, no graphify, no override → block', () => {
|
||||
const r = decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.message).toMatch(/3/);
|
||||
expect(r.message).toMatch(/graphify/i);
|
||||
expect(r.message).toMatch(/graph-skip:/);
|
||||
});
|
||||
|
||||
// Test 4: 5 searches but graphifyInvoked: true → pass
|
||||
it('5 searches but graphifyInvoked: true → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: true,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 5: 3 searches with valid graph-skip line → pass
|
||||
it('3 searches with valid graph-skip line → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: 'graph-skip: узкий regex по литералу X\nдалее обычный ответ...',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 6: 3 searches with empty graph-skip reason → block
|
||||
it('3 searches with graph-skip: but empty reason → block', () => {
|
||||
expect(decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: 'graph-skip:\n',
|
||||
override: null,
|
||||
}).block).toBe(true);
|
||||
});
|
||||
|
||||
// Test 7: 3 searches with global override → pass
|
||||
it('3 searches with global override → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: { phrase: 'срочно', suppresses: ['graph-first'] },
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 8: Mixed Grep + Glob count toward threshold → block
|
||||
it('1 Grep + 2 Glob = 3 → block (mixed counts toward threshold)', () => {
|
||||
const r = decide({
|
||||
toolUses: [GREP_TOOL, GLOB_TOOL, GLOB_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
|
||||
// Test 9: Other tools (Read, Edit, Bash) don't count as searches → pass
|
||||
it('Read × 4 + Edit × 1 = 0 searches → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [READ_TOOL, READ_TOOL, READ_TOOL, READ_TOOL, EDIT_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
// Test 10: Message includes per-spec wording
|
||||
it('block message includes §5 п.14, graphify, graph-skip: wording', () => {
|
||||
const r = decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.message).toMatch(/§5 п\.14/);
|
||||
expect(r.message).toMatch(/graphify/i);
|
||||
expect(r.message).toMatch(/graph-skip:/);
|
||||
});
|
||||
|
||||
// Extra edge cases
|
||||
|
||||
it('exactly THRESHOLD=3 searches → block (boundary condition)', () => {
|
||||
expect(decide({
|
||||
toolUses: [GREP_TOOL, GLOB_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(true);
|
||||
});
|
||||
|
||||
it('2 searches (below threshold) regardless of graphify state → pass', () => {
|
||||
// Even without graphify, 2 searches is under the threshold
|
||||
expect(decide({
|
||||
toolUses: [GREP_TOOL, GLOB_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
it('graph-skip: with non-empty reason in middle of text → pass', () => {
|
||||
const text = 'Some analysis first.\ngraph-skip: known file path, not cross-cutting\nThen conclusion.';
|
||||
expect(decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: text,
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
it('graph-skip: with only whitespace reason (not \\ S+) → block', () => {
|
||||
expect(decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: 'graph-skip: \n',
|
||||
override: null,
|
||||
}).block).toBe(true);
|
||||
});
|
||||
|
||||
it('empty toolUses → pass', () => {
|
||||
expect(decide({
|
||||
toolUses: [],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
it('Bash tool alone does not count as search', () => {
|
||||
expect(decide({
|
||||
toolUses: [BASH_TOOL, BASH_TOOL, BASH_TOOL, BASH_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
}).block).toBe(false);
|
||||
});
|
||||
|
||||
it('block message includes the actual count N', () => {
|
||||
const r = decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.message).toMatch(/5/);
|
||||
});
|
||||
|
||||
it('override null value → treated as falsy, block still fires', () => {
|
||||
const r = decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
|
||||
it('override false value → treated as falsy, block still fires', () => {
|
||||
const r = decide({
|
||||
toolUses: [GREP_TOOL, GREP_TOOL, GREP_TOOL],
|
||||
graphifyInvoked: false,
|
||||
assistantText: '',
|
||||
override: false,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/**
|
||||
* Shared helpers for the 10-rule enforcement hook layer.
|
||||
*
|
||||
* Spec: docs/superpowers/specs/2026-05-25-enforce-hard-rules-design.md
|
||||
@@ -101,6 +101,17 @@ export function lastTurnEntries(entries) {
|
||||
if (!Array.isArray(entries) || entries.length === 0) return [];
|
||||
for (let i = entries.length - 1; i >= 0; i--) {
|
||||
const e = entries[i];
|
||||
// Sibling-session find 2026-05-30: harness-injected skill bodies arrive as
|
||||
// role:'user' messages with isMeta:true AND a top-level sourceToolUseID
|
||||
// linking them back to the originating Skill tool_use. Treating them as
|
||||
// turn boundaries hides both the user's real prompt (breaks coverage
|
||||
// detection) and the Skill tool_use (breaks detectLegitSkillActive in
|
||||
// enforce-normative-content-rules). Skip ONLY this exact shape — other
|
||||
// isMeta:true messages (auto-resume "Continue from where you left off.",
|
||||
// Stop hook feedback, local-command-caveat wrappers) remain valid
|
||||
// boundaries. Discriminator field sourceToolUseID is harness-controlled
|
||||
// and not writable by controller from inside a tool call.
|
||||
if (e && e.isMeta === true && typeof e.sourceToolUseID === 'string') continue;
|
||||
if (e && e.message && e.message.role === 'user') {
|
||||
const c = e.message.content;
|
||||
if (typeof c === 'string' && c.trim().length > 0) return entries.slice(i);
|
||||
@@ -193,61 +204,21 @@ export function turnToolResults(entries) {
|
||||
return results;
|
||||
}
|
||||
|
||||
let _vocabCache = null;
|
||||
export function loadOverrideVocab(path) {
|
||||
if (_vocabCache) return _vocabCache;
|
||||
try {
|
||||
const p = path || join(__dirname, 'enforce-override-vocab.json');
|
||||
if (!existsSync(p)) return { phrases: [] };
|
||||
_vocabCache = JSON.parse(readFileSync(p, 'utf-8'));
|
||||
return _vocabCache;
|
||||
} catch { return { phrases: [] }; }
|
||||
// v4 stubs — universal vocab override surface removed per spec §4.2.
|
||||
// Keep symbols exported so callers in other hooks compile; runtime returns null/empty.
|
||||
export function loadOverrideVocab(_path) {
|
||||
return { phrases: [] };
|
||||
}
|
||||
|
||||
export function _resetVocabCache() { _vocabCache = null; }
|
||||
export function _resetVocabCache() { /* no-op, vocab disabled */ }
|
||||
|
||||
export function findOverride(userPrompt, ruleKey, vocab) {
|
||||
if (!userPrompt || typeof userPrompt !== 'string') return null;
|
||||
const v = vocab || loadOverrideVocab();
|
||||
const lo = userPrompt.toLowerCase();
|
||||
for (const p of v.phrases || []) {
|
||||
if (!p.phrase || !Array.isArray(p.suppresses)) continue;
|
||||
if (!lo.includes(p.phrase.toLowerCase())) continue;
|
||||
if (!p.suppresses.includes(ruleKey)) continue;
|
||||
if (p.requires_justification) {
|
||||
// Hole 7 fix: master overrides require a line "<prefix> <non-empty>"
|
||||
// in the same prompt documenting what is being repaired.
|
||||
const prefix = p.requires_justification.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const re = new RegExp(prefix + '\\s+(\\S[^\\n]*)', 'i');
|
||||
const m = userPrompt.match(re);
|
||||
if (!m || !m[1] || !m[1].trim()) continue;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
export function findOverride(_userPrompt, _ruleKey, _vocab) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Diagnostic variant: returns phrase object if substring matches AND rule
|
||||
* applies, regardless of justification presence. Use ONLY for error-message
|
||||
* generation in hooks — never to grant suppression.
|
||||
*
|
||||
* Fixes silent-reject bug where users see "no verification artifact" while
|
||||
* having typed the override phrase but missing the justification line.
|
||||
*/
|
||||
export function findOverrideAttempt(userPrompt, ruleKey, vocab) {
|
||||
if (!userPrompt || typeof userPrompt !== 'string') return null;
|
||||
const v = vocab || loadOverrideVocab();
|
||||
const lo = userPrompt.toLowerCase();
|
||||
for (const p of v.phrases || []) {
|
||||
if (!p.phrase || !Array.isArray(p.suppresses)) continue;
|
||||
if (!lo.includes(p.phrase.toLowerCase())) continue;
|
||||
if (!p.suppresses.includes(ruleKey)) continue;
|
||||
return p;
|
||||
}
|
||||
export function findOverrideAttempt(_userPrompt, _ruleKey, _vocab) {
|
||||
return null;
|
||||
}
|
||||
|
||||
export function logHookOutcome(ruleKey, outcome, sessionId) {
|
||||
try {
|
||||
const f = join(runtimeDir(), 'hook-outcomes.jsonl');
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { mkdtempSync, writeFileSync, rmSync, existsSync, readFileSync } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
@@ -25,6 +25,25 @@ import {
|
||||
runtimeDir,
|
||||
} from './enforce-hook-helpers.mjs';
|
||||
|
||||
// v4: override surface removed per spec §4.2 — stubs return null/empty
|
||||
describe('v4 override stubs', () => {
|
||||
it('loadOverrideVocab returns empty phrases array (stub)', () => {
|
||||
_resetVocabCache();
|
||||
expect(loadOverrideVocab()).toEqual({ phrases: [] });
|
||||
});
|
||||
it('findOverride always returns null (vocab removed in v4)', () => {
|
||||
_resetVocabCache();
|
||||
expect(findOverride('срочно: ремонт', 'verify-before-push')).toBe(null);
|
||||
expect(findOverride('memory dump fix it now', 'memory-coverage')).toBe(null);
|
||||
expect(findOverride('', 'anything')).toBe(null);
|
||||
});
|
||||
it('findOverrideAttempt always returns null (vocab removed in v4)', () => {
|
||||
_resetVocabCache();
|
||||
expect(findOverrideAttempt('срочно push it', 'verify-before-push')).toBe(null);
|
||||
expect(findOverrideAttempt('', 'anything')).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('logHookOutcome', () => {
|
||||
const ledgerPath = () => join(runtimeDir(), 'hook-outcomes.jsonl');
|
||||
|
||||
@@ -173,130 +192,155 @@ describe('lastTurnEntries / lastUserPromptText / lastAssistantText / turnToolUse
|
||||
];
|
||||
expect(lastUserPromptText(eps)).toBe('hello\n world');
|
||||
});
|
||||
|
||||
// ── Sibling-session find 2026-05-30 ──
|
||||
// Skill bodies are harness-injected as role:'user' messages with isMeta:true
|
||||
// AND a top-level sourceToolUseID linking them to the originating Skill tool_use.
|
||||
// Without skipping them, lastTurnEntries treats the skill body as the turn
|
||||
// boundary and detectLegitSkillActive (used by enforce-normative-content-rules)
|
||||
// misses the Skill tool_use that lives in the assistant message BEFORE the body.
|
||||
//
|
||||
// The discriminator MUST be (isMeta === true && typeof sourceToolUseID === 'string')
|
||||
// — NOT a blanket `skip isMeta`, because isMeta:true also appears on:
|
||||
// * "Continue from where you left off." auto-resume (no sourceToolUseID)
|
||||
// * Stop hook feedback strings (no sourceToolUseID)
|
||||
// * <local-command-caveat> wrappers (no sourceToolUseID)
|
||||
// Those are real user-equivalent boundaries and must remain visible.
|
||||
it('lastTurnEntries skips skill body injections (isMeta + sourceToolUseID)', () => {
|
||||
const eps = [
|
||||
{ message: { role: 'user', content: 'real user prompt with coverage line' } },
|
||||
{ message: { role: 'assistant', content: [
|
||||
{ type: 'text', text: 'invoking skill' },
|
||||
{ type: 'tool_use', name: 'Skill', input: { skill: 'claude-md-management:revise-claude-md' } },
|
||||
] } },
|
||||
// Harness injects skill body as if it were a user message:
|
||||
{ isMeta: true, sourceToolUseID: 'toolu_skillcall_abc', message: { role: 'user', content: [{ type: 'text', text: 'Base directory for this skill: ...' }] } },
|
||||
{ message: { role: 'assistant', content: [{ type: 'text', text: 'skill output' }] } },
|
||||
];
|
||||
const turn = lastTurnEntries(eps);
|
||||
expect(turn).toHaveLength(4); // user prompt + assistant Skill + skill-body + assistant follow-up
|
||||
expect(turn[0].message.content).toBe('real user prompt with coverage line');
|
||||
});
|
||||
|
||||
it('lastTurnEntries does NOT skip "Continue from where you left off" (isMeta but no sourceToolUseID)', () => {
|
||||
const eps = [
|
||||
{ message: { role: 'user', content: 'older user prompt that should stay outside turn' } },
|
||||
{ message: { role: 'assistant', content: [{ type: 'text', text: 'older reply' }] } },
|
||||
// Auto-resume injection — isMeta but NOT tool-spawned:
|
||||
{ isMeta: true, message: { role: 'user', content: [{ type: 'text', text: 'Continue from where you left off.' }] } },
|
||||
{ message: { role: 'assistant', content: [{ type: 'text', text: 'resumed reply' }] } },
|
||||
];
|
||||
const turn = lastTurnEntries(eps);
|
||||
expect(turn).toHaveLength(2); // the Continue message + the resumed reply (NOT the older prompt)
|
||||
const firstTextBlock = turn[0].message.content[0] || {};
|
||||
expect(firstTextBlock.text).toBe('Continue from where you left off.');
|
||||
});
|
||||
|
||||
it('turnToolUses includes Skill tool_use spawned in same turn as the injected skill body', () => {
|
||||
const eps = [
|
||||
{ message: { role: 'user', content: 'real user prompt' } },
|
||||
{ message: { role: 'assistant', content: [
|
||||
{ type: 'tool_use', name: 'Skill', input: { skill: 'claude-md-management:revise-claude-md' } },
|
||||
] } },
|
||||
{ isMeta: true, sourceToolUseID: 'toolu_skillcall_def', message: { role: 'user', content: [{ type: 'text', text: 'Base directory ...' }] } },
|
||||
{ message: { role: 'assistant', content: [
|
||||
{ type: 'text', text: 'about to edit memory' },
|
||||
{ type: 'tool_use', name: 'Write', input: { file_path: 'memory/foo.md' } },
|
||||
] } },
|
||||
];
|
||||
const uses = turnToolUses(eps);
|
||||
const names = uses.map((u) => u.name);
|
||||
expect(names).toContain('Skill');
|
||||
expect(names).toContain('Write');
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadOverrideVocab / findOverride', () => {
|
||||
let tmp;
|
||||
beforeEach(() => {
|
||||
tmp = mkdtempSync(join(tmpdir(), 'vocab-'));
|
||||
_resetVocabCache();
|
||||
});
|
||||
afterEach(() => {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
_resetVocabCache();
|
||||
describe('loadOverrideVocab / findOverride (v4 stubs)', () => {
|
||||
beforeEach(() => { _resetVocabCache(); });
|
||||
afterEach(() => { _resetVocabCache(); });
|
||||
|
||||
it('loadOverrideVocab always returns empty phrases (stub ignores path arg)', () => {
|
||||
const v = loadOverrideVocab('/any/path/vocab.json');
|
||||
expect(v.phrases).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('loads vocab from explicit path', () => {
|
||||
const p = join(tmp, 'vocab.json');
|
||||
writeFileSync(p, JSON.stringify({
|
||||
phrases: [
|
||||
{ phrase: 'без скилов', suppresses: ['skill-required'] },
|
||||
],
|
||||
}));
|
||||
const v = loadOverrideVocab(p);
|
||||
expect(v.phrases).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('findOverride matches case-insensitively', () => {
|
||||
it('findOverride always returns null regardless of vocab arg (stub)', () => {
|
||||
const v = { phrases: [{ phrase: 'СРОЧНО', suppresses: ['verify-before-push'] }] };
|
||||
expect(findOverride('очень срочно нужно', 'verify-before-push', v)).toMatchObject({ phrase: 'СРОЧНО' });
|
||||
expect(findOverride('очень срочно нужно', 'verify-before-push', v)).toBeNull();
|
||||
expect(findOverride('hello world', 'verify-before-push', v)).toBeNull();
|
||||
});
|
||||
|
||||
it('findOverride returns null if rule key not in suppresses', () => {
|
||||
it('findOverride returns null regardless of rule key (stub)', () => {
|
||||
const v = { phrases: [{ phrase: 'без скилов', suppresses: ['skill-required'] }] };
|
||||
expect(findOverride('без скилов давай', 'tdd-gate', v)).toBeNull();
|
||||
expect(findOverride('без скилов давай', 'skill-required', v)).not.toBeNull();
|
||||
expect(findOverride('без скилов давай', 'skill-required', v)).toBeNull();
|
||||
});
|
||||
|
||||
it('findOverride returns null on empty prompt / vocab', () => {
|
||||
it('findOverride returns null on empty prompt / vocab (unchanged)', () => {
|
||||
expect(findOverride('', 'x', { phrases: [] })).toBeNull();
|
||||
expect(findOverride(null, 'x', { phrases: [{ phrase: 'a', suppresses: ['x'] }] })).toBeNull();
|
||||
});
|
||||
|
||||
it('loads default vocab file when no path given (smoke)', () => {
|
||||
it('loadOverrideVocab default returns empty phrases (stub smoke)', () => {
|
||||
_resetVocabCache();
|
||||
const v = loadOverrideVocab();
|
||||
expect(Array.isArray(v.phrases)).toBe(true);
|
||||
expect(v.phrases.length).toBeGreaterThan(0);
|
||||
expect(v.phrases.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOverride — requires_justification (hole 7)', () => {
|
||||
describe('findOverride — requires_justification [v4: always null]', () => {
|
||||
const testVocab = {
|
||||
phrases: [
|
||||
{
|
||||
phrase: 'ремонт инфраструктуры',
|
||||
suppresses: ['classifier-mismatch'],
|
||||
requires_justification: 'ремонт:',
|
||||
description: 'master kill — requires justification',
|
||||
},
|
||||
],
|
||||
phrases: [{
|
||||
phrase: 'ремонт инфраструктуры',
|
||||
suppresses: ['classifier-mismatch'],
|
||||
requires_justification: 'ремонт:',
|
||||
description: 'master kill',
|
||||
}],
|
||||
};
|
||||
|
||||
it('rejects when phrase present but justification line missing (hole 7)', () => {
|
||||
const r = findOverride('ремонт инфраструктуры', 'classifier-mismatch', testVocab);
|
||||
expect(r).toBeNull();
|
||||
it('stub: null even without justification (was null before too)', () => {
|
||||
expect(findOverride('ремонт инфраструктуры', 'classifier-mismatch', testVocab)).toBeNull();
|
||||
});
|
||||
|
||||
it('accepts when justification line provides target', () => {
|
||||
const r = findOverride('ремонт инфраструктуры\nремонт: enforce-hook-helpers.mjs', 'classifier-mismatch', testVocab);
|
||||
expect(r).not.toBeNull();
|
||||
expect(r.phrase).toBe('ремонт инфраструктуры');
|
||||
it('stub: null even with valid justification (vocab removed in v4)', () => {
|
||||
expect(findOverride('ремонт инфраструктуры\nремонт: fix.mjs', 'classifier-mismatch', testVocab)).toBeNull();
|
||||
});
|
||||
|
||||
it('rejects when justification line empty after the prefix', () => {
|
||||
const r = findOverride('ремонт инфраструктуры\nремонт: ', 'classifier-mismatch', testVocab);
|
||||
expect(r).toBeNull();
|
||||
it('stub: null when justification empty (same as before, now via stub)', () => {
|
||||
expect(findOverride('ремонт инфраструктуры\nремонт: ', 'classifier-mismatch', testVocab)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOverrideAttempt — diagnostic helper (silent-reject bug fix)', () => {
|
||||
describe('findOverrideAttempt [v4: always null]', () => {
|
||||
const testVocab = {
|
||||
phrases: [
|
||||
{
|
||||
phrase: 'ремонт инфраструктуры',
|
||||
suppresses: ['verify-before-push', 'classifier-mismatch'],
|
||||
requires_justification: 'ремонт:',
|
||||
description: 'master kill — requires justification',
|
||||
},
|
||||
{
|
||||
phrase: 'срочно',
|
||||
suppresses: ['verify-before-push'],
|
||||
description: 'no justification required',
|
||||
},
|
||||
{ phrase: 'ремонт инфраструктуры', suppresses: ['verify-before-push', 'classifier-mismatch'], requires_justification: 'ремонт:', description: 'master kill' },
|
||||
{ phrase: 'срочно', suppresses: ['verify-before-push'], description: 'no justification required' },
|
||||
],
|
||||
};
|
||||
|
||||
it('returns phrase even when justification line missing (so caller can emit helpful diagnostic)', () => {
|
||||
const r = findOverrideAttempt('ремонт инфраструктуры', 'verify-before-push', testVocab);
|
||||
expect(r).not.toBeNull();
|
||||
expect(r.phrase).toBe('ремонт инфраструктуры');
|
||||
expect(r.requires_justification).toBe('ремонт:');
|
||||
it('stub: null even when justification line missing (vocab removed in v4)', () => {
|
||||
expect(findOverrideAttempt('ремонт инфраструктуры', 'verify-before-push', testVocab)).toBeNull();
|
||||
});
|
||||
|
||||
it('returns phrase when justification IS provided (same behaviour as findOverride for success path)', () => {
|
||||
const r = findOverrideAttempt('ремонт инфраструктуры\nремонт: observer refresh', 'verify-before-push', testVocab);
|
||||
expect(r).not.toBeNull();
|
||||
expect(r.phrase).toBe('ремонт инфраструктуры');
|
||||
it('stub: null even when justification IS provided (vocab removed in v4)', () => {
|
||||
expect(findOverrideAttempt('ремонт инфраструктуры\nремонт: observer refresh', 'verify-before-push', testVocab)).toBeNull();
|
||||
});
|
||||
|
||||
it('returns phrase for non-justification overrides (e.g., срочно)', () => {
|
||||
const r = findOverrideAttempt('срочно надо', 'verify-before-push', testVocab);
|
||||
expect(r).not.toBeNull();
|
||||
expect(r.phrase).toBe('срочно');
|
||||
it('stub: null for срочно override (vocab removed in v4)', () => {
|
||||
expect(findOverrideAttempt('срочно надо', 'verify-before-push', testVocab)).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when phrase substring not in prompt', () => {
|
||||
it('returns null when phrase substring not in prompt (still null via stub)', () => {
|
||||
expect(findOverrideAttempt('hello world', 'verify-before-push', testVocab)).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when rule key not in suppresses (phrase irrelevant)', () => {
|
||||
const r = findOverrideAttempt('ремонт инфраструктуры', 'tdd-gate-other', testVocab);
|
||||
expect(r).toBeNull();
|
||||
it('returns null when rule key not in suppresses (still null via stub)', () => {
|
||||
expect(findOverrideAttempt('ремонт инфраструктуры', 'tdd-gate-other', testVocab)).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null on empty / null prompt', () => {
|
||||
it('returns null on empty / null prompt (unchanged)', () => {
|
||||
expect(findOverrideAttempt('', 'verify-before-push', testVocab)).toBeNull();
|
||||
expect(findOverrideAttempt(null, 'verify-before-push', testVocab)).toBeNull();
|
||||
});
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* PreToolUse(mcp__*) wrapper for tools/mcp-tool-classifier.mjs.
|
||||
* Router-gate v4 spec §5.3 + v4.1 G1/G12.
|
||||
*
|
||||
* Classifier categorises MCP tool calls; default-deny on unknown.
|
||||
* 'ask' decision is treated as block (controller must seek explicit approval).
|
||||
* Fail-CLOSE on internal error.
|
||||
*/
|
||||
import { fileURLToPath } from 'url';
|
||||
import {
|
||||
readStdin,
|
||||
parseEventJson,
|
||||
exitDecision,
|
||||
} from './enforce-hook-helpers.mjs';
|
||||
import { classifyMcpTool } from './mcp-tool-classifier.mjs';
|
||||
|
||||
export function decide({ toolName, toolInput }) {
|
||||
const name = String(toolName || '');
|
||||
if (!name.startsWith('mcp__')) return { block: false, reason: null };
|
||||
const verdict = classifyMcpTool(name, toolInput || {}, {});
|
||||
if (!verdict) return { block: false, reason: null };
|
||||
if (verdict.decision === 'block' || verdict.decision === 'ask') {
|
||||
return { block: true, reason: verdict.reason || `${name} requires approval (decision=${verdict.decision})` };
|
||||
}
|
||||
return { block: false, reason: null };
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const raw = await readStdin();
|
||||
const event = parseEventJson(raw);
|
||||
const r = decide({ toolName: event.tool_name, toolInput: event.tool_input });
|
||||
if (r.block) {
|
||||
return exitDecision({ block: true, message: `[mcp-classification] ${r.reason}` });
|
||||
}
|
||||
return exitDecision({ block: false });
|
||||
} catch {
|
||||
return exitDecision({ block: true, message: '[mcp-classification] внутренняя ошибка — fail-CLOSE' });
|
||||
}
|
||||
}
|
||||
|
||||
const isCli = process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1];
|
||||
if (isCli) main();
|
||||
@@ -0,0 +1,13 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide } from './enforce-mcp-classification.mjs';
|
||||
|
||||
describe('enforce-mcp-classification decide()', () => {
|
||||
it('allows non-mcp tools (no-op)', () => {
|
||||
expect(decide({ toolName: 'Bash', toolInput: { command: 'ls' } }).block).toBe(false);
|
||||
});
|
||||
it('blocks an unknown mcp tool (default-deny)', () => {
|
||||
const r = decide({ toolName: 'mcp__unknown__doSomething', toolInput: {} });
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/not in gate-config classification/);
|
||||
});
|
||||
});
|
||||
@@ -1,170 +0,0 @@
|
||||
// PreToolUse hook: hard-block 6th+ usage of same override-phrase in one day.
|
||||
// Phase 2 of router-hooks fixes (per brain-retro #9 candidate 6 + self-retrospect 28.05).
|
||||
//
|
||||
// Reads:
|
||||
// - hook input JSON (passed via stdin)
|
||||
// - ~/.claude/runtime/override-usage.jsonl (today's usage log)
|
||||
// - tools/enforce-override-vocab.json (7 phrases)
|
||||
//
|
||||
// Writes (stdout):
|
||||
// - empty if no block
|
||||
// - JSON {decision: "block", reason: "..."} if 6th phrase usage detected
|
||||
//
|
||||
// Bypass: BYPASS_PHRASE in current prompt -> no block (counter unchanged).
|
||||
|
||||
import { readFileSync, existsSync } from 'fs';
|
||||
import { join, dirname } from 'path';
|
||||
import { homedir } from 'os';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
export const THRESHOLD = 5;
|
||||
export const RATE_WINDOW_MIN = 10;
|
||||
export const RATE_THRESHOLD = 5;
|
||||
export const BYPASS_PHRASE = 'лимит снят';
|
||||
|
||||
function loadVocab() {
|
||||
const vocabPath = join(__dirname, 'enforce-override-vocab.json');
|
||||
if (!existsSync(vocabPath)) return [];
|
||||
try {
|
||||
const j = JSON.parse(readFileSync(vocabPath, 'utf-8'));
|
||||
return Array.isArray(j.phrases) ? j.phrases.map(p => p.phrase) : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export const VOCAB = loadVocab();
|
||||
|
||||
export function findPhrasesInPrompt(prompt) {
|
||||
if (typeof prompt !== 'string' || !prompt) return [];
|
||||
const lower = prompt.toLowerCase();
|
||||
return VOCAB.filter(p => lower.includes(p.toLowerCase()));
|
||||
}
|
||||
|
||||
export function countTodayUsage(rawLog, phrase, now = new Date()) {
|
||||
if (typeof rawLog !== 'string' || !rawLog) return 0;
|
||||
const today = now.toISOString().slice(0, 10);
|
||||
let count = 0;
|
||||
for (const line of rawLog.split('\n')) {
|
||||
if (!line) continue;
|
||||
try {
|
||||
const e = JSON.parse(line);
|
||||
if (e.phrase === phrase && typeof e.ts === 'string' && e.ts.slice(0, 10) === today) {
|
||||
count++;
|
||||
}
|
||||
} catch {
|
||||
// ignore malformed lines
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
export function countWindowUsage(rawLog, phrase, now = new Date(), windowMinutes = 10) {
|
||||
if (typeof rawLog !== 'string' || !rawLog) return 0;
|
||||
const cutoffMs = now.getTime() - windowMinutes * 60_000;
|
||||
let count = 0;
|
||||
for (const line of rawLog.split('\n')) {
|
||||
if (!line) continue;
|
||||
try {
|
||||
const e = JSON.parse(line);
|
||||
if (e.phrase !== phrase) continue;
|
||||
if (typeof e.ts !== 'string') continue;
|
||||
const tsMs = Date.parse(e.ts);
|
||||
if (Number.isFinite(tsMs) && tsMs >= cutoffMs && tsMs <= now.getTime()) {
|
||||
count++;
|
||||
}
|
||||
} catch {
|
||||
// ignore malformed
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
export function shouldBlock(prompt, rawLog, now = new Date()) {
|
||||
if (typeof prompt === 'string' && prompt.toLowerCase().includes(BYPASS_PHRASE.toLowerCase())) {
|
||||
return { block: false, bypass: true };
|
||||
}
|
||||
const phrases = findPhrasesInPrompt(prompt);
|
||||
for (const phrase of phrases) {
|
||||
const todayCount = countTodayUsage(rawLog, phrase, now);
|
||||
if (todayCount >= THRESHOLD) {
|
||||
return {
|
||||
block: true,
|
||||
phrase,
|
||||
todayCount,
|
||||
triggered: 'daily',
|
||||
reason: `daily count ${todayCount} >= ${THRESHOLD}`,
|
||||
};
|
||||
}
|
||||
const windowCount = countWindowUsage(rawLog, phrase, now, RATE_WINDOW_MIN);
|
||||
if (windowCount >= RATE_THRESHOLD) {
|
||||
return {
|
||||
block: true,
|
||||
phrase,
|
||||
windowCount,
|
||||
triggered: 'rate',
|
||||
reason: `rate-window count ${windowCount} >= ${RATE_THRESHOLD} in ${RATE_WINDOW_MIN} min`,
|
||||
};
|
||||
}
|
||||
}
|
||||
return { block: false };
|
||||
}
|
||||
|
||||
export function buildBlockOutput({ phrase, todayCount, windowCount, triggered }) {
|
||||
if (triggered === 'rate') {
|
||||
return {
|
||||
decision: 'block',
|
||||
reason:
|
||||
`[enforce-override-limit] Override-фраза «${phrase}» использована ${windowCount} раз за последние ${RATE_WINDOW_MIN} минут (порог ${RATE_THRESHOLD}). ` +
|
||||
`Rate-spike обнаружен — это шаблонная привычка обхода, не реальная нужда. ` +
|
||||
`Сделай ПАУЗУ 10 минут перед следующим override, или вызови AskUserQuestion и попроси заказчика подтвердить новый bypass через «${BYPASS_PHRASE}» (счётчик НЕ сбрасывается).`,
|
||||
};
|
||||
}
|
||||
return {
|
||||
decision: 'block',
|
||||
reason:
|
||||
`[enforce-override-limit] Override-фраза «${phrase}» уже использована ${todayCount} раз сегодня (порог ${THRESHOLD}/день per phrase). ` +
|
||||
`Это 6-е или последующее использование — hard-block per Phase 2 plan. ` +
|
||||
`Чтобы продолжить, вызови AskUserQuestion и спроси заказчика явно. ` +
|
||||
`Если он подтверждает — следующий промпт должен содержать фразу «${BYPASS_PHRASE}» (one-shot bypass, счётчик НЕ сбрасывается).`,
|
||||
};
|
||||
}
|
||||
|
||||
// CLI: read hook input from stdin, write block-JSON to stdout if needed.
|
||||
async function main() {
|
||||
try {
|
||||
let raw = '';
|
||||
for await (const chunk of process.stdin) raw += chunk;
|
||||
let input;
|
||||
try { input = JSON.parse(raw || '{}'); } catch { input = {}; }
|
||||
|
||||
// Find current user prompt - different hook payloads use different fields.
|
||||
const prompt =
|
||||
input?.prompt ||
|
||||
input?.hook_event?.prompt ||
|
||||
input?.user_prompt ||
|
||||
input?.transcript?.[input?.transcript?.length - 1]?.content ||
|
||||
'';
|
||||
|
||||
const logPath = join(homedir(), '.claude', 'runtime', 'override-usage.jsonl');
|
||||
const rawLog = existsSync(logPath) ? readFileSync(logPath, 'utf-8') : '';
|
||||
|
||||
const decision = shouldBlock(prompt, rawLog);
|
||||
if (decision.block) {
|
||||
process.stdout.write(JSON.stringify(buildBlockOutput(decision)));
|
||||
process.exit(0);
|
||||
}
|
||||
// No block - silent pass.
|
||||
process.exit(0);
|
||||
} catch {
|
||||
// Fail-open: any internal error must NOT block the user.
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Run as CLI if this file is the entrypoint (not when imported by tests).
|
||||
const isCli = process.argv[1] && process.argv[1].replace(/\\/g, '/').endsWith('/enforce-override-limit.mjs');
|
||||
if (isCli) main();
|
||||
@@ -1,255 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { execFileSync } from 'child_process';
|
||||
import { writeFileSync, mkdtempSync, rmSync } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join, dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const projectRoot = join(dirname(fileURLToPath(import.meta.url)), '..');
|
||||
import {
|
||||
countTodayUsage,
|
||||
countWindowUsage,
|
||||
findPhrasesInPrompt,
|
||||
shouldBlock,
|
||||
buildBlockOutput,
|
||||
VOCAB,
|
||||
THRESHOLD,
|
||||
BYPASS_PHRASE,
|
||||
} from './enforce-override-limit.mjs';
|
||||
|
||||
describe('VOCAB + THRESHOLD constants', () => {
|
||||
it('exports 7 phrases', () => {
|
||||
expect(VOCAB.length).toBe(7);
|
||||
expect(VOCAB).toContain('recovery');
|
||||
expect(VOCAB).toContain('ремонт инфраструктуры');
|
||||
expect(VOCAB).toContain('без скилов');
|
||||
});
|
||||
it('threshold is 5', () => {
|
||||
expect(THRESHOLD).toBe(5);
|
||||
});
|
||||
it('bypass phrase is "лимит снят"', () => {
|
||||
expect(BYPASS_PHRASE).toBe('лимит снят');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findPhrasesInPrompt', () => {
|
||||
it('finds single phrase case-insensitively', () => {
|
||||
expect(findPhrasesInPrompt('сделай recovery быстро')).toEqual(['recovery']);
|
||||
expect(findPhrasesInPrompt('сделай RECOVERY')).toEqual(['recovery']);
|
||||
});
|
||||
it('finds multiple phrases in one prompt', () => {
|
||||
const found = findPhrasesInPrompt('срочно: recovery и быстрый коммит');
|
||||
expect(found.sort()).toEqual(['быстрый коммит', 'recovery', 'срочно'].sort());
|
||||
});
|
||||
it('returns empty array on no match', () => {
|
||||
expect(findPhrasesInPrompt('обычный текст без override')).toEqual([]);
|
||||
});
|
||||
it('handles empty/null prompt', () => {
|
||||
expect(findPhrasesInPrompt('')).toEqual([]);
|
||||
expect(findPhrasesInPrompt(null)).toEqual([]);
|
||||
expect(findPhrasesInPrompt(undefined)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('countTodayUsage', () => {
|
||||
it('counts entries for given phrase on given date', () => {
|
||||
const log = [
|
||||
'{"ts":"2026-05-28T10:00:00.000Z","phrase":"recovery"}',
|
||||
'{"ts":"2026-05-28T11:00:00.000Z","phrase":"recovery"}',
|
||||
'{"ts":"2026-05-28T12:00:00.000Z","phrase":"ремонт инфраструктуры"}',
|
||||
'{"ts":"2026-05-27T10:00:00.000Z","phrase":"recovery"}', // вчера, не считается
|
||||
].join('\n');
|
||||
expect(countTodayUsage(log, 'recovery', new Date('2026-05-28T15:00:00Z'))).toBe(2);
|
||||
expect(countTodayUsage(log, 'ремонт инфраструктуры', new Date('2026-05-28T15:00:00Z'))).toBe(1);
|
||||
expect(countTodayUsage(log, 'recovery', new Date('2026-05-27T15:00:00Z'))).toBe(1);
|
||||
});
|
||||
it('returns 0 on empty/malformed log', () => {
|
||||
expect(countTodayUsage('', 'recovery', new Date())).toBe(0);
|
||||
expect(countTodayUsage(null, 'recovery', new Date())).toBe(0);
|
||||
expect(countTodayUsage('not json\nалсо not\n', 'recovery', new Date())).toBe(0);
|
||||
});
|
||||
it('ignores malformed JSON lines mixed with valid', () => {
|
||||
const log = [
|
||||
'{"ts":"2026-05-28T10:00:00.000Z","phrase":"recovery"}',
|
||||
'broken line',
|
||||
'{"ts":"2026-05-28T11:00:00.000Z","phrase":"recovery"}',
|
||||
].join('\n');
|
||||
expect(countTodayUsage(log, 'recovery', new Date('2026-05-28T15:00:00Z'))).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shouldBlock', () => {
|
||||
const now = new Date('2026-05-28T15:00:00Z');
|
||||
const fourUses = Array.from({ length: 4 }, (_, i) =>
|
||||
`{"ts":"2026-05-28T0${i}:00:00.000Z","phrase":"recovery"}`
|
||||
).join('\n');
|
||||
const fiveUses = Array.from({ length: 5 }, (_, i) =>
|
||||
`{"ts":"2026-05-28T0${i}:00:00.000Z","phrase":"recovery"}`
|
||||
).join('\n');
|
||||
|
||||
it('returns {block:false} when no override phrase in prompt', () => {
|
||||
const r = shouldBlock('обычный текст', fiveUses, now);
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
it('returns {block:false} when phrase used 4 times today (below threshold)', () => {
|
||||
const r = shouldBlock('сделай recovery', fourUses, now);
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
it('returns {block:true} when phrase used 5 times today (this is 6th)', () => {
|
||||
const r = shouldBlock('сделай recovery', fiveUses, now);
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.phrase).toBe('recovery');
|
||||
expect(r.todayCount).toBe(5);
|
||||
});
|
||||
it('returns {block:false} when bypass phrase "лимит снят" present', () => {
|
||||
const r = shouldBlock('сделай recovery лимит снят', fiveUses, now);
|
||||
expect(r.block).toBe(false);
|
||||
expect(r.bypass).toBe(true);
|
||||
});
|
||||
it('blocks on FIRST exceeding phrase when multiple present', () => {
|
||||
const log = [fiveUses, '{"ts":"2026-05-28T05:00:00.000Z","phrase":"срочно"}'].join('\n');
|
||||
const r = shouldBlock('срочно сделай recovery', log, now);
|
||||
expect(r.block).toBe(true);
|
||||
// Either recovery or срочно could be first found; must be a real over-threshold one.
|
||||
expect(['recovery', 'срочно']).toContain(r.phrase);
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildBlockOutput', () => {
|
||||
it('returns JSON with decision: block and informative reason', () => {
|
||||
const out = buildBlockOutput({ phrase: 'recovery', todayCount: 5 });
|
||||
expect(out).toHaveProperty('decision', 'block');
|
||||
expect(out.reason).toContain('recovery');
|
||||
expect(out.reason).toContain('5');
|
||||
expect(out.reason).toContain('лимит снят');
|
||||
});
|
||||
});
|
||||
|
||||
describe('countWindowUsage', () => {
|
||||
it('counts only entries within window minutes of now', () => {
|
||||
const now = new Date('2026-05-28T13:00:00Z');
|
||||
const log = [
|
||||
// 5 min ago — IN window
|
||||
JSON.stringify({ ts: '2026-05-28T12:55:00.000Z', phrase: 'recovery', session_id: 's1', rule: 'r1' }),
|
||||
// 8 min ago — IN window
|
||||
JSON.stringify({ ts: '2026-05-28T12:52:00.000Z', phrase: 'recovery', session_id: 's1', rule: 'r2' }),
|
||||
// 11 min ago — OUT of window
|
||||
JSON.stringify({ ts: '2026-05-28T12:49:00.000Z', phrase: 'recovery', session_id: 's1', rule: 'r3' }),
|
||||
// different phrase — OUT
|
||||
JSON.stringify({ ts: '2026-05-28T12:55:00.000Z', phrase: 'без скилов', session_id: 's1', rule: 'r4' }),
|
||||
].join('\n');
|
||||
expect(countWindowUsage(log, 'recovery', now, 10)).toBe(2);
|
||||
});
|
||||
|
||||
it('returns 0 on empty log', () => {
|
||||
expect(countWindowUsage('', 'recovery', new Date(), 10)).toBe(0);
|
||||
});
|
||||
|
||||
it('handles malformed lines gracefully', () => {
|
||||
const now = new Date('2026-05-28T13:00:00Z');
|
||||
const log = [
|
||||
'not-json',
|
||||
JSON.stringify({ ts: '2026-05-28T12:55:00.000Z', phrase: 'recovery' }),
|
||||
'{broken',
|
||||
].join('\n');
|
||||
expect(countWindowUsage(log, 'recovery', now, 10)).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shouldBlock with rate-window', () => {
|
||||
const now = new Date('2026-05-28T13:00:00Z');
|
||||
|
||||
it('blocks when same phrase used 5+ times within rate window (rate-trigger)', () => {
|
||||
// 5 events all within last 3 minutes — same calendar day, threshold reached on rate axis
|
||||
const log = [
|
||||
JSON.stringify({ ts: '2026-05-28T12:58:30.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T12:58:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T12:57:30.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T12:57:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T12:56:30.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
].join('\n');
|
||||
const result = shouldBlock('делай recovery', log, now);
|
||||
expect(result.block).toBe(true);
|
||||
expect(result.phrase).toBe('recovery');
|
||||
expect(result.triggered).toBe('daily');
|
||||
// Note: at exactly 5 today+5 in window, daily wins because daily check comes first
|
||||
// We test pure rate-trigger in next case.
|
||||
});
|
||||
|
||||
it('blocks via rate-trigger when daily count is below daily threshold but rate fires (4 spread + 5 in window)', () => {
|
||||
// Wait: we cannot have 5 in window without those 5 also counting toward day.
|
||||
// To isolate rate trigger only: we'd need daily < 5 AND window >= 5 — impossible since window ⊂ day.
|
||||
// So we instead test that when triggered, the result distinguishes which axis fired.
|
||||
// Skipped — covered by 'blocks at exactly 5 daily' above. Pure rate-only path is empty by construction.
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
|
||||
it('does NOT block when rate-window count < RATE_THRESHOLD AND daily count < THRESHOLD', () => {
|
||||
const log = [
|
||||
JSON.stringify({ ts: '2026-05-28T12:55:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T12:50:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
].join('\n');
|
||||
const result = shouldBlock('делай recovery', log, now);
|
||||
expect(result.block).toBe(false);
|
||||
});
|
||||
|
||||
it('blocks via rate-trigger when daily count is 6+ historical but recent rate spike also present', () => {
|
||||
// 4 entries from earlier today (>10min ago) + 5 entries in last 9 minutes
|
||||
// Daily = 9 (>= 5, would block on daily)
|
||||
// We check that the response indicates which axis triggered. Daily check comes first per impl.
|
||||
const log = [
|
||||
// Old today entries (12+ min ago)
|
||||
JSON.stringify({ ts: '2026-05-28T11:00:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T11:05:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T11:10:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T11:15:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
// Recent (in window)
|
||||
JSON.stringify({ ts: '2026-05-28T12:55:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T12:56:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T12:57:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T12:58:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
JSON.stringify({ ts: '2026-05-28T12:59:00.000Z', phrase: 'recovery', session_id: 's' }),
|
||||
].join('\n');
|
||||
const result = shouldBlock('делай recovery', log, now);
|
||||
expect(result.block).toBe(true);
|
||||
// Daily check runs first, so 'daily' wins here
|
||||
expect(result.triggered).toBe('daily');
|
||||
});
|
||||
|
||||
it('returns triggered=rate when daily count is below THRESHOLD via small log but window=THRESHOLD', () => {
|
||||
// Construct a case where shouldBlock would trigger only by rate.
|
||||
// Since rate window ⊂ day, this requires daily < 5 AND window >= 5 — impossible.
|
||||
// The path 'triggered=rate' only fires when daily check passes (todayCount < THRESHOLD)
|
||||
// AND windowCount >= RATE_THRESHOLD. Since RATE_THRESHOLD = THRESHOLD = 5 and window ⊂ day,
|
||||
// windowCount <= dayCount, so windowCount >= 5 implies dayCount >= 5.
|
||||
// Therefore in current config rate-trigger is unreachable. Document this and skip.
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('CLI e2e', () => {
|
||||
let tmpDir;
|
||||
beforeEach(() => { tmpDir = mkdtempSync(join(tmpdir(), 'ovrl-')); });
|
||||
afterEach(() => { try { rmSync(tmpDir, { recursive: true, force: true }); } catch {} });
|
||||
|
||||
it('writes block JSON when threshold exceeded', () => {
|
||||
const input = JSON.stringify({ prompt: 'обычный prompt без override' });
|
||||
const out = execFileSync('node', ['tools/enforce-override-limit.mjs'], {
|
||||
input,
|
||||
cwd: projectRoot,
|
||||
encoding: 'utf-8',
|
||||
timeout: 5000,
|
||||
});
|
||||
expect(out.trim()).toBe('');
|
||||
});
|
||||
|
||||
it('silent pass when CLI given empty stdin', () => {
|
||||
const out = execFileSync('node', ['tools/enforce-override-limit.mjs'], {
|
||||
input: '',
|
||||
cwd: projectRoot,
|
||||
encoding: 'utf-8',
|
||||
timeout: 5000,
|
||||
});
|
||||
expect(out.trim()).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -1,83 +0,0 @@
|
||||
{
|
||||
"version": 1,
|
||||
"comment": "Hard-coded override phrases. Substring-match (case-insensitive) against user's last prompt. Each phrase suppresses one or more rule categories for ONE prompt only.",
|
||||
"phrases": [
|
||||
{
|
||||
"phrase": "без скилов",
|
||||
"suppresses": [
|
||||
"skill-required",
|
||||
"coverage-skill-match",
|
||||
"classifier-mismatch",
|
||||
"graph-first",
|
||||
"chain-recommendation",
|
||||
"semgrep-security"
|
||||
],
|
||||
"description": "Skill discipline relaxed for this one prompt"
|
||||
},
|
||||
{
|
||||
"phrase": "direct ok",
|
||||
"suppresses": [
|
||||
"skill-required",
|
||||
"coverage-skill-match",
|
||||
"classifier-mismatch",
|
||||
"graph-first",
|
||||
"chain-recommendation",
|
||||
"semgrep-security"
|
||||
],
|
||||
"description": "Direct work allowed without skill invocation"
|
||||
},
|
||||
{
|
||||
"phrase": "срочно",
|
||||
"suppresses": [
|
||||
"verify-before-commit",
|
||||
"verify-before-push",
|
||||
"tdd-gate",
|
||||
"graph-first",
|
||||
"chain-recommendation",
|
||||
"semgrep-security"
|
||||
],
|
||||
"description": "Urgency override: skip verification + TDD gate + graph/chain enforcement"
|
||||
},
|
||||
{
|
||||
"phrase": "быстрый коммит",
|
||||
"suppresses": [
|
||||
"verify-before-commit",
|
||||
"tdd-gate",
|
||||
"writing-plans-required",
|
||||
"graph-first",
|
||||
"chain-recommendation",
|
||||
"semgrep-security"
|
||||
],
|
||||
"description": "Quick commit: skip TDD + verify + plans + graph/chain enforcement"
|
||||
},
|
||||
{
|
||||
"phrase": "recovery",
|
||||
"suppresses": [
|
||||
"branch-switch",
|
||||
"git-recovery"
|
||||
],
|
||||
"description": "Git recovery only — branch-state mismatch ok. Does NOT suppress graph-first / chain-recommendation / semgrep-security (use specific phrases for those)."
|
||||
},
|
||||
{
|
||||
"phrase": "memory dump",
|
||||
"suppresses": [
|
||||
"memory-sync-coverage",
|
||||
"skill-required",
|
||||
"graph-first",
|
||||
"chain-recommendation",
|
||||
"semgrep-security"
|
||||
],
|
||||
"description": "Memory write without separate coverage announcement"
|
||||
},
|
||||
{
|
||||
"phrase": "ремонт инфраструктуры",
|
||||
"suppresses": [
|
||||
"tdd-gate",
|
||||
"verify-before-commit",
|
||||
"verify-before-push"
|
||||
],
|
||||
"requires_justification": "ремонт:",
|
||||
"description": "Infrastructure repair — bypass TDD-gate + verify hooks only. Other rules (skill-required, classifier-mismatch, chain-recommendation, graph-first, semgrep-security, memory-sync-coverage, coverage-skill-match, writing-plans-required) require their own override phrases."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* enforce-parallel-session-lock — PreToolUse wrapper around the pure
|
||||
* parallel-session-lock module (router-gate v4 Stream H Task 7).
|
||||
*
|
||||
* Prevents two Claude sessions on the same workspace from concurrently
|
||||
* mutating files. When session B tries a mutating tool while session A
|
||||
* holds a fresh (non-stale) lock, B is blocked with a message naming A's
|
||||
* pid for human triage.
|
||||
*
|
||||
* Activation: settings.json registration is deferred to Phase H-α/H-β
|
||||
* batch step. main() is a no-op (exit 0) until then.
|
||||
*/
|
||||
import { acquire, release, refresh, computeWorkspaceHash } from './parallel-session-lock.mjs';
|
||||
|
||||
/**
|
||||
* Pure decision: given an acquire() result, decide block/allow.
|
||||
*
|
||||
* @param {object} args
|
||||
* @param {object|null|undefined} args.acquireResult - from parallel-session-lock.acquire()
|
||||
* @param {string} args.sessionId - current session id
|
||||
* @returns {{block: boolean, reason?: string}}
|
||||
*/
|
||||
export function decide({ acquireResult, sessionId }) {
|
||||
// Fail-open if no acquire result (treat as internal error — never lockout).
|
||||
if (!acquireResult || typeof acquireResult !== 'object') return { block: false };
|
||||
if (acquireResult.acquired) return { block: false };
|
||||
const holder = acquireResult.holder || {};
|
||||
return {
|
||||
block: true,
|
||||
reason: `parallel session lock held by ${holder.session_id || 'unknown'} (pid ${holder.pid || '?'}) — wait or close that session first`,
|
||||
};
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// No-op until settings.json registration + Stop-hook release wiring lands
|
||||
// in the deferred Phase H-α/H-β batch step. Activating this hook before
|
||||
// the release pathway is wired would lock the user out of their own
|
||||
// session on first abnormal exit.
|
||||
let input = '';
|
||||
for await (const chunk of process.stdin) input += chunk;
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1].replace(/\\/g, '/')}` || (process.argv[1] || '').endsWith('enforce-parallel-session-lock.mjs')) {
|
||||
main().catch(() => process.exit(0));
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// tools/enforce-parallel-session-lock.test.mjs
|
||||
// Stream H Task 7 — wrapper tests around the pure parallel-session-lock module.
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide } from './enforce-parallel-session-lock.mjs';
|
||||
|
||||
describe('enforce-parallel-session-lock wrapper (Stream H Task 7)', () => {
|
||||
it('allow when acquire succeeded (fresh own-lock)', () => {
|
||||
const r = decide({
|
||||
acquireResult: { acquired: true, holder: { session_id: 's1', pid: 100, acquired_at: 1000 } },
|
||||
sessionId: 's1',
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
|
||||
it('block when another session holds the lock', () => {
|
||||
const r = decide({
|
||||
acquireResult: { acquired: false, holder: { session_id: 'other-session', pid: 999, acquired_at: 500 } },
|
||||
sessionId: 's1',
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/parallel session lock.*other-session/i);
|
||||
});
|
||||
|
||||
it('allow when same-session re-acquires (takeover)', () => {
|
||||
const r = decide({
|
||||
acquireResult: { acquired: true, holder: { session_id: 's1', pid: 100, acquired_at: 2000 } },
|
||||
sessionId: 's1',
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
|
||||
it('fail-open when acquireResult is missing (internal error path)', () => {
|
||||
expect(decide({ acquireResult: null, sessionId: 's1' }).block).toBe(false);
|
||||
expect(decide({ acquireResult: undefined, sessionId: 's1' }).block).toBe(false);
|
||||
});
|
||||
|
||||
it('block message identifies the other holder pid for human triage', () => {
|
||||
const r = decide({
|
||||
acquireResult: { acquired: false, holder: { session_id: 'other', pid: 42, acquired_at: 0 } },
|
||||
sessionId: 's1',
|
||||
});
|
||||
expect(r.reason).toMatch(/pid 42/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* PreToolUse(Read) wrapper — path-deny for Read tool.
|
||||
* Router-gate v4 emergency fix (Smoke 5 2026-05-30).
|
||||
*
|
||||
* Spec §3.1 declared transcript JSONL hard-deny but Read tool had NO
|
||||
* path-protection — controller could Read ~/.claude/projects/*.jsonl
|
||||
* (parent context exfil from other sessions). Same for runtime artifacts,
|
||||
* .env, normative files.
|
||||
*
|
||||
* Reuses DEFAULT_PROTECTED_PATTERNS from shell-content-rules.mjs.
|
||||
* Fail-CLOSE on internal error (security default).
|
||||
*/
|
||||
import { fileURLToPath } from 'url';
|
||||
import {
|
||||
readStdin,
|
||||
parseEventJson,
|
||||
exitDecision,
|
||||
} from './enforce-hook-helpers.mjs';
|
||||
import { defaultPathNormalize, isProtectedPath, DEFAULT_PROTECTED_PATTERNS } from './shell-content-rules.mjs';
|
||||
|
||||
export function decide({ toolName, filePath }) {
|
||||
if (toolName !== 'Read') return { block: false, reason: null };
|
||||
const fp = String(filePath || '');
|
||||
if (!fp) return { block: false, reason: null };
|
||||
if (isProtectedPath(fp, defaultPathNormalize, DEFAULT_PROTECTED_PATTERNS)) {
|
||||
return {
|
||||
block: true,
|
||||
reason: `path «${defaultPathNormalize(fp)}» protected against Read (§3.1 transcript/runtime/normative hard-deny)`,
|
||||
};
|
||||
}
|
||||
return { block: false, reason: null };
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const raw = await readStdin();
|
||||
const event = parseEventJson(raw);
|
||||
const r = decide({
|
||||
toolName: event.tool_name,
|
||||
filePath: event.tool_input?.file_path || event.tool_input?.filePath,
|
||||
});
|
||||
if (r.block) {
|
||||
return exitDecision({ block: true, message: `[read-path-deny] ${r.reason}` });
|
||||
}
|
||||
return exitDecision({ block: false });
|
||||
} catch {
|
||||
return exitDecision({ block: true, message: '[read-path-deny] внутренняя ошибка — fail-CLOSE' });
|
||||
}
|
||||
}
|
||||
|
||||
const isCli = process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1];
|
||||
if (isCli) main();
|
||||
@@ -0,0 +1,30 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide } from './enforce-read-path-deny.mjs';
|
||||
|
||||
describe('enforce-read-path-deny decide()', () => {
|
||||
it('allows Read on normal project file', () => {
|
||||
const r = decide({ toolName: 'Read', filePath: 'docs/observer/STATUS.md' });
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
it('blocks Read on ~/.claude/projects/*.jsonl transcript', () => {
|
||||
const r = decide({ toolName: 'Read', filePath: '~/.claude/projects/abc-session.jsonl' });
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/protected/i);
|
||||
});
|
||||
it('blocks Read on absolute /c/Users/.../.claude/projects/x.jsonl', () => {
|
||||
const r = decide({ toolName: 'Read', filePath: '/c/Users/Administrator/.claude/projects/proj/session.jsonl' });
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
it('blocks Read on ~/.claude/runtime/*.json (runtime artifacts)', () => {
|
||||
const r = decide({ toolName: 'Read', filePath: '~/.claude/runtime/router-state-x.json' });
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
it('blocks Read on .env', () => {
|
||||
const r = decide({ toolName: 'Read', filePath: '.env' });
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
it('allows non-Read tool calls (no-op)', () => {
|
||||
const r = decide({ toolName: 'Bash', filePath: 'whatever' });
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* PreToolUse(Edit|Write|MultiEdit|Bash) wrapper for tools/self-debrief-detector.mjs.
|
||||
* Router-gate v4.1 spec §3.12 (NEW).
|
||||
*
|
||||
* Reads last controller text from transcript; if it matches self-debrief patterns
|
||||
* (я заметил паттерн / generalisable lesson / etc.) AND no self-retrospect or
|
||||
* brain-retro Skill in recent turns — block.
|
||||
*
|
||||
* Fail-CLOSE on internal error.
|
||||
*/
|
||||
import { fileURLToPath } from 'url';
|
||||
import {
|
||||
readStdin,
|
||||
parseEventJson,
|
||||
readTranscript,
|
||||
exitDecision,
|
||||
} from './enforce-hook-helpers.mjs';
|
||||
import { detectSelfDebrief } from './self-debrief-detector.mjs';
|
||||
|
||||
/** Extract last assistant (controller) text from transcript. */
|
||||
export function lastControllerText(transcript) {
|
||||
const recs = transcript || [];
|
||||
for (let i = recs.length - 1; i >= 0; i--) {
|
||||
const r = recs[i];
|
||||
if (r && r.type === 'text' && r.role === 'assistant') return String(r.text || '');
|
||||
if (r && r.role === 'assistant' && typeof r.content === 'string') return r.content;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
export function decide({ controllerText, transcript }) {
|
||||
const r = detectSelfDebrief(controllerText, transcript || []);
|
||||
if (r.action === 'hard_block_next_mutating') {
|
||||
return { block: true, reason: r.reason };
|
||||
}
|
||||
return { block: false, reason: null };
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const raw = await readStdin();
|
||||
const event = parseEventJson(raw);
|
||||
const mutating = ['Edit', 'Write', 'MultiEdit', 'Bash'];
|
||||
if (!mutating.includes(event.tool_name)) return exitDecision({ block: false });
|
||||
|
||||
const transcript = readTranscript(event.transcript_path);
|
||||
const controllerText = lastControllerText(transcript);
|
||||
const r = decide({ controllerText, transcript });
|
||||
if (r.block) {
|
||||
return exitDecision({ block: true, message: `[self-debrief-detector] ${r.reason}` });
|
||||
}
|
||||
return exitDecision({ block: false });
|
||||
} catch {
|
||||
return exitDecision({ block: true, message: '[self-debrief-detector] внутренняя ошибка — fail-CLOSE' });
|
||||
}
|
||||
}
|
||||
|
||||
const isCli = process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1];
|
||||
if (isCli) main();
|
||||
@@ -0,0 +1,25 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide } from './enforce-self-debrief-detector.mjs';
|
||||
|
||||
describe('enforce-self-debrief-detector decide()', () => {
|
||||
it('allows neutral controller text', () => {
|
||||
expect(decide({ controllerText: 'Implementing feature X.', transcript: [] }).block).toBe(false);
|
||||
});
|
||||
it('blocks retrospect-style text without self-retrospect skill call', () => {
|
||||
const r = decide({
|
||||
controllerText: 'Я заметил паттерн в своих ответах — generalisable lesson: ...',
|
||||
transcript: [],
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/self-debrief hard-block/);
|
||||
});
|
||||
it('allows retrospect-style text when self-retrospect was invoked recently', () => {
|
||||
const r = decide({
|
||||
controllerText: 'я обобщаю опыт',
|
||||
transcript: [
|
||||
{ type: 'tool_use', name: 'Skill', input: { skill: 'self-retrospect' }, turn: 1 },
|
||||
],
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,135 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Rule — Semgrep on security-edit.
|
||||
*
|
||||
* PreToolUse Bash hook. When the controller invokes `git commit` and the staged
|
||||
* diff includes auth/billing/CSV/webhook files but Semgrep has not been run in
|
||||
* this session, block with remediation instructions.
|
||||
*
|
||||
* Three escape hatches:
|
||||
* 1. Run Semgrep first via Bash (`npm run sast`, `semgrep ...`).
|
||||
* 2. Write semgrep-skip: <non-empty reason> on a line in the assistant text.
|
||||
* 3. User prompt contains a global override phrase (vocab-driven).
|
||||
*
|
||||
* Spec: self-retrospect 28.05 habit #4. brain-retro #9 + retro-7 background.
|
||||
*/
|
||||
|
||||
import { execFileSync } from 'child_process';
|
||||
import {
|
||||
readStdin,
|
||||
parseEventJson,
|
||||
readTranscript,
|
||||
lastUserPromptText,
|
||||
lastAssistantText,
|
||||
sessionToolUses,
|
||||
findOverride,
|
||||
logOverride,
|
||||
exitDecision,
|
||||
} from './enforce-hook-helpers.mjs';
|
||||
|
||||
const RULE_KEY = 'semgrep-security';
|
||||
const GIT_COMMIT_RE = /^\s*git\s+commit\b/;
|
||||
const SEMGREP_SKIP_RE = /^semgrep-skip:\s*\S+/m;
|
||||
const SEMGREP_CMD_RE = /\b(semgrep\b|composer\s+sast\b|npm\s+run\s+sast\b)/i;
|
||||
|
||||
const SECURITY_PATH_PATTERNS = [
|
||||
/(?:^|\/)(?:Auth|Authenticate|Authenticated|Authorization|Authorize)\b/i,
|
||||
/Billing/i,
|
||||
/Ledger/i,
|
||||
/(?:Csv|CSV)/i,
|
||||
/(?:^|\/)Imports\b/i,
|
||||
/Webhook/i,
|
||||
];
|
||||
|
||||
export function isSecurityRelevantPath(path) {
|
||||
if (!path || typeof path !== 'string') return false;
|
||||
const norm = path.replace(/\\/g, '/');
|
||||
for (const re of SECURITY_PATH_PATTERNS) {
|
||||
if (re.test(norm)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function extractStagedFiles(stdout) {
|
||||
if (!stdout || typeof stdout !== 'string') return [];
|
||||
return stdout.split('\n').map((s) => s.trim()).filter(Boolean);
|
||||
}
|
||||
|
||||
export function sessionRanSemgrep(toolUses) {
|
||||
if (!Array.isArray(toolUses)) return false;
|
||||
for (const u of toolUses) {
|
||||
if (!u || u.name !== 'Bash') continue;
|
||||
const cmd = String((u.input && u.input.command) || '');
|
||||
if (SEMGREP_CMD_RE.test(cmd)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function decide({ command, stagedFiles, semgrepRan, assistantText, override }) {
|
||||
// Step 1: only act on git commit invocations.
|
||||
if (typeof command !== 'string' || !GIT_COMMIT_RE.test(command)) return { block: false };
|
||||
|
||||
// Step 2: global override -> pass.
|
||||
if (override) return { block: false };
|
||||
|
||||
// Step 3: identify security-relevant staged files.
|
||||
const security = (Array.isArray(stagedFiles) ? stagedFiles : []).filter(isSecurityRelevantPath);
|
||||
if (security.length === 0) return { block: false };
|
||||
|
||||
// Step 4: Semgrep already ran this session -> pass.
|
||||
if (semgrepRan) return { block: false };
|
||||
|
||||
// Step 5: inline semgrep-skip with non-empty reason -> pass.
|
||||
if (typeof assistantText === 'string' && SEMGREP_SKIP_RE.test(assistantText)) return { block: false };
|
||||
|
||||
// Step 6: block.
|
||||
const list = security.slice(0, 5).map((p) => ' - ' + p).join('\n');
|
||||
const extra = security.length > 5 ? ' ... (+' + (security.length - 5) + ' ещё)\n' : '';
|
||||
const message = [
|
||||
'[enforce-semgrep-security] В коммите есть ' + security.length + ' файл(ов) с security-влиянием (auth/billing/CSV/webhook):',
|
||||
list + (extra ? '\n' + extra : ''),
|
||||
'но Semgrep не запускался в этой сессии (self-retrospect 28.05 привычка #4).',
|
||||
'Сделай ОДНО из трёх:',
|
||||
' 1. Запусти Semgrep на diff: `npm run sast` (или `semgrep scan --config p/php app/`).',
|
||||
' 2. Добавь строку semgrep-skip: <одна строка причины> в свой ответ.',
|
||||
' 3. Попроси у пользователя глобальный override (без скилов / direct ok / срочно / быстрый коммит / recovery / memory dump / ремонт инфраструктуры).',
|
||||
].join('\n');
|
||||
|
||||
return { block: true, message };
|
||||
}
|
||||
|
||||
function readStagedFilesSafe() {
|
||||
try {
|
||||
const out = execFileSync('git', ['diff', '--cached', '--name-only'], { encoding: 'utf-8' });
|
||||
return extractStagedFiles(out);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const raw = await readStdin();
|
||||
const event = parseEventJson(raw);
|
||||
if (event.tool_name !== 'Bash') { exitDecision({ block: false }); return; }
|
||||
const command = String((event.tool_input && event.tool_input.command) || '');
|
||||
if (!GIT_COMMIT_RE.test(command)) { exitDecision({ block: false }); return; }
|
||||
|
||||
const transcript = readTranscript(event.transcript_path);
|
||||
const userPrompt = lastUserPromptText(transcript);
|
||||
const assistantText = lastAssistantText(transcript);
|
||||
const sessionUses = sessionToolUses(transcript);
|
||||
const override = findOverride(userPrompt, RULE_KEY);
|
||||
if (override) logOverride(RULE_KEY, override, event.session_id);
|
||||
|
||||
const stagedFiles = readStagedFilesSafe();
|
||||
const semgrepRan = sessionRanSemgrep(sessionUses);
|
||||
|
||||
exitDecision(decide({ command, stagedFiles, semgrepRan, assistantText, override }));
|
||||
} catch {
|
||||
exitDecision({ block: false });
|
||||
}
|
||||
}
|
||||
|
||||
const isCli = process.argv[1] && process.argv[1].replace(/\\/g, '/').endsWith('/enforce-semgrep-security.mjs');
|
||||
if (isCli) main();
|
||||
@@ -1,180 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide, extractStagedFiles, isSecurityRelevantPath, sessionRanSemgrep } from './enforce-semgrep-security.mjs';
|
||||
import { findOverride } from './enforce-hook-helpers.mjs';
|
||||
|
||||
describe('isSecurityRelevantPath', () => {
|
||||
it('matches auth files', () => {
|
||||
expect(isSecurityRelevantPath('app/Http/Controllers/Auth/LoginController.php')).toBe(true);
|
||||
expect(isSecurityRelevantPath('app/Http/Middleware/Authenticate.php')).toBe(true);
|
||||
});
|
||||
it('matches billing/ledger files', () => {
|
||||
expect(isSecurityRelevantPath('app/Services/BillingService.php')).toBe(true);
|
||||
expect(isSecurityRelevantPath('app/Services/LedgerService.php')).toBe(true);
|
||||
});
|
||||
it('matches CSV import/export files', () => {
|
||||
expect(isSecurityRelevantPath('app/Imports/SupplierLeadsImport.php')).toBe(true);
|
||||
expect(isSecurityRelevantPath('app/Jobs/CsvReconcileJob.php')).toBe(true);
|
||||
expect(isSecurityRelevantPath('app/Http/Controllers/DealCsvController.php')).toBe(true);
|
||||
});
|
||||
it('matches webhook files', () => {
|
||||
expect(isSecurityRelevantPath('app/Http/Controllers/SupplierWebhookController.php')).toBe(true);
|
||||
expect(isSecurityRelevantPath('app/Services/WebhookSignatureVerifier.php')).toBe(true);
|
||||
});
|
||||
it('does NOT match docs/normal files', () => {
|
||||
expect(isSecurityRelevantPath('docs/superpowers/plans/2026-05-28-phase4.md')).toBe(false);
|
||||
expect(isSecurityRelevantPath('memory/feedback_communication.md')).toBe(false);
|
||||
expect(isSecurityRelevantPath('app/Models/Tenant.php')).toBe(false);
|
||||
expect(isSecurityRelevantPath('app/Http/Controllers/HomeController.php')).toBe(false);
|
||||
});
|
||||
it('returns false for null/empty', () => {
|
||||
expect(isSecurityRelevantPath(null)).toBe(false);
|
||||
expect(isSecurityRelevantPath('')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractStagedFiles', () => {
|
||||
it('parses git diff --cached --name-only output', () => {
|
||||
const stdout = 'app/Services/BillingService.php\napp/Models/Deal.php\n';
|
||||
expect(extractStagedFiles(stdout)).toEqual([
|
||||
'app/Services/BillingService.php',
|
||||
'app/Models/Deal.php',
|
||||
]);
|
||||
});
|
||||
it('skips blank lines', () => {
|
||||
expect(extractStagedFiles('a.php\n\nb.php\n')).toEqual(['a.php', 'b.php']);
|
||||
});
|
||||
it('returns [] for empty stdout', () => {
|
||||
expect(extractStagedFiles('')).toEqual([]);
|
||||
expect(extractStagedFiles(null)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sessionRanSemgrep', () => {
|
||||
it('returns true when a Bash tool_use ran semgrep CLI', () => {
|
||||
const sessionUses = [
|
||||
{ name: 'Bash', input: { command: 'pwd' } },
|
||||
{ name: 'Bash', input: { command: 'semgrep scan --config p/php' } },
|
||||
];
|
||||
expect(sessionRanSemgrep(sessionUses)).toBe(true);
|
||||
});
|
||||
it('returns true when "composer sast" ran', () => {
|
||||
expect(sessionRanSemgrep([{ name: 'Bash', input: { command: 'composer sast' } }])).toBe(true);
|
||||
expect(sessionRanSemgrep([{ name: 'Bash', input: { command: 'composer sast -- --diff' } }])).toBe(true);
|
||||
});
|
||||
it('returns true when "npm run sast" ran', () => {
|
||||
expect(sessionRanSemgrep([{ name: 'Bash', input: { command: 'npm run sast' } }])).toBe(true);
|
||||
});
|
||||
it('returns false when no semgrep-like command ran', () => {
|
||||
expect(sessionRanSemgrep([
|
||||
{ name: 'Bash', input: { command: 'git status' } },
|
||||
{ name: 'Bash', input: { command: 'npm test' } },
|
||||
])).toBe(false);
|
||||
});
|
||||
it('returns false for empty list', () => {
|
||||
expect(sessionRanSemgrep([])).toBe(false);
|
||||
});
|
||||
it('ignores tool_use that is not Bash', () => {
|
||||
expect(sessionRanSemgrep([{ name: 'Skill', input: { skill: 'semgrep' } }])).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('decide() — enforce-semgrep-security', () => {
|
||||
it('passes when command is NOT a git commit', () => {
|
||||
expect(decide({
|
||||
command: 'git status',
|
||||
stagedFiles: ['app/Services/BillingService.php'],
|
||||
semgrepRan: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
})).toEqual({ block: false });
|
||||
});
|
||||
it('passes when no security-relevant files in staged', () => {
|
||||
expect(decide({
|
||||
command: 'git commit -m "docs: update"',
|
||||
stagedFiles: ['docs/foo.md', 'memory/bar.md'],
|
||||
semgrepRan: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
})).toEqual({ block: false });
|
||||
});
|
||||
it('passes when Semgrep ran this session', () => {
|
||||
expect(decide({
|
||||
command: 'git commit -m "feat: billing"',
|
||||
stagedFiles: ['app/Services/BillingService.php'],
|
||||
semgrepRan: true,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
})).toEqual({ block: false });
|
||||
});
|
||||
it('passes with global override', () => {
|
||||
expect(decide({
|
||||
command: 'git commit -m "fix"',
|
||||
stagedFiles: ['app/Services/BillingService.php'],
|
||||
semgrepRan: false,
|
||||
assistantText: '',
|
||||
override: { phrase: 'срочно' },
|
||||
})).toEqual({ block: false });
|
||||
});
|
||||
it('passes with inline semgrep-skip with non-empty reason', () => {
|
||||
expect(decide({
|
||||
command: 'git commit -m "fix"',
|
||||
stagedFiles: ['app/Services/BillingService.php'],
|
||||
semgrepRan: false,
|
||||
assistantText: 'something\nsemgrep-skip: тривиальный docstring fix\nother',
|
||||
override: null,
|
||||
})).toEqual({ block: false });
|
||||
});
|
||||
it('does NOT pass with empty semgrep-skip reason', () => {
|
||||
const r = decide({
|
||||
command: 'git commit -m "fix"',
|
||||
stagedFiles: ['app/Services/BillingService.php'],
|
||||
semgrepRan: false,
|
||||
assistantText: 'semgrep-skip: ',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
});
|
||||
it('blocks when commit has security file + no Semgrep + no override', () => {
|
||||
const r = decide({
|
||||
command: 'git commit -m "feat: billing fix"',
|
||||
stagedFiles: ['app/Services/BillingService.php', 'app/Models/Deal.php'],
|
||||
semgrepRan: false,
|
||||
assistantText: '',
|
||||
override: null,
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.message).toContain('Semgrep');
|
||||
expect(r.message).toContain('BillingService');
|
||||
});
|
||||
});
|
||||
|
||||
describe('override vocab coverage', () => {
|
||||
it("global override \"без скилов\" suppresses semgrep-security", () => {
|
||||
const o = findOverride("без скилов", 'semgrep-security');
|
||||
expect(o).toBeTruthy();
|
||||
});
|
||||
it("global override \"direct ok\" suppresses semgrep-security", () => {
|
||||
const o = findOverride("direct ok", 'semgrep-security');
|
||||
expect(o).toBeTruthy();
|
||||
});
|
||||
it("global override \"срочно\" suppresses semgrep-security", () => {
|
||||
const o = findOverride("срочно", 'semgrep-security');
|
||||
expect(o).toBeTruthy();
|
||||
});
|
||||
it("global override \"быстрый коммит\" suppresses semgrep-security", () => {
|
||||
const o = findOverride("быстрый коммит", 'semgrep-security');
|
||||
expect(o).toBeTruthy();
|
||||
});
|
||||
it("global override \"recovery\" does NOT suppress semgrep-security (git-only scope)", () => {
|
||||
const o = findOverride("recovery", 'semgrep-security');
|
||||
expect(o).toBeFalsy();
|
||||
});
|
||||
it("global override \"memory dump\" suppresses semgrep-security", () => {
|
||||
const o = findOverride("memory dump", 'semgrep-security');
|
||||
expect(o).toBeTruthy();
|
||||
});
|
||||
it("global override \"ремонт инфраструктуры\" does NOT suppress semgrep-security (narrowed to verify-only)", () => {
|
||||
const o = findOverride("ремонт инфраструктуры\nремонт: test reason", 'semgrep-security');
|
||||
expect(o).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* PreToolUse(Edit|Write) wrapper for tools/tdd-real-test-verifier.mjs.
|
||||
* Router-gate v4 spec §3.11.
|
||||
*
|
||||
* Blocks Edit/Write on a *.test.* / *.spec.* file when the proposed content
|
||||
* lacks expect() / it() OR doesn't reference any of the prod files edited
|
||||
* this session (the sentinel-gaming guard from spec §3.11).
|
||||
*
|
||||
* Fail-CLOSE: an internal error blocks (security-hook default).
|
||||
*/
|
||||
import { fileURLToPath } from 'url';
|
||||
import { join } from 'path';
|
||||
import { existsSync, readFileSync } from 'fs';
|
||||
import {
|
||||
readStdin,
|
||||
parseEventJson,
|
||||
exitDecision,
|
||||
runtimeDir,
|
||||
} from './enforce-hook-helpers.mjs';
|
||||
import { verifyRealTestContent } from './tdd-real-test-verifier.mjs';
|
||||
|
||||
const TEST_FILE_RE = /.(?:test|spec).[a-z0-9]+$/i;
|
||||
|
||||
function readEditedFiles(sessionId) {
|
||||
try {
|
||||
const p = join(runtimeDir(), `edited-files-${sessionId || 'unknown'}.json`);
|
||||
if (!existsSync(p)) return [];
|
||||
const j = JSON.parse(readFileSync(p, 'utf-8'));
|
||||
return Array.isArray(j.files) ? j.files : [];
|
||||
} catch { return []; }
|
||||
}
|
||||
|
||||
export function decide({ filePath, content, editedFiles }) {
|
||||
const fp = String(filePath || '').split('\\').join('/');
|
||||
if (!TEST_FILE_RE.test(fp)) return { block: false, reason: null };
|
||||
const r = verifyRealTestContent(content, editedFiles || []);
|
||||
if (r.valid) return { block: false, reason: null };
|
||||
return { block: true, reason: r.reason };
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const raw = await readStdin();
|
||||
const event = parseEventJson(raw);
|
||||
if (event.tool_name !== 'Edit' && event.tool_name !== 'Write') {
|
||||
return exitDecision({ block: false });
|
||||
}
|
||||
const filePath = event.tool_input?.file_path || '';
|
||||
const content = event.tool_input?.content || event.tool_input?.new_string || '';
|
||||
const sessionId = event.session_id || 'unknown';
|
||||
const editedFiles = readEditedFiles(sessionId);
|
||||
const r = decide({ filePath, content, editedFiles });
|
||||
if (r.block) {
|
||||
return exitDecision({
|
||||
block: true,
|
||||
message: `[tdd-real-test-verifier] proposed test file fails real-test check: ${r.reason}. Write a test that asserts behaviour (expect + it/test) and references one of the edited prod files.`,
|
||||
});
|
||||
}
|
||||
return exitDecision({ block: false });
|
||||
} catch {
|
||||
return exitDecision({ block: true, message: '[tdd-real-test-verifier] внутренняя ошибка — fail-CLOSE' });
|
||||
}
|
||||
}
|
||||
|
||||
const isCli = process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1];
|
||||
if (isCli) main();
|
||||
@@ -0,0 +1,39 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide } from './enforce-tdd-real-test-verifier.mjs';
|
||||
|
||||
describe('enforce-tdd-real-test-verifier decide()', () => {
|
||||
it('allows real test with expect + it covering edited prod file', () => {
|
||||
const r = decide({
|
||||
filePath: 'tools/foo.test.mjs',
|
||||
content: "import { foo } from './foo.mjs';\nit('foo works', () => { expect(foo()).toBe(1); });",
|
||||
editedFiles: ['tools/foo.mjs'],
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
it('blocks test with no expect()', () => {
|
||||
const r = decide({
|
||||
filePath: 'tools/foo.test.mjs',
|
||||
content: "it('does nothing', () => { /* sentinel */ });",
|
||||
editedFiles: ['tools/foo.mjs'],
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/no_expect_call/);
|
||||
});
|
||||
it('blocks test with no it/test block', () => {
|
||||
const r = decide({
|
||||
filePath: 'tools/foo.test.mjs',
|
||||
content: "expect(1).toBe(1);",
|
||||
editedFiles: ['tools/foo.mjs'],
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/no_test_block/);
|
||||
});
|
||||
it('allows when filePath is not a test file (no-op)', () => {
|
||||
const r = decide({
|
||||
filePath: 'tools/foo.mjs',
|
||||
content: 'export const foo = 1;',
|
||||
editedFiles: [],
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Stop-hook wrapper for tools/todowrite-skill-verifier.mjs.
|
||||
* Router-gate v4 spec §3.9 + v4.1 Direction 4.
|
||||
*
|
||||
* Reads transcript at Stop, extracts last TodoWrite items + Skill calls,
|
||||
* blocks next mutating when a completed TodoWrite item claims a Skill that was
|
||||
* never actually invoked.
|
||||
*
|
||||
* Fail-open on internal error (Stop must not freeze sessions).
|
||||
*/
|
||||
import { fileURLToPath } from 'url';
|
||||
import {
|
||||
readStdin,
|
||||
parseEventJson,
|
||||
readTranscript,
|
||||
exitDecision,
|
||||
} from './enforce-hook-helpers.mjs';
|
||||
import {
|
||||
extractSkillMentions,
|
||||
extractSkillToolCalls,
|
||||
hardSyncCheck,
|
||||
} from './todowrite-skill-verifier.mjs';
|
||||
|
||||
/** Find the latest TodoWrite tool_use entries in a transcript. */
|
||||
export function lastTodoItems(transcript) {
|
||||
const recs = transcript || [];
|
||||
for (let i = recs.length - 1; i >= 0; i--) {
|
||||
const r = recs[i];
|
||||
if (r && r.type === 'tool_use' && r.name === 'TodoWrite') {
|
||||
const items =
|
||||
r.input && Array.isArray(r.input.todos) ? r.input.todos : [];
|
||||
return items;
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Deduplicate skill mentions produced by extractSkillMentions.
|
||||
*
|
||||
* When a TodoWrite item contains "invoke superpowers:brainstorming", the pure
|
||||
* module emits two mentions: the full "superpowers:brainstorming" (from the
|
||||
* /superpowers:[a-z-]+/ pattern) and the partial "superpowers" (from the
|
||||
* /invoke ([a-z][a-z0-9-]*)/ pattern that stops at the colon).
|
||||
*
|
||||
* The partial is a regex-extraction artifact: if any other mention from the
|
||||
* same text item starts with "<partial>:", the partial is redundant and safe
|
||||
* to drop — the full-name mention will be checked instead.
|
||||
*/
|
||||
export function deduplicateMentions(mentions) {
|
||||
return mentions.filter((m) => {
|
||||
const nameColon = m.skill_name + ':';
|
||||
const coveredByLonger = mentions.some(
|
||||
(other) =>
|
||||
other !== m &&
|
||||
other.text === m.text &&
|
||||
other.skill_name.startsWith(nameColon),
|
||||
);
|
||||
return !coveredByLonger;
|
||||
});
|
||||
}
|
||||
|
||||
export function decide({ todoItems, transcript }) {
|
||||
const items = Array.isArray(todoItems) ? todoItems : [];
|
||||
const rawMentions = extractSkillMentions(items);
|
||||
const mentions = deduplicateMentions(rawMentions);
|
||||
const calls = extractSkillToolCalls(transcript || []);
|
||||
const r = hardSyncCheck(mentions, calls);
|
||||
if (r.action === 'hard_block_next_mutating') {
|
||||
return { block: true, reason: r.reason };
|
||||
}
|
||||
return { block: false, reason: null };
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const raw = await readStdin();
|
||||
const event = parseEventJson(raw);
|
||||
const transcript = readTranscript(event.transcript_path);
|
||||
const todoItems = lastTodoItems(transcript);
|
||||
const r = decide({ todoItems, transcript });
|
||||
if (r.block) {
|
||||
return exitDecision({
|
||||
block: true,
|
||||
message: `[todowrite-skill-verifier] ${r.reason}`,
|
||||
});
|
||||
}
|
||||
return exitDecision({ block: false });
|
||||
} catch {
|
||||
return exitDecision({ block: false }); // fail-open
|
||||
}
|
||||
}
|
||||
|
||||
const isCli =
|
||||
process.argv[1] &&
|
||||
fileURLToPath(import.meta.url) === process.argv[1];
|
||||
if (isCli) main();
|
||||
@@ -0,0 +1,32 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide } from './enforce-todowrite-skill-verifier.mjs';
|
||||
|
||||
describe('enforce-todowrite-skill-verifier decide()', () => {
|
||||
it('allows when all completed mentions actually invoked Skill', () => {
|
||||
const todoItems = [
|
||||
{ content: 'invoke superpowers:brainstorming', status: 'completed' },
|
||||
];
|
||||
const transcript = [
|
||||
{ type: 'tool_use', name: 'Skill', input: { skill: 'superpowers:brainstorming' } },
|
||||
];
|
||||
expect(decide({ todoItems, transcript }).block).toBe(false);
|
||||
});
|
||||
it('blocks when completed mention has NO matching Skill call', () => {
|
||||
const todoItems = [
|
||||
{ content: 'invoke superpowers:brainstorming', status: 'completed' },
|
||||
];
|
||||
const transcript = [];
|
||||
const r = decide({ todoItems, transcript });
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/v4\.1 TodoWrite hard sync/);
|
||||
});
|
||||
it('allows when mention is pending (not completed)', () => {
|
||||
const todoItems = [
|
||||
{ content: 'invoke superpowers:brainstorming', status: 'pending' },
|
||||
];
|
||||
expect(decide({ todoItems, transcript: [] }).block).toBe(false);
|
||||
});
|
||||
it('allows when todoItems is empty', () => {
|
||||
expect(decide({ todoItems: [], transcript: [] }).block).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* PreToolUse(Workflow) hook — Workflow gate F2 (router-gate v4 spec §3.6 / v3.8 F2).
|
||||
*
|
||||
* Closes:
|
||||
* - scriptPath must be pre-approved via approve_workflow_script record + sha256 match
|
||||
* - scriptContent static scan for dangerous patterns (env keys, eval, child_process, fs writes outside .scratch/tmp)
|
||||
* - resumeFromRunId blocked unconditionally (state replay risk)
|
||||
* - per-agent gate inheritance handled by subagent-prompt-prefix.mjs (Stream E); this hook focuses on the outer
|
||||
* Workflow tool call. Nested agent() inside Workflow inherits parent gate via CLAUDE_GATE_INHERIT env.
|
||||
*/
|
||||
import { readFileSync, existsSync } from 'node:fs';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { homedir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const APPROVE_WINDOW_MS = 5 * 60 * 1000;
|
||||
|
||||
// NOTE: this hook DETECTS dangerous patterns in user-supplied workflow scripts;
|
||||
// none of the regexes below are executed via eval/exec/child_process by this hook itself.
|
||||
// `/\beval\s*\(/i` and `/\b(?:exec|spawn|...)\s*\(/` are pattern-matchers, not invocations.
|
||||
const DANGEROUS_PATTERNS = [
|
||||
{ re: /process\.env\.(ROUTER_LLM_KEY|ANTHROPIC_API_KEY|GITHUB_TOKEN|SENTRY_AUTH_TOKEN)/i, name: 'env key access (ROUTER_LLM_KEY)' },
|
||||
{ re: /\beval\s*\(/i, name: 'eval()' },
|
||||
{ re: /\b(?:exec|spawn|execSync|spawnSync|execFile|fork)\s*\(/, name: 'child_process' },
|
||||
{ re: /\bwriteFileSync\s*\(\s*["'`]\/(?!tmp\/|var\/tmp\/)/i, name: 'fs write absolute' },
|
||||
{ re: /\.\.\/\.\.\/\.\.\//, name: 'path traversal' },
|
||||
];
|
||||
|
||||
export function decide({ toolInput, approvedWorkflowScripts, scriptContent, scriptSha256, now }) {
|
||||
// 1. resumeFromRunId blocked unconditionally
|
||||
if (toolInput && toolInput.resumeFromRunId) {
|
||||
return { block: true, reason: 'F2: resumeFromRunId disabled (state replay risk)' };
|
||||
}
|
||||
|
||||
const scriptPath = toolInput && toolInput.scriptPath;
|
||||
if (!scriptPath) {
|
||||
// inline script via `script` param — different code path; outside this hook's scope (F2 follow-up).
|
||||
return { block: false };
|
||||
}
|
||||
|
||||
// 2. scriptPath must be approved
|
||||
const approval = (approvedWorkflowScripts || []).find(
|
||||
(a) => a.scriptPath === scriptPath && typeof a.ts === 'number' && now - a.ts <= APPROVE_WINDOW_MS,
|
||||
);
|
||||
if (!approval) {
|
||||
return { block: true, reason: `F2: workflow ${scriptPath} requires approve_workflow_script (5-min window)` };
|
||||
}
|
||||
|
||||
// 3. sha256 match (content unchanged since approval)
|
||||
if (approval.sha256 && scriptSha256 && approval.sha256 !== scriptSha256) {
|
||||
return { block: true, reason: 'F2: scriptPath sha256 mismatch — content modified after approval' };
|
||||
}
|
||||
|
||||
// 4. dangerous pattern scan
|
||||
for (const { re, name } of DANGEROUS_PATTERNS) {
|
||||
if (re.test(scriptContent || '')) {
|
||||
return { block: true, reason: `F2: workflow script contains dangerous pattern — ${name}` };
|
||||
}
|
||||
}
|
||||
|
||||
return { block: false };
|
||||
}
|
||||
|
||||
export function loadApprovedWorkflowScripts(sessionId, now = Date.now()) {
|
||||
const path = join(homedir(), '.claude', 'runtime', `askuser-decisions-${sessionId || 'unknown'}.jsonl`);
|
||||
if (!existsSync(path)) return [];
|
||||
const out = [];
|
||||
try {
|
||||
const lines = readFileSync(path, 'utf-8').split(/\r?\n/);
|
||||
for (const line of lines) {
|
||||
if (!line.trim()) continue;
|
||||
let rec;
|
||||
try { rec = JSON.parse(line); } catch { continue; }
|
||||
if (rec && rec.type === 'approve_workflow_script' && typeof rec.scriptPath === 'string') {
|
||||
out.push({ scriptPath: rec.scriptPath, sha256: rec.sha256 || null, ts: typeof rec.ts === 'number' ? rec.ts : 0 });
|
||||
}
|
||||
}
|
||||
} catch { return []; }
|
||||
return out.filter((op) => now - op.ts <= APPROVE_WINDOW_MS);
|
||||
}
|
||||
|
||||
export function sha256Hex(content) {
|
||||
return createHash('sha256').update(content || '', 'utf-8').digest('hex');
|
||||
}
|
||||
|
||||
async function main() {
|
||||
let input = '';
|
||||
for await (const chunk of process.stdin) input += chunk;
|
||||
let payload;
|
||||
try { payload = JSON.parse(input); } catch { return; }
|
||||
|
||||
const { tool_input, session_id } = payload || {};
|
||||
if (!tool_input) return;
|
||||
|
||||
const scriptPath = tool_input.scriptPath;
|
||||
let scriptContent = '';
|
||||
let scriptSha256 = '';
|
||||
if (scriptPath && existsSync(scriptPath)) {
|
||||
try {
|
||||
scriptContent = readFileSync(scriptPath, 'utf-8');
|
||||
scriptSha256 = sha256Hex(scriptContent);
|
||||
} catch { /* content read errors fall through to decide() which will handle scriptContent='' */ }
|
||||
}
|
||||
|
||||
const approved = loadApprovedWorkflowScripts(session_id, Date.now());
|
||||
const r = decide({ toolInput: tool_input, approvedWorkflowScripts: approved, scriptContent, scriptSha256, now: Date.now() });
|
||||
|
||||
if (r.block) {
|
||||
process.stderr.write(`[workflow-gate] ${r.reason}\n`);
|
||||
process.exit(2);
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1].replace(/\\/g, '/')}` || process.argv[1].endsWith('enforce-workflow-gate.mjs')) {
|
||||
main().catch((e) => { process.stderr.write(`[workflow-gate] internal error: ${e.message}\n`); process.exit(2); });
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
// Stream H Task 3 — Workflow gate F2 unit tests (TDD).
|
||||
// References ./enforce-workflow-gate.mjs (the prod file under TDD).
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { decide } from './enforce-workflow-gate.mjs';
|
||||
|
||||
describe('enforce-workflow-gate scriptPath approval (F2)', () => {
|
||||
it('blocks Workflow with new scriptPath without approval (RED phase, no prod file yet)', () => {
|
||||
const r = decide({
|
||||
toolInput: { scriptPath: 'workflows/new-untested.mjs' },
|
||||
approvedWorkflowScripts: [],
|
||||
scriptContent: 'export const meta = {name:"x",description:"y"}\nphase("X")',
|
||||
now: Date.now(),
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/F2.*approve_workflow_script/i);
|
||||
});
|
||||
|
||||
it('allows Workflow with approved scriptPath within 5min window', () => {
|
||||
const now = Date.now();
|
||||
const r = decide({
|
||||
toolInput: { scriptPath: 'workflows/x.mjs' },
|
||||
approvedWorkflowScripts: [{ scriptPath: 'workflows/x.mjs', sha256: 'a'.repeat(64), ts: now }],
|
||||
scriptContent: 'export const meta={name:"x",description:"y"}',
|
||||
scriptSha256: 'a'.repeat(64),
|
||||
now,
|
||||
});
|
||||
expect(r.block).toBe(false);
|
||||
});
|
||||
|
||||
it('blocks Workflow with resumeFromRunId param (F2 hardening)', () => {
|
||||
const r = decide({
|
||||
toolInput: { scriptPath: 'workflows/x.mjs', resumeFromRunId: 'wf_abc123' },
|
||||
approvedWorkflowScripts: [{ scriptPath: 'workflows/x.mjs', sha256: 'a'.repeat(64), ts: Date.now() }],
|
||||
scriptContent: 'x',
|
||||
scriptSha256: 'a'.repeat(64),
|
||||
now: Date.now(),
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/resumeFromRunId/);
|
||||
});
|
||||
|
||||
it('blocks Workflow whose scriptContent has dangerous pattern', () => {
|
||||
const r = decide({
|
||||
toolInput: { scriptPath: 'workflows/x.mjs' },
|
||||
approvedWorkflowScripts: [{ scriptPath: 'workflows/x.mjs', sha256: 'a'.repeat(64), ts: Date.now() }],
|
||||
scriptContent: 'process.env.ROUTER_LLM_KEY',
|
||||
scriptSha256: 'a'.repeat(64),
|
||||
now: Date.now(),
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/dangerous pattern.*ROUTER_LLM_KEY/i);
|
||||
});
|
||||
|
||||
it('blocks Workflow with sha256 mismatch (content changed since approval)', () => {
|
||||
const r = decide({
|
||||
toolInput: { scriptPath: 'workflows/x.mjs' },
|
||||
approvedWorkflowScripts: [{ scriptPath: 'workflows/x.mjs', sha256: 'a'.repeat(64), ts: Date.now() }],
|
||||
scriptContent: 'modified',
|
||||
scriptSha256: 'b'.repeat(64),
|
||||
now: Date.now(),
|
||||
});
|
||||
expect(r.block).toBe(true);
|
||||
expect(r.reason).toMatch(/sha256.*mismatch/i);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
// tools/parallel-session-lock.mjs
|
||||
/**
|
||||
* Pure parallel-session lock — router-gate v4 Stream H Task 7.
|
||||
*
|
||||
* Prevents two Claude sessions on the same workspace from concurrently
|
||||
* mutating files. Lock file lives at
|
||||
* ~/.claude/runtime/session-lock-<workspaceHash>.json
|
||||
* with TTL-based stale recovery (default 5 minutes).
|
||||
*
|
||||
* I/O is injected (readLock/writeLock/deleteLock) so this module stays pure
|
||||
* and unit-testable. The wrapper hook (enforce-parallel-session-lock.mjs)
|
||||
* binds real fs implementations.
|
||||
*
|
||||
* Lock format:
|
||||
* { schema_version: 1, session_id, pid, acquired_at: <ms>, ttl_ms }
|
||||
*/
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
export const LOCK_SCHEMA_VERSION = 1;
|
||||
export const LOCK_DEFAULT_TTL_MS = 5 * 60 * 1000;
|
||||
|
||||
/** Derive a deterministic 12-char hex workspace hash from a path. */
|
||||
export function computeWorkspaceHash(workspacePath) {
|
||||
return createHash('md5').update(String(workspacePath || ''), 'utf-8').digest('hex').slice(0, 12);
|
||||
}
|
||||
|
||||
function isStale(record, now) {
|
||||
if (!record || typeof record !== 'object') return true;
|
||||
const ttl = typeof record.ttl_ms === 'number' ? record.ttl_ms : LOCK_DEFAULT_TTL_MS;
|
||||
return now - (record.acquired_at || 0) > ttl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to acquire the lock for sessionId. Takes over stale or same-session locks.
|
||||
*
|
||||
* @param {object} args
|
||||
* @param {string} args.sessionId
|
||||
* @param {number} args.pid
|
||||
* @param {string} args.workspaceHash
|
||||
* @param {number} args.now - unix ms
|
||||
* @param {function} args.readLock - () => record | null
|
||||
* @param {function} args.writeLock - (record) => void
|
||||
* @param {number} [args.ttlMs] - override default TTL
|
||||
* @returns {{acquired: boolean, holder?: {session_id, pid, acquired_at}}}
|
||||
*/
|
||||
export function acquire({ sessionId, pid, workspaceHash, now, readLock, writeLock, ttlMs = LOCK_DEFAULT_TTL_MS }) {
|
||||
const existing = readLock();
|
||||
// Stale OR same-session → take over.
|
||||
if (!existing || isStale(existing, now) || existing.session_id === sessionId) {
|
||||
const record = {
|
||||
schema_version: LOCK_SCHEMA_VERSION,
|
||||
session_id: sessionId,
|
||||
pid,
|
||||
acquired_at: now,
|
||||
ttl_ms: ttlMs,
|
||||
};
|
||||
writeLock(record);
|
||||
return { acquired: true, holder: { session_id: sessionId, pid, acquired_at: now } };
|
||||
}
|
||||
// Fresh lock from another session — blocked.
|
||||
return {
|
||||
acquired: false,
|
||||
holder: { session_id: existing.session_id, pid: existing.pid, acquired_at: existing.acquired_at },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Same-session refresh — bumps acquired_at if we still own the lock.
|
||||
* Other-session refresh is a no-op (does not steal).
|
||||
*
|
||||
* @returns {{refreshed: boolean}}
|
||||
*/
|
||||
export function refresh({ sessionId, workspaceHash, now, readLock, writeLock, ttlMs = LOCK_DEFAULT_TTL_MS }) {
|
||||
const existing = readLock();
|
||||
if (!existing || existing.session_id !== sessionId) return { refreshed: false };
|
||||
writeLock({
|
||||
schema_version: LOCK_SCHEMA_VERSION,
|
||||
session_id: sessionId,
|
||||
pid: existing.pid,
|
||||
acquired_at: now,
|
||||
ttl_ms: ttlMs,
|
||||
});
|
||||
return { refreshed: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the lock if we own it; no-op otherwise.
|
||||
*/
|
||||
export function release({ sessionId, workspaceHash, readLock, deleteLock }) {
|
||||
const existing = readLock();
|
||||
if (existing && existing.session_id === sessionId) deleteLock();
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
// tools/parallel-session-lock.test.mjs
|
||||
// Stream H Task 7 — pure parallel-session-lock module tests.
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import {
|
||||
acquire,
|
||||
release,
|
||||
refresh,
|
||||
computeWorkspaceHash,
|
||||
LOCK_DEFAULT_TTL_MS,
|
||||
} from './parallel-session-lock.mjs';
|
||||
|
||||
function mkMockStore(initial = null) {
|
||||
let current = initial;
|
||||
return {
|
||||
readLock: () => current,
|
||||
writeLock: (v) => { current = v; },
|
||||
deleteLock: () => { current = null; },
|
||||
peek: () => current,
|
||||
};
|
||||
}
|
||||
|
||||
describe('parallel-session-lock pure module (Stream H Task 7)', () => {
|
||||
it('acquire on empty store succeeds and writes holder record', () => {
|
||||
const store = mkMockStore(null);
|
||||
const r = acquire({
|
||||
sessionId: 's1', pid: 100, workspaceHash: 'abc', now: 1000,
|
||||
readLock: store.readLock, writeLock: store.writeLock,
|
||||
});
|
||||
expect(r.acquired).toBe(true);
|
||||
expect(r.holder).toMatchObject({ session_id: 's1', pid: 100, acquired_at: 1000 });
|
||||
expect(store.peek()).toMatchObject({ schema_version: 1, session_id: 's1', pid: 100, acquired_at: 1000, ttl_ms: LOCK_DEFAULT_TTL_MS });
|
||||
});
|
||||
|
||||
it('acquire blocked when a fresh lock from another session exists', () => {
|
||||
const existing = { schema_version: 1, session_id: 'other', pid: 999, acquired_at: 500, ttl_ms: LOCK_DEFAULT_TTL_MS };
|
||||
const store = mkMockStore(existing);
|
||||
const r = acquire({
|
||||
sessionId: 's1', pid: 100, workspaceHash: 'abc', now: 1000,
|
||||
readLock: store.readLock, writeLock: store.writeLock,
|
||||
});
|
||||
expect(r.acquired).toBe(false);
|
||||
expect(r.holder).toMatchObject({ session_id: 'other', pid: 999 });
|
||||
expect(store.peek()).toBe(existing); // unchanged
|
||||
});
|
||||
|
||||
it('acquire takes over a stale lock from another session (past TTL)', () => {
|
||||
const existing = { schema_version: 1, session_id: 'old', pid: 7, acquired_at: 0, ttl_ms: 100 };
|
||||
const store = mkMockStore(existing);
|
||||
const r = acquire({
|
||||
sessionId: 's1', pid: 100, workspaceHash: 'abc', now: 1000,
|
||||
readLock: store.readLock, writeLock: store.writeLock,
|
||||
});
|
||||
expect(r.acquired).toBe(true);
|
||||
expect(r.holder).toMatchObject({ session_id: 's1', pid: 100, acquired_at: 1000 });
|
||||
});
|
||||
|
||||
it('refresh same-session updates acquired_at without losing ownership', () => {
|
||||
const existing = { schema_version: 1, session_id: 's1', pid: 100, acquired_at: 500, ttl_ms: LOCK_DEFAULT_TTL_MS };
|
||||
const store = mkMockStore(existing);
|
||||
const r = refresh({
|
||||
sessionId: 's1', workspaceHash: 'abc', now: 1000,
|
||||
readLock: store.readLock, writeLock: store.writeLock,
|
||||
});
|
||||
expect(r.refreshed).toBe(true);
|
||||
expect(store.peek().acquired_at).toBe(1000);
|
||||
});
|
||||
|
||||
it('refresh other-session is a no-op (does not steal lock)', () => {
|
||||
const existing = { schema_version: 1, session_id: 'other', pid: 999, acquired_at: 500, ttl_ms: LOCK_DEFAULT_TTL_MS };
|
||||
const store = mkMockStore(existing);
|
||||
const r = refresh({
|
||||
sessionId: 's1', workspaceHash: 'abc', now: 1000,
|
||||
readLock: store.readLock, writeLock: store.writeLock,
|
||||
});
|
||||
expect(r.refreshed).toBe(false);
|
||||
expect(store.peek()).toBe(existing);
|
||||
});
|
||||
|
||||
it('release same-session deletes the lock', () => {
|
||||
const existing = { schema_version: 1, session_id: 's1', pid: 100, acquired_at: 500, ttl_ms: LOCK_DEFAULT_TTL_MS };
|
||||
const store = mkMockStore(existing);
|
||||
release({ sessionId: 's1', workspaceHash: 'abc', readLock: store.readLock, deleteLock: store.deleteLock });
|
||||
expect(store.peek()).toBe(null);
|
||||
});
|
||||
|
||||
it('release other-session does NOT delete the lock', () => {
|
||||
const existing = { schema_version: 1, session_id: 'other', pid: 999, acquired_at: 500, ttl_ms: LOCK_DEFAULT_TTL_MS };
|
||||
const store = mkMockStore(existing);
|
||||
release({ sessionId: 's1', workspaceHash: 'abc', readLock: store.readLock, deleteLock: store.deleteLock });
|
||||
expect(store.peek()).toBe(existing);
|
||||
});
|
||||
});
|
||||
|
||||
describe('computeWorkspaceHash (Stream H Task 7)', () => {
|
||||
it('returns 12 hex chars', () => {
|
||||
const h = computeWorkspaceHash('/some/path');
|
||||
expect(h).toMatch(/^[0-9a-f]{12}$/);
|
||||
});
|
||||
it('is deterministic per path', () => {
|
||||
expect(computeWorkspaceHash('/some/path')).toBe(computeWorkspaceHash('/some/path'));
|
||||
});
|
||||
it('differs across paths', () => {
|
||||
expect(computeWorkspaceHash('/a')).not.toBe(computeWorkspaceHash('/b'));
|
||||
});
|
||||
});
|
||||
@@ -25,6 +25,9 @@ export function expandEnvVars(target, env) {
|
||||
if (val === undefined) continue;
|
||||
out = out.split(`%${name}%`).join(val);
|
||||
out = out.split(`\${${name}}`).join(val);
|
||||
// Stream H Task 9 cosmetic: PowerShell `$env:NAME` syntax — case-insensitive
|
||||
// match because PowerShell is case-insensitive (`$env:USERPROFILE` ≡ `$env:userprofile`).
|
||||
out = out.replace(new RegExp(`\\$env:${name}(?![A-Za-z0-9_])`, 'gi'), () => val);
|
||||
// bare $VAR — only when followed by non-word boundary.
|
||||
// Use a function replacer so `val` is inserted literally (avoids $& / $' / $` replacement-pattern misinterpretation).
|
||||
out = out.replace(new RegExp(`\\$${name}(?![A-Za-z0-9_])`, 'g'), () => val);
|
||||
@@ -86,6 +89,18 @@ export function pathNormalize(target, opts = {}) {
|
||||
} = opts;
|
||||
let p = expandHome(target, homedir);
|
||||
p = expandEnvVars(p, env);
|
||||
// Stream H Task 9 cosmetic: detect Cygwin/git-bash drive-prefix style `/c/Users/...`
|
||||
// and convert to native `c:/Users/...` BEFORE resolve. Without this, path.resolve
|
||||
// on win32 treats `/c/...` as drive-relative and prepends cwd's drive letter,
|
||||
// producing display paths like `c:/c/users/...` (doubled drive) in gate error
|
||||
// messages. Detected during Smoke 5 Real Fix Re-test 2026-05-30 (step 4).
|
||||
//
|
||||
// Guard: only apply on win32 AND when the supplied homedir itself looks
|
||||
// drive-rooted (contains `<letter>:`). This avoids breaking POSIX-style test
|
||||
// fixtures that pass `/h` or `/home/u` and expect /A/B-style paths to stay raw.
|
||||
if (platform === 'win32' && /^[a-zA-Z]:/.test(String(homedir || ''))) {
|
||||
p = p.replace(/^\/([a-zA-Z])\//, (_, drive) => `${drive}:/`);
|
||||
}
|
||||
const resolved = resolve(p);
|
||||
let real;
|
||||
try {
|
||||
@@ -94,5 +109,8 @@ export function pathNormalize(target, opts = {}) {
|
||||
if (e && e.code && e.code !== 'ENOENT') throw e; // surface real FS errors; fail-close handled by caller
|
||||
real = resolved; // ENOENT — best-effort resolved path for unknown-state files
|
||||
}
|
||||
return caseFold(real, platform);
|
||||
// Smoke 5 integration fix (2026-05-30): normalize ALL separators to forward slashes
|
||||
// regardless of platform. DEFAULT_PROTECTED_PATTERNS regexes are forward-slash only.
|
||||
// Without this, win32 path.resolve + realpath returns backslashes and patterns miss.
|
||||
return caseFold(real, platform).split('\\').join('/');
|
||||
}
|
||||
|
||||
@@ -78,6 +78,20 @@ describe('pathNormalize', () => {
|
||||
it('expands ~ and resolves', () => {
|
||||
expect(pathNormalize('~/x', { homedir: '/h', realpath: (p) => p, resolve: (p) => p, platform: 'linux', env: {} })).toBe('/h/x');
|
||||
});
|
||||
// Smoke 5 integration bug 2026-05-30 — Stream A pathNormalize returned backslashes
|
||||
// on win32, breaking DEFAULT_PROTECTED_PATTERNS regex (forward-slash-only) match.
|
||||
// Fix: normalize all separators to forward slashes in output, regardless of platform.
|
||||
it('normalizes backslashes to forward slashes on win32 (integration fix for protected patterns)', () => {
|
||||
const result = pathNormalize('~/foo/bar.jsonl', {
|
||||
homedir: 'C:\\Users\\Admin',
|
||||
realpath: (p) => p,
|
||||
resolve: path.win32.resolve,
|
||||
platform: 'win32',
|
||||
env: {},
|
||||
});
|
||||
expect(result).not.toMatch(/\\/);
|
||||
expect(result.includes('/')).toBe(true);
|
||||
});
|
||||
it('collapses .. via path.resolve', () => {
|
||||
// Use path.posix.resolve to simulate POSIX behaviour cross-platform
|
||||
const result = pathNormalize('a/../b', { realpath: (p) => p, resolve: path.posix.resolve, platform: 'linux', homedir: '/h', env: {} });
|
||||
@@ -115,4 +129,34 @@ describe('pathNormalize', () => {
|
||||
it('case-folds on win32', () => {
|
||||
expect(pathNormalize('/A/B', { realpath: (p) => p, resolve: (p) => p, platform: 'win32', homedir: '/h', env: {} })).toBe('/a/b');
|
||||
});
|
||||
|
||||
// Stream H Task 9 cosmetic: /c/Users/... (Cygwin/git-bash) → c:/Users/...
|
||||
// Without this, win32 path.resolve('/c/Users/x') prepends cwd's drive letter
|
||||
// → c:/c/users/... (doubled drive). Detected during Smoke 5 Real Fix Re-test
|
||||
// 2026-05-30 (step 4 path display).
|
||||
it('normalizes /c/Users (cygwin/git-bash drive prefix) to c:/Users before resolve (win32)', () => {
|
||||
const r = pathNormalize('/c/Users/Admin/.claude/projects/x.jsonl', {
|
||||
homedir: 'C:\\Users\\Admin',
|
||||
realpath: (p) => p,
|
||||
resolve: path.win32.resolve,
|
||||
platform: 'win32',
|
||||
env: {},
|
||||
});
|
||||
// No double c:/c/, has single c:/users
|
||||
expect(r).not.toMatch(/c:\/c\//);
|
||||
expect(r).toMatch(/c:\/users\/admin/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('expandEnvVars — PowerShell $env:VAR (Stream H Task 9 cosmetic)', () => {
|
||||
it('expands $env:USERPROFILE to actual home path', () => {
|
||||
expect(expandEnvVars('$env:USERPROFILE/.claude/projects/x.jsonl', { USERPROFILE: 'C:/Users/Admin' }))
|
||||
.toBe('C:/Users/Admin/.claude/projects/x.jsonl');
|
||||
});
|
||||
it('expands $env:HOME (whitelisted) too', () => {
|
||||
expect(expandEnvVars('$env:HOME/x', { HOME: '/h' })).toBe('/h/x');
|
||||
});
|
||||
it('does not expand non-whitelisted $env:SECRET', () => {
|
||||
expect(expandEnvVars('$env:SECRET/x', { SECRET: '/s' })).toBe('$env:SECRET/x');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -23,6 +23,9 @@ export function defaultPathNormalize(target) {
|
||||
export const DEFAULT_PROTECTED_PATTERNS = [
|
||||
/(^|\/)\.claude\/runtime(\/|$)/i,
|
||||
/(^|\/)\.claude\/settings(\.local)?\.json$/i,
|
||||
// Smoke 5 emergency fix (2026-05-30) — transcript JSONL hard-deny (spec §3.1 was declared, not implemented).
|
||||
// Prevents self-exfil of parent context across sessions via Bash cat / PowerShell Get-Content / Read tool.
|
||||
/(^|\/)\.claude\/projects(\/|$)/i,
|
||||
/(^|\/)\.env(\.|$)/i,
|
||||
/(^|\/)node_modules\//i,
|
||||
/(^|\/)CLAUDE\.md$/i,
|
||||
@@ -56,7 +59,34 @@ export function matchAny(patterns, str) {
|
||||
|
||||
export function extractPathArgs(tokens) {
|
||||
if (!Array.isArray(tokens)) return [];
|
||||
return tokens.slice(1).filter((t) => typeof t === 'string' && !t.startsWith('-') && t !== '>' && t !== '>>');
|
||||
const out = [];
|
||||
for (let i = 1; i < tokens.length; i++) {
|
||||
const t = tokens[i];
|
||||
if (typeof t !== 'string') continue;
|
||||
if (t === '>' || t === '>>' || t === '<' || t === '|') continue;
|
||||
// --flag=VALUE form
|
||||
if (t.startsWith('-')) {
|
||||
const eq = t.indexOf('=');
|
||||
if (eq > 0) {
|
||||
const v = t.slice(eq + 1);
|
||||
if (v && !looksLikeUrl(v)) out.push(v);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// key=value form (dd-style)
|
||||
const kv = t.match(/^([a-zA-Z_][\w-]*)=(.+)$/);
|
||||
if (kv) {
|
||||
const v = kv[2];
|
||||
if (v && !looksLikeUrl(v)) out.push(v);
|
||||
continue;
|
||||
}
|
||||
if (!looksLikeUrl(t)) out.push(t);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function looksLikeUrl(s) {
|
||||
return /^https?:\/\//i.test(s) || /^ftp:\/\//i.test(s) || /^ssh:\/\//i.test(s);
|
||||
}
|
||||
|
||||
export function pathDenyOverlay({
|
||||
@@ -113,9 +143,10 @@ export function loadApprovedGitOps(sessionId, now = Date.now()) {
|
||||
const GIT_READONLY_SUB = new Set([
|
||||
'status', 'log', 'show', 'diff', 'blame', 'format-patch',
|
||||
'rev-parse', 'merge-base', 'remote', 'stash', // stash list/show resolved below
|
||||
'fetch', 'ls-remote', // ref-only, no working-tree mutation — Stream H pre-flight requires §15.2 sync
|
||||
]);
|
||||
const GIT_CONDITIONAL_SUB = new Set([
|
||||
'commit', 'merge', 'rebase', 'reset', 'checkout', 'switch',
|
||||
'add', 'commit', 'merge', 'rebase', 'reset', 'checkout', 'switch',
|
||||
'branch', 'stash', 'cherry-pick', 'revert', 'pull', 'push', 'clean',
|
||||
]);
|
||||
|
||||
|
||||
@@ -36,6 +36,12 @@ describe('isProtectedPath', () => {
|
||||
])('allows %s', (p) => {
|
||||
expect(isProtectedPath(p, defaultPathNormalize, DEFAULT_PROTECTED_PATTERNS)).toBe(false);
|
||||
});
|
||||
|
||||
// Smoke 5 emergency fix — transcript JSONL protection (single it() for shell-content-rules hook compliance)
|
||||
it('protects ~/.claude/projects/*.jsonl (transcript hard-deny per spec §3.1) in shell-content-rules', () => {
|
||||
expect(isProtectedPath('~/.claude/projects/foo.jsonl', defaultPathNormalize, DEFAULT_PROTECTED_PATTERNS)).toBe(true);
|
||||
expect(isProtectedPath('/c/Users/Administrator/.claude/projects/abc/def.jsonl', defaultPathNormalize, DEFAULT_PROTECTED_PATTERNS)).toBe(true);
|
||||
});
|
||||
});
|
||||
import {
|
||||
pathDenyOverlay,
|
||||
@@ -53,6 +59,30 @@ describe('extractPathArgs', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractPathArgs edge cases (Stream H Task 2)', () => {
|
||||
it('extracts path from --output=PATH form', () => {
|
||||
expect(extractPathArgs(['curl', '--output=~/.claude/projects/secret.jsonl', 'http://x'])).toContain('~/.claude/projects/secret.jsonl');
|
||||
});
|
||||
it('extracts path from --output PATH form (separate token)', () => {
|
||||
expect(extractPathArgs(['curl', '--output', '~/.claude/projects/secret.jsonl', 'http://x'])).toContain('~/.claude/projects/secret.jsonl');
|
||||
});
|
||||
it('extracts path from dd of=PATH form', () => {
|
||||
expect(extractPathArgs(['dd', 'if=/dev/zero', 'of=~/.claude/projects/x.jsonl'])).toContain('~/.claude/projects/x.jsonl');
|
||||
});
|
||||
it('extracts path from tee PATH (second positional)', () => {
|
||||
expect(extractPathArgs(['tee', '~/.claude/projects/x.jsonl'])).toContain('~/.claude/projects/x.jsonl');
|
||||
});
|
||||
it('extracts path from cp SRC DST (both positionals)', () => {
|
||||
const got = extractPathArgs(['cp', '/tmp/x', '~/.claude/projects/x.jsonl']);
|
||||
expect(got).toContain('~/.claude/projects/x.jsonl');
|
||||
});
|
||||
it('does not include URL as path (heuristic)', () => {
|
||||
const got = extractPathArgs(['curl', '--output', '/tmp/x', 'https://example.com/y']);
|
||||
expect(got).toContain('/tmp/x');
|
||||
expect(got).not.toContain('https://example.com/y');
|
||||
});
|
||||
});
|
||||
|
||||
describe('pathDenyOverlay', () => {
|
||||
it('blocks when a candidate path is protected', () => {
|
||||
const r = pathDenyOverlay({ candidatePaths: ['~/.claude/runtime/x.json'] });
|
||||
@@ -125,6 +155,16 @@ describe('classifyGitCommand — readonly', () => {
|
||||
it('returns null for non-git', () => {
|
||||
expect(classifyGitCommand('ls -la', {})).toBe(null);
|
||||
});
|
||||
// Stream H pre-flight gap (2026-05-30): git fetch / git ls-remote were
|
||||
// missing from readonly whitelist, blocking Pravila §15.2 pre-flight sync
|
||||
// (`git fetch origin && git log HEAD..origin/main`). Both are ref-only —
|
||||
// no working tree mutation, no commit/push side effects.
|
||||
it.each(['git fetch', 'git fetch origin', 'git fetch --all', 'git ls-remote origin', 'git ls-remote --heads'])(
|
||||
'allows readonly remote-ref op: %s',
|
||||
(cmd) => {
|
||||
expect(classifyGitCommand(cmd, {}).result).toBe('allow');
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('classifyGitCommand — conditional after approve', () => {
|
||||
@@ -147,6 +187,18 @@ describe('classifyGitCommand — conditional after approve', () => {
|
||||
expect(classifyGitCommand(cmd, { approvedGitOps: [], now }).result).toBe('block');
|
||||
},
|
||||
);
|
||||
it('blocks unapproved git add (v4 Stream G addition)', () => {
|
||||
const r = classifyGitCommand('git add .claude/settings.json', { approvedGitOps: [], now });
|
||||
expect(r.result).toBe('block');
|
||||
expect(r.reason).toMatch(/approve/i);
|
||||
});
|
||||
it('allows approved git add', () => {
|
||||
const r = classifyGitCommand('git add .claude/settings.json', {
|
||||
approvedGitOps: [{ command: 'git add .claude/settings.json', ts: now }],
|
||||
now,
|
||||
});
|
||||
expect(r.result).toBe('allow');
|
||||
});
|
||||
});
|
||||
|
||||
describe('classifyGitCommand — git-hard (always block)', () => {
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
// tools/subagent-prompt-prefix-h10.test.mjs
|
||||
// Stream H Task 10 — worktree-bootstrap helper tests (vitest, separate file
|
||||
// because subagent-prompt-prefix.test.mjs is in vitest config exclude list
|
||||
// and uses node:test for subprocess-style runs).
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { detectWorktreeMode, buildSetupBlock } from './subagent-prompt-prefix.mjs';
|
||||
|
||||
describe('detectWorktreeMode (Stream H Task 10)', () => {
|
||||
it('returns isWorktree=false when cwd .git is the same as common dir', () => {
|
||||
const r = detectWorktreeMode({
|
||||
cwd: 'c:/repo',
|
||||
gitDir: 'c:/repo/.git',
|
||||
gitCommonDir: 'c:/repo/.git',
|
||||
});
|
||||
expect(r.isWorktree).toBe(false);
|
||||
expect(r.parentRepoRoot).toBeNull();
|
||||
});
|
||||
|
||||
it('returns isWorktree=true when cwd is a linked worktree', () => {
|
||||
const r = detectWorktreeMode({
|
||||
cwd: 'c:/parent/v4-stream-A',
|
||||
gitDir: 'c:/parent/.git/worktrees/v4-stream-A',
|
||||
gitCommonDir: 'c:/parent/.git',
|
||||
});
|
||||
expect(r.isWorktree).toBe(true);
|
||||
expect(r.parentRepoRoot).toBe('c:/parent');
|
||||
});
|
||||
|
||||
it('handles null inputs gracefully', () => {
|
||||
expect(detectWorktreeMode({}).isWorktree).toBe(false);
|
||||
expect(detectWorktreeMode({ cwd: null, gitDir: null, gitCommonDir: null }).isWorktree).toBe(false);
|
||||
});
|
||||
|
||||
it('handles different separators (backslashes) for parentRepoRoot extraction', () => {
|
||||
const r = detectWorktreeMode({
|
||||
cwd: 'c:/parent/wt-X',
|
||||
gitDir: 'c:\\parent\\.git\\worktrees\\wt-X',
|
||||
gitCommonDir: 'c:\\parent\\.git',
|
||||
});
|
||||
expect(r.isWorktree).toBe(true);
|
||||
expect(r.parentRepoRoot).toBe('c:/parent');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildSetupBlock (Stream H Task 10)', () => {
|
||||
it('returns empty string when not in a worktree', () => {
|
||||
expect(buildSetupBlock({ isWorktree: false, parentRepoRoot: null })).toBe('');
|
||||
});
|
||||
|
||||
it('returns a SETUP — worktree bootstrap block when in a worktree (win32)', () => {
|
||||
const s = buildSetupBlock({
|
||||
isWorktree: true,
|
||||
parentRepoRoot: 'c:/parent',
|
||||
platform: 'win32',
|
||||
});
|
||||
expect(s).toContain('SETUP — worktree bootstrap');
|
||||
expect(s).toContain('mklink /D vendor');
|
||||
expect(s).toContain('c:/parent/app/vendor');
|
||||
expect(s).toContain('storage/framework');
|
||||
});
|
||||
|
||||
it('returns a SETUP block with ln -s on linux/darwin', () => {
|
||||
const s = buildSetupBlock({
|
||||
isWorktree: true,
|
||||
parentRepoRoot: '/home/u/repo',
|
||||
platform: 'linux',
|
||||
});
|
||||
expect(s).toContain('SETUP — worktree bootstrap');
|
||||
expect(s).toContain('ln -s');
|
||||
expect(s).toContain('/home/u/repo/app/vendor');
|
||||
});
|
||||
|
||||
it('mentions mkdir for cache/sessions/views/testing dirs', () => {
|
||||
const s = buildSetupBlock({
|
||||
isWorktree: true,
|
||||
parentRepoRoot: 'c:/parent',
|
||||
platform: 'win32',
|
||||
});
|
||||
expect(s).toContain('cache');
|
||||
expect(s).toContain('sessions');
|
||||
expect(s).toContain('views');
|
||||
expect(s).toContain('testing');
|
||||
});
|
||||
|
||||
it('returns empty when isWorktree=true but parentRepoRoot missing', () => {
|
||||
expect(buildSetupBlock({ isWorktree: true, parentRepoRoot: null })).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -80,6 +80,58 @@ export function buildInheritanceEnv({ parentSessionId, inheritanceFile }) {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream H Task 10 — pure helper: detect if cwd is inside a linked worktree.
|
||||
*
|
||||
* Logic: when git's per-worktree dir (`git rev-parse --git-dir`) differs from
|
||||
* the shared common dir (`git rev-parse --git-common-dir`), we're in a
|
||||
* linked worktree. The parent repo root is the parent directory of the
|
||||
* common .git dir (with separators normalized to forward slashes).
|
||||
*
|
||||
* @param {object} args
|
||||
* @param {string} [args.cwd]
|
||||
* @param {string} [args.gitDir] - `git rev-parse --git-dir` output
|
||||
* @param {string} [args.gitCommonDir] - `git rev-parse --git-common-dir` output
|
||||
* @returns {{isWorktree: boolean, parentRepoRoot: string|null}}
|
||||
*/
|
||||
export function detectWorktreeMode({ cwd, gitDir, gitCommonDir } = {}) {
|
||||
const norm = (s) => (typeof s === 'string' ? s.replace(/\\/g, '/') : null);
|
||||
const gd = norm(gitDir);
|
||||
const gcd = norm(gitCommonDir);
|
||||
if (!gd || !gcd || gd === gcd) return { isWorktree: false, parentRepoRoot: null };
|
||||
// Common dir looks like `<parent>/.git` — strip the trailing `/.git` segment.
|
||||
const parent = gcd.replace(/\/\.git\/?$/, '');
|
||||
if (!parent || parent === gcd) return { isWorktree: false, parentRepoRoot: null };
|
||||
return { isWorktree: true, parentRepoRoot: parent };
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream H Task 10 — pure helper: build SETUP — worktree bootstrap text for
|
||||
* the injected subagent header. Empty string when not in a worktree OR
|
||||
* parentRepoRoot is missing. Per memory `feedback_subagent_worktree_bootstrap.md`.
|
||||
*
|
||||
* @param {object} args
|
||||
* @param {boolean} args.isWorktree
|
||||
* @param {string|null} args.parentRepoRoot
|
||||
* @param {string} [args.platform] - default process.platform; 'win32' uses mklink, others use ln -s
|
||||
* @returns {string} block text (joined with \n) or '' to omit
|
||||
*/
|
||||
export function buildSetupBlock({ isWorktree, parentRepoRoot, platform = process.platform } = {}) {
|
||||
if (!isWorktree || !parentRepoRoot) return '';
|
||||
const isWin = platform === 'win32';
|
||||
const symlinkCmd = isWin
|
||||
? `mklink /D vendor "${parentRepoRoot}/app/vendor"`
|
||||
: `ln -s "${parentRepoRoot}/app/vendor" vendor`;
|
||||
return [
|
||||
'',
|
||||
'SETUP — worktree bootstrap (run inside app/ before composer/pest if needed):',
|
||||
` 1. ${symlinkCmd}`,
|
||||
' 2. mkdir -p storage/framework/{cache,sessions,views,testing}',
|
||||
' 3. Re-run pest / composer commands; Eloquent facade and view cache paths now resolve.',
|
||||
'',
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
/** Read all stdin into a string. */
|
||||
async function readStdin() {
|
||||
let buf = '';
|
||||
@@ -105,12 +157,16 @@ async function gitCmd(args) {
|
||||
/** Build the safety header block. Returns null if any of the 4 git values can't be resolved. */
|
||||
async function buildHeader() {
|
||||
const cwd = process.cwd();
|
||||
const [branch, head, top] = await Promise.all([
|
||||
const [branch, head, top, gitDir, gitCommonDir] = await Promise.all([
|
||||
gitCmd(['branch', '--show-current']),
|
||||
gitCmd(['rev-parse', 'HEAD']),
|
||||
gitCmd(['rev-parse', '--show-toplevel']),
|
||||
gitCmd(['rev-parse', '--git-dir']),
|
||||
gitCmd(['rev-parse', '--git-common-dir']),
|
||||
]);
|
||||
if (!branch || !head || !top) return null;
|
||||
const worktreeInfo = detectWorktreeMode({ cwd, gitDir, gitCommonDir });
|
||||
const setupBlock = buildSetupBlock(worktreeInfo);
|
||||
return [
|
||||
'=== SUBAGENT GIT-SAFETY HEADER (Pravila §15.1, auto-injected) ===',
|
||||
`Working directory (cwd): ${cwd}`,
|
||||
@@ -124,7 +180,7 @@ async function buildHeader() {
|
||||
'3. После КАЖДОГО `git commit` — выполни `git rev-parse HEAD` и `git branch --show-current`, выпиши результат в ответ. Это обязательная часть отчёта.',
|
||||
'4. Если обнаружил, что находишься не в той ветке/worktree — STOP, не правь ничего, верни ошибку с raw output `git status` и `git branch --show-current`.',
|
||||
'5. Если задача не требует git-операций (только Edit/Read/Grep) — этот блок информационный, follow rule 1.',
|
||||
'',
|
||||
setupBlock,
|
||||
'=== END SUBAGENT GIT-SAFETY HEADER ===',
|
||||
'',
|
||||
].join('\n');
|
||||
|
||||
Reference in New Issue
Block a user