@dennisonbertram/agent-drive 0.1.1 → 0.2.1
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 +15 -2
- package/dist/SKILL.md +23 -3
- package/dist/account-seal.d.ts +124 -0
- package/dist/account-seal.js +575 -0
- package/dist/account-seal.js.map +1 -0
- package/dist/backup.d.ts +9 -4
- package/dist/backup.js +329 -44
- package/dist/backup.js.map +1 -1
- package/dist/capture-spool.d.ts +10 -4
- package/dist/capture-spool.js +124 -41
- package/dist/capture-spool.js.map +1 -1
- package/dist/capture.d.ts +1 -0
- package/dist/capture.js +15 -2
- package/dist/capture.js.map +1 -1
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +307 -29
- package/dist/cli.js.map +1 -1
- package/dist/client.d.ts +40 -3
- package/dist/client.js +268 -16
- package/dist/client.js.map +1 -1
- package/dist/config.d.ts +4 -2
- package/dist/config.js +213 -27
- package/dist/config.js.map +1 -1
- package/dist/credentials.d.ts +12 -2
- package/dist/credentials.js +97 -39
- package/dist/credentials.js.map +1 -1
- package/dist/crypto-format.d.ts +8 -0
- package/dist/crypto-format.js +170 -15
- package/dist/crypto-format.js.map +1 -1
- package/dist/crypto.d.ts +6 -0
- package/dist/crypto.js +42 -1
- package/dist/crypto.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/mcp.js +81 -30
- package/dist/mcp.js.map +1 -1
- package/dist/node-transfer.d.ts +15 -0
- package/dist/node-transfer.js +205 -0
- package/dist/node-transfer.js.map +1 -0
- package/dist/seal.d.ts +7 -2
- package/dist/seal.js +12 -8
- package/dist/seal.js.map +1 -1
- package/dist/sync.d.ts +1 -1
- package/dist/sync.js +43 -11
- package/dist/sync.js.map +1 -1
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
2
3
|
import { parseArgs } from 'node:util';
|
|
3
|
-
import { readFile
|
|
4
|
+
import { readFile } from 'node:fs/promises';
|
|
4
5
|
import { AgentDriveClient, AgentDriveApiError, publicOutput } from './client.js';
|
|
5
6
|
import { generateDriveKey } from './crypto.js';
|
|
6
7
|
import { apiErrorLines, createdDriveNote, localKeyMap, notBackedUpMessage, pickCheckDrive, runCheck, backupCandidates, } from './check.js';
|
|
7
8
|
import { BACKUP_HINT, confirmCredentials, exportCredentials, importCredentials, issueCredentials, loadConfig, reportEnvBackups, unbackedUp, reportKeyBackup, } from './credentials.js';
|
|
8
9
|
import { installSkill, packagedSkillPath } from './skill.js';
|
|
9
|
-
import { configPath, driveKeyEnvironmentName, machineName, resolveLoginApiUrl, resolveDriveId, resolveConnection, resolveDriveKey, writeConfig, } from './config.js';
|
|
10
|
+
import { configPath, driveKeyEnvironmentName, keysForOrigin, machineName, readConfig, resolveLoginApiUrl, resolveDriveId, resolveConnection, resolveDriveKey, writeConfig, } from './config.js';
|
|
11
|
+
import { autoSealHeldKeys, clearAgentOnlyIntent, heldSealTargets, pendingAgentOnlyNames, recordAgentOnlyDrive, recordAgentOnlyIntent, sealClientWithDeadline, sealDrivesToAccount, UNKNOWN_TYPE_HINT, } from './account-seal.js';
|
|
10
12
|
import { syncDirectory } from './sync.js';
|
|
11
13
|
import { backupDirectory } from './backup.js';
|
|
12
14
|
import { CLIPBOARD_CLEAR_SECONDS } from './backup-targets.js';
|
|
13
15
|
import { defaultAgentName, deviceLogin, grantLogin, shouldOpenBrowser } from './device.js';
|
|
14
16
|
import { runMcp } from './mcp.js';
|
|
15
17
|
import { runCapture } from './capture.js';
|
|
18
|
+
import { getFile, putFile } from './node-transfer.js';
|
|
19
|
+
// Resolves to the package root from both src/ and dist/.
|
|
20
|
+
const { version: cliVersion } = createRequire(import.meta.url)('../package.json');
|
|
16
21
|
function parse(raw) {
|
|
17
22
|
return parseArgs({
|
|
18
23
|
args: raw,
|
|
@@ -47,6 +52,9 @@ function parse(raw) {
|
|
|
47
52
|
'expires-in': { type: 'string' },
|
|
48
53
|
'no-browser': { type: 'boolean' },
|
|
49
54
|
'dry-run': { type: 'boolean' },
|
|
55
|
+
exclude: { type: 'string', multiple: true },
|
|
56
|
+
gitignore: { type: 'boolean' },
|
|
57
|
+
yes: { type: 'boolean' },
|
|
50
58
|
to: { type: 'string' },
|
|
51
59
|
from: { type: 'string' },
|
|
52
60
|
force: { type: 'boolean' },
|
|
@@ -56,6 +64,8 @@ function parse(raw) {
|
|
|
56
64
|
'read-only': { type: 'boolean' },
|
|
57
65
|
method: { type: 'string' },
|
|
58
66
|
target: { type: 'string' },
|
|
67
|
+
'agent-only': { type: 'boolean' },
|
|
68
|
+
repin: { type: 'boolean' },
|
|
59
69
|
},
|
|
60
70
|
allowPositionals: true,
|
|
61
71
|
strict: true,
|
|
@@ -88,8 +98,35 @@ async function stdinText() {
|
|
|
88
98
|
process.stdin.on('error', reject);
|
|
89
99
|
});
|
|
90
100
|
}
|
|
101
|
+
// Automatic sealing runs in the background with its own deadline-bounded client so it can never
|
|
102
|
+
// delay or fail the command it piggybacks on. Commands only SCHEDULE seal work here; `done`
|
|
103
|
+
// starts it after the command's output has been written and then waits on it so tests can
|
|
104
|
+
// observe it.
|
|
105
|
+
const pendingSeals = new Set();
|
|
106
|
+
const scheduledSeals = [];
|
|
107
|
+
function sealInBackground(work) {
|
|
108
|
+
const task = Promise.resolve()
|
|
109
|
+
.then(work)
|
|
110
|
+
.catch(() => { })
|
|
111
|
+
.finally(() => pendingSeals.delete(task));
|
|
112
|
+
pendingSeals.add(task);
|
|
113
|
+
}
|
|
114
|
+
/** Deadline-bounded sealing hook shared by connect/backup/create/key-change paths. */
|
|
115
|
+
function sealHook(connection, opts) {
|
|
116
|
+
scheduledSeals.push(async () => {
|
|
117
|
+
const seal = sealClientWithDeadline(connection.apiUrl, connection.apiKey, machineName());
|
|
118
|
+
await autoSealHeldKeys({
|
|
119
|
+
client: seal.client,
|
|
120
|
+
config: connection.config,
|
|
121
|
+
...opts,
|
|
122
|
+
deadline: seal.deadline,
|
|
123
|
+
deadlineAt: seal.deadlineAt,
|
|
124
|
+
warn: (line) => process.stderr.write(`${line}\n`),
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
91
128
|
async function connect(args) {
|
|
92
|
-
const connection = await resolveConnection();
|
|
129
|
+
const connection = await resolveConnection(args.values.api);
|
|
93
130
|
await reportEnvBackups(connection.apiUrl, connection.apiKey);
|
|
94
131
|
// Kept apart from args.values.drive: `check` has its own selection when --drive is omitted.
|
|
95
132
|
try {
|
|
@@ -100,18 +137,59 @@ async function connect(args) {
|
|
|
100
137
|
}
|
|
101
138
|
const drive = args.values.drive?.[0] ?? defaultDrive;
|
|
102
139
|
const driveKey = drive
|
|
103
|
-
? await resolveDriveKey(drive, args.values['key-file'], connection.config)
|
|
140
|
+
? await resolveDriveKey(drive, connection.apiUrl, args.values['key-file'], connection.config)
|
|
104
141
|
: undefined;
|
|
105
|
-
|
|
142
|
+
const client = new AgentDriveClient({
|
|
106
143
|
apiUrl: connection.apiUrl,
|
|
107
144
|
apiKey: connection.apiKey,
|
|
108
145
|
machine: machineName(),
|
|
109
146
|
...(driveKey ? { driveKey } : {}),
|
|
110
147
|
});
|
|
148
|
+
// Throttled (24h/drive) account-key sealing on any command that loads a drive key. The exact
|
|
149
|
+
// key the command resolved goes with it, so sealing never falls back to a different stored key.
|
|
150
|
+
if (driveKey && drive)
|
|
151
|
+
sealHook(connection, {
|
|
152
|
+
selectedDriveId: drive,
|
|
153
|
+
keys: { [drive]: driveKey },
|
|
154
|
+
...(args.values['key-file'] ? { keyFile: args.values['key-file'] } : {}),
|
|
155
|
+
});
|
|
156
|
+
// Callers that seal later work (drive create) must reuse this exact connection: resolving
|
|
157
|
+
// again could seal through an account or origin that changed since the command connected.
|
|
158
|
+
return { client, connection };
|
|
111
159
|
}
|
|
112
160
|
// Set by connect() when no --drive was given and no single default exists.
|
|
113
161
|
let missingDrive;
|
|
114
162
|
let defaultDrive;
|
|
163
|
+
/** Forced account-key seal after login/grant/import put drive keys on this machine. */
|
|
164
|
+
function sealAfterKeyChange(connection) {
|
|
165
|
+
scheduledSeals.push(async () => {
|
|
166
|
+
const seal = sealClientWithDeadline(connection.apiUrl, connection.apiKey, machineName());
|
|
167
|
+
await autoSealHeldKeys({
|
|
168
|
+
client: seal.client,
|
|
169
|
+
config: connection.config,
|
|
170
|
+
force: true,
|
|
171
|
+
deadline: seal.deadline,
|
|
172
|
+
deadlineAt: seal.deadlineAt,
|
|
173
|
+
warn: (line) => process.stderr.write(`${line}\n`),
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
async function sealSavedConnectionAfterKeyChange() {
|
|
178
|
+
const saved = await readConfig();
|
|
179
|
+
if (!saved.apiUrl || !saved.apiKey)
|
|
180
|
+
return;
|
|
181
|
+
let config = saved;
|
|
182
|
+
try {
|
|
183
|
+
// Keep the imported/saved connection as the seal client, but include valid in-scope
|
|
184
|
+
// environment-bundle keys using the same origin-aware merge as ordinary config loads.
|
|
185
|
+
config = await loadConfig(saved.apiUrl, saved.apiKey);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
// A malformed or unrelated environment bundle must not turn a successful import into a
|
|
189
|
+
// command failure. The saved keys remain safe to seal with their saved connection.
|
|
190
|
+
}
|
|
191
|
+
sealAfterKeyChange({ apiUrl: saved.apiUrl, apiKey: saved.apiKey, config });
|
|
192
|
+
}
|
|
115
193
|
function requiredDrive(args) {
|
|
116
194
|
const id = args.values.drive?.[0] ?? defaultDrive;
|
|
117
195
|
if (!id)
|
|
@@ -148,10 +226,21 @@ async function run(command, raw) {
|
|
|
148
226
|
command: wrapped,
|
|
149
227
|
...(flags.values.agent ? { agent: flags.values.agent } : {}),
|
|
150
228
|
...(flags.values.drive?.[0] ? { drive: flags.values.drive[0] } : {}),
|
|
229
|
+
...(flags.values.api ? { apiUrl: flags.values.api } : {}),
|
|
151
230
|
});
|
|
152
231
|
return;
|
|
153
232
|
}
|
|
154
233
|
const args = parse(raw);
|
|
234
|
+
// One flag set is parsed for every command, so backup-only flags parse for sync
|
|
235
|
+
// too — and would be silently ignored, turning `sync --dry-run` into a real
|
|
236
|
+
// upload. Reject them before connect() performs any key or network lookup.
|
|
237
|
+
if (command === 'sync') {
|
|
238
|
+
const backupOnly = ['dry-run', 'exclude', 'gitignore']
|
|
239
|
+
.filter((name) => args.values[name] !== undefined)
|
|
240
|
+
.map((name) => `--${name}`);
|
|
241
|
+
if (backupOnly.length)
|
|
242
|
+
throw new Error(`sync does not support ${backupOnly.join(' ')}; use backup instead.`);
|
|
243
|
+
}
|
|
155
244
|
if (command === 'login') {
|
|
156
245
|
const apiUrl = await resolveLoginApiUrl(args.values.api);
|
|
157
246
|
const apiKey = process.env.AGENT_DRIVE_API_KEY ?? (args.values['token-stdin'] ? await stdinText() : '');
|
|
@@ -163,6 +252,7 @@ async function run(command, raw) {
|
|
|
163
252
|
log: (line) => process.stdout.write(`${line}\n`),
|
|
164
253
|
openBrowser: shouldOpenBrowser(args.values['no-browser']),
|
|
165
254
|
});
|
|
255
|
+
await sealSavedConnectionAfterKeyChange();
|
|
166
256
|
return;
|
|
167
257
|
}
|
|
168
258
|
const client = new AgentDriveClient({ apiUrl, apiKey, machine: machineName() });
|
|
@@ -176,20 +266,31 @@ async function run(command, raw) {
|
|
|
176
266
|
if (command === 'backup') {
|
|
177
267
|
if (!args.positionals[0])
|
|
178
268
|
throw new Error('backup requires LOCAL_DIRECTORY.');
|
|
179
|
-
const connection = await resolveConnection();
|
|
269
|
+
const connection = await resolveConnection(args.values.api);
|
|
180
270
|
await reportEnvBackups(connection.apiUrl, connection.apiKey);
|
|
181
271
|
const driveId = resolveDriveId(args.values.drive?.[0], connection.config, connection.apiUrl);
|
|
182
|
-
const key = await resolveDriveKey(driveId, args.values['key-file'], connection.config);
|
|
272
|
+
const key = await resolveDriveKey(driveId, connection.apiUrl, args.values['key-file'], connection.config);
|
|
273
|
+
const dryRun = args.values['dry-run'] ?? false;
|
|
274
|
+
// Throttled account-key sealing, same as the generic connect() path (backup bypasses it).
|
|
275
|
+
// A dry run previews the upload and must not seal — it neither creates nor holds anything.
|
|
276
|
+
if (key && !dryRun)
|
|
277
|
+
sealHook(connection, {
|
|
278
|
+
selectedDriveId: driveId,
|
|
279
|
+
keys: { [driveId]: key },
|
|
280
|
+
...(args.values['key-file'] ? { keyFile: args.values['key-file'] } : {}),
|
|
281
|
+
});
|
|
183
282
|
const backupClient = new AgentDriveClient({
|
|
184
283
|
apiUrl: connection.apiUrl,
|
|
185
284
|
apiKey: connection.apiKey,
|
|
186
285
|
machine: machineName(),
|
|
187
286
|
...(key ? { driveKey: key } : {}),
|
|
188
287
|
});
|
|
189
|
-
const dryRun = args.values['dry-run'] ?? false;
|
|
190
288
|
const result = await backupDirectory(backupClient, driveId, args.positionals[0], args.values.prefix ?? `/backups/${machineName()}`, {
|
|
191
289
|
dryRun,
|
|
192
290
|
...(args.values['key-file'] ? { keyFile: args.values['key-file'] } : {}),
|
|
291
|
+
...(args.values.exclude ? { exclude: args.values.exclude } : {}),
|
|
292
|
+
gitignore: args.values.gitignore ?? false,
|
|
293
|
+
yes: args.values.yes ?? false,
|
|
193
294
|
log: (line) => process.stderr.write(`${line}\n`),
|
|
194
295
|
});
|
|
195
296
|
for (const path of result.wouldUpload)
|
|
@@ -242,9 +343,10 @@ async function run(command, raw) {
|
|
|
242
343
|
if (sub === 'issue') {
|
|
243
344
|
if (!args.values.name)
|
|
244
345
|
throw new Error('--name is required.');
|
|
245
|
-
const client = await connect(args);
|
|
346
|
+
const { client, connection } = await connect(args);
|
|
246
347
|
const result = await issueCredentials({
|
|
247
348
|
name: args.values.name,
|
|
349
|
+
apiUrl: connection.apiUrl,
|
|
248
350
|
...(args.values.drive?.[0] ? { drive: args.values.drive[0] } : {}),
|
|
249
351
|
...(args.values.scope ? { scopes: args.values.scope } : {}),
|
|
250
352
|
...(args.values['write-path'] ? { writePaths: args.values['write-path'] } : {}),
|
|
@@ -270,6 +372,7 @@ async function run(command, raw) {
|
|
|
270
372
|
const text = args.values.from ? await readFile(args.values.from, 'utf8') : await stdinText();
|
|
271
373
|
const ids = await importCredentials(text, args.values.from ? 'file' : 'stdout');
|
|
272
374
|
process.stdout.write(`imported ${ids.length} drive key(s) into ${configPath()}\n`);
|
|
375
|
+
await sealSavedConnectionAfterKeyChange();
|
|
273
376
|
return;
|
|
274
377
|
}
|
|
275
378
|
if (sub === 'status') {
|
|
@@ -289,7 +392,7 @@ async function run(command, raw) {
|
|
|
289
392
|
const code = args.positionals[0];
|
|
290
393
|
if (!code)
|
|
291
394
|
throw new Error('grant requires the CODE shown by `agent-drive login`.');
|
|
292
|
-
const connection = await resolveConnection();
|
|
395
|
+
const connection = await resolveConnection(args.values.api);
|
|
293
396
|
const drive = args.values.drive?.[0];
|
|
294
397
|
await grantLogin({
|
|
295
398
|
code,
|
|
@@ -305,31 +408,106 @@ async function run(command, raw) {
|
|
|
305
408
|
config: connection.config,
|
|
306
409
|
log: (line) => process.stdout.write(`${line}\n`),
|
|
307
410
|
});
|
|
411
|
+
sealAfterKeyChange(connection);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
if (command === 'account' && args.positionals[0] === 'seal') {
|
|
415
|
+
const connection = await resolveConnection(args.values.api);
|
|
416
|
+
const client = new AgentDriveClient({
|
|
417
|
+
apiUrl: connection.apiUrl,
|
|
418
|
+
apiKey: connection.apiKey,
|
|
419
|
+
machine: machineName(),
|
|
420
|
+
});
|
|
421
|
+
const driveIds = args.values.drive?.length ? args.values.drive : undefined;
|
|
422
|
+
// Paginate fully. A key without `read` cannot list (403): then no drive's type is proven
|
|
423
|
+
// by the listing, so only locally marked agent-only drives are known — everything else is
|
|
424
|
+
// skipped as 'unknown type' rather than sent a key it may not accept.
|
|
425
|
+
const flags = new Map();
|
|
426
|
+
let listed = [];
|
|
427
|
+
const pendingNames = await pendingAgentOnlyNames(client.apiOrigin);
|
|
428
|
+
try {
|
|
429
|
+
let cursor;
|
|
430
|
+
for (let page = 0; page < 50; page += 1) {
|
|
431
|
+
const res = await client.listDrives(undefined, cursor);
|
|
432
|
+
for (const drive of res.items ?? []) {
|
|
433
|
+
listed.push(drive.id);
|
|
434
|
+
flags.set(drive.id, drive.agentOnly === true || pendingNames.has(drive.name));
|
|
435
|
+
}
|
|
436
|
+
if (!res.nextCursor)
|
|
437
|
+
break;
|
|
438
|
+
cursor = res.nextCursor;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
catch (error) {
|
|
442
|
+
if (!(error instanceof AgentDriveApiError && error.status === 403))
|
|
443
|
+
throw error;
|
|
444
|
+
listed = null;
|
|
445
|
+
}
|
|
446
|
+
const candidates = driveIds ?? [
|
|
447
|
+
...new Set([
|
|
448
|
+
...Object.keys(keysForOrigin(connection.config, client.apiOrigin)),
|
|
449
|
+
...(listed ?? []),
|
|
450
|
+
]),
|
|
451
|
+
];
|
|
452
|
+
const targets = await heldSealTargets({
|
|
453
|
+
config: connection.config,
|
|
454
|
+
origin: client.apiOrigin,
|
|
455
|
+
driveIds: candidates,
|
|
456
|
+
agentOnly: flags,
|
|
457
|
+
// With a single --drive, AGENT_DRIVE_KEY and --key-file resolve for it like commands do.
|
|
458
|
+
...(driveIds?.length === 1 ? { selectedDriveId: driveIds[0] } : {}),
|
|
459
|
+
...(args.values['key-file'] ? { keyFile: args.values['key-file'] } : {}),
|
|
460
|
+
});
|
|
461
|
+
if (driveIds?.length) {
|
|
462
|
+
const missing = driveIds.filter((id) => !targets.some((t) => t.driveId === id));
|
|
463
|
+
if (missing.length)
|
|
464
|
+
throw new Error(`No drive key on this machine for ${missing.join(', ')}.`);
|
|
465
|
+
}
|
|
466
|
+
if (!targets.length) {
|
|
467
|
+
process.stdout.write('No drive keys on this machine.\n');
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const result = await sealDrivesToAccount({
|
|
471
|
+
client,
|
|
472
|
+
config: connection.config,
|
|
473
|
+
targets,
|
|
474
|
+
force: true,
|
|
475
|
+
repin: args.values.repin === true,
|
|
476
|
+
onRepair: () => process.stderr.write('The account-key pin file was unreadable, so a new one was started. Pins for other API origins are gone — run `agent-drive account seal --repin` under each of them.\n'),
|
|
477
|
+
requireVerifiedType: true,
|
|
478
|
+
verifiedDriveIds: new Set(listed ?? []),
|
|
479
|
+
});
|
|
480
|
+
for (const target of targets)
|
|
481
|
+
process.stdout.write(`${target.driveId}: ${result.get(target.driveId)}\n`);
|
|
482
|
+
const statuses = [...result.values()];
|
|
483
|
+
if (statuses.includes('unknown type'))
|
|
484
|
+
process.stderr.write(`${UNKNOWN_TYPE_HINT}\n`);
|
|
485
|
+
if (statuses.some((status) => status === 'failed' || status === 'unknown type'))
|
|
486
|
+
process.exitCode = 1;
|
|
308
487
|
return;
|
|
309
488
|
}
|
|
310
|
-
const client = await connect(args);
|
|
489
|
+
const { client, connection } = await connect(args);
|
|
311
490
|
if (command === 'whoami') {
|
|
312
491
|
const who = await client.whoami();
|
|
313
|
-
const config =
|
|
492
|
+
const config = connection.config;
|
|
314
493
|
return output({
|
|
315
494
|
...who,
|
|
316
|
-
localDriveKeys: await localKeyMap(who.driveIds, (id) => resolveDriveKey(id, args.values['key-file'], config)),
|
|
495
|
+
localDriveKeys: await localKeyMap(who.driveIds, (id) => resolveDriveKey(id, connection.apiUrl, args.values['key-file'], config)),
|
|
317
496
|
keyBackup: Object.fromEntries(Object.keys(config.driveKeys ?? {})
|
|
318
497
|
.filter((id) => !who.driveIds || who.driveIds.includes(id))
|
|
319
498
|
.map((id) => [id, config.keyBackup?.[id] ?? null])),
|
|
320
499
|
});
|
|
321
500
|
}
|
|
322
501
|
if (command === 'check') {
|
|
323
|
-
const connection = await resolveConnection();
|
|
324
502
|
const who = await client.whoami();
|
|
325
503
|
const candidates = who.driveIds ?? Object.keys(connection.config.driveKeys ?? {});
|
|
326
504
|
const explicit = args.values.drive?.[0];
|
|
327
|
-
const keys = await localKeyMap([...candidates, ...(explicit ? [explicit] : [])], (id) => resolveDriveKey(id, args.values['key-file'], connection.config));
|
|
505
|
+
const keys = await localKeyMap([...candidates, ...(explicit ? [explicit] : [])], (id) => resolveDriveKey(id, connection.apiUrl, args.values['key-file'], connection.config));
|
|
328
506
|
const driveId = pickCheckDrive(explicit, candidates, keys);
|
|
329
507
|
const unbacked = unbackedUp(connection.config, backupCandidates(keys, driveId, args.values['key-file']));
|
|
330
508
|
if (unbacked.length)
|
|
331
509
|
throw new Error(notBackedUpMessage(unbacked));
|
|
332
|
-
const key = await resolveDriveKey(driveId, args.values['key-file'], connection.config);
|
|
510
|
+
const key = await resolveDriveKey(driveId, connection.apiUrl, args.values['key-file'], connection.config);
|
|
333
511
|
const checker = new AgentDriveClient({
|
|
334
512
|
apiUrl: connection.apiUrl,
|
|
335
513
|
apiKey: connection.apiKey,
|
|
@@ -345,23 +523,69 @@ async function run(command, raw) {
|
|
|
345
523
|
const name = args.values.name ?? args.positionals[1];
|
|
346
524
|
if (!name)
|
|
347
525
|
throw new Error('A drive name is required.');
|
|
348
|
-
const
|
|
526
|
+
const agentOnly = args.values['agent-only'] === true;
|
|
527
|
+
const origin = client.apiOrigin;
|
|
528
|
+
// Record the user's choice BEFORE the remote drive exists: if this state write fails the
|
|
529
|
+
// command stops here, so a state failure can never strand a remote drive with no mark.
|
|
530
|
+
if (agentOnly)
|
|
531
|
+
await recordAgentOnlyIntent(name, origin);
|
|
532
|
+
const drive = await client.createDrive(name, limit(args.values.quota), agentOnly || undefined);
|
|
533
|
+
// Seal through the same connection that created the drive — a second resolveConnection
|
|
534
|
+
// could pick up an account or origin another process just changed.
|
|
349
535
|
const saved = await loadConfig();
|
|
350
|
-
const existingKey = await resolveDriveKey(drive.id, args.values['key-file'], saved);
|
|
536
|
+
const existingKey = await resolveDriveKey(drive.id, connection.apiUrl, args.values['key-file'], saved);
|
|
537
|
+
let sealedKey = existingKey;
|
|
351
538
|
if (drive.created) {
|
|
352
539
|
const key = generateDriveKey();
|
|
353
540
|
await writeConfig({
|
|
354
541
|
driveKeys: { [drive.id]: key },
|
|
355
|
-
driveKeyOrigins: { [drive.id]:
|
|
542
|
+
driveKeyOrigins: { [drive.id]: origin },
|
|
356
543
|
});
|
|
357
544
|
output(drive);
|
|
358
545
|
process.stderr.write(`${createdDriveNote(configPath())}\n${BACKUP_HINT}\n`);
|
|
546
|
+
sealedKey = key;
|
|
359
547
|
}
|
|
360
548
|
else {
|
|
361
549
|
if (!existingKey)
|
|
362
550
|
throw new Error(`Drive ${drive.id} already exists; load its backup through ${driveKeyEnvironmentName(drive.id)}.`);
|
|
363
551
|
output(drive);
|
|
364
552
|
}
|
|
553
|
+
if (agentOnly) {
|
|
554
|
+
if (drive.created) {
|
|
555
|
+
// The user's flag — not the API's echo — marks the drive agent-only. If recording the
|
|
556
|
+
// id fails, the name intent recorded above still stops seals via the listing.
|
|
557
|
+
try {
|
|
558
|
+
await recordAgentOnlyDrive(drive.id, origin);
|
|
559
|
+
await clearAgentOnlyIntent(name, origin);
|
|
560
|
+
}
|
|
561
|
+
catch {
|
|
562
|
+
// The pending intent stays as the fallback marker.
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
await clearAgentOnlyIntent(name, origin).catch(() => { });
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
else if (drive.created) {
|
|
570
|
+
// An earlier --agent-only attempt for this name may have died mid-create; the user's flag
|
|
571
|
+
// is authoritative, so an ordinary create retires its stale intent. Best-effort: keeping
|
|
572
|
+
// it only mis-marks (fail closed), never leaks a key. The name check is a lock-free read
|
|
573
|
+
// first, so the usual case never waits on the state lock.
|
|
574
|
+
if ((await pendingAgentOnlyNames(origin)).has(name))
|
|
575
|
+
await clearAgentOnlyIntent(name, origin).catch(() => { });
|
|
576
|
+
}
|
|
577
|
+
// Agent-only is also learned from the API's own state when this machine did not set the flag
|
|
578
|
+
// (a drive created elsewhere): seals skip this drive even if a listing later disagrees.
|
|
579
|
+
if (drive.agentOnly === true && (!agentOnly || !drive.created))
|
|
580
|
+
await recordAgentOnlyDrive(drive.id, origin).catch(() => { });
|
|
581
|
+
// Reloaded after the write above so the seal sees the just-created key — and seals that
|
|
582
|
+
// exact key, never a different stored or env key.
|
|
583
|
+
sealHook({ ...connection, config: await loadConfig() }, {
|
|
584
|
+
driveIds: [drive.id],
|
|
585
|
+
force: true,
|
|
586
|
+
selectedDriveId: drive.id,
|
|
587
|
+
...(sealedKey ? { keys: { [drive.id]: sealedKey } } : {}),
|
|
588
|
+
});
|
|
365
589
|
return;
|
|
366
590
|
}
|
|
367
591
|
if (command === 'key' && args.positionals[0] === 'create') {
|
|
@@ -459,7 +683,7 @@ async function run(command, raw) {
|
|
|
459
683
|
throw new Error('put requires LOCAL and REMOTE.');
|
|
460
684
|
if (args.values.overwrite && args.values['if-version'])
|
|
461
685
|
throw new Error('--overwrite and --if-version are mutually exclusive.');
|
|
462
|
-
const version = await client
|
|
686
|
+
const version = await putFile(client, requiredDrive(args), local, remote, {
|
|
463
687
|
...(args.values.overwrite ? { mode: 'overwrite' } : {}),
|
|
464
688
|
...(args.values['if-version'] ? { ifVersionId: args.values['if-version'] } : {}),
|
|
465
689
|
...(args.values.description ? { description: args.values.description } : {}),
|
|
@@ -472,21 +696,50 @@ async function run(command, raw) {
|
|
|
472
696
|
const remote = args.positionals[0];
|
|
473
697
|
if (!remote)
|
|
474
698
|
throw new Error('get requires REMOTE.');
|
|
475
|
-
const result = await client.get(requiredDrive(args), remote, args.values.version ? { versionId: args.values.version } : {});
|
|
476
699
|
if (args.values.output) {
|
|
477
|
-
await
|
|
478
|
-
|
|
700
|
+
return output(await getFile(client, requiredDrive(args), remote, args.values.output, {
|
|
701
|
+
...(args.values.version ? { versionId: args.values.version } : {}),
|
|
702
|
+
overwrite: true,
|
|
703
|
+
}));
|
|
479
704
|
}
|
|
480
|
-
|
|
705
|
+
const result = await client.getStream(requiredDrive(args), remote, {
|
|
706
|
+
...(args.values.version ? { versionId: args.values.version } : {}),
|
|
707
|
+
maxPlaintextBytes: 1024 * 1024,
|
|
708
|
+
});
|
|
709
|
+
if (result.plaintextBytes > 1024 * 1024) {
|
|
710
|
+
await result.cancel();
|
|
481
711
|
throw new Error('Binary or large output requires --output.');
|
|
712
|
+
}
|
|
713
|
+
const chunks = [];
|
|
714
|
+
let length = 0;
|
|
715
|
+
try {
|
|
716
|
+
for await (const chunk of result.plaintext) {
|
|
717
|
+
length += chunk.byteLength;
|
|
718
|
+
if (length > 1024 * 1024)
|
|
719
|
+
throw new Error('Binary or large output requires --output.');
|
|
720
|
+
chunks.push(chunk);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
catch (error) {
|
|
724
|
+
await result.cancel(error);
|
|
725
|
+
throw error;
|
|
726
|
+
}
|
|
727
|
+
const bytes = new Uint8Array(length);
|
|
728
|
+
let offset = 0;
|
|
729
|
+
for (const chunk of chunks) {
|
|
730
|
+
bytes.set(chunk, offset);
|
|
731
|
+
offset += chunk.byteLength;
|
|
732
|
+
}
|
|
482
733
|
let text;
|
|
483
734
|
try {
|
|
484
|
-
text = new TextDecoder('utf-8', { fatal: true }).decode(
|
|
735
|
+
text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
485
736
|
}
|
|
486
737
|
catch {
|
|
738
|
+
await result.cancel();
|
|
487
739
|
throw new Error('Binary output requires --output.');
|
|
488
740
|
}
|
|
489
741
|
if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f]/u.test(text)) {
|
|
742
|
+
await result.cancel();
|
|
490
743
|
throw new Error('Binary output requires --output.');
|
|
491
744
|
}
|
|
492
745
|
process.stdout.write(`${text}${text.endsWith('\n') ? '' : '\n'}`);
|
|
@@ -523,7 +776,7 @@ Commands:
|
|
|
523
776
|
whoami Also shows localDriveKeys (which drives have a key here)
|
|
524
777
|
check [--drive ID] Write, read back, compare a test file (read-only keys: decrypt an existing file); needs backed-up keys; prints "check ok"
|
|
525
778
|
agent create --name NAME
|
|
526
|
-
drive create --name NAME
|
|
779
|
+
drive create --name NAME [--agent-only]
|
|
527
780
|
key create --agent ID --drive ID --label LABEL --scope read --scope write [--write-path P]...
|
|
528
781
|
key revoke ID
|
|
529
782
|
put LOCAL REMOTE --drive ID [--if-version ID | --overwrite]
|
|
@@ -540,7 +793,15 @@ Commands:
|
|
|
540
793
|
share list --drive ID
|
|
541
794
|
share revoke SHARE_ID --drive ID
|
|
542
795
|
sync LOCAL_DIRECTORY --drive ID [--prefix PATH] [--overwrite-existing]
|
|
543
|
-
|
|
796
|
+
sync takes no filter or preview flags; the backup-only flags below are rejected, not ignored.
|
|
797
|
+
backup LOCAL_DIRECTORY [--drive ID] [--prefix PATH] [--exclude GLOB]... [--gitignore] [--dry-run] [--yes]
|
|
798
|
+
Scans the folder first, then uploads. --gitignore skips paths the folder's .gitignore excludes
|
|
799
|
+
and .git control metadata (a .git directory or a linked worktree's .git file). Without it or
|
|
800
|
+
--exclude, everything is uploaded, .git included.
|
|
801
|
+
--exclude GLOB matches the root-relative path: 'build/**' skips everything under build/,
|
|
802
|
+
'**/node_modules' skips node_modules at any depth, a bare 'node_modules' only the top-level one.
|
|
803
|
+
Over 5000 files or 1 GiB needs --yes (or preview with --dry-run). One backup per prefix at a time.
|
|
804
|
+
version Print the CLI version
|
|
544
805
|
grant CODE [--drive ID] [--read-only] [--scope S]... [--write-path P]... Seal this machine's drive key for the login showing CODE; the owner then approves it on /connect
|
|
545
806
|
credentials export (--to 1password [--vault NAME] | --to clipboard [--keep-clipboard] | --to PATH [--force] | --stdout)
|
|
546
807
|
Back up the API key and drive keys (bundle "adcred1.…"). Offer 1Password first, then the clipboard (for Apple Passwords or another manager), then a file outside the repo
|
|
@@ -549,6 +810,7 @@ Commands:
|
|
|
549
810
|
credentials confirm [--method stdout] Record a --stdout export as stored (export --stdout | STORE && credentials confirm)
|
|
550
811
|
credentials import [--from PATH] Merge a bundle (file or stdin) into the config
|
|
551
812
|
credentials status Which drive keys are backed up, when, and how
|
|
813
|
+
account seal [--drive ID] [--repin] Seal held drive keys to your account key for the console
|
|
552
814
|
skill install [--target claude|codex|DIR] Install the agent-drive SKILL.md for your agent
|
|
553
815
|
capture [--drive ID] [--agent NAME] -- COMMAND [ARGS...] Run an agent with its LLM calls saved as encrypted traces under /traces
|
|
554
816
|
mcp
|
|
@@ -561,17 +823,24 @@ memory, never written to disk; its drives count as backed up).
|
|
|
561
823
|
`;
|
|
562
824
|
// For `capture`, only flags before `--` are ours; the rest belongs to the wrapped command.
|
|
563
825
|
const ownArgs = name === 'capture' && raw.includes('--') ? raw.slice(0, raw.indexOf('--')) : raw;
|
|
826
|
+
let done;
|
|
564
827
|
if (!name || name === 'help' || name === '--help' || ownArgs.includes('--help')) {
|
|
565
828
|
process.stdout.write(help);
|
|
829
|
+
done = Promise.resolve();
|
|
830
|
+
}
|
|
831
|
+
else if (name === 'version' || name === '--version') {
|
|
832
|
+
process.stdout.write(`${cliVersion}\n`);
|
|
833
|
+
done = Promise.resolve();
|
|
566
834
|
}
|
|
567
835
|
else if (name === 'mcp') {
|
|
568
|
-
runMcp().catch(() => {
|
|
836
|
+
done = runMcp().catch(() => {
|
|
569
837
|
process.stderr.write('MCP server failed to start.\n');
|
|
570
838
|
process.exitCode = 1;
|
|
571
839
|
});
|
|
572
840
|
}
|
|
573
841
|
else {
|
|
574
|
-
run(name, raw)
|
|
842
|
+
done = run(name, raw)
|
|
843
|
+
.catch((error) => {
|
|
575
844
|
if (error instanceof AgentDriveApiError) {
|
|
576
845
|
process.stderr.write(`${apiErrorLines(error).join('\n')}\n`);
|
|
577
846
|
}
|
|
@@ -579,6 +848,15 @@ else {
|
|
|
579
848
|
process.stderr.write(`${error instanceof Error ? error.message : 'Command failed.'}\n`);
|
|
580
849
|
}
|
|
581
850
|
process.exitCode = 1;
|
|
851
|
+
})
|
|
852
|
+
.then(() => {
|
|
853
|
+
// Only now, once the command's output has settled, do the scheduled seals start.
|
|
854
|
+
for (const work of scheduledSeals.splice(0))
|
|
855
|
+
sealInBackground(work);
|
|
856
|
+
return Promise.allSettled([...pendingSeals]).then(() => undefined);
|
|
582
857
|
});
|
|
583
858
|
}
|
|
859
|
+
// Exported so in-process tests can await a command's full lifecycle (including best-effort
|
|
860
|
+
// side effects like account sealing) instead of racing its first output line.
|
|
861
|
+
export { done };
|
|
584
862
|
//# sourceMappingURL=cli.js.map
|