@clagentic/console 1.10.0 → 1.11.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/README.md +14 -1
- package/bin/cli.js +38 -16
- package/lib/agents.js +1 -55
- package/lib/bash-auto-approve.js +133 -0
- package/lib/build-identity.js +23 -5
- package/lib/build-sha.json +2 -2
- package/lib/build-update-check.js +0 -62
- package/lib/build-update-offer.js +23 -13
- package/lib/build-update-restart.js +59 -43
- package/lib/cli/arg-validation.js +50 -0
- package/lib/cli/daemon-launch.js +5 -4
- package/lib/cli/menus.js +2 -2
- package/lib/cli/schedule-input-subcommands.js +327 -0
- package/lib/cli/session-subcommands.js +101 -17
- package/lib/client-build-id.js +56 -0
- package/lib/daemon.js +38 -6
- package/lib/identity/index.js +9 -4
- package/lib/injected-sender.js +225 -0
- package/lib/message-sender-record.js +133 -0
- package/lib/pending-prompts.js +55 -0
- package/lib/permission-mode-policy.js +63 -0
- package/lib/permission-settings.js +96 -0
- package/lib/permission-summary.js +67 -0
- package/lib/project-connection.js +10 -11
- package/lib/project-external-trigger.js +9 -1
- package/lib/project-loop.js +50 -23
- package/lib/project-permission-settings.js +59 -0
- package/lib/project-prompt-responses.js +25 -15
- package/lib/project-sessions.js +164 -60
- package/lib/project-user-message.js +18 -47
- package/lib/project.js +46 -74
- package/lib/public/app.js +4 -7
- package/lib/public/css/channel-layout.css +20 -0
- package/lib/public/css/diagnostics.css +7 -0
- package/lib/public/css/input.css +78 -0
- package/lib/public/css/menus.css +17 -0
- package/lib/public/css/overlays.css +0 -3
- package/lib/public/css/scheduler-modal.css +5 -0
- package/lib/public/index.html +44 -8
- package/lib/public/modules/app-connection.js +18 -1
- package/lib/public/modules/app-loop-ui.js +11 -1
- package/lib/public/modules/app-messages.js +23 -30
- package/lib/public/modules/app-panels.js +10 -3
- package/lib/public/modules/app-rate-limit.js +64 -88
- package/lib/public/modules/app-rendering.js +37 -6
- package/lib/public/modules/approve-tools-toggle.js +33 -0
- package/lib/public/modules/at-agents.js +8 -0
- package/lib/public/modules/build-guard.js +61 -0
- package/lib/public/modules/build-update-detail.js +1 -4
- package/lib/public/modules/diagnostics.js +98 -9
- package/lib/public/modules/injected-identity.js +78 -0
- package/lib/public/modules/injected-label.js +62 -0
- package/lib/public/modules/input.js +64 -114
- package/lib/public/modules/loop-hooks-warning.js +20 -0
- package/lib/public/modules/mode-options.js +35 -16
- package/lib/public/modules/permission-settings.js +56 -0
- package/lib/public/modules/project-settings.js +3 -0
- package/lib/public/modules/rate-limit-state.js +47 -22
- package/lib/public/modules/schedule-panel.js +234 -0
- package/lib/public/modules/schedule-time.js +193 -0
- package/lib/public/modules/scheduled-input-actions.js +19 -0
- package/lib/public/modules/scheduler-config.js +12 -0
- package/lib/public/modules/scheduler.js +8 -0
- package/lib/public/modules/server-settings.js +5 -9
- package/lib/public/modules/settings-defaults.js +18 -4
- package/lib/runtime-env.js +4 -0
- package/lib/scheduled-input/ipc.js +92 -0
- package/lib/scheduled-input/rate-limit-resume.js +53 -0
- package/lib/scheduled-input/records.js +350 -0
- package/lib/scheduled-input/service.js +743 -0
- package/lib/scheduled-input/store.js +132 -0
- package/lib/scheduled-input/ws.js +194 -0
- package/lib/scheduler.js +4 -0
- package/lib/sdk-bridge.js +194 -175
- package/lib/sdk-message-processor.js +13 -5
- package/lib/sdk-skill-discovery.js +17 -0
- package/lib/sender-ref.js +109 -0
- package/lib/server.js +37 -11
- package/lib/session-directory.js +224 -0
- package/lib/session-file-scan.js +55 -0
- package/lib/session-input-ipc.js +21 -0
- package/lib/session-input.js +396 -23
- package/lib/session-name-holder.js +82 -0
- package/lib/session-resume.js +3 -0
- package/lib/sessions.js +111 -2
- package/lib/turn-initiator.js +6 -2
- package/lib/updater.js +8 -2
- package/lib/ws-schema.js +20 -9
- package/lib/yoke/adapters/claude-worker.js +3 -1
- package/lib/yoke/adapters/claude.js +13 -17
- package/lib/yoke/adapters/codex.js +18 -5
- package/lib/yoke/adapters/permission-relay.js +9 -0
- package/lib/yoke/interface.js +7 -1
- package/package.json +1 -1
- package/scripts/check-test-count.js +1 -1
- package/scripts/install-local-test.js +45 -1
- package/scripts/postinstall.js +1 -0
|
@@ -7,18 +7,18 @@
|
|
|
7
7
|
// 1. request() with no token answers with the offer: both builds, the commits
|
|
8
8
|
// between them, and the live sessions and background agents a restart now
|
|
9
9
|
// would end. It restarts nothing.
|
|
10
|
-
// 2. request(token) with the token from that offer.
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
10
|
+
// 2. request(token) with the token from that offer. A known, unexpired,
|
|
11
|
+
// unspent token schedules the restart. The confirm card is the consent and
|
|
12
|
+
// says that sessions and turns starting before the restart end with it, so
|
|
13
|
+
// a changed session set is journaled and does not refuse the click.
|
|
14
|
+
// The confirmed restart fires at once. The second step's card already names the
|
|
15
|
+
// sessions and agents it ends, so that card is the guard; there is no further
|
|
16
|
+
// wait. From the confirm until the process exits the restart is pending and a
|
|
17
|
+
// repeat request returns that state instead of offering again.
|
|
18
18
|
|
|
19
19
|
var crypto = require('crypto');
|
|
20
20
|
|
|
21
|
-
var { collectInterruptions,
|
|
21
|
+
var { collectInterruptions, buildOffer } = require('./build-update-offer');
|
|
22
22
|
|
|
23
23
|
var OFFER_TTL_MS = 10 * 60 * 1000;
|
|
24
24
|
var MAX_OUTSTANDING_OFFERS = 20;
|
|
@@ -29,16 +29,14 @@ var MAX_OUTSTANDING_OFFERS = 20;
|
|
|
29
29
|
* @param {function(): Array<{slug: string, sm: object}>} o.getProjects
|
|
30
30
|
* @param {function(object): Promise<object>} o.listChanges — ({fromSha, toSha}) -> changes
|
|
31
31
|
* @param {function(): void} o.restart — performs the restart; called only after confirmation
|
|
32
|
-
* @param {function(
|
|
33
|
-
*
|
|
32
|
+
* @param {function(): void} [o.onScheduled] — called once, just before the restart
|
|
33
|
+
* is performed, so clients can swap the offer for the restarting state
|
|
34
34
|
* @param {function(string): void} [o.log]
|
|
35
|
-
* @param {number} [o.waitMs] — how long running turns get to finish
|
|
36
35
|
* @param {function(): number} [o.nowMs]
|
|
37
36
|
*/
|
|
38
37
|
function createBuildUpdateRestart(o) {
|
|
39
38
|
var log = typeof o.log === 'function' ? o.log : function () {};
|
|
40
39
|
var now = typeof o.nowMs === 'function' ? o.nowMs : Date.now;
|
|
41
|
-
var waitMs = o.waitMs > 0 ? o.waitMs : 60 * 1000;
|
|
42
40
|
var offers = new Map(); // token -> { keys, installedKey, expires }
|
|
43
41
|
var restarting = false;
|
|
44
42
|
|
|
@@ -76,16 +74,23 @@ function createBuildUpdateRestart(o) {
|
|
|
76
74
|
assessment: assessment,
|
|
77
75
|
changes: changes,
|
|
78
76
|
interruptions: interruptions,
|
|
79
|
-
waitSeconds: Math.round(waitMs / 1000),
|
|
80
77
|
token: token,
|
|
81
78
|
});
|
|
82
79
|
return { status: 'confirm', offer: offer, note: note || null };
|
|
83
80
|
}
|
|
84
81
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
82
|
+
// Names what differs between the offer the operator saw and now, for the journal.
|
|
83
|
+
function describeChange(offered, assessment) {
|
|
84
|
+
var parts = [];
|
|
85
|
+
if (installedKeyOf(assessment) !== offered.installedKey) parts.push('a different build was installed');
|
|
86
|
+
var was = new Set(offered.keys);
|
|
87
|
+
var nowKeys = collectInterruptions(o.getProjects()).keys;
|
|
88
|
+
var is = new Set(nowKeys);
|
|
89
|
+
var added = nowKeys.filter(function (k) { return !was.has(k); });
|
|
90
|
+
var gone = offered.keys.filter(function (k) { return !is.has(k); });
|
|
91
|
+
if (added.length) parts.push('now live and not offered: ' + added.join(', '));
|
|
92
|
+
if (gone.length) parts.push('offered and no longer live: ' + gone.join(', '));
|
|
93
|
+
return parts.join('; ');
|
|
89
94
|
}
|
|
90
95
|
|
|
91
96
|
/**
|
|
@@ -94,6 +99,15 @@ function createBuildUpdateRestart(o) {
|
|
|
94
99
|
* status: 'not_stale' | 'confirm' | 'restarting'
|
|
95
100
|
*/
|
|
96
101
|
async function request(token) {
|
|
102
|
+
// A restart is a one-way door: once confirmed, any further request (a second
|
|
103
|
+
// tab, a card that was still on screen, a replayed frame) gets the pending
|
|
104
|
+
// state back. It never issues another offer, so the operator is not asked to
|
|
105
|
+
// confirm twice, and it never restarts a second time.
|
|
106
|
+
if (restarting) {
|
|
107
|
+
offers.delete(token);
|
|
108
|
+
log('Restart request ignored: a confirmed restart is already pending');
|
|
109
|
+
return { status: 'restarting', already: true };
|
|
110
|
+
}
|
|
97
111
|
var assessment = o.assess();
|
|
98
112
|
if (assessment.state !== 'stale') {
|
|
99
113
|
return { status: 'not_stale', assessment: assessment };
|
|
@@ -103,37 +117,39 @@ function createBuildUpdateRestart(o) {
|
|
|
103
117
|
|
|
104
118
|
var offered = offers.get(token);
|
|
105
119
|
offers.delete(token); // one confirmation per offer
|
|
120
|
+
// A confirmation that restarts nothing must leave a journal line: without
|
|
121
|
+
// one, a click answered by a fresh offer is indistinguishable from a click
|
|
122
|
+
// that never arrived.
|
|
106
123
|
if (!offered || offered.expires <= now()) {
|
|
124
|
+
log('Restart confirmation not applied: the offer was unknown or expired; issuing a fresh one');
|
|
107
125
|
return issueOffer(assessment, 'That offer expired. Review the current one.');
|
|
108
126
|
}
|
|
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
127
|
restarting = true;
|
|
128
|
+
// The confirm card is the consent, and it says sessions and turns that start
|
|
129
|
+
// before the restart end with it. A changed set is journaled, never refused.
|
|
130
|
+
var changed = describeChange(offered, assessment);
|
|
131
|
+
if (changed) log('Restart confirmed after the offer changed (' + changed + '); proceeding');
|
|
115
132
|
log('Operator confirmed restart to adopt build ' + assessment.installedBuildSha +
|
|
116
|
-
';
|
|
133
|
+
'; restarting now');
|
|
117
134
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
return { status: 'restarting', waitSeconds: Math.round(waitMs / 1000) };
|
|
135
|
+
// Everyone's restart card is withdrawn and replaced by the one pending state
|
|
136
|
+
// before the restart begins.
|
|
137
|
+
if (typeof o.onScheduled === 'function') {
|
|
138
|
+
try {
|
|
139
|
+
o.onScheduled();
|
|
140
|
+
} catch (e) {
|
|
141
|
+
log('Could not announce the pending restart: ' + (e && e.message ? e.message : e));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// The pending guard stays set until the process exits. A restart that throws
|
|
145
|
+
// leaves nothing pending, so the operator can ask again.
|
|
146
|
+
try {
|
|
147
|
+
o.restart();
|
|
148
|
+
} catch (e) {
|
|
149
|
+
restarting = false;
|
|
150
|
+
throw e;
|
|
151
|
+
}
|
|
152
|
+
return { status: 'restarting' };
|
|
137
153
|
}
|
|
138
154
|
|
|
139
155
|
return { request: request };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// lib/cli/arg-validation.js
|
|
2
|
+
//
|
|
3
|
+
// Rejects CLI arguments that bin/cli.js's flag loop does not recognise, so an
|
|
4
|
+
// unknown leading word or flag fails with a usage error instead of falling
|
|
5
|
+
// through to daemon control (--shutdown, --restart, ...) or a daemon start.
|
|
6
|
+
// Kept as a pure function so it is testable without spawning the CLI.
|
|
7
|
+
|
|
8
|
+
// Flags that take no value.
|
|
9
|
+
var BOOLEAN_FLAGS = [
|
|
10
|
+
"--no-https", "--local-cert", "--no-update", "--skip-update", "--dev",
|
|
11
|
+
"--watch", "-w", "--debug", "-y", "--yes", "--shutdown", "--restart",
|
|
12
|
+
"--no-restart", "--list", "--activity-diagnostics", "--process-build-status",
|
|
13
|
+
"--headless", "--multi-user", "--os-users",
|
|
14
|
+
// Removed flag: the flag loop prints its own dedicated message and exits 1.
|
|
15
|
+
"--dangerously-skip-permissions",
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
// Flags that consume the next argument as their value, whatever it looks like.
|
|
19
|
+
var VALUE_FLAGS = ["-p", "--port", "--host", "--bind", "--pin", "--add", "--remove"];
|
|
20
|
+
|
|
21
|
+
var HELP_FLAGS = ["-h", "--help"];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Returns the first argument that is neither a known flag nor a flag's
|
|
25
|
+
* consumed value, or null when every argument is recognised. Scanning stops at
|
|
26
|
+
* -h/--help, which prints usage and exits before any later token is acted on.
|
|
27
|
+
*/
|
|
28
|
+
function findUnrecognisedArg(args) {
|
|
29
|
+
for (var i = 0; i < args.length; i++) {
|
|
30
|
+
var arg = args[i];
|
|
31
|
+
if (HELP_FLAGS.indexOf(arg) !== -1) return null;
|
|
32
|
+
if (VALUE_FLAGS.indexOf(arg) !== -1) {
|
|
33
|
+
i++;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (BOOLEAN_FLAGS.indexOf(arg) !== -1) continue;
|
|
37
|
+
return arg;
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function unrecognisedArgMessage(arg) {
|
|
43
|
+
return "clagentic-console: unrecognised argument '" + arg + "'\n" +
|
|
44
|
+
"Run 'clagentic-console --help' for usage.";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = {
|
|
48
|
+
findUnrecognisedArg: findUnrecognisedArg,
|
|
49
|
+
unrecognisedArgMessage: unrecognisedArgMessage,
|
|
50
|
+
};
|
package/lib/cli/daemon-launch.js
CHANGED
|
@@ -243,7 +243,7 @@ async function restartDaemonFromConfig(opts) {
|
|
|
243
243
|
// ==============================
|
|
244
244
|
/**
|
|
245
245
|
* `cliOpts`: { port, useHttps, forceMkcert, debugMode, headlessMode,
|
|
246
|
-
*
|
|
246
|
+
* osUsersMode, cliPin, cwd } — the subset of
|
|
247
247
|
* parsed CLI flags that shape daemon startup. Passed explicitly since this
|
|
248
248
|
* module no longer owns arg parsing (that stays in bin/cli.js).
|
|
249
249
|
*/
|
|
@@ -359,12 +359,13 @@ async function forkDaemon(mode, keepAwake, extraProjects, addCwd, wantOsUsers, c
|
|
|
359
359
|
debug: cliOpts.debugMode,
|
|
360
360
|
headless: cliOpts.headlessMode,
|
|
361
361
|
keepAwake: keepAwake,
|
|
362
|
-
dangerouslySkipPermissions: cliOpts.dangerouslySkipPermissions,
|
|
363
362
|
osUsers: wantOsUsers || cliOpts.osUsersMode,
|
|
364
363
|
mode: mode || "multi",
|
|
365
364
|
setupCompleted: true,
|
|
366
365
|
projects: allProjects,
|
|
367
366
|
});
|
|
367
|
+
// A key left by an older release is dead; drop it when the file is rewritten.
|
|
368
|
+
delete config.dangerouslySkipPermissions;
|
|
368
369
|
|
|
369
370
|
ensureConfigDir();
|
|
370
371
|
saveConfig(config);
|
|
@@ -446,7 +447,7 @@ async function forkDaemon(mode, keepAwake, extraProjects, addCwd, wantOsUsers, c
|
|
|
446
447
|
// Dev mode — foreground daemon with file watching
|
|
447
448
|
// ==============================
|
|
448
449
|
/**
|
|
449
|
-
* `cliOpts`: { port, useHttps,
|
|
450
|
+
* `cliOpts`: { port, useHttps, watchMode,
|
|
450
451
|
* noRestart, cwd }.
|
|
451
452
|
*/
|
|
452
453
|
async function devMode(mode, keepAwake, existingPinHash, wantOsUsers, cliOpts) {
|
|
@@ -544,12 +545,12 @@ async function devMode(mode, keepAwake, existingPinHash, wantOsUsers, cliOpts) {
|
|
|
544
545
|
mkcertDetected: mkcertDetected,
|
|
545
546
|
debug: true,
|
|
546
547
|
keepAwake: keepAwake || false,
|
|
547
|
-
dangerouslySkipPermissions: cliOpts.dangerouslySkipPermissions,
|
|
548
548
|
mode: mode || "multi",
|
|
549
549
|
setupCompleted: true,
|
|
550
550
|
projects: allProjects,
|
|
551
551
|
osUsers: wantOsUsers || (prevDevConfig ? (prevDevConfig.osUsers || false) : false),
|
|
552
552
|
});
|
|
553
|
+
delete config.dangerouslySkipPermissions;
|
|
553
554
|
|
|
554
555
|
ensureConfigDir();
|
|
555
556
|
saveConfig(config);
|
package/lib/cli/menus.js
CHANGED
|
@@ -78,7 +78,7 @@ function promptRestoreProjects(projects, callback) {
|
|
|
78
78
|
// ==============================
|
|
79
79
|
/**
|
|
80
80
|
* `defaultPort` seeds the port prompt (mirrors the CLI's --port / default
|
|
81
|
-
* value). `cliOpts`: { host
|
|
81
|
+
* value). `cliOpts`: { host } — preserved onto
|
|
82
82
|
* the config when OS-user isolation is requested without root, mirroring
|
|
83
83
|
* the original bin/cli.js behavior of reading those from parsed CLI flags.
|
|
84
84
|
*/
|
|
@@ -197,7 +197,7 @@ function setup(callback, defaultPort, cliOpts) {
|
|
|
197
197
|
existingCfg.mode = "multi";
|
|
198
198
|
existingCfg.osUsers = true;
|
|
199
199
|
existingCfg.setupCompleted = true;
|
|
200
|
-
|
|
200
|
+
delete existingCfg.dangerouslySkipPermissions;
|
|
201
201
|
saveConfig(existingCfg);
|
|
202
202
|
log(sym.bar);
|
|
203
203
|
log(sym.warn + " " + a.yellow + "OS user isolation requires root." + a.reset);
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
// lib/cli/schedule-input-subcommands.js
|
|
2
|
+
//
|
|
3
|
+
// `clagentic-console schedule-input create | list | delete`: schedule text to be
|
|
4
|
+
// sent into a console session later, once or on a repeat that always has an end,
|
|
5
|
+
// through the running daemon's IPC socket. The same scheduled input the clock
|
|
6
|
+
// button in the browser makes: a record made here shows in that list, and the
|
|
7
|
+
// reverse. Each verb prints the daemon's result as JSON on stdout.
|
|
8
|
+
//
|
|
9
|
+
// Flag/value pairs only, so each command shape can be allow-listed:
|
|
10
|
+
//
|
|
11
|
+
// schedule-input create --session <id> [--project <slug>]
|
|
12
|
+
// (--in <dur> | --at <time> | --every <dur> (--until <time> | --count <n>))
|
|
13
|
+
// (--prompt <text|-> | --prompt-file <path>)
|
|
14
|
+
// schedule-input list [--session <id>] [--project <slug>] [--state armed|done|ended|any]
|
|
15
|
+
// schedule-input delete --id <id> [--project <slug>]
|
|
16
|
+
//
|
|
17
|
+
// <dur> is a number of minutes or a run of s, m, h and d parts (90, 10m, 1h30m).
|
|
18
|
+
// <time> is a local HH:MM, or a local YYYY-MM-DDTHH:MM; an HH:MM already past
|
|
19
|
+
// today means tomorrow. The times are resolved here, in the caller's own time
|
|
20
|
+
// zone, and the daemon is sent absolute times.
|
|
21
|
+
//
|
|
22
|
+
// Exit codes (the same pattern as open-session and send-input):
|
|
23
|
+
// 0 done (scheduled, listed, deleted)
|
|
24
|
+
// 1 refused, error, or not found
|
|
25
|
+
|
|
26
|
+
var fs = require("fs");
|
|
27
|
+
|
|
28
|
+
var senderRef = require("../sender-ref");
|
|
29
|
+
var { LIMITS, isCount } = require("../scheduled-input/records");
|
|
30
|
+
var { requestDaemon, withSenderRef, EXIT } = require("./session-subcommands");
|
|
31
|
+
|
|
32
|
+
// --- The time inputs: a duration ("10m", "1h30m"), a local clock time ("20:01")
|
|
33
|
+
// and a local date-time ("2026-03-01T20:01"). Pure: each takes the current time
|
|
34
|
+
// as an argument. The browser client carries a small mirror of resolveClockTime
|
|
35
|
+
// (lib/public/modules/schedule-time.js) because it cannot load this CommonJS
|
|
36
|
+
// file; test/scheduled-input-client.test.js feeds both the same cases.
|
|
37
|
+
|
|
38
|
+
var UNIT_MS = Object.freeze({ s: 1000, m: 60000, h: 3600000, d: 86400000 });
|
|
39
|
+
|
|
40
|
+
var DURATION_RE = /^(?:\d+[smhd])+$/;
|
|
41
|
+
var DURATION_PART_RE = /(\d+)([smhd])/g;
|
|
42
|
+
var CLOCK_RE = /^(\d{1,2}):(\d{2})$/;
|
|
43
|
+
var DATE_TIME_RE = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{1,2}):(\d{2})$/;
|
|
44
|
+
|
|
45
|
+
// A bare number is minutes, the unit the person's own field uses.
|
|
46
|
+
// Returns { ms } or { error }.
|
|
47
|
+
function parseDuration(raw) {
|
|
48
|
+
var text = typeof raw === "string" ? raw.trim().toLowerCase() : "";
|
|
49
|
+
if (/^\d+$/.test(text)) text += "m";
|
|
50
|
+
if (!DURATION_RE.test(text)) {
|
|
51
|
+
return { error: "not a duration: " + JSON.stringify(String(raw).slice(0, 32)) + " (use for example 90, 10m, 2h, 1h30m)" };
|
|
52
|
+
}
|
|
53
|
+
var total = 0;
|
|
54
|
+
var part;
|
|
55
|
+
DURATION_PART_RE.lastIndex = 0;
|
|
56
|
+
while ((part = DURATION_PART_RE.exec(text)) !== null) {
|
|
57
|
+
total += parseInt(part[1], 10) * UNIT_MS[part[2]];
|
|
58
|
+
}
|
|
59
|
+
if (!Number.isSafeInteger(total) || total <= 0) return { error: "a duration must be longer than zero" };
|
|
60
|
+
return { ms: total };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function validClock(hours, minutes) {
|
|
64
|
+
return hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// A local clock time resolves to its next occurrence strictly after `now`: a
|
|
68
|
+
// time that has passed today means tomorrow. A full local date-time is taken as
|
|
69
|
+
// given and must lie in the future. Returns { ms, tomorrow } or { error }.
|
|
70
|
+
function resolveClockTime(raw, now) {
|
|
71
|
+
var text = typeof raw === "string" ? raw.trim() : "";
|
|
72
|
+
var clock = CLOCK_RE.exec(text);
|
|
73
|
+
if (clock) {
|
|
74
|
+
var h = parseInt(clock[1], 10);
|
|
75
|
+
var m = parseInt(clock[2], 10);
|
|
76
|
+
if (!validClock(h, m)) return { error: "not a time of day: " + JSON.stringify(text) + " (use HH:MM, 00:00 to 23:59)" };
|
|
77
|
+
var candidate = new Date(now);
|
|
78
|
+
candidate.setHours(h, m, 0, 0);
|
|
79
|
+
var tomorrow = false;
|
|
80
|
+
if (candidate.getTime() <= now) {
|
|
81
|
+
// setDate keeps the wall-clock time across a daylight-saving change.
|
|
82
|
+
candidate.setDate(candidate.getDate() + 1);
|
|
83
|
+
tomorrow = true;
|
|
84
|
+
}
|
|
85
|
+
return { ms: candidate.getTime(), tomorrow: tomorrow };
|
|
86
|
+
}
|
|
87
|
+
var full = DATE_TIME_RE.exec(text);
|
|
88
|
+
if (full) {
|
|
89
|
+
var y = parseInt(full[1], 10);
|
|
90
|
+
var mo = parseInt(full[2], 10);
|
|
91
|
+
var d = parseInt(full[3], 10);
|
|
92
|
+
var hh = parseInt(full[4], 10);
|
|
93
|
+
var mm = parseInt(full[5], 10);
|
|
94
|
+
// The fields are checked as typed: Date() carries an out-of-range minute or
|
|
95
|
+
// hour into the next hour or day, so a check after construction cannot tell
|
|
96
|
+
// 20:99 from 21:39.
|
|
97
|
+
if (!validClock(hh, mm)) return { error: "not a date and time: " + JSON.stringify(text) + " (the time of day must be 00:00 to 23:59)" };
|
|
98
|
+
var when = new Date(y, mo - 1, d, hh, mm, 0, 0);
|
|
99
|
+
// Every field must come back unchanged, which also refuses a day the month
|
|
100
|
+
// lacks and a wall-clock time that a daylight-saving change skips.
|
|
101
|
+
var roundTrips = when.getFullYear() === y && when.getMonth() === mo - 1 && when.getDate() === d
|
|
102
|
+
&& when.getHours() === hh && when.getMinutes() === mm;
|
|
103
|
+
if (!roundTrips) return { error: "not a date and time: " + JSON.stringify(text) };
|
|
104
|
+
if (when.getTime() <= now) return { error: "that date and time has already passed: " + JSON.stringify(text) };
|
|
105
|
+
return { ms: when.getTime(), tomorrow: false };
|
|
106
|
+
}
|
|
107
|
+
return { error: "not a time: " + JSON.stringify(text.slice(0, 32)) + " (use HH:MM, or YYYY-MM-DDTHH:MM, local time)" };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function pad2(n) {
|
|
111
|
+
return (n < 10 ? "0" : "") + n;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// "2026-03-01 20:01" in the host's local time: the unambiguous absolute form
|
|
115
|
+
// printed beside every resolved time.
|
|
116
|
+
function formatLocal(ms) {
|
|
117
|
+
var d = new Date(ms);
|
|
118
|
+
return d.getFullYear() + "-" + pad2(d.getMonth() + 1) + "-" + pad2(d.getDate()) + " " + pad2(d.getHours()) + ":" + pad2(d.getMinutes());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
var SUBCOMMANDS = Object.freeze(["create", "list", "delete"]);
|
|
122
|
+
|
|
123
|
+
var FLAGS = {
|
|
124
|
+
"--session": "sessionId",
|
|
125
|
+
"--project": "projectSlug",
|
|
126
|
+
"--in": "in",
|
|
127
|
+
"--at": "at",
|
|
128
|
+
"--every": "every",
|
|
129
|
+
"--until": "until",
|
|
130
|
+
"--count": "count",
|
|
131
|
+
"--prompt": "prompt",
|
|
132
|
+
"--prompt-file": "promptFile",
|
|
133
|
+
"--id": "id",
|
|
134
|
+
"--state": "state",
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
var ALLOWED = {
|
|
138
|
+
create: ["sessionId", "projectSlug", "in", "at", "every", "until", "count", "prompt", "promptFile"],
|
|
139
|
+
list: ["sessionId", "projectSlug", "state"],
|
|
140
|
+
delete: ["id", "projectSlug"],
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
var USAGE = {
|
|
144
|
+
create: "Usage: clagentic-console schedule-input create --session <id> [--project <slug>] (--in <dur> | --at <time> | --every <dur> (--until <time> | --count <n>)) (--prompt <text|-> | --prompt-file <path>)",
|
|
145
|
+
list: "Usage: clagentic-console schedule-input list [--session <id>] [--project <slug>] [--state armed|done|ended|any]",
|
|
146
|
+
delete: "Usage: clagentic-console schedule-input delete --id <id> [--project <slug>]",
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
var IPC_COMMAND = {
|
|
150
|
+
create: "schedule_input_create",
|
|
151
|
+
list: "schedule_input_list",
|
|
152
|
+
delete: "schedule_input_delete",
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
var COUNT_RE = /^\d+$/;
|
|
156
|
+
|
|
157
|
+
function has(obj, key) {
|
|
158
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function given(parsed, key) {
|
|
162
|
+
return parsed[key] !== undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// The timing flags of create, resolved against `now` (ms). Returns
|
|
166
|
+
// { fields, resolved } or { error }: fields go to the daemon, resolved says what
|
|
167
|
+
// the times came to (absolute, local, and whether a clock time rolled to
|
|
168
|
+
// tomorrow).
|
|
169
|
+
function resolveTiming(parsed, now) {
|
|
170
|
+
var shapes = ["in", "at", "every"].filter(function (k) { return given(parsed, k); });
|
|
171
|
+
if (shapes.length !== 1) return { error: "Give exactly one of --in, --at or --every" };
|
|
172
|
+
var shape = shapes[0];
|
|
173
|
+
var resolved = {};
|
|
174
|
+
|
|
175
|
+
if (shape !== "every") {
|
|
176
|
+
if (given(parsed, "until") || given(parsed, "count")) return { error: "--until and --count belong to --every" };
|
|
177
|
+
if (shape === "in") {
|
|
178
|
+
var dur = parseDuration(parsed.in);
|
|
179
|
+
if (dur.error) return { error: "--in: " + dur.error };
|
|
180
|
+
resolved.at = now + dur.ms;
|
|
181
|
+
} else {
|
|
182
|
+
var when = resolveClockTime(parsed.at, now);
|
|
183
|
+
if (when.error) return { error: "--at: " + when.error };
|
|
184
|
+
resolved.at = when.ms;
|
|
185
|
+
resolved.tomorrow = when.tomorrow;
|
|
186
|
+
}
|
|
187
|
+
return { fields: { at: resolved.at }, resolved: { at: resolved.at, atLocal: formatLocal(resolved.at), tomorrow: !!resolved.tomorrow } };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
var every = parseDuration(parsed.every);
|
|
191
|
+
if (every.error) return { error: "--every: " + every.error };
|
|
192
|
+
if (given(parsed, "until") === given(parsed, "count")) {
|
|
193
|
+
return { error: "A repeat needs an end: give exactly one of --until or --count" };
|
|
194
|
+
}
|
|
195
|
+
var fields = { every: every.ms };
|
|
196
|
+
var out = { every: every.ms, firstAt: now + every.ms, firstAtLocal: formatLocal(now + every.ms) };
|
|
197
|
+
if (given(parsed, "until")) {
|
|
198
|
+
var until = resolveClockTime(parsed.until, now);
|
|
199
|
+
if (until.error) return { error: "--until: " + until.error };
|
|
200
|
+
fields.until = until.ms;
|
|
201
|
+
out.until = until.ms;
|
|
202
|
+
out.untilLocal = formatLocal(until.ms);
|
|
203
|
+
out.tomorrow = until.tomorrow;
|
|
204
|
+
} else {
|
|
205
|
+
var count = COUNT_RE.test(parsed.count) ? Number(parsed.count) : NaN;
|
|
206
|
+
if (!isCount(count)) return { error: "--count must be a whole number from 1 to " + LIMITS.MAX_COUNT };
|
|
207
|
+
fields.count = count;
|
|
208
|
+
out.count = fields.count;
|
|
209
|
+
}
|
|
210
|
+
return { fields: fields, resolved: out };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Parses the arguments after the subcommand. Returns { request, resolved } or
|
|
214
|
+
// { error }. Pure: reading a "-" prompt from stdin or a --prompt-file is the
|
|
215
|
+
// handler's job. env supplies the calling session's project as the default.
|
|
216
|
+
function parseScheduleArgs(sub, args, now, env) {
|
|
217
|
+
if (!has(ALLOWED, sub)) return { error: "Unknown schedule-input command: " + sub };
|
|
218
|
+
var allowed = ALLOWED[sub];
|
|
219
|
+
var parsed = {};
|
|
220
|
+
for (var i = 0; i < args.length; i++) {
|
|
221
|
+
var key = has(FLAGS, args[i]) ? FLAGS[args[i]] : null;
|
|
222
|
+
if (!key || allowed.indexOf(key) === -1) return { error: "Unknown option for schedule-input " + sub + ": " + args[i] };
|
|
223
|
+
if (i + 1 >= args.length) return { error: "Missing value for " + args[i] };
|
|
224
|
+
if (given(parsed, key)) return { error: "Option given twice: " + args[i] };
|
|
225
|
+
parsed[key] = args[++i];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
var request = { cmd: IPC_COMMAND[sub] };
|
|
229
|
+
if (given(parsed, "projectSlug")) request.projectSlug = parsed.projectSlug;
|
|
230
|
+
|
|
231
|
+
if (sub === "delete") {
|
|
232
|
+
if (!given(parsed, "id")) return { error: "Missing required option --id" };
|
|
233
|
+
request.id = parsed.id;
|
|
234
|
+
return { request: request };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (sub === "list") {
|
|
238
|
+
if (given(parsed, "sessionId")) request.sessionId = parsed.sessionId;
|
|
239
|
+
if (given(parsed, "state")) request.state = parsed.state;
|
|
240
|
+
return { request: request };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (!given(parsed, "sessionId")) return { error: "Missing required option --session" };
|
|
244
|
+
request.sessionId = parsed.sessionId;
|
|
245
|
+
// The session's own project when the caller is a console session and names none.
|
|
246
|
+
if (!request.projectSlug) {
|
|
247
|
+
var ref = senderRef.fromEnv(env);
|
|
248
|
+
if (ref) request.projectSlug = ref.project;
|
|
249
|
+
}
|
|
250
|
+
if (!request.projectSlug) return { error: "Missing required option --project (this shell is not inside a console session)" };
|
|
251
|
+
if (given(parsed, "prompt") === given(parsed, "promptFile")) {
|
|
252
|
+
return { error: "Give the text one way only: --prompt <text|-> or --prompt-file <path>" };
|
|
253
|
+
}
|
|
254
|
+
if (given(parsed, "prompt")) request.prompt = parsed.prompt;
|
|
255
|
+
else request.promptFile = parsed.promptFile;
|
|
256
|
+
var timing = resolveTiming(parsed, now);
|
|
257
|
+
if (timing.error) return { error: timing.error };
|
|
258
|
+
Object.assign(request, timing.fields);
|
|
259
|
+
return { request: request, resolved: timing.resolved };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Reads a "-" prompt from stdin and a --prompt-file from disk, as the verbs of
|
|
263
|
+
// session-subcommands read their prompt. Returns an error string or null.
|
|
264
|
+
function loadPrompt(request, readStdin, readFile) {
|
|
265
|
+
if (request.promptFile !== undefined) {
|
|
266
|
+
try { request.prompt = readFile(request.promptFile).replace(/\n$/, ""); }
|
|
267
|
+
catch (e) { return "Cannot read --prompt-file " + request.promptFile + ": " + (e && e.message ? e.message : String(e)); }
|
|
268
|
+
delete request.promptFile;
|
|
269
|
+
} else if (request.prompt === "-") {
|
|
270
|
+
request.prompt = readStdin().replace(/\n$/, "");
|
|
271
|
+
}
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Says, beside the daemon's answer, what the times came to in the caller's own
|
|
276
|
+
// local time, so a clock time that rolled to tomorrow is plain to see.
|
|
277
|
+
function withResolved(res, resolved) {
|
|
278
|
+
if (res && res.ok && resolved) res.resolved = resolved;
|
|
279
|
+
return res;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function exitCodeFor(res) {
|
|
283
|
+
return res && res.ok ? EXIT.OK : EXIT.FAILED;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function fail(message) {
|
|
287
|
+
console.error(message);
|
|
288
|
+
process.exit(1);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function runScheduleInput(args) {
|
|
292
|
+
var sub = args[0];
|
|
293
|
+
if (SUBCOMMANDS.indexOf(sub) === -1) {
|
|
294
|
+
console.error("Unknown schedule-input command: " + String(sub));
|
|
295
|
+
fail("Usage: clagentic-console schedule-input (" + SUBCOMMANDS.join(" | ") + ") ...");
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
var parsed = parseScheduleArgs(sub, args.slice(1), Date.now(), process.env);
|
|
299
|
+
if (parsed.error) {
|
|
300
|
+
console.error(parsed.error);
|
|
301
|
+
fail(USAGE[sub]);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
var request = parsed.request;
|
|
305
|
+
if (sub === "create") {
|
|
306
|
+
var promptError = loadPrompt(request, function () { return fs.readFileSync(0, "utf8"); }, function (p) { return fs.readFileSync(p, "utf8"); });
|
|
307
|
+
if (promptError) { fail(promptError); return; }
|
|
308
|
+
withSenderRef(request, process.env);
|
|
309
|
+
}
|
|
310
|
+
requestDaemon(request).then(function (res) {
|
|
311
|
+
console.log(JSON.stringify(withResolved(res, parsed.resolved), null, 2));
|
|
312
|
+
// Not process.exit: on a pipe it can cut the JSON off before stdout drains.
|
|
313
|
+
process.exitCode = exitCodeFor(res);
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
module.exports = {
|
|
318
|
+
parseDuration: parseDuration,
|
|
319
|
+
resolveClockTime: resolveClockTime,
|
|
320
|
+
formatLocal: formatLocal,
|
|
321
|
+
parseScheduleArgs: parseScheduleArgs,
|
|
322
|
+
loadPrompt: loadPrompt,
|
|
323
|
+
withResolved: withResolved,
|
|
324
|
+
exitCodeFor: exitCodeFor,
|
|
325
|
+
runScheduleInput: runScheduleInput,
|
|
326
|
+
USAGE: USAGE,
|
|
327
|
+
};
|