aegisone 1.0.24 → 1.0.25
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 +2 -2
- package/package.json +1 -1
- package/scripts/predist.mjs +1 -1
- package/src/permissions.js +1 -1
- package/vendor/cli/bin/aegiscode.js +915 -2
- package/vendor/cli/package.json +1 -1
- package/vendor/cli/src/app.js +1 -1
- package/vendor/cli/src/autonomous.js +515 -1
- package/vendor/cli/src/caps.js +879 -1
- package/vendor/cli/src/chatflow.js +1 -1
- package/vendor/cli/src/commands.js +1 -1
- package/vendor/cli/src/engine.js +1 -1
- package/vendor/cli/src/gate.js +55 -1
- package/vendor/cli/src/history.js +1 -1
- package/vendor/cli/src/limits.js +190 -1
- package/vendor/cli/src/mcp.js +694 -1
- package/vendor/cli/src/models.js +1 -1
- package/vendor/cli/src/overlays.js +1 -1
- package/vendor/cli/src/permissions.js +290 -1
- package/vendor/cli/src/provenance.js +182 -2
- package/vendor/cli/src/provenance.lock.json +2 -2
- package/vendor/cli/vendor/client/aegis.js +1 -1
- package/vendor/cli/vendor/client/attest.js +1 -1
- package/vendor/cli/vendor/client/byok-catalog.js +1 -1
- package/vendor/cli/vendor/client/catalog-client.js +3 -0
- package/vendor/cli/vendor/client/conversation-export.js +3 -0
- package/vendor/cli/vendor/client/drive-upload.js +1 -1
- package/vendor/cli/vendor/client/session-store.js +1 -1
- package/vendor/cli/vendor/desktop/lib/local/attachments.js +3 -0
- package/vendor/cli/vendor/desktop/lib/local/claude-pro.js +1 -1
- package/vendor/cli/vendor/desktop/lib/local/context.js +1 -1
- package/vendor/cli/vendor/desktop/lib/local/engine.js +1 -1
- package/vendor/cli/vendor/desktop/lib/local/exec-plan.js +3 -0
- package/vendor/cli/vendor/desktop/lib/local/local.js +1 -1
- package/vendor/cli/vendor/desktop/lib/local/prompt.js +1 -1
- package/vendor/cli/vendor/desktop/lib/local/session-rounds.js +1 -1
- package/vendor/cli/vendor/desktop/lib/local/shell.js +1 -1
- package/vendor/cli/vendor/desktop/lib/local/spill.js +3 -0
- package/vendor/cli/vendor/desktop/lib/local/tools.js +1 -1
- package/vendor/cli/vendor/one/src/permissions.js +1 -1
- package/vendor/desktop/lib/auto.js +1 -1
- package/vendor/desktop/lib/google-connect.js +1 -1
- package/vendor/desktop/lib/local/attachments.js +3 -0
- package/vendor/desktop/lib/local/claude-pro.js +1 -1
- package/vendor/desktop/lib/local/context.js +1 -1
- package/vendor/desktop/lib/local/engine.js +1 -1
- package/vendor/desktop/lib/local/exec-plan.js +3 -0
- package/vendor/desktop/lib/local/local.js +1 -1
- package/vendor/desktop/lib/local/prompt.js +1 -1
- package/vendor/desktop/lib/local/session-rounds.js +1 -1
- package/vendor/desktop/lib/local/shell.js +1 -1
- package/vendor/desktop/lib/local/spill.js +3 -0
- package/vendor/desktop/lib/local/tools.js +1 -1
- package/vendor/desktop/main.js +1 -1
- package/vendor/desktop/package.json +3 -2
- package/vendor/desktop/preload.js +1 -1
- package/vendor/desktop/renderer/app.js +1 -1
- package/vendor/desktop/renderer/budget.js +1 -1
- package/vendor/desktop/renderer/fingerprint.js +2 -2
- package/vendor/desktop/renderer/index.html +44 -4
- package/vendor/desktop/renderer/quick.html +4 -1
- package/vendor/desktop/renderer/quick.js +1 -1
- package/vendor/desktop/renderer/stream-policy.js +1 -1
- package/vendor/desktop/renderer/style.css +124 -0
- package/vendor/desktop/vendor/aegis.js +1 -1
- package/vendor/desktop/vendor/attest.js +1 -1
- package/vendor/desktop/vendor/byok-catalog.js +1 -1
- package/vendor/desktop/vendor/catalog-client.js +3 -0
- package/vendor/desktop/vendor/conversation-export.js +3 -0
- package/vendor/desktop/vendor/drive-upload.js +1 -1
- package/vendor/desktop/vendor/session-store.js +1 -1
|
@@ -1,7 +1,340 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
3
3
|
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
4
|
-
|
|
4
|
+
// AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
|
|
5
|
+
// Conveying a modified copy, or running one as a network service, owes the
|
|
6
|
+
// corresponding source under this licence (§5/§13) — see LICENSING.md.
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `aegiscode` — the AEGIS terminal host.
|
|
11
|
+
*
|
|
12
|
+
* Third host over the same two shared pieces the other two use: the thin
|
|
13
|
+
* transport (client/aegis.js) and the tool registry (mcp/tools.js). The MCP
|
|
14
|
+
* host answers a coding agent's tool calls; the desktop is a window; this is
|
|
15
|
+
* the shell. None of them owns a brain — routing, tiers, memory and billing all
|
|
16
|
+
* stay behind aegiscloud.org.
|
|
17
|
+
*
|
|
18
|
+
* This file is argument parsing and process lifecycle only. Everything
|
|
19
|
+
* testable lives in ../src/app.js.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const path = require('node:path');
|
|
23
|
+
|
|
24
|
+
// A closed pipe is not a failure. `aegiscode --terminal | head`, PowerShell's
|
|
25
|
+
// `| Select-Object -First 5`, and a pager the user quits all close the read end
|
|
26
|
+
// while we still have lines to write, and the default unhandled `error` event
|
|
27
|
+
// on stdout turns that into a stack trace over the answer. Every line of output
|
|
28
|
+
// this host produces is disposable, so the first EPIPE ends the process cleanly.
|
|
29
|
+
for (const stream of [process.stdout, process.stderr]) {
|
|
30
|
+
stream.on('error', (e) => {
|
|
31
|
+
if (e && (e.code === 'EPIPE' || e.code === 'ERR_STREAM_DESTROYED')) process.exit(0);
|
|
32
|
+
throw e;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const HELP = `aegiscode — AEGIS in your shell.
|
|
37
|
+
|
|
38
|
+
Usage:
|
|
39
|
+
aegiscode interactive session
|
|
40
|
+
aegiscode "question" one-shot, then exit
|
|
41
|
+
aegiscode -p "question" same, explicit
|
|
42
|
+
echo "q" | aegiscode -p - read the prompt from stdin
|
|
43
|
+
|
|
44
|
+
Account:
|
|
45
|
+
aegiscode connect sign this machine in — registers a device account
|
|
46
|
+
if it has none (idempotent per machine), and that
|
|
47
|
+
key is what cloud memory and the record vault use
|
|
48
|
+
aegiscode connect --status is this machine connected? (--json; exit 1 if not)
|
|
49
|
+
aegiscode connect --logout sign out (forget the saved key here)
|
|
50
|
+
aegiscode login [<key>] save your AEGIS API key (prompts, no echo, if omitted)
|
|
51
|
+
aegiscode logout remove the saved key
|
|
52
|
+
aegiscode key status show which key is in use and where it came from
|
|
53
|
+
aegiscode topup [EUR] add credit to the token bank (2..1000, default 10):
|
|
54
|
+
opens the payment page, then confirms the credit
|
|
55
|
+
here — the account this machine auto-provisions
|
|
56
|
+
has no browser session, so the page cannot show it
|
|
57
|
+
|
|
58
|
+
Automation:
|
|
59
|
+
aegiscode autonomous help run queued tasks with no terminal at all
|
|
60
|
+
aegiscode mcp install put AEGIS inside VS Code / Cursor / Zed /
|
|
61
|
+
Cline / Windsurf / … ('mcp list' shows them)
|
|
62
|
+
aegiscode mcp status which editors are configured, and do they work
|
|
63
|
+
aegiscode mcp shim OpenAI-compatible endpoint for clients that
|
|
64
|
+
take a base URL but speak no MCP
|
|
65
|
+
aegiscode mcp help all subcommands and their flags
|
|
66
|
+
|
|
67
|
+
Attestation (φ(α) — a sealed record of what a session was):
|
|
68
|
+
aegiscode fingerprint status which authorship key is in use, and where
|
|
69
|
+
aegiscode fingerprint keygen mint a private key (0600, never printed)
|
|
70
|
+
aegiscode fingerprint mint seal the last session as a record
|
|
71
|
+
aegiscode fingerprint verify <file> recompute a record's attestation
|
|
72
|
+
aegiscode fingerprint backup --to DIR copy the key somewhere safe
|
|
73
|
+
aegiscode fingerprint publish [<file>] publish a record, link the copy
|
|
74
|
+
aegiscode fingerprint anchor <file> is the published copy still ours?
|
|
75
|
+
aegiscode fingerprint vault --audit what is in the vault, and is a key in it
|
|
76
|
+
aegiscode fingerprint help all subcommands and their flags
|
|
77
|
+
|
|
78
|
+
Code protection (the agent-key gate on aegiscodex-dev / aegiscode-plugin):
|
|
79
|
+
aegiscode gate status can THIS machine edit a gated tree?
|
|
80
|
+
aegiscode gate import-key [PATH] adopt a key dropped at PATH
|
|
81
|
+
(default ~/Downloads/agent.key),
|
|
82
|
+
then delete PATH
|
|
83
|
+
aegiscode gate bind-machine trust this machine in the manifest
|
|
84
|
+
aegiscode gate rotate mint a fresh key, burn the old one
|
|
85
|
+
aegiscode gate add-key add another key (from stdin)
|
|
86
|
+
aegiscode gate lock-status the server-side half of the lock
|
|
87
|
+
aegiscode gate help all subcommands and their flags
|
|
88
|
+
|
|
89
|
+
Provenance (this binary, and whether it is still the published one):
|
|
90
|
+
aegiscode provenance licence, source, and a self-check of
|
|
91
|
+
this build against the published seal
|
|
92
|
+
(--json for scripts; exit 1 if a
|
|
93
|
+
sealed unit has moved)
|
|
94
|
+
|
|
95
|
+
Options:
|
|
96
|
+
-m, --model <id> pin a model id (see /models; default: server choice)
|
|
97
|
+
--class <name> run this turn on aegis|byok|local|pro. The one-shot
|
|
98
|
+
path had no way to state a class, so '-p' always ran
|
|
99
|
+
the pooled route (DEFAULT_CLASS) no matter what was
|
|
100
|
+
pinned — a headless turn could not use a BYOK key at
|
|
101
|
+
all. '--class byok' needs '--model <provider>:<model>'.
|
|
102
|
+
--base <url> API base (default $AEGIS_API_BASE or aegiscloud.org)
|
|
103
|
+
--key <key> API key for THIS RUN only — it is not saved. Use
|
|
104
|
+
"aegiscode login" to store one for good.
|
|
105
|
+
--json with -p: emit JSON instead of text
|
|
106
|
+
--no-stream buffer the answer instead of streaming it
|
|
107
|
+
--max-tokens <n> output ceiling hint
|
|
108
|
+
--light light theme
|
|
109
|
+
--width <cols> force a render width (useful for piping/logs)
|
|
110
|
+
--automode skip tool-approval prompts (exec/writeFile/editFile
|
|
111
|
+
run without asking) — same as the in-session /automode
|
|
112
|
+
--yolo alias of --automode (the pre-rename spelling)
|
|
113
|
+
--ascii draw with the ASCII mark and glyph set (no Unicode).
|
|
114
|
+
Same as AEGIS_ART=ascii; /terminal ascii toggles it
|
|
115
|
+
mid-session.
|
|
116
|
+
--no-color no colour at all (same as NO_COLOR=1)
|
|
117
|
+
--terminal print the resolved terminal capabilities and exit —
|
|
118
|
+
one report shape on every platform, so pasting it
|
|
119
|
+
from PowerShell (or into a bug report) reads the same
|
|
120
|
+
as pasting it from zsh
|
|
121
|
+
-c, --continue skip onboarding and resume the most recent session
|
|
122
|
+
-h, --help this text
|
|
123
|
+
-v, --version print the version
|
|
124
|
+
|
|
125
|
+
In-session: type /help for commands, /quit to exit, esc/ctrl+c to interrupt a
|
|
126
|
+
running call. Plain text is a prompt (identical to /ask) — a question that
|
|
127
|
+
needs a file read, a shell command, or an edit is handled the same turn.
|
|
128
|
+
`;
|
|
129
|
+
|
|
130
|
+
function parseArgs(argv) {
|
|
131
|
+
const opts = {
|
|
132
|
+
model: null,
|
|
133
|
+
base: null,
|
|
134
|
+
key: null,
|
|
135
|
+
json: false,
|
|
136
|
+
// No `--class` flag given: `null` (not DEFAULT_CLASS) so the stored config
|
|
137
|
+
// pin still wins, exactly as it does for a launch with no `--model`.
|
|
138
|
+
class: null,
|
|
139
|
+
stream: true,
|
|
140
|
+
maxTokens: undefined,
|
|
141
|
+
light: false,
|
|
142
|
+
width: null,
|
|
143
|
+
yolo: false,
|
|
144
|
+
// Three-state, like `color`: `null` means "no art flag was given, let the
|
|
145
|
+
// capability probe decide". It must NOT default to `false`, because `false`
|
|
146
|
+
// is how `--unicode` is spelled — a default of `false` sets
|
|
147
|
+
// `AEGIS_ART=unicode` on every run and silently out-ranks the legacy-console
|
|
148
|
+
// ASCII downgrade, so a plain `aegiscode` on an old Windows console would
|
|
149
|
+
// draw Unicode blocks it cannot render.
|
|
150
|
+
ascii: null,
|
|
151
|
+
color: null,
|
|
152
|
+
terminal: false,
|
|
153
|
+
continue: false,
|
|
154
|
+
prompt: null,
|
|
155
|
+
help: false,
|
|
156
|
+
version: false,
|
|
157
|
+
command: null,
|
|
158
|
+
commandArg: null,
|
|
159
|
+
// `autonomous` owns the rest of the line verbatim (its flags are the
|
|
160
|
+
// subcommand's, not this parser's) — see the `head === 'autonomous'` branch.
|
|
161
|
+
commandArgv: null,
|
|
162
|
+
};
|
|
163
|
+
const rest = [];
|
|
164
|
+
|
|
165
|
+
// An account subcommand is `argv[0]` and nothing else — position 0 only, so
|
|
166
|
+
// `aegiscode -p "key"` (a one-shot prompt whose text happens to be one of
|
|
167
|
+
// these words) is never hijacked into an account operation.
|
|
168
|
+
const head = argv[0];
|
|
169
|
+
if (head && !head.startsWith('-') && ACCOUNT_COMMANDS.has(head)) {
|
|
170
|
+
const arg = argv[1];
|
|
171
|
+
opts.command = head;
|
|
172
|
+
opts.commandArg = arg && !arg.startsWith('-') ? arg : null;
|
|
173
|
+
argv = argv.slice(opts.commandArg ? 2 : 1);
|
|
174
|
+
} else if (head === 'autonomous') {
|
|
175
|
+
// Same position-0 rule, one difference: a queue subcommand owns the REST of
|
|
176
|
+
// the line (including flags, which are the subcommand's, not the session
|
|
177
|
+
// parser's), so the tail is handed over verbatim instead of parsed here.
|
|
178
|
+
// `aegiscode autonomous "write a file"` is not a thing — `add` is — so a
|
|
179
|
+
// bare `autonomous` is help, not a prompt.
|
|
180
|
+
const sub = argv[1] && !argv[1].startsWith('-') ? argv[1] : 'help';
|
|
181
|
+
opts.command = 'autonomous';
|
|
182
|
+
opts.commandArg = sub;
|
|
183
|
+
opts.commandArgv = argv.slice(argv[1] && !argv[1].startsWith('-') ? 2 : 1);
|
|
184
|
+
argv = [];
|
|
185
|
+
} else if (head === 'mcp') {
|
|
186
|
+
// The editor-integration surface, wired exactly like `autonomous` and for
|
|
187
|
+
// the same reason: everything after `mcp <sub>` is the subcommand's argv
|
|
188
|
+
// (`--target cursor,zed` is not this parser's flag), so the tail is handed
|
|
189
|
+
// over untouched. A bare `mcp` is the status report — the question a user
|
|
190
|
+
// actually has ("is AEGIS in my editors?") — not a prompt and not an error.
|
|
191
|
+
const sub = argv[1] && !argv[1].startsWith('-') ? argv[1] : 'status';
|
|
192
|
+
opts.command = 'mcp';
|
|
193
|
+
opts.commandArg = sub;
|
|
194
|
+
opts.commandArgv = argv.slice(argv[1] && !argv[1].startsWith('-') ? 2 : 1);
|
|
195
|
+
argv = [];
|
|
196
|
+
} else if (head === 'fingerprint') {
|
|
197
|
+
// Same position-0 rule and the same reason as `mcp`/`autonomous`: the tail
|
|
198
|
+
// belongs to the subcommand (`--secret-file`, `--code-root`, `--force` are
|
|
199
|
+
// not this parser's flags), so it is handed over verbatim. A bare
|
|
200
|
+
// `fingerprint` is the status report — "is a key configured here?" is the
|
|
201
|
+
// question a user has — not a prompt and not an error.
|
|
202
|
+
const sub = argv[1] && !argv[1].startsWith('-') ? argv[1] : 'status';
|
|
203
|
+
opts.command = 'fingerprint';
|
|
204
|
+
opts.commandArg = sub;
|
|
205
|
+
opts.commandArgv = argv.slice(argv[1] && !argv[1].startsWith('-') ? 2 : 1);
|
|
206
|
+
argv = [];
|
|
207
|
+
} else if (head === 'gate') {
|
|
208
|
+
// Same position-0 rule and the same reason as `fingerprint`/`mcp`: the
|
|
209
|
+
// tail belongs to the subcommand (`--cwd`, `--paths a b c`, `--keep` are
|
|
210
|
+
// agent-gate.mjs's flags, not this parser's), so it is handed over
|
|
211
|
+
// verbatim. A bare `gate` is the status report — "can this machine edit
|
|
212
|
+
// here?" — not a prompt.
|
|
213
|
+
const sub = argv[1] && !argv[1].startsWith('-') ? argv[1] : 'status';
|
|
214
|
+
opts.command = 'gate';
|
|
215
|
+
opts.commandArg = sub;
|
|
216
|
+
opts.commandArgv = argv.slice(argv[1] && !argv[1].startsWith('-') ? 2 : 1);
|
|
217
|
+
argv = [];
|
|
218
|
+
} else if (head === 'connect') {
|
|
219
|
+
// Same position-0 rule and the same passthrough as `gate`/`fingerprint`:
|
|
220
|
+
// everything after `connect` is the subcommand's (`--status`, `--logout`,
|
|
221
|
+
// `--app`, `--base`), so the tail is handed over verbatim — and a bare
|
|
222
|
+
// `connect` means "sign this machine in", which is the thing a new user
|
|
223
|
+
// actually needs, not a help page.
|
|
224
|
+
opts.command = 'connect';
|
|
225
|
+
opts.commandArg = null;
|
|
226
|
+
opts.commandArgv = argv.slice(1);
|
|
227
|
+
argv = [];
|
|
228
|
+
} else if (head === 'provenance') {
|
|
229
|
+
// Same position-0 rule and the same reason as `gate`: it is a one-shot
|
|
230
|
+
// question about THIS BINARY, not a prompt that happens to start with the
|
|
231
|
+
// word. Unlike `gate` the tail needs no passthrough — the only flag it
|
|
232
|
+
// takes is `--json`, which this parser already owns.
|
|
233
|
+
opts.command = 'provenance';
|
|
234
|
+
argv = argv.slice(1);
|
|
235
|
+
} else if (head === 'topup') {
|
|
236
|
+
// Same position-0 rule and the same reason as `gate`: the one argument is
|
|
237
|
+
// the amount (`aegiscode topup 20`), never a prompt whose text happens to
|
|
238
|
+
// be "topup". A bare `aegiscode topup` means the default 10 EUR — the
|
|
239
|
+
// server's own default — rather than usage text, because "fund this
|
|
240
|
+
// account" is a complete intent.
|
|
241
|
+
const amount = argv[1] && !argv[1].startsWith('-') ? argv[1] : null;
|
|
242
|
+
opts.command = 'topup';
|
|
243
|
+
opts.commandArg = amount;
|
|
244
|
+
argv = amount ? argv.slice(2) : argv.slice(1);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
for (let i = 0; i < argv.length; i++) {
|
|
248
|
+
const a = argv[i];
|
|
249
|
+
const next = () => {
|
|
250
|
+
const v = argv[++i];
|
|
251
|
+
if (v === undefined) throw new Error(`${a} needs a value`);
|
|
252
|
+
return v;
|
|
253
|
+
};
|
|
254
|
+
switch (a) {
|
|
255
|
+
case '-h':
|
|
256
|
+
case '--help':
|
|
257
|
+
opts.help = true;
|
|
258
|
+
break;
|
|
259
|
+
case '-v':
|
|
260
|
+
case '--version':
|
|
261
|
+
opts.version = true;
|
|
262
|
+
break;
|
|
263
|
+
case '-p':
|
|
264
|
+
case '--print':
|
|
265
|
+
// `-p` takes an optional prompt; a bare `-p` means "read stdin".
|
|
266
|
+
opts.prompt = argv[i + 1] && !argv[i + 1].startsWith('-') ? next() : '-';
|
|
267
|
+
break;
|
|
268
|
+
case '-m':
|
|
269
|
+
case '--model':
|
|
270
|
+
opts.model = next();
|
|
271
|
+
break;
|
|
272
|
+
case '--class':
|
|
273
|
+
opts.class = next();
|
|
274
|
+
break;
|
|
275
|
+
case '--base':
|
|
276
|
+
opts.base = next();
|
|
277
|
+
break;
|
|
278
|
+
case '--key':
|
|
279
|
+
opts.key = next();
|
|
280
|
+
break;
|
|
281
|
+
case '--json':
|
|
282
|
+
opts.json = true;
|
|
283
|
+
break;
|
|
284
|
+
case '--no-stream':
|
|
285
|
+
opts.stream = false;
|
|
286
|
+
break;
|
|
287
|
+
case '--stream':
|
|
288
|
+
opts.stream = true;
|
|
289
|
+
break;
|
|
290
|
+
case '--max-tokens':
|
|
291
|
+
opts.maxTokens = Number(next());
|
|
292
|
+
break;
|
|
293
|
+
case '--light':
|
|
294
|
+
opts.light = true;
|
|
295
|
+
break;
|
|
296
|
+
case '--automode':
|
|
297
|
+
case '--yolo':
|
|
298
|
+
opts.yolo = true;
|
|
299
|
+
break;
|
|
300
|
+
case '-c':
|
|
301
|
+
case '--continue':
|
|
302
|
+
opts.continue = true;
|
|
303
|
+
break;
|
|
304
|
+
case '--width':
|
|
305
|
+
opts.width = Number(next());
|
|
306
|
+
break;
|
|
307
|
+
case '--ascii':
|
|
308
|
+
// The same switch as AEGIS_ART=ascii, spelled as a flag because it is
|
|
309
|
+
// the one thing a user has to do to fix a mangled welcome mark, and
|
|
310
|
+
// asking them to export a variable and restart the shell first is how
|
|
311
|
+
// the mark stayed broken.
|
|
312
|
+
opts.ascii = true;
|
|
313
|
+
break;
|
|
314
|
+
case '--unicode':
|
|
315
|
+
opts.ascii = false;
|
|
316
|
+
break;
|
|
317
|
+
case '--no-color':
|
|
318
|
+
case '--no-colour':
|
|
319
|
+
opts.color = false;
|
|
320
|
+
break;
|
|
321
|
+
case '--color':
|
|
322
|
+
case '--colour':
|
|
323
|
+
opts.color = true;
|
|
324
|
+
break;
|
|
325
|
+
case '--terminal':
|
|
326
|
+
case '--caps':
|
|
327
|
+
opts.terminal = true;
|
|
328
|
+
break;
|
|
329
|
+
default:
|
|
330
|
+
if (a.startsWith('-') && a !== '-') throw new Error(`unknown option: ${a}`);
|
|
331
|
+
rest.push(a);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (!opts.prompt && rest.length) opts.prompt = rest.join(' ');
|
|
335
|
+
return opts;
|
|
336
|
+
}
|
|
337
|
+
|
|
5
338
|
/**
|
|
6
339
|
* `aegiscode provenance` — what this binary is, and whether its safety files are
|
|
7
340
|
* the ones that were published.
|
|
@@ -17,4 +350,584 @@
|
|
|
17
350
|
* cheap attack must be the loud one). A fork that has deliberately rebuilt is
|
|
18
351
|
* expected to regenerate the manifest and exit 0 naming itself; one that has
|
|
19
352
|
* not now says so instead of staying silent.
|
|
20
|
-
*/
|
|
353
|
+
*/
|
|
354
|
+
function runProvenance(opts = {}) {
|
|
355
|
+
const pkg = require(path.join(__dirname, '..', 'package.json'));
|
|
356
|
+
const provenance = require('../src/provenance.js');
|
|
357
|
+
const described = provenance.describe({ version: pkg.version });
|
|
358
|
+
if (opts.json) {
|
|
359
|
+
process.stdout.write(
|
|
360
|
+
JSON.stringify(
|
|
361
|
+
{
|
|
362
|
+
product: provenance.IDENTITY.product,
|
|
363
|
+
bin: provenance.IDENTITY.bin,
|
|
364
|
+
version: pkg.version,
|
|
365
|
+
license: provenance.IDENTITY.license,
|
|
366
|
+
source: provenance.IDENTITY.source,
|
|
367
|
+
homepage: provenance.IDENTITY.homepage,
|
|
368
|
+
copyright: provenance.IDENTITY.copyright,
|
|
369
|
+
safety: {
|
|
370
|
+
ok: described.report.ok,
|
|
371
|
+
checked: described.report.checked,
|
|
372
|
+
problems: described.report.problems,
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
null,
|
|
376
|
+
2
|
|
377
|
+
) + '\n'
|
|
378
|
+
);
|
|
379
|
+
return described.ok ? 0 : 1;
|
|
380
|
+
}
|
|
381
|
+
process.stdout.write(described.text);
|
|
382
|
+
return described.ok ? 0 : 1;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Account subcommands: the in-band way to give this host a key.
|
|
387
|
+
*
|
|
388
|
+
* They exist because an `AEGIS_API_KEY` export was previously the *only* way
|
|
389
|
+
* in, and an export does not survive a new terminal. `--key` remains purely
|
|
390
|
+
* per-run (CI, a rotation test) so there is exactly one thing a user has to
|
|
391
|
+
* remember: `aegiscode login` saves it, `aegiscode logout` removes it.
|
|
392
|
+
*/
|
|
393
|
+
const ACCOUNT_COMMANDS = new Set(['login', 'logout', 'key']);
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Run `login` / `logout` / `key` and return a process exit code.
|
|
397
|
+
*
|
|
398
|
+
* Exit codes are meaningful because these are the commands a script calls:
|
|
399
|
+
* 0 = done, 1 = the server refused the credential, 2 = no credential and no
|
|
400
|
+
* way to ask for one (no TTY).
|
|
401
|
+
*/
|
|
402
|
+
async function runAccountCommand(command, arg, io = {}) {
|
|
403
|
+
const stdout = io.stdout || process.stdout;
|
|
404
|
+
const stderr = io.stderr || process.stderr;
|
|
405
|
+
const stdin = io.stdin || process.stdin;
|
|
406
|
+
const { credentials, readSecret, maskKey } = loadAccountDeps();
|
|
407
|
+
|
|
408
|
+
const where = () => credentials.credentialsPath();
|
|
409
|
+
|
|
410
|
+
if (command === 'logout' || (command === 'key' && ['clear', 'remove', 'rm'].includes(String(arg || '').toLowerCase()))) {
|
|
411
|
+
const res = credentials.clearApiKey();
|
|
412
|
+
stdout.write(
|
|
413
|
+
res.cleared
|
|
414
|
+
? `aegiscode: key removed from ${res.path}\n`
|
|
415
|
+
: 'aegiscode: no saved key to remove\n'
|
|
416
|
+
);
|
|
417
|
+
if (credentials.legacyKeyOnDisk()) {
|
|
418
|
+
stdout.write(
|
|
419
|
+
'aegiscode: note — a plaintext copy is also in config.json, written by an older ' +
|
|
420
|
+
'AEGIS CLI; delete its "aegiscloud" block as well to finish removing it\n'
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
return 0;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (command === 'key' && ['status', 'show', ''].includes(String(arg || '').toLowerCase())) {
|
|
427
|
+
const st = credentials.keyStatus();
|
|
428
|
+
if (io.json) {
|
|
429
|
+
stdout.write(
|
|
430
|
+
JSON.stringify(
|
|
431
|
+
{
|
|
432
|
+
configured: st.configured,
|
|
433
|
+
source: st.source,
|
|
434
|
+
source_label: credentials.sourceLabel(st.source),
|
|
435
|
+
path: st.path,
|
|
436
|
+
file_mode: st.fileMode,
|
|
437
|
+
masked: st.configured ? maskKey(st.key) : null,
|
|
438
|
+
memory_token: st.memoryToken,
|
|
439
|
+
verified_at: st.verifiedAt,
|
|
440
|
+
plaintext_copy_in_config: st.legacyPlaintext,
|
|
441
|
+
},
|
|
442
|
+
null,
|
|
443
|
+
2
|
|
444
|
+
) + '\n'
|
|
445
|
+
);
|
|
446
|
+
return st.configured ? 0 : 1;
|
|
447
|
+
}
|
|
448
|
+
stdout.write(
|
|
449
|
+
[
|
|
450
|
+
`key: ${st.configured ? maskKey(st.key) : 'not set'}`,
|
|
451
|
+
`source: ${credentials.sourceLabel(st.source)}`,
|
|
452
|
+
`file: ${st.path}${st.fileMode ? ` (${st.fileMode})` : ''}`,
|
|
453
|
+
`memory: ${st.memoryToken ? 'token held (cloud sync ready)' : 'no token'}`,
|
|
454
|
+
st.verifiedAt ? `verified: ${st.verifiedAt}` : null,
|
|
455
|
+
st.legacyPlaintext
|
|
456
|
+
? `note: a plaintext copy also sits in config.json — re-save with \`aegiscode login\` to move it`
|
|
457
|
+
: null,
|
|
458
|
+
]
|
|
459
|
+
.filter(Boolean)
|
|
460
|
+
.join('\n') + '\n'
|
|
461
|
+
);
|
|
462
|
+
return st.configured ? 0 : 1;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// `login [<key>]` and `key <api_key>`: take the key inline or ask for it.
|
|
466
|
+
let key = arg && arg !== 'status' ? String(arg).trim() : '';
|
|
467
|
+
if (!key) {
|
|
468
|
+
if (!stdin.isTTY) {
|
|
469
|
+
stderr.write(
|
|
470
|
+
'aegiscode: no terminal to prompt on — pass the key: `aegiscode login <api_key>`\n' +
|
|
471
|
+
' (or set AEGIS_API_KEY for a single run)\n'
|
|
472
|
+
);
|
|
473
|
+
return 2;
|
|
474
|
+
}
|
|
475
|
+
key = await readSecret('AEGIS API key (kept off screen, Enter to cancel): ', { stdin, stdout });
|
|
476
|
+
}
|
|
477
|
+
if (!key) {
|
|
478
|
+
stderr.write('aegiscode: no key given — nothing saved\n');
|
|
479
|
+
return 2;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const { createApp } = require('../src/app.js');
|
|
483
|
+
const app = createApp({ interactive: false });
|
|
484
|
+
const res = await app.setApiKey(key);
|
|
485
|
+
if (!res.ok) {
|
|
486
|
+
stderr.write(`aegiscode: ${res.message || 'that key could not be saved'}\n`);
|
|
487
|
+
return 2;
|
|
488
|
+
}
|
|
489
|
+
const who = res.account && (res.account.email || res.account.plan);
|
|
490
|
+
stdout.write(`aegiscode: key saved to ${res.path}${who ? ` — ${who}` : ''}\n`);
|
|
491
|
+
if (res.error) {
|
|
492
|
+
const status = res.error.status;
|
|
493
|
+
stderr.write(`aegiscode: the account check failed: ${res.error.message}\n`);
|
|
494
|
+
if (status === 401 || status === 403) {
|
|
495
|
+
stderr.write('aegiscode: the key is stored but the server refused it — re-run with a fresh key\n');
|
|
496
|
+
return 1;
|
|
497
|
+
}
|
|
498
|
+
stderr.write('aegiscode: (stored anyway — the server could not be reached to confirm it)\n');
|
|
499
|
+
return 0;
|
|
500
|
+
}
|
|
501
|
+
stdout.write('aegiscode: verified — run `aegiscode` to start a session\n');
|
|
502
|
+
return 0;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** Lazily required so `--version`/`--help` stay dependency-free and instant. */
|
|
506
|
+
function loadAccountDeps() {
|
|
507
|
+
const credentials = require('../src/credentials.js');
|
|
508
|
+
const { readSecret } = require('../src/secret.js');
|
|
509
|
+
const { maskKey } = require('../src/format.js');
|
|
510
|
+
return { credentials, readSecret, maskKey };
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function readStdin() {
|
|
514
|
+
return new Promise((resolve) => {
|
|
515
|
+
let buf = '';
|
|
516
|
+
process.stdin.setEncoding('utf8');
|
|
517
|
+
process.stdin.on('data', (c) => {
|
|
518
|
+
buf += c;
|
|
519
|
+
});
|
|
520
|
+
process.stdin.on('end', () => resolve(buf.trim()));
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
async function main(argv = process.argv.slice(2)) {
|
|
525
|
+
let opts;
|
|
526
|
+
try {
|
|
527
|
+
opts = parseArgs(argv);
|
|
528
|
+
} catch (e) {
|
|
529
|
+
process.stderr.write(`aegiscode: ${e.message}\n\n${HELP}`);
|
|
530
|
+
return 2;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
if (opts.help) {
|
|
534
|
+
process.stdout.write(HELP);
|
|
535
|
+
return 0;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
const pkg = require(path.join(__dirname, '..', 'package.json'));
|
|
539
|
+
if (opts.version) {
|
|
540
|
+
process.stdout.write(pkg.version + '\n');
|
|
541
|
+
return 0;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// ── interlock (see src/limits.js) ────────────────────────────────────────
|
|
545
|
+
//
|
|
546
|
+
// The second gate, and it runs before every subcommand dispatch below — gate,
|
|
547
|
+
// mcp, autonomous, provenance, the session — because a check that ran after
|
|
548
|
+
// the dispatch it is supposed to stop is a check that reports instead of
|
|
549
|
+
// gating.
|
|
550
|
+
//
|
|
551
|
+
// It asks whether the safety files are still the sealed set *as a whole*, and
|
|
552
|
+
// when they are not it says exactly one bland thing — naming no file, no cause
|
|
553
|
+
// and no remedy — and refuses the privileged subcommands (`gate`,
|
|
554
|
+
// `provenance`, `mcp`), so the build that failed the check cannot use the
|
|
555
|
+
// gate's own key management or report on itself. Sessions still start, marked
|
|
556
|
+
// `AEGIS_REDUCED_MODE=1` for modules that opt-in.
|
|
557
|
+
//
|
|
558
|
+
// Deliberately NOT switchable by environment variable: a gate with a
|
|
559
|
+
// documented off switch is a gate the manipulator reads about in the same file
|
|
560
|
+
// as the gate. Re-sealing is a manual act —
|
|
561
|
+
// `node tools/provenance.mjs lock --write` — and a tree that is mid-edit trips
|
|
562
|
+
// this on purpose.
|
|
563
|
+
//
|
|
564
|
+
// This call site is the EARLIEST refusal, not the only one. `armed()` is
|
|
565
|
+
// asked again, independently, by each module that owns one of these
|
|
566
|
+
// privileges — src/gate.js, src/mcp.js, src/autonomous.js,
|
|
567
|
+
// src/permissions.js — because a check invoked from exactly one place is
|
|
568
|
+
// removed by deleting one line, and this is the file a manipulator is already
|
|
569
|
+
// editing. Deleting the block below leaves four refusals behind, in four files
|
|
570
|
+
// the sealed set covers.
|
|
571
|
+
let interlockFired = false;
|
|
572
|
+
try {
|
|
573
|
+
const limits = require('../src/limits.js');
|
|
574
|
+
interlockFired = limits.armed();
|
|
575
|
+
if (interlockFired && limits.isPrivileged(opts.command)) return limits.refuse();
|
|
576
|
+
} catch {
|
|
577
|
+
/* a gate must never be the reason the program fails to start */
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// ── the shared env file, before any credential is resolved ───────────────
|
|
581
|
+
//
|
|
582
|
+
// `~/.aegiscode/.env` read into process.env once. This is what makes key
|
|
583
|
+
// setup ONE instruction in every host — "put it in ~/.aegiscode/.env" —
|
|
584
|
+
// instead of a `login` plus a `/byok-key <provider>` prompt per provider:
|
|
585
|
+
// both resolution paths already read AEGIS_API_KEY and <PROVIDER>_API_KEY
|
|
586
|
+
// out of the environment (src/credentials.js, the byok class in the shared
|
|
587
|
+
// engine), they simply had nothing filling it. A variable already exported
|
|
588
|
+
// in the launching shell always wins. Placed AFTER the help/version
|
|
589
|
+
// early-exits so those stay instant, and before caps because everything
|
|
590
|
+
// below this point may resolve a credential.
|
|
591
|
+
try {
|
|
592
|
+
const { resolveClientModule } = require('../src/shared.js');
|
|
593
|
+
const envResult = require(resolveClientModule('env-file.js')).loadEnvFile();
|
|
594
|
+
if (envResult.loose) {
|
|
595
|
+
process.stderr.write(
|
|
596
|
+
`aegiscode: ${envResult.file} is readable by other accounts ` +
|
|
597
|
+
`(mode ${envResult.mode.toString(8)}) — consider \`chmod 600\` on it.\n`
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
} catch (e) {
|
|
601
|
+
process.stderr.write(`aegiscode: could not read ~/.aegiscode/.env: ${e && e.message}\n`);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// ── terminal capabilities, resolved before anything else is loaded ────────
|
|
605
|
+
//
|
|
606
|
+
// Ordering is the whole point. `theme.js` materialises its palettes and its
|
|
607
|
+
// glyph table at load time and `art.js` picks its stencil at load time, and
|
|
608
|
+
// `app.js` pulls in both. Resolving caps first means the welcome screen is
|
|
609
|
+
// drawn for the terminal actually in use — PowerShell with VT enabled gets
|
|
610
|
+
// the same mark as zsh, a legacy console gets the ASCII pass — instead of
|
|
611
|
+
// whatever the first probe guessed being frozen into a module that is never
|
|
612
|
+
// re-evaluated.
|
|
613
|
+
const { setCaps, describe } = require('../src/caps.js');
|
|
614
|
+
const capEnv = { ...process.env };
|
|
615
|
+
if (opts.ascii === true) capEnv.AEGIS_ART = 'ascii';
|
|
616
|
+
if (opts.ascii === false) capEnv.AEGIS_ART = 'unicode';
|
|
617
|
+
if (opts.color === false) capEnv.NO_COLOR = '1';
|
|
618
|
+
if (opts.color === true) {
|
|
619
|
+
// An explicit --color has to beat an inherited NO_COLOR, and NO_COLOR is
|
|
620
|
+
// checked first, so it is removed rather than out-ranked.
|
|
621
|
+
delete capEnv.NO_COLOR;
|
|
622
|
+
capEnv.FORCE_COLOR = '3';
|
|
623
|
+
}
|
|
624
|
+
if (opts.width) capEnv.AEGIS_WIDTH = String(opts.width);
|
|
625
|
+
setCaps(null, capEnv);
|
|
626
|
+
|
|
627
|
+
// `--terminal` answers "why does it look like that here?" in one shape on
|
|
628
|
+
// every platform, so a paste from PowerShell reads like one from zsh. It
|
|
629
|
+
// prints the resolved values and exits before a session, a config read or a
|
|
630
|
+
// network call can colour the answer.
|
|
631
|
+
if (opts.terminal) {
|
|
632
|
+
const rows = describe();
|
|
633
|
+
const key = rows.reduce((m, [k]) => Math.max(m, k.length), 0);
|
|
634
|
+
process.stdout.write(`aegiscode ${pkg.version} — terminal capabilities\n\n`);
|
|
635
|
+
for (const [k, v] of rows) process.stdout.write(` ${k.padEnd(key)} ${v}\n`);
|
|
636
|
+
process.stdout.write('\nOverride with --ascii / --color / --light / --width, or AEGIS_ART,\nAEGIS_STAR, AEGIS_COLOR, AEGIS_BACKGROUND, AEGIS_BACKGROUND_PROBE, AEGIS_VT,\nAEGIS_WIDTH (see README).\n');
|
|
637
|
+
return 0;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Apply the flags that the shared client reads from the environment, before
|
|
641
|
+
// anything constructs it.
|
|
642
|
+
if (opts.base) process.env.AEGIS_API_BASE = opts.base;
|
|
643
|
+
if (opts.key) process.env.AEGIS_API_KEY = opts.key;
|
|
644
|
+
|
|
645
|
+
// Account subcommands run before a session is built: they are one-shot,
|
|
646
|
+
// non-interactive, and must work in a script (`aegiscode login "$KEY"`) as
|
|
647
|
+
// well as at a prompt.
|
|
648
|
+
// φ(α): the authorship key and the sealed session records. Dispatched here
|
|
649
|
+
// rather than in the account set because its subcommands own their flags —
|
|
650
|
+
// and before a session is built, because `fingerprint mint` must work with no
|
|
651
|
+
// terminal at all (a CI job sealing what it just ran), exactly like the queue.
|
|
652
|
+
if (opts.command === 'fingerprint') {
|
|
653
|
+
const { runFingerprint } = require('../src/fingerprint.js');
|
|
654
|
+
return runFingerprint(opts.commandArg || 'status', opts.commandArgv || [], {
|
|
655
|
+
json: opts.json,
|
|
656
|
+
stdout: process.stdout,
|
|
657
|
+
stderr: process.stderr,
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// Signing this machine in (and registering it, on a fresh install). Before a
|
|
662
|
+
// session is built, for the same reason `fingerprint` is: it is a one-shot
|
|
663
|
+
// question with a scriptable answer, and it must work with no terminal at all
|
|
664
|
+
// — the first-run prompt, CI, and `aegiscode connect --status` in a health
|
|
665
|
+
// check all need it without a TUI.
|
|
666
|
+
if (opts.command === 'connect') {
|
|
667
|
+
const { runConnect } = require('../src/connect.js');
|
|
668
|
+
return runConnect(opts.commandArgv || [], {
|
|
669
|
+
json: opts.json,
|
|
670
|
+
stdout: process.stdout,
|
|
671
|
+
stderr: process.stderr,
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// The agent-key gate on aegiscodex-dev / aegiscode-plugin. Dispatched here,
|
|
676
|
+
// before a session is built, for the same reason `fingerprint` is: it is a
|
|
677
|
+
// one-shot administrative question ("can this machine edit that tree?"),
|
|
678
|
+
// not a prompt, and it must work with no terminal at all (a fresh npm
|
|
679
|
+
// install with no git clone is exactly the case this exists for).
|
|
680
|
+
if (opts.command === 'gate') {
|
|
681
|
+
const { runGate } = require('../src/gate.js');
|
|
682
|
+
return runGate(opts.commandArg || 'status', opts.commandArgv || []);
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// Funding the account, dispatched here rather than among the account commands
|
|
686
|
+
// because it is interactive in a different way: it opens a browser and then
|
|
687
|
+
// BLOCKS on the balance endpoint until the Stripe webhook lands the credit
|
|
688
|
+
// (see src/topup.js for why blocking is right here and wrong in the desktop).
|
|
689
|
+
// The one failure this fixes is the account auto-provision creates — a device
|
|
690
|
+
// key whose owner can never sign in to `/topup`, the session-gated page every
|
|
691
|
+
// old link pointed at.
|
|
692
|
+
if (opts.command === 'topup') {
|
|
693
|
+
const { runTopupCommand } = require('../src/topup.js');
|
|
694
|
+
return runTopupCommand(opts.commandArg, {
|
|
695
|
+
json: opts.json,
|
|
696
|
+
stdin: process.stdin,
|
|
697
|
+
stdout: process.stdout,
|
|
698
|
+
stderr: process.stderr,
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
if (ACCOUNT_COMMANDS.has(opts.command)) {
|
|
703
|
+
return runAccountCommand(opts.command, opts.commandArg, {
|
|
704
|
+
json: opts.json,
|
|
705
|
+
stdin: process.stdin,
|
|
706
|
+
stdout: process.stdout,
|
|
707
|
+
stderr: process.stderr,
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// The work queue. Parsed at position 0 like the account subcommands (see
|
|
712
|
+
// parseArgs), but dispatched here rather than there because it is the one
|
|
713
|
+
// surface that must work with NO terminal at all: a systemd timer runs
|
|
714
|
+
// `aegiscode autonomous proceed` from a unit with stdin on /dev/null, and a
|
|
715
|
+
// cron job reads the exit status to decide whether to page somebody. So the
|
|
716
|
+
// subcommand's own argv is handed over untouched, the process's real streams
|
|
717
|
+
// are passed through, and the subcommand's exit code is what the shell sees
|
|
718
|
+
// (0 done, 1 a task failed / another drain holds the lock, 2 usage or no
|
|
719
|
+
// credential) — never the interactive chat path's "no terminal and no prompt".
|
|
720
|
+
if (opts.command === 'autonomous') {
|
|
721
|
+
// A drain IS the thing the presence panel exists to show: a machine working
|
|
722
|
+
// through a queue with no terminal open. So this path beacons too — with
|
|
723
|
+
// `autonomous: true` and the live pending depth — and it beacons BEFORE the
|
|
724
|
+
// lock is taken, so a drain that exits immediately because another process
|
|
725
|
+
// holds the lock still registers as "this machine is here".
|
|
726
|
+
const presence = require('../src/presence.js');
|
|
727
|
+
const credentials = require('../src/credentials.js');
|
|
728
|
+
presence.start({
|
|
729
|
+
credentials,
|
|
730
|
+
autonomous: true,
|
|
731
|
+
queueDepth: presence.queueDepthReader(),
|
|
732
|
+
});
|
|
733
|
+
// The beacon's timer is unref'd, so nothing here holds the process open;
|
|
734
|
+
// this just cancels a scheduled beat that would otherwise fire mid-teardown.
|
|
735
|
+
process.once('exit', () => presence.stop());
|
|
736
|
+
|
|
737
|
+
const { runQueueCommand } = require('../src/autonomous.js');
|
|
738
|
+
return runQueueCommand(opts.commandArg, opts.commandArgv || [], {
|
|
739
|
+
json: opts.json,
|
|
740
|
+
stdin: process.stdin,
|
|
741
|
+
stdout: process.stdout,
|
|
742
|
+
stderr: process.stderr,
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
// The editor-integration surface. Dispatched here rather than among the
|
|
747
|
+
// account commands because it is not about THIS run: `mcp install` edits files
|
|
748
|
+
// other programs read, and `mcp serve` / `mcp shim` are long-running
|
|
749
|
+
// processes that must not be wrapped in a session (they are what an editor
|
|
750
|
+
// spawns behind a session of its own). Loaded lazily so `--help`,
|
|
751
|
+
// `--version` and an ordinary prompt never pay for the host registry.
|
|
752
|
+
if (opts.command === 'mcp') {
|
|
753
|
+
const { runMcpCommand } = require('../src/mcp.js');
|
|
754
|
+
return runMcpCommand(opts.commandArg, opts.commandArgv || [], {
|
|
755
|
+
json: opts.json,
|
|
756
|
+
stdin: process.stdin,
|
|
757
|
+
stdout: process.stdout,
|
|
758
|
+
stderr: process.stderr,
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
// What this binary is, and whether its safety files are the published ones.
|
|
763
|
+
// Dispatched before a session is built, like `gate`/`fingerprint`: it is a
|
|
764
|
+
// question about this install, it must work with no terminal at all (an audit
|
|
765
|
+
// script), and it must not depend on a credential being configured. It is NOT
|
|
766
|
+
// part of the startup path — the runtime self-check that warns on stderr is in
|
|
767
|
+
// src/provenance.js and runs below, for sessions.
|
|
768
|
+
//
|
|
769
|
+
// The interlock below runs first and can make `provenance` unavailable, which
|
|
770
|
+
// is not an oversight: a build that fails the silent gate does not get to
|
|
771
|
+
// report on itself.
|
|
772
|
+
if (opts.command === 'provenance' && !interlockFired) {
|
|
773
|
+
return runProvenance({ json: opts.json });
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
const { createApp } = require('../src/app.js');
|
|
777
|
+
|
|
778
|
+
// ── provenance self-check (one stderr line, never fatal) ─────────────────
|
|
779
|
+
//
|
|
780
|
+
// A build whose gate, permission evaluator, approval classes or runner were
|
|
781
|
+
// edited announces it, once, before the session starts — instead of importing
|
|
782
|
+
// the neutered file and saying nothing. Never throws and never blocks: a
|
|
783
|
+
// diagnostic that can stop the program it reports on is worse than none (see
|
|
784
|
+
// src/provenance.js, which also records why a MISSING manifest is a failure
|
|
785
|
+
// rather than a pass). A development tree silences it with
|
|
786
|
+
// AEGIS_NO_PROVENANCE_CHECK=1 — and the silence is printed at the time, so it
|
|
787
|
+
// is a fact on the record rather than a quiet default.
|
|
788
|
+
//
|
|
789
|
+
// Skipped when the interlock has already fired: that path emits exactly one
|
|
790
|
+
// bland line, and following it with a more descriptive one would hand back
|
|
791
|
+
// everything the bland line withheld.
|
|
792
|
+
if (!interlockFired) try {
|
|
793
|
+
const provenance = require('../src/provenance.js');
|
|
794
|
+
const line = provenance.warning(provenance.selfCheck());
|
|
795
|
+
if (line) process.stderr.write(line);
|
|
796
|
+
} catch {
|
|
797
|
+
/* a diagnostic must never be the reason a session fails to start */
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// ── presence beacon (aegis1 /api/v1/heartbeat) ───────────────────────────
|
|
801
|
+
//
|
|
802
|
+
// "Who is running aegiscode right now" — the admin panel's presence card.
|
|
803
|
+
// Started for every SESSION, not for the account/lifecycle subcommands above
|
|
804
|
+
// (`login`, `key status`, `--version`, `mcp install` are things a user does
|
|
805
|
+
// once, not a machine being present).
|
|
806
|
+
//
|
|
807
|
+
// Keyed on the credential store's own resolution (env → credentials.json →
|
|
808
|
+
// config.json) so the beacon carries the key this session will actually use,
|
|
809
|
+
// and on the machine fingerprint (client/presence.js) so one laptop is one
|
|
810
|
+
// machine in the panel rather than one row per launch. No key ⇒ presence.js
|
|
811
|
+
// returns an inert store: no timer, no request, no 401 loop.
|
|
812
|
+
//
|
|
813
|
+
// Never fatal: `start()` swallows a load failure and returns an inert store,
|
|
814
|
+
// because a beacon is diagnostic and a session is not.
|
|
815
|
+
const presence = require('../src/presence.js');
|
|
816
|
+
presence.start({
|
|
817
|
+
credentials: require('../src/credentials.js'),
|
|
818
|
+
queueDepth: presence.queueDepthReader(),
|
|
819
|
+
});
|
|
820
|
+
// Every return path below ends the process, and the beacon's timer is
|
|
821
|
+
// unref'd, so this is tidiness rather than a requirement — but it means a
|
|
822
|
+
// quit never races a beat.
|
|
823
|
+
process.once('exit', () => presence.stop());
|
|
824
|
+
|
|
825
|
+
let prompt = opts.prompt;
|
|
826
|
+
if (prompt === '-') {
|
|
827
|
+
if (process.stdin.isTTY) {
|
|
828
|
+
process.stderr.write('aegiscode: -p - expects a prompt on stdin\n');
|
|
829
|
+
return 2;
|
|
830
|
+
}
|
|
831
|
+
prompt = await readStdin();
|
|
832
|
+
if (!prompt) {
|
|
833
|
+
process.stderr.write('aegiscode: empty stdin\n');
|
|
834
|
+
return 2;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
const app = createApp({
|
|
839
|
+
model: opts.model,
|
|
840
|
+
class: opts.class,
|
|
841
|
+
stream: opts.stream,
|
|
842
|
+
light: opts.light,
|
|
843
|
+
maxTokens: opts.maxTokens,
|
|
844
|
+
...(opts.effort ? { effort: opts.effort } : {}),
|
|
845
|
+
width: opts.width ? () => opts.width : undefined,
|
|
846
|
+
interactive: !prompt && Boolean(process.stdin.isTTY),
|
|
847
|
+
// ONLY a flag states an answer here. This line used to read `!opts.yolo`,
|
|
848
|
+
// which is a boolean on every launch — and `app.js`'s gate returns a pinned
|
|
849
|
+
// `options.confirmMode` the moment it is not `undefined`, so that one `true`
|
|
850
|
+
// short-circuited the store read the whole ON8 posture is built on: an
|
|
851
|
+
// automode the user had turned on (`/automode`, the window's switch, both of
|
|
852
|
+
// which persist) was overruled by the absence of a launch flag, and the CLI
|
|
853
|
+
// kept asking for exactly as long as the process lived. `undefined` hands
|
|
854
|
+
// the question to the one read — `resolveSkip()`, then `confirmPosture()` —
|
|
855
|
+
// which is where the flag/env and the shared store are resolved, live, on
|
|
856
|
+
// every tool round.
|
|
857
|
+
confirmMode: opts.yolo ? false : undefined,
|
|
858
|
+
continue: opts.continue,
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
if (prompt) return app.runOnce(prompt, { json: opts.json });
|
|
862
|
+
|
|
863
|
+
if (!process.stdin.isTTY) {
|
|
864
|
+
process.stderr.write(
|
|
865
|
+
'aegiscode: no terminal and no prompt. Use `aegiscode -p "question"` or pipe a prompt in.\n'
|
|
866
|
+
);
|
|
867
|
+
return 2;
|
|
868
|
+
}
|
|
869
|
+
// The reference leaves a resume hint on exit; without one there is no way to
|
|
870
|
+
// discover that a session was persisted at all (`/resume` lists them, but a
|
|
871
|
+
// user who just quit is not looking at a command list).
|
|
872
|
+
//
|
|
873
|
+
// It is written from two places on purpose, and latched so it can only ever
|
|
874
|
+
// be written once. It used to be a single write after the await below, which
|
|
875
|
+
// made the hint depend on the session promise settling: when the key pump
|
|
876
|
+
// ended and the loop drained instead — the same failure that ate the alt
|
|
877
|
+
// screen teardown — the process exited before this line ran, and the frames
|
|
878
|
+
// differed by exactly these 81 bytes. That is what the parity suite was
|
|
879
|
+
// reporting, and it was load-dependent: measured 3 of 4 runs losing the hint
|
|
880
|
+
// while four pty runs competed for the cpu, 0 of 12 when they did not. The
|
|
881
|
+
// teardown is guaranteed the same way (see chatflow.js); the hint now is too,
|
|
882
|
+
// so the bytes no longer depend on which of the two paths ended the process.
|
|
883
|
+
// The hint is handed to the screen module instead of being scheduled here.
|
|
884
|
+
// It is emitted *by* `leaveAltScreen()`, i.e. by the restore itself, because
|
|
885
|
+
// position cannot be guaranteed by ordering where three paths can reach the
|
|
886
|
+
// teardown (the loop's `finally`, the session's `exit` handler, this file's
|
|
887
|
+
// `exit` handler) and their relative order is timing-dependent. That race put
|
|
888
|
+
// the same 81 bytes in two different positions from run to run — the parity
|
|
889
|
+
// suite saw linux/darwin/win32 diverge and then converge, at random, with
|
|
890
|
+
// platform swapped out for luck. See `queueExitNotice` in src/screen.js.
|
|
891
|
+
const screen = require('../src/screen.js');
|
|
892
|
+
screen.queueExitNotice('\nResume this session with:\n aegiscode --continue (or /resume for the list)\n');
|
|
893
|
+
// A queued notice is consumed exactly once, by the first restore to run; the
|
|
894
|
+
// flush covers the paths that never leave an alternate screen (a crash before
|
|
895
|
+
// the screen was taken, a host that drives the loop with its own stdin), so
|
|
896
|
+
// the hint cannot be lost by the very mechanism that guarantees its position.
|
|
897
|
+
const leaveHint = () => {
|
|
898
|
+
try {
|
|
899
|
+
if (screen.isAltScreen()) screen.leaveAltScreen();
|
|
900
|
+
screen.flushExitNotice();
|
|
901
|
+
} catch { /* the terminal is already gone; nothing to say it to */ }
|
|
902
|
+
};
|
|
903
|
+
const running = app.runInteractive();
|
|
904
|
+
process.once('exit', leaveHint);
|
|
905
|
+
const code = await running;
|
|
906
|
+
leaveHint();
|
|
907
|
+
return code;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
if (require.main === module) {
|
|
911
|
+
main()
|
|
912
|
+
.then(async (code) => {
|
|
913
|
+
// Before this process exits, let an in-flight presence beat finish. A
|
|
914
|
+
// one-shot run reaches this line in tens of milliseconds, and
|
|
915
|
+
// `process.exit()` would otherwise kill the beacon's first request
|
|
916
|
+
// mid-flight — the machine would run a whole command and still never
|
|
917
|
+
// appear in the admin panel. Bounded (1.5 s) so the beacon can never be
|
|
918
|
+
// the reason a CLI hangs; `flush()` is a no-op when nothing is in flight
|
|
919
|
+
// or when the beacon is off.
|
|
920
|
+
try {
|
|
921
|
+
await require('../src/presence.js').flush();
|
|
922
|
+
} catch {
|
|
923
|
+
/* a beacon must never affect the exit code */
|
|
924
|
+
}
|
|
925
|
+
process.exit(code || 0);
|
|
926
|
+
})
|
|
927
|
+
.catch((e) => {
|
|
928
|
+
process.stderr.write(`aegiscode: ${e && e.message ? e.message : e}\n`);
|
|
929
|
+
process.exit(1);
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
module.exports = { main, parseArgs, HELP, ACCOUNT_COMMANDS, runAccountCommand };
|