@cortexkit/opencode-antigravity-auth 3.0.12 → 3.0.14
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 +9839 -6706
- package/dist/cli.js.map +4 -4
- package/dist/cli.meta.json +722 -175
- package/dist/index.js +4902 -1443
- package/dist/index.js.map +4 -4
- package/dist/index.meta.json +756 -190
- package/dist/server.js +8671 -4993
- package/dist/server.js.map +4 -4
- package/dist/server.meta.json +1 -1
- package/dist/src/__fixtures__/runtime-layout-process.d.ts +2 -0
- package/dist/src/__fixtures__/runtime-layout-process.d.ts.map +1 -0
- package/dist/src/__fixtures__/runtime-layout-process.js +88 -0
- package/dist/src/__fixtures__/runtime-layout-process.js.map +1 -0
- package/dist/src/cli.d.ts +26 -0
- package/dist/src/cli.d.ts.map +1 -1
- package/dist/src/cli.js +44 -1
- package/dist/src/cli.js.map +1 -1
- package/dist/src/common-auth-embedded/claustrum/consumer.d.ts +77 -2
- package/dist/src/common-auth-embedded/claustrum/consumer.js +320 -35
- package/dist/src/common-auth-embedded/claustrum/custody.d.ts +36 -1
- package/dist/src/common-auth-embedded/claustrum/custody.js +95 -11
- package/dist/src/common-auth-embedded/claustrum/enrollment.d.ts +9 -0
- package/dist/src/common-auth-embedded/claustrum/enrollment.js +52 -20
- package/dist/src/common-auth-embedded/claustrum/hub.d.ts +195 -0
- package/dist/src/common-auth-embedded/claustrum/hub.js +1032 -0
- package/dist/src/common-auth-embedded/claustrum/index.d.ts +13 -3
- package/dist/src/common-auth-embedded/claustrum/index.js +37 -4
- package/dist/src/common-auth-embedded/claustrum/quota-writer.d.ts +8 -1
- package/dist/src/common-auth-embedded/claustrum/quota-writer.js +34 -12
- package/dist/src/common-auth-embedded/claustrum/roster.d.ts +49 -1
- package/dist/src/common-auth-embedded/claustrum/roster.js +187 -12
- package/dist/src/common-auth-embedded/claustrum/shutdown.js +42 -0
- package/dist/src/common-auth-embedded/fs/file-read-cache.js +57 -0
- package/dist/src/common-auth-embedded/fs/refresh-file-lock.d.ts +1 -1
- package/dist/src/common-auth-embedded/fs/refresh-file-lock.js +36 -14
- package/dist/src/common-auth-embedded/opencode2/errors.d.ts +4 -1
- package/dist/src/common-auth-embedded/opencode2/index.d.ts +2 -2
- package/dist/src/common-auth-embedded/opencode2/index.js +1 -1
- package/dist/src/common-auth-embedded/opencode2/install.d.ts +7 -5
- package/dist/src/common-auth-embedded/opencode2/install.js +139 -13
- package/dist/src/common-auth-embedded/opencode2/types.d.ts +42 -10
- package/dist/src/common-auth-embedded/quota/commit-interval.d.ts +9 -0
- package/dist/src/common-auth-embedded/quota/commit-interval.js +18 -0
- package/dist/src/common-auth-embedded/rpc/rpc-server.js +29 -19
- package/dist/src/common-auth-embedded/setup/command.js +40 -47
- package/dist/src/common-auth-embedded/setup/flags.d.ts +50 -0
- package/dist/src/common-auth-embedded/setup/flags.js +123 -0
- package/dist/src/common-auth-embedded/setup/index.d.ts +1 -0
- package/dist/src/common-auth-embedded/setup/index.js +1 -0
- package/dist/src/common-auth-embedded/setup/register.d.ts +2 -0
- package/dist/src/common-auth-embedded/setup/register.js +90 -11
- package/dist/src/common-auth-embedded/setup/types.d.ts +7 -1
- package/dist/src/common-auth-embedded/source-output.json +173 -109
- package/dist/src/common-auth-embedded/store/mutate.d.ts +2 -0
- package/dist/src/common-auth-embedded/store/mutate.js +4 -0
- package/dist/src/common-auth-embedded/store/pool.d.ts +6 -1
- package/dist/src/common-auth-embedded/store/pool.js +1 -1
- package/dist/src/common-auth-embedded/store/quota-writer.js +27 -13
- package/dist/src/common-auth-embedded/store/settings.d.ts +1 -1
- package/dist/src/common-auth-embedded/store/settings.js +16 -6
- package/dist/src/ga/rpc/protocol.d.ts +2 -0
- package/dist/src/ga/rpc/protocol.d.ts.map +1 -1
- package/dist/src/ga/rpc/protocol.js +2 -2
- package/dist/src/ga/rpc/protocol.js.map +1 -1
- package/dist/src/ga/server/auth.d.ts.map +1 -1
- package/dist/src/ga/server/auth.js +7 -2
- package/dist/src/ga/server/auth.js.map +1 -1
- package/dist/src/ga/server/index.d.ts.map +1 -1
- package/dist/src/ga/server/index.js +25 -0
- package/dist/src/ga/server/index.js.map +1 -1
- package/dist/src/ga/tui/host-ga.d.ts.map +1 -1
- package/dist/src/ga/tui/host-ga.js +2 -1
- package/dist/src/ga/tui/host-ga.js.map +1 -1
- package/dist/src/plugin/__fixtures__/controlled-timeouts.d.ts +7 -0
- package/dist/src/plugin/__fixtures__/controlled-timeouts.d.ts.map +1 -0
- package/dist/src/plugin/__fixtures__/controlled-timeouts.js +41 -0
- package/dist/src/plugin/__fixtures__/controlled-timeouts.js.map +1 -0
- package/dist/src/plugin/__fixtures__/release-model-ids.d.ts +2 -0
- package/dist/src/plugin/__fixtures__/release-model-ids.d.ts.map +1 -0
- package/dist/src/plugin/__fixtures__/release-model-ids.js +11 -0
- package/dist/src/plugin/__fixtures__/release-model-ids.js.map +1 -0
- package/dist/src/plugin/__fixtures__/verified-store.d.ts +3 -0
- package/dist/src/plugin/__fixtures__/verified-store.d.ts.map +1 -0
- package/dist/src/plugin/__fixtures__/verified-store.js +50 -0
- package/dist/src/plugin/__fixtures__/verified-store.js.map +1 -0
- package/dist/src/plugin/auth-loader.d.ts +7 -1
- package/dist/src/plugin/auth-loader.d.ts.map +1 -1
- package/dist/src/plugin/auth-loader.js +4 -4
- package/dist/src/plugin/auth-loader.js.map +1 -1
- package/dist/src/plugin/background-vault-quota-refresh.d.ts +10 -0
- package/dist/src/plugin/background-vault-quota-refresh.d.ts.map +1 -1
- package/dist/src/plugin/background-vault-quota-refresh.js +33 -5
- package/dist/src/plugin/background-vault-quota-refresh.js.map +1 -1
- package/dist/src/plugin/cache/signature-cache.d.ts +7 -1
- package/dist/src/plugin/cache/signature-cache.d.ts.map +1 -1
- package/dist/src/plugin/cache/signature-cache.js +10 -13
- package/dist/src/plugin/cache/signature-cache.js.map +1 -1
- package/dist/src/plugin/command-data.d.ts +2 -2
- package/dist/src/plugin/command-data.d.ts.map +1 -1
- package/dist/src/plugin/command-data.js +2 -0
- package/dist/src/plugin/command-data.js.map +1 -1
- package/dist/src/plugin/config/loader.d.ts +2 -1
- package/dist/src/plugin/config/loader.d.ts.map +1 -1
- package/dist/src/plugin/config/loader.js +4 -2
- package/dist/src/plugin/config/loader.js.map +1 -1
- package/dist/src/plugin/config/operator-settings-schema.d.ts +1 -1
- package/dist/src/plugin/config/schema.d.ts +1 -1
- package/dist/src/plugin/config/schema.js +1 -1
- package/dist/src/plugin/debug.d.ts.map +1 -1
- package/dist/src/plugin/debug.js +18 -19
- package/dist/src/plugin/debug.js.map +1 -1
- package/dist/src/plugin/errors.js +1 -1
- package/dist/src/plugin/errors.js.map +1 -1
- package/dist/src/plugin/ga-location-services.d.ts +28 -2
- package/dist/src/plugin/ga-location-services.d.ts.map +1 -1
- package/dist/src/plugin/ga-location-services.js +118 -31
- package/dist/src/plugin/ga-location-services.js.map +1 -1
- package/dist/src/plugin/ga-shared-vault-custody.d.ts +78 -0
- package/dist/src/plugin/ga-shared-vault-custody.d.ts.map +1 -0
- package/dist/src/plugin/ga-shared-vault-custody.js +177 -0
- package/dist/src/plugin/ga-shared-vault-custody.js.map +1 -0
- package/dist/src/plugin/index.d.ts.map +1 -1
- package/dist/src/plugin/index.js +38 -6
- package/dist/src/plugin/index.js.map +1 -1
- package/dist/src/plugin/killswitch.d.ts.map +1 -1
- package/dist/src/plugin/killswitch.js +13 -7
- package/dist/src/plugin/killswitch.js.map +1 -1
- package/dist/src/plugin/oauth-methods.d.ts.map +1 -1
- package/dist/src/plugin/oauth-methods.js +3 -0
- package/dist/src/plugin/oauth-methods.js.map +1 -1
- package/dist/src/plugin/shared/request-services.d.ts.map +1 -1
- package/dist/src/plugin/shared/request-services.js +2 -3
- package/dist/src/plugin/shared/request-services.js.map +1 -1
- package/dist/src/plugin/shared/source-sidebar.d.ts +6 -0
- package/dist/src/plugin/shared/source-sidebar.d.ts.map +1 -1
- package/dist/src/plugin/shared/source-sidebar.js +1 -1
- package/dist/src/plugin/shared/source-sidebar.js.map +1 -1
- package/dist/src/plugin/shared/vault-request-credentials.d.ts +8 -1
- package/dist/src/plugin/shared/vault-request-credentials.d.ts.map +1 -1
- package/dist/src/plugin/shared/vault-request-credentials.js +38 -5
- package/dist/src/plugin/shared/vault-request-credentials.js.map +1 -1
- package/dist/src/plugin/storage.d.ts +5 -2
- package/dist/src/plugin/storage.d.ts.map +1 -1
- package/dist/src/plugin/storage.js +5 -4
- package/dist/src/plugin/storage.js.map +1 -1
- package/dist/src/plugin/ui/auth-menu.d.ts.map +1 -1
- package/dist/src/plugin/ui/auth-menu.js +3 -0
- package/dist/src/plugin/ui/auth-menu.js.map +1 -1
- package/dist/src/plugin/ui/quota-status.d.ts.map +1 -1
- package/dist/src/plugin/ui/quota-status.js +4 -2
- package/dist/src/plugin/ui/quota-status.js.map +1 -1
- package/dist/src/plugin/vault-custody.d.ts +57 -15
- package/dist/src/plugin/vault-custody.d.ts.map +1 -1
- package/dist/src/plugin/vault-custody.js +259 -45
- package/dist/src/plugin/vault-custody.js.map +1 -1
- package/dist/src/plugin/vault-insula-quota.d.ts.map +1 -1
- package/dist/src/plugin/vault-insula-quota.js +4 -0
- package/dist/src/plugin/vault-insula-quota.js.map +1 -1
- package/dist/src/plugin/vault-sidebar-projection.d.ts.map +1 -1
- package/dist/src/process-heartbeat.d.ts +96 -0
- package/dist/src/process-heartbeat.d.ts.map +1 -0
- package/dist/src/process-heartbeat.js +285 -0
- package/dist/src/process-heartbeat.js.map +1 -0
- package/dist/src/rpc/rpc-server.d.ts +8 -0
- package/dist/src/rpc/rpc-server.d.ts.map +1 -1
- package/dist/src/rpc/rpc-server.js +26 -1
- package/dist/src/rpc/rpc-server.js.map +1 -1
- package/dist/src/runtime-layout.d.ts +100 -0
- package/dist/src/runtime-layout.d.ts.map +1 -0
- package/dist/src/runtime-layout.js +1242 -0
- package/dist/src/runtime-layout.js.map +1 -0
- package/dist/src/runtime-paths.d.ts +132 -0
- package/dist/src/runtime-paths.d.ts.map +1 -0
- package/dist/src/runtime-paths.js +214 -0
- package/dist/src/runtime-paths.js.map +1 -0
- package/dist/src/setup.d.ts.map +1 -1
- package/dist/src/setup.js +31 -5
- package/dist/src/setup.js.map +1 -1
- package/dist/src/sidebar-projection.d.ts +3 -1
- package/dist/src/sidebar-projection.d.ts.map +1 -1
- package/dist/src/sidebar-projection.js +10 -1
- package/dist/src/sidebar-projection.js.map +1 -1
- package/dist/src/sidebar-state.d.ts +10 -1
- package/dist/src/sidebar-state.d.ts.map +1 -1
- package/dist/src/sidebar-state.js +12 -2
- package/dist/src/sidebar-state.js.map +1 -1
- package/dist/src/tui/quota-layout.d.ts +3 -1
- package/dist/src/tui/quota-layout.d.ts.map +1 -1
- package/dist/src/tui/quota-layout.js +6 -1
- package/dist/src/tui/quota-layout.js.map +1 -1
- package/dist/src/vault-folder.d.ts +38 -0
- package/dist/src/vault-folder.d.ts.map +1 -0
- package/dist/src/vault-folder.js +91 -0
- package/dist/src/vault-folder.js.map +1 -0
- package/dist/src/version-fence.d.ts +54 -0
- package/dist/src/version-fence.d.ts.map +1 -0
- package/dist/src/version-fence.js +254 -0
- package/dist/src/version-fence.js.map +1 -0
- package/dist/tui-build-map.json +9 -9
- package/dist/tui-ga-build-map.json +13 -13
- package/dist/types/src/cli.d.ts +26 -0
- package/dist/types/src/cli.d.ts.map +1 -1
- package/dist/types/src/common-auth-embedded/claustrum/consumer.d.ts +77 -2
- package/dist/types/src/common-auth-embedded/claustrum/custody.d.ts +36 -1
- package/dist/types/src/common-auth-embedded/claustrum/enrollment.d.ts +9 -0
- package/dist/types/src/common-auth-embedded/claustrum/hub.d.ts +195 -0
- package/dist/types/src/common-auth-embedded/claustrum/index.d.ts +13 -3
- package/dist/types/src/common-auth-embedded/claustrum/quota-writer.d.ts +8 -1
- package/dist/types/src/common-auth-embedded/claustrum/roster.d.ts +49 -1
- package/dist/types/src/common-auth-embedded/fs/refresh-file-lock.d.ts +1 -1
- package/dist/types/src/common-auth-embedded/opencode2/errors.d.ts +4 -1
- package/dist/types/src/common-auth-embedded/opencode2/index.d.ts +2 -2
- package/dist/types/src/common-auth-embedded/opencode2/install.d.ts +7 -5
- package/dist/types/src/common-auth-embedded/opencode2/types.d.ts +42 -10
- package/dist/types/src/common-auth-embedded/quota/commit-interval.d.ts +9 -0
- package/dist/types/src/common-auth-embedded/setup/flags.d.ts +50 -0
- package/dist/types/src/common-auth-embedded/setup/index.d.ts +1 -0
- package/dist/types/src/common-auth-embedded/setup/register.d.ts +2 -0
- package/dist/types/src/common-auth-embedded/setup/types.d.ts +7 -1
- package/dist/types/src/common-auth-embedded/store/mutate.d.ts +2 -0
- package/dist/types/src/common-auth-embedded/store/pool.d.ts +6 -1
- package/dist/types/src/common-auth-embedded/store/settings.d.ts +1 -1
- package/dist/types/src/ga/rpc/protocol.d.ts +2 -0
- package/dist/types/src/ga/rpc/protocol.d.ts.map +1 -1
- package/dist/types/src/ga/server/auth.d.ts.map +1 -1
- package/dist/types/src/ga/server/index.d.ts.map +1 -1
- package/dist/types/src/plugin/auth-loader.d.ts +7 -1
- package/dist/types/src/plugin/auth-loader.d.ts.map +1 -1
- package/dist/types/src/plugin/background-vault-quota-refresh.d.ts +10 -0
- package/dist/types/src/plugin/background-vault-quota-refresh.d.ts.map +1 -1
- package/dist/types/src/plugin/cache/signature-cache.d.ts +7 -1
- package/dist/types/src/plugin/cache/signature-cache.d.ts.map +1 -1
- package/dist/types/src/plugin/command-data.d.ts +2 -2
- package/dist/types/src/plugin/command-data.d.ts.map +1 -1
- package/dist/types/src/plugin/config/loader.d.ts +2 -1
- package/dist/types/src/plugin/config/loader.d.ts.map +1 -1
- package/dist/types/src/plugin/debug.d.ts.map +1 -1
- package/dist/types/src/plugin/ga-location-services.d.ts +28 -2
- package/dist/types/src/plugin/ga-location-services.d.ts.map +1 -1
- package/dist/types/src/plugin/ga-shared-vault-custody.d.ts +78 -0
- package/dist/types/src/plugin/ga-shared-vault-custody.d.ts.map +1 -0
- package/dist/types/src/plugin/index.d.ts.map +1 -1
- package/dist/types/src/plugin/killswitch.d.ts.map +1 -1
- package/dist/types/src/plugin/oauth-methods.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/request-services.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/source-sidebar.d.ts +6 -0
- package/dist/types/src/plugin/shared/source-sidebar.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/vault-request-credentials.d.ts +8 -1
- package/dist/types/src/plugin/shared/vault-request-credentials.d.ts.map +1 -1
- package/dist/types/src/plugin/storage.d.ts +5 -2
- package/dist/types/src/plugin/storage.d.ts.map +1 -1
- package/dist/types/src/plugin/ui/auth-menu.d.ts.map +1 -1
- package/dist/types/src/plugin/ui/quota-status.d.ts.map +1 -1
- package/dist/types/src/plugin/vault-custody.d.ts +57 -15
- package/dist/types/src/plugin/vault-custody.d.ts.map +1 -1
- package/dist/types/src/plugin/vault-insula-quota.d.ts.map +1 -1
- package/dist/types/src/plugin/vault-sidebar-projection.d.ts.map +1 -1
- package/dist/types/src/process-heartbeat.d.ts +96 -0
- package/dist/types/src/process-heartbeat.d.ts.map +1 -0
- package/dist/types/src/rpc/rpc-server.d.ts +8 -0
- package/dist/types/src/rpc/rpc-server.d.ts.map +1 -1
- package/dist/types/src/runtime-layout.d.ts +100 -0
- package/dist/types/src/runtime-layout.d.ts.map +1 -0
- package/dist/types/src/runtime-paths.d.ts +132 -0
- package/dist/types/src/runtime-paths.d.ts.map +1 -0
- package/dist/types/src/setup.d.ts.map +1 -1
- package/dist/types/src/sidebar-state.d.ts +10 -1
- package/dist/types/src/sidebar-state.d.ts.map +1 -1
- package/dist/types/src/vault-folder.d.ts +38 -0
- package/dist/types/src/vault-folder.d.ts.map +1 -0
- package/dist/types/src/version-fence.d.ts +54 -0
- package/dist/types/src/version-fence.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/common-auth-embedded/claustrum/consumer.d.ts +77 -2
- package/src/common-auth-embedded/claustrum/consumer.js +320 -35
- package/src/common-auth-embedded/claustrum/custody.d.ts +36 -1
- package/src/common-auth-embedded/claustrum/custody.js +95 -11
- package/src/common-auth-embedded/claustrum/enrollment.d.ts +9 -0
- package/src/common-auth-embedded/claustrum/enrollment.js +52 -20
- package/src/common-auth-embedded/claustrum/hub.d.ts +195 -0
- package/src/common-auth-embedded/claustrum/hub.js +1032 -0
- package/src/common-auth-embedded/claustrum/index.d.ts +13 -3
- package/src/common-auth-embedded/claustrum/index.js +37 -4
- package/src/common-auth-embedded/claustrum/quota-writer.d.ts +8 -1
- package/src/common-auth-embedded/claustrum/quota-writer.js +34 -12
- package/src/common-auth-embedded/claustrum/roster.d.ts +49 -1
- package/src/common-auth-embedded/claustrum/roster.js +187 -12
- package/src/common-auth-embedded/claustrum/shutdown.js +42 -0
- package/src/common-auth-embedded/fs/file-read-cache.js +57 -0
- package/src/common-auth-embedded/fs/refresh-file-lock.d.ts +1 -1
- package/src/common-auth-embedded/fs/refresh-file-lock.js +36 -14
- package/src/common-auth-embedded/opencode2/errors.d.ts +4 -1
- package/src/common-auth-embedded/opencode2/index.d.ts +2 -2
- package/src/common-auth-embedded/opencode2/index.js +1 -1
- package/src/common-auth-embedded/opencode2/install.d.ts +7 -5
- package/src/common-auth-embedded/opencode2/install.js +139 -13
- package/src/common-auth-embedded/opencode2/types.d.ts +42 -10
- package/src/common-auth-embedded/quota/commit-interval.d.ts +9 -0
- package/src/common-auth-embedded/quota/commit-interval.js +18 -0
- package/src/common-auth-embedded/rpc/rpc-server.js +29 -19
- package/src/common-auth-embedded/setup/command.js +40 -47
- package/src/common-auth-embedded/setup/flags.d.ts +50 -0
- package/src/common-auth-embedded/setup/flags.js +123 -0
- package/src/common-auth-embedded/setup/index.d.ts +1 -0
- package/src/common-auth-embedded/setup/index.js +1 -0
- package/src/common-auth-embedded/setup/register.d.ts +2 -0
- package/src/common-auth-embedded/setup/register.js +90 -11
- package/src/common-auth-embedded/setup/types.d.ts +7 -1
- package/src/common-auth-embedded/source-output.json +173 -109
- package/src/common-auth-embedded/store/mutate.d.ts +2 -0
- package/src/common-auth-embedded/store/mutate.js +4 -0
- package/src/common-auth-embedded/store/pool.d.ts +6 -1
- package/src/common-auth-embedded/store/pool.js +1 -1
- package/src/common-auth-embedded/store/quota-writer.js +27 -13
- package/src/common-auth-embedded/store/settings.d.ts +1 -1
- package/src/common-auth-embedded/store/settings.js +16 -6
- package/src/plugin/command-data.ts +4 -2
- package/src/sidebar-state.ts +23 -3
- package/src/tui-compiled/sidebar-state.js +4 -3
- package/src/tui-compiled/tui/quota-layout.js +3 -2
- package/src/tui-ga-compiled/host-ga.js +2 -1
- package/src/tui-ga-compiled/shared/8ae1927e5910ea81-sidebar-projection.js +9 -2
- package/src/tui-ga-compiled/shared/dd4c9e1a907f8ef0-quota-layout.js +3 -2
- package/src/tui-ga-raw/host-ga.tsx +12 -1
- package/src/tui-ga-raw/shared/8ae1927e5910ea81-sidebar-projection.js +9 -2
- package/src/tui-ga-raw/shared/dd4c9e1a907f8ef0-quota-layout.tsx +9 -2
- package/src/tui-raw/sidebar-state.js +4 -3
- package/src/tui-raw/tui/quota-layout.tsx +9 -2
|
@@ -0,0 +1,1242 @@
|
|
|
1
|
+
// Moves the plugin's vault files out of OpenCode's config folder, once.
|
|
2
|
+
//
|
|
3
|
+
// OpenCode 2 watches its config folder, and the plugin rewrites its vault
|
|
4
|
+
// files there every few seconds: the vault roster, the vault provider state
|
|
5
|
+
// and the enrollment files, each with lock files, eviction markers and staged
|
|
6
|
+
// temporary files. They move to the state home (`runtime-paths.ts` names both
|
|
7
|
+
// folders). The account store, `antigravity.json` and the TUI preferences stay
|
|
8
|
+
// in OpenCode's config folder: the account store records its own absolute
|
|
9
|
+
// paths and refuses to open anywhere else.
|
|
10
|
+
//
|
|
11
|
+
// This is a port of openai-auth's `runtime-layout.ts`, with these changes:
|
|
12
|
+
// only the five `opencode-*` vault files below move (Pi's files can share the
|
|
13
|
+
// vault folder and are never touched); the move holds the vault's own locks;
|
|
14
|
+
// there is no state-file override and no sessions file; an existing,
|
|
15
|
+
// non-private destination vault folder on Windows keeps the legacy layout.
|
|
16
|
+
//
|
|
17
|
+
// The rules:
|
|
18
|
+
// - A process fixes its layout for a config folder once, at plugin start,
|
|
19
|
+
// before it builds the vault, and never switches while it runs. A live
|
|
20
|
+
// switch would race the writes the process already has in flight against
|
|
21
|
+
// the old paths.
|
|
22
|
+
// - `layout.json` in the state-home folder makes that folder authoritative.
|
|
23
|
+
// Without it, any old vault file keeps the config folder authoritative;
|
|
24
|
+
// with no old file either (a fresh install) the marker is written straight
|
|
25
|
+
// away. `config-dir.json` names the config folder the state-home folder
|
|
26
|
+
// belongs to; a folder that names another one is refused and left alone.
|
|
27
|
+
// - The move, and the fresh install's marker, wait while any other live
|
|
28
|
+
// process could still use the old files: one that runs an older version, or
|
|
29
|
+
// one on the legacy layout (its heartbeat says so, `process-heartbeat.ts`).
|
|
30
|
+
// A process that starts while one of those is alive takes the legacy layout
|
|
31
|
+
// too. So the move happens at the first start after every OpenCode using
|
|
32
|
+
// that config folder has been closed or restarted on this version.
|
|
33
|
+
// - Every decision is made holding the layout lock in the state-home folder.
|
|
34
|
+
// A legacy choice is allowed only once it is in this process's heartbeat,
|
|
35
|
+
// written while that lock is held, so a process deciding later sees it. A
|
|
36
|
+
// process that cannot take the lock or write that record refuses to start
|
|
37
|
+
// (`RuntimeLayoutRefusal`) rather than run unseen on the old files.
|
|
38
|
+
// - The move holds the layout lock, then the vault's own locks at the old
|
|
39
|
+
// files: `ceremony` at the enrollment state, `claustrum-roster-write` at
|
|
40
|
+
// the roster and `antigravity-vault-state` at the provider state, the order
|
|
41
|
+
// every vault writer takes them in. Under all of them it reads the
|
|
42
|
+
// heartbeats again, refuses when an old file and its new place are one
|
|
43
|
+
// physical file, and refuses when the state-home vault folder holds a vault
|
|
44
|
+
// file that is not one of the copies an unfinished move listed in
|
|
45
|
+
// `layout-moving.json`. Each file is copied owner-only and made durable,
|
|
46
|
+
// read back and compared byte for byte; the marker is written once every
|
|
47
|
+
// copy is verified, and only then are the old files removed, each one
|
|
48
|
+
// after comparing its bytes with its copy once more. A crash before the
|
|
49
|
+
// marker leaves the old files authoritative and the next start copies
|
|
50
|
+
// again over the partial new ones. A crash after it leaves the new files
|
|
51
|
+
// authoritative; the next
|
|
52
|
+
// start takes the same locks, reads the heartbeats again under them, and
|
|
53
|
+
// removes old copies identical to the new ones. A differing old file, or a
|
|
54
|
+
// copy that cannot be read, refuses the start: every vault file counts as a
|
|
55
|
+
// credential. An identical old file that cannot be removed (Windows may
|
|
56
|
+
// hold a file open), or that Windows keeps open so it cannot be compared,
|
|
57
|
+
// is logged once as a clean-up failure, not refused: the marker then does
|
|
58
|
+
// not record the old files as dealt with, so the next start tries the
|
|
59
|
+
// removal again. Once the marker records that the old files are dealt
|
|
60
|
+
// with, old vault files that reappear later refuse the start.
|
|
61
|
+
// - An enrollment that keeps the host in vault mode (approved, or blocked
|
|
62
|
+
// because its token is missing) keeps one old file: the enrollment state
|
|
63
|
+
// file stays in the old vault folder, byte for byte the copy that moved,
|
|
64
|
+
// and only the old token is removed. Every version reads such a state
|
|
65
|
+
// without a token as vault mode with a missing token, never as local, so an
|
|
66
|
+
// older version that still looks in the config folder refuses requests
|
|
67
|
+
// instead of serving local accounts. Later starts accept vault mode there
|
|
68
|
+
// whether or not an older version has since rewrote that file
|
|
69
|
+
// (`isVaultShapedKeptState`); the marker's SHA-256 of it (`keptVaultState`)
|
|
70
|
+
// is no longer compared. An older version's reconcile turns an approved
|
|
71
|
+
// state into `blocked missing_token`, and a file at that path holds no
|
|
72
|
+
// secret, so the file is re-read and accepted while it still names this
|
|
73
|
+
// plugin's enrollment, has no token beside it, and is pending in neither
|
|
74
|
+
// direction. Anything else at that path is a conflict like any other
|
|
75
|
+
// reappeared file. Disconnect on this version removes such a leftover
|
|
76
|
+
// (`removeMovedVaultStateLeftover`). A pending or denied enrollment state
|
|
77
|
+
// protects nothing and is removed like the other files. Until the old
|
|
78
|
+
// files are dealt with, though, a marker that records `keptVaultState`
|
|
79
|
+
// says the move left that file on purpose: an old state file identical to
|
|
80
|
+
// its state-home copy then stays even when the re-read rejects it, since
|
|
81
|
+
// removing it would let an older version read this host as local.
|
|
82
|
+
// - The layout lock is checked again right before every destructive step:
|
|
83
|
+
// each unlink attempt of an old file, the removal of `layout-moving.json`
|
|
84
|
+
// and of the old vault folder. `layout-moving.json` is the last thing a
|
|
85
|
+
// move removes, and one left behind is removed at the next start, under
|
|
86
|
+
// the lock. A move that fails leaves the process on the legacy layout
|
|
87
|
+
// (registered) and logs the error class once; the next start tries again.
|
|
88
|
+
// Nothing here runs on a request.
|
|
89
|
+
// - The signature cache moved to the state home as well, but it is a cache:
|
|
90
|
+
// it is recreated there, not copied. The old file in OpenCode's config
|
|
91
|
+
// folder (`legacySignatureCacheFile`) is deleted once, in the same
|
|
92
|
+
// clean-up step that records the old vault files as dealt with.
|
|
93
|
+
import { createHash } from 'node:crypto';
|
|
94
|
+
import { chmod, mkdir, readdir, readFile, rmdir, stat, unlink, } from 'node:fs/promises';
|
|
95
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
96
|
+
import { loadCommonAuthClaustrum, loadCommonAuthFs, } from '@cortexkit/antigravity-auth-core';
|
|
97
|
+
import { createLogger } from './plugin/logger';
|
|
98
|
+
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';
|
|
101
|
+
import { versionFenceOpen } from './version-fence';
|
|
102
|
+
/** Name of the lock held while a process decides or moves the layout. */
|
|
103
|
+
export const RUNTIME_LAYOUT_LOCK_NAME = 'runtime-layout';
|
|
104
|
+
/** Lists the copies an unfinished move has put in the state-home folder. */
|
|
105
|
+
export const RUNTIME_LAYOUT_MANIFEST_FILE = 'layout-moving.json';
|
|
106
|
+
/**
|
|
107
|
+
* The enrollment state file. A move leaves it in the old folder when it keeps
|
|
108
|
+
* the host in vault mode without its token (approved, or blocked because the
|
|
109
|
+
* token is missing).
|
|
110
|
+
*/
|
|
111
|
+
export const VAULT_ENROLLMENT_STATE_FILE = 'opencode-enrollment-state.json';
|
|
112
|
+
/** The enrollment token file, the one vault file a kept state must not sit beside. */
|
|
113
|
+
const VAULT_ENROLLMENT_TOKEN_FILE = 'opencode-enrollment.json';
|
|
114
|
+
/**
|
|
115
|
+
* The vault files that move, and the only ones the move ever reads, copies,
|
|
116
|
+
* compares or removes in either vault folder. Anything else there (Pi's
|
|
117
|
+
* files, when Pi's account file points into OpenCode's config folder, or a
|
|
118
|
+
* file a later library version adds) is left alone; a new vault file has to
|
|
119
|
+
* be added here on purpose.
|
|
120
|
+
*/
|
|
121
|
+
export const VAULT_FILE_NAMES = [
|
|
122
|
+
VAULT_ENROLLMENT_TOKEN_FILE,
|
|
123
|
+
VAULT_ENROLLMENT_STATE_FILE,
|
|
124
|
+
'opencode-roster.json',
|
|
125
|
+
'opencode-state.json',
|
|
126
|
+
'opencode-mode.json',
|
|
127
|
+
];
|
|
128
|
+
/** Every lease this module takes lasts this long and is renewed while held. */
|
|
129
|
+
const LOCK_TTL_MS = 10_000;
|
|
130
|
+
/**
|
|
131
|
+
* The vault's own locks, in the order its writers take them: the enrollment
|
|
132
|
+
* ceremony (`claustrum/enrollment.js`), the roster write
|
|
133
|
+
* (`claustrum/roster.js`), then the provider state
|
|
134
|
+
* (`vault-provider-state.ts`). Each waits for the writer's lease to run out
|
|
135
|
+
* plus 5 s: a crashed writer's ceremony lease lasts 30 s, the others 10 s.
|
|
136
|
+
* `opencode-mode.json` has no lock; the comparison before its unlink covers
|
|
137
|
+
* it.
|
|
138
|
+
*/
|
|
139
|
+
const DATA_LOCKS = [
|
|
140
|
+
{ name: 'ceremony', file: VAULT_ENROLLMENT_STATE_FILE, waitMs: 35_000 },
|
|
141
|
+
{
|
|
142
|
+
name: 'claustrum-roster-write',
|
|
143
|
+
file: 'opencode-roster.json',
|
|
144
|
+
waitMs: 15_000,
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
name: 'antigravity-vault-state',
|
|
148
|
+
file: 'opencode-state.json',
|
|
149
|
+
waitMs: 15_000,
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
/**
|
|
153
|
+
* How long a starting process waits for the layout lock: 75 seconds. A move
|
|
154
|
+
* holding it may wait for each data lock in turn (35 s + 15 s + 15 s), and a
|
|
155
|
+
* crashed holder's lease ends after `LOCK_TTL_MS` (10 s).
|
|
156
|
+
*/
|
|
157
|
+
const LAYOUT_LOCK_WAIT_MS = DATA_LOCKS.reduce((sum, lock) => sum + lock.waitMs, 0) + LOCK_TTL_MS;
|
|
158
|
+
const LOCK_RETRY_MS = 50;
|
|
159
|
+
/** A wait for a lock longer than this is logged once, naming the lock. */
|
|
160
|
+
const LOCK_WAIT_NOTICE_MS = 1_000;
|
|
161
|
+
/** A marker read that fails for a reason other than absence is retried. */
|
|
162
|
+
const MARKER_READ_ATTEMPTS = 5;
|
|
163
|
+
const MARKER_READ_RETRY_MS = 50;
|
|
164
|
+
// The same budget common-auth's `replaceWindowsFile` gives a rename that
|
|
165
|
+
// Windows refuses because another process has the file open.
|
|
166
|
+
const SHARING_RETRY_DELAYS_MS = [0, 10, 20, 40, 80, 100];
|
|
167
|
+
const SHARING_RETRY_BUDGET_MS = 500;
|
|
168
|
+
/** The message logged once by a process that keeps the legacy layout. */
|
|
169
|
+
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.";
|
|
170
|
+
/**
|
|
171
|
+
* Plugin start stops when it cannot settle a safe layout: it could not take
|
|
172
|
+
* the layout lock or write its heartbeat (so other processes could not see
|
|
173
|
+
* which files it uses), could not trust the marker, found conflicting vault
|
|
174
|
+
* files, or found a state-home folder owned by another config folder.
|
|
175
|
+
*/
|
|
176
|
+
export class RuntimeLayoutRefusal extends Error {
|
|
177
|
+
stateDir;
|
|
178
|
+
errorClass;
|
|
179
|
+
constructor(stateDir, errorClass, message = refusalMessage(stateDir, errorClass)) {
|
|
180
|
+
super(message);
|
|
181
|
+
this.stateDir = stateDir;
|
|
182
|
+
this.errorClass = errorClass;
|
|
183
|
+
this.name = 'RuntimeLayoutRefusal';
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function refusalMessage(stateDir, errorClass) {
|
|
187
|
+
// The private-files check refused the folder itself: it is a link, or
|
|
188
|
+
// others can reach it. Creating or writing it is not the problem.
|
|
189
|
+
if (errorClass === 'ERR_PRIVATE_FILES_SECURITY')
|
|
190
|
+
return `antigravity-auth cannot start: ${stateDir} is not a folder only your user can access, or it is a link, so the plugin will not use it (${errorClass}). Replace it with a plain folder of your own, then restart OpenCode.`;
|
|
191
|
+
return `antigravity-auth cannot start: it could not settle where its vault files live in ${stateDir} (${errorClass}). Check that this folder can be created and written by your user, then restart OpenCode.`;
|
|
192
|
+
}
|
|
193
|
+
function foreignFolder(stateDir, identity) {
|
|
194
|
+
return new RuntimeLayoutRefusal(stateDir, 'foreign-folder', `antigravity-auth cannot start: ${stateDir} belongs to another OpenCode config folder, not ${identity}. It was left untouched. Move it aside, then restart OpenCode.`);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* What to do about a `layout.json` that cannot be trusted: check whether the
|
|
198
|
+
* old vault files are still in OpenCode's config folder; delete `layout.json`
|
|
199
|
+
* only if they are (the next start then decides again), and otherwise keep
|
|
200
|
+
* the runtime folder as it is.
|
|
201
|
+
*/
|
|
202
|
+
function markerRemedy(markerPath, oldVaultDir) {
|
|
203
|
+
return `Check that the old vault files are still in ${oldVaultDir}. If they are, delete ${markerPath}, then restart OpenCode; if they are not, keep ${dirname(markerPath)} as it is.`;
|
|
204
|
+
}
|
|
205
|
+
/** A `layout.json` that records a move from another config folder. */
|
|
206
|
+
function foreignMarker(markerPath, identity, oldVaultDir) {
|
|
207
|
+
return new RuntimeLayoutRefusal(dirname(markerPath), 'foreign-marker', `antigravity-auth cannot start: ${markerPath} belongs to another OpenCode config folder, not ${identity}, so it cannot tell whether this folder's vault files were moved. ${markerRemedy(markerPath, oldVaultDir)}`);
|
|
208
|
+
}
|
|
209
|
+
function credentialConflict(stateDir, file) {
|
|
210
|
+
return new RuntimeLayoutRefusal(stateDir, 'credential-conflict', `antigravity-auth cannot start: vault files conflict between ${file.old} and ${file.next}. Keep the newer one, delete the other, then restart OpenCode.`);
|
|
211
|
+
}
|
|
212
|
+
// Per-process bookkeeping, on the global object because OpenCode 2 may load
|
|
213
|
+
// more than one copy of this bundle into one process.
|
|
214
|
+
const stateKey = Symbol.for('@cortexkit/antigravity-auth/runtime-layout-runs');
|
|
215
|
+
const globalState = globalThis;
|
|
216
|
+
const processState = globalState[stateKey] ?? {
|
|
217
|
+
runs: new Map(),
|
|
218
|
+
logged: new Set(),
|
|
219
|
+
};
|
|
220
|
+
globalState[stateKey] = processState;
|
|
221
|
+
/** Test seam: forget this process's runs and log lines. */
|
|
222
|
+
export function __resetRuntimeLayoutRunsForTest() {
|
|
223
|
+
processState.runs.clear();
|
|
224
|
+
processState.logged.clear();
|
|
225
|
+
}
|
|
226
|
+
function logOnce(key, write) {
|
|
227
|
+
if (processState.logged.has(key))
|
|
228
|
+
return;
|
|
229
|
+
processState.logged.add(key);
|
|
230
|
+
write();
|
|
231
|
+
}
|
|
232
|
+
function errorClass(error) {
|
|
233
|
+
if (error instanceof RuntimeLayoutRefusal)
|
|
234
|
+
return error.errorClass;
|
|
235
|
+
const code = error?.code;
|
|
236
|
+
if (typeof code === 'string' && code)
|
|
237
|
+
return code;
|
|
238
|
+
return error instanceof Error ? error.name : typeof error;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* The name of the file an error is about, without its folder, and the
|
|
242
|
+
* private-file stage when there is one, for a log line.
|
|
243
|
+
*/
|
|
244
|
+
function errorFile(error) {
|
|
245
|
+
const { path, stage } = (error ?? {});
|
|
246
|
+
return {
|
|
247
|
+
...(typeof path === 'string' && path ? { file: basename(path) } : {}),
|
|
248
|
+
...(typeof stage === 'string' && stage ? { stage } : {}),
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
class LayoutMoveError extends Error {
|
|
252
|
+
code;
|
|
253
|
+
constructor(code, message) {
|
|
254
|
+
super(message);
|
|
255
|
+
this.code = code;
|
|
256
|
+
this.name = 'LayoutMoveError';
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* An old vault file identical to its new copy that could not be removed, or
|
|
261
|
+
* that Windows kept open so it could not be compared. Not a refusal: the
|
|
262
|
+
* start goes on in the state home, and the marker is left without
|
|
263
|
+
* `oldFilesRemoved` so the next start removes it. `path` is the first such
|
|
264
|
+
* file, for the log line.
|
|
265
|
+
*/
|
|
266
|
+
class OldVaultFileKeptError extends LayoutMoveError {
|
|
267
|
+
path;
|
|
268
|
+
constructor(path) {
|
|
269
|
+
super('old-files-kept', `${path} could not be removed`);
|
|
270
|
+
this.path = path;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/** The one warning for old files a move or a later start failed to remove. */
|
|
274
|
+
function logCleanupFailure(location, log, step, error) {
|
|
275
|
+
logOnce(`${location.key}\0cleanup`, () => log.warn("antigravity-auth could not finish removing its old vault files from OpenCode's config folder; it tries again at the next start", {
|
|
276
|
+
configDir: location.configDir,
|
|
277
|
+
step,
|
|
278
|
+
errorClass: errorClass(error),
|
|
279
|
+
...errorFile(error),
|
|
280
|
+
}));
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Decide, once per process, where this config folder's vault files live,
|
|
284
|
+
* moving them first when that is safe. Call at plugin start before anything
|
|
285
|
+
* resolves a vault path. Resolves to the layout `getVaultDir()` in
|
|
286
|
+
* `runtime-paths.ts` now uses; rejects with `RuntimeLayoutRefusal` when the
|
|
287
|
+
* plugin must not start.
|
|
288
|
+
*/
|
|
289
|
+
export function prepareRuntimeLayout(options) {
|
|
290
|
+
const location = runtimeLayoutLocation();
|
|
291
|
+
const decided = decidedRuntimeLayout(location);
|
|
292
|
+
if (decided)
|
|
293
|
+
return Promise.resolve(decided);
|
|
294
|
+
const runKey = `${location.stateHomeDir}\0${location.key}`;
|
|
295
|
+
let run = processState.runs.get(runKey);
|
|
296
|
+
if (!run) {
|
|
297
|
+
run = decide(location, options)
|
|
298
|
+
.catch((error) => {
|
|
299
|
+
// Anything not handled on the way stops the start the same way.
|
|
300
|
+
if (error instanceof RuntimeLayoutRefusal)
|
|
301
|
+
throw error;
|
|
302
|
+
throw new RuntimeLayoutRefusal(location.stateHomeDir, errorClass(error));
|
|
303
|
+
})
|
|
304
|
+
.finally(() => {
|
|
305
|
+
processState.runs.delete(runKey);
|
|
306
|
+
});
|
|
307
|
+
processState.runs.set(runKey, run);
|
|
308
|
+
}
|
|
309
|
+
return run;
|
|
310
|
+
}
|
|
311
|
+
async function decide(location, options) {
|
|
312
|
+
const log = options.log ?? createLogger('runtime-layout');
|
|
313
|
+
const stateDir = location.stateHomeDir;
|
|
314
|
+
const markerPath = join(stateDir, RUNTIME_LAYOUT_MARKER_FILE);
|
|
315
|
+
const files = layoutFiles(location, options.accountFile);
|
|
316
|
+
const fence = options.fence ?? defaultFence(options.version);
|
|
317
|
+
const stateHome = async () => {
|
|
318
|
+
await recordProcessLayout({
|
|
319
|
+
key: location.key,
|
|
320
|
+
layout: 'state-home',
|
|
321
|
+
version: options.version,
|
|
322
|
+
});
|
|
323
|
+
setRuntimeLayout(location, 'state-home');
|
|
324
|
+
return 'state-home';
|
|
325
|
+
};
|
|
326
|
+
// Read-only first: whose folder this is, and whether the move is done.
|
|
327
|
+
let marker = await readMarker(markerPath, stateDir, files.vault.old);
|
|
328
|
+
await assertOwnFolder(location, marker, files);
|
|
329
|
+
if (marker?.oldFilesRemoved) {
|
|
330
|
+
await refuseReappearedFiles(location, files);
|
|
331
|
+
// A move that stopped right after its last marker write left its
|
|
332
|
+
// `layout-moving.json`; it is removed below, under the layout lock.
|
|
333
|
+
const manifestLeft = await exists(join(stateDir, RUNTIME_LAYOUT_MANIFEST_FILE)).catch(() => true);
|
|
334
|
+
if (!manifestLeft)
|
|
335
|
+
return stateHome();
|
|
336
|
+
}
|
|
337
|
+
// A state-home folder that is OpenCode's config folder itself (a link or a
|
|
338
|
+
// mount) gets no chmod or note of its own; the move then stays legacy.
|
|
339
|
+
const aliased = await samePhysical(location.configDir, stateDir);
|
|
340
|
+
let layoutLock;
|
|
341
|
+
try {
|
|
342
|
+
if (!aliased)
|
|
343
|
+
await ensureOwnerOnlyDir(stateDir);
|
|
344
|
+
layoutLock = await takeLock(RUNTIME_LAYOUT_LOCK_NAME, markerPath, options.layoutLockWaitMs ?? LAYOUT_LOCK_WAIT_MS, log);
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
// Only an existing marker settles the layout without the lock.
|
|
348
|
+
marker = await readMarker(markerPath, stateDir, files.vault.old);
|
|
349
|
+
if (marker) {
|
|
350
|
+
if (marker.oldFilesRemoved)
|
|
351
|
+
await refuseReappearedFiles(location, files);
|
|
352
|
+
return stateHome();
|
|
353
|
+
}
|
|
354
|
+
if (error instanceof LayoutMoveError && error.code === 'lock-timeout')
|
|
355
|
+
throw new RuntimeLayoutRefusal(stateDir, 'lock-timeout', `antigravity-auth cannot start: ${error.message} Try again, or restart OpenCode, once that OpenCode has finished the move.`);
|
|
356
|
+
throw new RuntimeLayoutRefusal(stateDir, errorClass(error));
|
|
357
|
+
}
|
|
358
|
+
const lock = layoutLock;
|
|
359
|
+
const legacy = async () => {
|
|
360
|
+
try {
|
|
361
|
+
await lock.assertOwned();
|
|
362
|
+
await recordProcessLayout({
|
|
363
|
+
key: location.key,
|
|
364
|
+
layout: 'legacy',
|
|
365
|
+
version: options.version,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
catch (error) {
|
|
369
|
+
throw new RuntimeLayoutRefusal(stateDir, errorClass(error));
|
|
370
|
+
}
|
|
371
|
+
setRuntimeLayout(location, 'legacy');
|
|
372
|
+
return 'legacy';
|
|
373
|
+
};
|
|
374
|
+
try {
|
|
375
|
+
marker = await readMarker(markerPath, stateDir, files.vault.old);
|
|
376
|
+
await assertOwnFolder(location, marker, files);
|
|
377
|
+
if (!aliased)
|
|
378
|
+
await writeOwnerNote(location);
|
|
379
|
+
if (marker) {
|
|
380
|
+
if (!marker.oldFilesRemoved && !aliased)
|
|
381
|
+
await finishOldFiles(location, files, markerPath, marker, lock, {
|
|
382
|
+
fence,
|
|
383
|
+
log,
|
|
384
|
+
steps: options.steps,
|
|
385
|
+
oldSignatureCache: options.oldSignatureCacheFile ?? legacySignatureCacheFile(),
|
|
386
|
+
});
|
|
387
|
+
else if (marker.oldFilesRemoved) {
|
|
388
|
+
await refuseReappearedFiles(location, files);
|
|
389
|
+
await removeLeftoverManifest(location, lock, options.steps, log);
|
|
390
|
+
}
|
|
391
|
+
return await stateHome();
|
|
392
|
+
}
|
|
393
|
+
if (aliased) {
|
|
394
|
+
logOnce(`${location.key}\0aliased`, () => log.warn("antigravity-auth keeps its vault files in OpenCode's config folder: its state folder is that same folder", { configDir: location.configDir, stateDir }));
|
|
395
|
+
return await legacy();
|
|
396
|
+
}
|
|
397
|
+
const gate = await fence();
|
|
398
|
+
if (!gate.open) {
|
|
399
|
+
logDeferral(location, gate.blockers, log);
|
|
400
|
+
return await legacy();
|
|
401
|
+
}
|
|
402
|
+
let markerStarted = false;
|
|
403
|
+
try {
|
|
404
|
+
const outcome = await moveFiles(location, files, markerPath, lock, {
|
|
405
|
+
fence,
|
|
406
|
+
log,
|
|
407
|
+
steps: options.steps,
|
|
408
|
+
destinationPrivate: options.destinationPrivate ?? defaultDestinationPrivate,
|
|
409
|
+
oldSignatureCache: options.oldSignatureCacheFile ?? legacySignatureCacheFile(),
|
|
410
|
+
onMarker: () => {
|
|
411
|
+
markerStarted = true;
|
|
412
|
+
},
|
|
413
|
+
});
|
|
414
|
+
if (outcome.kind === 'deferred') {
|
|
415
|
+
logDeferral(location, outcome.blockers, log);
|
|
416
|
+
return await legacy();
|
|
417
|
+
}
|
|
418
|
+
if (outcome.kind === 'refused') {
|
|
419
|
+
logOnce(`${location.key}\0refused`, () => log.warn(outcome.reason === 'destination-not-private'
|
|
420
|
+
? "antigravity-auth keeps its vault files in OpenCode's config folder: the vault folder in its state folder already exists and is not private to your user. It was left unchanged; remove it if it holds nothing you need, then restart OpenCode."
|
|
421
|
+
: "antigravity-auth keeps its vault files in OpenCode's config folder: it will not move them", { reason: outcome.reason, paths: outcome.paths, stateDir }));
|
|
422
|
+
return await legacy();
|
|
423
|
+
}
|
|
424
|
+
if (outcome.moved)
|
|
425
|
+
log.info("antigravity-auth moved its vault files out of OpenCode's config folder", { from: files.vault.old, to: files.vault.next });
|
|
426
|
+
return await stateHome();
|
|
427
|
+
}
|
|
428
|
+
catch (error) {
|
|
429
|
+
if (error instanceof RuntimeLayoutRefusal)
|
|
430
|
+
throw error;
|
|
431
|
+
// Once the marker write began, the marker on disk decides.
|
|
432
|
+
if (markerStarted &&
|
|
433
|
+
(await readMarker(markerPath, stateDir, files.vault.old)))
|
|
434
|
+
return await stateHome();
|
|
435
|
+
logOnce(`${location.key}\0failed`, () => log.warn("antigravity-auth's vault files stay in OpenCode's config folder: moving them failed; the move is tried again at the next start", {
|
|
436
|
+
configDir: location.configDir,
|
|
437
|
+
errorClass: errorClass(error),
|
|
438
|
+
...errorFile(error),
|
|
439
|
+
}));
|
|
440
|
+
return await legacy();
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
finally {
|
|
444
|
+
await lock.release().catch(() => { });
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function defaultFence(version) {
|
|
448
|
+
return () => versionFenceOpen({ currentVersion: version, layoutMove: true });
|
|
449
|
+
}
|
|
450
|
+
function logDeferral(location, blockers, log) {
|
|
451
|
+
logOnce(`${location.key}\0deferred`, () => log.info(RUNTIME_LAYOUT_DEFERRED_MESSAGE, {
|
|
452
|
+
configDir: location.configDir,
|
|
453
|
+
blockers,
|
|
454
|
+
}));
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* On Windows, an existing destination vault folder must already be private
|
|
458
|
+
* to this user: the enrollment token writer refuses to rewrite a token in a
|
|
459
|
+
* folder that is not, and this module never changes the DACL of a folder it
|
|
460
|
+
* did not create. A missing folder is created private by the writer.
|
|
461
|
+
*/
|
|
462
|
+
async function defaultDestinationPrivate(dir) {
|
|
463
|
+
if (process.platform !== 'win32')
|
|
464
|
+
return true;
|
|
465
|
+
if (!(await exists(dir)))
|
|
466
|
+
return true;
|
|
467
|
+
try {
|
|
468
|
+
const { verifyPrivatePath } = await import('@cortexkit/private-files');
|
|
469
|
+
await verifyPrivatePath(dir, 'directory');
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
catch {
|
|
473
|
+
return false;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Copy every old vault file, then write the marker, then remove the old
|
|
478
|
+
* files. Holds the data locks throughout; the caller holds the layout lock.
|
|
479
|
+
*/
|
|
480
|
+
async function moveFiles(location, files, markerPath, layoutLock, input) {
|
|
481
|
+
const stateDir = location.stateHomeDir;
|
|
482
|
+
const locks = await takeDataLocks(files, input.log);
|
|
483
|
+
let oldVaultEmptied = false;
|
|
484
|
+
try {
|
|
485
|
+
// Heartbeats read again under every lock, right before the copy: a
|
|
486
|
+
// process that started since the first check and kept the legacy layout
|
|
487
|
+
// has recorded it by now.
|
|
488
|
+
const gate = await input.fence();
|
|
489
|
+
if (!gate.open)
|
|
490
|
+
return { kind: 'deferred', blockers: gate.blockers };
|
|
491
|
+
const aliases = await physicalAliases(location, files);
|
|
492
|
+
if (aliases.length > 0)
|
|
493
|
+
return { kind: 'refused', reason: 'same-physical-file', paths: aliases };
|
|
494
|
+
// A vault file in the state-home vault folder that no unfinished move of
|
|
495
|
+
// ours listed was put there by someone else: leave it.
|
|
496
|
+
const listed = await readManifest(stateDir);
|
|
497
|
+
const foreign = (await destinationFiles(files)).filter((path) => !listed.has(path));
|
|
498
|
+
if (foreign.length > 0)
|
|
499
|
+
return { kind: 'refused', reason: 'destination-in-use', paths: foreign };
|
|
500
|
+
if (!(await input.destinationPrivate(files.vault.next)))
|
|
501
|
+
return {
|
|
502
|
+
kind: 'refused',
|
|
503
|
+
reason: 'destination-not-private',
|
|
504
|
+
paths: [files.vault.next],
|
|
505
|
+
};
|
|
506
|
+
const present = await presentOldFiles(files);
|
|
507
|
+
await layoutLock.assertOwned();
|
|
508
|
+
await writeManifest(stateDir, [
|
|
509
|
+
...new Set([...listed, ...present.map((file) => file.next)]),
|
|
510
|
+
]);
|
|
511
|
+
await input.steps?.afterManifest?.();
|
|
512
|
+
// Copies an earlier, interrupted move left whose source has since gone.
|
|
513
|
+
const wanted = new Set(present.map((file) => file.next));
|
|
514
|
+
for (const path of listed) {
|
|
515
|
+
if (wanted.has(path))
|
|
516
|
+
continue;
|
|
517
|
+
await layoutLock.assertOwned();
|
|
518
|
+
await unlink(path).catch((error) => {
|
|
519
|
+
if (error.code !== 'ENOENT')
|
|
520
|
+
throw error;
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
for (const file of present) {
|
|
524
|
+
await layoutLock.assertOwned();
|
|
525
|
+
await copyVerified(file);
|
|
526
|
+
}
|
|
527
|
+
await input.steps?.afterCopy?.();
|
|
528
|
+
await layoutLock.assertOwned();
|
|
529
|
+
for (const lock of locks)
|
|
530
|
+
await lock.assertOwned();
|
|
531
|
+
const keptVaultState = await keepableVaultState(files, present);
|
|
532
|
+
const marker = {
|
|
533
|
+
version: 1,
|
|
534
|
+
oldDir: location.key,
|
|
535
|
+
...(keptVaultState ? { keptVaultState } : {}),
|
|
536
|
+
};
|
|
537
|
+
input.onMarker();
|
|
538
|
+
await writeMarker(markerPath, marker);
|
|
539
|
+
await input.steps?.afterMarker?.();
|
|
540
|
+
await layoutLock.assertOwned();
|
|
541
|
+
try {
|
|
542
|
+
await removeOldCopies(location, files, present, layoutLock, {
|
|
543
|
+
log: input.log,
|
|
544
|
+
keptVaultState,
|
|
545
|
+
steps: input.steps,
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
catch (error) {
|
|
549
|
+
if (!(error instanceof OldVaultFileKeptError))
|
|
550
|
+
throw error;
|
|
551
|
+
// The copies are verified and the marker is written, so this process
|
|
552
|
+
// runs on the new files; the marker stays without `oldFilesRemoved` and
|
|
553
|
+
// the next start removes what is left.
|
|
554
|
+
logCleanupFailure(location, input.log, 'remove-old-copies', error);
|
|
555
|
+
return { kind: 'moved', moved: present.length > 0 };
|
|
556
|
+
}
|
|
557
|
+
oldVaultEmptied = true;
|
|
558
|
+
await removeOldSignatureCache(location, input.oldSignatureCache, layoutLock, input.steps, input.log);
|
|
559
|
+
await layoutLock.assertOwned();
|
|
560
|
+
await writeMarker(markerPath, { ...marker, oldFilesRemoved: true });
|
|
561
|
+
await removeManifestOwned(stateDir, layoutLock, input.steps);
|
|
562
|
+
return { kind: 'moved', moved: present.length > 0 };
|
|
563
|
+
}
|
|
564
|
+
finally {
|
|
565
|
+
for (const lock of locks.reverse())
|
|
566
|
+
await lock.release().catch(() => { });
|
|
567
|
+
// The vault locks are released now, so their lock files are gone and
|
|
568
|
+
// the emptied old vault folder can go; rmdir leaves a folder that still
|
|
569
|
+
// holds anything.
|
|
570
|
+
if (oldVaultEmptied)
|
|
571
|
+
await removeOldVaultFolder(files, layoutLock, input.steps);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* A start after a crash that came after the marker: under the same locks as
|
|
576
|
+
* the move, remove identical old copies and refuse differing ones. Waits,
|
|
577
|
+
* like the move, while a process that could still use the old files is
|
|
578
|
+
* alive, and reads the heartbeats again once it holds the data locks.
|
|
579
|
+
*/
|
|
580
|
+
async function finishOldFiles(location, files, markerPath, marker, layoutLock, input) {
|
|
581
|
+
let locks = [];
|
|
582
|
+
let done = false;
|
|
583
|
+
// Which step failed, for the warning below.
|
|
584
|
+
let step = 'fence';
|
|
585
|
+
try {
|
|
586
|
+
// Read first so a closed fence does not wait for the data locks, and
|
|
587
|
+
// again under them, as the move does: a process that started meanwhile
|
|
588
|
+
// and kept the legacy layout has recorded it by then.
|
|
589
|
+
if (!(await input.fence()).open)
|
|
590
|
+
return;
|
|
591
|
+
step = 'data-locks';
|
|
592
|
+
locks = await takeDataLocks(files, input.log);
|
|
593
|
+
step = 'fence';
|
|
594
|
+
if (!(await input.fence()).open)
|
|
595
|
+
return;
|
|
596
|
+
step = 'same-file-check';
|
|
597
|
+
const aliases = await physicalAliases(location, files);
|
|
598
|
+
if (aliases.length > 0) {
|
|
599
|
+
logOnce(`${location.key}\0cleanup-aliased`, () => input.log.warn("antigravity-auth leaves its old vault files in OpenCode's config folder: they are the same files as its new ones", { paths: aliases }));
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
step = 'remove-old-copies';
|
|
603
|
+
await layoutLock.assertOwned();
|
|
604
|
+
await removeOldCopies(location, files, await presentOldFiles(files), layoutLock, {
|
|
605
|
+
log: input.log,
|
|
606
|
+
keptVaultState: marker.keptVaultState,
|
|
607
|
+
steps: input.steps,
|
|
608
|
+
});
|
|
609
|
+
done = true;
|
|
610
|
+
step = 'signature-cache';
|
|
611
|
+
await removeOldSignatureCache(location, input.oldSignatureCache, layoutLock, input.steps, input.log);
|
|
612
|
+
step = 'marker';
|
|
613
|
+
await layoutLock.assertOwned();
|
|
614
|
+
await writeMarker(markerPath, { ...marker, oldFilesRemoved: true });
|
|
615
|
+
step = 'manifest';
|
|
616
|
+
await removeManifestOwned(location.stateHomeDir, layoutLock, input.steps);
|
|
617
|
+
}
|
|
618
|
+
catch (error) {
|
|
619
|
+
if (error instanceof RuntimeLayoutRefusal)
|
|
620
|
+
throw error;
|
|
621
|
+
logCleanupFailure(location, input.log, step, error);
|
|
622
|
+
}
|
|
623
|
+
finally {
|
|
624
|
+
for (const lock of locks.reverse())
|
|
625
|
+
await lock.release().catch(() => { });
|
|
626
|
+
if (done)
|
|
627
|
+
await removeOldVaultFolder(files, layoutLock, input.steps);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Remove the `layout-moving.json` a move left behind when it stopped after
|
|
632
|
+
* marking its old files dealt with; the caller holds the layout lock. A
|
|
633
|
+
* failure is logged and retried at the next start.
|
|
634
|
+
*/
|
|
635
|
+
async function removeLeftoverManifest(location, layoutLock, steps, log) {
|
|
636
|
+
try {
|
|
637
|
+
await removeManifestOwned(location.stateHomeDir, layoutLock, steps);
|
|
638
|
+
}
|
|
639
|
+
catch (error) {
|
|
640
|
+
logOnce(`${location.key}\0manifest`, () => log.warn(`antigravity-auth could not remove ${RUNTIME_LAYOUT_MANIFEST_FILE} from its state folder; it tries again at the next start`, { stateDir: location.stateHomeDir, errorClass: errorClass(error) }));
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
/** Remove the move's manifest once the layout lock is confirmed still held. */
|
|
644
|
+
async function removeManifestOwned(stateDir, layoutLock, steps) {
|
|
645
|
+
const path = join(stateDir, RUNTIME_LAYOUT_MANIFEST_FILE);
|
|
646
|
+
if (!(await exists(path)))
|
|
647
|
+
return;
|
|
648
|
+
await beforeRemove(path, layoutLock, steps);
|
|
649
|
+
await removeManifest(stateDir);
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Remove the old vault folder if it is empty, once the layout lock is
|
|
653
|
+
* confirmed still held. Best effort: rmdir leaves a folder that still holds
|
|
654
|
+
* anything, such as the enrollment state file a vault-mode move keeps or
|
|
655
|
+
* Pi's files.
|
|
656
|
+
*/
|
|
657
|
+
async function removeOldVaultFolder(files, layoutLock, steps) {
|
|
658
|
+
try {
|
|
659
|
+
await beforeRemove(files.vault.old, layoutLock, steps);
|
|
660
|
+
await rmdir(files.vault.old);
|
|
661
|
+
}
|
|
662
|
+
catch { }
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Remove the signature cache older versions kept in OpenCode's config folder
|
|
666
|
+
* (`legacySignatureCacheFile`), once: the move calls this right before it
|
|
667
|
+
* records its old files as dealt with, and never again after. It is a cache
|
|
668
|
+
* that rebuilds itself, so it is deleted, never copied or compared. Best
|
|
669
|
+
* effort: a failure is logged once and the move goes on.
|
|
670
|
+
*/
|
|
671
|
+
async function removeOldSignatureCache(location, path, layoutLock, steps, log) {
|
|
672
|
+
try {
|
|
673
|
+
if (!(await exists(path)))
|
|
674
|
+
return;
|
|
675
|
+
await beforeRemove(path, layoutLock, steps);
|
|
676
|
+
await unlink(path);
|
|
677
|
+
}
|
|
678
|
+
catch (error) {
|
|
679
|
+
if (error?.code === 'ENOENT')
|
|
680
|
+
return;
|
|
681
|
+
logOnce(`${location.key}\0signature-cache`, () => log.warn("antigravity-auth could not remove its old signature cache from OpenCode's config folder; it is no longer used and can be deleted", { path, errorClass: errorClass(error) }));
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
/** The test seam, then the lease check that must come right before a removal. */
|
|
685
|
+
async function beforeRemove(path, layoutLock, steps) {
|
|
686
|
+
await steps?.beforeRemove?.(path);
|
|
687
|
+
await layoutLock.assertOwned();
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* The vault's own locks at the old vault folder (`DATA_LOCKS`). Taken only
|
|
691
|
+
* when that folder exists, because taking a lock creates missing parent
|
|
692
|
+
* folders; taken whether or not each file exists yet, so a writer about to
|
|
693
|
+
* create one waits too.
|
|
694
|
+
*/
|
|
695
|
+
async function takeDataLocks(files, log) {
|
|
696
|
+
const locks = [];
|
|
697
|
+
try {
|
|
698
|
+
if (await exists(files.vault.old))
|
|
699
|
+
for (const lock of DATA_LOCKS)
|
|
700
|
+
locks.push(await takeLock(lock.name, join(files.vault.old, lock.file), lock.waitMs, log));
|
|
701
|
+
return locks;
|
|
702
|
+
}
|
|
703
|
+
catch (error) {
|
|
704
|
+
for (const lock of locks.reverse())
|
|
705
|
+
await lock.release().catch(() => { });
|
|
706
|
+
throw error;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
function layoutFiles(location, accountFile) {
|
|
710
|
+
return {
|
|
711
|
+
vault: {
|
|
712
|
+
old: legacyVaultDir(accountFile ?? legacyAccountFile(location.configDir)),
|
|
713
|
+
next: stateHomeVaultDir(location),
|
|
714
|
+
},
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
const SHA256_HEX = /^[0-9a-f]{64}$/;
|
|
718
|
+
function sha256(bytes) {
|
|
719
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
720
|
+
}
|
|
721
|
+
/** The vault enrollment state file, in both layouts. */
|
|
722
|
+
function vaultStateFile(files) {
|
|
723
|
+
return {
|
|
724
|
+
old: join(files.vault.old, VAULT_ENROLLMENT_STATE_FILE),
|
|
725
|
+
next: join(files.vault.next, VAULT_ENROLLMENT_STATE_FILE),
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* The SHA-256 of the enrollment state file this move copied, when that state
|
|
730
|
+
* keeps a host in vault mode without its token: approved, or blocked because
|
|
731
|
+
* the token is missing. common-auth's `enrollmentAuthority` reads either one
|
|
732
|
+
* as 'vault' when no token is next to it, so leaving it in the old vault
|
|
733
|
+
* folder keeps an older version in vault mode. Any other state (pending,
|
|
734
|
+
* denied, unreadable) protects nothing, and the old copy is removed as usual.
|
|
735
|
+
*/
|
|
736
|
+
async function keepableVaultState(files, present) {
|
|
737
|
+
const state = vaultStateFile(files);
|
|
738
|
+
if (!present.some((file) => file.old === state.old))
|
|
739
|
+
return undefined;
|
|
740
|
+
// The copy, verified byte for byte against the old file.
|
|
741
|
+
const bytes = await readFile(state.next);
|
|
742
|
+
let value;
|
|
743
|
+
try {
|
|
744
|
+
value = JSON.parse(bytes.toString('utf8'));
|
|
745
|
+
}
|
|
746
|
+
catch {
|
|
747
|
+
return undefined;
|
|
748
|
+
}
|
|
749
|
+
const vaultMode = value?.phase === 'approved' ||
|
|
750
|
+
(value?.phase === 'blocked' && value.errorCode === 'missing_token');
|
|
751
|
+
return vaultMode ? sha256(bytes) : undefined;
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* True when the enrollment state file of the old vault folder is the leftover
|
|
755
|
+
* a move may have left there, after an older version rewrote it.
|
|
756
|
+
*
|
|
757
|
+
* The file is re-read through common-auth's
|
|
758
|
+
* `readClaustrumEnrollmentStatus` with the enrollment name this plugin
|
|
759
|
+
* proposes, so it is judged the way every version of the plugin reads it. It
|
|
760
|
+
* counts as the expected leftover when the library reads it as vault mode
|
|
761
|
+
* (approved, or blocked with `missing_token`: what a reconcile writes when it
|
|
762
|
+
* finds the token gone), names this plugin's enrollment, and no token file
|
|
763
|
+
* sits beside it. A token back in the old folder means it is enrolled again,
|
|
764
|
+
* so which copy is current is unknown. Wrong name, pending, denied,
|
|
765
|
+
* unparseable or unsafe to read is a conflict, not the leftover.
|
|
766
|
+
*
|
|
767
|
+
* The library reports the name of the state for an approved file, and of
|
|
768
|
+
* this plugin's proposal for a state that has none (an approved token file
|
|
769
|
+
* alone); both are this plugin's when the comparison passes.
|
|
770
|
+
*/
|
|
771
|
+
async function isVaultShapedKeptState(files) {
|
|
772
|
+
const vault = files.vault.old;
|
|
773
|
+
const present = await vaultFiles(vault);
|
|
774
|
+
// A token file counts even when the library would refuse to read it: the
|
|
775
|
+
// old folder is enrolled again either way.
|
|
776
|
+
if (present.includes(VAULT_ENROLLMENT_TOKEN_FILE))
|
|
777
|
+
return false;
|
|
778
|
+
const claustrum = await loadCommonAuthClaustrum();
|
|
779
|
+
let status;
|
|
780
|
+
try {
|
|
781
|
+
status = await claustrum.readClaustrumEnrollmentStatus(claustrum.hostEnrollmentPaths({
|
|
782
|
+
stateDir: vault,
|
|
783
|
+
host: OPENCODE_VAULT_HOST,
|
|
784
|
+
}), OPENCODE_VAULT_ENROLLMENT_NAME);
|
|
785
|
+
}
|
|
786
|
+
catch {
|
|
787
|
+
// Unreadable, unsafe or unparseable: not the leftover this rule accepts.
|
|
788
|
+
return false;
|
|
789
|
+
}
|
|
790
|
+
const vaultShape = status.state === 'approved' ||
|
|
791
|
+
(status.state === 'blocked' && status.code === 'missing_token');
|
|
792
|
+
const proposedName = 'proposedName' in status ? status.proposedName : undefined;
|
|
793
|
+
return vaultShape && proposedName === OPENCODE_VAULT_ENROLLMENT_NAME;
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* After Disconnect, remove the enrollment state file a move left in the old
|
|
797
|
+
* vault folder, so an older version no longer reads this host as enrolled.
|
|
798
|
+
* Acts only for this config folder's state-home vault folder (`vaultDir`), and
|
|
799
|
+
* only on a vault-shaped leftover of this plugin's enrollment
|
|
800
|
+
* (`isVaultShapedKeptState`); any other file there stays. The emptied old
|
|
801
|
+
* vault folder goes too. `accountFile` is the one the move was given
|
|
802
|
+
* (`RuntimeLayoutOptions.accountFile`).
|
|
803
|
+
*/
|
|
804
|
+
export async function removeMovedVaultStateLeftover(vaultDir, accountFile) {
|
|
805
|
+
const location = runtimeLayoutLocation();
|
|
806
|
+
const files = layoutFiles(location, accountFile);
|
|
807
|
+
if (resolve(vaultDir) !== resolve(files.vault.next))
|
|
808
|
+
return;
|
|
809
|
+
const stateDir = location.stateHomeDir;
|
|
810
|
+
const marker = await readMarker(join(stateDir, RUNTIME_LAYOUT_MARKER_FILE), stateDir, files.vault.old);
|
|
811
|
+
// The marker must still name this config folder's finished move; its
|
|
812
|
+
// recorded hash is not what decides.
|
|
813
|
+
if (!marker?.oldDir || marker.oldDir !== location.key)
|
|
814
|
+
return;
|
|
815
|
+
const leftover = vaultStateFile(files).old;
|
|
816
|
+
if (!(await isVaultShapedKeptState(files)))
|
|
817
|
+
return;
|
|
818
|
+
await unlink(leftover).catch((error) => {
|
|
819
|
+
if (error.code !== 'ENOENT')
|
|
820
|
+
throw error;
|
|
821
|
+
});
|
|
822
|
+
await rmdir(files.vault.old).catch(() => { });
|
|
823
|
+
}
|
|
824
|
+
/** The allow-listed vault files that are regular files directly in a folder. */
|
|
825
|
+
async function vaultFiles(dir) {
|
|
826
|
+
let entries;
|
|
827
|
+
try {
|
|
828
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
829
|
+
}
|
|
830
|
+
catch (error) {
|
|
831
|
+
const code = error.code;
|
|
832
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
833
|
+
return [];
|
|
834
|
+
throw error;
|
|
835
|
+
}
|
|
836
|
+
const allowed = new Set(VAULT_FILE_NAMES);
|
|
837
|
+
return entries
|
|
838
|
+
.filter((entry) => entry.isFile() && allowed.has(entry.name))
|
|
839
|
+
.map((entry) => entry.name)
|
|
840
|
+
.sort((a, b) => VAULT_FILE_NAMES.indexOf(a) -
|
|
841
|
+
VAULT_FILE_NAMES.indexOf(b));
|
|
842
|
+
}
|
|
843
|
+
/** Every vault file that exists in the old layout, paired with its target. */
|
|
844
|
+
async function presentOldFiles(files) {
|
|
845
|
+
return (await vaultFiles(files.vault.old)).map((name) => ({
|
|
846
|
+
old: join(files.vault.old, name),
|
|
847
|
+
next: join(files.vault.next, name),
|
|
848
|
+
}));
|
|
849
|
+
}
|
|
850
|
+
/** Every vault file that exists in the state-home layout. */
|
|
851
|
+
async function destinationFiles(files) {
|
|
852
|
+
return (await vaultFiles(files.vault.next)).map((name) => join(files.vault.next, name));
|
|
853
|
+
}
|
|
854
|
+
/**
|
|
855
|
+
* Old paths that are the same physical file or folder as their new place (a
|
|
856
|
+
* link, a hard link or a mount): moving those would copy a file onto itself
|
|
857
|
+
* and then delete it.
|
|
858
|
+
*/
|
|
859
|
+
async function physicalAliases(location, files) {
|
|
860
|
+
const pairs = [
|
|
861
|
+
[location.configDir, location.stateHomeDir],
|
|
862
|
+
[files.vault.old, files.vault.next],
|
|
863
|
+
];
|
|
864
|
+
for (const name of VAULT_FILE_NAMES)
|
|
865
|
+
pairs.push([join(files.vault.old, name), join(files.vault.next, name)]);
|
|
866
|
+
const aliased = [];
|
|
867
|
+
for (const [old, next] of pairs)
|
|
868
|
+
if (await samePhysical(old, next))
|
|
869
|
+
aliased.push(old);
|
|
870
|
+
return aliased;
|
|
871
|
+
}
|
|
872
|
+
/** True when both paths exist and are one file or folder (device and inode). */
|
|
873
|
+
async function samePhysical(a, b) {
|
|
874
|
+
const id = async (path) => {
|
|
875
|
+
try {
|
|
876
|
+
const info = await stat(path, { bigint: true });
|
|
877
|
+
// A file system without inode numbers cannot prove anything here.
|
|
878
|
+
return info.ino === 0n ? undefined : `${info.dev}:${info.ino}`;
|
|
879
|
+
}
|
|
880
|
+
catch (error) {
|
|
881
|
+
const code = error.code;
|
|
882
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
883
|
+
return undefined;
|
|
884
|
+
throw error;
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
const left = await id(a);
|
|
888
|
+
return left !== undefined && left === (await id(b));
|
|
889
|
+
}
|
|
890
|
+
async function copyVerified(file) {
|
|
891
|
+
const bytes = await readFile(file.old);
|
|
892
|
+
const text = bytes.toString('utf8');
|
|
893
|
+
// The private writer takes text; a file that is not valid UTF-8 would not
|
|
894
|
+
// survive the round trip, so it is not copied at all.
|
|
895
|
+
if (!Buffer.from(text, 'utf8').equals(bytes))
|
|
896
|
+
throw new LayoutMoveError('not-utf8', `${file.old} is not UTF-8 text`);
|
|
897
|
+
await writePrivateText(file.next, text);
|
|
898
|
+
if (!(await readFile(file.next)).equals(bytes))
|
|
899
|
+
throw new LayoutMoveError('copy-mismatch', `${file.next} does not match ${file.old}`);
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* Remove each old file whose bytes equal its new copy, comparing again right
|
|
903
|
+
* before every unlink attempt. A differing old file, or one whose copy
|
|
904
|
+
* cannot be read for a reason other than Windows holding it open, refuses
|
|
905
|
+
* the start: it may hold the only current vault credential. The vault-shaped
|
|
906
|
+
* enrollment state file the move left there (`isVaultShapedKeptState`) stays
|
|
907
|
+
* on purpose. An identical file that cannot be removed, or one Windows keeps
|
|
908
|
+
* open so it cannot be compared, throws `OldVaultFileKeptError` once every
|
|
909
|
+
* other file was tried: the caller logs it once and leaves the marker without
|
|
910
|
+
* `oldFilesRemoved`, so the next start tries again. When the marker records a
|
|
911
|
+
* kept enrollment state (`keptVaultState`), an old state file whose bytes
|
|
912
|
+
* equal its state-home copy stays too, even when the leftover check rejects
|
|
913
|
+
* it. The caller holds the data locks.
|
|
914
|
+
*/
|
|
915
|
+
async function removeOldCopies(location, files, present, layoutLock, input) {
|
|
916
|
+
const kept = [];
|
|
917
|
+
const keptState = vaultStateFile(files).old;
|
|
918
|
+
// The enrollment state file comes last: whether it may stay depends on the
|
|
919
|
+
// old token being gone, and the token is one of the files removed here.
|
|
920
|
+
const ordered = [
|
|
921
|
+
...present.filter((file) => file.old !== keptState),
|
|
922
|
+
...present.filter((file) => file.old === keptState),
|
|
923
|
+
];
|
|
924
|
+
for (const file of ordered) {
|
|
925
|
+
await layoutLock.assertOwned();
|
|
926
|
+
if (file.old === keptState) {
|
|
927
|
+
if (await isVaultShapedKeptState(files)) {
|
|
928
|
+
// Kept on purpose, but only next to a copy that is still readable.
|
|
929
|
+
if (!(await readable(file.next)))
|
|
930
|
+
throw credentialConflict(location.stateHomeDir, file);
|
|
931
|
+
continue;
|
|
932
|
+
}
|
|
933
|
+
// An old vault file still in place (the token, say) means another try
|
|
934
|
+
// at the next start; the state file is judged then, beside whatever is
|
|
935
|
+
// left by then.
|
|
936
|
+
if (kept.length > 0)
|
|
937
|
+
continue;
|
|
938
|
+
// The move left a state file here on purpose. Whenever the leftover
|
|
939
|
+
// check above rejects it (a read that failed for a moment is enough),
|
|
940
|
+
// a copy identical to the state-home one must still stay: removing it
|
|
941
|
+
// would make an older version read this host as local. Differing bytes
|
|
942
|
+
// are judged below.
|
|
943
|
+
if (input.keptVaultState && (await sameBytes(file.old, file.next)))
|
|
944
|
+
continue;
|
|
945
|
+
}
|
|
946
|
+
if (!(await unlinkIfIdentical(file, location.stateHomeDir, () => beforeRemove(file.old, layoutLock, input.steps))))
|
|
947
|
+
kept.push(file.old);
|
|
948
|
+
}
|
|
949
|
+
const [first] = kept;
|
|
950
|
+
if (first !== undefined)
|
|
951
|
+
throw new OldVaultFileKeptError(first);
|
|
952
|
+
}
|
|
953
|
+
async function readable(path) {
|
|
954
|
+
try {
|
|
955
|
+
await readFile(path);
|
|
956
|
+
return true;
|
|
957
|
+
}
|
|
958
|
+
catch {
|
|
959
|
+
return false;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
/** True when both files can be read and hold the same bytes. */
|
|
963
|
+
async function sameBytes(a, b) {
|
|
964
|
+
try {
|
|
965
|
+
return (await readFile(a)).equals(await readFile(b));
|
|
966
|
+
}
|
|
967
|
+
catch {
|
|
968
|
+
return false;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* On Windows, a file another process has open (an antivirus scan, an indexer,
|
|
973
|
+
* a writer that has not closed it yet) can refuse to be read or removed for a
|
|
974
|
+
* moment. These codes do not say whether the file exists or what it holds.
|
|
975
|
+
*/
|
|
976
|
+
function isWindowsSharingError(error) {
|
|
977
|
+
const code = error?.code;
|
|
978
|
+
return (process.platform === 'win32' &&
|
|
979
|
+
(code === 'EBUSY' || code === 'EPERM' || code === 'EACCES'));
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Unlink `file.old` only while its bytes equal `file.next`, retrying on
|
|
983
|
+
* Windows while another process has either file open. `guard` runs right
|
|
984
|
+
* before every unlink attempt and throws when the removal must not happen.
|
|
985
|
+
* True when the file is gone. False when it stays because Windows kept it
|
|
986
|
+
* open past the retry budget, whether for the comparison or for the removal,
|
|
987
|
+
* or because the removal failed for another reason. Differing files, or
|
|
988
|
+
* files unreadable for any other reason, throw `credentialConflict`.
|
|
989
|
+
*/
|
|
990
|
+
async function unlinkIfIdentical(file, stateDir, guard) {
|
|
991
|
+
const started = performance.now();
|
|
992
|
+
for (const delay of SHARING_RETRY_DELAYS_MS) {
|
|
993
|
+
if (delay > 0)
|
|
994
|
+
await new Promise((done) => setTimeout(done, delay));
|
|
995
|
+
let same;
|
|
996
|
+
try {
|
|
997
|
+
same = (await readFile(file.old)).equals(await readFile(file.next));
|
|
998
|
+
}
|
|
999
|
+
catch (error) {
|
|
1000
|
+
if (error.code === 'ENOENT' &&
|
|
1001
|
+
!(await exists(file.old)))
|
|
1002
|
+
return true;
|
|
1003
|
+
// A file Windows holds open is not evidence that the two copies differ:
|
|
1004
|
+
// compare again shortly, and past the budget leave the old file for the
|
|
1005
|
+
// next start, as when the unlink itself is refused.
|
|
1006
|
+
if (isWindowsSharingError(error)) {
|
|
1007
|
+
if (performance.now() - started >= SHARING_RETRY_BUDGET_MS)
|
|
1008
|
+
return false;
|
|
1009
|
+
continue;
|
|
1010
|
+
}
|
|
1011
|
+
// Unreadable on either side for another reason: which copy is current
|
|
1012
|
+
// is unknown.
|
|
1013
|
+
throw credentialConflict(stateDir, file);
|
|
1014
|
+
}
|
|
1015
|
+
if (!same)
|
|
1016
|
+
throw credentialConflict(stateDir, file);
|
|
1017
|
+
await guard();
|
|
1018
|
+
try {
|
|
1019
|
+
await unlink(file.old);
|
|
1020
|
+
return true;
|
|
1021
|
+
}
|
|
1022
|
+
catch (error) {
|
|
1023
|
+
if (error.code === 'ENOENT')
|
|
1024
|
+
return true;
|
|
1025
|
+
if (!isWindowsSharingError(error) ||
|
|
1026
|
+
performance.now() - started >= SHARING_RETRY_BUDGET_MS)
|
|
1027
|
+
return false;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
return false;
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
* After a finished move, an old vault file in the config folder means an
|
|
1034
|
+
* older version wrote it (re-enrolled, toggled the mode, synced the roster).
|
|
1035
|
+
* Which copy is current is unknown, so the start is refused, naming both
|
|
1036
|
+
* paths. The one exception is the vault-shaped enrollment state file the move
|
|
1037
|
+
* left there on purpose (`isVaultShapedKeptState`), which an older version may
|
|
1038
|
+
* have rewritten since.
|
|
1039
|
+
*/
|
|
1040
|
+
async function refuseReappearedFiles(location, files) {
|
|
1041
|
+
const keptState = vaultStateFile(files).old;
|
|
1042
|
+
for (const name of await vaultFiles(files.vault.old)) {
|
|
1043
|
+
const old = join(files.vault.old, name);
|
|
1044
|
+
if (old === keptState && (await isVaultShapedKeptState(files)))
|
|
1045
|
+
continue;
|
|
1046
|
+
throw credentialConflict(location.stateHomeDir, {
|
|
1047
|
+
old,
|
|
1048
|
+
next: join(files.vault.next, name),
|
|
1049
|
+
});
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* The marker, or undefined when it does not exist. It is written atomically
|
|
1054
|
+
* after all copies are verified, so malformed contents mean outside damage,
|
|
1055
|
+
* not evidence that the move completed. Other read failures are retried
|
|
1056
|
+
* briefly, then refuse the start: they prove neither presence nor absence.
|
|
1057
|
+
*/
|
|
1058
|
+
async function readMarker(path, stateDir, oldVaultDir) {
|
|
1059
|
+
let text;
|
|
1060
|
+
let failure;
|
|
1061
|
+
for (let attempt = 0; attempt < MARKER_READ_ATTEMPTS; attempt++) {
|
|
1062
|
+
if (attempt > 0)
|
|
1063
|
+
await new Promise((done) => setTimeout(done, MARKER_READ_RETRY_MS));
|
|
1064
|
+
try {
|
|
1065
|
+
text = await readFile(path, 'utf8');
|
|
1066
|
+
break;
|
|
1067
|
+
}
|
|
1068
|
+
catch (error) {
|
|
1069
|
+
const code = error.code;
|
|
1070
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
1071
|
+
return undefined;
|
|
1072
|
+
failure = error;
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
if (text === undefined)
|
|
1076
|
+
throw new RuntimeLayoutRefusal(stateDir, errorClass(failure));
|
|
1077
|
+
try {
|
|
1078
|
+
const value = JSON.parse(text);
|
|
1079
|
+
if (value && typeof value === 'object' && !Array.isArray(value))
|
|
1080
|
+
return {
|
|
1081
|
+
version: 1,
|
|
1082
|
+
...(typeof value.oldDir === 'string' ? { oldDir: value.oldDir } : {}),
|
|
1083
|
+
...(value.oldFilesRemoved === true ? { oldFilesRemoved: true } : {}),
|
|
1084
|
+
...(typeof value.keptVaultState === 'string' &&
|
|
1085
|
+
SHA256_HEX.test(value.keptVaultState)
|
|
1086
|
+
? { keptVaultState: value.keptVaultState }
|
|
1087
|
+
: {}),
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
catch { }
|
|
1091
|
+
throw new RuntimeLayoutRefusal(stateDir, 'malformed-marker', `antigravity-auth cannot start: ${path} is malformed, so it cannot tell whether its vault files were moved. ${markerRemedy(path, oldVaultDir)}`);
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Refuse a state-home folder whose `config-dir.json` or marker names another
|
|
1095
|
+
* config folder: its files belong to that folder's OpenCode.
|
|
1096
|
+
*/
|
|
1097
|
+
async function assertOwnFolder(location, marker, files) {
|
|
1098
|
+
const stateDir = location.stateHomeDir;
|
|
1099
|
+
if (marker?.oldDir !== undefined && marker.oldDir !== location.key)
|
|
1100
|
+
throw foreignMarker(join(stateDir, RUNTIME_LAYOUT_MARKER_FILE), location.key, files.vault.old);
|
|
1101
|
+
let text;
|
|
1102
|
+
try {
|
|
1103
|
+
text = await readFile(join(stateDir, RUNTIME_CONFIG_DIR_NOTE_FILE), 'utf8');
|
|
1104
|
+
}
|
|
1105
|
+
catch (error) {
|
|
1106
|
+
const code = error.code;
|
|
1107
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
1108
|
+
return;
|
|
1109
|
+
throw new RuntimeLayoutRefusal(stateDir, errorClass(error));
|
|
1110
|
+
}
|
|
1111
|
+
let owner;
|
|
1112
|
+
try {
|
|
1113
|
+
owner = JSON.parse(text)?.configDir;
|
|
1114
|
+
}
|
|
1115
|
+
catch { }
|
|
1116
|
+
if (owner !== location.key)
|
|
1117
|
+
throw foreignFolder(stateDir, location.key);
|
|
1118
|
+
}
|
|
1119
|
+
/** Name this folder's config folder, once; the caller holds the layout lock. */
|
|
1120
|
+
async function writeOwnerNote(location) {
|
|
1121
|
+
const path = join(location.stateHomeDir, RUNTIME_CONFIG_DIR_NOTE_FILE);
|
|
1122
|
+
if (await exists(path))
|
|
1123
|
+
return;
|
|
1124
|
+
await writePrivateText(path, `${JSON.stringify({ version: 1, configDir: location.key }, null, 2)}\n`);
|
|
1125
|
+
}
|
|
1126
|
+
/** The copies an unfinished move listed; empty when there is none. */
|
|
1127
|
+
async function readManifest(stateDir) {
|
|
1128
|
+
let text;
|
|
1129
|
+
try {
|
|
1130
|
+
text = await readFile(join(stateDir, RUNTIME_LAYOUT_MANIFEST_FILE), 'utf8');
|
|
1131
|
+
}
|
|
1132
|
+
catch (error) {
|
|
1133
|
+
if (error.code === 'ENOENT')
|
|
1134
|
+
return new Set();
|
|
1135
|
+
throw error;
|
|
1136
|
+
}
|
|
1137
|
+
// A manifest that does not parse proves nothing about whose files these
|
|
1138
|
+
// are; the destination check then treats them as someone else's.
|
|
1139
|
+
try {
|
|
1140
|
+
const value = JSON.parse(text);
|
|
1141
|
+
if (Array.isArray(value?.files))
|
|
1142
|
+
return new Set(value.files.filter((path) => typeof path === 'string'));
|
|
1143
|
+
}
|
|
1144
|
+
catch { }
|
|
1145
|
+
return new Set();
|
|
1146
|
+
}
|
|
1147
|
+
async function writeManifest(stateDir, paths) {
|
|
1148
|
+
await writePrivateText(join(stateDir, RUNTIME_LAYOUT_MANIFEST_FILE), `${JSON.stringify({ version: 1, files: paths }, null, 2)}\n`);
|
|
1149
|
+
}
|
|
1150
|
+
async function removeManifest(stateDir) {
|
|
1151
|
+
await unlink(join(stateDir, RUNTIME_LAYOUT_MANIFEST_FILE)).catch((error) => {
|
|
1152
|
+
if (error.code !== 'ENOENT')
|
|
1153
|
+
throw error;
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
async function writeMarker(path, marker) {
|
|
1157
|
+
await writePrivateText(path, `${JSON.stringify(marker, null, 2)}\n`);
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* Owner-only, durable write through common-auth's atomic writer: mode 0600,
|
|
1161
|
+
* the file and then its folder synced on POSIX; the private-files ACL and a
|
|
1162
|
+
* file flush on Windows, where folder sync is not available. On Windows the
|
|
1163
|
+
* writer creates every missing folder with an owner-only ACL itself; this
|
|
1164
|
+
* module never calls mkdir there.
|
|
1165
|
+
*/
|
|
1166
|
+
async function writePrivateText(path, text) {
|
|
1167
|
+
await ensureOwnerOnlyDir(dirname(path));
|
|
1168
|
+
const fs = await loadCommonAuthFs();
|
|
1169
|
+
await fs.writeJsonAtomic(path, null, {
|
|
1170
|
+
serialize: () => text,
|
|
1171
|
+
durable: true,
|
|
1172
|
+
durability: 'file-only-on-windows',
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
/**
|
|
1176
|
+
* On POSIX, create the folder as 0700 and tighten it if it exists (as the
|
|
1177
|
+
* heartbeat folder does). On Windows the private-files writer and the lock
|
|
1178
|
+
* create every missing folder with an owner-only ACL themselves.
|
|
1179
|
+
*/
|
|
1180
|
+
async function ensureOwnerOnlyDir(path) {
|
|
1181
|
+
if (process.platform === 'win32')
|
|
1182
|
+
return;
|
|
1183
|
+
await mkdir(path, { recursive: true, mode: 0o700 });
|
|
1184
|
+
await chmod(path, 0o700);
|
|
1185
|
+
}
|
|
1186
|
+
/**
|
|
1187
|
+
* Take one lease, waiting up to `waitMs` while another process holds it. A
|
|
1188
|
+
* wait longer than a second is logged once, naming the lock, so a slow start
|
|
1189
|
+
* says what it is waiting for; a wait that runs out fails with a message
|
|
1190
|
+
* that names it too.
|
|
1191
|
+
*/
|
|
1192
|
+
async function takeLock(name, path, waitMs, log) {
|
|
1193
|
+
const fs = await loadCommonAuthFs();
|
|
1194
|
+
const started = performance.now();
|
|
1195
|
+
let announced = false;
|
|
1196
|
+
for (;;) {
|
|
1197
|
+
let lock = null;
|
|
1198
|
+
try {
|
|
1199
|
+
lock = await fs.acquireRefreshFileLock({
|
|
1200
|
+
name,
|
|
1201
|
+
path,
|
|
1202
|
+
ttlMs: LOCK_TTL_MS,
|
|
1203
|
+
renew: true,
|
|
1204
|
+
});
|
|
1205
|
+
}
|
|
1206
|
+
catch (error) {
|
|
1207
|
+
// On Windows a holder that releases while this attempt reads its lock
|
|
1208
|
+
// file makes the attempt fail with ENOENT: the lock is free, so try
|
|
1209
|
+
// again rather than fail.
|
|
1210
|
+
if (error.code !== 'ENOENT')
|
|
1211
|
+
throw error;
|
|
1212
|
+
}
|
|
1213
|
+
if (lock)
|
|
1214
|
+
return lock;
|
|
1215
|
+
const waited = performance.now() - started;
|
|
1216
|
+
if (waited >= waitMs)
|
|
1217
|
+
throw new LayoutMoveError('lock-timeout', `it waited ${Math.round(waitMs / 1000)} s for the ${name} lock at ${path}.${lockHolderText(name)}`);
|
|
1218
|
+
if (!announced && waited >= LOCK_WAIT_NOTICE_MS) {
|
|
1219
|
+
announced = true;
|
|
1220
|
+
log.info(`antigravity-auth is waiting up to ${Math.round(waitMs / 1000)} s for the ${name} lock at ${path}.${lockHolderText(name)}`, { lock: name, path });
|
|
1221
|
+
}
|
|
1222
|
+
await new Promise((done) => setTimeout(done, LOCK_RETRY_MS));
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
function lockHolderText(name) {
|
|
1226
|
+
return name === RUNTIME_LAYOUT_LOCK_NAME
|
|
1227
|
+
? ' Another OpenCode holds it while it decides where the vault files live or moves them; a lock left by one that crashed expires by itself.'
|
|
1228
|
+
: ' Another OpenCode holds it while it writes that vault file; a lock left by one that crashed expires by itself.';
|
|
1229
|
+
}
|
|
1230
|
+
async function exists(path) {
|
|
1231
|
+
try {
|
|
1232
|
+
await stat(path);
|
|
1233
|
+
return true;
|
|
1234
|
+
}
|
|
1235
|
+
catch (error) {
|
|
1236
|
+
const code = error.code;
|
|
1237
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
1238
|
+
return false;
|
|
1239
|
+
throw error;
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
//# sourceMappingURL=runtime-layout.js.map
|