anhedral 0.11.2 → 0.12.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 +107 -14
- package/dist/bin.js +48 -493
- package/dist/capabilities.d.ts +3 -3
- package/dist/capability-contract.js +4 -0
- package/dist/cli-arguments.d.ts +8 -0
- package/dist/cli-arguments.js +32 -0
- package/dist/cli-commands.d.ts +3 -0
- package/dist/cli-commands.js +114 -0
- package/dist/cli-delivery.d.ts +3 -0
- package/dist/cli-delivery.js +172 -0
- package/dist/cli-output.js +231 -205
- package/dist/delivery-evidence.d.ts +62 -0
- package/dist/delivery-evidence.js +186 -0
- package/dist/delivery.d.ts +3 -40
- package/dist/delivery.js +4 -181
- package/dist/dependencies.d.ts +1 -0
- package/dist/dependencies.js +2 -0
- package/dist/mobile-delivery.d.ts +21 -44
- package/dist/mobile-delivery.js +127 -80
- package/dist/provider-auth.js +3 -3
- package/dist/scaffold/documentation.js +1 -0
- package/dist/scaffold/stream.d.ts +2 -0
- package/dist/scaffold/stream.js +114 -0
- package/dist/service-readers.js +1 -1
- package/dist/services-command.js +8 -6
- package/dist/ship.d.ts +15 -15
- package/dist/stack-registry.d.ts +4 -2
- package/dist/stack-registry.js +16 -0
- package/dist/transaction-lock.d.ts +15 -0
- package/dist/transaction-lock.js +227 -0
- package/dist/transaction.js +2 -208
- package/dist/vercel-project.d.ts +101 -0
- package/dist/vercel-project.js +305 -0
- package/dist/vercel.d.ts +3 -36
- package/dist/vercel.js +3 -300
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/software-factory.md +339 -3
- package/docs/stack-standard.md +138 -30
- package/docs/verification.md +444 -0
- package/package.json +2 -5
- package/docs/cli-design.md +0 -67
- package/docs/plugin-ui.md +0 -102
- package/docs/provider-configuration.md +0 -116
- package/docs/void-verification.md +0 -185
package/dist/stack-registry.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ declare const PRODUCT_ITEMS: {
|
|
|
16
16
|
r2: ItemDefinition;
|
|
17
17
|
kv: ItemDefinition;
|
|
18
18
|
realtime: ItemDefinition;
|
|
19
|
+
stream: ItemDefinition;
|
|
19
20
|
queues: ItemDefinition;
|
|
20
21
|
cron: ItemDefinition;
|
|
21
22
|
workflows: ItemDefinition;
|
|
@@ -31,7 +32,7 @@ declare const PRODUCT_ITEMS: {
|
|
|
31
32
|
styling: ItemDefinition;
|
|
32
33
|
};
|
|
33
34
|
export type StandardProduct = keyof typeof PRODUCT_ITEMS;
|
|
34
|
-
export declare const STANDARD_PRODUCTS: readonly ("next" | "queues" | "basin" | "vinext" | "hono" | "realtime" | "cron" | "workflows" | "electron" | "wxt" | "expo" | "styling" | "neon" | "d1" | "neon-realtime" | "local-data" | "clerk" | "better-auth" | "r2" | "kv" | "openai" | "ai-sdk" | "workers-ai" | "resend" | "stripe" | "revenuecat" | "sentry" | "posthog")[];
|
|
35
|
+
export declare const STANDARD_PRODUCTS: readonly ("next" | "queues" | "basin" | "vinext" | "hono" | "realtime" | "cron" | "workflows" | "electron" | "wxt" | "expo" | "styling" | "neon" | "d1" | "neon-realtime" | "local-data" | "clerk" | "better-auth" | "r2" | "kv" | "stream" | "openai" | "ai-sdk" | "workers-ai" | "resend" | "stripe" | "revenuecat" | "sentry" | "posthog")[];
|
|
35
36
|
declare const DEFINITIONS: {
|
|
36
37
|
vinext: ItemDefinition;
|
|
37
38
|
next: ItemDefinition;
|
|
@@ -48,6 +49,7 @@ declare const DEFINITIONS: {
|
|
|
48
49
|
r2: ItemDefinition;
|
|
49
50
|
kv: ItemDefinition;
|
|
50
51
|
realtime: ItemDefinition;
|
|
52
|
+
stream: ItemDefinition;
|
|
51
53
|
queues: ItemDefinition;
|
|
52
54
|
cron: ItemDefinition;
|
|
53
55
|
workflows: ItemDefinition;
|
|
@@ -69,7 +71,7 @@ declare const DEFINITIONS: {
|
|
|
69
71
|
export type StackItemId = keyof typeof DEFINITIONS;
|
|
70
72
|
export declare function getStackDefinition(name: string): ItemDefinition;
|
|
71
73
|
export declare function getStackItem(name: string): StackRegistryItem;
|
|
72
|
-
export declare const STACK_ITEMS: Record<"next" | "queues" | "basin" | "vinext" | "hono" | "realtime" | "cron" | "workflows" | "electron" | "eslint" | "typescript" | "wxt" | "expo" | "ui" | "workspace" | "styling" | "neon" | "d1" | "neon-realtime" | "local-data" | "clerk" | "better-auth" | "r2" | "kv" | "openai" | "ai-sdk" | "workers-ai" | "resend" | "stripe" | "revenuecat" | "sentry" | "posthog", StackRegistryItem>;
|
|
74
|
+
export declare const STACK_ITEMS: Record<"next" | "queues" | "basin" | "vinext" | "hono" | "realtime" | "cron" | "workflows" | "electron" | "eslint" | "typescript" | "wxt" | "expo" | "ui" | "workspace" | "styling" | "neon" | "d1" | "neon-realtime" | "local-data" | "clerk" | "better-auth" | "r2" | "kv" | "stream" | "openai" | "ai-sdk" | "workers-ai" | "resend" | "stripe" | "revenuecat" | "sentry" | "posthog", StackRegistryItem>;
|
|
73
75
|
export declare const STACK_REGISTRY: {
|
|
74
76
|
schemaVersion: number;
|
|
75
77
|
name: string;
|
package/dist/stack-registry.js
CHANGED
|
@@ -7,6 +7,7 @@ import { GENERATOR_VERSION } from './version.js';
|
|
|
7
7
|
import { scaffoldWeb } from './scaffold/web.js';
|
|
8
8
|
import { scaffoldApi } from './scaffold/api.js';
|
|
9
9
|
import { scaffoldDatabase } from './scaffold/database.js';
|
|
10
|
+
import { scaffoldStream } from './scaffold/stream.js';
|
|
10
11
|
import { scaffoldNeonRealtime } from './scaffold/neon-realtime.js';
|
|
11
12
|
import { scaffoldBetterAuth, scaffoldClerk } from './scaffold/auth.js';
|
|
12
13
|
import { scaffoldRealtime, scaffoldQueues, scaffoldCron, scaffoldWorkflows, } from './scaffold/services.js';
|
|
@@ -231,6 +232,21 @@ export interface LocalDataStore {
|
|
|
231
232
|
setup: requirements('Rooms and collaboration', ['Wrangler', 'Browser control'], ['Cloudflare Durable Objects deployment access'], [], ['Realtime Worker, Durable Object namespace/migration and WebSocket authorization'], ['Authorized room isolation, disconnect/reconnect and deployed WebSockets']),
|
|
232
233
|
install: scaffoldRealtime,
|
|
233
234
|
},
|
|
235
|
+
stream: {
|
|
236
|
+
type: 'integration',
|
|
237
|
+
description: 'Cloudflare Stream one-to-many live video with WHIP ingest and WHEP playback (GA)',
|
|
238
|
+
targets: ['packages/stream'],
|
|
239
|
+
runtime: 'server',
|
|
240
|
+
environment: ['STREAM_ACCOUNT_ID', 'STREAM_API_TOKEN'],
|
|
241
|
+
setup: requirements('Ultra-low latency one-to-many live video', ['Cloudflare Stream API or dashboard', 'Browser control'], ['Client-owned Cloudflare account with Stream enabled; usage-based delivery billing'], [
|
|
242
|
+
'Server-only STREAM_ACCOUNT_ID and account-scoped STREAM_API_TOKEN with Stream Write permission',
|
|
243
|
+
], [
|
|
244
|
+
'Manual Stream activation and live input setup; server management factory with secret WHIP and WHEP endpoints',
|
|
245
|
+
], [
|
|
246
|
+
'Creator/input ownership, viewer authorization and signed playback when private, browser media/ICE, reconnect and session cleanup; live behavior unverified',
|
|
247
|
+
]),
|
|
248
|
+
install: scaffoldStream,
|
|
249
|
+
},
|
|
234
250
|
queues: {
|
|
235
251
|
type: 'service',
|
|
236
252
|
description: 'Queue producer binding, consumer and dead-letter handling',
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const LOCK_FILE = ".anhedral.lock";
|
|
2
|
+
type LockOwner = {
|
|
3
|
+
version: 1;
|
|
4
|
+
pid: number;
|
|
5
|
+
hostname: string;
|
|
6
|
+
token: string;
|
|
7
|
+
createdAt: string;
|
|
8
|
+
};
|
|
9
|
+
type TransactionLock = {
|
|
10
|
+
lockPath: string;
|
|
11
|
+
owner: LockOwner;
|
|
12
|
+
};
|
|
13
|
+
export declare function acquireTransactionLock(root: string): TransactionLock;
|
|
14
|
+
export declare function releaseTransactionLock(lock: TransactionLock): void;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { linkSync, lstatSync, readdirSync, rmSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { readBoundedFile } from './bounded-file.js';
|
|
6
|
+
export const LOCK_FILE = '.anhedral.lock';
|
|
7
|
+
function isRecord(value) {
|
|
8
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
function lstatIfPresent(target) {
|
|
11
|
+
try {
|
|
12
|
+
return lstatSync(target);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error.code === 'ENOENT')
|
|
16
|
+
return null;
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function pathEntryExists(target) {
|
|
21
|
+
return lstatIfPresent(target) !== null;
|
|
22
|
+
}
|
|
23
|
+
function parseLockOwner(lockPath) {
|
|
24
|
+
if (lstatSync(lockPath).isSymbolicLink()) {
|
|
25
|
+
throw new Error(`Refusing symbolic-link transaction lock at ${lockPath}.`);
|
|
26
|
+
}
|
|
27
|
+
let value;
|
|
28
|
+
try {
|
|
29
|
+
value = JSON.parse(readBoundedFile(lockPath, 16 * 1024).toString('utf8'));
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
throw new Error(`Cannot read transaction lock owner at ${lockPath}; remove it only after confirming no Anhedral process is running.`, { cause: error });
|
|
33
|
+
}
|
|
34
|
+
if (!isRecord(value) ||
|
|
35
|
+
value.version !== 1 ||
|
|
36
|
+
!Number.isSafeInteger(value.pid) ||
|
|
37
|
+
value.pid <= 0 ||
|
|
38
|
+
typeof value.hostname !== 'string' ||
|
|
39
|
+
typeof value.token !== 'string' ||
|
|
40
|
+
typeof value.createdAt !== 'string') {
|
|
41
|
+
throw new Error(`Invalid transaction lock owner at ${lockPath}; remove it only after confirming no Anhedral process is running.`);
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
function readLockOwnerIfPresent(lockPath) {
|
|
46
|
+
try {
|
|
47
|
+
return parseLockOwner(lockPath);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
const candidate = error;
|
|
51
|
+
if (candidate.code === 'ENOENT' || candidate.cause?.code === 'ENOENT')
|
|
52
|
+
return null;
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function isProcessAlive(pid) {
|
|
57
|
+
try {
|
|
58
|
+
process.kill(pid, 0);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
const code = error.code;
|
|
63
|
+
if (code === 'ESRCH')
|
|
64
|
+
return false;
|
|
65
|
+
if (code === 'EPERM')
|
|
66
|
+
return true;
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function cleanupTemporaryLocks(root, owner, temporaryPath) {
|
|
71
|
+
for (const entry of readdirSync(root)) {
|
|
72
|
+
if (!entry.startsWith(`${LOCK_FILE}.`) || !entry.endsWith('.tmp'))
|
|
73
|
+
continue;
|
|
74
|
+
const staleTemporaryPath = path.join(root, entry);
|
|
75
|
+
if (staleTemporaryPath === temporaryPath)
|
|
76
|
+
continue;
|
|
77
|
+
let temporaryOwner;
|
|
78
|
+
try {
|
|
79
|
+
temporaryOwner = parseLockOwner(staleTemporaryPath);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
throw new Error(`Cannot safely classify transaction lock artifact at ${staleTemporaryPath}; inspect it manually before retrying.`, { cause: error });
|
|
83
|
+
}
|
|
84
|
+
if (temporaryOwner.hostname !== owner.hostname) {
|
|
85
|
+
throw new Error(`Transaction lock artifact at ${staleTemporaryPath} belongs to another host; inspect it manually before retrying.`);
|
|
86
|
+
}
|
|
87
|
+
if (!isProcessAlive(temporaryOwner.pid)) {
|
|
88
|
+
unlinkSync(staleTemporaryPath);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function claimReclamation(lockPath, reclaimPath, temporaryPath, owner) {
|
|
93
|
+
try {
|
|
94
|
+
linkSync(temporaryPath, reclaimPath);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
if (error.code === 'EEXIST') {
|
|
98
|
+
const reclaimer = readLockOwnerIfPresent(reclaimPath);
|
|
99
|
+
if (!reclaimer) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
if (reclaimer.hostname === owner.hostname && !isProcessAlive(reclaimer.pid)) {
|
|
103
|
+
unlinkSync(reclaimPath);
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
throw new Error(`A stale Anhedral lock is being recovered (${lockPath}, pid ${reclaimer.pid} on ${reclaimer.hostname}); retry the command.`);
|
|
107
|
+
}
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
function reclaimTransactionLock(lockPath, reclaimPath, temporaryPath, owner) {
|
|
113
|
+
const existing = readLockOwnerIfPresent(lockPath);
|
|
114
|
+
if (!existing) {
|
|
115
|
+
return 'retry';
|
|
116
|
+
}
|
|
117
|
+
if (existing.hostname !== owner.hostname || isProcessAlive(existing.pid)) {
|
|
118
|
+
throw new Error(`Another Anhedral operation is already running (${lockPath}, pid ${existing.pid} on ${existing.hostname}).`);
|
|
119
|
+
}
|
|
120
|
+
if (!claimReclamation(lockPath, reclaimPath, temporaryPath, owner))
|
|
121
|
+
return 'retry';
|
|
122
|
+
try {
|
|
123
|
+
if (!pathEntryExists(lockPath))
|
|
124
|
+
return 'next';
|
|
125
|
+
const current = parseLockOwner(lockPath);
|
|
126
|
+
if (current.token !== existing.token)
|
|
127
|
+
return 'next';
|
|
128
|
+
if (current.hostname !== owner.hostname || isProcessAlive(current.pid)) {
|
|
129
|
+
throw new Error(`Another Anhedral operation is already running (${lockPath}, pid ${current.pid} on ${current.hostname}).`);
|
|
130
|
+
}
|
|
131
|
+
unlinkSync(lockPath);
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
if (pathEntryExists(reclaimPath)) {
|
|
135
|
+
const reclaimer = parseLockOwner(reclaimPath);
|
|
136
|
+
if (reclaimer.token === owner.token)
|
|
137
|
+
unlinkSync(reclaimPath);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return 'next';
|
|
141
|
+
}
|
|
142
|
+
function assertLockRoot(root) {
|
|
143
|
+
const rootStat = lstatIfPresent(root);
|
|
144
|
+
if (!rootStat)
|
|
145
|
+
throw new Error(`Transaction root is not a directory: ${root}`);
|
|
146
|
+
if (rootStat.isSymbolicLink()) {
|
|
147
|
+
throw new Error(`Refusing transaction root that is a symbolic link: ${root}`);
|
|
148
|
+
}
|
|
149
|
+
if (!rootStat.isDirectory())
|
|
150
|
+
throw new Error(`Transaction root is not a directory: ${root}`);
|
|
151
|
+
}
|
|
152
|
+
function clearDeadReclaimer(lockPath, reclaimPath, owner) {
|
|
153
|
+
const activeReclaimer = readLockOwnerIfPresent(reclaimPath);
|
|
154
|
+
if (activeReclaimer) {
|
|
155
|
+
if (activeReclaimer.hostname === owner.hostname && !isProcessAlive(activeReclaimer.pid)) {
|
|
156
|
+
try {
|
|
157
|
+
unlinkSync(reclaimPath);
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
if (error.code !== 'ENOENT')
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
throw new Error(`An Anhedral lock is being recovered (${lockPath}, pid ${activeReclaimer.pid} on ${activeReclaimer.hostname}); retry the command.`);
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
function cleanupAcquiredLock(root, lockPath, owner, temporaryPath) {
|
|
170
|
+
try {
|
|
171
|
+
cleanupTemporaryLocks(root, owner, temporaryPath);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
const current = readLockOwnerIfPresent(lockPath);
|
|
175
|
+
if (current?.token === owner.token)
|
|
176
|
+
unlinkSync(lockPath);
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
export function acquireTransactionLock(root) {
|
|
181
|
+
assertLockRoot(root);
|
|
182
|
+
const lockPath = path.join(root, LOCK_FILE);
|
|
183
|
+
const reclaimPath = `${lockPath}.reclaim`;
|
|
184
|
+
const owner = {
|
|
185
|
+
version: 1,
|
|
186
|
+
pid: process.pid,
|
|
187
|
+
hostname: os.hostname(),
|
|
188
|
+
token: `${process.pid}-${randomUUID()}`,
|
|
189
|
+
createdAt: new Date().toISOString(),
|
|
190
|
+
};
|
|
191
|
+
const temporaryPath = `${lockPath}.${owner.token}.tmp`;
|
|
192
|
+
writeFileSync(temporaryPath, JSON.stringify(owner) + '\n', {
|
|
193
|
+
encoding: 'utf8',
|
|
194
|
+
flag: 'wx',
|
|
195
|
+
mode: 0o600,
|
|
196
|
+
});
|
|
197
|
+
try {
|
|
198
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
199
|
+
if (clearDeadReclaimer(lockPath, reclaimPath, owner)) {
|
|
200
|
+
attempt -= 1;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
linkSync(temporaryPath, lockPath);
|
|
205
|
+
cleanupAcquiredLock(root, lockPath, owner, temporaryPath);
|
|
206
|
+
return { lockPath, owner };
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
if (error.code !== 'EEXIST')
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
if (reclaimTransactionLock(lockPath, reclaimPath, temporaryPath, owner) === 'retry')
|
|
213
|
+
attempt -= 1;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
finally {
|
|
217
|
+
rmSync(temporaryPath, { force: true });
|
|
218
|
+
}
|
|
219
|
+
throw new Error(`Could not acquire Anhedral transaction lock (${lockPath}).`);
|
|
220
|
+
}
|
|
221
|
+
export function releaseTransactionLock(lock) {
|
|
222
|
+
if (!pathEntryExists(lock.lockPath))
|
|
223
|
+
return;
|
|
224
|
+
const current = parseLockOwner(lock.lockPath);
|
|
225
|
+
if (current.token === lock.owner.token)
|
|
226
|
+
unlinkSync(lock.lockPath);
|
|
227
|
+
}
|
package/dist/transaction.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
import { LOCK_FILE, acquireTransactionLock, releaseTransactionLock } from './transaction-lock.js';
|
|
1
2
|
import { readBoundedFile } from './bounded-file.js';
|
|
2
|
-
import { closeSync, cpSync, fsyncSync,
|
|
3
|
+
import { closeSync, cpSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmdirSync, rmSync, unlinkSync, writeFileSync, } from 'node:fs';
|
|
3
4
|
import { createHash, randomUUID } from 'node:crypto';
|
|
4
|
-
import os from 'node:os';
|
|
5
5
|
import path from 'node:path';
|
|
6
|
-
const LOCK_FILE = '.anhedral.lock';
|
|
7
6
|
const JOURNAL_FILE = '.anhedral-journal.json';
|
|
8
7
|
const TRANSACTION_DIRECTORY = '.anhedral-txn';
|
|
9
8
|
const JOURNAL_VERSION = 2;
|
|
@@ -235,211 +234,6 @@ function removeEntryDurably(target) {
|
|
|
235
234
|
if (pathEntryExists(parent))
|
|
236
235
|
syncDirectory(parent);
|
|
237
236
|
}
|
|
238
|
-
function parseLockOwner(lockPath) {
|
|
239
|
-
if (lstatSync(lockPath).isSymbolicLink()) {
|
|
240
|
-
throw new Error(`Refusing symbolic-link transaction lock at ${lockPath}.`);
|
|
241
|
-
}
|
|
242
|
-
let value;
|
|
243
|
-
try {
|
|
244
|
-
value = JSON.parse(readBoundedFile(lockPath, 16 * 1024).toString('utf8'));
|
|
245
|
-
}
|
|
246
|
-
catch (error) {
|
|
247
|
-
throw new Error(`Cannot read transaction lock owner at ${lockPath}; remove it only after confirming no Anhedral process is running.`, { cause: error });
|
|
248
|
-
}
|
|
249
|
-
if (!isRecord(value) ||
|
|
250
|
-
value.version !== 1 ||
|
|
251
|
-
!Number.isSafeInteger(value.pid) ||
|
|
252
|
-
value.pid <= 0 ||
|
|
253
|
-
typeof value.hostname !== 'string' ||
|
|
254
|
-
typeof value.token !== 'string' ||
|
|
255
|
-
typeof value.createdAt !== 'string') {
|
|
256
|
-
throw new Error(`Invalid transaction lock owner at ${lockPath}; remove it only after confirming no Anhedral process is running.`);
|
|
257
|
-
}
|
|
258
|
-
return value;
|
|
259
|
-
}
|
|
260
|
-
function readLockOwnerIfPresent(lockPath) {
|
|
261
|
-
try {
|
|
262
|
-
return parseLockOwner(lockPath);
|
|
263
|
-
}
|
|
264
|
-
catch (error) {
|
|
265
|
-
const candidate = error;
|
|
266
|
-
if (candidate.code === 'ENOENT' || candidate.cause?.code === 'ENOENT')
|
|
267
|
-
return null;
|
|
268
|
-
throw error;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
function isProcessAlive(pid) {
|
|
272
|
-
try {
|
|
273
|
-
process.kill(pid, 0);
|
|
274
|
-
return true;
|
|
275
|
-
}
|
|
276
|
-
catch (error) {
|
|
277
|
-
const code = error.code;
|
|
278
|
-
if (code === 'ESRCH')
|
|
279
|
-
return false;
|
|
280
|
-
if (code === 'EPERM')
|
|
281
|
-
return true;
|
|
282
|
-
throw error;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
function cleanupTemporaryLocks(root, owner, temporaryPath) {
|
|
286
|
-
for (const entry of readdirSync(root)) {
|
|
287
|
-
if (!entry.startsWith(`${LOCK_FILE}.`) || !entry.endsWith('.tmp'))
|
|
288
|
-
continue;
|
|
289
|
-
const staleTemporaryPath = path.join(root, entry);
|
|
290
|
-
if (staleTemporaryPath === temporaryPath)
|
|
291
|
-
continue;
|
|
292
|
-
let temporaryOwner;
|
|
293
|
-
try {
|
|
294
|
-
temporaryOwner = parseLockOwner(staleTemporaryPath);
|
|
295
|
-
}
|
|
296
|
-
catch (error) {
|
|
297
|
-
throw new Error(`Cannot safely classify transaction lock artifact at ${staleTemporaryPath}; inspect it manually before retrying.`, { cause: error });
|
|
298
|
-
}
|
|
299
|
-
if (temporaryOwner.hostname !== owner.hostname) {
|
|
300
|
-
throw new Error(`Transaction lock artifact at ${staleTemporaryPath} belongs to another host; inspect it manually before retrying.`);
|
|
301
|
-
}
|
|
302
|
-
if (!isProcessAlive(temporaryOwner.pid)) {
|
|
303
|
-
unlinkSync(staleTemporaryPath);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
function claimReclamation(lockPath, reclaimPath, temporaryPath, owner) {
|
|
308
|
-
try {
|
|
309
|
-
linkSync(temporaryPath, reclaimPath);
|
|
310
|
-
}
|
|
311
|
-
catch (error) {
|
|
312
|
-
if (error.code === 'EEXIST') {
|
|
313
|
-
const reclaimer = readLockOwnerIfPresent(reclaimPath);
|
|
314
|
-
if (!reclaimer) {
|
|
315
|
-
return false;
|
|
316
|
-
}
|
|
317
|
-
if (reclaimer.hostname === owner.hostname && !isProcessAlive(reclaimer.pid)) {
|
|
318
|
-
unlinkSync(reclaimPath);
|
|
319
|
-
return false;
|
|
320
|
-
}
|
|
321
|
-
throw new Error(`A stale Anhedral lock is being recovered (${lockPath}, pid ${reclaimer.pid} on ${reclaimer.hostname}); retry the command.`);
|
|
322
|
-
}
|
|
323
|
-
throw error;
|
|
324
|
-
}
|
|
325
|
-
return true;
|
|
326
|
-
}
|
|
327
|
-
function reclaimTransactionLock(lockPath, reclaimPath, temporaryPath, owner) {
|
|
328
|
-
const existing = readLockOwnerIfPresent(lockPath);
|
|
329
|
-
if (!existing) {
|
|
330
|
-
return 'retry';
|
|
331
|
-
}
|
|
332
|
-
if (existing.hostname !== owner.hostname || isProcessAlive(existing.pid)) {
|
|
333
|
-
throw new Error(`Another Anhedral operation is already running (${lockPath}, pid ${existing.pid} on ${existing.hostname}).`);
|
|
334
|
-
}
|
|
335
|
-
if (!claimReclamation(lockPath, reclaimPath, temporaryPath, owner))
|
|
336
|
-
return 'retry';
|
|
337
|
-
try {
|
|
338
|
-
if (!pathEntryExists(lockPath))
|
|
339
|
-
return 'next';
|
|
340
|
-
const current = parseLockOwner(lockPath);
|
|
341
|
-
if (current.token !== existing.token)
|
|
342
|
-
return 'next';
|
|
343
|
-
if (current.hostname !== owner.hostname || isProcessAlive(current.pid)) {
|
|
344
|
-
throw new Error(`Another Anhedral operation is already running (${lockPath}, pid ${current.pid} on ${current.hostname}).`);
|
|
345
|
-
}
|
|
346
|
-
unlinkSync(lockPath);
|
|
347
|
-
}
|
|
348
|
-
finally {
|
|
349
|
-
if (pathEntryExists(reclaimPath)) {
|
|
350
|
-
const reclaimer = parseLockOwner(reclaimPath);
|
|
351
|
-
if (reclaimer.token === owner.token)
|
|
352
|
-
unlinkSync(reclaimPath);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
return 'next';
|
|
356
|
-
}
|
|
357
|
-
function assertLockRoot(root) {
|
|
358
|
-
const rootStat = lstatIfPresent(root);
|
|
359
|
-
if (!rootStat)
|
|
360
|
-
throw new Error(`Transaction root is not a directory: ${root}`);
|
|
361
|
-
if (rootStat.isSymbolicLink()) {
|
|
362
|
-
throw new Error(`Refusing transaction root that is a symbolic link: ${root}`);
|
|
363
|
-
}
|
|
364
|
-
if (!rootStat.isDirectory())
|
|
365
|
-
throw new Error(`Transaction root is not a directory: ${root}`);
|
|
366
|
-
}
|
|
367
|
-
function clearDeadReclaimer(lockPath, reclaimPath, owner) {
|
|
368
|
-
const activeReclaimer = readLockOwnerIfPresent(reclaimPath);
|
|
369
|
-
if (activeReclaimer) {
|
|
370
|
-
if (activeReclaimer.hostname === owner.hostname && !isProcessAlive(activeReclaimer.pid)) {
|
|
371
|
-
try {
|
|
372
|
-
unlinkSync(reclaimPath);
|
|
373
|
-
}
|
|
374
|
-
catch (error) {
|
|
375
|
-
if (error.code !== 'ENOENT')
|
|
376
|
-
throw error;
|
|
377
|
-
}
|
|
378
|
-
return true;
|
|
379
|
-
}
|
|
380
|
-
throw new Error(`An Anhedral lock is being recovered (${lockPath}, pid ${activeReclaimer.pid} on ${activeReclaimer.hostname}); retry the command.`);
|
|
381
|
-
}
|
|
382
|
-
return false;
|
|
383
|
-
}
|
|
384
|
-
function cleanupAcquiredLock(root, lockPath, owner, temporaryPath) {
|
|
385
|
-
try {
|
|
386
|
-
cleanupTemporaryLocks(root, owner, temporaryPath);
|
|
387
|
-
}
|
|
388
|
-
catch (error) {
|
|
389
|
-
const current = readLockOwnerIfPresent(lockPath);
|
|
390
|
-
if (current?.token === owner.token)
|
|
391
|
-
unlinkSync(lockPath);
|
|
392
|
-
throw error;
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
function acquireTransactionLock(root) {
|
|
396
|
-
assertLockRoot(root);
|
|
397
|
-
const lockPath = path.join(root, LOCK_FILE);
|
|
398
|
-
const reclaimPath = `${lockPath}.reclaim`;
|
|
399
|
-
const owner = {
|
|
400
|
-
version: 1,
|
|
401
|
-
pid: process.pid,
|
|
402
|
-
hostname: os.hostname(),
|
|
403
|
-
token: `${process.pid}-${randomUUID()}`,
|
|
404
|
-
createdAt: new Date().toISOString(),
|
|
405
|
-
};
|
|
406
|
-
const temporaryPath = `${lockPath}.${owner.token}.tmp`;
|
|
407
|
-
writeFileSync(temporaryPath, JSON.stringify(owner) + '\n', {
|
|
408
|
-
encoding: 'utf8',
|
|
409
|
-
flag: 'wx',
|
|
410
|
-
mode: 0o600,
|
|
411
|
-
});
|
|
412
|
-
try {
|
|
413
|
-
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
414
|
-
if (clearDeadReclaimer(lockPath, reclaimPath, owner)) {
|
|
415
|
-
attempt -= 1;
|
|
416
|
-
continue;
|
|
417
|
-
}
|
|
418
|
-
try {
|
|
419
|
-
linkSync(temporaryPath, lockPath);
|
|
420
|
-
cleanupAcquiredLock(root, lockPath, owner, temporaryPath);
|
|
421
|
-
return { lockPath, owner };
|
|
422
|
-
}
|
|
423
|
-
catch (error) {
|
|
424
|
-
if (error.code !== 'EEXIST')
|
|
425
|
-
throw error;
|
|
426
|
-
}
|
|
427
|
-
if (reclaimTransactionLock(lockPath, reclaimPath, temporaryPath, owner) === 'retry')
|
|
428
|
-
attempt -= 1;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
finally {
|
|
432
|
-
rmSync(temporaryPath, { force: true });
|
|
433
|
-
}
|
|
434
|
-
throw new Error(`Could not acquire Anhedral transaction lock (${lockPath}).`);
|
|
435
|
-
}
|
|
436
|
-
function releaseTransactionLock(lock) {
|
|
437
|
-
if (!pathEntryExists(lock.lockPath))
|
|
438
|
-
return;
|
|
439
|
-
const current = parseLockOwner(lock.lockPath);
|
|
440
|
-
if (current.token === lock.owner.token)
|
|
441
|
-
unlinkSync(lock.lockPath);
|
|
442
|
-
}
|
|
443
237
|
function assertExpectedTempRoot(root, candidate, kind) {
|
|
444
238
|
const resolved = path.resolve(candidate);
|
|
445
239
|
const tokenPattern = '(\\d+-[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})';
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { DeliveryRunner } from './project-state.js';
|
|
2
|
+
export declare const cli: string[];
|
|
3
|
+
export interface WebProject {
|
|
4
|
+
schemaVersion: 1;
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
ownership: 'customer';
|
|
8
|
+
provider: 'vercel';
|
|
9
|
+
directory: 'apps/web';
|
|
10
|
+
teamId: string | null;
|
|
11
|
+
projectId: string | null;
|
|
12
|
+
scope: string | null;
|
|
13
|
+
audience: 'unconfigured' | 'public' | 'private';
|
|
14
|
+
use: 'unconfigured' | 'personal' | 'commercial';
|
|
15
|
+
repository?: {
|
|
16
|
+
repoId: number;
|
|
17
|
+
nameWithOwner: string;
|
|
18
|
+
productionBranch?: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export interface WebRelease {
|
|
22
|
+
id: string;
|
|
23
|
+
commit: string;
|
|
24
|
+
operation: 'ship' | 'rollback' | 'adopt' | 'recover';
|
|
25
|
+
provenance?: 'cli' | 'git';
|
|
26
|
+
branch?: string;
|
|
27
|
+
state: 'running' | 'deployed' | 'failed';
|
|
28
|
+
startedAt: string;
|
|
29
|
+
deploymentId?: string;
|
|
30
|
+
url?: string;
|
|
31
|
+
deploymentUrl?: string;
|
|
32
|
+
restoredReleaseId?: string;
|
|
33
|
+
verification?: {
|
|
34
|
+
state: 'passed' | 'failed';
|
|
35
|
+
checkedAt: string;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export interface ProviderProject {
|
|
39
|
+
id: string;
|
|
40
|
+
name: string;
|
|
41
|
+
accountId: string;
|
|
42
|
+
framework: string;
|
|
43
|
+
rootDirectory: string;
|
|
44
|
+
sourceFilesOutsideRootDirectory?: boolean;
|
|
45
|
+
link?: {
|
|
46
|
+
type: string;
|
|
47
|
+
org?: string;
|
|
48
|
+
repo?: string;
|
|
49
|
+
repoId?: number;
|
|
50
|
+
productionBranch?: string;
|
|
51
|
+
};
|
|
52
|
+
ssoProtection?: {
|
|
53
|
+
deploymentType?: string;
|
|
54
|
+
};
|
|
55
|
+
passwordProtection?: unknown;
|
|
56
|
+
targets?: {
|
|
57
|
+
production?: {
|
|
58
|
+
id?: string;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export interface ProviderDeployment {
|
|
63
|
+
id: string;
|
|
64
|
+
projectId: string;
|
|
65
|
+
ownerId: string;
|
|
66
|
+
readyState: string;
|
|
67
|
+
target: string;
|
|
68
|
+
url: string;
|
|
69
|
+
alias?: string[];
|
|
70
|
+
meta?: Record<string, string>;
|
|
71
|
+
gitSource?: {
|
|
72
|
+
type?: string;
|
|
73
|
+
repoId?: number | string;
|
|
74
|
+
org?: string;
|
|
75
|
+
repo?: string;
|
|
76
|
+
sha?: string;
|
|
77
|
+
ref?: string;
|
|
78
|
+
prId?: number | null;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export declare function registerVercelApplication(root: string): WebProject;
|
|
82
|
+
export declare function readWeb(root: string): WebProject;
|
|
83
|
+
export declare function env(project?: WebProject): NodeJS.ProcessEnv;
|
|
84
|
+
export declare function context(root: string, project: WebProject): void;
|
|
85
|
+
export declare function api(root: string, run: DeliveryRunner, endpoint: string, scope?: string, body?: unknown, method?: string): Promise<any>;
|
|
86
|
+
export declare function paginated(root: string, run: DeliveryRunner, endpoint: string, key: string, scope?: string): Promise<any[]>;
|
|
87
|
+
export declare function validateProvider(project: WebProject, value: ProviderProject): void;
|
|
88
|
+
export declare function matchesRepository(link: ProviderProject['link'], repository: NonNullable<WebProject['repository']>): boolean;
|
|
89
|
+
export declare function inspect(root: string, project: WebProject, run: DeliveryRunner): Promise<ProviderProject>;
|
|
90
|
+
export declare function history(root: string, project: WebProject): {
|
|
91
|
+
schemaVersion: 1;
|
|
92
|
+
projectId: string;
|
|
93
|
+
releases: WebRelease[];
|
|
94
|
+
};
|
|
95
|
+
export declare function source(root: string, run: DeliveryRunner): Promise<string>;
|
|
96
|
+
export declare function https(value: string): string;
|
|
97
|
+
export declare function validBranch(branch: string): boolean;
|
|
98
|
+
export declare function deployment(project: WebProject, value: ProviderDeployment, commit: string, provenance?: 'cli' | 'git', branch?: string): void;
|
|
99
|
+
export declare function validateRelease(project: WebProject, value: ProviderDeployment, release: WebRelease): void;
|
|
100
|
+
export declare function committedManifest(root: string, project: WebProject, run: DeliveryRunner): Promise<string>;
|
|
101
|
+
export declare function remoteBranch(root: string, branch: string, commit: string, run: DeliveryRunner): Promise<void>;
|