@astrale-os/cli 0.8.0-alpha.0 → 0.8.1-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/dist/astrale.js +62968 -41240
- package/dist/public/connect-core.js +55030 -0
- package/dist/public/keys/index.js +45631 -0
- package/dist/public/paths/index.js +43990 -0
- package/dist/types/admin/instance/model.d.ts +40 -0
- package/dist/types/connect-core.d.ts +22 -0
- package/dist/types/connection/auth.d.ts +33 -0
- package/dist/types/errors.d.ts +23 -0
- package/dist/types/identity/registry.d.ts +25 -0
- package/dist/types/keys/algorithm.d.ts +6 -0
- package/dist/types/keys/credential.d.ts +21 -0
- package/dist/types/keys/index.d.ts +4 -0
- package/dist/types/keys/pair.d.ts +34 -0
- package/dist/types/lib/admin-target.d.ts +74 -0
- package/dist/types/lib/ca-fetch.d.ts +2 -0
- package/dist/types/lib/config.d.ts +71 -0
- package/dist/types/lib/format.d.ts +1 -0
- package/dist/types/lib/idp-session.d.ts +33 -0
- package/dist/types/lib/idp.d.ts +397 -0
- package/dist/types/lib/instance-target.d.ts +36 -0
- package/dist/types/lib/instance.d.ts +186 -0
- package/dist/types/lib/log.d.ts +30 -0
- package/dist/types/lib/login-flow.d.ts +49 -0
- package/dist/types/lib/meta.d.ts +36 -0
- package/dist/types/lib/output.d.ts +83 -0
- package/dist/types/lib/table.d.ts +18 -0
- package/dist/types/lib/validation.d.ts +11 -0
- package/dist/types/paths/index.d.ts +2 -0
- package/dist/types/state/exchange-credentials.d.ts +25 -0
- package/dist/types/state/files.d.ts +9 -0
- package/dist/types/state/identities.d.ts +42 -0
- package/dist/types/state/index.d.ts +8 -0
- package/dist/types/state/paths.d.ts +32 -0
- package/package.json +30 -8
- package/src/admin/.spec/api.d.ts +3 -0
- package/src/admin/.spec/architecture.md +15 -0
- package/src/admin/.spec/icon.svg +3 -0
- package/src/admin/.spec/layout.ts +6 -0
- package/src/admin/catalog/.spec/api.d.ts +65 -0
- package/src/admin/catalog/.spec/architecture.md +6 -0
- package/src/admin/catalog/.spec/icon.svg +3 -0
- package/src/admin/catalog/.spec/layout.ts +6 -0
- package/src/admin/catalog/__tests__/client.test.ts +171 -0
- package/src/admin/catalog/client.ts +214 -0
- package/src/admin/catalog/index.ts +13 -0
- package/src/admin/catalog/model.ts +42 -0
- package/src/admin/catalog/tsconfig.json +8 -0
- package/src/admin/graph/.spec/api.d.ts +28 -0
- package/src/admin/graph/.spec/architecture.md +5 -0
- package/src/admin/graph/.spec/icon.svg +3 -0
- package/src/admin/graph/.spec/layout.ts +3 -0
- package/src/admin/graph/index.ts +6 -0
- package/src/admin/graph/nodes.ts +56 -0
- package/src/admin/index.ts +2 -0
- package/src/admin/instance/.spec/api.d.ts +66 -0
- package/src/admin/instance/.spec/architecture.md +10 -0
- package/src/admin/instance/.spec/icon.svg +3 -0
- package/src/admin/instance/.spec/layout.ts +6 -0
- package/src/admin/instance/__tests__/client.test.ts +289 -0
- package/src/admin/instance/client.ts +298 -0
- package/src/admin/instance/index.ts +16 -0
- package/src/admin/instance/model.ts +65 -0
- package/src/admin/instance/tsconfig.json +8 -0
- package/src/commands/__tests__/admin-instance.test.ts +3 -36
- package/src/commands/__tests__/call.test.ts +21 -68
- package/src/commands/__tests__/domain-install-owned.test.ts +26 -27
- package/src/commands/__tests__/domain-list.test.ts +2 -6
- package/src/commands/__tests__/logs.test.ts +50 -98
- package/src/commands/__tests__/ls.test.ts +21 -17
- package/src/commands/__tests__/read-commands.test.ts +171 -101
- package/src/commands/__tests__/view.test.ts +100 -51
- package/src/commands/admin/status.ts +3 -3
- package/src/commands/admin/use.ts +29 -6
- package/src/commands/auth/login.ts +1 -1
- package/src/commands/auth/logout.ts +4 -2
- package/src/commands/auth/status.ts +2 -2
- package/src/commands/auth/token.ts +2 -2
- package/src/commands/browser.ts +1 -1
- package/src/commands/call.ts +55 -91
- package/src/commands/describe.ts +52 -147
- package/src/commands/domain/install.ts +81 -50
- package/src/commands/domain/list.ts +4 -8
- package/src/commands/domain/publish.ts +9 -31
- package/src/commands/get.ts +32 -65
- package/src/commands/identity/__tests__/register.test.ts +93 -0
- package/src/commands/identity/create.ts +2 -2
- package/src/commands/identity/delete.ts +2 -2
- package/src/commands/identity/export.ts +7 -33
- package/src/commands/identity/import.ts +21 -70
- package/src/commands/identity/list.ts +2 -2
- package/src/commands/identity/register.ts +148 -117
- package/src/commands/identity/sync.ts +2 -2
- package/src/commands/identity/unsync.ts +2 -2
- package/src/commands/identity/use.ts +2 -2
- package/src/commands/identity/whoami.ts +2 -2
- package/src/commands/idp/add.ts +1 -1
- package/src/commands/idp/list.ts +1 -1
- package/src/commands/idp/refresh.ts +1 -1
- package/src/commands/idp/remove.ts +2 -2
- package/src/commands/idp/show.ts +1 -1
- package/src/commands/instance/active.ts +2 -2
- package/src/commands/instance/bookmark.ts +8 -2
- package/src/commands/instance/create.ts +1 -1
- package/src/commands/instance/delete.ts +4 -10
- package/src/commands/instance/forget.ts +1 -1
- package/src/commands/instance/list.ts +3 -3
- package/src/commands/instance/status.ts +5 -7
- package/src/commands/instance/use.ts +3 -3
- package/src/commands/logs.ts +152 -279
- package/src/commands/ls.ts +78 -204
- package/src/commands/mutate.ts +70 -139
- package/src/commands/query.ts +100 -278
- package/src/commands/session/analyze.ts +1 -1
- package/src/commands/session/list.ts +1 -1
- package/src/commands/setup.ts +1 -1
- package/src/commands/status.ts +1 -1
- package/src/commands/studio.ts +1 -1
- package/src/commands/token.ts +26 -21
- package/src/commands/update.ts +1 -1
- package/src/commands/use.ts +2 -3
- package/src/commands/view-serve.ts +1 -1
- package/src/commands/view.ts +67 -109
- package/src/connect-core.test.ts +38 -0
- package/src/connect-core.ts +42 -0
- package/src/connection/.spec/api.d.ts +111 -0
- package/src/connection/.spec/architecture.md +41 -0
- package/src/connection/.spec/capabilities/connection.ts +13 -0
- package/src/connection/.spec/examples/call.ts +7 -0
- package/src/connection/.spec/flows/command.ts +20 -0
- package/src/connection/.spec/flows/session.ts +61 -0
- package/src/connection/.spec/laws/connection.ts +181 -0
- package/src/connection/.spec/layout.ts +19 -0
- package/src/{kernel → connection}/__tests__/auth.test.ts +26 -2
- package/src/connection/__tests__/ca-fetch.test.ts +53 -0
- package/src/connection/__tests__/call.test.ts +17 -0
- package/src/connection/__tests__/credential.test.ts +109 -0
- package/src/connection/__tests__/errors.test.ts +96 -0
- package/src/connection/__tests__/exchange.test.ts +224 -0
- package/src/connection/__tests__/fixtures/localhost-cert.pem +19 -0
- package/src/connection/__tests__/fixtures/localhost-key.base64 +1 -0
- package/src/connection/__tests__/managed.test.ts +39 -0
- package/src/connection/__tests__/self.test.ts +51 -0
- package/src/connection/__tests__/session.test.ts +140 -0
- package/src/connection/__tests__/target.test.ts +93 -0
- package/src/{kernel → connection}/auth.ts +8 -8
- package/src/connection/call.ts +7 -0
- package/src/{kernel/run.ts → connection/command.ts} +22 -13
- package/src/connection/credential.ts +114 -0
- package/src/{kernel → connection}/errors.ts +32 -9
- package/src/connection/exchange.ts +274 -0
- package/src/connection/index.ts +8 -0
- package/src/connection/self.ts +120 -0
- package/src/connection/session.ts +176 -0
- package/src/connection/target.ts +111 -0
- package/src/connection/tsconfig.json +6 -0
- package/src/graph/.spec/api.d.ts +34 -0
- package/src/graph/.spec/laws/documents.ts +24 -0
- package/src/graph/.spec/laws/projection.ts +13 -0
- package/src/graph/.spec/layout.ts +6 -0
- package/src/graph/__tests__/mutation.test.ts +43 -0
- package/src/graph/__tests__/projection.test.ts +22 -0
- package/src/graph/__tests__/query.test.ts +154 -0
- package/src/graph/index.ts +3 -0
- package/src/graph/mutation.ts +18 -0
- package/src/graph/projection.ts +21 -0
- package/src/graph/query.ts +108 -0
- package/src/graph/tsconfig.json +6 -0
- package/src/identity/.history/ard/bootstrap-root-transfer.md +57 -0
- package/src/identity/.history/ard/domain-mediated-registration.md +36 -0
- package/src/identity/.spec/api.d.ts +124 -0
- package/src/identity/.spec/architecture.md +33 -0
- package/src/identity/.spec/capabilities/identity.ts +7 -0
- package/src/identity/.spec/flows/bootstrap-root.ts +79 -0
- package/src/identity/.spec/flows/domain-mediated-registration.ts +51 -0
- package/src/identity/.spec/flows/import.ts +29 -0
- package/src/identity/.spec/laws/identity.ts +56 -0
- package/src/identity/.spec/layout.ts +13 -0
- package/src/identity/.spec/schemas/identity-export-v1.schema.json +25 -0
- package/src/identity/__tests__/fixtures/registry-journey.ts +53 -0
- package/src/identity/__tests__/registration.test.ts +64 -0
- package/src/identity/__tests__/registry.test.ts +70 -0
- package/src/identity/__tests__/transfer.test.ts +198 -0
- package/src/identity/index.ts +23 -0
- package/src/identity/registration.ts +72 -0
- package/src/identity/registry.ts +172 -0
- package/src/identity/transfer.ts +187 -0
- package/src/identity/tsconfig.json +6 -0
- package/src/index.ts +1 -0
- package/src/keys/.spec/api.d.ts +74 -0
- package/src/keys/.spec/architecture.md +13 -0
- package/src/keys/.spec/laws/keys.ts +43 -0
- package/src/keys/.spec/layout.ts +6 -0
- package/src/keys/__tests__/algorithm.test.ts +39 -0
- package/src/keys/__tests__/keys.test.ts +168 -0
- package/src/keys/algorithm.ts +33 -0
- package/src/keys/credential.ts +112 -0
- package/src/keys/index.ts +14 -0
- package/src/keys/pair.ts +219 -0
- package/src/keys/tsconfig.json +6 -0
- package/src/lib/__tests__/admin-target.test.ts +67 -7
- package/src/lib/__tests__/binary.test.ts +2 -3
- package/src/lib/__tests__/command-dx.test.ts +2 -2
- package/src/lib/__tests__/config.test.ts +3 -2
- package/src/lib/__tests__/idp.test.ts +4 -6
- package/src/lib/__tests__/instance-target.test.ts +7 -4
- package/src/lib/__tests__/local-status.test.ts +1 -1
- package/src/lib/__tests__/log.test.ts +31 -0
- package/src/lib/__tests__/meta.test.ts +55 -0
- package/src/lib/__tests__/sdk-deps.test.ts +1 -1
- package/src/lib/__tests__/self.test.ts +8 -230
- package/src/lib/__tests__/use-target.test.ts +1 -1
- package/src/lib/__tests__/view-open-intent.test.ts +78 -104
- package/src/lib/__tests__/view-session.test.ts +99 -0
- package/src/lib/admin-domain.ts +39 -25
- package/src/lib/admin-instance.ts +38 -43
- package/src/lib/admin-target.ts +67 -13
- package/src/lib/binary.ts +13 -34
- package/src/lib/browser.ts +1 -1
- package/src/{kernel → lib}/ca-fetch.ts +35 -27
- package/src/lib/config.ts +1 -1
- package/src/lib/idp-session.ts +1 -1
- package/src/lib/idp.ts +3 -8
- package/src/lib/instance-target.ts +12 -8
- package/src/lib/instance.ts +34 -2
- package/src/lib/local-status.ts +2 -2
- package/src/lib/log.ts +3 -1
- package/src/lib/login-flow.ts +32 -5
- package/src/lib/meta.ts +15 -11
- package/src/lib/provision-instance.ts +4 -12
- package/src/lib/self.ts +5 -147
- package/src/lib/update.ts +1 -1
- package/src/lib/use-target.ts +1 -1
- package/src/lib/validation.ts +2 -2
- package/src/lib/view/open-intent.ts +7 -37
- package/src/lib/view/port-allocation.ts +1 -1
- package/src/lib/view/resolve.ts +52 -45
- package/src/lib/view/server.ts +16 -33
- package/src/lib/view/session.ts +51 -22
- package/src/paths/.spec/api.d.ts +13 -0
- package/src/paths/.spec/architecture.md +5 -0
- package/src/paths/.spec/layout.ts +3 -0
- package/src/paths/index.ts +13 -0
- package/src/program/.spec/api.d.ts +42 -0
- package/src/program/.spec/architecture.md +18 -0
- package/src/program/.spec/laws/program.ts +29 -0
- package/src/program/.spec/layout.ts +14 -0
- package/src/program/__tests__/program.test.ts +359 -0
- package/src/program/build.ts +190 -0
- package/src/program/command.ts +42 -0
- package/src/program/index.ts +2 -0
- package/src/program/options.ts +40 -0
- package/src/{registry.ts → program/registry.ts} +5 -1
- package/src/program/tsconfig.json +6 -0
- package/src/setup/steps/admin.ts +18 -3
- package/src/setup/steps/instance.ts +1 -1
- package/src/state/.spec/api.d.ts +133 -0
- package/src/state/.spec/architecture.md +31 -0
- package/src/state/.spec/capabilities/state.ts +7 -0
- package/src/state/.spec/flows/identity-update.ts +22 -0
- package/src/state/.spec/laws/state.ts +73 -0
- package/src/state/.spec/layout.ts +14 -0
- package/src/state/__tests__/exchange-credentials.test.ts +188 -0
- package/src/state/__tests__/files.test.ts +113 -0
- package/src/state/__tests__/fixtures/identity-transition.ts +37 -0
- package/src/state/__tests__/identities.test.ts +142 -0
- package/src/state/__tests__/paths.test.ts +27 -0
- package/src/state/exchange-credentials.ts +219 -0
- package/src/state/files.ts +129 -0
- package/src/state/identities.ts +236 -0
- package/src/state/index.ts +29 -0
- package/src/state/paths.ts +61 -0
- package/src/state/tsconfig.json +6 -0
- package/src/telemetry/__tests__/redact.test.ts +3 -3
- package/src/telemetry/__tests__/trigger.test.ts +1 -1
- package/src/telemetry/settings.ts +1 -1
- package/src/telemetry/store.ts +1 -1
- package/src/test-utils.ts +1 -1
- package/studio/client/dist/assets/elk-api-D0cBetPW.js +1 -0
- package/studio/client/dist/assets/elk-worker.min-C9JGDOE-.js +6312 -0
- package/studio/client/dist/assets/index-BQnJ5sgd.css +1 -0
- package/studio/client/dist/assets/index-ChOr3yP0.css +1 -0
- package/studio/client/dist/assets/index-Dspir4w7.js +81 -0
- package/studio/client/dist/assets/index-bVD2KJgz.js +8 -0
- package/studio/client/dist/index.html +2 -2
- package/studio/package.json +10 -1
- package/studio/server/introspect/extractor.ts +1 -1
- package/studio/server/state/views.test.ts +3 -7
- package/studio/server/state/views.ts +23 -30
- package/studio/tsconfig.json +23 -0
- package/tsconfig.json +1 -1
- package/viewer/dist/main.js +65 -39
- package/viewer/tsconfig.json +13 -0
- package/src/command.ts +0 -42
- package/src/commands/__tests__/help-contract.test.ts +0 -164
- package/src/kernel/__tests__/client-owned-lookup.test.ts +0 -51
- package/src/kernel/__tests__/errors.test.ts +0 -43
- package/src/kernel/__tests__/expand.test.ts +0 -123
- package/src/kernel/client.ts +0 -214
- package/src/kernel/expand.ts +0 -192
- package/src/kernel/graph.ts +0 -96
- package/src/kernel/index.ts +0 -29
- package/src/kernel/options.ts +0 -22
- package/src/kernel/types.ts +0 -14
- package/src/lib/__tests__/domain-identity.test.ts +0 -60
- package/src/lib/__tests__/fs-atomic.test.ts +0 -104
- package/src/lib/__tests__/identity.test.ts +0 -79
- package/src/lib/__tests__/keys.test.ts +0 -129
- package/src/lib/domain-identity.ts +0 -49
- package/src/lib/env.ts +0 -49
- package/src/lib/fs-atomic.ts +0 -126
- package/src/lib/identity.ts +0 -256
- package/src/lib/keys.ts +0 -294
- package/src/lib/paths.ts +0 -11
- package/src/program.ts +0 -213
- package/studio/client/dist/assets/index-BaqEuIQJ.js +0 -109
- package/studio/client/dist/assets/index-jRdExahh.css +0 -1
package/src/setup/steps/admin.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { SetupContext, SetupStep } from '../types'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
AdminTargetConfigSchema,
|
|
5
|
+
DEFAULT_ADMIN_DOMAIN_ISSUER,
|
|
6
|
+
DEFAULT_ADMIN_TARGET_NAME,
|
|
7
|
+
} from '../../lib/admin-target'
|
|
4
8
|
import { readConfig, writeConfig } from '../../lib/config'
|
|
5
9
|
import { readLocalStatus } from '../../lib/local-status'
|
|
6
10
|
import { log } from '../../lib/log'
|
|
@@ -13,13 +17,24 @@ async function setAdminUrl(url: string): Promise<void> {
|
|
|
13
17
|
const config = await readConfig()
|
|
14
18
|
await writeConfig({
|
|
15
19
|
...config,
|
|
16
|
-
admin: AdminTargetConfigSchema.parse({
|
|
20
|
+
admin: AdminTargetConfigSchema.parse({
|
|
21
|
+
name: DEFAULT_ADMIN_TARGET_NAME,
|
|
22
|
+
url,
|
|
23
|
+
kernelIssuer: url,
|
|
24
|
+
domainIssuer: config.admin.domainIssuer ?? DEFAULT_ADMIN_DOMAIN_ISSUER,
|
|
25
|
+
}),
|
|
17
26
|
})
|
|
18
27
|
}
|
|
19
28
|
|
|
20
29
|
async function setAdminBookmark(bookmark: string): Promise<void> {
|
|
21
30
|
const config = await readConfig()
|
|
22
|
-
await writeConfig({
|
|
31
|
+
await writeConfig({
|
|
32
|
+
...config,
|
|
33
|
+
admin: AdminTargetConfigSchema.parse({
|
|
34
|
+
instance: bookmark,
|
|
35
|
+
domainIssuer: config.admin.domainIssuer ?? DEFAULT_ADMIN_DOMAIN_ISSUER,
|
|
36
|
+
}),
|
|
37
|
+
})
|
|
23
38
|
}
|
|
24
39
|
|
|
25
40
|
/**
|
|
@@ -4,7 +4,7 @@ import type { OwnedInstanceInfo } from '../../lib/admin-instance'
|
|
|
4
4
|
import type { SetupContext, SetupStep } from '../types'
|
|
5
5
|
|
|
6
6
|
import { AstraleError } from '../../errors'
|
|
7
|
-
import { listOwnedInstances } from '../../
|
|
7
|
+
import { listOwnedInstances } from '../../lib/admin-instance'
|
|
8
8
|
import { normalizeInstanceKernelUrl, setActive, upsertManagedBookmark } from '../../lib/instance'
|
|
9
9
|
import { readLocalStatus } from '../../lib/local-status'
|
|
10
10
|
import { log, withSpinner } from '../../lib/log'
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/** Environment values that affect CLI-owned state placement. */
|
|
2
|
+
export interface PathEnvironment {
|
|
3
|
+
readonly ASTRALE_HOME?: string
|
|
4
|
+
readonly ASTRALE_KEYS_DIR?: string
|
|
5
|
+
readonly ASTRALE_DATA_DIR?: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** One captured set of CLI-owned durable-state coordinates. */
|
|
9
|
+
export interface Paths {
|
|
10
|
+
readonly home: string
|
|
11
|
+
readonly keys: string
|
|
12
|
+
readonly data: string
|
|
13
|
+
readonly config: string
|
|
14
|
+
readonly install: string
|
|
15
|
+
readonly identities: string
|
|
16
|
+
readonly instances: string
|
|
17
|
+
readonly idps: string
|
|
18
|
+
readonly idpSessionsDir: string
|
|
19
|
+
readonly exchangeCredentials: string
|
|
20
|
+
idpDir(name: string): string
|
|
21
|
+
idpSession(identityName: string): string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Resolve explicit home, environment overrides, and platform home exactly once. */
|
|
25
|
+
export function createPaths(home?: string, environment?: PathEnvironment): Paths
|
|
26
|
+
|
|
27
|
+
export const paths: Paths
|
|
28
|
+
export const ASTRALE_HOME: string
|
|
29
|
+
export const KEYS_DIR: string
|
|
30
|
+
export const DATA_DIR: string
|
|
31
|
+
export const CONFIG_PATH: string
|
|
32
|
+
export const INSTALL_PATH: string
|
|
33
|
+
export const IDENTITIES_PATH: string
|
|
34
|
+
export const INSTANCES_PATH: string
|
|
35
|
+
export const IDPS_PATH: string
|
|
36
|
+
export const IDP_SESSIONS_DIR: string
|
|
37
|
+
export const EXCHANGE_CREDENTIALS_PATH: string
|
|
38
|
+
|
|
39
|
+
export namespace exchange {
|
|
40
|
+
interface Artifact {
|
|
41
|
+
readonly version: 1
|
|
42
|
+
readonly entries: Record<string, Entry>
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface Key {
|
|
46
|
+
readonly kernelIssuer: string
|
|
47
|
+
readonly domainIssuer: string
|
|
48
|
+
readonly user: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface Entry {
|
|
52
|
+
readonly credential: string
|
|
53
|
+
readonly expiresAt: number
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Persist exact Domain bearer tokens under a private, cross-process synchronized cache. */
|
|
58
|
+
export class ExchangeCredentialCache {
|
|
59
|
+
constructor(path?: string)
|
|
60
|
+
getOrRefresh(
|
|
61
|
+
key: exchange.Key,
|
|
62
|
+
refresh: () => Promise<exchange.Entry>,
|
|
63
|
+
now?: () => number,
|
|
64
|
+
): Promise<string>
|
|
65
|
+
deleteKernel(kernelIssuer: string): Promise<void>
|
|
66
|
+
clear(): Promise<void>
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type IdentitySource = 'key' | 'idp'
|
|
70
|
+
export type IdentityMode = 'local' | 'remote'
|
|
71
|
+
|
|
72
|
+
export interface Registration {
|
|
73
|
+
readonly iss: string
|
|
74
|
+
readonly sub: string
|
|
75
|
+
readonly registeredAt: string
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface Identity {
|
|
79
|
+
readonly subject: string
|
|
80
|
+
readonly createdAt: string
|
|
81
|
+
readonly source?: IdentitySource
|
|
82
|
+
readonly mode?: IdentityMode
|
|
83
|
+
readonly kid?: string
|
|
84
|
+
readonly idp?: string
|
|
85
|
+
readonly issuer?: string
|
|
86
|
+
readonly audience?: string
|
|
87
|
+
readonly claims?: Readonly<Record<string, unknown>>
|
|
88
|
+
readonly registrations?: Readonly<Record<string, Registration>>
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Decoded semantic identity state; persistence-version metadata is not exposed to consumers. */
|
|
92
|
+
export interface IdentityStore {
|
|
93
|
+
readonly default: string
|
|
94
|
+
readonly identities: Readonly<Record<string, Identity>>
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export const IDENTITY_STORE_VERSION: 1
|
|
98
|
+
|
|
99
|
+
export interface IdentityStoreOptions {
|
|
100
|
+
readonly path?: string
|
|
101
|
+
readonly now?: () => Date
|
|
102
|
+
readonly lock?: FileLockOptions
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface IdentityUpdate<Value> {
|
|
106
|
+
readonly next: IdentityStore
|
|
107
|
+
readonly value: Value
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Decode the missing, legacy, or current identity file without writing or migrating it. */
|
|
111
|
+
export function readIdentityStore(options?: IdentityStoreOptions): Promise<IdentityStore>
|
|
112
|
+
|
|
113
|
+
/** Reread and commit one identity transition under the file's cross-process lock. */
|
|
114
|
+
export function updateIdentityStore<Value>(
|
|
115
|
+
transition: (current: IdentityStore) => IdentityUpdate<Value> | Promise<IdentityUpdate<Value>>,
|
|
116
|
+
options?: IdentityStoreOptions,
|
|
117
|
+
): Promise<Value>
|
|
118
|
+
|
|
119
|
+
/** Atomically replace one private CLI state file through a same-directory temporary file. */
|
|
120
|
+
export function atomicWrite(path: string, data: string): Promise<void>
|
|
121
|
+
|
|
122
|
+
export interface FileLockOptions {
|
|
123
|
+
readonly pollIntervalMs?: number
|
|
124
|
+
readonly staleAfterMs?: number
|
|
125
|
+
readonly timeoutMs?: number
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Run one state transition under a bounded cross-process file lock. */
|
|
129
|
+
export function withFileLock<Value>(
|
|
130
|
+
lockPath: string,
|
|
131
|
+
transition: () => Promise<Value>,
|
|
132
|
+
options?: FileLockOptions,
|
|
133
|
+
): Promise<Value>
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# CLI state architecture
|
|
2
|
+
|
|
3
|
+
This module owns CLI filesystem placement, the durable file-transition primitives shared by state
|
|
4
|
+
registries, and each registry's persistence admission. `createPaths` captures explicit and
|
|
5
|
+
environment coordinates once. `atomicWrite` publishes one complete `0600` file through a
|
|
6
|
+
same-directory temporary file. `withFileLock` bounds cross-process read-modify-write exclusion and
|
|
7
|
+
releases its lock on every terminal path.
|
|
8
|
+
|
|
9
|
+
The identity registry decodes the legacy unversioned shape and current V1 envelope into one semantic
|
|
10
|
+
`IdentityStore`. Reads never migrate. The first successful mutation of a legacy file preserves its
|
|
11
|
+
exact bytes at `identities.json.v0.bak` before publishing V1. Invalid or newer files remain untouched
|
|
12
|
+
and fail closed. Identity orchestration owns key and IdP-session effects above this module.
|
|
13
|
+
|
|
14
|
+
The exchange registry owns a versioned `exchange.Artifact`. Each entry is keyed by the atomic
|
|
15
|
+
`(Kernel issuer, Domain issuer, registered User)` identity and stores only one inspected Domain
|
|
16
|
+
credential and expiry. It reuses the same atomic-write and bounded file-lock primitives, maintains
|
|
17
|
+
owner-only directory and file modes, and remains distinct from Client's process-local route cache.
|
|
18
|
+
|
|
19
|
+
Shape decoding, migrations, and backup policy remain with each semantic registry. Commands do not
|
|
20
|
+
call these primitives directly, and Kernel Client owns no CLI filesystem state.
|
|
21
|
+
|
|
22
|
+
```mermaid
|
|
23
|
+
flowchart LR
|
|
24
|
+
C[CLI command] --> I[identity orchestration]
|
|
25
|
+
I --> R[IdentityStore transition]
|
|
26
|
+
R --> L[withFileLock]
|
|
27
|
+
L --> B[legacy backup when needed]
|
|
28
|
+
B --> A[atomicWrite V1]
|
|
29
|
+
A --> F[identities.json]
|
|
30
|
+
P[createPaths] --> R
|
|
31
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { IdentityStore, IdentityUpdate } from '../api.js'
|
|
2
|
+
|
|
3
|
+
declare const withIdentityLock: <Value>(transition: () => Promise<Value>) => Promise<Value>
|
|
4
|
+
declare const readLatest: () => Promise<{
|
|
5
|
+
readonly store: IdentityStore
|
|
6
|
+
readonly legacyBytes?: string
|
|
7
|
+
}>
|
|
8
|
+
declare const preserveLegacyBackup: (bytes: string) => Promise<void>
|
|
9
|
+
declare const publishV1: (store: IdentityStore) => Promise<void>
|
|
10
|
+
|
|
11
|
+
/** One identity mutation rereads, transitions, backs up legacy bytes, then publishes exactly once. */
|
|
12
|
+
export function updateIdentity<Value>(
|
|
13
|
+
transition: (current: IdentityStore) => IdentityUpdate<Value> | Promise<IdentityUpdate<Value>>,
|
|
14
|
+
): Promise<Value> {
|
|
15
|
+
return withIdentityLock(async () => {
|
|
16
|
+
const current = await readLatest()
|
|
17
|
+
const update = await transition(current.store)
|
|
18
|
+
if (current.legacyBytes !== undefined) await preserveLegacyBackup(current.legacyBytes)
|
|
19
|
+
await publishV1(update.next)
|
|
20
|
+
return update.value
|
|
21
|
+
})
|
|
22
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { defineLaw } from '@astrale-os/spec/authoring'
|
|
2
|
+
|
|
3
|
+
export const CLI_STATE_PATHS_CAPTURED = defineLaw({
|
|
4
|
+
id: 'CLI-STATE-PATHS-CAPTURED',
|
|
5
|
+
statement:
|
|
6
|
+
'Path construction applies explicit-home then environment then platform-home precedence once; later environment mutation cannot change the returned coordinates.',
|
|
7
|
+
tests: [{ file: '__tests__/paths.test.ts', id: 'TEST-CLI-STATE-PATHS-CAPTURED' }],
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
export const CLI_STATE_ATOMIC_REPLACEMENT = defineLaw({
|
|
11
|
+
id: 'CLI-STATE-ATOMIC-REPLACEMENT',
|
|
12
|
+
statement:
|
|
13
|
+
'A successful replacement publishes one complete mode-0600 file and leaves no temporary file; a failed replacement never publishes partial content.',
|
|
14
|
+
tests: [
|
|
15
|
+
{ file: '__tests__/files.test.ts', id: 'TEST-CLI-STATE-ATOMIC-WRITE' },
|
|
16
|
+
{ file: '__tests__/files.test.ts', id: 'TEST-CLI-STATE-ATOMIC-WRITE-FAILURE' },
|
|
17
|
+
],
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
export const CLI_STATE_LOCK_BOUNDED = defineLaw({
|
|
21
|
+
id: 'CLI-STATE-LOCK-BOUNDED',
|
|
22
|
+
statement:
|
|
23
|
+
'Contending transitions serialize, abandoned locks are recoverable, acquisition is bounded, and every acquired lock is released after success or failure.',
|
|
24
|
+
tests: [
|
|
25
|
+
{ file: '__tests__/files.test.ts', id: 'TEST-CLI-STATE-LOCK-SERIALIZES' },
|
|
26
|
+
{ file: '__tests__/files.test.ts', id: 'TEST-CLI-STATE-LOCK-RECOVERS' },
|
|
27
|
+
{ file: '__tests__/files.test.ts', id: 'TEST-CLI-STATE-LOCK-RELEASES-AND-BOUNDS' },
|
|
28
|
+
],
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
export const CLI_STATE_IDENTITY_READ_SAFE = defineLaw({
|
|
32
|
+
id: 'CLI-STATE-IDENTITY-READ-SAFE',
|
|
33
|
+
statement:
|
|
34
|
+
'Missing, legacy, and current identity files decode without a write; malformed or unsupported-version files fail without replacement.',
|
|
35
|
+
tests: [
|
|
36
|
+
{ file: '__tests__/identities.test.ts', id: 'TEST-CLI-STATE-IDENTITY-READ-SAFE' },
|
|
37
|
+
{ file: '__tests__/identities.test.ts', id: 'TEST-CLI-STATE-IDENTITY-FAILS-CLOSED' },
|
|
38
|
+
],
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
export const CLI_STATE_IDENTITY_MIGRATION = defineLaw({
|
|
42
|
+
id: 'CLI-STATE-IDENTITY-MIGRATION',
|
|
43
|
+
statement:
|
|
44
|
+
'The first successful mutation of legacy identity state preserves its exact bytes once before publishing V1; concurrent mutations reread and retain every committed transition.',
|
|
45
|
+
tests: [
|
|
46
|
+
{ file: '__tests__/identities.test.ts', id: 'TEST-CLI-STATE-IDENTITY-MIGRATES' },
|
|
47
|
+
{ file: '__tests__/identities.test.ts', id: 'TEST-CLI-STATE-IDENTITY-CONCURRENT' },
|
|
48
|
+
],
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
export const CLI_STATE_EXCHANGE_CACHE = defineLaw({
|
|
52
|
+
id: 'CLI-STATE-EXCHANGE-CACHE',
|
|
53
|
+
statement:
|
|
54
|
+
'The durable exchange Artifact partitions Domain credentials by exact Kernel issuer, Domain issuer, and registered User, requires the sole nested Kernel proof subject to equal that User, admits matching token claims and expiry, refreshes under process and file-lock singleflight, and publishes only owner-private atomic state.',
|
|
55
|
+
tests: [
|
|
56
|
+
{
|
|
57
|
+
file: '__tests__/exchange-credentials.test.ts',
|
|
58
|
+
id: 'TEST-CLI-EXCHANGE-CACHE-EXACT-KEY-AND-PRIVATE-MODE',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
file: '__tests__/exchange-credentials.test.ts',
|
|
62
|
+
id: 'TEST-CLI-EXCHANGE-CACHE-SINGLEFLIGHT-CROSS-INSTANCE',
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
file: '__tests__/exchange-credentials.test.ts',
|
|
66
|
+
id: 'TEST-CLI-EXCHANGE-CACHE-REJECTS-STALE-OR-MISBOUND',
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
file: '__tests__/exchange-credentials.test.ts',
|
|
70
|
+
id: 'TEST-CLI-EXCHANGE-CACHE-LIFECYCLE-INVALIDATION',
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
})
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineLayout } from '@astrale-os/spec/authoring'
|
|
2
|
+
|
|
3
|
+
export default defineLayout({
|
|
4
|
+
entries: [
|
|
5
|
+
'__tests__/',
|
|
6
|
+
'exchange-credentials.ts',
|
|
7
|
+
'files.ts',
|
|
8
|
+
'identities.ts',
|
|
9
|
+
'index.ts',
|
|
10
|
+
'paths.ts',
|
|
11
|
+
'tsconfig.json',
|
|
12
|
+
],
|
|
13
|
+
exact: true,
|
|
14
|
+
})
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { mkdtemp, readFile, rm, stat } from 'node:fs/promises'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
import { ExchangeCredentialCache } from '../exchange-credentials'
|
|
7
|
+
|
|
8
|
+
let directory: string
|
|
9
|
+
let path: string
|
|
10
|
+
|
|
11
|
+
beforeEach(async () => {
|
|
12
|
+
directory = await mkdtemp(join(tmpdir(), 'astrale-exchange-cache-'))
|
|
13
|
+
path = join(directory, 'private', 'credentials.json')
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
afterEach(async () => {
|
|
17
|
+
await rm(directory, { recursive: true, force: true })
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
describe('exchange credential cache', () => {
|
|
21
|
+
/** @evidence TEST-CLI-EXCHANGE-CACHE-EXACT-KEY-AND-PRIVATE-MODE */
|
|
22
|
+
test('partitions by Kernel, Domain, and User under private filesystem modes', async () => {
|
|
23
|
+
const cache = new ExchangeCredentialCache(path)
|
|
24
|
+
const now = () => 100
|
|
25
|
+
const first = key('https://kernel-a.example', 'https://domain.example', 'user-a')
|
|
26
|
+
const second = key('https://kernel-a.example', 'https://domain.example', 'user-b')
|
|
27
|
+
let refreshes = 0
|
|
28
|
+
|
|
29
|
+
const resolve = (candidate: typeof first) =>
|
|
30
|
+
cache.getOrRefresh(
|
|
31
|
+
candidate,
|
|
32
|
+
async () => {
|
|
33
|
+
refreshes += 1
|
|
34
|
+
return {
|
|
35
|
+
credential: token(candidate, 200),
|
|
36
|
+
expiresAt: 200,
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
now,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
await expect(resolve(first)).resolves.toBe(token(first, 200))
|
|
43
|
+
await expect(resolve(first)).resolves.toBe(token(first, 200))
|
|
44
|
+
await expect(resolve(second)).resolves.toBe(token(second, 200))
|
|
45
|
+
expect(refreshes).toBe(2)
|
|
46
|
+
expect((await stat(path)).mode & 0o777).toBe(0o600)
|
|
47
|
+
expect((await stat(join(directory, 'private'))).mode & 0o777).toBe(0o700)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
/** @evidence TEST-CLI-EXCHANGE-CACHE-SINGLEFLIGHT-CROSS-INSTANCE */
|
|
51
|
+
test('singleflights refreshes in-process and through the cross-process file lock', async () => {
|
|
52
|
+
const left = new ExchangeCredentialCache(path)
|
|
53
|
+
const right = new ExchangeCredentialCache(path)
|
|
54
|
+
const candidate = key('https://kernel.example', 'https://domain.example', 'user')
|
|
55
|
+
let refreshes = 0
|
|
56
|
+
const refresh = async () => {
|
|
57
|
+
refreshes += 1
|
|
58
|
+
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
59
|
+
return {
|
|
60
|
+
credential: token(candidate, 200),
|
|
61
|
+
expiresAt: 200,
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const values = await Promise.all([
|
|
66
|
+
left.getOrRefresh(candidate, refresh, () => 100),
|
|
67
|
+
left.getOrRefresh(candidate, refresh, () => 100),
|
|
68
|
+
right.getOrRefresh(candidate, refresh, () => 100),
|
|
69
|
+
])
|
|
70
|
+
expect(new Set(values).size).toBe(1)
|
|
71
|
+
expect(refreshes).toBe(1)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
/** @evidence TEST-CLI-EXCHANGE-CACHE-REJECTS-STALE-OR-MISBOUND */
|
|
75
|
+
test('removes stale entries and rejects a freshly returned misbound token', async () => {
|
|
76
|
+
const cache = new ExchangeCredentialCache(path)
|
|
77
|
+
const candidate = key('https://kernel.example', 'https://domain.example', 'user')
|
|
78
|
+
await cache.getOrRefresh(
|
|
79
|
+
candidate,
|
|
80
|
+
async () => ({
|
|
81
|
+
credential: token(candidate, 120),
|
|
82
|
+
expiresAt: 120,
|
|
83
|
+
}),
|
|
84
|
+
() => 50,
|
|
85
|
+
)
|
|
86
|
+
let refreshes = 0
|
|
87
|
+
await cache.getOrRefresh(
|
|
88
|
+
candidate,
|
|
89
|
+
async () => {
|
|
90
|
+
refreshes += 1
|
|
91
|
+
return {
|
|
92
|
+
credential: token(candidate, 220),
|
|
93
|
+
expiresAt: 220,
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
() => 100,
|
|
97
|
+
)
|
|
98
|
+
expect(refreshes).toBe(1)
|
|
99
|
+
|
|
100
|
+
await expect(
|
|
101
|
+
cache.getOrRefresh(
|
|
102
|
+
key('https://other-kernel.example', candidate.domainIssuer, candidate.user),
|
|
103
|
+
async () => ({
|
|
104
|
+
credential: token(candidate, 220),
|
|
105
|
+
expiresAt: 220,
|
|
106
|
+
}),
|
|
107
|
+
() => 100,
|
|
108
|
+
),
|
|
109
|
+
).rejects.toThrow(/inconsistent with its cache key/i)
|
|
110
|
+
|
|
111
|
+
await expect(
|
|
112
|
+
cache.getOrRefresh(
|
|
113
|
+
key(candidate.kernelIssuer, candidate.domainIssuer, 'other-user'),
|
|
114
|
+
async () => ({ credential: token(candidate, 220), expiresAt: 220 }),
|
|
115
|
+
() => 100,
|
|
116
|
+
),
|
|
117
|
+
).rejects.toThrow(/inconsistent with its cache key/i)
|
|
118
|
+
|
|
119
|
+
for (const malformed of ['outer-delegation', 'proof-without-delegation'] as const) {
|
|
120
|
+
const malformedCandidate = key(
|
|
121
|
+
candidate.kernelIssuer,
|
|
122
|
+
candidate.domainIssuer,
|
|
123
|
+
`user-${malformed}`,
|
|
124
|
+
)
|
|
125
|
+
await expect(
|
|
126
|
+
cache.getOrRefresh(
|
|
127
|
+
malformedCandidate,
|
|
128
|
+
async () => ({
|
|
129
|
+
credential: token(malformedCandidate, 220, malformed),
|
|
130
|
+
expiresAt: 220,
|
|
131
|
+
}),
|
|
132
|
+
() => 100,
|
|
133
|
+
),
|
|
134
|
+
).rejects.toThrow(/inconsistent with its cache key/i)
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
/** @evidence TEST-CLI-EXCHANGE-CACHE-LIFECYCLE-INVALIDATION */
|
|
139
|
+
test('invalidates one Kernel or the entire cache', async () => {
|
|
140
|
+
const cache = new ExchangeCredentialCache(path)
|
|
141
|
+
const a = key('https://kernel-a.example', 'https://domain.example', 'user')
|
|
142
|
+
const b = key('https://kernel-b.example', 'https://domain.example', 'user')
|
|
143
|
+
for (const candidate of [a, b]) {
|
|
144
|
+
await cache.getOrRefresh(
|
|
145
|
+
candidate,
|
|
146
|
+
async () => ({
|
|
147
|
+
credential: token(candidate, 200),
|
|
148
|
+
expiresAt: 200,
|
|
149
|
+
}),
|
|
150
|
+
() => 100,
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
await cache.deleteKernel(a.kernelIssuer)
|
|
154
|
+
expect(await readFile(path, 'utf8')).not.toContain(a.kernelIssuer)
|
|
155
|
+
expect(await readFile(path, 'utf8')).toContain(b.kernelIssuer)
|
|
156
|
+
await cache.clear()
|
|
157
|
+
expect(JSON.parse(await readFile(path, 'utf8')).entries).toEqual({})
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
function key(kernelIssuer: string, domainIssuer: string, user: string) {
|
|
162
|
+
return { kernelIssuer, domainIssuer, user }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function token(
|
|
166
|
+
candidate: ReturnType<typeof key>,
|
|
167
|
+
exp: number,
|
|
168
|
+
malformed?: 'outer-delegation' | 'proof-without-delegation',
|
|
169
|
+
): string {
|
|
170
|
+
const encode = (value: unknown) => Buffer.from(JSON.stringify(value)).toString('base64url')
|
|
171
|
+
const proof = `${encode({ alg: 'EdDSA', typ: 'JWT' })}.${encode({
|
|
172
|
+
iss: candidate.kernelIssuer,
|
|
173
|
+
sub: candidate.user,
|
|
174
|
+
aud: candidate.kernelIssuer,
|
|
175
|
+
exp,
|
|
176
|
+
...(malformed === 'proof-without-delegation'
|
|
177
|
+
? {}
|
|
178
|
+
: { delegation: { v: 1, expr: { kind: 'identity', id: candidate.user } } }),
|
|
179
|
+
})}.signature`
|
|
180
|
+
return `${encode({ alg: 'EdDSA', typ: 'JWT' })}.${encode({
|
|
181
|
+
iss: candidate.domainIssuer,
|
|
182
|
+
sub: 'domain',
|
|
183
|
+
aud: candidate.kernelIssuer,
|
|
184
|
+
exp,
|
|
185
|
+
grant: { v: 1, expr: { kind: 'identity', credential: proof } },
|
|
186
|
+
...(malformed === 'outer-delegation' ? { delegation: { credential: proof } } : {}),
|
|
187
|
+
})}.signature`
|
|
188
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import {
|
|
3
|
+
access,
|
|
4
|
+
mkdir,
|
|
5
|
+
mkdtemp,
|
|
6
|
+
readdir,
|
|
7
|
+
readFile,
|
|
8
|
+
rm,
|
|
9
|
+
stat,
|
|
10
|
+
utimes,
|
|
11
|
+
writeFile,
|
|
12
|
+
} from 'node:fs/promises'
|
|
13
|
+
import { tmpdir } from 'node:os'
|
|
14
|
+
import { join } from 'node:path'
|
|
15
|
+
|
|
16
|
+
import { atomicWrite, withFileLock } from '../files'
|
|
17
|
+
|
|
18
|
+
let temporaryDirectory: string
|
|
19
|
+
|
|
20
|
+
beforeEach(async () => {
|
|
21
|
+
temporaryDirectory = await mkdtemp(join(tmpdir(), 'astrale-state-files-'))
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
afterEach(async () => {
|
|
25
|
+
await rm(temporaryDirectory, { recursive: true, force: true })
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
describe('atomicWrite', () => {
|
|
29
|
+
/** @evidence TEST-CLI-STATE-ATOMIC-WRITE */
|
|
30
|
+
test('publishes a synced mode-0600 file without temporary residue', async () => {
|
|
31
|
+
const path = join(temporaryDirectory, 'nested', 'state.json')
|
|
32
|
+
|
|
33
|
+
await atomicWrite(path, '{"value":1}')
|
|
34
|
+
|
|
35
|
+
expect(await readFile(path, 'utf-8')).toBe('{"value":1}')
|
|
36
|
+
expect((await stat(path)).mode & 0o777).toBe(0o600)
|
|
37
|
+
expect(await readdir(join(temporaryDirectory, 'nested'))).toEqual(['state.json'])
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
/** @evidence TEST-CLI-STATE-ATOMIC-WRITE-FAILURE */
|
|
41
|
+
test('does not disturb the target or retain a temporary file after failure', async () => {
|
|
42
|
+
const path = join(temporaryDirectory, 'target')
|
|
43
|
+
await mkdir(path)
|
|
44
|
+
await writeFile(join(path, 'retained'), 'stable')
|
|
45
|
+
|
|
46
|
+
await expect(atomicWrite(path, 'replacement')).rejects.toThrow()
|
|
47
|
+
|
|
48
|
+
expect(await readFile(join(path, 'retained'), 'utf-8')).toBe('stable')
|
|
49
|
+
expect((await readdir(temporaryDirectory)).sort()).toEqual(['target'])
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe('withFileLock', () => {
|
|
54
|
+
/** @evidence TEST-CLI-STATE-LOCK-SERIALIZES */
|
|
55
|
+
test('serializes contending read-modify-write transitions', async () => {
|
|
56
|
+
const lockPath = join(temporaryDirectory, 'counter.lock')
|
|
57
|
+
const counterPath = join(temporaryDirectory, 'counter')
|
|
58
|
+
await writeFile(counterPath, '0')
|
|
59
|
+
|
|
60
|
+
const bump = () =>
|
|
61
|
+
withFileLock(
|
|
62
|
+
lockPath,
|
|
63
|
+
async () => {
|
|
64
|
+
const value = Number(await readFile(counterPath, 'utf-8'))
|
|
65
|
+
await new Promise((resolve) => setTimeout(resolve, 2))
|
|
66
|
+
await writeFile(counterPath, String(value + 1))
|
|
67
|
+
},
|
|
68
|
+
{ pollIntervalMs: 2 },
|
|
69
|
+
)
|
|
70
|
+
await Promise.all(Array.from({ length: 20 }, bump))
|
|
71
|
+
|
|
72
|
+
expect(await readFile(counterPath, 'utf-8')).toBe('20')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
/** @evidence TEST-CLI-STATE-LOCK-RECOVERS */
|
|
76
|
+
test('recovers stale and dead-owner locks', async () => {
|
|
77
|
+
const stalePath = join(temporaryDirectory, 'stale.lock')
|
|
78
|
+
await writeFile(stalePath, JSON.stringify({ pid: process.pid }))
|
|
79
|
+
const past = new Date(Date.now() - 60_000)
|
|
80
|
+
await utimes(stalePath, past, past)
|
|
81
|
+
await expect(withFileLock(stalePath, async () => 'stale', { pollIntervalMs: 2 })).resolves.toBe(
|
|
82
|
+
'stale',
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
const deadPath = join(temporaryDirectory, 'dead.lock')
|
|
86
|
+
await writeFile(deadPath, JSON.stringify({ pid: 99_999_999 }))
|
|
87
|
+
await expect(withFileLock(deadPath, async () => 'dead', { pollIntervalMs: 2 })).resolves.toBe(
|
|
88
|
+
'dead',
|
|
89
|
+
)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
/** @evidence TEST-CLI-STATE-LOCK-RELEASES-AND-BOUNDS */
|
|
93
|
+
test('releases after failure and bounds acquisition behind a live owner', async () => {
|
|
94
|
+
const releasedPath = join(temporaryDirectory, 'released.lock')
|
|
95
|
+
await expect(
|
|
96
|
+
withFileLock(releasedPath, async () => {
|
|
97
|
+
throw new Error('transition failed')
|
|
98
|
+
}),
|
|
99
|
+
).rejects.toThrow('transition failed')
|
|
100
|
+
await expect(access(releasedPath)).rejects.toThrow()
|
|
101
|
+
await expect(withFileLock(releasedPath, async () => 'next')).resolves.toBe('next')
|
|
102
|
+
|
|
103
|
+
const heldPath = join(temporaryDirectory, 'held.lock')
|
|
104
|
+
await writeFile(heldPath, JSON.stringify({ pid: process.pid }))
|
|
105
|
+
await expect(
|
|
106
|
+
withFileLock(heldPath, async () => 'unreachable', {
|
|
107
|
+
pollIntervalMs: 10,
|
|
108
|
+
staleAfterMs: 60_000,
|
|
109
|
+
timeoutMs: 40,
|
|
110
|
+
}),
|
|
111
|
+
).rejects.toThrow('Timed out')
|
|
112
|
+
})
|
|
113
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { updateIdentityStore } from '../../identities'
|
|
2
|
+
|
|
3
|
+
const [action, name, argument] = process.argv.slice(2)
|
|
4
|
+
|
|
5
|
+
if (action === 'default' && name !== undefined) {
|
|
6
|
+
await updateIdentityStore((current) => {
|
|
7
|
+
if (!current.identities[name]) throw new Error(`Identity "${name}" not found`)
|
|
8
|
+
return { next: { ...current, default: name }, value: undefined }
|
|
9
|
+
})
|
|
10
|
+
} else if (action === 'registration' && name !== undefined && argument !== undefined) {
|
|
11
|
+
await updateIdentityStore((current) => {
|
|
12
|
+
const identity = current.identities[name]
|
|
13
|
+
if (!identity) throw new Error(`Identity "${name}" not found`)
|
|
14
|
+
return {
|
|
15
|
+
next: {
|
|
16
|
+
...current,
|
|
17
|
+
identities: {
|
|
18
|
+
...current.identities,
|
|
19
|
+
[name]: {
|
|
20
|
+
...identity,
|
|
21
|
+
registrations: {
|
|
22
|
+
...identity.registrations,
|
|
23
|
+
[argument]: {
|
|
24
|
+
iss: `https://${argument}.example.test`,
|
|
25
|
+
sub: `${name}-${argument}`,
|
|
26
|
+
registeredAt: '2025-01-01T00:00:00.000Z',
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
value: undefined,
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
} else {
|
|
36
|
+
throw new Error('Invalid identity-transition fixture arguments')
|
|
37
|
+
}
|