@openfairygui/backend 0.2.0-alpha.19 → 0.2.0-alpha.20

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