@yaks/git 0.0.0-stage → 0.2.36

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 (183) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +754 -2
  3. package/_dist/address.d.ts +6 -0
  4. package/_dist/address.d.ts.map +1 -0
  5. package/_dist/checkout_vocab.d.ts +3 -0
  6. package/_dist/checkout_vocab.d.ts.map +1 -0
  7. package/_dist/cites.d.ts +48 -0
  8. package/_dist/cites.d.ts.map +1 -0
  9. package/_dist/commit.d.ts +36 -0
  10. package/_dist/commit.d.ts.map +1 -0
  11. package/_dist/comp.d.ts +29 -0
  12. package/_dist/comp.d.ts.map +1 -0
  13. package/_dist/door.d.ts +29 -0
  14. package/_dist/door.d.ts.map +1 -0
  15. package/_dist/format.d.ts +2 -0
  16. package/_dist/format.d.ts.map +1 -0
  17. package/_dist/hold.d.ts +6 -0
  18. package/_dist/hold.d.ts.map +1 -0
  19. package/_dist/host.d.ts +126 -0
  20. package/_dist/host.d.ts.map +1 -0
  21. package/_dist/http.d.ts +33 -0
  22. package/_dist/http.d.ts.map +1 -0
  23. package/_dist/import.d.ts +13 -0
  24. package/_dist/import.d.ts.map +1 -0
  25. package/_dist/index.d.ts +80 -0
  26. package/_dist/index.d.ts.map +1 -0
  27. package/_dist/land.d.ts +77 -0
  28. package/_dist/land.d.ts.map +1 -0
  29. package/_dist/mod.d.ts +73 -0
  30. package/_dist/mod.d.ts.map +1 -0
  31. package/_dist/objects.d.ts +40 -0
  32. package/_dist/objects.d.ts.map +1 -0
  33. package/_dist/oid.d.ts +24 -0
  34. package/_dist/oid.d.ts.map +1 -0
  35. package/_dist/pack-store.d.ts +9 -0
  36. package/_dist/pack-store.d.ts.map +1 -0
  37. package/_dist/pack.d.ts +26 -0
  38. package/_dist/pack.d.ts.map +1 -0
  39. package/_dist/pkt.d.ts +14 -0
  40. package/_dist/pkt.d.ts.map +1 -0
  41. package/_dist/placement.d.ts +4 -0
  42. package/_dist/placement.d.ts.map +1 -0
  43. package/_dist/plugin.d.ts +43 -0
  44. package/_dist/plugin.d.ts.map +1 -0
  45. package/_dist/project.d.ts +20 -0
  46. package/_dist/project.d.ts.map +1 -0
  47. package/_dist/push.d.ts +42 -0
  48. package/_dist/push.d.ts.map +1 -0
  49. package/_dist/refs.d.ts +81 -0
  50. package/_dist/refs.d.ts.map +1 -0
  51. package/_dist/rewrite.d.ts +35 -0
  52. package/_dist/rewrite.d.ts.map +1 -0
  53. package/_dist/service.d.ts +35 -0
  54. package/_dist/service.d.ts.map +1 -0
  55. package/_dist/sha1.d.ts +11 -0
  56. package/_dist/sha1.d.ts.map +1 -0
  57. package/_dist/source.d.ts +12 -0
  58. package/_dist/source.d.ts.map +1 -0
  59. package/_dist/tools.d.ts +11 -0
  60. package/_dist/tools.d.ts.map +1 -0
  61. package/_dist/tree.d.ts +28 -0
  62. package/_dist/tree.d.ts.map +1 -0
  63. package/_dist/unpack.d.ts +5 -0
  64. package/_dist/unpack.d.ts.map +1 -0
  65. package/_dist/vocab.d.ts +8 -0
  66. package/_dist/vocab.d.ts.map +1 -0
  67. package/_dist/workspace.d.ts +17 -0
  68. package/_dist/workspace.d.ts.map +1 -0
  69. package/address.js +9 -0
  70. package/address.js.map +1 -0
  71. package/address.ts +15 -0
  72. package/cache.js +9 -0
  73. package/cache.js.map +1 -0
  74. package/cache.ts +6 -0
  75. package/checkout.js +18 -0
  76. package/checkout.js.map +1 -0
  77. package/checkout.ts +24 -0
  78. package/checkout_vocab.js +14 -0
  79. package/checkout_vocab.js.map +1 -0
  80. package/checkout_vocab.ts +11 -0
  81. package/cites.js +189 -0
  82. package/cites.js.map +1 -0
  83. package/cites.ts +206 -0
  84. package/commit.js +128 -0
  85. package/commit.js.map +1 -0
  86. package/commit.ts +183 -0
  87. package/comp.js +94 -0
  88. package/comp.js.map +1 -0
  89. package/comp.ts +111 -0
  90. package/deno.json +1 -0
  91. package/door.js +88 -0
  92. package/door.js.map +1 -0
  93. package/door.ts +142 -0
  94. package/format.js +43 -0
  95. package/format.js.map +1 -0
  96. package/format.ts +49 -0
  97. package/hold.js +17 -0
  98. package/hold.js.map +1 -0
  99. package/hold.ts +21 -0
  100. package/host.js +1024 -0
  101. package/host.js.map +1 -0
  102. package/host.ts +1140 -0
  103. package/http.js +268 -0
  104. package/http.js.map +1 -0
  105. package/http.ts +329 -0
  106. package/import.js +141 -0
  107. package/import.js.map +1 -0
  108. package/import.ts +128 -0
  109. package/index.js +496 -0
  110. package/index.js.map +1 -0
  111. package/index.ts +634 -0
  112. package/land.js +788 -0
  113. package/land.js.map +1 -0
  114. package/land.ts +871 -0
  115. package/mod.js +73 -0
  116. package/mod.js.map +1 -0
  117. package/mod.ts +74 -0
  118. package/objects.js +331 -0
  119. package/objects.js.map +1 -0
  120. package/objects.ts +390 -0
  121. package/oid.js +50 -0
  122. package/oid.js.map +1 -0
  123. package/oid.ts +83 -0
  124. package/pack-index.js +103 -0
  125. package/pack-index.js.map +1 -0
  126. package/pack-index.ts +104 -0
  127. package/pack-store.js +216 -0
  128. package/pack-store.js.map +1 -0
  129. package/pack-store.ts +292 -0
  130. package/pack.js +121 -0
  131. package/pack.js.map +1 -0
  132. package/pack.ts +146 -0
  133. package/package.json +78 -4
  134. package/pkt.js +73 -0
  135. package/pkt.js.map +1 -0
  136. package/pkt.ts +93 -0
  137. package/placement.js +44 -0
  138. package/placement.js.map +1 -0
  139. package/placement.ts +38 -0
  140. package/plugin.js +66 -0
  141. package/plugin.js.map +1 -0
  142. package/plugin.ts +88 -0
  143. package/process-cwd.py +57 -0
  144. package/project.js +142 -0
  145. package/project.js.map +1 -0
  146. package/project.ts +151 -0
  147. package/push.js +401 -0
  148. package/push.js.map +1 -0
  149. package/push.ts +451 -0
  150. package/refs.js +176 -0
  151. package/refs.js.map +1 -0
  152. package/refs.ts +285 -0
  153. package/rewrite.js +176 -0
  154. package/rewrite.js.map +1 -0
  155. package/rewrite.ts +173 -0
  156. package/service.js +104 -0
  157. package/service.js.map +1 -0
  158. package/service.ts +194 -0
  159. package/sha1.js +115 -0
  160. package/sha1.js.map +1 -0
  161. package/sha1.ts +146 -0
  162. package/source.js +100 -0
  163. package/source.js.map +1 -0
  164. package/source.ts +116 -0
  165. package/spare.js +108 -0
  166. package/spare.js.map +1 -0
  167. package/spare.ts +94 -0
  168. package/tools.js +278 -0
  169. package/tools.js.map +1 -0
  170. package/tools.ts +310 -0
  171. package/tree.js +105 -0
  172. package/tree.js.map +1 -0
  173. package/tree.ts +132 -0
  174. package/unpack.js +207 -0
  175. package/unpack.js.map +1 -0
  176. package/unpack.ts +210 -0
  177. package/vocab.js +20 -0
  178. package/vocab.js.map +1 -0
  179. package/vocab.json +386 -0
  180. package/vocab.ts +22 -0
  181. package/workspace.js +449 -0
  182. package/workspace.js.map +1 -0
  183. package/workspace.ts +445 -0
package/workspace.ts ADDED
@@ -0,0 +1,445 @@
1
+ // Git acts through one explicitly connected filesystem. The transport may be
2
+ // local, SSH or a sandbox; no failed or unknown target chooses the host disk.
3
+ import type { Bundle, Comp, Graph } from 'jsr:@yaks/graph@0.2.36'
4
+ import { line } from '@yaks/query'
5
+ import {
6
+ absent,
7
+ and,
8
+ contains,
9
+ eq,
10
+ fields,
11
+ limit,
12
+ list,
13
+ present,
14
+ want,
15
+ } from '@yaks/query'
16
+ import { quote } from '@yaks/machine/connectors'
17
+ import type { ComputerTransport } from 'jsr:@yaks/machine@0.2.36'
18
+ import { filesystemEid } from './placement.js'
19
+ import {
20
+ type CheckoutRequest,
21
+ type Held,
22
+ repositoryEid,
23
+ worktreeEid,
24
+ } from './host.js'
25
+ import { refEid } from './comp.js'
26
+ import { cache } from './cache.js'
27
+
28
+ /** Resolve the stable filesystem, never a convenience directory. */
29
+ export let filesystem = async (
30
+ g: Graph,
31
+ workspace: string,
32
+ ): Promise<Bundle> => {
33
+ let [target] = await g.get([workspace])
34
+ let place = target?.workspace as Comp | undefined
35
+ if (!place || typeof place.root != 'string' || !place.root.startsWith('/')) {
36
+ throw Error('Git requires an explicit workspace with an absolute root')
37
+ }
38
+ if (target.sandbox) return target
39
+ if (place.sandbox) {
40
+ let [sandbox] = await g.get([String(place.sandbox)])
41
+ let enclosing = sandbox?.workspace as Comp | undefined
42
+ if (!sandbox?.sandbox || !enclosing || enclosing.machine != place.machine) {
43
+ throw Error('Workspace and enclosing sandbox placement disagree')
44
+ }
45
+ return sandbox
46
+ }
47
+ if (typeof place.machine != 'string') {
48
+ throw Error('Git computer placement is unknown')
49
+ }
50
+ let [computer] = await g.get([place.machine])
51
+ if (!computer?.machine) throw Error('Git workspace has no computer')
52
+ let eid = filesystemEid(place.machine)
53
+ let [root] = await g.get([eid])
54
+ if (!root) {
55
+ await g.apply([{
56
+ entity: { eid },
57
+ workspace: { machine: place.machine, root: '/' },
58
+ }])
59
+ ;[root] = await g.get([eid])
60
+ }
61
+ let canonical = root.workspace as Comp
62
+ if (
63
+ canonical.root != '/' || canonical.machine != place.machine ||
64
+ canonical.sandbox || root.sandbox
65
+ ) {
66
+ throw Error('Computer filesystem workspace has conflicting placement')
67
+ }
68
+ return root
69
+ }
70
+
71
+ let absolute = (path: string) => {
72
+ if (!path.startsWith('/') || path.includes('\0')) {
73
+ throw Error('Git paths must be absolute')
74
+ }
75
+ return path
76
+ }
77
+
78
+ /** Operations bound to one explicitly connected filesystem. */
79
+ export type WorkspaceGit = {
80
+ discover: (cwd?: string) => Promise<Bundle>
81
+ create: (request: CheckoutRequest, source?: string) => Promise<Bundle>
82
+ reclaim: (id: string) => Promise<Held | 'referenced' | undefined>
83
+ context: string
84
+ }
85
+
86
+ /** Git on a selected workspace. The caller connects this exact filesystem
87
+ * before binding the transport. All paths, realpath and locks run there. */
88
+ export let workspaceGit = async (
89
+ g: Graph,
90
+ workspace: string,
91
+ transport: ComputerTransport,
92
+ ): Promise<WorkspaceGit> => {
93
+ let [target] = await g.get([workspace])
94
+ let context = await filesystem(g, workspace)
95
+ let place = target.workspace as Comp
96
+ let root = absolute(String(place.root))
97
+ let exec = (script: string) => transport.execute('set -eu\n' + script)
98
+ let at = (cwd: string, args: string[]) =>
99
+ exec(`git -C ${quote(absolute(cwd))} ${args.map(quote).join(' ')}`)
100
+ let real = async (path: string, missing = false) =>
101
+ (await exec(
102
+ `realpath ${missing ? '-m' : '-e'} -- ${quote(absolute(path))}`,
103
+ )).trimEnd()
104
+ let same = async (bundles: Bundle[]) => {
105
+ let previous = new Map(
106
+ (await g.get(bundles.map((b) => b.entity.eid))).map((
107
+ b,
108
+ ) => [b.entity.eid, b]),
109
+ )
110
+ let changed = bundles.filter((b) =>
111
+ Object.entries(b).some(([name, value]) =>
112
+ name != 'entity' &&
113
+ Object.entries(value as Comp).some(([key, val]) =>
114
+ ((previous.get(b.entity.eid)?.[name] as Comp | undefined)?.[key] ??
115
+ null) !== (val ?? null)
116
+ )
117
+ )
118
+ )
119
+ if (changed.length) await g.apply(changed)
120
+ }
121
+ let discover = async (cwd = root): Promise<Bundle> => {
122
+ let dirs = (await at(cwd, [
123
+ 'rev-parse',
124
+ '--path-format=absolute',
125
+ '--show-toplevel',
126
+ '--git-common-dir',
127
+ '--absolute-git-dir',
128
+ ])).trimEnd().split('\n')
129
+ if (dirs.length != 3) {
130
+ throw Error('Git returned invalid checkout directories')
131
+ }
132
+ let [path, common, gitdir] = await Promise.all(dirs.map((p) => real(p)))
133
+ let repository = repositoryEid(common, context.entity.eid)
134
+ let eid = worktreeEid(repository, path)
135
+ let head = (await exec(
136
+ `git -C ${quote(cwd)} rev-parse --verify HEAD 2>/dev/null || :`,
137
+ )).trimEnd()
138
+ let branch = (await exec(`git -C ${quote(cwd)} symbolic-ref -q HEAD || :`))
139
+ .trimEnd()
140
+ let refs = await at(cwd, [
141
+ 'for-each-ref',
142
+ '--format=%(refname)%09%(objectname)%09%(symref)',
143
+ ])
144
+ let bundles: Bundle[] = [{
145
+ entity: { eid: repository },
146
+ repository: { workspace: context.entity.eid, common },
147
+ }]
148
+ let found = new Set<string>()
149
+ for (let line of refs.trimEnd().split('\n').filter(Boolean)) {
150
+ let [name, oid, symbolic] = line.split('\t')
151
+ let id = refEid(repository, name)
152
+ found.add(id)
153
+ bundles.push({
154
+ entity: { eid: id },
155
+ ref: {
156
+ app: repository,
157
+ name,
158
+ oid,
159
+ target: symbolic || null,
160
+ present: true,
161
+ },
162
+ })
163
+ }
164
+ if (branch && !found.has(refEid(repository, branch))) {
165
+ let id = refEid(repository, branch)
166
+ found.add(id)
167
+ bundles.push({
168
+ entity: { eid: id },
169
+ ref: {
170
+ app: repository,
171
+ name: branch,
172
+ oid: null,
173
+ target: null,
174
+ present: false,
175
+ },
176
+ })
177
+ }
178
+ for (let old of await g.read(line`.ref.app=${repository}`)) {
179
+ if (!found.has(old.entity.eid)) {
180
+ bundles.push({
181
+ entity: old.entity,
182
+ ref: { present: false, oid: null, target: null },
183
+ })
184
+ }
185
+ }
186
+ let [old] = await g.get([eid])
187
+ let tree: Bundle = {
188
+ entity: { eid },
189
+ workspace: {
190
+ machine: place.machine ?? null,
191
+ root: path,
192
+ sandbox: context.sandbox ? context.entity.eid : null,
193
+ },
194
+ worktree: {
195
+ repository,
196
+ gitdir,
197
+ head: head || null,
198
+ branch: branch ? refEid(repository, branch) : null,
199
+ managed: Boolean((old?.worktree as Comp | undefined)?.managed),
200
+ },
201
+ }
202
+ bundles.push(tree)
203
+ await same(bundles)
204
+ return (await g.get([eid]))[0]
205
+ }
206
+ let create = async (
207
+ request: CheckoutRequest,
208
+ source = root,
209
+ ): Promise<Bundle> => {
210
+ let home = await discover(source)
211
+ let repository = String((home.worktree as Comp).repository)
212
+ let [repo] = await g.get([repository])
213
+ let common = String((repo.repository as Comp).common)
214
+ let path = await real(request.path, true)
215
+ let eid = worktreeEid(repository, path)
216
+ let [old] = await g.get([eid])
217
+ let intent = old?.checkout as Comp | undefined
218
+ let base = (await at(source, [
219
+ 'rev-parse',
220
+ '--verify',
221
+ '--end-of-options',
222
+ (request.base ?? String(intent?.base ?? 'HEAD')) + '^{commit}',
223
+ ])).trimEnd()
224
+ if (
225
+ intent &&
226
+ (intent.base != base ||
227
+ (intent.branch ?? null) != (request.branch ?? null))
228
+ ) throw Error('Checkout request conflicts with pinned intent')
229
+ if (old?.worktree && !intent) {
230
+ throw Error('Existing checkout must be attached, not created again')
231
+ }
232
+ if (!intent) {
233
+ await exec(`test ! -e ${quote(path)}`)
234
+ await g.apply([{
235
+ entity: { eid },
236
+ checkout: {
237
+ repository,
238
+ path,
239
+ base,
240
+ requested: request.base ?? 'HEAD',
241
+ branch: request.branch ?? null,
242
+ state: 'preparing',
243
+ error: null,
244
+ },
245
+ $was: { checkout: { repository: null } },
246
+ }])
247
+ }
248
+ if (intent?.state == 'ready') {
249
+ let tree = await discover(path)
250
+ if ((tree.worktree as Comp).repository != repository) {
251
+ throw Error('Worktree repository mismatch')
252
+ }
253
+ return tree
254
+ }
255
+ try {
256
+ // The target's advisory lock spans existence check and Git mutation.
257
+ // A crash after Git succeeds is recovered only if HEAD and branch agree.
258
+ let expected = request.branch ? 'refs/heads/' + request.branch : ''
259
+ await exec(`mkdir -p -- ${quote(common + '/yaks-locks')}
260
+ exec 9>${quote(common + '/yaks-locks/' + eid)}
261
+ flock -x 9
262
+ if test -e ${quote(path)}; then
263
+ test "$(git -C ${quote(path)} rev-parse HEAD)" = ${quote(base)}
264
+ test "$(git -C ${quote(path)} symbolic-ref -q HEAD || :)" = ${quote(expected)}
265
+ test "$(realpath -e -- "$(git -C ${
266
+ quote(path)
267
+ } rev-parse --path-format=absolute --git-common-dir)")" = ${quote(common)}
268
+ else
269
+ git -C ${quote(source)} worktree add ${
270
+ request.branch ? '-b ' + quote(request.branch) : '--detach'
271
+ } ${quote(path)} ${quote(base)}
272
+ fi`)
273
+ let tree = await discover(path)
274
+ if ((tree.worktree as Comp).repository != repository) {
275
+ throw Error('Worktree repository mismatch')
276
+ }
277
+ await g.apply([{
278
+ entity: tree.entity,
279
+ worktree: { managed: true },
280
+ checkout: { state: 'ready', error: null },
281
+ }])
282
+ return (await g.get([tree.entity.eid]))[0]
283
+ } catch (error) {
284
+ await g.apply([{
285
+ entity: { eid },
286
+ checkout: { state: 'failed', error: String(error) },
287
+ }])
288
+ throw error
289
+ }
290
+ }
291
+ /** Only a positively managed checkout can be explicitly reclaimed. Unknown
292
+ * references or commands keep it; cleanup is never a consequence of home. */
293
+ let reclaim = async (
294
+ id: string,
295
+ ): Promise<Held | 'referenced' | undefined> => {
296
+ let [tree] = await g.get([id])
297
+ if (!tree?.worktree || !(tree.worktree as Comp).managed) {
298
+ throw Error('Git reclaims only explicitly managed worktrees')
299
+ }
300
+ let checkoutContext = await filesystem(g, id)
301
+ if (checkoutContext.entity.eid != context.entity.eid) {
302
+ throw Error('Worktree belongs to another filesystem')
303
+ }
304
+ if (await retained(g, id)) return 'referenced'
305
+ let path = absolute(String((tree.workspace as Comp).root))
306
+ let [repo] = await g.get([String((tree.worktree as Comp).repository)])
307
+ let common = String((repo.repository as Comp).common)
308
+ if ((tree.worktree as Comp).gitdir == common) {
309
+ throw Error('Primary checkout cannot be reclaimed')
310
+ }
311
+ // Dirty work is always kept, independent of process visibility.
312
+ if ((await at(path, ['status', '--porcelain'])).trim()) return 'dirty'
313
+ let ignored = await at(path, [
314
+ 'ls-files',
315
+ '--others',
316
+ '--ignored',
317
+ '--exclude-standard',
318
+ '--directory',
319
+ '-z',
320
+ ])
321
+ if (ignored.split('\0').some((file) => file && !cache(file))) return 'dirty'
322
+
323
+ // Same-user processes outside the graph keep their cwd and open files.
324
+ // An unreadable live process refuses collection rather than proving absence.
325
+ let used = await exec(`
326
+ uid=$(id -u)
327
+ for proc in /proc/[0-9]*; do
328
+ test -d "$proc" || continue
329
+ test "$(stat -c %u "$proc" 2>/dev/null || :)" = "$uid" || continue
330
+ for file in "$proc/cwd" "$proc"/fd/*; do
331
+ test -L "$file" || continue
332
+ link=$(readlink "$file") || { test ! -e "$proc" && continue; printf referenced; exit 0; }
333
+ case "$link" in ${quote(path)}|${
334
+ quote(path)
335
+ }/*) printf referenced; exit 0;; esac
336
+ done
337
+ done`)
338
+ if (used) return 'referenced'
339
+ // No orphan deletion or force. Detached/no-commit checkouts are kept.
340
+ let result = (await exec(`
341
+ if test -n "$(git -C ${quote(path)} status --porcelain)"; then echo dirty
342
+ elif test -z "$(git -C ${
343
+ quote(path)
344
+ } symbolic-ref -q HEAD || :)"; then echo unlanded
345
+ elif test -z "$(git -C ${
346
+ quote(path)
347
+ } for-each-ref --contains HEAD --format='%(refname)' refs/heads/ | grep -Fxv "$(git -C ${
348
+ quote(path)
349
+ } symbolic-ref HEAD)" || :)"; then echo unlanded
350
+ else
351
+ branch=$(git -C ${quote(path)} symbolic-ref --short HEAD)
352
+ git -C ${quote(path)} worktree remove ${quote(path)}
353
+ git --git-dir=${quote(common)} branch -D -- "$branch"
354
+ fi`)).trimEnd()
355
+ if (result == 'dirty' || result == 'unlanded') return result
356
+ // Keep the observation as restore history, never reconstruct it silently.
357
+ }
358
+ return { discover, create, reclaim, context: context.entity.eid }
359
+ }
360
+
361
+ let eids = (rows: Bundle[]) => rows.map((b) => b.entity.eid)
362
+
363
+ // Whether any of these sessions still holds its checkout: its transcript is
364
+ // not over, or its process still runs. A session's status is derived from its
365
+ // transcript (@yaks/session) and dear to read, so only the sessions whose home
366
+ // is in the checkout are asked.
367
+ let holding = async (g: Graph, sessions: string[]): Promise<boolean> => {
368
+ if (!sessions.length) return false
369
+ let comps = ['session', 'process', 'exit'].filter((c) => g.vocab.comp(c))
370
+ return (await g.get([...new Set(sessions)], comps)).some((b) =>
371
+ !['settled', 'stopped', 'failed'].includes(
372
+ String((b.session as Comp | undefined)?.status),
373
+ ) || Boolean(b.process && !b.exit)
374
+ )
375
+ }
376
+
377
+ /** Graph references that keep a checkout: a process that has not exited, or
378
+ * the home of a session that has not settled. A running process uses its
379
+ * launch workspace even after a session switches its default. Missing
380
+ * placement is not evidence that it runs elsewhere. */
381
+ export let retained = async (g: Graph, workspace: string): Promise<boolean> => {
382
+ let [target] = await g.get([workspace], ['workspace'])
383
+ let place = target?.workspace as Comp | undefined
384
+ if (!place || typeof place.root != 'string') return true
385
+ let root = place.root.replace(/\/+$/, '')
386
+ let inTree = (path: unknown) =>
387
+ typeof path == 'string' &&
388
+ (path == place.root || path.startsWith(root + '/'))
389
+ let beside = (b: Bundle) => {
390
+ let p = b.workspace as Comp | undefined
391
+ return p && p.machine == place.machine &&
392
+ (p.sandbox ?? (b.sandbox ? b.entity.eid : null)) ==
393
+ (place.sandbox ?? null)
394
+ }
395
+ let sandbox = g.vocab.comp('sandbox') ? ['sandbox'] : []
396
+ let contexts = await g.read(and(
397
+ present('workspace'),
398
+ ...root ? [contains('workspace.root', root)] : [],
399
+ ...sandbox.map(want),
400
+ ))
401
+ let ids = contexts.filter((b) =>
402
+ beside(b) && inTree((b.workspace as Comp).root)
403
+ ).map((b) => b.entity.eid)
404
+ for (let name of ['process', 'home']) {
405
+ if (!g.vocab.prop(name, 'workspace') || !g.vocab.prop(name, 'cwd')) {
406
+ continue
407
+ }
408
+ let rows = await g.read(and(
409
+ present(name),
410
+ ...name == 'process' ? [absent('exit')] : [],
411
+ ...root ? [contains(`${name}.cwd`, root)] : [],
412
+ fields('entity.eid', `${name}.workspace`, `${name}.cwd`),
413
+ ))
414
+ let refs = [...new Set(rows.map((b) => (b[name] as Comp).workspace))]
415
+ .filter((id): id is string => typeof id == 'string')
416
+ let local = new Set(
417
+ (await g.get(refs, ['workspace', ...sandbox]))
418
+ .filter(beside).map((b) => b.entity.eid),
419
+ )
420
+ let here = rows.filter((b) => {
421
+ let p = b[name] as Comp
422
+ return local.has(String(p.workspace)) && inTree(p.cwd)
423
+ })
424
+ if (name == 'process' ? here.length : await holding(g, eids(here))) {
425
+ return true
426
+ }
427
+ }
428
+ // Recheck direct attachments after the cwd checks: another process can
429
+ // attach while those reads yield. Never keep a liveness snapshot across passes.
430
+ for (let name of ['home', 'process']) {
431
+ if (!ids.length || !g.vocab.prop(name, 'workspace')) continue
432
+ let attached = await g.read(and(
433
+ present(name),
434
+ ...name == 'process' ? [absent('exit'), limit(1)] : [],
435
+ eq(`${name}.workspace`, list(...ids)),
436
+ fields('entity.eid'),
437
+ ))
438
+ if (
439
+ name == 'process' ? attached.length : await holding(g, eids(attached))
440
+ ) {
441
+ return true
442
+ }
443
+ }
444
+ return false
445
+ }