anhedral 0.17.0 → 0.19.0
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 +33 -68
- package/assets/contracts/LICENSE +201 -0
- package/assets/contracts/LICENSE.environment +201 -0
- package/assets/contracts/LICENSE.operations +201 -0
- package/assets/contracts/deployment-provenance.json +15 -0
- package/assets/contracts/environment-provenance.json +11 -0
- package/assets/contracts/operations-provenance.json +12 -0
- package/assets/launch/application.server.ts.txt +13 -0
- package/assets/launch/auth-client.ts.txt +4 -0
- package/assets/launch/home.tsx.txt +20 -0
- package/assets/launch/minimal-application.server.ts.txt +5 -0
- package/assets/launch/minimal-server.ts.txt +7 -0
- package/assets/launch/oj/OJ.LICENSE +21 -0
- package/assets/launch/oj/run.mjs +82 -0
- package/assets/launch/oj/runtime.json +21 -0
- package/assets/launch/request-context.ts.txt +3 -0
- package/assets/launch/root.tsx.txt +26 -0
- package/assets/launch/router.tsx.txt +21 -0
- package/assets/launch/server.ts.txt +15 -0
- package/assets/launch/sign-in.tsx.txt +53 -0
- package/assets/launch/styles.css.txt +19 -0
- package/assets/security/credential-patterns.d.mts +3 -0
- package/assets/security/credential-patterns.mjs +59 -0
- package/assets/stack/api-index.ts.txt +6 -1
- package/assets/stack/stripe.ts.txt +30 -13
- package/assets/ui/catalog.json +36 -324
- package/dist/{platforms/web.d.ts → api-proxy.d.ts} +1 -3
- package/dist/api-proxy.js +48 -0
- package/dist/bin.js +55 -36
- package/dist/capability-catalog.d.ts +36 -57
- package/dist/capability-catalog.js +65 -67
- package/dist/capability-command.d.ts +3 -3
- package/dist/capability-command.js +146 -59
- package/dist/capability-editor.js +6 -5
- package/dist/capability-recipes.js +9 -296
- package/dist/cli-arguments.d.ts +2 -0
- package/dist/cli-arguments.js +101 -50
- package/dist/cli-commands.d.ts +1 -1
- package/dist/cli-commands.js +31 -5
- package/dist/cli-errors.d.ts +18 -0
- package/dist/cli-errors.js +63 -0
- package/dist/cli-output.d.ts +22 -0
- package/dist/cli-output.js +42 -7
- package/dist/command-runner.js +88 -0
- package/dist/command-schema.d.ts +16 -0
- package/dist/command-schema.js +496 -21
- package/dist/command-types.d.ts +1 -0
- package/dist/command-types.js +1 -0
- package/dist/completion.d.ts +2 -0
- package/dist/completion.js +201 -0
- package/dist/composition.d.ts +25 -0
- package/dist/composition.js +28 -0
- package/dist/contracts/cli-transport.d.ts +316 -0
- package/dist/contracts/cli-transport.js +236 -0
- package/dist/contracts/deployment.d.ts +233 -0
- package/dist/contracts/deployment.js +198 -0
- package/dist/contracts/environment-file.d.ts +7 -0
- package/dist/contracts/environment-file.js +112 -0
- package/dist/contracts/environment-transport.d.ts +79 -0
- package/dist/contracts/environment-transport.js +110 -0
- package/dist/contracts/production-transport.d.ts +71 -0
- package/dist/contracts/production-transport.js +56 -0
- package/dist/contracts/runtime-logs-transport.d.ts +127 -0
- package/dist/contracts/runtime-logs-transport.js +50 -0
- package/dist/contracts/source-bundle.d.ts +11 -0
- package/dist/contracts/source-bundle.js +197 -0
- package/dist/contracts/source-profile.d.ts +4 -0
- package/dist/contracts/source-profile.js +5 -0
- package/dist/contracts/staging-transport.d.ts +79 -0
- package/dist/contracts/staging-transport.js +43 -0
- package/dist/dependencies.d.ts +9 -27
- package/dist/dependencies.js +19 -66
- package/dist/index.d.ts +4 -2
- package/dist/index.js +3 -1
- package/dist/launch.d.ts +27 -0
- package/dist/launch.js +262 -0
- package/dist/platform-command.d.ts +9 -0
- package/dist/platform-command.js +787 -0
- package/dist/platform-environment.d.ts +18 -0
- package/dist/platform-environment.js +257 -0
- package/dist/platform-operations.d.ts +26 -0
- package/dist/platform-operations.js +155 -0
- package/dist/platform-production.d.ts +16 -0
- package/dist/platform-production.js +135 -0
- package/dist/platform-source.d.ts +8 -0
- package/dist/platform-source.js +111 -0
- package/dist/platform-state.d.ts +15 -0
- package/dist/platform-state.js +229 -0
- package/dist/platforms/tanstack.d.ts +10 -0
- package/dist/platforms/tanstack.js +248 -0
- package/dist/registry/usage.d.ts +4 -1
- package/dist/registry/usage.js +59 -4
- package/dist/scaffold/bootstrap.d.ts +3 -3
- package/dist/scaffold/styling.d.ts +3 -2
- package/dist/transaction.js +28 -3
- package/dist/ui-catalog.d.ts +3 -5
- package/dist/ui-catalog.js +8 -24
- package/dist/ui-command.d.ts +3 -1
- package/dist/ui-command.js +74 -72
- package/dist/ui-project.d.ts +4 -2
- package/dist/ui-project.js +40 -38
- package/dist/util.js +40 -61
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/capabilities.md +23 -89
- package/docs/software-factory.md +208 -9
- package/docs/stack-standard.md +7 -17
- package/docs/ui.md +9 -22
- package/package.json +26 -22
- package/assets/stack/ai-schema.ts.txt +0 -5
- package/assets/stack/ai.ts.txt +0 -30
- package/assets/stack/desktop.rs.txt +0 -21
- package/assets/stack/events-schema.ts.txt +0 -12
- package/assets/stack/events.ts.txt +0 -59
- package/assets/stack/mobile-auth.ts.txt +0 -25
- package/assets/stack/mobile-billing.tsx.txt +0 -63
- package/assets/stack/mobile-session.tsx.txt +0 -83
- package/assets/stack/native-button.tsx.txt +0 -108
- package/assets/stack/native-global.css.txt +0 -4
- package/assets/stack/native-lib-utils.ts.txt +0 -5
- package/assets/stack/native-metro.config.js.txt +0 -6
- package/assets/stack/native-nativewind-env.d.ts.txt +0 -1
- package/assets/stack/native-tailwind.config.js.txt +0 -73
- package/assets/stack/native-text.tsx.txt +0 -89
- package/assets/stack/purchases.ts.txt +0 -65
- package/assets/stack/revenuecat.ts.txt +0 -88
- package/assets/stack/rtc-schema.ts.txt +0 -10
- package/assets/stack/rtc.ts.txt +0 -122
- package/assets/stack/web-sign-in.tsx.txt +0 -51
- package/dist/capabilities.d.ts +0 -11
- package/dist/capabilities.js +0 -14
- package/dist/dependency-order.d.ts +0 -2
- package/dist/dependency-order.js +0 -24
- package/dist/platforms/lint.d.ts +0 -1
- package/dist/platforms/lint.js +0 -5
- package/dist/platforms/web.js +0 -251
- package/dist/project-config.d.ts +0 -3
- package/dist/project-config.js +0 -16
- package/dist/registry/context.d.ts +0 -3
- package/dist/registry/context.js +0 -20
- package/dist/registry/options.d.ts +0 -2
- package/dist/registry/options.js +0 -44
- package/dist/registry/resolve.d.ts +0 -11
- package/dist/registry/resolve.js +0 -46
- package/dist/registry/types.d.ts +0 -60
- package/dist/scaffold/api.d.ts +0 -2
- package/dist/scaffold/api.js +0 -38
- package/dist/scaffold/backend.d.ts +0 -2
- package/dist/scaffold/backend.js +0 -36
- package/dist/scaffold/compose.d.ts +0 -4
- package/dist/scaffold/compose.js +0 -30
- package/dist/scaffold/database.d.ts +0 -2
- package/dist/scaffold/database.js +0 -41
- package/dist/scaffold/documentation.d.ts +0 -2
- package/dist/scaffold/documentation.js +0 -71
- package/dist/scaffold/environment.d.ts +0 -4
- package/dist/scaffold/environment.js +0 -22
- package/dist/scaffold/stack-documentation.d.ts +0 -2
- package/dist/scaffold/stack-documentation.js +0 -6
- package/dist/scaffold/storage.d.ts +0 -2
- package/dist/scaffold/storage.js +0 -21
- package/dist/scaffold/workspace.d.ts +0 -3
- package/dist/scaffold/workspace.js +0 -131
- package/dist/stack-registry.d.ts +0 -35
- package/dist/stack-registry.js +0 -189
- package/dist/standard.d.ts +0 -8
- package/dist/standard.js +0 -94
- package/dist/workspace-graph.d.ts +0 -7
- package/dist/workspace-graph.js +0 -74
- package/docs/anhedral-stack.md +0 -12
- package/docs/assets/anhedral-stack-light.png +0 -0
- package/docs/assets/anhedral-stack.png +0 -0
- package/docs/native-ui.md +0 -47
- /package/dist/{registry/types.js → command-runner.d.ts} +0 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type OutputOptions } from './cli-output.js';
|
|
2
|
+
type Context = {
|
|
3
|
+
origin: string;
|
|
4
|
+
principalId: string;
|
|
5
|
+
projectId: string;
|
|
6
|
+
teamId: string;
|
|
7
|
+
};
|
|
8
|
+
type Request = (path: string, method: string, body?: unknown) => Promise<unknown>;
|
|
9
|
+
export declare function readEnvironmentSecret(file: string): Uint8Array;
|
|
10
|
+
declare function stdinBytes(): Promise<Uint8Array>;
|
|
11
|
+
export declare function validateEnvironmentArguments(args: string[]): {
|
|
12
|
+
positional: string[];
|
|
13
|
+
has: (flag: string) => boolean;
|
|
14
|
+
value: (flag: string) => string | undefined;
|
|
15
|
+
values: (flag: string) => string[];
|
|
16
|
+
};
|
|
17
|
+
export declare function runEnvironmentCommand(args: string[], output: OutputOptions, context?: Context, request?: Request, readInput?: typeof stdinBytes): Promise<void>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { constants, fstatSync, lstatSync, openSync, readSync, closeSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { commandArguments, commandArgumentError } from './cli-arguments.js';
|
|
6
|
+
import { createReporter } from './cli-output.js';
|
|
7
|
+
import { environmentImportSchema, environmentImportReceiptSchema, environmentMetadataSchema, environmentNameSchema, projectEnvironmentSchema, } from './contracts/environment-transport.js';
|
|
8
|
+
import { inferredEnvironmentKind, parseEnvironmentFile } from './contracts/environment-file.js';
|
|
9
|
+
import { platformFailure, readPrivate, writePrivate } from './platform-state.js';
|
|
10
|
+
const planSchema = z
|
|
11
|
+
.object({
|
|
12
|
+
origin: z.string(),
|
|
13
|
+
principalId: z.uuid(),
|
|
14
|
+
projectId: z.uuid(),
|
|
15
|
+
teamId: z.uuid(),
|
|
16
|
+
environment: projectEnvironmentSchema,
|
|
17
|
+
requestId: z.uuid(),
|
|
18
|
+
variables: z
|
|
19
|
+
.array(z
|
|
20
|
+
.object({
|
|
21
|
+
name: environmentNameSchema,
|
|
22
|
+
kind: z.enum(['secret', 'config']),
|
|
23
|
+
note: z.string(),
|
|
24
|
+
expectedVersion: z.number().int().nonnegative(),
|
|
25
|
+
})
|
|
26
|
+
.strict())
|
|
27
|
+
.max(128),
|
|
28
|
+
receipt: environmentImportReceiptSchema.optional(),
|
|
29
|
+
})
|
|
30
|
+
.strict();
|
|
31
|
+
export function readEnvironmentSecret(file) {
|
|
32
|
+
const path = resolve(file);
|
|
33
|
+
for (let parent = dirname(path);; parent = dirname(parent)) {
|
|
34
|
+
const info = lstatSync(parent);
|
|
35
|
+
if (!info.isDirectory() || info.isSymbolicLink())
|
|
36
|
+
throw platformFailure('Environment input has a symlinked or invalid directory.');
|
|
37
|
+
if (dirname(parent) === parent)
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
const descriptor = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0));
|
|
41
|
+
try {
|
|
42
|
+
const info = fstatSync(descriptor);
|
|
43
|
+
if (!info.isFile() ||
|
|
44
|
+
info.size > 65536 ||
|
|
45
|
+
(process.platform !== 'win32' &&
|
|
46
|
+
(info.uid !== process.getuid?.() || (info.mode & 0o077) !== 0)))
|
|
47
|
+
throw platformFailure('Environment input must be your regular owner-only file (mode 0600), at most 64 KiB.');
|
|
48
|
+
const data = Buffer.alloc(65537);
|
|
49
|
+
let length = 0;
|
|
50
|
+
while (length < data.length) {
|
|
51
|
+
const read = readSync(descriptor, data, length, data.length - length, null);
|
|
52
|
+
if (!read)
|
|
53
|
+
break;
|
|
54
|
+
length += read;
|
|
55
|
+
}
|
|
56
|
+
if (length > 65536)
|
|
57
|
+
throw platformFailure('Environment input exceeds 64 KiB.');
|
|
58
|
+
return data.subarray(0, length);
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
closeSync(descriptor);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function stdinBytes() {
|
|
65
|
+
if (process.stdin.isTTY)
|
|
66
|
+
throw platformFailure('Pipe a value or dotenv contents to --stdin; interactive secret entry is not supported.');
|
|
67
|
+
const chunks = [];
|
|
68
|
+
let length = 0;
|
|
69
|
+
const timeout = setTimeout(() => process.stdin.destroy(new Error('Environment input timed out.')), 30000);
|
|
70
|
+
try {
|
|
71
|
+
for await (const item of process.stdin) {
|
|
72
|
+
const bytes = Buffer.from(item);
|
|
73
|
+
length += bytes.length;
|
|
74
|
+
if (length > 65536)
|
|
75
|
+
throw platformFailure('Environment input exceeds 64 KiB.');
|
|
76
|
+
chunks.push(bytes);
|
|
77
|
+
}
|
|
78
|
+
return Buffer.concat(chunks);
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
clearTimeout(timeout);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export function validateEnvironmentArguments(args) {
|
|
85
|
+
const options = commandArguments('env', args);
|
|
86
|
+
const [operation, name] = options.positional;
|
|
87
|
+
if (operation === 'list') {
|
|
88
|
+
if (['--file', '--stdin', '--type', '--replace', '--new', '--dry-run'].some((flag) => options.has(flag)))
|
|
89
|
+
throw commandArgumentError('env', 'env list accepts only context and environment options.');
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
if (!options.value('--environment'))
|
|
93
|
+
throw commandArgumentError('env', 'An explicit --environment staging|production is required.');
|
|
94
|
+
if (options.has('--file') === options.has('--stdin'))
|
|
95
|
+
throw commandArgumentError('env', 'Choose exactly one --file <file> or --stdin. Values cannot be supplied in arguments.');
|
|
96
|
+
if (operation === 'set' && !environmentNameSchema.safeParse(name).success)
|
|
97
|
+
throw commandArgumentError('env', 'Use an uppercase variable name.');
|
|
98
|
+
}
|
|
99
|
+
return options;
|
|
100
|
+
}
|
|
101
|
+
export async function runEnvironmentCommand(args, output, context, request, readInput = stdinBytes) {
|
|
102
|
+
const options = validateEnvironmentArguments(args);
|
|
103
|
+
const [operation, name] = options.positional;
|
|
104
|
+
const reporter = createReporter(output, (value) => {
|
|
105
|
+
const data = value;
|
|
106
|
+
return (`${data.status} ${data.environment ?? ''}\n` +
|
|
107
|
+
(data.variables ?? data.imported ?? [])
|
|
108
|
+
.map((entry) => `${entry.name}${entry.version ? ` v${entry.version}` : ''}`)
|
|
109
|
+
.join('\n'));
|
|
110
|
+
}, true);
|
|
111
|
+
const environment = options.value('--environment');
|
|
112
|
+
if (environment && !projectEnvironmentSchema.safeParse(environment).success)
|
|
113
|
+
throw commandArgumentError('env', 'Choose staging or production.');
|
|
114
|
+
if (operation === 'list') {
|
|
115
|
+
if (!context || !request)
|
|
116
|
+
throw platformFailure('Link and sign in to the intended project first.');
|
|
117
|
+
const metadata = environmentMetadataSchema.parse(await request(`/projects/${context.projectId}/environment`, 'GET'));
|
|
118
|
+
reporter.result({
|
|
119
|
+
status: 'listed',
|
|
120
|
+
projectId: context.projectId,
|
|
121
|
+
environment: environment ?? 'all',
|
|
122
|
+
variables: metadata.variables.filter((entry) => !environment || entry.environment === environment),
|
|
123
|
+
});
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const explicitKind = options.value('--type');
|
|
127
|
+
let entries;
|
|
128
|
+
try {
|
|
129
|
+
const bytes = options.has('--file')
|
|
130
|
+
? readEnvironmentSecret(options.value('--file'))
|
|
131
|
+
: await readInput();
|
|
132
|
+
if (bytes.length > 65536)
|
|
133
|
+
throw new Error('Environment input exceeds 64 KiB.');
|
|
134
|
+
const text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
135
|
+
entries = operation === 'set' ? [{ name: name, value: text }] : parseEnvironmentFile(text);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
if (error instanceof Error && error.name === 'CliArgumentError')
|
|
139
|
+
throw error;
|
|
140
|
+
throw platformFailure(error instanceof Error && /^(Line \d+:|Environment (files|file|input))/.test(error.message)
|
|
141
|
+
? error.message
|
|
142
|
+
: 'Could not read private UTF-8 environment input. No variables were uploaded.');
|
|
143
|
+
}
|
|
144
|
+
const variables = entries
|
|
145
|
+
.map((entry) => ({
|
|
146
|
+
...entry,
|
|
147
|
+
kind: (explicitKind ?? inferredEnvironmentKind(entry.name)),
|
|
148
|
+
note: '',
|
|
149
|
+
expectedVersion: 0,
|
|
150
|
+
}))
|
|
151
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
152
|
+
if (!environmentImportSchema.safeParse({ requestId: randomUUID(), environment, variables }).success)
|
|
153
|
+
throw platformFailure('Check variable names, type and size. Empty values, reserved names and public-prefixed secrets are rejected. No variables were uploaded.');
|
|
154
|
+
if (options.has('--dry-run')) {
|
|
155
|
+
reporter.result({
|
|
156
|
+
status: 'dry_run',
|
|
157
|
+
environment,
|
|
158
|
+
projectId: context?.projectId ?? null,
|
|
159
|
+
projectVerified: false,
|
|
160
|
+
variables: variables.map(({ name, kind }) => ({ name, kind })),
|
|
161
|
+
applied: false,
|
|
162
|
+
});
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (!context || !request)
|
|
166
|
+
throw platformFailure('Link and sign in to the intended project first.');
|
|
167
|
+
const statePath = join(process.cwd(), '.anhedral', 'platform', `environment-${environment}.json`);
|
|
168
|
+
const raw = await readPrivate(statePath);
|
|
169
|
+
const parsed = raw === null ? null : planSchema.safeParse(raw);
|
|
170
|
+
if (parsed && !parsed.success)
|
|
171
|
+
throw platformFailure('Saved environment import state is invalid. Preserve it and reconcile the selected context.');
|
|
172
|
+
const previous = parsed?.success ? parsed.data : null;
|
|
173
|
+
if (previous &&
|
|
174
|
+
(previous.origin !== context.origin ||
|
|
175
|
+
previous.principalId !== context.principalId ||
|
|
176
|
+
previous.projectId !== context.projectId ||
|
|
177
|
+
previous.teamId !== context.teamId ||
|
|
178
|
+
previous.environment !== environment))
|
|
179
|
+
throw platformFailure('Saved environment import belongs to a different identity or project. Preserve it and select the original context.');
|
|
180
|
+
if (previous &&
|
|
181
|
+
!options.has('--new') &&
|
|
182
|
+
JSON.stringify(previous.variables.map(({ name, kind }) => ({ name, kind }))) !==
|
|
183
|
+
JSON.stringify(variables.map(({ name, kind }) => ({ name, kind }))))
|
|
184
|
+
throw platformFailure('Saved import names or types changed. Use --new for another import.');
|
|
185
|
+
if (previous?.receipt &&
|
|
186
|
+
(previous.receipt.requestId !== previous.requestId ||
|
|
187
|
+
previous.receipt.imported.length !== previous.variables.length ||
|
|
188
|
+
previous.receipt.imported.some((entry, index) => entry.name !== previous.variables[index]?.name ||
|
|
189
|
+
entry.kind !== previous.variables[index]?.kind ||
|
|
190
|
+
entry.environment !== previous.environment ||
|
|
191
|
+
(previous.variables[index].expectedVersion > 0 &&
|
|
192
|
+
entry.version !== previous.variables[index].expectedVersion + 1))))
|
|
193
|
+
throw platformFailure('Saved environment receipt differs from its import. Preserve state and reconcile before retrying.');
|
|
194
|
+
if (previous?.receipt && !options.has('--new')) {
|
|
195
|
+
reporter.result({
|
|
196
|
+
status: 'already_imported',
|
|
197
|
+
projectId: context.projectId,
|
|
198
|
+
environment,
|
|
199
|
+
...previous.receipt,
|
|
200
|
+
nextAction: 'Use --new to start another import. Existing values additionally require --replace.',
|
|
201
|
+
});
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
let state;
|
|
205
|
+
if (previous && !options.has('--new')) {
|
|
206
|
+
if (JSON.stringify(previous.variables.map(({ name, kind }) => ({ name, kind }))) !==
|
|
207
|
+
JSON.stringify(variables.map(({ name, kind }) => ({ name, kind }))))
|
|
208
|
+
throw platformFailure('Pending import names or types changed. Restore its original input or use --new.');
|
|
209
|
+
state = previous;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
const metadata = environmentMetadataSchema.parse(await request(`/projects/${context.projectId}/environment`, 'GET'));
|
|
213
|
+
const existing = new Map(metadata.variables
|
|
214
|
+
.filter((entry) => entry.environment === environment)
|
|
215
|
+
.map((entry) => [entry.name, entry]));
|
|
216
|
+
if (!options.has('--replace') && variables.some((entry) => existing.has(entry.name)))
|
|
217
|
+
throw platformFailure('Some variables already exist. Review their names with env list, then explicitly use --replace.');
|
|
218
|
+
state = {
|
|
219
|
+
...context,
|
|
220
|
+
environment: projectEnvironmentSchema.parse(environment),
|
|
221
|
+
requestId: randomUUID(),
|
|
222
|
+
variables: variables.map(({ name, kind, note }) => ({
|
|
223
|
+
name,
|
|
224
|
+
kind,
|
|
225
|
+
note,
|
|
226
|
+
expectedVersion: existing.get(name)?.version ?? 0,
|
|
227
|
+
})),
|
|
228
|
+
};
|
|
229
|
+
// Persist only retry identity and versions. No value or value digest is saved.
|
|
230
|
+
await writePrivate(statePath, state);
|
|
231
|
+
}
|
|
232
|
+
const input = environmentImportSchema.parse({
|
|
233
|
+
requestId: state.requestId,
|
|
234
|
+
environment,
|
|
235
|
+
variables: variables.map((entry, index) => ({
|
|
236
|
+
...entry,
|
|
237
|
+
expectedVersion: state.variables[index].expectedVersion,
|
|
238
|
+
})),
|
|
239
|
+
});
|
|
240
|
+
const receipt = environmentImportReceiptSchema.parse(await request(`/projects/${context.projectId}/environment/import`, 'POST', input));
|
|
241
|
+
if (receipt.requestId !== state.requestId ||
|
|
242
|
+
receipt.imported.length !== variables.length ||
|
|
243
|
+
receipt.imported.some((entry, index) => entry.name !== variables[index]?.name ||
|
|
244
|
+
entry.kind !== variables[index]?.kind ||
|
|
245
|
+
entry.environment !== environment ||
|
|
246
|
+
(state.variables[index].expectedVersion > 0 &&
|
|
247
|
+
entry.version !== state.variables[index].expectedVersion + 1)))
|
|
248
|
+
throw platformFailure('Environment receipt differs from this import. Preserve saved state before retrying.');
|
|
249
|
+
await writePrivate(statePath, { ...state, receipt });
|
|
250
|
+
reporter.result({
|
|
251
|
+
status: 'imported',
|
|
252
|
+
projectId: context.projectId,
|
|
253
|
+
environment,
|
|
254
|
+
...receipt,
|
|
255
|
+
appliedToRunningDeployments: false,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type OutputOptions } from './cli-output.js';
|
|
2
|
+
type Context = {
|
|
3
|
+
origin: string;
|
|
4
|
+
principalId: string;
|
|
5
|
+
projectId: string;
|
|
6
|
+
teamId: string;
|
|
7
|
+
};
|
|
8
|
+
type Request = (path: string, method: string, body?: unknown) => Promise<unknown>;
|
|
9
|
+
export declare function validateStagingArguments(args: string[]): {
|
|
10
|
+
positional: string[];
|
|
11
|
+
has: (flag: string) => boolean;
|
|
12
|
+
value: (flag: string) => string | undefined;
|
|
13
|
+
values: (flag: string) => string[];
|
|
14
|
+
};
|
|
15
|
+
export declare function validateLogsArguments(args: string[]): {
|
|
16
|
+
environment: "staging" | "production";
|
|
17
|
+
range: "1h" | "24h" | "7d";
|
|
18
|
+
search: string;
|
|
19
|
+
deploymentId?: string | undefined;
|
|
20
|
+
level?: "debug" | "info" | "warn" | "error" | undefined;
|
|
21
|
+
source?: "request" | "console" | "exception" | undefined;
|
|
22
|
+
cursor?: string | undefined;
|
|
23
|
+
};
|
|
24
|
+
export declare function runStagingCommand(args: string[], output: OutputOptions, context: Context, request: Request): Promise<void>;
|
|
25
|
+
export declare function runLogsCommand(args: string[], output: OutputOptions, context: Context, request: Request): Promise<void>;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { stripVTControlCharacters } from 'node:util';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { commandArguments, commandArgumentError } from './cli-arguments.js';
|
|
6
|
+
import { createReporter } from './cli-output.js';
|
|
7
|
+
import { stagingCreateSchema, stagingStateSchema, stagingCreateReceiptSchema, } from './contracts/staging-transport.js';
|
|
8
|
+
import { runtimeLogQuerySchema, runtimeLogPageSchema } from './contracts/runtime-logs-transport.js';
|
|
9
|
+
import { platformFailure, readPrivate, writePrivate } from './platform-state.js';
|
|
10
|
+
const contextSchema = z.object({
|
|
11
|
+
origin: z.string(),
|
|
12
|
+
principalId: z.uuid(),
|
|
13
|
+
projectId: z.uuid(),
|
|
14
|
+
teamId: z.uuid(),
|
|
15
|
+
});
|
|
16
|
+
const receiptSchema = stagingCreateReceiptSchema;
|
|
17
|
+
const stateSchema = contextSchema
|
|
18
|
+
.extend({ input: stagingCreateSchema, receipt: receiptSchema.optional() })
|
|
19
|
+
.strict();
|
|
20
|
+
const visible = (value) => stripVTControlCharacters(String(value ?? '')).replace(/[\x00-\x1f\x7f]/g, ' ');
|
|
21
|
+
export function validateStagingArguments(args) {
|
|
22
|
+
const options = commandArguments('staging', args);
|
|
23
|
+
const [operation] = options.positional;
|
|
24
|
+
if (operation === 'status' && (options.has('--candidate') || options.has('--new')))
|
|
25
|
+
throw commandArgumentError('staging', 'Candidate and --new apply to staging deploy only.');
|
|
26
|
+
if (operation === 'deploy' && !z.uuid().safeParse(options.value('--candidate')).success)
|
|
27
|
+
throw commandArgumentError('staging', 'Select a ready candidate UUID with --candidate.');
|
|
28
|
+
return options;
|
|
29
|
+
}
|
|
30
|
+
export function validateLogsArguments(args) {
|
|
31
|
+
const options = commandArguments('logs', args);
|
|
32
|
+
const parsed = runtimeLogQuerySchema.safeParse({
|
|
33
|
+
environment: options.value('--environment') ?? 'staging',
|
|
34
|
+
deploymentId: options.value('--deployment'),
|
|
35
|
+
level: options.value('--level'),
|
|
36
|
+
source: options.value('--source'),
|
|
37
|
+
range: options.value('--range'),
|
|
38
|
+
search: options.value('--search'),
|
|
39
|
+
cursor: options.value('--cursor'),
|
|
40
|
+
});
|
|
41
|
+
if (!parsed.success)
|
|
42
|
+
throw commandArgumentError('logs', 'Check the environment, deployment, level, source, range and cursor filters.');
|
|
43
|
+
return parsed.data;
|
|
44
|
+
}
|
|
45
|
+
export async function runStagingCommand(args, output, context, request) {
|
|
46
|
+
const options = validateStagingArguments(args);
|
|
47
|
+
const [operation] = options.positional;
|
|
48
|
+
const reporter = createReporter(output, (value) => {
|
|
49
|
+
const data = value;
|
|
50
|
+
if (data.status === 'requested' || data.status === 'already_requested')
|
|
51
|
+
return `Staging deployment requested (${visible(data.deployment.id)}).\nRun anhedral staging status to follow preparation.`;
|
|
52
|
+
const state = data.staging;
|
|
53
|
+
return [
|
|
54
|
+
`Staging generation ${state.generation}.`,
|
|
55
|
+
state.enabled === false
|
|
56
|
+
? 'Persistent staging is not configured.'
|
|
57
|
+
: `Active deployment: ${visible(state.activeDeploymentId ?? 'none')}.`,
|
|
58
|
+
...state.deployments.map((entry) => `${entry.id} ${entry.status} ${entry.sourceRevision.slice(0, 12)}`),
|
|
59
|
+
].join('\n');
|
|
60
|
+
}, true);
|
|
61
|
+
const path = `/projects/${context.projectId}/staging`;
|
|
62
|
+
const checkedState = (value) => {
|
|
63
|
+
const state = stagingStateSchema.parse(value);
|
|
64
|
+
if (state.deployments.some((entry) => entry.developmentId !== context.projectId))
|
|
65
|
+
throw platformFailure('Staging history belongs to another project.');
|
|
66
|
+
return state;
|
|
67
|
+
};
|
|
68
|
+
if (operation === 'status') {
|
|
69
|
+
reporter.result({
|
|
70
|
+
status: 'read',
|
|
71
|
+
environment: 'staging',
|
|
72
|
+
projectId: context.projectId,
|
|
73
|
+
staging: checkedState(await request(path, 'GET')),
|
|
74
|
+
});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const candidateId = options.value('--candidate');
|
|
78
|
+
const file = join(process.cwd(), '.anhedral', 'platform', 'staging.json');
|
|
79
|
+
const raw = await readPrivate(file);
|
|
80
|
+
const parsed = raw === null ? null : stateSchema.safeParse(raw);
|
|
81
|
+
if (parsed && !parsed.success)
|
|
82
|
+
throw platformFailure('Saved staging deployment state is invalid. Preserve it before retrying.');
|
|
83
|
+
const previous = parsed?.success ? parsed.data : null;
|
|
84
|
+
if (previous &&
|
|
85
|
+
Object.entries(context).some(([key, value]) => previous[key] !== value))
|
|
86
|
+
throw platformFailure('Saved staging deployment belongs to a different identity, team or project. Restore its original context.');
|
|
87
|
+
if (previous && !options.has('--new') && previous.input.candidateId !== candidateId)
|
|
88
|
+
throw platformFailure('The candidate differs from the pending staging deployment. Use --new for another deployment.');
|
|
89
|
+
if (previous?.receipt &&
|
|
90
|
+
(previous.receipt.developmentId !== context.projectId ||
|
|
91
|
+
previous.receipt.requestId !== previous.input.requestId ||
|
|
92
|
+
previous.receipt.candidateId !== previous.input.candidateId ||
|
|
93
|
+
previous.receipt.generation !== previous.input.expectedGeneration + 1))
|
|
94
|
+
throw platformFailure('Saved staging receipt differs from its request. Preserve state and reconcile before retrying.');
|
|
95
|
+
if (previous?.receipt && !options.has('--new')) {
|
|
96
|
+
reporter.result({
|
|
97
|
+
status: 'already_requested',
|
|
98
|
+
environment: 'staging',
|
|
99
|
+
projectId: context.projectId,
|
|
100
|
+
requestId: previous.input.requestId,
|
|
101
|
+
deployment: previous.receipt,
|
|
102
|
+
});
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
let state;
|
|
106
|
+
if (previous && !options.has('--new'))
|
|
107
|
+
state = previous;
|
|
108
|
+
else {
|
|
109
|
+
const current = checkedState(await request(path, 'GET'));
|
|
110
|
+
if (current.enabled !== true || !current.canDeploy)
|
|
111
|
+
throw platformFailure('Staging deployment is not configured or current build permission is unavailable.');
|
|
112
|
+
state = {
|
|
113
|
+
...context,
|
|
114
|
+
input: stagingCreateSchema.parse({
|
|
115
|
+
requestId: randomUUID(),
|
|
116
|
+
candidateId,
|
|
117
|
+
expectedGeneration: current.generation,
|
|
118
|
+
}),
|
|
119
|
+
};
|
|
120
|
+
await writePrivate(file, state);
|
|
121
|
+
}
|
|
122
|
+
const receipt = receiptSchema.parse(await request(`${path}/deployments`, 'POST', state.input));
|
|
123
|
+
if (receipt.developmentId !== context.projectId ||
|
|
124
|
+
receipt.requestId !== state.input.requestId ||
|
|
125
|
+
receipt.candidateId !== state.input.candidateId ||
|
|
126
|
+
receipt.generation !== state.input.expectedGeneration + 1)
|
|
127
|
+
throw platformFailure('Staging receipt differs from this request. Preserve saved state before retrying.');
|
|
128
|
+
await writePrivate(file, { ...state, receipt });
|
|
129
|
+
reporter.result({
|
|
130
|
+
status: 'requested',
|
|
131
|
+
environment: 'staging',
|
|
132
|
+
projectId: context.projectId,
|
|
133
|
+
requestId: state.input.requestId,
|
|
134
|
+
deployment: receipt,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
export async function runLogsCommand(args, output, context, request) {
|
|
138
|
+
const query = validateLogsArguments(args);
|
|
139
|
+
const params = new URLSearchParams(Object.entries(query).filter((entry) => typeof entry[1] === 'string'));
|
|
140
|
+
const page = runtimeLogPageSchema.parse(await request(`/projects/${context.projectId}/runtime-logs?${params}`, 'GET'));
|
|
141
|
+
if (page.environment !== query.environment ||
|
|
142
|
+
page.entries.some((entry) => entry.environment !== query.environment ||
|
|
143
|
+
(query.deploymentId && entry.deploymentId !== query.deploymentId)))
|
|
144
|
+
throw platformFailure('Runtime log response differs from the selected environment or deployment.');
|
|
145
|
+
const reporter = createReporter(output, (value) => {
|
|
146
|
+
const logs = value.logs;
|
|
147
|
+
return [
|
|
148
|
+
logs.state === 'ready' ? `Runtime logs: ${logs.environment}.` : visible(logs.reason),
|
|
149
|
+
...logs.entries.map((entry) => `${entry.occurredAt} ${entry.level} ${entry.component}/${entry.kind} ${visible(entry.message)}`),
|
|
150
|
+
...(logs.nextCursor ? [`More entries: use --cursor ${logs.nextCursor}.`] : []),
|
|
151
|
+
...(logs.dropped ? [`${logs.dropped} entries were dropped by collection limits.`] : []),
|
|
152
|
+
].join('\n');
|
|
153
|
+
}, true);
|
|
154
|
+
reporter.result({ projectId: context.projectId, logs: page });
|
|
155
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type OutputOptions } from './cli-output.js';
|
|
2
|
+
type Context = {
|
|
3
|
+
origin: string;
|
|
4
|
+
principalId: string;
|
|
5
|
+
projectId: string;
|
|
6
|
+
teamId: string;
|
|
7
|
+
};
|
|
8
|
+
type Request = (path: string, method: string, body?: unknown) => Promise<unknown>;
|
|
9
|
+
export declare function validateProductionArguments(args: string[]): {
|
|
10
|
+
positional: string[];
|
|
11
|
+
has: (flag: string) => boolean;
|
|
12
|
+
value: (flag: string) => string | undefined;
|
|
13
|
+
values: (flag: string) => string[];
|
|
14
|
+
};
|
|
15
|
+
export declare function runProductionCommand(args: string[], output: OutputOptions, context: Context, request: Request): Promise<void>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { stripVTControlCharacters } from 'node:util';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { commandArguments, commandArgumentError } from './cli-arguments.js';
|
|
6
|
+
import { createReporter } from './cli-output.js';
|
|
7
|
+
import { productionCreateSchema, productionCreateReceiptSchema, productionStateSchema, } from './contracts/production-transport.js';
|
|
8
|
+
import { platformFailure, readPrivate, writePrivate, withProjectLock } from './platform-state.js';
|
|
9
|
+
const stateSchema = z
|
|
10
|
+
.object({
|
|
11
|
+
origin: z.string(),
|
|
12
|
+
principalId: z.uuid(),
|
|
13
|
+
projectId: z.uuid(),
|
|
14
|
+
teamId: z.uuid(),
|
|
15
|
+
input: productionCreateSchema,
|
|
16
|
+
receipt: productionCreateReceiptSchema.optional(),
|
|
17
|
+
})
|
|
18
|
+
.strict();
|
|
19
|
+
const visible = (value) => stripVTControlCharacters(String(value ?? '')).replace(/[\x00-\x1f\x7f]/g, ' ');
|
|
20
|
+
export function validateProductionArguments(args) {
|
|
21
|
+
const options = commandArguments('production', args);
|
|
22
|
+
const [operation] = options.positional;
|
|
23
|
+
if (operation === 'status' &&
|
|
24
|
+
['--candidate', '--name', '--new'].some((flag) => options.has(flag)))
|
|
25
|
+
throw commandArgumentError('production', 'Candidate, name and --new apply to production deploy only.');
|
|
26
|
+
if (operation === 'deploy') {
|
|
27
|
+
if (!z.uuid().safeParse(options.value('--candidate')).success)
|
|
28
|
+
throw commandArgumentError('production', 'Select a ready candidate UUID with --candidate.');
|
|
29
|
+
if (!productionCreateSchema.shape.name.safeParse(options.value('--name')).success)
|
|
30
|
+
throw commandArgumentError('production', 'Select a valid application name with --name.');
|
|
31
|
+
}
|
|
32
|
+
return options;
|
|
33
|
+
}
|
|
34
|
+
async function runProductionOperation(args, output, context, request) {
|
|
35
|
+
const options = validateProductionArguments(args);
|
|
36
|
+
const [operation] = options.positional;
|
|
37
|
+
const reporter = createReporter(output, (value) => {
|
|
38
|
+
const data = value;
|
|
39
|
+
if (data.status === 'requested' || data.status === 'already_requested')
|
|
40
|
+
return `Production deployment requested (${visible(data.deployment.id)}).\nRun anhedral production status to follow publication.`;
|
|
41
|
+
const state = data.production;
|
|
42
|
+
return [
|
|
43
|
+
`Production generation ${state.generation}.`,
|
|
44
|
+
state.enabled === false
|
|
45
|
+
? 'Production deployment is not configured.'
|
|
46
|
+
: `Active deployment: ${visible(state.activeDeploymentId ?? 'none')}.`,
|
|
47
|
+
...(state.requiresIndependentApproval
|
|
48
|
+
? ['This team requires an independent production approver.']
|
|
49
|
+
: []),
|
|
50
|
+
...state.deployments.map((entry) => `${visible(entry.id)} ${visible(entry.status)} ${visible(entry.sourceRevision.slice(0, 12))}`),
|
|
51
|
+
].join('\n');
|
|
52
|
+
}, true);
|
|
53
|
+
const path = `/projects/${context.projectId}/production`;
|
|
54
|
+
const checkedState = (value) => {
|
|
55
|
+
const state = productionStateSchema.parse(value);
|
|
56
|
+
if (state.deployments.some((entry) => entry.developmentId !== context.projectId))
|
|
57
|
+
throw platformFailure('Production history belongs to another project.');
|
|
58
|
+
return state;
|
|
59
|
+
};
|
|
60
|
+
if (operation === 'status') {
|
|
61
|
+
reporter.result({
|
|
62
|
+
status: 'read',
|
|
63
|
+
environment: 'production',
|
|
64
|
+
projectId: context.projectId,
|
|
65
|
+
production: checkedState(await request(path, 'GET')),
|
|
66
|
+
});
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const candidateId = options.value('--candidate'), name = options.value('--name');
|
|
70
|
+
const file = join(process.cwd(), '.anhedral', 'platform', 'production.json');
|
|
71
|
+
const raw = await readPrivate(file), parsed = raw === null ? null : stateSchema.safeParse(raw);
|
|
72
|
+
if (parsed && !parsed.success)
|
|
73
|
+
throw platformFailure('Saved production deployment state is invalid. Preserve it before retrying.');
|
|
74
|
+
const previous = parsed?.success ? parsed.data : null;
|
|
75
|
+
if (previous &&
|
|
76
|
+
Object.entries(context).some(([key, value]) => previous[key] !== value))
|
|
77
|
+
throw platformFailure('Saved production deployment belongs to a different identity, team or project. Restore its original context.');
|
|
78
|
+
if (previous &&
|
|
79
|
+
!options.has('--new') &&
|
|
80
|
+
(previous.input.candidateId !== candidateId || previous.input.name !== name))
|
|
81
|
+
throw platformFailure('The candidate or name differs from the pending production deployment. Use --new for another deployment.');
|
|
82
|
+
const validReceipt = (receipt, input) => receipt.developmentId === context.projectId &&
|
|
83
|
+
receipt.requestId === input.requestId &&
|
|
84
|
+
receipt.candidateId === input.candidateId &&
|
|
85
|
+
receipt.generation === input.expectedGeneration + 1;
|
|
86
|
+
if (previous?.receipt && !validReceipt(previous.receipt, previous.input))
|
|
87
|
+
throw platformFailure('Saved production receipt differs from its request. Preserve state and reconcile before retrying.');
|
|
88
|
+
if (previous?.receipt && !options.has('--new')) {
|
|
89
|
+
reporter.result({
|
|
90
|
+
status: 'already_requested',
|
|
91
|
+
environment: 'production',
|
|
92
|
+
projectId: context.projectId,
|
|
93
|
+
requestId: previous.input.requestId,
|
|
94
|
+
deployment: previous.receipt,
|
|
95
|
+
});
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
let state;
|
|
99
|
+
if (previous && !options.has('--new'))
|
|
100
|
+
state = previous;
|
|
101
|
+
else {
|
|
102
|
+
const current = checkedState(await request(path, 'GET'));
|
|
103
|
+
if (current.requiresIndependentApproval)
|
|
104
|
+
throw platformFailure('This team requires an independent production approver. Request approval in Platform before production publication.', 'Review the team production approval policy in Platform.', 'permission');
|
|
105
|
+
if (current.enabled !== true || !current.canDeploy)
|
|
106
|
+
throw platformFailure('Production deployment is not configured or current production permission is unavailable.');
|
|
107
|
+
state = {
|
|
108
|
+
...context,
|
|
109
|
+
input: productionCreateSchema.parse({
|
|
110
|
+
requestId: randomUUID(),
|
|
111
|
+
candidateId,
|
|
112
|
+
name,
|
|
113
|
+
expectedGeneration: current.generation,
|
|
114
|
+
}),
|
|
115
|
+
};
|
|
116
|
+
await writePrivate(file, state);
|
|
117
|
+
}
|
|
118
|
+
const receipt = productionCreateReceiptSchema.parse(await request(`${path}/deployments`, 'POST', state.input));
|
|
119
|
+
if (!validReceipt(receipt, state.input))
|
|
120
|
+
throw platformFailure('Production receipt differs from this request. Preserve saved state before retrying.');
|
|
121
|
+
await writePrivate(file, { ...state, receipt });
|
|
122
|
+
reporter.result({
|
|
123
|
+
status: 'requested',
|
|
124
|
+
environment: 'production',
|
|
125
|
+
projectId: context.projectId,
|
|
126
|
+
requestId: state.input.requestId,
|
|
127
|
+
deployment: receipt,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
export async function runProductionCommand(args, output, context, request) {
|
|
131
|
+
const options = validateProductionArguments(args);
|
|
132
|
+
if (options.positional[0] === 'deploy')
|
|
133
|
+
return withProjectLock(join(process.cwd(), '.anhedral', 'platform'), () => runProductionOperation(args, output, context, request));
|
|
134
|
+
return runProductionOperation(args, output, context, request);
|
|
135
|
+
}
|