@webpieces/ai-hook-rules 0.4.822 → 0.4.824

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webpieces/ai-hook-rules",
3
- "version": "0.4.822",
3
+ "version": "0.4.824",
4
4
  "description": "Pluggable write-time validation framework for AI coding agents (@webpieces/ai-hook-rules). Claude Code PreToolUse + openclaw before_tool_call adapters share one rule engine.",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -25,7 +25,7 @@
25
25
  "directory": "packages/tooling/ai-hook-rules"
26
26
  },
27
27
  "dependencies": {
28
- "@webpieces/rules-config": "0.4.822",
28
+ "@webpieces/rules-config": "0.4.824",
29
29
  "inversify": "7.10.4",
30
30
  "reflect-metadata": "0.2.2"
31
31
  },
@@ -12,7 +12,7 @@ export declare class PrCreationOrPushGuardRule extends BashRuleBase<PrLifecycleG
12
12
  */
13
13
  private readonly upsertPrCommand;
14
14
  constructor(config: PrLifecycleGuardConfig, upsertPrCommand: string);
15
- readonly description = "Block manual `git push` and direct PR creation/edit (gh pr / gh api / curl) so pushes and PRs go only through the gated upsert-pr command.";
15
+ readonly description = "Block manual `git push` and direct PR creation/edit so AI uses the gated flow; a human may personally use the interactive wp-human-post-pr escape hatch.";
16
16
  get fixHint(): FixHint;
17
17
  check(ctx: BashContext): readonly Violation[];
18
18
  /**
@@ -17,25 +17,25 @@ const INSTRUCT_FILE = 'webpieces.git-workflow.md';
17
17
  * else the AI can see mentions it.
18
18
  */
19
19
  function fixHintFor(upsertPrCommand) {
20
- return new fix_hint_1.FixHint('Direct PR creation/update AND manual `git push` are blocked.', 'Never push or open/update a PR by hand. There are TWO gated destinations — pick by what you were\n'
21
- + 'actually trying to do:\n\n'
22
- + 'LANDING ON MAIN (a PR, prod-quality, reviewed):\n'
23
- + ` ${upsertPrCommand}\n`
24
- + ' It updates the branch from main (3-point merge) and runs the real build (nx affected), then\n'
25
- + ' instructs you to write summary.json and run `pnpm wp-finish-upsert-pr`, which assembles the\n'
26
- + ' dashboard and creates/updates the PR — and pushes for you. A failing build = no push, no PR.\n\n'
27
- + 'JUST WANT IT ON THE SHARED DEV SERVER (no PR, not landing on main):\n'
28
- + ` ${rules_config_1.WP_PUSH_DEV}\n`
29
- + ' It publishes a DISPOSABLE copy of your branch that the dev environment composes and rebuilds.\n'
30
- + ' Your feature branch is never moved and never acquires another developer\'s commits, which is\n'
31
- + ' the whole reason the copy exists. Do NOT open a PR to main just to test something in dev.\n\n'
32
- + 'Both push internally as child processes this hook never sees, so the gated commands are\n'
33
- + 'unaffected by this guard. There is nothing to paste or attest to; the commands do the work.\n'
34
- + 'If a HUMAN genuinely needs an out-of-band push (neither destination above), do NOT do it\n'
35
- + 'yourself — ask them to run the push, since a manual push bypasses the build gate, summary.json,\n'
36
- + 'and dashboard.\n'
37
- + 'Full branch → update → PR flow: READ the instruct-ai git-workflow doc at the absolute path on the violation line above.\n'
38
- + 'Add this to your memory so you don\'t forget next time and waste tokens.');
20
+ return new fix_hint_1.FixHint('Direct PR creation/update AND manual `git push` are blocked.', 'Never push or open/update a PR by hand. There are TWO gated destinations — pick by what you were\n' +
21
+ 'actually trying to do:\n\n' +
22
+ 'LANDING ON MAIN (a PR, prod-quality, reviewed):\n' +
23
+ ` ${upsertPrCommand}\n` +
24
+ ' It updates the branch from main (3-point merge) and runs the real build (nx affected), then\n' +
25
+ ' instructs you to write summary.json and run `pnpm wp-finish-upsert-pr`, which assembles the\n' +
26
+ ' dashboard and creates/updates the PR — and pushes for you. A failing build = no push, no PR.\n\n' +
27
+ 'JUST WANT IT ON THE SHARED DEV SERVER (no PR, not landing on main):\n' +
28
+ ` ${rules_config_1.WP_PUSH_DEV}\n` +
29
+ ' It publishes a DISPOSABLE copy of your branch that the dev environment composes and rebuilds.\n' +
30
+ " Your feature branch is never moved and never acquires another developer's commits, which is\n" +
31
+ ' the whole reason the copy exists. Do NOT open a PR to main just to test something in dev.\n\n' +
32
+ 'Both push internally as child processes this hook never sees, so the gated commands are\n' +
33
+ 'unaffected by this guard. There is nothing to paste or attest to; the commands do the work.\n' +
34
+ 'A HUMAN who has personally inspected the work may run `pnpm wp-human-post-pr` themselves.\n' +
35
+ 'That is an interactive human attestation and visibly skips automated build/review. AI must\n' +
36
+ "never run it for the human or answer `human` on the human's behalf.\n" +
37
+ 'Full branch → update → PR flow: READ the instruct-ai git-workflow doc at the absolute path on the violation line above.\n' +
38
+ "Add this to your memory so you don't forget next time and waste tokens.");
39
39
  }
40
40
  // Detect every way an agent could push or open/update a PR directly, so the ONLY path left is the
41
41
  // gated flow (wp-start-upsert-pr → wp-finish-upsert-pr, whose internal `git push` / `gh pr create`
@@ -48,11 +48,17 @@ function isBlockedPrOrPush(cmd) {
48
48
  if (/\bgh\s+pr\s+(create|edit)\b/.test(cmd))
49
49
  return true;
50
50
  const ghApiPulls = /\bgh\s+api\b[^\n]*\/pulls\b/.test(cmd);
51
- if (ghApiPulls && (/--method\s+POST/i.test(cmd) || /-X\s+POST/i.test(cmd) || /\s-f\b/.test(cmd) || /\s-F\b/.test(cmd) || /--field\b/.test(cmd))) {
51
+ if (ghApiPulls &&
52
+ (/--method\s+POST/i.test(cmd) ||
53
+ /-X\s+POST/i.test(cmd) ||
54
+ /\s-f\b/.test(cmd) ||
55
+ /\s-F\b/.test(cmd) ||
56
+ /--field\b/.test(cmd))) {
52
57
  return true;
53
58
  }
54
59
  const curlPulls = /\bcurl\b[^\n]*api\.github\.com[^\n]*\/pulls\b/.test(cmd);
55
- if (curlPulls && (/-X\s*POST/i.test(cmd) || /--request\s+POST/i.test(cmd) || /(\s-d\b|--data\b)/.test(cmd))) {
60
+ if (curlPulls &&
61
+ (/-X\s*POST/i.test(cmd) || /--request\s+POST/i.test(cmd) || /(\s-d\b|--data\b)/.test(cmd))) {
56
62
  return true;
57
63
  }
58
64
  return false;
@@ -78,7 +84,9 @@ function targetsDevEnvironment(cmd, namespace, devBranch) {
78
84
  return false;
79
85
  // Split on whitespace AND `:` so both halves of a `HEAD:<dest>` refspec are examined. Matching whole
80
86
  // tokens (never a substring) keeps a branch merely NAMED `develop` from reading as the dev branch.
81
- return push[1].split(/[\s:]+/).some((token) => token === devBranch || token === `refs/heads/${devBranch}`);
87
+ return push[1]
88
+ .split(/[\s:]+/)
89
+ .some((token) => token === devBranch || token === `refs/heads/${devBranch}`);
82
90
  }
83
91
  class PrCreationOrPushGuardRule extends rule_base_1.BashRuleBase {
84
92
  /**
@@ -93,8 +101,10 @@ class PrCreationOrPushGuardRule extends rule_base_1.BashRuleBase {
93
101
  super(config, 'pr-creation-or-push-guard', rules_config_1.PR_LIFECYCLE_GUARD_KEY);
94
102
  this.upsertPrCommand = upsertPrCommand;
95
103
  }
96
- description = 'Block manual `git push` and direct PR creation/edit (gh pr / gh api / curl) so pushes and PRs go only through the gated upsert-pr command.';
97
- get fixHint() { return fixHintFor(this.upsertPrCommand); }
104
+ description = 'Block manual `git push` and direct PR creation/edit so AI uses the gated flow; a human may personally use the interactive wp-human-post-pr escape hatch.';
105
+ get fixHint() {
106
+ return fixHintFor(this.upsertPrCommand);
107
+ }
98
108
  check(ctx) {
99
109
  if (!isBlockedPrOrPush(ctx.commandCode))
100
110
  return [];
@@ -104,11 +114,15 @@ class PrCreationOrPushGuardRule extends rule_base_1.BashRuleBase {
104
114
  (0, rules_config_1.writeTemplate)(ctx.workspaceRoot, INSTRUCT_FILE);
105
115
  const docPath = new rules_config_1.RepoRootFinder().instructAiDocPath(ctx.workspaceRoot, INSTRUCT_FILE);
106
116
  if (this.aimedAtDevEnvironment(ctx)) {
107
- return [new types_1.Violation(1, truncate(ctx.command), 'Manual push is blocked — but this one is aimed at the shared DEV environment, which has its own\n'
108
- + `gated command. Use \`${rules_config_1.WP_PUSH_DEV}\` (publishes a disposable copy; never moves your branch, never\n`
109
- + `opens a PR). Do NOT use ${this.upsertPrCommand} for this — that lands on main. Full flow: READ ${docPath}.`)];
117
+ return [
118
+ new types_1.Violation(1, truncate(ctx.command), 'Manual push is blocked — but this one is aimed at the shared DEV environment, which has its own\n' +
119
+ `gated command. Use \`${rules_config_1.WP_PUSH_DEV}\` (publishes a disposable copy; never moves your branch, never\n` +
120
+ `opens a PR). Do NOT use ${this.upsertPrCommand} for this — that lands on main. Full flow: READ ${docPath}.`),
121
+ ];
110
122
  }
111
- return [new types_1.Violation(1, truncate(ctx.command), `Manual push / direct PR is blocked — use the gated flow (${this.upsertPrCommand}). Full flow: READ ${docPath}.`)];
123
+ return [
124
+ new types_1.Violation(1, truncate(ctx.command), `Manual push / direct PR is blocked — AI uses the gated flow (${this.upsertPrCommand}). A human may personally run pnpm wp-human-post-pr; AI never answers its attestation. Full flow: READ ${docPath}.`),
125
+ ];
112
126
  }
113
127
  /**
114
128
  * Read the configured dev refs, falling back to the defaults when this repo has no `pr-gate` section
@@ -119,7 +133,9 @@ class PrCreationOrPushGuardRule extends rule_base_1.BashRuleBase {
119
133
  */
120
134
  aimedAtDevEnvironment(ctx) {
121
135
  const devDeploy = (0, rules_config_1.loadAndValidate)(ctx.governedRoot).prGate.devDeploy;
122
- return targetsDevEnvironment(ctx.commandCode, devDeploy.branchNamespace === '' ? rules_config_1.DEFAULT_DEV_BRANCH_NAMESPACE : devDeploy.branchNamespace, devDeploy.devBranch === '' ? rules_config_1.DEFAULT_DEV_BRANCH : devDeploy.devBranch);
136
+ return targetsDevEnvironment(ctx.commandCode, devDeploy.branchNamespace === ''
137
+ ? rules_config_1.DEFAULT_DEV_BRANCH_NAMESPACE
138
+ : devDeploy.branchNamespace, devDeploy.devBranch === '' ? rules_config_1.DEFAULT_DEV_BRANCH : devDeploy.devBranch);
123
139
  }
124
140
  }
125
141
  exports.PrCreationOrPushGuardRule = PrCreationOrPushGuardRule;
@@ -1 +1 @@
1
- {"version":3,"file":"pr-creation-or-push-guard.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/ai-hook-rules/src/core/rules/pr-creation-or-push-guard.ts"],"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,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;IAC/E,CAAC;CACJ;AAjDD,8DAiDC","sourcesContent":["import {\n DEFAULT_DEV_BRANCH,\n DEFAULT_DEV_BRANCH_NAMESPACE,\n PrLifecycleGuardConfig,\n PR_LIFECYCLE_GUARD_KEY,\n RepoRootFinder,\n WP_PUSH_DEV,\n loadAndValidate,\n writeTemplate,\n} from '@webpieces/rules-config';\n\nimport type { BashContext, Violation } from '../types';\nimport { Violation as V } from '../types';\nimport { BashRuleBase } from '../rule-base';\nimport { FixHint } from '../fix-hint';\n\nconst INSTRUCT_FILE = 'webpieces.git-workflow.md';\n\n/**\n * The hint BRANCHES ON INTENT, and that is not polish.\n *\n * There are now two legitimate destinations for a blocked push — land it on `main`, or get it onto the\n * shared dev server — and the block alone cannot tell them apart. The old flat hint said \"everything goes\n * through `wp-start-upsert-pr`\", so an AI told \"put my branch on dev\" would try `git push`, get blocked,\n * read this, and open a PR to main: the exact opposite of what it was asked to do, and the destructive\n * direction of the two. Naming both destinations is what makes the dev flow discoverable at all — nothing\n * else the AI can see mentions it.\n */\nfunction fixHintFor(upsertPrCommand: string): FixHint {\n return new FixHint(\n 'Direct PR creation/update AND manual `git push` are blocked.',\n 'Never push or open/update a PR by hand. There are TWO gated destinations — pick by what you were\\n'\n + 'actually trying to do:\\n\\n'\n + 'LANDING ON MAIN (a PR, prod-quality, reviewed):\\n'\n + ` ${upsertPrCommand}\\n`\n + ' It updates the branch from main (3-point merge) and runs the real build (nx affected), then\\n'\n + ' instructs you to write summary.json and run `pnpm wp-finish-upsert-pr`, which assembles the\\n'\n + ' dashboard and creates/updates the PR — and pushes for you. A failing build = no push, no PR.\\n\\n'\n + 'JUST WANT IT ON THE SHARED DEV SERVER (no PR, not landing on main):\\n'\n + ` ${WP_PUSH_DEV}\\n`\n + ' It publishes a DISPOSABLE copy of your branch that the dev environment composes and rebuilds.\\n'\n + ' Your feature branch is never moved and never acquires another developer\\'s commits, which is\\n'\n + ' the whole reason the copy exists. Do NOT open a PR to main just to test something in dev.\\n\\n'\n + 'Both push internally as child processes this hook never sees, so the gated commands are\\n'\n + 'unaffected by this guard. There is nothing to paste or attest to; the commands do the work.\\n'\n + 'If a HUMAN genuinely needs an out-of-band push (neither destination above), do NOT do it\\n'\n + 'yourself — ask them to run the push, since a manual push bypasses the build gate, summary.json,\\n'\n + 'and dashboard.\\n'\n + 'Full branch → update → PR flow: READ the instruct-ai git-workflow doc at the absolute path on the violation line above.\\n'\n + 'Add this to your memory so you don\\'t forget next time and waste tokens.',\n );\n}\n\n// Detect every way an agent could push or open/update a PR directly, so the ONLY path left is the\n// gated flow (wp-start-upsert-pr → wp-finish-upsert-pr, whose internal `git push` / `gh pr create`\n// run as child processes the hook never sees). Read-only `gh pr list` / `gh api .../pulls` GET are\n// intentionally allowed.\nfunction isBlockedPrOrPush(cmd: string): boolean {\n // A manual push is always blocked — the gated flow pushes for you behind the build gate.\n if (/\\bgit\\s+push\\b/.test(cmd)) return true;\n\n if (/\\bgh\\s+pr\\s+(create|edit)\\b/.test(cmd)) return true;\n\n const ghApiPulls = /\\bgh\\s+api\\b[^\\n]*\\/pulls\\b/.test(cmd);\n if (ghApiPulls && (/--method\\s+POST/i.test(cmd) || /-X\\s+POST/i.test(cmd) || /\\s-f\\b/.test(cmd) || /\\s-F\\b/.test(cmd) || /--field\\b/.test(cmd))) {\n return true;\n }\n\n const curlPulls = /\\bcurl\\b[^\\n]*api\\.github\\.com[^\\n]*\\/pulls\\b/.test(cmd);\n if (curlPulls && (/-X\\s*POST/i.test(cmd) || /--request\\s+POST/i.test(cmd) || /(\\s-d\\b|--data\\b)/.test(cmd))) {\n return true;\n }\n return false;\n}\n\nfunction truncate(s: string): string {\n const MAX = 120;\n return s.length <= MAX ? s : s.slice(0, MAX) + '…';\n}\n\n/**\n * Does this blocked push AIM at the shared dev environment?\n *\n * The refspec is the one place the AI already stated its intent unambiguously, so reading it is strictly\n * better than making the AI choose between two hints it has no way to disambiguate. A push whose\n * destination is the dev namespace or the dev branch itself gets the `wp-push-dev` remedy on the `→`\n * line; everything else keeps the PR-flow remedy it has always had.\n */\n// webpieces-disable no-function-outside-class -- module-level guard helper, matches the rest of this file\nfunction targetsDevEnvironment(cmd: string, namespace: string, devBranch: string): boolean {\n if (cmd.includes(`${namespace}/`)) return true;\n const push = /\\bgit\\s+push\\b([^\\n;&|]*)/.exec(cmd);\n if (push === null) return false;\n // Split on whitespace AND `:` so both halves of a `HEAD:<dest>` refspec are examined. Matching whole\n // tokens (never a substring) keeps a branch merely NAMED `develop` from reading as the dev branch.\n return push[1].split(/[\\s:]+/).some(\n (token: string): boolean => token === devBranch || token === `refs/heads/${devBranch}`);\n}\n\nexport class PrCreationOrPushGuardRule extends BashRuleBase<PrLifecycleGuardConfig> {\n /**\n * The gated command this guard names, handed in by the LOADER from `commands.guardHints\n * .prCreationOrPush` (already defaulted there). It is a constructor parameter, not a config field:\n * the field `pr-creation-or-push-guard.upsertPrCommand` was a SECOND SPELLING that beat the\n * commands section at the point of use, and the loader's name-keyed injection that fed it could\n * miss a rename without failing the build. One spelling, wired at compile time.\n */\n private readonly upsertPrCommand: string;\n\n constructor(config: PrLifecycleGuardConfig, upsertPrCommand: string) {\n super(config, 'pr-creation-or-push-guard', PR_LIFECYCLE_GUARD_KEY);\n this.upsertPrCommand = upsertPrCommand;\n }\n\n readonly description = 'Block manual `git push` and direct PR creation/edit (gh pr / gh api / curl) so pushes and PRs go only through the gated upsert-pr command.';\n get fixHint(): FixHint { return fixHintFor(this.upsertPrCommand); }\n\n check(ctx: BashContext): readonly Violation[] {\n if (!isBlockedPrOrPush(ctx.commandCode)) return [];\n // Materialize the doc we are about to send the AI to. Pointing at a path that does not exist\n // (nothing has run a `wp-*` command in this tree yet) costs the AI a turn to discover; a STALE\n // copy from an older @webpieces is just as bad, so overwrite rather than write-if-missing.\n writeTemplate(ctx.workspaceRoot, INSTRUCT_FILE);\n const docPath = new RepoRootFinder().instructAiDocPath(ctx.workspaceRoot, INSTRUCT_FILE);\n if (this.aimedAtDevEnvironment(ctx)) {\n return [new V(1, truncate(ctx.command),\n 'Manual push is blocked — but this one is aimed at the shared DEV environment, which has its own\\n'\n + `gated command. Use \\`${WP_PUSH_DEV}\\` (publishes a disposable copy; never moves your branch, never\\n`\n + `opens a PR). Do NOT use ${this.upsertPrCommand} for this — that lands on main. Full flow: READ ${docPath}.`)];\n }\n return [new V(1, truncate(ctx.command),\n `Manual push / direct PR is blocked — use the gated flow (${this.upsertPrCommand}). Full flow: READ ${docPath}.`)];\n }\n\n /**\n * Read the configured dev refs, falling back to the defaults when this repo has no `pr-gate` section\n * at all (a consumer that has not adopted the gate still gets a correct hint for the default names).\n *\n * Calling the loader here is safe by construction: an INVALID config blocks every Bash call long\n * before a rule runs, so reaching this line already means the config loaded.\n */\n private aimedAtDevEnvironment(ctx: BashContext): boolean {\n const devDeploy = loadAndValidate(ctx.governedRoot).prGate.devDeploy;\n return targetsDevEnvironment(\n ctx.commandCode,\n devDeploy.branchNamespace === '' ? DEFAULT_DEV_BRANCH_NAMESPACE : devDeploy.branchNamespace,\n devDeploy.devBranch === '' ? DEFAULT_DEV_BRANCH : devDeploy.devBranch);\n }\n}\n"]}
1
+ {"version":3,"file":"pr-creation-or-push-guard.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/ai-hook-rules/src/core/rules/pr-creation-or-push-guard.ts"],"names":[],"mappings":";;;AAAA,0DASiC;AAGjC,oCAA0C;AAC1C,4CAA4C;AAC5C,0CAAsC;AAEtC,MAAM,aAAa,GAAG,2BAA2B,CAAC;AAElD;;;;;;;;;GASG;AACH,SAAS,UAAU,CAAC,eAAuB;IACvC,OAAO,IAAI,kBAAO,CACd,8DAA8D,EAC9D,oGAAoG;QAChG,4BAA4B;QAC5B,mDAAmD;QACnD,KAAK,eAAe,IAAI;QACxB,iGAAiG;QACjG,iGAAiG;QACjG,oGAAoG;QACpG,uEAAuE;QACvE,KAAK,0BAAW,IAAI;QACpB,mGAAmG;QACnG,iGAA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AAa,EAAE,aAAa,CAAC,CAAC;QACzF,IAAI,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,EAAE,CAAC;YAClC,OAAO;gBACH,IAAI,iBAAC,CACD,CAAC,EACD,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EACrB,mGAAmG;oBAC/F,wBAAwB,0BAAW,mEAAmE;oBACtG,2BAA2B,IAAI,CAAC,eAAe,mDAAmD,OAAO,GAAG,CACnH;aACJ,CAAC;QACN,CAAC;QACD,OAAO;YACH,IAAI,iBAAC,CACD,CAAC,EACD,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EACrB,gEAAgE,IAAI,CAAC,eAAe,0GAA0G,OAAO,GAAG,CAC3M;SACJ,CAAC;IACN,CAAC;IAED;;;;;;OAMG;IACK,qBAAqB,CAAC,GAAgB;QAC1C,MAAM,SAAS,GAAG,IAAA,8BAAe,EAAC,GAAG,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC;QACrE,OAAO,qBAAqB,CACxB,GAAG,CAAC,WAAW,EACf,SAAS,CAAC,eAAe,KAAK,EAAE;YAC5B,CAAC,CAAC,2CAA4B;YAC9B,CAAC,CAAC,SAAS,CAAC,eAAe,EAC/B,SAAS,CAAC,SAAS,KAAK,EAAE,CAAC,CAAC,CAAC,iCAAkB,CAAC,CAAC,CAAC,SAAS,CAAC,SAAS,CACxE,CAAC;IACN,CAAC;CACJ;AAjED,8DAiEC","sourcesContent":["import {\n DEFAULT_DEV_BRANCH,\n DEFAULT_DEV_BRANCH_NAMESPACE,\n PrLifecycleGuardConfig,\n PR_LIFECYCLE_GUARD_KEY,\n RepoRootFinder,\n WP_PUSH_DEV,\n loadAndValidate,\n writeTemplate,\n} from '@webpieces/rules-config';\n\nimport type { BashContext, Violation } from '../types';\nimport { Violation as V } from '../types';\nimport { BashRuleBase } from '../rule-base';\nimport { FixHint } from '../fix-hint';\n\nconst INSTRUCT_FILE = 'webpieces.git-workflow.md';\n\n/**\n * The hint BRANCHES ON INTENT, and that is not polish.\n *\n * There are now two legitimate destinations for a blocked push — land it on `main`, or get it onto the\n * shared dev server — and the block alone cannot tell them apart. The old flat hint said \"everything goes\n * through `wp-start-upsert-pr`\", so an AI told \"put my branch on dev\" would try `git push`, get blocked,\n * read this, and open a PR to main: the exact opposite of what it was asked to do, and the destructive\n * direction of the two. Naming both destinations is what makes the dev flow discoverable at all — nothing\n * else the AI can see mentions it.\n */\nfunction fixHintFor(upsertPrCommand: string): FixHint {\n return new FixHint(\n 'Direct PR creation/update AND manual `git push` are blocked.',\n 'Never push or open/update a PR by hand. There are TWO gated destinations — pick by what you were\\n' +\n 'actually trying to do:\\n\\n' +\n 'LANDING ON MAIN (a PR, prod-quality, reviewed):\\n' +\n ` ${upsertPrCommand}\\n` +\n ' It updates the branch from main (3-point merge) and runs the real build (nx affected), then\\n' +\n ' instructs you to write summary.json and run `pnpm wp-finish-upsert-pr`, which assembles the\\n' +\n ' dashboard and creates/updates the PR — and pushes for you. A failing build = no push, no PR.\\n\\n' +\n 'JUST WANT IT ON THE SHARED DEV SERVER (no PR, not landing on main):\\n' +\n ` ${WP_PUSH_DEV}\\n` +\n ' It publishes a DISPOSABLE copy of your branch that the dev environment composes and rebuilds.\\n' +\n \" Your feature branch is never moved and never acquires another developer's commits, which is\\n\" +\n ' the whole reason the copy exists. Do NOT open a PR to main just to test something in dev.\\n\\n' +\n 'Both push internally as child processes this hook never sees, so the gated commands are\\n' +\n 'unaffected by this guard. There is nothing to paste or attest to; the commands do the work.\\n' +\n 'A HUMAN who has personally inspected the work may run `pnpm wp-human-post-pr` themselves.\\n' +\n 'That is an interactive human attestation and visibly skips automated build/review. AI must\\n' +\n \"never run it for the human or answer `human` on the human's behalf.\\n\" +\n 'Full branch → update → PR flow: READ the instruct-ai git-workflow doc at the absolute path on the violation line above.\\n' +\n \"Add this to your memory so you don't forget next time and waste tokens.\",\n );\n}\n\n// Detect every way an agent could push or open/update a PR directly, so the ONLY path left is the\n// gated flow (wp-start-upsert-pr → wp-finish-upsert-pr, whose internal `git push` / `gh pr create`\n// run as child processes the hook never sees). Read-only `gh pr list` / `gh api .../pulls` GET are\n// intentionally allowed.\nfunction isBlockedPrOrPush(cmd: string): boolean {\n // A manual push is always blocked — the gated flow pushes for you behind the build gate.\n if (/\\bgit\\s+push\\b/.test(cmd)) return true;\n\n if (/\\bgh\\s+pr\\s+(create|edit)\\b/.test(cmd)) return true;\n\n const ghApiPulls = /\\bgh\\s+api\\b[^\\n]*\\/pulls\\b/.test(cmd);\n if (\n ghApiPulls &&\n (/--method\\s+POST/i.test(cmd) ||\n /-X\\s+POST/i.test(cmd) ||\n /\\s-f\\b/.test(cmd) ||\n /\\s-F\\b/.test(cmd) ||\n /--field\\b/.test(cmd))\n ) {\n return true;\n }\n\n const curlPulls = /\\bcurl\\b[^\\n]*api\\.github\\.com[^\\n]*\\/pulls\\b/.test(cmd);\n if (\n curlPulls &&\n (/-X\\s*POST/i.test(cmd) || /--request\\s+POST/i.test(cmd) || /(\\s-d\\b|--data\\b)/.test(cmd))\n ) {\n return true;\n }\n return false;\n}\n\nfunction truncate(s: string): string {\n const MAX = 120;\n return s.length <= MAX ? s : s.slice(0, MAX) + '…';\n}\n\n/**\n * Does this blocked push AIM at the shared dev environment?\n *\n * The refspec is the one place the AI already stated its intent unambiguously, so reading it is strictly\n * better than making the AI choose between two hints it has no way to disambiguate. A push whose\n * destination is the dev namespace or the dev branch itself gets the `wp-push-dev` remedy on the `→`\n * line; everything else keeps the PR-flow remedy it has always had.\n */\n// webpieces-disable no-function-outside-class -- module-level guard helper, matches the rest of this file\nfunction targetsDevEnvironment(cmd: string, namespace: string, devBranch: string): boolean {\n if (cmd.includes(`${namespace}/`)) return true;\n const push = /\\bgit\\s+push\\b([^\\n;&|]*)/.exec(cmd);\n if (push === null) return false;\n // Split on whitespace AND `:` so both halves of a `HEAD:<dest>` refspec are examined. Matching whole\n // tokens (never a substring) keeps a branch merely NAMED `develop` from reading as the dev branch.\n return push[1]\n .split(/[\\s:]+/)\n .some(\n (token: string): boolean => token === devBranch || token === `refs/heads/${devBranch}`,\n );\n}\n\nexport class PrCreationOrPushGuardRule extends BashRuleBase<PrLifecycleGuardConfig> {\n /**\n * The gated command this guard names, handed in by the LOADER from `commands.guardHints\n * .prCreationOrPush` (already defaulted there). It is a constructor parameter, not a config field:\n * the field `pr-creation-or-push-guard.upsertPrCommand` was a SECOND SPELLING that beat the\n * commands section at the point of use, and the loader's name-keyed injection that fed it could\n * miss a rename without failing the build. One spelling, wired at compile time.\n */\n private readonly upsertPrCommand: string;\n\n constructor(config: PrLifecycleGuardConfig, upsertPrCommand: string) {\n super(config, 'pr-creation-or-push-guard', PR_LIFECYCLE_GUARD_KEY);\n this.upsertPrCommand = upsertPrCommand;\n }\n\n readonly description =\n 'Block manual `git push` and direct PR creation/edit so AI uses the gated flow; a human may personally use the interactive wp-human-post-pr escape hatch.';\n get fixHint(): FixHint {\n return fixHintFor(this.upsertPrCommand);\n }\n\n check(ctx: BashContext): readonly Violation[] {\n if (!isBlockedPrOrPush(ctx.commandCode)) return [];\n // Materialize the doc we are about to send the AI to. Pointing at a path that does not exist\n // (nothing has run a `wp-*` command in this tree yet) costs the AI a turn to discover; a STALE\n // copy from an older @webpieces is just as bad, so overwrite rather than write-if-missing.\n writeTemplate(ctx.workspaceRoot, INSTRUCT_FILE);\n const docPath = new RepoRootFinder().instructAiDocPath(ctx.workspaceRoot, INSTRUCT_FILE);\n if (this.aimedAtDevEnvironment(ctx)) {\n return [\n new V(\n 1,\n truncate(ctx.command),\n 'Manual push is blocked — but this one is aimed at the shared DEV environment, which has its own\\n' +\n `gated command. Use \\`${WP_PUSH_DEV}\\` (publishes a disposable copy; never moves your branch, never\\n` +\n `opens a PR). Do NOT use ${this.upsertPrCommand} for this — that lands on main. Full flow: READ ${docPath}.`,\n ),\n ];\n }\n return [\n new V(\n 1,\n truncate(ctx.command),\n `Manual push / direct PR is blocked — AI uses the gated flow (${this.upsertPrCommand}). A human may personally run pnpm wp-human-post-pr; AI never answers its attestation. Full flow: READ ${docPath}.`,\n ),\n ];\n }\n\n /**\n * Read the configured dev refs, falling back to the defaults when this repo has no `pr-gate` section\n * at all (a consumer that has not adopted the gate still gets a correct hint for the default names).\n *\n * Calling the loader here is safe by construction: an INVALID config blocks every Bash call long\n * before a rule runs, so reaching this line already means the config loaded.\n */\n private aimedAtDevEnvironment(ctx: BashContext): boolean {\n const devDeploy = loadAndValidate(ctx.governedRoot).prGate.devDeploy;\n return targetsDevEnvironment(\n ctx.commandCode,\n devDeploy.branchNamespace === ''\n ? DEFAULT_DEV_BRANCH_NAMESPACE\n : devDeploy.branchNamespace,\n devDeploy.devBranch === '' ? DEFAULT_DEV_BRANCH : devDeploy.devBranch,\n );\n }\n}\n"]}