@rt-tools/agent-kit 0.37.1 → 0.38.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/README.md +5 -5
- package/assets/checks/action-of.mjs +154 -0
- package/assets/checks/board-runs.github.mjs +40 -5
- package/assets/checks/check-board.github.mjs +38 -7
- package/assets/checks/check-state-next.mjs +22 -10
- package/assets/checks/check-turn-map.mjs +26 -6
- package/assets/checks/check-work-steps.mjs +4 -173
- package/assets/checks/decide.mjs +285 -0
- package/assets/checks/decision-table.json +21365 -0
- package/assets/checks/decision-table.mjs +495 -0
- package/assets/checks/epic-window.mjs +98 -0
- package/assets/checks/flow-condition.mjs +64 -0
- package/assets/checks/flow-data.mjs +136 -0
- package/assets/checks/flow-forecast.mjs +330 -0
- package/assets/checks/flow-guard-run.mjs +90 -0
- package/assets/checks/flow-guard.mjs +294 -0
- package/assets/checks/flow-model.mjs +271 -0
- package/assets/checks/flow-override.mjs +130 -0
- package/assets/checks/flow-plan.mjs +206 -0
- package/assets/checks/flow-scaffold.mjs +224 -0
- package/assets/checks/flow-step.mjs +365 -0
- package/assets/checks/flow-sync.mjs +296 -0
- package/assets/checks/flow-syntax.mjs +292 -0
- package/assets/checks/flow-verdict.mjs +112 -0
- package/assets/checks/merge-read.mjs +239 -0
- package/assets/checks/owner-nudges.mjs +389 -0
- package/assets/checks/push-inputs.mjs +59 -16
- package/assets/checks/record-count.mjs +410 -0
- package/assets/checks/record-recount.mjs +187 -0
- package/assets/checks/record-turn.mjs +155 -0
- package/assets/checks/task-flow-script.mjs +292 -0
- package/assets/checks/tree-assignment.mjs +26 -7
- package/assets/checks/work-result.mjs +281 -0
- package/assets/checks/work-steps-lib.mjs +205 -0
- package/assets/commands/night.md +6 -6
- package/assets/defaults/gate-map.sh +39 -12
- package/assets/defaults/merges.sh +48 -0
- package/assets/defaults/project.sh +6 -5
- package/assets/defaults/turn-map.md +21 -22
- package/assets/docs/GLOSSARY.md +5 -1
- package/assets/hooks/ask-judge.md +65 -0
- package/assets/hooks/browser-guard-device-id.sh +11 -9
- package/assets/hooks/browser-guard-no-asking.sh +12 -6
- package/assets/hooks/browser-guard-no-listing.sh +13 -7
- package/assets/hooks/browser-guard-no-other-drivers.sh +25 -17
- package/assets/hooks/browser-guard-require-select.sh +15 -15
- package/assets/hooks/commit-msg.sh +13 -3
- package/assets/hooks/conscience-guard.sh +9 -25
- package/assets/hooks/decide.sh +141 -0
- package/assets/hooks/deny-tail.sh +16 -11
- package/assets/hooks/dev-server-guard.sh +27 -20
- package/assets/hooks/dispatch.sh +47 -4
- package/assets/hooks/docs-guard.sh +31 -200
- package/assets/hooks/end-conversation-guard.sh +12 -21
- package/assets/hooks/epic-stop-guard.sh +54 -152
- package/assets/hooks/exam-guard.sh +32 -65
- package/assets/hooks/facts-docs.sh +221 -0
- package/assets/hooks/facts-flow.sh +173 -0
- package/assets/hooks/facts.sh +308 -0
- package/assets/hooks/flow-step-guard.sh +93 -0
- package/assets/hooks/git-guard-delivery-body.sh +4 -4
- package/assets/hooks/git-guard-delivery-conflict.sh +16 -10
- package/assets/hooks/git-guard-delivery-draft.sh +3 -3
- package/assets/hooks/git-guard-delivery-epic.sh +18 -9
- package/assets/hooks/git-guard-delivery-folder.sh +32 -7
- package/assets/hooks/git-guard-delivery-merge.sh +2 -2
- package/assets/hooks/git-guard-delivery-merged.sh +12 -1
- package/assets/hooks/git-guard-delivery-pipeline.sh +1 -1
- package/assets/hooks/git-guard-delivery-rerun.sh +32 -0
- package/assets/hooks/git-guard-delivery-signature.sh +82 -45
- package/assets/hooks/git-guard-delivery-task.sh +47 -0
- package/assets/hooks/git-guard-delivery.sh +112 -101
- package/assets/hooks/git-guard-discard.sh +8 -18
- package/assets/hooks/git-guard-main.sh +13 -19
- package/assets/hooks/git-guard-no-verify.sh +56 -16
- package/assets/hooks/git-guard-push-ahead.sh +15 -4
- package/assets/hooks/git-guard-push-marks.sh +53 -1
- package/assets/hooks/git-guard-push-tests.sh +30 -51
- package/assets/hooks/git-guard-tree-assignment.sh +7 -4
- package/assets/hooks/guard-note.sh +8 -6
- package/assets/hooks/handoff-entry-guard.sh +19 -38
- package/assets/hooks/handoff-write.sh +3 -1
- package/assets/hooks/hook-input.sh +0 -41
- package/assets/hooks/metric-marks.sh +19 -7
- package/assets/hooks/observe.sh +1 -1
- package/assets/hooks/override-write-guard.sh +27 -9
- package/assets/hooks/pre-push.sh +14 -10
- package/assets/hooks/prose-style-guard.sh +6 -19
- package/assets/hooks/pull-merges.sh +76 -0
- package/assets/hooks/push-set.sh +11 -4
- package/assets/hooks/qa-dataid-guard.sh +7 -17
- package/assets/hooks/record-turn.sh +52 -0
- package/assets/hooks/rerun-guard.sh +11 -16
- package/assets/hooks/reuse-first-guard.sh +7 -21
- package/assets/hooks/role-calls.sh +25 -3
- package/assets/hooks/rule-source-guard.sh +54 -21
- package/assets/hooks/skill-gate-rearm.sh +18 -5
- package/assets/hooks/skill-gate.sh +156 -20
- package/assets/hooks/skill-loaded.sh +18 -3
- package/assets/hooks/sql-guard-target.sh +2 -2
- package/assets/hooks/sql-guard-write.sh +12 -12
- package/assets/hooks/sql-guard.sh +58 -16
- package/assets/hooks/task-context-load.sh +3 -1
- package/assets/hooks/task-flow-context.sh +5 -18
- package/assets/hooks/task-flow-draft-guard.sh +25 -74
- package/assets/hooks/task-flow-guard.sh +28 -130
- package/assets/hooks/tree-assignment-guard.sh +25 -14
- package/assets/hooks/turn-entry-load.sh +3 -1
- package/assets/hooks/turn-exit-guard.sh +92 -452
- package/assets/hooks/turn-exit-role.sh +3 -1
- package/assets/hooks/turn-judge.md +103 -0
- package/assets/hooks/window-fill-guard.sh +22 -41
- package/assets/patterns/git-workflow-commit.azure.md +9 -155
- package/assets/patterns/git-workflow-commit.github.md +11 -269
- package/assets/patterns/git-workflow-commit.gitlab.md +9 -161
- package/assets/patterns/git-workflow-docker.md +8 -231
- package/assets/patterns/git-workflow-freshness.md +8 -56
- package/assets/patterns/git-workflow-merge.md +6 -176
- package/assets/patterns/git-workflow-migration.md +5 -75
- package/assets/patterns/git-workflow-pr-body.github.md +6 -159
- package/assets/patterns/git-workflow-pr-ready.md +9 -70
- package/assets/patterns/git-workflow-pr.azure.md +13 -285
- package/assets/patterns/git-workflow-pr.github.md +13 -251
- package/assets/patterns/git-workflow-pr.gitlab.md +13 -286
- package/assets/patterns/git-workflow-restart.md +5 -42
- package/assets/patterns/git-workflow-secrets.md +10 -76
- package/assets/patterns/git-workflow-stack.md +7 -207
- package/assets/patterns/status-report-table.github.md +1 -1
- package/assets/patterns/task-flow-archive.md +12 -233
- package/assets/patterns/task-flow-close.md +16 -292
- package/assets/patterns/task-flow-cycle.md +13 -39
- package/assets/patterns/task-flow-graph.needs-graph.md +8 -62
- package/assets/patterns/task-flow-handoff.md +13 -161
- package/assets/patterns/task-flow-resume.md +18 -218
- package/assets/patterns/task-flow-start.md +9 -247
- package/assets/patterns/turn-entry-map.md +2 -2
- package/assets/pitfalls/agent-kit-extend.md +200 -0
- package/assets/pitfalls/agent-kit.2.md +323 -0
- package/assets/pitfalls/angular-patterns.md +98 -0
- package/assets/pitfalls/browser-verification.md +265 -0
- package/assets/pitfalls/component-structure.md +98 -0
- package/assets/pitfalls/dependencies.md +92 -0
- package/assets/pitfalls/deploy-flow.azure.md +171 -0
- package/assets/pitfalls/deploy-flow.github.md +211 -0
- package/assets/pitfalls/deploy-flow.gitlab.md +173 -0
- package/assets/pitfalls/doc-style.2.md +286 -0
- package/assets/pitfalls/entity-conventions.needs-admin.md +96 -0
- package/assets/pitfalls/entity-models.md +89 -0
- package/assets/pitfalls/git-workflow-commit.azure.md +190 -0
- package/assets/pitfalls/git-workflow-commit.github.md +307 -0
- package/assets/pitfalls/git-workflow-commit.gitlab.md +196 -0
- package/assets/pitfalls/git-workflow-docker.md +275 -0
- package/assets/pitfalls/git-workflow-freshness.md +88 -0
- package/assets/pitfalls/git-workflow-hosting.azure.md +31 -0
- package/assets/pitfalls/git-workflow-hosting.github.md +32 -0
- package/assets/pitfalls/git-workflow-hosting.gitlab.md +31 -0
- package/assets/pitfalls/git-workflow-merge.md +220 -0
- package/assets/pitfalls/git-workflow-migration.md +103 -0
- package/assets/pitfalls/git-workflow-pr-body.github.md +185 -0
- package/assets/pitfalls/git-workflow-pr-ready.md +104 -0
- package/assets/pitfalls/git-workflow-pr.2.azure.md +172 -0
- package/assets/pitfalls/git-workflow-pr.2.github.md +184 -0
- package/assets/pitfalls/git-workflow-pr.2.gitlab.md +180 -0
- package/assets/pitfalls/git-workflow-pr.azure.md +159 -0
- package/assets/pitfalls/git-workflow-pr.github.md +107 -0
- package/assets/pitfalls/git-workflow-pr.gitlab.md +151 -0
- package/assets/pitfalls/git-workflow-push.azure.md +30 -0
- package/assets/pitfalls/git-workflow-push.github.md +30 -0
- package/assets/pitfalls/git-workflow-push.gitlab.md +30 -0
- package/assets/pitfalls/git-workflow-queue.azure.md +30 -0
- package/assets/pitfalls/git-workflow-queue.github.md +30 -0
- package/assets/pitfalls/git-workflow-queue.gitlab.md +30 -0
- package/assets/pitfalls/git-workflow-restart.md +72 -0
- package/assets/pitfalls/git-workflow-secrets.md +114 -0
- package/assets/pitfalls/git-workflow-stack.md +254 -0
- package/assets/pitfalls/git-workflow.2.azure.md +291 -0
- package/assets/pitfalls/git-workflow.2.github.md +294 -0
- package/assets/pitfalls/git-workflow.2.gitlab.md +278 -0
- package/assets/pitfalls/lib-layers.md +114 -0
- package/assets/pitfalls/lists.md +117 -0
- package/assets/pitfalls/navigation.md +94 -0
- package/assets/pitfalls/reuse-first.md +201 -0
- package/assets/pitfalls/spec-driven.2.md +318 -0
- package/assets/pitfalls/styling-bem.2.md +125 -0
- package/assets/pitfalls/task-flow-archive.md +264 -0
- package/assets/pitfalls/task-flow-close.2.md +165 -0
- package/assets/pitfalls/task-flow-close.md +167 -0
- package/assets/pitfalls/task-flow-cycle.md +73 -0
- package/assets/pitfalls/task-flow-graph.needs-graph.md +98 -0
- package/assets/pitfalls/task-flow-handoff.md +200 -0
- package/assets/pitfalls/task-flow-handover.md +30 -0
- package/assets/pitfalls/task-flow-owner.md +29 -0
- package/assets/pitfalls/task-flow-plan.md +29 -0
- package/assets/pitfalls/task-flow-resume.md +257 -0
- package/assets/pitfalls/task-flow-start.md +287 -0
- package/assets/pitfalls/task-flow.2.md +303 -0
- package/assets/pitfalls/task-flow.md +4 -4
- package/assets/pitfalls/testing.2.md +279 -0
- package/assets/pitfalls/turn-conduct.md +16 -17
- package/assets/pitfalls/typescript-conventions.md +161 -0
- package/assets/pitfalls/write-a-skill.md +128 -0
- package/assets/rules/angular-patterns.md +21 -86
- package/assets/rules/autonomous-work.md +2 -2
- package/assets/rules/browser-verification.md +28 -256
- package/assets/rules/component-structure.md +25 -87
- package/assets/rules/dependencies.md +20 -78
- package/assets/rules/deploy-flow.azure.md +26 -156
- package/assets/rules/deploy-flow.github.md +34 -200
- package/assets/rules/deploy-flow.gitlab.md +27 -159
- package/assets/rules/doc-style.md +32 -286
- package/assets/rules/entity-conventions.needs-admin.md +26 -82
- package/assets/rules/entity-models.md +14 -63
- package/assets/rules/git-workflow-hosting.azure.md +26 -0
- package/assets/rules/git-workflow-hosting.github.md +38 -0
- package/assets/rules/git-workflow-hosting.gitlab.md +27 -0
- package/assets/rules/git-workflow-push.azure.md +28 -0
- package/assets/rules/git-workflow-push.github.md +28 -0
- package/assets/rules/git-workflow-push.gitlab.md +28 -0
- package/assets/rules/git-workflow-queue.azure.md +23 -0
- package/assets/rules/git-workflow-queue.github.md +30 -0
- package/assets/rules/git-workflow-queue.gitlab.md +23 -0
- package/assets/rules/git-workflow.azure.md +21 -282
- package/assets/rules/git-workflow.github.md +24 -284
- package/assets/rules/git-workflow.gitlab.md +21 -269
- package/assets/rules/lib-layers.md +30 -103
- package/assets/rules/lists.md +21 -105
- package/assets/rules/navigation.md +10 -69
- package/assets/rules/reuse-first.md +20 -195
- package/assets/rules/spec-driven.md +35 -308
- package/assets/rules/styling-bem.md +20 -117
- package/assets/rules/task-flow-handover.md +24 -0
- package/assets/rules/task-flow-owner.md +34 -0
- package/assets/rules/task-flow-plan.md +38 -0
- package/assets/rules/task-flow.md +22 -300
- package/assets/rules/testing.md +32 -273
- package/assets/rules/turn-conduct.md +100 -104
- package/assets/rules/turn-entry.md +2 -2
- package/assets/rules/typescript-conventions.md +41 -147
- package/assets/samples/tasks/_template/plan.md +30 -6
- package/assets/samples/tasks/_template/progress.md +0 -1
- package/assets/skills/agent-kit-extend.md +25 -168
- package/assets/skills/agent-kit.md +27 -299
- package/assets/skills/write-a-skill.md +20 -106
- package/assets/task-flow/flow.js +188 -0
- package/assets/task-flow/flow.md +102 -0
- package/assets/task-flow/measure/agreement.mjs +24 -0
- package/assets/task-flow/measure/copy.mjs +22 -0
- package/assets/task-flow/measure/epic-of.mjs +50 -0
- package/assets/task-flow/measure/epic-text.mjs +35 -0
- package/assets/task-flow/measure/epic.mjs +114 -0
- package/assets/task-flow/measure/folder.mjs +16 -0
- package/assets/task-flow/measure/footprint.mjs +28 -0
- package/assets/task-flow/measure/grill.mjs +21 -0
- package/assets/task-flow/measure/host.mjs +8 -0
- package/assets/task-flow/measure/owner.mjs +20 -0
- package/assets/task-flow/measure/plan.mjs +27 -0
- package/assets/task-flow/measure/pr.mjs +20 -0
- package/assets/task-flow/measure/progress.mjs +50 -0
- package/assets/task-flow/measure/session.mjs +175 -0
- package/assets/task-flow/measure/task.mjs +23 -0
- package/assets/task-flow/measure/text.mjs +77 -0
- package/assets/task-flow/source/git.mjs +86 -0
- package/assets/task-flow/source/host.mjs +172 -0
- package/assets/task-flow/source/marks.mjs +56 -0
- package/assets/task-flow/source/owner.mjs +74 -0
- package/assets/task-flow/source/records.mjs +116 -0
- package/assets/task-flow/source/session.mjs +232 -0
- package/assets/task-flow/source/steps.mjs +28 -0
- package/assets/task-flow/source/tree.mjs +42 -0
- package/bin/agent-kit.d.ts.map +1 -1
- package/bin/agent-kit.js +30 -0
- package/bin/agent-kit.js.map +1 -1
- package/lib/agent-checks.d.ts +55 -0
- package/lib/agent-checks.d.ts.map +1 -0
- package/lib/agent-checks.js +209 -0
- package/lib/agent-checks.js.map +1 -0
- package/lib/assets.d.ts.map +1 -1
- package/lib/assets.js +7 -2
- package/lib/assets.js.map +1 -1
- package/lib/cargo.d.ts +29 -0
- package/lib/cargo.d.ts.map +1 -1
- package/lib/cargo.js +27 -1
- package/lib/cargo.js.map +1 -1
- package/lib/cascade.d.ts.map +1 -1
- package/lib/cascade.js +3 -1
- package/lib/cascade.js.map +1 -1
- package/lib/commands.d.ts.map +1 -1
- package/lib/commands.js +78 -2
- package/lib/commands.js.map +1 -1
- package/lib/config.d.ts +18 -0
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +18 -1
- package/lib/config.js.map +1 -1
- package/lib/epic-window-send.d.ts +74 -0
- package/lib/epic-window-send.d.ts.map +1 -0
- package/lib/epic-window-send.js +166 -0
- package/lib/epic-window-send.js.map +1 -0
- package/lib/hooks-map.d.ts +4 -0
- package/lib/hooks-map.d.ts.map +1 -1
- package/lib/hooks-map.js +2 -2
- package/lib/hooks-map.js.map +1 -1
- package/lib/integrity.d.ts +3 -0
- package/lib/integrity.d.ts.map +1 -1
- package/lib/integrity.js +7 -0
- package/lib/integrity.js.map +1 -1
- package/lib/metric-lines.d.ts +2 -1
- package/lib/metric-lines.d.ts.map +1 -1
- package/lib/metric-lines.js +49 -1
- package/lib/metric-lines.js.map +1 -1
- package/lib/metric-send-mark.d.ts +6 -3
- package/lib/metric-send-mark.d.ts.map +1 -1
- package/lib/metric-send-mark.js +15 -5
- package/lib/metric-send-mark.js.map +1 -1
- package/lib/metric-send.d.ts +6 -2
- package/lib/metric-send.d.ts.map +1 -1
- package/lib/metric-send.js +47 -13
- package/lib/metric-send.js.map +1 -1
- package/lib/metrics.d.ts +17 -0
- package/lib/metrics.d.ts.map +1 -1
- package/lib/metrics.js +26 -15
- package/lib/metrics.js.map +1 -1
- package/lib/observations.js +3 -3
- package/lib/observations.js.map +1 -1
- package/lib/outcomes.d.ts +5 -2
- package/lib/outcomes.d.ts.map +1 -1
- package/lib/outcomes.js +5 -4
- package/lib/outcomes.js.map +1 -1
- package/lib/push-gate.d.ts +13 -0
- package/lib/push-gate.d.ts.map +1 -1
- package/lib/push-gate.js +11 -7
- package/lib/push-gate.js.map +1 -1
- package/lib/push-set-usefulness.d.ts +78 -0
- package/lib/push-set-usefulness.d.ts.map +1 -0
- package/lib/push-set-usefulness.js +225 -0
- package/lib/push-set-usefulness.js.map +1 -0
- package/lib/sync.d.ts +10 -0
- package/lib/sync.d.ts.map +1 -1
- package/lib/sync.js +14 -1
- package/lib/sync.js.map +1 -1
- package/lib/work-result.d.ts +43 -0
- package/lib/work-result.d.ts.map +1 -0
- package/lib/work-result.js +39 -0
- package/lib/work-result.js.map +1 -0
- package/package.json +1 -1
- package/assets/hooks/claim-guard.sh +0 -212
- package/assets/hooks/grill-gate-ask.sh +0 -26
- package/assets/hooks/grill-gate-bypass.sh +0 -61
- package/assets/hooks/grill-gate-options.sh +0 -50
- package/assets/hooks/grill-gate.sh +0 -482
- package/assets/hooks/postmortem-guard.sh +0 -124
- package/assets/hooks/proposal-guard.sh +0 -154
- package/assets/hooks/stand-login-guard.sh +0 -107
- package/assets/hooks/turn-exit-epic.sh +0 -173
- package/assets/hooks/turn-exit-patterns.sh +0 -123
- package/assets/hooks/turn-exit-verdict.sh +0 -149
- package/assets/hooks/waiting-turn-guard.sh +0 -320
- package/assets/hooks/work-start-guard.sh +0 -175
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The counter of a session record: what the owner said and what the agent was refused, read from the
|
|
3
|
+
* conversation record Claude Code keeps per session.
|
|
4
|
+
*
|
|
5
|
+
* ownerWords(entries) the owner's words, oldest first: { text, at, session, kind }
|
|
6
|
+
* calls(entries) the calls of the agent in the main thread: { id, at, session, branch, tool }
|
|
7
|
+
* refusals(entries) the refusals, oldest first: { at, session, branch, kind, check }
|
|
8
|
+
* checkOf(text) the check a refusal text names, by the table below
|
|
9
|
+
* pairs(entries) ordered pairs of different checks refusing one after another: { first, second, at, session }
|
|
10
|
+
* interrupts(entries) the entries where the owner interrupted a turn: { at, session }
|
|
11
|
+
* corrections(entries, patterns) the owner's words the set of correction patterns matches
|
|
12
|
+
*
|
|
13
|
+
* A pair is two refusals of different checks with no successful call between them in one session;
|
|
14
|
+
* a repeat of the same check is not a pair. The set of correction patterns is passed in by the caller
|
|
15
|
+
* from the hook that declares it, so the two never hold copies of their own.
|
|
16
|
+
*
|
|
17
|
+
* A refusal is one of three classes, told by the first line of the result. A hook refused a call
|
|
18
|
+
* before it ran (`hook`), the permission mode turned it down (`permission`), or a hook at the end of a
|
|
19
|
+
* turn refused to let it end (`stop`). Every other error result — a non-zero exit of a command, a
|
|
20
|
+
* call the owner turned down, an interrupt, an error of the tool itself — is not a refusal. A stop
|
|
21
|
+
* refusal is counted from the entry `Stop hook feedback:` only; the summary entry of the stop hooks
|
|
22
|
+
* names the same refusal and is not counted again.
|
|
23
|
+
*
|
|
24
|
+
* The owner's words are what a person typed. A wrapper of the harness is no word of the owner: an
|
|
25
|
+
* interrupt mark, a command wrapper or its output, a notification of a task, the feedback of a hook,
|
|
26
|
+
* a meta entry or the summary of a squeeze. A message a person typed while a turn was going on is a
|
|
27
|
+
* word of the owner too: the record keeps it as an entry `attachment` of type `queued_command` whose
|
|
28
|
+
* origin kind is `human`, not as an entry `user`. A queued message the same session later repeats by
|
|
29
|
+
* a typed one counts once.
|
|
30
|
+
*
|
|
31
|
+
* The decisions take the entries as they are and never read the clock or the disk.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/** The opening of a typed entry that the harness writes, not a person. */
|
|
35
|
+
const WRAPPER =
|
|
36
|
+
/^(?:\[Request interrupted|Stop hook feedback:|PreToolUse:|PostToolUse:|Caveat:|This session is being continued|Background \(from the session|The app was quit|Another Claude session sent a message)/;
|
|
37
|
+
|
|
38
|
+
/** An opening tag of the harness; the text a person pasted opens with a tag too and stays a word. */
|
|
39
|
+
const HARNESS_TAG = /^<(?!pasted_content\b)[a-z][\w-]*[\s>/]/;
|
|
40
|
+
|
|
41
|
+
const isWrapper = (text) => WRAPPER.test(text) || HARNESS_TAG.test(text);
|
|
42
|
+
|
|
43
|
+
const textOf = (content) => {
|
|
44
|
+
if (typeof content === 'string') return content;
|
|
45
|
+
if (!Array.isArray(content)) return null;
|
|
46
|
+
if (content.some((part) => part?.type === 'tool_result')) return null;
|
|
47
|
+
const text = content
|
|
48
|
+
.filter((part) => part?.type === 'text')
|
|
49
|
+
.map((part) => part.text)
|
|
50
|
+
.join('\n');
|
|
51
|
+
return text || null;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** The text a person typed in this entry, before a turn or during one; null for anything else. */
|
|
55
|
+
export function typedOf(entry) {
|
|
56
|
+
if (!entry || entry.isMeta || entry.isCompactSummary) return null;
|
|
57
|
+
if (entry.type === 'user') {
|
|
58
|
+
if ((entry.origin?.kind ?? 'human') !== 'human') return null;
|
|
59
|
+
const text = textOf(entry.message?.content);
|
|
60
|
+
return text && !isWrapper(text.trimStart()) ? { text, kind: 'typed' } : null;
|
|
61
|
+
}
|
|
62
|
+
const queued = entry.type === 'attachment' ? entry.attachment : null;
|
|
63
|
+
if (queued?.type !== 'queued_command' || queued.origin?.kind !== 'human') return null;
|
|
64
|
+
const text = textOf(queued.prompt);
|
|
65
|
+
return text && !isWrapper(text.trimStart()) ? { text, kind: 'queued' } : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const same = (a, b) => a.trim() === b.trim();
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The beginning of a menu question that names the owner's earlier answer: such a question asks again
|
|
72
|
+
* what the owner already decided. The set is fixed and visible; a question in other words is not
|
|
73
|
+
* counted.
|
|
74
|
+
*/
|
|
75
|
+
export const REASK = /ваш прошлый ответ|вы уже ответили|не зачла ваш ответ/iu;
|
|
76
|
+
|
|
77
|
+
/** How much of the beginning of a question the re-ask sign reads. */
|
|
78
|
+
export const REASK_SPAN = 120;
|
|
79
|
+
|
|
80
|
+
/** Whether a menu question asks again what the owner already answered. */
|
|
81
|
+
export const isReask = (question) => REASK.test(String(question ?? '').slice(0, REASK_SPAN));
|
|
82
|
+
|
|
83
|
+
/** The text of a hook the harness hands back to a call it refused. */
|
|
84
|
+
const HOOK_ERROR = /^\S+ hook error: |^Agent hook condition was not met: /;
|
|
85
|
+
|
|
86
|
+
/** The text the permission mode hands back to a call it turned down. */
|
|
87
|
+
const PERMISSION = /^Permission for this action was denied/;
|
|
88
|
+
|
|
89
|
+
/** The entry the harness writes when a hook at the end of a turn refused. */
|
|
90
|
+
const STOP_FEEDBACK = /^Stop hook feedback:/;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The table from a refusal text to the check it names; the first matching line wins. A check that
|
|
94
|
+
* names itself is taken by its name; the rest are named here.
|
|
95
|
+
*/
|
|
96
|
+
const CHECK_TABLE = [
|
|
97
|
+
// A neighbouring guard that names the load of a rule is the same round as the gate refusal,
|
|
98
|
+
// only under another hook name, so it is counted as the gate before its own name is read.
|
|
99
|
+
[/The named move is held by the rules gate/, () => 'skill-gate'],
|
|
100
|
+
[/BLOCKED by ([a-z0-9][a-z0-9-]*):/, (m) => m[1]],
|
|
101
|
+
[/Refused by the rules gate/, () => 'skill-gate'],
|
|
102
|
+
[/Refused: the epic is over/, () => 'epic-over'],
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
/** The check a refusal text names, or `unnamed`. */
|
|
106
|
+
export function checkOf(text) {
|
|
107
|
+
for (const [pattern, name] of CHECK_TABLE) {
|
|
108
|
+
const match = pattern.exec(text ?? '');
|
|
109
|
+
if (match) return name(match);
|
|
110
|
+
}
|
|
111
|
+
return 'unnamed';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const resultText = (content) =>
|
|
115
|
+
typeof content === 'string'
|
|
116
|
+
? content
|
|
117
|
+
: Array.isArray(content)
|
|
118
|
+
? content.map((part) => (typeof part === 'string' ? part : (part?.text ?? ''))).join('\n')
|
|
119
|
+
: '';
|
|
120
|
+
|
|
121
|
+
const mainThread = (entry) => !entry?.isSidechain;
|
|
122
|
+
|
|
123
|
+
const unique = (entries) => {
|
|
124
|
+
const seen = new Set();
|
|
125
|
+
return entries.filter((entry) => {
|
|
126
|
+
const id = entry?.uuid;
|
|
127
|
+
if (!id) return true;
|
|
128
|
+
if (seen.has(id)) return false;
|
|
129
|
+
seen.add(id);
|
|
130
|
+
return true;
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/** The calls of the agent in the main thread of each session; a call of a role is not one. */
|
|
135
|
+
export function calls(entries) {
|
|
136
|
+
const out = [];
|
|
137
|
+
for (const entry of unique(entries)) {
|
|
138
|
+
if (entry?.type !== 'assistant' || !mainThread(entry) || !Array.isArray(entry.message?.content)) continue;
|
|
139
|
+
entry.message.content.forEach((part, index) => {
|
|
140
|
+
if (part?.type !== 'tool_use') return;
|
|
141
|
+
out.push({
|
|
142
|
+
id: part.id ?? `${entry.uuid}:${index}`,
|
|
143
|
+
at: entry.timestamp ?? null,
|
|
144
|
+
session: entry.sessionId ?? null,
|
|
145
|
+
branch: entry.gitBranch ?? null,
|
|
146
|
+
tool: part.name ?? null,
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The class of an error result by its first line, or null when it is not a refusal. */
|
|
154
|
+
export function refusalClass(text) {
|
|
155
|
+
const opening = (text ?? '').trimStart();
|
|
156
|
+
if (HOOK_ERROR.test(opening)) return 'hook';
|
|
157
|
+
if (PERMISSION.test(opening)) return 'permission';
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The refusals of the entries, oldest first. */
|
|
162
|
+
export function refusals(entries) {
|
|
163
|
+
const out = [];
|
|
164
|
+
for (const entry of unique(entries)) {
|
|
165
|
+
if (entry?.type !== 'user' || !mainThread(entry)) continue;
|
|
166
|
+
const where = { at: entry.timestamp ?? null, session: entry.sessionId ?? null, branch: entry.gitBranch ?? null };
|
|
167
|
+
const content = entry.message?.content;
|
|
168
|
+
const typed = typeof content === 'string' ? content : Array.isArray(content) && !content.some((p) => p?.type === 'tool_result') ? resultText(content) : null;
|
|
169
|
+
if (typed !== null) {
|
|
170
|
+
if (STOP_FEEDBACK.test(typed.trimStart())) out.push({ ...where, kind: 'stop', check: checkOf(typed) });
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (!Array.isArray(content)) continue;
|
|
174
|
+
for (const part of content) {
|
|
175
|
+
if (part?.type !== 'tool_result' || part.is_error !== true) continue;
|
|
176
|
+
const text = resultText(part.content);
|
|
177
|
+
const kind = refusalClass(text);
|
|
178
|
+
if (kind) out.push({ ...where, kind, check: kind === 'permission' ? 'permission' : checkOf(text) });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The owner's words of the entries, oldest first; an entry copied into a resumed session counts once. */
|
|
185
|
+
export function ownerWords(entries) {
|
|
186
|
+
const seen = new Set();
|
|
187
|
+
const words = [];
|
|
188
|
+
for (const entry of entries) {
|
|
189
|
+
const id = entry?.uuid ?? entry?.attachment?.delivery_id;
|
|
190
|
+
if (id && seen.has(id)) continue;
|
|
191
|
+
if (id) seen.add(id);
|
|
192
|
+
const word = typedOf(entry);
|
|
193
|
+
if (!word) continue;
|
|
194
|
+
const session = entry.sessionId ?? null;
|
|
195
|
+
const repeat =
|
|
196
|
+
word.kind === 'typed' && words.some((w) => w.kind === 'queued' && w.session === session && same(w.text, word.text));
|
|
197
|
+
if (repeat) continue;
|
|
198
|
+
words.push({ ...word, at: entry.timestamp ?? entry.attachment?.timestamp ?? null, session });
|
|
199
|
+
}
|
|
200
|
+
return words;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** The tools whose successful call changes a file of the tree. */
|
|
204
|
+
const EDIT_TOOLS = new Set(['Edit', 'Write', 'MultiEdit', 'NotebookEdit']);
|
|
205
|
+
const COMMIT_COMMAND = /\bgit\b(?:\s+-c\s+\S+)*\s+commit\b/;
|
|
206
|
+
const INTERRUPT = /^\[Request interrupted/;
|
|
207
|
+
const ANSWERED = /^(?:The user answered|Your questions have been answered)/;
|
|
208
|
+
|
|
209
|
+
/** The task of a branch: its key, or the branch itself when it names no task. */
|
|
210
|
+
export const taskOf = (branch) => (branch ? (branch.match(/^([A-Z]+-\d+)(?:-|$)/)?.[1] ?? branch) : null);
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The events of the entries, oldest first — what every count below is made of, and what the hook at
|
|
214
|
+
* the end of a turn writes as observations:
|
|
215
|
+
*
|
|
216
|
+
* call a call of the agent (`tool`)
|
|
217
|
+
* ok a call whose result came back without an error
|
|
218
|
+
* refusal a refusal (`kind`, `check`)
|
|
219
|
+
* change a change of the tree: a successful edit or write of a file, or a successful commit;
|
|
220
|
+
* `analysis` when the file is an incident analysis
|
|
221
|
+
* word a word of the owner (`kind`: typed or queued)
|
|
222
|
+
* menu an answer of the owner chosen in a menu
|
|
223
|
+
* interrupt an entry where the owner interrupted a turn or a call
|
|
224
|
+
* ask a menu question (`reask`: it names the owner's earlier answer)
|
|
225
|
+
*
|
|
226
|
+
* Each event carries `at`, `session` and `branch`; `answered` on a menu answer is the task of the
|
|
227
|
+
* question it answers.
|
|
228
|
+
*/
|
|
229
|
+
/** The path of an incident analysis: a file under a directory named so, or the word the cut keeps for it. */
|
|
230
|
+
const ANALYSIS_PATH = /(^|\/)postmortems(\/|$)/;
|
|
231
|
+
|
|
232
|
+
export function eventsOf(entries) {
|
|
233
|
+
const list = unique(entries).filter(mainThread);
|
|
234
|
+
const results = new Map();
|
|
235
|
+
for (const entry of list) {
|
|
236
|
+
if (!Array.isArray(entry?.message?.content)) continue;
|
|
237
|
+
for (const part of entry.message.content) if (part?.type === 'tool_result') results.set(part.tool_use_id, part.is_error === true);
|
|
238
|
+
}
|
|
239
|
+
const out = [];
|
|
240
|
+
const askTask = new Map();
|
|
241
|
+
const words = ownerWords(list);
|
|
242
|
+
const wordAt = new Set(words.map((w) => `${w.session}|${w.at}|${w.kind}`));
|
|
243
|
+
for (const entry of list) {
|
|
244
|
+
const where = { at: entry.timestamp ?? entry.attachment?.timestamp ?? null, session: entry.sessionId ?? null, branch: entry.gitBranch ?? null };
|
|
245
|
+
const content = Array.isArray(entry.message?.content) ? entry.message.content : [];
|
|
246
|
+
if (entry.type === 'assistant') {
|
|
247
|
+
for (const part of content) {
|
|
248
|
+
if (part?.type !== 'tool_use') continue;
|
|
249
|
+
out.push({ ...where, ev: 'call', tool: part.name ?? null });
|
|
250
|
+
const failed = results.get(part.id) === true;
|
|
251
|
+
const edit = EDIT_TOOLS.has(part.name);
|
|
252
|
+
const commit = part.name === 'Bash' && COMMIT_COMMAND.test(String(part.input?.command ?? ''));
|
|
253
|
+
if (!failed && (edit || commit)) {
|
|
254
|
+
const analysis = edit && ANALYSIS_PATH.test(String(part.input?.file_path ?? ''));
|
|
255
|
+
out.push({ ...where, ev: 'change', ...(analysis ? { analysis: true } : {}) });
|
|
256
|
+
}
|
|
257
|
+
if (part.name === 'AskUserQuestion') {
|
|
258
|
+
askTask.set(part.id, taskOf(where.branch));
|
|
259
|
+
const questions = Array.isArray(part.input?.questions) ? part.input.questions : [];
|
|
260
|
+
out.push({ ...where, ev: 'ask', reask: questions.some((q) => q?.reask === true || isReask(q?.question)) });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
const word = typedOf(entry);
|
|
266
|
+
if (word && wordAt.has(`${where.session}|${where.at}|${word.kind}`)) out.push({ ...where, ev: 'word', kind: word.kind });
|
|
267
|
+
if (entry.type !== 'user') continue;
|
|
268
|
+
for (const refusal of refusals([entry])) out.push({ ...where, ev: 'refusal', kind: refusal.kind, check: refusal.check });
|
|
269
|
+
for (const part of content) {
|
|
270
|
+
if (part?.type !== 'tool_result') continue;
|
|
271
|
+
if (part.is_error !== true) out.push({ ...where, ev: 'ok' });
|
|
272
|
+
if (part.is_error !== true && ANSWERED.test(resultText(part.content).trimStart()))
|
|
273
|
+
out.push({ ...where, ev: 'menu', answered: askTask.get(part.tool_use_id) ?? taskOf(where.branch) });
|
|
274
|
+
}
|
|
275
|
+
if (entry.toolUseResult?.answers && !content.some((p) => p?.type === 'tool_result' && ANSWERED.test(resultText(p.content).trimStart()))) {
|
|
276
|
+
const id = content.find((p) => p?.type === 'tool_result')?.tool_use_id;
|
|
277
|
+
out.push({ ...where, ev: 'menu', answered: askTask.get(id) ?? taskOf(where.branch) });
|
|
278
|
+
}
|
|
279
|
+
const texts = typeof entry.message?.content === 'string' ? [entry.message.content] : content.map((p) => (p?.type === 'tool_result' ? resultText(p.content) : (p?.text ?? '')));
|
|
280
|
+
if (texts.some((text) => INTERRUPT.test(text.trimStart()))) out.push({ ...where, ev: 'interrupt' });
|
|
281
|
+
}
|
|
282
|
+
return out.filter((e) => e.at).sort((a, b) => a.at.localeCompare(b.at));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Ordered pairs of different checks refusing one after another in a session, with no successful call between. */
|
|
286
|
+
export function pairsOf(events) {
|
|
287
|
+
const out = [];
|
|
288
|
+
const last = new Map();
|
|
289
|
+
for (const e of events) {
|
|
290
|
+
if (e.ev === 'ok') last.delete(e.session);
|
|
291
|
+
if (e.ev !== 'refusal') continue;
|
|
292
|
+
const before = last.get(e.session);
|
|
293
|
+
if (before && before !== e.check) out.push({ first: before, second: e.check, at: e.at, session: e.session });
|
|
294
|
+
last.set(e.session, e.check);
|
|
295
|
+
}
|
|
296
|
+
return out;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const minutesBetween = (from, to) => Math.max(0, Math.round((Date.parse(to) - Date.parse(from)) / 60000));
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* The idle stretches: from a refusal to the next change of the tree in any session the events hold.
|
|
303
|
+
* A refusal inside an open stretch adds nothing; a stretch no change closes ends at the last event of
|
|
304
|
+
* its day. A stretch belongs to the day it started, in UTC.
|
|
305
|
+
*/
|
|
306
|
+
export function idleOf(events) {
|
|
307
|
+
const lastOfDay = new Map();
|
|
308
|
+
for (const e of events) lastOfDay.set(e.at.slice(0, 10), e.at);
|
|
309
|
+
const out = [];
|
|
310
|
+
let open = null;
|
|
311
|
+
for (const e of events) {
|
|
312
|
+
if (e.ev === 'refusal' && !open) open = e.at;
|
|
313
|
+
else if (e.ev === 'change' && open) {
|
|
314
|
+
out.push({ from: open, to: e.at, minutes: minutesBetween(open, e.at), day: open.slice(0, 10) });
|
|
315
|
+
open = null;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (open) {
|
|
319
|
+
const to = lastOfDay.get(open.slice(0, 10)) ?? open;
|
|
320
|
+
out.push({ from: open, to, minutes: minutesBetween(open, to), day: open.slice(0, 10), open: true });
|
|
321
|
+
}
|
|
322
|
+
return out;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* The re-asks: a menu question whose beginning names the owner's earlier answer, asked in a task
|
|
327
|
+
* where a menu question was already answered. A question a check refused counts: it was asked.
|
|
328
|
+
*/
|
|
329
|
+
export function reasksOf(events) {
|
|
330
|
+
const answered = new Set();
|
|
331
|
+
const out = [];
|
|
332
|
+
for (const e of events) {
|
|
333
|
+
if (e.ev === 'menu') answered.add(e.answered);
|
|
334
|
+
if (e.ev === 'ask' && e.reask && answered.has(taskOf(e.branch))) out.push({ at: e.at, session: e.session, task: taskOf(e.branch) });
|
|
335
|
+
}
|
|
336
|
+
return out;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** The length from which a series of refusals of one check is a dead end. */
|
|
340
|
+
export const DEAD_END_SERIES = 3;
|
|
341
|
+
|
|
342
|
+
/** The longest pause between two refusals of one series; a longer one starts a new series. */
|
|
343
|
+
export const DEAD_END_GAP_MS = 15 * 60 * 1000;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* The dead ends: DEAD_END_SERIES or more refusals of one check in one session and one task, each
|
|
347
|
+
* within DEAD_END_GAP_MS of the one before, that the executor did not leave by itself — a word of the
|
|
348
|
+
* owner, typed or chosen in a menu, fell between the first and the last of them, or no change of the
|
|
349
|
+
* tree came after the last one in that session.
|
|
350
|
+
*/
|
|
351
|
+
export function deadEndsOf(events) {
|
|
352
|
+
const series = new Map();
|
|
353
|
+
const groups = [];
|
|
354
|
+
for (const e of events) {
|
|
355
|
+
if (e.ev !== 'refusal') continue;
|
|
356
|
+
const key = `${e.session}|${taskOf(e.branch)}|${e.check}`;
|
|
357
|
+
const open = series.get(key);
|
|
358
|
+
if (open && Date.parse(e.at) - Date.parse(open[open.length - 1].at) <= DEAD_END_GAP_MS) open.push(e);
|
|
359
|
+
else {
|
|
360
|
+
const group = [e];
|
|
361
|
+
series.set(key, group);
|
|
362
|
+
groups.push(group);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const out = [];
|
|
366
|
+
for (const group of groups) {
|
|
367
|
+
if (group.length < DEAD_END_SERIES) continue;
|
|
368
|
+
const { session, check, branch } = group[0];
|
|
369
|
+
const from = group[0].at;
|
|
370
|
+
const to = group[group.length - 1].at;
|
|
371
|
+
const answered = events.some((e) => (e.ev === 'word' || e.ev === 'menu') && e.session === session && e.at > from && e.at < to);
|
|
372
|
+
const moved = events.some((e) => e.ev === 'change' && e.session === session && e.at > to);
|
|
373
|
+
if (answered || !moved) out.push({ session, task: taskOf(branch), check, count: group.length, from, to });
|
|
374
|
+
}
|
|
375
|
+
return out;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// The same counts read straight from the entries of a record.
|
|
379
|
+
export const pairs = (entries) => pairsOf(eventsOf(entries));
|
|
380
|
+
export const idle = (entries) => idleOf(eventsOf(entries));
|
|
381
|
+
export const reasks = (entries) => reasksOf(eventsOf(entries));
|
|
382
|
+
export const deadEnds = (entries) => deadEndsOf(eventsOf(entries));
|
|
383
|
+
export const treeChanges = (entries) => eventsOf(entries).filter((e) => e.ev === 'change');
|
|
384
|
+
export const interrupts = (entries) => eventsOf(entries).filter((e) => e.ev === 'interrupt');
|
|
385
|
+
|
|
386
|
+
/** The reasons the rules gate writes into its refusal note, in the order the report names them. */
|
|
387
|
+
export const GATE_REASONS = ['too-long', 'together', 'not-read'];
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* The rules gate on a day, from its own notes in the observations: a refusal `gate-deny` counted by
|
|
391
|
+
* the reason it carries in `why` (a note with no reason, or a reason the list does not know, counts as
|
|
392
|
+
* `none`), and a delivery `gate-deliver` — one rule put into the session with no refusal. A note of
|
|
393
|
+
* the hook scenarios carries the version `dev` and is not counted: it judged no session of the tree.
|
|
394
|
+
*/
|
|
395
|
+
export function gateOf(observations, day) {
|
|
396
|
+
const refused = Object.fromEntries([...GATE_REASONS, 'none'].map((why) => [why, 0]));
|
|
397
|
+
let delivered = 0;
|
|
398
|
+
for (const o of observations) {
|
|
399
|
+
if (o.v === 'dev' || !String(o.t ?? '').startsWith(day)) continue;
|
|
400
|
+
if (o.ev === 'gate-deny') refused[GATE_REASONS.includes(o.why) ? o.why : 'none'] += 1;
|
|
401
|
+
if (o.ev === 'gate-deliver') delivered += 1;
|
|
402
|
+
}
|
|
403
|
+
return { refused, delivered };
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** The owner's words the set of correction patterns matches, a message queued during a turn included. */
|
|
407
|
+
export function corrections(entries, patterns) {
|
|
408
|
+
const pattern = new RegExp(patterns, 'iu');
|
|
409
|
+
return ownerWords(entries).filter((word) => pattern.test(word.text));
|
|
410
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* The recount of past days from the saved session records of this tree, by the counter the hook at
|
|
4
|
+
* the end of a turn uses. The hook counts only the turns after it was laid out; a day before that is
|
|
5
|
+
* counted here once, on demand.
|
|
6
|
+
*
|
|
7
|
+
* node record-recount.mjs --tree <root> --observations <dir> (--day YYYY-MM-DD | --days N --until YYYY-MM-DD)
|
|
8
|
+
* [--projects <dir>] [--patterns <correction patterns>]
|
|
9
|
+
*
|
|
10
|
+
* A saved record belongs to the tree when its working directory is the tree or a copy of it. A copy
|
|
11
|
+
* already removed from disk is recognised by its branches: they stand in the history of the tree.
|
|
12
|
+
*
|
|
13
|
+
* A session the hook already counted on a day is not recounted for that day: the hook counts a
|
|
14
|
+
* session whole on its first turn, days before its layout included. A day recounted before is
|
|
15
|
+
* replaced: the lines a recount wrote carry `src: "recount"` and are removed before the new ones are
|
|
16
|
+
* written, so a second run does not add to the first. The day is a calendar day of UTC.
|
|
17
|
+
*/
|
|
18
|
+
import { execFileSync } from 'node:child_process';
|
|
19
|
+
import { createReadStream, existsSync, readFileSync, readdirSync, statSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
20
|
+
import { createInterface } from 'node:readline';
|
|
21
|
+
import { homedir } from 'node:os';
|
|
22
|
+
import { basename, join } from 'node:path';
|
|
23
|
+
|
|
24
|
+
import { observationsOf } from './record-turn.mjs';
|
|
25
|
+
|
|
26
|
+
const DAY = 24 * 60 * 60 * 1000;
|
|
27
|
+
|
|
28
|
+
const option = (argv, name) => {
|
|
29
|
+
const at = argv.indexOf(name);
|
|
30
|
+
return at >= 0 ? argv[at + 1] : undefined;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** The POSIX `cksum` of a text: the session sign the hooks write is computed by it. */
|
|
34
|
+
export function cksum(text) {
|
|
35
|
+
const bytes = Buffer.from(text, 'utf8');
|
|
36
|
+
let crc = 0;
|
|
37
|
+
const step = (byte) => {
|
|
38
|
+
crc ^= byte << 24;
|
|
39
|
+
for (let i = 0; i < 8; i += 1) crc = crc & 0x80000000 ? ((crc << 1) ^ 0x04c11db7) >>> 0 : (crc << 1) >>> 0;
|
|
40
|
+
};
|
|
41
|
+
for (const byte of bytes) step(byte);
|
|
42
|
+
for (let length = bytes.length; length > 0; length >>>= 8) step(length & 0xff);
|
|
43
|
+
return String(~crc >>> 0);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The days of a window, oldest first. */
|
|
47
|
+
export function daysOf({ day, days, until }) {
|
|
48
|
+
if (day) return [day];
|
|
49
|
+
const end = Date.parse(`${until}T00:00:00Z`);
|
|
50
|
+
return Array.from({ length: Number(days) }, (_, i) => new Date(end - (Number(days) - 1 - i) * DAY).toISOString().slice(0, 10));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const git = (root, args) => {
|
|
54
|
+
try {
|
|
55
|
+
return execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
56
|
+
} catch {
|
|
57
|
+
return '';
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** The paths of the tree and its copies on this machine, and the branch names its history knows. */
|
|
62
|
+
function treeOf(root) {
|
|
63
|
+
const paths = new Set([root]);
|
|
64
|
+
for (const line of git(root, ['worktree', 'list', '--porcelain']).split('\n')) if (line.startsWith('worktree ')) paths.add(line.slice(9));
|
|
65
|
+
const branches = new Set();
|
|
66
|
+
for (const ref of git(root, ['for-each-ref', '--format=%(refname:short)', 'refs/heads', 'refs/remotes']).split('\n')) {
|
|
67
|
+
if (ref) branches.add(ref.replace(/^[^/]+\//, '')).add(ref);
|
|
68
|
+
}
|
|
69
|
+
for (const subject of git(root, ['log', '--all', '--merges', '--format=%s']).split('\n')) {
|
|
70
|
+
const from = subject.match(/from [^/\s]+\/(\S+)/)?.[1];
|
|
71
|
+
if (from) branches.add(from);
|
|
72
|
+
}
|
|
73
|
+
return { paths, branches };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The directory name Claude Code gives the records of a working directory. */
|
|
77
|
+
const dirNameOf = (path) => path.replace(/[/.]/g, '-');
|
|
78
|
+
|
|
79
|
+
const atOf = (entry) => entry.timestamp ?? entry.attachment?.timestamp ?? '';
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The entries of a record that fall on the days, read line by line: a record grows past what one
|
|
83
|
+
* string holds. The signs of whose record it is — the working directory and the branches — are read
|
|
84
|
+
* from every line.
|
|
85
|
+
*/
|
|
86
|
+
async function readRecordDays(file, days) {
|
|
87
|
+
const entries = [];
|
|
88
|
+
const cwds = new Set();
|
|
89
|
+
const branches = new Set();
|
|
90
|
+
const lines = createInterface({ input: createReadStream(file, 'utf8'), crlfDelay: Infinity });
|
|
91
|
+
for await (const line of lines) {
|
|
92
|
+
if (!line.trim()) continue;
|
|
93
|
+
let entry;
|
|
94
|
+
try {
|
|
95
|
+
entry = JSON.parse(line);
|
|
96
|
+
} catch {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (typeof entry.cwd === 'string') cwds.add(entry.cwd);
|
|
100
|
+
if (typeof entry.gitBranch === 'string') branches.add(entry.gitBranch);
|
|
101
|
+
if (days.has(atOf(entry).slice(0, 10))) entries.push(entry);
|
|
102
|
+
}
|
|
103
|
+
return { entries, cwds, branches };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The session files of the tree with their entries of the days: by the working directory, or by the branches. */
|
|
107
|
+
export async function recordsOf(root, projects, dayList) {
|
|
108
|
+
const { paths, branches } = treeOf(root);
|
|
109
|
+
const own = new Set([...paths].map(dirNameOf));
|
|
110
|
+
const days = new Set(dayList);
|
|
111
|
+
const since = Date.parse(`${dayList[0]}T00:00:00Z`);
|
|
112
|
+
const out = [];
|
|
113
|
+
if (!existsSync(projects)) return out;
|
|
114
|
+
for (const name of readdirSync(projects)) {
|
|
115
|
+
const dir = join(projects, name);
|
|
116
|
+
if (!statSync(dir).isDirectory()) continue;
|
|
117
|
+
for (const file of readdirSync(dir).filter((f) => f.endsWith('.jsonl'))) {
|
|
118
|
+
const path = join(dir, file);
|
|
119
|
+
if (statSync(path).mtimeMs < since) continue;
|
|
120
|
+
const read = await readRecordDays(path, days);
|
|
121
|
+
if (read.entries.length === 0) continue;
|
|
122
|
+
const mine =
|
|
123
|
+
own.has(name) ||
|
|
124
|
+
[...read.cwds].some((cwd) => [...paths].some((p) => cwd === p || cwd.startsWith(`${p}/`))) ||
|
|
125
|
+
[...read.branches].some((b) => b !== 'main' && b !== 'HEAD' && branches.has(b));
|
|
126
|
+
if (mine) out.push({ session: basename(file, '.jsonl'), entries: read.entries });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Recounts the days; returns per day what happened: `recounted N`, and `hook M` for the sessions the hook counted. */
|
|
133
|
+
export async function recount({ root, observations, days, projects, patterns }) {
|
|
134
|
+
const records = await recordsOf(root, projects, days);
|
|
135
|
+
const result = {};
|
|
136
|
+
mkdirSync(observations, { recursive: true });
|
|
137
|
+
for (const day of days) {
|
|
138
|
+
const file = join(observations, `${day}.jsonl`);
|
|
139
|
+
const lines = existsSync(file) ? readFileSync(file, 'utf8').split('\n').filter(Boolean) : [];
|
|
140
|
+
const parsed = lines.map((line) => {
|
|
141
|
+
try {
|
|
142
|
+
return JSON.parse(line);
|
|
143
|
+
} catch {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
const counted = new Set(parsed.filter((o) => o && typeof o.ev === 'string' && o.ev.startsWith('record-') && o.src !== 'recount').map((o) => o.sid));
|
|
148
|
+
const kept = lines.filter((_, i) => parsed[i]?.src !== 'recount');
|
|
149
|
+
const added = [];
|
|
150
|
+
let left = 0;
|
|
151
|
+
for (const { session, entries } of records) {
|
|
152
|
+
const ofDay = entries.filter((e) => atOf(e).startsWith(day));
|
|
153
|
+
if (ofDay.length === 0) continue;
|
|
154
|
+
const sid = cksum(session);
|
|
155
|
+
if (counted.has(sid)) {
|
|
156
|
+
left += 1;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const byDay = observationsOf(ofDay, { sid, patterns });
|
|
160
|
+
for (const line of byDay.get(day) ?? []) added.push(JSON.stringify({ ...JSON.parse(line), src: 'recount' }));
|
|
161
|
+
}
|
|
162
|
+
writeFileSync(file, [...kept, ...added].join('\n') + (kept.length + added.length > 0 ? '\n' : ''));
|
|
163
|
+
result[day] = left > 0 ? `recounted ${added.length}, hook ${left}` : `recounted ${added.length}`;
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The correction patterns, read from the hook that declares them, as the hook at the end of a turn reads them. */
|
|
169
|
+
export function patternsOf(root) {
|
|
170
|
+
const hook = join(root, '.claude', 'hooks', 'owner-corrections.sh');
|
|
171
|
+
if (!existsSync(hook)) return undefined;
|
|
172
|
+
return readFileSync(hook, 'utf8').match(/^RT_CORRECTION_PATTERNS='(.*)'$/m)?.[1];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function main(argv) {
|
|
176
|
+
const root = option(argv, '--tree') ?? process.cwd();
|
|
177
|
+
const observations = option(argv, '--observations') ?? join(root, '.claude', 'rt-kit', 'observations');
|
|
178
|
+
const day = option(argv, '--day');
|
|
179
|
+
const days = daysOf({ day, days: option(argv, '--days') ?? '1', until: option(argv, '--until') ?? new Date().toISOString().slice(0, 10) });
|
|
180
|
+
const projects = option(argv, '--projects') ?? join(homedir(), '.claude', 'projects');
|
|
181
|
+
const patterns = option(argv, '--patterns') ?? patternsOf(root);
|
|
182
|
+
const result = await recount({ root, observations, days, projects, patterns });
|
|
183
|
+
for (const [d, what] of Object.entries(result)) console.log(`${d}: ${what}`);
|
|
184
|
+
return 0;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (process.argv[1] && import.meta.url === new URL(`file://${process.argv[1]}`).href) main(process.argv.slice(2)).then((code) => process.exit(code));
|