@gotcos/glasses-server 6.31.0 → 6.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2219,6 +2219,41 @@ unsaved capture, and makes batch status stop lying about finished work.
2219
2219
 
2220
2220
  # Changelog
2221
2221
 
2222
+ ## [6.32.0] - 2026-08-16
2223
+
2224
+ ### Continue now blocks on WORKING, not on merely open
2225
+
2226
+ Continuing a Claude Code thread from COS used to be refused whenever any foreign
2227
+ process held it. That came from `~/.claude/sessions/<pid>.json`, which records an
2228
+ OPEN WINDOW, not an agent generating: a session that finished ten minutes ago
2229
+ with the window still up looked identical to one mid-turn. Anyone who leaves
2230
+ Claude Code windows open was therefore refused on exactly the threads they work
2231
+ in, and allowed only on the ones they had abandoned.
2232
+
2233
+ - A foreign holder is now terminal only while it is **demonstrably writing**. A
2234
+ holder measured idle (registry record alive, transcript stale past the same
2235
+ 30s window `running_active` uses) is continuable.
2236
+ - New refusal reason **`native_thread_working`**, distinct from
2237
+ `live_desktop_process`, because the two ask for different things. Working
2238
+ clears itself in seconds and the copy says to wait; `live_desktop_process` now
2239
+ means COS could not measure the thread at all and says so rather than implying
2240
+ the thread is busy.
2241
+ - Off by default behind **`COS_THREAD_ATTACH_IDLE_HOLDER=1`**. Unset, no
2242
+ transcript clock is wired, every foreign holder reads unknown, and the gate
2243
+ behaves exactly as it did in 6.31.0. Composes with `COS_THREAD_ATTACH_ENABLED`.
2244
+
2245
+ Verified against a real interactive `claude` 2.1.229 held open and idle before
2246
+ the gate was touched. Writing into it delivers normally and never corrupts:
2247
+ across six injections and six desktop turns, including three writers landing
2248
+ within 45ms of each other, every transcript prefix stayed byte-identical, with
2249
+ no unparseable lines and no dangling parent references. What it DOES do is fork
2250
+ the conversation: the holder's in-memory view is stale, so its next turn branches
2251
+ off the pre-injection tail and the two writers stop seeing each other. Nothing is
2252
+ lost, and the divergence is caught one layer up by the head watermark, which
2253
+ changes on a desktop write and refuses the next COS turn with
2254
+ `native_thread_changed`. The full canary, including what it does NOT license, is
2255
+ recorded in `server/lib/thread-occupancy.ts` under THE IDLE-HOLDER RELAXATION.
2256
+
2222
2257
  ## [6.31.0] - 2026-08-16
2223
2258
 
2224
2259
  ### "Running" meant an open window, not a working agent
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gotcos/glasses-server",
3
- "version": "6.31.0",
3
+ "version": "6.32.0",
4
4
  "description": "COS Glasses \u2014 self-hosted AI heads-up-display server for Even G2 smart glasses, powered by Claude Code, Codex, or Cursor Agent CLI",
5
5
  "type": "module",
6
6
  "bin": {
package/server/index.ts CHANGED
@@ -23,7 +23,12 @@ import { claudeSessionsRouter } from './routes/claude-sessions.js'
23
23
  import { createAgentSessionBindingsRouter } from './routes/agent-session-bindings.js'
24
24
  import { AgentSessionBindingRegistry } from './lib/agent-session-binding-registry.js'
25
25
  import { cosSpawnedPids } from './lib/agent-session-ownership-store.js'
26
- import { realOccupancyDirs, realOccupancyProbes } from './lib/occupancy-probes.js'
26
+ import {
27
+ idleHolderContinueEnabled,
28
+ realOccupancyDirs,
29
+ realOccupancyProbes,
30
+ withTranscriptClock,
31
+ } from './lib/occupancy-probes.js'
27
32
  import { realAttachedWorkspaceDeps, resolveAttachedWorkspace } from './lib/attached-workspace.js'
28
33
  import { deliverAttachedTurn, realAttachedTurnDeps } from './lib/attached-provider-adapter.js'
29
34
  import { forkThread, realForkDeps } from './lib/fork-thread.js'
@@ -301,10 +306,28 @@ bindingReapTimer.unref()
301
306
 
302
307
  // Built once. Each of these reads the disk, so sharing them keeps an attach from
303
308
  // re-deriving roots per request.
304
- const occupancyProbes = realOccupancyProbes(cosSpawnedPids)
305
309
  const occupancyDirs = realOccupancyDirs()
306
310
  const nativeHeadDeps = realNativeHeadDeps()
307
311
  const attachedWorkspaceDeps = realAttachedWorkspaceDeps(nativeHeadDeps)
312
+ /**
313
+ * THE ONE PLACE the idle-holder relaxation is switched on (6.32.0).
314
+ *
315
+ * Without the clock, a foreign holder is terminal and Continue is refused for
316
+ * any thread with a Claude Code window open on it — including the ones Miles
317
+ * actually works in, because he leaves those windows open. With it, a holder
318
+ * measured idle is continuable and only a holder measured WRITING is refused.
319
+ *
320
+ * Both gates read this same object — the route's detector via `probes` below,
321
+ * and the adapter's pre-spawn preflight via `occupancyProbes` at the closure
322
+ * further down — so they cannot come to different conclusions. That is the whole
323
+ * reason the clock is a probe rather than a check bolted onto each call site.
324
+ *
325
+ * Read the canary evidence in `thread-occupancy.ts` under THE IDLE-HOLDER
326
+ * RELAXATION before changing this line.
327
+ */
328
+ const occupancyProbes = idleHolderContinueEnabled()
329
+ ? withTranscriptClock(realOccupancyProbes(cosSpawnedPids), nativeHeadDeps)
330
+ : realOccupancyProbes(cosSpawnedPids)
308
331
 
309
332
  /**
310
333
  * The shim between the route's request shape and the adapter's.
@@ -49,6 +49,7 @@ import {
49
49
  import { homedir } from 'node:os'
50
50
  import { basename, join, resolve } from 'node:path'
51
51
  import { NATIVE_THREAD_ID_RE } from './native-thread-id.js'
52
+ import { transcriptPathFor, type NativeHeadDeps } from './native-head.js'
52
53
  import { parseProcStartUtcMs, type OccupancyDirs, type OccupancyProbes } from './thread-occupancy.js'
53
54
 
54
55
  // Re-exported rather than reimplemented. `claudeSessionsDir` already encodes the
@@ -440,5 +441,61 @@ export function realOccupancyProbes(ledger: SpawnLedgerAccessor): OccupancyProbe
440
441
  readFile,
441
442
  lockHolders,
442
443
  cosSpawnedPids: () => sanitizeLedger(ledger()),
444
+ // transcriptMtimeMs is DELIBERATELY absent here. See `withTranscriptClock`.
445
+ }
446
+ }
447
+
448
+ /**
449
+ * Is the idle-holder relaxation switched on for this install?
450
+ *
451
+ * ONE READER, exported so every other module imports it rather than re-reading
452
+ * `process.env` — the same anti-drift rule `threadAttachEnabled` follows, and for
453
+ * the same reason: a second copy is how a surface comes to advertise a write path
454
+ * the gate will refuse.
455
+ *
456
+ * `=== '1'` opt-IN, not `!== '0'` opt-out. This decides whether COS may write
457
+ * into a conversation a human still has open, so anything ambiguous — unset,
458
+ * empty, 'true', 'yes' — reads as OFF. It composes with
459
+ * `COS_THREAD_ATTACH_ENABLED`: with attach itself off, this flag does nothing at
460
+ * all, because there is no write path to relax.
461
+ */
462
+ export function idleHolderContinueEnabled(): boolean {
463
+ return process.env.COS_THREAD_ATTACH_IDLE_HOLDER === '1'
464
+ }
465
+
466
+ /**
467
+ * Add a transcript clock to a probe set, so an idle foreign holder can be told
468
+ * apart from a working one.
469
+ *
470
+ * WHY THIS IS A SEPARATE WRAPPER rather than a member of `realOccupancyProbes`:
471
+ * the strict gate has to be what you get by DEFAULT. A probe set built the
472
+ * ordinary way has no clock, every foreign holder reads `unknown`, and
473
+ * `threadOccupancy` refuses exactly as it did before 6.32.0. Turning the
474
+ * relaxation on is then one visible call at one wiring site, and turning it off
475
+ * is deleting that call — no rollback, no second code path to keep in step.
476
+ *
477
+ * Null on every failure: an unresolvable id, a missing file, a stat that threw.
478
+ * `holderActivity` reads null as `unknown`, which refuses. There is deliberately
479
+ * no path from "could not read the transcript" to "the holder is idle".
480
+ *
481
+ * `statSync` and not `stat`: `OccupancyProbes` is synchronous throughout, and the
482
+ * adapter's pre-spawn preflight REFUSES a thenable rather than awaiting one —
483
+ * awaiting there would reopen the very race the check exists to close.
484
+ */
485
+ export function withTranscriptClock(
486
+ probes: OccupancyProbes,
487
+ headDeps: NativeHeadDeps,
488
+ ): OccupancyProbes {
489
+ return {
490
+ ...probes,
491
+ transcriptMtimeMs: (provider, threadId) => {
492
+ try {
493
+ const path = transcriptPathFor(provider, threadId, headDeps)
494
+ if (path === null) return null
495
+ return statSync(path).mtimeMs
496
+ } catch {
497
+ return null
498
+ }
499
+ },
443
500
  }
444
501
  }
@@ -32,6 +32,7 @@ import {
32
32
  claudeOwners,
33
33
  codexLockPath,
34
34
  codexOwners,
35
+ isActiveRecently,
35
36
  type OccupancyDirs,
36
37
  type OccupancyProbes,
37
38
  type ThreadOwner,
@@ -210,49 +211,19 @@ export function noOccupancyKnown(): OccupiedScan {
210
211
  }
211
212
 
212
213
  /**
213
- * How recently the transcript must have been written for a held thread to read
214
- * as WORKING rather than merely OPEN.
214
+ * The freshness window and the raw reading now live in `thread-occupancy.ts`.
215
215
  *
216
- * WHY A SECOND SIGNAL EXISTS AT ALL. `owners` answers "a process holds this
217
- * thread", which is not the question the user is asking when he looks at the
218
- * lens. He watched a session finish on his Mac while the glasses still showed it
219
- * active, because the window was still open and the registry record therefore
220
- * still existed. Occupancy has no clock in it; the transcript does.
216
+ * MOVED, not duplicated (6.32.0). The write gate needs the same window this
217
+ * hint uses, and it is the lower module, so keeping a second copy here would be
218
+ * two definitions of "recently" that could drift. Re-exported so every existing
219
+ * importer of these two names is unaffected.
221
220
  *
222
- * MEASURED, not assumed: while a session generates, its jsonl mtime tracks the
223
- * wall clock to within a second, and when generation stops the mtime goes stale
224
- * while the registry record stays exactly where it was. That divergence is the
225
- * whole signal.
226
- *
227
- * 30 seconds is deliberately far wider than the observed sub-second write
228
- * cadence. The gap this has to survive is a long tool call or a slow first token
229
- * between writes, and a window sized to the cadence would flap a working session
230
- * to OPEN and back every time the model paused to think. The cost of the wide
231
- * window is bounded and known: a session that stops is reported working for up
232
- * to 30 more seconds, which is a late correction rather than a permanent lie.
221
+ * Note the polarity difference before reusing `isActiveRecently` anywhere new:
222
+ * for a HINT, its `false` safely means "render OPEN". For a GATE, that same
223
+ * `false` would mean "allow the write", and unmeasurable must not mean allowed.
224
+ * `holderActivity` in `thread-occupancy.ts` is the gate-side reading.
233
225
  */
234
- export const ACTIVE_RECENTLY_WINDOW_MS = 30_000
235
-
236
- /**
237
- * Was this transcript written inside the window?
238
- *
239
- * NULL IS NOT "RECENT". An unresolvable path, an unreadable file, a stat that
240
- * threw — every one of them arrives here as null and answers false, so the row
241
- * falls back to OPEN. That is not a fail-open: OPEN is still a true statement
242
- * about a thread with a live owner. It is the STRONGER claim, "an agent is
243
- * working in here", that has to be earned by an actual observation.
244
- *
245
- * A timestamp far in the FUTURE is refused for the same reason rather than
246
- * treated as maximally fresh. Skew of a few seconds is normal and lands inside
247
- * the window; a file dated next week is a clock this server cannot reason about,
248
- * and letting it manufacture a permanent "working" badge would rebuild the bug
249
- * from the other direction.
250
- */
251
- export function isActiveRecently(mtimeMs: number | null | undefined, nowMs: number): boolean {
252
- if (typeof mtimeMs !== 'number' || !Number.isFinite(mtimeMs)) return false
253
- if (!Number.isFinite(nowMs)) return false
254
- return Math.abs(nowMs - mtimeMs) <= ACTIVE_RECENTLY_WINDOW_MS
255
- }
226
+ export { ACTIVE_RECENTLY_WINDOW_MS, isActiveRecently } from './thread-occupancy.js'
256
227
 
257
228
  /**
258
229
  * Fill in `activeRecently` for a scan, from transcript write times.
@@ -56,6 +56,12 @@ export interface ThreadOwner {
56
56
 
57
57
  export type OccupancyReason =
58
58
  | 'live_desktop_process'
59
+ // A foreign holder that is DEMONSTRABLY generating right now, as opposed to
60
+ // merely holding the thread open. Separate from `live_desktop_process` because
61
+ // the two ask the user for different things: this one clears by itself in
62
+ // seconds and is worth waiting out, the other needs a window closed. Only
63
+ // reachable when a transcript clock is wired — see THE IDLE-HOLDER RELAXATION below.
64
+ | 'native_thread_working'
59
65
  | 'unsupported_provider'
60
66
  | 'invalid_thread_id'
61
67
  | 'detector_unavailable'
@@ -73,13 +79,30 @@ export interface Occupancy {
73
79
  owners: ThreadOwner[]
74
80
  /** Null only when attachable. Drives the Control/lens footer copy. */
75
81
  reason: OccupancyReason | null
82
+ /**
83
+ * This verdict is attachable DESPITE a foreign owner, because that owner was
84
+ * measured idle. See THE IDLE-HOLDER RELAXATION below.
85
+ *
86
+ * It exists so the permissive outcome has to be DECLARED rather than inferred
87
+ * from `attachable` alone. `projectAttachability` treats a foreign owner on an
88
+ * attachable verdict as a contradiction and forces it back to refused; without
89
+ * a positive marker, relaxing the gate would mean deleting that check, and the
90
+ * check is what catches a future detector that flips `attachable` by accident.
91
+ * With the marker, the only way to reach the permissive path is to say so.
92
+ *
93
+ * Absent (not false) on every other verdict, so `=== true` is the only test.
94
+ */
95
+ idleHolder?: true
76
96
  }
77
97
 
78
98
  /**
79
99
  * What a scan could not establish. Any non-null value forbids attaching, even
80
100
  * with zero owners — that is the whole point of the type.
81
101
  */
82
- type Doubt = Exclude<OccupancyReason, 'live_desktop_process' | 'unsupported_provider' | 'invalid_thread_id'> | null
102
+ type Doubt = Exclude<
103
+ OccupancyReason,
104
+ 'live_desktop_process' | 'native_thread_working' | 'unsupported_provider' | 'invalid_thread_id'
105
+ > | null
83
106
 
84
107
  interface ScanResult {
85
108
  owners: ThreadOwner[]
@@ -111,6 +134,127 @@ export interface OccupancyProbes {
111
134
  * membership — the start time is what makes the claim checkable.
112
135
  */
113
136
  cosSpawnedPids: () => ReadonlyMap<number, number>
137
+ /**
138
+ * Transcript write time for this thread, epoch ms, or null when it cannot be
139
+ * read. OPTIONAL, and its ABSENCE is the default.
140
+ *
141
+ * Absent means this install has no clock on the thread, which means every
142
+ * foreign holder stays `live_desktop_process` — byte-for-byte the behaviour
143
+ * that shipped before the idle-holder relaxation existed. That is why it is
144
+ * optional rather than required: the strict gate is what you get by doing
145
+ * nothing, and the relaxation has to be wired on purpose (see `server/index.ts`,
146
+ * where the wiring is itself behind `COS_THREAD_ATTACH_IDLE_HOLDER`).
147
+ *
148
+ * Null is NOT idle. See `holderActivity` for why that distinction is the whole
149
+ * safety property here.
150
+ */
151
+ transcriptMtimeMs?: (provider: OccupancyProvider, threadId: string) => number | null
152
+ }
153
+
154
+ /**
155
+ * How recently the transcript must have been written for a held thread to read
156
+ * as WORKING rather than merely OPEN.
157
+ *
158
+ * WHY A SECOND SIGNAL EXISTS AT ALL. `owners` answers "a process holds this
159
+ * thread", which is not the question the user is asking. He watched a session
160
+ * finish on his Mac while the glasses still showed it active, because the window
161
+ * was still open and the registry record therefore still existed. Occupancy has
162
+ * no clock in it; the transcript does.
163
+ *
164
+ * MEASURED, not assumed: while a session generates, its jsonl mtime tracks the
165
+ * wall clock to within a second, and when generation stops the mtime goes stale
166
+ * while the registry record stays exactly where it was. That divergence is the
167
+ * whole signal.
168
+ *
169
+ * 30 seconds is deliberately far wider than the observed sub-second write
170
+ * cadence. The gap this has to survive is a long tool call or a slow first token
171
+ * between writes, and a window sized to the cadence would flap a working session
172
+ * to OPEN and back every time the model paused to think. The cost of the wide
173
+ * window is bounded and known: a session that stops is reported working for up
174
+ * to 30 more seconds, which is a late correction rather than a permanent lie.
175
+ *
176
+ * LIVES HERE, not in `occupied-threads.ts`, since 6.32.0. The display hint and
177
+ * the write gate must agree on what "recently" means, and the gate is the lower
178
+ * module of the two. `occupied-threads.ts` re-exports it, so every existing
179
+ * importer is unaffected.
180
+ */
181
+ export const ACTIVE_RECENTLY_WINDOW_MS = 30_000
182
+
183
+ /**
184
+ * Was this transcript written inside the window?
185
+ *
186
+ * NULL IS NOT "RECENT". An unresolvable path, an unreadable file, a stat that
187
+ * threw — every one of them arrives here as null and answers false, so the row
188
+ * falls back to OPEN. That is not a fail-open FOR A DISPLAY HINT: OPEN is still
189
+ * a true statement about a thread with a live owner. It is the STRONGER claim,
190
+ * "an agent is working in here", that has to be earned by an actual observation.
191
+ *
192
+ * A timestamp far in the FUTURE is refused for the same reason rather than
193
+ * treated as maximally fresh. Skew of a few seconds is normal and lands inside
194
+ * the window; a file dated next week is a clock this server cannot reason about,
195
+ * and letting it manufacture a permanent "working" badge would rebuild the bug
196
+ * from the other direction.
197
+ *
198
+ * DO NOT CALL THIS FROM A WRITE GATE. Its false has two meanings — "measured,
199
+ * and stale" and "could not measure" — and a gate that treats the second as the
200
+ * first is fail-open. `holderActivity` is the gate-side reading.
201
+ */
202
+ export function isActiveRecently(mtimeMs: number | null | undefined, nowMs: number): boolean {
203
+ if (typeof mtimeMs !== 'number' || !Number.isFinite(mtimeMs)) return false
204
+ if (!Number.isFinite(nowMs)) return false
205
+ return Math.abs(nowMs - mtimeMs) <= ACTIVE_RECENTLY_WINDOW_MS
206
+ }
207
+
208
+ /**
209
+ * What a foreign holder is doing, for a caller that must DECIDE rather than
210
+ * render.
211
+ *
212
+ * THREE VALUES, AND THE THIRD IS THE POINT. `isActiveRecently` collapses "I
213
+ * measured a stale file" and "I could not measure anything" into the same
214
+ * `false`, which is correct for a badge — the weaker claim, OPEN, is true either
215
+ * way — and catastrophic for a gate, where that same `false` would mean ALLOW
216
+ * THE WRITE. Same reading, opposite polarity, so the gate gets its own function
217
+ * instead of reusing the hint's boolean.
218
+ *
219
+ * Only `idle` is a positive observation of an idle holder: a real number, not in
220
+ * the future, measured outside the window. Everything else is `unknown` and
221
+ * refuses. This is the same "no owner found is not no owner" rule the rest of
222
+ * this module runs on, applied to the clock instead of the registry.
223
+ *
224
+ * The window itself is NOT redefined here — `isActiveRecently` decides what
225
+ * recent means, so the badge and the gate can never drift apart on it.
226
+ */
227
+ export type HolderActivity = 'working' | 'idle' | 'unknown'
228
+
229
+ export function holderActivity(mtimeMs: number | null | undefined, nowMs: number): HolderActivity {
230
+ if (typeof mtimeMs !== 'number' || !Number.isFinite(mtimeMs)) return 'unknown'
231
+ if (!Number.isFinite(nowMs)) return 'unknown'
232
+ // A future-dated transcript is a clock this server cannot reason about. The
233
+ // hint may treat a few seconds of skew as freshness; the gate may not treat
234
+ // ANY amount of it as idleness, because "stale" is the permissive answer here
235
+ // and a wrong clock would hand it out forever.
236
+ if (mtimeMs > nowMs + ACTIVE_RECENTLY_WINDOW_MS) return 'unknown'
237
+ return isActiveRecently(mtimeMs, nowMs) ? 'working' : 'idle'
238
+ }
239
+
240
+ /**
241
+ * Read the transcript clock for a thread, refusing to guess.
242
+ *
243
+ * A probe that is absent, throws, or answers null all land on `unknown`, which
244
+ * refuses. There is no path from a failed reading to a permissive verdict.
245
+ */
246
+ function readHolderActivity(
247
+ provider: OccupancyProvider,
248
+ threadId: string,
249
+ probes: OccupancyProbes,
250
+ nowMs: number,
251
+ ): HolderActivity {
252
+ if (typeof probes.transcriptMtimeMs !== 'function') return 'unknown'
253
+ try {
254
+ return holderActivity(probes.transcriptMtimeMs(provider, threadId), nowMs)
255
+ } catch {
256
+ return 'unknown'
257
+ }
114
258
  }
115
259
 
116
260
  /** A Claude registry filename is exactly `<pid>.json`. Not `*.json`. */
@@ -321,14 +465,81 @@ export interface OccupancyDirs {
321
465
  codexLocksDir: string
322
466
  }
323
467
 
468
+ // ===========================================================================
469
+ // THE IDLE-HOLDER RELAXATION (6.32.0)
470
+ // ===========================================================================
471
+ //
472
+ // This loosens a guard that was deliberate. Read this before touching it, and
473
+ // do not widen it further without repeating the experiment.
474
+ //
475
+ // WHAT CHANGED. A foreign owner used to be terminal on its own. It is now
476
+ // terminal only while that owner is DEMONSTRABLY WRITING. A holder measured
477
+ // idle — registry record alive, transcript stale — is continuable.
478
+ //
479
+ // WHY THE OLD RULE WAS WRONG. The registry records an OPEN WINDOW, not active
480
+ // generation. `~/.claude/sessions/<pid>.json` for a session that finished ten
481
+ // minutes ago is byte-identical to one mid-turn. Miles keeps Claude Code
482
+ // windows open, so Continue was refused for exactly the threads he cares about
483
+ // and allowed only for the ones he had abandoned. The gate was measuring the
484
+ // wrong thing, not measuring it too strictly.
485
+ //
486
+ // THE CANARY, run 2026-08-16 against a real interactive `claude` 2.1.229 held
487
+ // open in tmux (pid 48446, `kind: interactive`, `entrypoint: claude-desktop`,
488
+ // indistinguishable from a desktop window) in a scratch workspace. Turns were
489
+ // injected with the EXACT argv this server spawns. Raw findings:
490
+ //
491
+ // 1. DELIVERY WORKS. `claude -p --resume <id>` into an idle-held thread
492
+ // exits 0 in ~5s and returns its result. Same session id, same file.
493
+ // 2. NO CORRUPTION. Across 6 injections and 6 desktop turns, including
494
+ // three SDK writers landing within 45ms of each other on the same node,
495
+ // the transcript stayed append-only: every prefix byte-identical before
496
+ // and after (`cmp`), zero unparseable lines, zero dangling parentUuid.
497
+ // Claude Code appends whole rows, so byte interleaving does not occur.
498
+ // 3. THE HOLDER IGNORES THE INJECTED TURN — this is the real finding. Its
499
+ // in-memory view is stale and stays stale. Asked afterwards to list every
500
+ // word it had been told to reply with, it answered "ALPHA": the injected
501
+ // "BRAVO" was invisible to it.
502
+ // 4. NOTHING IS CLOBBERED, BUT THE THREAD FORKS. The holder's next turn
503
+ // parented to the PRE-INJECTION tail, making the transcript a tree. Both
504
+ // turns survive in full; they are on different branches, and from then on
505
+ // each writer sees only its own. A later `--resume` read back
506
+ // "ALPHA, BRAVO" and never saw the desktop's "CHARLIE".
507
+ // 5. THE WORKING CASE IS DIFFERENT AND STAYS BLOCKED. With the holder
508
+ // generating, the transcript mtime was 6.2s old and `holderActivity`
509
+ // returned `working`, so this relaxation does not fire there at all.
510
+ //
511
+ // SO THE ANSWER IS: no clobbering, no corruption, no data loss — but the two
512
+ // views diverge silently after the write, and neither side is told by THIS
513
+ // module. What makes that acceptable is the guard one layer up: `nativeHead`
514
+ // digests the transcript tail, and a desktop write CHANGES it (measured:
515
+ // nh1:faba1b35... -> nh1:ace93a1f...), so the next COS turn on that binding is
516
+ // refused with `native_thread_changed` and the user is offered refresh, continue
517
+ // anyway, or fork. Divergence is caught by the CONTENT watermark, which is the
518
+ // signal that can actually see it. Occupancy never could.
519
+ //
520
+ // WHAT THIS IS NOT A LICENCE FOR. Do not extend the same reasoning to the
521
+ // `doubt` reasons below: those mean the scan could not SEE, and an unreadable
522
+ // registry is not an idle holder. Do not relax the `working` branch on the
523
+ // grounds that "nothing got corrupted in the canary either" — case 5 was never
524
+ // run to completion precisely because it stays blocked. And do not reach for
525
+ // `isActiveRecently` here; its `false` means "stale OR unmeasurable", and only
526
+ // `holderActivity` separates those.
527
+ //
528
+ // TURNING IT OFF. Unwire `probes.transcriptMtimeMs` (in practice: unset
529
+ // `COS_THREAD_ATTACH_IDLE_HOLDER`). Every foreign holder then reads `unknown`
530
+ // and refuses, which is the pre-6.32.0 gate exactly. No rollback needed.
531
+
324
532
  /**
325
533
  * The Phase 0 attach precondition.
326
534
  *
327
535
  * Returns attachable ONLY when a supported provider proved its detector exists,
328
- * read every candidate record, and found no foreign owner. Every other outcome
329
- * names why. The whole function is wrapped: a throwing probe — including the
330
- * spawn ledger, which is the most safety-critical of them — is `probe_failed`,
536
+ * read every candidate record, and found no owner it must respect. Every other
537
+ * outcome names why. The whole function is wrapped: a throwing probe — including
538
+ * the spawn ledger, which is the most safety-critical of them — is `probe_failed`,
331
539
  * never an exception escaping into a route.
540
+ *
541
+ * A foreign owner measured IDLE is the one exception, and it is marked
542
+ * `idleHolder` on the way out. See THE IDLE-HOLDER RELAXATION above.
332
543
  */
333
544
  export function threadOccupancy(
334
545
  provider: string,
@@ -358,10 +569,23 @@ export function threadOccupancy(
358
569
 
359
570
  const foreign = result.owners.filter(o => !o.selfOwned)
360
571
  if (foreign.length > 0) {
361
- return { attachable: false, owners: result.owners, reason: 'live_desktop_process' }
572
+ const activity = readHolderActivity(provider, threadId, probes, Date.now())
573
+ if (activity !== 'idle') {
574
+ return {
575
+ attachable: false,
576
+ owners: result.owners,
577
+ reason: activity === 'working' ? 'native_thread_working' : 'live_desktop_process',
578
+ }
579
+ }
580
+ // FALL THROUGH, deliberately. See THE IDLE-HOLDER RELAXATION below for why this is
581
+ // safe and what it is NOT. Everything after this point still applies: a scan
582
+ // that could not establish something still refuses on the next line.
362
583
  }
363
584
  if (result.doubt !== null) {
364
585
  return { attachable: false, owners: result.owners, reason: result.doubt }
365
586
  }
587
+ if (foreign.length > 0) {
588
+ return { attachable: true, owners: result.owners, reason: null, idleHolder: true }
589
+ }
366
590
  return { attachable: true, owners: result.owners, reason: null }
367
591
  }
@@ -391,8 +391,17 @@ export const REASON_COPY: Record<OccupancyReason, string> = {
391
391
  // supported permanent configuration, not a degraded one (plan 4.9).
392
392
  attach_disabled:
393
393
  'Continuing a thread on your Mac is turned off. COS is read-only here. Fork it instead.',
394
+ // Held open by another app AND COS has no clock on the thread, so it cannot
395
+ // tell working from idle. Since 6.32.0 an idle holder is continuable, which
396
+ // makes this the "could not measure" case rather than the "someone else is
397
+ // here" case, and the copy says which.
394
398
  live_desktop_process:
395
- 'Open on your Mac. COS will not write into a thread another app is holding. Fork it instead.',
399
+ 'Open on your Mac, and COS cannot tell whether it is still working. It will not write into it. Fork it instead.',
400
+ // Measured, and the answer was yes. Deliberately a different instruction from
401
+ // the line above: this clears on its own within seconds, so the useful advice
402
+ // is to wait, with fork as the fallback rather than the recommendation.
403
+ native_thread_working:
404
+ 'Your Mac is writing to this thread right now. Wait a few seconds and try again, or fork it.',
396
405
  unsupported_provider:
397
406
  'This assistant cannot be continued from COS yet. Fork it instead.',
398
407
  invalid_thread_id:
@@ -672,14 +681,26 @@ export interface AttachabilityBody {
672
681
  * permissive way if simply forwarded: attachable with a reason, attachable with an
673
682
  * owner that is not provably ours, and a non-array owners field. Any of them is a
674
683
  * defect upstream, and a defect must not resolve to permissive.
684
+ *
685
+ * THE ONE EXEMPTION, added with the idle-holder relaxation in 6.32.0: a foreign
686
+ * owner is allowed on an attachable verdict when the verdict itself carries
687
+ * `idleHolder === true`. The check is not weakened by this, it is made explicit —
688
+ * before, "attachable" and "no foreign owner" were the same claim, so a detector
689
+ * that flipped `attachable` by mistake was caught here. It still is. What can no
690
+ * longer be caught here is a detector that ALSO sets `idleHolder`, which takes a
691
+ * deliberate edit in `thread-occupancy.ts` rather than an accident, and which the
692
+ * mutation tests over that file cover.
675
693
  */
676
694
  export function projectAttachability(verdict: Occupancy): AttachabilityBody {
677
695
  const owners = Array.isArray(verdict?.owners) ? verdict.owners : null
696
+ // Strictly `=== true`. An idleHolder of 1, 'yes', or {} is a malformed verdict,
697
+ // and a malformed verdict must not buy an exemption.
698
+ const foreignOwnerDeclared = verdict?.idleHolder === true
678
699
  const sound =
679
700
  verdict?.attachable === true &&
680
701
  verdict.reason === null &&
681
702
  owners !== null &&
682
- owners.every(owner => owner?.selfOwned === true)
703
+ owners.every(owner => owner?.selfOwned === true || foreignOwnerDeclared)
683
704
  const reason: OccupancyReason | null = sound ? null : ((verdict?.reason ?? 'probe_failed') as OccupancyReason)
684
705
  return {
685
706
  attachable: sound,