viewable-one 0.12.6 → 0.12.8
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 +101 -0
- package/SPAWN.md +74 -60
- package/glytch.mjs +6 -2
- package/lib/acp.mjs +12 -1
- package/lib/agent-selection.mjs +84 -0
- package/lib/agents.mjs +26 -1
- package/lib/child.mjs +51 -23
- package/lib/codex.mjs +42 -2
- package/lib/codexApps.mjs +17 -2
- package/lib/codexToolInputs.mjs +131 -10
- package/lib/conversationRules.mjs +31 -2
- package/lib/detect.mjs +44 -4
- package/lib/doctor.mjs +3 -1
- package/lib/groupStop.mjs +81 -0
- package/lib/multiwatch.mjs +25 -2
- package/lib/otherLogins.mjs +282 -0
- package/lib/permissionAsk.mjs +134 -7
- package/lib/runnerAdmission.mjs +5 -1
- package/lib/runnerApi.mjs +7 -2
- package/lib/runnerCompletion.mjs +10 -2
- package/lib/runnerExecute.mjs +10 -7
- package/lib/runnerLaunch.mjs +14 -4
- package/lib/runnerRecovery.mjs +46 -1
- package/lib/runnerWorkspace.mjs +9 -0
- package/lib/selfUninstall.mjs +15 -4
- package/lib/worktree.mjs +110 -111
- package/lib/writePolicy.mjs +342 -13
- package/package.json +1 -1
- package/spawn-runner.mjs +5 -1
package/lib/worktree.mjs
CHANGED
|
@@ -27,8 +27,7 @@
|
|
|
27
27
|
// usually thrown away.
|
|
28
28
|
|
|
29
29
|
import { execFile } from "node:child_process";
|
|
30
|
-
import { symlink,
|
|
31
|
-
import { hostname } from "node:os";
|
|
30
|
+
import { symlink, access, readFile, writeFile, lstat } from "node:fs/promises";
|
|
32
31
|
import { canonicalPath, staysInProject } from "./projectPaths.mjs";
|
|
33
32
|
import { basename, isAbsolute, join, resolve, sep } from "node:path";
|
|
34
33
|
// THE PROCESS KILLING LIVES IN procs.mjs since 2026-09-02: it is about
|
|
@@ -42,6 +41,7 @@ import { basename, isAbsolute, join, resolve, sep } from "node:path";
|
|
|
42
41
|
// from here is what keeps these two modules a straight line instead of a
|
|
43
42
|
// cycle, which is the thing check-portable --strict now looks for.
|
|
44
43
|
import { CHAT_PREFIX, GLYTCH, killProcessesIn } from "./procs.mjs";
|
|
44
|
+
import { acquireAdmission } from "./admission.mjs";
|
|
45
45
|
export { CHAT_PREFIX, GLYTCH };
|
|
46
46
|
export { isOursToKill, parseProcessTable, reapOrphanedWorktreeProcesses, reapableWorktreeName } from "./procs.mjs";
|
|
47
47
|
|
|
@@ -99,121 +99,91 @@ const exists = async (path) => {
|
|
|
99
99
|
// inside one runner can no longer both `git checkout main` and merge at once.
|
|
100
100
|
// This is the lock that has been here since 2026-09-01.
|
|
101
101
|
//
|
|
102
|
-
// CROSS-PROCESS,
|
|
103
|
-
//
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
//
|
|
107
|
-
//
|
|
108
|
-
//
|
|
109
|
-
// about two processes running `git checkout main` in the same second.
|
|
102
|
+
// CROSS-PROCESS, through the SAME exclusive-hard-link protocol the runner's
|
|
103
|
+
// admission lock uses (`admission.mjs`), filed in Git's COMMON directory.
|
|
104
|
+
// `spawn-runner.mjs merge <path>` and `delete-worktree <path>` are a SECOND
|
|
105
|
+
// process, typed by a person, calling straight into mergeWorktree while a
|
|
106
|
+
// watcher is running. The claim files keep two runners from taking the same
|
|
107
|
+
// REQUEST; they say nothing about two processes running `git checkout main` in
|
|
108
|
+
// the same second.
|
|
110
109
|
//
|
|
111
|
-
//
|
|
112
|
-
//
|
|
113
|
-
//
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
// a
|
|
110
|
+
// THE COMMON DIRECTORY, NOT `<root>/.git` (#6558). `.git` is a FILE in a linked
|
|
111
|
+
// worktree, so the old `mkdir <root>/.git/pm-repo.lock` always failed there,
|
|
112
|
+
// and a bridge started from a linked worktree never held the lock at all.
|
|
113
|
+
// `git rev-parse --git-common-dir` is the one folder every worktree of a repo
|
|
114
|
+
// shares, so the checkout and each of its linked worktrees now contend for the
|
|
115
|
+
// same lock. It is inside `.git/` for the same reason as before: mergeWorktreeImpl
|
|
116
|
+
// refuses a dirty checkout, and `git status` never reports that folder.
|
|
117
|
+
//
|
|
118
|
+
// NO LOCK, NO CHANGE (#6558). This used to log "proceeding without it" when the
|
|
119
|
+
// lock could not be made or the wait ran out, and then ran the change anyway,
|
|
120
|
+
// which is the one outcome a lock exists to prevent: checkout, stash, merge and
|
|
121
|
+
// reset steps from two callers interleaved. Now a repository that cannot be
|
|
122
|
+
// locked, or a holder that outlasts the wait, REFUSES the change with a
|
|
123
|
+
// RepoLockError that says who holds it. The one exception is a folder that is
|
|
124
|
+
// not a git repository at all: there is nothing to lock and no repository
|
|
125
|
+
// change to make.
|
|
118
126
|
const lockChains = new Map(); // root -> Promise that resolves when the lock is free
|
|
119
127
|
|
|
120
|
-
/** How long
|
|
121
|
-
* mutations here take seconds; ten minutes is a crashed holder, not a busy
|
|
122
|
-
* one. */
|
|
123
|
-
export const LOCK_STALE_MS = 10 * 60_000;
|
|
124
|
-
/** How long to wait for another process to finish before going ahead anyway. */
|
|
128
|
+
/** How long to wait for another holder to finish before refusing the change. */
|
|
125
129
|
export const LOCK_WAIT_MS = 60_000;
|
|
126
130
|
const LOCK_POLL_MS = 150;
|
|
127
131
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const lockDirFor = (root) => join(resolve(root), ".git", "pm-repo.lock");
|
|
135
|
-
|
|
136
|
-
const stillAlive = (pid) => {
|
|
137
|
-
try {
|
|
138
|
-
process.kill(pid, 0);
|
|
139
|
-
return true;
|
|
140
|
-
} catch (error) {
|
|
141
|
-
// EPERM means it exists and belongs to somebody else.
|
|
142
|
-
return error?.code === "EPERM";
|
|
132
|
+
/** Thrown by withRepoLock, and never run past: the callback did NOT run. */
|
|
133
|
+
export class RepoLockError extends Error {
|
|
134
|
+
constructor(detail) {
|
|
135
|
+
super(detail);
|
|
136
|
+
this.name = "RepoLockError";
|
|
137
|
+
this.code = "REPO_LOCK";
|
|
143
138
|
}
|
|
144
|
-
}
|
|
139
|
+
}
|
|
145
140
|
|
|
146
|
-
/**
|
|
147
|
-
*
|
|
141
|
+
/** Git's common directory for `root`, absolute; null when `root` is not in a
|
|
142
|
+
* repository. Relative output (`.git`) is relative to `root`. */
|
|
143
|
+
async function commonDirOf(root) {
|
|
144
|
+
const found = await git(["rev-parse", "--git-common-dir"], root);
|
|
145
|
+
const out = found.stdout.trim();
|
|
146
|
+
return found.ok && out ? resolve(root, out) : null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Where the repository's lock lives, for tests and docs. */
|
|
150
|
+
export const repoLockDirFor = (commonDir) => join(commonDir, "pm-repo-admission");
|
|
151
|
+
|
|
152
|
+
/** Take the cross-process lock. Returns `{ release }` once it is REALLY held,
|
|
153
|
+
* or `{ release: null }` for a folder that is not a repository. Throws
|
|
154
|
+
* RepoLockError when the lock cannot be held. */
|
|
148
155
|
async function acquireFileLock(root, log) {
|
|
149
|
-
const
|
|
156
|
+
const commonDir = await commonDirOf(root);
|
|
157
|
+
if (!commonDir) {
|
|
158
|
+
log("not a git repository; nothing to lock");
|
|
159
|
+
return { release: null };
|
|
160
|
+
}
|
|
161
|
+
const directory = repoLockDirFor(commonDir);
|
|
150
162
|
const startedAt = Date.now();
|
|
151
163
|
const deadline = startedAt + LOCK_WAIT_MS;
|
|
152
|
-
|
|
153
|
-
// WAITED, NAMED (2026-09-21, the claim-to-spawn gap). `busy` is only true
|
|
154
|
-
// once the FIRST `mkdir` has already lost to EEXIST - so a lock taken on
|
|
155
|
-
// the first try logs nothing, same as before, and only a caller that
|
|
156
|
-
// actually queued behind another holder pays for a line. `lastOwner` is
|
|
157
|
-
// whichever `owner.json` was last read while waiting, so the eventual
|
|
158
|
-
// success can say who it waited on even though that holder may have
|
|
159
|
-
// released and vanished by the time this loop wins.
|
|
160
|
-
let busy = false;
|
|
161
|
-
let lastOwner = null;
|
|
164
|
+
let waited = false;
|
|
162
165
|
for (;;) {
|
|
166
|
+
let admission;
|
|
163
167
|
try {
|
|
164
|
-
await
|
|
165
|
-
await writeFile(join(dir, "owner.json"), `${JSON.stringify(mine)}\n`, "utf8").catch(() => {});
|
|
166
|
-
if (busy) {
|
|
167
|
-
const waitedSecs = ((Date.now() - startedAt) / 1000).toFixed(1);
|
|
168
|
-
log(
|
|
169
|
-
`took the repo lock at ${dir} after waiting ${waitedSecs}s ` +
|
|
170
|
-
`(previously held by pid ${lastOwner?.pid ?? "?"} on ${lastOwner?.host ?? "?"})`,
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
return async () => { await rm(dir, { recursive: true, force: true }).catch(() => {}); };
|
|
168
|
+
admission = await acquireAdmission(root, { directory });
|
|
174
169
|
} catch (error) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
// filesystem that will not mkdir must not stop a merge.
|
|
179
|
-
log(`could not take the repo lock (${error?.code ?? error}); proceeding without it`);
|
|
180
|
-
return null;
|
|
181
|
-
}
|
|
182
|
-
busy = true;
|
|
183
|
-
let owner = null;
|
|
184
|
-
try { owner = JSON.parse(await readFile(join(dir, "owner.json"), "utf8")); } catch { /* mid-write, or crashed between mkdir and write */ }
|
|
185
|
-
if (owner) lastOwner = owner;
|
|
186
|
-
// A holder that just won the `mkdir` has not written `owner.json` yet,
|
|
187
|
-
// so a reader landing in that gap sees the same ENOENT a crashed holder
|
|
188
|
-
// would leave forever. Treating "unreadable" as "infinitely old" (age
|
|
189
|
-
// measured from `at: 0`) cleared a LIVE lock out from under its holder
|
|
190
|
-
// on every such read - a real cross-process race, not just a test one.
|
|
191
|
-
// Falling back to the lock DIRECTORY's own age keeps a lock that was
|
|
192
|
-
// just taken safe (its mtime is now) while still reclaiming one whose
|
|
193
|
-
// holder crashed before ever writing owner.json (the dir ages past
|
|
194
|
-
// LOCK_STALE_MS same as always).
|
|
195
|
-
let age;
|
|
196
|
-
if (owner) {
|
|
197
|
-
age = Date.now() - (Number(owner.at) || 0);
|
|
198
|
-
} else {
|
|
199
|
-
const dirStat = await stat(dir).catch(() => null);
|
|
200
|
-
age = dirStat ? Date.now() - dirStat.mtimeMs : 0;
|
|
201
|
-
}
|
|
202
|
-
const deadHolder = owner?.host === mine.host && Number.isFinite(owner?.pid) && !stillAlive(owner.pid);
|
|
203
|
-
if (deadHolder || age > LOCK_STALE_MS) {
|
|
204
|
-
log(`clearing a ${deadHolder ? "dead" : "stale"} repo lock at ${dir} (pid ${owner?.pid ?? "?"})`);
|
|
205
|
-
await rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
if (Date.now() >= deadline) {
|
|
209
|
-
log(
|
|
210
|
-
`waited ${Math.round(LOCK_WAIT_MS / 1000)}s for the repo lock held by pid ${owner?.pid ?? "?"} ` +
|
|
211
|
-
`on ${owner?.host ?? "?"}; going ahead anyway`,
|
|
212
|
-
);
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
215
|
-
await new Promise((r) => setTimeout(r, LOCK_POLL_MS));
|
|
170
|
+
// A read-only repository or a filesystem that will not link: the lock
|
|
171
|
+
// cannot be held, so the change does not run.
|
|
172
|
+
throw new RepoLockError(`Could not take the repo lock at ${directory} (${error?.code ?? error?.message ?? error}). Nothing was changed.`);
|
|
216
173
|
}
|
|
174
|
+
if (admission.ok) {
|
|
175
|
+
if (waited) log(`took the repo lock at ${directory} after waiting ${((Date.now() - startedAt) / 1000).toFixed(1)}s`);
|
|
176
|
+
return { release: admission.release };
|
|
177
|
+
}
|
|
178
|
+
waited = true;
|
|
179
|
+
if (Date.now() >= deadline) {
|
|
180
|
+
throw new RepoLockError(
|
|
181
|
+
`Waited ${Math.round(LOCK_WAIT_MS / 1000)}s for the repo lock and another process still holds it. ` +
|
|
182
|
+
`Nothing was changed. ${admission.detail} ` +
|
|
183
|
+
`Once you have confirmed that process is stopped, delete ${join(directory, ".admission.lock")} and try again.`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
await new Promise((r) => setTimeout(r, LOCK_POLL_MS));
|
|
217
187
|
}
|
|
218
188
|
}
|
|
219
189
|
|
|
@@ -233,21 +203,36 @@ async function acquireRepoLock(root) {
|
|
|
233
203
|
* first, so a queue of callers inside this process waits on a promise rather
|
|
234
204
|
* than on a directory. Preparation may supply check/own: check cancellation
|
|
235
205
|
* after each lock wait, then own the started mutation through its result.
|
|
236
|
-
* Callers must not treat the queued lock wait itself as an owned mutation.
|
|
206
|
+
* Callers must not treat the queued lock wait itself as an owned mutation.
|
|
207
|
+
*
|
|
208
|
+
* `fn` runs only while the lock is held. When it cannot be taken this throws
|
|
209
|
+
* RepoLockError without running `fn`; the exported wrappers below turn that into
|
|
210
|
+
* each operation's own refusal shape. */
|
|
237
211
|
export async function withRepoLock(root, fn, log = () => {}, lifecycle = {}) {
|
|
238
212
|
const release = await acquireRepoLock(root);
|
|
239
213
|
let releaseFile = null;
|
|
240
214
|
try {
|
|
241
215
|
lifecycle.check?.();
|
|
242
|
-
releaseFile = await acquireFileLock(root, log);
|
|
216
|
+
({ release: releaseFile } = await acquireFileLock(root, log));
|
|
243
217
|
lifecycle.check?.();
|
|
244
218
|
return await (lifecycle.own ? lifecycle.own(fn) : fn());
|
|
245
219
|
} finally {
|
|
246
|
-
if (releaseFile) await releaseFile();
|
|
220
|
+
if (releaseFile) await releaseFile().catch(() => {});
|
|
247
221
|
release();
|
|
248
222
|
}
|
|
249
223
|
}
|
|
250
224
|
|
|
225
|
+
/** Run `fn` under the repo lock; a refused lock becomes `onRefused(detail)`.
|
|
226
|
+
* It does not log: the caller that reads the refusal says it once. */
|
|
227
|
+
async function lockedOr(root, fn, lifecycle, onRefused) {
|
|
228
|
+
try {
|
|
229
|
+
return await withRepoLock(root, fn, () => {}, lifecycle);
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (!(error instanceof RepoLockError)) throw error;
|
|
232
|
+
return onRefused(error.message);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
251
236
|
/** The repo's default branch, LOCAL only: `main` if `refs/heads/main` exists,
|
|
252
237
|
* else `master` if that exists, else null.
|
|
253
238
|
*
|
|
@@ -1166,30 +1151,44 @@ function cachePathspecs(entries) {
|
|
|
1166
1151
|
// mergeWorktree calls on the same root are guaranteed to run sequentially, order
|
|
1167
1152
|
// preserved. Reads (branchOfWorktree, worktreeSummary)
|
|
1168
1153
|
// stay unlocked.
|
|
1154
|
+
// A REFUSED LOCK IS NOT "NOT A REPOSITORY" (#6558 judge). makeWorktree and
|
|
1155
|
+
// reuseWorktree mark it `lockRefused`, and the runner then spawns NOTHING:
|
|
1156
|
+
// falling back to the linked folder would put the new session in the very
|
|
1157
|
+
// checkout the holder is stashing, merging or resetting.
|
|
1169
1158
|
export async function makeWorktree(root, id, log = () => {}, title = "", lifecycle) {
|
|
1170
|
-
return
|
|
1159
|
+
return lockedOr(root, () => makeWorktreeImpl(root, id, log, title), lifecycle, (detail) => ({
|
|
1160
|
+
cwd: root,
|
|
1161
|
+
isolated: false,
|
|
1162
|
+
lockRefused: true,
|
|
1163
|
+
detail,
|
|
1164
|
+
}));
|
|
1171
1165
|
}
|
|
1172
1166
|
|
|
1173
1167
|
/** The chat tree's mint takes the repo lock like every other git mutation:
|
|
1174
1168
|
* two rooms' first messages to the same agent, typed at once, are two
|
|
1175
1169
|
* `worktree add` calls for one path. */
|
|
1176
1170
|
export async function makeChatWorktree(root, path, log = () => {}, lifecycle) {
|
|
1177
|
-
return
|
|
1171
|
+
return lockedOr(root, () => makeChatWorktreeImpl(root, path, log), lifecycle, (detail) => ({ ok: false, reason: detail }));
|
|
1178
1172
|
}
|
|
1179
1173
|
|
|
1180
1174
|
export async function cleanWorktree(root, made, log = () => {}, wroteFiles = []) {
|
|
1181
|
-
return
|
|
1175
|
+
return lockedOr(root, () => cleanWorktreeImpl(root, made, log, wroteFiles), undefined, (detail) => {
|
|
1176
|
+
log(detail);
|
|
1177
|
+
return { removed: false, detail: `${made?.detail ?? ""}\nKEPT: ${detail}` };
|
|
1178
|
+
});
|
|
1182
1179
|
}
|
|
1183
1180
|
|
|
1181
|
+
/** The tree, null when it is gone, or `{ lockRefused: true, detail }` when the
|
|
1182
|
+
* lock could not be held: the tree may well still be there. */
|
|
1184
1183
|
export async function reuseWorktree(root, existingPath, log = () => {}, lifecycle) {
|
|
1185
|
-
return
|
|
1184
|
+
return lockedOr(root, () => reuseWorktreeImpl(root, existingPath, log), lifecycle, (detail) => ({ lockRefused: true, detail }));
|
|
1186
1185
|
}
|
|
1187
1186
|
|
|
1188
1187
|
export async function mergeWorktree(root, worktreePath, log = () => {}) {
|
|
1189
|
-
return
|
|
1188
|
+
return lockedOr(root, () => mergeWorktreeImpl(root, worktreePath, log), undefined, (detail) => ({ ok: false, detail }));
|
|
1190
1189
|
}
|
|
1191
1190
|
|
|
1192
1191
|
export async function deleteWorktree(root, worktreePath, log = () => {}) {
|
|
1193
|
-
return
|
|
1192
|
+
return lockedOr(root, () => deleteWorktreeImpl(root, worktreePath, log), undefined, (detail) => ({ ok: false, detail }));
|
|
1194
1193
|
}
|
|
1195
1194
|
|