my-durable 0.0.115 → 0.0.117
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/apps/dpi-backend/src/backup.ts +41 -0
- package/apps/dpi-backend/src/diagnostics.ts +12 -1
- package/apps/dpi-backend/src/git.ts +92 -35
- package/apps/dpi-backend/src/store.ts +46 -6
- package/apps/dpi-backend/src/vitals.ts +6 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-5H8W3a_-.js +83 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/{index-lji31QeB.css → index-DD66rPUN.css} +1 -1
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/index.html +2 -2
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/composables.ts +29 -2
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/export-bundle.ts +14 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/markdown.ts +7 -2
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/GitPanel.vue +11 -8
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TunnelStatusBar.vue +5 -1
- package/apps/dpi-playground/apps/dpi-playground-hub/src/main.ts +12 -2
- package/apps/dpi-playground/apps/dpi-playground-hub/src/mux.ts +153 -17
- package/apps/dpi-playground/apps/dpi-playground-hub/src/tunnel-transport.ts +38 -11
- package/apps/dpi-sdk/src/client.ts +53 -2
- package/apps/dpi-sdk/src/node/ops.ts +6 -2
- package/apps/dpi-sdk/src/node/private-file.ts +34 -1
- package/apps/dpi-sdk/src/node/registry.ts +5 -1
- package/apps/dpi-sdk/src/protocol/limits.ts +21 -0
- package/docs/verification.md +18 -0
- package/package.json +1 -1
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-jjYABCI4.js +0 -83
|
@@ -113,6 +113,25 @@ export const defaultBackupIo = (): BackupIo => ({
|
|
|
113
113
|
now: () => Date.now(),
|
|
114
114
|
});
|
|
115
115
|
|
|
116
|
+
// ---------------------------------------------------------------- mutex
|
|
117
|
+
|
|
118
|
+
// run-11 h2-3: create and restore share ONE advisory, per-process gate.
|
|
119
|
+
// Two backup flows racing inside one process (concurrent CLI invocations,
|
|
120
|
+
// tests, a create begun while a restore still walks) used to interleave their
|
|
121
|
+
// fs phases — a restore's renameAside could swallow files a half-finished
|
|
122
|
+
// create was still reading. Cross-process races remain the healthz probe's
|
|
123
|
+
// job; this gate only serializes what one process can actually race.
|
|
124
|
+
let backupGate: Promise<unknown> = Promise.resolve();
|
|
125
|
+
|
|
126
|
+
/** Run `op` holding the backup gate — every create/restore passes through
|
|
127
|
+
* here. FIFO by call order, advisory, failure-safe: a rejected predecessor
|
|
128
|
+
* still releases (the gate chains on a swallowed catch, never on `run`). */
|
|
129
|
+
function withBackupGate<T>(op: () => Promise<T>): Promise<T> {
|
|
130
|
+
const run = backupGate.then(op, op);
|
|
131
|
+
backupGate = run.catch(() => {});
|
|
132
|
+
return run;
|
|
133
|
+
}
|
|
134
|
+
|
|
116
135
|
// ---------------------------------------------------------------- create
|
|
117
136
|
|
|
118
137
|
/** Entry 0 of every backup zip: what was backed up, by which build. */
|
|
@@ -196,6 +215,11 @@ async function walkHome(io: BackupIo, home: string, out: string, maxBytes: numbe
|
|
|
196
215
|
* module's.
|
|
197
216
|
*/
|
|
198
217
|
export async function createBackup(io: BackupIo, opts: CreateBackupOptions): Promise<CreateBackupResult> {
|
|
218
|
+
// run-11 h2-3: serialized against restore (and other creates) per process.
|
|
219
|
+
return withBackupGate(() => createBackupLocked(io, opts));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function createBackupLocked(io: BackupIo, opts: CreateBackupOptions): Promise<CreateBackupResult> {
|
|
199
223
|
// R9 (run-8 u7): the probe restore already runs, mirrored verbatim — a
|
|
200
224
|
// backup taken while the daemon answers captures catalog.sqlite,
|
|
201
225
|
// agent.sqlite and -wal/-shm at different instants, i.e. a live "backup"
|
|
@@ -214,6 +238,18 @@ export async function createBackup(io: BackupIo, opts: CreateBackupOptions): Pro
|
|
|
214
238
|
if (!homeStat.isDirectory()) throw invalid(`home is not a directory: ${home}`);
|
|
215
239
|
|
|
216
240
|
const files = await walkHome(io, home, out, opts.maxBytes ?? MAX_BACKUP_BYTES);
|
|
241
|
+
// run-11 h2-3: the walk can take minutes on a real home, and a daemon
|
|
242
|
+
// started during it writes catalog.sqlite/agent.sqlite while we are about
|
|
243
|
+
// to read them — the archive would be torn exactly like the pre-probe case.
|
|
244
|
+
// Re-probe AFTER the walk and BEFORE the first entry byte is read: any
|
|
245
|
+
// answer aborts on the same surface as the refusal above, with nothing
|
|
246
|
+
// written (the zip build and io.writeFile have not run yet).
|
|
247
|
+
if (opts.healthzUrl && (await deviceIsRunning(io, opts.healthzUrl))) {
|
|
248
|
+
throw new BackupError(
|
|
249
|
+
"device_running",
|
|
250
|
+
"the device came online during the backup — a live sqlite handle would tear the archive; stop it (`dpi device stop`) and retry",
|
|
251
|
+
);
|
|
252
|
+
}
|
|
217
253
|
const bytes = files.reduce((n, f) => n + f.size, 0);
|
|
218
254
|
const manifest: BackupManifest = {
|
|
219
255
|
createdAt: new Date(io.now()).toISOString(),
|
|
@@ -510,6 +546,11 @@ export async function deviceIsRunning(io: BackupIo, healthzUrl: string): Promise
|
|
|
510
546
|
* manifest always comes first.
|
|
511
547
|
*/
|
|
512
548
|
export async function restoreBackup(io: BackupIo, opts: RestoreBackupOptions): Promise<RestoreBackupResult> {
|
|
549
|
+
// run-11 h2-3: serialized against create (and other restores) per process.
|
|
550
|
+
return withBackupGate(() => restoreBackupLocked(io, opts));
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
async function restoreBackupLocked(io: BackupIo, opts: RestoreBackupOptions): Promise<RestoreBackupResult> {
|
|
513
554
|
const home = resolve(opts.home);
|
|
514
555
|
if (opts.healthzUrl && (await deviceIsRunning(io, opts.healthzUrl))) {
|
|
515
556
|
throw new BackupError("device_running", "the device is running — stop it first (`dpi device stop`)");
|
|
@@ -253,9 +253,20 @@ function diagnosticsDoctorIo(io: DiagnosticsIo): DoctorIo {
|
|
|
253
253
|
env: io.env,
|
|
254
254
|
nodeVersion: io.nodeVersion,
|
|
255
255
|
// doctor's FetchLike wants json(); this io speaks text() — adapt, never reimplement.
|
|
256
|
+
// r10 h2-1: the adapter used to res.text() the WHOLE body — the doctor
|
|
257
|
+
// section was the one diagnostics read that bypassed the R9 byte caps
|
|
258
|
+
// ("duration is not a byte budget"). Route through the capped reader.
|
|
256
259
|
fetchImpl: async (url, init) => {
|
|
257
260
|
const res = await io.fetch(url, init);
|
|
258
|
-
return {
|
|
261
|
+
return {
|
|
262
|
+
ok: res.ok,
|
|
263
|
+
status: res.status,
|
|
264
|
+
json: async () => {
|
|
265
|
+
const raw = await readCappedBody(res, API_BODY_MAX_BYTES);
|
|
266
|
+
if (raw.length > API_BODY_MAX_BYTES) throw Error(`doctor body over ${API_BODY_MAX_BYTES} bytes`);
|
|
267
|
+
return JSON.parse(raw);
|
|
268
|
+
},
|
|
269
|
+
};
|
|
259
270
|
},
|
|
260
271
|
access: io.accessSync,
|
|
261
272
|
exists: io.existsSync,
|
|
@@ -165,13 +165,20 @@ export function parseRemote(url?: string): ParsedRemote | undefined {
|
|
|
165
165
|
path = path.replace(/\/+$/, "").replace(/\.git$/, "");
|
|
166
166
|
if (!host || !path) return undefined;
|
|
167
167
|
const h = host.toLowerCase();
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
168
|
+
// r10 h2-6: forge classification is EXACT (host or subdomain of the known
|
|
169
|
+
// forge, or a dedicated DNS label for self-hosted gitlab/bitbucket). The old
|
|
170
|
+
// substring regexes matched github.com.evil.com → GitHub-styled PR buttons
|
|
171
|
+
// on lookalike hosts. A host carrying '@' (scp-form with two @) is never real.
|
|
172
|
+
if (h.includes("@")) return undefined;
|
|
173
|
+
const labels = h.split(".").filter(Boolean);
|
|
174
|
+
const forge: Forge =
|
|
175
|
+
h === "github.com" || h.endsWith(".github.com")
|
|
176
|
+
? "github"
|
|
177
|
+
: h === "gitlab.com" || h.endsWith(".gitlab.com") || labels.includes("gitlab")
|
|
178
|
+
? "gitlab"
|
|
179
|
+
: h === "bitbucket.org" || h.endsWith(".bitbucket.org") || labels.includes("bitbucket")
|
|
180
|
+
? "bitbucket"
|
|
181
|
+
: "other";
|
|
175
182
|
const scheme = /^http:\/\//.test(raw) ? "http" : "https";
|
|
176
183
|
return { host, path, forge, webUrl: `${scheme}://${host}/${path}` };
|
|
177
184
|
}
|
|
@@ -209,33 +216,61 @@ export function isNotARepoError(e: unknown): boolean {
|
|
|
209
216
|
* actually starts, so queueing never eats the timeout. */
|
|
210
217
|
export const GIT_SPAWN_BUDGET = 12;
|
|
211
218
|
let gitSpawnLive = 0;
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
+
// run-11 h2-2: the single FIFO queue starved interactive ops — one
|
|
220
|
+
// 50-path status-batch parks ~650 low spawns, and the status panel the user
|
|
221
|
+
// is LOOKING at queued behind all of them. Two wake lanes now: high holds
|
|
222
|
+
// interactive single ops (a bare /git/status, the /git mutation route);
|
|
223
|
+
// low holds every fan-out (status-batch, the boot project-seed walk,
|
|
224
|
+
// git-files turns). FIFO within each lane; a freed slot wakes high first;
|
|
225
|
+
// nobody is ever refused and no spawn changes shape — only the wake order.
|
|
226
|
+
const gitSpawnQueueHigh: (() => void)[] = [];
|
|
227
|
+
const gitSpawnQueueLow: (() => void)[] = [];
|
|
228
|
+
|
|
229
|
+
/** Take a slot (or queue FIFO in your lane for one) — the caller owns the slot
|
|
230
|
+
* until releaseGitSpawn(). No nesting: a slot is held across exactly ONE
|
|
231
|
+
* child, so a queued git child can never be waiting on a spawn that needs
|
|
232
|
+
* this queue to drain — no deadlock. */
|
|
233
|
+
function acquireGitSpawn(priority?: "high" | "low"): Promise<void> {
|
|
219
234
|
if (gitSpawnLive < GIT_SPAWN_BUDGET) {
|
|
220
235
|
gitSpawnLive++;
|
|
221
236
|
return Promise.resolve();
|
|
222
237
|
}
|
|
223
|
-
return new Promise<void>((wake) =>
|
|
238
|
+
return new Promise<void>((wake) => (priority === "high" ? gitSpawnQueueHigh : gitSpawnQueueLow).push(wake));
|
|
224
239
|
}
|
|
225
240
|
|
|
226
|
-
/** Free a slot — handed straight to the longest-waiting caller
|
|
227
|
-
*
|
|
241
|
+
/** Free a slot — handed straight to the longest-waiting HIGH caller first
|
|
242
|
+
* (run-11 h2-2), then the longest-waiting low one, when either exists (the
|
|
243
|
+
* live count already covers the handover), else returned. */
|
|
228
244
|
function releaseGitSpawn() {
|
|
229
|
-
const next =
|
|
245
|
+
const next = gitSpawnQueueHigh.shift() ?? gitSpawnQueueLow.shift();
|
|
230
246
|
if (next) next();
|
|
231
247
|
else gitSpawnLive--;
|
|
232
248
|
}
|
|
233
249
|
|
|
250
|
+
/** run-11 h2-2: the semaphore's state for deviceVitals — live children,
|
|
251
|
+
* waiting callers, and how many of those waiters are interactive ("high")
|
|
252
|
+
* ops parked behind a fan-out. Pure read, no side effects. */
|
|
253
|
+
export function gitSpawnStats(): { live: number; queued: number; queuedHigh: number } {
|
|
254
|
+
return {
|
|
255
|
+
live: gitSpawnLive,
|
|
256
|
+
queued: gitSpawnQueueHigh.length + gitSpawnQueueLow.length,
|
|
257
|
+
queuedHigh: gitSpawnQueueHigh.length,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
234
261
|
/** One git call; rejects on non-zero exit with stderr in the message. Every
|
|
235
262
|
* spawn runs under the budget above — see GIT_SPAWN_BUDGET. (gh() stays
|
|
236
|
-
* outside: forge calls are single, user-initiated, and never fan out.)
|
|
237
|
-
|
|
238
|
-
|
|
263
|
+
* outside: forge calls are single, user-initiated, and never fan out.)
|
|
264
|
+
* `priority: "high"` (run-11 h2-2) only changes the WAKE order while the
|
|
265
|
+
* budget is saturated — the per-op timeout still starts at child start. */
|
|
266
|
+
export function git(
|
|
267
|
+
cwd: string,
|
|
268
|
+
args: string[],
|
|
269
|
+
timeout = GIT_TIMEOUT,
|
|
270
|
+
pin?: RepoPin,
|
|
271
|
+
priority?: "high" | "low",
|
|
272
|
+
): Promise<string> {
|
|
273
|
+
return acquireGitSpawn(priority)
|
|
239
274
|
.then(
|
|
240
275
|
() =>
|
|
241
276
|
new Promise<string>((ok, fail) => {
|
|
@@ -313,7 +348,7 @@ export async function gitStatusBatch(paths: string[]): Promise<Record<string, Gi
|
|
|
313
348
|
const entries = await Promise.all(
|
|
314
349
|
unique.map(async (p): Promise<[string, GitStatus | { error: string }]> => {
|
|
315
350
|
try {
|
|
316
|
-
return [p, await gitStatus(p)];
|
|
351
|
+
return [p, await gitStatus(p, Date.now(), "low")];
|
|
317
352
|
} catch (e) {
|
|
318
353
|
return [p, { error: String((e as Error)?.message ?? e).slice(0, 120) }];
|
|
319
354
|
}
|
|
@@ -408,11 +443,11 @@ function ensureWatcher(root: string) {
|
|
|
408
443
|
* never served). The absolute git dir is NOT confined: redirects and linked
|
|
409
444
|
* worktrees whose gitdir lives elsewhere are operator territory and are
|
|
410
445
|
* followed via the returned pin (the agent itself is unsandboxed by design). */
|
|
411
|
-
export async function resolveRepoPin(path: string): Promise<RepoPin | undefined> {
|
|
446
|
+
export async function resolveRepoPin(path: string, priority?: "high" | "low"): Promise<RepoPin | undefined> {
|
|
412
447
|
if (!repoRootAllowed) return undefined;
|
|
413
448
|
const [topLevel, gitDir] = await Promise.all([
|
|
414
|
-
git(path, ["rev-parse", "--show-toplevel"]),
|
|
415
|
-
git(path, ["rev-parse", "--absolute-git-dir"]),
|
|
449
|
+
git(path, ["rev-parse", "--show-toplevel"], GIT_TIMEOUT, undefined, priority),
|
|
450
|
+
git(path, ["rev-parse", "--absolute-git-dir"], GIT_TIMEOUT, undefined, priority),
|
|
416
451
|
]);
|
|
417
452
|
const pin: RepoPin = { gitDir: gitDir.trim(), workTree: topLevel.trim() };
|
|
418
453
|
// R23 (operator decision): durable-pi WRAPS pi/pi-durable — the agent is
|
|
@@ -444,8 +479,11 @@ export function clearGitStatusCache() {
|
|
|
444
479
|
}
|
|
445
480
|
|
|
446
481
|
/** Structured status for a folder: everything the UIs show, one call.
|
|
447
|
-
* `now` is injectable so a test can age the caches without sleeping.
|
|
448
|
-
|
|
482
|
+
* `now` is injectable so a test can age the caches without sleeping.
|
|
483
|
+
* run-11 h2-2: a bare call (the /git/status route, session create) is an
|
|
484
|
+
* INTERACTIVE single op and rides the high spawn lane; fan-out callers
|
|
485
|
+
* (gitStatusBatch, the store's boot seed) pass "low" explicitly. */
|
|
486
|
+
export async function gitStatus(path: string, now = Date.now(), priority: "high" | "low" = "high"): Promise<GitStatus> {
|
|
449
487
|
// One key per repo, not per spelling: `D:\\x`, `d:/x/` and `D:/x` land on
|
|
450
488
|
// the SAME entry (cacheKey), so single-flight dedupes every spelling.
|
|
451
489
|
const key = cacheKey(path);
|
|
@@ -456,14 +494,14 @@ export async function gitStatus(path: string, now = Date.now()): Promise<GitStat
|
|
|
456
494
|
if (failed && now - failed.at < STATUS_FAIL_TTL) throw new Error(failed.msg);
|
|
457
495
|
const running = statusInFlight.get(key);
|
|
458
496
|
if (running) return running;
|
|
459
|
-
const p = statusOnce(path, key).finally(() => {
|
|
497
|
+
const p = statusOnce(path, key, priority).finally(() => {
|
|
460
498
|
if (statusInFlight.get(key) === p) statusInFlight.delete(key);
|
|
461
499
|
});
|
|
462
500
|
statusInFlight.set(key, p);
|
|
463
501
|
return p;
|
|
464
502
|
}
|
|
465
503
|
|
|
466
|
-
async function statusOnce(path: string, key: string): Promise<GitStatus> {
|
|
504
|
+
async function statusOnce(path: string, key: string, priority: "high" | "low"): Promise<GitStatus> {
|
|
467
505
|
const out: GitStatus = { repo: false };
|
|
468
506
|
try {
|
|
469
507
|
// Reach git at all before trusting any "empty" answer: git missing from
|
|
@@ -477,7 +515,7 @@ async function statusOnce(path: string, key: string): Promise<GitStatus> {
|
|
|
477
515
|
// The pin rides GIT_DIR/GIT_WORK_TREE into every later spawn regardless.
|
|
478
516
|
let pin: RepoPin | undefined;
|
|
479
517
|
try {
|
|
480
|
-
pin = await resolveRepoPin(path);
|
|
518
|
+
pin = await resolveRepoPin(path, priority);
|
|
481
519
|
} catch (e) {
|
|
482
520
|
if (!String((e as Error).message).includes("start dirs")) throw e;
|
|
483
521
|
out.repo = true; // repo presence only — no root, files, branches,
|
|
@@ -486,8 +524,8 @@ async function statusOnce(path: string, key: string): Promise<GitStatus> {
|
|
|
486
524
|
statusFailCache.delete(key);
|
|
487
525
|
return out;
|
|
488
526
|
}
|
|
489
|
-
const g = (args: string[], t?: number) => git(path, args, t, pin);
|
|
490
|
-
const root = pin?.workTree ?? (await git(path, ["rev-parse", "--show-toplevel"])).trim();
|
|
527
|
+
const g = (args: string[], t?: number) => git(path, args, t, pin, priority);
|
|
528
|
+
const root = pin?.workTree ?? (await git(path, ["rev-parse", "--show-toplevel"], GIT_TIMEOUT, pin, priority)).trim();
|
|
491
529
|
out.repo = true;
|
|
492
530
|
out.root = root;
|
|
493
531
|
// Everything after toplevel runs in PARALLEL — status is the long pole
|
|
@@ -569,7 +607,24 @@ async function statusOnce(path: string, key: string): Promise<GitStatus> {
|
|
|
569
607
|
// resolve an identity at all. That is the only case worth warning about.
|
|
570
608
|
out.identityComplete = Boolean(identMatch?.[1]?.trim() && identMatch?.[2]?.trim());
|
|
571
609
|
if (!out.identityComplete) delete out.identity;
|
|
572
|
-
if (remoteP.status === "fulfilled")
|
|
610
|
+
if (remoteP.status === "fulfilled") {
|
|
611
|
+
out.remoteUrl = remoteP.value.trim() || undefined;
|
|
612
|
+
// r10 h2-5: `git remote get-url origin` echoes credential-bearing URLs
|
|
613
|
+
// verbatim (https://user:PAT@forge/…) to every token-holding viewer.
|
|
614
|
+
// Strip userinfo before the value enters status responses and caches.
|
|
615
|
+
if (out.remoteUrl) {
|
|
616
|
+
try {
|
|
617
|
+
const u = new URL(out.remoteUrl);
|
|
618
|
+
if (u.username || u.password) {
|
|
619
|
+
u.username = "";
|
|
620
|
+
u.password = "";
|
|
621
|
+
out.remoteUrl = u.toString();
|
|
622
|
+
}
|
|
623
|
+
} catch {
|
|
624
|
+
/* ssh/scp form: no password syntax */
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
573
628
|
const parsed = parseRemote(out.remoteUrl);
|
|
574
629
|
if (parsed) {
|
|
575
630
|
out.forge = parsed.forge;
|
|
@@ -744,8 +799,10 @@ export async function gitOp(
|
|
|
744
799
|
// operator territory — see resolveRepoPin); the resolved pin rides
|
|
745
800
|
// GIT_DIR/GIT_WORK_TREE into EVERY spawn below, so per-spawn .git discovery
|
|
746
801
|
// never re-runs and the check cannot be raced.
|
|
747
|
-
const pin = await resolveRepoPin(path);
|
|
748
|
-
|
|
802
|
+
const pin = await resolveRepoPin(path, "high");
|
|
803
|
+
// run-11 h2-2: gitOp IS the mutation route — every op here is one click a
|
|
804
|
+
// user is waiting on, so all of its spawns (guard + data) ride the high lane.
|
|
805
|
+
const g = (args: string[], t?: number) => git(path, args, t, pin, "high");
|
|
749
806
|
const ghP = (args: string[], t?: number) => gh(path, args, t, pin);
|
|
750
807
|
switch (op) {
|
|
751
808
|
case "diff":
|
|
@@ -212,6 +212,22 @@ const TRANSCRIPT_HITS_PER_AGENT = 3;
|
|
|
212
212
|
* total cardinality (leader-probe: 2 conversations → 5 hits, linear in forks). */
|
|
213
213
|
const TRANSCRIPT_HITS_PER_SESSION = 12;
|
|
214
214
|
|
|
215
|
+
/** run-11 h2-4: the missing-dir memo's TTL. An operator who manually copies a
|
|
216
|
+
* zombie row's sessions/<id> dir back has NO API touch to clear the memo, so
|
|
217
|
+
* a Set<string> hid the session from scope=all search for the process
|
|
218
|
+
* lifetime. Memoize-at timestamp + lazy expiry on read: the row re-enters
|
|
219
|
+
* search ≤5 min later; a re-probe that still ENOENTs re-memoizes with a fresh
|
|
220
|
+
* stamp, so nothing polls more often than one open attempt per 5 min. */
|
|
221
|
+
const MISSING_DIR_TTL_MS = 5 * 60_000;
|
|
222
|
+
|
|
223
|
+
/** run-11 h2-4 test seam: the missing-dir memo's clock, injectable like
|
|
224
|
+
* git.ts's setGitExecForTests so the 5-min TTL is testable without sleeping
|
|
225
|
+
* (only this memo reads this clock — the store's own Date.now stays real). */
|
|
226
|
+
let missingDirNow: () => number = Date.now;
|
|
227
|
+
export function setMissingDirClockForTests(fn?: () => number) {
|
|
228
|
+
missingDirNow = fn ?? Date.now;
|
|
229
|
+
}
|
|
230
|
+
|
|
215
231
|
/** Scheduler granularity. Schedules are minute-granular, so 30 s is enough and
|
|
216
232
|
* the timer is unref'd: it never holds the process open. */
|
|
217
233
|
const SCHEDULE_TICK_MS = 30_000;
|
|
@@ -838,8 +854,11 @@ export class Store {
|
|
|
838
854
|
});
|
|
839
855
|
// Seed the registry from history: a device that predates projects still
|
|
840
856
|
// shows every root its sessions already live in (idempotent by rootPath).
|
|
857
|
+
// run-11 h2-2: this boot loop is a status FAN-OUT over every row — it
|
|
858
|
+
// rides the low spawn lane so one interactive status never queues
|
|
859
|
+
// behind the whole boot walk.
|
|
841
860
|
for (const row of store.list()) {
|
|
842
|
-
const status = await gitStatus(row.cwd).catch(() => undefined);
|
|
861
|
+
const status = await gitStatus(row.cwd, Date.now(), "low").catch(() => undefined);
|
|
843
862
|
await store.projects
|
|
844
863
|
.ensure({ rootPath: status?.repo ? (status.root ?? row.cwd) : row.cwd, kind: status?.repo ? "git" : "plain" })
|
|
845
864
|
.catch(() => undefined);
|
|
@@ -2003,8 +2022,10 @@ export class Store {
|
|
|
2003
2022
|
// run-9 G (run-8 u1): the pre-open stat failed ENOENT — the session dir
|
|
2004
2023
|
// is gone (zombie row). Memo it so scope=all search stops paying for it;
|
|
2005
2024
|
// any later SUCCESSFUL open un-memos (the dir may come back: restore,
|
|
2006
|
-
// manual copy-back — crash-window variant B).
|
|
2007
|
-
|
|
2025
|
+
// manual copy-back — crash-window variant B). run-11 h2-4: the memo now
|
|
2026
|
+
// carries Date.now() so isMissingDirMemoized can expire it (5 min) —
|
|
2027
|
+
// the manual copy-back case has no open to clear it with.
|
|
2028
|
+
if (!create && (e as NodeJS.ErrnoException)?.code === "ENOENT") this.missingDirSessions.set(row.id, missingDirNow());
|
|
2008
2029
|
throw e;
|
|
2009
2030
|
}
|
|
2010
2031
|
try {
|
|
@@ -3001,7 +3022,9 @@ export class Store {
|
|
|
3001
3022
|
// or on the first failed open) never burns a TRANSCRIPT_SESSION_LIMIT
|
|
3002
3023
|
// slot — skipped BEFORE the budget check, so the slots go to sessions
|
|
3003
3024
|
// whose data is actually present. Un-memoed when the dir comes back.
|
|
3004
|
-
|
|
3025
|
+
// run-11 h2-4: TTL-bounded — the skip lasts at most 5 min per memo,
|
|
3026
|
+
// so a manually restored dir re-enters search on its own.
|
|
3027
|
+
if (this.isMissingDirMemoized(row.id)) continue;
|
|
3005
3028
|
if (sessionsScanned >= TRANSCRIPT_SESSION_LIMIT || entriesScanned >= TRANSCRIPT_ENTRY_BUDGET) {
|
|
3006
3029
|
truncated = true;
|
|
3007
3030
|
break;
|
|
@@ -3262,8 +3285,25 @@ export class Store {
|
|
|
3262
3285
|
* them WITHOUT burning one of the 100 TRANSCRIPT_SESSION_LIMIT slots per
|
|
3263
3286
|
* query (they used to cost a harness-throw + reopen stat attempt AND a
|
|
3264
3287
|
* slot, newest-first). Mirrors the salvage memo's lifecycle: cleared when
|
|
3265
|
-
* the dir is back — any successful open, a completed archive, a restore.
|
|
3266
|
-
|
|
3288
|
+
* the dir is back — any successful open, a completed archive, a restore.
|
|
3289
|
+
* run-11 h2-4: Set → Map<id, memoized-at> and TTL-bounded (5 min, lazy
|
|
3290
|
+
* expiry in isMissingDirMemoized) — a manual dir restore re-enters search
|
|
3291
|
+
* without needing an API touch to clear the memo. */
|
|
3292
|
+
private missingDirSessions = new Map<string, number>();
|
|
3293
|
+
|
|
3294
|
+
/** run-11 h2-4: TTL-bounded read of the missing-dir memo. An entry older
|
|
3295
|
+
* than MISSING_DIR_TTL_MS is dropped on read — the next search re-probes
|
|
3296
|
+
* the row (a still-missing dir ENOENTs and re-memoizes with a fresh
|
|
3297
|
+
* stamp; a restored dir opens and the :2136 delete clears it for good). */
|
|
3298
|
+
private isMissingDirMemoized(id: string): boolean {
|
|
3299
|
+
const at = this.missingDirSessions.get(id);
|
|
3300
|
+
if (at === undefined) return false;
|
|
3301
|
+
if (missingDirNow() - at > MISSING_DIR_TTL_MS) {
|
|
3302
|
+
this.missingDirSessions.delete(id);
|
|
3303
|
+
return false;
|
|
3304
|
+
}
|
|
3305
|
+
return true;
|
|
3306
|
+
}
|
|
3267
3307
|
|
|
3268
3308
|
/**
|
|
3269
3309
|
* R19 (run-5 h1): the salvage memo is per-process so a corrupt session is not
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import { statfsSync } from "node:fs";
|
|
11
11
|
import { cpus, loadavg, uptime as osUptime, freemem, totalmem } from "node:os";
|
|
12
12
|
import { monitorEventLoopDelay } from "node:perf_hooks";
|
|
13
|
+
import { gitSpawnStats } from "./git.js";
|
|
13
14
|
|
|
14
15
|
// --- Event-loop lag (R7 perf lane) -----------------------------------------
|
|
15
16
|
//
|
|
@@ -99,6 +100,10 @@ export interface DeviceVitals {
|
|
|
99
100
|
disk: DiskVitals;
|
|
100
101
|
/** Event-loop lag — the number `loadavg` cannot express: how long ready JS waited to run. */
|
|
101
102
|
eventLoop: EventLoopLagVitals;
|
|
103
|
+
/** Git spawn semaphore (run-11 h2-2): children live now, callers waiting
|
|
104
|
+
* for a slot, and how many waiters sit in the interactive "high" lane —
|
|
105
|
+
* the number that says "a status-batch fan-out is stalling the panel". */
|
|
106
|
+
gitSpawns: { live: number; queued: number; queuedHigh: number };
|
|
102
107
|
pid: number;
|
|
103
108
|
at: number;
|
|
104
109
|
}
|
|
@@ -137,6 +142,7 @@ export function deviceVitals(diskPath: string, startedAt: number, now = Date.now
|
|
|
137
142
|
},
|
|
138
143
|
disk: diskVitals(diskPath),
|
|
139
144
|
eventLoop: eventLoopLag(),
|
|
145
|
+
gitSpawns: gitSpawnStats(),
|
|
140
146
|
pid: process.pid,
|
|
141
147
|
at: now,
|
|
142
148
|
};
|