@aragon-agent/cli 0.6.71 → 0.6.72
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/dist/account/config.d.ts +61 -0
- package/dist/account/config.js +83 -0
- package/dist/account/device-client.d.ts +113 -0
- package/dist/account/device-client.js +425 -0
- package/dist/account/format.d.ts +51 -0
- package/dist/account/format.js +102 -0
- package/dist/account/login-flow.d.ts +68 -0
- package/dist/account/login-flow.js +305 -0
- package/dist/account/session.d.ts +62 -0
- package/dist/account/session.js +63 -0
- package/dist/account/store.d.ts +86 -0
- package/dist/account/store.js +231 -0
- package/dist/app/App.js +191 -1
- package/dist/commands/account-command.d.ts +86 -0
- package/dist/commands/account-command.js +165 -0
- package/dist/commands/builtins.js +15 -0
- package/dist/commands/catalog.js +16 -0
- package/dist/config/app-paths.d.ts +15 -0
- package/dist/config/app-paths.js +17 -0
- package/package.json +3 -3
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<home>/account.json` - the on-disk credential for the device-flow login (C3).
|
|
3
|
+
*
|
|
4
|
+
* FILE SHAPE (version 1, and that version field is the whole migration story):
|
|
5
|
+
*
|
|
6
|
+
* {
|
|
7
|
+
* "version": 1,
|
|
8
|
+
* "baseUrl": "https://aragonagent.com",
|
|
9
|
+
* "token": "<plaintext bearer token>",
|
|
10
|
+
* "tokenPrefix": "aragon_pat_ab12cd34ef56",
|
|
11
|
+
* "user": { "id": "usr_...", "username": "ada", "email": "ada@example.com" },
|
|
12
|
+
* "scopes": ["account:read"],
|
|
13
|
+
* "issuedAt": 1767000000000,
|
|
14
|
+
* "expiresAt": 1768728000000
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* THE TOKEN IS PLAINTEXT ON PURPOSE. An OS keychain would be better and is not
|
|
18
|
+
* reachable from a CLI that must also run under WSL, in CI and over SSH; what is
|
|
19
|
+
* reachable is 0600 plus an atomic write, which is what `~/.aws/credentials`,
|
|
20
|
+
* `~/.npmrc` and `gh` all do. The file never holds a refresh token, so the blast
|
|
21
|
+
* radius of a leak is one revocable credential with a 20-day ceiling.
|
|
22
|
+
*
|
|
23
|
+
* NEVER THROWS. Every failure mode here - missing file, garbage bytes, a JSON
|
|
24
|
+
* document of the wrong shape, an unwritable home - is an ANSWER, because the
|
|
25
|
+
* only caller that matters runs at startup and a throw there is a CLI that will
|
|
26
|
+
* not start because of a credential file. `readAccount()` answers `null`,
|
|
27
|
+
* `writeAccount()` answers `false`, `clearAccount()` answers whether the file is
|
|
28
|
+
* gone afterwards.
|
|
29
|
+
*/
|
|
30
|
+
/** The only shape this module reads or writes (C3). */
|
|
31
|
+
export declare const ACCOUNT_SCHEMA_VERSION = 1;
|
|
32
|
+
/** POSIX mode for a file holding a bearer token. Ignored on win32. */
|
|
33
|
+
export declare const ACCOUNT_FILE_MODE = 384;
|
|
34
|
+
export interface AccountUser {
|
|
35
|
+
readonly id: string;
|
|
36
|
+
readonly username: string;
|
|
37
|
+
readonly email?: string;
|
|
38
|
+
}
|
|
39
|
+
export interface AccountRecord {
|
|
40
|
+
readonly version: number;
|
|
41
|
+
readonly baseUrl: string;
|
|
42
|
+
readonly token: string;
|
|
43
|
+
readonly tokenPrefix: string;
|
|
44
|
+
readonly user: AccountUser;
|
|
45
|
+
readonly scopes: readonly string[];
|
|
46
|
+
/** Epoch milliseconds. */
|
|
47
|
+
readonly issuedAt: number;
|
|
48
|
+
/** Epoch milliseconds; the device grant's own lifetime, ~20 days (C2). */
|
|
49
|
+
readonly expiresAt: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The credential on disk, or `null`.
|
|
53
|
+
*
|
|
54
|
+
* `null` covers: no file, an empty file, a half-written file, a document that is
|
|
55
|
+
* not JSON, a JSON scalar, a document of the wrong shape and a document from a
|
|
56
|
+
* newer schema. Callers treat all of them the same way - "not signed in" - which
|
|
57
|
+
* is the only answer that cannot be wrong about a file it could not parse.
|
|
58
|
+
*/
|
|
59
|
+
export declare function readAccount(): AccountRecord | null;
|
|
60
|
+
/**
|
|
61
|
+
* Write the credential atomically: temp file in the same directory, then rename.
|
|
62
|
+
*
|
|
63
|
+
* WHY RENAME AND NOT `writeFileSync(path)`: a write that dies half-way (a full
|
|
64
|
+
* disk, a killed process, a power cut) leaves a TRUNCATED JSON document, and the
|
|
65
|
+
* reader above would then report `signed-out` for a user who did nothing wrong.
|
|
66
|
+
* `rename` is atomic within a directory, so a reader sees either the old file or
|
|
67
|
+
* the new one - never a torn one.
|
|
68
|
+
*
|
|
69
|
+
* THE TEMP NAME CARRIES pid AND A RANDOM SUFFIX: two `aragon` processes logging
|
|
70
|
+
* in at once (a terminal and a desktop host) must not write the same temp file.
|
|
71
|
+
*
|
|
72
|
+
* `chmodSync` AFTER the rename, matching `config/ui-state.ts`: `rename` does not
|
|
73
|
+
* preserve the mode on every platform, and a 0600 temp file renamed into place
|
|
74
|
+
* is only 0600 by luck otherwise. win32 has no POSIX mode to set, so it is
|
|
75
|
+
* skipped there rather than warned about.
|
|
76
|
+
*/
|
|
77
|
+
export declare function writeAccount(record: AccountRecord): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Delete the credential. IDEMPOTENT: `true` means "there is no credential file
|
|
80
|
+
* now", which is also the answer when there never was one.
|
|
81
|
+
*
|
|
82
|
+
* `false` means the file is still there, and the only cause is a filesystem
|
|
83
|
+
* refusal (a Windows share with an open handle, a read-only directory). The
|
|
84
|
+
* caller may then say "sign-out failed" instead of lying.
|
|
85
|
+
*/
|
|
86
|
+
export declare function clearAccount(): boolean;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<home>/account.json` - the on-disk credential for the device-flow login (C3).
|
|
3
|
+
*
|
|
4
|
+
* FILE SHAPE (version 1, and that version field is the whole migration story):
|
|
5
|
+
*
|
|
6
|
+
* {
|
|
7
|
+
* "version": 1,
|
|
8
|
+
* "baseUrl": "https://aragonagent.com",
|
|
9
|
+
* "token": "<plaintext bearer token>",
|
|
10
|
+
* "tokenPrefix": "aragon_pat_ab12cd34ef56",
|
|
11
|
+
* "user": { "id": "usr_...", "username": "ada", "email": "ada@example.com" },
|
|
12
|
+
* "scopes": ["account:read"],
|
|
13
|
+
* "issuedAt": 1767000000000,
|
|
14
|
+
* "expiresAt": 1768728000000
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* THE TOKEN IS PLAINTEXT ON PURPOSE. An OS keychain would be better and is not
|
|
18
|
+
* reachable from a CLI that must also run under WSL, in CI and over SSH; what is
|
|
19
|
+
* reachable is 0600 plus an atomic write, which is what `~/.aws/credentials`,
|
|
20
|
+
* `~/.npmrc` and `gh` all do. The file never holds a refresh token, so the blast
|
|
21
|
+
* radius of a leak is one revocable credential with a 20-day ceiling.
|
|
22
|
+
*
|
|
23
|
+
* NEVER THROWS. Every failure mode here - missing file, garbage bytes, a JSON
|
|
24
|
+
* document of the wrong shape, an unwritable home - is an ANSWER, because the
|
|
25
|
+
* only caller that matters runs at startup and a throw there is a CLI that will
|
|
26
|
+
* not start because of a credential file. `readAccount()` answers `null`,
|
|
27
|
+
* `writeAccount()` answers `false`, `clearAccount()` answers whether the file is
|
|
28
|
+
* gone afterwards.
|
|
29
|
+
*/
|
|
30
|
+
import { randomUUID } from 'node:crypto';
|
|
31
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync, } from 'node:fs';
|
|
32
|
+
import process from 'node:process';
|
|
33
|
+
import { registerSecret } from '@aragon-agent/tui/model/logging/secret-registry';
|
|
34
|
+
import { getAccountPath, getHomeRoot } from '../config/app-paths.js';
|
|
35
|
+
import { getLogger } from '../logging/logger.js';
|
|
36
|
+
/** The only shape this module reads or writes (C3). */
|
|
37
|
+
export const ACCOUNT_SCHEMA_VERSION = 1;
|
|
38
|
+
/** POSIX mode for a file holding a bearer token. Ignored on win32. */
|
|
39
|
+
export const ACCOUNT_FILE_MODE = 0o600;
|
|
40
|
+
function isNonEmptyString(value) {
|
|
41
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
42
|
+
}
|
|
43
|
+
function isEpochMs(value) {
|
|
44
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0;
|
|
45
|
+
}
|
|
46
|
+
function readUser(value) {
|
|
47
|
+
if (!value || typeof value !== 'object')
|
|
48
|
+
return null;
|
|
49
|
+
const raw = value;
|
|
50
|
+
if (!isNonEmptyString(raw.id) || !isNonEmptyString(raw.username))
|
|
51
|
+
return null;
|
|
52
|
+
return {
|
|
53
|
+
id: raw.id,
|
|
54
|
+
username: raw.username,
|
|
55
|
+
...(isNonEmptyString(raw.email) ? { email: raw.email } : {}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function readScopes(value) {
|
|
59
|
+
if (!Array.isArray(value))
|
|
60
|
+
return null;
|
|
61
|
+
if (!value.every((entry) => typeof entry === 'string'))
|
|
62
|
+
return null;
|
|
63
|
+
return [...value];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Validate a parsed document against C3, or answer `null`.
|
|
67
|
+
*
|
|
68
|
+
* STRICT ON PURPOSE, NOT `Partial<>`-TOLERANT: every field here answers a
|
|
69
|
+
* question the user asks out loud ("who am I signed in as", "does this token
|
|
70
|
+
* still work"), so a document missing one of them is not a credential with a
|
|
71
|
+
* gap - it is a file this build does not understand, and `null` sends the caller
|
|
72
|
+
* down the documented `signed-out` path instead of printing `undefined`.
|
|
73
|
+
*
|
|
74
|
+
* AN UNKNOWN `version` IS ALSO `null`, deliberately: `2` means a newer CLI wrote
|
|
75
|
+
* a shape this build does not know, and guessing at it would be the one way to
|
|
76
|
+
* make an upgrade lose someone's login instead of asking them to log in again.
|
|
77
|
+
*/
|
|
78
|
+
function toAccountRecord(value) {
|
|
79
|
+
if (!value || typeof value !== 'object')
|
|
80
|
+
return null;
|
|
81
|
+
const raw = value;
|
|
82
|
+
if (raw.version !== ACCOUNT_SCHEMA_VERSION)
|
|
83
|
+
return null;
|
|
84
|
+
if (!isNonEmptyString(raw.baseUrl))
|
|
85
|
+
return null;
|
|
86
|
+
if (!isNonEmptyString(raw.token))
|
|
87
|
+
return null;
|
|
88
|
+
if (!isNonEmptyString(raw.tokenPrefix))
|
|
89
|
+
return null;
|
|
90
|
+
const user = readUser(raw.user);
|
|
91
|
+
if (user === null)
|
|
92
|
+
return null;
|
|
93
|
+
const scopes = readScopes(raw.scopes);
|
|
94
|
+
if (scopes === null)
|
|
95
|
+
return null;
|
|
96
|
+
if (!isEpochMs(raw.issuedAt) || !isEpochMs(raw.expiresAt))
|
|
97
|
+
return null;
|
|
98
|
+
return {
|
|
99
|
+
version: ACCOUNT_SCHEMA_VERSION,
|
|
100
|
+
baseUrl: raw.baseUrl,
|
|
101
|
+
token: raw.token,
|
|
102
|
+
tokenPrefix: raw.tokenPrefix,
|
|
103
|
+
user,
|
|
104
|
+
scopes,
|
|
105
|
+
issuedAt: raw.issuedAt,
|
|
106
|
+
expiresAt: raw.expiresAt,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Register the token with the redactor the moment it enters the process.
|
|
111
|
+
*
|
|
112
|
+
* Belt AND braces: `login-flow.ts` registers the token it receives, and this
|
|
113
|
+
* does it for a token READ BACK from disk, because the two cases that leak are
|
|
114
|
+
* "a stack trace printed the request" and "a debug record carried the parsed
|
|
115
|
+
* object", and both happen after a read. Registration is idempotent and
|
|
116
|
+
* process-lifetime (see `secret-registry.ts`).
|
|
117
|
+
*/
|
|
118
|
+
function rememberSecret(token) {
|
|
119
|
+
registerSecret(token);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The credential on disk, or `null`.
|
|
123
|
+
*
|
|
124
|
+
* `null` covers: no file, an empty file, a half-written file, a document that is
|
|
125
|
+
* not JSON, a JSON scalar, a document of the wrong shape and a document from a
|
|
126
|
+
* newer schema. Callers treat all of them the same way - "not signed in" - which
|
|
127
|
+
* is the only answer that cannot be wrong about a file it could not parse.
|
|
128
|
+
*/
|
|
129
|
+
export function readAccount() {
|
|
130
|
+
const path = getAccountPath();
|
|
131
|
+
if (!existsSync(path))
|
|
132
|
+
return null;
|
|
133
|
+
let text;
|
|
134
|
+
try {
|
|
135
|
+
text = readFileSync(path, 'utf-8');
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
getLogger().warn('cli', 'account.read_failed', { path, code: errorCodeOf(error) });
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
let parsed;
|
|
142
|
+
try {
|
|
143
|
+
parsed = JSON.parse(text);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
// Deliberately not an error record: a torn file is expected after a crash and
|
|
147
|
+
// the user-visible outcome is "signed out", which is not a fault to report.
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
const record = toAccountRecord(parsed);
|
|
151
|
+
if (record === null)
|
|
152
|
+
return null;
|
|
153
|
+
rememberSecret(record.token);
|
|
154
|
+
return record;
|
|
155
|
+
}
|
|
156
|
+
function errorCodeOf(error) {
|
|
157
|
+
const code = error?.code;
|
|
158
|
+
return typeof code === 'string' ? code : 'unknown';
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Write the credential atomically: temp file in the same directory, then rename.
|
|
162
|
+
*
|
|
163
|
+
* WHY RENAME AND NOT `writeFileSync(path)`: a write that dies half-way (a full
|
|
164
|
+
* disk, a killed process, a power cut) leaves a TRUNCATED JSON document, and the
|
|
165
|
+
* reader above would then report `signed-out` for a user who did nothing wrong.
|
|
166
|
+
* `rename` is atomic within a directory, so a reader sees either the old file or
|
|
167
|
+
* the new one - never a torn one.
|
|
168
|
+
*
|
|
169
|
+
* THE TEMP NAME CARRIES pid AND A RANDOM SUFFIX: two `aragon` processes logging
|
|
170
|
+
* in at once (a terminal and a desktop host) must not write the same temp file.
|
|
171
|
+
*
|
|
172
|
+
* `chmodSync` AFTER the rename, matching `config/ui-state.ts`: `rename` does not
|
|
173
|
+
* preserve the mode on every platform, and a 0600 temp file renamed into place
|
|
174
|
+
* is only 0600 by luck otherwise. win32 has no POSIX mode to set, so it is
|
|
175
|
+
* skipped there rather than warned about.
|
|
176
|
+
*/
|
|
177
|
+
export function writeAccount(record) {
|
|
178
|
+
const path = getAccountPath();
|
|
179
|
+
const tmp = `${path}.tmp-${process.pid}-${randomUUID().slice(0, 8)}`;
|
|
180
|
+
const body = `${JSON.stringify({ ...record, version: ACCOUNT_SCHEMA_VERSION }, null, 2)}\n`;
|
|
181
|
+
try {
|
|
182
|
+
mkdirSync(getHomeRoot(), { recursive: true });
|
|
183
|
+
writeFileSync(tmp, body, { encoding: 'utf-8', mode: ACCOUNT_FILE_MODE });
|
|
184
|
+
renameSync(tmp, path);
|
|
185
|
+
if (process.platform !== 'win32')
|
|
186
|
+
chmodSync(path, ACCOUNT_FILE_MODE);
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
try {
|
|
190
|
+
rmSync(tmp, { force: true });
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
// The temp file is already gone, or the directory is unwritable; either way
|
|
194
|
+
// there is nothing further to try and the answer is still `false`.
|
|
195
|
+
}
|
|
196
|
+
getLogger().warn('cli', 'account.write_failed', { path, code: errorCodeOf(error) });
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
rememberSecret(record.token);
|
|
200
|
+
getLogger().info('cli', 'account.saved', {
|
|
201
|
+
path,
|
|
202
|
+
username: record.user.username,
|
|
203
|
+
expiresAt: record.expiresAt,
|
|
204
|
+
scopes: record.scopes.length,
|
|
205
|
+
});
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Delete the credential. IDEMPOTENT: `true` means "there is no credential file
|
|
210
|
+
* now", which is also the answer when there never was one.
|
|
211
|
+
*
|
|
212
|
+
* `false` means the file is still there, and the only cause is a filesystem
|
|
213
|
+
* refusal (a Windows share with an open handle, a read-only directory). The
|
|
214
|
+
* caller may then say "sign-out failed" instead of lying.
|
|
215
|
+
*/
|
|
216
|
+
export function clearAccount() {
|
|
217
|
+
const path = getAccountPath();
|
|
218
|
+
const existed = existsSync(path);
|
|
219
|
+
try {
|
|
220
|
+
rmSync(path, { force: true });
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
getLogger().warn('cli', 'account.clear_failed', { path, code: errorCodeOf(error) });
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
if (existsSync(path))
|
|
227
|
+
return false;
|
|
228
|
+
if (existed)
|
|
229
|
+
getLogger().info('cli', 'account.cleared', { path });
|
|
230
|
+
return true;
|
|
231
|
+
}
|
package/dist/app/App.js
CHANGED
|
@@ -69,6 +69,10 @@ import { useGovernanceRounds } from './use-governance-rounds.js';
|
|
|
69
69
|
// deferred arming and the banner. Same adapter shape as `use-team-mode` above.
|
|
70
70
|
import { redTeamToastTtlMs, useRedTeamMode, } from './use-redteam-mode.js';
|
|
71
71
|
import { RedTeamDisclaimerOverlay } from '@aragon-agent/tui/ui/overlays/RedTeamDisclaimerOverlay';
|
|
72
|
+
// THE SIGN-IN PANEL (`/login`, C6): pure presentation, four phases. Imported beside the waiver
|
|
73
|
+
// overlay because it is the same kind of surface - one Ink component the App drives from
|
|
74
|
+
// `state.overlay`.
|
|
75
|
+
import { LoginOverlay } from '@aragon-agent/tui/ui/overlays/LoginOverlay';
|
|
72
76
|
import { teamStatusFromView } from '@aragon-agent/tui/model/team/team-mode';
|
|
73
77
|
import { FORCED_NUDGE_GRACE_MS } from '@aragon-agent/tui/model/team/limits';
|
|
74
78
|
import { TeamWakeDriver } from '../host/team-wake-driver.js';
|
|
@@ -86,6 +90,16 @@ import { INDEX_SKILL_NAME, buildIndexConfirmSummary, hasProjectIndex, indexBuild
|
|
|
86
90
|
import { installConsoleBridge } from '@aragon-agent/tui/ui/console-bridge';
|
|
87
91
|
import { publishExitSnapshot } from '@aragon-agent/tui/ui/exit-snapshot';
|
|
88
92
|
import { publishSessionView } from './session-bridge.js';
|
|
93
|
+
// THE DEVICE-FLOW ACCOUNT LAYER (`/login`, `/logout`: C3-C6). The network state machine and the
|
|
94
|
+
// 0600 credential file live in `account/`; this file owns only the panel's four-phase state and
|
|
95
|
+
// the promise that drives it. `setAccountCommandPort` is how the two commands reach the App -
|
|
96
|
+
// `CommandContext` has no account field and `commands/registry.ts` is outside this work package.
|
|
97
|
+
import { setAccountCommandPort, startupAccountNotice, } from '../commands/account-command.js';
|
|
98
|
+
import { readAccountStatus } from '../account/session.js';
|
|
99
|
+
import { clearAccount } from '../account/store.js';
|
|
100
|
+
import { formatIsoDate } from '../account/format.js';
|
|
101
|
+
import { startLogin } from '../account/login-flow.js';
|
|
102
|
+
import { getAccountPath } from '../config/app-paths.js';
|
|
89
103
|
import { OverlayFrame, overlayPageSize } from '@aragon-agent/tui/ui/layout/OverlayFrame';
|
|
90
104
|
import { helpRows } from '@aragon-agent/tui/ui/overlays/HelpOverlay';
|
|
91
105
|
import { ModelPicker } from '@aragon-agent/tui/ui/overlays/ModelPicker';
|
|
@@ -251,6 +265,15 @@ function firstUserText(entries) {
|
|
|
251
265
|
* shape of refusal as `tooSmall` - and the header keeps naming `Esc close`.
|
|
252
266
|
*/
|
|
253
267
|
const DECISION_OVERLAYS = new Set(['confirm', 'question', 'plan']);
|
|
268
|
+
/** The panel before anything has been asked of the server (`/login` and every retry start here). */
|
|
269
|
+
const INITIAL_LOGIN_VIEW = {
|
|
270
|
+
phase: 'prompt',
|
|
271
|
+
link: '',
|
|
272
|
+
code: '',
|
|
273
|
+
elapsedSec: 0,
|
|
274
|
+
account: null,
|
|
275
|
+
message: '',
|
|
276
|
+
};
|
|
254
277
|
/** The LAST finished assistant message's text, or `null` — the source for memory `l`. */
|
|
255
278
|
function lastAssistantText(entries) {
|
|
256
279
|
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
@@ -1691,6 +1714,154 @@ export function App({ controller, version, initialOverlay, initialPrompt, confir
|
|
|
1691
1714
|
// --- Helpers -----------------------------------------------------------
|
|
1692
1715
|
const notify = (level, text) => dispatch({ type: 'notice', level, text });
|
|
1693
1716
|
const toast = (level, text) => dispatch({ type: 'pushToast', level, text });
|
|
1717
|
+
// --- Device-flow sign-in (`/login`, C5 / C6) -----------------------------
|
|
1718
|
+
//
|
|
1719
|
+
// THE FLOW IS NOT THE PANEL, and that separation is the whole design. `startLogin` is a
|
|
1720
|
+
// promise THIS component owns; `state.overlay === 'login'` only decides whether the panel is
|
|
1721
|
+
// on screen. So `Esc` hides the panel and the device poll keeps running (C6: "waiting Esc =
|
|
1722
|
+
// onClose, the flow does not stop"), and the outcome is announced from the promise's
|
|
1723
|
+
// continuation - which is the only place that still knows about it after a hide.
|
|
1724
|
+
const [loginView, setLoginView] = useState(INITIAL_LOGIN_VIEW);
|
|
1725
|
+
/** ONE FLOW AT A TIME: a second Enter must not start a second authorization. */
|
|
1726
|
+
const loginBusy = useRef(false);
|
|
1727
|
+
/** The abort handle, so UNMOUNTING stops a poll nobody can see. `Esc` never touches it. */
|
|
1728
|
+
const loginAbort = useRef(null);
|
|
1729
|
+
/** The startup-notice latch: spec 6.6 asks for ONE prompt per launch, not one per render. */
|
|
1730
|
+
const accountNoticeShown = useRef(false);
|
|
1731
|
+
/** `/login` and the panel's `prompt` phase: show the panel, ask the server nothing yet. */
|
|
1732
|
+
const openLoginPanel = () => {
|
|
1733
|
+
// A FLOW ALREADY IN FLIGHT IS NOT RESET. `/login` typed while the device poll is waiting
|
|
1734
|
+
// (a second terminal, a second thought) has to show the code that is still valid; dropping
|
|
1735
|
+
// back to `prompt` would hide it behind an Enter that `startLoginFlow` then refuses, and
|
|
1736
|
+
// the panel would look broken while the sign-in was in fact progressing.
|
|
1737
|
+
if (!loginBusy.current)
|
|
1738
|
+
setLoginView(INITIAL_LOGIN_VIEW);
|
|
1739
|
+
dispatch({ type: 'setOverlay', overlay: 'login' });
|
|
1740
|
+
};
|
|
1741
|
+
/**
|
|
1742
|
+
* Apply a finished flow: the success toast plus the durable transcript notice, or the
|
|
1743
|
+
* `error` phase carrying the account layer's own sentence.
|
|
1744
|
+
*
|
|
1745
|
+
* THE SUCCESS NOTICE IS DISPATCHED HERE RATHER THAN BY THE PANEL because `Esc` may have
|
|
1746
|
+
* hidden the panel minutes ago; a 20-day credential that quietly landed on disk is the one
|
|
1747
|
+
* outcome a user must not be able to miss.
|
|
1748
|
+
*/
|
|
1749
|
+
const settleLogin = (outcome) => {
|
|
1750
|
+
if (!mounted.current)
|
|
1751
|
+
return;
|
|
1752
|
+
if (!outcome.ok) {
|
|
1753
|
+
setLoginView((view) => ({ ...view, phase: 'error', message: outcome.message }));
|
|
1754
|
+
return;
|
|
1755
|
+
}
|
|
1756
|
+
const { username, expiresAt } = outcome.account;
|
|
1757
|
+
setLoginView({
|
|
1758
|
+
phase: 'success', link: '', code: '', elapsedSec: 0,
|
|
1759
|
+
account: { username, expiresAt }, message: '',
|
|
1760
|
+
});
|
|
1761
|
+
toast('success', `Signed in as ${username}.`);
|
|
1762
|
+
notify('info', `${username} signed in. The credential expires on ${formatIsoDate(expiresAt)}; ` +
|
|
1763
|
+
'run /login any time to renew it.');
|
|
1764
|
+
};
|
|
1765
|
+
/**
|
|
1766
|
+
* Run one device-flow login, DETACHED from the composer (the panel's Enter, and every retry).
|
|
1767
|
+
*
|
|
1768
|
+
* `waiting` IS SET BEFORE THE FIRST REQUEST on purpose: the panel must never sit on the
|
|
1769
|
+
* `prompt` phase (whose Enter is live) while a POST it already started is in flight, or a
|
|
1770
|
+
* second Enter would open a second authorization.
|
|
1771
|
+
*/
|
|
1772
|
+
const startLoginFlow = async () => {
|
|
1773
|
+
if (loginBusy.current) {
|
|
1774
|
+
toast('info', 'A sign-in is already in progress.');
|
|
1775
|
+
return;
|
|
1776
|
+
}
|
|
1777
|
+
loginBusy.current = true;
|
|
1778
|
+
const abort = new AbortController();
|
|
1779
|
+
loginAbort.current = abort;
|
|
1780
|
+
setLoginView({ ...INITIAL_LOGIN_VIEW, phase: 'waiting' });
|
|
1781
|
+
let outcome = null;
|
|
1782
|
+
try {
|
|
1783
|
+
outcome = await startLogin({
|
|
1784
|
+
signal: abort.signal,
|
|
1785
|
+
onCode: (code) => {
|
|
1786
|
+
if (!mounted.current)
|
|
1787
|
+
return;
|
|
1788
|
+
setLoginView({
|
|
1789
|
+
phase: 'waiting', link: code.link, code: code.code,
|
|
1790
|
+
elapsedSec: 0, account: null, message: '',
|
|
1791
|
+
});
|
|
1792
|
+
},
|
|
1793
|
+
// The flow's own clock: every poll iteration reports how long we have waited, so the
|
|
1794
|
+
// panel's seconds come from the same timer the polls are scheduled on.
|
|
1795
|
+
onProgress: ({ elapsedSec }) => {
|
|
1796
|
+
if (!mounted.current)
|
|
1797
|
+
return;
|
|
1798
|
+
setLoginView((view) => (view.phase === 'waiting' ? { ...view, elapsedSec } : view));
|
|
1799
|
+
},
|
|
1800
|
+
});
|
|
1801
|
+
}
|
|
1802
|
+
catch (error) {
|
|
1803
|
+
// `startLogin` promises never to throw; this is the honest answer if that ever changes,
|
|
1804
|
+
// because a rejection here would otherwise vanish as an unhandled promise rejection.
|
|
1805
|
+
getLogger().warn('cli', 'account.login_threw', {
|
|
1806
|
+
reason: error instanceof Error ? error.name : 'unknown',
|
|
1807
|
+
});
|
|
1808
|
+
}
|
|
1809
|
+
finally {
|
|
1810
|
+
loginBusy.current = false;
|
|
1811
|
+
loginAbort.current = null;
|
|
1812
|
+
}
|
|
1813
|
+
if (!mounted.current)
|
|
1814
|
+
return;
|
|
1815
|
+
if (outcome === null) {
|
|
1816
|
+
setLoginView((view) => ({
|
|
1817
|
+
...view,
|
|
1818
|
+
phase: 'error',
|
|
1819
|
+
message: 'Sign-in failed unexpectedly. Run /login to try again.',
|
|
1820
|
+
}));
|
|
1821
|
+
return;
|
|
1822
|
+
}
|
|
1823
|
+
settleLogin(outcome);
|
|
1824
|
+
};
|
|
1825
|
+
/**
|
|
1826
|
+
* Install the App's account port and contribute NO context fields.
|
|
1827
|
+
*
|
|
1828
|
+
* A SPREAD FOR ITS SIDE EFFECT, which is unusual enough to justify in place: `CommandContext`
|
|
1829
|
+
* has no account field and `commands/registry.ts` (where it is declared) is outside this work
|
|
1830
|
+
* package, so the two commands read the port from `commands/account-command.ts`'s own
|
|
1831
|
+
* registration seam instead. Installing it from `makeCtx` means the port always belongs to
|
|
1832
|
+
* the App that is dispatching the command, and a build that never builds a context
|
|
1833
|
+
* (headless, a sub-agent, a host double) leaves the commands on their honest "not available"
|
|
1834
|
+
* branch.
|
|
1835
|
+
*/
|
|
1836
|
+
const installAccountPort = () => {
|
|
1837
|
+
setAccountCommandPort({
|
|
1838
|
+
status: () => readAccountStatus(),
|
|
1839
|
+
accountPath: getAccountPath,
|
|
1840
|
+
openLogin: openLoginPanel,
|
|
1841
|
+
signOut: () => {
|
|
1842
|
+
const hadCredential = readAccountStatus().state !== 'signed-out';
|
|
1843
|
+
return { ok: clearAccount(), hadCredential };
|
|
1844
|
+
},
|
|
1845
|
+
});
|
|
1846
|
+
return {};
|
|
1847
|
+
};
|
|
1848
|
+
// THE ONE STARTUP LINE (spec 6.6): read the credential once, say ONE thing about it, and
|
|
1849
|
+
// stay completely silent for `signed-out` and `valid`. The latch is a ref rather than state
|
|
1850
|
+
// because a re-render must not replay it, and it is set BEFORE the read so nothing in the
|
|
1851
|
+
// account layer can turn this into a notice on every frame.
|
|
1852
|
+
useEffect(() => {
|
|
1853
|
+
if (accountNoticeShown.current)
|
|
1854
|
+
return;
|
|
1855
|
+
accountNoticeShown.current = true;
|
|
1856
|
+
const text = startupAccountNotice(readAccountStatus());
|
|
1857
|
+
if (text !== null)
|
|
1858
|
+
notify('info', text);
|
|
1859
|
+
// Mount-only by design: `/login` and `/logout` report for themselves.
|
|
1860
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1861
|
+
}, []);
|
|
1862
|
+
// A poll nobody can see must not outlive the tree that could show it. This is the ONLY
|
|
1863
|
+
// abort: `Esc` deliberately leaves the flow running (C6).
|
|
1864
|
+
useEffect(() => () => loginAbort.current?.abort(), []);
|
|
1694
1865
|
// --- Quick prompt library: two one-shot channels and the store port ------
|
|
1695
1866
|
const refreshPromptLibrary = useCallback(() => setPromptLibrary(loadPromptLibrary()), []);
|
|
1696
1867
|
/**
|
|
@@ -2282,6 +2453,10 @@ export function App({ controller, version, initialOverlay, initialPrompt, confir
|
|
|
2282
2453
|
};
|
|
2283
2454
|
const invocationCardsOn = invocationCardsEnabled();
|
|
2284
2455
|
const makeCtx = (args) => ({
|
|
2456
|
+
// INSTALLED FIRST, BEFORE ANY FIELD IS READ, so `/login` and `/logout` can never run against
|
|
2457
|
+
// a port from a previous App instance. See `installAccountPort` for why this is a spread and
|
|
2458
|
+
// not a field on `CommandContext`.
|
|
2459
|
+
...installAccountPort(),
|
|
2285
2460
|
args,
|
|
2286
2461
|
invocationCards: invocationCardsOn,
|
|
2287
2462
|
requestCopy: copyCoordinator.requestCopy,
|
|
@@ -3344,6 +3519,13 @@ export function App({ controller, version, initialOverlay, initialPrompt, confir
|
|
|
3344
3519
|
const overlay = stateRef.current.overlay;
|
|
3345
3520
|
if (overlay === 'settings')
|
|
3346
3521
|
return;
|
|
3522
|
+
// THE SIGN-IN PANEL OWNS ITS `Esc` (C6). `App` steps aside rather than closing it here:
|
|
3523
|
+
// the panel's own handler calls `onClose`, which hides it and NOTHING ELSE. Closing it in
|
|
3524
|
+
// both places would be one state change twice, and it would put the abort handle within
|
|
3525
|
+
// reach of this branch - which is exactly the coupling C6 forbids (Esc must not stop the
|
|
3526
|
+
// device flow).
|
|
3527
|
+
if (overlay === 'login')
|
|
3528
|
+
return;
|
|
3347
3529
|
// THE MEMORY MANAGER OWNS ITS Esc, like settings: the editor's first Esc only ARMS the
|
|
3348
3530
|
// discard, and closing the overlay here as well would throw the half-typed note away on
|
|
3349
3531
|
// that first press (AC-31). Only when it is actually mounted: with no port nothing renders
|
|
@@ -3851,7 +4033,15 @@ export function App({ controller, version, initialOverlay, initialPrompt, confir
|
|
|
3851
4033
|
// THE WAIVER (red-team-security-mode 4.3). Rendered from the reducer's overlay field like
|
|
3852
4034
|
// every other surface, so it cannot be open at the same time as a decision dialog - the
|
|
3853
4035
|
// requests that would have replaced it are queued in `queuedDecisions` instead (D-21).
|
|
3854
|
-
_jsx(RedTeamDisclaimerOverlay, { isActive: !tooSmall, cols: viewCols, maxRows: overlayMaxRows, theme: theme, caps: caps, onAccept: acceptRedTeamWaiver, onDecline: declineRedTeamWaiver })) : overlay === '
|
|
4036
|
+
_jsx(RedTeamDisclaimerOverlay, { isActive: !tooSmall, cols: viewCols, maxRows: overlayMaxRows, theme: theme, caps: caps, onAccept: acceptRedTeamWaiver, onDecline: declineRedTeamWaiver })) : overlay === 'login' ? (
|
|
4037
|
+
// THE SIGN-IN PANEL (C6). THE BRANCH AND THE `Overlay` MEMBER SHIP TOGETHER (R-11), like
|
|
4038
|
+
// `agents` and `governance` above: a missing branch is not a type error, it is a SILENT
|
|
4039
|
+
// BLANK FRAME behind a keyboard that `active={overlay === null}` has already disabled.
|
|
4040
|
+
//
|
|
4041
|
+
// `onStart` runs the flow; `onClose` HIDES the panel and nothing else - the device poll it
|
|
4042
|
+
// started keeps running and reports through `settleLogin`, which is why a success notice
|
|
4043
|
+
// can still arrive after the user pressed Esc.
|
|
4044
|
+
_jsx(LoginOverlay, { isActive: !tooSmall, phase: loginView.phase, link: loginView.link, code: loginView.code, elapsedSec: loginView.elapsedSec, ...(loginView.account === null ? {} : { account: loginView.account }), ...(loginView.message === '' ? {} : { message: loginView.message }), maxRows: overlayMaxRows, cols: viewCols, theme: theme, caps: caps, onStart: () => { void startLoginFlow(); }, onClose: () => dispatch({ type: 'setOverlay', overlay: null }) })) : overlay === 'confirm' && confirmState ? (_jsx(ConfirmDialog, { isActive: !tooSmall, state: confirmState, maxRows: overlayMaxRows, cols: viewCols, theme: theme, caps: caps, onClose: closeConfirm })) : overlay === 'question' && humanRequest?.kind === 'questions' ? (_jsx(QuestionOverlay, { isActive: !tooSmall, questions: humanRequest.questions, maxRows: overlayMaxRows, cols: viewCols, theme: theme, caps: caps, onSubmit: (answers) => resolveHuman({ kind: 'answers', answers, cancelled: false }), ...interviewProps })) : overlay === 'plan' && humanRequest?.kind === 'plan' ? (_jsx(PlanReviewOverlay, { isActive: !tooSmall, plan: humanRequest.plan, maxRows: overlayMaxRows, cols: viewCols, scrollOffset: overlayScroll, onScrollClamp: setOverlayScroll, theme: theme, caps: caps, onVerdict: handlePlanVerdict })) : null;
|
|
3855
4045
|
const density = cfg.density;
|
|
3856
4046
|
const transcriptWindow = cfg.transcriptWindow ?? DEFAULT_TRANSCRIPT_WINDOW;
|
|
3857
4047
|
// The fixed status bar remains visible underneath every overlay and toast.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/login` and `/logout` (C5): the two slash commands over the CLI's account layer.
|
|
3
|
+
*
|
|
4
|
+
* WHAT LIVES HERE AND WHY. The command body lives in its own module for the reason
|
|
5
|
+
* `agents-command.ts`, `kit-command.ts` and `queue-command.ts` record: `builtins.ts` is past the
|
|
6
|
+
* thousand-line guideline. The network and the credential file are NOT here either - they are
|
|
7
|
+
* `account/login-flow.ts` and `account/store.ts`; this module turns a status into transcript
|
|
8
|
+
* lines and asks the App to open the sign-in panel.
|
|
9
|
+
*
|
|
10
|
+
* THE PORT SEAM (`AccountCommandPort`), AND WHY IT IS A MODULE-LEVEL REGISTRATION rather than a
|
|
11
|
+
* field on `CommandContext`: this work package owns this file and `App.tsx` but NOT
|
|
12
|
+
* `commands/registry.ts`, where `CommandContext` is declared, and adding a required field to it
|
|
13
|
+
* would break the dozens of fixtures in this package that build the context by literal. The App
|
|
14
|
+
* installs the port from `makeCtx` and replaces it on every command run, so a stale App instance
|
|
15
|
+
* cannot outlive its own panel. With NO port installed the two commands answer honestly instead
|
|
16
|
+
* of throwing or reaching for the network - which is what a headless context, a sub-agent and a
|
|
17
|
+
* host double must get.
|
|
18
|
+
*
|
|
19
|
+
* ASCII ONLY - `commands/` is inside `glyphs.test.ts::inScope`, and these strings reach a
|
|
20
|
+
* legacy console.
|
|
21
|
+
*/
|
|
22
|
+
import type { CommandRegistry } from './registry.js';
|
|
23
|
+
import type { AccountStatus } from '../account/session.js';
|
|
24
|
+
/** C5, verbatim. `catalog.ts` and `BUILTIN_COMMAND_NAMES` carry the same bytes. */
|
|
25
|
+
export declare const LOGIN_DESCRIPTION = "Sign in with your aragonagent.com account (20 days); no args shows status";
|
|
26
|
+
/** C5, verbatim. */
|
|
27
|
+
export declare const LOGOUT_DESCRIPTION = "Sign out and delete the local account credential";
|
|
28
|
+
/** What `/logout` did. `ok: false` means the credential file is still there. */
|
|
29
|
+
export interface AccountSignOutResult {
|
|
30
|
+
readonly ok: boolean;
|
|
31
|
+
/** Whether a credential existed before the delete, so "nothing to do" is not "deleted". */
|
|
32
|
+
readonly hadCredential: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The App's side of the feature: read the credential, open the panel, delete the credential.
|
|
36
|
+
*
|
|
37
|
+
* THREE METHODS, NOT ONE, because they are three different user intentions and the command has
|
|
38
|
+
* to report each one truthfully: `status` is a read, `openLogin` starts a device flow the App
|
|
39
|
+
* owns (so `/login` returns before the person has touched their phone), and `signOut` must be
|
|
40
|
+
* able to answer "nothing to delete" as well as "still there, the filesystem refused".
|
|
41
|
+
*/
|
|
42
|
+
export interface AccountCommandPort {
|
|
43
|
+
/** Fresh read. The App reads the file, so `/login` never shows a cached credential. */
|
|
44
|
+
status(): AccountStatus;
|
|
45
|
+
/** Absolute path of `<home>/account.json`, for the reports below. */
|
|
46
|
+
accountPath(): string;
|
|
47
|
+
/** Open the four-phase panel and run `startLogin` in the background (C6). */
|
|
48
|
+
openLogin(): void;
|
|
49
|
+
signOut(): AccountSignOutResult;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Install (or clear) the App's account port.
|
|
53
|
+
*
|
|
54
|
+
* CALLED FROM `App.tsx::makeCtx`, so the port always belongs to the App that is running the
|
|
55
|
+
* command. `null` removes it, which is how a test - or an App that unmounts - leaves the two
|
|
56
|
+
* commands answering the honest "not available" branch.
|
|
57
|
+
*/
|
|
58
|
+
export declare function setAccountCommandPort(port: AccountCommandPort | null): void;
|
|
59
|
+
/** The installed port, or `null` in a context that has none. */
|
|
60
|
+
export declare function getAccountCommandPort(): AccountCommandPort | null;
|
|
61
|
+
/**
|
|
62
|
+
* The report `/login` prints before it opens the panel: the state first, then the three facts a
|
|
63
|
+
* user can act on.
|
|
64
|
+
*
|
|
65
|
+
* THE TOKEN IS NEVER ONE OF THEM. `tokenPrefix` is what `account.json` already stores for
|
|
66
|
+
* exactly this purpose (`sha256` lives on the server; the prefix is the identifier a person
|
|
67
|
+
* recognises), and it is printed with a trailing `...` so nobody mistakes it for the secret.
|
|
68
|
+
* The full token is not reachable from `AccountStatus` at all, which is the structural half of
|
|
69
|
+
* "never show a plaintext token": this module has no API that could return one.
|
|
70
|
+
*/
|
|
71
|
+
export declare function accountStatusLines(status: AccountStatus, accountPath: string): string[];
|
|
72
|
+
/**
|
|
73
|
+
* The ONE startup line for the credential, or `null` when there is nothing to say (spec 6.6).
|
|
74
|
+
*
|
|
75
|
+
* `expiring` gets the account layer's own wording (it names the days left and `/login`);
|
|
76
|
+
* `expired` gets `describeExpiry`, because a credential that stopped working is not "expiring"
|
|
77
|
+
* and the user has to be told to sign in again rather than "renew". `signed-out` and `valid`
|
|
78
|
+
* stay completely silent: a terminal that greets a guest with a sign-in pitch on every launch is
|
|
79
|
+
* nagging, and the panel is one keystroke away.
|
|
80
|
+
*
|
|
81
|
+
* RETURNING A STRING RATHER THAN DISPATCHING keeps the "one notice per launch" rule in the App
|
|
82
|
+
* (a ref guard there) and lets this be asserted without mounting a terminal.
|
|
83
|
+
*/
|
|
84
|
+
export declare function startupAccountNotice(status: AccountStatus): string | null;
|
|
85
|
+
/** Register both commands. Unconditional, so they can always explain a missing port. */
|
|
86
|
+
export declare function registerAccountCommands(registry: CommandRegistry): void;
|