observability-toolkit-hooks 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONFIG.md +144 -0
- package/README.md +201 -2
- package/dist/handlers/compact.js +38 -0
- package/dist/handlers/mcp-status.js +66 -0
- package/dist/handlers/notification.js +45 -0
- package/dist/handlers/post-tool-agent.js +157 -0
- package/dist/handlers/post-tool-changelog-sync.js +148 -0
- package/dist/handlers/post-tool-code-structure.js +116 -0
- package/dist/handlers/post-tool-edit-ledger.js +15 -0
- package/dist/handlers/post-tool-failure.js +83 -0
- package/dist/handlers/post-tool-git.js +79 -0
- package/dist/handlers/post-tool-mcp.js +105 -0
- package/dist/handlers/post-tool-outcome.js +48 -0
- package/dist/handlers/post-tool-task-state.js +74 -0
- package/dist/handlers/post-tool.js +132 -0
- package/dist/handlers/pre-tool-agent.js +192 -0
- package/dist/handlers/pre-tool.js +218 -0
- package/dist/handlers/session-end.js +39 -0
- package/dist/handlers/session-start.js +262 -0
- package/dist/handlers/stop-error-reminder.js +195 -0
- package/dist/handlers/stop-quality-evaluation.js +58 -0
- package/dist/handlers/stop-session-summary.js +62 -0
- package/dist/handlers/stop-token-metrics.js +83 -0
- package/dist/handlers/stop.js +25 -0
- package/dist/handlers/subagent.js +105 -0
- package/dist/handlers/tool-event-router.js +39 -0
- package/dist/handlers/user-prompt.js +218 -0
- package/dist/hook-runner.js +10 -0
- package/dist/hook-runner.js.map +7 -0
- package/dist/lib/agent/agent-context.js +123 -0
- package/dist/lib/agent/agent-evaluations.js +155 -0
- package/dist/lib/agent/agent-file-budget.js +65 -0
- package/dist/lib/agent/agent-sources.js +165 -0
- package/dist/lib/agent/synth-spans.js +51 -0
- package/dist/lib/core/bounded-label.js +6 -0
- package/dist/lib/core/category-rules.js +11 -0
- package/dist/lib/core/child-result.js +19 -0
- package/dist/lib/core/config-dir-recovery.js +22 -0
- package/dist/lib/core/constants.js +171 -0
- package/dist/lib/core/detached-runner.js +55 -0
- package/dist/lib/core/file-utils.js +353 -0
- package/dist/lib/core/format-utils.js +51 -0
- package/dist/lib/core/http-utils.js +74 -0
- package/dist/lib/core/http1-fetch.js +14 -0
- package/dist/lib/core/identity-key-resolver.js +149 -0
- package/dist/lib/core/jsonl-tail.js +207 -0
- package/dist/lib/core/load-envrc.js +72 -0
- package/dist/lib/core/parsers.js +43 -0
- package/dist/lib/core/regex.js +27 -0
- package/dist/lib/core/settings-env.js +31 -0
- package/dist/lib/core/shell-command.js +122 -0
- package/dist/lib/core/units.js +97 -0
- package/dist/lib/git/git-cache.js +158 -0
- package/dist/lib/git/git-commit.js +189 -0
- package/dist/lib/git/git-exec.js +30 -0
- package/dist/lib/mcp/mcp-completeness.js +40 -0
- package/dist/lib/mcp/mcp-config.js +118 -0
- package/dist/lib/mcp/mcp-failure-latch.js +22 -0
- package/dist/lib/mcp/mcp-identity-launcher.js +43 -0
- package/dist/lib/otel/attributes.js +322 -0
- package/dist/lib/otel/evaluation-attrs.js +51 -0
- package/dist/lib/otel/file-exporters.js +169 -0
- package/dist/lib/otel/host-resource.js +61 -0
- package/dist/lib/otel/otel-headers-helper.js +26 -0
- package/dist/lib/otel/otel-monitor.js +181 -0
- package/dist/lib/otel/otel.js +235 -0
- package/dist/lib/otel/otlp-headers.js +101 -0
- package/dist/lib/otel/otlp-post.js +122 -0
- package/dist/lib/otel/otlp-serialize.js +142 -0
- package/dist/lib/otel/otlp-signal.js +2 -0
- package/dist/lib/otel/remote-spans.js +142 -0
- package/dist/lib/otel/service-identity.js +10 -0
- package/dist/lib/otel/telemetry-alerts.js +43 -0
- package/dist/lib/otel/token-metrics.js +122 -0
- package/dist/lib/otel/trace-context.js +81 -0
- package/dist/lib/quality/judge-meta-eval.js +70 -0
- package/dist/lib/quality/judge-prompts.js +104 -0
- package/dist/lib/quality/judge-providers.js +193 -0
- package/dist/lib/quality/judge-transport.js +93 -0
- package/dist/lib/quality/llm-judge.js +170 -0
- package/dist/lib/quality/quality-budget.js +99 -0
- package/dist/lib/quality/quality-degradation.js +156 -0
- package/dist/lib/quality/quality-eval-handoff.js +74 -0
- package/dist/lib/quality/quality-evaluator.js +178 -0
- package/dist/lib/quality/quality-sampler.js +43 -0
- package/dist/lib/quality/quality-signals.js +150 -0
- package/dist/lib/runtime/categorizers.js +101 -0
- package/dist/lib/runtime/hook-input.js +16 -0
- package/dist/lib/runtime/hook-output.js +50 -0
- package/dist/lib/runtime/output-analyzer.js +160 -0
- package/dist/lib/runtime/run-handlers.js +32 -0
- package/dist/lib/runtime/tool-input.js +111 -0
- package/dist/lib/runtime/tool-output.js +44 -0
- package/dist/lib/runtime/unknown-tool.js +32 -0
- package/dist/lib/session/edit-ledger.js +29 -0
- package/dist/lib/session/forked-skill.js +39 -0
- package/dist/lib/session/handoffs.js +77 -0
- package/dist/lib/session/session-state.js +169 -0
- package/dist/lib/session/skill-rules.js +196 -0
- package/dist/lib/session/task-tracker.js +83 -0
- package/dist/lib/shipper/shipper-credentials.js +38 -0
- package/dist/lib/shipper/shipper-cursor.js +89 -0
- package/dist/lib/shipper/shipper-status.js +174 -0
- package/dist/lib/shipper/span-shipper.js +272 -0
- package/dist/lib/shipper/telemetry-shipper.js +39 -0
- package/dist/lib/transcript/agent-transcript-metrics.js +34 -0
- package/dist/lib/transcript/context-breakdown.js +134 -0
- package/dist/lib/transcript/context-history.js +69 -0
- package/dist/lib/transcript/context-tracker.js +226 -0
- package/dist/lib/transcript/message-count.js +49 -0
- package/dist/lib/transcript/model-ids.js +36 -0
- package/dist/lib/transcript/model-pricing.js +153 -0
- package/dist/lib/transcript/token-metrics-checkpoint.js +152 -0
- package/dist/lib/transcript/tool-call-durations.js +64 -0
- package/dist/lib/transcript/transcript-entry.js +67 -0
- package/dist/lib/transcript/transcript-fold.js +130 -0
- package/dist/lib/transcript/transcript-layout.js +39 -0
- package/dist/lib/transcript/transcript-parser.js +117 -0
- package/dist/runner/agent-context-4XTCLMLZ.js +23 -0
- package/dist/runner/agent-context-4XTCLMLZ.js.map +7 -0
- package/dist/runner/agent-file-budget-Q33ESBBE.js +68 -0
- package/dist/runner/agent-file-budget-Q33ESBBE.js.map +7 -0
- package/dist/runner/agent-transcript-metrics-W5CZZFMI.js +39 -0
- package/dist/runner/agent-transcript-metrics-W5CZZFMI.js.map +7 -0
- package/dist/runner/chunk-2NTNAJZE.js +49 -0
- package/dist/runner/chunk-2NTNAJZE.js.map +7 -0
- package/dist/runner/chunk-4KWBQTTG.js +78 -0
- package/dist/runner/chunk-4KWBQTTG.js.map +7 -0
- package/dist/runner/chunk-52RY6LQT.js +699 -0
- package/dist/runner/chunk-52RY6LQT.js.map +7 -0
- package/dist/runner/chunk-7OM5CYES.js +8695 -0
- package/dist/runner/chunk-7OM5CYES.js.map +7 -0
- package/dist/runner/chunk-7PEOKGYR.js +1568 -0
- package/dist/runner/chunk-7PEOKGYR.js.map +7 -0
- package/dist/runner/chunk-AGSGKELQ.js +126 -0
- package/dist/runner/chunk-AGSGKELQ.js.map +7 -0
- package/dist/runner/chunk-AVM7PJFW.js +12 -0
- package/dist/runner/chunk-AVM7PJFW.js.map +7 -0
- package/dist/runner/chunk-C3WHUMU5.js +178 -0
- package/dist/runner/chunk-C3WHUMU5.js.map +7 -0
- package/dist/runner/chunk-DGB4NSSD.js +157 -0
- package/dist/runner/chunk-DGB4NSSD.js.map +7 -0
- package/dist/runner/chunk-DNSTWC26.js +151 -0
- package/dist/runner/chunk-DNSTWC26.js.map +7 -0
- package/dist/runner/chunk-FDJJ3TYU.js +449 -0
- package/dist/runner/chunk-FDJJ3TYU.js.map +7 -0
- package/dist/runner/chunk-FGKYDQHZ.js +137 -0
- package/dist/runner/chunk-FGKYDQHZ.js.map +7 -0
- package/dist/runner/chunk-GG2I4TGI.js +582 -0
- package/dist/runner/chunk-GG2I4TGI.js.map +7 -0
- package/dist/runner/chunk-GNNKUMRF.js +60 -0
- package/dist/runner/chunk-GNNKUMRF.js.map +7 -0
- package/dist/runner/chunk-I7TD2QVR.js +118 -0
- package/dist/runner/chunk-I7TD2QVR.js.map +7 -0
- package/dist/runner/chunk-IJ4SLUBV.js +78 -0
- package/dist/runner/chunk-IJ4SLUBV.js.map +7 -0
- package/dist/runner/chunk-IY7AP7NA.js +32 -0
- package/dist/runner/chunk-IY7AP7NA.js.map +7 -0
- package/dist/runner/chunk-J3F3Z3EQ.js +177 -0
- package/dist/runner/chunk-J3F3Z3EQ.js.map +7 -0
- package/dist/runner/chunk-JLEO5OHU.js +104 -0
- package/dist/runner/chunk-JLEO5OHU.js.map +7 -0
- package/dist/runner/chunk-JRNNLOPR.js +168 -0
- package/dist/runner/chunk-JRNNLOPR.js.map +7 -0
- package/dist/runner/chunk-KGSA5JYP.js +82 -0
- package/dist/runner/chunk-KGSA5JYP.js.map +7 -0
- package/dist/runner/chunk-LBCZAYFR.js +125 -0
- package/dist/runner/chunk-LBCZAYFR.js.map +7 -0
- package/dist/runner/chunk-LHWRXY2G.js +41 -0
- package/dist/runner/chunk-LHWRXY2G.js.map +7 -0
- package/dist/runner/chunk-LQ2LGUYJ.js +41 -0
- package/dist/runner/chunk-LQ2LGUYJ.js.map +7 -0
- package/dist/runner/chunk-LYY6RCIE.js +158 -0
- package/dist/runner/chunk-LYY6RCIE.js.map +7 -0
- package/dist/runner/chunk-M4R4QIQH.js +41 -0
- package/dist/runner/chunk-M4R4QIQH.js.map +7 -0
- package/dist/runner/chunk-MC75VXMX.js +207 -0
- package/dist/runner/chunk-MC75VXMX.js.map +7 -0
- package/dist/runner/chunk-NKZU4RRG.js +104 -0
- package/dist/runner/chunk-NKZU4RRG.js.map +7 -0
- package/dist/runner/chunk-PJHWHJ3K.js +56 -0
- package/dist/runner/chunk-PJHWHJ3K.js.map +7 -0
- package/dist/runner/chunk-RIGVYJ2O.js +114 -0
- package/dist/runner/chunk-RIGVYJ2O.js.map +7 -0
- package/dist/runner/chunk-RWRGC7ZK.js +93 -0
- package/dist/runner/chunk-RWRGC7ZK.js.map +7 -0
- package/dist/runner/chunk-SRMAJQMV.js +54 -0
- package/dist/runner/chunk-SRMAJQMV.js.map +7 -0
- package/dist/runner/chunk-TRMFD36J.js +47 -0
- package/dist/runner/chunk-TRMFD36J.js.map +7 -0
- package/dist/runner/chunk-U4IOZYQ3.js +21 -0
- package/dist/runner/chunk-U4IOZYQ3.js.map +7 -0
- package/dist/runner/chunk-UEXDE3WP.js +83 -0
- package/dist/runner/chunk-UEXDE3WP.js.map +7 -0
- package/dist/runner/chunk-UW3GQZSM.js +50 -0
- package/dist/runner/chunk-UW3GQZSM.js.map +7 -0
- package/dist/runner/chunk-V2GSP6VR.js +46 -0
- package/dist/runner/chunk-V2GSP6VR.js.map +7 -0
- package/dist/runner/chunk-VOV4Y5VU.js +81 -0
- package/dist/runner/chunk-VOV4Y5VU.js.map +7 -0
- package/dist/runner/chunk-VT5T6OEI.js +65 -0
- package/dist/runner/chunk-VT5T6OEI.js.map +7 -0
- package/dist/runner/chunk-VUYVFWIN.js +12 -0
- package/dist/runner/chunk-VUYVFWIN.js.map +7 -0
- package/dist/runner/chunk-VVGOPPEN.js +36 -0
- package/dist/runner/chunk-VVGOPPEN.js.map +7 -0
- package/dist/runner/chunk-VXFSLM6P.js +14 -0
- package/dist/runner/chunk-VXFSLM6P.js.map +7 -0
- package/dist/runner/chunk-W3EK7JUT.js +26125 -0
- package/dist/runner/chunk-W3EK7JUT.js.map +7 -0
- package/dist/runner/chunk-WJHNCOUF.js +52 -0
- package/dist/runner/chunk-WJHNCOUF.js.map +7 -0
- package/dist/runner/chunk-WU7ANGCW.js +269 -0
- package/dist/runner/chunk-WU7ANGCW.js.map +7 -0
- package/dist/runner/chunk-X7XLMSZX.js +44 -0
- package/dist/runner/chunk-X7XLMSZX.js.map +7 -0
- package/dist/runner/chunk-XSO3B4QN.js +124 -0
- package/dist/runner/chunk-XSO3B4QN.js.map +7 -0
- package/dist/runner/chunk-YGFN3LN2.js +174 -0
- package/dist/runner/chunk-YGFN3LN2.js.map +7 -0
- package/dist/runner/chunk-YHYDBLVB.js +33 -0
- package/dist/runner/chunk-YHYDBLVB.js.map +7 -0
- package/dist/runner/compact-36NP3PKT.js +60 -0
- package/dist/runner/compact-36NP3PKT.js.map +7 -0
- package/dist/runner/getMachineId-bsd-AGENNHN6.js +42 -0
- package/dist/runner/getMachineId-bsd-AGENNHN6.js.map +7 -0
- package/dist/runner/getMachineId-bsd-I5WZGCL4.js +33 -0
- package/dist/runner/getMachineId-bsd-I5WZGCL4.js.map +7 -0
- package/dist/runner/getMachineId-bsd-ZXK225P7.js +33 -0
- package/dist/runner/getMachineId-bsd-ZXK225P7.js.map +7 -0
- package/dist/runner/getMachineId-darwin-47GDLEXL.js +42 -0
- package/dist/runner/getMachineId-darwin-47GDLEXL.js.map +7 -0
- package/dist/runner/getMachineId-darwin-5CRLMDD6.js +33 -0
- package/dist/runner/getMachineId-darwin-5CRLMDD6.js.map +7 -0
- package/dist/runner/getMachineId-darwin-ADG4I32N.js +33 -0
- package/dist/runner/getMachineId-darwin-ADG4I32N.js.map +7 -0
- package/dist/runner/getMachineId-linux-5KVS3T2M.js +27 -0
- package/dist/runner/getMachineId-linux-5KVS3T2M.js.map +7 -0
- package/dist/runner/getMachineId-linux-GDFB5GSO.js +34 -0
- package/dist/runner/getMachineId-linux-GDFB5GSO.js.map +7 -0
- package/dist/runner/getMachineId-linux-NQMWCRO3.js +27 -0
- package/dist/runner/getMachineId-linux-NQMWCRO3.js.map +7 -0
- package/dist/runner/getMachineId-unsupported-5PFIGMVX.js +18 -0
- package/dist/runner/getMachineId-unsupported-5PFIGMVX.js.map +7 -0
- package/dist/runner/getMachineId-unsupported-DW27K3ZX.js +18 -0
- package/dist/runner/getMachineId-unsupported-DW27K3ZX.js.map +7 -0
- package/dist/runner/getMachineId-unsupported-XV3FHTHD.js +25 -0
- package/dist/runner/getMachineId-unsupported-XV3FHTHD.js.map +7 -0
- package/dist/runner/getMachineId-win-5JVMW3EI.js +44 -0
- package/dist/runner/getMachineId-win-5JVMW3EI.js.map +7 -0
- package/dist/runner/getMachineId-win-SNESQF7R.js +35 -0
- package/dist/runner/getMachineId-win-SNESQF7R.js.map +7 -0
- package/dist/runner/getMachineId-win-UJYHSBM2.js +35 -0
- package/dist/runner/getMachineId-win-UJYHSBM2.js.map +7 -0
- package/dist/runner/git-commit-BA53DOJQ.js +30 -0
- package/dist/runner/git-commit-BA53DOJQ.js.map +7 -0
- package/dist/runner/hook-runner-JHH6PGKE.js +220 -0
- package/dist/runner/hook-runner-JHH6PGKE.js.map +7 -0
- package/dist/runner/judge-transport-R4TF5LEH.js +91 -0
- package/dist/runner/judge-transport-R4TF5LEH.js.map +7 -0
- package/dist/runner/mcp-status-QEUH2KGX.js +89 -0
- package/dist/runner/mcp-status-QEUH2KGX.js.map +7 -0
- package/dist/runner/notification-FKMBCSXF.js +69 -0
- package/dist/runner/notification-FKMBCSXF.js.map +7 -0
- package/dist/runner/post-tool-3MRHTBKH.js +192 -0
- package/dist/runner/post-tool-3MRHTBKH.js.map +7 -0
- package/dist/runner/post-tool-agent-BB34MNHI.js +370 -0
- package/dist/runner/post-tool-agent-BB34MNHI.js.map +7 -0
- package/dist/runner/post-tool-changelog-sync-OP2TT2I7.js +147 -0
- package/dist/runner/post-tool-changelog-sync-OP2TT2I7.js.map +7 -0
- package/dist/runner/post-tool-code-structure-LE7J2SC2.js +130 -0
- package/dist/runner/post-tool-code-structure-LE7J2SC2.js.map +7 -0
- package/dist/runner/post-tool-edit-ledger-IJMEYHKF.js +30 -0
- package/dist/runner/post-tool-edit-ledger-IJMEYHKF.js.map +7 -0
- package/dist/runner/post-tool-failure-XTH2I2Z7.js +111 -0
- package/dist/runner/post-tool-failure-XTH2I2Z7.js.map +7 -0
- package/dist/runner/post-tool-git-E46HZGZO.js +94 -0
- package/dist/runner/post-tool-git-E46HZGZO.js.map +7 -0
- package/dist/runner/post-tool-mcp-AMCDY5GH.js +144 -0
- package/dist/runner/post-tool-mcp-AMCDY5GH.js.map +7 -0
- package/dist/runner/post-tool-task-state-TLDOI33H.js +100 -0
- package/dist/runner/post-tool-task-state-TLDOI33H.js.map +7 -0
- package/dist/runner/pre-tool-SQE2FZQO.js +245 -0
- package/dist/runner/pre-tool-SQE2FZQO.js.map +7 -0
- package/dist/runner/pre-tool-agent-OZUPVFUI.js +218 -0
- package/dist/runner/pre-tool-agent-OZUPVFUI.js.map +7 -0
- package/dist/runner/quality-evaluator-XYNDKDRG.js +901 -0
- package/dist/runner/quality-evaluator-XYNDKDRG.js.map +7 -0
- package/dist/runner/session-end-3MJTDLLA.js +57 -0
- package/dist/runner/session-end-3MJTDLLA.js.map +7 -0
- package/dist/runner/session-start-5ZMCLKBS.js +352 -0
- package/dist/runner/session-start-5ZMCLKBS.js.map +7 -0
- package/dist/runner/shipper-status-MADX6SPG.js +37 -0
- package/dist/runner/shipper-status-MADX6SPG.js.map +7 -0
- package/dist/runner/span-shipper-4HQDHC47.js +3408 -0
- package/dist/runner/span-shipper-4HQDHC47.js.map +7 -0
- package/dist/runner/stop-RTKSIYJV.js +596 -0
- package/dist/runner/stop-RTKSIYJV.js.map +7 -0
- package/dist/runner/subagent-CBCG4OLZ.js +116 -0
- package/dist/runner/subagent-CBCG4OLZ.js.map +7 -0
- package/dist/runner/token-metrics-AD2RAFGM.js +31 -0
- package/dist/runner/token-metrics-AD2RAFGM.js.map +7 -0
- package/dist/runner/user-prompt-T5NZ2UJD.js +376 -0
- package/dist/runner/user-prompt-T5NZ2UJD.js.map +7 -0
- package/package.json +68 -5
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Categorizers as ordered first-match-wins rule tables. `categorizeBuiltinTool`
|
|
3
|
+
* is an exact-name lookup whose table is `BUILTIN_TOOLS_BY_CATEGORY` in
|
|
4
|
+
* constants.ts, so membership and category cannot drift (CCH7).
|
|
5
|
+
*/
|
|
6
|
+
import { OTHER_LABEL } from '../core/bounded-label.js';
|
|
7
|
+
import { matchCategory } from '../core/category-rules.js';
|
|
8
|
+
import { BUILTIN_TOOLS_BY_CATEGORY, UNKNOWN_TOOL_CATEGORY } from '../core/constants.js';
|
|
9
|
+
import { JS_EXTENSIONS, TS_EXTENSIONS, alternation, fileExtensionPattern } from '../core/regex.js';
|
|
10
|
+
import { AGENT_ATTRS, GENAI_TOOL_NAME_ATTR, TOOL_ATTRS } from '../otel/attributes.js';
|
|
11
|
+
const SKILL_CATEGORY_RULES = [
|
|
12
|
+
// otel-* before report/review.
|
|
13
|
+
{ category: 'observability', substrings: ['otel', 'telemetry', 'observ'] },
|
|
14
|
+
{ category: 'reporting', substrings: ['report', 'summary', 'dashboard'] },
|
|
15
|
+
{ category: 'content', substrings: ['content', 'blog'] },
|
|
16
|
+
{ category: 'development', substrings: ['dev', 'code', 'refactor', 'backlog', 'implement'] },
|
|
17
|
+
{ category: 'testing', substrings: ['test', 'debug', 'bugfix'] },
|
|
18
|
+
{ category: 'documentation', substrings: ['doc', 'readme'] },
|
|
19
|
+
{ category: 'version-control', substrings: ['git', 'commit'], pattern: /^pr-|-pr-|-pr$/ },
|
|
20
|
+
{ category: 'devops', substrings: ['build', 'deploy', 'cicd'] },
|
|
21
|
+
{ category: 'security', substrings: ['auth', 'security'] },
|
|
22
|
+
{ category: 'api', substrings: ['api', 'route'] },
|
|
23
|
+
{ category: 'frontend', substrings: ['ui', 'frontend', 'design'] },
|
|
24
|
+
{ category: 'database', substrings: ['db', 'database', 'redis'] },
|
|
25
|
+
{ category: 'error-handling', substrings: ['error', 'sentry', 'bug'] },
|
|
26
|
+
// After domain-specific rows (pr-reviewer→version-control, security-audit→security).
|
|
27
|
+
{ category: 'review', substrings: ['review', 'audit', 'improv'] },
|
|
28
|
+
];
|
|
29
|
+
export function categorizeSkill(skillName) {
|
|
30
|
+
return matchCategory(skillName.toLowerCase(), SKILL_CATEGORY_RULES, 'general');
|
|
31
|
+
}
|
|
32
|
+
const AGENT_CATEGORY_RULES = [
|
|
33
|
+
// agent-auditor is review, not security: it must precede the `audit` row.
|
|
34
|
+
{ category: 'review', substrings: ['review', 'agent-auditor'] },
|
|
35
|
+
{ category: 'scraping', substrings: ['webscraping', 'scraper', 'scraping'] },
|
|
36
|
+
{ category: 'research', substrings: ['web-research'] },
|
|
37
|
+
{ category: 'error-handling', substrings: ['error', 'resolver'] },
|
|
38
|
+
{ category: 'observability', substrings: ['quality-monitor', 'telemetry', 'observability', 'splunk'] },
|
|
39
|
+
{ category: 'code', substrings: ['code', 'refactor'] },
|
|
40
|
+
{ category: 'testing', substrings: ['test', 'debug', 'bugfix'] },
|
|
41
|
+
{ category: 'documentation', substrings: ['doc', 'architecture'] },
|
|
42
|
+
{ category: 'security', substrings: ['security', 'auth', 'audit'] },
|
|
43
|
+
{ category: 'shell', substrings: ['bash', 'shell'] },
|
|
44
|
+
{ category: 'cache', substrings: ['redis', 'cache'] },
|
|
45
|
+
{ category: 'web', substrings: ['html', 'seo'] },
|
|
46
|
+
{ category: 'exploration', substrings: ['explore', 'research'] },
|
|
47
|
+
{ category: 'planning', substrings: ['plan'] },
|
|
48
|
+
{ category: 'design', substrings: ['ui', 'ux', 'design'] },
|
|
49
|
+
{ category: 'routing', substrings: ['route'] },
|
|
50
|
+
{ category: 'quality', substrings: ['typo', 'spelling'] },
|
|
51
|
+
{ category: 'guidance', substrings: ['guide', 'expert'] },
|
|
52
|
+
];
|
|
53
|
+
export function categorizeAgent(agentType) {
|
|
54
|
+
return matchCategory(agentType.toLowerCase(), AGENT_CATEGORY_RULES, 'general');
|
|
55
|
+
}
|
|
56
|
+
/** Low-cardinality (registry name or `skill:<name>`), so safe as metric labels. */
|
|
57
|
+
export function agentTypeLabels(agentType) {
|
|
58
|
+
return { [AGENT_ATTRS.TYPE]: agentType, [AGENT_ATTRS.CATEGORY]: categorizeAgent(agentType) };
|
|
59
|
+
}
|
|
60
|
+
const BUILTIN_TOOL_CATEGORY = new Map(Object.entries(BUILTIN_TOOLS_BY_CATEGORY).flatMap(([category, tools]) => tools.map(tool => [tool, category])));
|
|
61
|
+
export function categorizeBuiltinTool(toolName) {
|
|
62
|
+
return BUILTIN_TOOL_CATEGORY.get(toolName) ?? UNKNOWN_TOOL_CATEGORY;
|
|
63
|
+
}
|
|
64
|
+
/** Name and category labels shared by the builtin pre- and post-tool series. */
|
|
65
|
+
export function builtinToolLabels(toolName) {
|
|
66
|
+
return { [GENAI_TOOL_NAME_ATTR]: toolName, [TOOL_ATTRS.CATEGORY]: categorizeBuiltinTool(toolName) };
|
|
67
|
+
}
|
|
68
|
+
/** Notification `level` values that escalate on their own. */
|
|
69
|
+
const ESCALATING_LEVEL = { ERROR: 'error', WARNING: 'warning' };
|
|
70
|
+
/** How the harness titles a background task that ended in failure: `<task title> failed`. */
|
|
71
|
+
const FAILED_TASK_SUFFIX = ' failed';
|
|
72
|
+
/**
|
|
73
|
+
* Severity comes from `level`, plus the harness's failed-task title. The rest of a
|
|
74
|
+
* message quotes session text ("X needs your input: …"), so a keyword scan over it
|
|
75
|
+
* turned prompts that mention an error into error notifications.
|
|
76
|
+
*/
|
|
77
|
+
export function classifyNotification(level, message) {
|
|
78
|
+
if (level === ESCALATING_LEVEL.ERROR || message.trimEnd().toLowerCase().endsWith(FAILED_TASK_SUFFIX))
|
|
79
|
+
return 'error';
|
|
80
|
+
if (level === ESCALATING_LEVEL.WARNING)
|
|
81
|
+
return 'warning';
|
|
82
|
+
return 'info';
|
|
83
|
+
}
|
|
84
|
+
const FILE_CATEGORY_RULES = [
|
|
85
|
+
{ category: 'hooks-handler', substrings: ['/hooks/handlers/'] },
|
|
86
|
+
{ category: 'hooks-lib', substrings: ['/hooks/lib/'] },
|
|
87
|
+
{ category: 'scripts', substrings: ['/scripts/'] },
|
|
88
|
+
];
|
|
89
|
+
export function getFileCategory(filePath) {
|
|
90
|
+
return matchCategory(filePath, FILE_CATEGORY_RULES, OTHER_LABEL);
|
|
91
|
+
}
|
|
92
|
+
const JS_TS_EXTENSIONS = [...TS_EXTENSIONS, ...JS_EXTENSIONS];
|
|
93
|
+
const JS_TS_FILE = fileExtensionPattern(JS_TS_EXTENSIONS);
|
|
94
|
+
/** Tests, config and declaration files: JS/TS by extension, not hand-written source */
|
|
95
|
+
const JS_TS_NON_SOURCE_FILE = new RegExp(`\\.(test|spec|config|d)\\.${alternation(JS_TS_EXTENSIONS)}$`);
|
|
96
|
+
export function isJsTsFile(filePath) {
|
|
97
|
+
return JS_TS_FILE.test(filePath);
|
|
98
|
+
}
|
|
99
|
+
export function isJsTsNonSourceFile(filePath) {
|
|
100
|
+
return JS_TS_NON_SOURCE_FILE.test(filePath);
|
|
101
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook payload types. `HookInput` is the runner's superset; handlers take a
|
|
3
|
+
* per-event `Pick` so they cannot drift from it (HRV13). Every field is optional:
|
|
4
|
+
* handlers degrade, never throw. Shapes verified against Claude Code 2.1.220 (CCH4).
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* SessionStart's lifecycle discriminator; the only resume signal, there is no boolean.
|
|
8
|
+
* `fork` is `--resume --fork-session`: the transcript continues under a new session id.
|
|
9
|
+
*/
|
|
10
|
+
export const SESSION_START_SOURCE = {
|
|
11
|
+
STARTUP: 'startup',
|
|
12
|
+
RESUME: 'resume',
|
|
13
|
+
CLEAR: 'clear',
|
|
14
|
+
COMPACT: 'compact',
|
|
15
|
+
FORK: 'fork',
|
|
16
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-facing stdout. Plain stdout reaches the model only on UserPromptSubmit,
|
|
3
|
+
* UserPromptExpansion and SessionStart; every other event needs the JSON
|
|
4
|
+
* `hookSpecificOutput.additionalContext` payload. Handlers queue sections and the
|
|
5
|
+
* runner flushes once per process, because one process can host several handlers
|
|
6
|
+
* and two JSON objects on stdout do not parse. An empty queue emits nothing, so a
|
|
7
|
+
* plain-stdout event never gains a stray JSON line.
|
|
8
|
+
*/
|
|
9
|
+
import { HOOK_EXIT } from '../core/constants.js';
|
|
10
|
+
import { writeStderr } from '../core/format-utils.js';
|
|
11
|
+
import { getHookTrigger } from '../otel/otel-monitor.js';
|
|
12
|
+
const sections = [];
|
|
13
|
+
const stdoutWriter = (chunk) => { process.stdout.write(chunk); };
|
|
14
|
+
let writer = stdoutWriter;
|
|
15
|
+
/** Test seam. */
|
|
16
|
+
export function setHookOutputWriter(next) {
|
|
17
|
+
writer = next ?? stdoutWriter;
|
|
18
|
+
}
|
|
19
|
+
/** Reaches the model only on SessionStart, UserPromptSubmit and UserPromptExpansion; elsewhere use `addAdditionalContext`. */
|
|
20
|
+
export function writePlainContext(text) {
|
|
21
|
+
writer(`${text}\n`);
|
|
22
|
+
}
|
|
23
|
+
export function addAdditionalContext(text) {
|
|
24
|
+
const trimmed = text.trim();
|
|
25
|
+
if (trimmed)
|
|
26
|
+
sections.push(trimmed);
|
|
27
|
+
}
|
|
28
|
+
/** Draining makes a second call a no-op: `denyToolUse` flushes before exit 2 and the runner flushes at the end. */
|
|
29
|
+
export function flushAdditionalContext() {
|
|
30
|
+
if (sections.length === 0)
|
|
31
|
+
return;
|
|
32
|
+
const additionalContext = peekAdditionalContext();
|
|
33
|
+
sections.length = 0;
|
|
34
|
+
writer(`${JSON.stringify({
|
|
35
|
+
hookSpecificOutput: { hookEventName: getHookTrigger(), additionalContext },
|
|
36
|
+
})}\n`);
|
|
37
|
+
}
|
|
38
|
+
/** Test seam. */
|
|
39
|
+
export function resetAdditionalContext() {
|
|
40
|
+
sections.length = 0;
|
|
41
|
+
}
|
|
42
|
+
export function peekAdditionalContext() {
|
|
43
|
+
return sections.join('\n\n');
|
|
44
|
+
}
|
|
45
|
+
/** Only exit 2 blocks, with stderr as the reason; exit 1 fails open silently (CCH5). */
|
|
46
|
+
export function denyToolUse(reason) {
|
|
47
|
+
flushAdditionalContext();
|
|
48
|
+
writeStderr(reason);
|
|
49
|
+
process.exit(HOOK_EXIT.BLOCKING);
|
|
50
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/** Tool-output failure detection. Patterns are module-scope (H2). */
|
|
2
|
+
import { BUILTIN_TOOLS_BY_CATEGORY, EDIT_TOOLS, THRESHOLDS, TASK_TOOLS } from '../core/constants.js';
|
|
3
|
+
import { countNonEmptyLines } from '../core/format-utils.js';
|
|
4
|
+
import { UNKNOWN_ERROR_TYPE } from '../otel/attributes.js';
|
|
5
|
+
/** Word boundary: not setTimeout/clearTimeout in code content. */
|
|
6
|
+
const TIMEOUT_PATTERN = /\btimeout\b|\btimed?\s*out\b/i;
|
|
7
|
+
const ERROR_PATTERN = /\berror\b/i;
|
|
8
|
+
const ZERO_ERRORS_PATTERN = /\b0 errors?\b/i;
|
|
9
|
+
const FAILED_PATTERN = /\bfailed\b/i;
|
|
10
|
+
const ZERO_FAILED_PATTERN = /\b0 failed\b/i;
|
|
11
|
+
const EXCEPTION_PATTERN = /\bexception\b/i;
|
|
12
|
+
const WRITE_SUCCESS_PATTERN = /has been (?:updated|written) successfully/;
|
|
13
|
+
const WRITE_ERROR_PATTERN = /^Error\b/m;
|
|
14
|
+
const PERMISSION_DENIED_MARKER = 'Permission denied';
|
|
15
|
+
const RATE_LIMIT_PATTERN = /\b429\b|rate.?limit|too many requests|throttl/i;
|
|
16
|
+
// Real error signals only; excludes audit-report phrases like "error count" or "no error found".
|
|
17
|
+
const AGENT_ERROR_PATTERN = /(?:\berror:|\b[A-Za-z]+Error:|(?<!no )\berror\s+(?:occurred|detected|found|raised|thrown)\b|\berror\s+TS\d+:)/i;
|
|
18
|
+
const SCAN_HEAD_TAIL_CHUNK_BYTES = 5000;
|
|
19
|
+
/** The one scan-bounding policy (PT10); the analyzers apply it themselves, so no caller can hand them an unbounded payload. */
|
|
20
|
+
export function boundedScanText(output, strategy = 'head') {
|
|
21
|
+
if (output.length <= THRESHOLDS.LARGE_OUTPUT_BYTES)
|
|
22
|
+
return output;
|
|
23
|
+
return strategy === 'head'
|
|
24
|
+
? output.slice(0, THRESHOLDS.LARGE_OUTPUT_BYTES)
|
|
25
|
+
: output.slice(0, SCAN_HEAD_TAIL_CHUNK_BYTES) + output.slice(-SCAN_HEAD_TAIL_CHUNK_BYTES);
|
|
26
|
+
}
|
|
27
|
+
export function mentionsRateLimit(output) {
|
|
28
|
+
return RATE_LIMIT_PATTERN.test(boundedScanText(output, 'head-tail'));
|
|
29
|
+
}
|
|
30
|
+
/** Narrower than `analyzeOutput`: agent output routinely discusses errors without having hit one. */
|
|
31
|
+
export function mentionsAgentError(output) {
|
|
32
|
+
return AGENT_ERROR_PATTERN.test(boundedScanText(output, 'head-tail'));
|
|
33
|
+
}
|
|
34
|
+
/** First match wins. The `auth` gap is bounded: `.*` on a single-line JSON payload backtracked for ~13 ms per `auth` hit. */
|
|
35
|
+
const ERROR_CATEGORIES = [
|
|
36
|
+
{ pattern: /econnrefused|etimedout|enotfound|econnreset|enetunreach|socket hang up|connection refused|connection.?failed|connection.?error/i, category: 'network', errorType: 'connection' },
|
|
37
|
+
{ pattern: RATE_LIMIT_PATTERN, category: 'rate_limit' },
|
|
38
|
+
{ pattern: /\b401\b|\b403\b|unauthorized|invalid.?credentials|auth.{0,40}fail/i, category: 'auth', errorType: 'authentication' },
|
|
39
|
+
{ pattern: TIMEOUT_PATTERN, category: 'timeout' },
|
|
40
|
+
{ pattern: /\b400\b|invalid.?input|validation|bad request/i, category: 'validation' },
|
|
41
|
+
{ pattern: /permission/i, category: 'permission' },
|
|
42
|
+
{ pattern: /not found|\b404\b/i, category: 'not_found' },
|
|
43
|
+
{ pattern: /no active project|invalid.?project|missing.?configuration|not.?configured|config.?error/i, category: 'configuration' },
|
|
44
|
+
];
|
|
45
|
+
function classifyBoundedText(text) {
|
|
46
|
+
for (const { pattern, category, errorType } of ERROR_CATEGORIES) {
|
|
47
|
+
if (pattern.test(text))
|
|
48
|
+
return { errorType: errorType ?? category, errorCategory: category };
|
|
49
|
+
}
|
|
50
|
+
return { errorType: UNKNOWN_ERROR_TYPE, errorCategory: UNKNOWN_ERROR_TYPE };
|
|
51
|
+
}
|
|
52
|
+
/** Classification only; the text is already known to be an error. */
|
|
53
|
+
export function classifyError(output, strategy = 'head') {
|
|
54
|
+
return classifyBoundedText(boundedScanText(output, strategy));
|
|
55
|
+
}
|
|
56
|
+
/** Per line, so a "0 errors" summary cannot suppress a genuine error elsewhere (OA-002). */
|
|
57
|
+
function hasErrorWording(output) {
|
|
58
|
+
return output.split('\n').some(line => ERROR_PATTERN.test(line) && !ZERO_ERRORS_PATTERN.test(line));
|
|
59
|
+
}
|
|
60
|
+
function hasFailureWording(output) {
|
|
61
|
+
return hasErrorWording(output) ||
|
|
62
|
+
(FAILED_PATTERN.test(output) && !ZERO_FAILED_PATTERN.test(output));
|
|
63
|
+
}
|
|
64
|
+
export function analyzeOutput(output) {
|
|
65
|
+
const coerced = output ?? '';
|
|
66
|
+
const text = boundedScanText(coerced);
|
|
67
|
+
const hasError = hasFailureWording(text) || EXCEPTION_PATTERN.test(text);
|
|
68
|
+
const classified = hasError ? classifyBoundedText(text) : undefined;
|
|
69
|
+
return {
|
|
70
|
+
outputSize: coerced.length,
|
|
71
|
+
hasError,
|
|
72
|
+
errorType: classified?.errorType,
|
|
73
|
+
errorCategory: classified?.errorCategory,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const NOT_FOUND = { errorType: 'not_found', errorCategory: 'not_found' };
|
|
77
|
+
const PERMISSION = { errorType: 'permission', errorCategory: 'permission' };
|
|
78
|
+
const TIMEOUT = { errorType: 'timeout', errorCategory: 'timeout' };
|
|
79
|
+
const labelled = (errorType) => ({ errorType });
|
|
80
|
+
/** In order: most specific marker first. */
|
|
81
|
+
function firstMatch(output, markers) {
|
|
82
|
+
for (const [marker, detected] of markers) {
|
|
83
|
+
if (output.includes(marker))
|
|
84
|
+
return detected;
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
function detectWriteError(output) {
|
|
89
|
+
// Success first: edited file content ("throw new Error(...)") is otherwise a false positive.
|
|
90
|
+
if (WRITE_SUCCESS_PATTERN.test(output))
|
|
91
|
+
return undefined;
|
|
92
|
+
if (!WRITE_ERROR_PATTERN.test(output) && !output.includes(PERMISSION_DENIED_MARKER))
|
|
93
|
+
return undefined;
|
|
94
|
+
return firstMatch(output, [
|
|
95
|
+
[PERMISSION_DENIED_MARKER, PERMISSION],
|
|
96
|
+
['not found', NOT_FOUND],
|
|
97
|
+
]) ?? labelled('write_error');
|
|
98
|
+
}
|
|
99
|
+
function detectWriteErrorLine(output) {
|
|
100
|
+
return WRITE_ERROR_PATTERN.test(output) ? labelled('write_error') : undefined;
|
|
101
|
+
}
|
|
102
|
+
function detectBashError(output) {
|
|
103
|
+
const marker = firstMatch(output, [
|
|
104
|
+
['command not found', labelled('command_not_found')],
|
|
105
|
+
[PERMISSION_DENIED_MARKER, PERMISSION],
|
|
106
|
+
['No such file or directory', NOT_FOUND],
|
|
107
|
+
]);
|
|
108
|
+
if (marker)
|
|
109
|
+
return marker;
|
|
110
|
+
return output.includes('error:') && !output.includes('0 error') ? labelled('execution_error') : undefined;
|
|
111
|
+
}
|
|
112
|
+
function detectFetchError(output) {
|
|
113
|
+
if (TIMEOUT_PATTERN.test(output))
|
|
114
|
+
return TIMEOUT;
|
|
115
|
+
return output.includes('Error fetching') || output.includes('Failed to fetch') ? labelled('fetch_error') : undefined;
|
|
116
|
+
}
|
|
117
|
+
/** Generic failure wording reported under a tool-specific label. */
|
|
118
|
+
function failureAs(errorType) {
|
|
119
|
+
return output => (hasFailureWording(output) ? labelled(errorType) : undefined);
|
|
120
|
+
}
|
|
121
|
+
const detectReadError = output => firstMatch(output, [
|
|
122
|
+
['does not exist', NOT_FOUND],
|
|
123
|
+
[PERMISSION_DENIED_MARKER, PERMISSION],
|
|
124
|
+
['Is a directory', labelled('invalid_path')],
|
|
125
|
+
]);
|
|
126
|
+
const detectorEntries = (tools, detector) => Array.from(tools, tool => [tool, detector]);
|
|
127
|
+
/**
|
|
128
|
+
* Keyed from the harness's own tool groups, so a tool added to a group gets its group's detector
|
|
129
|
+
* instead of silently reading as success. Tools absent here always report success.
|
|
130
|
+
*/
|
|
131
|
+
const ERROR_DETECTORS = new Map([
|
|
132
|
+
['Read', detectReadError],
|
|
133
|
+
...detectorEntries(EDIT_TOOLS, detectWriteError),
|
|
134
|
+
...detectorEntries(BUILTIN_TOOLS_BY_CATEGORY.shell, detectBashError),
|
|
135
|
+
...detectorEntries(BUILTIN_TOOLS_BY_CATEGORY.web, detectFetchError),
|
|
136
|
+
...detectorEntries(BUILTIN_TOOLS_BY_CATEGORY.planning, detectWriteErrorLine),
|
|
137
|
+
...detectorEntries(BUILTIN_TOOLS_BY_CATEGORY.notebook, detectWriteErrorLine),
|
|
138
|
+
[TASK_TOOLS.KILL_SHELL, failureAs('shell_error')],
|
|
139
|
+
[TASK_TOOLS.OUTPUT, failureAs('task_error')],
|
|
140
|
+
]);
|
|
141
|
+
const EMPTY_RESULT_MARKERS = {
|
|
142
|
+
Glob: 'No files found',
|
|
143
|
+
Grep: 'No matches found',
|
|
144
|
+
};
|
|
145
|
+
function countResultLines(output, emptyMarker) {
|
|
146
|
+
return output.includes(emptyMarker) ? 0 : countNonEmptyLines(output);
|
|
147
|
+
}
|
|
148
|
+
export function analyzeBuiltinOutput(toolName, output) {
|
|
149
|
+
const coerced = output ?? '';
|
|
150
|
+
const safeOutput = boundedScanText(coerced);
|
|
151
|
+
const detected = ERROR_DETECTORS.get(toolName)?.(safeOutput);
|
|
152
|
+
const emptyMarker = EMPTY_RESULT_MARKERS[toolName];
|
|
153
|
+
return {
|
|
154
|
+
outputSize: coerced.length,
|
|
155
|
+
hasError: detected !== undefined,
|
|
156
|
+
errorType: detected?.errorType,
|
|
157
|
+
errorCategory: detected && (detected.errorCategory ?? classifyBoundedText(safeOutput).errorCategory),
|
|
158
|
+
resultCount: emptyMarker === undefined ? undefined : countResultLines(safeOutput, emptyMarker),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** `runHandlers` (collect rejections without short-circuiting, HSF27) and `lazy` (defer a module import to first call). */
|
|
2
|
+
import { errorMessage, errorTypeName, writeStderr } from '../core/format-utils.js';
|
|
3
|
+
import { UNITS } from '../core/units.js';
|
|
4
|
+
import { ERROR_TYPE_ATTR, POST_TOOL_HANDLER_ATTRS, POST_TOOL_METRICS } from '../otel/attributes.js';
|
|
5
|
+
import { recordMetric } from '../otel/otel.js';
|
|
6
|
+
/** Import on first call: one event per process, so eager imports loaded every handler graph to run one (PT14). A rejected import surfaces like a handler throw. */
|
|
7
|
+
export function lazy(load) {
|
|
8
|
+
return async (...args) => { await (await load())(...args); };
|
|
9
|
+
}
|
|
10
|
+
const REJECTION_LOG_PREFIX = '[post-tool]';
|
|
11
|
+
/**
|
|
12
|
+
* Settle already-started handler promises; rejections are logged and counted
|
|
13
|
+
* (`POST_TOOL_METRICS.HANDLER_REJECTIONS`), never short-circuit. A circuit
|
|
14
|
+
* breaker over pre-started promises cannot prevent work (PT1).
|
|
15
|
+
*/
|
|
16
|
+
export async function runHandlers(label, ...handlers) {
|
|
17
|
+
const results = await Promise.allSettled(handlers);
|
|
18
|
+
const errors = [];
|
|
19
|
+
for (const [i, r] of results.entries()) {
|
|
20
|
+
if (r.status === 'rejected') {
|
|
21
|
+
const errorMsg = errorMessage(r.reason);
|
|
22
|
+
writeStderr(`${REJECTION_LOG_PREFIX} Handler ${i} in "${label}" group rejected:`, errorMsg);
|
|
23
|
+
recordMetric(POST_TOOL_METRICS.HANDLER_REJECTIONS, 1, {
|
|
24
|
+
[POST_TOOL_HANDLER_ATTRS.GROUP]: label,
|
|
25
|
+
[POST_TOOL_HANDLER_ATTRS.INDEX]: String(i),
|
|
26
|
+
[ERROR_TYPE_ATTR]: errorTypeName(r.reason),
|
|
27
|
+
}, { unit: UNITS.REJECTION });
|
|
28
|
+
errors.push({ index: i, error: errorMsg });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return { failures: errors.length, total: handlers.length, errors };
|
|
32
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/** Tool-input extraction and routing helpers shared by pre-tool and post-tool handlers. */
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { extname, isAbsolute, join, resolve } from 'node:path';
|
|
4
|
+
import { AGENT_TOOLS, BUILTIN_TOOLS, EDIT_TOOLS, GIT_COMMIT_SUBCOMMAND, PLUGIN_TOOLS, MCP_TOOL_PREFIX } from '../core/constants.js';
|
|
5
|
+
export function isEditTool(name) {
|
|
6
|
+
return EDIT_TOOLS.has(name);
|
|
7
|
+
}
|
|
8
|
+
/** String field of a raw `tool_input`; undefined unless already a string, since `String()` yields "[object Object]" (CQ-007). */
|
|
9
|
+
export function readString(toolInput, key) {
|
|
10
|
+
const val = toolInput?.[key];
|
|
11
|
+
return typeof val === 'string' ? val : undefined;
|
|
12
|
+
}
|
|
13
|
+
/** `readString` on a hook payload. */
|
|
14
|
+
export function getStringField(input, key) {
|
|
15
|
+
return readString(input.tool_input, key);
|
|
16
|
+
}
|
|
17
|
+
/** Scalar field of a raw `tool_input` as text; objects and arrays are dropped rather than read as "[object Object]". */
|
|
18
|
+
export function getScalarText(toolInput, key) {
|
|
19
|
+
const value = toolInput?.[key];
|
|
20
|
+
if (typeof value === 'string')
|
|
21
|
+
return value;
|
|
22
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
23
|
+
return String(value);
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
/** The agent an Agent tool call spawns (`Task` is the pre-rename alias); undefined for every other tool. */
|
|
27
|
+
export function spawnedAgentType(input) {
|
|
28
|
+
return AGENT_TOOLS.has(input.tool_name ?? '') ? getStringField(input, 'subagent_type') : undefined;
|
|
29
|
+
}
|
|
30
|
+
/** Fast pre-screen for a Bash command that may have committed, before loading `git-commit.ts`. */
|
|
31
|
+
export function mayRunGitCommit(input) {
|
|
32
|
+
return input.tool_name === 'Bash' && (getStringField(input, 'command')?.includes(GIT_COMMIT_SUBCOMMAND) ?? false);
|
|
33
|
+
}
|
|
34
|
+
/** `cd <dir>` opening a command and followed by `&&`, `;`, a newline or the end; the target may be quoted. `cd dir || x` is not matched, since that cd may have failed. */
|
|
35
|
+
const LEADING_CD_PATTERN = /^[ \t]*cd[ \t]+(?:"([^"]+)"|'([^']+)'|([^\s;&|]+))[ \t]*(?:&&|;|\n|$)/;
|
|
36
|
+
/** Shell expansion the hook cannot evaluate (`$VAR`, `$(…)`, backticks, escapes, `~user`); such a target falls back to `cwd` rather than becoming a made-up path. */
|
|
37
|
+
const UNEXPANDED_TARGET_PATTERN = /[$`\\]|^~[^/]/;
|
|
38
|
+
const HOME_PREFIX = '~';
|
|
39
|
+
/**
|
|
40
|
+
* Where a tool's work happened: a Bash command's leading `cd` target, else the event's `cwd`.
|
|
41
|
+
* Claude Code resets the shell to the project directory when a `cd` leaves it, so `cwd` alone
|
|
42
|
+
* never shows work done in another repo.
|
|
43
|
+
*/
|
|
44
|
+
export function toolWorkingDirectory(input) {
|
|
45
|
+
const target = input.tool_name === 'Bash' ? leadingCdTarget(getStringField(input, 'command')) : undefined;
|
|
46
|
+
return target ? resolveCdTarget(target, input.cwd) : input.cwd;
|
|
47
|
+
}
|
|
48
|
+
/** The directory a command's opening `cd` names; undefined for none, or for `cd -`. */
|
|
49
|
+
function leadingCdTarget(command) {
|
|
50
|
+
const match = LEADING_CD_PATTERN.exec(command ?? '');
|
|
51
|
+
const target = match?.[1] ?? match?.[2] ?? match?.[3];
|
|
52
|
+
return target === undefined || target === '-' || UNEXPANDED_TARGET_PATTERN.test(target) ? undefined : target;
|
|
53
|
+
}
|
|
54
|
+
function resolveCdTarget(target, cwd) {
|
|
55
|
+
if (target === HOME_PREFIX || target.startsWith(`${HOME_PREFIX}/`))
|
|
56
|
+
return join(homedir(), target.slice(HOME_PREFIX.length));
|
|
57
|
+
if (isAbsolute(target))
|
|
58
|
+
return target;
|
|
59
|
+
return resolve(cwd, target);
|
|
60
|
+
}
|
|
61
|
+
/** The file a tool targets: `file_path`, or NotebookEdit's `notebook_path`. */
|
|
62
|
+
export function getTargetPath(toolInput) {
|
|
63
|
+
return readString(toolInput, 'file_path') ?? readString(toolInput, 'notebook_path');
|
|
64
|
+
}
|
|
65
|
+
/** Extension without the dot; '' for `Makefile`, dotfiles, or a dot only in a directory name. */
|
|
66
|
+
export function getFileExtension(filePath) {
|
|
67
|
+
return extname(filePath).slice(1);
|
|
68
|
+
}
|
|
69
|
+
/** Non-empty `field` values across a MultiEdit's `edits[]`, joined by newlines; '' when none. */
|
|
70
|
+
function joinMultiEditField(toolInput, field) {
|
|
71
|
+
const edits = Array.isArray(toolInput?.edits) ? toolInput.edits : [];
|
|
72
|
+
return edits
|
|
73
|
+
.map((e) => (e && typeof e === 'object' ? e[field] : undefined))
|
|
74
|
+
.filter((s) => typeof s === 'string' && s !== '')
|
|
75
|
+
.join('\n');
|
|
76
|
+
}
|
|
77
|
+
/** Text an edit tool replaces; null for Write, which replaces a whole file. */
|
|
78
|
+
export function getReplacedText(toolInput, toolName) {
|
|
79
|
+
if (toolName === 'MultiEdit')
|
|
80
|
+
return joinMultiEditField(toolInput, 'old_string') || null;
|
|
81
|
+
if (toolName !== 'Edit')
|
|
82
|
+
return null;
|
|
83
|
+
return readString(toolInput, 'old_string') || null;
|
|
84
|
+
}
|
|
85
|
+
function writtenText(text, isPartial) {
|
|
86
|
+
return text ? { text, isPartial } : null;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Text written by an edit tool plus whether it is a partial hunk (Write: whole file).
|
|
90
|
+
* Null when the input carries no content; callers gate on {@link isEditTool} (HSF5).
|
|
91
|
+
* The switch is exhaustive over `EditTool`, so a new file tool fails to compile here.
|
|
92
|
+
*/
|
|
93
|
+
export function getWrittenText(toolInput, toolName) {
|
|
94
|
+
switch (toolName) {
|
|
95
|
+
case 'Write': return writtenText(readString(toolInput, 'content'), false);
|
|
96
|
+
case 'Edit': return writtenText(readString(toolInput, 'new_string'), true);
|
|
97
|
+
case 'MultiEdit': return writtenText(joinMultiEditField(toolInput, 'new_string'), true);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Routing category (mcp > plugin > agent > builtin, by subType hint or name); null when unrecognised. */
|
|
101
|
+
export function classifyToolCategory(toolName, subType) {
|
|
102
|
+
if (subType === 'mcp' || toolName.startsWith(MCP_TOOL_PREFIX))
|
|
103
|
+
return 'mcp';
|
|
104
|
+
if (subType === 'plugin' || PLUGIN_TOOLS.has(toolName))
|
|
105
|
+
return 'plugin';
|
|
106
|
+
if (subType === 'agent' || AGENT_TOOLS.has(toolName))
|
|
107
|
+
return 'agent';
|
|
108
|
+
if (subType === 'builtin' || BUILTIN_TOOLS.has(toolName))
|
|
109
|
+
return 'builtin';
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One bounded string view of `tool_response` (preferred) or legacy `tool_output`
|
|
3
|
+
* for every PostToolUse handler. Serialisation of an oversized payload is abandoned
|
|
4
|
+
* part-way (ER4) and replaced by a sentinel, never truncated into malformed JSON.
|
|
5
|
+
*/
|
|
6
|
+
import { THRESHOLDS } from '../core/constants.js';
|
|
7
|
+
import { memoizeIn } from '../core/file-utils.js';
|
|
8
|
+
/** Thrown from the replacer to abandon serialisation early; never escapes `stringifyBounded`. */
|
|
9
|
+
class ResponseTooLarge extends Error {
|
|
10
|
+
}
|
|
11
|
+
/** Stringify, bailing once string content alone passes the limit, so a huge payload is never materialised. */
|
|
12
|
+
function stringifyBounded(value) {
|
|
13
|
+
let remaining = THRESHOLDS.MAX_JSON_SIZE_BYTES;
|
|
14
|
+
try {
|
|
15
|
+
const json = JSON.stringify(value, (_key, v) => {
|
|
16
|
+
if (typeof v === 'string') {
|
|
17
|
+
remaining -= v.length;
|
|
18
|
+
if (remaining < 0)
|
|
19
|
+
throw new ResponseTooLarge();
|
|
20
|
+
}
|
|
21
|
+
return v;
|
|
22
|
+
});
|
|
23
|
+
return json.length > THRESHOLDS.MAX_JSON_SIZE_BYTES
|
|
24
|
+
? `[response truncated: ${json.length} chars]`
|
|
25
|
+
: json;
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
return error instanceof ResponseTooLarge ? '[response too large to serialize]' : '[unserializable response]';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function resolveUncached(input) {
|
|
32
|
+
if (input.tool_response == null)
|
|
33
|
+
return input.tool_output ?? '';
|
|
34
|
+
if (typeof input.tool_response === 'string') {
|
|
35
|
+
return input.tool_response.slice(0, THRESHOLDS.MAX_JSON_SIZE_BYTES);
|
|
36
|
+
}
|
|
37
|
+
return stringifyBounded(input.tool_response);
|
|
38
|
+
}
|
|
39
|
+
/** Keyed on the input object: router and handlers all ask for the same object's text. */
|
|
40
|
+
const resolvedByInput = new WeakMap();
|
|
41
|
+
/** One resolution per event. */
|
|
42
|
+
export function resolveOutputText(input) {
|
|
43
|
+
return memoizeIn(resolvedByInput, input, () => resolveUncached(input));
|
|
44
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fallback metric for tool calls `classifyToolCategory` does not recognise (CCH7),
|
|
3
|
+
* so a new harness tool or malformed name surfaces as a rising
|
|
4
|
+
* `integritystudio.tool.unknown` series instead of vanishing from every denominator.
|
|
5
|
+
*/
|
|
6
|
+
import { MAX_METRIC_LABEL_LENGTH, UNKNOWN_TOOL_CATEGORY, UNKNOWN_TOOL_NAME } from '../core/constants.js';
|
|
7
|
+
import { truncate } from '../core/format-utils.js';
|
|
8
|
+
import { UNITS } from '../core/units.js';
|
|
9
|
+
import { SESSION_ATTRIBUTES, GENAI_TOOL_NAME_ATTR, HOOK_TRIGGER_ATTR, UNKNOWN_HOOK_TYPE, HOOK_NAMES, HOOK_TYPE_ATTR, TOOL_ATTRS } from '../otel/attributes.js';
|
|
10
|
+
import { instrumentHook, getHookTrigger } from '../otel/otel-monitor.js';
|
|
11
|
+
/** Metric recorded once per unrecognised tool call, on both Pre- and PostToolUse; `trigger` tells the two apart. */
|
|
12
|
+
export const UNKNOWN_TOOL_METRIC = 'integritystudio.tool.unknown';
|
|
13
|
+
/** Record an uninstrumented tool call. The name is unvetted, so the label is bounded. */
|
|
14
|
+
export async function recordUnknownTool(toolName, sessionId) {
|
|
15
|
+
const tool = toolName
|
|
16
|
+
? truncate(toolName, MAX_METRIC_LABEL_LENGTH, '')
|
|
17
|
+
: UNKNOWN_TOOL_NAME;
|
|
18
|
+
await instrumentHook(HOOK_NAMES.UNKNOWN_TOOL, (ctx) => {
|
|
19
|
+
ctx.addAttributes({
|
|
20
|
+
[GENAI_TOOL_NAME_ATTR]: tool,
|
|
21
|
+
[TOOL_ATTRS.CATEGORY]: UNKNOWN_TOOL_CATEGORY,
|
|
22
|
+
[SESSION_ATTRIBUTES.SESSION_ID]: sessionId ?? '',
|
|
23
|
+
});
|
|
24
|
+
ctx.logger.warn('Uninstrumented tool call — not in BUILTIN_TOOLS_BY_CATEGORY', {
|
|
25
|
+
[GENAI_TOOL_NAME_ATTR]: tool,
|
|
26
|
+
});
|
|
27
|
+
ctx.recordMetric(UNKNOWN_TOOL_METRIC, 1, {
|
|
28
|
+
[GENAI_TOOL_NAME_ATTR]: tool,
|
|
29
|
+
[HOOK_TRIGGER_ATTR]: getHookTrigger(),
|
|
30
|
+
}, { unit: UNITS.INVOCATION });
|
|
31
|
+
}, { [HOOK_TYPE_ATTR]: UNKNOWN_HOOK_TYPE });
|
|
32
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The session's edit ledger: one line per Write/Edit/MultiEdit, written by the
|
|
3
|
+
* PostToolUse router, read by the Stop error reminder.
|
|
4
|
+
* Paths are symlink-resolved to match `git rev-parse`.
|
|
5
|
+
*/
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { appendToLog, readTextOrNull, realFilePath } from '../core/file-utils.js';
|
|
8
|
+
import { sessionStateDir } from './session-state.js';
|
|
9
|
+
const EDIT_LEDGER = 'edits.log';
|
|
10
|
+
/** The `agent` field of a main-loop edit. */
|
|
11
|
+
export const MAIN_LOOP_AGENT = '';
|
|
12
|
+
/** Append one edit to the session's ledger; `appendToLog` creates the directory. */
|
|
13
|
+
export function logEdit(sessionId, tool, agent, filePath) {
|
|
14
|
+
appendToLog(sessionStateDir(sessionId), EDIT_LEDGER, tool, agent, realFilePath(filePath));
|
|
15
|
+
}
|
|
16
|
+
/** Every edit logged this session, oldest first. Malformed lines are skipped. */
|
|
17
|
+
export function loadEdits(sessionId) {
|
|
18
|
+
const text = readTextOrNull(join(sessionStateDir(sessionId), EDIT_LEDGER));
|
|
19
|
+
if (text === null)
|
|
20
|
+
return [];
|
|
21
|
+
const entries = [];
|
|
22
|
+
for (const line of text.split('\n')) {
|
|
23
|
+
const [timestamp, tool, agent, path] = line.split('\t');
|
|
24
|
+
const timestampMs = Date.parse(timestamp ?? '');
|
|
25
|
+
if (tool && agent !== undefined && path && !Number.isNaN(timestampMs))
|
|
26
|
+
entries.push({ timestampMs, tool, agent, path });
|
|
27
|
+
}
|
|
28
|
+
return entries;
|
|
29
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Forked-skill attribution (CSV1). A forked skill runs as `general-purpose`, so
|
|
3
|
+
* `agent_type` alone cannot tell it from a plain subagent; the skill name comes
|
|
4
|
+
* from the `subagents/agent-<id>.forked-skill.json` record Claude Code writes at
|
|
5
|
+
* spawn, beside the session transcript.
|
|
6
|
+
*/
|
|
7
|
+
import { dirname, join } from 'node:path';
|
|
8
|
+
import { CLAUDE_HOME } from '../core/constants.js';
|
|
9
|
+
import { loadJson } from '../core/file-utils.js';
|
|
10
|
+
import { AGENT_FILE_PREFIX, projectDirPaths, subagentsDir } from '../transcript/transcript-layout.js';
|
|
11
|
+
/** Agent-name prefix for a forked skill, so a skill never collides with a same-named agent. */
|
|
12
|
+
const SKILL_AGENT_PREFIX = 'skill:';
|
|
13
|
+
export const FORKED_SKILL_SUFFIX = '.forked-skill.json';
|
|
14
|
+
/** The skill an agent in `agentsDir` was forked for; undefined for a missing, torn or foreign record. */
|
|
15
|
+
export function readForkedSkillName(agentsDir, agentId) {
|
|
16
|
+
const skillName = loadJson(join(agentsDir, `${AGENT_FILE_PREFIX}${agentId}${FORKED_SKILL_SUFFIX}`))?.skillName;
|
|
17
|
+
return typeof skillName === 'string' && skillName ? skillName : undefined;
|
|
18
|
+
}
|
|
19
|
+
function forkedSkillName(sessionId, agentId, transcriptPath, home) {
|
|
20
|
+
const dirs = transcriptPath ? [dirname(transcriptPath)] : projectDirPaths(home);
|
|
21
|
+
for (const dir of dirs) {
|
|
22
|
+
const skillName = readForkedSkillName(subagentsDir(dir, sessionId), agentId);
|
|
23
|
+
if (skillName)
|
|
24
|
+
return skillName;
|
|
25
|
+
}
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The name a subagent is attributed to: `skill:<name>` for a forked skill, else
|
|
30
|
+
* its agent type; undefined on the main loop. With no transcript path, every
|
|
31
|
+
* project directory is searched.
|
|
32
|
+
*/
|
|
33
|
+
export function resolveAgentName(input, home = CLAUDE_HOME) {
|
|
34
|
+
const { agent_type: agentType, agent_id: agentId, session_id: sessionId, transcript_path: transcriptPath } = input;
|
|
35
|
+
if (!agentType)
|
|
36
|
+
return undefined;
|
|
37
|
+
const skill = agentId && sessionId ? forkedSkillName(sessionId, agentId, transcriptPath, home) : undefined;
|
|
38
|
+
return skill ? `${SKILL_AGENT_PREFIX}${skill}` : agentType;
|
|
39
|
+
}
|