mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-09-11 22:51:26 +00:00
commit
750d111c58
43 changed files with 1548 additions and 61 deletions
12
.orchestration/active_intents.yaml
Normal file
12
.orchestration/active_intents.yaml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
active_intents:
|
||||
- id: "INT-001"
|
||||
name: "Build Weather API"
|
||||
status: "IN_PROGRESS"
|
||||
owned_scope:
|
||||
- "src/api/**"
|
||||
constraints:
|
||||
- "Use REST conventions"
|
||||
- "Return JSON"
|
||||
acceptance_criteria:
|
||||
- "GET /weather returns 200 with location and temperature"
|
||||
|
||||
4
.orchestration/agent_trace.jsonl
Normal file
4
.orchestration/agent_trace.jsonl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{"id":"e7b89cb4-6121-45f5-bc17-b43d641f6cc3","timestamp":"2026-02-21T18:32:36.210Z","vcs":{"revision_id":"abc123"},"files":[{"relative_path":"src/api/weather.ts","conversations":[{"url":"session-1","contributor":{"entity_type":"AI","model_identifier":"test-model"},"ranges":[{"start_line":1,"end_line":2,"content_hash":"sha256:2e4ec5cf14cfeb55ef32cb663ffd2c4fe6ab22bb2d2fe77ee8e284fe62866cac"}],"related":[{"type":"specification","value":"INT-001"}]}]}]}
|
||||
{"id":"c446252d-db42-480f-bde7-1336b81a03cd","timestamp":"2026-02-21T18:32:36.223Z","files":[{"relative_path":"src/api/forecast.ts","conversations":[{"contributor":{"entity_type":"AI","model_identifier":"unknown"},"ranges":[{"start_line":1,"end_line":1,"content_hash":"sha256:c47272be41ffa9d8a897b81b3f9f6d5e13fbb5d6dd4609a7b20c282950f11842"}],"related":[{"type":"specification","value":"INT-001"}]}]}]}
|
||||
{"id":"c5b3ffd3-d912-41dd-95c6-bcf76fbe08a3","timestamp":"2026-02-21T18:32:36.226Z","files":[{"relative_path":"src/api/trace.ts","conversations":[{"contributor":{"entity_type":"AI","model_identifier":"unknown"},"ranges":[{"start_line":1,"end_line":1,"content_hash":"sha256:a3d4bb671e8b19ed161b408c183193afc9fa361f573c5187dded809930d14413"}],"related":[{"type":"specification","value":"INT-001"},{"type":"request","value":"REQ-12345"}]}]}]}
|
||||
{"id":"26635dad-41f9-4ced-b73a-a555f2d3071e","timestamp":"2026-02-21T18:32:36.257Z","vcs":{"revision_id":"rev-1"},"files":[{"relative_path":"src/api/demo.ts","conversations":[{"url":"log-1","contributor":{"entity_type":"AI","model_identifier":"model-1"},"ranges":[{"start_line":1,"end_line":1,"content_hash":"sha256:bd994c26b795571c7c1e0357cba348f1d0238b648b748b9ec7a4d0de264d212f"}],"related":[{"type":"specification","value":"INT-001"}]}]}]}
|
||||
78
docs/testing-traceability.md
Normal file
78
docs/testing-traceability.md
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
# Testing the AI-Native Git Layer (Traceability)
|
||||
|
||||
How to test the semantic tracking ledger and `agent_trace.jsonl` **in the terminal**.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- From the **repo root**: `.orchestration/active_intents.yaml` must exist (it does in this repo).
|
||||
- Node/pnpm: `pnpm install` already run.
|
||||
|
||||
## 1. Run the full hook test suite
|
||||
|
||||
Runs content-hash, context-loader, scope, pre-hook, post-hook, and middleware:
|
||||
|
||||
```bash
|
||||
pnpm test:hooks
|
||||
```
|
||||
|
||||
Or without pnpm:
|
||||
|
||||
```bash
|
||||
npx tsx scripts/test-hooks.ts
|
||||
```
|
||||
|
||||
## 2. Run the trace scenario (one flow end-to-end)
|
||||
|
||||
Simulates: **select_active_intent** → **write_to_file** (pre-hook) → **post-hook** appends to `agent_trace.jsonl`, then prints the last trace entry.
|
||||
|
||||
```bash
|
||||
pnpm test:trace-scenario
|
||||
```
|
||||
|
||||
Or:
|
||||
|
||||
```bash
|
||||
npx tsx scripts/test-trace-scenario.ts
|
||||
```
|
||||
|
||||
You should see:
|
||||
|
||||
- Intent `INT-001` loaded
|
||||
- `write_to_file` allowed (in scope)
|
||||
- A new line in `.orchestration/agent_trace.jsonl`
|
||||
- The last entry printed with `intent_id`, `content_hash`, and **REQ-ID** in `related`
|
||||
|
||||
## 3. Inspect the trace file yourself
|
||||
|
||||
After running the scenario (or after real agent writes):
|
||||
|
||||
```bash
|
||||
# Last 3 trace entries (pretty-printed)
|
||||
tail -n 3 .orchestration/agent_trace.jsonl | while read line; do echo "$line" | jq .; done
|
||||
|
||||
# Or just the raw last line
|
||||
tail -n 1 .orchestration/agent_trace.jsonl
|
||||
```
|
||||
|
||||
## 4. Create your own scenario
|
||||
|
||||
Copy `scripts/test-trace-scenario.ts` and change:
|
||||
|
||||
- `intent_id` (must exist in `.orchestration/active_intents.yaml`)
|
||||
- `path` (must be under that intent’s `owned_scope`, e.g. `src/api/**`)
|
||||
- `content` and `mutation_class` (`AST_REFACTOR` | `INTENT_EVOLUTION` | `NEW_FILE`)
|
||||
|
||||
Then run:
|
||||
|
||||
```bash
|
||||
pnpm exec tsx scripts/your-scenario.ts
|
||||
```
|
||||
|
||||
## Summary
|
||||
|
||||
| Command | What it does |
|
||||
| -------------------------- | ----------------------------------------------------------------------- |
|
||||
| `pnpm test:hooks` | All hook unit-style checks (content-hash, pre/post-hook, middleware). |
|
||||
| `pnpm test:trace-scenario` | One full flow: select intent → write → append trace → print last entry. |
|
||||
|
||||
Both run in the terminal with no VS Code or extension required.
|
||||
|
|
@ -12,6 +12,8 @@
|
|||
"lint": "turbo lint --log-order grouped --output-logs new-only",
|
||||
"check-types": "turbo check-types --log-order grouped --output-logs new-only",
|
||||
"test": "turbo test --log-order grouped --output-logs new-only",
|
||||
"test:hooks": "pnpm exec tsx scripts/test-hooks.ts",
|
||||
"test:trace-scenario": "pnpm exec tsx scripts/test-trace-scenario.ts",
|
||||
"format": "turbo format --log-order grouped --output-logs new-only",
|
||||
"build": "turbo build --log-order grouped --output-logs new-only",
|
||||
"bundle": "turbo bundle --log-order grouped --output-logs new-only",
|
||||
|
|
@ -32,7 +34,7 @@
|
|||
"@dotenvx/dotenvx": "^1.34.0",
|
||||
"@roo-code/config-typescript": "workspace:^",
|
||||
"@types/glob": "^9.0.0",
|
||||
"@types/node": "^24.1.0",
|
||||
"@types/node": "^24.2.1",
|
||||
"@vscode/vsce": "3.3.2",
|
||||
"esbuild": "^0.25.0",
|
||||
"eslint": "^9.27.0",
|
||||
|
|
@ -45,6 +47,7 @@
|
|||
"ovsx": "0.10.4",
|
||||
"prettier": "^3.4.2",
|
||||
"rimraf": "^6.0.1",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsx": "^4.19.3",
|
||||
"turbo": "^2.5.6",
|
||||
"typescript": "5.8.3"
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ export const toolNames = [
|
|||
"run_slash_command",
|
||||
"skill",
|
||||
"generate_image",
|
||||
"append_lesson_learned",
|
||||
"custom_tool",
|
||||
] as const
|
||||
|
||||
|
|
|
|||
172
pnpm-lock.yaml
generated
172
pnpm-lock.yaml
generated
|
|
@ -33,7 +33,7 @@ importers:
|
|||
specifier: ^9.0.0
|
||||
version: 9.0.0
|
||||
'@types/node':
|
||||
specifier: ^24.1.0
|
||||
specifier: ^24.2.1
|
||||
version: 24.2.1
|
||||
'@vscode/vsce':
|
||||
specifier: 3.3.2
|
||||
|
|
@ -71,6 +71,9 @@ importers:
|
|||
rimraf:
|
||||
specifier: ^6.0.1
|
||||
version: 6.0.1
|
||||
ts-node:
|
||||
specifier: ^10.9.2
|
||||
version: 10.9.2(@types/node@24.2.1)(typescript@5.8.3)
|
||||
tsx:
|
||||
specifier: ^4.19.3
|
||||
version: 4.19.4
|
||||
|
|
@ -420,7 +423,7 @@ importers:
|
|||
version: 3.4.0
|
||||
tailwindcss-animate:
|
||||
specifier: ^1.0.7
|
||||
version: 1.0.7(tailwindcss@3.4.17)
|
||||
version: 1.0.7(tailwindcss@3.4.17(ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3)))
|
||||
tldts:
|
||||
specifier: ^6.1.86
|
||||
version: 6.1.86
|
||||
|
|
@ -436,7 +439,7 @@ importers:
|
|||
version: link:../../packages/config-typescript
|
||||
'@tailwindcss/typography':
|
||||
specifier: ^0.5.19
|
||||
version: 0.5.19(tailwindcss@3.4.17)
|
||||
version: 0.5.19(tailwindcss@3.4.17(ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3)))
|
||||
'@types/node':
|
||||
specifier: 20.x
|
||||
version: 20.17.57
|
||||
|
|
@ -457,7 +460,7 @@ importers:
|
|||
version: 8.5.6
|
||||
tailwindcss:
|
||||
specifier: ^3.4.17
|
||||
version: 3.4.17
|
||||
version: 3.4.17(ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3))
|
||||
vitest:
|
||||
specifier: ^4.0.18
|
||||
version: 4.0.18(@opentelemetry/api@1.9.0)(@types/node@20.17.57)(jiti@2.4.2)(jsdom@26.1.0)(lightningcss@1.30.1)(tsx@4.19.4)(yaml@2.8.0)
|
||||
|
|
@ -1992,6 +1995,10 @@ packages:
|
|||
'@corex/deepmerge@4.0.43':
|
||||
resolution: {integrity: sha512-N8uEMrMPL0cu/bdboEWpQYb/0i2K5Qn8eCsxzOmxSggJbbQte7ljMRoXm917AbntqTGOzdTu+vP3KOOzoC70HQ==}
|
||||
|
||||
'@cspotcode/source-map-support@0.8.1':
|
||||
resolution: {integrity: sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==}
|
||||
engines: {node: '>=12'}
|
||||
|
||||
'@csstools/color-helpers@5.0.2':
|
||||
resolution: {integrity: sha512-JqWH1vsgdGcw2RR6VliXXdA0/59LttzlU8UlRT/iUUsEeWfYq8I+K0yhihEUTTHLRm1EXvpsCx3083EU15ecsA==}
|
||||
engines: {node: '>=18'}
|
||||
|
|
@ -2529,6 +2536,9 @@ packages:
|
|||
'@jridgewell/trace-mapping@0.3.25':
|
||||
resolution: {integrity: sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==}
|
||||
|
||||
'@jridgewell/trace-mapping@0.3.9':
|
||||
resolution: {integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==}
|
||||
|
||||
'@kwsites/file-exists@1.1.1':
|
||||
resolution: {integrity: sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==}
|
||||
|
||||
|
|
@ -4347,6 +4357,18 @@ packages:
|
|||
peerDependencies:
|
||||
typescript: '>=5.7.2'
|
||||
|
||||
'@tsconfig/node10@1.0.12':
|
||||
resolution: {integrity: sha512-UCYBaeFvM11aU2y3YPZ//O5Rhj+xKyzy7mvcIoAjASbigy8mHMryP5cK7dgjlz2hWxh1g5pLw084E0a/wlUSFQ==}
|
||||
|
||||
'@tsconfig/node12@1.0.11':
|
||||
resolution: {integrity: sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==}
|
||||
|
||||
'@tsconfig/node14@1.0.3':
|
||||
resolution: {integrity: sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==}
|
||||
|
||||
'@tsconfig/node16@1.0.4':
|
||||
resolution: {integrity: sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==}
|
||||
|
||||
'@tybys/wasm-util@0.9.0':
|
||||
resolution: {integrity: sha512-6+7nlbMVX/PVDCwaIQ8nTOPveOcFLSt8GcXdx8hD0bt39uWxYT88uXzqTd4fTvqta7oeUJqudepapKNt2DYJFw==}
|
||||
|
||||
|
|
@ -4883,10 +4905,9 @@ packages:
|
|||
peerDependencies:
|
||||
acorn: ^6.0.0 || ^7.0.0 || ^8.0.0
|
||||
|
||||
acorn@8.14.1:
|
||||
resolution: {integrity: sha512-OvQ/2pUDKmgfCg++xsTX1wGxfTaszcHVcTctW4UJB4hibJx2HXxxO5UmVgyjMa+ZDsiaf5wWLXYpRWMmBI0QHg==}
|
||||
acorn-walk@8.3.5:
|
||||
resolution: {integrity: sha512-HEHNfbars9v4pgpW6SO1KSPkfoS0xVOM/9UzkJltjlsHZmJasxg8aXkuZa7SMf8vKGIBhpUsPluQSqhJFCqebw==}
|
||||
engines: {node: '>=0.4.0'}
|
||||
hasBin: true
|
||||
|
||||
acorn@8.15.0:
|
||||
resolution: {integrity: sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==}
|
||||
|
|
@ -4984,6 +5005,9 @@ packages:
|
|||
resolution: {integrity: sha512-ZcbTaIqJOfCc03QwD468Unz/5Ir8ATtvAHsK+FdXbDIbGfihqh9mrvdcYunQzqn4HrvWWaFyaxJhGZagaJJpPQ==}
|
||||
engines: {node: '>= 14'}
|
||||
|
||||
arg@4.1.3:
|
||||
resolution: {integrity: sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==}
|
||||
|
||||
arg@5.0.2:
|
||||
resolution: {integrity: sha512-PYjyFOLKQ9y57JvQ6QLo8dAgNqswh8M1RMJYdQduT6xbWSgK36P/Z/v+p888pM69jMMfS8Xd8F6I1kQ/I9HUGg==}
|
||||
|
||||
|
|
@ -5614,6 +5638,9 @@ packages:
|
|||
resolution: {integrity: sha512-piICUB6ei4IlTv1+653yq5+KoqfBYmj9bw6LqXoOneTMDXk5nM1qt12mFW1caG3LlJXEKW1Bp0WggEmIfQB34g==}
|
||||
engines: {node: '>= 14'}
|
||||
|
||||
create-require@1.1.1:
|
||||
resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==}
|
||||
|
||||
cross-fetch@4.0.0:
|
||||
resolution: {integrity: sha512-e4a5N8lVvuLgAWgnCrLr2PP0YyDOTHa9H/Rj54dirp61qXnNq46m82bRhNqIA5VccJtWBvPTFRV3TtvHUKPB1g==}
|
||||
|
||||
|
|
@ -6020,6 +6047,10 @@ packages:
|
|||
resolution: {integrity: sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==}
|
||||
engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}
|
||||
|
||||
diff@4.0.4:
|
||||
resolution: {integrity: sha512-X07nttJQkwkfKfvTPG/KSnE2OMdcUCao6+eXF3wmnIQRn2aPAHH3VxDbDOdegkd6JbPsXqShpvEOHfAT+nCNwQ==}
|
||||
engines: {node: '>=0.3.1'}
|
||||
|
||||
diff@5.2.0:
|
||||
resolution: {integrity: sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==}
|
||||
engines: {node: '>=0.3.1'}
|
||||
|
|
@ -6927,6 +6958,7 @@ packages:
|
|||
glob@11.1.0:
|
||||
resolution: {integrity: sha512-vuNwKSaKiqm7g0THUBu2x7ckSs3XJLXE+2ssL7/MfTGPLLcrJQ/4Uq1CjPTtO5cCIiRxqvN6Twy1qOwhL0Xjcw==}
|
||||
engines: {node: 20 || >=22}
|
||||
deprecated: Old versions of glob are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me
|
||||
hasBin: true
|
||||
|
||||
global-agent@3.0.0:
|
||||
|
|
@ -8085,6 +8117,9 @@ packages:
|
|||
resolution: {integrity: sha512-hXdUTZYIVOt1Ex//jAQi+wTZZpUpwBj/0QsOzqegb3rGMMeJiSEu5xLHnYfBrRV4RH2+OCSOO95Is/7x1WJ4bw==}
|
||||
engines: {node: '>=10'}
|
||||
|
||||
make-error@1.3.6:
|
||||
resolution: {integrity: sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==}
|
||||
|
||||
mammoth@1.9.1:
|
||||
resolution: {integrity: sha512-4S2v1eP4Yo4so0zGNicJKcP93su3wDPcUk+xvkjSG75nlNjSkDJu8BhWQ+e54BROM0HfA6nPzJn12S6bq2Ko6w==}
|
||||
engines: {node: '>=12.0.0'}
|
||||
|
|
@ -9016,6 +9051,7 @@ packages:
|
|||
prebuild-install@7.1.3:
|
||||
resolution: {integrity: sha512-8Mf2cbV7x1cXPUILADGI3wuhfqWvtiLA1iclTDbFRZkgRQS0NqsPZphna9V+HyTEadheuPmjaJMsbzKQFOzLug==}
|
||||
engines: {node: '>=10'}
|
||||
deprecated: No longer maintained. Please contact the author of the relevant native addon; alternatives are available.
|
||||
hasBin: true
|
||||
|
||||
prelude-ls@1.2.1:
|
||||
|
|
@ -10101,7 +10137,7 @@ packages:
|
|||
tar@7.4.3:
|
||||
resolution: {integrity: sha512-5S7Va8hKfV7W5U6g3aYxXmlPoZVAwUMy9AOKyF2fVuZa2UD3qZjg578OrLRt8PcNN1PleVaL/5/yYATNL0ICUw==}
|
||||
engines: {node: '>=18'}
|
||||
deprecated: Old versions of tar are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exhorbitant rates) by contacting i@izs.me
|
||||
deprecated: Old versions of tar are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me
|
||||
|
||||
term-size@2.2.1:
|
||||
resolution: {integrity: sha512-wK0Ri4fOGjv/XPy8SBHZChl8CM7uMc5VML7SqiQ0zG7+J5Vr+RMQDoHa2CNT6KHUnTGIXH34UDMkPzAUyapBZg==}
|
||||
|
|
@ -10259,6 +10295,20 @@ packages:
|
|||
ts-interface-checker@0.1.13:
|
||||
resolution: {integrity: sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==}
|
||||
|
||||
ts-node@10.9.2:
|
||||
resolution: {integrity: sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==}
|
||||
hasBin: true
|
||||
peerDependencies:
|
||||
'@swc/core': '>=1.2.50'
|
||||
'@swc/wasm': '>=1.2.50'
|
||||
'@types/node': '*'
|
||||
typescript: '>=2.7'
|
||||
peerDependenciesMeta:
|
||||
'@swc/core':
|
||||
optional: true
|
||||
'@swc/wasm':
|
||||
optional: true
|
||||
|
||||
tslib@1.14.1:
|
||||
resolution: {integrity: sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==}
|
||||
|
||||
|
|
@ -10582,6 +10632,9 @@ packages:
|
|||
resolution: {integrity: sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==}
|
||||
hasBin: true
|
||||
|
||||
v8-compile-cache-lib@3.0.1:
|
||||
resolution: {integrity: sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==}
|
||||
|
||||
v8-to-istanbul@9.3.0:
|
||||
resolution: {integrity: sha512-kiGUalWN+rgBJ/1OHZsBtU4rXZOfj/7rKQxULKlIzwzQSvMJUUNgPwJEEh7gU6xEVxC0ahoOBvN2YI8GH6FNgA==}
|
||||
engines: {node: '>=10.12.0'}
|
||||
|
|
@ -11063,6 +11116,10 @@ packages:
|
|||
yazl@2.5.1:
|
||||
resolution: {integrity: sha512-phENi2PLiHnHb6QBVot+dJnaAZ0xosj7p3fWl+znIjBDlnMI2PsZCJZ306BPTFOaHf5qdDEI8x5qFrSOBN5vrw==}
|
||||
|
||||
yn@3.1.1:
|
||||
resolution: {integrity: sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==}
|
||||
engines: {node: '>=6'}
|
||||
|
||||
yocto-queue@0.1.0:
|
||||
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
|
||||
engines: {node: '>=10'}
|
||||
|
|
@ -12198,6 +12255,10 @@ snapshots:
|
|||
|
||||
'@corex/deepmerge@4.0.43': {}
|
||||
|
||||
'@cspotcode/source-map-support@0.8.1':
|
||||
dependencies:
|
||||
'@jridgewell/trace-mapping': 0.3.9
|
||||
|
||||
'@csstools/color-helpers@5.0.2': {}
|
||||
|
||||
'@csstools/css-calc@2.1.4(@csstools/css-parser-algorithms@3.0.5(@csstools/css-tokenizer@3.0.4))(@csstools/css-tokenizer@3.0.4)':
|
||||
|
|
@ -12653,7 +12714,12 @@ snapshots:
|
|||
'@jridgewell/trace-mapping@0.3.25':
|
||||
dependencies:
|
||||
'@jridgewell/resolve-uri': 3.1.2
|
||||
'@jridgewell/sourcemap-codec': 1.5.0
|
||||
'@jridgewell/sourcemap-codec': 1.5.5
|
||||
|
||||
'@jridgewell/trace-mapping@0.3.9':
|
||||
dependencies:
|
||||
'@jridgewell/resolve-uri': 3.1.2
|
||||
'@jridgewell/sourcemap-codec': 1.5.5
|
||||
|
||||
'@kwsites/file-exists@1.1.1':
|
||||
dependencies:
|
||||
|
|
@ -14456,10 +14522,10 @@ snapshots:
|
|||
postcss: 8.5.4
|
||||
tailwindcss: 4.1.8
|
||||
|
||||
'@tailwindcss/typography@0.5.19(tailwindcss@3.4.17)':
|
||||
'@tailwindcss/typography@0.5.19(tailwindcss@3.4.17(ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3)))':
|
||||
dependencies:
|
||||
postcss-selector-parser: 6.0.10
|
||||
tailwindcss: 3.4.17
|
||||
tailwindcss: 3.4.17(ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3))
|
||||
|
||||
'@tailwindcss/vite@4.1.6(vite@6.3.6(@types/node@20.17.57)(jiti@2.4.2)(lightningcss@1.30.1)(tsx@4.19.4)(yaml@2.8.0))':
|
||||
dependencies:
|
||||
|
|
@ -14528,6 +14594,14 @@ snapshots:
|
|||
dependencies:
|
||||
typescript: 5.8.3
|
||||
|
||||
'@tsconfig/node10@1.0.12': {}
|
||||
|
||||
'@tsconfig/node12@1.0.11': {}
|
||||
|
||||
'@tsconfig/node14@1.0.3': {}
|
||||
|
||||
'@tsconfig/node16@1.0.4': {}
|
||||
|
||||
'@tybys/wasm-util@0.9.0':
|
||||
dependencies:
|
||||
tslib: 2.8.1
|
||||
|
|
@ -15073,7 +15147,7 @@ snapshots:
|
|||
sirv: 3.0.1
|
||||
tinyglobby: 0.2.14
|
||||
tinyrainbow: 2.0.0
|
||||
vitest: 3.2.4(@types/debug@4.1.12)(@types/node@24.2.1)(@vitest/ui@3.2.4)(jiti@2.4.2)(jsdom@26.1.0)(lightningcss@1.30.1)(tsx@4.19.4)(yaml@2.8.0)
|
||||
vitest: 3.2.4(@types/debug@4.1.12)(@types/node@20.17.57)(@vitest/ui@3.2.4)(jiti@2.4.2)(jsdom@26.1.0)(lightningcss@1.30.1)(tsx@4.19.4)(yaml@2.8.0)
|
||||
|
||||
'@vitest/utils@3.2.4':
|
||||
dependencies:
|
||||
|
|
@ -15210,15 +15284,13 @@ snapshots:
|
|||
mime-types: 3.0.1
|
||||
negotiator: 1.0.0
|
||||
|
||||
acorn-jsx@5.3.2(acorn@8.14.1):
|
||||
dependencies:
|
||||
acorn: 8.14.1
|
||||
|
||||
acorn-jsx@5.3.2(acorn@8.15.0):
|
||||
dependencies:
|
||||
acorn: 8.15.0
|
||||
|
||||
acorn@8.14.1: {}
|
||||
acorn-walk@8.3.5:
|
||||
dependencies:
|
||||
acorn: 8.15.0
|
||||
|
||||
acorn@8.15.0: {}
|
||||
|
||||
|
|
@ -15338,6 +15410,8 @@ snapshots:
|
|||
tar-stream: 3.1.7
|
||||
zip-stream: 6.0.1
|
||||
|
||||
arg@4.1.3: {}
|
||||
|
||||
arg@5.0.2: {}
|
||||
|
||||
argparse@1.0.10:
|
||||
|
|
@ -15997,6 +16071,8 @@ snapshots:
|
|||
crc-32: 1.2.2
|
||||
readable-stream: 4.7.0
|
||||
|
||||
create-require@1.1.1: {}
|
||||
|
||||
cross-fetch@4.0.0:
|
||||
dependencies:
|
||||
node-fetch: 2.7.0
|
||||
|
|
@ -16385,6 +16461,8 @@ snapshots:
|
|||
|
||||
diff-sequences@29.6.3: {}
|
||||
|
||||
diff@4.0.4: {}
|
||||
|
||||
diff@5.2.0: {}
|
||||
|
||||
dijkstrajs@1.0.3: {}
|
||||
|
|
@ -16876,8 +16954,8 @@ snapshots:
|
|||
|
||||
espree@10.3.0:
|
||||
dependencies:
|
||||
acorn: 8.14.1
|
||||
acorn-jsx: 5.3.2(acorn@8.14.1)
|
||||
acorn: 8.15.0
|
||||
acorn-jsx: 5.3.2(acorn@8.15.0)
|
||||
eslint-visitor-keys: 4.2.0
|
||||
|
||||
espree@10.4.0:
|
||||
|
|
@ -18671,6 +18749,8 @@ snapshots:
|
|||
dependencies:
|
||||
semver: 7.7.3
|
||||
|
||||
make-error@1.3.6: {}
|
||||
|
||||
mammoth@1.9.1:
|
||||
dependencies:
|
||||
'@xmldom/xmldom': 0.8.10
|
||||
|
|
@ -19219,7 +19299,7 @@ snapshots:
|
|||
|
||||
mlly@1.7.4:
|
||||
dependencies:
|
||||
acorn: 8.14.1
|
||||
acorn: 8.15.0
|
||||
pathe: 2.0.3
|
||||
pkg-types: 1.3.1
|
||||
ufo: 1.6.1
|
||||
|
|
@ -19796,12 +19876,13 @@ snapshots:
|
|||
camelcase-css: 2.0.1
|
||||
postcss: 8.5.6
|
||||
|
||||
postcss-load-config@4.0.2(postcss@8.5.6):
|
||||
postcss-load-config@4.0.2(postcss@8.5.6)(ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3)):
|
||||
dependencies:
|
||||
lilconfig: 3.1.3
|
||||
yaml: 2.8.0
|
||||
optionalDependencies:
|
||||
postcss: 8.5.6
|
||||
ts-node: 10.9.2(@types/node@20.17.57)(typescript@5.8.3)
|
||||
|
||||
postcss-load-config@6.0.1(jiti@2.4.2)(postcss@8.5.6)(tsx@4.19.4)(yaml@2.8.0):
|
||||
dependencies:
|
||||
|
|
@ -21203,15 +21284,15 @@ snapshots:
|
|||
|
||||
tailwind-merge@3.4.0: {}
|
||||
|
||||
tailwindcss-animate@1.0.7(tailwindcss@3.4.17):
|
||||
tailwindcss-animate@1.0.7(tailwindcss@3.4.17(ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3))):
|
||||
dependencies:
|
||||
tailwindcss: 3.4.17
|
||||
tailwindcss: 3.4.17(ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3))
|
||||
|
||||
tailwindcss-animate@1.0.7(tailwindcss@4.1.6):
|
||||
dependencies:
|
||||
tailwindcss: 4.1.6
|
||||
|
||||
tailwindcss@3.4.17:
|
||||
tailwindcss@3.4.17(ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3)):
|
||||
dependencies:
|
||||
'@alloc/quick-lru': 5.2.0
|
||||
arg: 5.0.2
|
||||
|
|
@ -21230,7 +21311,7 @@ snapshots:
|
|||
postcss: 8.5.6
|
||||
postcss-import: 15.1.0(postcss@8.5.6)
|
||||
postcss-js: 4.0.1(postcss@8.5.6)
|
||||
postcss-load-config: 4.0.2(postcss@8.5.6)
|
||||
postcss-load-config: 4.0.2(postcss@8.5.6)(ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3))
|
||||
postcss-nested: 6.2.0(postcss@8.5.6)
|
||||
postcss-selector-parser: 6.1.2
|
||||
resolve: 1.22.10
|
||||
|
|
@ -21398,6 +21479,43 @@ snapshots:
|
|||
|
||||
ts-interface-checker@0.1.13: {}
|
||||
|
||||
ts-node@10.9.2(@types/node@20.17.57)(typescript@5.8.3):
|
||||
dependencies:
|
||||
'@cspotcode/source-map-support': 0.8.1
|
||||
'@tsconfig/node10': 1.0.12
|
||||
'@tsconfig/node12': 1.0.11
|
||||
'@tsconfig/node14': 1.0.3
|
||||
'@tsconfig/node16': 1.0.4
|
||||
'@types/node': 20.17.57
|
||||
acorn: 8.15.0
|
||||
acorn-walk: 8.3.5
|
||||
arg: 4.1.3
|
||||
create-require: 1.1.1
|
||||
diff: 4.0.4
|
||||
make-error: 1.3.6
|
||||
typescript: 5.8.3
|
||||
v8-compile-cache-lib: 3.0.1
|
||||
yn: 3.1.1
|
||||
optional: true
|
||||
|
||||
ts-node@10.9.2(@types/node@24.2.1)(typescript@5.8.3):
|
||||
dependencies:
|
||||
'@cspotcode/source-map-support': 0.8.1
|
||||
'@tsconfig/node10': 1.0.12
|
||||
'@tsconfig/node12': 1.0.11
|
||||
'@tsconfig/node14': 1.0.3
|
||||
'@tsconfig/node16': 1.0.4
|
||||
'@types/node': 24.2.1
|
||||
acorn: 8.15.0
|
||||
acorn-walk: 8.3.5
|
||||
arg: 4.1.3
|
||||
create-require: 1.1.1
|
||||
diff: 4.0.4
|
||||
make-error: 1.3.6
|
||||
typescript: 5.8.3
|
||||
v8-compile-cache-lib: 3.0.1
|
||||
yn: 3.1.1
|
||||
|
||||
tslib@1.14.1: {}
|
||||
|
||||
tslib@2.6.2: {}
|
||||
|
|
@ -21749,6 +21867,8 @@ snapshots:
|
|||
|
||||
uuid@9.0.1: {}
|
||||
|
||||
v8-compile-cache-lib@3.0.1: {}
|
||||
|
||||
v8-to-istanbul@9.3.0:
|
||||
dependencies:
|
||||
'@jridgewell/trace-mapping': 0.3.25
|
||||
|
|
@ -22422,6 +22542,8 @@ snapshots:
|
|||
dependencies:
|
||||
buffer-crc32: 0.2.13
|
||||
|
||||
yn@3.1.1: {}
|
||||
|
||||
yocto-queue@0.1.0: {}
|
||||
|
||||
yocto-queue@1.2.1: {}
|
||||
|
|
|
|||
62
scripts/pre-hook.spec.ts
Normal file
62
scripts/pre-hook.spec.ts
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import { describe, it, expect, beforeEach } from "vitest"
|
||||
import { PreHook } from "../pre-hook"
|
||||
|
||||
describe("PreHook", () => {
|
||||
let activeIntentId: string | null
|
||||
let preHook: PreHook
|
||||
|
||||
beforeEach(() => {
|
||||
activeIntentId = null
|
||||
preHook = new PreHook({
|
||||
cwd: process.cwd(),
|
||||
getActiveIntentId: () => activeIntentId,
|
||||
setActiveIntentId: (id) => {
|
||||
activeIntentId = id
|
||||
},
|
||||
requireIntentForDestructiveOnly: true,
|
||||
})
|
||||
})
|
||||
|
||||
it("blocks destructive write without active intent", async () => {
|
||||
activeIntentId = null
|
||||
const res = await preHook.intercept("write_to_file", {
|
||||
path: "src/api/weather.ts",
|
||||
content: "// test",
|
||||
})
|
||||
expect(res.blocked).toBe(true)
|
||||
expect(res.error).toEqual(expect.stringMatching(/select an active intent/i))
|
||||
})
|
||||
|
||||
it("blocks write outside owned scope (scope violation)", async () => {
|
||||
activeIntentId = "INT-001"
|
||||
const res = await preHook.intercept("write_to_file", {
|
||||
path: "src/db/db.ts",
|
||||
content: "// should be blocked",
|
||||
})
|
||||
expect(res.blocked).toBe(true)
|
||||
expect(res.error).toEqual(expect.stringMatching(/scope violation/i))
|
||||
})
|
||||
|
||||
it("recovery loop: retry after select_active_intent succeeds", async () => {
|
||||
// initial attempt without intent
|
||||
activeIntentId = null
|
||||
const attempt1 = await preHook.intercept("write_to_file", {
|
||||
path: "src/api/weather.ts",
|
||||
content: "// first",
|
||||
})
|
||||
expect(attempt1.blocked).toBe(true)
|
||||
|
||||
// select active intent (handshake)
|
||||
const handshake = await preHook.intercept("select_active_intent", { intent_id: "INT-001" })
|
||||
expect(handshake.blocked).toBe(false)
|
||||
expect(handshake.injectResult).toEqual(expect.stringContaining("<intent_context>"))
|
||||
expect(activeIntentId).toBe("INT-001")
|
||||
|
||||
// retry write (in-owned-scope path)
|
||||
const attempt2 = await preHook.intercept("write_to_file", {
|
||||
path: "src/api/weather.ts",
|
||||
content: "// second",
|
||||
})
|
||||
expect(attempt2.blocked).toBe(false)
|
||||
})
|
||||
})
|
||||
72
scripts/pre-hook.test.ts
Normal file
72
scripts/pre-hook.test.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import test from "node:test"
|
||||
import assert from "node:assert/strict"
|
||||
import { PreHook } from "../src/hooks/pre-hook"
|
||||
|
||||
const cwd = process.cwd()
|
||||
|
||||
test("blocks destructive write without active intent", async () => {
|
||||
let activeIntentId: string | null = null
|
||||
const preHook = new PreHook({
|
||||
cwd,
|
||||
getActiveIntentId: () => activeIntentId,
|
||||
setActiveIntentId: (id) => {
|
||||
activeIntentId = id
|
||||
},
|
||||
requireIntentForDestructiveOnly: true,
|
||||
})
|
||||
|
||||
const res = await preHook.intercept("write_to_file", {
|
||||
path: "src/api/weather.ts",
|
||||
content: "// test",
|
||||
})
|
||||
assert.equal(res.blocked, true)
|
||||
assert.match(String(res.error), /select an active intent/i)
|
||||
})
|
||||
|
||||
test("blocks write outside owned scope (scope violation)", async () => {
|
||||
let activeIntentId: string | null = "INT-001"
|
||||
const preHook = new PreHook({
|
||||
cwd,
|
||||
getActiveIntentId: () => activeIntentId,
|
||||
setActiveIntentId: (id) => {
|
||||
activeIntentId = id
|
||||
},
|
||||
requireIntentForDestructiveOnly: true,
|
||||
})
|
||||
|
||||
const res = await preHook.intercept("write_to_file", {
|
||||
path: "src/db/db.ts",
|
||||
content: "// should be blocked",
|
||||
})
|
||||
assert.equal(res.blocked, true)
|
||||
assert.match(String(res.error), /scope violation/i)
|
||||
})
|
||||
|
||||
test("recovery loop: retry after select_active_intent succeeds", async () => {
|
||||
let activeIntentId: string | null = null
|
||||
const preHook = new PreHook({
|
||||
cwd,
|
||||
getActiveIntentId: () => activeIntentId,
|
||||
setActiveIntentId: (id) => {
|
||||
activeIntentId = id
|
||||
},
|
||||
requireIntentForDestructiveOnly: true,
|
||||
})
|
||||
|
||||
const attempt1 = await preHook.intercept("write_to_file", {
|
||||
path: "src/api/weather.ts",
|
||||
content: "// first",
|
||||
})
|
||||
assert.equal(attempt1.blocked, true)
|
||||
|
||||
const handshake = await preHook.intercept("select_active_intent", { intent_id: "INT-001" })
|
||||
assert.equal(handshake.blocked, false)
|
||||
assert.ok(typeof handshake.injectResult === "string" && handshake.injectResult.includes("<intent_context>"))
|
||||
assert.equal(activeIntentId, "INT-001")
|
||||
|
||||
const attempt2 = await preHook.intercept("write_to_file", {
|
||||
path: "src/api/weather.ts",
|
||||
content: "// second",
|
||||
})
|
||||
assert.equal(attempt2.blocked, false)
|
||||
})
|
||||
272
scripts/test-hooks.ts
Normal file
272
scripts/test-hooks.ts
Normal file
|
|
@ -0,0 +1,272 @@
|
|||
/**
|
||||
* Run each hook component and assert expected behavior.
|
||||
* Run from repo root: pnpm tsx scripts/test-hooks.ts
|
||||
*/
|
||||
import fs from "fs/promises"
|
||||
import path from "path"
|
||||
|
||||
import { contentHash, contentHashForRange } from "../src/hooks/content-hash"
|
||||
import { loadIntentContext, buildIntentContextXml } from "../src/hooks/context-loader"
|
||||
import { pathInScope } from "../src/hooks/scope"
|
||||
import { PreHook } from "../src/hooks/pre-hook"
|
||||
import { HookMiddleware } from "../src/hooks/middleware"
|
||||
import { appendAgentTrace } from "../src/hooks/post-hook"
|
||||
|
||||
const cwd = process.cwd()
|
||||
|
||||
function assert(condition: boolean, message: string): void {
|
||||
if (!condition) throw new Error(`FAIL: ${message}`)
|
||||
}
|
||||
|
||||
async function run(name: string, fn: () => Promise<void> | void): Promise<void> {
|
||||
try {
|
||||
await fn()
|
||||
console.log(` OK ${name}`)
|
||||
} catch (e) {
|
||||
console.error(` FAIL ${name}`)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log("=== 1. content-hash ===\n")
|
||||
|
||||
await run("contentHash returns sha256: prefix", async () => {
|
||||
const h = contentHash("hello")
|
||||
assert(h.startsWith("sha256:"), "prefix")
|
||||
assert(h.length === 71, "length 7 prefix + 64 hex")
|
||||
})
|
||||
|
||||
await run("contentHash is deterministic", async () => {
|
||||
const a = contentHash("same")
|
||||
const b = contentHash("same")
|
||||
assert(a === b, "same input => same hash")
|
||||
})
|
||||
|
||||
await run("contentHashForRange hashes line range", async () => {
|
||||
const text = "line1\nline2\nline3\nline4"
|
||||
const h = contentHashForRange(text, 2, 3)
|
||||
assert(h.startsWith("sha256:"), "prefix")
|
||||
// line2\nline3
|
||||
const expected = contentHash("line2\nline3")
|
||||
assert(h === expected, "range hash matches manual slice")
|
||||
})
|
||||
|
||||
console.log("\n=== 2. context-loader ===\n")
|
||||
|
||||
await run("loadIntentContext returns null when file missing", async () => {
|
||||
const ctx = await loadIntentContext("/nonexistent", "INT-001")
|
||||
assert(ctx === null, "missing dir => null")
|
||||
})
|
||||
|
||||
await run("loadIntentContext loads INT-001 from .orchestration/active_intents.yaml", async () => {
|
||||
const ctx = await loadIntentContext(cwd, "INT-001")
|
||||
assert(ctx !== null, "context exists")
|
||||
assert(ctx!.id === "INT-001", "id")
|
||||
assert(ctx!.name === "Build Weather API", "name")
|
||||
assert(Array.isArray(ctx!.owned_scope) && ctx!.owned_scope!.includes("src/api/**"), "owned_scope")
|
||||
assert(Array.isArray(ctx!.constraints) && ctx!.constraints!.length > 0, "constraints")
|
||||
})
|
||||
|
||||
await run("loadIntentContext returns null for unknown intent", async () => {
|
||||
const ctx = await loadIntentContext(cwd, "INT-999")
|
||||
assert(ctx === null, "unknown id => null")
|
||||
})
|
||||
|
||||
await run("buildIntentContextXml produces valid XML block", async () => {
|
||||
const ctx = await loadIntentContext(cwd, "INT-001")
|
||||
assert(ctx !== null, "context exists")
|
||||
const xml = buildIntentContextXml(ctx!)
|
||||
assert(xml.includes("<intent_context>"), "root tag")
|
||||
assert(xml.includes("<id>INT-001</id>"), "id")
|
||||
assert(xml.includes("<constraint>"), "constraints")
|
||||
assert(xml.includes("<scope>"), "scope")
|
||||
})
|
||||
|
||||
console.log("\n=== 3. scope ===\n")
|
||||
|
||||
await run("pathInScope: empty scope => allowed", async () => {
|
||||
assert(pathInScope("any/file.ts", [], cwd) === true, "empty scope allows all")
|
||||
})
|
||||
|
||||
await run("pathInScope: src/api/** matches src/api/weather.ts", async () => {
|
||||
assert(pathInScope("src/api/weather.ts", ["src/api/**"], cwd) === true, "in scope")
|
||||
})
|
||||
|
||||
await run("pathInScope: src/api/** does not match src/other/file.ts", async () => {
|
||||
assert(pathInScope("src/other/unauthorized.ts", ["src/api/**"], cwd) === false, "out of scope")
|
||||
})
|
||||
|
||||
await run("pathInScope: exact file matches", async () => {
|
||||
assert(pathInScope("src/middleware/jwt.ts", ["src/middleware/jwt.ts"], cwd) === true, "exact match")
|
||||
})
|
||||
|
||||
console.log("\n=== 4. pre-hook (PreHook) ===\n")
|
||||
|
||||
let activeIntentId: string | null = null
|
||||
const preHook = new PreHook({
|
||||
cwd,
|
||||
getActiveIntentId: () => activeIntentId,
|
||||
setActiveIntentId: (id) => {
|
||||
activeIntentId = id
|
||||
},
|
||||
requireIntentForDestructiveOnly: true,
|
||||
})
|
||||
|
||||
await run("PreHook: write_to_file with no intent => blocked", async () => {
|
||||
activeIntentId = null
|
||||
const r = await preHook.intercept("write_to_file", { path: "src/api/x.ts", content: "x" })
|
||||
assert(r.blocked === true, "blocked")
|
||||
assert(r.error != null && r.error.includes("select an active intent"), "error message")
|
||||
})
|
||||
|
||||
await run("PreHook: select_active_intent with valid ID => injectResult XML", async () => {
|
||||
const r = await preHook.intercept("select_active_intent", { intent_id: "INT-001" })
|
||||
assert(r.blocked === false, "not blocked")
|
||||
assert(r.injectResult != null && r.injectResult.includes("<intent_context>"), "XML injected")
|
||||
assert(activeIntentId === "INT-001", "active intent set")
|
||||
})
|
||||
|
||||
await run("PreHook: select_active_intent with invalid ID => blocked", async () => {
|
||||
const r = await preHook.intercept("select_active_intent", { intent_id: "INT-999" })
|
||||
assert(r.blocked === true, "blocked")
|
||||
assert(r.error != null && r.error.includes("INT-999"), "error mentions id")
|
||||
})
|
||||
|
||||
await run("PreHook: write_to_file in owned_scope => allowed", async () => {
|
||||
activeIntentId = "INT-001"
|
||||
const r = await preHook.intercept("write_to_file", {
|
||||
path: "src/api/weather.ts",
|
||||
content: "// code",
|
||||
})
|
||||
assert(r.blocked === false, "not blocked")
|
||||
})
|
||||
|
||||
await run("PreHook: write_to_file outside owned_scope => blocked", async () => {
|
||||
activeIntentId = "INT-001"
|
||||
const r = await preHook.intercept("write_to_file", {
|
||||
path: "src/other/unauthorized.ts",
|
||||
content: "// bad",
|
||||
})
|
||||
assert(r.blocked === true, "blocked")
|
||||
assert(r.error != null && r.error.includes("Scope Violation"), "scope violation message")
|
||||
})
|
||||
|
||||
await run("PreHook: read_file (safe) without intent => allowed", async () => {
|
||||
activeIntentId = null
|
||||
const r = await preHook.intercept("read_file", { path: "src/api/x.ts" })
|
||||
assert(r.blocked === false, "safe tool allowed without intent")
|
||||
})
|
||||
|
||||
// TDD: path traversal must be blocked (test first, then implement)
|
||||
await run("PreHook: write_to_file with path traversal (..) => blocked", async () => {
|
||||
activeIntentId = "INT-001"
|
||||
const r = await preHook.intercept("write_to_file", {
|
||||
path: "src/api/../../../etc/escape.ts",
|
||||
content: "// path traversal",
|
||||
})
|
||||
assert(r.blocked === true, "blocked")
|
||||
assert(r.error != null && r.error.toLowerCase().includes("path"), "error mentions path/traversal")
|
||||
})
|
||||
|
||||
console.log("\n=== 5. post-hook (appendAgentTrace) ===\n")
|
||||
|
||||
const tracePath = path.join(cwd, ".orchestration", "agent_trace.jsonl")
|
||||
// Start fresh for this test
|
||||
try {
|
||||
await fs.unlink(tracePath)
|
||||
} catch {
|
||||
// ignore if missing
|
||||
}
|
||||
|
||||
await run("appendAgentTrace creates .orchestration/agent_trace.jsonl", async () => {
|
||||
await appendAgentTrace(cwd, {
|
||||
relativePath: "src/api/weather.ts",
|
||||
content: "// weather API\nconst x = 1;",
|
||||
intentId: "INT-001",
|
||||
mutationClass: "INTENT_EVOLUTION",
|
||||
sessionLogId: "session-1",
|
||||
modelIdentifier: "test-model",
|
||||
vcsRevisionId: "abc123",
|
||||
})
|
||||
const raw = await fs.readFile(tracePath, "utf-8")
|
||||
const line = raw.trim().split("\n")[0]
|
||||
assert(line != null, "at least one line")
|
||||
const entry = JSON.parse(line!)
|
||||
assert(entry.id != null, "id")
|
||||
assert(entry.timestamp != null, "timestamp")
|
||||
assert(entry.vcs?.revision_id === "abc123", "vcs")
|
||||
assert(entry.files?.length === 1, "one file")
|
||||
assert(entry.files[0].relative_path === "src/api/weather.ts", "path")
|
||||
const conv = entry.files[0].conversations[0]
|
||||
assert(conv.contributor?.entity_type === "AI", "contributor")
|
||||
assert(conv.ranges?.[0]?.content_hash?.startsWith("sha256:"), "content_hash")
|
||||
assert(conv.related?.[0]?.value === "INT-001", "related intent")
|
||||
})
|
||||
|
||||
await run("appendAgentTrace appends second entry", async () => {
|
||||
await appendAgentTrace(cwd, {
|
||||
relativePath: "src/api/forecast.ts",
|
||||
content: "// forecast",
|
||||
intentId: "INT-001",
|
||||
})
|
||||
const raw = await fs.readFile(tracePath, "utf-8")
|
||||
const lines = raw.trim().split("\n").filter(Boolean)
|
||||
assert(lines.length >= 2, "two or more lines")
|
||||
})
|
||||
|
||||
await run("appendAgentTrace injects REQ-ID into related array", async () => {
|
||||
await appendAgentTrace(cwd, {
|
||||
relativePath: "src/api/trace.ts",
|
||||
content: "// trace",
|
||||
intentId: "INT-001",
|
||||
reqId: "REQ-12345",
|
||||
})
|
||||
const raw = await fs.readFile(tracePath, "utf-8")
|
||||
const lines = raw.trim().split("\n").filter(Boolean)
|
||||
const lastLine = lines[lines.length - 1]
|
||||
assert(lastLine != null, "last line exists")
|
||||
const entry = JSON.parse(lastLine)
|
||||
const conv = entry.files[0].conversations[0]
|
||||
const reqRelated = conv.related?.find((r: { type: string }) => r.type === "request")
|
||||
assert(reqRelated != null && reqRelated.value === "REQ-12345", "REQ-ID in related array")
|
||||
})
|
||||
|
||||
console.log("\n=== 6. middleware (full flow) ===\n")
|
||||
|
||||
activeIntentId = null // reset so we simulate: select_intent then write
|
||||
const middleware = new HookMiddleware({
|
||||
preHook,
|
||||
getActiveIntentId: () => activeIntentId,
|
||||
getCwd: () => cwd,
|
||||
getSessionLogId: () => "log-1",
|
||||
getModelId: () => "model-1",
|
||||
getVcsRevisionId: () => "rev-1",
|
||||
})
|
||||
|
||||
await run(
|
||||
"Middleware: preToolUse(select_active_intent) then preToolUse(write_to_file) then postToolUse",
|
||||
async () => {
|
||||
const r1 = await middleware.preToolUse("select_active_intent", { intent_id: "INT-001" })
|
||||
assert(!r1.blocked && r1.injectResult != null, "select ok")
|
||||
const r2 = await middleware.preToolUse("write_to_file", {
|
||||
path: "src/api/demo.ts",
|
||||
content: "// demo",
|
||||
})
|
||||
assert(!r2.blocked, "write allowed")
|
||||
await middleware.postToolUse("write_to_file", { path: "src/api/demo.ts", content: "// demo" }, {})
|
||||
const raw = await fs.readFile(tracePath, "utf-8")
|
||||
const lastLine = raw.trim().split("\n").filter(Boolean).pop()
|
||||
assert(lastLine != null, "new line")
|
||||
const entry = JSON.parse(lastLine!)
|
||||
assert(entry.files[0].relative_path === "src/api/demo.ts", "demo.ts traced")
|
||||
},
|
||||
)
|
||||
|
||||
console.log("\n=== All hook checks passed. ===\n")
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
114
scripts/test-trace-scenario.ts
Normal file
114
scripts/test-trace-scenario.ts
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
#!/usr/bin/env npx tsx
|
||||
/**
|
||||
* Scenario test for the AI-Native Git Layer (agent_trace.jsonl).
|
||||
*
|
||||
* Run from repo root:
|
||||
* pnpm tsx scripts/test-trace-scenario.ts
|
||||
* or
|
||||
* npx tsx scripts/test-trace-scenario.ts
|
||||
*
|
||||
* This script simulates: select_active_intent → write_to_file → post-hook appends to agent_trace.jsonl.
|
||||
* It then prints the last trace entry so you can verify intent_id, mutation_class, content_hash, and REQ-ID.
|
||||
*/
|
||||
import fs from "fs/promises"
|
||||
import path from "path"
|
||||
|
||||
import { PreHook } from "../src/hooks/pre-hook"
|
||||
import { HookMiddleware } from "../src/hooks/middleware"
|
||||
import { contentHash } from "../src/hooks/content-hash"
|
||||
|
||||
const cwd = process.cwd()
|
||||
const tracePath = path.join(cwd, ".orchestration", "agent_trace.jsonl")
|
||||
|
||||
async function main() {
|
||||
console.log("=== Traceability scenario (terminal test) ===\n")
|
||||
|
||||
let activeIntentId: string | null = null
|
||||
const preHook = new PreHook({
|
||||
cwd,
|
||||
getActiveIntentId: () => activeIntentId,
|
||||
setActiveIntentId: (id) => {
|
||||
activeIntentId = id
|
||||
},
|
||||
requireIntentForDestructiveOnly: true,
|
||||
})
|
||||
|
||||
const middleware = new HookMiddleware({
|
||||
preHook,
|
||||
getActiveIntentId: () => activeIntentId,
|
||||
getCwd: () => cwd,
|
||||
getReqId: () => "REQ-scenario-" + Date.now(),
|
||||
getSessionLogId: () => "session-scenario",
|
||||
getModelId: () => "scenario-runner",
|
||||
getVcsRevisionId: () => undefined,
|
||||
})
|
||||
|
||||
// Step 1: Select intent (like the agent would)
|
||||
console.log("1. select_active_intent(INT-001) ...")
|
||||
const selectResult = await middleware.preToolUse("select_active_intent", { intent_id: "INT-001" })
|
||||
if (selectResult.blocked) {
|
||||
console.error(" FAIL: select_active_intent blocked:", selectResult.error)
|
||||
process.exit(1)
|
||||
}
|
||||
console.log(" OK – intent context loaded\n")
|
||||
|
||||
// Step 2: Simulate write_to_file with intent_id and mutation_class
|
||||
const writeParams = {
|
||||
path: "src/api/weather.ts",
|
||||
content: "// Weather API\nconst getWeather = () => ({ temp: 72 })\nexport { getWeather }\n",
|
||||
intent_id: "INT-001",
|
||||
mutation_class: "INTENT_EVOLUTION" as const,
|
||||
}
|
||||
console.log("2. write_to_file (pre-hook check) ...")
|
||||
const preWrite = await middleware.preToolUse("write_to_file", writeParams)
|
||||
if (preWrite.blocked) {
|
||||
console.error(" FAIL: write_to_file blocked:", preWrite.error)
|
||||
process.exit(1)
|
||||
}
|
||||
console.log(" OK – within scope\n")
|
||||
|
||||
// Step 3: Post-hook appends to agent_trace.jsonl (like after real write)
|
||||
console.log("3. postToolUse (append to agent_trace.jsonl) ...")
|
||||
await middleware.postToolUse("write_to_file", writeParams, {})
|
||||
console.log(" OK – trace entry appended\n")
|
||||
|
||||
// Step 4: Read and display the last trace entry
|
||||
const raw = await fs.readFile(tracePath, "utf-8")
|
||||
const lines = raw.trim().split("\n").filter(Boolean)
|
||||
const lastLine = lines[lines.length - 1]
|
||||
if (!lastLine) {
|
||||
console.error(" FAIL: no lines in agent_trace.jsonl")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const entry = JSON.parse(lastLine)
|
||||
const expectedHash = contentHash(writeParams.content)
|
||||
|
||||
console.log("4. Last trace entry (from .orchestration/agent_trace.jsonl):\n")
|
||||
console.log(JSON.stringify(entry, null, 2))
|
||||
console.log("\n--- Checks ---")
|
||||
console.log(
|
||||
" intent (specification):",
|
||||
entry.files[0].conversations[0].related?.find((r: { type: string }) => r.type === "specification")?.value ??
|
||||
"missing",
|
||||
)
|
||||
console.log(
|
||||
" REQ-ID (request): ",
|
||||
entry.files[0].conversations[0].related?.find((r: { type: string }) => r.type === "request")?.value ??
|
||||
"missing",
|
||||
)
|
||||
console.log(
|
||||
" content_hash in ranges: ",
|
||||
entry.files[0].conversations[0].ranges[0]?.content_hash?.startsWith("sha256:") ? "yes" : "no",
|
||||
)
|
||||
console.log(
|
||||
" expected hash match: ",
|
||||
entry.files[0].conversations[0].ranges[0]?.content_hash === expectedHash ? "yes" : "no",
|
||||
)
|
||||
console.log("\n=== Scenario done. ===\n")
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
47
scripts/test_phase1.js
Normal file
47
scripts/test_phase1.js
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
|
||||
// Load active intents
|
||||
const intentsPath = path.join(__dirname, "..", ".orchestration", "active_intents.yaml")
|
||||
const yaml = require("js-yaml")
|
||||
const intents = yaml.load(fs.readFileSync(intentsPath, "utf8")).active_intents
|
||||
|
||||
// Simple PreHook simulation
|
||||
function preHook(intentId, targetFile) {
|
||||
const intent = intents.find((i) => i.id === intentId)
|
||||
if (!intent) {
|
||||
throw new Error("You must select a valid active Intent ID before writing code.")
|
||||
}
|
||||
|
||||
const allowed = intent.owned_scope.some((scope) => targetFile.startsWith(scope.replace("**", "")))
|
||||
if (!allowed) {
|
||||
throw new Error(`Scope Violation: ${intentId} is not authorized to edit ${targetFile}`)
|
||||
}
|
||||
|
||||
console.log(`PreHook Passed: ${targetFile} is within scope for ${intentId}`)
|
||||
}
|
||||
|
||||
// Simulate AI writing a file
|
||||
function writeFile(intentId, filePath, content) {
|
||||
preHook(intentId, filePath)
|
||||
fs.writeFileSync(filePath, content)
|
||||
console.log(`File written successfully: ${filePath}`)
|
||||
}
|
||||
|
||||
// === TEST CASES ===
|
||||
try {
|
||||
// Valid case
|
||||
writeFile("INT-001", "src/api/weather.ts", "// Weather API code here")
|
||||
|
||||
// Invalid scope
|
||||
writeFile("INT-001", "src/db/db.ts", "// DB code here")
|
||||
} catch (err) {
|
||||
console.error("Error:", err.message)
|
||||
}
|
||||
|
||||
try {
|
||||
// Invalid intent
|
||||
writeFile("INT-999", "src/api/weather.ts", "// Should fail")
|
||||
} catch (err) {
|
||||
console.error("Error:", err.message)
|
||||
}
|
||||
25
scripts/test_post_hook.ts
Normal file
25
scripts/test_post_hook.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import path from "path"
|
||||
import { appendAgentTrace } from "./post-hook" // adjust path if needed
|
||||
import fs from "fs/promises"
|
||||
|
||||
async function runTest() {
|
||||
const cwd = process.cwd()
|
||||
const testFile = "src/test_file.txt"
|
||||
|
||||
// Ensure the src folder exists
|
||||
await fs.mkdir(path.join(cwd, "src"), { recursive: true })
|
||||
|
||||
// Call your Post-Hook
|
||||
await appendAgentTrace(cwd, {
|
||||
relativePath: testFile,
|
||||
content: "console.log('hello world')",
|
||||
intentId: "INT-001",
|
||||
mutationClass: "CREATE",
|
||||
reqId: "REQ-123",
|
||||
sessionLogId: "SESSION-456",
|
||||
})
|
||||
|
||||
console.log("✅ Test completed. Check .orchestration/agent_trace.jsonl and intent_map.md")
|
||||
}
|
||||
|
||||
runTest()
|
||||
1
src/api/weather.ts
Normal file
1
src/api/weather.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
// Weather API code here
|
||||
|
|
@ -468,6 +468,12 @@ export class NativeToolCallParser {
|
|||
nativeArgs = {
|
||||
path: partialArgs.path,
|
||||
content: partialArgs.content,
|
||||
intent_id: partialArgs.intent_id,
|
||||
mutation_class: partialArgs.mutation_class as
|
||||
| "AST_REFACTOR"
|
||||
| "INTENT_EVOLUTION"
|
||||
| "NEW_FILE"
|
||||
| undefined,
|
||||
}
|
||||
}
|
||||
break
|
||||
|
|
@ -527,6 +533,15 @@ export class NativeToolCallParser {
|
|||
}
|
||||
break
|
||||
|
||||
case "append_lesson_learned":
|
||||
if (partialArgs.lesson !== undefined || partialArgs.file_path !== undefined) {
|
||||
nativeArgs = {
|
||||
lesson: partialArgs.lesson,
|
||||
file_path: partialArgs.file_path,
|
||||
}
|
||||
}
|
||||
break
|
||||
|
||||
case "search_files":
|
||||
if (partialArgs.path !== undefined || partialArgs.regex !== undefined) {
|
||||
nativeArgs = {
|
||||
|
|
@ -905,6 +920,21 @@ export class NativeToolCallParser {
|
|||
nativeArgs = {
|
||||
path: args.path,
|
||||
content: args.content,
|
||||
intent_id: args.intent_id,
|
||||
mutation_class: args.mutation_class as
|
||||
| "AST_REFACTOR"
|
||||
| "INTENT_EVOLUTION"
|
||||
| "NEW_FILE"
|
||||
| undefined,
|
||||
} as NativeArgsFor<TName>
|
||||
}
|
||||
break
|
||||
|
||||
case "append_lesson_learned":
|
||||
if (args.lesson !== undefined) {
|
||||
nativeArgs = {
|
||||
lesson: args.lesson,
|
||||
file_path: args.file_path,
|
||||
} as NativeArgsFor<TName>
|
||||
}
|
||||
break
|
||||
|
|
|
|||
|
|
@ -34,12 +34,14 @@ import { updateTodoListTool } from "../tools/UpdateTodoListTool"
|
|||
import { runSlashCommandTool } from "../tools/RunSlashCommandTool"
|
||||
import { skillTool } from "../tools/SkillTool"
|
||||
import { generateImageTool } from "../tools/GenerateImageTool"
|
||||
import { appendLessonLearnedTool } from "../tools/AppendLessonLearnedTool"
|
||||
import { applyDiffTool as applyDiffToolClass } from "../tools/ApplyDiffTool"
|
||||
import { isValidToolName, validateToolUse } from "../tools/validateToolUse"
|
||||
import { codebaseSearchTool } from "../tools/CodebaseSearchTool"
|
||||
|
||||
import { formatResponse } from "../prompts/responses"
|
||||
import { sanitizeToolUseId } from "../../utils/tool-id"
|
||||
import { HookMiddleware, PreHook } from "../../hooks"
|
||||
|
||||
/**
|
||||
* Processes and presents assistant message content to the user interface.
|
||||
|
|
@ -383,6 +385,8 @@ export async function presentAssistantMessage(cline: Task) {
|
|||
return `[${block.name} for '${block.params.skill}'${block.params.args ? ` with args: ${block.params.args}` : ""}]`
|
||||
case "generate_image":
|
||||
return `[${block.name} for '${block.params.path}']`
|
||||
case "append_lesson_learned":
|
||||
return `[${block.name}]`
|
||||
default:
|
||||
return `[${block.name}]`
|
||||
}
|
||||
|
|
@ -603,6 +607,17 @@ export async function presentAssistantMessage(cline: Task) {
|
|||
stateExperiments,
|
||||
includedTools,
|
||||
)
|
||||
|
||||
// Hook Engine: Pre-Hook validation for intent enforcement and guardrails
|
||||
const preHookResult = await hookMiddleware.preToolUse(block.name, block.params)
|
||||
if (preHookResult.blocked) {
|
||||
pushToolResult(preHookResult.error || "Tool blocked by pre-hook")
|
||||
break
|
||||
}
|
||||
if (preHookResult.injectResult) {
|
||||
pushToolResult(preHookResult.injectResult)
|
||||
break
|
||||
}
|
||||
} catch (error) {
|
||||
cline.consecutiveMistakeCount++
|
||||
// For validation errors (unknown tool, tool not allowed for mode), we need to:
|
||||
|
|
@ -675,15 +690,48 @@ export async function presentAssistantMessage(cline: Task) {
|
|||
}
|
||||
}
|
||||
|
||||
// Hook Engine: Post-Hook for write_to_file appends to agent_trace.jsonl
|
||||
const preHook = new PreHook({
|
||||
cwd: cline.cwd,
|
||||
getActiveIntentId: () => cline.getActiveIntentId(),
|
||||
setActiveIntentId: (id) => cline.setActiveIntentId(id),
|
||||
requireIntentForDestructiveOnly: true,
|
||||
})
|
||||
const hookMiddleware = new HookMiddleware({
|
||||
preHook,
|
||||
getActiveIntentId: () => cline.getActiveIntentId(),
|
||||
getCwd: () => cline.cwd,
|
||||
getReqId: () => cline.taskId,
|
||||
getSessionLogId: () => undefined,
|
||||
getModelId: () => cline.api.getModel()?.id,
|
||||
getVcsRevisionId: () => undefined,
|
||||
})
|
||||
|
||||
switch (block.name) {
|
||||
case "write_to_file":
|
||||
case "select_active_intent":
|
||||
// Handled entirely by pre-hook (injectResult pushed above)
|
||||
break
|
||||
case "write_to_file": {
|
||||
await checkpointSaveAndMark(cline)
|
||||
await writeToFileTool.handle(cline, block as ToolUse<"write_to_file">, {
|
||||
askApproval,
|
||||
handleError,
|
||||
pushToolResult,
|
||||
onWriteToFileSuccess: async (p) => {
|
||||
await hookMiddleware.postToolUse(
|
||||
"write_to_file",
|
||||
{
|
||||
path: p.path,
|
||||
content: p.content,
|
||||
intent_id: p.intent_id,
|
||||
mutation_class: p.mutation_class,
|
||||
},
|
||||
{},
|
||||
)
|
||||
},
|
||||
})
|
||||
break
|
||||
}
|
||||
case "update_todo_list":
|
||||
await updateTodoListTool.handle(cline, block as ToolUse<"update_todo_list">, {
|
||||
askApproval,
|
||||
|
|
@ -841,6 +889,13 @@ export async function presentAssistantMessage(cline: Task) {
|
|||
pushToolResult,
|
||||
})
|
||||
break
|
||||
case "append_lesson_learned":
|
||||
await appendLessonLearnedTool.handle(cline, block as ToolUse<"append_lesson_learned">, {
|
||||
askApproval,
|
||||
handleError,
|
||||
pushToolResult,
|
||||
})
|
||||
break
|
||||
case "generate_image":
|
||||
await checkpointSaveAndMark(cline)
|
||||
await generateImageTool.handle(cline, block as ToolUse<"generate_image">, {
|
||||
|
|
|
|||
|
|
@ -30,6 +30,27 @@ export const formatResponse = {
|
|||
error,
|
||||
}),
|
||||
|
||||
/** Standardized JSON for autonomous recovery when user rejects a destructive tool. */
|
||||
toolErrorUserRejected: (toolName?: string) =>
|
||||
JSON.stringify({
|
||||
status: "error",
|
||||
type: "user_rejected",
|
||||
message: "The user rejected this operation.",
|
||||
tool: toolName,
|
||||
suggestion: "Do not retry the same operation; try a different approach or ask the user for permission.",
|
||||
}),
|
||||
|
||||
/** Standardized JSON for scope violation so the LLM can request scope expansion. */
|
||||
toolErrorScopeViolation: (intentId: string, filename: string) =>
|
||||
JSON.stringify({
|
||||
status: "error",
|
||||
type: "scope_violation",
|
||||
message: `Scope Violation: ${intentId} is not authorized to edit [${filename}]. Request scope expansion.`,
|
||||
intent_id: intentId,
|
||||
path: filename,
|
||||
suggestion: "Request scope expansion in .orchestration/active_intents.yaml or choose another intent.",
|
||||
}),
|
||||
|
||||
rooIgnoreError: (path: string) =>
|
||||
JSON.stringify({
|
||||
status: "error",
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import { isEmpty } from "../../utils/object"
|
|||
import { McpHub } from "../../services/mcp/McpHub"
|
||||
import { CodeIndexManager } from "../../services/code-index/manager"
|
||||
import { SkillsManager } from "../../services/skills/SkillsManager"
|
||||
import { INTENT_DRIVEN_PROMPT_SNIPPET } from "../../hooks/intent-prompt-snippet"
|
||||
|
||||
import type { SystemPromptSettings } from "./types"
|
||||
import {
|
||||
|
|
@ -84,6 +85,8 @@ async function generatePrompt(
|
|||
|
||||
const basePrompt = `${roleDefinition}
|
||||
|
||||
${INTENT_DRIVEN_PROMPT_SNIPPET}
|
||||
|
||||
${markdownFormattingSection()}
|
||||
|
||||
${getSharedToolUseSection()}${toolsCatalog}
|
||||
|
|
|
|||
33
src/core/prompts/tools/native-tools/append_lesson_learned.ts
Normal file
33
src/core/prompts/tools/native-tools/append_lesson_learned.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import type OpenAI from "openai"
|
||||
|
||||
const APPEND_LESSON_LEARNED_DESCRIPTION = `Append a "Lesson Learned" entry to CLAUDE.md (or another file). Use this when a verification step (linter, test, or build) fails so that future sessions can avoid the same mistake.
|
||||
|
||||
Call this after a failed verification: record what went wrong and how to fix or avoid it. The lesson is appended under a "## Lessons Learned" section.`
|
||||
|
||||
const LESSON_PARAMETER_DESCRIPTION = `The lesson text to record (e.g. what failed, why, and how to fix or avoid it).`
|
||||
|
||||
const FILE_PATH_PARAMETER_DESCRIPTION = `Optional. File to append to. Defaults to CLAUDE.md. Use a path relative to the workspace.`
|
||||
|
||||
export default {
|
||||
type: "function",
|
||||
function: {
|
||||
name: "append_lesson_learned",
|
||||
description: APPEND_LESSON_LEARNED_DESCRIPTION,
|
||||
strict: true,
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
lesson: {
|
||||
type: "string",
|
||||
description: LESSON_PARAMETER_DESCRIPTION,
|
||||
},
|
||||
file_path: {
|
||||
type: "string",
|
||||
description: FILE_PATH_PARAMETER_DESCRIPTION,
|
||||
},
|
||||
},
|
||||
required: ["lesson"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
} satisfies OpenAI.Chat.ChatCompletionTool
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import type OpenAI from "openai"
|
||||
import { selectActiveIntentToolDefinition } from "../../../../hooks/select-active-intent-tool"
|
||||
import accessMcpResource from "./access_mcp_resource"
|
||||
import { apply_diff } from "./apply_diff"
|
||||
import applyPatch from "./apply_patch"
|
||||
|
|
@ -20,6 +21,7 @@ import searchFiles from "./search_files"
|
|||
import switchMode from "./switch_mode"
|
||||
import updateTodoList from "./update_todo_list"
|
||||
import writeToFile from "./write_to_file"
|
||||
import appendLessonLearned from "./append_lesson_learned"
|
||||
|
||||
export { getMcpServerTools } from "./mcp_server"
|
||||
export { convertOpenAIToolToAnthropic, convertOpenAIToolsToAnthropic } from "./converters"
|
||||
|
|
@ -47,6 +49,7 @@ export function getNativeTools(options: NativeToolsOptions = {}): OpenAI.Chat.Ch
|
|||
}
|
||||
|
||||
return [
|
||||
selectActiveIntentToolDefinition,
|
||||
accessMcpResource,
|
||||
apply_diff,
|
||||
applyPatch,
|
||||
|
|
@ -68,6 +71,7 @@ export function getNativeTools(options: NativeToolsOptions = {}): OpenAI.Chat.Ch
|
|||
switchMode,
|
||||
updateTodoList,
|
||||
writeToFile,
|
||||
appendLessonLearned,
|
||||
] satisfies OpenAI.Chat.ChatCompletionTool[]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import type OpenAI from "openai"
|
||||
|
||||
const MUTATION_CLASS_ENUM = ["AST_REFACTOR", "INTENT_EVOLUTION", "NEW_FILE"] as const
|
||||
|
||||
const WRITE_TO_FILE_DESCRIPTION = `Request to write content to a file. This tool is primarily used for creating new files or for scenarios where a complete rewrite of an existing file is intentionally required. If the file exists, it will be overwritten. If it doesn't exist, it will be created. This tool will automatically create any directories needed to write the file.
|
||||
|
||||
**Important:** You should prefer using other editing tools over write_to_file when making changes to existing files, since write_to_file is slower and cannot handle large files. Use write_to_file primarily for new file creation.
|
||||
|
|
@ -8,13 +10,22 @@ When using this tool, use it directly with the desired content. You do not need
|
|||
|
||||
When creating a new project, organize all new files within a dedicated project directory unless the user specifies otherwise. Structure the project logically, adhering to best practices for the specific type of project being created.
|
||||
|
||||
**Traceability:** You MUST provide intent_id (the active intent from select_active_intent) and mutation_class:
|
||||
- AST_REFACTOR: Syntax/structural change, same intent (e.g. rename, format, extract function).
|
||||
- INTENT_EVOLUTION: New feature or behavior change tied to the intent.
|
||||
- NEW_FILE: Creating a file that did not exist before.
|
||||
|
||||
Example: Writing a configuration file
|
||||
{ "path": "frontend-config.json", "content": "{\\n \\"apiEndpoint\\": \\"https://api.example.com\\",\\n \\"theme\\": {\\n \\"primaryColor\\": \\"#007bff\\"\\n }\\n}" }`
|
||||
{ "path": "frontend-config.json", "content": "{\\n \\"apiEndpoint\\": \\"https://api.example.com\\",\\n \\"theme\\": {\\n \\"primaryColor\\": \\"#007bff\\"\\n }\\n}", "intent_id": "INT-001", "mutation_class": "INTENT_EVOLUTION" }`
|
||||
|
||||
const PATH_PARAMETER_DESCRIPTION = `The path of the file to write to (relative to the current workspace directory)`
|
||||
|
||||
const CONTENT_PARAMETER_DESCRIPTION = `The content to write to the file. ALWAYS provide the COMPLETE intended content of the file, without any truncation or omissions. You MUST include ALL parts of the file, even if they haven't been modified. Do NOT include line numbers in the content.`
|
||||
|
||||
const INTENT_ID_DESCRIPTION = `The ID of the active intent (from select_active_intent) that this write serves. Required for traceability.`
|
||||
|
||||
const MUTATION_CLASS_DESCRIPTION = `Semantic classification: AST_REFACTOR (syntax change, same intent), INTENT_EVOLUTION (new feature/behavior), or NEW_FILE (creating a new file).`
|
||||
|
||||
export default {
|
||||
type: "function",
|
||||
function: {
|
||||
|
|
@ -32,8 +43,17 @@ export default {
|
|||
type: "string",
|
||||
description: CONTENT_PARAMETER_DESCRIPTION,
|
||||
},
|
||||
intent_id: {
|
||||
type: "string",
|
||||
description: INTENT_ID_DESCRIPTION,
|
||||
},
|
||||
mutation_class: {
|
||||
type: "string",
|
||||
enum: MUTATION_CLASS_ENUM,
|
||||
description: MUTATION_CLASS_DESCRIPTION,
|
||||
},
|
||||
},
|
||||
required: ["path", "content"],
|
||||
required: ["path", "content", "intent_id", "mutation_class"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -266,6 +266,10 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
|
|||
|
||||
providerRef: WeakRef<ClineProvider>
|
||||
private readonly globalStoragePath: string
|
||||
/** Active intent ID set by select_active_intent (per task/session). Used by Hook Engine for scope enforcement. */
|
||||
private _activeIntentId: string | null = null
|
||||
/** Per-file content hashes from read_file (path -> sha256 hash). Used for optimistic locking on write_to_file. */
|
||||
private _fileReadHashes: Map<string, string> = new Map()
|
||||
abort: boolean = false
|
||||
currentRequestAbortController?: AbortController
|
||||
skipPrevResponseIdOnce: boolean = false
|
||||
|
|
@ -4670,6 +4674,25 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
|
|||
return this.workspacePath
|
||||
}
|
||||
|
||||
/** Active intent ID for Hook Engine (select_active_intent / scope enforcement). */
|
||||
public getActiveIntentId(): string | null {
|
||||
return this._activeIntentId
|
||||
}
|
||||
|
||||
public setActiveIntentId(id: string | null): void {
|
||||
this._activeIntentId = id
|
||||
}
|
||||
|
||||
/** Record content hash for a file read by read_file (optimistic locking: write compares disk hash to this). */
|
||||
public recordFileReadHash(relativePath: string, contentHash: string): void {
|
||||
this._fileReadHashes.set(relativePath, contentHash)
|
||||
}
|
||||
|
||||
/** Get the content hash recorded when the agent last read this file (if any). */
|
||||
public getFileReadHash(relativePath: string): string | undefined {
|
||||
return this._fileReadHashes.get(relativePath)
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides convenient access to high-level message operations.
|
||||
* Uses lazy initialization - the MessageManager is only created when first accessed.
|
||||
|
|
|
|||
80
src/core/tools/AppendLessonLearnedTool.ts
Normal file
80
src/core/tools/AppendLessonLearnedTool.ts
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
import path from "path"
|
||||
import fs from "fs/promises"
|
||||
|
||||
import { Task } from "../task/Task"
|
||||
import { formatResponse } from "../prompts/responses"
|
||||
import { getReadablePath } from "../../utils/path"
|
||||
import type { ToolUse } from "../../shared/tools"
|
||||
|
||||
import { BaseTool, ToolCallbacks } from "./BaseTool"
|
||||
|
||||
const DEFAULT_LESSONS_FILE = "CLAUDE.md"
|
||||
const LESSONS_HEADER = "## Lessons Learned"
|
||||
|
||||
interface AppendLessonLearnedParams {
|
||||
lesson: string
|
||||
file_path?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a "Lesson Learned" entry to CLAUDE.md (or file_path).
|
||||
* Used when a verification step (linter/test) fails so the agent can record what went wrong.
|
||||
*/
|
||||
export class AppendLessonLearnedTool extends BaseTool<"append_lesson_learned"> {
|
||||
readonly name = "append_lesson_learned" as const
|
||||
|
||||
async execute(params: AppendLessonLearnedParams, task: Task, callbacks: ToolCallbacks): Promise<void> {
|
||||
const { pushToolResult, handleError } = callbacks
|
||||
const relPath = params.file_path ?? DEFAULT_LESSONS_FILE
|
||||
const lesson = params.lesson?.trim()
|
||||
|
||||
if (!lesson) {
|
||||
task.consecutiveMistakeCount++
|
||||
task.recordToolError("append_lesson_learned")
|
||||
pushToolResult(await task.sayAndCreateMissingParamError("append_lesson_learned", "lesson"))
|
||||
return
|
||||
}
|
||||
|
||||
const absolutePath = path.resolve(task.cwd, relPath)
|
||||
|
||||
try {
|
||||
let content: string
|
||||
try {
|
||||
content = await fs.readFile(absolutePath, "utf-8")
|
||||
} catch (err: unknown) {
|
||||
if ((err as NodeJS.ErrnoException)?.code === "ENOENT") {
|
||||
content = ""
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
const timestamp = new Date().toISOString().slice(0, 10)
|
||||
const entry = `\n- **${timestamp}**: ${lesson.replace(/\n/g, " ")}\n`
|
||||
|
||||
if (!content.includes(LESSONS_HEADER)) {
|
||||
content = content.trimEnd()
|
||||
if (content) content += "\n\n"
|
||||
content += `${LESSONS_HEADER}\n${entry}`
|
||||
} else {
|
||||
const headerIndex = content.indexOf(LESSONS_HEADER)
|
||||
const afterHeader = content.indexOf("\n", headerIndex) + 1
|
||||
content = content.slice(0, afterHeader) + entry + content.slice(afterHeader)
|
||||
}
|
||||
|
||||
await fs.writeFile(absolutePath, content, "utf-8")
|
||||
|
||||
const readablePath = getReadablePath(task.cwd, relPath)
|
||||
pushToolResult(`Appended lesson to ${readablePath}.`)
|
||||
} catch (error) {
|
||||
await handleError("append_lesson_learned", error as Error)
|
||||
pushToolResult(
|
||||
formatResponse.toolError(
|
||||
`Failed to append lesson to ${getReadablePath(task.cwd, relPath)}: ${(error as Error).message}`,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const appendLessonLearnedTool = new AppendLessonLearnedTool()
|
||||
|
|
@ -3,6 +3,14 @@ import type { ToolName } from "@roo-code/types"
|
|||
import { Task } from "../task/Task"
|
||||
import type { ToolUse, HandleError, PushToolResult, AskApproval, NativeToolArgs } from "../../shared/tools"
|
||||
|
||||
/** Params passed to onWriteToFileSuccess after a successful write_to_file (for Hook Engine post-hook). */
|
||||
export interface WriteToFileSuccessParams {
|
||||
path: string
|
||||
content: string
|
||||
intent_id?: string
|
||||
mutation_class?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Callbacks passed to tool execution
|
||||
*/
|
||||
|
|
@ -11,6 +19,8 @@ export interface ToolCallbacks {
|
|||
handleError: HandleError
|
||||
pushToolResult: PushToolResult
|
||||
toolCallId?: string
|
||||
/** Called after successful write_to_file for Hook Engine agent_trace / post-hook. */
|
||||
onWriteToFileSuccess?: (params: WriteToFileSuccessParams) => void | Promise<void>
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import { extractTextFromFile, addLineNumbers, getSupportedBinaryFormats } from "
|
|||
import { readWithIndentation, readWithSlice } from "../../integrations/misc/indentation-reader"
|
||||
import { DEFAULT_LINE_LIMIT } from "../prompts/tools/native-tools/read_file"
|
||||
import type { ToolUse, PushToolResult } from "../../shared/tools"
|
||||
import { contentHash } from "../../hooks/content-hash"
|
||||
|
||||
import {
|
||||
DEFAULT_MAX_IMAGE_FILE_SIZE_MB,
|
||||
|
|
@ -219,6 +220,8 @@ export class ReadFileTool extends BaseTool<"read_file"> {
|
|||
const result = this.processTextFile(fileContent, entry)
|
||||
|
||||
await task.fileContextTracker.trackFileContext(relPath, "read_tool" as RecordSource)
|
||||
// Optimistic locking: record hash so write_to_file can detect parallel edits
|
||||
task.recordFileReadHash(relPath, contentHash(fileContent))
|
||||
|
||||
updateFileResult(relPath, {
|
||||
nativeContent: `File: ${relPath}\n${result}`,
|
||||
|
|
@ -393,6 +396,7 @@ export class ReadFileTool extends BaseTool<"read_file"> {
|
|||
const lineCount = content.split("\n").length
|
||||
|
||||
await task.fileContextTracker.trackFileContext(relPath, "read_tool" as RecordSource)
|
||||
task.recordFileReadHash(relPath, contentHash(content))
|
||||
|
||||
updateFileResult(relPath, {
|
||||
nativeContent:
|
||||
|
|
@ -798,6 +802,8 @@ export class ReadFileTool extends BaseTool<"read_file"> {
|
|||
|
||||
// Track file in context
|
||||
await task.fileContextTracker.trackFileContext(relPath, "read_tool")
|
||||
// Optimistic locking: record full-file hash for write_to_file stale detection
|
||||
task.recordFileReadHash(relPath, contentHash(rawContent))
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error)
|
||||
results.push(`File: ${relPath}\nError: ${errorMsg}`)
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ import { convertNewFileToUnifiedDiff, computeDiffStats, sanitizeUnifiedDiff } fr
|
|||
import type { ToolUse } from "../../shared/tools"
|
||||
|
||||
import { BaseTool, ToolCallbacks } from "./BaseTool"
|
||||
import { contentHash } from "../../hooks/content-hash"
|
||||
|
||||
interface WriteToFileParams {
|
||||
path: string
|
||||
|
|
@ -67,6 +68,21 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
|
|||
task.diffViewProvider.editType = fileExists ? "modify" : "create"
|
||||
}
|
||||
|
||||
// Optimistic locking: if we have a read-hash for this file, ensure disk hasn't changed (parallel agent or human edit)
|
||||
if (fileExists) {
|
||||
const expectedHash = task.getFileReadHash(relPath)
|
||||
if (expectedHash !== undefined) {
|
||||
const currentContent = await fs.readFile(absolutePath, "utf-8")
|
||||
const currentHash = contentHash(currentContent)
|
||||
if (currentHash !== expectedHash) {
|
||||
const staleError = `Stale File: The file "${relPath}" was modified since you read it (by another agent or the user). Your write is blocked to avoid overwriting those changes. Re-read the file with read_file and then apply your edits.`
|
||||
pushToolResult(formatResponse.toolError(staleError))
|
||||
await task.diffViewProvider.reset()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create parent directories early for new files to prevent ENOENT errors
|
||||
// in subsequent operations (e.g., diffViewProvider.open, fs.readFile)
|
||||
if (!fileExists) {
|
||||
|
|
@ -179,6 +195,15 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
|
|||
|
||||
pushToolResult(message)
|
||||
|
||||
const mutationClass = (params as Record<string, unknown>).mutation_class as string | undefined
|
||||
const intentId = (params as Record<string, unknown>).intent_id as string | undefined
|
||||
await callbacks.onWriteToFileSuccess?.({
|
||||
path: relPath,
|
||||
content: newContent,
|
||||
intent_id: intentId,
|
||||
mutation_class: mutationClass,
|
||||
})
|
||||
|
||||
await task.diffViewProvider.reset()
|
||||
this.resetPartialState()
|
||||
|
||||
|
|
|
|||
|
|
@ -3,14 +3,23 @@ import crypto from "crypto"
|
|||
const HASH_PREFIX = "sha256:"
|
||||
|
||||
/**
|
||||
* Compute a SHA-256 content hash for spatial independence.
|
||||
* If lines move, the hash of the content block remains valid.
|
||||
* Generate a SHA-256 hash of string content (spatial hashing utility).
|
||||
* Returns a prefixed hex string (e.g. "sha256:abc123...") for traceability.
|
||||
* Content hash remains valid even if line positions change.
|
||||
*/
|
||||
export function contentHash(content: string): string {
|
||||
const hash = crypto.createHash("sha256").update(content, "utf8").digest("hex")
|
||||
return `${HASH_PREFIX}${hash}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for contentHash - generates SHA-256 hash of string content.
|
||||
* Use for spatial hashing in agent trace and diff operations.
|
||||
*/
|
||||
export function sha256Hash(content: string): string {
|
||||
return contentHash(content)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a logical code block (e.g. by line range) and return its hash.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -34,8 +34,9 @@ export async function loadIntentContext(cwd: string, intentId: string): Promise<
|
|||
|
||||
/**
|
||||
* Build an XML block to inject as the tool result for select_active_intent.
|
||||
* Optionally include related agent trace entries for consolidated context.
|
||||
*/
|
||||
export function buildIntentContextXml(context: IntentContext): string {
|
||||
export function buildIntentContextXml(context: IntentContext, relatedTracePaths: string[] = []): string {
|
||||
const constraintsXml =
|
||||
context.constraints.length > 0
|
||||
? context.constraints.map((c) => ` <constraint>${escapeXml(c)}</constraint>`).join("\n")
|
||||
|
|
@ -45,9 +46,13 @@ export function buildIntentContextXml(context: IntentContext): string {
|
|||
? context.owned_scope.map((s) => ` <scope>${escapeXml(s)}</scope>`).join("\n")
|
||||
: " <scope>No scope restriction</scope>"
|
||||
const criteriaXml =
|
||||
(context.acceptance_criteria?.length ?? 0 > 0)
|
||||
(context.acceptance_criteria?.length ?? 0) > 0
|
||||
? context.acceptance_criteria!.map((a) => ` <criterion>${escapeXml(a)}</criterion>`).join("\n")
|
||||
: " <criterion>None specified</criterion>"
|
||||
const traceXml =
|
||||
relatedTracePaths.length > 0
|
||||
? relatedTracePaths.map((p) => ` <file>${escapeXml(p)}</file>`).join("\n")
|
||||
: " <file>None yet</file>"
|
||||
|
||||
return `<intent_context>
|
||||
<id>${escapeXml(context.id)}</id>
|
||||
|
|
@ -62,9 +67,34 @@ ${scopeXml}
|
|||
<acceptance_criteria>
|
||||
${criteriaXml}
|
||||
</acceptance_criteria>
|
||||
<related_agent_trace>
|
||||
${traceXml}
|
||||
</related_agent_trace>
|
||||
</intent_context>`
|
||||
}
|
||||
|
||||
/**
|
||||
* Load intent from active_intents.yaml, gather related agent_trace entries for that intent,
|
||||
* and return a consolidated XML context block (for Pre-Hook injection).
|
||||
*/
|
||||
export async function buildConsolidatedIntentContextXml(cwd: string, intentId: string): Promise<string | null> {
|
||||
const context = await loadIntentContext(cwd, intentId)
|
||||
if (!context) return null
|
||||
const traceLines = await readRecentTraceForIntent(cwd, intentId, 20)
|
||||
const paths = new Set<string>()
|
||||
for (const line of traceLines) {
|
||||
try {
|
||||
const entry = JSON.parse(line) as { files?: Array<{ relative_path?: string }> }
|
||||
for (const f of entry.files ?? []) {
|
||||
if (f.relative_path) paths.add(f.relative_path)
|
||||
}
|
||||
} catch {
|
||||
// skip malformed lines
|
||||
}
|
||||
}
|
||||
return buildIntentContextXml(context, [...paths])
|
||||
}
|
||||
|
||||
function escapeXml(s: string): string {
|
||||
return s
|
||||
.replace(/&/g, "&")
|
||||
|
|
|
|||
31
src/hooks/format.ts
Normal file
31
src/hooks/format.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* Minimal tool error formatters for pre-hook.
|
||||
* Avoids importing from core/prompts/responses to prevent vscode dependency in Node scripts.
|
||||
*/
|
||||
export const toolErrorFormat = {
|
||||
toolError: (error?: string) =>
|
||||
JSON.stringify({
|
||||
status: "error",
|
||||
message: "The tool execution failed",
|
||||
error,
|
||||
}),
|
||||
|
||||
toolErrorScopeViolation: (intentId: string, filename: string) =>
|
||||
JSON.stringify({
|
||||
status: "error",
|
||||
type: "scope_violation",
|
||||
message: `Scope Violation: ${intentId} is not authorized to edit [${filename}]. Request scope expansion.`,
|
||||
intent_id: intentId,
|
||||
path: filename,
|
||||
suggestion: "Request scope expansion in .orchestration/active_intents.yaml or choose another intent.",
|
||||
}),
|
||||
|
||||
toolErrorUserRejected: (toolName?: string) =>
|
||||
JSON.stringify({
|
||||
status: "error",
|
||||
type: "user_rejected",
|
||||
message: "The user rejected this operation.",
|
||||
tool: toolName,
|
||||
suggestion: "Do not retry the same operation; try a different approach or ask the user for permission.",
|
||||
}),
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ export * from "./types"
|
|||
export * from "./content-hash"
|
||||
export * from "./context-loader"
|
||||
export * from "./scope"
|
||||
export * from "./intent-ignore"
|
||||
export * from "./pre-hook"
|
||||
export * from "./post-hook"
|
||||
export * from "./middleware"
|
||||
|
|
|
|||
54
src/hooks/intent-ignore.ts
Normal file
54
src/hooks/intent-ignore.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import fs from "fs/promises"
|
||||
import path from "path"
|
||||
|
||||
import { pathMatchesAnyPattern } from "./scope"
|
||||
|
||||
const DEFAULT_INTENT_IGNORE_NAME = ".intentignore"
|
||||
const ORCHESTRATION_DIR = ".orchestration"
|
||||
const INTENT_PREFIX = "intent:"
|
||||
|
||||
export interface IntentIgnoreResult {
|
||||
pathPatterns: string[]
|
||||
excludedIntentIds: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Load .intentignore-style file: path patterns (one per line) and optional
|
||||
* "intent:ID" lines to exclude specific intents from receiving changes.
|
||||
* Convention: lines starting with "intent:" are intent IDs to exclude; all other
|
||||
* non-empty, non-comment lines are path glob patterns that no write may touch.
|
||||
*
|
||||
* @param cwd - Workspace root
|
||||
* @param intentIgnorePath - Optional path relative to cwd (e.g. ".orchestration/.intentignore")
|
||||
*/
|
||||
export async function loadIntentIgnore(cwd: string, intentIgnorePath?: string): Promise<IntentIgnoreResult> {
|
||||
const filePath = intentIgnorePath
|
||||
? path.resolve(cwd, intentIgnorePath)
|
||||
: path.join(cwd, ORCHESTRATION_DIR, DEFAULT_INTENT_IGNORE_NAME)
|
||||
try {
|
||||
const raw = await fs.readFile(filePath, "utf-8")
|
||||
const pathPatterns: string[] = []
|
||||
const excludedIntentIds: string[] = []
|
||||
for (const line of raw.split("\n")) {
|
||||
const trimmed = line.trim()
|
||||
if (!trimmed || trimmed.startsWith("#")) continue
|
||||
if (trimmed.startsWith(INTENT_PREFIX)) {
|
||||
excludedIntentIds.push(trimmed.slice(INTENT_PREFIX.length).trim())
|
||||
} else {
|
||||
pathPatterns.push(trimmed)
|
||||
}
|
||||
}
|
||||
return { pathPatterns, excludedIntentIds }
|
||||
} catch {
|
||||
return { pathPatterns: [], excludedIntentIds: [] }
|
||||
}
|
||||
}
|
||||
|
||||
export function isPathIgnored(relativePath: string, pathPatterns: string[]): boolean {
|
||||
return pathMatchesAnyPattern(relativePath, pathPatterns)
|
||||
}
|
||||
|
||||
export function isIntentExcluded(intentId: string | null, excludedIntentIds: string[]): boolean {
|
||||
if (!intentId) return false
|
||||
return excludedIntentIds.some((id) => id === intentId)
|
||||
}
|
||||
|
|
@ -6,6 +6,8 @@ export interface HookMiddlewareOptions {
|
|||
preHook: PreHook
|
||||
getActiveIntentId: () => string | null
|
||||
getCwd: () => string
|
||||
/** REQ-ID from Phase 1 - injected into agent_trace related array */
|
||||
getReqId?: () => string | undefined
|
||||
getSessionLogId?: () => string | undefined
|
||||
getModelId?: () => string | undefined
|
||||
getVcsRevisionId?: () => string | undefined
|
||||
|
|
@ -39,7 +41,8 @@ export class HookMiddleware {
|
|||
const contentParam = params.content
|
||||
if (typeof pathParam !== "string" || typeof contentParam !== "string") return
|
||||
|
||||
const intentId = this.options.getActiveIntentId()
|
||||
const intentId =
|
||||
(typeof params.intent_id === "string" ? params.intent_id.trim() : null) || this.options.getActiveIntentId()
|
||||
const mutationClass = (params.mutation_class as "AST_REFACTOR" | "INTENT_EVOLUTION" | "NEW_FILE") ?? "UNKNOWN"
|
||||
|
||||
await appendAgentTrace(this.options.getCwd(), {
|
||||
|
|
@ -47,6 +50,7 @@ export class HookMiddleware {
|
|||
content: contentParam,
|
||||
intentId,
|
||||
mutationClass,
|
||||
reqId: this.options.getReqId?.(),
|
||||
sessionLogId: this.options.getSessionLogId?.(),
|
||||
modelIdentifier: this.options.getModelId?.(),
|
||||
vcsRevisionId: this.options.getVcsRevisionId?.(),
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ export interface PostHookWriteParams {
|
|||
content: string
|
||||
intentId: string | null
|
||||
mutationClass?: MutationClass
|
||||
/** REQ-ID from Phase 1 - injected into related array for traceability */
|
||||
reqId?: string
|
||||
sessionLogId?: string
|
||||
modelIdentifier?: string
|
||||
vcsRevisionId?: string
|
||||
|
|
@ -28,6 +30,7 @@ export async function appendAgentTrace(cwd: string, params: PostHookWriteParams)
|
|||
content,
|
||||
intentId,
|
||||
mutationClass = "UNKNOWN",
|
||||
reqId,
|
||||
sessionLogId,
|
||||
modelIdentifier = "unknown",
|
||||
vcsRevisionId,
|
||||
|
|
@ -39,11 +42,15 @@ export async function appendAgentTrace(cwd: string, params: PostHookWriteParams)
|
|||
|
||||
const lines = content.split("\n")
|
||||
const fullRangeHash = contentHash(content)
|
||||
const related: Array<{ type: string; value: string }> = []
|
||||
if (intentId) related.push({ type: "specification", value: intentId })
|
||||
if (reqId) related.push({ type: "request", value: reqId })
|
||||
|
||||
const conversation: AgentTraceConversation = {
|
||||
url: sessionLogId,
|
||||
contributor: { entity_type: "AI", model_identifier: modelIdentifier },
|
||||
ranges: [{ start_line: 1, end_line: lines.length, content_hash: fullRangeHash }],
|
||||
related: intentId ? [{ type: "specification", value: intentId }] : [],
|
||||
related,
|
||||
}
|
||||
|
||||
const fileEntry: AgentTraceFileEntry = {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,20 @@
|
|||
import * as vscode from "vscode"
|
||||
import path from "path"
|
||||
|
||||
import type { HookResult, IntentContext, MutationClass } from "./types"
|
||||
import type { HookResult } from "./types"
|
||||
import { DESTRUCTIVE_TOOLS } from "./types"
|
||||
import { loadIntentContext, buildIntentContextXml } from "./context-loader"
|
||||
import { loadIntentContext, buildConsolidatedIntentContextXml } from "./context-loader"
|
||||
import { loadIntentIgnore, isPathIgnored, isIntentExcluded } from "./intent-ignore"
|
||||
import type { IntentIgnoreResult } from "./intent-ignore"
|
||||
import { pathInScope } from "./scope"
|
||||
import { toolErrorFormat } from "./format"
|
||||
|
||||
/** Block paths that escape workspace (.. or absolute outside cwd). */
|
||||
function isPathTraversal(relPath: string, cwd: string): boolean {
|
||||
const normalized = path.normalize(relPath)
|
||||
if (normalized.includes("..")) return true
|
||||
const resolved = path.resolve(cwd, relPath)
|
||||
return !resolved.startsWith(cwd)
|
||||
}
|
||||
|
||||
export interface PreHookOptions {
|
||||
cwd: string
|
||||
|
|
@ -20,11 +30,19 @@ export interface PreHookOptions {
|
|||
/**
|
||||
* Pre-Hook: intercepts tool execution to enforce intent context and scope.
|
||||
* - select_active_intent: load context, return XML, set active intent.
|
||||
* - Destructive tools: require active intent; optional HITL; scope check for write_to_file.
|
||||
* - Destructive tools: require active intent; .intentignore exclusion; scope check for write_to_file; optional UI-blocking approval.
|
||||
*/
|
||||
export class PreHook {
|
||||
private intentIgnoreCache: IntentIgnoreResult | null = null
|
||||
|
||||
constructor(private options: PreHookOptions) {}
|
||||
|
||||
private async getIntentIgnore(): Promise<IntentIgnoreResult> {
|
||||
if (this.intentIgnoreCache) return this.intentIgnoreCache
|
||||
this.intentIgnoreCache = await loadIntentIgnore(this.options.cwd, this.options.intentIgnorePath)
|
||||
return this.intentIgnoreCache
|
||||
}
|
||||
|
||||
async intercept(toolName: string, params: Record<string, unknown>): Promise<HookResult> {
|
||||
const { cwd, getActiveIntentId, setActiveIntentId } = this.options
|
||||
|
||||
|
|
@ -34,23 +52,22 @@ export class PreHook {
|
|||
if (!intentId) {
|
||||
return { blocked: true, error: "You must provide a valid intent_id when calling select_active_intent." }
|
||||
}
|
||||
const context = await loadIntentContext(cwd, intentId)
|
||||
if (!context) {
|
||||
const xml = await buildConsolidatedIntentContextXml(cwd, intentId)
|
||||
if (!xml) {
|
||||
return {
|
||||
blocked: true,
|
||||
error: `You must cite a valid active Intent ID. Intent "${intentId}" was not found in .orchestration/active_intents.yaml.`,
|
||||
}
|
||||
}
|
||||
setActiveIntentId(intentId)
|
||||
const xml = buildIntentContextXml(context)
|
||||
return { blocked: false, injectResult: xml }
|
||||
}
|
||||
|
||||
const isDestructive = (DESTRUCTIVE_TOOLS as readonly string[]).includes(toolName)
|
||||
const requireIntent = this.options.requireIntentForDestructiveOnly ? isDestructive : true
|
||||
const activeId = getActiveIntentId()
|
||||
|
||||
if (requireIntent) {
|
||||
const activeId = getActiveIntentId()
|
||||
if (!activeId) {
|
||||
return {
|
||||
blocked: true,
|
||||
|
|
@ -58,32 +75,67 @@ export class PreHook {
|
|||
}
|
||||
}
|
||||
|
||||
// Scope enforcement for write_to_file
|
||||
if (toolName === "write_to_file" && params.path) {
|
||||
const relPath = String(params.path)
|
||||
const ignore = await this.getIntentIgnore()
|
||||
if (isIntentExcluded(activeId, ignore.excludedIntentIds)) {
|
||||
return {
|
||||
blocked: true,
|
||||
error: toolErrorFormat.toolError(
|
||||
`Intent ${activeId} is listed in .intentignore and cannot be modified. Choose another intent or ask the user to update .intentignore.`,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// Scope and .intentignore path checks for file-writing tools
|
||||
const filePathParam =
|
||||
toolName === "write_to_file"
|
||||
? params.path
|
||||
: [
|
||||
"edit",
|
||||
"search_and_replace",
|
||||
"search_replace",
|
||||
"edit_file",
|
||||
"apply_patch",
|
||||
"apply_diff",
|
||||
].includes(toolName)
|
||||
? (params.file_path ?? params.path)
|
||||
: undefined
|
||||
if (filePathParam) {
|
||||
const relPath = String(filePathParam)
|
||||
if (isPathTraversal(relPath, cwd)) {
|
||||
return {
|
||||
blocked: true,
|
||||
error: `Path traversal not allowed: "${relPath}" would escape the workspace. Use a path relative to the workspace only.`,
|
||||
}
|
||||
}
|
||||
if (isPathIgnored(relPath, ignore.pathPatterns)) {
|
||||
return {
|
||||
blocked: true,
|
||||
error: toolErrorFormat.toolError(
|
||||
`Path "${relPath}" is excluded by .intentignore. You are not authorized to edit it.`,
|
||||
),
|
||||
}
|
||||
}
|
||||
const context = await loadIntentContext(cwd, activeId)
|
||||
if (context && context.owned_scope.length > 0 && !pathInScope(relPath, context.owned_scope, cwd)) {
|
||||
return {
|
||||
blocked: true,
|
||||
error: `Scope Violation: ${activeId} is not authorized to edit "${relPath}". Request scope expansion in active_intents.yaml or choose another intent.`,
|
||||
error: toolErrorFormat.toolErrorScopeViolation(activeId, relPath),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Optional: HITL for destructive tools (can be wired via askApproval in host)
|
||||
// UI-blocking authorization for destructive tools (e.g. showWarningMessage Approve/Reject)
|
||||
if (isDestructive && this.options.confirmDestructive) {
|
||||
const approved = await this.options.confirmDestructive(toolName, params)
|
||||
if (!approved) {
|
||||
return {
|
||||
blocked: true,
|
||||
error: toolErrorFormat.toolErrorUserRejected(toolName),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { blocked: false }
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional: prompt for Human-in-the-Loop approval on destructive actions.
|
||||
* Call this from the host when askApproval is invoked for destructive tools.
|
||||
*/
|
||||
static async askApprovalDestructive(toolName: string, message: string): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
vscode.window
|
||||
.showWarningMessage(`Approve destructive action: ${toolName}?`, { modal: true }, "Approve", "Reject")
|
||||
.then((choice) => resolve(choice === "Approve"))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,3 +37,24 @@ function simpleGlobMatch(path: string, pattern: string): boolean {
|
|||
)
|
||||
return re.test(path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a relative path matches any of the given glob-like patterns.
|
||||
* Used by .intentignore to exclude paths from edits.
|
||||
*/
|
||||
export function pathMatchesAnyPattern(relativePath: string, patterns: string[]): boolean {
|
||||
if (!patterns || patterns.length === 0) return false
|
||||
const normalized = path.normalize(relativePath).replace(/\\/g, "/")
|
||||
for (const pattern of patterns) {
|
||||
const p = path.normalize(pattern).replace(/\\/g, "/")
|
||||
if (p.endsWith("/**")) {
|
||||
const prefix = p.slice(0, -3)
|
||||
if (normalized === prefix || normalized.startsWith(prefix + "/")) return true
|
||||
} else if (p.includes("*")) {
|
||||
if (simpleGlobMatch(normalized, p)) return true
|
||||
} else {
|
||||
if (normalized === p || normalized.endsWith("/" + p)) return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,6 +64,14 @@ export interface AgentTraceEntry {
|
|||
/** Safe = read-only; Destructive = write, delete, execute */
|
||||
export type CommandClass = "safe" | "destructive"
|
||||
|
||||
/**
|
||||
* Classify a tool name as Safe (read) or Destructive (write, delete, execute).
|
||||
* Used by the Hook Engine for authorization and UI-blocking.
|
||||
*/
|
||||
export function classifyCommand(toolName: string): CommandClass {
|
||||
return (DESTRUCTIVE_TOOLS as readonly string[]).includes(toolName) ? "destructive" : "safe"
|
||||
}
|
||||
|
||||
export const DESTRUCTIVE_TOOLS = [
|
||||
"write_to_file",
|
||||
"apply_diff",
|
||||
|
|
|
|||
|
|
@ -80,6 +80,10 @@ export const toolParamNames = [
|
|||
// read_file legacy format parameter (backward compatibility)
|
||||
"files",
|
||||
"line_ranges",
|
||||
// write_to_file traceability (AI-Native Git Layer)
|
||||
"intent_id",
|
||||
"mutation_class",
|
||||
"lesson", // append_lesson_learned
|
||||
] as const
|
||||
|
||||
export type ToolParamName = (typeof toolParamNames)[number]
|
||||
|
|
@ -114,7 +118,13 @@ export type NativeToolArgs = {
|
|||
switch_mode: { mode_slug: string; reason: string }
|
||||
update_todo_list: { todos: string }
|
||||
use_mcp_tool: { server_name: string; tool_name: string; arguments?: Record<string, unknown> }
|
||||
write_to_file: { path: string; content: string }
|
||||
write_to_file: {
|
||||
path: string
|
||||
content: string
|
||||
intent_id?: string
|
||||
mutation_class?: "AST_REFACTOR" | "INTENT_EVOLUTION" | "NEW_FILE"
|
||||
}
|
||||
append_lesson_learned: { lesson: string; file_path?: string }
|
||||
// Add more tools as they are migrated to native protocol
|
||||
}
|
||||
|
||||
|
|
@ -288,6 +298,7 @@ export const TOOL_DISPLAY_NAMES: Record<ToolName, string> = {
|
|||
run_slash_command: "run slash command",
|
||||
skill: "load skill",
|
||||
generate_image: "generate images",
|
||||
append_lesson_learned: "append lessons learned",
|
||||
custom_tool: "use custom tools",
|
||||
} as const
|
||||
|
||||
|
|
|
|||
57
test-guardrails.js
Normal file
57
test-guardrails.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
const { PreHook } = require("../src/hooks/pre-hook")
|
||||
|
||||
async function testGuardrails() {
|
||||
const cwd = process.cwd()
|
||||
let activeIntentId = null
|
||||
|
||||
const preHook = new PreHook({
|
||||
cwd,
|
||||
getActiveIntentId: () => activeIntentId,
|
||||
setActiveIntentId: (id) => {
|
||||
activeIntentId = id
|
||||
},
|
||||
requireIntentForDestructiveOnly: true,
|
||||
})
|
||||
|
||||
console.log("🧪 Testing Guardrails in Demo Workspace\n")
|
||||
|
||||
// Test 1: No intent ID
|
||||
console.log("1. Testing write_to_file WITHOUT intent ID:")
|
||||
const result1 = await preHook.intercept("write_to_file", {
|
||||
path: "src/config/app.ts",
|
||||
content: "// config",
|
||||
})
|
||||
console.log(" Blocked:", result1.blocked)
|
||||
console.log(" Error:", result1.error?.substring(0, 100) + "...")
|
||||
|
||||
// Test 2: Select valid intent
|
||||
console.log("\n2. Testing select_active_intent with INT-001:")
|
||||
const result2 = await preHook.intercept("select_active_intent", {
|
||||
intent_id: "INT-001",
|
||||
})
|
||||
console.log(" Blocked:", result2.blocked)
|
||||
console.log(" InjectResult:", result2.injectResult ? "XML injected" : "None")
|
||||
activeIntentId = "INT-001"
|
||||
|
||||
// Test 3: Scope violation
|
||||
console.log("\n3. Testing write_to_file with scope violation:")
|
||||
const result3 = await preHook.intercept("write_to_file", {
|
||||
path: "src/db/database.ts",
|
||||
content: "// database",
|
||||
})
|
||||
console.log(" Blocked:", result3.blocked)
|
||||
console.log(" Error:", result3.error?.substring(0, 100) + "...")
|
||||
|
||||
// Test 4: Valid scope
|
||||
console.log("\n4. Testing write_to_file in valid scope:")
|
||||
const result4 = await preHook.intercept("write_to_file", {
|
||||
path: "src/api/weather.ts",
|
||||
content: "// weather API",
|
||||
})
|
||||
console.log(" Blocked:", result4.blocked)
|
||||
console.log(" Error:", result4.error || "None")
|
||||
|
||||
console.log("\n✅ Guardrail tests completed!")
|
||||
}
|
||||
|
||||
testGuardrails().catch(console.error)
|
||||
23
test-trace.js
Normal file
23
test-trace.js
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import { appendAgentTrace } from "../src/hooks/post-hook"
|
||||
|
||||
async function testTrace() {
|
||||
const cwd = process.cwd()
|
||||
|
||||
console.log("🧪 Testing Trace Generation in Demo Workspace\n")
|
||||
|
||||
await appendAgentTrace(cwd, {
|
||||
relativePath: "src/api/weather.ts",
|
||||
content: "// weather API\nexport function getWeather() {\n return { temp: 72, condition: 'sunny' };\n}",
|
||||
intentId: "INT-001",
|
||||
mutationClass: "NEW_FILE",
|
||||
reqId: "REQ-123",
|
||||
sessionLogId: "SESSION-456",
|
||||
modelIdentifier: "test-model",
|
||||
vcsRevisionId: "abc123",
|
||||
})
|
||||
|
||||
console.log("✅ Trace entry created!")
|
||||
console.log("📁 Check .orchestration/agent_trace.jsonl")
|
||||
}
|
||||
|
||||
testTrace().catch(console.error)
|
||||
12
todo-demo/.orchestration/active_intents.yaml
Normal file
12
todo-demo/.orchestration/active_intents.yaml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
active_intents:
|
||||
- id: "INT-001"
|
||||
name: "Build Todo App"
|
||||
status: "IN_PROGRESS"
|
||||
owned_scope:
|
||||
- "src/**"
|
||||
constraints:
|
||||
- "Use TypeScript"
|
||||
- "Keep components simple"
|
||||
acceptance_criteria:
|
||||
- "Add todo functionality works"
|
||||
- "Delete todo functionality works"
|
||||
1
todo-demo/.orchestration/agent_trace.jsonl
Normal file
1
todo-demo/.orchestration/agent_trace.jsonl
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"id": "trace-001", "timestamp": "2025-02-21T21:50:00.000Z", "files": [{"relative_path": "src/components/TodoList.tsx", "conversations": [{"url": "session-1", "contributor": {"entity_type": "AI", "model_identifier": "test-model"}, "ranges": [{"start_line": 1, "end_line": 15, "content_hash": "sha256:abc123def456789"}], "related": [{"type": "specification", "value": "INT-001"}]}]}]}
|
||||
32
todo-demo/src/components/TodoList.tsx
Normal file
32
todo-demo/src/components/TodoList.tsx
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import React from "react"
|
||||
|
||||
interface Todo {
|
||||
id: string
|
||||
text: string
|
||||
completed: boolean
|
||||
}
|
||||
|
||||
interface TodoListProps {
|
||||
todos: Todo[]
|
||||
}
|
||||
|
||||
const TodoList: React.FC<TodoListProps> = ({ todos }) => {
|
||||
return (
|
||||
<div>
|
||||
<h1>Todo List</h1>
|
||||
{todos.length === 0 ? (
|
||||
<p>No todos yet!</p>
|
||||
) : (
|
||||
<ul>
|
||||
{todos.map((todo) => (
|
||||
<li key={todo.id} style={{ textDecoration: todo.completed ? "line-through" : "none" }}>
|
||||
{todo.text}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default TodoList
|
||||
11
weather-api-demo/.orchestration/active_intents.yaml
Normal file
11
weather-api-demo/.orchestration/active_intents.yaml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
active_intents:
|
||||
- id: "INT-001"
|
||||
name: "Build Weather API"
|
||||
status: "IN_PROGRESS"
|
||||
owned_scope:
|
||||
- "src/api/**"
|
||||
constraints:
|
||||
- "Use REST conventions"
|
||||
- "Return JSON"
|
||||
acceptance_criteria:
|
||||
- "GET /weather returns 200"
|
||||
Loading…
Add table
Reference in a new issue