@webpieces/agent-workflow-rules 0.0.0-stage → 0.0.1
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 +23 -2
- package/package.json +45 -4
- package/src/adapters/agent-response.d.ts +5 -0
- package/src/adapters/agent-response.js +86 -0
- package/src/adapters/agent-response.js.map +1 -0
- package/src/adapters/codex-subagent-guard.d.ts +45 -0
- package/src/adapters/codex-subagent-guard.js +88 -0
- package/src/adapters/codex-subagent-guard.js.map +1 -0
- package/src/adapters/guards-hook.d.ts +3 -0
- package/src/adapters/guards-hook.js +30 -0
- package/src/adapters/guards-hook.js.map +1 -0
- package/src/adapters/hook-app-fixtures.d.ts +68 -0
- package/src/adapters/hook-app-fixtures.js +170 -0
- package/src/adapters/hook-app-fixtures.js.map +1 -0
- package/src/adapters/hook-core.d.ts +22 -0
- package/src/adapters/hook-core.js +359 -0
- package/src/adapters/hook-core.js.map +1 -0
- package/src/adapters/hook-pipeline-evaluator.d.ts +5 -0
- package/src/adapters/hook-pipeline-evaluator.js +18 -0
- package/src/adapters/hook-pipeline-evaluator.js.map +1 -0
- package/src/adapters/review-identity-stamper.d.ts +20 -0
- package/src/adapters/review-identity-stamper.js +34 -0
- package/src/adapters/review-identity-stamper.js.map +1 -0
- package/src/bin/codex-trust.d.ts +105 -0
- package/src/bin/codex-trust.js +205 -0
- package/src/bin/codex-trust.js.map +1 -0
- package/src/bin/dev-hook-install.d.ts +1 -0
- package/src/bin/dev-hook-install.js +80 -0
- package/src/bin/dev-hook-install.js.map +1 -0
- package/src/bin/dev-hook-uninstall.d.ts +1 -0
- package/src/bin/dev-hook-uninstall.js +45 -0
- package/src/bin/dev-hook-uninstall.js.map +1 -0
- package/src/bin/hook-registration.d.ts +429 -0
- package/src/bin/hook-registration.js +661 -0
- package/src/bin/hook-registration.js.map +1 -0
- package/src/bin/install-entry.d.ts +19 -0
- package/src/bin/install-entry.js +128 -0
- package/src/bin/install-entry.js.map +1 -0
- package/src/bin/l0-allowlist.d.ts +46 -0
- package/src/bin/l0-allowlist.js +527 -0
- package/src/bin/l0-allowlist.js.map +1 -0
- package/src/bin/l0-codex-read.d.ts +20 -0
- package/src/bin/l0-codex-read.js +108 -0
- package/src/bin/l0-codex-read.js.map +1 -0
- package/src/bin/l0-decide.d.ts +49 -0
- package/src/bin/l0-decide.js +90 -0
- package/src/bin/l0-decide.js.map +1 -0
- package/src/bin/l0-entry.d.ts +103 -0
- package/src/bin/l0-entry.js +98 -0
- package/src/bin/l0-entry.js.map +1 -0
- package/src/bin/l0-ignored-tools.d.ts +38 -0
- package/src/bin/l0-ignored-tools.js +44 -0
- package/src/bin/l0-ignored-tools.js.map +1 -0
- package/src/bin/managed-env.d.ts +46 -0
- package/src/bin/managed-env.js +50 -0
- package/src/bin/managed-env.js.map +1 -0
- package/src/bin/neighbour-hooks.d.ts +42 -0
- package/src/bin/neighbour-hooks.js +177 -0
- package/src/bin/neighbour-hooks.js.map +1 -0
- package/src/bin/reviewer-agent.d.ts +19 -0
- package/src/bin/reviewer-agent.js +135 -0
- package/src/bin/reviewer-agent.js.map +1 -0
- package/src/bin/settings-shape.d.ts +37 -0
- package/src/bin/settings-shape.js +17 -0
- package/src/bin/settings-shape.js.map +1 -0
- package/src/bin/setup-config.d.ts +20 -0
- package/src/bin/setup-config.js +371 -0
- package/src/bin/setup-config.js.map +1 -0
- package/src/bin/setup.d.ts +78 -0
- package/src/bin/setup.js +411 -0
- package/src/bin/setup.js.map +1 -0
- package/src/bin/shim-audit-log.d.ts +119 -0
- package/src/bin/shim-audit-log.js +286 -0
- package/src/bin/shim-audit-log.js.map +1 -0
- package/src/bin/shim-deny-reason.d.ts +1 -0
- package/src/bin/shim-deny-reason.js +244 -0
- package/src/bin/shim-deny-reason.js.map +1 -0
- package/src/bin/shim-drift-fix.d.ts +1 -0
- package/src/bin/shim-drift-fix.js +73 -0
- package/src/bin/shim-drift-fix.js.map +1 -0
- package/src/bin/shim-testkit.d.ts +107 -0
- package/src/bin/shim-testkit.js +197 -0
- package/src/bin/shim-testkit.js.map +1 -0
- package/src/bin/shim-version-drift.d.ts +1 -0
- package/src/bin/shim-version-drift.js +164 -0
- package/src/bin/shim-version-drift.js.map +1 -0
- package/src/bin/shim.d.ts +16 -0
- package/src/bin/shim.js +641 -0
- package/src/bin/shim.js.map +1 -0
- package/src/bin/upgrade-shim.d.ts +18 -0
- package/src/bin/upgrade-shim.js +283 -0
- package/src/bin/upgrade-shim.js.map +1 -0
- package/src/configs/main-sync-guard-configs.d.ts +38 -0
- package/src/configs/main-sync-guard-configs.js +54 -0
- package/src/configs/main-sync-guard-configs.js.map +1 -0
- package/src/configs/rule-configs.d.ts +47 -0
- package/src/configs/rule-configs.js +96 -0
- package/src/configs/rule-configs.js.map +1 -0
- package/src/core/ai-type-context.d.ts +36 -0
- package/src/core/ai-type-context.js +45 -0
- package/src/core/ai-type-context.js.map +1 -0
- package/src/core/decision-log.d.ts +190 -0
- package/src/core/decision-log.js +409 -0
- package/src/core/decision-log.js.map +1 -0
- package/src/core/excluded-path-escape.d.ts +101 -0
- package/src/core/excluded-path-escape.js +236 -0
- package/src/core/excluded-path-escape.js.map +1 -0
- package/src/core/force-to-root.d.ts +32 -0
- package/src/core/force-to-root.js +60 -0
- package/src/core/force-to-root.js.map +1 -0
- package/src/core/guard-index-doc.d.ts +63 -0
- package/src/core/guard-index-doc.js +172 -0
- package/src/core/guard-index-doc.js.map +1 -0
- package/src/core/instruct-ai-writer.d.ts +1 -0
- package/src/core/instruct-ai-writer.js +6 -0
- package/src/core/instruct-ai-writer.js.map +1 -0
- package/src/core/l0-matrix.d.ts +102 -0
- package/src/core/l0-matrix.js +357 -0
- package/src/core/l0-matrix.js.map +1 -0
- package/src/core/l0-tooling-doc.d.ts +45 -0
- package/src/core/l0-tooling-doc.js +258 -0
- package/src/core/l0-tooling-doc.js.map +1 -0
- package/src/core/l1-doc.d.ts +7 -0
- package/src/core/l1-doc.js +340 -0
- package/src/core/l1-doc.js.map +1 -0
- package/src/core/l1-matrix-doc.d.ts +31 -0
- package/src/core/l1-matrix-doc.js +111 -0
- package/src/core/l1-matrix-doc.js.map +1 -0
- package/src/core/l1-rows.d.ts +167 -0
- package/src/core/l1-rows.js +262 -0
- package/src/core/l1-rows.js.map +1 -0
- package/src/core/l2-doc.d.ts +7 -0
- package/src/core/l2-doc.js +385 -0
- package/src/core/l2-doc.js.map +1 -0
- package/src/core/l2-matrix-doc.d.ts +23 -0
- package/src/core/l2-matrix-doc.js +72 -0
- package/src/core/l2-matrix-doc.js.map +1 -0
- package/src/core/l2-row.d.ts +54 -0
- package/src/core/l2-row.js +75 -0
- package/src/core/l2-row.js.map +1 -0
- package/src/core/l2-rows.d.ts +56 -0
- package/src/core/l2-rows.js +272 -0
- package/src/core/l2-rows.js.map +1 -0
- package/src/core/load-rules.d.ts +9 -0
- package/src/core/load-rules.js +65 -0
- package/src/core/load-rules.js.map +1 -0
- package/src/core/log-stream.d.ts +117 -0
- package/src/core/log-stream.js +156 -0
- package/src/core/log-stream.js.map +1 -0
- package/src/core/log-streams.d.ts +48 -0
- package/src/core/log-streams.js +55 -0
- package/src/core/log-streams.js.map +1 -0
- package/src/core/log-target.d.ts +21 -0
- package/src/core/log-target.js +29 -0
- package/src/core/log-target.js.map +1 -0
- package/src/core/main-sync-log.d.ts +16 -0
- package/src/core/main-sync-log.js +108 -0
- package/src/core/main-sync-log.js.map +1 -0
- package/src/core/main-sync-refresh.d.ts +39 -0
- package/src/core/main-sync-refresh.js +96 -0
- package/src/core/main-sync-refresh.js.map +1 -0
- package/src/core/main-sync-timeout.d.ts +33 -0
- package/src/core/main-sync-timeout.js +95 -0
- package/src/core/main-sync-timeout.js.map +1 -0
- package/src/core/matrix-cures.d.ts +12 -0
- package/src/core/matrix-cures.js +56 -0
- package/src/core/matrix-cures.js.map +1 -0
- package/src/core/missing-directory.d.ts +38 -0
- package/src/core/missing-directory.js +66 -0
- package/src/core/missing-directory.js.map +1 -0
- package/src/core/read-only-inspection.d.ts +34 -0
- package/src/core/read-only-inspection.js +88 -0
- package/src/core/read-only-inspection.js.map +1 -0
- package/src/core/rejection-log.d.ts +9 -0
- package/src/core/rejection-log.js +262 -0
- package/src/core/rejection-log.js.map +1 -0
- package/src/core/rules/branch-creation-guard.d.ts +113 -0
- package/src/core/rules/branch-creation-guard.js +425 -0
- package/src/core/rules/branch-creation-guard.js.map +1 -0
- package/src/core/rules/branch-switch-scan.d.ts +43 -0
- package/src/core/rules/branch-switch-scan.js +132 -0
- package/src/core/rules/branch-switch-scan.js.map +1 -0
- package/src/core/rules/build-output-pipe-guard.d.ts +56 -0
- package/src/core/rules/build-output-pipe-guard.js +120 -0
- package/src/core/rules/build-output-pipe-guard.js.map +1 -0
- package/src/core/rules/build-output-pipe-scan.d.ts +47 -0
- package/src/core/rules/build-output-pipe-scan.js +84 -0
- package/src/core/rules/build-output-pipe-scan.js.map +1 -0
- package/src/core/rules/cap-remedies.d.ts +39 -0
- package/src/core/rules/cap-remedies.js +59 -0
- package/src/core/rules/cap-remedies.js.map +1 -0
- package/src/core/rules/commit-message-substitution-guard.d.ts +72 -0
- package/src/core/rules/commit-message-substitution-guard.js +134 -0
- package/src/core/rules/commit-message-substitution-guard.js.map +1 -0
- package/src/core/rules/commit-message-substitution-scan.d.ts +42 -0
- package/src/core/rules/commit-message-substitution-scan.js +145 -0
- package/src/core/rules/commit-message-substitution-scan.js.map +1 -0
- package/src/core/rules/content-read-scan.d.ts +76 -0
- package/src/core/rules/content-read-scan.js +176 -0
- package/src/core/rules/content-read-scan.js.map +1 -0
- package/src/core/rules/cure-prefix-scan.d.ts +64 -0
- package/src/core/rules/cure-prefix-scan.js +133 -0
- package/src/core/rules/cure-prefix-scan.js.map +1 -0
- package/src/core/rules/feature-branch-guard.d.ts +121 -0
- package/src/core/rules/feature-branch-guard.js +327 -0
- package/src/core/rules/feature-branch-guard.js.map +1 -0
- package/src/core/rules/index.d.ts +1 -0
- package/src/core/rules/index.js +5 -0
- package/src/core/rules/index.js.map +1 -0
- package/src/core/rules/judged-tree.d.ts +66 -0
- package/src/core/rules/judged-tree.js +97 -0
- package/src/core/rules/judged-tree.js.map +1 -0
- package/src/core/rules/main-freshness.d.ts +37 -0
- package/src/core/rules/main-freshness.js +61 -0
- package/src/core/rules/main-freshness.js.map +1 -0
- package/src/core/rules/merge-in-progress-guard.d.ts +17 -0
- package/src/core/rules/merge-in-progress-guard.js +124 -0
- package/src/core/rules/merge-in-progress-guard.js.map +1 -0
- package/src/core/rules/merged-branch-bash-guard.d.ts +62 -0
- package/src/core/rules/merged-branch-bash-guard.js +171 -0
- package/src/core/rules/merged-branch-bash-guard.js.map +1 -0
- package/src/core/rules/merged-branch-message.d.ts +59 -0
- package/src/core/rules/merged-branch-message.js +137 -0
- package/src/core/rules/merged-branch-message.js.map +1 -0
- package/src/core/rules/pr-creation-or-push-guard.d.ts +26 -0
- package/src/core/rules/pr-creation-or-push-guard.js +150 -0
- package/src/core/rules/pr-creation-or-push-guard.js.map +1 -0
- package/src/core/rules/pr-merge-guard.d.ts +36 -0
- package/src/core/rules/pr-merge-guard.js +87 -0
- package/src/core/rules/pr-merge-guard.js.map +1 -0
- package/src/core/rules/read-stale-guard.d.ts +136 -0
- package/src/core/rules/read-stale-guard.js +328 -0
- package/src/core/rules/read-stale-guard.js.map +1 -0
- package/src/core/rules/recovery-allowlist.d.ts +49 -0
- package/src/core/rules/recovery-allowlist.js +187 -0
- package/src/core/rules/recovery-allowlist.js.map +1 -0
- package/src/core/rules/redirect-how-to-merge-main.d.ts +16 -0
- package/src/core/rules/redirect-how-to-merge-main.js +159 -0
- package/src/core/rules/redirect-how-to-merge-main.js.map +1 -0
- package/src/core/rules/stale-main-bash-guard.d.ts +198 -0
- package/src/core/rules/stale-main-bash-guard.js +363 -0
- package/src/core/rules/stale-main-bash-guard.js.map +1 -0
- package/src/core/rules/stale-main-message.d.ts +62 -0
- package/src/core/rules/stale-main-message.js +102 -0
- package/src/core/rules/stale-main-message.js.map +1 -0
- package/src/core/rules/tree-recovery.d.ts +92 -0
- package/src/core/rules/tree-recovery.js +165 -0
- package/src/core/rules/tree-recovery.js.map +1 -0
- package/src/core/rules/wait-spin-guard.d.ts +128 -0
- package/src/core/rules/wait-spin-guard.js +211 -0
- package/src/core/rules/wait-spin-guard.js.map +1 -0
- package/src/core/rules/wait-spin-scan.d.ts +118 -0
- package/src/core/rules/wait-spin-scan.js +203 -0
- package/src/core/rules/wait-spin-scan.js.map +1 -0
- package/src/core/rules/whole-repo-build-guard.d.ts +125 -0
- package/src/core/rules/whole-repo-build-guard.js +257 -0
- package/src/core/rules/whole-repo-build-guard.js.map +1 -0
- package/src/core/rules/whole-repo-build-scan.d.ts +28 -0
- package/src/core/rules/whole-repo-build-scan.js +198 -0
- package/src/core/rules/whole-repo-build-scan.js.map +1 -0
- package/src/core/runner.d.ts +19 -0
- package/src/core/runner.js +482 -0
- package/src/core/runner.js.map +1 -0
- package/src/core/session-call-history.d.ts +44 -0
- package/src/core/session-call-history.js +79 -0
- package/src/core/session-call-history.js.map +1 -0
- package/src/core/sync-main.d.ts +46 -0
- package/src/core/sync-main.js +176 -0
- package/src/core/sync-main.js.map +1 -0
- package/src/core/version-sync.d.ts +219 -0
- package/src/core/version-sync.js +558 -0
- package/src/core/version-sync.js.map +1 -0
- package/src/core/webpieces-versions.d.ts +98 -0
- package/src/core/webpieces-versions.js +206 -0
- package/src/core/webpieces-versions.js.map +1 -0
- package/src/core/workflow-hook-rules.d.ts +5 -0
- package/src/core/workflow-hook-rules.js +15 -0
- package/src/core/workflow-hook-rules.js.map +1 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +28 -0
- package/src/index.js.map +1 -0
- package/src/policy-keys.d.ts +2 -0
- package/src/policy-keys.js +6 -0
- package/src/policy-keys.js.map +1 -0
- package/src/rule-migrations.d.ts +3 -0
- package/src/rule-migrations.js +22 -0
- package/src/rule-migrations.js.map +1 -0
- package/src/rule-pack.d.ts +2 -0
- package/src/rule-pack.js +20 -0
- package/src/rule-pack.js.map +1 -0
- package/src/rule-settings.d.ts +5 -0
- package/src/rule-settings.js +31 -0
- package/src/rule-settings.js.map +1 -0
- package/src/safeguard-catalog.d.ts +3 -0
- package/src/safeguard-catalog.js +16 -0
- package/src/safeguard-catalog.js.map +1 -0
- package/templates/ai-hook.sh +497 -0
- package/templates/claude-settings-hook.json +28 -0
- package/templates/webpieces-reviewer.md +60 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EreMatchSet = exports.ShimTestkit = exports.ShimRun = exports.PreToolUseDecision = exports.HookSpecificOutput = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const tooling_testkit_1 = require("@webpieces/tooling-testkit");
|
|
6
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
7
|
+
const path = tslib_1.__importStar(require("path"));
|
|
8
|
+
const child_process_1 = require("child_process");
|
|
9
|
+
const shim_1 = require("./shim");
|
|
10
|
+
/**
|
|
11
|
+
* The shim's PreToolUse deny payload, as it prints it on stdout. Named (not an inline literal on the
|
|
12
|
+
* JSON.parse cast) so the wire shape this testkit depends on is stated once, in one place.
|
|
13
|
+
*/
|
|
14
|
+
class HookSpecificOutput {
|
|
15
|
+
permissionDecisionReason;
|
|
16
|
+
constructor(permissionDecisionReason) {
|
|
17
|
+
this.permissionDecisionReason = permissionDecisionReason;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.HookSpecificOutput = HookSpecificOutput;
|
|
21
|
+
/** The decision envelope wrapping {@link HookSpecificOutput}. */
|
|
22
|
+
class PreToolUseDecision {
|
|
23
|
+
hookSpecificOutput;
|
|
24
|
+
constructor(hookSpecificOutput) {
|
|
25
|
+
this.hookSpecificOutput = hookSpecificOutput;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.PreToolUseDecision = PreToolUseDecision;
|
|
29
|
+
/** The outcome of one shim invocation. Data-only → a class, per CLAUDE.md. */
|
|
30
|
+
class ShimRun {
|
|
31
|
+
status;
|
|
32
|
+
stdout;
|
|
33
|
+
stderr;
|
|
34
|
+
constructor(status, stdout, stderr) {
|
|
35
|
+
this.status = status;
|
|
36
|
+
this.stdout = stdout;
|
|
37
|
+
this.stderr = stderr;
|
|
38
|
+
}
|
|
39
|
+
/** True when the shim emitted a PreToolUse deny. */
|
|
40
|
+
isDenied() {
|
|
41
|
+
return this.stdout.includes('"permissionDecision":"deny"');
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The deny REASON, parsed out of the PreToolUse JSON.
|
|
45
|
+
* @throws if this run was not a deny (there is no reason to read).
|
|
46
|
+
*/
|
|
47
|
+
denyReason() {
|
|
48
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
49
|
+
const decision = JSON.parse(this.stdout);
|
|
50
|
+
return decision.hookSpecificOutput.permissionDecisionReason;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.ShimRun = ShimRun;
|
|
54
|
+
/**
|
|
55
|
+
* ShimTestkit — the shared harness for driving the rendered shim through a REAL /bin/sh.
|
|
56
|
+
*
|
|
57
|
+
* Extracted so setup.spec.ts and shim-drift.spec.ts drive the shim the SAME way instead of each
|
|
58
|
+
* keeping its own copy: the shim's entire contract is "what /bin/sh actually does with it", so two
|
|
59
|
+
* drifting harnesses would silently become two different contracts.
|
|
60
|
+
*
|
|
61
|
+
* An instance class (not module-scope functions) because this is normal source to the linter — only
|
|
62
|
+
* *.spec.ts is exempt from no-function-outside-class, and a testkit should not need a disable comment
|
|
63
|
+
* to exist.
|
|
64
|
+
*/
|
|
65
|
+
class ShimTestkit {
|
|
66
|
+
/** A throwaway repo root under the OS temp dir. */
|
|
67
|
+
mktmp() {
|
|
68
|
+
return tooling_testkit_1.policyFixture.makeRepo('wp-setup-');
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Run the rendered shim exactly as Claude Code would: `sh <shim> <bin> ...`, from a repo cwd,
|
|
72
|
+
* piping tool-payload JSON on stdin. spawnSync never throws on non-zero exit.
|
|
73
|
+
*/
|
|
74
|
+
runShim(root, bin, stdin) {
|
|
75
|
+
// Place the shim at its REAL relative location (<root>/.claude/webpieces/ai-hook.sh) so its
|
|
76
|
+
// self-location (`dirname $0/../..` → <root>) resolves the bin correctly. Run it from a SUBDIR
|
|
77
|
+
// to prove it does not depend on the caller's cwd.
|
|
78
|
+
const shimAbs = path.join(root, '.claude', 'webpieces', 'ai-hook.sh');
|
|
79
|
+
fs.mkdirSync(path.dirname(shimAbs), { recursive: true });
|
|
80
|
+
fs.writeFileSync(shimAbs, (0, shim_1.renderShim)(), { mode: 0o755 });
|
|
81
|
+
const subdir = path.join(root, 'packages', 'deep', 'sub');
|
|
82
|
+
fs.mkdirSync(subdir, { recursive: true });
|
|
83
|
+
const r = (0, child_process_1.spawnSync)('/bin/sh', [shimAbs, bin], { cwd: subdir, input: stdin, encoding: 'utf8' });
|
|
84
|
+
return new ShimRun(r.status, r.stdout, r.stderr);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* A repo root staged so the shim's VERSION-DRIFT check (fault D) fires: an installed guard bin, a
|
|
88
|
+
* declared @webpieces/pr-gate pin in package.json, and a different installed version in
|
|
89
|
+
* node_modules. The fake bin prints EXECED, so "the guards actually ran" is observable in stdout —
|
|
90
|
+
* pass matching versions to stage the no-drift case instead.
|
|
91
|
+
*
|
|
92
|
+
* Lives here rather than in one spec because two spec files now need the identical staging, and a
|
|
93
|
+
* second copy is a second definition of what "drift" means.
|
|
94
|
+
*/
|
|
95
|
+
stageDriftRoot(declared, installed) {
|
|
96
|
+
const root = this.mktmp();
|
|
97
|
+
const binDir = path.join(root, 'node_modules', '.bin');
|
|
98
|
+
fs.mkdirSync(binDir, { recursive: true });
|
|
99
|
+
fs.writeFileSync(path.join(binDir, 'wp-ai-guards-hook'), '#!/bin/sh\nprintf EXECED\n', { mode: 0o755 });
|
|
100
|
+
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ dependencies: { '@webpieces/pr-gate': declared } }, null, 2) + '\n');
|
|
101
|
+
const manifestDir = path.join(root, 'node_modules', '@webpieces', 'pr-gate');
|
|
102
|
+
fs.mkdirSync(manifestDir, { recursive: true });
|
|
103
|
+
fs.writeFileSync(path.join(manifestDir, 'package.json'), JSON.stringify({ name: '@webpieces/pr-gate', version: installed }, null, 2) + '\n');
|
|
104
|
+
return root;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Make `root` a real git repo whose CURRENT BRANCH is `branch`, and return it.
|
|
108
|
+
*
|
|
109
|
+
* The fault-D deny asks `git branch --show-current` to decide whether "move forward to what origin
|
|
110
|
+
* pins" is a legal command here (it is on main, and it destroys the fork point on a feature branch),
|
|
111
|
+
* so the branch is now an INPUT to the message and has to be stageable. `symbolic-ref` rather than a
|
|
112
|
+
* commit: `--show-current` answers on an UNBORN branch, so this needs no user identity, no index and
|
|
113
|
+
* no object write — which is also why a staged root with NO git dir at all keeps answering '' and
|
|
114
|
+
* lands on the conservative, non-main half.
|
|
115
|
+
*/
|
|
116
|
+
stageBranch(root, branch) {
|
|
117
|
+
(0, child_process_1.spawnSync)('git', ['init', '-q'], { cwd: root, encoding: 'utf8' });
|
|
118
|
+
(0, child_process_1.spawnSync)('git', ['symbolic-ref', 'HEAD', `refs/heads/${branch}`], { cwd: root, encoding: 'utf8' });
|
|
119
|
+
return root;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* A repo root that DECLARES @webpieces/agent-workflow-rules but has nothing installed — fault X, the
|
|
123
|
+
* ordinary fresh-clone / new-worktree case whose cure really is `pnpm install`.
|
|
124
|
+
*
|
|
125
|
+
* A bare mktmp() root is NOT this case, and that distinction is the whole point of fault U: with no
|
|
126
|
+
* package.json at all, nothing asks for the package and `pnpm install` is a no-op. Every X spec must
|
|
127
|
+
* stage the declaration explicitly, or it silently asserts against the U message instead.
|
|
128
|
+
*/
|
|
129
|
+
stageDeclaredRoot(declared = '0.4.574') {
|
|
130
|
+
const root = this.mktmp();
|
|
131
|
+
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ devDependencies: { '@webpieces/agent-workflow-rules': declared } }, null, 2) + '\n');
|
|
132
|
+
return root;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* A throwaway repo root that OWNS a committed shim at shimPath(root) with the given contents
|
|
136
|
+
* (null = no shim at all, i.e. a fresh clone / global install). Shared by the two spec files that
|
|
137
|
+
* exercise the committed-shim self-guard, so "a root with a shim in it" has one definition.
|
|
138
|
+
*/
|
|
139
|
+
stageCommittedShim(content) {
|
|
140
|
+
const root = this.mktmp();
|
|
141
|
+
if (content !== null) {
|
|
142
|
+
const p = (0, shim_1.shimPath)(root);
|
|
143
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
144
|
+
fs.writeFileSync(p, content);
|
|
145
|
+
}
|
|
146
|
+
return root;
|
|
147
|
+
}
|
|
148
|
+
/** A Bash tool payload, as Claude Code sends it on stdin. */
|
|
149
|
+
bashPayload(command) {
|
|
150
|
+
return JSON.stringify({ tool_name: 'Bash', tool_input: { command } });
|
|
151
|
+
}
|
|
152
|
+
/** A Read payload — the L0 allowlist entry that keeps a broken tree inspectable. */
|
|
153
|
+
readPayload(filePath) {
|
|
154
|
+
return JSON.stringify({ tool_name: 'Read', tool_input: { file_path: filePath } });
|
|
155
|
+
}
|
|
156
|
+
/** A file-tool payload, for the always-allowed webpieces.config.json recovery target. */
|
|
157
|
+
filePayload(toolName, filePath) {
|
|
158
|
+
return JSON.stringify({ tool_name: toolName, tool_input: { file_path: filePath } });
|
|
159
|
+
}
|
|
160
|
+
/** True when `cmd` matches a POSIX ERE, judged by the SAME `grep -E` the shim itself runs. */
|
|
161
|
+
ereMatches(ere, cmd) {
|
|
162
|
+
return this.ereMatchSet(ere, [cmd]).matched(cmd);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Which of `cmds` that same `grep -E` matches — answered in ONE grep process for the whole batch.
|
|
166
|
+
*
|
|
167
|
+
* grep is a line matcher, so feeding N commands as N lines asks exactly the question `-Eq` answers
|
|
168
|
+
* per command; the engine, the ERE and the anchors are unchanged. What changes is cost: a process
|
|
169
|
+
* spawn is ~5ms on an idle machine but ~100ms when the suite runs projects in parallel, so a
|
|
170
|
+
* 16-command twin check used to be 16 spawns (~2s of pure spawn latency) for one grep pass. That
|
|
171
|
+
* is what made these files miss the per-test timeout under load.
|
|
172
|
+
*/
|
|
173
|
+
ereMatchSet(ere, cmds) {
|
|
174
|
+
for (const cmd of cmds) {
|
|
175
|
+
// A command carrying a newline would arrive at grep as TWO lines and be judged as two
|
|
176
|
+
// different commands — a silently wrong answer. Nothing in the allowlists does this.
|
|
177
|
+
if (cmd.includes('\n'))
|
|
178
|
+
throw new Error(`ereMatchSet cannot batch a multi-line command: ${JSON.stringify(cmd)}`);
|
|
179
|
+
}
|
|
180
|
+
const result = (0, child_process_1.spawnSync)('grep', ['-E', ere], { input: cmds.join('\n'), encoding: 'utf8' });
|
|
181
|
+
const hits = new Set((result.stdout ?? '').split('\n').filter((line) => line !== ''));
|
|
182
|
+
return new EreMatchSet(hits);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
exports.ShimTestkit = ShimTestkit;
|
|
186
|
+
/** The lines `grep -E` matched in one batched run — ask it per command with {@link matched}. */
|
|
187
|
+
class EreMatchSet {
|
|
188
|
+
hits;
|
|
189
|
+
constructor(hits) {
|
|
190
|
+
this.hits = hits;
|
|
191
|
+
}
|
|
192
|
+
matched(cmd) {
|
|
193
|
+
return this.hits.has(cmd);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
exports.EreMatchSet = EreMatchSet;
|
|
197
|
+
//# sourceMappingURL=shim-testkit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shim-testkit.js","sourceRoot":"","sources":["../../../../../../packages/tooling/agent-workflow-rules/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IACrD,CAAC;IAED;;;;;;;;OAQG;IACH,WAAW,CAAC,GAAW,EAAE,IAAuB;QAC5C,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACrB,sFAAsF;YACtF,qFAAqF;YACrF,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC;gBAAE,MAAM,IAAI,KAAK,CAAC,kDAAkD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACrH,CAAC;QACD,MAAM,MAAM,GAAG,IAAA,yBAAS,EAAC,MAAM,EAAE,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;QAC5F,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,IAAY,EAAW,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC;QACvG,OAAO,IAAI,WAAW,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;CACJ;AAnID,kCAmIC;AAED,gGAAgG;AAChG,MAAa,WAAW;IACH,IAAI,CAAsB;IAE3C,YAAY,IAAyB;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;IAED,OAAO,CAAC,GAAW;QACf,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;CACJ;AAVD,kCAUC","sourcesContent":["import { policyFixture } from '@webpieces/tooling-testkit';\nimport { specTempDirs } from '@webpieces/tooling-testkit';\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport { spawnSync } from 'child_process';\nimport { renderShim, shimPath } from './shim';\n\n\n/**\n * The shim's PreToolUse deny payload, as it prints it on stdout. Named (not an inline literal on the\n * JSON.parse cast) so the wire shape this testkit depends on is stated once, in one place.\n */\nexport class HookSpecificOutput {\n constructor(public readonly permissionDecisionReason: string) {}\n}\n\n/** The decision envelope wrapping {@link HookSpecificOutput}. */\nexport class PreToolUseDecision {\n constructor(public readonly hookSpecificOutput: HookSpecificOutput) {}\n}\n\n/** The outcome of one shim invocation. Data-only → a class, per CLAUDE.md. */\nexport class ShimRun {\n constructor(\n public readonly status: number | null,\n public readonly stdout: string,\n public readonly stderr: string,\n ) {}\n\n /** True when the shim emitted a PreToolUse deny. */\n isDenied(): boolean {\n return this.stdout.includes('\"permissionDecision\":\"deny\"');\n }\n\n /**\n * The deny REASON, parsed out of the PreToolUse JSON.\n * @throws if this run was not a deny (there is no reason to read).\n */\n denyReason(): string {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n const decision = JSON.parse(this.stdout) as PreToolUseDecision;\n return decision.hookSpecificOutput.permissionDecisionReason;\n }\n}\n\n/**\n * ShimTestkit — the shared harness for driving the rendered shim through a REAL /bin/sh.\n *\n * Extracted so setup.spec.ts and shim-drift.spec.ts drive the shim the SAME way instead of each\n * keeping its own copy: the shim's entire contract is \"what /bin/sh actually does with it\", so two\n * drifting harnesses would silently become two different contracts.\n *\n * An instance class (not module-scope functions) because this is normal source to the linter — only\n * *.spec.ts is exempt from no-function-outside-class, and a testkit should not need a disable comment\n * to exist.\n */\nexport class ShimTestkit {\n /** A throwaway repo root under the OS temp dir. */\n mktmp(): string {\n return policyFixture.makeRepo('wp-setup-');\n }\n\n /**\n * Run the rendered shim exactly as Claude Code would: `sh <shim> <bin> ...`, from a repo cwd,\n * piping tool-payload JSON on stdin. spawnSync never throws on non-zero exit.\n */\n runShim(root: string, bin: string, stdin: string): ShimRun {\n // Place the shim at its REAL relative location (<root>/.claude/webpieces/ai-hook.sh) so its\n // self-location (`dirname $0/../..` → <root>) resolves the bin correctly. Run it from a SUBDIR\n // to prove it does not depend on the caller's cwd.\n const shimAbs = path.join(root, '.claude', 'webpieces', 'ai-hook.sh');\n fs.mkdirSync(path.dirname(shimAbs), { recursive: true });\n fs.writeFileSync(shimAbs, renderShim(), { mode: 0o755 });\n const subdir = path.join(root, 'packages', 'deep', 'sub');\n fs.mkdirSync(subdir, { recursive: true });\n const r = spawnSync('/bin/sh', [shimAbs, bin], { cwd: subdir, input: stdin, encoding: 'utf8' });\n return new ShimRun(r.status, r.stdout, r.stderr);\n }\n\n /**\n * A repo root staged so the shim's VERSION-DRIFT check (fault D) fires: an installed guard bin, a\n * declared @webpieces/pr-gate pin in package.json, and a different installed version in\n * node_modules. The fake bin prints EXECED, so \"the guards actually ran\" is observable in stdout —\n * pass matching versions to stage the no-drift case instead.\n *\n * Lives here rather than in one spec because two spec files now need the identical staging, and a\n * second copy is a second definition of what \"drift\" means.\n */\n stageDriftRoot(declared: string, installed: string): string {\n const root = this.mktmp();\n const binDir = path.join(root, 'node_modules', '.bin');\n fs.mkdirSync(binDir, { recursive: true });\n fs.writeFileSync(path.join(binDir, 'wp-ai-guards-hook'), '#!/bin/sh\\nprintf EXECED\\n', { mode: 0o755 });\n fs.writeFileSync(path.join(root, 'package.json'),\n JSON.stringify({ dependencies: { '@webpieces/pr-gate': declared } }, null, 2) + '\\n');\n const manifestDir = path.join(root, 'node_modules', '@webpieces', 'pr-gate');\n fs.mkdirSync(manifestDir, { recursive: true });\n fs.writeFileSync(path.join(manifestDir, 'package.json'),\n JSON.stringify({ name: '@webpieces/pr-gate', version: installed }, null, 2) + '\\n');\n return root;\n }\n\n /**\n * Make `root` a real git repo whose CURRENT BRANCH is `branch`, and return it.\n *\n * The fault-D deny asks `git branch --show-current` to decide whether \"move forward to what origin\n * pins\" is a legal command here (it is on main, and it destroys the fork point on a feature branch),\n * so the branch is now an INPUT to the message and has to be stageable. `symbolic-ref` rather than a\n * commit: `--show-current` answers on an UNBORN branch, so this needs no user identity, no index and\n * no object write — which is also why a staged root with NO git dir at all keeps answering '' and\n * lands on the conservative, non-main half.\n */\n stageBranch(root: string, branch: string): string {\n spawnSync('git', ['init', '-q'], { cwd: root, encoding: 'utf8' });\n spawnSync('git', ['symbolic-ref', 'HEAD', `refs/heads/${branch}`], { cwd: root, encoding: 'utf8' });\n return root;\n }\n\n /**\n * A repo root that DECLARES @webpieces/agent-workflow-rules but has nothing installed — fault X, the\n * ordinary fresh-clone / new-worktree case whose cure really is `pnpm install`.\n *\n * A bare mktmp() root is NOT this case, and that distinction is the whole point of fault U: with no\n * package.json at all, nothing asks for the package and `pnpm install` is a no-op. Every X spec must\n * stage the declaration explicitly, or it silently asserts against the U message instead.\n */\n stageDeclaredRoot(declared: string = '0.4.574'): string {\n const root = this.mktmp();\n fs.writeFileSync(path.join(root, 'package.json'),\n JSON.stringify({ devDependencies: { '@webpieces/agent-workflow-rules': declared } }, null, 2) + '\\n');\n return root;\n }\n\n /**\n * A throwaway repo root that OWNS a committed shim at shimPath(root) with the given contents\n * (null = no shim at all, i.e. a fresh clone / global install). Shared by the two spec files that\n * exercise the committed-shim self-guard, so \"a root with a shim in it\" has one definition.\n */\n stageCommittedShim(content: string | null): string {\n const root = this.mktmp();\n if (content !== null) {\n const p = shimPath(root);\n fs.mkdirSync(path.dirname(p), { recursive: true });\n fs.writeFileSync(p, content);\n }\n return root;\n }\n\n /** A Bash tool payload, as Claude Code sends it on stdin. */\n bashPayload(command: string): string {\n return JSON.stringify({ tool_name: 'Bash', tool_input: { command } });\n }\n\n /** A Read payload — the L0 allowlist entry that keeps a broken tree inspectable. */\n readPayload(filePath: string): string {\n return JSON.stringify({ tool_name: 'Read', tool_input: { file_path: filePath } });\n }\n\n /** A file-tool payload, for the always-allowed webpieces.config.json recovery target. */\n filePayload(toolName: string, filePath: string): string {\n return JSON.stringify({ tool_name: toolName, tool_input: { file_path: filePath } });\n }\n\n /** True when `cmd` matches a POSIX ERE, judged by the SAME `grep -E` the shim itself runs. */\n ereMatches(ere: string, cmd: string): boolean {\n return this.ereMatchSet(ere, [cmd]).matched(cmd);\n }\n\n /**\n * Which of `cmds` that same `grep -E` matches — answered in ONE grep process for the whole batch.\n *\n * grep is a line matcher, so feeding N commands as N lines asks exactly the question `-Eq` answers\n * per command; the engine, the ERE and the anchors are unchanged. What changes is cost: a process\n * spawn is ~5ms on an idle machine but ~100ms when the suite runs projects in parallel, so a\n * 16-command twin check used to be 16 spawns (~2s of pure spawn latency) for one grep pass. That\n * is what made these files miss the per-test timeout under load.\n */\n ereMatchSet(ere: string, cmds: readonly string[]): EreMatchSet {\n for (const cmd of cmds) {\n // A command carrying a newline would arrive at grep as TWO lines and be judged as two\n // different commands — a silently wrong answer. Nothing in the allowlists does this.\n if (cmd.includes('\\n')) throw new Error(`ereMatchSet cannot batch a multi-line command: ${JSON.stringify(cmd)}`);\n }\n const result = spawnSync('grep', ['-E', ere], { input: cmds.join('\\n'), encoding: 'utf8' });\n const hits = new Set((result.stdout ?? '').split('\\n').filter((line: string): boolean => line !== ''));\n return new EreMatchSet(hits);\n }\n}\n\n/** The lines `grep -E` matched in one batched run — ask it per command with {@link matched}. */\nexport class EreMatchSet {\n private readonly hits: ReadonlySet<string>;\n\n constructor(hits: ReadonlySet<string>) {\n this.hits = hits;\n }\n\n matched(cmd: string): boolean {\n return this.hits.has(cmd);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const VERSION_DRIFT_GUARD_SH = "# --- webpieces version-drift guard (pure sh \u2014 runs even when the installed guard bin is stale) -----\n# The committed shim is version-agnostic, so it keeps working right after a git pull, BEFORE the\n# matching pnpm install. That is exactly when node_modules can be STALE: an OLDER @webpieces than\n# package.json now pins, whose outdated validator rejects the NEWER webpieces.config.json with baffling\n# \"unknown rule\" errors. Detect that drift HERE (before exec'ing the possibly-stale bin): compare every\n# EXACT-pinned @webpieces/* version in the root package.json against the version actually installed in\n# node_modules; the first mismatch wins. Range specs (^ ~ workspace:*) are skipped, so they never\n# false-positive; best-effort \u2014 a version we cannot read is skipped. On drift we fall through to the\n# SAME fail-closed path as a missing bin (allow only pnpm install, deny the rest).\n#\n# pnpm CATALOGS: a dep pinned via \"catalog:\" / \"catalog:<name>\" carries NO digit-version in package.json,\n# so the old scraper matched nothing and the guard was BLIND to it \u2014 DRIFT_PKG stayed empty and the\n# stale bin ran (the 2026-07 \"0.3.369 vs 0.4.405\" incident). Resolve those specs through the top-level\n# `catalogs:` block of pnpm-lock.yaml (catalog -> pkg -> resolved version) before comparing.\n#\n# THE SAME PASS ANSWERS FAULT U (2026-08-05). Scraping root package.json is also the only way to learn\n# whether @webpieces/agent-workflow-rules is DECLARED at all, and that is the difference between \"not installed\n# yet\" (X, cured by pnpm install) and \"nothing asks for it\" (U, where pnpm install is a guaranteed\n# no-op). WP_PIN carries the first EXACT @webpieces pin found, so U's deny can prescribe the version the\n# rest of the repo is already on rather than an unpinned add. Both are set BEFORE the range/catalog\n# `continue`s, so a repo pinning the package by range still counts as having declared it.\nDRIFT_PKG=\"\"\nDRIFT_DECLARED=\"\"\nDRIFT_INSTALLED=\"\"\nWP_HOOK_PKG_DECLARED=\"\"\nWP_PIN=\"\"\nif [ -f \"$ROOT/package.json\" ]; then\n # Only when a @webpieces dep actually uses a \"catalog:\" spec do we scan the (possibly huge) lockfile \u2014\n # a cheap grep keeps the common, catalog-free repo from paying that cost on every tool call. One awk\n # pass over pnpm-lock.yaml emits \"<catalog> <@webpieces/pkg> <version>\" lines for the sh lookup below;\n # \\047 is a single quote (so this awk program carries none and stays safely single-quotable in sh).\n WP_CATALOGS=\"\"\n WP_WS_CATALOGS=\"\"\n # THE PIN LIVES IN pnpm-workspace.yaml, and the LOCK is only the fallback (2026-08-20).\n #\n # L0 used to learn the pin from pnpm-lock.yaml's `catalogs:` alone, while L1's WebpiecesVersions.readPin\n # reads pnpm-workspace.yaml. Two notions of \"the pin\", and the gap is exactly where the cure lands: an\n # agent told to raise this tree's pin edits pnpm-workspace.yaml, re-runs, and L0 still reports the OLD\n # number \u2014 because only `pnpm install` rewrites the lock \u2014 so it concludes the edit did nothing and\n # reaches for something worse. Reading the workspace manifest FIRST makes the edit visible immediately.\n #\n # It resolves the same two YAML shapes readPin does, and that is not optional: a repo pinning the whole\n # @webpieces family in lockstep writes the version ONCE as `&wp 0.4.669` and aliases the rest as `*wp`,\n # so an anchor-blind read silently nulls the leg on precisely the repos that pin most carefully. Both\n # `catalog:` (the default catalog) and `catalogs:` (named ones) are walked. A value that is not a plain\n # digit-version (a range) is NOT emitted, so a loose pinner falls through to the lock rather than being\n # compared against an incomparable spec.\n if grep -Eq '\"@webpieces/[^\"]*\"[[:space:]]*:[[:space:]]*\"catalog:' \"$ROOT/package.json\" 2>/dev/null && [ -f \"$ROOT/pnpm-workspace.yaml\" ]; then\n WP_WS_CATALOGS=\"$(awk '\n { n=0; while (substr($0,n+1,1)==\" \") n++; c=substr($0,n+1) }\n c==\"\" || substr(c,1,1)==\"#\" { next }\n {\n ai=index(c,\":\")\n if (ai>0) {\n av=substr(c,ai+1); sub(/^[ \\t]+/,\"\",av)\n if (substr(av,1,1)==\"&\") {\n an=substr(av,2); sub(/[ \\t].*/,\"\",an)\n sub(/^&[^ \\t]+[ \\t]*/,\"\",av)\n sub(/[ \\t]+#.*/,\"\",av); gsub(/[\"\\047]/,\"\",av); sub(/[ \\t].*/,\"\",av)\n if (an!=\"\" && av!=\"\") anch[an]=av\n }\n }\n }\n n==0 { mode=(c ~ /^catalog: *$/)?1:((c ~ /^catalogs: *$/)?2:0); cat=(mode==1)?\"default\":\"\"; next }\n mode==0 { next }\n mode==2 && c ~ /^[^:]+: *$/ { cat=c; sub(/: *$/,\"\",cat); gsub(/[\"\\047 ]/,\"\",cat); next }\n {\n ki=index(c,\":\")\n if (ki<=0) next\n k=substr(c,1,ki-1); gsub(/[\"\\047 ]/,\"\",k)\n if (substr(k,1,11)!=\"@webpieces/\") next\n v=substr(c,ki+1); sub(/^[ \\t]+/,\"\",v); sub(/^&[^ \\t]+[ \\t]*/,\"\",v)\n sub(/[ \\t]+#.*/,\"\",v); gsub(/[\"\\047]/,\"\",v); sub(/[ \\t].*/,\"\",v)\n if (v==\"\") next\n nn++; key[nn]=cat \" \" k; ali[nn]=(substr(v,1,1)==\"*\")?substr(v,2):\"\"; val[nn]=v\n }\n END { for (i=1;i<=nn;i++) { vv=(ali[i]==\"\")?val[i]:anch[ali[i]]; if (vv ~ /^[0-9]/) print key[i] \" \" vv } }\n ' \"$ROOT/pnpm-workspace.yaml\" 2>/dev/null)\"\n fi\n if grep -Eq '\"@webpieces/[^\"]*\"[[:space:]]*:[[:space:]]*\"catalog:' \"$ROOT/package.json\" 2>/dev/null && [ -f \"$ROOT/pnpm-lock.yaml\" ]; then\n WP_CATALOGS=\"$(awk '\n { n=0; while (substr($0,n+1,1)==\" \") n++; c=substr($0,n+1) }\n c==\"\" { next }\n n==0 { incat=(c ~ /^catalogs: *$/)?1:0; cat=\"\"; pkg=\"\"; next }\n incat==0 { next }\n n==2 { cat=c; sub(/:.*/,\"\",cat); pkg=\"\"; next }\n n==4 { pkg=c; sub(/: *$/,\"\",pkg); gsub(/[\"\\047]/,\"\",pkg); next }\n n==6 && substr(pkg,1,11)==\"@webpieces/\" && c ~ /^version:/ {\n v=c; sub(/^version: */,\"\",v); gsub(/[\"\\047 ]/,\"\",v);\n if (cat!=\"\" && v!=\"\") print cat \" \" pkg \" \" v\n }\n ' \"$ROOT/pnpm-lock.yaml\" 2>/dev/null)\"\n fi\n while IFS=' ' read -r WP_NAME WP_DECL; do\n [ -n \"$WP_NAME\" ] || continue\n # Fault U's input: the package is DECLARED (in any spec shape, in any dependency block of the root\n # manifest). Recorded before every `continue` below, so a range or catalog spec still counts.\n [ \"$WP_NAME\" = \"agent-workflow-rules\" ] && WP_HOOK_PKG_DECLARED=1\n # Resolve the declared spec to an EXACT version, or skip it: ranges (^ ~ workspace:*) never drift,\n # and a catalog spec we cannot resolve is best-effort skipped rather than guessed.\n case \"$WP_DECL\" in\n catalog:*)\n WP_CAT=\"${WP_DECL#catalog:}\"; [ -n \"$WP_CAT\" ] || WP_CAT=\"default\"\n WP_DECL=\"$(printf '%s\\n' \"$WP_WS_CATALOGS\" | awk -v c=\"$WP_CAT\" -v p=\"@webpieces/$WP_NAME\" '$1==c && $2==p {print $3; exit}')\"\n [ -n \"$WP_DECL\" ] || WP_DECL=\"$(printf '%s\\n' \"$WP_CATALOGS\" | awk -v c=\"$WP_CAT\" -v p=\"@webpieces/$WP_NAME\" '$1==c && $2==p {print $3; exit}')\"\n [ -n \"$WP_DECL\" ] || continue ;;\n [0-9]*) : ;;\n *) continue ;;\n esac\n # The release the rest of this repo is on \u2014 what fault U's cure should pin to.\n [ -n \"$WP_PIN\" ] || WP_PIN=\"$WP_DECL\"\n WP_MANIFEST=\"$BIN_ROOT/node_modules/@webpieces/$WP_NAME/package.json\"\n [ -f \"$WP_MANIFEST\" ] || continue\n WP_INST=\"$(sed -n 's/.*\"version\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1/p' \"$WP_MANIFEST\" | head -n1)\"\n [ -n \"$WP_INST\" ] || continue\n if [ \"$WP_DECL\" != \"$WP_INST\" ]; then\n DRIFT_PKG=\"@webpieces/$WP_NAME\"\n DRIFT_DECLARED=\"$WP_DECL\"\n DRIFT_INSTALLED=\"$WP_INST\"\n break\n fi\n done <<WPEOF\n$(sed -n 's/.*\"@webpieces\\/\\([A-Za-z0-9._-]*\\)\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1 \\2/p' \"$ROOT/package.json\")\nWPEOF\nfi\n# A BORROWED BIN IS NOT SINGLE-TREE DRIFT, SO L0 MUST NOT ANSWER IT (2026-08-20).\n#\n# RESOLVE_BIN_SH walks UP for the bin, so in a linked worktree with no node_modules of its own BIN_ROOT\n# is the MAIN tree. The scan above then compares $ROOT's DECLARED pin against $BIN_ROOT's INSTALLED\n# version \u2014 a CROSS-TREE comparison it was reporting as fault D, single-tree drift. Everything\n# downstream of that mislabel was wrong:\n# \u2022 the cure. 'pnpm install' cannot \"align node_modules\" in a tree that has none; it MANUFACTURES one,\n# at this tree's stale pin. That is precisely the state L1 row 8 (trinary-version-skew) blocks, so\n# the L0 cure created the next block. One measured agent was walked from a pin disagreement, through\n# 'pnpm install' offered as \"(preferred) ... usually right\", to a DOWNGRADED engine and a total block.\n# \u2022 the analysis. Row 8 already reads all FOUR versions (both trees' pins and both installs), already\n# knows which direction to move, already detects a deliberate pin bump, and already carries the\n# escalate-and-STOP protocol. It could never run: ai-hook.sh hard-exits on any sh-side fault, so D\n# PREEMPTED the guard that had the right answer.\n# So: when the bin came from another tree, raise NO fault here and let the binary run. D stays exactly as\n# it was when ROOT == BIN_ROOT \u2014 that IS single-tree drift, and row 8 cannot see it (VersionSyncGuard\n# only applies to a linked worktree compared against a DIFFERENT main tree). Coverage does not gap: if\n# the two pins agree but the two installs differ, row 8's quartet still has two distinct members and it\n# still fires. X / U / K are untouched \u2014 a MISSING or CRASHED bin is about the bin, not about a version.\nif [ \"$BIN_ROOT\" != \"$ROOT\" ]; then\n DRIFT_PKG=\"\"\n DRIFT_DECLARED=\"\"\n DRIFT_INSTALLED=\"\"\nfi";
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VERSION_DRIFT_GUARD_SH = void 0;
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Shell fragment: L0's VERSION-DRIFT guard (fault D), in pure sh.
|
|
6
|
+
//
|
|
7
|
+
// Its own module for the same reason shim-audit-log.ts and shim-drift-fix.ts are: shim.ts renders the
|
|
8
|
+
// whole shim body and is at its file-size cap. It is spliced back into renderShim() verbatim,
|
|
9
|
+
// byte-for-byte, and it imports nothing — this has to run on a tree too broken to load the rule engine.
|
|
10
|
+
//
|
|
11
|
+
// The rationale for every branch is IN the fragment, as shell comments, and stays there: unlike
|
|
12
|
+
// shim-drift-fix.ts's Fix Options, this text is the guard's own reasoning about a tree it cannot
|
|
13
|
+
// otherwise explain, and a reader debugging a live block is reading the rendered .sh, not this file.
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
exports.VERSION_DRIFT_GUARD_SH = `# --- webpieces version-drift guard (pure sh — runs even when the installed guard bin is stale) -----
|
|
16
|
+
# The committed shim is version-agnostic, so it keeps working right after a git pull, BEFORE the
|
|
17
|
+
# matching pnpm install. That is exactly when node_modules can be STALE: an OLDER @webpieces than
|
|
18
|
+
# package.json now pins, whose outdated validator rejects the NEWER webpieces.config.json with baffling
|
|
19
|
+
# "unknown rule" errors. Detect that drift HERE (before exec'ing the possibly-stale bin): compare every
|
|
20
|
+
# EXACT-pinned @webpieces/* version in the root package.json against the version actually installed in
|
|
21
|
+
# node_modules; the first mismatch wins. Range specs (^ ~ workspace:*) are skipped, so they never
|
|
22
|
+
# false-positive; best-effort — a version we cannot read is skipped. On drift we fall through to the
|
|
23
|
+
# SAME fail-closed path as a missing bin (allow only pnpm install, deny the rest).
|
|
24
|
+
#
|
|
25
|
+
# pnpm CATALOGS: a dep pinned via "catalog:" / "catalog:<name>" carries NO digit-version in package.json,
|
|
26
|
+
# so the old scraper matched nothing and the guard was BLIND to it — DRIFT_PKG stayed empty and the
|
|
27
|
+
# stale bin ran (the 2026-07 "0.3.369 vs 0.4.405" incident). Resolve those specs through the top-level
|
|
28
|
+
# \`catalogs:\` block of pnpm-lock.yaml (catalog -> pkg -> resolved version) before comparing.
|
|
29
|
+
#
|
|
30
|
+
# THE SAME PASS ANSWERS FAULT U (2026-08-05). Scraping root package.json is also the only way to learn
|
|
31
|
+
# whether @webpieces/agent-workflow-rules is DECLARED at all, and that is the difference between "not installed
|
|
32
|
+
# yet" (X, cured by pnpm install) and "nothing asks for it" (U, where pnpm install is a guaranteed
|
|
33
|
+
# no-op). WP_PIN carries the first EXACT @webpieces pin found, so U's deny can prescribe the version the
|
|
34
|
+
# rest of the repo is already on rather than an unpinned add. Both are set BEFORE the range/catalog
|
|
35
|
+
# \`continue\`s, so a repo pinning the package by range still counts as having declared it.
|
|
36
|
+
DRIFT_PKG=""
|
|
37
|
+
DRIFT_DECLARED=""
|
|
38
|
+
DRIFT_INSTALLED=""
|
|
39
|
+
WP_HOOK_PKG_DECLARED=""
|
|
40
|
+
WP_PIN=""
|
|
41
|
+
if [ -f "$ROOT/package.json" ]; then
|
|
42
|
+
# Only when a @webpieces dep actually uses a "catalog:" spec do we scan the (possibly huge) lockfile —
|
|
43
|
+
# a cheap grep keeps the common, catalog-free repo from paying that cost on every tool call. One awk
|
|
44
|
+
# pass over pnpm-lock.yaml emits "<catalog> <@webpieces/pkg> <version>" lines for the sh lookup below;
|
|
45
|
+
# \\047 is a single quote (so this awk program carries none and stays safely single-quotable in sh).
|
|
46
|
+
WP_CATALOGS=""
|
|
47
|
+
WP_WS_CATALOGS=""
|
|
48
|
+
# THE PIN LIVES IN pnpm-workspace.yaml, and the LOCK is only the fallback (2026-08-20).
|
|
49
|
+
#
|
|
50
|
+
# L0 used to learn the pin from pnpm-lock.yaml's \`catalogs:\` alone, while L1's WebpiecesVersions.readPin
|
|
51
|
+
# reads pnpm-workspace.yaml. Two notions of "the pin", and the gap is exactly where the cure lands: an
|
|
52
|
+
# agent told to raise this tree's pin edits pnpm-workspace.yaml, re-runs, and L0 still reports the OLD
|
|
53
|
+
# number — because only \`pnpm install\` rewrites the lock — so it concludes the edit did nothing and
|
|
54
|
+
# reaches for something worse. Reading the workspace manifest FIRST makes the edit visible immediately.
|
|
55
|
+
#
|
|
56
|
+
# It resolves the same two YAML shapes readPin does, and that is not optional: a repo pinning the whole
|
|
57
|
+
# @webpieces family in lockstep writes the version ONCE as \`&wp 0.4.669\` and aliases the rest as \`*wp\`,
|
|
58
|
+
# so an anchor-blind read silently nulls the leg on precisely the repos that pin most carefully. Both
|
|
59
|
+
# \`catalog:\` (the default catalog) and \`catalogs:\` (named ones) are walked. A value that is not a plain
|
|
60
|
+
# digit-version (a range) is NOT emitted, so a loose pinner falls through to the lock rather than being
|
|
61
|
+
# compared against an incomparable spec.
|
|
62
|
+
if grep -Eq '"@webpieces/[^"]*"[[:space:]]*:[[:space:]]*"catalog:' "$ROOT/package.json" 2>/dev/null && [ -f "$ROOT/pnpm-workspace.yaml" ]; then
|
|
63
|
+
WP_WS_CATALOGS="$(awk '
|
|
64
|
+
{ n=0; while (substr($0,n+1,1)==" ") n++; c=substr($0,n+1) }
|
|
65
|
+
c=="" || substr(c,1,1)=="#" { next }
|
|
66
|
+
{
|
|
67
|
+
ai=index(c,":")
|
|
68
|
+
if (ai>0) {
|
|
69
|
+
av=substr(c,ai+1); sub(/^[ \\t]+/,"",av)
|
|
70
|
+
if (substr(av,1,1)=="&") {
|
|
71
|
+
an=substr(av,2); sub(/[ \\t].*/,"",an)
|
|
72
|
+
sub(/^&[^ \\t]+[ \\t]*/,"",av)
|
|
73
|
+
sub(/[ \\t]+#.*/,"",av); gsub(/["\\047]/,"",av); sub(/[ \\t].*/,"",av)
|
|
74
|
+
if (an!="" && av!="") anch[an]=av
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
n==0 { mode=(c ~ /^catalog: *$/)?1:((c ~ /^catalogs: *$/)?2:0); cat=(mode==1)?"default":""; next }
|
|
79
|
+
mode==0 { next }
|
|
80
|
+
mode==2 && c ~ /^[^:]+: *$/ { cat=c; sub(/: *$/,"",cat); gsub(/["\\047 ]/,"",cat); next }
|
|
81
|
+
{
|
|
82
|
+
ki=index(c,":")
|
|
83
|
+
if (ki<=0) next
|
|
84
|
+
k=substr(c,1,ki-1); gsub(/["\\047 ]/,"",k)
|
|
85
|
+
if (substr(k,1,11)!="@webpieces/") next
|
|
86
|
+
v=substr(c,ki+1); sub(/^[ \\t]+/,"",v); sub(/^&[^ \\t]+[ \\t]*/,"",v)
|
|
87
|
+
sub(/[ \\t]+#.*/,"",v); gsub(/["\\047]/,"",v); sub(/[ \\t].*/,"",v)
|
|
88
|
+
if (v=="") next
|
|
89
|
+
nn++; key[nn]=cat " " k; ali[nn]=(substr(v,1,1)=="*")?substr(v,2):""; val[nn]=v
|
|
90
|
+
}
|
|
91
|
+
END { for (i=1;i<=nn;i++) { vv=(ali[i]=="")?val[i]:anch[ali[i]]; if (vv ~ /^[0-9]/) print key[i] " " vv } }
|
|
92
|
+
' "$ROOT/pnpm-workspace.yaml" 2>/dev/null)"
|
|
93
|
+
fi
|
|
94
|
+
if grep -Eq '"@webpieces/[^"]*"[[:space:]]*:[[:space:]]*"catalog:' "$ROOT/package.json" 2>/dev/null && [ -f "$ROOT/pnpm-lock.yaml" ]; then
|
|
95
|
+
WP_CATALOGS="$(awk '
|
|
96
|
+
{ n=0; while (substr($0,n+1,1)==" ") n++; c=substr($0,n+1) }
|
|
97
|
+
c=="" { next }
|
|
98
|
+
n==0 { incat=(c ~ /^catalogs: *$/)?1:0; cat=""; pkg=""; next }
|
|
99
|
+
incat==0 { next }
|
|
100
|
+
n==2 { cat=c; sub(/:.*/,"",cat); pkg=""; next }
|
|
101
|
+
n==4 { pkg=c; sub(/: *$/,"",pkg); gsub(/["\\047]/,"",pkg); next }
|
|
102
|
+
n==6 && substr(pkg,1,11)=="@webpieces/" && c ~ /^version:/ {
|
|
103
|
+
v=c; sub(/^version: */,"",v); gsub(/["\\047 ]/,"",v);
|
|
104
|
+
if (cat!="" && v!="") print cat " " pkg " " v
|
|
105
|
+
}
|
|
106
|
+
' "$ROOT/pnpm-lock.yaml" 2>/dev/null)"
|
|
107
|
+
fi
|
|
108
|
+
while IFS=' ' read -r WP_NAME WP_DECL; do
|
|
109
|
+
[ -n "$WP_NAME" ] || continue
|
|
110
|
+
# Fault U's input: the package is DECLARED (in any spec shape, in any dependency block of the root
|
|
111
|
+
# manifest). Recorded before every \`continue\` below, so a range or catalog spec still counts.
|
|
112
|
+
[ "$WP_NAME" = "agent-workflow-rules" ] && WP_HOOK_PKG_DECLARED=1
|
|
113
|
+
# Resolve the declared spec to an EXACT version, or skip it: ranges (^ ~ workspace:*) never drift,
|
|
114
|
+
# and a catalog spec we cannot resolve is best-effort skipped rather than guessed.
|
|
115
|
+
case "$WP_DECL" in
|
|
116
|
+
catalog:*)
|
|
117
|
+
WP_CAT="\${WP_DECL#catalog:}"; [ -n "$WP_CAT" ] || WP_CAT="default"
|
|
118
|
+
WP_DECL="$(printf '%s\\n' "$WP_WS_CATALOGS" | awk -v c="$WP_CAT" -v p="@webpieces/$WP_NAME" '$1==c && $2==p {print $3; exit}')"
|
|
119
|
+
[ -n "$WP_DECL" ] || WP_DECL="$(printf '%s\\n' "$WP_CATALOGS" | awk -v c="$WP_CAT" -v p="@webpieces/$WP_NAME" '$1==c && $2==p {print $3; exit}')"
|
|
120
|
+
[ -n "$WP_DECL" ] || continue ;;
|
|
121
|
+
[0-9]*) : ;;
|
|
122
|
+
*) continue ;;
|
|
123
|
+
esac
|
|
124
|
+
# The release the rest of this repo is on — what fault U's cure should pin to.
|
|
125
|
+
[ -n "$WP_PIN" ] || WP_PIN="$WP_DECL"
|
|
126
|
+
WP_MANIFEST="$BIN_ROOT/node_modules/@webpieces/$WP_NAME/package.json"
|
|
127
|
+
[ -f "$WP_MANIFEST" ] || continue
|
|
128
|
+
WP_INST="$(sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\\([^"]*\\)".*/\\1/p' "$WP_MANIFEST" | head -n1)"
|
|
129
|
+
[ -n "$WP_INST" ] || continue
|
|
130
|
+
if [ "$WP_DECL" != "$WP_INST" ]; then
|
|
131
|
+
DRIFT_PKG="@webpieces/$WP_NAME"
|
|
132
|
+
DRIFT_DECLARED="$WP_DECL"
|
|
133
|
+
DRIFT_INSTALLED="$WP_INST"
|
|
134
|
+
break
|
|
135
|
+
fi
|
|
136
|
+
done <<WPEOF
|
|
137
|
+
$(sed -n 's/.*"@webpieces\\/\\([A-Za-z0-9._-]*\\)"[[:space:]]*:[[:space:]]*"\\([^"]*\\)".*/\\1 \\2/p' "$ROOT/package.json")
|
|
138
|
+
WPEOF
|
|
139
|
+
fi
|
|
140
|
+
# A BORROWED BIN IS NOT SINGLE-TREE DRIFT, SO L0 MUST NOT ANSWER IT (2026-08-20).
|
|
141
|
+
#
|
|
142
|
+
# RESOLVE_BIN_SH walks UP for the bin, so in a linked worktree with no node_modules of its own BIN_ROOT
|
|
143
|
+
# is the MAIN tree. The scan above then compares $ROOT's DECLARED pin against $BIN_ROOT's INSTALLED
|
|
144
|
+
# version — a CROSS-TREE comparison it was reporting as fault D, single-tree drift. Everything
|
|
145
|
+
# downstream of that mislabel was wrong:
|
|
146
|
+
# • the cure. 'pnpm install' cannot "align node_modules" in a tree that has none; it MANUFACTURES one,
|
|
147
|
+
# at this tree's stale pin. That is precisely the state L1 row 8 (trinary-version-skew) blocks, so
|
|
148
|
+
# the L0 cure created the next block. One measured agent was walked from a pin disagreement, through
|
|
149
|
+
# 'pnpm install' offered as "(preferred) ... usually right", to a DOWNGRADED engine and a total block.
|
|
150
|
+
# • the analysis. Row 8 already reads all FOUR versions (both trees' pins and both installs), already
|
|
151
|
+
# knows which direction to move, already detects a deliberate pin bump, and already carries the
|
|
152
|
+
# escalate-and-STOP protocol. It could never run: ai-hook.sh hard-exits on any sh-side fault, so D
|
|
153
|
+
# PREEMPTED the guard that had the right answer.
|
|
154
|
+
# So: when the bin came from another tree, raise NO fault here and let the binary run. D stays exactly as
|
|
155
|
+
# it was when ROOT == BIN_ROOT — that IS single-tree drift, and row 8 cannot see it (VersionSyncGuard
|
|
156
|
+
# only applies to a linked worktree compared against a DIFFERENT main tree). Coverage does not gap: if
|
|
157
|
+
# the two pins agree but the two installs differ, row 8's quartet still has two distinct members and it
|
|
158
|
+
# still fires. X / U / K are untouched — a MISSING or CRASHED bin is about the bin, not about a version.
|
|
159
|
+
if [ "$BIN_ROOT" != "$ROOT" ]; then
|
|
160
|
+
DRIFT_PKG=""
|
|
161
|
+
DRIFT_DECLARED=""
|
|
162
|
+
DRIFT_INSTALLED=""
|
|
163
|
+
fi`;
|
|
164
|
+
//# sourceMappingURL=shim-version-drift.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shim-version-drift.js","sourceRoot":"","sources":["../../../../../../packages/tooling/agent-workflow-rules/src/bin/shim-version-drift.ts"],"names":[],"mappings":";;;AAAA,8EAA8E;AAC9E,kEAAkE;AAClE,EAAE;AACF,sGAAsG;AACtG,8FAA8F;AAC9F,wGAAwG;AACxG,EAAE;AACF,gGAAgG;AAChG,iGAAiG;AACjG,qGAAqG;AACrG,8EAA8E;AACjE,QAAA,sBAAsB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoJnC,CAAC","sourcesContent":["// ---------------------------------------------------------------------------\n// Shell fragment: L0's VERSION-DRIFT guard (fault D), in pure sh.\n//\n// Its own module for the same reason shim-audit-log.ts and shim-drift-fix.ts are: shim.ts renders the\n// whole shim body and is at its file-size cap. It is spliced back into renderShim() verbatim,\n// byte-for-byte, and it imports nothing — this has to run on a tree too broken to load the rule engine.\n//\n// The rationale for every branch is IN the fragment, as shell comments, and stays there: unlike\n// shim-drift-fix.ts's Fix Options, this text is the guard's own reasoning about a tree it cannot\n// otherwise explain, and a reader debugging a live block is reading the rendered .sh, not this file.\n// ---------------------------------------------------------------------------\nexport const VERSION_DRIFT_GUARD_SH = `# --- webpieces version-drift guard (pure sh — runs even when the installed guard bin is stale) -----\n# The committed shim is version-agnostic, so it keeps working right after a git pull, BEFORE the\n# matching pnpm install. That is exactly when node_modules can be STALE: an OLDER @webpieces than\n# package.json now pins, whose outdated validator rejects the NEWER webpieces.config.json with baffling\n# \"unknown rule\" errors. Detect that drift HERE (before exec'ing the possibly-stale bin): compare every\n# EXACT-pinned @webpieces/* version in the root package.json against the version actually installed in\n# node_modules; the first mismatch wins. Range specs (^ ~ workspace:*) are skipped, so they never\n# false-positive; best-effort — a version we cannot read is skipped. On drift we fall through to the\n# SAME fail-closed path as a missing bin (allow only pnpm install, deny the rest).\n#\n# pnpm CATALOGS: a dep pinned via \"catalog:\" / \"catalog:<name>\" carries NO digit-version in package.json,\n# so the old scraper matched nothing and the guard was BLIND to it — DRIFT_PKG stayed empty and the\n# stale bin ran (the 2026-07 \"0.3.369 vs 0.4.405\" incident). Resolve those specs through the top-level\n# \\`catalogs:\\` block of pnpm-lock.yaml (catalog -> pkg -> resolved version) before comparing.\n#\n# THE SAME PASS ANSWERS FAULT U (2026-08-05). Scraping root package.json is also the only way to learn\n# whether @webpieces/agent-workflow-rules is DECLARED at all, and that is the difference between \"not installed\n# yet\" (X, cured by pnpm install) and \"nothing asks for it\" (U, where pnpm install is a guaranteed\n# no-op). WP_PIN carries the first EXACT @webpieces pin found, so U's deny can prescribe the version the\n# rest of the repo is already on rather than an unpinned add. Both are set BEFORE the range/catalog\n# \\`continue\\`s, so a repo pinning the package by range still counts as having declared it.\nDRIFT_PKG=\"\"\nDRIFT_DECLARED=\"\"\nDRIFT_INSTALLED=\"\"\nWP_HOOK_PKG_DECLARED=\"\"\nWP_PIN=\"\"\nif [ -f \"$ROOT/package.json\" ]; then\n # Only when a @webpieces dep actually uses a \"catalog:\" spec do we scan the (possibly huge) lockfile —\n # a cheap grep keeps the common, catalog-free repo from paying that cost on every tool call. One awk\n # pass over pnpm-lock.yaml emits \"<catalog> <@webpieces/pkg> <version>\" lines for the sh lookup below;\n # \\\\047 is a single quote (so this awk program carries none and stays safely single-quotable in sh).\n WP_CATALOGS=\"\"\n WP_WS_CATALOGS=\"\"\n # THE PIN LIVES IN pnpm-workspace.yaml, and the LOCK is only the fallback (2026-08-20).\n #\n # L0 used to learn the pin from pnpm-lock.yaml's \\`catalogs:\\` alone, while L1's WebpiecesVersions.readPin\n # reads pnpm-workspace.yaml. Two notions of \"the pin\", and the gap is exactly where the cure lands: an\n # agent told to raise this tree's pin edits pnpm-workspace.yaml, re-runs, and L0 still reports the OLD\n # number — because only \\`pnpm install\\` rewrites the lock — so it concludes the edit did nothing and\n # reaches for something worse. Reading the workspace manifest FIRST makes the edit visible immediately.\n #\n # It resolves the same two YAML shapes readPin does, and that is not optional: a repo pinning the whole\n # @webpieces family in lockstep writes the version ONCE as \\`&wp 0.4.669\\` and aliases the rest as \\`*wp\\`,\n # so an anchor-blind read silently nulls the leg on precisely the repos that pin most carefully. Both\n # \\`catalog:\\` (the default catalog) and \\`catalogs:\\` (named ones) are walked. A value that is not a plain\n # digit-version (a range) is NOT emitted, so a loose pinner falls through to the lock rather than being\n # compared against an incomparable spec.\n if grep -Eq '\"@webpieces/[^\"]*\"[[:space:]]*:[[:space:]]*\"catalog:' \"$ROOT/package.json\" 2>/dev/null && [ -f \"$ROOT/pnpm-workspace.yaml\" ]; then\n WP_WS_CATALOGS=\"$(awk '\n { n=0; while (substr($0,n+1,1)==\" \") n++; c=substr($0,n+1) }\n c==\"\" || substr(c,1,1)==\"#\" { next }\n {\n ai=index(c,\":\")\n if (ai>0) {\n av=substr(c,ai+1); sub(/^[ \\\\t]+/,\"\",av)\n if (substr(av,1,1)==\"&\") {\n an=substr(av,2); sub(/[ \\\\t].*/,\"\",an)\n sub(/^&[^ \\\\t]+[ \\\\t]*/,\"\",av)\n sub(/[ \\\\t]+#.*/,\"\",av); gsub(/[\"\\\\047]/,\"\",av); sub(/[ \\\\t].*/,\"\",av)\n if (an!=\"\" && av!=\"\") anch[an]=av\n }\n }\n }\n n==0 { mode=(c ~ /^catalog: *$/)?1:((c ~ /^catalogs: *$/)?2:0); cat=(mode==1)?\"default\":\"\"; next }\n mode==0 { next }\n mode==2 && c ~ /^[^:]+: *$/ { cat=c; sub(/: *$/,\"\",cat); gsub(/[\"\\\\047 ]/,\"\",cat); next }\n {\n ki=index(c,\":\")\n if (ki<=0) next\n k=substr(c,1,ki-1); gsub(/[\"\\\\047 ]/,\"\",k)\n if (substr(k,1,11)!=\"@webpieces/\") next\n v=substr(c,ki+1); sub(/^[ \\\\t]+/,\"\",v); sub(/^&[^ \\\\t]+[ \\\\t]*/,\"\",v)\n sub(/[ \\\\t]+#.*/,\"\",v); gsub(/[\"\\\\047]/,\"\",v); sub(/[ \\\\t].*/,\"\",v)\n if (v==\"\") next\n nn++; key[nn]=cat \" \" k; ali[nn]=(substr(v,1,1)==\"*\")?substr(v,2):\"\"; val[nn]=v\n }\n END { for (i=1;i<=nn;i++) { vv=(ali[i]==\"\")?val[i]:anch[ali[i]]; if (vv ~ /^[0-9]/) print key[i] \" \" vv } }\n ' \"$ROOT/pnpm-workspace.yaml\" 2>/dev/null)\"\n fi\n if grep -Eq '\"@webpieces/[^\"]*\"[[:space:]]*:[[:space:]]*\"catalog:' \"$ROOT/package.json\" 2>/dev/null && [ -f \"$ROOT/pnpm-lock.yaml\" ]; then\n WP_CATALOGS=\"$(awk '\n { n=0; while (substr($0,n+1,1)==\" \") n++; c=substr($0,n+1) }\n c==\"\" { next }\n n==0 { incat=(c ~ /^catalogs: *$/)?1:0; cat=\"\"; pkg=\"\"; next }\n incat==0 { next }\n n==2 { cat=c; sub(/:.*/,\"\",cat); pkg=\"\"; next }\n n==4 { pkg=c; sub(/: *$/,\"\",pkg); gsub(/[\"\\\\047]/,\"\",pkg); next }\n n==6 && substr(pkg,1,11)==\"@webpieces/\" && c ~ /^version:/ {\n v=c; sub(/^version: */,\"\",v); gsub(/[\"\\\\047 ]/,\"\",v);\n if (cat!=\"\" && v!=\"\") print cat \" \" pkg \" \" v\n }\n ' \"$ROOT/pnpm-lock.yaml\" 2>/dev/null)\"\n fi\n while IFS=' ' read -r WP_NAME WP_DECL; do\n [ -n \"$WP_NAME\" ] || continue\n # Fault U's input: the package is DECLARED (in any spec shape, in any dependency block of the root\n # manifest). Recorded before every \\`continue\\` below, so a range or catalog spec still counts.\n [ \"$WP_NAME\" = \"agent-workflow-rules\" ] && WP_HOOK_PKG_DECLARED=1\n # Resolve the declared spec to an EXACT version, or skip it: ranges (^ ~ workspace:*) never drift,\n # and a catalog spec we cannot resolve is best-effort skipped rather than guessed.\n case \"$WP_DECL\" in\n catalog:*)\n WP_CAT=\"\\${WP_DECL#catalog:}\"; [ -n \"$WP_CAT\" ] || WP_CAT=\"default\"\n WP_DECL=\"$(printf '%s\\\\n' \"$WP_WS_CATALOGS\" | awk -v c=\"$WP_CAT\" -v p=\"@webpieces/$WP_NAME\" '$1==c && $2==p {print $3; exit}')\"\n [ -n \"$WP_DECL\" ] || WP_DECL=\"$(printf '%s\\\\n' \"$WP_CATALOGS\" | awk -v c=\"$WP_CAT\" -v p=\"@webpieces/$WP_NAME\" '$1==c && $2==p {print $3; exit}')\"\n [ -n \"$WP_DECL\" ] || continue ;;\n [0-9]*) : ;;\n *) continue ;;\n esac\n # The release the rest of this repo is on — what fault U's cure should pin to.\n [ -n \"$WP_PIN\" ] || WP_PIN=\"$WP_DECL\"\n WP_MANIFEST=\"$BIN_ROOT/node_modules/@webpieces/$WP_NAME/package.json\"\n [ -f \"$WP_MANIFEST\" ] || continue\n WP_INST=\"$(sed -n 's/.*\"version\"[[:space:]]*:[[:space:]]*\"\\\\([^\"]*\\\\)\".*/\\\\1/p' \"$WP_MANIFEST\" | head -n1)\"\n [ -n \"$WP_INST\" ] || continue\n if [ \"$WP_DECL\" != \"$WP_INST\" ]; then\n DRIFT_PKG=\"@webpieces/$WP_NAME\"\n DRIFT_DECLARED=\"$WP_DECL\"\n DRIFT_INSTALLED=\"$WP_INST\"\n break\n fi\n done <<WPEOF\n$(sed -n 's/.*\"@webpieces\\\\/\\\\([A-Za-z0-9._-]*\\\\)\"[[:space:]]*:[[:space:]]*\"\\\\([^\"]*\\\\)\".*/\\\\1 \\\\2/p' \"$ROOT/package.json\")\nWPEOF\nfi\n# A BORROWED BIN IS NOT SINGLE-TREE DRIFT, SO L0 MUST NOT ANSWER IT (2026-08-20).\n#\n# RESOLVE_BIN_SH walks UP for the bin, so in a linked worktree with no node_modules of its own BIN_ROOT\n# is the MAIN tree. The scan above then compares $ROOT's DECLARED pin against $BIN_ROOT's INSTALLED\n# version — a CROSS-TREE comparison it was reporting as fault D, single-tree drift. Everything\n# downstream of that mislabel was wrong:\n# • the cure. 'pnpm install' cannot \"align node_modules\" in a tree that has none; it MANUFACTURES one,\n# at this tree's stale pin. That is precisely the state L1 row 8 (trinary-version-skew) blocks, so\n# the L0 cure created the next block. One measured agent was walked from a pin disagreement, through\n# 'pnpm install' offered as \"(preferred) ... usually right\", to a DOWNGRADED engine and a total block.\n# • the analysis. Row 8 already reads all FOUR versions (both trees' pins and both installs), already\n# knows which direction to move, already detects a deliberate pin bump, and already carries the\n# escalate-and-STOP protocol. It could never run: ai-hook.sh hard-exits on any sh-side fault, so D\n# PREEMPTED the guard that had the right answer.\n# So: when the bin came from another tree, raise NO fault here and let the binary run. D stays exactly as\n# it was when ROOT == BIN_ROOT — that IS single-tree drift, and row 8 cannot see it (VersionSyncGuard\n# only applies to a linked worktree compared against a DIFFERENT main tree). Coverage does not gap: if\n# the two pins agree but the two installs differ, row 8's quartet still has two distinct members and it\n# still fires. X / U / K are untouched — a MISSING or CRASHED bin is about the bin, not about a version.\nif [ \"$BIN_ROOT\" != \"$ROOT\" ]; then\n DRIFT_PKG=\"\"\n DRIFT_DECLARED=\"\"\n DRIFT_INSTALLED=\"\"\nfi`;\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * from './l0-entry';
|
|
2
|
+
export * from './l0-allowlist';
|
|
3
|
+
export * from './l0-decide';
|
|
4
|
+
export * from './l0-ignored-tools';
|
|
5
|
+
export * from './l0-codex-read';
|
|
6
|
+
export * from './shim-audit-log';
|
|
7
|
+
export declare const SHIM_MARKER = ".claude/webpieces/ai-hook.sh";
|
|
8
|
+
export declare function shimPath(projectRoot: string): string;
|
|
9
|
+
export declare const NO_CHAINING_RULE: string;
|
|
10
|
+
export declare function renderShim(): string;
|
|
11
|
+
export declare function findShimRoot(cwd: string): string | null;
|
|
12
|
+
export declare function healShim(cwd: string): void;
|
|
13
|
+
export declare function governingShimRoot(moduleDir?: string): string | null;
|
|
14
|
+
export declare function committedShimStale(root?: string | null): boolean;
|
|
15
|
+
export declare function isShimCureCommand(command: string): boolean;
|
|
16
|
+
export declare function installedShimRulesVersion(): string;
|