@clagentic/console 1.9.0 → 1.10.0-beta.2
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/LICENSE +117 -12
- package/LICENSES/MIT-legacy.txt +22 -0
- package/LICENSES/README.md +15 -0
- package/README.md +9 -3
- package/bin/cli.js +40 -5
- package/deploy/clagentic-console.service +10 -6
- package/lib/agent-dispatch-tool.js +20 -0
- package/lib/audit.js +3 -2
- package/lib/build-changes.js +79 -0
- package/lib/build-identity.js +192 -0
- package/lib/build-monitor.js +125 -0
- package/lib/build-sha.json +2 -2
- package/lib/build-update-check.js +138 -0
- package/lib/build-update-offer.js +141 -0
- package/lib/build-update-restart.js +142 -0
- package/lib/build-update-standing.js +184 -0
- package/lib/cli/daemon-launch.js +25 -22
- package/lib/cli/ipc-subcommands.js +6 -0
- package/lib/cli/session-subcommands.js +122 -0
- package/lib/cli-sessions.js +8 -1
- package/lib/codex-defaults.js +8 -2
- package/lib/config.js +32 -20
- package/lib/daemon-env-settings.js +84 -0
- package/lib/daemon-projects.js +57 -0
- package/lib/daemon.js +288 -205
- package/lib/drain.js +74 -17
- package/lib/env-compat.js +61 -0
- package/lib/extension-commands.js +45 -0
- package/lib/history-visibility.js +4 -0
- package/lib/idle-adapter-reaper.js +71 -0
- package/lib/link-trap.js +78 -0
- package/lib/mcp-local.js +8 -6
- package/lib/mcp-tool-input.js +116 -0
- package/lib/mcp-tool-schema.js +179 -0
- package/lib/memory-limits.js +61 -5
- package/lib/memory-setting-validate.js +65 -0
- package/lib/memory-shed.js +13 -32
- package/lib/new-work-gate.js +47 -0
- package/lib/os-users.js +5 -3
- package/lib/project-access.js +139 -0
- package/lib/project-connection.js +37 -22
- package/lib/project-external-trigger.js +166 -105
- package/lib/project-file-scope.js +172 -0
- package/lib/project-file-watch.js +86 -61
- package/lib/project-filesystem.js +144 -128
- package/lib/project-git.js +119 -0
- package/lib/project-http.js +44 -28
- package/lib/project-loop.js +17 -9
- package/lib/project-mcp.js +53 -72
- package/lib/project-message-gate.js +70 -0
- package/lib/project-notifications.js +31 -4
- package/lib/project-prompt-responses.js +217 -0
- package/lib/project-sessions.js +192 -355
- package/lib/project-user-message.js +27 -15
- package/lib/project.js +212 -147
- package/lib/projects-dir.js +90 -0
- package/lib/prompt-access.js +22 -0
- package/lib/prompt-kinds/answer-limits.js +15 -0
- package/lib/prompt-kinds/ask-user.js +58 -0
- package/lib/prompt-kinds/codecs.js +97 -0
- package/lib/prompt-kinds/elicitation.js +77 -0
- package/lib/prompt-kinds/extension.js +39 -0
- package/lib/prompt-kinds/index.js +68 -0
- package/lib/prompt-kinds/permission.js +114 -0
- package/lib/prompt-kinds/plan.js +79 -0
- package/lib/prompt-kinds/tool-call.js +38 -0
- package/lib/prompt-registry.js +680 -0
- package/lib/public/app.js +1 -1
- package/lib/public/css/diagnostics.css +80 -0
- package/lib/public/css/input.css +17 -3
- package/lib/public/css/menus.css +10 -0
- package/lib/public/css/messages.css +5 -0
- package/lib/public/css/rewind.css +22 -0
- package/lib/public/index.html +4 -12
- package/lib/public/modules/agent-dispatch-tool.js +9 -0
- package/lib/public/modules/app-connection.js +7 -0
- package/lib/public/modules/app-cursors.js +7 -5
- package/lib/public/modules/app-messages.js +52 -47
- package/lib/public/modules/app-notifications.js +47 -71
- package/lib/public/modules/app-panels.js +61 -59
- package/lib/public/modules/app-projects.js +2 -1
- package/lib/public/modules/app-rendering.js +4 -10
- package/lib/public/modules/build-update-detail.js +54 -0
- package/lib/public/modules/diagnostics.js +170 -20
- package/lib/public/modules/filebrowser.js +18 -16
- package/lib/public/modules/history-visibility.js +4 -0
- package/lib/public/modules/mention.js +3 -2
- package/lib/public/modules/mode-options.js +38 -0
- package/lib/public/modules/project-settings.js +19 -8
- package/lib/public/modules/prompt-banner.js +78 -0
- package/lib/public/modules/prompt-card.js +201 -0
- package/lib/public/modules/prompt-kinds/ask-user-codec.js +131 -0
- package/lib/public/modules/prompt-kinds/ask-user.js +213 -0
- package/lib/public/modules/prompt-kinds/elicitation-codec.js +343 -0
- package/lib/public/modules/prompt-kinds/elicitation.js +176 -0
- package/lib/public/modules/prompt-kinds/index.js +18 -0
- package/lib/public/modules/prompt-kinds/own-key.js +29 -0
- package/lib/public/modules/prompt-kinds/package.json +3 -0
- package/lib/public/modules/prompt-kinds/permission-choices.js +31 -0
- package/lib/public/modules/prompt-kinds/permission.js +209 -0
- package/lib/public/modules/prompt-kinds/plan.js +102 -0
- package/lib/public/modules/prompt-state.js +142 -0
- package/lib/public/modules/prompts.js +134 -0
- package/lib/public/modules/rewind.js +4 -1
- package/lib/public/modules/server-settings.js +2 -4
- package/lib/public/modules/sidebar-mobile.js +19 -9
- package/lib/public/modules/sidebar-sessions.js +21 -14
- package/lib/public/modules/sticky-notes.js +3 -2
- package/lib/public/modules/team-panel.js +5 -4
- package/lib/public/modules/tools.js +81 -906
- package/lib/public/modules/vendor-picker.js +53 -0
- package/lib/public/modules/vendor-registry.js +142 -0
- package/lib/public/sw.js +3 -1
- package/lib/rc-migrate.js +44 -0
- package/lib/runtime-env-keys.js +109 -0
- package/lib/runtime-env.js +236 -0
- package/lib/sdk-bridge.js +348 -284
- package/lib/sdk-message-processor.js +117 -146
- package/lib/sdk-skill-discovery.js +65 -60
- package/lib/server-hub-sessions.js +13 -10
- package/lib/server-palette.js +28 -19
- package/lib/server.js +156 -115
- package/lib/service-install.js +96 -0
- package/lib/session-capabilities.js +76 -0
- package/lib/session-id.js +33 -0
- package/lib/session-input-ipc.js +32 -0
- package/lib/session-input.js +379 -0
- package/lib/session-reclaim.js +284 -0
- package/lib/session-resume.js +172 -0
- package/lib/sessions.js +61 -30
- package/lib/supervisor.js +39 -0
- package/lib/turn-initiator.js +108 -0
- package/lib/users-permissions.js +9 -6
- package/lib/users.js +9 -0
- package/lib/utils.js +4 -2
- package/lib/vendor-auth.js +22 -0
- package/lib/vendor-registry-message.js +66 -0
- package/lib/ws-origin.js +99 -0
- package/lib/ws-schema.js +36 -23
- package/lib/yoke/adapters/claude-capabilities.js +38 -0
- package/lib/yoke/adapters/claude-vendor.js +34 -0
- package/lib/yoke/adapters/claude-worker.js +57 -87
- package/lib/yoke/adapters/claude.js +182 -88
- package/lib/yoke/adapters/codex-capabilities.js +40 -0
- package/lib/yoke/adapters/codex-vendor.js +46 -0
- package/lib/yoke/adapters/codex.js +462 -211
- package/lib/yoke/adapters/effort-control.js +34 -0
- package/lib/yoke/adapters/permission-relay.js +58 -0
- package/lib/yoke/adapters/worker-mcp-servers.js +81 -0
- package/lib/yoke/auth-checks.js +91 -0
- package/lib/yoke/capabilities.js +49 -0
- package/lib/yoke/codex-app-server.js +30 -56
- package/lib/yoke/codex-binary.js +254 -0
- package/lib/yoke/codex-command.js +122 -0
- package/lib/yoke/codex-protocol.js +96 -0
- package/lib/yoke/codex-version.js +135 -0
- package/lib/yoke/env-ownership.js +40 -0
- package/lib/yoke/index.js +84 -174
- package/lib/yoke/install-probe.js +136 -0
- package/lib/yoke/instructions.js +7 -11
- package/lib/yoke/interface.js +206 -4
- package/lib/yoke/package.json +1 -1
- package/lib/yoke/provider-credentials.js +96 -0
- package/lib/yoke/registry.js +377 -0
- package/package.json +12 -9
- package/scripts/check-sdk-lock-drift.js +160 -0
- package/scripts/check-test-count.js +76 -1
- package/scripts/postinstall.js +218 -136
- package/scripts/prompt-harness-history.js +202 -0
- package/scripts/verify-installed-build.js +59 -17
- package/scripts/verify-installed.js +67 -0
- package/lib/sdk-permission-ownership.js +0 -136
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// build-monitor.js — turns repeated build assessments into a confirmed verdict
|
|
4
|
+
// and the journal lines that go with it.
|
|
5
|
+
//
|
|
6
|
+
// It only observes and logs. It does not talk to clients and it does not know
|
|
7
|
+
// whether an offer is shown: the offer is derived from `confirmedStale()` by
|
|
8
|
+
// lib/build-update-standing.js after every poll. Nothing here restarts anything;
|
|
9
|
+
// the one actuator is the operator's confirmed click handled in
|
|
10
|
+
// lib/build-update-restart.js.
|
|
11
|
+
//
|
|
12
|
+
// Rules:
|
|
13
|
+
// - A stale verdict must be seen for the same installed build on
|
|
14
|
+
// `confirmations` consecutive checks before it is confirmed, so a poll that
|
|
15
|
+
// lands in the middle of an install (files half-replaced) never offers a
|
|
16
|
+
// restart for a build that is not there yet.
|
|
17
|
+
// - An inconclusive poll always breaks a streak that is not yet confirmed,
|
|
18
|
+
// even while an older build stays confirmed. It never touches the confirmed
|
|
19
|
+
// verdict: it neither clears it nor counts toward a reminder.
|
|
20
|
+
// - An adopted verdict clears the confirmation.
|
|
21
|
+
// - The journal gets a warning when a streak is first confirmed and a reminder
|
|
22
|
+
// every `reminderEvery` stale checks for the same build, from a counter of
|
|
23
|
+
// its own.
|
|
24
|
+
|
|
25
|
+
var DEFAULT_CONFIRMATIONS = 2;
|
|
26
|
+
// A confirmed stale build is repeated in the journal at this many checks, so a
|
|
27
|
+
// reader of the log sees it without it filling the log every minute.
|
|
28
|
+
var DEFAULT_REMINDER_EVERY = 30;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {object} o
|
|
32
|
+
* @param {function(): object} o.assess — returns an assessBuild() result
|
|
33
|
+
* @param {function(string): void} [o.log]
|
|
34
|
+
* @param {number} [o.confirmations]
|
|
35
|
+
* @param {number} [o.reminderEvery]
|
|
36
|
+
*/
|
|
37
|
+
function createBuildMonitor(o) {
|
|
38
|
+
var confirmations = o.confirmations > 0 ? o.confirmations : DEFAULT_CONFIRMATIONS;
|
|
39
|
+
var reminderEvery = o.reminderEvery > 0 ? o.reminderEvery : DEFAULT_REMINDER_EVERY;
|
|
40
|
+
var log = typeof o.log === 'function' ? o.log : function () {};
|
|
41
|
+
|
|
42
|
+
var pendingKey = null;
|
|
43
|
+
var seen = 0;
|
|
44
|
+
var confirmedKey = null;
|
|
45
|
+
var confirmed = null;
|
|
46
|
+
var sinceConfirm = 0;
|
|
47
|
+
var lastInconclusive = null;
|
|
48
|
+
var current = null;
|
|
49
|
+
|
|
50
|
+
function keyOf(a) { return a.installedBuildSha + ':' + a.installedContentHash; }
|
|
51
|
+
|
|
52
|
+
function check() {
|
|
53
|
+
var a = o.assess();
|
|
54
|
+
current = a;
|
|
55
|
+
|
|
56
|
+
if (a.state === 'inconclusive') {
|
|
57
|
+
// Unknown is not stale and not adopted. Samples on either side of it are
|
|
58
|
+
// not consecutive evidence, so the unconfirmed candidate is always dropped.
|
|
59
|
+
// The confirmed verdict and its reminder counter are not touched.
|
|
60
|
+
pendingKey = null;
|
|
61
|
+
seen = 0;
|
|
62
|
+
if (lastInconclusive !== a.reason) {
|
|
63
|
+
lastInconclusive = a.reason;
|
|
64
|
+
log('Build check inconclusive (' + a.reason + '): ' + a.detail);
|
|
65
|
+
}
|
|
66
|
+
return a;
|
|
67
|
+
}
|
|
68
|
+
lastInconclusive = null;
|
|
69
|
+
|
|
70
|
+
if (a.state === 'adopted') {
|
|
71
|
+
pendingKey = null;
|
|
72
|
+
seen = 0;
|
|
73
|
+
if (confirmedKey !== null) {
|
|
74
|
+
confirmedKey = null;
|
|
75
|
+
confirmed = null;
|
|
76
|
+
sinceConfirm = 0;
|
|
77
|
+
log('The installed build is the one this process loaded.');
|
|
78
|
+
}
|
|
79
|
+
return a;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
var key = keyOf(a);
|
|
83
|
+
if (key === confirmedKey) {
|
|
84
|
+
// Still the confirmed build: keep its latest assessment and count a reminder.
|
|
85
|
+
pendingKey = key;
|
|
86
|
+
seen = confirmations;
|
|
87
|
+
confirmed = a;
|
|
88
|
+
sinceConfirm += 1;
|
|
89
|
+
if (sinceConfirm % reminderEvery === 0) {
|
|
90
|
+
log('Still serving build ' + a.loadedBuildSha + '; build ' + a.installedBuildSha +
|
|
91
|
+
' is installed. The Clagentic: Console UI offers the restart.');
|
|
92
|
+
}
|
|
93
|
+
return a;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (key !== pendingKey) {
|
|
97
|
+
pendingKey = key;
|
|
98
|
+
seen = 1;
|
|
99
|
+
} else {
|
|
100
|
+
seen += 1;
|
|
101
|
+
}
|
|
102
|
+
if (seen >= confirmations) {
|
|
103
|
+
confirmedKey = key;
|
|
104
|
+
confirmed = a;
|
|
105
|
+
sinceConfirm = 0;
|
|
106
|
+
log('WARNING: this process is serving STALE code: ' + a.detail + '.');
|
|
107
|
+
log('WARNING: restarting is your decision. Use "Restart to apply update" in the Clagentic: Console UI, or restart the service yourself.');
|
|
108
|
+
}
|
|
109
|
+
return a;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
check: check,
|
|
114
|
+
/** The last assessment, or null before the first check. */
|
|
115
|
+
current: function () { return current; },
|
|
116
|
+
/**
|
|
117
|
+
* The confirmed stale assessment, or null when none is confirmed. This is
|
|
118
|
+
* the desired offer state: it survives inconclusive polls and is replaced
|
|
119
|
+
* only by adoption or by another confirmed stale build.
|
|
120
|
+
*/
|
|
121
|
+
confirmedStale: function () { return confirmed; },
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module.exports = { createBuildMonitor: createBuildMonitor, DEFAULT_CONFIRMATIONS: DEFAULT_CONFIRMATIONS };
|
package/lib/build-sha.json
CHANGED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// build-update-check.js — lr-e85fec: detects that a newer build has been
|
|
2
|
+
// installed on disk than the one this running daemon process loaded at
|
|
3
|
+
// startup, so the daemon can OFFER the operator a restart instead of the
|
|
4
|
+
// condition sitting invisible until someone remembers to check.
|
|
5
|
+
//
|
|
6
|
+
// This is deliberately detection-only. It never restarts anything itself —
|
|
7
|
+
// see lib/daemon.js's onRestartForBuildUpdate for the operator-initiated
|
|
8
|
+
// actuator, and this module's own header note below for why that split is
|
|
9
|
+
// load-bearing, not incidental.
|
|
10
|
+
//
|
|
11
|
+
// Reuses existing signals rather than inventing new ones:
|
|
12
|
+
// - `loadedBuildSha` (lib/daemon.js): captured ONCE at process startup from
|
|
13
|
+
// lib/build-sha.json — never re-read, so it always reflects what THIS
|
|
14
|
+
// process actually has in its module cache (lr-dc9a3b).
|
|
15
|
+
// - the on-disk lib/build-sha.json file: re-read fresh on every poll here,
|
|
16
|
+
// so it reflects whatever `npm install -g` most recently wrote — the
|
|
17
|
+
// same file scripts/verify-installed-build.js compares against merged
|
|
18
|
+
// HEAD at merge time.
|
|
19
|
+
//
|
|
20
|
+
// A daemon process that started before lib/build-sha.json existed (a dev
|
|
21
|
+
// checkout that never ran `npm pack`) reports loadedBuildSha === null. That
|
|
22
|
+
// is a legitimate "no build-identity signal available" state (see
|
|
23
|
+
// lib/daemon.js's own comment on the same condition) — never surfaced as a
|
|
24
|
+
// staleness diagnostic, since there is nothing to compare against.
|
|
25
|
+
|
|
26
|
+
'use strict';
|
|
27
|
+
|
|
28
|
+
var fs = require('fs');
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Read the current on-disk build-sha.json's `sha` field.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} buildShaPath — absolute path to lib/build-sha.json.
|
|
34
|
+
* @returns {string|null} the SHA, or null if the file is missing/malformed
|
|
35
|
+
* (a dev checkout that has never run `npm pack`, or a filesystem race with
|
|
36
|
+
* an in-progress install write — both are "unknown," not "stale").
|
|
37
|
+
*/
|
|
38
|
+
function readInstalledBuildSha(buildShaPath) {
|
|
39
|
+
try {
|
|
40
|
+
var raw = fs.readFileSync(buildShaPath, 'utf8');
|
|
41
|
+
var parsed = JSON.parse(raw);
|
|
42
|
+
if (parsed && typeof parsed.sha === 'string') return parsed.sha;
|
|
43
|
+
} catch (_) {
|
|
44
|
+
// Missing/unreadable/malformed — treated as "unknown," not "stale."
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Pure comparison: does the on-disk build differ from what this process
|
|
51
|
+
* loaded at startup?
|
|
52
|
+
*
|
|
53
|
+
* @param {object} opts
|
|
54
|
+
* @param {string|null} opts.loadedBuildSha — captured once at daemon startup.
|
|
55
|
+
* @param {string|null} opts.installedBuildSha — freshly read from disk.
|
|
56
|
+
* @returns {{stale: boolean, loadedBuildSha: string|null, installedBuildSha: string|null}}
|
|
57
|
+
* stale is true only when BOTH shas are known non-null strings and they
|
|
58
|
+
* differ — never true from an absent/unreadable signal on either side,
|
|
59
|
+
* matching lib/daemon.js's own "no signal" (never guess) discipline for
|
|
60
|
+
* loadedBuildSha above.
|
|
61
|
+
*/
|
|
62
|
+
function checkForNewerInstalledBuild(opts) {
|
|
63
|
+
var o = opts || {};
|
|
64
|
+
var loaded = typeof o.loadedBuildSha === 'string' ? o.loadedBuildSha : null;
|
|
65
|
+
var installed = typeof o.installedBuildSha === 'string' ? o.installedBuildSha : null;
|
|
66
|
+
return {
|
|
67
|
+
stale: !!(loaded && installed && loaded !== installed),
|
|
68
|
+
loadedBuildSha: loaded,
|
|
69
|
+
installedBuildSha: installed,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Bounded wait for in-flight sessions to finish, then invoke `onIdle()`.
|
|
75
|
+
* Extracted out of lib/daemon.js's onRestartForBuildUpdate so this timing
|
|
76
|
+
* logic is unit-testable without spinning up a real daemon process (the
|
|
77
|
+
* daemon script itself has no other exported surface — see
|
|
78
|
+
* test/daemon-bootstrap-guard.test.js's subprocess-spawn approach for why
|
|
79
|
+
* that path is reserved for bootstrap-only cases, not routine logic).
|
|
80
|
+
*
|
|
81
|
+
* This is the ONLY thing standing between "operator clicked restart" and
|
|
82
|
+
* "daemon actually restarts" — it never fires without that click (see
|
|
83
|
+
* lib/project-sessions.js's restart_for_build_update handler, which is the
|
|
84
|
+
* sole caller of the onRestartForBuildUpdate that wraps this). It exists so
|
|
85
|
+
* that click does not also mean "drop whatever is running right now" when
|
|
86
|
+
* waiting a bounded amount of time can avoid that.
|
|
87
|
+
*
|
|
88
|
+
* @param {object} opts
|
|
89
|
+
* @param {function(): number} opts.getActiveCount — same shape as
|
|
90
|
+
* drain.js's opts.getActiveCount (lib/sdk-bridge.js's getActiveLiveCount).
|
|
91
|
+
* @param {function(): void} opts.onIdle — called exactly once, either when
|
|
92
|
+
* getActiveCount() reaches <= 0 or when the timeout elapses.
|
|
93
|
+
* @param {number} [opts.timeoutMs] — defaults to DEFAULT_RESTART_WAIT_MS.
|
|
94
|
+
* @param {number} [opts.pollIntervalMs] — defaults to 1000.
|
|
95
|
+
* @param {function(function(): void, number): void} [opts.setTimeoutFn] —
|
|
96
|
+
* injectable for tests (default: a thin wrapper over the global
|
|
97
|
+
* setTimeout). Called as setTimeoutFn(fn, delayMs); must invoke fn with no
|
|
98
|
+
* arguments after that delay, same contract as the global setTimeout.
|
|
99
|
+
* @param {function(): number} [opts.nowMs] — injectable for tests (default
|
|
100
|
+
* Date.now).
|
|
101
|
+
*/
|
|
102
|
+
var DEFAULT_RESTART_WAIT_MS = 60 * 1000;
|
|
103
|
+
|
|
104
|
+
function waitForIdleThenAct(opts) {
|
|
105
|
+
var o = opts || {};
|
|
106
|
+
if (typeof o.getActiveCount !== 'function') {
|
|
107
|
+
throw new Error('waitForIdleThenAct: opts.getActiveCount must be a function');
|
|
108
|
+
}
|
|
109
|
+
if (typeof o.onIdle !== 'function') {
|
|
110
|
+
throw new Error('waitForIdleThenAct: opts.onIdle must be a function');
|
|
111
|
+
}
|
|
112
|
+
var timeoutMs = (typeof o.timeoutMs === 'number' && o.timeoutMs > 0) ? o.timeoutMs : DEFAULT_RESTART_WAIT_MS;
|
|
113
|
+
var pollIntervalMs = (typeof o.pollIntervalMs === 'number' && o.pollIntervalMs > 0) ? o.pollIntervalMs : 1000;
|
|
114
|
+
var now = typeof o.nowMs === 'function' ? o.nowMs : Date.now;
|
|
115
|
+
var scheduleFn = typeof o.setTimeoutFn === 'function' ? o.setTimeoutFn : function (fn, delay) { setTimeout(fn, delay); };
|
|
116
|
+
|
|
117
|
+
var deadline = now() + timeoutMs;
|
|
118
|
+
var acted = false;
|
|
119
|
+
|
|
120
|
+
function tick() {
|
|
121
|
+
if (acted) return; // idempotent — a stray extra tick must never double-fire
|
|
122
|
+
if (o.getActiveCount() <= 0 || now() >= deadline) {
|
|
123
|
+
acted = true;
|
|
124
|
+
o.onIdle();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
scheduleFn(tick, pollIntervalMs);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
tick();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
module.exports = {
|
|
134
|
+
readInstalledBuildSha: readInstalledBuildSha,
|
|
135
|
+
checkForNewerInstalledBuild: checkForNewerInstalledBuild,
|
|
136
|
+
waitForIdleThenAct: waitForIdleThenAct,
|
|
137
|
+
DEFAULT_RESTART_WAIT_MS: DEFAULT_RESTART_WAIT_MS,
|
|
138
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// build-update-offer.js — what the operator is told before a restart to adopt
|
|
4
|
+
// an installed build: which build is running, which is installed, and exactly
|
|
5
|
+
// what the restart will end.
|
|
6
|
+
//
|
|
7
|
+
// "Live" is the same notion the shutdown path uses: a session that holds a
|
|
8
|
+
// running agent process (queryInstance or worker). That process dies with the
|
|
9
|
+
// daemon, and with it any background agent the session dispatched.
|
|
10
|
+
|
|
11
|
+
var SESSION_TITLE_MAX = 80;
|
|
12
|
+
var liveAgentTasks = require('./session-reclaim').liveAgentTasks;
|
|
13
|
+
|
|
14
|
+
function oneLine(text, max, fallback) {
|
|
15
|
+
if (typeof text !== 'string') return fallback;
|
|
16
|
+
var line = text.replace(/\s+/g, ' ').trim();
|
|
17
|
+
if (!line) return fallback;
|
|
18
|
+
return line.length > max ? line.slice(0, max - 3) + '...' : line;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isLive(session) {
|
|
22
|
+
return !!(session && (session.queryInstance || session.worker));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Everything a restart now would end, across every project.
|
|
27
|
+
*
|
|
28
|
+
* @param {Array<{slug: string, sm: {sessions: Map}}>} projects
|
|
29
|
+
* @returns {{sessions: Array<object>, sessionCount: number, processingCount: number, agentCount: number, keys: string[]}}
|
|
30
|
+
*/
|
|
31
|
+
function collectInterruptions(projects) {
|
|
32
|
+
var sessions = [];
|
|
33
|
+
var keys = [];
|
|
34
|
+
var agentCount = 0;
|
|
35
|
+
var processingCount = 0;
|
|
36
|
+
(projects || []).forEach(function (proj) {
|
|
37
|
+
if (!proj || !proj.sm || !proj.sm.sessions) return;
|
|
38
|
+
proj.sm.sessions.forEach(function (session) {
|
|
39
|
+
if (!isLive(session)) return;
|
|
40
|
+
var key = proj.slug + '#' + session.localId;
|
|
41
|
+
var agents = liveAgentTasks(session);
|
|
42
|
+
var processing = !!session.isProcessing;
|
|
43
|
+
keys.push(key);
|
|
44
|
+
// Busy state is part of what the operator was shown: a session offered as
|
|
45
|
+
// idle that starts a turn afterwards is not covered.
|
|
46
|
+
if (processing) keys.push(key + '#busy');
|
|
47
|
+
agents.forEach(function (a) { keys.push(key + '#' + a.id); });
|
|
48
|
+
agentCount += agents.length;
|
|
49
|
+
if (processing) processingCount += 1;
|
|
50
|
+
sessions.push({
|
|
51
|
+
key: key,
|
|
52
|
+
slug: proj.slug,
|
|
53
|
+
localId: session.localId,
|
|
54
|
+
title: oneLine(session.title, SESSION_TITLE_MAX, 'Untitled session'),
|
|
55
|
+
processing: processing,
|
|
56
|
+
agents: agents.map(function (a) { return a.label; }),
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
return {
|
|
61
|
+
sessions: sessions,
|
|
62
|
+
sessionCount: sessions.length,
|
|
63
|
+
processingCount: processingCount,
|
|
64
|
+
agentCount: agentCount,
|
|
65
|
+
keys: keys,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** True when every key in `current` was already in `offered`. */
|
|
70
|
+
function coveredBy(current, offered) {
|
|
71
|
+
var have = new Set(offered);
|
|
72
|
+
return current.every(function (k) { return have.has(k); });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function plural(n, word) {
|
|
76
|
+
return n + ' ' + word + (n === 1 ? '' : 's');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The operator-facing statement of what a restart ends. */
|
|
80
|
+
function describeInterruptions(i, waitSeconds) {
|
|
81
|
+
if (i.sessionCount === 0) {
|
|
82
|
+
return 'No session is live, so the restart ends nothing that is running.';
|
|
83
|
+
}
|
|
84
|
+
var lines = [
|
|
85
|
+
'Restarting Clagentic: Console now ends ' + plural(i.sessionCount, 'live session') +
|
|
86
|
+
(i.agentCount > 0 ? ' and ' + plural(i.agentCount, 'background agent') : '') + '.',
|
|
87
|
+
];
|
|
88
|
+
i.sessions.forEach(function (s) {
|
|
89
|
+
var state = s.processing ? 'running a turn' : 'idle';
|
|
90
|
+
var agents = s.agents.length > 0
|
|
91
|
+
? '; background agents: ' + s.agents.map(function (l) { return '"' + l + '"'; }).join(', ')
|
|
92
|
+
: '';
|
|
93
|
+
lines.push('- [' + s.slug + '] ' + s.title + ' (' + state + agents + ')');
|
|
94
|
+
});
|
|
95
|
+
lines.push('Running turns and agents get up to ' + waitSeconds + ' seconds to finish first. ' +
|
|
96
|
+
'Ended sessions resume when you send them a message.');
|
|
97
|
+
return lines.join('\n');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The offer shown for a stale build. `interruptions` is included only for the
|
|
102
|
+
* admin who is about to confirm; the version that goes to every connected
|
|
103
|
+
* client leaves it out because session titles and agent names are session
|
|
104
|
+
* content.
|
|
105
|
+
*/
|
|
106
|
+
function buildOffer(o) {
|
|
107
|
+
var a = o.assessment;
|
|
108
|
+
var offer = {
|
|
109
|
+
loadedBuildSha: a.loadedBuildSha,
|
|
110
|
+
installedBuildSha: a.installedBuildSha,
|
|
111
|
+
reason: a.reason,
|
|
112
|
+
changes: o.changes || { available: false, commits: [], truncated: false, reason: null },
|
|
113
|
+
};
|
|
114
|
+
if (o.interruptions) {
|
|
115
|
+
offer.interruptions = {
|
|
116
|
+
sessions: o.interruptions.sessions,
|
|
117
|
+
sessionCount: o.interruptions.sessionCount,
|
|
118
|
+
agentCount: o.interruptions.agentCount,
|
|
119
|
+
};
|
|
120
|
+
offer.interruptionText = describeInterruptions(o.interruptions, o.waitSeconds);
|
|
121
|
+
}
|
|
122
|
+
if (o.token) offer.token = o.token;
|
|
123
|
+
return offer;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** One line naming both builds, for the notice and the log. */
|
|
127
|
+
function summarizeOffer(a) {
|
|
128
|
+
var short = function (s) { return s ? s.slice(0, 12) : '(unknown)'; };
|
|
129
|
+
return a.reason === 'content'
|
|
130
|
+
? 'Build ' + short(a.installedBuildSha) + ' is installed with different files than the running process loaded.'
|
|
131
|
+
: 'Running build ' + short(a.loadedBuildSha) + '; installed build ' + short(a.installedBuildSha) + '.';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = {
|
|
135
|
+
collectInterruptions: collectInterruptions,
|
|
136
|
+
coveredBy: coveredBy,
|
|
137
|
+
describeInterruptions: describeInterruptions,
|
|
138
|
+
buildOffer: buildOffer,
|
|
139
|
+
summarizeOffer: summarizeOffer,
|
|
140
|
+
liveAgentTasks: liveAgentTasks,
|
|
141
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// build-update-restart.js — the only path from "a newer build is installed" to
|
|
4
|
+
// "the daemon restarts", and it starts only from an operator's confirmed click.
|
|
5
|
+
//
|
|
6
|
+
// Two steps, both initiated by the operator over the admin WebSocket:
|
|
7
|
+
// 1. request() with no token answers with the offer: both builds, the commits
|
|
8
|
+
// between them, and the live sessions and background agents a restart now
|
|
9
|
+
// would end. It restarts nothing.
|
|
10
|
+
// 2. request(token) with the token from that offer. The restart is scheduled
|
|
11
|
+
// only when the offer is still true: the installed build is the one
|
|
12
|
+
// offered, and nothing is live now that the operator was not shown. If
|
|
13
|
+
// anything changed, a fresh offer is returned instead and the operator
|
|
14
|
+
// confirms again.
|
|
15
|
+
// A running turn is given a bounded time to finish; when the restart finally
|
|
16
|
+
// fires, it re-checks the same condition and cancels if a session started
|
|
17
|
+
// after the operator confirmed.
|
|
18
|
+
|
|
19
|
+
var crypto = require('crypto');
|
|
20
|
+
|
|
21
|
+
var { collectInterruptions, coveredBy, buildOffer } = require('./build-update-offer');
|
|
22
|
+
|
|
23
|
+
var OFFER_TTL_MS = 10 * 60 * 1000;
|
|
24
|
+
var MAX_OUTSTANDING_OFFERS = 20;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {object} o
|
|
28
|
+
* @param {function(): object} o.assess — fresh assessBuild() result
|
|
29
|
+
* @param {function(): Array<{slug: string, sm: object}>} o.getProjects
|
|
30
|
+
* @param {function(object): Promise<object>} o.listChanges — ({fromSha, toSha}) -> changes
|
|
31
|
+
* @param {function(): void} o.restart — performs the restart; called only after confirmation
|
|
32
|
+
* @param {function(function, object): void} o.waitForIdleThenAct — build-update-check's
|
|
33
|
+
* @param {function(string): void} o.announce — tells every client why a confirmed restart was cancelled
|
|
34
|
+
* @param {function(string): void} [o.log]
|
|
35
|
+
* @param {number} [o.waitMs] — how long running turns get to finish
|
|
36
|
+
* @param {function(): number} [o.nowMs]
|
|
37
|
+
*/
|
|
38
|
+
function createBuildUpdateRestart(o) {
|
|
39
|
+
var log = typeof o.log === 'function' ? o.log : function () {};
|
|
40
|
+
var now = typeof o.nowMs === 'function' ? o.nowMs : Date.now;
|
|
41
|
+
var waitMs = o.waitMs > 0 ? o.waitMs : 60 * 1000;
|
|
42
|
+
var offers = new Map(); // token -> { keys, installedKey, expires }
|
|
43
|
+
var restarting = false;
|
|
44
|
+
|
|
45
|
+
function installedKeyOf(a) { return a.installedBuildSha + ':' + a.installedContentHash; }
|
|
46
|
+
|
|
47
|
+
function prune() {
|
|
48
|
+
var t = now();
|
|
49
|
+
offers.forEach(function (v, token) { if (v.expires <= t) offers.delete(token); });
|
|
50
|
+
while (offers.size >= MAX_OUTSTANDING_OFFERS) offers.delete(offers.keys().next().value);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function issueOffer(assessment, note) {
|
|
54
|
+
var changes;
|
|
55
|
+
try {
|
|
56
|
+
changes = await o.listChanges({ fromSha: assessment.loadedBuildSha, toSha: assessment.installedBuildSha });
|
|
57
|
+
} catch (e) {
|
|
58
|
+
changes = { available: false, commits: [], truncated: false, reason: 'the change list failed' };
|
|
59
|
+
log('Could not list changes for the build-update offer: ' + (e && e.message ? e.message : e));
|
|
60
|
+
}
|
|
61
|
+
// Reading the change list takes time. The offer reflects the build and the
|
|
62
|
+
// live sessions as they are now, not as they were when it was asked for.
|
|
63
|
+
var fresh = o.assess();
|
|
64
|
+
if (fresh.state !== 'stale' || installedKeyOf(fresh) !== installedKeyOf(assessment)) {
|
|
65
|
+
return { status: 'not_stale', assessment: fresh };
|
|
66
|
+
}
|
|
67
|
+
var interruptions = collectInterruptions(o.getProjects());
|
|
68
|
+
prune();
|
|
69
|
+
var token = crypto.randomBytes(16).toString('hex');
|
|
70
|
+
offers.set(token, {
|
|
71
|
+
keys: interruptions.keys.slice(),
|
|
72
|
+
installedKey: installedKeyOf(assessment),
|
|
73
|
+
expires: now() + OFFER_TTL_MS,
|
|
74
|
+
});
|
|
75
|
+
var offer = buildOffer({
|
|
76
|
+
assessment: assessment,
|
|
77
|
+
changes: changes,
|
|
78
|
+
interruptions: interruptions,
|
|
79
|
+
waitSeconds: Math.round(waitMs / 1000),
|
|
80
|
+
token: token,
|
|
81
|
+
});
|
|
82
|
+
return { status: 'confirm', offer: offer, note: note || null };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function stillCovered(offered, assessment) {
|
|
86
|
+
var a = assessment;
|
|
87
|
+
return a.state === 'stale' && installedKeyOf(a) === offered.installedKey
|
|
88
|
+
&& coveredBy(collectInterruptions(o.getProjects()).keys, offered.keys);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @param {?string} token — the token of an offer the operator was shown
|
|
93
|
+
* @returns {Promise<{status: string, offer?: object, note?: string}>}
|
|
94
|
+
* status: 'not_stale' | 'confirm' | 'restarting'
|
|
95
|
+
*/
|
|
96
|
+
async function request(token) {
|
|
97
|
+
var assessment = o.assess();
|
|
98
|
+
if (assessment.state !== 'stale') {
|
|
99
|
+
return { status: 'not_stale', assessment: assessment };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!token) return issueOffer(assessment, null);
|
|
103
|
+
|
|
104
|
+
var offered = offers.get(token);
|
|
105
|
+
offers.delete(token); // one confirmation per offer
|
|
106
|
+
if (!offered || offered.expires <= now()) {
|
|
107
|
+
return issueOffer(assessment, 'That offer expired. Review the current one.');
|
|
108
|
+
}
|
|
109
|
+
if (!stillCovered(offered, assessment)) {
|
|
110
|
+
return issueOffer(assessment,
|
|
111
|
+
'Something changed since you opened the offer (a build or a live session). Review the current one.');
|
|
112
|
+
}
|
|
113
|
+
if (restarting) return { status: 'restarting' };
|
|
114
|
+
restarting = true;
|
|
115
|
+
log('Operator confirmed restart to adopt build ' + assessment.installedBuildSha +
|
|
116
|
+
'; waiting up to ' + Math.round(waitMs / 1000) + 's for running turns');
|
|
117
|
+
|
|
118
|
+
o.waitForIdleThenAct({
|
|
119
|
+
getActiveCount: function () {
|
|
120
|
+
var i = collectInterruptions(o.getProjects());
|
|
121
|
+
return i.processingCount + i.agentCount;
|
|
122
|
+
},
|
|
123
|
+
timeoutMs: waitMs,
|
|
124
|
+
onIdle: function () {
|
|
125
|
+
var fresh = o.assess();
|
|
126
|
+
if (!stillCovered(offered, fresh)) {
|
|
127
|
+
restarting = false;
|
|
128
|
+
log('Confirmed restart cancelled: the installed build or the live sessions changed after confirmation');
|
|
129
|
+
o.announce('The restart you confirmed was cancelled because the installed build or the live ' +
|
|
130
|
+
'sessions changed while it waited. Nothing was restarted. Open the update offer again to review it.');
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
o.restart();
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
return { status: 'restarting', waitSeconds: Math.round(waitMs / 1000) };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return { request: request };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
module.exports = { createBuildUpdateRestart: createBuildUpdateRestart, OFFER_TTL_MS: OFFER_TTL_MS };
|