@webpieces/ai-hook-rules 0.4.839 → 0.4.840
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 +12 -140
- package/package.json +7 -15
- package/src/adapters/hook-app-fixtures.d.ts +1 -1
- package/src/adapters/hook-app-fixtures.js +0 -8
- package/src/adapters/hook-app-fixtures.js.map +1 -1
- package/src/adapters/hook-core.d.ts +1 -21
- package/src/adapters/hook-core.js +22 -341
- package/src/adapters/hook-core.js.map +1 -1
- package/src/core/load-rules.d.ts +4 -39
- package/src/core/load-rules.js +15 -127
- package/src/core/load-rules.js.map +1 -1
- package/src/core/rules/catch-error-pattern.d.ts +3 -3
- package/src/core/rules/catch-error-pattern.js +13 -13
- package/src/core/rules/catch-error-pattern.js.map +1 -1
- package/src/core/rules/index.d.ts +1 -14
- package/src/core/rules/index.js +1 -17
- package/src/core/rules/index.js.map +1 -1
- package/src/core/rules/match-rule.d.ts +3 -3
- package/src/core/rules/match-rule.js +6 -6
- package/src/core/rules/match-rule.js.map +1 -1
- package/src/core/rules/max-file-lines.d.ts +3 -3
- package/src/core/rules/max-file-lines.js +7 -7
- package/src/core/rules/max-file-lines.js.map +1 -1
- package/src/core/rules/no-any-unknown.d.ts +3 -3
- package/src/core/rules/no-any-unknown.js +6 -6
- package/src/core/rules/no-any-unknown.js.map +1 -1
- package/src/core/rules/no-custom-css.d.ts +3 -3
- package/src/core/rules/no-custom-css.js +7 -7
- package/src/core/rules/no-custom-css.js.map +1 -1
- package/src/core/rules/no-destructure.d.ts +3 -3
- package/src/core/rules/no-destructure.js +7 -7
- package/src/core/rules/no-destructure.js.map +1 -1
- package/src/core/rules/no-implicit-any.d.ts +3 -3
- package/src/core/rules/no-implicit-any.js +6 -6
- package/src/core/rules/no-implicit-any.js.map +1 -1
- package/src/core/rules/no-js-files.d.ts +3 -3
- package/src/core/rules/no-js-files.js +6 -6
- package/src/core/rules/no-js-files.js.map +1 -1
- package/src/core/rules/no-process-exit-outside-main.d.ts +3 -3
- package/src/core/rules/no-process-exit-outside-main.js +7 -7
- package/src/core/rules/no-process-exit-outside-main.js.map +1 -1
- package/src/core/rules/no-symbol-di-tokens.d.ts +3 -3
- package/src/core/rules/no-symbol-di-tokens.js +7 -7
- package/src/core/rules/no-symbol-di-tokens.js.map +1 -1
- package/src/core/rules/no-unmanaged-exceptions.d.ts +3 -3
- package/src/core/rules/no-unmanaged-exceptions.js +9 -9
- package/src/core/rules/no-unmanaged-exceptions.js.map +1 -1
- package/src/core/rules/require-return-type.d.ts +3 -3
- package/src/core/rules/require-return-type.js +6 -6
- package/src/core/rules/require-return-type.js.map +1 -1
- package/src/core/rules/throw-cause-required.d.ts +3 -3
- package/src/core/rules/throw-cause-required.js +9 -9
- package/src/core/rules/throw-cause-required.js.map +1 -1
- package/src/core/rules/validate-ts-in-src.d.ts +3 -3
- package/src/core/rules/validate-ts-in-src.js +7 -7
- package/src/core/rules/validate-ts-in-src.js.map +1 -1
- package/src/core/runner.d.ts +10 -19
- package/src/core/runner.js +24 -622
- package/src/core/runner.js.map +1 -1
- package/src/index.d.ts +2 -15
- package/src/index.js +4 -54
- package/src/index.js.map +1 -1
- package/openclaw.plugin.json +0 -15
- package/src/adapters/agent-adapters.d.ts +0 -13
- package/src/adapters/agent-adapters.js +0 -23
- package/src/adapters/agent-adapters.js.map +0 -1
- package/src/adapters/agent-payload.d.ts +0 -48
- package/src/adapters/agent-payload.js +0 -30
- package/src/adapters/agent-payload.js.map +0 -1
- package/src/adapters/agent-response.d.ts +0 -5
- package/src/adapters/agent-response.js +0 -86
- package/src/adapters/agent-response.js.map +0 -1
- package/src/adapters/claude-code-adapter.d.ts +0 -26
- package/src/adapters/claude-code-adapter.js +0 -69
- package/src/adapters/claude-code-adapter.js.map +0 -1
- package/src/adapters/codex-adapter.d.ts +0 -19
- package/src/adapters/codex-adapter.js +0 -48
- package/src/adapters/codex-adapter.js.map +0 -1
- package/src/adapters/codex-subagent-guard.d.ts +0 -45
- package/src/adapters/codex-subagent-guard.js +0 -88
- package/src/adapters/codex-subagent-guard.js.map +0 -1
- package/src/adapters/detect-ai.d.ts +0 -70
- package/src/adapters/detect-ai.js +0 -82
- package/src/adapters/detect-ai.js.map +0 -1
- package/src/adapters/guards-hook.d.ts +0 -3
- package/src/adapters/guards-hook.js +0 -30
- package/src/adapters/guards-hook.js.map +0 -1
- package/src/adapters/openclaw-plugin.d.ts +0 -14
- package/src/adapters/openclaw-plugin.js +0 -87
- package/src/adapters/openclaw-plugin.js.map +0 -1
- package/src/adapters/review-identity-stamper.d.ts +0 -20
- package/src/adapters/review-identity-stamper.js +0 -34
- package/src/adapters/review-identity-stamper.js.map +0 -1
- package/src/bin/codex-trust.d.ts +0 -105
- package/src/bin/codex-trust.js +0 -205
- package/src/bin/codex-trust.js.map +0 -1
- package/src/bin/dev-hook-install.d.ts +0 -1
- package/src/bin/dev-hook-install.js +0 -82
- package/src/bin/dev-hook-install.js.map +0 -1
- package/src/bin/dev-hook-uninstall.d.ts +0 -1
- package/src/bin/dev-hook-uninstall.js +0 -45
- package/src/bin/dev-hook-uninstall.js.map +0 -1
- package/src/bin/hook-registration.d.ts +0 -429
- package/src/bin/hook-registration.js +0 -661
- package/src/bin/hook-registration.js.map +0 -1
- package/src/bin/install-entry.d.ts +0 -19
- package/src/bin/install-entry.js +0 -128
- package/src/bin/install-entry.js.map +0 -1
- package/src/bin/l0-allowlist.d.ts +0 -49
- package/src/bin/l0-allowlist.js +0 -528
- package/src/bin/l0-allowlist.js.map +0 -1
- package/src/bin/l0-codex-read.d.ts +0 -21
- package/src/bin/l0-codex-read.js +0 -109
- package/src/bin/l0-codex-read.js.map +0 -1
- package/src/bin/l0-decide.d.ts +0 -50
- package/src/bin/l0-decide.js +0 -103
- package/src/bin/l0-decide.js.map +0 -1
- package/src/bin/l0-entry.d.ts +0 -103
- package/src/bin/l0-entry.js +0 -98
- package/src/bin/l0-entry.js.map +0 -1
- package/src/bin/l0-ignored-tools.d.ts +0 -38
- package/src/bin/l0-ignored-tools.js +0 -44
- package/src/bin/l0-ignored-tools.js.map +0 -1
- package/src/bin/managed-env.d.ts +0 -46
- package/src/bin/managed-env.js +0 -50
- package/src/bin/managed-env.js.map +0 -1
- package/src/bin/neighbour-hooks.d.ts +0 -42
- package/src/bin/neighbour-hooks.js +0 -177
- package/src/bin/neighbour-hooks.js.map +0 -1
- package/src/bin/reviewer-agent.d.ts +0 -19
- package/src/bin/reviewer-agent.js +0 -135
- package/src/bin/reviewer-agent.js.map +0 -1
- package/src/bin/settings-shape.d.ts +0 -37
- package/src/bin/settings-shape.js +0 -17
- package/src/bin/settings-shape.js.map +0 -1
- package/src/bin/setup-config.d.ts +0 -19
- package/src/bin/setup-config.js +0 -370
- package/src/bin/setup-config.js.map +0 -1
- package/src/bin/setup.d.ts +0 -78
- package/src/bin/setup.js +0 -400
- package/src/bin/setup.js.map +0 -1
- package/src/bin/shim-audit-log.d.ts +0 -119
- package/src/bin/shim-audit-log.js +0 -286
- package/src/bin/shim-audit-log.js.map +0 -1
- package/src/bin/shim-deny-reason.d.ts +0 -1
- package/src/bin/shim-deny-reason.js +0 -244
- package/src/bin/shim-deny-reason.js.map +0 -1
- package/src/bin/shim-drift-fix.d.ts +0 -1
- package/src/bin/shim-drift-fix.js +0 -72
- package/src/bin/shim-drift-fix.js.map +0 -1
- package/src/bin/shim-testkit.d.ts +0 -107
- package/src/bin/shim-testkit.js +0 -197
- package/src/bin/shim-testkit.js.map +0 -1
- package/src/bin/shim-version-drift.d.ts +0 -1
- package/src/bin/shim-version-drift.js +0 -164
- package/src/bin/shim-version-drift.js.map +0 -1
- package/src/bin/shim.d.ts +0 -16
- package/src/bin/shim.js +0 -636
- package/src/bin/shim.js.map +0 -1
- package/src/bin/upgrade-shim.d.ts +0 -18
- package/src/bin/upgrade-shim.js +0 -283
- package/src/bin/upgrade-shim.js.map +0 -1
- package/src/core/ai-type-context.d.ts +0 -36
- package/src/core/ai-type-context.js +0 -45
- package/src/core/ai-type-context.js.map +0 -1
- package/src/core/apply-patch-parse.d.ts +0 -36
- package/src/core/apply-patch-parse.js +0 -154
- package/src/core/apply-patch-parse.js.map +0 -1
- package/src/core/build-context.d.ts +0 -10
- package/src/core/build-context.js +0 -66
- package/src/core/build-context.js.map +0 -1
- package/src/core/command-scan.d.ts +0 -122
- package/src/core/command-scan.js +0 -282
- package/src/core/command-scan.js.map +0 -1
- package/src/core/custom-rule-adapter.d.ts +0 -22
- package/src/core/custom-rule-adapter.js +0 -50
- package/src/core/custom-rule-adapter.js.map +0 -1
- package/src/core/decision-log.d.ts +0 -190
- package/src/core/decision-log.js +0 -406
- package/src/core/decision-log.js.map +0 -1
- package/src/core/delete-scoped-rules.d.ts +0 -8
- package/src/core/delete-scoped-rules.js +0 -31
- package/src/core/delete-scoped-rules.js.map +0 -1
- package/src/core/disable-directives.d.ts +0 -8
- package/src/core/disable-directives.js +0 -80
- package/src/core/disable-directives.js.map +0 -1
- package/src/core/effective-tree.d.ts +0 -204
- package/src/core/effective-tree.js +0 -277
- package/src/core/effective-tree.js.map +0 -1
- package/src/core/excluded-path-escape.d.ts +0 -101
- package/src/core/excluded-path-escape.js +0 -236
- package/src/core/excluded-path-escape.js.map +0 -1
- package/src/core/excluded-paths.d.ts +0 -23
- package/src/core/excluded-paths.js +0 -54
- package/src/core/excluded-paths.js.map +0 -1
- package/src/core/fix-hint.d.ts +0 -40
- package/src/core/fix-hint.js +0 -53
- package/src/core/fix-hint.js.map +0 -1
- package/src/core/force-to-root.d.ts +0 -32
- package/src/core/force-to-root.js +0 -60
- package/src/core/force-to-root.js.map +0 -1
- package/src/core/guard-index-doc.d.ts +0 -63
- package/src/core/guard-index-doc.js +0 -172
- package/src/core/guard-index-doc.js.map +0 -1
- package/src/core/instruct-ai-writer.d.ts +0 -1
- package/src/core/instruct-ai-writer.js +0 -6
- package/src/core/instruct-ai-writer.js.map +0 -1
- package/src/core/l0-fault-codes.d.ts +0 -85
- package/src/core/l0-fault-codes.js +0 -121
- package/src/core/l0-fault-codes.js.map +0 -1
- package/src/core/l0-matrix.d.ts +0 -114
- package/src/core/l0-matrix.js +0 -416
- package/src/core/l0-matrix.js.map +0 -1
- package/src/core/l0-tooling-doc.d.ts +0 -45
- package/src/core/l0-tooling-doc.js +0 -258
- package/src/core/l0-tooling-doc.js.map +0 -1
- package/src/core/l1-doc.d.ts +0 -7
- package/src/core/l1-doc.js +0 -340
- package/src/core/l1-doc.js.map +0 -1
- package/src/core/l1-matrix-doc.d.ts +0 -31
- package/src/core/l1-matrix-doc.js +0 -111
- package/src/core/l1-matrix-doc.js.map +0 -1
- package/src/core/l1-rows.d.ts +0 -167
- package/src/core/l1-rows.js +0 -262
- package/src/core/l1-rows.js.map +0 -1
- package/src/core/l2-doc.d.ts +0 -7
- package/src/core/l2-doc.js +0 -385
- package/src/core/l2-doc.js.map +0 -1
- package/src/core/l2-matrix-doc.d.ts +0 -23
- package/src/core/l2-matrix-doc.js +0 -72
- package/src/core/l2-matrix-doc.js.map +0 -1
- package/src/core/l2-row.d.ts +0 -54
- package/src/core/l2-row.js +0 -75
- package/src/core/l2-row.js.map +0 -1
- package/src/core/l2-rows.d.ts +0 -56
- package/src/core/l2-rows.js +0 -272
- package/src/core/l2-rows.js.map +0 -1
- package/src/core/load-config.d.ts +0 -1
- package/src/core/load-config.js +0 -9
- package/src/core/load-config.js.map +0 -1
- package/src/core/log-stream.d.ts +0 -117
- package/src/core/log-stream.js +0 -156
- package/src/core/log-stream.js.map +0 -1
- package/src/core/log-streams.d.ts +0 -48
- package/src/core/log-streams.js +0 -55
- package/src/core/log-streams.js.map +0 -1
- package/src/core/log-target.d.ts +0 -21
- package/src/core/log-target.js +0 -29
- package/src/core/log-target.js.map +0 -1
- package/src/core/main-sync-log.d.ts +0 -16
- package/src/core/main-sync-log.js +0 -105
- package/src/core/main-sync-log.js.map +0 -1
- package/src/core/main-sync-refresh.d.ts +0 -39
- package/src/core/main-sync-refresh.js +0 -96
- package/src/core/main-sync-refresh.js.map +0 -1
- package/src/core/main-sync-timeout.d.ts +0 -32
- package/src/core/main-sync-timeout.js +0 -94
- package/src/core/main-sync-timeout.js.map +0 -1
- package/src/core/matrix-cures.d.ts +0 -12
- package/src/core/matrix-cures.js +0 -56
- package/src/core/matrix-cures.js.map +0 -1
- package/src/core/missing-directory.d.ts +0 -38
- package/src/core/missing-directory.js +0 -66
- package/src/core/missing-directory.js.map +0 -1
- package/src/core/read-only-inspection.d.ts +0 -34
- package/src/core/read-only-inspection.js +0 -88
- package/src/core/read-only-inspection.js.map +0 -1
- package/src/core/rejection-log.d.ts +0 -9
- package/src/core/rejection-log.js +0 -258
- package/src/core/rejection-log.js.map +0 -1
- package/src/core/report.d.ts +0 -21
- package/src/core/report.js +0 -73
- package/src/core/report.js.map +0 -1
- package/src/core/rule-base.d.ts +0 -39
- package/src/core/rule-base.js +0 -36
- package/src/core/rule-base.js.map +0 -1
- package/src/core/rules/branch-creation-guard.d.ts +0 -113
- package/src/core/rules/branch-creation-guard.js +0 -422
- package/src/core/rules/branch-creation-guard.js.map +0 -1
- package/src/core/rules/branch-switch-scan.d.ts +0 -43
- package/src/core/rules/branch-switch-scan.js +0 -132
- package/src/core/rules/branch-switch-scan.js.map +0 -1
- package/src/core/rules/build-output-pipe-guard.d.ts +0 -56
- package/src/core/rules/build-output-pipe-guard.js +0 -120
- package/src/core/rules/build-output-pipe-guard.js.map +0 -1
- package/src/core/rules/build-output-pipe-scan.d.ts +0 -47
- package/src/core/rules/build-output-pipe-scan.js +0 -84
- package/src/core/rules/build-output-pipe-scan.js.map +0 -1
- package/src/core/rules/cap-remedies.d.ts +0 -39
- package/src/core/rules/cap-remedies.js +0 -59
- package/src/core/rules/cap-remedies.js.map +0 -1
- package/src/core/rules/commit-message-substitution-guard.d.ts +0 -72
- package/src/core/rules/commit-message-substitution-guard.js +0 -134
- package/src/core/rules/commit-message-substitution-guard.js.map +0 -1
- package/src/core/rules/commit-message-substitution-scan.d.ts +0 -42
- package/src/core/rules/commit-message-substitution-scan.js +0 -145
- package/src/core/rules/commit-message-substitution-scan.js.map +0 -1
- package/src/core/rules/content-read-scan.d.ts +0 -76
- package/src/core/rules/content-read-scan.js +0 -176
- package/src/core/rules/content-read-scan.js.map +0 -1
- package/src/core/rules/cure-prefix-scan.d.ts +0 -64
- package/src/core/rules/cure-prefix-scan.js +0 -133
- package/src/core/rules/cure-prefix-scan.js.map +0 -1
- package/src/core/rules/feature-branch-guard.d.ts +0 -121
- package/src/core/rules/feature-branch-guard.js +0 -326
- package/src/core/rules/feature-branch-guard.js.map +0 -1
- package/src/core/rules/judged-tree.d.ts +0 -66
- package/src/core/rules/judged-tree.js +0 -97
- package/src/core/rules/judged-tree.js.map +0 -1
- package/src/core/rules/main-freshness.d.ts +0 -37
- package/src/core/rules/main-freshness.js +0 -61
- package/src/core/rules/main-freshness.js.map +0 -1
- package/src/core/rules/merge-in-progress-guard.d.ts +0 -17
- package/src/core/rules/merge-in-progress-guard.js +0 -122
- package/src/core/rules/merge-in-progress-guard.js.map +0 -1
- package/src/core/rules/merged-branch-bash-guard.d.ts +0 -62
- package/src/core/rules/merged-branch-bash-guard.js +0 -170
- package/src/core/rules/merged-branch-bash-guard.js.map +0 -1
- package/src/core/rules/merged-branch-message.d.ts +0 -59
- package/src/core/rules/merged-branch-message.js +0 -137
- package/src/core/rules/merged-branch-message.js.map +0 -1
- package/src/core/rules/pr-creation-or-push-guard.d.ts +0 -26
- package/src/core/rules/pr-creation-or-push-guard.js +0 -146
- package/src/core/rules/pr-creation-or-push-guard.js.map +0 -1
- package/src/core/rules/pr-merge-guard.d.ts +0 -36
- package/src/core/rules/pr-merge-guard.js +0 -86
- package/src/core/rules/pr-merge-guard.js.map +0 -1
- package/src/core/rules/read-stale-guard.d.ts +0 -136
- package/src/core/rules/read-stale-guard.js +0 -326
- package/src/core/rules/read-stale-guard.js.map +0 -1
- package/src/core/rules/recovery-allowlist.d.ts +0 -49
- package/src/core/rules/recovery-allowlist.js +0 -187
- package/src/core/rules/recovery-allowlist.js.map +0 -1
- package/src/core/rules/redirect-how-to-merge-main.d.ts +0 -16
- package/src/core/rules/redirect-how-to-merge-main.js +0 -157
- package/src/core/rules/redirect-how-to-merge-main.js.map +0 -1
- package/src/core/rules/shell-segment-scan.d.ts +0 -64
- package/src/core/rules/shell-segment-scan.js +0 -88
- package/src/core/rules/shell-segment-scan.js.map +0 -1
- package/src/core/rules/stale-main-bash-guard.d.ts +0 -198
- package/src/core/rules/stale-main-bash-guard.js +0 -361
- package/src/core/rules/stale-main-bash-guard.js.map +0 -1
- package/src/core/rules/stale-main-message.d.ts +0 -62
- package/src/core/rules/stale-main-message.js +0 -102
- package/src/core/rules/stale-main-message.js.map +0 -1
- package/src/core/rules/tree-recovery.d.ts +0 -92
- package/src/core/rules/tree-recovery.js +0 -165
- package/src/core/rules/tree-recovery.js.map +0 -1
- package/src/core/rules/wait-spin-guard.d.ts +0 -128
- package/src/core/rules/wait-spin-guard.js +0 -211
- package/src/core/rules/wait-spin-guard.js.map +0 -1
- package/src/core/rules/wait-spin-scan.d.ts +0 -118
- package/src/core/rules/wait-spin-scan.js +0 -203
- package/src/core/rules/wait-spin-scan.js.map +0 -1
- package/src/core/rules/whole-repo-build-guard.d.ts +0 -125
- package/src/core/rules/whole-repo-build-guard.js +0 -257
- package/src/core/rules/whole-repo-build-guard.js.map +0 -1
- package/src/core/rules/whole-repo-build-scan.d.ts +0 -28
- package/src/core/rules/whole-repo-build-scan.js +0 -198
- package/src/core/rules/whole-repo-build-scan.js.map +0 -1
- package/src/core/session-call-history.d.ts +0 -44
- package/src/core/session-call-history.js +0 -79
- package/src/core/session-call-history.js.map +0 -1
- package/src/core/shell-read-parity.d.ts +0 -56
- package/src/core/shell-read-parity.js +0 -155
- package/src/core/shell-read-parity.js.map +0 -1
- package/src/core/strip-ts-noise.d.ts +0 -1
- package/src/core/strip-ts-noise.js +0 -185
- package/src/core/strip-ts-noise.js.map +0 -1
- package/src/core/sync-main.d.ts +0 -46
- package/src/core/sync-main.js +0 -175
- package/src/core/sync-main.js.map +0 -1
- package/src/core/target-tree.d.ts +0 -84
- package/src/core/target-tree.js +0 -148
- package/src/core/target-tree.js.map +0 -1
- package/src/core/to-error.d.ts +0 -5
- package/src/core/to-error.js +0 -37
- package/src/core/to-error.js.map +0 -1
- package/src/core/types.d.ts +0 -152
- package/src/core/types.js +0 -200
- package/src/core/types.js.map +0 -1
- package/src/core/version-sync.d.ts +0 -219
- package/src/core/version-sync.js +0 -558
- package/src/core/version-sync.js.map +0 -1
- package/src/core/webpieces-versions.d.ts +0 -98
- package/src/core/webpieces-versions.js +0 -206
- package/src/core/webpieces-versions.js.map +0 -1
- package/templates/ai-hook.sh +0 -497
- package/templates/claude-settings-hook.json +0 -28
- package/templates/webpieces-reviewer.md +0 -60
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ShellReadParity = exports.SED_RANGE_BODY = exports.READ_COMMANDS = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const fs = tslib_1.__importStar(require("fs"));
|
|
6
|
-
const path = tslib_1.__importStar(require("path"));
|
|
7
|
-
/**
|
|
8
|
-
* READ PARITY, and it is a CODEX-ONLY surface.
|
|
9
|
-
*
|
|
10
|
-
* Claude Code has a first-class `Read` tool, so the read-scoped guard (read-stale-guard) and the
|
|
11
|
-
* `calls/` audit trail see every file the agent opens. Codex has no such tool: a file read arrives as
|
|
12
|
-
* `Bash` running `sed -n '1,240p' <path>` (measured). Without this, every Codex read is invisible to
|
|
13
|
-
* the read guard and to the audit log, and the two harnesses cannot be compared at all.
|
|
14
|
-
*
|
|
15
|
-
* The caller gates this on `aiType === 'codex'`. Nothing here is reachable from a Claude Code payload,
|
|
16
|
-
* and a spec pins that.
|
|
17
|
-
*
|
|
18
|
-
* DELIBERATELY CONSERVATIVE, because a false positive here is not a missed read — it is a Read GUARD
|
|
19
|
-
* verdict applied to a command that was never a read, i.e. a new way to block work:
|
|
20
|
-
*
|
|
21
|
-
* - ONE command only. Any `;` `&&` `||` `|` backtick `$(` or redirect and the answer is "not a read";
|
|
22
|
-
* the command still runs the ordinary bash guards, exactly as today.
|
|
23
|
-
* - `argv[0]` must be one of the six pure pagers, or `sed -n '<range>p'`.
|
|
24
|
-
* - EVERY non-flag argument must resolve to a file that exists inside the tree. `cat /etc/passwd` is
|
|
25
|
-
* not a read of this repo, and neither is a heredoc.
|
|
26
|
-
*
|
|
27
|
-
* A match does NOT replace the bash guards — the caller runs BOTH, and either may deny.
|
|
28
|
-
*/
|
|
29
|
-
exports.READ_COMMANDS = new Set(['cat', 'head', 'tail', 'less', 'more', 'bat']);
|
|
30
|
-
/** Shell syntax that makes a command more than one command, or redirects it. Any hit ⇒ not a read. */
|
|
31
|
-
const NOT_ONE_COMMAND = /[;|&`<>]|\$\(/;
|
|
32
|
-
/** Flags that consume the FOLLOWING token as their value, so it is never mistaken for a file. */
|
|
33
|
-
const VALUE_FLAGS = new Set(['-n', '-c', '-b', '-e', '-f']);
|
|
34
|
-
/**
|
|
35
|
-
* The only `sed` script shape that is a read: a line range printed with `-n` — as a regex BODY, so the
|
|
36
|
-
* L0 allowlist's ERE twin can be built from the same characters instead of retyping them.
|
|
37
|
-
*
|
|
38
|
-
* EXPORTED for `bin/l0-allowlist.ts`. The L0 entry that lets a Codex session read its way out of an L0
|
|
39
|
-
* fault has to mean the same thing as this module or the two definitions of "read-shaped" drift, and a
|
|
40
|
-
* drifted L0 entry is either a hole or a deadlock. It cannot literally CALL this module — L0's sh half
|
|
41
|
-
* has no JS at all — so the shared thing is the vocabulary, and `codex-l0-read.spec.ts` asserts the two
|
|
42
|
-
* agree over a corpus.
|
|
43
|
-
*/
|
|
44
|
-
exports.SED_RANGE_BODY = '[0-9]+(,[0-9]+)?p';
|
|
45
|
-
const SED_RANGE = new RegExp('^' + exports.SED_RANGE_BODY + '$');
|
|
46
|
-
class ShellReadParity {
|
|
47
|
-
/**
|
|
48
|
-
* The absolute paths this command reads, or an empty list when it is not a read-shaped command.
|
|
49
|
-
* `root` is the tree the read must fall inside; a path outside it is not this repo's read.
|
|
50
|
-
*/
|
|
51
|
-
readTargets(command, cwd, root) {
|
|
52
|
-
const trimmed = command.trim();
|
|
53
|
-
if (trimmed === '' || NOT_ONE_COMMAND.test(trimmed))
|
|
54
|
-
return [];
|
|
55
|
-
const tokens = this.tokenize(trimmed);
|
|
56
|
-
if (tokens === null || tokens.length < 2)
|
|
57
|
-
return [];
|
|
58
|
-
const argv0 = tokens[0];
|
|
59
|
-
const rest = argv0 === 'sed' ? this.sedOperands(tokens.slice(1)) : (exports.READ_COMMANDS.has(argv0) ? tokens.slice(1) : null);
|
|
60
|
-
if (rest === null)
|
|
61
|
-
return [];
|
|
62
|
-
const files = [];
|
|
63
|
-
let index = 0;
|
|
64
|
-
while (index < rest.length) {
|
|
65
|
-
const token = rest[index];
|
|
66
|
-
if (token.startsWith('-') && token !== '-') {
|
|
67
|
-
if (VALUE_FLAGS.has(token))
|
|
68
|
-
index += 1;
|
|
69
|
-
index += 1;
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
const resolved = path.resolve(cwd, token);
|
|
73
|
-
if (!this.isFileInTree(resolved, root))
|
|
74
|
-
return [];
|
|
75
|
-
files.push(resolved);
|
|
76
|
-
index += 1;
|
|
77
|
-
}
|
|
78
|
-
return files;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* `sed`'s operands, or null when this `sed` invocation is not a plain range print. `-n` and a
|
|
82
|
-
* single `<range>p` script are BOTH required: without `-n` sed echoes and edits, and any other
|
|
83
|
-
* script is a transformation, not a read.
|
|
84
|
-
*/
|
|
85
|
-
sedOperands(args) {
|
|
86
|
-
if (!args.includes('-n'))
|
|
87
|
-
return null;
|
|
88
|
-
const scripts = args.filter((a) => !a.startsWith('-'));
|
|
89
|
-
if (scripts.length < 2)
|
|
90
|
-
return null;
|
|
91
|
-
if (!SED_RANGE.test(scripts[0]))
|
|
92
|
-
return null;
|
|
93
|
-
return scripts.slice(1);
|
|
94
|
-
}
|
|
95
|
-
isFileInTree(resolved, root) {
|
|
96
|
-
const rootWithSep = root.endsWith(path.sep) ? root : root + path.sep;
|
|
97
|
-
if (!resolved.startsWith(rootWithSep))
|
|
98
|
-
return false;
|
|
99
|
-
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
100
|
-
try {
|
|
101
|
-
return fs.statSync(resolved).isFile();
|
|
102
|
-
}
|
|
103
|
-
catch (err) {
|
|
104
|
-
// Nothing is swallowed here: a token we cannot stat is simply NOT a read target, which is an
|
|
105
|
-
// ANSWER, and the command still runs the ordinary bash guards either way. The commented-out
|
|
106
|
-
// toError() call below is the form `catch-error-pattern` requires for a deliberately ignored
|
|
107
|
-
// error — it is the rule's spelling of "ignored on purpose", not a disabled line.
|
|
108
|
-
//const error = toError(err);
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Split on whitespace, honouring single and double quotes. Returns null on an unterminated quote —
|
|
114
|
-
* a command we cannot read confidently is never a read.
|
|
115
|
-
*
|
|
116
|
-
* There is no escape or expansion handling here on purpose: `NOT_ONE_COMMAND` has already refused
|
|
117
|
-
* everything carrying shell syntax, so what reaches this is a literal argv.
|
|
118
|
-
*/
|
|
119
|
-
tokenize(command) {
|
|
120
|
-
const tokens = [];
|
|
121
|
-
let current = '';
|
|
122
|
-
let quote = '';
|
|
123
|
-
let started = false;
|
|
124
|
-
for (const ch of command) {
|
|
125
|
-
if (quote !== '') {
|
|
126
|
-
if (ch === quote)
|
|
127
|
-
quote = '';
|
|
128
|
-
else
|
|
129
|
-
current += ch;
|
|
130
|
-
continue;
|
|
131
|
-
}
|
|
132
|
-
if (ch === '\'' || ch === '"') {
|
|
133
|
-
quote = ch;
|
|
134
|
-
started = true;
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
if (ch === ' ' || ch === '\t' || ch === '\n') {
|
|
138
|
-
if (started)
|
|
139
|
-
tokens.push(current);
|
|
140
|
-
current = '';
|
|
141
|
-
started = false;
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
current += ch;
|
|
145
|
-
started = true;
|
|
146
|
-
}
|
|
147
|
-
if (quote !== '')
|
|
148
|
-
return null;
|
|
149
|
-
if (started)
|
|
150
|
-
tokens.push(current);
|
|
151
|
-
return tokens;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
exports.ShellReadParity = ShellReadParity;
|
|
155
|
-
//# sourceMappingURL=shell-read-parity.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"shell-read-parity.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/core/shell-read-parity.ts"],"names":[],"mappings":";;;;AAAA,+CAAyB;AACzB,mDAA6B;AAE7B;;;;;;;;;;;;;;;;;;;;;GAqBG;AACU,QAAA,aAAa,GAAwB,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;AAE1G,sGAAsG;AACtG,MAAM,eAAe,GAAG,eAAe,CAAC;AAExC,iGAAiG;AACjG,MAAM,WAAW,GAAwB,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAEjF;;;;;;;;;GASG;AACU,QAAA,cAAc,GAAG,mBAAmB,CAAC;AAElD,MAAM,SAAS,GAAG,IAAI,MAAM,CAAC,GAAG,GAAG,sBAAc,GAAG,GAAG,CAAC,CAAC;AAEzD,MAAa,eAAe;IACxB;;;OAGG;IACH,WAAW,CAAC,OAAe,EAAE,GAAW,EAAE,IAAY;QAClD,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;QAC/B,IAAI,OAAO,KAAK,EAAE,IAAI,eAAe,CAAC,IAAI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;QACf,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,KAAK,MAAM,EAAE,IAAI,OAAO,EAAE,CAAC;YACvB,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;gBACf,IAAI,EAAE,KAAK,KAAK;oBAAE,KAAK,GAAG,EAAE,CAAC;;oBACxB,OAAO,IAAI,EAAE,CAAC;gBACnB,SAAS;YACb,CAAC;YACD,IAAI,EAAE,KAAK,IAAI,IAAI,EAAE,KAAK,GAAG,EAAE,CAAC;gBAAC,KAAK,GAAG,EAAE,CAAC;gBAAC,OAAO,GAAG,IAAI,CAAC;gBAAC,SAAS;YAAC,CAAC;YACxE,IAAI,EAAE,KAAK,GAAG,IAAI,EAAE,KAAK,IAAI,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC;gBAC3C,IAAI,OAAO;oBAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBAClC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,GAAG,KAAK,CAAC;gBAChB,SAAS;YACb,CAAC;YACD,OAAO,IAAI,EAAE,CAAC;YACd,OAAO,GAAG,IAAI,CAAC;QACnB,CAAC;QACD,IAAI,KAAK,KAAK,EAAE;YAAE,OAAO,IAAI,CAAC;QAC9B,IAAI,OAAO;YAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAClC,OAAO,MAAM,CAAC;IAClB,CAAC;CACJ;AA7FD,0CA6FC","sourcesContent":["import * as fs from 'fs';\nimport * as path from 'path';\n\n/**\n * READ PARITY, and it is a CODEX-ONLY surface.\n *\n * Claude Code has a first-class `Read` tool, so the read-scoped guard (read-stale-guard) and the\n * `calls/` audit trail see every file the agent opens. Codex has no such tool: a file read arrives as\n * `Bash` running `sed -n '1,240p' <path>` (measured). Without this, every Codex read is invisible to\n * the read guard and to the audit log, and the two harnesses cannot be compared at all.\n *\n * The caller gates this on `aiType === 'codex'`. Nothing here is reachable from a Claude Code payload,\n * and a spec pins that.\n *\n * DELIBERATELY CONSERVATIVE, because a false positive here is not a missed read — it is a Read GUARD\n * verdict applied to a command that was never a read, i.e. a new way to block work:\n *\n * - ONE command only. Any `;` `&&` `||` `|` backtick `$(` or redirect and the answer is \"not a read\";\n * the command still runs the ordinary bash guards, exactly as today.\n * - `argv[0]` must be one of the six pure pagers, or `sed -n '<range>p'`.\n * - EVERY non-flag argument must resolve to a file that exists inside the tree. `cat /etc/passwd` is\n * not a read of this repo, and neither is a heredoc.\n *\n * A match does NOT replace the bash guards — the caller runs BOTH, and either may deny.\n */\nexport const READ_COMMANDS: ReadonlySet<string> = new Set(['cat', 'head', 'tail', 'less', 'more', 'bat']);\n\n/** Shell syntax that makes a command more than one command, or redirects it. Any hit ⇒ not a read. */\nconst NOT_ONE_COMMAND = /[;|&`<>]|\\$\\(/;\n\n/** Flags that consume the FOLLOWING token as their value, so it is never mistaken for a file. */\nconst VALUE_FLAGS: ReadonlySet<string> = new Set(['-n', '-c', '-b', '-e', '-f']);\n\n/**\n * The only `sed` script shape that is a read: a line range printed with `-n` — as a regex BODY, so the\n * L0 allowlist's ERE twin can be built from the same characters instead of retyping them.\n *\n * EXPORTED for `bin/l0-allowlist.ts`. The L0 entry that lets a Codex session read its way out of an L0\n * fault has to mean the same thing as this module or the two definitions of \"read-shaped\" drift, and a\n * drifted L0 entry is either a hole or a deadlock. It cannot literally CALL this module — L0's sh half\n * has no JS at all — so the shared thing is the vocabulary, and `codex-l0-read.spec.ts` asserts the two\n * agree over a corpus.\n */\nexport const SED_RANGE_BODY = '[0-9]+(,[0-9]+)?p';\n\nconst SED_RANGE = new RegExp('^' + SED_RANGE_BODY + '$');\n\nexport class ShellReadParity {\n /**\n * The absolute paths this command reads, or an empty list when it is not a read-shaped command.\n * `root` is the tree the read must fall inside; a path outside it is not this repo's read.\n */\n readTargets(command: string, cwd: string, root: string): readonly string[] {\n const trimmed = command.trim();\n if (trimmed === '' || NOT_ONE_COMMAND.test(trimmed)) return [];\n const tokens = this.tokenize(trimmed);\n if (tokens === null || tokens.length < 2) return [];\n\n const argv0 = tokens[0];\n const rest = argv0 === 'sed' ? this.sedOperands(tokens.slice(1)) : (READ_COMMANDS.has(argv0) ? tokens.slice(1) : null);\n if (rest === null) return [];\n\n const files: string[] = [];\n let index = 0;\n while (index < rest.length) {\n const token = rest[index];\n if (token.startsWith('-') && token !== '-') {\n if (VALUE_FLAGS.has(token)) index += 1;\n index += 1;\n continue;\n }\n const resolved = path.resolve(cwd, token);\n if (!this.isFileInTree(resolved, root)) return [];\n files.push(resolved);\n index += 1;\n }\n return files;\n }\n\n /**\n * `sed`'s operands, or null when this `sed` invocation is not a plain range print. `-n` and a\n * single `<range>p` script are BOTH required: without `-n` sed echoes and edits, and any other\n * script is a transformation, not a read.\n */\n private sedOperands(args: readonly string[]): readonly string[] | null {\n if (!args.includes('-n')) return null;\n const scripts = args.filter((a: string): boolean => !a.startsWith('-'));\n if (scripts.length < 2) return null;\n if (!SED_RANGE.test(scripts[0])) return null;\n return scripts.slice(1);\n }\n\n private isFileInTree(resolved: string, root: string): boolean {\n const rootWithSep = root.endsWith(path.sep) ? root : root + path.sep;\n if (!resolved.startsWith(rootWithSep)) return false;\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return fs.statSync(resolved).isFile();\n } catch (err: unknown) {\n // Nothing is swallowed here: a token we cannot stat is simply NOT a read target, which is an\n // ANSWER, and the command still runs the ordinary bash guards either way. The commented-out\n // toError() call below is the form `catch-error-pattern` requires for a deliberately ignored\n // error — it is the rule's spelling of \"ignored on purpose\", not a disabled line.\n //const error = toError(err);\n return false;\n }\n }\n\n /**\n * Split on whitespace, honouring single and double quotes. Returns null on an unterminated quote —\n * a command we cannot read confidently is never a read.\n *\n * There is no escape or expansion handling here on purpose: `NOT_ONE_COMMAND` has already refused\n * everything carrying shell syntax, so what reaches this is a literal argv.\n */\n private tokenize(command: string): readonly string[] | null {\n const tokens: string[] = [];\n let current = '';\n let quote = '';\n let started = false;\n for (const ch of command) {\n if (quote !== '') {\n if (ch === quote) quote = '';\n else current += ch;\n continue;\n }\n if (ch === '\\'' || ch === '\"') { quote = ch; started = true; continue; }\n if (ch === ' ' || ch === '\\t' || ch === '\\n') {\n if (started) tokens.push(current);\n current = '';\n started = false;\n continue;\n }\n current += ch;\n started = true;\n }\n if (quote !== '') return null;\n if (started) tokens.push(current);\n return tokens;\n }\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function stripTsNoise(source: string): string;
|
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stripTsNoise = stripTsNoise;
|
|
4
|
-
class StripState {
|
|
5
|
-
source;
|
|
6
|
-
len;
|
|
7
|
-
out;
|
|
8
|
-
stack;
|
|
9
|
-
state;
|
|
10
|
-
braceDepth;
|
|
11
|
-
i;
|
|
12
|
-
constructor(source) {
|
|
13
|
-
this.source = source;
|
|
14
|
-
this.len = source.length;
|
|
15
|
-
this.out = new Array(source.length);
|
|
16
|
-
this.stack = [];
|
|
17
|
-
this.state = 'code';
|
|
18
|
-
this.braceDepth = 0;
|
|
19
|
-
this.i = 0;
|
|
20
|
-
}
|
|
21
|
-
ch() { return this.source[this.i]; }
|
|
22
|
-
next() { return this.i + 1 < this.len ? this.source[this.i + 1] : ''; }
|
|
23
|
-
emit(idx, ch) { this.out[idx] = ch; }
|
|
24
|
-
blank(idx, ch) { this.out[idx] = ch === '\n' ? '\n' : ' '; }
|
|
25
|
-
pushState(newState) {
|
|
26
|
-
this.stack.push({ state: this.state, braceDepth: this.braceDepth });
|
|
27
|
-
this.state = newState;
|
|
28
|
-
}
|
|
29
|
-
popState() {
|
|
30
|
-
const frame = this.stack.pop();
|
|
31
|
-
this.state = frame.state;
|
|
32
|
-
this.braceDepth = frame.braceDepth;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function handleCodeOrInterp(s) {
|
|
36
|
-
const ch = s.ch();
|
|
37
|
-
const next = s.next();
|
|
38
|
-
if (s.state === 'templateInterp') {
|
|
39
|
-
if (ch === '{') {
|
|
40
|
-
s.braceDepth += 1;
|
|
41
|
-
s.emit(s.i, ch);
|
|
42
|
-
s.i += 1;
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
if (ch === '}') {
|
|
46
|
-
if (s.braceDepth === 0) {
|
|
47
|
-
s.emit(s.i, ch);
|
|
48
|
-
s.i += 1;
|
|
49
|
-
s.popState();
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
s.braceDepth -= 1;
|
|
53
|
-
s.emit(s.i, ch);
|
|
54
|
-
s.i += 1;
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (ch === '/' && next === '/') {
|
|
59
|
-
s.emit(s.i, '/');
|
|
60
|
-
s.emit(s.i + 1, '/');
|
|
61
|
-
s.i += 2;
|
|
62
|
-
s.pushState('lineComment');
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (ch === '/' && next === '*') {
|
|
66
|
-
s.emit(s.i, '/');
|
|
67
|
-
s.emit(s.i + 1, '*');
|
|
68
|
-
s.i += 2;
|
|
69
|
-
s.pushState('blockComment');
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
if (ch === '"') {
|
|
73
|
-
s.emit(s.i, '"');
|
|
74
|
-
s.i += 1;
|
|
75
|
-
s.pushState('dquote');
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
if (ch === "'") {
|
|
79
|
-
s.emit(s.i, "'");
|
|
80
|
-
s.i += 1;
|
|
81
|
-
s.pushState('squote');
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
if (ch === '`') {
|
|
85
|
-
s.emit(s.i, '`');
|
|
86
|
-
s.i += 1;
|
|
87
|
-
s.pushState('template');
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
s.emit(s.i, ch);
|
|
91
|
-
s.i += 1;
|
|
92
|
-
}
|
|
93
|
-
function handleLineComment(s) {
|
|
94
|
-
if (s.ch() === '\n') {
|
|
95
|
-
s.emit(s.i, '\n');
|
|
96
|
-
s.i += 1;
|
|
97
|
-
s.popState();
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
s.blank(s.i, s.ch());
|
|
101
|
-
s.i += 1;
|
|
102
|
-
}
|
|
103
|
-
function handleBlockComment(s) {
|
|
104
|
-
if (s.ch() === '*' && s.next() === '/') {
|
|
105
|
-
s.emit(s.i, '*');
|
|
106
|
-
s.emit(s.i + 1, '/');
|
|
107
|
-
s.i += 2;
|
|
108
|
-
s.popState();
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
s.blank(s.i, s.ch());
|
|
112
|
-
s.i += 1;
|
|
113
|
-
}
|
|
114
|
-
function handleStringLiteral(s, quoteChar) {
|
|
115
|
-
const ch = s.ch();
|
|
116
|
-
if (ch === '\\' && s.i + 1 < s.len) {
|
|
117
|
-
s.blank(s.i, ch);
|
|
118
|
-
s.blank(s.i + 1, s.source[s.i + 1]);
|
|
119
|
-
s.i += 2;
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
if (ch === quoteChar) {
|
|
123
|
-
s.emit(s.i, quoteChar);
|
|
124
|
-
s.i += 1;
|
|
125
|
-
s.popState();
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
s.blank(s.i, ch);
|
|
129
|
-
s.i += 1;
|
|
130
|
-
}
|
|
131
|
-
function handleTemplate(s) {
|
|
132
|
-
const ch = s.ch();
|
|
133
|
-
if (ch === '\\' && s.i + 1 < s.len) {
|
|
134
|
-
s.blank(s.i, ch);
|
|
135
|
-
s.blank(s.i + 1, s.source[s.i + 1]);
|
|
136
|
-
s.i += 2;
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
if (ch === '`') {
|
|
140
|
-
s.emit(s.i, '`');
|
|
141
|
-
s.i += 1;
|
|
142
|
-
s.popState();
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
if (ch === '$' && s.next() === '{') {
|
|
146
|
-
s.emit(s.i, '$');
|
|
147
|
-
s.emit(s.i + 1, '{');
|
|
148
|
-
s.i += 2;
|
|
149
|
-
s.stack.push({ state: 'template', braceDepth: 0 });
|
|
150
|
-
s.state = 'templateInterp';
|
|
151
|
-
s.braceDepth = 0;
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
s.blank(s.i, ch);
|
|
155
|
-
s.i += 1;
|
|
156
|
-
}
|
|
157
|
-
function stripTsNoise(source) {
|
|
158
|
-
const s = new StripState(source);
|
|
159
|
-
while (s.i < s.len) {
|
|
160
|
-
if (s.state === 'code' || s.state === 'templateInterp') {
|
|
161
|
-
handleCodeOrInterp(s);
|
|
162
|
-
}
|
|
163
|
-
else if (s.state === 'lineComment') {
|
|
164
|
-
handleLineComment(s);
|
|
165
|
-
}
|
|
166
|
-
else if (s.state === 'blockComment') {
|
|
167
|
-
handleBlockComment(s);
|
|
168
|
-
}
|
|
169
|
-
else if (s.state === 'dquote') {
|
|
170
|
-
handleStringLiteral(s, '"');
|
|
171
|
-
}
|
|
172
|
-
else if (s.state === 'squote') {
|
|
173
|
-
handleStringLiteral(s, "'");
|
|
174
|
-
}
|
|
175
|
-
else if (s.state === 'template') {
|
|
176
|
-
handleTemplate(s);
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
s.emit(s.i, s.ch());
|
|
180
|
-
s.i += 1;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return s.out.join('');
|
|
184
|
-
}
|
|
185
|
-
//# sourceMappingURL=strip-ts-noise.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"strip-ts-noise.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/core/strip-ts-noise.ts"],"names":[],"mappings":";;AA0FA,oCAYC;AAjGD,MAAM,UAAU;IACH,MAAM,CAAS;IACf,GAAG,CAAS;IACZ,GAAG,CAAW;IACd,K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AAC;aAC1D,IAAI,CAAC,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YAAC,mBAAmB,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QAAC,CAAC;aAC1D,IAAI,CAAC,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YAAC,mBAAmB,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QAAC,CAAC;aAC1D,IAAI,CAAC,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;YAAC,cAAc,CAAC,CAAC,CAAC,CAAC;QAAC,CAAC;aAClD,CAAC;YAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QAAC,CAAC;IAC3C,CAAC;IACD,OAAO,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC1B,CAAC","sourcesContent":["interface StackFrame {\n readonly state: string;\n readonly braceDepth: number;\n}\n\nclass StripState {\n readonly source: string;\n readonly len: number;\n readonly out: string[];\n readonly stack: StackFrame[];\n state: string;\n braceDepth: number;\n i: number;\n\n constructor(source: string) {\n this.source = source;\n this.len = source.length;\n this.out = new Array<string>(source.length);\n this.stack = [];\n this.state = 'code';\n this.braceDepth = 0;\n this.i = 0;\n }\n\n ch(): string { return this.source[this.i]; }\n next(): string { return this.i + 1 < this.len ? this.source[this.i + 1] : ''; }\n emit(idx: number, ch: string): void { this.out[idx] = ch; }\n blank(idx: number, ch: string): void { this.out[idx] = ch === '\\n' ? '\\n' : ' '; }\n\n pushState(newState: string): void {\n this.stack.push({ state: this.state, braceDepth: this.braceDepth });\n this.state = newState;\n }\n\n popState(): void {\n const frame = this.stack.pop()!;\n this.state = frame.state;\n this.braceDepth = frame.braceDepth;\n }\n}\n\nfunction handleCodeOrInterp(s: StripState): void {\n const ch = s.ch();\n const next = s.next();\n\n if (s.state === 'templateInterp') {\n if (ch === '{') { s.braceDepth += 1; s.emit(s.i, ch); s.i += 1; return; }\n if (ch === '}') {\n if (s.braceDepth === 0) { s.emit(s.i, ch); s.i += 1; s.popState(); return; }\n s.braceDepth -= 1; s.emit(s.i, ch); s.i += 1; return;\n }\n }\n if (ch === '/' && next === '/') { s.emit(s.i, '/'); s.emit(s.i + 1, '/'); s.i += 2; s.pushState('lineComment'); return; }\n if (ch === '/' && next === '*') { s.emit(s.i, '/'); s.emit(s.i + 1, '*'); s.i += 2; s.pushState('blockComment'); return; }\n if (ch === '\"') { s.emit(s.i, '\"'); s.i += 1; s.pushState('dquote'); return; }\n if (ch === \"'\") { s.emit(s.i, \"'\"); s.i += 1; s.pushState('squote'); return; }\n if (ch === '`') { s.emit(s.i, '`'); s.i += 1; s.pushState('template'); return; }\n s.emit(s.i, ch); s.i += 1;\n}\n\nfunction handleLineComment(s: StripState): void {\n if (s.ch() === '\\n') { s.emit(s.i, '\\n'); s.i += 1; s.popState(); return; }\n s.blank(s.i, s.ch()); s.i += 1;\n}\n\nfunction handleBlockComment(s: StripState): void {\n if (s.ch() === '*' && s.next() === '/') { s.emit(s.i, '*'); s.emit(s.i + 1, '/'); s.i += 2; s.popState(); return; }\n s.blank(s.i, s.ch()); s.i += 1;\n}\n\nfunction handleStringLiteral(s: StripState, quoteChar: string): void {\n const ch = s.ch();\n if (ch === '\\\\' && s.i + 1 < s.len) { s.blank(s.i, ch); s.blank(s.i + 1, s.source[s.i + 1]); s.i += 2; return; }\n if (ch === quoteChar) { s.emit(s.i, quoteChar); s.i += 1; s.popState(); return; }\n s.blank(s.i, ch); s.i += 1;\n}\n\nfunction handleTemplate(s: StripState): void {\n const ch = s.ch();\n if (ch === '\\\\' && s.i + 1 < s.len) { s.blank(s.i, ch); s.blank(s.i + 1, s.source[s.i + 1]); s.i += 2; return; }\n if (ch === '`') { s.emit(s.i, '`'); s.i += 1; s.popState(); return; }\n if (ch === '$' && s.next() === '{') {\n s.emit(s.i, '$'); s.emit(s.i + 1, '{'); s.i += 2;\n s.stack.push({ state: 'template', braceDepth: 0 });\n s.state = 'templateInterp'; s.braceDepth = 0;\n return;\n }\n s.blank(s.i, ch); s.i += 1;\n}\n\nexport function stripTsNoise(source: string): string {\n const s = new StripState(source);\n while (s.i < s.len) {\n if (s.state === 'code' || s.state === 'templateInterp') { handleCodeOrInterp(s); }\n else if (s.state === 'lineComment') { handleLineComment(s); }\n else if (s.state === 'blockComment') { handleBlockComment(s); }\n else if (s.state === 'dquote') { handleStringLiteral(s, '\"'); }\n else if (s.state === 'squote') { handleStringLiteral(s, \"'\"); }\n else if (s.state === 'template') { handleTemplate(s); }\n else { s.emit(s.i, s.ch()); s.i += 1; }\n }\n return s.out.join('');\n}\n"]}
|
package/src/core/sync-main.d.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { StreamIdentity } from './log-stream';
|
|
2
|
-
/**
|
|
3
|
-
* The detached, fire-and-forget refresher spawned (by file path, not a bin) from
|
|
4
|
-
* main-sync-refresh.ts. It does the SLOW work (merged-PR lookup + git fetch + merge-base +
|
|
5
|
-
* same-file-overlap) and writes `.webpieces/main-sync-status.json` so the next hook call reads it
|
|
6
|
-
* instantly. Nobody reads our exit code or output — we run after the spawning hook has returned.
|
|
7
|
-
*
|
|
8
|
-
* Concurrency: a lock file (`.webpieces/main-sync.lock.json`) holds `inprocess`/`finished` + a start
|
|
9
|
-
* epoch + the holder's pid, taken with an ATOMIC exclusive create. If another refresher already holds
|
|
10
|
-
* it and is alive, we exit immediately (don't pile up `git fetch`es). If the holder is finished, dead,
|
|
11
|
-
* or older than hangTimeoutMinutes, the lock is reclaimed and we proceed.
|
|
12
|
-
*
|
|
13
|
-
* argv: [, , repoRoot, hangTimeoutMinutes, sessionId, agentId, hook]
|
|
14
|
-
*
|
|
15
|
-
* The last three are the SPAWNER's LogStream identity, and adopting them here is the whole reason
|
|
16
|
-
* they are passed. This process has its own fresh module-level `logStream`; left unidentified it
|
|
17
|
-
* named every line `unknown-coordinator-hook.log` — one shared file that every
|
|
18
|
-
* agent's refresher child appended to concurrently (the PIPE_BUF tearing LogStream exists to remove),
|
|
19
|
-
* and which held this run's START/FINISH while the parent's SPAWN_ATTEMPT sat in a different file
|
|
20
|
-
* entirely. Adopting the parent's identity puts one refresh cycle back in one stream. Absent argv
|
|
21
|
-
* (a hand-run of this file) leaves the default, which still prefixes.
|
|
22
|
-
*/
|
|
23
|
-
export declare function main(): void;
|
|
24
|
-
/**
|
|
25
|
-
* Parse the hang timeout off argv, WITHOUT `||`.
|
|
26
|
-
*
|
|
27
|
-
* `Number(argv[3]) || DEFAULT` silently rewrote a configured `0` — "never treat a lock as stale" — into
|
|
28
|
-
* the 5-minute default, because `0` is falsy. The knob now has exactly one reader (the branch-state
|
|
29
|
-
* policy entry), so a value that survives the config only to be discarded at the process boundary is
|
|
30
|
-
* the whole knob being a lie. Only a MISSING or non-numeric argument falls back.
|
|
31
|
-
*/
|
|
32
|
-
export declare function hangTimeoutFromArgv(raw: string | undefined): number;
|
|
33
|
-
/**
|
|
34
|
-
* The spawner's LogStream identity as it arrives on this child's argv — positions 4/5/6, which is
|
|
35
|
-
* where `triggerMainSyncRefresh` puts them (its own array is offset by two: node + script path).
|
|
36
|
-
*
|
|
37
|
-
* Exported so ONE test can pin BOTH ends of the contract at once. The bug this closes is precisely a
|
|
38
|
-
* disagreement between two files about argv positions, and a test that only checks the reader would
|
|
39
|
-
* have stayed green through it.
|
|
40
|
-
*/
|
|
41
|
-
export declare function spawnerIdentity(argv: string[]): StreamIdentity;
|
|
42
|
-
/**
|
|
43
|
-
* The refresh itself, argv-free so it can be driven by a test. `main()` is only the argv parse — a
|
|
44
|
-
* test importing another module's `main` is what no-process-exit-outside-main exists to prevent.
|
|
45
|
-
*/
|
|
46
|
-
export declare function refreshMainSync(repoRoot: string, hangTimeoutMinutes: number, argvDetail?: string): void;
|
package/src/core/sync-main.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.main = main;
|
|
4
|
-
exports.hangTimeoutFromArgv = hangTimeoutFromArgv;
|
|
5
|
-
exports.spawnerIdentity = spawnerIdentity;
|
|
6
|
-
exports.refreshMainSync = refreshMainSync;
|
|
7
|
-
const repo_workflow_core_1 = require("@webpieces/repo-workflow-core");
|
|
8
|
-
const rules_config_1 = require("@webpieces/rules-config");
|
|
9
|
-
const to_error_1 = require("./to-error");
|
|
10
|
-
const main_sync_log_1 = require("./main-sync-log");
|
|
11
|
-
const log_stream_1 = require("./log-stream");
|
|
12
|
-
/**
|
|
13
|
-
* The detached, fire-and-forget refresher spawned (by file path, not a bin) from
|
|
14
|
-
* main-sync-refresh.ts. It does the SLOW work (merged-PR lookup + git fetch + merge-base +
|
|
15
|
-
* same-file-overlap) and writes `.webpieces/main-sync-status.json` so the next hook call reads it
|
|
16
|
-
* instantly. Nobody reads our exit code or output — we run after the spawning hook has returned.
|
|
17
|
-
*
|
|
18
|
-
* Concurrency: a lock file (`.webpieces/main-sync.lock.json`) holds `inprocess`/`finished` + a start
|
|
19
|
-
* epoch + the holder's pid, taken with an ATOMIC exclusive create. If another refresher already holds
|
|
20
|
-
* it and is alive, we exit immediately (don't pile up `git fetch`es). If the holder is finished, dead,
|
|
21
|
-
* or older than hangTimeoutMinutes, the lock is reclaimed and we proceed.
|
|
22
|
-
*
|
|
23
|
-
* argv: [, , repoRoot, hangTimeoutMinutes, sessionId, agentId, hook]
|
|
24
|
-
*
|
|
25
|
-
* The last three are the SPAWNER's LogStream identity, and adopting them here is the whole reason
|
|
26
|
-
* they are passed. This process has its own fresh module-level `logStream`; left unidentified it
|
|
27
|
-
* named every line `unknown-coordinator-hook.log` — one shared file that every
|
|
28
|
-
* agent's refresher child appended to concurrently (the PIPE_BUF tearing LogStream exists to remove),
|
|
29
|
-
* and which held this run's START/FINISH while the parent's SPAWN_ATTEMPT sat in a different file
|
|
30
|
-
* entirely. Adopting the parent's identity puts one refresh cycle back in one stream. Absent argv
|
|
31
|
-
* (a hand-run of this file) leaves the default, which still prefixes.
|
|
32
|
-
*/
|
|
33
|
-
function main() {
|
|
34
|
-
log_stream_1.logStream.identify(spawnerIdentity(process.argv));
|
|
35
|
-
refreshMainSync(process.argv[2] ?? process.cwd(), hangTimeoutFromArgv(process.argv[3]), process.argv.slice(2).join(' '));
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Parse the hang timeout off argv, WITHOUT `||`.
|
|
39
|
-
*
|
|
40
|
-
* `Number(argv[3]) || DEFAULT` silently rewrote a configured `0` — "never treat a lock as stale" — into
|
|
41
|
-
* the 5-minute default, because `0` is falsy. The knob now has exactly one reader (the branch-state
|
|
42
|
-
* policy entry), so a value that survives the config only to be discarded at the process boundary is
|
|
43
|
-
* the whole knob being a lie. Only a MISSING or non-numeric argument falls back.
|
|
44
|
-
*/
|
|
45
|
-
// webpieces-disable no-function-outside-class -- argv parse for this detached main(), matching the file's existing shape
|
|
46
|
-
function hangTimeoutFromArgv(raw) {
|
|
47
|
-
if (raw === undefined || raw.trim() === '')
|
|
48
|
-
return repo_workflow_core_1.DEFAULT_HANG_TIMEOUT_MINUTES;
|
|
49
|
-
const parsed = Number(raw);
|
|
50
|
-
return Number.isFinite(parsed) ? parsed : repo_workflow_core_1.DEFAULT_HANG_TIMEOUT_MINUTES;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* The spawner's LogStream identity as it arrives on this child's argv — positions 4/5/6, which is
|
|
54
|
-
* where `triggerMainSyncRefresh` puts them (its own array is offset by two: node + script path).
|
|
55
|
-
*
|
|
56
|
-
* Exported so ONE test can pin BOTH ends of the contract at once. The bug this closes is precisely a
|
|
57
|
-
* disagreement between two files about argv positions, and a test that only checks the reader would
|
|
58
|
-
* have stayed green through it.
|
|
59
|
-
*/
|
|
60
|
-
// webpieces-disable no-function-outside-class -- argv parse for this detached main(), matching the file's existing shape
|
|
61
|
-
function spawnerIdentity(argv) {
|
|
62
|
-
return new log_stream_1.StreamIdentity(argv[4] ?? '', argv[5] ?? '', argv[6] ?? 'hook');
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* The refresh itself, argv-free so it can be driven by a test. `main()` is only the argv parse — a
|
|
66
|
-
* test importing another module's `main` is what no-process-exit-outside-main exists to prevent.
|
|
67
|
-
*/
|
|
68
|
-
// webpieces-disable no-function-outside-class -- module-level entry point of this detached refresher, matching the file's existing shape
|
|
69
|
-
function refreshMainSync(repoRoot, hangTimeoutMinutes, argvDetail = '') {
|
|
70
|
-
const startedMs = Date.now();
|
|
71
|
-
// First action: prove the detached child actually started. If the async-refresh stream has no START line
|
|
72
|
-
// for a spawn, the child never launched (or died before this point).
|
|
73
|
-
(0, main_sync_log_1.logSyncEvent)(repoRoot, new main_sync_log_1.SyncLogEvent('START', process.pid, '-', `argv=${argvDetail}`));
|
|
74
|
-
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
75
|
-
try {
|
|
76
|
-
// ONE atomic acquire — not a check followed by a write. The old check-then-write pair had a
|
|
77
|
-
// gap in which two detached children could both pass the check and then both run `git fetch`.
|
|
78
|
-
const lock = (0, repo_workflow_core_1.tryAcquireMainSyncLock)(repoRoot, hangTimeoutMinutes);
|
|
79
|
-
if (!lock) {
|
|
80
|
-
(0, main_sync_log_1.logSyncEvent)(repoRoot, new main_sync_log_1.SyncLogEvent('SKIP_INPROGRESS', process.pid, '-', 'another refresh is in progress'));
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
84
|
-
try {
|
|
85
|
-
// EVERY worktree's branch, not just ours. The cache is shared by the whole repo but used
|
|
86
|
-
// to describe only the branch of whichever tree won this lock, so every other worktree's
|
|
87
|
-
// guards read a cross-branch cache and abstained. One fetch + one `gh` call now arms all
|
|
88
|
-
// of them; the map is rebuilt from the live worktree set, so dead branches drop out.
|
|
89
|
-
const file = (0, repo_workflow_core_1.computeAllMainSyncStatuses)(repoRoot);
|
|
90
|
-
(0, repo_workflow_core_1.writeMainSyncStatusFile)(repoRoot, file);
|
|
91
|
-
// Second slow signal, same lock, same detached run: which local branches are dead. One bulk
|
|
92
|
-
// `gh pr list --state merged` call. The branch-creation-guard reads the result to enforce its
|
|
93
|
-
// cap without ever touching the network itself. Deliberately allowed to go stale.
|
|
94
|
-
const mergedBranches = new repo_workflow_core_1.MergedBranchesService();
|
|
95
|
-
// `false`: the background refresher NEVER ignores a standing agent lock. That flag is
|
|
96
|
-
// wp-cleanup's, and it means "a human asked for this run" — nothing an unattended
|
|
97
|
-
// refresher can claim on somebody's behalf.
|
|
98
|
-
const cache = mergedBranches.computeMergedBranches(repoRoot, false);
|
|
99
|
-
mergedBranches.writeMergedBranches(repoRoot, cache);
|
|
100
|
-
// Third step, same detached run: actually DELETE the dead branches. Reporting them was
|
|
101
|
-
// never enough — the reap was only ever a `git branch -D` string in a fix hint, which an
|
|
102
|
-
// agent reads as destructive and stalls on, so nothing was ever cleaned. Here nobody has
|
|
103
|
-
// to be asked. Reuses the verdicts we JUST computed (no second `gh` call).
|
|
104
|
-
const reaped = autoReap(repoRoot, cache);
|
|
105
|
-
// FINISH after a successful write — START-without-FINISH means we were killed mid-run.
|
|
106
|
-
const names = Object.keys(file.branches);
|
|
107
|
-
(0, main_sync_log_1.logSyncEvent)(repoRoot, new main_sync_log_1.SyncLogEvent('FINISH', process.pid, names.join(','), `branches=${String(names.length)} ${names.map(summarize(file)).join(' ')} deletableBranches=${String(cache.deletable.length)} reaped=${String(reaped)} ms=${String(Date.now() - startedMs)}`));
|
|
108
|
-
}
|
|
109
|
-
finally {
|
|
110
|
-
// Always flip the lock off so a compute failure can't wedge the guard until the
|
|
111
|
-
// staleness reclaim kicks in.
|
|
112
|
-
(0, repo_workflow_core_1.writeMainSyncLock)(repoRoot, (0, repo_workflow_core_1.finishedLock)(lock.started));
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
catch (err) {
|
|
116
|
-
const error = (0, to_error_1.toError)(err);
|
|
117
|
-
// Detached: swallow so a transient git/fs error never leaves poison state (the next hook call
|
|
118
|
-
// spawns a fresh refresher) — but record WHY it died so the failure isn't invisible.
|
|
119
|
-
(0, main_sync_log_1.logSyncEvent)(repoRoot, new main_sync_log_1.SyncLogEvent('ERROR', process.pid, '-', `${error.message} | ${error.stack ?? ''}`));
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* One `branch(merged/forkPoint/conflict)` token per recorded branch, for the FINISH log line. The log
|
|
124
|
-
* is the only evidence that a given worktree's branch was actually armed by a refresh, so it has to
|
|
125
|
-
* name every branch written, not just the tree the refresher happened to run in.
|
|
126
|
-
*/
|
|
127
|
-
// webpieces-disable no-function-outside-class -- module-level log formatter of this detached main(), matching the file's existing shape
|
|
128
|
-
function summarize(file) {
|
|
129
|
-
return (branch) => {
|
|
130
|
-
const status = file.branches[branch];
|
|
131
|
-
if (status === undefined)
|
|
132
|
-
return `${branch}(?)`;
|
|
133
|
-
return `${branch}(merged=${String(status.branchAlreadyMerged)}:${status.mergedPr} fork=${String(status.hasForkPoint)} conflict=${String(status.conflict)})`;
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Delete the branches the verdicts just declared dead. Returns how many actually went.
|
|
138
|
-
*
|
|
139
|
-
* WHY it is safe to do this unattended: every candidate is provably dead (merged PR / squash backup
|
|
140
|
-
* of a merged branch / zero commits of its own), `main` and any worktree-held branch are excluded
|
|
141
|
-
* upstream, and each delete is logged with the branch's pre-delete SHA plus the exact command that
|
|
142
|
-
* restores it. WHY it is safe to do it HERE: this refresher already recomputed those verdicts on
|
|
143
|
-
* this very run, so it is acting on evidence seconds old, not on the deliberately-stale cache file.
|
|
144
|
-
*
|
|
145
|
-
* Swallows everything. We are detached and fire-and-forget: cleanup failing must never damage the
|
|
146
|
-
* main-sync status this process exists to produce — but every failure is logged, because a silent
|
|
147
|
-
* background deletion is exactly what nobody should have to trust.
|
|
148
|
-
*/
|
|
149
|
-
// webpieces-disable no-function-outside-class -- module-level helper of this detached main(), matching the file's existing shape
|
|
150
|
-
function autoReap(repoRoot, cache) {
|
|
151
|
-
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
152
|
-
try {
|
|
153
|
-
const config = (0, rules_config_1.loadAndValidate)(repoRoot).rulesConfig['branch-creation-guard'];
|
|
154
|
-
// Strictly opt-IN: only an explicit `true` reaps. `autoReapMergedBranches` is schema-required,
|
|
155
|
-
// so every validated config states an answer — which means "absent" here is not a consumer
|
|
156
|
-
// who wants the default, it is a config that never passed validation. Deleting branches on
|
|
157
|
-
// that basis would be deleting on a preference nobody expressed.
|
|
158
|
-
if (config?.mode === 'OFF' || config?.autoReapMergedBranches !== true)
|
|
159
|
-
return 0;
|
|
160
|
-
const result = new repo_workflow_core_1.BranchReaper().reap(repoRoot, 'auto-reap', cache);
|
|
161
|
-
for (const failure of result.failed) {
|
|
162
|
-
(0, main_sync_log_1.logSyncEvent)(repoRoot, new main_sync_log_1.SyncLogEvent('ERROR', process.pid, failure.branch, `reap failed: ${failure.error}`));
|
|
163
|
-
}
|
|
164
|
-
return result.reaped.length;
|
|
165
|
-
}
|
|
166
|
-
catch (err) {
|
|
167
|
-
const error = (0, to_error_1.toError)(err);
|
|
168
|
-
(0, main_sync_log_1.logSyncEvent)(repoRoot, new main_sync_log_1.SyncLogEvent('ERROR', process.pid, '-', `autoReap: ${error.message}`));
|
|
169
|
-
return 0;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
if (require.main === module) {
|
|
173
|
-
main();
|
|
174
|
-
}
|
|
175
|
-
//# sourceMappingURL=sync-main.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sync-main.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/core/sync-main.ts"],"names":[],"mappings":";;AA4BA,oBAOC;AAWD,kDAIC;AAWD,0CAEC;AAOD,0CA0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KAAK,MAAM,EAAE,CAAC;IAC1B,IAAI,EAAE,CAAC;AACX,CAAC","sourcesContent":["import { BranchReaper, DEFAULT_HANG_TIMEOUT_MINUTES, MainSyncStatusFile, MergedBranchesCache, MergedBranchesService, ReapResult, computeAllMainSyncStatuses, writeMainSyncStatusFile, writeMainSyncLock, tryAcquireMainSyncLock, finishedLock } from '@webpieces/repo-workflow-core';\nimport { loadAndValidate } from '@webpieces/rules-config';\n\nimport { toError } from './to-error';\nimport { logSyncEvent, SyncLogEvent } from './main-sync-log';\nimport { logStream, StreamIdentity } from './log-stream';\n\n/**\n * The detached, fire-and-forget refresher spawned (by file path, not a bin) from\n * main-sync-refresh.ts. It does the SLOW work (merged-PR lookup + git fetch + merge-base +\n * same-file-overlap) and writes `.webpieces/main-sync-status.json` so the next hook call reads it\n * instantly. Nobody reads our exit code or output — we run after the spawning hook has returned.\n *\n * Concurrency: a lock file (`.webpieces/main-sync.lock.json`) holds `inprocess`/`finished` + a start\n * epoch + the holder's pid, taken with an ATOMIC exclusive create. If another refresher already holds\n * it and is alive, we exit immediately (don't pile up `git fetch`es). If the holder is finished, dead,\n * or older than hangTimeoutMinutes, the lock is reclaimed and we proceed.\n *\n * argv: [, , repoRoot, hangTimeoutMinutes, sessionId, agentId, hook]\n *\n * The last three are the SPAWNER's LogStream identity, and adopting them here is the whole reason\n * they are passed. This process has its own fresh module-level `logStream`; left unidentified it\n * named every line `unknown-coordinator-hook.log` — one shared file that every\n * agent's refresher child appended to concurrently (the PIPE_BUF tearing LogStream exists to remove),\n * and which held this run's START/FINISH while the parent's SPAWN_ATTEMPT sat in a different file\n * entirely. Adopting the parent's identity puts one refresh cycle back in one stream. Absent argv\n * (a hand-run of this file) leaves the default, which still prefixes.\n */\nexport function main(): void {\n logStream.identify(spawnerIdentity(process.argv));\n refreshMainSync(\n process.argv[2] ?? process.cwd(),\n hangTimeoutFromArgv(process.argv[3]),\n process.argv.slice(2).join(' '),\n );\n}\n\n/**\n * Parse the hang timeout off argv, WITHOUT `||`.\n *\n * `Number(argv[3]) || DEFAULT` silently rewrote a configured `0` — \"never treat a lock as stale\" — into\n * the 5-minute default, because `0` is falsy. The knob now has exactly one reader (the branch-state\n * policy entry), so a value that survives the config only to be discarded at the process boundary is\n * the whole knob being a lie. Only a MISSING or non-numeric argument falls back.\n */\n// webpieces-disable no-function-outside-class -- argv parse for this detached main(), matching the file's existing shape\nexport function hangTimeoutFromArgv(raw: string | undefined): number {\n if (raw === undefined || raw.trim() === '') return DEFAULT_HANG_TIMEOUT_MINUTES;\n const parsed = Number(raw);\n return Number.isFinite(parsed) ? parsed : DEFAULT_HANG_TIMEOUT_MINUTES;\n}\n\n/**\n * The spawner's LogStream identity as it arrives on this child's argv — positions 4/5/6, which is\n * where `triggerMainSyncRefresh` puts them (its own array is offset by two: node + script path).\n *\n * Exported so ONE test can pin BOTH ends of the contract at once. The bug this closes is precisely a\n * disagreement between two files about argv positions, and a test that only checks the reader would\n * have stayed green through it.\n */\n// webpieces-disable no-function-outside-class -- argv parse for this detached main(), matching the file's existing shape\nexport function spawnerIdentity(argv: string[]): StreamIdentity {\n return new StreamIdentity(argv[4] ?? '', argv[5] ?? '', argv[6] ?? 'hook');\n}\n\n/**\n * The refresh itself, argv-free so it can be driven by a test. `main()` is only the argv parse — a\n * test importing another module's `main` is what no-process-exit-outside-main exists to prevent.\n */\n// webpieces-disable no-function-outside-class -- module-level entry point of this detached refresher, matching the file's existing shape\nexport function refreshMainSync(repoRoot: string, hangTimeoutMinutes: number, argvDetail: string = ''): void {\n const startedMs = Date.now();\n\n // First action: prove the detached child actually started. If the async-refresh stream has no START line\n // for a spawn, the child never launched (or died before this point).\n logSyncEvent(repoRoot, new SyncLogEvent('START', process.pid, '-', `argv=${argvDetail}`));\n\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n // ONE atomic acquire — not a check followed by a write. The old check-then-write pair had a\n // gap in which two detached children could both pass the check and then both run `git fetch`.\n const lock = tryAcquireMainSyncLock(repoRoot, hangTimeoutMinutes);\n if (!lock) {\n logSyncEvent(repoRoot, new SyncLogEvent('SKIP_INPROGRESS', process.pid, '-', 'another refresh is in progress'));\n return;\n }\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n // EVERY worktree's branch, not just ours. The cache is shared by the whole repo but used\n // to describe only the branch of whichever tree won this lock, so every other worktree's\n // guards read a cross-branch cache and abstained. One fetch + one `gh` call now arms all\n // of them; the map is rebuilt from the live worktree set, so dead branches drop out.\n const file = computeAllMainSyncStatuses(repoRoot);\n writeMainSyncStatusFile(repoRoot, file);\n\n // Second slow signal, same lock, same detached run: which local branches are dead. One bulk\n // `gh pr list --state merged` call. The branch-creation-guard reads the result to enforce its\n // cap without ever touching the network itself. Deliberately allowed to go stale.\n const mergedBranches = new MergedBranchesService();\n // `false`: the background refresher NEVER ignores a standing agent lock. That flag is\n // wp-cleanup's, and it means \"a human asked for this run\" — nothing an unattended\n // refresher can claim on somebody's behalf.\n const cache = mergedBranches.computeMergedBranches(repoRoot, false);\n mergedBranches.writeMergedBranches(repoRoot, cache);\n\n // Third step, same detached run: actually DELETE the dead branches. Reporting them was\n // never enough — the reap was only ever a `git branch -D` string in a fix hint, which an\n // agent reads as destructive and stalls on, so nothing was ever cleaned. Here nobody has\n // to be asked. Reuses the verdicts we JUST computed (no second `gh` call).\n const reaped = autoReap(repoRoot, cache);\n\n // FINISH after a successful write — START-without-FINISH means we were killed mid-run.\n const names = Object.keys(file.branches);\n logSyncEvent(repoRoot, new SyncLogEvent(\n 'FINISH', process.pid, names.join(','),\n `branches=${String(names.length)} ${names.map(summarize(file)).join(' ')} deletableBranches=${String(cache.deletable.length)} reaped=${String(reaped)} ms=${String(Date.now() - startedMs)}`,\n ));\n } finally {\n // Always flip the lock off so a compute failure can't wedge the guard until the\n // staleness reclaim kicks in.\n writeMainSyncLock(repoRoot, finishedLock(lock.started));\n }\n } catch (err: unknown) {\n const error = toError(err);\n // Detached: swallow so a transient git/fs error never leaves poison state (the next hook call\n // spawns a fresh refresher) — but record WHY it died so the failure isn't invisible.\n logSyncEvent(repoRoot, new SyncLogEvent('ERROR', process.pid, '-', `${error.message} | ${error.stack ?? ''}`));\n }\n}\n\n/**\n * One `branch(merged/forkPoint/conflict)` token per recorded branch, for the FINISH log line. The log\n * is the only evidence that a given worktree's branch was actually armed by a refresh, so it has to\n * name every branch written, not just the tree the refresher happened to run in.\n */\n// webpieces-disable no-function-outside-class -- module-level log formatter of this detached main(), matching the file's existing shape\nfunction summarize(file: MainSyncStatusFile): (branch: string) => string {\n return (branch: string): string => {\n const status = file.branches[branch];\n if (status === undefined) return `${branch}(?)`;\n return `${branch}(merged=${String(status.branchAlreadyMerged)}:${status.mergedPr} fork=${String(status.hasForkPoint)} conflict=${String(status.conflict)})`;\n };\n}\n\n/**\n * Delete the branches the verdicts just declared dead. Returns how many actually went.\n *\n * WHY it is safe to do this unattended: every candidate is provably dead (merged PR / squash backup\n * of a merged branch / zero commits of its own), `main` and any worktree-held branch are excluded\n * upstream, and each delete is logged with the branch's pre-delete SHA plus the exact command that\n * restores it. WHY it is safe to do it HERE: this refresher already recomputed those verdicts on\n * this very run, so it is acting on evidence seconds old, not on the deliberately-stale cache file.\n *\n * Swallows everything. We are detached and fire-and-forget: cleanup failing must never damage the\n * main-sync status this process exists to produce — but every failure is logged, because a silent\n * background deletion is exactly what nobody should have to trust.\n */\n// webpieces-disable no-function-outside-class -- module-level helper of this detached main(), matching the file's existing shape\nfunction autoReap(repoRoot: string, cache: MergedBranchesCache): number {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const config = loadAndValidate(repoRoot).rulesConfig['branch-creation-guard'];\n // Strictly opt-IN: only an explicit `true` reaps. `autoReapMergedBranches` is schema-required,\n // so every validated config states an answer — which means \"absent\" here is not a consumer\n // who wants the default, it is a config that never passed validation. Deleting branches on\n // that basis would be deleting on a preference nobody expressed.\n if (config?.mode === 'OFF' || config?.autoReapMergedBranches !== true) return 0;\n\n const result: ReapResult = new BranchReaper().reap(repoRoot, 'auto-reap', cache);\n for (const failure of result.failed) {\n logSyncEvent(repoRoot, new SyncLogEvent(\n 'ERROR', process.pid, failure.branch, `reap failed: ${failure.error}`));\n }\n return result.reaped.length;\n } catch (err: unknown) {\n const error = toError(err);\n logSyncEvent(repoRoot, new SyncLogEvent('ERROR', process.pid, '-', `autoReap: ${error.message}`));\n return 0;\n }\n}\n\nif (require.main === module) {\n main();\n}\n"]}
|