shadok-ai 0.19.86 → 0.19.87
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/README.md +12 -0
- package/dist/claude-auto-update.d.ts +74 -0
- package/dist/claude-auto-update.js +88 -0
- package/dist/claude-auto-update.js.map +1 -0
- package/dist/server.js +155 -13
- package/dist/server.js.map +1 -1
- package/package.json +1 -1
- package/public/update-ready.d.ts +7 -0
package/README.md
CHANGED
|
@@ -575,6 +575,17 @@ Enter to skip; you can add it later from the web UI).
|
|
|
575
575
|
agent so a change that does not stick cannot loop. A message you send while
|
|
576
576
|
the respawn runs waits a few seconds for it instead of being refused. Clicking
|
|
577
577
|
still reloads at once; `SHADOK_AUTO_RELOAD=0` turns the automatic part off.
|
|
578
|
+
- **A failed Claude Code self-update is installed for you** — when an agent's
|
|
579
|
+
footer says *Auto-update failed* (typically an agent that runs as a user who
|
|
580
|
+
cannot write the global npm directory), the server checks the registry and,
|
|
581
|
+
if a newer Claude Code exists, installs it once for the whole machine; the
|
|
582
|
+
agents then reload onto it one at a time through the automatic reload above.
|
|
583
|
+
Never two installs at once (one in flight per server, plus a lock file shared
|
|
584
|
+
by every server on the host), nothing retried for an hour after an install
|
|
585
|
+
or 30 minutes after a failure, and an agent still on the previous binary
|
|
586
|
+
counts as "needs a reload", not "needs another install". A footer saying
|
|
587
|
+
*Update installed · Restart to apply* reloads the agent the same way.
|
|
588
|
+
`SHADOK_CLAUDE_AUTO_INSTALL=0` turns it off (the ⚠ button still works).
|
|
578
589
|
- **An agent that stops listening restarts itself** — Claude Code's terminal can
|
|
579
590
|
stop taking input while the process behind it stays alive (an upstream bug;
|
|
580
591
|
nothing on screen shows it). When a message cannot be typed into an agent for
|
|
@@ -1460,6 +1471,7 @@ machine, which silently shifts every daily prompt on a server running in UTC.
|
|
|
1460
1471
|
| `SHADOK_IDLE_MIN` | minutes with no client before a session is reclaimed (60) |
|
|
1461
1472
|
| `SHADOK_AUTO_RELOAD=0` | don't apply a pending "reload to apply" by itself — leave it to the ↻ click |
|
|
1462
1473
|
| `SHADOK_AUTO_RELOAD_IDLE_SEC` | how long an agent must be quiet before that automatic reload (120) |
|
|
1474
|
+
| `SHADOK_CLAUDE_AUTO_INSTALL=0` | don't install a failed Claude Code self-update by itself — leave it to the ⚠ button |
|
|
1463
1475
|
| `SHADOK_SUBMIT_RECOVERY=0` | don't restart an agent whose terminal stopped taking input — report the failed message as an error instead |
|
|
1464
1476
|
| `SHADOK_PERMISSION_MODE` | mode new agents start in (default `acceptEdits`) |
|
|
1465
1477
|
| `SHADOK_AUTOUPDATE=0` | don't apply a newer release by itself (**ON by default** under the supervisor, i.e. `npx shadok-ai`; a server run directly never self-updates, since nothing would respawn it). `0`/`false` turns it off, `1`/`true` forces it on; the GUI setting wins over either once used |
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude Code auto-install: when an agent's TUI footer says
|
|
3
|
+
* "✗ Auto-update failed", install the update server-side, once, by itself.
|
|
4
|
+
*
|
|
5
|
+
* Claude Code updates its own binary, but on many instances it cannot: the
|
|
6
|
+
* global npm directory belongs to root while a privilege-dropped agent runs as
|
|
7
|
+
* its group's user, so its updater fails and the footer says so. The cockpit
|
|
8
|
+
* already had the cure — the badge's "install then reload" button
|
|
9
|
+
* (`POST /claude-update`) — but it waited for a human to notice the badge on
|
|
10
|
+
* each agent. The server sees every agent's screen, so it can do it instead.
|
|
11
|
+
*
|
|
12
|
+
* The reload half needs nothing new: once the binary changes, every agent
|
|
13
|
+
* spawned on the old one carries the `claude` staleness reason, and the
|
|
14
|
+
* auto-reload sweep respawns them one at a time when each has nothing to lose
|
|
15
|
+
* (`src/auto-reload.ts`).
|
|
16
|
+
*
|
|
17
|
+
* What this module decides is WHEN an install is worth starting, and the rule
|
|
18
|
+
* is built so that it can never run twice at once nor loop:
|
|
19
|
+
* - one install at a time — in this process (`inFlight`, the same promise the
|
|
20
|
+
* button uses) and across processes (`externalInstall`: npm's reify dir or
|
|
21
|
+
* our lock file says someone else is installing);
|
|
22
|
+
* - only a footer on an agent RUNNING THE INSTALLED BINARY counts. An agent
|
|
23
|
+
* spawned on an older binary still shows the failed footer until it is
|
|
24
|
+
* reloaded, and the install already happened — that agent needs a reload,
|
|
25
|
+
* not a second install. Without this, every install would be followed by
|
|
26
|
+
* another for as long as one agent stayed un-reloaded;
|
|
27
|
+
* - after an install (or a check that found nothing newer) nothing is
|
|
28
|
+
* retried for `settleMs`, and after a failure for `failureBackoffMs`: a
|
|
29
|
+
* footer that survives the cure (an updater failing for another reason, a
|
|
30
|
+
* phrase an agent merely printed at the bottom of its answer) must not turn
|
|
31
|
+
* into an npm install every 30 seconds.
|
|
32
|
+
*/
|
|
33
|
+
export interface AutoInstallInput {
|
|
34
|
+
/** `SHADOK_CLAUDE_AUTO_INSTALL` is not off. */
|
|
35
|
+
enabled: boolean;
|
|
36
|
+
/** Agents whose footer reads "Auto-update failed" AND that run the binary
|
|
37
|
+
* installed right now (spawn version == current version, or unknown). */
|
|
38
|
+
failingOnCurrent: number;
|
|
39
|
+
/** This process is installing already (button or a previous sweep). */
|
|
40
|
+
inFlight: boolean;
|
|
41
|
+
/** Another process is installing (npm reify dir, or our lock held). */
|
|
42
|
+
externalInstall: boolean;
|
|
43
|
+
/** ms since the last attempt that ended in success or "already latest". */
|
|
44
|
+
sinceLastSettledMs: number;
|
|
45
|
+
/** ms since the last failed attempt. */
|
|
46
|
+
sinceLastFailureMs: number;
|
|
47
|
+
settleMs: number;
|
|
48
|
+
failureBackoffMs: number;
|
|
49
|
+
}
|
|
50
|
+
export type AutoInstallVerdict = {
|
|
51
|
+
install: true;
|
|
52
|
+
} | {
|
|
53
|
+
install: false;
|
|
54
|
+
why: string;
|
|
55
|
+
};
|
|
56
|
+
/** Pure: should the server start a Claude Code install right now? */
|
|
57
|
+
export declare function autoInstallVerdict(i: AutoInstallInput): AutoInstallVerdict;
|
|
58
|
+
/**
|
|
59
|
+
* Pure: does this agent's failed footer call for an INSTALL? Only when it runs
|
|
60
|
+
* the binary installed now — or when either version is unknown, since a failed
|
|
61
|
+
* `--version` probe must not hide a real failure (the time gates still bound it).
|
|
62
|
+
*/
|
|
63
|
+
export declare function failingOnInstalled(spawnVersion: string | null | undefined, currentVersion: string | null): boolean;
|
|
64
|
+
/** Pure: is the published version newer than the installed one? Unknown → assume yes. */
|
|
65
|
+
export declare function newerPublished(installed: string | null, published: string | null): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Pure: is a lock file (written with the time it was taken) still held? A
|
|
68
|
+
* lock older than `staleMs` belongs to an install that died without releasing
|
|
69
|
+
* it — an npm install never takes that long — so it no longer blocks anyone.
|
|
70
|
+
* Age only, never a pid: in a container pids are handed out again (invariant 45).
|
|
71
|
+
*/
|
|
72
|
+
export declare function lockHeld(takenAtMs: number | null, now: number, staleMs: number): boolean;
|
|
73
|
+
/** `SHADOK_CLAUDE_AUTO_INSTALL=0` turns it off; anything else (or unset) leaves it on. */
|
|
74
|
+
export declare function autoInstallEnabled(env: string | undefined): boolean;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude Code auto-install: when an agent's TUI footer says
|
|
3
|
+
* "✗ Auto-update failed", install the update server-side, once, by itself.
|
|
4
|
+
*
|
|
5
|
+
* Claude Code updates its own binary, but on many instances it cannot: the
|
|
6
|
+
* global npm directory belongs to root while a privilege-dropped agent runs as
|
|
7
|
+
* its group's user, so its updater fails and the footer says so. The cockpit
|
|
8
|
+
* already had the cure — the badge's "install then reload" button
|
|
9
|
+
* (`POST /claude-update`) — but it waited for a human to notice the badge on
|
|
10
|
+
* each agent. The server sees every agent's screen, so it can do it instead.
|
|
11
|
+
*
|
|
12
|
+
* The reload half needs nothing new: once the binary changes, every agent
|
|
13
|
+
* spawned on the old one carries the `claude` staleness reason, and the
|
|
14
|
+
* auto-reload sweep respawns them one at a time when each has nothing to lose
|
|
15
|
+
* (`src/auto-reload.ts`).
|
|
16
|
+
*
|
|
17
|
+
* What this module decides is WHEN an install is worth starting, and the rule
|
|
18
|
+
* is built so that it can never run twice at once nor loop:
|
|
19
|
+
* - one install at a time — in this process (`inFlight`, the same promise the
|
|
20
|
+
* button uses) and across processes (`externalInstall`: npm's reify dir or
|
|
21
|
+
* our lock file says someone else is installing);
|
|
22
|
+
* - only a footer on an agent RUNNING THE INSTALLED BINARY counts. An agent
|
|
23
|
+
* spawned on an older binary still shows the failed footer until it is
|
|
24
|
+
* reloaded, and the install already happened — that agent needs a reload,
|
|
25
|
+
* not a second install. Without this, every install would be followed by
|
|
26
|
+
* another for as long as one agent stayed un-reloaded;
|
|
27
|
+
* - after an install (or a check that found nothing newer) nothing is
|
|
28
|
+
* retried for `settleMs`, and after a failure for `failureBackoffMs`: a
|
|
29
|
+
* footer that survives the cure (an updater failing for another reason, a
|
|
30
|
+
* phrase an agent merely printed at the bottom of its answer) must not turn
|
|
31
|
+
* into an npm install every 30 seconds.
|
|
32
|
+
*/
|
|
33
|
+
/** Pure: should the server start a Claude Code install right now? */
|
|
34
|
+
export function autoInstallVerdict(i) {
|
|
35
|
+
const no = (why) => ({ install: false, why });
|
|
36
|
+
if (!i.enabled)
|
|
37
|
+
return no("disabled");
|
|
38
|
+
if (i.failingOnCurrent <= 0)
|
|
39
|
+
return no("no agent reports a failed update on the installed binary");
|
|
40
|
+
if (i.inFlight)
|
|
41
|
+
return no("an install is already running here");
|
|
42
|
+
if (i.externalInstall)
|
|
43
|
+
return no("another process is installing");
|
|
44
|
+
if (i.sinceLastSettledMs < i.settleMs)
|
|
45
|
+
return no("installed or checked recently");
|
|
46
|
+
if (i.sinceLastFailureMs < i.failureBackoffMs)
|
|
47
|
+
return no("last install failed recently");
|
|
48
|
+
return { install: true };
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Pure: does this agent's failed footer call for an INSTALL? Only when it runs
|
|
52
|
+
* the binary installed now — or when either version is unknown, since a failed
|
|
53
|
+
* `--version` probe must not hide a real failure (the time gates still bound it).
|
|
54
|
+
*/
|
|
55
|
+
export function failingOnInstalled(spawnVersion, currentVersion) {
|
|
56
|
+
if (!spawnVersion || !currentVersion)
|
|
57
|
+
return true;
|
|
58
|
+
return spawnVersion === currentVersion;
|
|
59
|
+
}
|
|
60
|
+
/** Pure: is the published version newer than the installed one? Unknown → assume yes. */
|
|
61
|
+
export function newerPublished(installed, published) {
|
|
62
|
+
if (!installed || !published)
|
|
63
|
+
return true;
|
|
64
|
+
const a = installed.split(".").map(Number);
|
|
65
|
+
const b = published.split(".").map(Number);
|
|
66
|
+
if (a.some(Number.isNaN) || b.some(Number.isNaN))
|
|
67
|
+
return installed !== published;
|
|
68
|
+
for (let k = 0; k < Math.max(a.length, b.length); k++) {
|
|
69
|
+
const d = (b[k] ?? 0) - (a[k] ?? 0);
|
|
70
|
+
if (d !== 0)
|
|
71
|
+
return d > 0;
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Pure: is a lock file (written with the time it was taken) still held? A
|
|
77
|
+
* lock older than `staleMs` belongs to an install that died without releasing
|
|
78
|
+
* it — an npm install never takes that long — so it no longer blocks anyone.
|
|
79
|
+
* Age only, never a pid: in a container pids are handed out again (invariant 45).
|
|
80
|
+
*/
|
|
81
|
+
export function lockHeld(takenAtMs, now, staleMs) {
|
|
82
|
+
return takenAtMs !== null && now - takenAtMs < staleMs;
|
|
83
|
+
}
|
|
84
|
+
/** `SHADOK_CLAUDE_AUTO_INSTALL=0` turns it off; anything else (or unset) leaves it on. */
|
|
85
|
+
export function autoInstallEnabled(env) {
|
|
86
|
+
return env === undefined || !/^(0|false|off|no)$/i.test(env.trim());
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=claude-auto-update.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claude-auto-update.js","sourceRoot":"","sources":["../src/claude-auto-update.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAsBH,qEAAqE;AACrE,MAAM,UAAU,kBAAkB,CAAC,CAAmB;IACpD,MAAM,EAAE,GAAG,CAAC,GAAW,EAAsB,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;IAC1E,IAAI,CAAC,CAAC,CAAC,OAAO;QAAE,OAAO,EAAE,CAAC,UAAU,CAAC,CAAC;IACtC,IAAI,CAAC,CAAC,gBAAgB,IAAI,CAAC;QAAE,OAAO,EAAE,CAAC,0DAA0D,CAAC,CAAC;IACnG,IAAI,CAAC,CAAC,QAAQ;QAAE,OAAO,EAAE,CAAC,oCAAoC,CAAC,CAAC;IAChE,IAAI,CAAC,CAAC,eAAe;QAAE,OAAO,EAAE,CAAC,+BAA+B,CAAC,CAAC;IAClE,IAAI,CAAC,CAAC,kBAAkB,GAAG,CAAC,CAAC,QAAQ;QAAE,OAAO,EAAE,CAAC,+BAA+B,CAAC,CAAC;IAClF,IAAI,CAAC,CAAC,kBAAkB,GAAG,CAAC,CAAC,gBAAgB;QAAE,OAAO,EAAE,CAAC,8BAA8B,CAAC,CAAC;IACzF,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AAC3B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,YAAuC,EAAE,cAA6B;IACvG,IAAI,CAAC,YAAY,IAAI,CAAC,cAAc;QAAE,OAAO,IAAI,CAAC;IAClD,OAAO,YAAY,KAAK,cAAc,CAAC;AACzC,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,cAAc,CAAC,SAAwB,EAAE,SAAwB;IAC/E,IAAI,CAAC,SAAS,IAAI,CAAC,SAAS;QAAE,OAAO,IAAI,CAAC;IAC1C,MAAM,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC3C,MAAM,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC3C,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,KAAK,SAAS,CAAC;IACjF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACtD,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QACpC,IAAI,CAAC,KAAK,CAAC;YAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,SAAwB,EAAE,GAAW,EAAE,OAAe;IAC7E,OAAO,SAAS,KAAK,IAAI,IAAI,GAAG,GAAG,SAAS,GAAG,OAAO,CAAC;AACzD,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,kBAAkB,CAAC,GAAuB;IACxD,OAAO,GAAG,KAAK,SAAS,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;AACtE,CAAC"}
|
package/dist/server.js
CHANGED
|
@@ -20,11 +20,13 @@ import { OFFER_TOOL, filesFileFor, loadOffers, saveOffers, offerVerdict, withOff
|
|
|
20
20
|
// Same implementation as the web client's preview (plain JS, loaded as is by
|
|
21
21
|
// the browser): one source for reading the in-flight text off the screen.
|
|
22
22
|
import { extractLiveText } from "../public/live-text.js";
|
|
23
|
+
import { claudeUpdateState } from "../public/update-ready.js";
|
|
23
24
|
import { findTransientErrors, newTransientErrors, RETRY_DELAYS_MS } from "./retry.js";
|
|
24
25
|
import { backgroundTasks, screenShowsWork, inputBoxVisible, moveToOption, boxText, SubmitError, IdleTimeoutError, keepWaitingAfterTimeout, idleTimeoutMessage } from "./detect.js";
|
|
25
26
|
import { submitRecoveryVerdict, submitWithRecovery, submitRecoveryEnabled, RECOVERY_COOLDOWN_MS, UnresponsiveError, UNRESPONSIVE_REPORT_TITLE, unresponsiveReportBody, } from "./submit-recovery.js";
|
|
26
27
|
import { busyLatchVerdict, busyLatchLog, BUSY_LATCH_GRACE_MS } from "./busy-latch.js";
|
|
27
28
|
import { autoReloadVerdict, autoReloadEnabled } from "./auto-reload.js";
|
|
29
|
+
import { autoInstallVerdict, autoInstallEnabled, failingOnInstalled, newerPublished, lockHeld } from "./claude-auto-update.js";
|
|
28
30
|
import { PtyPilot } from "./session.js";
|
|
29
31
|
import { ensureClaudeHome, ensureProjectTrusted, getDefaultModel, setDefaultModel, parseClaudeVersion, seedPlanForCwds, settingsPlan, } from "./claude-home.js";
|
|
30
32
|
import { mergeAppendedPrompts } from "./prompt-args.js";
|
|
@@ -7978,22 +7980,19 @@ app.get(["/media/:name", "/media/:key/:name"], (req, res) => {
|
|
|
7978
7980
|
// footer reports "Auto-update failed"; running agents still need a reload
|
|
7979
7981
|
// afterwards to pick up the new binary (the client does that). BROWSER-ONLY: a
|
|
7980
7982
|
// deliberate operator action, not something an agent should trigger for the
|
|
7981
|
-
// whole machine (cf. the profile-guardrail boundary).
|
|
7982
|
-
//
|
|
7983
|
-
|
|
7983
|
+
// whole machine (cf. the profile-guardrail boundary). The server now also does
|
|
7984
|
+
// it by itself (`claudeAutoInstallSweep`); both go through `installClaudeOnce`,
|
|
7985
|
+
// so a click and the sweep can never run two installs.
|
|
7984
7986
|
app.post("/claude-update", async (req, res) => {
|
|
7985
7987
|
if (!requestFromBrowser(req))
|
|
7986
7988
|
return res.status(403).json({ ok: false, error: "browser-only" });
|
|
7987
7989
|
try {
|
|
7988
|
-
|
|
7989
|
-
|
|
7990
|
-
|
|
7990
|
+
// The sweep may have installed it a minute ago: the badge on an agent not
|
|
7991
|
+
// reloaded yet still says "install", and a click must not reinstall.
|
|
7992
|
+
if (!claudeUpdateInFlight && Date.now() - claudeInstallSettledAt < CLAUDE_INSTALL_SETTLE_MS) {
|
|
7993
|
+
return res.json({ ok: true, already: true });
|
|
7991
7994
|
}
|
|
7992
|
-
await
|
|
7993
|
-
// The cached binary path rots on any reinstall (invariant 32): drop it so the
|
|
7994
|
-
// next spawn re-resolves the freshly installed launcher.
|
|
7995
|
-
claudeReady = null;
|
|
7996
|
-
rememberClaudeBin(null);
|
|
7995
|
+
await installClaudeOnce("button");
|
|
7997
7996
|
console.log("claude-update: install ok");
|
|
7998
7997
|
res.json({ ok: true });
|
|
7999
7998
|
}
|
|
@@ -8003,6 +8002,138 @@ app.post("/claude-update", async (req, res) => {
|
|
|
8003
8002
|
res.status(500).json({ ok: false, error });
|
|
8004
8003
|
}
|
|
8005
8004
|
});
|
|
8005
|
+
// ── Claude Code auto-install (see src/claude-auto-update.ts) ─────────────────
|
|
8006
|
+
const CLAUDE_AUTO_INSTALL = autoInstallEnabled(process.env.SHADOK_CLAUDE_AUTO_INSTALL);
|
|
8007
|
+
const CLAUDE_INSTALL_SETTLE_MS = 60 * 60_000;
|
|
8008
|
+
const CLAUDE_INSTALL_BACKOFF_MS = 30 * 60_000;
|
|
8009
|
+
const CLAUDE_INSTALL_LOCK_STALE_MS = 20 * 60_000;
|
|
8010
|
+
let claudeUpdateInFlight = null;
|
|
8011
|
+
let claudeInstallSettledAt = 0;
|
|
8012
|
+
let claudeInstallFailedAt = 0;
|
|
8013
|
+
/** Cross-process half of the single-flight: two shadok servers on one host share
|
|
8014
|
+
* the global npm directory, and two concurrent `npm i -g` collide (ENOTEMPTY).
|
|
8015
|
+
* `wx` creates the file only if absent — the one atomic test-and-set the fs has. */
|
|
8016
|
+
function claudeInstallLockFile() {
|
|
8017
|
+
return path.join(os.homedir(), ".shadok-ai", "claude-install.lock");
|
|
8018
|
+
}
|
|
8019
|
+
function readClaudeInstallLock() {
|
|
8020
|
+
try {
|
|
8021
|
+
const t = Number(fs.readFileSync(claudeInstallLockFile(), "utf8").trim());
|
|
8022
|
+
return Number.isFinite(t) ? t : 0;
|
|
8023
|
+
}
|
|
8024
|
+
catch {
|
|
8025
|
+
return null;
|
|
8026
|
+
}
|
|
8027
|
+
}
|
|
8028
|
+
function takeClaudeInstallLock() {
|
|
8029
|
+
const file = claudeInstallLockFile();
|
|
8030
|
+
if (lockHeld(readClaudeInstallLock(), Date.now(), CLAUDE_INSTALL_LOCK_STALE_MS))
|
|
8031
|
+
return false;
|
|
8032
|
+
try {
|
|
8033
|
+
fs.rmSync(file, { force: true });
|
|
8034
|
+
}
|
|
8035
|
+
catch { /* a stale lock we cannot remove: wx below fails */ }
|
|
8036
|
+
try {
|
|
8037
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
8038
|
+
fs.writeFileSync(file, String(Date.now()), { flag: "wx" });
|
|
8039
|
+
return true;
|
|
8040
|
+
}
|
|
8041
|
+
catch {
|
|
8042
|
+
return false; // someone took it between our read and our write
|
|
8043
|
+
}
|
|
8044
|
+
}
|
|
8045
|
+
/** THE one way to install Claude Code once it is running: the button, the
|
|
8046
|
+
* auto-install sweep and the missing-binary path all share this promise, and the
|
|
8047
|
+
* lock keeps another process out. The cached binary path rots on any reinstall
|
|
8048
|
+
* (invariant 32), and the version cache would hide the change from the staleness
|
|
8049
|
+
* sweep for a minute: both are dropped on success, so the reload half starts at
|
|
8050
|
+
* once. */
|
|
8051
|
+
function installClaudeOnce(by) {
|
|
8052
|
+
if (claudeUpdateInFlight)
|
|
8053
|
+
return claudeUpdateInFlight;
|
|
8054
|
+
if (!takeClaudeInstallLock())
|
|
8055
|
+
return Promise.reject(new Error("another Claude Code install is already running on this machine"));
|
|
8056
|
+
console.log(`claude-update: installing @anthropic-ai/claude-code (npm i -g) — ${by}`);
|
|
8057
|
+
claudeUpdateInFlight = installClaudeCli()
|
|
8058
|
+
.then(() => {
|
|
8059
|
+
claudeInstallSettledAt = Date.now();
|
|
8060
|
+
claudeReady = null;
|
|
8061
|
+
rememberClaudeBin(null);
|
|
8062
|
+
claudeVerCache = null;
|
|
8063
|
+
})
|
|
8064
|
+
.catch((e) => {
|
|
8065
|
+
claudeInstallFailedAt = Date.now();
|
|
8066
|
+
throw e;
|
|
8067
|
+
})
|
|
8068
|
+
.finally(() => {
|
|
8069
|
+
claudeUpdateInFlight = null;
|
|
8070
|
+
try {
|
|
8071
|
+
fs.rmSync(claudeInstallLockFile(), { force: true });
|
|
8072
|
+
}
|
|
8073
|
+
catch { /* stale after 20 min anyway */ }
|
|
8074
|
+
});
|
|
8075
|
+
return claudeUpdateInFlight;
|
|
8076
|
+
}
|
|
8077
|
+
/** The version npm would install, or null when the registry cannot say. */
|
|
8078
|
+
function publishedClaudeVersion() {
|
|
8079
|
+
return new Promise((resolve) => {
|
|
8080
|
+
const npm = process.platform === "win32" ? "npm.cmd" : "npm";
|
|
8081
|
+
execFile(npm, ["view", "@anthropic-ai/claude-code", "version"], { timeout: 30_000 }, (err, out) => resolve(err ? null : parseClaudeVersion(String(out)) ?? (String(out).trim() || null)));
|
|
8082
|
+
});
|
|
8083
|
+
}
|
|
8084
|
+
/** Every 30 s, beside the auto-reload sweep: an agent's footer says its own
|
|
8085
|
+
* update failed → install it for everyone, once. The reload follows through the
|
|
8086
|
+
* `claude` staleness reason. */
|
|
8087
|
+
let claudeAutoInstallChecking = false;
|
|
8088
|
+
async function claudeAutoInstallSweep() {
|
|
8089
|
+
if (!CLAUDE_AUTO_INSTALL || claudeAutoInstallChecking)
|
|
8090
|
+
return;
|
|
8091
|
+
const channels = loadChannels();
|
|
8092
|
+
let failing = 0;
|
|
8093
|
+
let current;
|
|
8094
|
+
for (const s of sessions.values()) {
|
|
8095
|
+
if (s.pilot.hasExited || claudeUpdateState(s.pilot.screen()) !== "failed")
|
|
8096
|
+
continue;
|
|
8097
|
+
if (current === undefined)
|
|
8098
|
+
current = currentClaudeVersion();
|
|
8099
|
+
const spawnVer = channels.find((c) => c.sessionId === s.id)?.spawnClaudeVer ?? null;
|
|
8100
|
+
if (failingOnInstalled(spawnVer, current))
|
|
8101
|
+
failing++;
|
|
8102
|
+
}
|
|
8103
|
+
const now = Date.now();
|
|
8104
|
+
const verdict = autoInstallVerdict({
|
|
8105
|
+
enabled: CLAUDE_AUTO_INSTALL,
|
|
8106
|
+
failingOnCurrent: failing,
|
|
8107
|
+
inFlight: !!claudeUpdateInFlight,
|
|
8108
|
+
externalInstall: claudeInstallInProgress() || lockHeld(readClaudeInstallLock(), now, CLAUDE_INSTALL_LOCK_STALE_MS),
|
|
8109
|
+
sinceLastSettledMs: now - claudeInstallSettledAt,
|
|
8110
|
+
sinceLastFailureMs: now - claudeInstallFailedAt,
|
|
8111
|
+
settleMs: CLAUDE_INSTALL_SETTLE_MS,
|
|
8112
|
+
failureBackoffMs: CLAUDE_INSTALL_BACKOFF_MS,
|
|
8113
|
+
});
|
|
8114
|
+
if (!verdict.install)
|
|
8115
|
+
return;
|
|
8116
|
+
claudeAutoInstallChecking = true;
|
|
8117
|
+
try {
|
|
8118
|
+
// A footer is a screen heuristic (invariant 2): ask the registry before
|
|
8119
|
+
// pulling a few hundred MB. Nothing newer → settle, nothing installed.
|
|
8120
|
+
const installed = current ?? currentClaudeVersion();
|
|
8121
|
+
const published = await publishedClaudeVersion();
|
|
8122
|
+
if (!newerPublished(installed, published)) {
|
|
8123
|
+
claudeInstallSettledAt = Date.now();
|
|
8124
|
+
console.log(`claude-update: ${failing} agent(s) report a failed auto-update, but ${installed} is already the latest — nothing to install`);
|
|
8125
|
+
return;
|
|
8126
|
+
}
|
|
8127
|
+
await installClaudeOnce(`auto: ${failing} agent(s) report a failed auto-update (${installed ?? "?"} → ${published ?? "latest"})`);
|
|
8128
|
+
console.log(`claude-update: auto-install ok (now ${currentClaudeVersion() ?? "?"}) — idle agents reload onto it one at a time`);
|
|
8129
|
+
}
|
|
8130
|
+
catch (e) {
|
|
8131
|
+
console.log(`claude-update: auto-install FAILED — ${e instanceof Error ? e.message : e} (next try in ${CLAUDE_INSTALL_BACKOFF_MS / 60_000} min)`);
|
|
8132
|
+
}
|
|
8133
|
+
finally {
|
|
8134
|
+
claudeAutoInstallChecking = false;
|
|
8135
|
+
}
|
|
8136
|
+
}
|
|
8006
8137
|
// The shared-ledger table — ONE per instance (keyed by the launch dir). Read by
|
|
8007
8138
|
// the GUI viewer (the version-menu / header button) AND by the `shadok-ledger`
|
|
8008
8139
|
// CLI's `check`/`list` (which run their own recall over these rows). Filtered by
|
|
@@ -9347,6 +9478,12 @@ function pendingReloadReasons(s, channels) {
|
|
|
9347
9478
|
const out = sessionStaleness(s, channels).map((r) => r.kind);
|
|
9348
9479
|
if ((s.appliedProfile ?? null) !== (s.profile ?? null))
|
|
9349
9480
|
out.push("profile");
|
|
9481
|
+
// Claude Code's own footer: "Update installed · Restart to apply". Its updater
|
|
9482
|
+
// may have written a binary the server's `claude --version` never sees (a
|
|
9483
|
+
// per-user install), so the `claude` reason above can miss it. The backoff in
|
|
9484
|
+
// autoReloadVerdict keeps a footer that survives the reload from looping.
|
|
9485
|
+
if (!out.includes("claude") && !s.pilot.hasExited && claudeUpdateState(s.pilot.screen()) === "restart")
|
|
9486
|
+
out.push("claude-restart");
|
|
9350
9487
|
return out;
|
|
9351
9488
|
}
|
|
9352
9489
|
async function autoReloadSweep() {
|
|
@@ -9594,7 +9731,7 @@ function ensureClaudeOnce(revalidate = true) {
|
|
|
9594
9731
|
// transient, so failing hard on the first sample would report a healthy
|
|
9595
9732
|
// install as broken. Nothing here runs at boot.
|
|
9596
9733
|
find: () => findClaudeBinWithRetry(liveClaudeDeps()),
|
|
9597
|
-
install:
|
|
9734
|
+
install: () => installClaudeOnce("missing binary"),
|
|
9598
9735
|
// Claude Code's auto-updater reinstalls the package in place; installing
|
|
9599
9736
|
// over it is how a fresh instance's first agent died on ENOTEMPTY.
|
|
9600
9737
|
installInProgress: claudeInstallInProgress,
|
|
@@ -12404,7 +12541,12 @@ server.listen(port, HOST, () => {
|
|
|
12404
12541
|
staleTimer.unref();
|
|
12405
12542
|
// Auto-reload: twice a minute, respawn (at most) one idle agent that is
|
|
12406
12543
|
// behind — the "reload to apply" badge applying itself. See autoReloadSweep.
|
|
12407
|
-
|
|
12544
|
+
// The install half first: a failed Claude Code self-update on any agent is
|
|
12545
|
+
// installed once, server-side, and the reload sweep then picks it up.
|
|
12546
|
+
const autoReloadTimer = setInterval(() => {
|
|
12547
|
+
void claudeAutoInstallSweep().catch(() => { });
|
|
12548
|
+
void autoReloadSweep().catch(() => { });
|
|
12549
|
+
}, 30_000);
|
|
12408
12550
|
autoReloadTimer.unref();
|
|
12409
12551
|
// The lead agent, if this instance has none. Deferred a beat so it dials a
|
|
12410
12552
|
// server that is already accepting, and never awaited: a cockpit that starts
|