lazycodex-ai 5.0.0-beta.62 → 5.0.0-beta.63

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.
Files changed (179) hide show
  1. package/dist/cli/doctor/checks/browser-provider.d.ts +2 -0
  2. package/dist/cli/index.js +64 -35
  3. package/dist/cli-node/index.js +64 -35
  4. package/package.json +2 -6
  5. package/packages/omo-codex/plugin/.codex-plugin/plugin.json +1 -1
  6. package/packages/omo-codex/plugin/components/bootstrap/dist/cli.js +2 -0
  7. package/packages/omo-codex/plugin/components/bootstrap/hooks/hooks.json +1 -1
  8. package/packages/omo-codex/plugin/components/bootstrap/package.json +1 -1
  9. package/packages/omo-codex/plugin/components/comment-checker/hooks/hooks.json +1 -1
  10. package/packages/omo-codex/plugin/components/comment-checker/package.json +1 -1
  11. package/packages/omo-codex/plugin/components/git-bash/hooks/hooks.json +2 -2
  12. package/packages/omo-codex/plugin/components/git-bash/package.json +1 -1
  13. package/packages/omo-codex/plugin/components/lazycodex-executor-verify/hooks/hooks.json +1 -1
  14. package/packages/omo-codex/plugin/components/lazycodex-executor-verify/package.json +1 -1
  15. package/packages/omo-codex/plugin/components/lsp/dist/.omo-runtime-manifest.json +2 -2
  16. package/packages/omo-codex/plugin/components/lsp/hooks/hooks.json +2 -2
  17. package/packages/omo-codex/plugin/components/lsp/package.json +1 -1
  18. package/packages/omo-codex/plugin/components/rules/hooks/hooks.json +4 -4
  19. package/packages/omo-codex/plugin/components/rules/package.json +1 -1
  20. package/packages/omo-codex/plugin/components/teammode/hooks/hooks.json +1 -1
  21. package/packages/omo-codex/plugin/components/teammode/package.json +1 -1
  22. package/packages/omo-codex/plugin/components/telemetry/hooks/hooks.json +1 -1
  23. package/packages/omo-codex/plugin/components/telemetry/package.json +1 -1
  24. package/packages/omo-codex/plugin/components/ultrawork/agents/plan.toml +2 -2
  25. package/packages/omo-codex/plugin/components/ultrawork/dist/cli.js +5 -4
  26. package/packages/omo-codex/plugin/components/ultrawork/hooks/hooks.json +1 -1
  27. package/packages/omo-codex/plugin/components/ultrawork/package.json +1 -1
  28. package/packages/omo-codex/plugin/components/ultrawork/src/directive-content.ts +1 -1
  29. package/packages/omo-codex/plugin/components/ulw-execute-continuation/directive.md +1 -1
  30. package/packages/omo-codex/plugin/components/ulw-execute-continuation/hooks/hooks.json +1 -1
  31. package/packages/omo-codex/plugin/components/ulw-execute-continuation/package.json +1 -1
  32. package/packages/omo-codex/plugin/components/ulw-loop/CHANGELOG.md +13 -0
  33. package/packages/omo-codex/plugin/components/ulw-loop/directive.md +5 -4
  34. package/packages/omo-codex/plugin/components/ulw-loop/dist/checkpoint.js +5 -4
  35. package/packages/omo-codex/plugin/components/ulw-loop/dist/cli.js +582 -322
  36. package/packages/omo-codex/plugin/components/ulw-loop/dist/domain-types.d.ts +5 -0
  37. package/packages/omo-codex/plugin/components/ulw-loop/dist/evidence.js +16 -12
  38. package/packages/omo-codex/plugin/components/ulw-loop/dist/ledger.d.ts +4 -0
  39. package/packages/omo-codex/plugin/components/ulw-loop/dist/ledger.js +34 -0
  40. package/packages/omo-codex/plugin/components/ulw-loop/dist/plan-commit.d.ts +19 -0
  41. package/packages/omo-codex/plugin/components/ulw-loop/dist/plan-commit.js +107 -0
  42. package/packages/omo-codex/plugin/components/ulw-loop/dist/plan-crud.d.ts +2 -1
  43. package/packages/omo-codex/plugin/components/ulw-loop/dist/plan-crud.js +48 -22
  44. package/packages/omo-codex/plugin/components/ulw-loop/dist/plan-io.d.ts +9 -7
  45. package/packages/omo-codex/plugin/components/ulw-loop/dist/plan-io.js +105 -114
  46. package/packages/omo-codex/plugin/components/ulw-loop/dist/plan-log.d.ts +15 -0
  47. package/packages/omo-codex/plugin/components/ulw-loop/dist/plan-log.js +86 -0
  48. package/packages/omo-codex/plugin/components/ulw-loop/dist/plan-missing-recovery.d.ts +5 -1
  49. package/packages/omo-codex/plugin/components/ulw-loop/dist/plan-missing-recovery.js +15 -3
  50. package/packages/omo-codex/plugin/components/ulw-loop/dist/review-blockers.js +3 -4
  51. package/packages/omo-codex/plugin/components/ulw-loop/dist/sdk/factory.js +12 -5
  52. package/packages/omo-codex/plugin/components/ulw-loop/dist/spawn-budget-io.d.ts +5 -0
  53. package/packages/omo-codex/plugin/components/ulw-loop/dist/spawn-budget-io.js +67 -0
  54. package/packages/omo-codex/plugin/components/ulw-loop/dist/spawn-guard.js +8 -62
  55. package/packages/omo-codex/plugin/components/ulw-loop/dist/state-lock.d.ts +11 -1
  56. package/packages/omo-codex/plugin/components/ulw-loop/dist/state-lock.js +120 -88
  57. package/packages/omo-codex/plugin/components/ulw-loop/dist/status-next-actions.d.ts +3 -2
  58. package/packages/omo-codex/plugin/components/ulw-loop/dist/status-next-actions.js +18 -10
  59. package/packages/omo-codex/plugin/components/ulw-loop/dist/steering-batch.js +4 -4
  60. package/packages/omo-codex/plugin/components/ulw-loop/dist/steering.js +5 -5
  61. package/packages/omo-codex/plugin/components/ulw-loop/dist/stop-resume-hook.js +8 -17
  62. package/packages/omo-codex/plugin/components/ulw-loop/hooks/hooks.json +5 -5
  63. package/packages/omo-codex/plugin/components/ulw-loop/package.json +1 -1
  64. package/packages/omo-codex/plugin/components/ulw-loop/skills/ulw-loop/references/full-workflow.md +2 -2
  65. package/packages/omo-codex/plugin/components/ulw-loop/src/checkpoint.ts +5 -4
  66. package/packages/omo-codex/plugin/components/ulw-loop/src/domain-types.ts +5 -0
  67. package/packages/omo-codex/plugin/components/ulw-loop/src/evidence.ts +16 -16
  68. package/packages/omo-codex/plugin/components/ulw-loop/src/ledger.ts +33 -0
  69. package/packages/omo-codex/plugin/components/ulw-loop/src/plan-commit.ts +124 -0
  70. package/packages/omo-codex/plugin/components/ulw-loop/src/plan-crud.ts +58 -51
  71. package/packages/omo-codex/plugin/components/ulw-loop/src/plan-io.ts +126 -140
  72. package/packages/omo-codex/plugin/components/ulw-loop/src/plan-log.ts +84 -0
  73. package/packages/omo-codex/plugin/components/ulw-loop/src/plan-missing-recovery.ts +26 -3
  74. package/packages/omo-codex/plugin/components/ulw-loop/src/review-blockers.ts +3 -3
  75. package/packages/omo-codex/plugin/components/ulw-loop/src/sdk/factory.ts +30 -5
  76. package/packages/omo-codex/plugin/components/ulw-loop/src/spawn-budget-io.ts +59 -0
  77. package/packages/omo-codex/plugin/components/ulw-loop/src/spawn-guard.ts +8 -59
  78. package/packages/omo-codex/plugin/components/ulw-loop/src/state-lock.ts +129 -111
  79. package/packages/omo-codex/plugin/components/ulw-loop/src/status-next-actions.ts +19 -10
  80. package/packages/omo-codex/plugin/components/ulw-loop/src/steering-batch.ts +4 -4
  81. package/packages/omo-codex/plugin/components/ulw-loop/src/steering.ts +5 -4
  82. package/packages/omo-codex/plugin/components/ulw-loop/src/stop-resume-hook.ts +8 -16
  83. package/packages/omo-codex/plugin/components/ulw-loop/test/fixtures/barrier.ts +13 -0
  84. package/packages/omo-codex/plugin/components/ulw-loop/test/fixtures/commit-child.ts +65 -0
  85. package/packages/omo-codex/plugin/components/ulw-loop/test/fixtures/commit-cli-qa.mjs +76 -0
  86. package/packages/omo-codex/plugin/components/ulw-loop/test/fixtures/commit-crash.ts +23 -0
  87. package/packages/omo-codex/plugin/components/ulw-loop/test/fixtures/commit-mutations.mjs +58 -0
  88. package/packages/omo-codex/plugin/components/ulw-loop/test/fixtures/commit-writer.ts +65 -0
  89. package/packages/omo-codex/plugin/components/ulw-loop/test/fixtures/lease-clock.ts +23 -0
  90. package/packages/omo-codex/plugin/components/ulw-loop/test/plan-commit-log.test.ts +252 -0
  91. package/packages/omo-codex/plugin/components/ulw-loop/test/plan-commit-recovery.test.ts +190 -0
  92. package/packages/omo-codex/plugin/components/ulw-loop/test/plan-io.test.ts +50 -35
  93. package/packages/omo-codex/plugin/components/ulw-loop/test/plan-log-newest.test.ts +72 -0
  94. package/packages/omo-codex/plugin/components/ulw-loop/test/sdk-completed-plan.test.ts +43 -0
  95. package/packages/omo-codex/plugin/components/ulw-loop/test/sdk-session-context.test.ts +21 -0
  96. package/packages/omo-codex/plugin/components/ulw-loop/test/state-lock.test.ts +92 -0
  97. package/packages/omo-codex/plugin/components/ulw-loop/test/status-next-actions-surface.test.ts +133 -0
  98. package/packages/omo-codex/plugin/components/ulw-loop/test/steering-batch.test.ts +12 -12
  99. package/packages/omo-codex/plugin/components/ulw-loop/test/steering.test.ts +4 -1
  100. package/packages/omo-codex/plugin/hooks/post-compact-resetting-git-bash-mcp-reminder.json +1 -1
  101. package/packages/omo-codex/plugin/hooks/post-compact-resetting-lsp-diagnostics-cache.json +1 -1
  102. package/packages/omo-codex/plugin/hooks/post-compact-resetting-project-rule-cache.json +1 -1
  103. package/packages/omo-codex/plugin/hooks/post-tool-use-checking-comments.json +1 -1
  104. package/packages/omo-codex/plugin/hooks/post-tool-use-checking-lsp-diagnostics.json +1 -1
  105. package/packages/omo-codex/plugin/hooks/post-tool-use-checking-thread-title-hygiene.json +1 -1
  106. package/packages/omo-codex/plugin/hooks/post-tool-use-matching-project-rules.json +1 -1
  107. package/packages/omo-codex/plugin/hooks/post-tool-use-recording-spawn-admission.json +1 -1
  108. package/packages/omo-codex/plugin/hooks/pre-tool-use-enforcing-unlimited-goal-budget.json +1 -1
  109. package/packages/omo-codex/plugin/hooks/pre-tool-use-guarding-ulw-loop-spawns.json +1 -1
  110. package/packages/omo-codex/plugin/hooks/pre-tool-use-recommending-git-bash-mcp.json +1 -1
  111. package/packages/omo-codex/plugin/hooks/session-start-checking-auto-update.json +1 -1
  112. package/packages/omo-codex/plugin/hooks/session-start-checking-bootstrap-provisioning.json +1 -1
  113. package/packages/omo-codex/plugin/hooks/session-start-loading-project-rules.json +1 -1
  114. package/packages/omo-codex/plugin/hooks/session-start-recording-session-telemetry.json +1 -1
  115. package/packages/omo-codex/plugin/hooks/stop-checking-ulw-execute-continuation.json +1 -1
  116. package/packages/omo-codex/plugin/hooks/stop-checking-ulw-loop-resume.json +1 -1
  117. package/packages/omo-codex/plugin/hooks/subagent-stop-verifying-lazycodex-executor-evidence.json +1 -1
  118. package/packages/omo-codex/plugin/hooks/user-prompt-submit-checking-ultrawork-trigger.json +1 -1
  119. package/packages/omo-codex/plugin/hooks/user-prompt-submit-checking-ulw-loop-steering.json +1 -1
  120. package/packages/omo-codex/plugin/hooks/user-prompt-submit-loading-project-rules.json +1 -1
  121. package/packages/omo-codex/plugin/package-lock.json +12 -12
  122. package/packages/omo-codex/plugin/package.json +1 -1
  123. package/packages/omo-codex/plugin/scripts/sync-skills.d.mts +4 -0
  124. package/packages/omo-codex/plugin/scripts/sync-skills.mjs +15 -7
  125. package/packages/omo-codex/plugin/skills/ast-grep/SKILL.md +12 -0
  126. package/packages/omo-codex/plugin/skills/debugging/references/tools/playwright-cli.md +79 -161
  127. package/packages/omo-codex/plugin/skills/frontend/SKILL.md +1 -1
  128. package/packages/omo-codex/plugin/skills/frontend/references/design/aside.md +1 -1
  129. package/packages/omo-codex/plugin/skills/frontend/references/design/clone-from-url.md +1 -1
  130. package/packages/omo-codex/plugin/skills/frontend/scripts/perfection/lighthouse-audit.py +2 -2
  131. package/packages/omo-codex/plugin/skills/review-work/SKILL.md +1 -1
  132. package/packages/omo-codex/plugin/skills/ultimate-browsing/ATTRIBUTION.md +1 -82
  133. package/packages/omo-codex/plugin/skills/ultimate-browsing/SKILL.md +17 -29
  134. package/packages/omo-codex/plugin/skills/ultimate-browsing/engine/AGENTS.md +2 -1
  135. package/packages/omo-codex/plugin/skills/ultimate-browsing/engine/templates/package.json +2 -2
  136. package/packages/omo-codex/plugin/skills/ultimate-browsing/engine/templates/playwright_mobile_chrome.js +5 -2
  137. package/packages/omo-codex/plugin/skills/ultimate-browsing/engine/templates/playwright_real_chrome.js +8 -6
  138. package/packages/omo-codex/plugin/skills/ultimate-browsing/engine/tests/test_playwright_stealth.py +86 -0
  139. package/packages/omo-codex/plugin/skills/ultimate-browsing/engine/tests/test_playwright_templates.py +4 -7
  140. package/packages/omo-codex/plugin/skills/ultimate-browsing/references/chrome-stealth.md +86 -100
  141. package/packages/omo-codex/plugin/skills/ultimate-browsing/references/insane-search/README.md +9 -3
  142. package/packages/omo-codex/plugin/skills/ultimate-browsing/references/insane-search/playwright.md +16 -13
  143. package/packages/omo-codex/plugin/skills/ultimate-browsing/scripts/extract_cookies.py +3 -3
  144. package/packages/omo-codex/plugin/skills/ultrawork/SKILL.md +5 -4
  145. package/packages/omo-codex/plugin/skills/ulw-execute/SKILL.md +1 -1
  146. package/packages/omo-codex/plugin/skills/ulw-loop/references/full-workflow.md +2 -2
  147. package/packages/omo-codex/plugin/skills/ulw-research/SKILL.md +3 -3
  148. package/packages/omo-codex/plugin/skills/visual-qa/AGENTS.md +1 -1
  149. package/packages/omo-codex/plugin/skills/visual-qa/SKILL.md +1 -1
  150. package/packages/omo-codex/plugin/skills/visual-qa/references/browser-setup.md +75 -0
  151. package/packages/omo-codex/scripts/install-dist/install-local.mjs +4 -2
  152. package/packages/prompts-core/prompts/ultrawork/codex.md +5 -4
  153. package/packages/shared-skills/skills/ast-grep/SKILL.md +12 -0
  154. package/packages/shared-skills/skills/debugging/references/tools/playwright-cli.md +79 -161
  155. package/packages/shared-skills/skills/frontend/SKILL.md +1 -1
  156. package/packages/shared-skills/skills/frontend/references/design/aside.md +1 -1
  157. package/packages/shared-skills/skills/frontend/references/design/clone-from-url.md +1 -1
  158. package/packages/shared-skills/skills/frontend/scripts/perfection/lighthouse-audit.py +2 -2
  159. package/packages/shared-skills/skills/review-work/SKILL.md +1 -1
  160. package/packages/shared-skills/skills/ultimate-browsing/ATTRIBUTION.md +1 -82
  161. package/packages/shared-skills/skills/ultimate-browsing/SKILL.md +17 -29
  162. package/packages/shared-skills/skills/ultimate-browsing/engine/AGENTS.md +2 -1
  163. package/packages/shared-skills/skills/ultimate-browsing/engine/templates/package.json +2 -2
  164. package/packages/shared-skills/skills/ultimate-browsing/engine/templates/playwright_mobile_chrome.js +5 -2
  165. package/packages/shared-skills/skills/ultimate-browsing/engine/templates/playwright_real_chrome.js +8 -6
  166. package/packages/shared-skills/skills/ultimate-browsing/engine/tests/test_playwright_stealth.py +86 -0
  167. package/packages/shared-skills/skills/ultimate-browsing/engine/tests/test_playwright_templates.py +4 -7
  168. package/packages/shared-skills/skills/ultimate-browsing/references/chrome-stealth.md +86 -100
  169. package/packages/shared-skills/skills/ultimate-browsing/references/insane-search/README.md +9 -3
  170. package/packages/shared-skills/skills/ultimate-browsing/references/insane-search/playwright.md +16 -13
  171. package/packages/shared-skills/skills/ultimate-browsing/scripts/extract_cookies.py +3 -3
  172. package/packages/shared-skills/skills/ulw-execute/SKILL.md +1 -1
  173. package/packages/shared-skills/skills/ulw-plan/scripts/scaffold-plan.mjs +1 -1
  174. package/packages/shared-skills/skills/ulw-research/SKILL.md +3 -3
  175. package/packages/shared-skills/skills/visual-qa/AGENTS.md +1 -1
  176. package/packages/shared-skills/skills/visual-qa/SKILL.md +1 -1
  177. package/packages/shared-skills/skills/visual-qa/references/browser-setup.md +75 -0
  178. package/packages/omo-codex/plugin/skills/visual-qa/references/agent-browser-setup.md +0 -45
  179. package/packages/shared-skills/skills/visual-qa/references/agent-browser-setup.md +0 -45
@@ -1,46 +1,88 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
- import { createReadStream, readdirSync } from "node:fs";
3
- import { appendFile, mkdir, readFile, rename, writeFile } from "node:fs/promises";
4
- import { createInterface } from "node:readline";
2
+ import { readdirSync } from "node:fs";
5
3
  import { aggregateCodexObjectiveForScope } from "./goal-status.js";
6
- import { repoRelative, ulwLoopDir, ulwLoopGoalsPath, ulwLoopLedgerPath, ulwLoopRelativeDir, ulwLoopStateLockPath, } from "./paths.js";
7
- import { planMissingRecovery } from "./plan-missing-recovery.js";
4
+ import { readLedger as reconcileLedger } from "./ledger.js";
5
+ import { repoRelative, ulwLoopDir, ulwLoopGoalsPath, ulwLoopRelativeDir, ulwLoopStateLockPath, } from "./paths.js";
6
+ import { commit, materialize, materializeSync } from "./plan-commit.js";
7
+ import { readOptional, reconcilePlan } from "./plan-log.js";
8
+ import { planMissingError } from "./plan-missing-recovery.js";
8
9
  import { withStateLock } from "./state-lock.js";
9
- import { iso, ULW_LOOP_DIR, ULW_LOOP_GOALS, ULW_LOOP_LEDGER, UlwLoopError } from "./types.js";
10
+ import { iso, ULW_LOOP_DIR, ULW_LOOP_GOALS, ULW_LOOP_LEDGER, UlwLoopError, } from "./types.js";
11
+ export function readLedger(repoRoot, scope) {
12
+ const lockPath = ulwLoopStateLockPath(repoRoot, scope);
13
+ if (heldLocks.getStore()?.has(lockPath)) {
14
+ assertStateLockOwned(lockPath);
15
+ materializeSync(ulwLoopDir(repoRoot, scope));
16
+ }
17
+ return reconcileLedger(repoRoot, scope);
18
+ }
19
+ export { planExists } from "./plan-log.js";
10
20
  const LEGACY_OBJECTIVE_PREFIX = `Complete all ulw-loop stories in ${ULW_LOOP_DIR}/${ULW_LOOP_GOALS}: `;
11
21
  const LEGACY_OBJECTIVE = `Complete all ulw-loop stories listed in ${ULW_LOOP_DIR}/${ULW_LOOP_GOALS}. Use ${ULW_LOOP_DIR}/${ULW_LOOP_LEDGER} as the durable audit trail.`;
12
22
  const locks = new Map();
13
- // Tracks which state dirs the CURRENT async continuation holds, so a read nested
14
- // inside a locked mutation can tell itself apart from an unlocked read elsewhere
15
- // in the same process.
16
23
  const heldLocks = new AsyncLocalStorage();
17
- function hasCode(error, code) {
18
- return error instanceof Error && "code" in error && error.code === code;
24
+ const lockOptions = new AsyncLocalStorage();
25
+ export function withMutationLockOptions(options, fn) {
26
+ return lockOptions.run(options, fn);
19
27
  }
20
- function isLegacyEnumeratedAggregateObjective(objective) {
21
- return objective === LEGACY_OBJECTIVE || Boolean(objective?.startsWith(LEGACY_OBJECTIVE_PREFIX));
28
+ function tokenAt(lockPath) {
29
+ const raw = readOptional(lockPath);
30
+ if (raw === undefined)
31
+ return undefined;
32
+ try {
33
+ const record = JSON.parse(raw);
34
+ return typeof record === "object" && record !== null && "token" in record && typeof record.token === "string"
35
+ ? record.token
36
+ : undefined;
37
+ }
38
+ catch (error) {
39
+ if (error instanceof SyntaxError)
40
+ return undefined;
41
+ throw error;
42
+ }
22
43
  }
23
- function isSteeringKind(value) {
24
- return (value === "steering_accepted" ||
25
- value === "steering_rejected" ||
26
- value === "criteria_revised" ||
27
- value === "batch_updated");
44
+ export function assertStateLockOwned(lockPath) {
45
+ const context = heldLocks.getStore()?.get(lockPath);
46
+ if (context === undefined)
47
+ return;
48
+ if (tokenAt(lockPath) !== context.token)
49
+ throw new UlwLoopError("The ulw-loop mutation lock changed owners.", "ULW_LOOP_LOCK_LOST");
50
+ }
51
+ export function migrationEntries(plan) {
52
+ for (const context of heldLocks.getStore()?.values() ?? []) {
53
+ const entries = context.migrations.get(plan.goalsPath);
54
+ if (entries !== undefined) {
55
+ context.migrations.delete(plan.goalsPath);
56
+ return entries;
57
+ }
58
+ }
59
+ return [];
28
60
  }
29
- export async function withUlwLoopMutationLock(repoRoot, scopeOrFn, maybeFn) {
61
+ export async function withUlwLoopMutationLock(repoRoot, scopeOrFn, maybeFn, options = {}) {
30
62
  const scope = typeof scopeOrFn === "function" ? undefined : scopeOrFn;
31
63
  const fn = typeof scopeOrFn === "function" ? scopeOrFn : maybeFn;
32
64
  if (fn === undefined)
33
65
  throw new UlwLoopError("Missing ulw-loop mutation body.", "ULW_LOOP_LOCK_BODY_MISSING");
34
66
  const lockKey = `${repoRoot}\0${ulwLoopRelativeDir(scope)}`;
35
67
  const lockPath = ulwLoopStateLockPath(repoRoot, scope);
36
- // The promise chain orders callers inside this process; the file lock is what
37
- // excludes every other process (each CLI invocation) touching the same state dir.
38
- const locked = () => withStateLock(lockPath, () => heldLocks.run(new Set([...(heldLocks.getStore() ?? []), lockKey]), fn));
68
+ const locked = () => withStateLock(lockPath, async (token) => {
69
+ return heldLocks.run(new Map([...(heldLocks.getStore() ?? []), [lockPath, { token, migrations: new Map() }]]), async () => {
70
+ for (let attempt = 0;; attempt += 1) {
71
+ try {
72
+ return await fn();
73
+ }
74
+ catch (error) {
75
+ if (!(error instanceof UlwLoopError) || error.code !== "ULW_LOOP_PUBLISH_CONFLICT")
76
+ throw error;
77
+ assertStateLockOwned(lockPath);
78
+ if (attempt !== 0)
79
+ throw error;
80
+ }
81
+ }
82
+ });
83
+ }, { ...lockOptions.getStore(), ...options });
39
84
  const prior = locks.get(lockKey) ?? Promise.resolve(undefined);
40
85
  const run = prior.then(locked, locked);
41
- // The stored gate resolves to undefined so the map never retains fn's result
42
- // (plans/audits), and it removes itself once no newer waiter replaced it —
43
- // otherwise a long-lived host leaks one entry per (repo, scope) forever.
44
86
  const gate = run.then(() => undefined, () => undefined);
45
87
  locks.set(lockKey, gate);
46
88
  void gate.then(() => {
@@ -49,47 +91,40 @@ export async function withUlwLoopMutationLock(repoRoot, scopeOrFn, maybeFn) {
49
91
  });
50
92
  return run;
51
93
  }
52
- export async function readUlwLoopPlan(repoRoot, scope) {
94
+ export function readUlwLoopPlanSync(repoRoot, scope) {
53
95
  const path = ulwLoopGoalsPath(repoRoot, scope);
54
- let raw;
55
- try {
56
- raw = await readFile(path, "utf8");
57
- }
58
- catch (error) {
59
- if (!hasCode(error, "ENOENT"))
60
- throw error;
61
- const recovery = planMissingRecovery(listUlwLoopSessionIds(repoRoot));
62
- throw new UlwLoopError(`No ulw-loop plan found at ${repoRelative(path, repoRoot)}.\n${recovery.message}`, "ULW_LOOP_PLAN_MISSING", { cause: error, ...(recovery.details === undefined ? {} : { details: recovery.details }) });
63
- }
64
- const parsed = JSON.parse(raw);
65
- if (parsed.version !== 1 || !Array.isArray(parsed.goals)) {
96
+ const parsed = reconcilePlan(ulwLoopDir(repoRoot, scope));
97
+ if (parsed === undefined)
98
+ throw planMissingError(repoRelative(path, repoRoot), listUlwLoopSessionIds(repoRoot));
99
+ if (parsed.version !== 1 || !Array.isArray(parsed.goals))
66
100
  throw new UlwLoopError(`Invalid ulw-loop plan at ${repoRelative(path, repoRoot)}.`, "ULW_LOOP_PLAN_INVALID");
67
- }
68
101
  const previousObjective = parsed.codexObjective;
69
102
  if ((parsed.codexGoalMode ?? "per_story") === "aggregate" &&
70
- isLegacyEnumeratedAggregateObjective(previousObjective)) {
71
- if (!(heldLocks.getStore()?.has(`${repoRoot}\0${ulwLoopRelativeDir(scope)}`) ?? false)) {
72
- // A read path (status/criteria) must not mutate state: mutating here runs
73
- // unlocked and a second reader could write a partially-migrated plan.
74
- throw new UlwLoopError(`The ulw-loop plan at ${repoRelative(path, repoRoot)} carries a legacy enumerated aggregate objective that must be migrated before reads continue. Run any state-mutating ulw-loop command once (e.g. \`record-evidence\`, \`steer\`, \`checkpoint\`) to migrate it under the state lock, then retry.`, "ULW_LOOP_MIGRATION_REQUIRED");
75
- }
76
- const now = iso();
103
+ previousObjective !== undefined &&
104
+ (previousObjective === LEGACY_OBJECTIVE || previousObjective.startsWith(LEGACY_OBJECTIVE_PREFIX))) {
77
105
  parsed.codexObjective = aggregateCodexObjectiveForScope(scope);
78
106
  parsed.codexObjectiveAliases = [...new Set([...(parsed.codexObjectiveAliases ?? []), previousObjective])];
79
- parsed.updatedAt = now;
80
- await writePlan(repoRoot, parsed, scope);
81
- await appendLedger(repoRoot, {
82
- at: now,
83
- kind: "aggregate_objective_migrated",
84
- message: "Migrated legacy enumerated aggregate Codex objective to the stable pointer objective.",
85
- before: { codexObjective: previousObjective },
86
- after: { codexObjective: parsed.codexObjective },
87
- }, scope);
107
+ heldLocks
108
+ .getStore()
109
+ ?.get(ulwLoopStateLockPath(repoRoot, scope))
110
+ ?.migrations.set(parsed.goalsPath, [
111
+ {
112
+ at: iso(),
113
+ kind: "aggregate_objective_migrated",
114
+ before: { codexObjective: previousObjective },
115
+ after: { codexObjective: parsed.codexObjective },
116
+ },
117
+ ]);
88
118
  }
89
119
  return parsed;
90
120
  }
91
- // Session dirs are the only recovery hint that matters when a plan or a scope is
92
- // missing: the caller is almost always meant to target one of these siblings.
121
+ export async function readUlwLoopPlan(repoRoot, scope) {
122
+ if (heldLocks.getStore()?.has(ulwLoopStateLockPath(repoRoot, scope))) {
123
+ assertStateLockOwned(ulwLoopStateLockPath(repoRoot, scope));
124
+ await materialize(ulwLoopDir(repoRoot, scope));
125
+ }
126
+ return readUlwLoopPlanSync(repoRoot, scope);
127
+ }
93
128
  export function listUlwLoopSessionIds(repoRoot) {
94
129
  try {
95
130
  return readdirSync(ulwLoopDir(repoRoot), { withFileTypes: true })
@@ -102,72 +137,28 @@ export function listUlwLoopSessionIds(repoRoot) {
102
137
  }
103
138
  }
104
139
  export async function writePlan(repoRoot, plan, scope) {
105
- await mkdir(ulwLoopDir(repoRoot, scope), { recursive: true });
106
- const path = ulwLoopGoalsPath(repoRoot, scope);
107
- const tmpPath = `${path}.${process.pid}.${Date.now()}.tmp`;
108
- await writeFile(tmpPath, `${JSON.stringify(plan, null, 2)}\n`, "utf8");
109
- await rename(tmpPath, path);
140
+ await commit(repoRoot, scope, { plan, entries: [] });
110
141
  }
111
142
  export async function appendLedger(repoRoot, entry, scope) {
112
143
  await appendLedgerEntries(repoRoot, [entry], scope);
113
144
  }
114
145
  export async function appendLedgerEntries(repoRoot, entries, scope) {
115
- if (entries.length === 0)
116
- return;
117
- await mkdir(ulwLoopDir(repoRoot, scope), { recursive: true });
118
- await appendFile(ulwLoopLedgerPath(repoRoot, scope), `${entries.map((entry) => JSON.stringify(entry)).join("\n")}\n`, "utf8");
146
+ if (entries.length > 0)
147
+ await commit(repoRoot, scope, { plan: await readUlwLoopPlan(repoRoot, scope), entries });
119
148
  }
120
- /**
121
- * Streams raw ledger lines without materializing the file. Real ledgers reach
122
- * many MB (legacy entries embedded full-plan snapshots), so `readFile` here
123
- * ballooned every steer/dedup path; line-at-a-time keeps memory O(longest line).
124
- */
125
- async function* ledgerLines(repoRoot, scope) {
126
- const stream = createReadStream(ulwLoopLedgerPath(repoRoot, scope), { encoding: "utf8" });
127
- const lines = createInterface({ input: stream, crlfDelay: Number.POSITIVE_INFINITY });
128
- try {
129
- for await (const line of lines) {
130
- if (line.trim().length > 0)
131
- yield line;
132
- }
133
- }
134
- catch (error) {
135
- if (!hasCode(error, "ENOENT"))
136
- throw error;
137
- }
138
- finally {
139
- lines.close();
140
- stream.destroy();
141
- }
149
+ function isSteeringKind(value) {
150
+ return (value === "steering_accepted" ||
151
+ value === "steering_rejected" ||
152
+ value === "criteria_revised" ||
153
+ value === "batch_updated");
142
154
  }
143
155
  export async function readSteeringLedgerEntries(repoRoot, scope) {
144
- const entries = [];
145
- for await (const line of ledgerLines(repoRoot, scope)) {
146
- const entry = JSON.parse(line);
147
- if (isSteeringKind(entry.kind))
148
- entries.push(entry);
149
- }
150
- return entries;
156
+ return readLedger(repoRoot, scope).filter((entry) => isSteeringKind(entry.kind));
151
157
  }
152
- /**
153
- * First accepted steering entry matching an idempotency key/prompt signature.
154
- * A cheap substring probe on the raw line skips JSON.parse for the vast
155
- * majority of entries, so dedup stays flat even on legacy multi-MB ledgers.
156
- */
157
158
  export async function findAcceptedSteeringLedgerEntry(repoRoot, key, scope) {
158
- const probe = JSON.stringify(key);
159
- for await (const line of ledgerLines(repoRoot, scope)) {
160
- if (!line.includes(probe))
161
- continue;
162
- const entry = JSON.parse(line);
163
- if (!isSteeringKind(entry.kind))
164
- continue;
165
- if (entry.steering?.invariant.accepted !== true)
166
- continue;
167
- if (entry.idempotencyKey === key ||
159
+ return readLedger(repoRoot, scope).find((entry) => isSteeringKind(entry.kind) &&
160
+ entry.steering?.invariant.accepted === true &&
161
+ (entry.idempotencyKey === key ||
168
162
  entry.steering.idempotencyKey === key ||
169
- entry.steering.promptSignature === key)
170
- return entry;
171
- }
172
- return undefined;
163
+ entry.steering.promptSignature === key));
173
164
  }
@@ -0,0 +1,15 @@
1
+ import { type UlwLoopScope } from "./paths.js";
2
+ import type { UlwLoopLedgerEntry, UlwLoopPlan } from "./types.js";
3
+ export interface PlanCommitRecord {
4
+ readonly version: 1;
5
+ readonly revision: number;
6
+ readonly plan: UlwLoopPlan;
7
+ readonly ledger: readonly UlwLoopLedgerEntry[];
8
+ }
9
+ export declare function hasCode(error: unknown, code: string): boolean;
10
+ export declare function readOptional(path: string): string | undefined;
11
+ export declare function logNames(dir: string): string[];
12
+ export declare function readRecords(dir: string): PlanCommitRecord[];
13
+ export declare function readNewestRecord(dir: string): PlanCommitRecord | undefined;
14
+ export declare function reconcilePlan(dir: string): UlwLoopPlan | undefined;
15
+ export declare function planExists(repoRoot: string, scope?: UlwLoopScope): boolean;
@@ -0,0 +1,86 @@
1
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { ulwLoopDir } from "./paths.js";
4
+ export function hasCode(error, code) {
5
+ return error instanceof Error && "code" in error && error.code === code;
6
+ }
7
+ export function readOptional(path) {
8
+ try {
9
+ return readFileSync(path, "utf8");
10
+ }
11
+ catch (error) {
12
+ if (hasCode(error, "ENOENT"))
13
+ return undefined;
14
+ throw error;
15
+ }
16
+ }
17
+ export function logNames(dir) {
18
+ try {
19
+ return readdirSync(join(dir, "revisions"))
20
+ .filter((name) => /^\d{8,}\.json$/.test(name))
21
+ .sort();
22
+ }
23
+ catch (error) {
24
+ if (hasCode(error, "ENOENT"))
25
+ return [];
26
+ throw error;
27
+ }
28
+ }
29
+ function readRecord(dir, name) {
30
+ try {
31
+ const record = JSON.parse(readFileSync(join(dir, "revisions", name), "utf8"));
32
+ return record.version === 1 &&
33
+ Number.isInteger(record.revision) &&
34
+ record.plan?.version === 1 &&
35
+ Array.isArray(record.plan.goals) &&
36
+ Array.isArray(record.ledger)
37
+ ? record
38
+ : undefined;
39
+ }
40
+ catch (error) {
41
+ if (!(error instanceof SyntaxError))
42
+ throw error;
43
+ return undefined;
44
+ }
45
+ }
46
+ // Only the audit trail needs every record; plan reads use readNewestRecord.
47
+ export function readRecords(dir) {
48
+ const records = [];
49
+ for (const name of logNames(dir)) {
50
+ const record = readRecord(dir, name);
51
+ if (record !== undefined)
52
+ records.push(record);
53
+ }
54
+ return records.sort((a, b) => a.revision - b.revision);
55
+ }
56
+ // Every ulw-loop status probe reads the plan, each record embeds a full plan copy, and records are never
57
+ // deleted - so a plan read parses the newest record only, newest first, and stops there. Names are ordered
58
+ // by their parsed revision because a wider zero-padding would break lexicographic order.
59
+ export function readNewestRecord(dir) {
60
+ const names = logNames(dir).sort((a, b) => Number.parseInt(b, 10) - Number.parseInt(a, 10));
61
+ for (const name of names) {
62
+ const record = readRecord(dir, name);
63
+ if (record !== undefined)
64
+ return record;
65
+ }
66
+ return undefined;
67
+ }
68
+ export function reconcilePlan(dir) {
69
+ const raw = readOptional(join(dir, "goals.json"));
70
+ let cached;
71
+ if (raw !== undefined) {
72
+ try {
73
+ cached = JSON.parse(raw);
74
+ }
75
+ catch (error) {
76
+ if (!(error instanceof SyntaxError))
77
+ throw error;
78
+ }
79
+ }
80
+ const latest = readNewestRecord(dir);
81
+ return latest !== undefined && latest.revision >= (cached?.revision ?? 0) ? latest.plan : cached;
82
+ }
83
+ export function planExists(repoRoot, scope) {
84
+ const dir = ulwLoopDir(repoRoot, scope);
85
+ return existsSync(join(dir, "goals.json")) || logNames(dir).length > 0;
86
+ }
@@ -1,4 +1,7 @@
1
+ import { UlwLoopError } from "./runtime.js";
2
+ import type { UlwLoopToolkitSurface } from "./surface.js";
1
3
  export declare const ULW_LOOP_CREATE_GOALS_COMMAND = "omo-agent-toolkit ulw-loop create-goals --brief \"<brief>\" --json";
4
+ export declare function createGoalsAction(surface: UlwLoopToolkitSurface): string;
2
5
  export interface PlanMissingRecovery {
3
6
  readonly message: string;
4
7
  readonly details?: {
@@ -9,6 +12,7 @@ export interface PlanMissingRecovery {
9
12
  * A missing plan is either "never bootstrapped" or "bootstrapped under a different
10
13
  * session id"; the recovery text has to answer both without the caller guessing.
11
14
  */
12
- export declare function planMissingRecovery(existingSessionIds: readonly string[]): PlanMissingRecovery;
15
+ export declare function planMissingRecovery(existingSessionIds: readonly string[], surface?: UlwLoopToolkitSurface): PlanMissingRecovery;
16
+ export declare function planMissingError(planPath: string, existingSessionIds: readonly string[], surface?: UlwLoopToolkitSurface): UlwLoopError;
13
17
  export declare function sessionScopeRequiredMessage(flag: string, existingSessionIds: readonly string[]): string;
14
18
  export declare function sessionIdRequiredMessage(flag: string): string;
@@ -1,15 +1,27 @@
1
+ import { UlwLoopError } from "./runtime.js";
1
2
  export const ULW_LOOP_CREATE_GOALS_COMMAND = 'omo-agent-toolkit ulw-loop create-goals --brief "<brief>" --json';
3
+ export function createGoalsAction(surface) {
4
+ return surface === "omo-senpi" ? 'agentToolkit.createGoals({ brief: "<brief>" })' : ULW_LOOP_CREATE_GOALS_COMMAND;
5
+ }
2
6
  /**
3
7
  * A missing plan is either "never bootstrapped" or "bootstrapped under a different
4
8
  * session id"; the recovery text has to answer both without the caller guessing.
5
9
  */
6
- export function planMissingRecovery(existingSessionIds) {
7
- const lines = [`Recovery: bootstrap the plan with \`${ULW_LOOP_CREATE_GOALS_COMMAND}\`.`];
10
+ export function planMissingRecovery(existingSessionIds, surface = "lazycodex") {
11
+ const lines = [`Recovery: bootstrap the plan with \`${createGoalsAction(surface)}\`.`];
8
12
  if (existingSessionIds.length === 0)
9
13
  return { message: lines.join("\n") };
10
- lines.push(`Existing ulw-loop session ids under .omo/ulw-loop/: ${existingSessionIds.join(", ")}. Re-run with \`--session-id <id>\` to target one of them.`);
14
+ lines.push(surface === "omo-senpi"
15
+ ? `Existing ulw-loop session ids under .omo/ulw-loop/: ${existingSessionIds.join(", ")}. The SDK is bound to the current session; resume the owning session to target its plan.`
16
+ : `Existing ulw-loop session ids under .omo/ulw-loop/: ${existingSessionIds.join(", ")}. Re-run with \`--session-id <id>\` to target one of them.`);
11
17
  return { message: lines.join("\n"), details: { existingSessionIds } };
12
18
  }
19
+ export function planMissingError(planPath, existingSessionIds, surface = "lazycodex") {
20
+ const recovery = planMissingRecovery(existingSessionIds, surface);
21
+ return new UlwLoopError(`No ulw-loop plan found at ${planPath}.\n${recovery.message}`, "ULW_LOOP_PLAN_MISSING", {
22
+ ...(recovery.details === undefined ? {} : { details: recovery.details }),
23
+ });
24
+ }
13
25
  export function sessionScopeRequiredMessage(flag, existingSessionIds) {
14
26
  const lines = [
15
27
  "No ulw-loop session scope: neither the session env (OMO_ULW_LOOP_SESSION_ID / CODEX_SESSION_ID / CODEX_THREAD_ID / PI_SESSION_ID) nor the flag names this run, and the shared .omo/ulw-loop root is never used implicitly because every session in this directory would read and overwrite it.",
@@ -1,8 +1,9 @@
1
1
  // biome-ignore-all format: compact port must stay within the requested pure LOC budget.
2
2
  import { CodexGoalSnapshotError, formatCodexGoalReconciliation, readCodexGoalSnapshotInput, reconcileCodexGoalSnapshot, } from "./codex-goal-snapshot.js";
3
3
  import { compatibleCodexObjectives, expectedCodexObjective, isFinalRunCompletionCandidate } from "./goal-status.js";
4
+ import { commit } from "./plan-commit.js";
4
5
  import { seedDefaultSuccessCriteria } from "./plan-crud.js";
5
- import { appendLedger, readUlwLoopPlan, withUlwLoopMutationLock, writePlan } from "./plan-io.js";
6
+ import { readUlwLoopPlan, withUlwLoopMutationLock } from "./plan-io.js";
6
7
  import { iso, UlwLoopError } from "./types.js";
7
8
  const BLOCKER_FIELDS = "blockedReason blockerSignature blockerOccurrenceCount requiredExternalDecision nonRetriable failedAt failureReason completedAt blocker blockerEvidence blockerOccurrences blockedAt".split(" ");
8
9
  function ulwLoopError(message, code) {
@@ -64,9 +65,7 @@ export async function recordFinalReviewBlockers(repoRoot, args, scope) {
64
65
  const summaryEntry = { at: now, kind: "goal_review_blocked", goalId: goal.id, status: goal.status, evidence: args.evidence, codexGoal, message: `Review blockers recorded; appended ${newGoal.id}.` };
65
66
  Reflect.set(summaryEntry, "kind", "blocker_recorded");
66
67
  const ledgerEntries = [blockedEntry, addedEntry, summaryEntry];
67
- await writePlan(repoRoot, plan, scope);
68
- for (const entry of ledgerEntries)
69
- await appendLedger(repoRoot, entry, scope);
68
+ await commit(repoRoot, scope, { plan, entries: ledgerEntries });
70
69
  return {
71
70
  plan,
72
71
  blockedGoal: goal,
@@ -1,9 +1,10 @@
1
1
  import { checkpointUlwLoop } from "../checkpoint.js";
2
2
  import { checkpointTemplate } from "../checkpoint-template.js";
3
3
  import { recordEvidence } from "../evidence.js";
4
- import { ulwLoopAttemptEvidenceDir } from "../paths.js";
4
+ import { normalizeUlwLoopSessionId, ulwLoopAttemptEvidenceDir, ulwLoopGoalsRelativePath, } from "../paths.js";
5
5
  import { addUlwLoopGoal, createUlwLoopPlan, startNextUlwLoop, summarizeUlwLoopPlan } from "../plan-crud.js";
6
- import { readUlwLoopPlan } from "../plan-io.js";
6
+ import { listUlwLoopSessionIds, readUlwLoopPlan } from "../plan-io.js";
7
+ import { planMissingError } from "../plan-missing-recovery.js";
7
8
  import { recordFinalReviewBlockers } from "../review-blockers.js";
8
9
  import { UlwLoopError } from "../runtime.js";
9
10
  import { statusNextActions } from "../status-next-actions.js";
@@ -24,8 +25,12 @@ function validateCodexGoalJson(raw) {
24
25
  function validateContext(context) {
25
26
  if (!context.cwd.trim())
26
27
  throw new UlwLoopError("cwd is required.", "ULW_LOOP_CWD_REQUIRED");
27
- if (!context.sessionId.trim())
28
+ const sessionId = context.sessionId.trim();
29
+ if (!sessionId)
28
30
  throw new UlwLoopError("ULW_LOOP_SESSION_ID_REQUIRED: sessionId is required.", "ULW_LOOP_SESSION_ID_REQUIRED");
31
+ const normalizedSessionId = normalizeUlwLoopSessionId(sessionId);
32
+ if (normalizedSessionId === null || /(?:^|[\\/])\.\.(?:[\\/]|$)/.test(sessionId))
33
+ throw new UlwLoopError("ULW_LOOP_SESSION_ID_INVALID: sessionId normalizes to null.", "ULW_LOOP_SESSION_ID_INVALID");
29
34
  if (context.surface !== "omo-senpi" && context.surface !== "lazycodex")
30
35
  throw new UlwLoopError("surface must be omo-senpi or lazycodex.", "ULW_LOOP_SURFACE_INVALID");
31
36
  }
@@ -73,6 +78,8 @@ export function createAgentToolkit(context, deps = {}) {
73
78
  return await notify(operation, { ok: true, operation, result, nextActions });
74
79
  }
75
80
  catch (error) {
81
+ if (error instanceof UlwLoopError && error.code === "ULW_LOOP_PLAN_MISSING" && context.surface === "omo-senpi")
82
+ return notify(operation, failure(operation, planMissingError(ulwLoopGoalsRelativePath(scope), listUlwLoopSessionIds(context.cwd), context.surface)));
76
83
  const response = caught(operation, error instanceof Error ? error : new Error("ULW_LOOP_ERROR"));
77
84
  return notify(operation, response);
78
85
  }
@@ -107,14 +114,14 @@ export function createAgentToolkit(context, deps = {}) {
107
114
  }
108
115
  },
109
116
  help: () => invoke("help", async () => ULW_LOOP_MANIFEST),
110
- createGoals: (args) => invoke("create-goals", () => createUlwLoopPlan(context.cwd, args, scope)),
117
+ createGoals: (args) => invoke("create-goals", () => createUlwLoopPlan(context.cwd, args, scope, context.surface)),
111
118
  status: () => invoke("status", async () => {
112
119
  const plan = await readUlwLoopPlan(context.cwd, scope);
113
120
  const active = plan.goals.find((goal) => goal.id === plan.activeGoalId);
114
121
  return {
115
122
  plan,
116
123
  summary: summarizeUlwLoopPlan(plan),
117
- nextActions: statusNextActions(plan),
124
+ nextActions: statusNextActions(plan, context.surface),
118
125
  // Attempt directories are an evidence-layout v2 concept; a v1 plan must not advertise one.
119
126
  ...(active === undefined || plan.evidenceLayoutVersion !== 2
120
127
  ? {}
@@ -0,0 +1,5 @@
1
+ export declare function readAdmissionBreaker(sessionId: string): string | null;
2
+ export declare function atomicWriteJson(targetPath: string, data: unknown): void;
3
+ export declare function isNonEmptyFile(path: string): boolean;
4
+ export declare function readCount(counterPath: string): number;
5
+ export declare function readCounts(counterPath: string): Record<string, number>;
@@ -0,0 +1,67 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { existsSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
3
+ import { dirname, join } from "node:path";
4
+ export function readAdmissionBreaker(sessionId) {
5
+ const dataDir = process.env["PLUGIN_DATA"];
6
+ if (typeof dataDir !== "string")
7
+ return null;
8
+ try {
9
+ const value = JSON.parse(readFileSync(join(dataDir, "spawn-breaker", `${sessionId}.json`), "utf8"));
10
+ return typeof value === "object" && value !== null && "reason" in value && typeof value.reason === "string"
11
+ ? value.reason
12
+ : "capacity limit";
13
+ }
14
+ catch {
15
+ return null;
16
+ }
17
+ }
18
+ // These hook budget files are not plan/audit state and remain outside the log.
19
+ export function atomicWriteJson(targetPath, data) {
20
+ const tmp = join(dirname(targetPath), `.tmp-${randomBytes(6).toString("hex")}`);
21
+ writeFileSync(tmp, JSON.stringify(data));
22
+ renameSync(tmp, targetPath);
23
+ }
24
+ export function isNonEmptyFile(path) {
25
+ try {
26
+ return existsSync(path) && statSync(path).size > 0;
27
+ }
28
+ catch (error) {
29
+ if (error instanceof Error)
30
+ return false;
31
+ throw error;
32
+ }
33
+ }
34
+ export function readCount(counterPath) {
35
+ try {
36
+ const parsed = JSON.parse(readFileSync(counterPath, "utf8"));
37
+ return typeof parsed === "object" &&
38
+ parsed !== null &&
39
+ "count" in parsed &&
40
+ typeof parsed.count === "number" &&
41
+ parsed.count >= 0
42
+ ? parsed.count
43
+ : 0;
44
+ }
45
+ catch (error) {
46
+ if (error instanceof Error)
47
+ return 0;
48
+ throw error;
49
+ }
50
+ }
51
+ export function readCounts(counterPath) {
52
+ try {
53
+ const parsed = JSON.parse(readFileSync(counterPath, "utf8"));
54
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
55
+ return {};
56
+ const counts = {};
57
+ for (const [key, value] of Object.entries(parsed))
58
+ if (typeof value === "number" && value >= 0)
59
+ counts[key] = value;
60
+ return counts;
61
+ }
62
+ catch (error) {
63
+ if (error instanceof Error)
64
+ return {};
65
+ throw error;
66
+ }
67
+ }