aegisone 1.0.34 → 1.0.35

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 (226) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +1 -1
  3. package/SOURCE-OFFER.txt +9 -0
  4. package/bin/aegis.js +1 -322
  5. package/package.json +4 -5
  6. package/scripts/bump.mjs +1 -211
  7. package/scripts/bundle.js +1 -129
  8. package/scripts/predist.mjs +1 -976
  9. package/src/child.js +1 -285
  10. package/src/dispatch.js +1 -269
  11. package/src/doctor.js +1 -639
  12. package/src/host.js +1 -111
  13. package/src/permissions.js +1 -383
  14. package/src/probes.js +1 -159
  15. package/src/release.js +1 -409
  16. package/vendor/cli/package.json +3 -7
  17. package/vendor/cli/scripts/check.mjs +1 -52
  18. package/vendor/cli/scripts/demo.mjs +1 -94
  19. package/vendor/cli/scripts/predist.mjs +1 -75
  20. package/vendor/cli/scripts/runtests.mjs +1 -56
  21. package/vendor/cli/scripts/seal-gate.mjs +1 -74
  22. package/vendor/cli/src/agents.js +1 -139
  23. package/vendor/cli/src/app.js +1 -2957
  24. package/vendor/cli/src/art.js +1 -289
  25. package/vendor/cli/src/chatflow.js +1 -3023
  26. package/vendor/cli/src/checkpoint.js +1 -90
  27. package/vendor/cli/src/clipboard.js +1 -424
  28. package/vendor/cli/src/cloudsync.js +1 -528
  29. package/vendor/cli/src/commands.js +1 -4089
  30. package/vendor/cli/src/config.js +1 -581
  31. package/vendor/cli/src/connect.js +1 -349
  32. package/vendor/cli/src/credentials.js +1 -33
  33. package/vendor/cli/src/deps.js +1 -232
  34. package/vendor/cli/src/devrun.js +1 -187
  35. package/vendor/cli/src/diff.js +1 -333
  36. package/vendor/cli/src/diffview.js +1 -296
  37. package/vendor/cli/src/engine.js +1 -297
  38. package/vendor/cli/src/entitlement.js +1 -355
  39. package/vendor/cli/src/events.js +1 -707
  40. package/vendor/cli/src/export.js +1 -67
  41. package/vendor/cli/src/filepreview.js +1 -153
  42. package/vendor/cli/src/fingerprint.js +1 -592
  43. package/vendor/cli/src/format.js +1 -259
  44. package/vendor/cli/src/fuzzy.js +1 -119
  45. package/vendor/cli/src/history.js +1 -423
  46. package/vendor/cli/src/init.js +1 -175
  47. package/vendor/cli/src/input.js +1 -139
  48. package/vendor/cli/src/keys.js +1 -183
  49. package/vendor/cli/src/markdown.js +1 -306
  50. package/vendor/cli/src/models.js +1 -507
  51. package/vendor/cli/src/naming.js +1 -57
  52. package/vendor/cli/src/overlays.js +1 -462
  53. package/vendor/cli/src/panels.js +1 -1418
  54. package/vendor/cli/src/ping.js +1 -226
  55. package/vendor/cli/src/presence.js +1 -238
  56. package/vendor/cli/src/render.js +1 -516
  57. package/vendor/cli/src/screen.js +1 -518
  58. package/vendor/cli/src/screens.js +1 -896
  59. package/vendor/cli/src/secret.js +1 -59
  60. package/vendor/cli/src/securefs.js +1 -359
  61. package/vendor/cli/src/settings.js +1 -155
  62. package/vendor/cli/src/shared.js +1 -52
  63. package/vendor/cli/src/sharedpaths.js +1 -53
  64. package/vendor/cli/src/signup.js +1 -156
  65. package/vendor/cli/src/summarize.js +1 -95
  66. package/vendor/cli/src/sysdiag.js +1 -585
  67. package/vendor/cli/src/system.js +1 -40
  68. package/vendor/cli/src/theme.js +1 -570
  69. package/vendor/cli/src/title.js +1 -220
  70. package/vendor/cli/src/todos.js +1 -115
  71. package/vendor/cli/src/tokens.js +1 -263
  72. package/vendor/cli/src/topup.js +1 -210
  73. package/vendor/cli/src/update.js +1 -191
  74. package/vendor/cli/vendor/client/aegis.js +1 -2441
  75. package/vendor/cli/vendor/client/attest.js +1 -518
  76. package/vendor/cli/vendor/client/authorship-key.js +1 -410
  77. package/vendor/cli/vendor/client/auto-provision.js +1 -134
  78. package/vendor/cli/vendor/client/brain-catalog.js +1 -268
  79. package/vendor/cli/vendor/client/byok-catalog.js +1 -199
  80. package/vendor/cli/vendor/client/catalog-client.js +1 -487
  81. package/vendor/cli/vendor/client/conversation-export.js +1 -177
  82. package/vendor/cli/vendor/client/credentials.js +1 -494
  83. package/vendor/cli/vendor/client/device-fingerprint.js +1 -141
  84. package/vendor/cli/vendor/client/drive-connect.js +1 -110
  85. package/vendor/cli/vendor/client/drive-upload.js +1 -234
  86. package/vendor/cli/vendor/client/env-file.js +1 -1273
  87. package/vendor/cli/vendor/client/fingerprint.js +1 -1064
  88. package/vendor/cli/vendor/client/foreign-memory.js +1 -669
  89. package/vendor/cli/vendor/client/google-signin.js +1 -133
  90. package/vendor/cli/vendor/client/hooks.js +1 -430
  91. package/vendor/cli/vendor/client/model-discovery.js +1 -373
  92. package/vendor/cli/vendor/client/naming.js +1 -283
  93. package/vendor/cli/vendor/client/platform.js +1 -937
  94. package/vendor/cli/vendor/client/prefs.js +1 -351
  95. package/vendor/cli/vendor/client/presence-constants.js +1 -107
  96. package/vendor/cli/vendor/client/presence.js +1 -438
  97. package/vendor/cli/vendor/client/record-vault.js +3 -1055
  98. package/vendor/cli/vendor/client/session-store.js +1 -575
  99. package/vendor/cli/vendor/client/topup.js +1 -297
  100. package/vendor/cli/vendor/client/touch.js +1 -458
  101. package/vendor/cli/vendor/client/update.js +1 -763
  102. package/vendor/cli/vendor/client/user-models.js +1 -381
  103. package/vendor/cli/vendor/desktop/lib/local/agents.js +1 -105
  104. package/vendor/cli/vendor/desktop/lib/local/attachments.js +1 -276
  105. package/vendor/cli/vendor/desktop/lib/local/autonomous.js +1 -1045
  106. package/vendor/cli/vendor/desktop/lib/local/claude-pro.js +1 -1028
  107. package/vendor/cli/vendor/desktop/lib/local/context.js +1 -125
  108. package/vendor/cli/vendor/desktop/lib/local/engine.js +1 -3666
  109. package/vendor/cli/vendor/desktop/lib/local/entitlement.js +1 -660
  110. package/vendor/cli/vendor/desktop/lib/local/git-scope.js +1 -228
  111. package/vendor/cli/vendor/desktop/lib/local/local.js +1 -2145
  112. package/vendor/cli/vendor/desktop/lib/local/prompt.js +1 -280
  113. package/vendor/cli/vendor/desktop/lib/local/queue.js +1 -560
  114. package/vendor/cli/vendor/desktop/lib/local/session-rounds.js +1 -238
  115. package/vendor/cli/vendor/desktop/lib/local/spill.js +1 -75
  116. package/vendor/cli/vendor/desktop/lib/local/turn-guard.js +1 -300
  117. package/vendor/cli/vendor/desktop/lib/local/worktree-lock.js +1 -235
  118. package/vendor/cli/vendor/desktop/lib/settings.js +1 -890
  119. package/vendor/cli/vendor/desktop/renderer/usage.js +1 -1239
  120. package/vendor/cli/vendor/hosts/install.js +1 -638
  121. package/vendor/cli/vendor/hosts/openai-shim.js +1 -515
  122. package/vendor/cli/vendor/hosts/spec.js +1 -164
  123. package/vendor/cli/vendor/hosts/targets.js +1 -346
  124. package/vendor/cli/vendor/mcp/server.js +1 -221
  125. package/vendor/cli/vendor/mcp/tools.js +1 -408
  126. package/vendor/cli/vendor/one/src/child.js +1 -285
  127. package/vendor/cli/vendor/one/src/permissions.js +1 -383
  128. package/vendor/desktop/bin/aegis.js +1 -32
  129. package/vendor/desktop/lib/account-key.js +1 -56
  130. package/vendor/desktop/lib/auto.js +1 -461
  131. package/vendor/desktop/lib/avatar/companion.js +1 -698
  132. package/vendor/desktop/lib/avatar/cosmetics.js +1 -532
  133. package/vendor/desktop/lib/avatar/events.js +1 -344
  134. package/vendor/desktop/lib/avatar/holder-turn.js +1 -350
  135. package/vendor/desktop/lib/avatar/holders.js +1 -612
  136. package/vendor/desktop/lib/avatar/identity.js +1 -641
  137. package/vendor/desktop/lib/avatar/level.js +1 -368
  138. package/vendor/desktop/lib/avatar/mirror.js +1 -524
  139. package/vendor/desktop/lib/avatar/persona.js +1 -388
  140. package/vendor/desktop/lib/avatar/profile.js +1 -977
  141. package/vendor/desktop/lib/avatar/register.js +1 -307
  142. package/vendor/desktop/lib/avatar/store.js +1 -472
  143. package/vendor/desktop/lib/avatar/tiers.js +1 -38
  144. package/vendor/desktop/lib/avatar/transfer.js +1 -415
  145. package/vendor/desktop/lib/avatar/turn.js +1 -216
  146. package/vendor/desktop/lib/avatar/voice.js +1 -328
  147. package/vendor/desktop/lib/avatar/xp.js +1 -404
  148. package/vendor/desktop/lib/deep-link.js +1 -120
  149. package/vendor/desktop/lib/fingerprint.js +2 -347
  150. package/vendor/desktop/lib/google-connect.js +1 -323
  151. package/vendor/desktop/lib/local/agents.js +1 -105
  152. package/vendor/desktop/lib/local/attachments.js +1 -276
  153. package/vendor/desktop/lib/local/autonomous.js +1 -1045
  154. package/vendor/desktop/lib/local/claude-pro.js +1 -1028
  155. package/vendor/desktop/lib/local/context.js +1 -125
  156. package/vendor/desktop/lib/local/engine.js +1 -3666
  157. package/vendor/desktop/lib/local/entitlement.js +1 -660
  158. package/vendor/desktop/lib/local/git-scope.js +1 -228
  159. package/vendor/desktop/lib/local/local.js +1 -2145
  160. package/vendor/desktop/lib/local/prompt.js +1 -280
  161. package/vendor/desktop/lib/local/queue.js +1 -560
  162. package/vendor/desktop/lib/local/session-rounds.js +1 -238
  163. package/vendor/desktop/lib/local/spill.js +1 -75
  164. package/vendor/desktop/lib/local/telemetry.js +1 -470
  165. package/vendor/desktop/lib/local/turn-guard.js +1 -300
  166. package/vendor/desktop/lib/local/worktree-lock.js +1 -235
  167. package/vendor/desktop/lib/provider-key-mirror.js +1 -252
  168. package/vendor/desktop/lib/quick-launcher.js +1 -84
  169. package/vendor/desktop/lib/settings.js +1 -890
  170. package/vendor/desktop/lib/sync/memory-queue.js +1 -60
  171. package/vendor/desktop/lib/sync/persist-gate.js +1 -148
  172. package/vendor/desktop/lib/sync/sessions.js +1 -106
  173. package/vendor/desktop/lib/window-state.js +1 -66
  174. package/vendor/desktop/package.json +2 -4
  175. package/vendor/desktop/preload.js +1 -679
  176. package/vendor/desktop/renderer/app.js +1 -7250
  177. package/vendor/desktop/renderer/avatar/assemble.js +1 -335
  178. package/vendor/desktop/renderer/avatar/avatar.js +1 -380
  179. package/vendor/desktop/renderer/avatar/cards.js +1 -141
  180. package/vendor/desktop/renderer/avatar/hud.js +1 -206
  181. package/vendor/desktop/renderer/avatar/machine.js +1 -213
  182. package/vendor/desktop/renderer/avatar/pane.js +1 -439
  183. package/vendor/desktop/renderer/avatar/parts.js +1 -437
  184. package/vendor/desktop/renderer/budget.js +1 -172
  185. package/vendor/desktop/renderer/companion.js +1 -223
  186. package/vendor/desktop/renderer/diff.js +1 -359
  187. package/vendor/desktop/renderer/diffview.js +1 -336
  188. package/vendor/desktop/renderer/filepreview.js +1 -211
  189. package/vendor/desktop/renderer/fingerprint.js +3 -789
  190. package/vendor/desktop/renderer/markdown.js +1 -168
  191. package/vendor/desktop/renderer/provider-mark.js +1 -311
  192. package/vendor/desktop/renderer/provider-toolbar.js +1 -134
  193. package/vendor/desktop/renderer/providers.js +1 -259
  194. package/vendor/desktop/renderer/quick.js +1 -285
  195. package/vendor/desktop/renderer/stream-policy.js +1 -387
  196. package/vendor/desktop/renderer/transcript-view.js +1 -323
  197. package/vendor/desktop/renderer/usage.js +1 -1239
  198. package/vendor/desktop/vendor/aegis.js +1 -2441
  199. package/vendor/desktop/vendor/attest.js +1 -518
  200. package/vendor/desktop/vendor/authorship-key.js +1 -410
  201. package/vendor/desktop/vendor/auto-provision.js +1 -134
  202. package/vendor/desktop/vendor/brain-catalog.js +1 -268
  203. package/vendor/desktop/vendor/byok-catalog.js +1 -199
  204. package/vendor/desktop/vendor/catalog-client.js +1 -487
  205. package/vendor/desktop/vendor/conversation-export.js +1 -177
  206. package/vendor/desktop/vendor/credentials.js +1 -494
  207. package/vendor/desktop/vendor/device-fingerprint.js +1 -141
  208. package/vendor/desktop/vendor/drive-connect.js +1 -110
  209. package/vendor/desktop/vendor/drive-upload.js +1 -234
  210. package/vendor/desktop/vendor/env-file.js +1 -1273
  211. package/vendor/desktop/vendor/fingerprint.js +1 -1064
  212. package/vendor/desktop/vendor/foreign-memory.js +1 -669
  213. package/vendor/desktop/vendor/google-signin.js +1 -133
  214. package/vendor/desktop/vendor/hooks.js +1 -430
  215. package/vendor/desktop/vendor/model-discovery.js +1 -373
  216. package/vendor/desktop/vendor/naming.js +1 -283
  217. package/vendor/desktop/vendor/platform.js +1 -937
  218. package/vendor/desktop/vendor/prefs.js +1 -351
  219. package/vendor/desktop/vendor/presence-constants.js +1 -107
  220. package/vendor/desktop/vendor/presence.js +1 -438
  221. package/vendor/desktop/vendor/record-vault.js +3 -1055
  222. package/vendor/desktop/vendor/session-store.js +1 -575
  223. package/vendor/desktop/vendor/topup.js +1 -297
  224. package/vendor/desktop/vendor/touch.js +1 -458
  225. package/vendor/desktop/vendor/update.js +1 -763
  226. package/vendor/desktop/vendor/user-models.js +1 -381
@@ -1,237 +1,3 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-or-later
2
2
  // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
- // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
- // Conveying a modified copy, or running one as a network service, owes the
5
- // corresponding source under this licence (§5/§13) — see LICENSING.md.
6
- 'use strict';
7
-
8
- /**
9
- * Cross-process lock on a git working tree.
10
- *
11
- * Ported from the upstream engine reference (ESM → CommonJS).
12
- *
13
- * Bug this exists for: nothing keyed on *the working tree*. A session-scoped
14
- * lock only stops two processes resuming the SAME transcript, so three
15
- * different sessions in one checkout are three different ids and therefore
16
- * all legal. A queue-worker lock only serialises queue drains. An interactive
17
- * session took no lock at all.
18
- *
19
- * So N agents editing one checkout was, by every existing lock, correct
20
- * behaviour — and it is destructive. It happened for real: one session ran
21
- * `git checkout HEAD --` against a file a second session was mid-edit on
22
- * (destroying it), a third ran `git stash` and parked everybody's tree, and a
23
- * test run on that shared tree reported failures belonging to a half-built
24
- * feature that was not the caller's. Git has no advisory locking, so none of
25
- * it produced an error — just unexplained file changes, which is exactly the
26
- * kind of evidence that gets misread as "my tool lied to me".
27
- *
28
- * Fix: a lock keyed on the repository root — the resource actually contended
29
- * — taken by every host that runs a turn (the desktop app and the CLI both
30
- * reach this file through desktop/lib/local/engine.js). Same atomic-marker +
31
- * PID-liveness pattern as the session lock, so a crashed holder is reclaimed
32
- * with no staleness window to tune and no manual cleanup.
33
- *
34
- * This lock is advisory and cooperative: it cannot stop a writer that does
35
- * not take it (a plain `git` in a terminal, another harness, an editor). Its
36
- * value is that our own hosts stop racing each other, and that a caller
37
- * receiving `null` knows to back off instead of writing blind.
38
- *
39
- * The stronger fix is one worktree per agent (`git worktree add`), which
40
- * removes the shared mutable tree entirely. This lock is what makes the
41
- * single-tree case safe when that is not available.
42
- */
43
-
44
- const fs = require('node:fs');
45
- const path = require('node:path');
46
- const os = require('node:os');
47
- const crypto = require('node:crypto');
48
-
49
- /**
50
- * Where lock files live.
51
- *
52
- * The plugin's data dir is ~/.aegiscode ($AEGISCODE_HOME), per config.js's
53
- * `aegisDir()` convention — but the dev engine's ~/.aegiscodex is honoured
54
- * too, and either `*_WORKTREE_LOCK_DIR` override points every host at ONE
55
- * directory. That override is the interlock: two separate tools disagreeing
56
- * about where the lock file goes is the same as having no lock, so operators
57
- * running the GUI, the CLI and the dev engine against one checkout should set
58
- * AEGISCODE_WORKTREE_LOCK_DIR (or AEGISCODEX_WORKTREE_LOCK_DIR) to a shared
59
- * path. Within this repo the desktop and CLI hosts always agree, because both
60
- * resolve this same helper.
61
- */
62
- function worktreeLockDir() {
63
- if (process.env.AEGISCODE_WORKTREE_LOCK_DIR) return process.env.AEGISCODE_WORKTREE_LOCK_DIR;
64
- if (process.env.AEGISCODEX_WORKTREE_LOCK_DIR) return process.env.AEGISCODEX_WORKTREE_LOCK_DIR;
65
- const home = process.env.AEGISCODE_HOME
66
- || process.env.AEGISCODEX_HOME
67
- || path.join(os.homedir(), '.aegiscode');
68
- return path.join(home, 'worktree-locks');
69
- }
70
-
71
- /** Default wait: a live holder is bounded by its own turn watchdog, so
72
- * waiting slightly past that lets a legitimate queued turn run once the
73
- * holder's deadline fires, rather than failing right as the tree is about to
74
- * free up. */
75
- const DEFAULT_WORKTREE_WAIT_MS = 11 * 60 * 1000;
76
-
77
- /** Sentinel token for the "not in a repo" no-op path — always truthy, never
78
- * matches a real acquisition, so a stray release() can't release anything. */
79
- const NOOP_TOKEN = '(no-worktree)';
80
-
81
- /**
82
- * Canonicalise the root so two paths to one repo — a symlink, a trailing
83
- * slash, a relative path, `/tmp/../repo` — agree on a single lock file.
84
- * Falls back to the resolved path when realpath fails.
85
- */
86
- function canonical(root) {
87
- if (!root) return null;
88
- const abs = path.resolve(String(root));
89
- try { return fs.realpathSync(abs); } catch { return abs; }
90
- }
91
-
92
- /** The lock file for `root`. The path is hashed so an arbitrarily long or
93
- * awkward root maps to one flat, safe filename; the root itself is written
94
- * inside the file for humans reading the lock dir. */
95
- function worktreeLockPath(root) {
96
- if (!root) return null;
97
- const key = crypto.createHash('sha1').update(canonical(root)).digest('hex').slice(0, 16);
98
- return path.join(worktreeLockDir(), key + '.lock');
99
- }
100
-
101
- function pidAlive(pid) {
102
- try { process.kill(pid, 0); return true; } catch { return false; }
103
- }
104
-
105
- function readHolder(root) {
106
- try { return JSON.parse(fs.readFileSync(worktreeLockPath(root), 'utf8')); } catch { return null; }
107
- }
108
-
109
- /**
110
- * The current holder of `root`'s lock, or null. Never throws and never
111
- * clears a stale lock — use `isWorktreeLocked` for the reclaiming check.
112
- */
113
- function worktreeLockHolder(root) {
114
- if (!root) return null;
115
- const h = readHolder(root);
116
- if (!h) return null;
117
- return {
118
- pid: h.pid, token: h.token, since: h.since,
119
- root: h.root || canonical(root), session: h.session || null,
120
- };
121
- }
122
-
123
- /**
124
- * True if `root` is held by a live process (any process, including this one
125
- * — two concurrent callers inside one process must still queue behind each
126
- * other). A lock file whose PID is dead or unparsable is stale and cleared as
127
- * a side effect, so callers never special-case it.
128
- */
129
- function isWorktreeLocked(root) {
130
- if (!root) return false;
131
- const holder = readHolder(root);
132
- // `Number(null)` is 0 and `process.kill(0, 0)` targets the caller's process
133
- // GROUP, so it succeeds — a missing lock file would otherwise read as a live
134
- // holder forever. Require a real pid. (The session lock has the same latent
135
- // trap; it never surfaced there only because it is called after EEXIST.)
136
- const pid = Number(holder && holder.pid);
137
- if (!Number.isInteger(pid) || pid <= 0 || !pidAlive(pid)) {
138
- try { fs.unlinkSync(worktreeLockPath(root)); } catch {}
139
- return false;
140
- }
141
- return true;
142
- }
143
-
144
- /** Exclusive-create the lock file for `token`. Returns true on success.
145
- * Best-effort: a data-dir failure other than "already exists" fails OPEN so a
146
- * broken filesystem can never block a turn that would otherwise run fine. */
147
- function tryAcquire(root, token, meta) {
148
- try {
149
- fs.mkdirSync(worktreeLockDir(), { recursive: true });
150
- const fd = fs.openSync(worktreeLockPath(root), 'wx');
151
- fs.writeFileSync(fd, JSON.stringify({
152
- pid: process.pid,
153
- token,
154
- since: Date.now(),
155
- root: canonical(root),
156
- session: (meta && meta.session) || null,
157
- }));
158
- fs.closeSync(fd);
159
- return true;
160
- } catch (e) {
161
- if (e && e.code === 'EEXIST') return false;
162
- return true;
163
- }
164
- }
165
-
166
- /** Release `root`'s lock — only if `token` matches the current holder, so
167
- * releasing a lock you don't (or no longer) own can never clobber whoever
168
- * holds it now. No-op on a falsy root/token or any filesystem error
169
- * (best-effort, like the rest of this repo's persistence helpers). */
170
- function releaseWorktreeLock(root, token) {
171
- if (!root || !token || token === NOOP_TOKEN) return;
172
- const holder = readHolder(root);
173
- if (holder && holder.token === token) {
174
- try { fs.unlinkSync(worktreeLockPath(root)); } catch {}
175
- }
176
- }
177
-
178
- /**
179
- * Acquire `root`'s lock, waiting out a live holder instead of racing it.
180
- * Resolves a truthy opaque token once acquired — pass it back to
181
- * `releaseWorktreeLock` — or `null` if `waitMs` elapses or `signal` aborts
182
- * first; either way the caller must not write. A falsy `root` (not inside a
183
- * git work tree — nothing shared to collide over) resolves the sentinel
184
- * immediately with no file touched.
185
- */
186
- async function acquireWorktreeLock(root, {
187
- waitMs = DEFAULT_WORKTREE_WAIT_MS, pollMs = 200, signal, session = null,
188
- } = {}) {
189
- if (!root) return NOOP_TOKEN;
190
- const token = crypto.randomUUID();
191
- const deadline = Date.now() + waitMs;
192
- for (;;) {
193
- if (signal && signal.aborted) return null;
194
- if (tryAcquire(root, token, { session })) return token;
195
- // Was stale, just reclaimed by clearing — retry now.
196
- if (!isWorktreeLocked(root)) continue;
197
- if (Date.now() >= deadline) return null;
198
- await new Promise((r) => setTimeout(r, pollMs));
199
- }
200
- }
201
-
202
- /**
203
- * Run `fn` holding `root`'s lock, releasing it on every exit path. Throws
204
- * `WorktreeBusyError` (rather than running unprotected) if the lock can't be
205
- * taken, because proceeding without it is the bug this module exists to
206
- * prevent.
207
- */
208
- class WorktreeBusyError extends Error {
209
- constructor(root, holder) {
210
- super(`working tree ${root} is locked by pid ${holder ? holder.pid : '?'}`);
211
- this.name = 'WorktreeBusyError';
212
- this.root = root;
213
- this.holder = holder || null;
214
- }
215
- }
216
-
217
- async function withWorktreeLock(root, opts, fn) {
218
- const token = await acquireWorktreeLock(root, opts);
219
- if (!token) throw new WorktreeBusyError(root, worktreeLockHolder(root));
220
- try {
221
- return await fn();
222
- } finally {
223
- releaseWorktreeLock(root, token);
224
- }
225
- }
226
-
227
- module.exports = {
228
- DEFAULT_WORKTREE_WAIT_MS,
229
- worktreeLockDir,
230
- worktreeLockPath,
231
- worktreeLockHolder,
232
- isWorktreeLocked,
233
- releaseWorktreeLock,
234
- acquireWorktreeLock,
235
- WorktreeBusyError,
236
- withWorktreeLock,
237
- };
3
+ "use strict";const fs=require("node:fs"),path=require("node:path"),os=require("node:os"),crypto=require("node:crypto");function worktreeLockDir(){if(process.env.AEGISCODE_WORKTREE_LOCK_DIR)return process.env.AEGISCODE_WORKTREE_LOCK_DIR;if(process.env.AEGISCODEX_WORKTREE_LOCK_DIR)return process.env.AEGISCODEX_WORKTREE_LOCK_DIR;const r=process.env.AEGISCODE_HOME||process.env.AEGISCODEX_HOME||path.join(os.homedir(),".aegiscode");return path.join(r,"worktree-locks")}const DEFAULT_WORKTREE_WAIT_MS=66e4,NOOP_TOKEN="(no-worktree)";function canonical(r){if(!r)return null;const e=path.resolve(String(r));try{return fs.realpathSync(e)}catch{return e}}function worktreeLockPath(r){if(!r)return null;const e=crypto.createHash("sha1").update(canonical(r)).digest("hex").slice(0,16);return path.join(worktreeLockDir(),e+".lock")}function pidAlive(r){try{return process.kill(r,0),!0}catch{return!1}}function readHolder(r){try{return JSON.parse(fs.readFileSync(worktreeLockPath(r),"utf8"))}catch{return null}}function worktreeLockHolder(r){if(!r)return null;const e=readHolder(r);return e?{pid:e.pid,token:e.token,since:e.since,root:e.root||canonical(r),session:e.session||null}:null}function isWorktreeLocked(r){if(!r)return!1;const e=readHolder(r),o=Number(e&&e.pid);if(!Number.isInteger(o)||o<=0||!pidAlive(o)){try{fs.unlinkSync(worktreeLockPath(r))}catch{}return!1}return!0}function tryAcquire(r,e,o){try{fs.mkdirSync(worktreeLockDir(),{recursive:!0});const t=fs.openSync(worktreeLockPath(r),"wx");return fs.writeFileSync(t,JSON.stringify({pid:process.pid,token:e,since:Date.now(),root:canonical(r),session:o&&o.session||null})),fs.closeSync(t),!0}catch(r){return!r||"EEXIST"!==r.code}}function releaseWorktreeLock(r,e){if(!r||!e||e===NOOP_TOKEN)return;const o=readHolder(r);if(o&&o.token===e)try{fs.unlinkSync(worktreeLockPath(r))}catch{}}async function acquireWorktreeLock(r,{waitMs:e=DEFAULT_WORKTREE_WAIT_MS,pollMs:o=200,signal:t,session:n=null}={}){if(!r)return NOOP_TOKEN;const c=crypto.randomUUID(),i=Date.now()+e;for(;;){if(t&&t.aborted)return null;if(tryAcquire(r,c,{session:n}))return c;if(isWorktreeLocked(r)){if(Date.now()>=i)return null;await new Promise((r=>setTimeout(r,o)))}}}class WorktreeBusyError extends Error{constructor(r,e){super(`working tree ${r} is locked by pid ${e?e.pid:"?"}`),this.name="WorktreeBusyError",this.root=r,this.holder=e||null}}async function withWorktreeLock(r,e,o){const t=await acquireWorktreeLock(r,e);if(!t)throw new WorktreeBusyError(r,worktreeLockHolder(r));try{return await o()}finally{releaseWorktreeLock(r,t)}}module.exports={DEFAULT_WORKTREE_WAIT_MS:DEFAULT_WORKTREE_WAIT_MS,worktreeLockDir:worktreeLockDir,worktreeLockPath:worktreeLockPath,worktreeLockHolder:worktreeLockHolder,isWorktreeLocked:isWorktreeLocked,releaseWorktreeLock:releaseWorktreeLock,acquireWorktreeLock:acquireWorktreeLock,WorktreeBusyError:WorktreeBusyError,withWorktreeLock:withWorktreeLock};