anhedral 0.21.0 → 0.22.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/assets/contracts/deployment-provenance.json +5 -5
- package/assets/launch/router.tsx.txt +1 -0
- package/dist/cli-commands.js +5 -17
- package/dist/cli-errors.js +1 -1
- package/dist/command-schema.d.ts +1 -0
- package/dist/command-schema.js +14 -0
- package/dist/completion.js +1 -1
- package/dist/{capability-command.d.ts → composition/command.d.ts} +3 -3
- package/dist/{capability-command.js → composition/command.js} +12 -12
- package/dist/{composition.d.ts → composition/composition.d.ts} +1 -1
- package/dist/{composition.js → composition/composition.js} +2 -2
- package/dist/{capability-editor.js → composition/editor.js} +1 -1
- package/dist/{capability-recipes.d.ts → composition/recipes.d.ts} +2 -2
- package/dist/{capability-recipes.js → composition/recipes.js} +7 -7
- package/dist/{capability-references.js → composition/references.js} +2 -2
- package/dist/{stack-update.d.ts → composition/update.d.ts} +3 -3
- package/dist/{stack-update.js → composition/update.js} +11 -11
- package/dist/contracts/cli-transport.d.ts +1 -8
- package/dist/contracts/cli-transport.js +0 -11
- package/dist/contracts/deployment.d.ts +4 -65
- package/dist/contracts/deployment.js +9 -60
- package/dist/contracts/production-transport.d.ts +2 -2
- package/dist/contracts/staging-transport.d.ts +2 -2
- package/dist/launch.js +5 -5
- package/dist/platform/auth.d.ts +14 -0
- package/dist/platform/auth.js +62 -0
- package/dist/{platform-command.d.ts → platform/command.d.ts} +2 -2
- package/dist/platform/command.js +279 -0
- package/dist/{credential-helper.js → platform/credential-helper.js} +1 -1
- package/dist/platform/deploy.d.ts +2 -0
- package/dist/platform/deploy.js +158 -0
- package/dist/{platform-environment.d.ts → platform/environment.d.ts} +1 -1
- package/dist/{platform-environment.js → platform/environment.js} +7 -7
- package/dist/platform/http.d.ts +7 -0
- package/dist/platform/http.js +105 -0
- package/dist/{platform-operations.d.ts → platform/operations.d.ts} +1 -1
- package/dist/{platform-operations.js → platform/operations.js} +5 -5
- package/dist/platform/output.d.ts +2 -0
- package/dist/platform/output.js +79 -0
- package/dist/{platform-production.d.ts → platform/production.d.ts} +1 -1
- package/dist/{platform-production.js → platform/production.js} +4 -4
- package/dist/platform/projects.d.ts +12 -0
- package/dist/platform/projects.js +125 -0
- package/dist/{platform-source.js → platform/source.js} +7 -7
- package/dist/platform/state-schema.d.ts +25 -0
- package/dist/platform/state-schema.js +11 -0
- package/dist/{platform-state.d.ts → platform/state.d.ts} +1 -1
- package/dist/{platform-state.js → platform/state.js} +3 -4
- package/dist/platform/types.d.ts +26 -0
- package/dist/platform/upload.d.ts +2 -0
- package/dist/platform/upload.js +60 -0
- package/dist/process/runner.d.ts +1 -0
- package/dist/{command-runner.js → process/runner.js} +1 -1
- package/dist/registry/usage.d.ts +1 -0
- package/dist/{platforms → scaffold}/tanstack.d.ts +2 -2
- package/dist/{platforms → scaffold}/tanstack.js +2 -2
- package/dist/transactions/durable-files.d.ts +8 -0
- package/dist/transactions/durable-files.js +132 -0
- package/dist/transactions/journal.d.ts +4 -0
- package/dist/transactions/journal.js +141 -0
- package/dist/{transaction-lock.js → transactions/lock.js} +2 -2
- package/dist/transactions/model.d.ts +18 -0
- package/dist/transactions/model.js +4 -0
- package/dist/transactions/paths.d.ts +12 -0
- package/dist/transactions/paths.js +155 -0
- package/dist/transactions/recovery.d.ts +7 -0
- package/dist/transactions/recovery.js +132 -0
- package/dist/{transaction.d.ts → transactions/transaction.d.ts} +1 -2
- package/dist/transactions/transaction.js +179 -0
- package/dist/util.js +2 -2
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/software-factory.md +45 -8
- package/package.json +19 -19
- package/dist/platform-command.js +0 -787
- package/dist/transaction.js +0 -716
- /package/dist/{capability-catalog.d.ts → composition/catalog.d.ts} +0 -0
- /package/dist/{capability-catalog.js → composition/catalog.js} +0 -0
- /package/dist/{capability-editor.d.ts → composition/editor.d.ts} +0 -0
- /package/dist/{capability-references.d.ts → composition/references.d.ts} +0 -0
- /package/dist/{credential-helper.d.ts → platform/credential-helper.d.ts} +0 -0
- /package/dist/{platform-source.d.ts → platform/source.d.ts} +0 -0
- /package/dist/{command-runner.d.ts → platform/types.js} +0 -0
- /package/dist/{command.d.ts → process/resolve-command.d.ts} +0 -0
- /package/dist/{command.js → process/resolve-command.js} +0 -0
- /package/dist/{transaction-lock.d.ts → transactions/lock.d.ts} +0 -0
|
@@ -19,8 +19,8 @@ export declare const stagingDeploymentSchema: z.ZodObject<{
|
|
|
19
19
|
preparing: "preparing";
|
|
20
20
|
ready: "ready";
|
|
21
21
|
failed: "failed";
|
|
22
|
-
superseded: "superseded";
|
|
23
22
|
deployed: "deployed";
|
|
23
|
+
superseded: "superseded";
|
|
24
24
|
}>;
|
|
25
25
|
summary: z.ZodNullable<z.ZodString>;
|
|
26
26
|
sourceRevision: z.ZodString;
|
|
@@ -48,8 +48,8 @@ export declare const stagingStateSchema: z.ZodObject<{
|
|
|
48
48
|
preparing: "preparing";
|
|
49
49
|
ready: "ready";
|
|
50
50
|
failed: "failed";
|
|
51
|
-
superseded: "superseded";
|
|
52
51
|
deployed: "deployed";
|
|
52
|
+
superseded: "superseded";
|
|
53
53
|
}>;
|
|
54
54
|
summary: z.ZodNullable<z.ZodString>;
|
|
55
55
|
sourceRevision: z.ZodString;
|
package/dist/launch.js
CHANGED
|
@@ -2,14 +2,14 @@ import { existsSync, lstatSync, mkdirSync, readdirSync, realpathSync, rmdirSync
|
|
|
2
2
|
import { randomUUID } from 'node:crypto';
|
|
3
3
|
import { spawnSync } from 'node:child_process';
|
|
4
4
|
import path from 'node:path';
|
|
5
|
-
import { scaffoldTanstack } from './
|
|
5
|
+
import { scaffoldTanstack } from './scaffold/tanstack.js';
|
|
6
6
|
import { bootstrapWorkspace, assertWorkspaceRuntime } from './scaffold/bootstrap.js';
|
|
7
7
|
import { scaffoldStyling } from './scaffold/styling.js';
|
|
8
8
|
import { json, put, sharedPackage } from './scaffold/files.js';
|
|
9
|
-
import { CapabilityEditor } from './
|
|
10
|
-
import { addCapabilityRecipe, composeCapabilities } from './
|
|
11
|
-
import { launchCompositionSchema } from './composition.js';
|
|
12
|
-
import { runStagedTransaction } from './transaction.js';
|
|
9
|
+
import { CapabilityEditor } from './composition/editor.js';
|
|
10
|
+
import { addCapabilityRecipe, composeCapabilities } from './composition/recipes.js';
|
|
11
|
+
import { launchCompositionSchema } from './composition/composition.js';
|
|
12
|
+
import { runStagedTransaction } from './transactions/transaction.js';
|
|
13
13
|
import { configureDependencyPatches, writeDependencyAudit } from './dependency-patches.js';
|
|
14
14
|
import { setPnpmPolicy } from './pnpm-policy.js';
|
|
15
15
|
import { GENERATOR_VERSION } from './version.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { commandArguments } from '../cli-arguments.js';
|
|
2
|
+
import { createReporter } from '../cli-output.js';
|
|
3
|
+
import { type SecretStore } from './state.js';
|
|
4
|
+
type Request = (path: string, method: string, body?: unknown) => Promise<unknown>;
|
|
5
|
+
export declare function runLogin({ options, origin, reporter, store, readCredential, request, sleep, }: {
|
|
6
|
+
options: ReturnType<typeof commandArguments>;
|
|
7
|
+
origin: string;
|
|
8
|
+
reporter: ReturnType<typeof createReporter>;
|
|
9
|
+
store: SecretStore;
|
|
10
|
+
readCredential: () => Promise<unknown>;
|
|
11
|
+
request: Request;
|
|
12
|
+
sleep?: (ms: number) => Promise<void>;
|
|
13
|
+
}): Promise<void>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { commandArgumentError, commandArguments } from '../cli-arguments.js';
|
|
3
|
+
import { createReporter } from '../cli-output.js';
|
|
4
|
+
import { cliDeviceSchema, cliLoginSchema } from '../contracts/cli-transport.js';
|
|
5
|
+
import { platformFailure } from './state.js';
|
|
6
|
+
const pending = z
|
|
7
|
+
.object({ origin: z.string(), device: cliDeviceSchema, expiresAt: z.number() })
|
|
8
|
+
.strict();
|
|
9
|
+
export async function runLogin({ options, origin, reporter, store, readCredential, request, sleep, }) {
|
|
10
|
+
const emit = (result) => reporter.result(result);
|
|
11
|
+
if (options.positional.length)
|
|
12
|
+
throw commandArgumentError('login', 'Login does not accept positional arguments.');
|
|
13
|
+
let state = pending.safeParse(await readCredential());
|
|
14
|
+
if (!options.has('--resume') ||
|
|
15
|
+
!state.success ||
|
|
16
|
+
state.data.origin !== origin ||
|
|
17
|
+
state.data.expiresAt <= Date.now()) {
|
|
18
|
+
const device = cliDeviceSchema.parse(await request('/auth/code', 'POST', {}));
|
|
19
|
+
if (new URL(device.verification_uri_complete).origin !== origin ||
|
|
20
|
+
new URL(device.verification_uri_complete).protocol !== new URL(origin).protocol)
|
|
21
|
+
throw platformFailure('Platform returned an unexpected verification origin.');
|
|
22
|
+
const saved = { origin, device, expiresAt: Date.now() + device.expires_in * 1000 };
|
|
23
|
+
await store.write(saved);
|
|
24
|
+
state = pending.safeParse(saved);
|
|
25
|
+
}
|
|
26
|
+
if (!state.success)
|
|
27
|
+
throw platformFailure('Login state is unavailable. Start login again.');
|
|
28
|
+
const saved = state.data;
|
|
29
|
+
if (options.has('--start') || (!reporter.canPrompt && !options.has('--resume'))) {
|
|
30
|
+
emit({
|
|
31
|
+
status: 'authorization_required',
|
|
32
|
+
verificationUrl: saved.device.verification_uri_complete,
|
|
33
|
+
userCode: saved.device.user_code,
|
|
34
|
+
expiresAt: new Date(saved.expiresAt).toISOString(),
|
|
35
|
+
nextAction: 'Approve in your browser, then run anhedral login --resume with the same API origin and credential store.',
|
|
36
|
+
});
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
reporter.diagnostic(`Approve CLI access at ${saved.device.verification_uri_complete}\nConfirm code ${saved.device.user_code}.`);
|
|
40
|
+
let interval = saved.device.interval * 1000;
|
|
41
|
+
while (Date.now() < saved.expiresAt) {
|
|
42
|
+
await (sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms))))(interval);
|
|
43
|
+
const result = await request('/auth/token', 'POST', {
|
|
44
|
+
device_code: saved.device.device_code,
|
|
45
|
+
});
|
|
46
|
+
const grant = cliLoginSchema.safeParse(result);
|
|
47
|
+
if (grant.success) {
|
|
48
|
+
await store.write({ ...grant.data, origin });
|
|
49
|
+
emit({ status: 'authenticated', origin, expiresAt: grant.data.expiresAt });
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const error = result.error;
|
|
53
|
+
if (error === 'slow_down')
|
|
54
|
+
interval += 5000;
|
|
55
|
+
else if (error !== 'authorization_pending') {
|
|
56
|
+
await store.clear();
|
|
57
|
+
throw platformFailure('Device approval was denied or expired. Start login again.');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
await store.clear();
|
|
61
|
+
throw platformFailure('Device approval expired. Start login again.');
|
|
62
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type OutputOptions } from '
|
|
2
|
-
import { type SecretStore } from './
|
|
1
|
+
import { type OutputOptions } from '../cli-output.js';
|
|
2
|
+
import { type SecretStore } from './state.js';
|
|
3
3
|
interface PlatformDependencies {
|
|
4
4
|
store?: SecretStore;
|
|
5
5
|
fetch?: typeof fetch;
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { unlink } from 'node:fs/promises';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { CliArgumentError, commandArgumentError, commandArguments } from '../cli-arguments.js';
|
|
6
|
+
import { CliFailure } from '../cli-errors.js';
|
|
7
|
+
import { createReporter } from '../cli-output.js';
|
|
8
|
+
import { cliLinkSchema, cliLoginSchema } from '../contracts/cli-transport.js';
|
|
9
|
+
import { runLogin } from './auth.js';
|
|
10
|
+
import { runDeploy } from './deploy.js';
|
|
11
|
+
import { runEnvironmentCommand, validateEnvironmentArguments } from './environment.js';
|
|
12
|
+
import { createPlatformRequest } from './http.js';
|
|
13
|
+
import { runLogsCommand, runStagingCommand, validateLogsArguments, validateStagingArguments, } from './operations.js';
|
|
14
|
+
import { formatPlatformResult } from './output.js';
|
|
15
|
+
import { runProductionCommand, validateProductionArguments } from './production.js';
|
|
16
|
+
import { runProjects, validateProjects } from './projects.js';
|
|
17
|
+
import { packageSource } from './source.js';
|
|
18
|
+
import { linkSchema } from './state-schema.js';
|
|
19
|
+
import { platformFailure as createPlatformFailure, platformOrigin, readPrivate, secretStore, withProjectLock, writePrivate, } from './state.js';
|
|
20
|
+
import { runSourceUpload } from './upload.js';
|
|
21
|
+
const credential = cliLoginSchema.extend({ origin: z.string() });
|
|
22
|
+
const hash = (bytes) => createHash('sha256').update(bytes).digest('hex');
|
|
23
|
+
export async function runPlatformCommand(command, args, output, dependencies = {}) {
|
|
24
|
+
try {
|
|
25
|
+
await runPlatformOperation(command, args, output, dependencies);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (error instanceof CliArgumentError || error instanceof CliFailure)
|
|
29
|
+
throw error;
|
|
30
|
+
if (error instanceof z.ZodError)
|
|
31
|
+
throw createPlatformFailure('Platform data or saved context does not match the supported protocol. Preserve saved state and verify the selected context before retrying.', `Run anhedral ${command} --help.`, 'context');
|
|
32
|
+
if (error instanceof SyntaxError)
|
|
33
|
+
throw createPlatformFailure('Selected local Platform input is not valid JSON. Preserve saved state and review the selected input before retrying.', `Run anhedral ${command} --help.`, 'context');
|
|
34
|
+
if (['ENOENT', 'ENOTDIR', 'EISDIR', 'EACCES', 'EPERM'].includes(error?.code ?? ''))
|
|
35
|
+
throw createPlatformFailure('A required local Platform file is missing or inaccessible. Review the selected working directory, manifest and credential store.', `Run anhedral ${command} --help.`, 'context');
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function runPlatformOperation(command, args, output, dependencies) {
|
|
40
|
+
const reporter = createReporter(output, (result) => formatPlatformResult(command, result), true);
|
|
41
|
+
const platformFailure = (message, category = 'context', retryable = false, nextAction = `Run anhedral ${command} --help.`) => createPlatformFailure(message, nextAction, category, retryable);
|
|
42
|
+
const options = commandArguments(command, args), origin = platformOrigin(options.value('--api-url'));
|
|
43
|
+
if (command === 'env')
|
|
44
|
+
validateEnvironmentArguments(args);
|
|
45
|
+
if (command === 'staging')
|
|
46
|
+
validateStagingArguments(args);
|
|
47
|
+
if (command === 'production')
|
|
48
|
+
validateProductionArguments(args);
|
|
49
|
+
if (command === 'logs')
|
|
50
|
+
validateLogsArguments(args);
|
|
51
|
+
const emit = (result) => reporter.result(result);
|
|
52
|
+
if (['login', 'logout', 'whoami', 'link', 'unlink'].includes(command) &&
|
|
53
|
+
options.positional.length)
|
|
54
|
+
throw commandArgumentError(command, 'Unexpected positional arguments.');
|
|
55
|
+
if (command === 'login' && options.has('--start') && options.has('--resume'))
|
|
56
|
+
throw commandArgumentError(command, 'Choose --start or --resume.');
|
|
57
|
+
if (command === 'source' &&
|
|
58
|
+
(options.positional.length !== 1 || !['upload', 'status'].includes(options.positional[0])))
|
|
59
|
+
throw commandArgumentError(command, 'Use source upload or source status.');
|
|
60
|
+
if (command === 'deploy' &&
|
|
61
|
+
(options.positional.length !== 1 ||
|
|
62
|
+
!['plan', 'prepare', 'status'].includes(options.positional[0])))
|
|
63
|
+
throw commandArgumentError(command, 'Use deploy plan, deploy prepare or deploy status.');
|
|
64
|
+
if (command === 'deploy' &&
|
|
65
|
+
options.positional[0] !== 'plan' &&
|
|
66
|
+
(options.has('--manifest') || options.has('--new')))
|
|
67
|
+
throw commandArgumentError(command, 'Manifest and --new options apply to deploy plan only.');
|
|
68
|
+
if (command === 'link' &&
|
|
69
|
+
(!z.uuid().safeParse(options.value('--project')).success ||
|
|
70
|
+
!z.uuid().safeParse(options.value('--team')).success))
|
|
71
|
+
throw commandArgumentError(command, 'Supply existing project and team UUIDs with --project and --team.');
|
|
72
|
+
const projectInput = command === 'projects' ? validateProjects(options) : undefined;
|
|
73
|
+
if (command === 'projects' && projectInput && options.has('--dry-run')) {
|
|
74
|
+
emit({
|
|
75
|
+
status: 'dry_run',
|
|
76
|
+
...projectInput,
|
|
77
|
+
teamId: options.value('--team') ?? null,
|
|
78
|
+
link: options.has('--link'),
|
|
79
|
+
initialization: 'source-upload',
|
|
80
|
+
});
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const root = process.cwd(), stateDir = join(root, '.anhedral', 'platform'), linkPath = join(stateDir, 'project.json'), uploadPath = join(stateDir, 'upload.json');
|
|
84
|
+
if (command === 'unlink') {
|
|
85
|
+
if ((await readPrivate(linkPath)) === null) {
|
|
86
|
+
emit({ status: 'unlinked' });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
await withProjectLock(stateDir, async () => {
|
|
90
|
+
await readPrivate(linkPath);
|
|
91
|
+
await unlink(linkPath).catch((e) => {
|
|
92
|
+
if (e.code !== 'ENOENT')
|
|
93
|
+
throw e;
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
emit({ status: 'unlinked' });
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (command === 'env' && options.has('--dry-run'))
|
|
100
|
+
return runEnvironmentCommand(args, output);
|
|
101
|
+
if (command === 'source' && options.has('--dry-run')) {
|
|
102
|
+
if (options.positional[0] !== 'upload')
|
|
103
|
+
throw commandArgumentError(command, 'Use source upload --dry-run.');
|
|
104
|
+
const source = await packageSource(root), archiveDigest = hash(source.archive);
|
|
105
|
+
emit({
|
|
106
|
+
status: 'dry_run',
|
|
107
|
+
archiveDigest,
|
|
108
|
+
inventoryDigest: source.inventoryDigest,
|
|
109
|
+
files: source.files,
|
|
110
|
+
includedPaths: source.paths,
|
|
111
|
+
bytes: source.bytes,
|
|
112
|
+
archiveBytes: source.archive.length,
|
|
113
|
+
});
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const store = dependencies.store ??
|
|
117
|
+
secretStore(origin, options.value('--credential-file') ? resolve(options.value('--credential-file')) : undefined);
|
|
118
|
+
const readCredential = async () => {
|
|
119
|
+
try {
|
|
120
|
+
return await store.read();
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
if (error instanceof SyntaxError)
|
|
124
|
+
throw platformFailure('Saved credential data is not valid JSON. Preserve it and reconcile the selected credential store.', 'auth', false, 'Run anhedral login --help.');
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const request = createPlatformRequest({
|
|
129
|
+
command,
|
|
130
|
+
operation: options.positional[0],
|
|
131
|
+
origin,
|
|
132
|
+
fetch: dependencies.fetch,
|
|
133
|
+
});
|
|
134
|
+
if (command === 'login')
|
|
135
|
+
return runLogin({
|
|
136
|
+
options,
|
|
137
|
+
origin,
|
|
138
|
+
reporter,
|
|
139
|
+
store,
|
|
140
|
+
readCredential,
|
|
141
|
+
request,
|
|
142
|
+
sleep: dependencies.sleep,
|
|
143
|
+
});
|
|
144
|
+
const saved = credential.safeParse(await readCredential());
|
|
145
|
+
if (!saved.success || saved.data.origin !== origin)
|
|
146
|
+
throw platformFailure('Log in to the selected API origin first.', 'auth', false, 'Run anhedral login with the same API origin and credential store.');
|
|
147
|
+
const token = saved.data.token;
|
|
148
|
+
if (command === 'logout') {
|
|
149
|
+
await request('/auth/logout', 'POST', {}, token);
|
|
150
|
+
try {
|
|
151
|
+
await store.clear();
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
throw platformFailure('Platform access was revoked, but the saved local credential could not be removed.', 'context', false, 'Remove the saved credential from the selected credential store before signing in again.');
|
|
155
|
+
}
|
|
156
|
+
emit({ status: 'logged_out', origin });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const actor = z
|
|
160
|
+
.object({
|
|
161
|
+
protocol: z.literal(1),
|
|
162
|
+
principalId: z.uuid(),
|
|
163
|
+
name: z.string(),
|
|
164
|
+
teamId: z.uuid().nullable(),
|
|
165
|
+
})
|
|
166
|
+
.parse(await request('/actor', 'GET', undefined, token));
|
|
167
|
+
if (command === 'whoami') {
|
|
168
|
+
const identity = { ...actor, origin, expiresAt: saved.data.expiresAt };
|
|
169
|
+
if (!options.has('--context')) {
|
|
170
|
+
emit(identity);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const savedLink = await readPrivate(linkPath);
|
|
174
|
+
const localLink = savedLink === null ? null : linkSchema.safeParse(savedLink);
|
|
175
|
+
if (localLink &&
|
|
176
|
+
(!localLink.success ||
|
|
177
|
+
localLink.data.origin !== origin ||
|
|
178
|
+
localLink.data.principalId !== actor.principalId))
|
|
179
|
+
throw platformFailure('The saved project link does not match the selected API origin and signed-in identity. Preserve it and explicitly link the intended project.', 'context', false, 'Run anhedral link --help.');
|
|
180
|
+
const file = options.value('--credential-file');
|
|
181
|
+
emit({
|
|
182
|
+
...identity,
|
|
183
|
+
context: {
|
|
184
|
+
apiOrigin: { value: origin, source: options.has('--api-url') ? 'flag' : 'default' },
|
|
185
|
+
identityVerification: 'live-platform',
|
|
186
|
+
credential: {
|
|
187
|
+
source: file ? 'flag' : dependencies.store ? 'injected' : 'os-secret-store',
|
|
188
|
+
kind: file
|
|
189
|
+
? 'owner-only-file'
|
|
190
|
+
: dependencies.store
|
|
191
|
+
? 'injected'
|
|
192
|
+
: process.platform === 'darwin'
|
|
193
|
+
? 'macos-keychain'
|
|
194
|
+
: process.platform === 'linux'
|
|
195
|
+
? 'linux-secret-service'
|
|
196
|
+
: 'unsupported',
|
|
197
|
+
},
|
|
198
|
+
project: localLink && localLink.success
|
|
199
|
+
? {
|
|
200
|
+
projectId: localLink.data.projectId,
|
|
201
|
+
teamId: localLink.data.teamId,
|
|
202
|
+
name: localLink.data.name,
|
|
203
|
+
source: '.anhedral/platform/project.json',
|
|
204
|
+
verification: 'saved-local-link',
|
|
205
|
+
access: 'not-verified',
|
|
206
|
+
}
|
|
207
|
+
: null,
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (command === 'projects')
|
|
213
|
+
return runProjects({
|
|
214
|
+
options,
|
|
215
|
+
origin,
|
|
216
|
+
actor,
|
|
217
|
+
token,
|
|
218
|
+
stateDir,
|
|
219
|
+
linkPath,
|
|
220
|
+
request,
|
|
221
|
+
emit,
|
|
222
|
+
platformFailure,
|
|
223
|
+
projectInput,
|
|
224
|
+
});
|
|
225
|
+
if (command === 'link') {
|
|
226
|
+
await withProjectLock(stateDir, async () => {
|
|
227
|
+
const input = z
|
|
228
|
+
.object({ projectId: z.uuid(), teamId: z.uuid() })
|
|
229
|
+
.parse({ projectId: options.value('--project'), teamId: options.value('--team') });
|
|
230
|
+
if (options.positional.length)
|
|
231
|
+
throw commandArgumentError(command, 'Use --project and --team.');
|
|
232
|
+
const link = cliLinkSchema.parse(await request('/link', 'POST', input, token, input.teamId));
|
|
233
|
+
if (link.principalId !== actor.principalId ||
|
|
234
|
+
link.projectId !== input.projectId ||
|
|
235
|
+
link.teamId !== input.teamId)
|
|
236
|
+
throw platformFailure('Project link identity differs.');
|
|
237
|
+
await writePrivate(linkPath, { ...link, origin });
|
|
238
|
+
emit({ ...link, origin });
|
|
239
|
+
});
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (['env', 'staging', 'production', 'logs'].includes(command)) {
|
|
243
|
+
const link = linkSchema.parse(await readPrivate(linkPath));
|
|
244
|
+
if (link.origin !== origin || link.principalId !== actor.principalId)
|
|
245
|
+
throw platformFailure('Link this project to the selected API origin and signed-in identity.');
|
|
246
|
+
const operation = command === 'env'
|
|
247
|
+
? runEnvironmentCommand
|
|
248
|
+
: command === 'staging'
|
|
249
|
+
? runStagingCommand
|
|
250
|
+
: command === 'production'
|
|
251
|
+
? runProductionCommand
|
|
252
|
+
: runLogsCommand;
|
|
253
|
+
return operation(args, output, { origin, principalId: actor.principalId, projectId: link.projectId, teamId: link.teamId }, (path, method, body) => request(path, method, body, token, link.teamId));
|
|
254
|
+
}
|
|
255
|
+
if (!['source', 'deploy'].includes(command) ||
|
|
256
|
+
options.positional.length !== 1 ||
|
|
257
|
+
!(command === 'deploy' ? ['plan', 'prepare', 'status'] : ['upload', 'status']).includes(options.positional[0]))
|
|
258
|
+
throw commandArgumentError(command, 'Use source upload/status or deploy plan/prepare/status.');
|
|
259
|
+
const link = linkSchema.parse(await readPrivate(linkPath));
|
|
260
|
+
if (link.origin !== origin || link.principalId !== actor.principalId)
|
|
261
|
+
throw platformFailure('Link this project to the selected API origin and signed-in identity.');
|
|
262
|
+
const sourceOperation = {
|
|
263
|
+
options,
|
|
264
|
+
origin,
|
|
265
|
+
actor,
|
|
266
|
+
token,
|
|
267
|
+
stateDir,
|
|
268
|
+
linkPath,
|
|
269
|
+
link,
|
|
270
|
+
uploadPath,
|
|
271
|
+
root,
|
|
272
|
+
request,
|
|
273
|
+
emit,
|
|
274
|
+
platformFailure,
|
|
275
|
+
};
|
|
276
|
+
if (command === 'deploy')
|
|
277
|
+
return runDeploy(sourceOperation);
|
|
278
|
+
return runSourceUpload(sourceOperation);
|
|
279
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { lstat, realpath } from 'node:fs/promises';
|
|
2
2
|
import { dirname, isAbsolute, join, parse } from 'node:path';
|
|
3
|
-
import { CliFailure } from '
|
|
3
|
+
import { CliFailure } from '../cli-errors.js';
|
|
4
4
|
const unavailable = () => new CliFailure('A trusted Linux Secret Service helper is unavailable.', 'PLATFORM_ACCESS_FAILED', 'Install a system-owned secret-tool, explicitly select a trusted system helper with ANHEDRAL_CLI_SECRET_TOOL_PATH, or use --credential-file.', 'context', false);
|
|
5
5
|
async function safePath(path, executable) {
|
|
6
6
|
// Validate the original chain as well as the canonical chain: an untrusted
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { commandArgumentError } from '../cli-arguments.js';
|
|
6
|
+
import { canonicalCliJson, cliPlanInputSchema, cliPlanSchema, cliPreparationSchema, sourceUploadReceiptSchema, } from '../contracts/cli-transport.js';
|
|
7
|
+
import { deploymentManifestSchema } from '../contracts/deployment.js';
|
|
8
|
+
import { visible } from './output.js';
|
|
9
|
+
import { uploadState } from './state-schema.js';
|
|
10
|
+
import { readPrivate, writePrivate } from './state.js';
|
|
11
|
+
const hash = (bytes) => createHash('sha256').update(bytes).digest('hex');
|
|
12
|
+
export async function runDeploy({ options, origin, actor, token, stateDir, link, uploadPath, request, emit, platformFailure, }) {
|
|
13
|
+
const command = 'deploy';
|
|
14
|
+
const localPath = join(stateDir, 'deployment.json');
|
|
15
|
+
const stateSchema = z
|
|
16
|
+
.object({
|
|
17
|
+
origin: z.string(),
|
|
18
|
+
principalId: z.uuid(),
|
|
19
|
+
projectId: z.uuid(),
|
|
20
|
+
planInput: cliPlanInputSchema,
|
|
21
|
+
plan: cliPlanSchema.optional(),
|
|
22
|
+
prepareRequestId: z.uuid().optional(),
|
|
23
|
+
candidateId: z.uuid().optional(),
|
|
24
|
+
})
|
|
25
|
+
.strict();
|
|
26
|
+
const stored = stateSchema.safeParse(await readPrivate(localPath));
|
|
27
|
+
const state = stored.success &&
|
|
28
|
+
stored.data.origin === origin &&
|
|
29
|
+
stored.data.principalId === actor.principalId &&
|
|
30
|
+
stored.data.projectId === link.projectId
|
|
31
|
+
? stored.data
|
|
32
|
+
: null;
|
|
33
|
+
const persist = (v) => writePrivate(localPath, v);
|
|
34
|
+
const checkedPlan = (plan, input) => {
|
|
35
|
+
const { digest, ...unsigned } = plan;
|
|
36
|
+
if (hash(new TextEncoder().encode(canonicalCliJson(unsigned))) !== digest ||
|
|
37
|
+
plan.projectId !== link.projectId ||
|
|
38
|
+
plan.uploadId !== input.uploadId ||
|
|
39
|
+
plan.requestId !== input.requestId ||
|
|
40
|
+
canonicalCliJson(plan.manifest) !== canonicalCliJson(input.manifest))
|
|
41
|
+
throw platformFailure('Deployment plan identity or integrity differs.');
|
|
42
|
+
return plan;
|
|
43
|
+
};
|
|
44
|
+
if (state?.plan)
|
|
45
|
+
checkedPlan(state.plan, state.planInput);
|
|
46
|
+
if (options.positional[0] === 'plan') {
|
|
47
|
+
const upload = uploadState.parse(await readPrivate(uploadPath));
|
|
48
|
+
if (upload.origin !== origin ||
|
|
49
|
+
upload.principalId !== actor.principalId ||
|
|
50
|
+
upload.input.projectId !== link.projectId ||
|
|
51
|
+
!upload.uploadId)
|
|
52
|
+
throw platformFailure('Choose a sealed upload for this linked identity.');
|
|
53
|
+
const receipt = sourceUploadReceiptSchema.parse(await request(`/uploads/${upload.uploadId}`, 'GET', undefined, token, link.teamId));
|
|
54
|
+
if (receipt.state !== 'sealed' ||
|
|
55
|
+
receipt.sourceRevision !== upload.input.archiveDigest ||
|
|
56
|
+
receipt.projectId !== link.projectId)
|
|
57
|
+
throw platformFailure('The saved source upload is not sealed.');
|
|
58
|
+
const file = options.value('--manifest');
|
|
59
|
+
if (!file)
|
|
60
|
+
throw commandArgumentError(command, 'Use deploy plan --manifest <file>.');
|
|
61
|
+
const handle = await import('node:fs/promises').then((fs) => fs.open(resolve(file), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK));
|
|
62
|
+
let manifest;
|
|
63
|
+
try {
|
|
64
|
+
const info = await handle.stat();
|
|
65
|
+
if (!info.isFile() || info.size > 32768)
|
|
66
|
+
throw platformFailure('Deployment manifest exceeds its limit.');
|
|
67
|
+
try {
|
|
68
|
+
manifest = JSON.parse(await handle.readFile('utf8'));
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
throw commandArgumentError(command, 'The deployment manifest is not valid JSON. Review it before retrying.');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
await handle.close();
|
|
76
|
+
}
|
|
77
|
+
const checkedManifest = deploymentManifestSchema.safeParse(manifest);
|
|
78
|
+
if (!checkedManifest.success)
|
|
79
|
+
throw commandArgumentError(command, 'The deployment manifest must match its foundation, capabilities, identity and runtime profile. TanStack Start uses the anhedral-stack runtime profile.');
|
|
80
|
+
const compared = cliPlanInputSchema.parse({
|
|
81
|
+
protocol: 1,
|
|
82
|
+
requestId: state && !options.has('--new') ? state.planInput.requestId : randomUUID(),
|
|
83
|
+
uploadId: upload.uploadId,
|
|
84
|
+
manifest: checkedManifest.data,
|
|
85
|
+
});
|
|
86
|
+
if (state &&
|
|
87
|
+
!options.has('--new') &&
|
|
88
|
+
canonicalCliJson(state.planInput) !== canonicalCliJson(compared))
|
|
89
|
+
throw platformFailure('Plan inputs changed. Use deploy plan --new to create a new request.');
|
|
90
|
+
const local = {
|
|
91
|
+
origin,
|
|
92
|
+
principalId: actor.principalId,
|
|
93
|
+
projectId: link.projectId,
|
|
94
|
+
planInput: compared,
|
|
95
|
+
};
|
|
96
|
+
// Persist before POST so lost plan responses can be recovered by the same request identity.
|
|
97
|
+
await persist({
|
|
98
|
+
...local,
|
|
99
|
+
...(state?.prepareRequestId && !options.has('--new')
|
|
100
|
+
? { prepareRequestId: state.prepareRequestId, candidateId: state.candidateId }
|
|
101
|
+
: {}),
|
|
102
|
+
});
|
|
103
|
+
const plan = cliPlanSchema.parse(await request('/plans', 'POST', compared, token, link.teamId));
|
|
104
|
+
const { digest, ...unsigned } = plan;
|
|
105
|
+
if (hash(new TextEncoder().encode(canonicalCliJson(unsigned))) !== digest ||
|
|
106
|
+
plan.projectId !== link.projectId ||
|
|
107
|
+
plan.uploadId !== compared.uploadId ||
|
|
108
|
+
plan.requestId !== compared.requestId ||
|
|
109
|
+
canonicalCliJson(plan.manifest) !== canonicalCliJson(compared.manifest) ||
|
|
110
|
+
plan.sourceRevision !== receipt.sourceRevision)
|
|
111
|
+
throw platformFailure('Deployment plan identity or integrity differs.');
|
|
112
|
+
await persist({
|
|
113
|
+
...local,
|
|
114
|
+
plan,
|
|
115
|
+
...(state?.prepareRequestId && !options.has('--new')
|
|
116
|
+
? { prepareRequestId: state.prepareRequestId, candidateId: state.candidateId }
|
|
117
|
+
: {}),
|
|
118
|
+
});
|
|
119
|
+
emit(plan);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (!state?.plan)
|
|
123
|
+
throw platformFailure('Create a deployment plan for this linked identity first.');
|
|
124
|
+
if (options.positional[0] === 'status') {
|
|
125
|
+
if (state.candidateId) {
|
|
126
|
+
const result = cliPreparationSchema.parse(await request(`/candidates/${state.candidateId}`, 'GET', undefined, token, link.teamId));
|
|
127
|
+
if (result.planId !== state.plan.id ||
|
|
128
|
+
result.planDigest !== state.plan.digest ||
|
|
129
|
+
result.candidateId !== state.candidateId ||
|
|
130
|
+
result.sourceRevision !== state.plan.sourceRevision)
|
|
131
|
+
throw platformFailure('Candidate receipt identity differs.');
|
|
132
|
+
emit(result);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
const plan = checkedPlan(cliPlanSchema.parse(await request(`/plans/${state.plan.id}`, 'GET', undefined, token, link.teamId)), state.planInput);
|
|
136
|
+
if (plan.digest !== state.plan.digest || plan.id !== state.plan.id)
|
|
137
|
+
throw platformFailure('Plan receipt identity differs.');
|
|
138
|
+
emit(plan);
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (state.plan.admission && state.plan.blockers.length) {
|
|
143
|
+
const issue = state.plan.admission.diagnostics[0];
|
|
144
|
+
throw platformFailure(issue
|
|
145
|
+
? `[${issue.code}] ${visible(issue.message)}`
|
|
146
|
+
: 'This source is not eligible for candidate preparation.', 'conflict', false, issue ? visible(issue.action) : 'Review anhedral deploy plan output before preparing.');
|
|
147
|
+
}
|
|
148
|
+
const requestId = state.prepareRequestId ?? randomUUID();
|
|
149
|
+
await persist({ ...state, prepareRequestId: requestId });
|
|
150
|
+
const prepared = cliPreparationSchema.parse(await request('/candidates', 'POST', { protocol: 1, requestId, planId: state.plan.id, planDigest: state.plan.digest }, token, link.teamId));
|
|
151
|
+
if (prepared.planId !== state.plan.id ||
|
|
152
|
+
prepared.planDigest !== state.plan.digest ||
|
|
153
|
+
prepared.sourceRevision !== state.plan.sourceRevision)
|
|
154
|
+
throw platformFailure('Candidate receipt identity differs.');
|
|
155
|
+
await persist({ ...state, prepareRequestId: requestId, candidateId: prepared.candidateId });
|
|
156
|
+
emit(prepared);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { constants, fstatSync, lstatSync, openSync, readSync, closeSync } from 'node:fs';
|
|
2
|
-
import { dirname, join, resolve } from 'node:path';
|
|
3
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { closeSync, constants, fstatSync, lstatSync, openSync, readSync } from 'node:fs';
|
|
3
|
+
import { dirname, join, resolve } from 'node:path';
|
|
4
4
|
import { z } from 'zod';
|
|
5
|
-
import {
|
|
6
|
-
import { createReporter } from '
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { platformFailure, readPrivate, writePrivate } from './
|
|
5
|
+
import { commandArgumentError, commandArguments } from '../cli-arguments.js';
|
|
6
|
+
import { createReporter } from '../cli-output.js';
|
|
7
|
+
import { inferredEnvironmentKind, parseEnvironmentFile } from '../contracts/environment-file.js';
|
|
8
|
+
import { environmentImportReceiptSchema, environmentImportSchema, environmentMetadataSchema, environmentNameSchema, projectEnvironmentSchema, } from '../contracts/environment-transport.js';
|
|
9
|
+
import { platformFailure, readPrivate, writePrivate } from './state.js';
|
|
10
10
|
const planSchema = z
|
|
11
11
|
.object({
|
|
12
12
|
origin: z.string(),
|