@webpieces/rules-config 0.3.203 → 0.3.204

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/rules-config",
3
- "version": "0.3.203",
3
+ "version": "0.3.204",
4
4
  "description": "Shared webpieces.config.json loader. Single source of truth for validation rule configuration consumed by @webpieces/ai-hook-rules, @webpieces/code-rules, and @webpieces/nx-webpieces-rules.",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -15,7 +15,8 @@ export declare class MainSyncStatus {
15
15
  export declare class MainSyncLock {
16
16
  state: string;
17
17
  started: number;
18
- constructor(state: string, started: number);
18
+ pid: number;
19
+ constructor(state: string, started: number, pid?: number);
19
20
  }
20
21
  export declare function mainSyncStatusPath(repoRoot: string): string;
21
22
  export declare function mainSyncLockPath(repoRoot: string): string;
@@ -25,7 +26,7 @@ export declare function readMainSyncLock(repoRoot: string): MainSyncLock | null;
25
26
  export declare function writeMainSyncLock(repoRoot: string, lock: MainSyncLock): void;
26
27
  export declare function isLockStale(lock: MainSyncLock, hangTimeoutMinutes: number, now?: number): boolean;
27
28
  export declare function isRefreshInProgress(repoRoot: string, hangTimeoutMinutes: number, now?: number): boolean;
28
- export declare function inProcessLock(now?: number): MainSyncLock;
29
+ export declare function inProcessLock(now?: number, pid?: number): MainSyncLock;
29
30
  export declare function finishedLock(started: number): MainSyncLock;
30
31
  /**
31
32
  * The SLOW path, run only inside the detached refresher. Computes every cached signal the
@@ -61,13 +61,18 @@ class MainSyncStatus {
61
61
  }
62
62
  }
63
63
  exports.MainSyncStatus = MainSyncStatus;
64
- // Concurrency state machine for the detached refresher. `started` is epoch milliseconds.
64
+ // Concurrency state machine for the detached refresher. `started` is epoch milliseconds. `pid` is
65
+ // the refresher process's pid (0 = unknown, e.g. a lock written by an older version) — used so a
66
+ // refresher that was KILLED before writing its finished lock (SIGKILL skips the finally) doesn't
67
+ // wedge `inprocess` for the whole hangTimeout: if its pid is gone, the lock is reclaimable now.
65
68
  class MainSyncLock {
66
69
  state;
67
70
  started;
68
- constructor(state, started) {
71
+ pid;
72
+ constructor(state, started, pid = 0) {
69
73
  this.state = state;
70
74
  this.started = started;
75
+ this.pid = pid;
71
76
  }
72
77
  }
73
78
  exports.MainSyncLock = MainSyncLock;
@@ -109,7 +114,7 @@ function readMainSyncLock(repoRoot) {
109
114
  if (!fs.existsSync(lockPath))
110
115
  return null;
111
116
  const raw = JSON.parse(fs.readFileSync(lockPath, 'utf8'));
112
- return new MainSyncLock(raw.state ?? LOCK_STATE_FINISHED, raw.started ?? 0);
117
+ return new MainSyncLock(raw.state ?? LOCK_STATE_FINISHED, raw.started ?? 0, raw.pid ?? 0);
113
118
  }
114
119
  catch (err) {
115
120
  const error = (0, to_error_1.toError)(err);
@@ -127,21 +132,42 @@ function writeMainSyncLock(repoRoot, lock) {
127
132
  function isLockStale(lock, hangTimeoutMinutes, now = Date.now()) {
128
133
  return now - lock.started > hangTimeoutMinutes * 60 * 1000;
129
134
  }
135
+ // Liveness probe: is `pid` still a running process? `process.kill(pid, 0)` sends no signal, it only
136
+ // tests existence — ESRCH means the process is gone, EPERM means it exists but isn't ours (alive).
137
+ // pid <= 0 means "unknown" (an old lock with no pid) → assume alive and fall back to staleness only.
138
+ function isProcessAlive(pid) {
139
+ if (pid <= 0)
140
+ return true;
141
+ // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
142
+ try {
143
+ process.kill(pid, 0);
144
+ return true;
145
+ }
146
+ catch (err) {
147
+ const error = (0, to_error_1.toError)(err);
148
+ // ESRCH = no such process (dead); anything else (e.g. EPERM = exists, not ours) = alive.
149
+ return !error.message.includes('ESRCH');
150
+ }
151
+ }
130
152
  // True when another refresher is actively running and we should NOT start a second one. A finished
131
- // lock, a missing lock, or a stale (hung) inprocess lock all return false.
153
+ // lock, a missing lock, a stale (hung) inprocess lock, OR an inprocess lock whose refresher pid is
154
+ // already dead (it was killed before writing its finished lock) all return false — so a killed
155
+ // refresher never wedges refreshes for the full hangTimeout.
132
156
  function isRefreshInProgress(repoRoot, hangTimeoutMinutes, now = Date.now()) {
133
157
  const lock = readMainSyncLock(repoRoot);
134
158
  if (!lock)
135
159
  return false;
136
160
  if (lock.state !== LOCK_STATE_INPROCESS)
137
161
  return false;
138
- return !isLockStale(lock, hangTimeoutMinutes, now);
162
+ if (isLockStale(lock, hangTimeoutMinutes, now))
163
+ return false;
164
+ return isProcessAlive(lock.pid);
139
165
  }
140
- function inProcessLock(now = Date.now()) {
141
- return new MainSyncLock(LOCK_STATE_INPROCESS, now);
166
+ function inProcessLock(now = Date.now(), pid = process.pid) {
167
+ return new MainSyncLock(LOCK_STATE_INPROCESS, now, pid);
142
168
  }
143
169
  function finishedLock(started) {
144
- return new MainSyncLock(LOCK_STATE_FINISHED, started);
170
+ return new MainSyncLock(LOCK_STATE_FINISHED, started, 0);
145
171
  }
146
172
  // Run a command capturing trimmed stdout; ok=false on spawn failure or non-zero exit.
147
173
  function capture(repoRoot, cmd, args) {
@@ -1 +1 @@
1
- {"version":3,"file":"main-sync-status.js","sourceRoot":"","sources":["../../../../../packages/tooling/rules-config/src/main-sync-status.ts"],"names":[],"mappings":";;;AAqGA,gDAEC;AAED,4CAEC;AAQD,gDAuBC;AAED,kDAIC;AAED,4CAYC;AAED,8CAIC;AAID,kCAEC;AAID,kDAKC;AAED,sCAEC;AAED,oCAEC;AA2CD,sDAuCC;AAMD,kDASC;AAKD,4DAeC;;AAhTD,iDAA0C;AAC1C,+CAAyB;AACzB,mDAA6B;AAE7B,2CAAgD;AAChD,yCAAqC;AAErC,oGAAoG;AACpG,qGAAqG;AACrG,2FAA2F;AAC3F,oGAAoG;AACpG,mGAAmG;AACnG,yDAAyD;AAEzD,mGAAmG;AACnG,6FAA6F;AAChF,QAAA,4BAA4B,GAAG,CAAC,CAAC;AAE9C,MAAM,qBAAqB,GAAG,uBAAuB,CAAC;AACtD,MAAM,mBA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wC;QACxC,iDAAiD,aAAa,KAAK;QACnE,oDAAoD,aAAa,EAAE;QACnE,4EAA4E,aAAa,GAAG;QAC5F,uFAAuF;KAC1F,CAAC;AACN,CAAC;AAED,kGAAkG;AAClG,oGAAoG;AACpG,iGAAiG;AACjG,SAAgB,wBAAwB,CAAC,QAAgB;IACrD,8DAA8D;IAC9D,IAAI,CAAC;QACD,MAAM,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;QACnC,MAAM,UAAU,GAAG,OAAO,CAAC,QAAQ,EAAE,KAAK,EAAE,CAAC,WAAW,EAAE,aAAa,CAAC,CAAC,CAAC;QAC1E,MAAM,WAAW,GAAG,OAAO,CAAC,QAAQ,EAAE,KAAK,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC,CAAC;QACpE,IAAI,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC,WAAW,CAAC,EAAE;YAAE,OAAO;QAC9C,MAAM,MAAM,GAAG,IAAI,cAAc,CAC7B,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,UAAU,CAAC,GAAG,EAAE,UAAU,CAAC,GAAG,EAAE,WAAW,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAChH,CAAC;QACF,mBAAmB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC1C,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACpB,MAAM,KAAK,GAAG,IAAA,kBAAO,EAAC,GAAG,CAAC,CAAC;QAC3B,KAAK,KAAK,CAAC;IACf,CAAC;AACL,CAAC","sourcesContent":["import { spawnSync } from 'child_process';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nimport { WEBPIECES_TMP_DIR } from './constants';\nimport { toError } from './to-error';\n\n// Shared \"is my feature branch healthy relative to origin/main?\" state. The SLOW signals (git fetch\n// + merge-base + same-file-overlap + a merged-PR lookup) are computed by the ai-hook-rules refresher\n// in a DETACHED background process so the PreToolUse hook never blocks on the network. The\n// feature-branch-guard then only READS this cached file (instant). pr-gate's merge flow also writes\n// it synchronously after a merge so the next edit is unblocked immediately. Lives here (the shared\n// dep of both packages) so neither depends on the other.\n\n// How long an `inprocess` refresher lock may sit before a new refresher assumes the prior run hung\n// and proceeds anyway. Overridable per-rule via FeatureBranchGuardConfig.hangTimeoutMinutes.\nexport const DEFAULT_HANG_TIMEOUT_MINUTES = 5;\n\nconst MAIN_SYNC_STATUS_FILE = 'main-sync-status.json';\nconst MAIN_SYNC_LOCK_FILE = 'main-sync.lock.json';\n\nconst LOCK_STATE_INPROCESS = 'inprocess';\nconst LOCK_STATE_FINISHED = 'finished';\n\n// Data-only (per CLAUDE.md, classes for data). `forkPoint` is null exactly when `hasForkPoint` is\n// false (no merge-base with origin/main). `branchAlreadyMerged` flags that this feature branch was\n// already merged into main (a merged PR exists) — the \"you're working on a finished branch\" case.\nexport class MainSyncStatus {\n branch: string;\n branchAlreadyMerged: boolean;\n mergedPr: string;\n hasForkPoint: boolean;\n forkPoint: string | null;\n originMain: string;\n featureHead: string;\n conflict: boolean;\n conflictFiles: string[];\n timestamp: string;\n\n constructor(\n branch: string,\n branchAlreadyMerged: boolean,\n mergedPr: string,\n hasForkPoint: boolean,\n forkPoint: string | null,\n originMain: string,\n featureHead: string,\n conflict: boolean,\n conflictFiles: string[],\n timestamp: string,\n ) {\n this.branch = branch;\n this.branchAlreadyMerged = branchAlreadyMerged;\n this.mergedPr = mergedPr;\n this.hasForkPoint = hasForkPoint;\n this.forkPoint = forkPoint;\n this.originMain = originMain;\n this.featureHead = featureHead;\n this.conflict = conflict;\n this.conflictFiles = conflictFiles;\n this.timestamp = timestamp;\n }\n}\n\n// Concurrency state machine for the detached refresher. `started` is epoch milliseconds.\nexport class MainSyncLock {\n state: string;\n started: number;\n\n constructor(state: string, started: number) {\n this.state = state;\n this.started = started;\n }\n}\n\n// Raw JSON shapes for the cast at the parse boundary —\n// keeps `any`/`unknown` out of the cast so no-any-unknown stays clean.\ninterface RawStatus {\n branch?: string;\n branchAlreadyMerged?: boolean;\n mergedPr?: string;\n hasForkPoint?: boolean;\n forkPoint?: string | null;\n originMain?: string;\n featureHead?: string;\n conflict?: boolean;\n conflictFiles?: string[];\n timestamp?: string;\n}\n\ninterface RawLock {\n state?: string;\n started?: number;\n}\n\n// Result of a captured git/gh invocation: ok=false on spawn failure or non-zero exit.\ninterface CmdCapture {\n ok: boolean;\n out: string;\n}\n\nexport function mainSyncStatusPath(repoRoot: string): string {\n return path.join(repoRoot, WEBPIECES_TMP_DIR, MAIN_SYNC_STATUS_FILE);\n}\n\nexport function mainSyncLockPath(repoRoot: string): string {\n return path.join(repoRoot, WEBPIECES_TMP_DIR, MAIN_SYNC_LOCK_FILE);\n}\n\nfunction ensureDir(filePath: string): void {\n fs.mkdirSync(path.dirname(filePath), { recursive: true });\n}\n\n// Pure read — any error (missing file, malformed JSON) returns null so the guard fails OPEN\n// (never block an edit because the cache is unreadable).\nexport function readMainSyncStatus(repoRoot: string): MainSyncStatus | null {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const statusPath = mainSyncStatusPath(repoRoot);\n if (!fs.existsSync(statusPath)) return null;\n const raw = JSON.parse(fs.readFileSync(statusPath, 'utf8')) as RawStatus;\n return new MainSyncStatus(\n raw.branch ?? '',\n raw.branchAlreadyMerged ?? false,\n raw.mergedPr ?? '',\n raw.hasForkPoint ?? true,\n raw.forkPoint ?? null,\n raw.originMain ?? '',\n raw.featureHead ?? '',\n raw.conflict ?? false,\n raw.conflictFiles ?? [],\n raw.timestamp ?? '',\n );\n } catch (err: unknown) {\n const error = toError(err);\n void error;\n return null;\n }\n}\n\nexport function writeMainSyncStatus(repoRoot: string, status: MainSyncStatus): void {\n const statusPath = mainSyncStatusPath(repoRoot);\n ensureDir(statusPath);\n fs.writeFileSync(statusPath, JSON.stringify(status, null, 2) + '\\n');\n}\n\nexport function readMainSyncLock(repoRoot: string): MainSyncLock | null {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const lockPath = mainSyncLockPath(repoRoot);\n if (!fs.existsSync(lockPath)) return null;\n const raw = JSON.parse(fs.readFileSync(lockPath, 'utf8')) as RawLock;\n return new MainSyncLock(raw.state ?? LOCK_STATE_FINISHED, raw.started ?? 0);\n } catch (err: unknown) {\n const error = toError(err);\n void error;\n return null;\n }\n}\n\nexport function writeMainSyncLock(repoRoot: string, lock: MainSyncLock): void {\n const lockPath = mainSyncLockPath(repoRoot);\n ensureDir(lockPath);\n fs.writeFileSync(lockPath, JSON.stringify(lock, null, 2) + '\\n');\n}\n\n// A lock is stale (the prior refresher is assumed hung) once it has been `inprocess` longer than\n// hangTimeoutMinutes. `now` is injectable for tests; defaults to Date.now().\nexport function isLockStale(lock: MainSyncLock, hangTimeoutMinutes: number, now: number = Date.now()): boolean {\n return now - lock.started > hangTimeoutMinutes * 60 * 1000;\n}\n\n// True when another refresher is actively running and we should NOT start a second one. A finished\n// lock, a missing lock, or a stale (hung) inprocess lock all return false.\nexport function isRefreshInProgress(repoRoot: string, hangTimeoutMinutes: number, now: number = Date.now()): boolean {\n const lock = readMainSyncLock(repoRoot);\n if (!lock) return false;\n if (lock.state !== LOCK_STATE_INPROCESS) return false;\n return !isLockStale(lock, hangTimeoutMinutes, now);\n}\n\nexport function inProcessLock(now: number = Date.now()): MainSyncLock {\n return new MainSyncLock(LOCK_STATE_INPROCESS, now);\n}\n\nexport function finishedLock(started: number): MainSyncLock {\n return new MainSyncLock(LOCK_STATE_FINISHED, started);\n}\n\n// Run a command capturing trimmed stdout; ok=false on spawn failure or non-zero exit.\nfunction capture(repoRoot: string, cmd: string, args: string[]): CmdCapture {\n const result = spawnSync(cmd, args, { cwd: repoRoot, encoding: 'utf8' });\n if (result.status !== 0 || typeof result.stdout !== 'string') return { ok: false, out: '' };\n return { ok: true, out: result.stdout.trim() };\n}\n\n// The actual checked-out branch in repoRoot — cwd-correct so the cache's `branch` label always\n// matches what the feature-branch-guard compares against (its own `git rev-parse` in the workspace).\nfunction gitBranch(repoRoot: string): string {\n const result = capture(repoRoot, 'git', ['rev-parse', '--abbrev-ref', 'HEAD']);\n return result.ok ? result.out : '';\n}\n\nfunction changedFiles(repoRoot: string, base: string, head: string): string[] {\n const result = capture(repoRoot, 'git', ['diff', '--name-only', base, head]);\n if (!result.ok || result.out === '') return [];\n return result.out.split('\\n').map((line: string): string => line.trim()).filter((line: string): boolean => line.length > 0);\n}\n\n// Has this feature branch already been merged into main? Reliable signal: a MERGED PR exists for the\n// branch. Best-effort — if gh is missing/unauthenticated we just report not-merged (false).\nfunction detectMergedPr(repoRoot: string, branch: string): string {\n if (!branch || branch === 'main') return '';\n const result = capture(repoRoot, 'gh', ['pr', 'list', '--head', branch, '--state', 'merged', '--json', 'number', '--jq', '.[0].number']);\n return result.ok ? result.out : '';\n}\n\n// A benign status that never blocks — used when origin/main can't be resolved (no remote yet,\n// offline before first fetch). hasForkPoint=true + conflict=false so the guard allows the edit.\nfunction benignStatus(branch: string, featureHead: string): MainSyncStatus {\n return new MainSyncStatus(branch, false, '', true, null, '', featureHead, false, [], new Date().toISOString());\n}\n\n/**\n * The SLOW path, run only inside the detached refresher. Computes every cached signal the\n * feature-branch-guard needs: whether the branch is already merged (merged PR), whether a fork point\n * with origin/main still exists, and whether origin/main and this branch touched the SAME file since\n * the fork point (the deliberately-simple conflict heuristic — it over-blocks rather than miss a real\n * conflict). Never run on the hook's blocking path.\n */\nexport function computeMainSyncStatus(repoRoot: string): MainSyncStatus {\n const branch = gitBranch(repoRoot);\n const mergedPr = detectMergedPr(repoRoot, branch);\n\n // Best-effort network refresh; offline just means we evaluate against the last-fetched ref.\n spawnSync('git', ['fetch', 'origin', 'main'], { cwd: repoRoot, stdio: 'ignore' });\n\n const head = capture(repoRoot, 'git', ['rev-parse', 'HEAD']);\n const originMain = capture(repoRoot, 'git', ['rev-parse', 'origin/main']);\n const featureHead = head.ok ? head.out : '';\n if (!head.ok || !originMain.ok) {\n const status = benignStatus(branch, featureHead);\n status.branchAlreadyMerged = mergedPr !== '';\n status.mergedPr = mergedPr;\n return status;\n }\n\n const forkPoint = capture(repoRoot, 'git', ['merge-base', 'origin/main', 'HEAD']);\n if (!forkPoint.ok || forkPoint.out === '') {\n // No common ancestor — main was merged into the branch. Force the human to squash.\n return new MainSyncStatus(branch, mergedPr !== '', mergedPr, false, null, originMain.out, featureHead, false, [], new Date().toISOString());\n }\n\n const featureFiles = new Set(changedFiles(repoRoot, forkPoint.out, 'HEAD'));\n const mainFiles = changedFiles(repoRoot, forkPoint.out, 'origin/main');\n const conflictFiles = mainFiles.filter((file: string): boolean => featureFiles.has(file));\n\n return new MainSyncStatus(\n branch,\n mergedPr !== '',\n mergedPr,\n true,\n forkPoint.out,\n originMain.out,\n featureHead,\n conflictFiles.length > 0,\n conflictFiles,\n new Date().toISOString(),\n );\n}\n\n// The recovery steps when there is no fork point with origin/main (someone merged main into the\n// branch, so a clean squash-merge is impossible). Shared so the feature-branch-guard and pr-gate's\n// findForkPoint check present the SAME instructions. The human must redo the work on a fresh branch —\n// deliberately painful so the bad merge gets noticed and reported.\nexport function squashRecoverySteps(currentBranch: string): string[] {\n return [\n '1. Switch to main: git checkout main',\n '2. Pull latest: git pull',\n `3. New branch (new name): git checkout -b ${currentBranch}-v2`,\n `4. Squash-merge old branch: git merge --squash ${currentBranch}`,\n `5. Commit the squash: git add -A && git commit -m \"Squashed from ${currentBranch}\"`,\n '6. If a PR exists: open a NEW PR for the -v2 branch and close the old one.',\n ];\n}\n\n// Synchronously stamp a clean \"up to date with main\" status — call right after a successful merge\n// (the branch now contains origin/main). Unblocks the next edit immediately without waiting for the\n// async refresher. Best-effort: a git failure is swallowed (the refresher will recompute later).\nexport function stampCleanMainSyncStatus(repoRoot: string): void {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const branch = gitBranch(repoRoot);\n const originMain = capture(repoRoot, 'git', ['rev-parse', 'origin/main']);\n const featureHead = capture(repoRoot, 'git', ['rev-parse', 'HEAD']);\n if (!originMain.ok || !featureHead.ok) return;\n const status = new MainSyncStatus(\n branch, false, '', true, originMain.out, originMain.out, featureHead.out, false, [], new Date().toISOString(),\n );\n writeMainSyncStatus(repoRoot, status);\n } catch (err: unknown) {\n const error = toError(err);\n void error;\n }\n}\n"]}
1
+ {"version":3,"file":"main-sync-status.js","sourceRoot":"","sources":["../../../../../packages/tooling/rules-config/src/main-sync-status.ts"],"names":[],"mappings":";;;AA2GA,gDAEC;AAED,4CAEC;AAQD,gDAuBC;AAED,kDAIC;AAED,4CAYC;AAED,8CAIC;AAID,kCAEC;AAsBD,kDAMC;AAED,sCAEC;AAED,oCAEC;AA2CD,sDAuCC;AAMD,kDASC;AAKD,4DAeC;;AAzUD,iDAA0C;AAC1C,+CAAyB;AACzB,mDAA6B;AAE7B,2CAAgD;AAChD,yCAAqC;AAErC,oGAAoG;AACpG,qGAAqG;AACrG,2FAA2F;AAC3F,oGAAoG;AACpG,mGAAmG;AACnG,yDAAyD;AAEzD,mGAAmG;AACnG,6FAA6F;AAChF,QAAA,4BAA4B,GAAG,CAAC,CAAC;AAE9C,MAAM,qBAAqB,GAAG,uBAAuB,CAAC;AACtD,MAAM,mBAAmB,GAAG,qBAAqB,CAAC;AAElD,MAAM,oBAAoB,GAAG,WAAW,CAAC;AACzC,MAAM,mBAAmB,GAAG,UAAU,CAAC;AAEvC,kGAAkG;AAClG,mGAAmG;AACnG,kGAAkG;AAClG,MAAa,cAAc;IACvB,MAAM,CAAS;IACf,mBAAmB,CAAU;IAC7B,QAAQ,CAAS;IACjB,YAAY,CAAU;IACtB,SAAS,CAAgB;IACzB,UAAU,CAAS;IACnB,WAAW,CAAS;IACpB,QAAQ,CAAU;IAClB,aAAa,CAAW;IACxB,SAAS,CAAS;IAElB,YACI,MAAc,EACd,mBAA4B,EAC5B,QAAgB,EAChB,YAAqB,EACrB,SAAwB,EACxB,UAAkB,EAClB,WAAmB,EACnB,QAAiB,EACjB,aAAuB,EACvB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AAO,EAAC,GAAG,CAAC,CAAC;QAC3B,KAAK,KAAK,CAAC;IACf,CAAC;AACL,CAAC","sourcesContent":["import { spawnSync } from 'child_process';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nimport { WEBPIECES_TMP_DIR } from './constants';\nimport { toError } from './to-error';\n\n// Shared \"is my feature branch healthy relative to origin/main?\" state. The SLOW signals (git fetch\n// + merge-base + same-file-overlap + a merged-PR lookup) are computed by the ai-hook-rules refresher\n// in a DETACHED background process so the PreToolUse hook never blocks on the network. The\n// feature-branch-guard then only READS this cached file (instant). pr-gate's merge flow also writes\n// it synchronously after a merge so the next edit is unblocked immediately. Lives here (the shared\n// dep of both packages) so neither depends on the other.\n\n// How long an `inprocess` refresher lock may sit before a new refresher assumes the prior run hung\n// and proceeds anyway. Overridable per-rule via FeatureBranchGuardConfig.hangTimeoutMinutes.\nexport const DEFAULT_HANG_TIMEOUT_MINUTES = 5;\n\nconst MAIN_SYNC_STATUS_FILE = 'main-sync-status.json';\nconst MAIN_SYNC_LOCK_FILE = 'main-sync.lock.json';\n\nconst LOCK_STATE_INPROCESS = 'inprocess';\nconst LOCK_STATE_FINISHED = 'finished';\n\n// Data-only (per CLAUDE.md, classes for data). `forkPoint` is null exactly when `hasForkPoint` is\n// false (no merge-base with origin/main). `branchAlreadyMerged` flags that this feature branch was\n// already merged into main (a merged PR exists) — the \"you're working on a finished branch\" case.\nexport class MainSyncStatus {\n branch: string;\n branchAlreadyMerged: boolean;\n mergedPr: string;\n hasForkPoint: boolean;\n forkPoint: string | null;\n originMain: string;\n featureHead: string;\n conflict: boolean;\n conflictFiles: string[];\n timestamp: string;\n\n constructor(\n branch: string,\n branchAlreadyMerged: boolean,\n mergedPr: string,\n hasForkPoint: boolean,\n forkPoint: string | null,\n originMain: string,\n featureHead: string,\n conflict: boolean,\n conflictFiles: string[],\n timestamp: string,\n ) {\n this.branch = branch;\n this.branchAlreadyMerged = branchAlreadyMerged;\n this.mergedPr = mergedPr;\n this.hasForkPoint = hasForkPoint;\n this.forkPoint = forkPoint;\n this.originMain = originMain;\n this.featureHead = featureHead;\n this.conflict = conflict;\n this.conflictFiles = conflictFiles;\n this.timestamp = timestamp;\n }\n}\n\n// Concurrency state machine for the detached refresher. `started` is epoch milliseconds. `pid` is\n// the refresher process's pid (0 = unknown, e.g. a lock written by an older version) — used so a\n// refresher that was KILLED before writing its finished lock (SIGKILL skips the finally) doesn't\n// wedge `inprocess` for the whole hangTimeout: if its pid is gone, the lock is reclaimable now.\nexport class MainSyncLock {\n state: string;\n started: number;\n pid: number;\n\n constructor(state: string, started: number, pid: number = 0) {\n this.state = state;\n this.started = started;\n this.pid = pid;\n }\n}\n\n// Raw JSON shapes for the cast at the parse boundary —\n// keeps `any`/`unknown` out of the cast so no-any-unknown stays clean.\ninterface RawStatus {\n branch?: string;\n branchAlreadyMerged?: boolean;\n mergedPr?: string;\n hasForkPoint?: boolean;\n forkPoint?: string | null;\n originMain?: string;\n featureHead?: string;\n conflict?: boolean;\n conflictFiles?: string[];\n timestamp?: string;\n}\n\ninterface RawLock {\n state?: string;\n started?: number;\n pid?: number;\n}\n\n// Result of a captured git/gh invocation: ok=false on spawn failure or non-zero exit.\ninterface CmdCapture {\n ok: boolean;\n out: string;\n}\n\nexport function mainSyncStatusPath(repoRoot: string): string {\n return path.join(repoRoot, WEBPIECES_TMP_DIR, MAIN_SYNC_STATUS_FILE);\n}\n\nexport function mainSyncLockPath(repoRoot: string): string {\n return path.join(repoRoot, WEBPIECES_TMP_DIR, MAIN_SYNC_LOCK_FILE);\n}\n\nfunction ensureDir(filePath: string): void {\n fs.mkdirSync(path.dirname(filePath), { recursive: true });\n}\n\n// Pure read — any error (missing file, malformed JSON) returns null so the guard fails OPEN\n// (never block an edit because the cache is unreadable).\nexport function readMainSyncStatus(repoRoot: string): MainSyncStatus | null {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const statusPath = mainSyncStatusPath(repoRoot);\n if (!fs.existsSync(statusPath)) return null;\n const raw = JSON.parse(fs.readFileSync(statusPath, 'utf8')) as RawStatus;\n return new MainSyncStatus(\n raw.branch ?? '',\n raw.branchAlreadyMerged ?? false,\n raw.mergedPr ?? '',\n raw.hasForkPoint ?? true,\n raw.forkPoint ?? null,\n raw.originMain ?? '',\n raw.featureHead ?? '',\n raw.conflict ?? false,\n raw.conflictFiles ?? [],\n raw.timestamp ?? '',\n );\n } catch (err: unknown) {\n const error = toError(err);\n void error;\n return null;\n }\n}\n\nexport function writeMainSyncStatus(repoRoot: string, status: MainSyncStatus): void {\n const statusPath = mainSyncStatusPath(repoRoot);\n ensureDir(statusPath);\n fs.writeFileSync(statusPath, JSON.stringify(status, null, 2) + '\\n');\n}\n\nexport function readMainSyncLock(repoRoot: string): MainSyncLock | null {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const lockPath = mainSyncLockPath(repoRoot);\n if (!fs.existsSync(lockPath)) return null;\n const raw = JSON.parse(fs.readFileSync(lockPath, 'utf8')) as RawLock;\n return new MainSyncLock(raw.state ?? LOCK_STATE_FINISHED, raw.started ?? 0, raw.pid ?? 0);\n } catch (err: unknown) {\n const error = toError(err);\n void error;\n return null;\n }\n}\n\nexport function writeMainSyncLock(repoRoot: string, lock: MainSyncLock): void {\n const lockPath = mainSyncLockPath(repoRoot);\n ensureDir(lockPath);\n fs.writeFileSync(lockPath, JSON.stringify(lock, null, 2) + '\\n');\n}\n\n// A lock is stale (the prior refresher is assumed hung) once it has been `inprocess` longer than\n// hangTimeoutMinutes. `now` is injectable for tests; defaults to Date.now().\nexport function isLockStale(lock: MainSyncLock, hangTimeoutMinutes: number, now: number = Date.now()): boolean {\n return now - lock.started > hangTimeoutMinutes * 60 * 1000;\n}\n\n// Liveness probe: is `pid` still a running process? `process.kill(pid, 0)` sends no signal, it only\n// tests existence — ESRCH means the process is gone, EPERM means it exists but isn't ours (alive).\n// pid <= 0 means \"unknown\" (an old lock with no pid) → assume alive and fall back to staleness only.\nfunction isProcessAlive(pid: number): boolean {\n if (pid <= 0) return true;\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n process.kill(pid, 0);\n return true;\n } catch (err: unknown) {\n const error = toError(err);\n // ESRCH = no such process (dead); anything else (e.g. EPERM = exists, not ours) = alive.\n return !error.message.includes('ESRCH');\n }\n}\n\n// True when another refresher is actively running and we should NOT start a second one. A finished\n// lock, a missing lock, a stale (hung) inprocess lock, OR an inprocess lock whose refresher pid is\n// already dead (it was killed before writing its finished lock) all return false — so a killed\n// refresher never wedges refreshes for the full hangTimeout.\nexport function isRefreshInProgress(repoRoot: string, hangTimeoutMinutes: number, now: number = Date.now()): boolean {\n const lock = readMainSyncLock(repoRoot);\n if (!lock) return false;\n if (lock.state !== LOCK_STATE_INPROCESS) return false;\n if (isLockStale(lock, hangTimeoutMinutes, now)) return false;\n return isProcessAlive(lock.pid);\n}\n\nexport function inProcessLock(now: number = Date.now(), pid: number = process.pid): MainSyncLock {\n return new MainSyncLock(LOCK_STATE_INPROCESS, now, pid);\n}\n\nexport function finishedLock(started: number): MainSyncLock {\n return new MainSyncLock(LOCK_STATE_FINISHED, started, 0);\n}\n\n// Run a command capturing trimmed stdout; ok=false on spawn failure or non-zero exit.\nfunction capture(repoRoot: string, cmd: string, args: string[]): CmdCapture {\n const result = spawnSync(cmd, args, { cwd: repoRoot, encoding: 'utf8' });\n if (result.status !== 0 || typeof result.stdout !== 'string') return { ok: false, out: '' };\n return { ok: true, out: result.stdout.trim() };\n}\n\n// The actual checked-out branch in repoRoot — cwd-correct so the cache's `branch` label always\n// matches what the feature-branch-guard compares against (its own `git rev-parse` in the workspace).\nfunction gitBranch(repoRoot: string): string {\n const result = capture(repoRoot, 'git', ['rev-parse', '--abbrev-ref', 'HEAD']);\n return result.ok ? result.out : '';\n}\n\nfunction changedFiles(repoRoot: string, base: string, head: string): string[] {\n const result = capture(repoRoot, 'git', ['diff', '--name-only', base, head]);\n if (!result.ok || result.out === '') return [];\n return result.out.split('\\n').map((line: string): string => line.trim()).filter((line: string): boolean => line.length > 0);\n}\n\n// Has this feature branch already been merged into main? Reliable signal: a MERGED PR exists for the\n// branch. Best-effort — if gh is missing/unauthenticated we just report not-merged (false).\nfunction detectMergedPr(repoRoot: string, branch: string): string {\n if (!branch || branch === 'main') return '';\n const result = capture(repoRoot, 'gh', ['pr', 'list', '--head', branch, '--state', 'merged', '--json', 'number', '--jq', '.[0].number']);\n return result.ok ? result.out : '';\n}\n\n// A benign status that never blocks — used when origin/main can't be resolved (no remote yet,\n// offline before first fetch). hasForkPoint=true + conflict=false so the guard allows the edit.\nfunction benignStatus(branch: string, featureHead: string): MainSyncStatus {\n return new MainSyncStatus(branch, false, '', true, null, '', featureHead, false, [], new Date().toISOString());\n}\n\n/**\n * The SLOW path, run only inside the detached refresher. Computes every cached signal the\n * feature-branch-guard needs: whether the branch is already merged (merged PR), whether a fork point\n * with origin/main still exists, and whether origin/main and this branch touched the SAME file since\n * the fork point (the deliberately-simple conflict heuristic — it over-blocks rather than miss a real\n * conflict). Never run on the hook's blocking path.\n */\nexport function computeMainSyncStatus(repoRoot: string): MainSyncStatus {\n const branch = gitBranch(repoRoot);\n const mergedPr = detectMergedPr(repoRoot, branch);\n\n // Best-effort network refresh; offline just means we evaluate against the last-fetched ref.\n spawnSync('git', ['fetch', 'origin', 'main'], { cwd: repoRoot, stdio: 'ignore' });\n\n const head = capture(repoRoot, 'git', ['rev-parse', 'HEAD']);\n const originMain = capture(repoRoot, 'git', ['rev-parse', 'origin/main']);\n const featureHead = head.ok ? head.out : '';\n if (!head.ok || !originMain.ok) {\n const status = benignStatus(branch, featureHead);\n status.branchAlreadyMerged = mergedPr !== '';\n status.mergedPr = mergedPr;\n return status;\n }\n\n const forkPoint = capture(repoRoot, 'git', ['merge-base', 'origin/main', 'HEAD']);\n if (!forkPoint.ok || forkPoint.out === '') {\n // No common ancestor — main was merged into the branch. Force the human to squash.\n return new MainSyncStatus(branch, mergedPr !== '', mergedPr, false, null, originMain.out, featureHead, false, [], new Date().toISOString());\n }\n\n const featureFiles = new Set(changedFiles(repoRoot, forkPoint.out, 'HEAD'));\n const mainFiles = changedFiles(repoRoot, forkPoint.out, 'origin/main');\n const conflictFiles = mainFiles.filter((file: string): boolean => featureFiles.has(file));\n\n return new MainSyncStatus(\n branch,\n mergedPr !== '',\n mergedPr,\n true,\n forkPoint.out,\n originMain.out,\n featureHead,\n conflictFiles.length > 0,\n conflictFiles,\n new Date().toISOString(),\n );\n}\n\n// The recovery steps when there is no fork point with origin/main (someone merged main into the\n// branch, so a clean squash-merge is impossible). Shared so the feature-branch-guard and pr-gate's\n// findForkPoint check present the SAME instructions. The human must redo the work on a fresh branch —\n// deliberately painful so the bad merge gets noticed and reported.\nexport function squashRecoverySteps(currentBranch: string): string[] {\n return [\n '1. Switch to main: git checkout main',\n '2. Pull latest: git pull',\n `3. New branch (new name): git checkout -b ${currentBranch}-v2`,\n `4. Squash-merge old branch: git merge --squash ${currentBranch}`,\n `5. Commit the squash: git add -A && git commit -m \"Squashed from ${currentBranch}\"`,\n '6. If a PR exists: open a NEW PR for the -v2 branch and close the old one.',\n ];\n}\n\n// Synchronously stamp a clean \"up to date with main\" status — call right after a successful merge\n// (the branch now contains origin/main). Unblocks the next edit immediately without waiting for the\n// async refresher. Best-effort: a git failure is swallowed (the refresher will recompute later).\nexport function stampCleanMainSyncStatus(repoRoot: string): void {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const branch = gitBranch(repoRoot);\n const originMain = capture(repoRoot, 'git', ['rev-parse', 'origin/main']);\n const featureHead = capture(repoRoot, 'git', ['rev-parse', 'HEAD']);\n if (!originMain.ok || !featureHead.ok) return;\n const status = new MainSyncStatus(\n branch, false, '', true, originMain.out, originMain.out, featureHead.out, false, [], new Date().toISOString(),\n );\n writeMainSyncStatus(repoRoot, status);\n } catch (err: unknown) {\n const error = toError(err);\n void error;\n }\n}\n"]}