@openfairygui/backend 0.2.0-alpha.3 → 0.2.0-alpha.30

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.
@@ -0,0 +1,513 @@
1
+ import type { UamProject, UamTransactionOperation } from '@openfairygui/core/uam';
2
+ import type { ApplyUamTransactionAppError } from '@openfairygui/functions/uam';
3
+ import type {
4
+ BACKEND_CAPABILITY_SCHEMA_VERSION,
5
+ BACKEND_COMPATIBILITY_POLICY,
6
+ BACKEND_CONTRACT_VERSION,
7
+ BackendDiagnostic,
8
+ BackendResponseMeta,
9
+ } from '../contracts.js';
10
+ import type { PathPolicyViolationError } from '../path-policy.js';
11
+
12
+ export interface BackendFileHandle {
13
+ writeFile(content: string): Promise<void>;
14
+ close(): Promise<void>;
15
+ }
16
+
17
+ export interface BackendFileStat {
18
+ isFile(): boolean;
19
+ isDirectory(): boolean;
20
+ }
21
+
22
+ export interface BackendFileSystem {
23
+ stat(filePath: string): Promise<BackendFileStat>;
24
+ readdir(dirPath: string): Promise<string[]>;
25
+ readFile(filePath: string): Promise<string>;
26
+ readFileRaw(filePath: string): Promise<Uint8Array>;
27
+ writeFile(filePath: string, content: string): Promise<void>;
28
+ writeFileRaw(filePath: string, data: Uint8Array): Promise<void>;
29
+ mkdir(dirPath: string, options?: { recursive?: boolean }): Promise<void>;
30
+ resolvePath(filePath: string): Promise<string>;
31
+ openExclusive(filePath: string): Promise<BackendFileHandle>;
32
+ unlink(filePath: string): Promise<void>;
33
+ rmdir(dirPath: string): Promise<void>;
34
+ join(...paths: string[]): string;
35
+ dirname(filePath: string): string;
36
+ resolve(...paths: string[]): string;
37
+ }
38
+
39
+ export interface BackendHostAdapter {
40
+ lockMetadata?(input: { canonicalPathKey: string; canonicalProjectPath: string; lockFilePath: string }): unknown;
41
+ }
42
+
43
+ export interface BackendArtifactBridgeCapability {
44
+ available: false;
45
+ requiredHost: 'node';
46
+ executionBoundary: 'external-bridge';
47
+ bridgeEntrypoint: '@openfairygui/backend/node';
48
+ reason: string;
49
+ }
50
+
51
+ export interface BackendCapabilityManifest {
52
+ browserSafe: true;
53
+ rootEntrypoint: '@openfairygui/backend';
54
+ nodeEntrypoint: '@openfairygui/backend/node';
55
+ adapters: {
56
+ fileSystem: {
57
+ injected: true;
58
+ requiredFor: readonly ['openSession', 'saveSession', 'materializeSession'];
59
+ };
60
+ projectStorage: {
61
+ injected: true;
62
+ browserSafe: true;
63
+ requiredFor: readonly ['openProjectSession.writeback', 'saveSession', 'materializeSession'];
64
+ adapterFactory: 'createBackendStorageFileSystem';
65
+ };
66
+ host: {
67
+ injected: true;
68
+ requiredFor: readonly ['advisoryLockMetadata'];
69
+ };
70
+ };
71
+ executionBoundaries: {
72
+ projectSession: 'in-process-browser-safe';
73
+ fileBackedSession: 'adapter-backed';
74
+ artifactPublish: BackendArtifactBridgeCapability;
75
+ artifactRestore: BackendArtifactBridgeCapability;
76
+ };
77
+ diagnostics: {
78
+ stableCodes: true;
79
+ errorDiagnosticMirror: true;
80
+ };
81
+ }
82
+
83
+ export interface BackendCapabilities {
84
+ contractVersion: typeof BACKEND_CONTRACT_VERSION;
85
+ capabilitySchemaVersion: typeof BACKEND_CAPABILITY_SCHEMA_VERSION;
86
+ transactionKernelOwner: '@openfairygui/core';
87
+ appSeamOwner: '@openfairygui/functions';
88
+ runtimeOwner: '@openfairygui/backend';
89
+ methods: readonly [
90
+ 'getCapabilities',
91
+ 'openSession',
92
+ 'openProjectSession',
93
+ 'getSession',
94
+ 'applyTransaction',
95
+ 'saveSession',
96
+ 'materializeSession',
97
+ 'closeSession',
98
+ 'getEvents',
99
+ 'getJob',
100
+ 'listJobs',
101
+ 'cancelJob',
102
+ 'getCacheSnapshot',
103
+ 'refreshCache',
104
+ ];
105
+ read: {
106
+ capabilitySnapshot: true;
107
+ sessionSnapshot: true;
108
+ };
109
+ authoring: {
110
+ applyTransaction: true;
111
+ saveSession: true;
112
+ resourceKinds: readonly string[];
113
+ nodeKinds: readonly string[];
114
+ gearKinds: readonly string[];
115
+ transactionScope: {
116
+ resourceKinds: readonly string[];
117
+ nodeKinds: readonly string[];
118
+ gearKinds: readonly string[];
119
+ };
120
+ unsupported: readonly ['artifact.publish', 'artifact.restore'];
121
+ };
122
+ artifact: {
123
+ publish: false;
124
+ restore: false;
125
+ status: 'bridge-required';
126
+ publishBridge: BackendArtifactBridgeCapability;
127
+ restoreBridge: BackendArtifactBridgeCapability;
128
+ };
129
+ manifest: BackendCapabilityManifest;
130
+ compatibilityPolicy: typeof BACKEND_COMPATIBILITY_POLICY;
131
+ runtime: {
132
+ sessionRuntime: true;
133
+ advisoryLocking: true;
134
+ coordinatedSave: true;
135
+ atomicSave: false;
136
+ staleRevisionProtection: true;
137
+ pathPolicy: {
138
+ canonicalization: 'realpath+normalized-casefold';
139
+ sessionIdentity: 'project-root';
140
+ saveTarget: 'opened-project-only';
141
+ outputTargets: 'deferred';
142
+ workspaceBoundary: 'project-root-only';
143
+ };
144
+ events: {
145
+ polling: true;
146
+ subscriptions: false;
147
+ retentionLimit: 1000;
148
+ sequenceScope: 'runtime';
149
+ };
150
+ jobs: {
151
+ inMemory: true;
152
+ cooperativeCancel: true;
153
+ persistent: false;
154
+ supportedKinds: readonly ['cache.refresh'];
155
+ artifactJobs: false;
156
+ completedRetentionLimit: 100;
157
+ };
158
+ cache: {
159
+ derivedReadOnly: true;
160
+ keyedBy: 'canonicalPathKey';
161
+ sourceOfTruth: false;
162
+ refreshMethod: 'refreshCache';
163
+ };
164
+ };
165
+ }
166
+
167
+ export interface BackendSessionSnapshot {
168
+ sessionId: string;
169
+ canonicalProjectPath: string;
170
+ revision: number;
171
+ lastSavedRevision: number;
172
+ dirty: boolean;
173
+ uamFidelity: 'full' | 'unsupported';
174
+ lockHeld: boolean;
175
+ capabilities: BackendCapabilities;
176
+ }
177
+
178
+ export interface MaterializeSessionSnapshot extends BackendSessionSnapshot {
179
+ mode: 'fullProject';
180
+ reason?: string;
181
+ materializeRevision: number;
182
+ saveRevision: number;
183
+ writtenPaths: string[];
184
+ skippedPaths: string[];
185
+ diagnostics: BackendDiagnostic[];
186
+ }
187
+
188
+ export interface BackendSuccess<T> {
189
+ ok: true;
190
+ meta: BackendResponseMeta;
191
+ data: T;
192
+ }
193
+
194
+ export interface BackendFailure<E extends BackendError = BackendError> {
195
+ ok: false;
196
+ meta: BackendResponseMeta;
197
+ error: E;
198
+ session?: BackendSessionSnapshot;
199
+ }
200
+
201
+ export type BackendResult<T, E extends BackendError = BackendError> = BackendSuccess<T> | BackendFailure<E>;
202
+
203
+ export interface SessionNotFoundError {
204
+ code: 'session_not_found';
205
+ message: string;
206
+ sessionId: string;
207
+ }
208
+
209
+ export interface SessionStaleWriteError {
210
+ code: 'stale_write';
211
+ message: string;
212
+ sessionId: string;
213
+ canonicalPathKey: string;
214
+ expectedRevision: number;
215
+ actualRevision: number;
216
+ }
217
+
218
+ export interface InProcessLockConflictError {
219
+ code: 'lock_conflict';
220
+ kind: 'in_process_session_exists';
221
+ message: string;
222
+ canonicalPathKey: string;
223
+ holderSessionId: string;
224
+ lockFilePath?: string;
225
+ }
226
+
227
+ export interface AdvisoryLockConflictError {
228
+ code: 'lock_conflict';
229
+ kind: 'advisory_lock_conflict';
230
+ message: string;
231
+ canonicalPathKey: string;
232
+ holderSessionId?: string;
233
+ lockFilePath: string;
234
+ }
235
+
236
+ export interface SavePartialFailureError {
237
+ code: 'save_partial_failure';
238
+ message: string;
239
+ sessionId: string;
240
+ canonicalPathKey: string;
241
+ attemptedRevision: number;
242
+ lastSavedRevision: number;
243
+ committedPaths: string[];
244
+ failedPaths: string[];
245
+ diskMayBePartiallyUpdated: true;
246
+ }
247
+
248
+ export interface UamFidelityUnsupportedError {
249
+ code: 'uam_fidelity_unsupported';
250
+ message: string;
251
+ sessionId: string;
252
+ canonicalPathKey: string;
253
+ }
254
+
255
+ export interface MaterializeValidationFailedError {
256
+ code: 'materialize_validation_failed';
257
+ message: string;
258
+ sessionId: string;
259
+ canonicalPathKey: string;
260
+ issueCount: number;
261
+ diagnostics: BackendDiagnostic[];
262
+ }
263
+
264
+ export interface MaterializeWriteFailedError {
265
+ code: 'write_failed';
266
+ message: string;
267
+ sessionId: string;
268
+ canonicalPathKey: string;
269
+ attemptedRevision: number;
270
+ lastSavedRevision: number;
271
+ writtenPaths: string[];
272
+ failedPaths: string[];
273
+ skippedPaths: string[];
274
+ diagnostics: BackendDiagnostic[];
275
+ diskMayBePartiallyUpdated: true;
276
+ }
277
+
278
+ export type BackendEventKind =
279
+ | 'session.opened'
280
+ | 'transaction.applied'
281
+ | 'transaction.rejected'
282
+ | 'save.started'
283
+ | 'save.completed'
284
+ | 'save.failed'
285
+ | 'session.closeRequested'
286
+ | 'session.closed'
287
+ | 'cache.invalidated'
288
+ | 'cache.updated'
289
+ | 'job.created'
290
+ | 'job.started'
291
+ | 'job.progress'
292
+ | 'job.cancelRequested'
293
+ | 'job.cancelled'
294
+ | 'job.completed'
295
+ | 'job.failed';
296
+
297
+ export interface BackendEvent {
298
+ sequence: number;
299
+ kind: BackendEventKind;
300
+ timestamp: string;
301
+ sessionId?: string;
302
+ canonicalPathKey?: string;
303
+ revision?: number;
304
+ cacheRevision?: number;
305
+ jobId?: string;
306
+ diagnostics: BackendDiagnostic[];
307
+ payload?: unknown;
308
+ }
309
+
310
+ export interface GetEventsInput {
311
+ sessionId: string;
312
+ after?: string;
313
+ limit?: number;
314
+ }
315
+
316
+ export interface GetEventsSnapshot {
317
+ events: BackendEvent[];
318
+ oldestSequence: number;
319
+ currentSequence: number;
320
+ cursorExpired: boolean;
321
+ }
322
+
323
+ export interface EventCursorInvalidError {
324
+ code: 'event_cursor_invalid';
325
+ message: string;
326
+ sessionId: string;
327
+ after: string;
328
+ }
329
+
330
+ export type BackendJobStatus = 'queued' | 'running' | 'completed' | 'failed' | 'cancelled';
331
+ export type BackendJobKind = 'cache.refresh';
332
+ export type BackendJobListStatusFilter = BackendJobStatus | 'active' | 'terminal';
333
+
334
+ export interface BackendJobProgress {
335
+ completed: number;
336
+ total?: number;
337
+ message?: string;
338
+ }
339
+
340
+ export interface BackendJobSnapshot {
341
+ jobId: string;
342
+ kind: BackendJobKind;
343
+ status: BackendJobStatus;
344
+ createdAt: string;
345
+ startedAt?: string;
346
+ finishedAt?: string;
347
+ sessionId?: string;
348
+ canonicalPathKey?: string;
349
+ revision?: number;
350
+ cacheRevision?: number;
351
+ diagnostics: BackendDiagnostic[];
352
+ progress?: BackendJobProgress;
353
+ result?: unknown;
354
+ error?: BackendError;
355
+ }
356
+
357
+ export interface BackendJobListSnapshot {
358
+ jobs: BackendJobSnapshot[];
359
+ }
360
+
361
+ export interface GetJobInput {
362
+ sessionId: string;
363
+ jobId: string;
364
+ }
365
+
366
+ export interface ListJobsInput {
367
+ sessionId: string;
368
+ status?: BackendJobListStatusFilter;
369
+ kind?: BackendJobKind;
370
+ limit?: number;
371
+ }
372
+
373
+ export interface CancelJobInput {
374
+ sessionId: string;
375
+ jobId: string;
376
+ }
377
+
378
+ export interface BackendJobNotFoundError {
379
+ code: 'job_not_found';
380
+ message: string;
381
+ sessionId: string;
382
+ jobId: string;
383
+ }
384
+
385
+ export interface BackendJobNotCancellableError {
386
+ code: 'job_not_cancellable';
387
+ message: string;
388
+ sessionId: string;
389
+ jobId: string;
390
+ status: 'completed' | 'failed' | 'cancelled';
391
+ }
392
+
393
+ export interface BackendJobCancelledError {
394
+ code: 'job_cancelled';
395
+ message: string;
396
+ sessionId: string;
397
+ jobId: string;
398
+ }
399
+
400
+ export interface CacheRefreshFailedError {
401
+ code: 'cache_refresh_failed';
402
+ message: string;
403
+ sessionId: string;
404
+ jobId: string;
405
+ causeCode?: string;
406
+ }
407
+
408
+ export interface BackendCapabilityUnavailableError {
409
+ code: 'capability_unavailable';
410
+ message: string;
411
+ capability: 'fileSystem' | 'artifact.publish' | 'artifact.restore';
412
+ requiredAdapter?: 'BackendFileSystem';
413
+ requiredHost?: 'node';
414
+ bridgeBoundary?: 'external-bridge';
415
+ }
416
+
417
+ export type BackendJobErrors =
418
+ | BackendJobNotFoundError
419
+ | BackendJobNotCancellableError
420
+ | BackendJobCancelledError
421
+ | CacheRefreshFailedError;
422
+
423
+ export interface BackendCacheSnapshot {
424
+ cacheRevision: number;
425
+ entries: BackendCacheEntry[];
426
+ }
427
+
428
+ export interface BackendCacheEntry {
429
+ canonicalPathKey: string;
430
+ sessionId?: string;
431
+ revision: number;
432
+ lastSavedRevision: number;
433
+ dirty: boolean;
434
+ valid: boolean;
435
+ indexedAt: string;
436
+ summary: {
437
+ resourceCount: number;
438
+ packageCount?: number;
439
+ diagnostics: BackendDiagnostic[];
440
+ };
441
+ }
442
+
443
+ export interface GetCacheSnapshotInput {
444
+ sessionId: string;
445
+ }
446
+
447
+ export interface RefreshCacheInput {
448
+ sessionId: string;
449
+ reason?: 'manual' | 'session_open' | 'after_save';
450
+ }
451
+
452
+ export type BackendError =
453
+ | SessionNotFoundError
454
+ | SessionStaleWriteError
455
+ | InProcessLockConflictError
456
+ | AdvisoryLockConflictError
457
+ | SavePartialFailureError
458
+ | UamFidelityUnsupportedError
459
+ | MaterializeValidationFailedError
460
+ | MaterializeWriteFailedError
461
+ | PathPolicyViolationError
462
+ | EventCursorInvalidError
463
+ | BackendJobNotFoundError
464
+ | BackendJobNotCancellableError
465
+ | BackendJobCancelledError
466
+ | CacheRefreshFailedError
467
+ | BackendCapabilityUnavailableError
468
+ | ApplyUamTransactionAppError;
469
+
470
+ export interface ApplySessionTransactionInput {
471
+ sessionId: string;
472
+ expectedRevision: number;
473
+ operations: UamTransactionOperation[];
474
+ }
475
+
476
+ export interface OpenProjectSessionInput {
477
+ project: UamProject;
478
+ sessionId?: string;
479
+ canonicalProjectPath?: string;
480
+ canonicalPathKey?: string;
481
+ storage?: BackendProjectSessionStorage;
482
+ }
483
+
484
+ export interface BackendProjectSessionStorage {
485
+ fileSystem: BackendFileSystem;
486
+ fairyPath: string;
487
+ canonicalProjectPath?: string;
488
+ canonicalPathKey?: string;
489
+ }
490
+
491
+ export interface SaveSessionInput {
492
+ sessionId: string;
493
+ expectedRevision?: number;
494
+ targetPath?: string;
495
+ fileSystem?: BackendFileSystem;
496
+ force?: boolean;
497
+ mode?: 'materializeCleanSession';
498
+ }
499
+
500
+ export interface MaterializeSessionInput {
501
+ sessionId: string;
502
+ expectedRevision?: number;
503
+ storage?: BackendProjectSessionStorage;
504
+ targetPath?: string;
505
+ fileSystem?: BackendFileSystem;
506
+ mode?: 'fullProject';
507
+ reason?: string;
508
+ }
509
+
510
+ export interface BackendRuntimeOptions {
511
+ fileSystem?: BackendFileSystem;
512
+ host?: BackendHostAdapter;
513
+ }