centralu 0.1.0-beta.56 → 0.1.0-beta.57

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.
@@ -7,7 +7,7 @@ import { existsSync, openSync, rmSync, writeFileSync, writeSync } from 'node:fs'
7
7
  import { userInfo } from 'node:os'
8
8
  import { join } from 'node:path'
9
9
  import { answerLine, isAlive, say, sayTo, runCaptured, tail, sleep } from './serve-util.mjs'
10
- import { SERVE_LOG_FILE, serveDataDir, ensureServeState, markStopped, rotateServeLog, markStarting, clearStarting, liveStarts, lockHolder, isWriter, readServePid } from './serve-files.mjs'
10
+ import { SERVE_LOG_FILE, serveDataDir, ensureServeState, markStopped, rotateServeLog, markStarting, clearStarting, liveStarts, lockHolder, isWriterWithin, isWriter, readServePid, STARTING_MAX_MS } from './serve-files.mjs'
11
11
  import { askPowershell, detachHow, windowsArg, wmiScript, parseWmiAnswer, wmiBlockedMessage, windowsPowershell, windowsTool, wslCommandLine } from './serve-windows.mjs'
12
12
  import { CHILD_FLAG } from './serve-args.mjs'
13
13
  import { startInSignIn } from './serve-autostart.mjs'
@@ -291,6 +291,7 @@ export async function runStop({
291
291
  graceMs = 10_000,
292
292
  kill = (pid, sig) => process.kill(pid, sig),
293
293
  write = (line) => process.stdout.write(line),
294
+ writer = isWriter,
294
295
  }) {
295
296
  const dataDir = serveDataDir(env, home)
296
297
  const deadline = Date.now() + budgetMs
@@ -311,13 +312,14 @@ export async function runStop({
311
312
  const tookTheFolder = () => {
312
313
  const h = lockHolder(dataDir)
313
314
  const key = h ? `lock:${h.pid}:${h.started}:${h.at}` : null
314
- if (key && !known.has(key)) known.set(key, isWriter(h) === true)
315
+ // A host started in another logon (its creation unreadable) took the folder too: counted for as long as a start takes
316
+ if (key && !known.has(key)) known.set(key, isWriterWithin(h, STARTING_MAX_MS, writer))
315
317
  if (!key || !known.get(key) || !isAlive(h.pid)) return (heldSince = null), false
316
318
  heldSince ??= Date.now()
317
319
  return Date.now() - heldSince > LOCK_GRACE_MS
318
320
  }
319
321
  let waiting = false
320
- for (let starts = liveStarts(dataDir, known); starts.length > 0 && !tookTheFolder(); starts = liveStarts(dataDir, known)) {
322
+ for (let starts = liveStarts(dataDir, known, { writer }); starts.length > 0 && !tookTheFolder(); starts = liveStarts(dataDir, known, { writer })) {
321
323
  if (!waiting) say(`centralu serve is starting a host for ${dataDir}; waiting for it, to stop it`)
322
324
  waiting = true
323
325
  if (Date.now() < deadline - STOP_RESERVE_MS) {
@@ -184,11 +184,12 @@ export function readStarting(dataDir) {
184
184
  * what was learnt of a record between calls (a Windows creation time costs a PowerShell), so a caller that asks again and
185
185
  * again pays it once per record.
186
186
  */
187
- export function liveStarts(dataDir, known = new Map(), { maxAgeMs = STARTING_MAX_MS, now = Date.now(), startOf = processStartTime, createdAt = processCreatedAt } = {}) {
187
+ export function liveStarts(dataDir, known = new Map(), { maxAgeMs = STARTING_MAX_MS, now = Date.now(), startOf = processStartTime, createdAt = processCreatedAt, writer = (r) => isWriter(r, startOf, createdAt) } = {}) {
188
188
  return readStarting(dataDir).filter((r) => {
189
189
  if (typeof r.at === 'number' && now - r.at > maxAgeMs) return false
190
190
  const key = `${r.pid}:${r.started}:${r.at}`
191
- if (!known.has(key)) known.set(key, isWriter(r, startOf, createdAt) === true)
191
+ // A launcher of another logon (creation unreadable) is a live start for as long as a start takes, as records here are
192
+ if (!known.has(key)) known.set(key, isWriterWithin(r, maxAgeMs, writer))
192
193
  return known.get(key) === true && isAlive(r.pid)
193
194
  })
194
195
  }
@@ -694,6 +695,17 @@ export function isWriter(holder, startOf = processStartTime, createdAt = process
694
695
  return created === null ? null : created <= holder.at + CLOCK_TICK_MS
695
696
  }
696
697
 
698
+ /**
699
+ * `isWriter` with its third answer settled: a process that lives but whose creation Windows will not tell (another logon's,
700
+ * `null`) counts as the writer while its record is younger than `maxAgeMs`, by the `at` it wrote. Asking for more
701
+ * (an elevated read, a prompt) is not an option for a launcher; the cost is a stranger that took a dead writer's pid
702
+ * within that time counting as the writer for the rest of it.
703
+ */
704
+ export function isWriterWithin(holder, maxAgeMs, writer = isWriter) {
705
+ const w = writer(holder)
706
+ return w === null ? typeof holder?.at === 'number' && Date.now() - holder.at < maxAgeMs : w
707
+ }
708
+
697
709
  /** One tick of the Windows clock */
698
710
  const CLOCK_TICK_MS = 16
699
711
 
@@ -18,7 +18,7 @@ import { closeSync, cpSync, existsSync, mkdirSync, openSync, readdirSync, readFi
18
18
  import { randomBytes } from 'node:crypto'
19
19
  import { join } from 'node:path'
20
20
  import { answerLine, replaceFile, runCaptured, say, sleep, tail, waitUntil } from './serve-util.mjs'
21
- import { isWriter, processStartTime, serveDataDir } from './serve-files.mjs'
21
+ import { isWriter, isWriterWithin, processStartTime, serveDataDir } from './serve-files.mjs'
22
22
  import { POINTER_WORD, managedLauncherScript, parsePointer, remoteLayout } from './serve-install.mjs'
23
23
  import { detachHow } from './serve-windows.mjs'
24
24
 
@@ -97,8 +97,7 @@ function readPointerFile(file) {
97
97
  /** Whether `switch.lock` names a live switcher (`isWriter`), a Windows pid of another logon counting for a while */
98
98
  export function lockIsLive(holder, writer = isWriter) {
99
99
  if (!holder || !Number.isInteger(holder.pid)) return false
100
- const w = writer(holder)
101
- return w === null ? Date.now() - holder.at < UNREADABLE_ALIVE_MS : w
100
+ return isWriterWithin(holder, UNREADABLE_ALIVE_MS, writer)
102
101
  }
103
102
 
104
103
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "centralu",
3
- "version": "0.1.0-beta.56",
3
+ "version": "0.1.0-beta.57",
4
4
  "description": "Run, watch and steer several Claude Code and Codex CLI sessions from one window",
5
5
  "license": "MIT",
6
6
  "author": "ijun17",
@@ -38,9 +38,9 @@
38
38
  "win32"
39
39
  ],
40
40
  "optionalDependencies": {
41
- "@centralu/darwin-arm64": "0.1.0-beta.56",
42
- "@centralu/linux-arm64": "0.1.0-beta.56",
43
- "@centralu/linux-x64": "0.1.0-beta.56",
44
- "@centralu/win32-x64": "0.1.0-beta.56"
41
+ "@centralu/darwin-arm64": "0.1.0-beta.57",
42
+ "@centralu/linux-arm64": "0.1.0-beta.57",
43
+ "@centralu/linux-x64": "0.1.0-beta.57",
44
+ "@centralu/win32-x64": "0.1.0-beta.57"
45
45
  }
46
46
  }