@openfairygui/backend 0.5.0-alpha.4 → 0.6.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 +3 -3
- package/dist/docs.cjs +190 -611
- package/dist/docs.d.cts +9 -27
- package/dist/docs.d.mts +9 -27
- package/dist/docs.mjs +190 -611
- package/dist/index.cjs +445 -635
- package/dist/index.d.cts +46 -160
- package/dist/index.d.mts +46 -160
- package/dist/index.mjs +445 -636
- package/dist/node.cjs +476 -664
- package/dist/node.d.cts +20 -94
- package/dist/node.d.mts +20 -94
- package/dist/node.mjs +476 -664
- package/package.json +3 -3
- package/src/contracts.ts +2 -2
- package/src/diagnostics.ts +4 -5
- package/src/generated/contracts.ts +208 -837
- package/src/generated/docs.ts +3 -3
- package/src/index.ts +2 -13
- package/src/node.ts +36 -30
- package/src/runtime/capabilities.ts +2 -12
- package/src/runtime/contracts.ts +33 -107
- package/src/runtime.ts +22 -57
- package/src/services/authoring-service.ts +9 -572
- package/src/services/cache-service.ts +32 -10
- package/src/services/context.ts +27 -14
- package/src/services/event-service.ts +10 -8
- package/src/services/persistence-service.ts +550 -0
- package/src/services/read-service.ts +18 -18
- package/src/services/runtime-service.ts +27 -36
- package/src/services/session-operation-queue.ts +21 -0
- package/src/services/session-project-writer.ts +3 -1
- package/src/services/session-registry.ts +70 -0
- package/src/services/session-utils.ts +4 -4
- package/src/services/job-service.ts +0 -238
|
@@ -1,39 +1,12 @@
|
|
|
1
1
|
import type { ProjectBranchDirectory, ProjectResourceFolder, ProjectSourceFile } from '@openfairygui/core/project-io';
|
|
2
|
-
import {
|
|
3
|
-
commitUamProjectSourcePaths,
|
|
4
|
-
materializeUamProject,
|
|
5
|
-
staleBranchDirectories,
|
|
6
|
-
staleResourceFolders,
|
|
7
|
-
staleSourceFiles,
|
|
8
|
-
type UamProject,
|
|
9
|
-
validateUamProject,
|
|
10
|
-
} from '@openfairygui/core/uam';
|
|
2
|
+
import { staleBranchDirectories, staleResourceFolders, staleSourceFiles, type UamProject } from '@openfairygui/core/uam';
|
|
11
3
|
import { type ApplyUamTransactionAppError, applyUamTransactionAppAsync } from '@openfairygui/functions/uam';
|
|
12
4
|
import type { BackendDiagnostic } from '../contracts.js';
|
|
13
|
-
import {
|
|
14
|
-
import type {
|
|
15
|
-
ApplySessionTransactionInput,
|
|
16
|
-
BackendCapabilityUnavailableError,
|
|
17
|
-
BackendFileSystem,
|
|
18
|
-
BackendResult,
|
|
19
|
-
BackendSessionSnapshot,
|
|
20
|
-
BackendTransactionPreview,
|
|
21
|
-
InProcessLockConflictError,
|
|
22
|
-
MaterializeSessionInput,
|
|
23
|
-
MaterializeSessionSnapshot,
|
|
24
|
-
MaterializeValidationFailedError,
|
|
25
|
-
MaterializeWriteFailedError,
|
|
26
|
-
SavePartialFailureError,
|
|
27
|
-
SaveSessionInput,
|
|
28
|
-
SessionNotFoundError,
|
|
29
|
-
SessionStaleWriteError,
|
|
30
|
-
TransactionPreviewError,
|
|
31
|
-
UamFidelityUnsupportedError,
|
|
32
|
-
} from '../runtime.js';
|
|
5
|
+
import type { ApplySessionTransactionInput, BackendResult, BackendSessionSnapshot, BackendTransactionPreview, SessionNotFoundError, SessionStaleWriteError, TransactionPreviewError } from '../runtime/contracts.js';
|
|
33
6
|
import type { CacheService } from './cache-service.js';
|
|
34
|
-
import { type BackendContext, failure, success } from './context.js';
|
|
7
|
+
import { type BackendContext, type BackendSessionState, failure, success } from './context.js';
|
|
35
8
|
import type { EventService } from './event-service.js';
|
|
36
|
-
import {
|
|
9
|
+
import type { SessionOperationQueue } from './session-operation-queue.js';
|
|
37
10
|
import { createSessionNotFoundError, createStaleWriteError, toSessionSnapshot } from './session-utils.js';
|
|
38
11
|
import { PreviewBudgetError, previewTransactionImpact } from './transaction-preview.js';
|
|
39
12
|
|
|
@@ -50,7 +23,7 @@ function branchDirectoryKey(directory: ProjectBranchDirectory): string {
|
|
|
50
23
|
}
|
|
51
24
|
|
|
52
25
|
function recordStaleProjectFiles(
|
|
53
|
-
session:
|
|
26
|
+
session: BackendSessionState,
|
|
54
27
|
previousProject: UamProject,
|
|
55
28
|
nextProject: UamProject,
|
|
56
29
|
): void {
|
|
@@ -91,74 +64,6 @@ function toBackendDiagnostics(error: ApplyUamTransactionAppError): BackendDiagno
|
|
|
91
64
|
];
|
|
92
65
|
}
|
|
93
66
|
|
|
94
|
-
function createCapabilityUnavailableError(message: string): BackendCapabilityUnavailableError {
|
|
95
|
-
return {
|
|
96
|
-
code: 'capability_unavailable',
|
|
97
|
-
message,
|
|
98
|
-
capability: 'fileSystem',
|
|
99
|
-
requiredAdapter: 'BackendFileSystem',
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function createUamFidelityUnsupportedError(
|
|
104
|
-
session: Parameters<typeof toSessionSnapshot>[0],
|
|
105
|
-
): UamFidelityUnsupportedError {
|
|
106
|
-
return {
|
|
107
|
-
code: 'uam_fidelity_unsupported',
|
|
108
|
-
message: 'The source project contains formal properties that the current UAM cannot preserve.',
|
|
109
|
-
sessionId: session.sessionId,
|
|
110
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function validationDiagnostics(sessionProject: Parameters<typeof validateUamProject>[0]): BackendDiagnostic[] {
|
|
115
|
-
const issues = validateUamProject(sessionProject);
|
|
116
|
-
return issues.map((issue) => ({
|
|
117
|
-
code: 'materialize_validation_failed',
|
|
118
|
-
message: issue.message,
|
|
119
|
-
severity: 'error',
|
|
120
|
-
path: issue.path,
|
|
121
|
-
operationKind: 'materializeSession',
|
|
122
|
-
}));
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function toMaterializeSnapshot(
|
|
126
|
-
session: Parameters<typeof toSessionSnapshot>[0],
|
|
127
|
-
capabilities: Parameters<typeof toSessionSnapshot>[1],
|
|
128
|
-
input: {
|
|
129
|
-
reason?: string;
|
|
130
|
-
writtenPaths: string[];
|
|
131
|
-
skippedPaths: string[];
|
|
132
|
-
diagnostics: BackendDiagnostic[];
|
|
133
|
-
},
|
|
134
|
-
): MaterializeSessionSnapshot {
|
|
135
|
-
return {
|
|
136
|
-
...toSessionSnapshot(session, capabilities),
|
|
137
|
-
mode: 'fullProject',
|
|
138
|
-
reason: input.reason,
|
|
139
|
-
materializeRevision: session.revision,
|
|
140
|
-
saveRevision: session.lastSavedRevision,
|
|
141
|
-
writtenPaths: [...input.writtenPaths],
|
|
142
|
-
skippedPaths: [...input.skippedPaths],
|
|
143
|
-
diagnostics: input.diagnostics.map((diagnostic) => ({ ...diagnostic })),
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function storageCanonicalTarget(input: NonNullable<MaterializeSessionInput['storage']>): {
|
|
148
|
-
fileSystem: BackendFileSystem;
|
|
149
|
-
fairyPath: string;
|
|
150
|
-
canonicalProjectPath: string;
|
|
151
|
-
canonicalPathKey: string;
|
|
152
|
-
} {
|
|
153
|
-
const canonicalProjectPath = input.canonicalProjectPath ?? (input.fileSystem.dirname(input.fairyPath) || '.');
|
|
154
|
-
return {
|
|
155
|
-
fileSystem: input.fileSystem,
|
|
156
|
-
fairyPath: input.fairyPath,
|
|
157
|
-
canonicalProjectPath,
|
|
158
|
-
canonicalPathKey: input.canonicalPathKey ?? normalizeComparablePath(canonicalProjectPath),
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
|
|
162
67
|
function detachSharedByteViews(value: unknown, seen = new WeakSet<object>()): void {
|
|
163
68
|
if (!value || typeof value !== 'object' || seen.has(value)) return;
|
|
164
69
|
seen.add(value);
|
|
@@ -174,37 +79,19 @@ function detachSharedByteViews(value: unknown, seen = new WeakSet<object>()): vo
|
|
|
174
79
|
}
|
|
175
80
|
|
|
176
81
|
export class AuthoringService {
|
|
177
|
-
private readonly sessionOperations = new Map<string, Promise<void>>();
|
|
178
|
-
|
|
179
82
|
public constructor(
|
|
180
|
-
private readonly context: BackendContext,
|
|
83
|
+
private readonly context: Pick<BackendContext, 'capabilities'> & { sessions: Pick<BackendContext['sessions'], 'get'> },
|
|
181
84
|
private readonly cacheService: CacheService,
|
|
182
85
|
private readonly eventService: EventService,
|
|
86
|
+
private readonly sessionOperations: SessionOperationQueue,
|
|
183
87
|
) {}
|
|
184
88
|
|
|
185
|
-
public async runSessionExclusive<T>(sessionId: string, operation: () => Promise<T>): Promise<T> {
|
|
186
|
-
const previous = this.sessionOperations.get(sessionId) ?? Promise.resolve();
|
|
187
|
-
let release = (): void => undefined;
|
|
188
|
-
const current = new Promise<void>((resolve) => {
|
|
189
|
-
release = resolve;
|
|
190
|
-
});
|
|
191
|
-
const tail = previous.then(() => current);
|
|
192
|
-
this.sessionOperations.set(sessionId, tail);
|
|
193
|
-
await previous;
|
|
194
|
-
try {
|
|
195
|
-
return await operation();
|
|
196
|
-
} finally {
|
|
197
|
-
release();
|
|
198
|
-
if (this.sessionOperations.get(sessionId) === tail) this.sessionOperations.delete(sessionId);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
89
|
public async preflightTransaction(
|
|
203
90
|
input: ApplySessionTransactionInput,
|
|
204
91
|
): Promise<BackendResult<BackendTransactionPreview, SessionNotFoundError | SessionStaleWriteError | ApplyUamTransactionAppError | TransactionPreviewError>> {
|
|
205
92
|
const queuedInput = structuredClone(input);
|
|
206
93
|
detachSharedByteViews(queuedInput);
|
|
207
|
-
return this.
|
|
94
|
+
return this.sessionOperations.run(queuedInput.sessionId, async () => {
|
|
208
95
|
const startedAt = Date.now();
|
|
209
96
|
const session = this.context.sessions.get(queuedInput.sessionId);
|
|
210
97
|
if (!session || session.closed) return failure('authoring', startedAt, createSessionNotFoundError(queuedInput.sessionId));
|
|
@@ -250,7 +137,7 @@ export class AuthoringService {
|
|
|
250
137
|
> {
|
|
251
138
|
const queuedInput = structuredClone(input);
|
|
252
139
|
detachSharedByteViews(queuedInput);
|
|
253
|
-
return this.
|
|
140
|
+
return this.sessionOperations.run(queuedInput.sessionId, () => this.applyTransactionExclusive(queuedInput));
|
|
254
141
|
}
|
|
255
142
|
|
|
256
143
|
private async applyTransactionExclusive(
|
|
@@ -339,454 +226,4 @@ export class AuthoringService {
|
|
|
339
226
|
});
|
|
340
227
|
}
|
|
341
228
|
|
|
342
|
-
public async saveSession(
|
|
343
|
-
input: SaveSessionInput,
|
|
344
|
-
): Promise<
|
|
345
|
-
BackendResult<
|
|
346
|
-
BackendSessionSnapshot | MaterializeSessionSnapshot,
|
|
347
|
-
| SessionNotFoundError
|
|
348
|
-
| SessionStaleWriteError
|
|
349
|
-
| SavePartialFailureError
|
|
350
|
-
| UamFidelityUnsupportedError
|
|
351
|
-
| MaterializeValidationFailedError
|
|
352
|
-
| MaterializeWriteFailedError
|
|
353
|
-
| PathPolicyViolationError
|
|
354
|
-
| InProcessLockConflictError
|
|
355
|
-
| BackendCapabilityUnavailableError
|
|
356
|
-
>
|
|
357
|
-
> {
|
|
358
|
-
if (input.force === true || input.mode === 'materializeCleanSession') {
|
|
359
|
-
return this.materializeSession({
|
|
360
|
-
sessionId: input.sessionId,
|
|
361
|
-
expectedRevision: input.expectedRevision,
|
|
362
|
-
targetPath: input.targetPath,
|
|
363
|
-
fileSystem: input.fileSystem,
|
|
364
|
-
mode: 'fullProject',
|
|
365
|
-
reason: 'force_save',
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
return this.runSessionExclusive(input.sessionId, () => this.saveSessionExclusive(input));
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
private finishSuccessfulSave(session: Parameters<typeof toSessionSnapshot>[0]): void {
|
|
372
|
-
session.lastSavedRevision = session.revision;
|
|
373
|
-
session.dirty = false;
|
|
374
|
-
const cacheEntry = this.cacheService.refreshSession(session);
|
|
375
|
-
this.eventService.emit({
|
|
376
|
-
kind: 'save.completed',
|
|
377
|
-
sessionId: session.sessionId,
|
|
378
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
379
|
-
revision: session.revision,
|
|
380
|
-
});
|
|
381
|
-
this.eventService.emit({
|
|
382
|
-
kind: 'cache.updated',
|
|
383
|
-
sessionId: session.sessionId,
|
|
384
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
385
|
-
revision: session.revision,
|
|
386
|
-
cacheRevision: cacheEntry.revision,
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
private async saveSessionExclusive(
|
|
391
|
-
input: SaveSessionInput,
|
|
392
|
-
): Promise<
|
|
393
|
-
BackendResult<
|
|
394
|
-
BackendSessionSnapshot | MaterializeSessionSnapshot,
|
|
395
|
-
| SessionNotFoundError
|
|
396
|
-
| SessionStaleWriteError
|
|
397
|
-
| SavePartialFailureError
|
|
398
|
-
| UamFidelityUnsupportedError
|
|
399
|
-
| MaterializeValidationFailedError
|
|
400
|
-
| MaterializeWriteFailedError
|
|
401
|
-
| PathPolicyViolationError
|
|
402
|
-
| InProcessLockConflictError
|
|
403
|
-
| BackendCapabilityUnavailableError
|
|
404
|
-
>
|
|
405
|
-
> {
|
|
406
|
-
const startedAt = Date.now();
|
|
407
|
-
const session = this.context.sessions.get(input.sessionId);
|
|
408
|
-
if (!session || session.closed) {
|
|
409
|
-
return failure('authoring', startedAt, createSessionNotFoundError(input.sessionId));
|
|
410
|
-
}
|
|
411
|
-
const fileSystem = session.fileSystem ?? input.fileSystem;
|
|
412
|
-
if (!fileSystem) {
|
|
413
|
-
return failure(
|
|
414
|
-
'authoring',
|
|
415
|
-
startedAt,
|
|
416
|
-
createCapabilityUnavailableError('saveSession requires an injected BackendFileSystem adapter.'),
|
|
417
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
418
|
-
{
|
|
419
|
-
sessionId: session.sessionId,
|
|
420
|
-
revision: session.revision,
|
|
421
|
-
},
|
|
422
|
-
);
|
|
423
|
-
}
|
|
424
|
-
if (input.expectedRevision !== undefined && input.expectedRevision !== session.revision) {
|
|
425
|
-
return failure(
|
|
426
|
-
'authoring',
|
|
427
|
-
startedAt,
|
|
428
|
-
createStaleWriteError(session, input.expectedRevision),
|
|
429
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
430
|
-
{
|
|
431
|
-
sessionId: session.sessionId,
|
|
432
|
-
revision: session.revision,
|
|
433
|
-
},
|
|
434
|
-
);
|
|
435
|
-
}
|
|
436
|
-
const targetViolation = await validateSaveTarget(fileSystem, session.fairyPath, input.targetPath);
|
|
437
|
-
if (targetViolation) {
|
|
438
|
-
return failure(
|
|
439
|
-
'authoring',
|
|
440
|
-
startedAt,
|
|
441
|
-
targetViolation,
|
|
442
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
443
|
-
{
|
|
444
|
-
sessionId: session.sessionId,
|
|
445
|
-
revision: session.revision,
|
|
446
|
-
},
|
|
447
|
-
);
|
|
448
|
-
}
|
|
449
|
-
if (!session.dirty) {
|
|
450
|
-
return success('authoring', startedAt, toSessionSnapshot(session, this.context.capabilities), {
|
|
451
|
-
sessionId: session.sessionId,
|
|
452
|
-
revision: session.revision,
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
if (session.uamFidelity === 'unsupported') {
|
|
456
|
-
return failure(
|
|
457
|
-
'authoring',
|
|
458
|
-
startedAt,
|
|
459
|
-
createUamFidelityUnsupportedError(session),
|
|
460
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
461
|
-
{
|
|
462
|
-
sessionId: session.sessionId,
|
|
463
|
-
revision: session.revision,
|
|
464
|
-
},
|
|
465
|
-
);
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
const committedPaths: string[] = [];
|
|
469
|
-
const failedPaths: string[] = [];
|
|
470
|
-
this.eventService.emit({
|
|
471
|
-
kind: 'save.started',
|
|
472
|
-
sessionId: session.sessionId,
|
|
473
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
474
|
-
revision: session.revision,
|
|
475
|
-
});
|
|
476
|
-
try {
|
|
477
|
-
await writeSessionProject({
|
|
478
|
-
fileSystem,
|
|
479
|
-
document: materializeUamProject(session.project),
|
|
480
|
-
fairyPath: session.fairyPath,
|
|
481
|
-
staleSourceFiles: [...session.pendingStaleSourceFiles.values()],
|
|
482
|
-
staleResourceFolders: [...session.pendingStaleResourceFolders.values()],
|
|
483
|
-
staleBranchDirectories: [...session.pendingStaleBranchDirectories.values()],
|
|
484
|
-
writtenPaths: committedPaths,
|
|
485
|
-
failedPaths,
|
|
486
|
-
});
|
|
487
|
-
session.fileSystem ??= fileSystem;
|
|
488
|
-
session.pendingStaleSourceFiles.clear();
|
|
489
|
-
session.pendingStaleResourceFolders.clear();
|
|
490
|
-
session.pendingStaleBranchDirectories.clear();
|
|
491
|
-
commitUamProjectSourcePaths(session.project);
|
|
492
|
-
this.finishSuccessfulSave(session);
|
|
493
|
-
return success('authoring', startedAt, toSessionSnapshot(session, this.context.capabilities), {
|
|
494
|
-
sessionId: session.sessionId,
|
|
495
|
-
revision: session.revision,
|
|
496
|
-
});
|
|
497
|
-
} catch (error) {
|
|
498
|
-
this.cacheService.invalidateSession(session);
|
|
499
|
-
this.eventService.emit({
|
|
500
|
-
kind: 'save.failed',
|
|
501
|
-
sessionId: session.sessionId,
|
|
502
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
503
|
-
revision: session.revision,
|
|
504
|
-
});
|
|
505
|
-
return failure(
|
|
506
|
-
'authoring',
|
|
507
|
-
startedAt,
|
|
508
|
-
{
|
|
509
|
-
code: 'save_partial_failure',
|
|
510
|
-
message: error instanceof Error ? error.message : String(error),
|
|
511
|
-
sessionId: session.sessionId,
|
|
512
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
513
|
-
attemptedRevision: session.revision,
|
|
514
|
-
lastSavedRevision: session.lastSavedRevision,
|
|
515
|
-
committedPaths,
|
|
516
|
-
failedPaths,
|
|
517
|
-
diskMayBePartiallyUpdated: !fileSystem.runProjectWriteTransaction,
|
|
518
|
-
},
|
|
519
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
520
|
-
{
|
|
521
|
-
sessionId: session.sessionId,
|
|
522
|
-
revision: session.revision,
|
|
523
|
-
},
|
|
524
|
-
);
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
public async materializeSession(
|
|
529
|
-
input: MaterializeSessionInput,
|
|
530
|
-
): Promise<
|
|
531
|
-
BackendResult<
|
|
532
|
-
MaterializeSessionSnapshot,
|
|
533
|
-
| SessionNotFoundError
|
|
534
|
-
| SessionStaleWriteError
|
|
535
|
-
| UamFidelityUnsupportedError
|
|
536
|
-
| MaterializeValidationFailedError
|
|
537
|
-
| MaterializeWriteFailedError
|
|
538
|
-
| PathPolicyViolationError
|
|
539
|
-
| InProcessLockConflictError
|
|
540
|
-
| BackendCapabilityUnavailableError
|
|
541
|
-
>
|
|
542
|
-
> {
|
|
543
|
-
return this.runSessionExclusive(input.sessionId, () => this.materializeSessionExclusive(input));
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
private async materializeSessionExclusive(
|
|
547
|
-
input: MaterializeSessionInput,
|
|
548
|
-
): Promise<
|
|
549
|
-
BackendResult<
|
|
550
|
-
MaterializeSessionSnapshot,
|
|
551
|
-
| SessionNotFoundError
|
|
552
|
-
| SessionStaleWriteError
|
|
553
|
-
| UamFidelityUnsupportedError
|
|
554
|
-
| MaterializeValidationFailedError
|
|
555
|
-
| MaterializeWriteFailedError
|
|
556
|
-
| PathPolicyViolationError
|
|
557
|
-
| InProcessLockConflictError
|
|
558
|
-
| BackendCapabilityUnavailableError
|
|
559
|
-
>
|
|
560
|
-
> {
|
|
561
|
-
const startedAt = Date.now();
|
|
562
|
-
const session = this.context.sessions.get(input.sessionId);
|
|
563
|
-
if (!session || session.closed) {
|
|
564
|
-
return failure('authoring', startedAt, createSessionNotFoundError(input.sessionId));
|
|
565
|
-
}
|
|
566
|
-
if (input.expectedRevision !== undefined && input.expectedRevision !== session.revision) {
|
|
567
|
-
return failure(
|
|
568
|
-
'authoring',
|
|
569
|
-
startedAt,
|
|
570
|
-
createStaleWriteError(session, input.expectedRevision),
|
|
571
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
572
|
-
{
|
|
573
|
-
sessionId: session.sessionId,
|
|
574
|
-
revision: session.revision,
|
|
575
|
-
},
|
|
576
|
-
);
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
const storageTarget = input.storage ? storageCanonicalTarget(input.storage) : null;
|
|
580
|
-
const fileSystem =
|
|
581
|
-
storageTarget?.fileSystem ?? input.fileSystem ?? session.fileSystem;
|
|
582
|
-
if (!fileSystem) {
|
|
583
|
-
return failure(
|
|
584
|
-
'authoring',
|
|
585
|
-
startedAt,
|
|
586
|
-
createCapabilityUnavailableError('materializeSession requires an injected BackendFileSystem adapter.'),
|
|
587
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
588
|
-
{
|
|
589
|
-
sessionId: session.sessionId,
|
|
590
|
-
revision: session.revision,
|
|
591
|
-
},
|
|
592
|
-
);
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
const fairyPath = storageTarget?.fairyPath ?? session.fairyPath;
|
|
596
|
-
const targetViolation = await validateSaveTarget(fileSystem, fairyPath, input.targetPath);
|
|
597
|
-
if (targetViolation) {
|
|
598
|
-
return failure(
|
|
599
|
-
'authoring',
|
|
600
|
-
startedAt,
|
|
601
|
-
targetViolation,
|
|
602
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
603
|
-
{
|
|
604
|
-
sessionId: session.sessionId,
|
|
605
|
-
revision: session.revision,
|
|
606
|
-
},
|
|
607
|
-
);
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
if (storageTarget) {
|
|
611
|
-
const holderSessionId = this.context.sessionsByPath.get(storageTarget.canonicalPathKey);
|
|
612
|
-
if (holderSessionId && holderSessionId !== session.sessionId) {
|
|
613
|
-
return failure(
|
|
614
|
-
'authoring',
|
|
615
|
-
startedAt,
|
|
616
|
-
{
|
|
617
|
-
code: 'lock_conflict',
|
|
618
|
-
kind: 'in_process_session_exists',
|
|
619
|
-
message: `Project is already open in this backend runtime: ${storageTarget.canonicalProjectPath}`,
|
|
620
|
-
canonicalPathKey: storageTarget.canonicalPathKey,
|
|
621
|
-
holderSessionId,
|
|
622
|
-
},
|
|
623
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
624
|
-
{
|
|
625
|
-
sessionId: session.sessionId,
|
|
626
|
-
revision: session.revision,
|
|
627
|
-
},
|
|
628
|
-
);
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
if (session.uamFidelity === 'unsupported') {
|
|
633
|
-
return failure(
|
|
634
|
-
'authoring',
|
|
635
|
-
startedAt,
|
|
636
|
-
createUamFidelityUnsupportedError(session),
|
|
637
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
638
|
-
{
|
|
639
|
-
sessionId: session.sessionId,
|
|
640
|
-
revision: session.revision,
|
|
641
|
-
},
|
|
642
|
-
);
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
const diagnostics = validationDiagnostics(session.project);
|
|
646
|
-
if (diagnostics.length > 0) {
|
|
647
|
-
const error: MaterializeValidationFailedError = {
|
|
648
|
-
code: 'materialize_validation_failed',
|
|
649
|
-
message: `UAM materialize validation failed with ${diagnostics.length} issue(s).`,
|
|
650
|
-
sessionId: session.sessionId,
|
|
651
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
652
|
-
issueCount: diagnostics.length,
|
|
653
|
-
diagnostics,
|
|
654
|
-
};
|
|
655
|
-
return failure('authoring', startedAt, error, toSessionSnapshot(session, this.context.capabilities), {
|
|
656
|
-
sessionId: session.sessionId,
|
|
657
|
-
revision: session.revision,
|
|
658
|
-
diagnostics,
|
|
659
|
-
});
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
let document: ReturnType<typeof materializeUamProject>;
|
|
663
|
-
try {
|
|
664
|
-
document = materializeUamProject(session.project);
|
|
665
|
-
} catch (error) {
|
|
666
|
-
const diagnosticsFromError: BackendDiagnostic[] = [
|
|
667
|
-
{
|
|
668
|
-
code: 'materialize_validation_failed',
|
|
669
|
-
message: error instanceof Error ? error.message : String(error),
|
|
670
|
-
severity: 'error',
|
|
671
|
-
operationKind: 'materializeSession',
|
|
672
|
-
},
|
|
673
|
-
];
|
|
674
|
-
return failure(
|
|
675
|
-
'authoring',
|
|
676
|
-
startedAt,
|
|
677
|
-
{
|
|
678
|
-
code: 'materialize_validation_failed',
|
|
679
|
-
message: error instanceof Error ? error.message : String(error),
|
|
680
|
-
sessionId: session.sessionId,
|
|
681
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
682
|
-
issueCount: diagnosticsFromError.length,
|
|
683
|
-
diagnostics: diagnosticsFromError,
|
|
684
|
-
},
|
|
685
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
686
|
-
{
|
|
687
|
-
sessionId: session.sessionId,
|
|
688
|
-
revision: session.revision,
|
|
689
|
-
diagnostics: diagnosticsFromError,
|
|
690
|
-
},
|
|
691
|
-
);
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
const writtenPaths: string[] = [];
|
|
695
|
-
const failedPaths: string[] = [];
|
|
696
|
-
const skippedPaths: string[] = [];
|
|
697
|
-
this.eventService.emit({
|
|
698
|
-
kind: 'save.started',
|
|
699
|
-
sessionId: session.sessionId,
|
|
700
|
-
canonicalPathKey: storageTarget?.canonicalPathKey ?? session.canonicalPathKey,
|
|
701
|
-
revision: session.revision,
|
|
702
|
-
});
|
|
703
|
-
try {
|
|
704
|
-
const isSessionStorageTarget = fileSystem === session.fileSystem && fairyPath === session.fairyPath;
|
|
705
|
-
await writeSessionProject({
|
|
706
|
-
fileSystem,
|
|
707
|
-
document,
|
|
708
|
-
fairyPath,
|
|
709
|
-
staleSourceFiles: isSessionStorageTarget ? [...session.pendingStaleSourceFiles.values()] : [],
|
|
710
|
-
staleResourceFolders: isSessionStorageTarget ? [...session.pendingStaleResourceFolders.values()] : [],
|
|
711
|
-
staleBranchDirectories: isSessionStorageTarget ? [...session.pendingStaleBranchDirectories.values()] : [],
|
|
712
|
-
writtenPaths,
|
|
713
|
-
failedPaths,
|
|
714
|
-
});
|
|
715
|
-
if (isSessionStorageTarget) {
|
|
716
|
-
session.pendingStaleSourceFiles.clear();
|
|
717
|
-
session.pendingStaleResourceFolders.clear();
|
|
718
|
-
session.pendingStaleBranchDirectories.clear();
|
|
719
|
-
}
|
|
720
|
-
if (storageTarget && !isSessionStorageTarget) {
|
|
721
|
-
session.pendingStaleSourceFiles.clear();
|
|
722
|
-
session.pendingStaleResourceFolders.clear();
|
|
723
|
-
session.pendingStaleBranchDirectories.clear();
|
|
724
|
-
}
|
|
725
|
-
if (isSessionStorageTarget || storageTarget) commitUamProjectSourcePaths(session.project);
|
|
726
|
-
if (storageTarget) {
|
|
727
|
-
this.context.sessionsByPath.delete(session.canonicalPathKey);
|
|
728
|
-
session.fileSystem = storageTarget.fileSystem;
|
|
729
|
-
session.fairyPath = storageTarget.fairyPath;
|
|
730
|
-
session.canonicalProjectPath = storageTarget.canonicalProjectPath;
|
|
731
|
-
session.canonicalPathKey = storageTarget.canonicalPathKey;
|
|
732
|
-
this.context.sessionsByPath.set(session.canonicalPathKey, session.sessionId);
|
|
733
|
-
}
|
|
734
|
-
this.finishSuccessfulSave(session);
|
|
735
|
-
return success(
|
|
736
|
-
'authoring',
|
|
737
|
-
startedAt,
|
|
738
|
-
toMaterializeSnapshot(session, this.context.capabilities, {
|
|
739
|
-
reason: input.reason,
|
|
740
|
-
writtenPaths,
|
|
741
|
-
skippedPaths,
|
|
742
|
-
diagnostics: [],
|
|
743
|
-
}),
|
|
744
|
-
{
|
|
745
|
-
sessionId: session.sessionId,
|
|
746
|
-
revision: session.revision,
|
|
747
|
-
},
|
|
748
|
-
);
|
|
749
|
-
} catch (error) {
|
|
750
|
-
const diagnosticsFromError: BackendDiagnostic[] = [
|
|
751
|
-
{
|
|
752
|
-
code: 'write_failed',
|
|
753
|
-
message: error instanceof Error ? error.message : String(error),
|
|
754
|
-
severity: 'error',
|
|
755
|
-
path: failedPaths[0],
|
|
756
|
-
operationKind: 'materializeSession',
|
|
757
|
-
},
|
|
758
|
-
];
|
|
759
|
-
this.cacheService.invalidateSession(session);
|
|
760
|
-
this.eventService.emit({
|
|
761
|
-
kind: 'save.failed',
|
|
762
|
-
sessionId: session.sessionId,
|
|
763
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
764
|
-
revision: session.revision,
|
|
765
|
-
diagnostics: diagnosticsFromError,
|
|
766
|
-
});
|
|
767
|
-
return failure(
|
|
768
|
-
'authoring',
|
|
769
|
-
startedAt,
|
|
770
|
-
{
|
|
771
|
-
code: 'write_failed',
|
|
772
|
-
message: error instanceof Error ? error.message : String(error),
|
|
773
|
-
sessionId: session.sessionId,
|
|
774
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
775
|
-
attemptedRevision: session.revision,
|
|
776
|
-
lastSavedRevision: session.lastSavedRevision,
|
|
777
|
-
writtenPaths,
|
|
778
|
-
failedPaths,
|
|
779
|
-
skippedPaths,
|
|
780
|
-
diagnostics: diagnosticsFromError,
|
|
781
|
-
diskMayBePartiallyUpdated: !fileSystem.runProjectWriteTransaction,
|
|
782
|
-
},
|
|
783
|
-
toSessionSnapshot(session, this.context.capabilities),
|
|
784
|
-
{
|
|
785
|
-
sessionId: session.sessionId,
|
|
786
|
-
revision: session.revision,
|
|
787
|
-
diagnostics: diagnosticsFromError,
|
|
788
|
-
},
|
|
789
|
-
);
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
229
|
}
|