@cortexkit/opencode-antigravity-auth 3.0.15 → 3.0.17
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/cli.js +26740 -24394
- package/dist/cli.js.map +4 -4
- package/dist/cli.meta.json +549 -452
- package/dist/index.js +34088 -31689
- package/dist/index.js.map +4 -4
- package/dist/index.meta.json +608 -550
- package/dist/server.js +28837 -26421
- package/dist/server.js.map +4 -4
- package/dist/server.meta.json +1 -1
- package/dist/src/__fixtures__/account-store-process.d.ts +2 -0
- package/dist/src/__fixtures__/account-store-process.d.ts.map +1 -0
- package/dist/src/__fixtures__/account-store-process.js +163 -0
- package/dist/src/__fixtures__/account-store-process.js.map +1 -0
- package/dist/src/cli.d.ts +22 -6
- package/dist/src/cli.d.ts.map +1 -1
- package/dist/src/cli.js +157 -9
- package/dist/src/cli.js.map +1 -1
- package/dist/src/common-auth-embedded/fs/refresh-file-lock.d.ts +13 -0
- package/dist/src/common-auth-embedded/fs/refresh-file-lock.js +159 -7
- package/dist/src/common-auth-embedded/pi-slot/lock.js +26 -1
- package/dist/src/common-auth-embedded/source-output.json +15 -15
- package/dist/src/ga/server/index.js +2 -2
- package/dist/src/ga/server/index.js.map +1 -1
- package/dist/src/plugin/__fixtures__/verified-store.js +2 -2
- package/dist/src/plugin/ga-location-services.d.ts +2 -0
- package/dist/src/plugin/ga-location-services.d.ts.map +1 -1
- package/dist/src/plugin/ga-location-services.js +3 -1
- package/dist/src/plugin/ga-location-services.js.map +1 -1
- package/dist/src/plugin/index.d.ts.map +1 -1
- package/dist/src/plugin/index.js +2 -2
- package/dist/src/plugin/index.js.map +1 -1
- package/dist/src/plugin/storage.d.ts +21 -0
- package/dist/src/plugin/storage.d.ts.map +1 -1
- package/dist/src/plugin/storage.js +103 -12
- package/dist/src/plugin/storage.js.map +1 -1
- package/dist/src/process-heartbeat.d.ts +33 -9
- package/dist/src/process-heartbeat.d.ts.map +1 -1
- package/dist/src/process-heartbeat.js +161 -61
- package/dist/src/process-heartbeat.js.map +1 -1
- package/dist/src/runtime-layout.d.ts +82 -1
- package/dist/src/runtime-layout.d.ts.map +1 -1
- package/dist/src/runtime-layout.js +415 -21
- package/dist/src/runtime-layout.js.map +1 -1
- package/dist/src/runtime-paths.d.ts +93 -9
- package/dist/src/runtime-paths.d.ts.map +1 -1
- package/dist/src/runtime-paths.js +153 -15
- package/dist/src/runtime-paths.js.map +1 -1
- package/dist/src/setup.d.ts.map +1 -1
- package/dist/src/setup.js +35 -7
- package/dist/src/setup.js.map +1 -1
- package/dist/src/vault-folder.d.ts +2 -0
- package/dist/src/vault-folder.d.ts.map +1 -1
- package/dist/src/vault-folder.js +8 -1
- package/dist/src/vault-folder.js.map +1 -1
- package/dist/src/version-fence.d.ts +37 -0
- package/dist/src/version-fence.d.ts.map +1 -1
- package/dist/src/version-fence.js +131 -12
- package/dist/src/version-fence.js.map +1 -1
- package/dist/tui-build-map.json +1 -1
- package/dist/tui-ga-build-map.json +1 -1
- package/dist/types/src/cli.d.ts +22 -6
- package/dist/types/src/cli.d.ts.map +1 -1
- package/dist/types/src/common-auth-embedded/fs/refresh-file-lock.d.ts +13 -0
- package/dist/types/src/plugin/ga-location-services.d.ts +2 -0
- package/dist/types/src/plugin/ga-location-services.d.ts.map +1 -1
- package/dist/types/src/plugin/index.d.ts.map +1 -1
- package/dist/types/src/plugin/storage.d.ts +21 -0
- package/dist/types/src/plugin/storage.d.ts.map +1 -1
- package/dist/types/src/process-heartbeat.d.ts +33 -9
- package/dist/types/src/process-heartbeat.d.ts.map +1 -1
- package/dist/types/src/runtime-layout.d.ts +82 -1
- package/dist/types/src/runtime-layout.d.ts.map +1 -1
- package/dist/types/src/runtime-paths.d.ts +93 -9
- package/dist/types/src/runtime-paths.d.ts.map +1 -1
- package/dist/types/src/setup.d.ts.map +1 -1
- package/dist/types/src/vault-folder.d.ts +2 -0
- package/dist/types/src/vault-folder.d.ts.map +1 -1
- package/dist/types/src/version-fence.d.ts +37 -0
- package/dist/types/src/version-fence.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/common-auth-embedded/fs/refresh-file-lock.d.ts +13 -0
- package/src/common-auth-embedded/fs/refresh-file-lock.js +159 -7
- package/src/common-auth-embedded/pi-slot/lock.js +26 -1
- package/src/common-auth-embedded/source-output.json +15 -15
|
@@ -4,9 +4,21 @@
|
|
|
4
4
|
// files there every few seconds: the vault roster, the vault provider state
|
|
5
5
|
// and the enrollment files, each with lock files, eviction markers and staged
|
|
6
6
|
// temporary files. They move to the state home (`runtime-paths.ts` names both
|
|
7
|
-
// folders).
|
|
8
|
-
//
|
|
9
|
-
//
|
|
7
|
+
// folders). `antigravity.json` and the TUI preferences stay in OpenCode's
|
|
8
|
+
// config folder.
|
|
9
|
+
//
|
|
10
|
+
// The account store (`antigravity-accounts.json`, its pointer and its
|
|
11
|
+
// generation folders) moves to the state home as well, but as a separate,
|
|
12
|
+
// offline operation: `prepareAccountStore` decides once per process where
|
|
13
|
+
// this config folder keeps it, moving it only while
|
|
14
|
+
// no other live process could still be using it in the config folder (the
|
|
15
|
+
// `storeMove` fence of `version-fence.ts`, which reads each process's
|
|
16
|
+
// heartbeat `stores` record). When that is not safe the process keeps the
|
|
17
|
+
// config folder for its whole life and logs why once. Setup and
|
|
18
|
+
// `antigravity-auth store relocate [--back] --offline` run the same move
|
|
19
|
+
// outside a start. The store's own operation is core's relocation
|
|
20
|
+
// (`createAccountRelocationFactory`); this module only decides when and
|
|
21
|
+
// between which two anchors.
|
|
10
22
|
//
|
|
11
23
|
// This is a port of openai-auth's `runtime-layout.ts`, with these changes:
|
|
12
24
|
// only the five `opencode-*` vault files below move (Pi's files can share the
|
|
@@ -30,6 +42,10 @@
|
|
|
30
42
|
// A process that starts while one of those is alive takes the legacy layout
|
|
31
43
|
// too. So the move happens at the first start after every OpenCode using
|
|
32
44
|
// that config folder has been closed or restarted on this version.
|
|
45
|
+
// - A locally authorized start settles the account store after the vault
|
|
46
|
+
// layout, under the layout lock, and records its anchor before using it.
|
|
47
|
+
// Vault-only starts inspect no store path and report `stores: undecided`;
|
|
48
|
+
// after disconnecting, their first local use decides under that same lock.
|
|
33
49
|
// - Every decision is made holding the layout lock in the state-home folder.
|
|
34
50
|
// A legacy choice is allowed only once it is in this process's heartbeat,
|
|
35
51
|
// written while that lock is held, so a process deciding later sees it. A
|
|
@@ -91,13 +107,14 @@
|
|
|
91
107
|
// folder (`legacySignatureCacheFile`) is deleted once, in the same
|
|
92
108
|
// clean-up step that records the old vault files as dealt with.
|
|
93
109
|
import { createHash } from 'node:crypto';
|
|
94
|
-
import { chmod, mkdir, readdir, readFile, rmdir, stat, unlink, } from 'node:fs/promises';
|
|
110
|
+
import { chmod, mkdir, readdir, readFile, rename, rmdir, stat, unlink, } from 'node:fs/promises';
|
|
95
111
|
import { basename, dirname, join, resolve } from 'node:path';
|
|
96
|
-
import { loadCommonAuthClaustrum, loadCommonAuthFs, } from '@cortexkit/antigravity-auth-core';
|
|
112
|
+
import { accountStorePointerPath, createAccountRelocationFactory, enrollmentAuthority, initializeFreshAccountStore, loadCommonAuthClaustrum, loadCommonAuthFs, loadCommonAuthStoreModules, readAccountStoreBinding, } from '@cortexkit/antigravity-auth-core';
|
|
97
113
|
import { createLogger } from './plugin/logger';
|
|
114
|
+
import { accountFileForVault } from './plugin/storage';
|
|
98
115
|
import { OPENCODE_VAULT_ENROLLMENT_NAME, OPENCODE_VAULT_HOST, } from './plugin/vault-custody';
|
|
99
|
-
import { recordProcessLayout } from './process-heartbeat';
|
|
100
|
-
import { decidedRuntimeLayout, legacyAccountFile, legacySignatureCacheFile, legacyVaultDir, RUNTIME_CONFIG_DIR_NOTE_FILE, RUNTIME_LAYOUT_MARKER_FILE, runtimeLayoutLocation, setRuntimeLayout, stateHomeVaultDir, } from './runtime-paths';
|
|
116
|
+
import { recordProcessAnchor, recordProcessLayout } from './process-heartbeat';
|
|
117
|
+
import { decidedAccountStoreAnchor, decidedRuntimeLayout, folderResolvesToItself, legacyAccountFile, legacySignatureCacheFile, legacyVaultDir, pathPresent, RUNTIME_CONFIG_DIR_NOTE_FILE, RUNTIME_LAYOUT_MARKER_FILE, readForwardedAccountStore, runtimeLayoutLocation, setAccountStoreAnchor, setRuntimeLayout, stateHomeAccountFile, stateHomeVaultDir, } from './runtime-paths';
|
|
101
118
|
import { versionFenceOpen } from './version-fence';
|
|
102
119
|
/** Name of the lock held while a process decides or moves the layout. */
|
|
103
120
|
export const RUNTIME_LAYOUT_LOCK_NAME = 'runtime-layout';
|
|
@@ -167,6 +184,18 @@ const SHARING_RETRY_DELAYS_MS = [0, 10, 20, 40, 80, 100];
|
|
|
167
184
|
const SHARING_RETRY_BUDGET_MS = 500;
|
|
168
185
|
/** The message logged once by a process that keeps the legacy layout. */
|
|
169
186
|
export const RUNTIME_LAYOUT_DEFERRED_MESSAGE = "antigravity-auth keeps its vault files in OpenCode's config folder until every running OpenCode using it has restarted on this version.";
|
|
187
|
+
/**
|
|
188
|
+
* The message logged once, in plain words, by a process that keeps the
|
|
189
|
+
* account store in OpenCode's config folder because another live OpenCode
|
|
190
|
+
* could still be using it there.
|
|
191
|
+
*/
|
|
192
|
+
export const ACCOUNT_STORE_DEFERRED_MESSAGE = "antigravity-auth keeps the account store in OpenCode's config folder until every running OpenCode using it has restarted on this version.";
|
|
193
|
+
/**
|
|
194
|
+
* The message logged once by a process that keeps the account store in
|
|
195
|
+
* OpenCode's config folder because the state-home folder's path goes through
|
|
196
|
+
* a link, which the account store refuses to open a file through.
|
|
197
|
+
*/
|
|
198
|
+
export const ACCOUNT_STORE_LINKED_PATH_MESSAGE = "antigravity-auth keeps the account store in OpenCode's config folder because the path to its state folder goes through a link. Setting XDG_STATE_HOME to the real path of that folder lets it move there.";
|
|
170
199
|
/**
|
|
171
200
|
* Plugin start stops when it cannot settle a safe layout: it could not take
|
|
172
201
|
* the layout lock or write its heartbeat (so other processes could not see
|
|
@@ -215,12 +244,14 @@ const stateKey = Symbol.for('@cortexkit/antigravity-auth/runtime-layout-runs');
|
|
|
215
244
|
const globalState = globalThis;
|
|
216
245
|
const processState = globalState[stateKey] ?? {
|
|
217
246
|
runs: new Map(),
|
|
247
|
+
storeRuns: new Map(),
|
|
218
248
|
logged: new Set(),
|
|
219
249
|
};
|
|
220
250
|
globalState[stateKey] = processState;
|
|
221
251
|
/** Test seam: forget this process's runs and log lines. */
|
|
222
252
|
export function __resetRuntimeLayoutRunsForTest() {
|
|
223
253
|
processState.runs.clear();
|
|
254
|
+
processState.storeRuns.clear();
|
|
224
255
|
processState.logged.clear();
|
|
225
256
|
}
|
|
226
257
|
function logOnce(key, write) {
|
|
@@ -315,12 +346,7 @@ async function decide(location, options) {
|
|
|
315
346
|
const files = layoutFiles(location, options.accountFile);
|
|
316
347
|
const fence = options.fence ?? defaultFence(options.version);
|
|
317
348
|
const stateHome = async () => {
|
|
318
|
-
await
|
|
319
|
-
key: location.key,
|
|
320
|
-
layout: 'state-home',
|
|
321
|
-
version: options.version,
|
|
322
|
-
});
|
|
323
|
-
setRuntimeLayout(location, 'state-home');
|
|
349
|
+
await settleVault(location, options, 'state-home', log);
|
|
324
350
|
return 'state-home';
|
|
325
351
|
};
|
|
326
352
|
// Read-only first: whose folder this is, and whether the move is done.
|
|
@@ -331,8 +357,10 @@ async function decide(location, options) {
|
|
|
331
357
|
// A move that stopped right after its last marker write left its
|
|
332
358
|
// `layout-moving.json`; it is removed below, under the layout lock.
|
|
333
359
|
const manifestLeft = await exists(join(stateDir, RUNTIME_LAYOUT_MANIFEST_FILE)).catch(() => true);
|
|
334
|
-
if (!manifestLeft)
|
|
360
|
+
if (!manifestLeft) {
|
|
361
|
+
await settleVault(location, options, 'state-home', log);
|
|
335
362
|
return stateHome();
|
|
363
|
+
}
|
|
336
364
|
}
|
|
337
365
|
// A state-home folder that is OpenCode's config folder itself (a link or a
|
|
338
366
|
// mount) gets no chmod or note of its own; the move then stays legacy.
|
|
@@ -359,16 +387,11 @@ async function decide(location, options) {
|
|
|
359
387
|
const legacy = async () => {
|
|
360
388
|
try {
|
|
361
389
|
await lock.assertOwned();
|
|
362
|
-
await
|
|
363
|
-
key: location.key,
|
|
364
|
-
layout: 'legacy',
|
|
365
|
-
version: options.version,
|
|
366
|
-
});
|
|
390
|
+
await settleVault(location, options, 'legacy', log);
|
|
367
391
|
}
|
|
368
392
|
catch (error) {
|
|
369
393
|
throw new RuntimeLayoutRefusal(stateDir, errorClass(error));
|
|
370
394
|
}
|
|
371
|
-
setRuntimeLayout(location, 'legacy');
|
|
372
395
|
return 'legacy';
|
|
373
396
|
};
|
|
374
397
|
try {
|
|
@@ -447,6 +470,377 @@ async function decide(location, options) {
|
|
|
447
470
|
function defaultFence(version) {
|
|
448
471
|
return () => versionFenceOpen({ currentVersion: version, layoutMove: true });
|
|
449
472
|
}
|
|
473
|
+
/**
|
|
474
|
+
* Record and fix this process's vault layout. Store preparation is separate:
|
|
475
|
+
* vault authority must be known before any account-store path is inspected.
|
|
476
|
+
*/
|
|
477
|
+
async function settleVault(location, options, layout, _log) {
|
|
478
|
+
await recordProcessLayout({
|
|
479
|
+
key: location.key,
|
|
480
|
+
layout,
|
|
481
|
+
version: options.version,
|
|
482
|
+
});
|
|
483
|
+
setRuntimeLayout(location, layout);
|
|
484
|
+
}
|
|
485
|
+
/** Read enrollment afresh; unreadable enrollment never authorizes local work. */
|
|
486
|
+
export async function localAccountStoreAuthorized() {
|
|
487
|
+
const location = runtimeLayoutLocation();
|
|
488
|
+
const claustrum = await loadCommonAuthClaustrum();
|
|
489
|
+
const vaultDir = decidedRuntimeLayout(location) === 'state-home'
|
|
490
|
+
? stateHomeVaultDir(location)
|
|
491
|
+
: legacyVaultDir(accountFileForVault(location.configDir));
|
|
492
|
+
return ((await enrollmentAuthority(claustrum.hostEnrollmentPaths({
|
|
493
|
+
stateDir: vaultDir,
|
|
494
|
+
host: OPENCODE_VAULT_HOST,
|
|
495
|
+
}), OPENCODE_VAULT_ENROLLMENT_NAME, claustrum)) === 'local');
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Decides, once per process, where this config folder's account store
|
|
499
|
+
* lives, and moves it into the state-home folder when that is safe. Call at
|
|
500
|
+
* a locally authorized start, or the first local use after disconnecting
|
|
501
|
+
* from the vault. The full decision and its heartbeat write hold the layout
|
|
502
|
+
* lock, including when an earlier start already moved the vault files. A
|
|
503
|
+
* process that already settled its anchor keeps it. Resolves to the anchor
|
|
504
|
+
* `getStoragePath()` in `plugin/storage.ts` now uses.
|
|
505
|
+
*
|
|
506
|
+
* An initial move runs only when every account store of this config folder
|
|
507
|
+
* is at the config folder anchor, no other live process could still be using
|
|
508
|
+
* it there (the `storeMove` fence, which also reads each heartbeat's
|
|
509
|
+
* `stores`), the state-home folder's path resolves through no link, and the
|
|
510
|
+
* store's own locks are free. Otherwise this process keeps the config folder
|
|
511
|
+
* for its whole life and says why once: a deferral is never a refusal. When
|
|
512
|
+
* a forwarding pointer already exists, its anchor is the answer whatever
|
|
513
|
+
* happens next, and an unfinished move there reports the store as unusable
|
|
514
|
+
* until a later start resumes it.
|
|
515
|
+
*/
|
|
516
|
+
export async function prepareAccountStore(options) {
|
|
517
|
+
const location = runtimeLayoutLocation();
|
|
518
|
+
if (!(await localAccountStoreAuthorized()))
|
|
519
|
+
return undefined;
|
|
520
|
+
const decided = decidedAccountStoreAnchor(location);
|
|
521
|
+
if (decided)
|
|
522
|
+
return decided;
|
|
523
|
+
const log = options.log ?? createLogger('runtime-layout');
|
|
524
|
+
const runKey = `${location.stateHomeDir}\0${location.key}`;
|
|
525
|
+
let run = processState.storeRuns.get(runKey);
|
|
526
|
+
if (!run) {
|
|
527
|
+
run = (async () => {
|
|
528
|
+
if (!(await samePhysical(location.configDir, location.stateHomeDir)))
|
|
529
|
+
await ensureOwnerOnlyDir(location.stateHomeDir);
|
|
530
|
+
const lock = await takeLock(RUNTIME_LAYOUT_LOCK_NAME, join(location.stateHomeDir, RUNTIME_LAYOUT_MARKER_FILE), options.layoutLockWaitMs ?? LAYOUT_LOCK_WAIT_MS, log);
|
|
531
|
+
try {
|
|
532
|
+
await lock.assertOwned();
|
|
533
|
+
// Enrollment may have changed while this start waited for the layout lock.
|
|
534
|
+
if (!(await localAccountStoreAuthorized()))
|
|
535
|
+
return undefined;
|
|
536
|
+
return await settleAccountStore(location, options, log);
|
|
537
|
+
}
|
|
538
|
+
finally {
|
|
539
|
+
await lock.release();
|
|
540
|
+
}
|
|
541
|
+
})().finally(() => {
|
|
542
|
+
processState.storeRuns.delete(runKey);
|
|
543
|
+
});
|
|
544
|
+
processState.storeRuns.set(runKey, run);
|
|
545
|
+
}
|
|
546
|
+
return run;
|
|
547
|
+
}
|
|
548
|
+
async function settleAccountStore(location, options, log) {
|
|
549
|
+
const decided = decidedAccountStoreAnchor(location);
|
|
550
|
+
if (decided)
|
|
551
|
+
return decided;
|
|
552
|
+
const anchor = await decideAccountStore(location, options, log);
|
|
553
|
+
setAccountStoreAnchor(location, anchor);
|
|
554
|
+
return anchor;
|
|
555
|
+
}
|
|
556
|
+
async function decideAccountStore(location, options, log) {
|
|
557
|
+
const keepConfig = () => recordAnchor(location, options, 'config', log);
|
|
558
|
+
const configAnchor = legacyAccountFile(location.configDir);
|
|
559
|
+
const stateAnchor = stateHomeAccountFile(location);
|
|
560
|
+
const forward = readForwardedAccountStore(configAnchor);
|
|
561
|
+
if (forward) {
|
|
562
|
+
// A forward names where the store went. This process can use that anchor
|
|
563
|
+
// only when it is the one this config folder resolves to; otherwise
|
|
564
|
+
// neither store is served (an error, never an empty store).
|
|
565
|
+
if (!sameSpelling(forward.legacyPath, stateAnchor)) {
|
|
566
|
+
logOnce(`${location.key}\0store-forward-elsewhere`, () => log.warn('the account store moved to another folder than this process resolved; check XDG_STATE_HOME', { movedTo: forward.legacyPath, expected: stateAnchor }));
|
|
567
|
+
}
|
|
568
|
+
// A moved store is usable only once its relocation finished; the store's
|
|
569
|
+
// own operation resumes or cancels an unfinished one.
|
|
570
|
+
await resumeForwardedStore(location, options, configAnchor, stateAnchor, log);
|
|
571
|
+
return recordAnchor(location, options, 'state-home', log);
|
|
572
|
+
}
|
|
573
|
+
// A deferred vault layout has not admitted the state-home destination.
|
|
574
|
+
if (decidedRuntimeLayout(location) === 'legacy')
|
|
575
|
+
return keepConfig();
|
|
576
|
+
const configStore = await presentAccountStore(configAnchor);
|
|
577
|
+
const stateStore = await presentAccountStore(stateAnchor);
|
|
578
|
+
if (configStore && stateStore) {
|
|
579
|
+
// Two stores and no forward saying which one is current: refuse rather
|
|
580
|
+
// than pick by age and split the accounts.
|
|
581
|
+
throw new RuntimeLayoutRefusal(location.stateHomeDir, 'two-account-stores', `antigravity-auth cannot start: two account stores exist, one at ${configAnchor} and one at ${stateAnchor}, and neither is marked as moved. Keep the one you want and move the other one aside, then restart OpenCode.`);
|
|
582
|
+
}
|
|
583
|
+
if (stateStore)
|
|
584
|
+
return recordAnchor(location, options, 'state-home', log);
|
|
585
|
+
if (configStore) {
|
|
586
|
+
if (!(await storeMovePathResolves(stateAnchor, location, log)))
|
|
587
|
+
return keepConfig();
|
|
588
|
+
return moveStore(location, options, configAnchor, stateAnchor, false, log, false);
|
|
589
|
+
}
|
|
590
|
+
// Nothing published at either anchor: a fresh installation initializes at
|
|
591
|
+
// the state-home anchor and leaves a forward at the config anchor, so a
|
|
592
|
+
// version without the store move refuses instead of creating a second
|
|
593
|
+
// store. A store that stopped before publishing its pointer starts over,
|
|
594
|
+
// exactly as an existing one would be moved.
|
|
595
|
+
if ((await storeArtefacts(stateAnchor)) > 0 &&
|
|
596
|
+
!(await abandonUnpublishedStore(location, stateAnchor, log)))
|
|
597
|
+
return keepConfig();
|
|
598
|
+
if (!(await storeMovePathResolves(stateAnchor, location, log)))
|
|
599
|
+
return keepConfig();
|
|
600
|
+
return initializeStore(location, options, stateAnchor, log);
|
|
601
|
+
}
|
|
602
|
+
/** A line a user may want to see: `log`, or `info` when the logger has none. */
|
|
603
|
+
function showLog(log, message, meta) {
|
|
604
|
+
if (log.log)
|
|
605
|
+
log.log(message, meta);
|
|
606
|
+
else
|
|
607
|
+
log.info(message, meta);
|
|
608
|
+
}
|
|
609
|
+
/** Record this process's store anchor and return it. */
|
|
610
|
+
async function recordAnchor(location, options, anchor, log) {
|
|
611
|
+
try {
|
|
612
|
+
await recordProcessAnchor({
|
|
613
|
+
key: location.key,
|
|
614
|
+
anchor,
|
|
615
|
+
version: options.version,
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
catch (error) {
|
|
619
|
+
throw new RuntimeLayoutRefusal(location.stateHomeDir, errorClass(error));
|
|
620
|
+
}
|
|
621
|
+
if (anchor === 'state-home')
|
|
622
|
+
showLog(log, 'antigravity-auth keeps its account store in its state folder', { accountStore: anchor });
|
|
623
|
+
return anchor;
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Whether the store may move into the state-home folder's path: that path
|
|
627
|
+
* must resolve through no link, because the account store refuses a file
|
|
628
|
+
* whose folder goes through one. Says so once when it does not.
|
|
629
|
+
*/
|
|
630
|
+
async function storeMovePathResolves(stateAnchor, location, log) {
|
|
631
|
+
if (folderResolvesToItself(join(location.stateHomeDir, 'antigravity-accounts.json')))
|
|
632
|
+
return true;
|
|
633
|
+
logOnce(`${location.key}\0store-linked-path`, () => log.warn(ACCOUNT_STORE_LINKED_PATH_MESSAGE, {
|
|
634
|
+
accountStore: stateAnchor,
|
|
635
|
+
stateFolder: location.stateHomeDir,
|
|
636
|
+
}));
|
|
637
|
+
return false;
|
|
638
|
+
}
|
|
639
|
+
/** Whether the store fence is open, with its one deferral log line. */
|
|
640
|
+
async function storeFenceOpen(location, options, log) {
|
|
641
|
+
const gate = await (options.storeFence ?? defaultStoreFence(options.version))();
|
|
642
|
+
if (gate.open)
|
|
643
|
+
return true;
|
|
644
|
+
logOnce(`${location.key}\0store-version-fence`, () => log.info(ACCOUNT_STORE_DEFERRED_MESSAGE, {
|
|
645
|
+
configDir: location.configDir,
|
|
646
|
+
blockers: gate.blockers,
|
|
647
|
+
}));
|
|
648
|
+
return false;
|
|
649
|
+
}
|
|
650
|
+
function defaultStoreFence(version) {
|
|
651
|
+
return () => versionFenceOpen({ currentVersion: version, storeMove: true });
|
|
652
|
+
}
|
|
653
|
+
function sameSpelling(left, right) {
|
|
654
|
+
return process.platform === 'win32'
|
|
655
|
+
? left.toLowerCase() === right.toLowerCase()
|
|
656
|
+
: left === right;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Whether anything is published at one anchor: a pointer of either schema, a
|
|
660
|
+
* pool file, or a store folder. A pointer that does not read as a forward
|
|
661
|
+
* still counts: its own reader refuses it, and no move starts over it.
|
|
662
|
+
*/
|
|
663
|
+
async function presentAccountStore(anchor) {
|
|
664
|
+
return ((await pathPresent(accountStorePointerPath(anchor))) ||
|
|
665
|
+
(await pathPresent(anchor)));
|
|
666
|
+
}
|
|
667
|
+
/** The account store's folder beside one anchor. */
|
|
668
|
+
function storeFolder(anchor) {
|
|
669
|
+
return `${anchor}.store`;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* How many leftovers an unpublished store left beside an anchor: its logical
|
|
673
|
+
* store folder, a generation folder, or one of its pointer stages.
|
|
674
|
+
*/
|
|
675
|
+
async function storeArtefacts(anchor) {
|
|
676
|
+
const names = await readdir(dirname(anchor)).catch(() => []);
|
|
677
|
+
const store = basename(storeFolder(anchor));
|
|
678
|
+
const pool = basename(anchor);
|
|
679
|
+
return names.filter((name) => name === store ||
|
|
680
|
+
name.startsWith(`${pool}.store.`) ||
|
|
681
|
+
(name.startsWith(pool) && name.endsWith('.tmp'))).length;
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* Moves an unfinished store at the state-home anchor out of the way, so the
|
|
685
|
+
* move can start again from scratch. Only a store that publishes nothing
|
|
686
|
+
* (leftovers of an interrupted move, or of a fresh installation that never
|
|
687
|
+
* published) is touched; anything else is left for its own operation.
|
|
688
|
+
*/
|
|
689
|
+
async function abandonUnpublishedStore(location, stateAnchor, log) {
|
|
690
|
+
const store = await loadCommonAuthStoreModules();
|
|
691
|
+
const binding = await readAccountStoreBinding(stateAnchor, store, Date.now);
|
|
692
|
+
if (binding.status === 'bound')
|
|
693
|
+
return false;
|
|
694
|
+
const folder = storeFolder(stateAnchor);
|
|
695
|
+
const archived = `${folder}.abandoned-${Date.now()}`;
|
|
696
|
+
await rename(folder, archived).catch((error) => {
|
|
697
|
+
if (error.code !== 'ENOENT')
|
|
698
|
+
throw error;
|
|
699
|
+
});
|
|
700
|
+
await unlink(accountStorePointerPath(stateAnchor)).catch(() => { });
|
|
701
|
+
log.info('antigravity-auth set an unfinished account store aside before starting its move again', { configDir: location.configDir, archived });
|
|
702
|
+
return true;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* One offline store move with the stopped-process confirmation `migrate`
|
|
706
|
+
* takes. The start-time move and setup call it while they hold the layout
|
|
707
|
+
* lock; `antigravity-auth store relocate` takes no lock and relies on that
|
|
708
|
+
* confirmation alone, because every OpenCode is stopped.
|
|
709
|
+
*/
|
|
710
|
+
export async function runStoreMove(input) {
|
|
711
|
+
const outcome = await createAccountRelocationFactory(await loadCommonAuthStoreModules())({
|
|
712
|
+
legacyPath: input.source,
|
|
713
|
+
targetPath: input.target,
|
|
714
|
+
...(input.back ? { back: true } : {}),
|
|
715
|
+
offline: { processesStopped: true },
|
|
716
|
+
now: Date.now,
|
|
717
|
+
});
|
|
718
|
+
if (outcome.status === 'completed')
|
|
719
|
+
return { status: 'completed', id: outcome.receipt.id };
|
|
720
|
+
if (outcome.status === 'pending')
|
|
721
|
+
return { status: 'pending', reason: outcome.reason };
|
|
722
|
+
return { status: 'cancelled' };
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Runs one store move for the start-time decision. The fence is read right
|
|
726
|
+
* before it, so a process that started meanwhile and kept the config folder
|
|
727
|
+
* stops this one. An initial move that cannot finish (another process holds
|
|
728
|
+
* a store lock, an unfinished move of another source) keeps this process at
|
|
729
|
+
* the config folder for its life, and the next start with an open fence
|
|
730
|
+
* finishes it. When an existing forward sent us here, the caller keeps that
|
|
731
|
+
* forward's anchor either way and the store reports itself unusable until a
|
|
732
|
+
* later attempt succeeds.
|
|
733
|
+
*/
|
|
734
|
+
async function moveStore(location, options, source, target, back, log, forwarded) {
|
|
735
|
+
const keepConfig = () => recordAnchor(location, options, 'config', log);
|
|
736
|
+
if (!(await storeFenceOpen(location, options, log)))
|
|
737
|
+
return keepConfig();
|
|
738
|
+
try {
|
|
739
|
+
const result = await (options.relocate ?? runStoreMove)({
|
|
740
|
+
source,
|
|
741
|
+
target,
|
|
742
|
+
back,
|
|
743
|
+
});
|
|
744
|
+
if (result.status !== 'completed') {
|
|
745
|
+
logOnce(`${location.key}\0store-move-unfinished`, () => log.info(ACCOUNT_STORE_DEFERRED_MESSAGE, {
|
|
746
|
+
configDir: location.configDir,
|
|
747
|
+
reason: result.status === 'pending' ? result.reason : result.status,
|
|
748
|
+
}));
|
|
749
|
+
// A forwarded store has no config-folder copy to fall back on: every
|
|
750
|
+
// reader must report the store as unusable, not serve the old one.
|
|
751
|
+
return forwarded ? 'state-home' : keepConfig();
|
|
752
|
+
}
|
|
753
|
+
showLog(log, "antigravity-auth moved the account store out of OpenCode's config folder", { from: source, to: target, generation: result.id });
|
|
754
|
+
}
|
|
755
|
+
catch (error) {
|
|
756
|
+
logOnce(`${location.key}\0store-move-failed`, () => log.warn("antigravity-auth's account store stays in OpenCode's config folder: moving it failed; the move is tried again at the next start", {
|
|
757
|
+
configDir: location.configDir,
|
|
758
|
+
errorClass: errorClass(error),
|
|
759
|
+
...errorFile(error),
|
|
760
|
+
}));
|
|
761
|
+
return forwarded ? 'state-home' : keepConfig();
|
|
762
|
+
}
|
|
763
|
+
return recordAnchor(location, options, 'state-home', log);
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Finishes or repeats the move an existing forward names. A move that
|
|
767
|
+
* stopped after its forward leaves a store no reader can bind; the store's
|
|
768
|
+
* own operation resumes it, or cancels it and captures again. A failure here
|
|
769
|
+
* is logged, and the anchor stays the state-home one the forward names, so
|
|
770
|
+
* every reader reports the store as unusable instead of serving the old one.
|
|
771
|
+
*/
|
|
772
|
+
async function resumeForwardedStore(location, options, configAnchor, stateAnchor, log) {
|
|
773
|
+
// A store that already binds there holds the finished move, and reading
|
|
774
|
+
// that is all this needs: a finished move is never rewritten.
|
|
775
|
+
const store = await loadCommonAuthStoreModules();
|
|
776
|
+
const binding = await readAccountStoreBinding(stateAnchor, store, Date.now);
|
|
777
|
+
if (binding.status === 'bound')
|
|
778
|
+
return;
|
|
779
|
+
try {
|
|
780
|
+
await moveStore(location, options, configAnchor, stateAnchor, false, log, true);
|
|
781
|
+
}
|
|
782
|
+
catch (error) {
|
|
783
|
+
logOnce(`${location.key}\0store-resume-failed`, () => log.warn("antigravity-auth's account store is still moving; it could not be finished at this start", {
|
|
784
|
+
configDir: location.configDir,
|
|
785
|
+
errorClass: errorClass(error),
|
|
786
|
+
...errorFile(error),
|
|
787
|
+
}));
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Creates an empty store at the state-home anchor for a genuinely fresh
|
|
792
|
+
* installation and leaves a forward at the config anchor, so a version
|
|
793
|
+
* without the store move refuses instead of creating a second, empty store.
|
|
794
|
+
* When the store cannot be created there, this process keeps the config
|
|
795
|
+
* folder and the next start tries again.
|
|
796
|
+
*/
|
|
797
|
+
async function initializeStore(location, options, stateAnchor, log) {
|
|
798
|
+
const keepConfig = () => recordAnchor(location, options, 'config', log);
|
|
799
|
+
if (!(await storeFenceOpen(location, options, log)))
|
|
800
|
+
return keepConfig();
|
|
801
|
+
try {
|
|
802
|
+
const store = await loadCommonAuthStoreModules();
|
|
803
|
+
const created = await initializeFreshAccountStore(store, {
|
|
804
|
+
legacyPath: stateAnchor,
|
|
805
|
+
now: Date.now,
|
|
806
|
+
});
|
|
807
|
+
if (created.status !== 'completed')
|
|
808
|
+
throw new LayoutMoveError('fresh-store', `the store is ${created.status}`);
|
|
809
|
+
await writeStateHomeStoreForward(created.receipt.id, stateAnchor);
|
|
810
|
+
}
|
|
811
|
+
catch (error) {
|
|
812
|
+
logOnce(`${location.key}\0store-init-failed`, () => log.warn("antigravity-auth's account store stays in OpenCode's config folder: creating it in its state folder failed; it is tried again at the next start", {
|
|
813
|
+
configDir: location.configDir,
|
|
814
|
+
errorClass: errorClass(error),
|
|
815
|
+
...errorFile(error),
|
|
816
|
+
}));
|
|
817
|
+
return keepConfig();
|
|
818
|
+
}
|
|
819
|
+
showLog(log, 'antigravity-auth created its account store in its state folder', {
|
|
820
|
+
accountStore: stateAnchor,
|
|
821
|
+
});
|
|
822
|
+
return recordAnchor(location, options, 'state-home', log);
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* Leaves a forwarding pointer at the config anchor for a store that now
|
|
826
|
+
* lives at the state-home anchor: the same schema-2 pointer a store move
|
|
827
|
+
* writes, so a version without the store move refuses it instead of reading
|
|
828
|
+
* "no accounts". Written atomically and durably through the account store's
|
|
829
|
+
* own writer.
|
|
830
|
+
*/
|
|
831
|
+
export async function writeStateHomeStoreForward(id, stateAnchor, configAnchor = legacyAccountFile(runtimeLayoutLocation().configDir)) {
|
|
832
|
+
const forward = {
|
|
833
|
+
schemaVersion: 2,
|
|
834
|
+
id,
|
|
835
|
+
directoryBasename: basename(storeFolder(stateAnchor)),
|
|
836
|
+
relocatedTo: { legacyPath: stateAnchor, id },
|
|
837
|
+
};
|
|
838
|
+
const fs = await loadCommonAuthFs();
|
|
839
|
+
await fs.writeJsonAtomic(accountStorePointerPath(configAnchor), forward, {
|
|
840
|
+
durable: true,
|
|
841
|
+
durability: 'file-only-on-windows',
|
|
842
|
+
});
|
|
843
|
+
}
|
|
450
844
|
function logDeferral(location, blockers, log) {
|
|
451
845
|
logOnce(`${location.key}\0deferred`, () => log.info(RUNTIME_LAYOUT_DEFERRED_MESSAGE, {
|
|
452
846
|
configDir: location.configDir,
|
|
@@ -709,7 +1103,7 @@ async function takeDataLocks(files, log) {
|
|
|
709
1103
|
function layoutFiles(location, accountFile) {
|
|
710
1104
|
return {
|
|
711
1105
|
vault: {
|
|
712
|
-
old: legacyVaultDir(accountFile ??
|
|
1106
|
+
old: legacyVaultDir(accountFile ?? accountFileForVault(location.configDir)),
|
|
713
1107
|
next: stateHomeVaultDir(location),
|
|
714
1108
|
},
|
|
715
1109
|
};
|