@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
package/dist/index.cjs
CHANGED
|
@@ -4,8 +4,8 @@ let _openfairygui_functions_uam = require("@openfairygui/functions/uam");
|
|
|
4
4
|
let _openfairygui_core_project_io = require("@openfairygui/core/project-io");
|
|
5
5
|
let _openfairygui_functions = require("@openfairygui/functions");
|
|
6
6
|
//#region src/contracts.ts
|
|
7
|
-
const BACKEND_CONTRACT_VERSION = "
|
|
8
|
-
const BACKEND_CAPABILITY_SCHEMA_VERSION =
|
|
7
|
+
const BACKEND_CONTRACT_VERSION = "3.0.0";
|
|
8
|
+
const BACKEND_CAPABILITY_SCHEMA_VERSION = 12;
|
|
9
9
|
const BACKEND_COMPATIBILITY_POLICY = {
|
|
10
10
|
incompatibleChange: "requires contractVersion bump",
|
|
11
11
|
capabilitySchemaChange: "requires capabilitySchemaVersion bump",
|
|
@@ -57,14 +57,6 @@ const write = {
|
|
|
57
57
|
kind: "host-action",
|
|
58
58
|
message: "Preserve the dirty session and inspect the error, actual target files and permissions with the host. Partial writes may have changed files: verify or recover them before a newly authorized save. Do not clear dirty state, discard memory or blindly retry."
|
|
59
59
|
};
|
|
60
|
-
const execution = {
|
|
61
|
-
kind: "host-action",
|
|
62
|
-
message: "Inspect the execution/cache failure and current session/revision. Preserve sources and unsaved work and report the cause to the host. Replan after resolution; failed execution or cache work is not a completed edit."
|
|
63
|
-
};
|
|
64
|
-
const job = {
|
|
65
|
-
kind: "host-action",
|
|
66
|
-
message: "Inspect the job ID and status with getJob/listJobs. Do not fabricate, restart or force-cancel a missing, terminal or non-cancellable job. Ask the host before scheduling new work; cancellation is not success."
|
|
67
|
-
};
|
|
68
60
|
/** Exhaustiveness and exact ownership are checked against the canonical unions. */
|
|
69
61
|
const BACKEND_DIAGNOSTIC_GUIDES = [
|
|
70
62
|
{
|
|
@@ -115,6 +107,14 @@ const BACKEND_DIAGNOSTIC_GUIDES = [
|
|
|
115
107
|
message: "Ask the host to confirm the runtime and project, recover any unsaved state, then explicitly open a new session if appropriate. Session IDs are runtime-local. Read its new revision and replan; never reuse an expired session or assume disk contains unsaved changes."
|
|
116
108
|
}
|
|
117
109
|
},
|
|
110
|
+
{
|
|
111
|
+
code: "session_close_failed",
|
|
112
|
+
owners: ["backend"],
|
|
113
|
+
remediation: {
|
|
114
|
+
kind: "host-action",
|
|
115
|
+
message: "The session and lock ownership remain registered. Inspect the reported lock release failure, correct the storage problem, then retry closeSession with the same session ID. Do not remove the lock file or open a replacement session to bypass the owner."
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
118
|
{
|
|
119
119
|
code: "path_policy_violation",
|
|
120
120
|
owners: ["backend"],
|
|
@@ -595,27 +595,10 @@ const BACKEND_DIAGNOSTIC_GUIDES = [
|
|
|
595
595
|
{
|
|
596
596
|
code: "execution_failure",
|
|
597
597
|
owners: ["core.transaction"],
|
|
598
|
-
remediation:
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
owners: ["backend"],
|
|
603
|
-
remediation: execution
|
|
604
|
-
},
|
|
605
|
-
{
|
|
606
|
-
code: "job_not_found",
|
|
607
|
-
owners: ["backend"],
|
|
608
|
-
remediation: job
|
|
609
|
-
},
|
|
610
|
-
{
|
|
611
|
-
code: "job_not_cancellable",
|
|
612
|
-
owners: ["backend"],
|
|
613
|
-
remediation: job
|
|
614
|
-
},
|
|
615
|
-
{
|
|
616
|
-
code: "job_cancelled",
|
|
617
|
-
owners: ["backend"],
|
|
618
|
-
remediation: job
|
|
598
|
+
remediation: {
|
|
599
|
+
kind: "host-action",
|
|
600
|
+
message: "Inspect the execution/cache failure and current session/revision. Preserve sources and unsaved work and report the cause to the host. Replan after resolution; failed execution or cache work is not a completed edit."
|
|
601
|
+
}
|
|
619
602
|
}
|
|
620
603
|
];
|
|
621
604
|
const BACKEND_DIAGNOSTICS_URI = "openfairygui://docs/diagnostics";
|
|
@@ -648,82 +631,15 @@ function enrichBackendDiagnostic(diagnostic, sessionId) {
|
|
|
648
631
|
};
|
|
649
632
|
}
|
|
650
633
|
//#endregion
|
|
651
|
-
//#region src/
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
const drivePrefix = driveMatch?.[1].toLowerCase() ?? "";
|
|
656
|
-
const remainder = driveMatch ? driveMatch[2] ?? "" : normalized;
|
|
657
|
-
const hasRoot = driveMatch ? true : remainder.startsWith("/");
|
|
658
|
-
const rawSegments = remainder.split("/").filter((segment) => segment.length > 0);
|
|
659
|
-
const segments = [];
|
|
660
|
-
for (const segment of rawSegments) {
|
|
661
|
-
if (segment === ".") continue;
|
|
662
|
-
if (segment === "..") {
|
|
663
|
-
if (segments.length > 0 && segments[segments.length - 1] !== "..") segments.pop();
|
|
664
|
-
else if (!hasRoot) segments.push("..");
|
|
665
|
-
continue;
|
|
666
|
-
}
|
|
667
|
-
segments.push(segment);
|
|
668
|
-
}
|
|
669
|
-
const joined = segments.join("/");
|
|
670
|
-
return (drivePrefix ? `${drivePrefix}/${joined}`.replace(/\/$/, "") : hasRoot ? `/${joined}`.replace(/\/$/, "") : joined || ".").toLowerCase();
|
|
671
|
-
}
|
|
672
|
-
function createRuntimePathPolicy() {
|
|
673
|
-
return {
|
|
674
|
-
canonicalization: "realpath+normalized-casefold",
|
|
675
|
-
sessionIdentity: "project-root",
|
|
676
|
-
saveTarget: "opened-project-only",
|
|
677
|
-
outputTargets: "deferred",
|
|
678
|
-
workspaceBoundary: "project-root-only"
|
|
679
|
-
};
|
|
680
|
-
}
|
|
681
|
-
async function assertProjectPathContained(fileSystem, projectRoot, targetPath) {
|
|
682
|
-
const [resolvedRoot, resolvedTarget] = await Promise.all([fileSystem.resolvePath(projectRoot), fileSystem.resolvePath(targetPath)]);
|
|
683
|
-
const root = normalizeComparablePath(resolvedRoot);
|
|
684
|
-
const target = normalizeComparablePath(resolvedTarget);
|
|
685
|
-
if (root === "." && !target.startsWith("/") && !/^[a-z]:\//i.test(target)) return;
|
|
686
|
-
if (target === root || target.startsWith(`${root}/`)) return;
|
|
687
|
-
const error = /* @__PURE__ */ new Error(`Project path escapes the opened root: ${targetPath}`);
|
|
688
|
-
error.code = "EACCES";
|
|
689
|
-
throw error;
|
|
690
|
-
}
|
|
691
|
-
async function resolveFairyPath(fileSystem, input) {
|
|
692
|
-
const resolvedInput = fileSystem.resolve(input);
|
|
693
|
-
const stat = await fileSystem.stat(resolvedInput);
|
|
694
|
-
if (stat.isFile() && resolvedInput.endsWith(".fairy")) return await fileSystem.resolvePath(resolvedInput);
|
|
695
|
-
if (stat.isDirectory()) {
|
|
696
|
-
const fairyFiles = (await fileSystem.readdir(resolvedInput)).filter((entry) => entry.endsWith(".fairy"));
|
|
697
|
-
if (fairyFiles.length === 1) return await fileSystem.resolvePath(fileSystem.join(resolvedInput, fairyFiles[0]));
|
|
698
|
-
if (fairyFiles.length > 1) throw new Error(`Multiple .fairy files found in ${resolvedInput}: ${fairyFiles.join(", ")}`);
|
|
699
|
-
throw new Error(`No .fairy file found in ${resolvedInput}`);
|
|
700
|
-
}
|
|
701
|
-
throw new Error(`Input is not a .fairy file or directory: ${resolvedInput}`);
|
|
702
|
-
}
|
|
703
|
-
async function resolveCanonicalProjectRoot(fileSystem, input) {
|
|
704
|
-
const fairyPath = await resolveFairyPath(fileSystem, input);
|
|
705
|
-
const canonicalProjectPath = await fileSystem.resolvePath(fileSystem.dirname(fairyPath));
|
|
706
|
-
return {
|
|
707
|
-
fairyPath,
|
|
708
|
-
canonicalProjectPath,
|
|
709
|
-
canonicalPathKey: normalizeComparablePath(canonicalProjectPath)
|
|
710
|
-
};
|
|
634
|
+
//#region src/services/context.ts
|
|
635
|
+
/** Borrow without copying the entire project before a read has checked its response budget. */
|
|
636
|
+
function readView(value) {
|
|
637
|
+
return value;
|
|
711
638
|
}
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
const allowedPath = await fileSystem.resolvePath(openedFairyPath);
|
|
716
|
-
if (normalizeComparablePath(attemptedPath) === normalizeComparablePath(allowedPath)) return null;
|
|
717
|
-
return {
|
|
718
|
-
code: "path_policy_violation",
|
|
719
|
-
message: `Save target is restricted to the originally opened project file: ${allowedPath}`,
|
|
720
|
-
policy: "save_target",
|
|
721
|
-
attemptedPath,
|
|
722
|
-
allowedPath
|
|
723
|
-
};
|
|
639
|
+
/** A detached copy is writable again; this is the only conversion from a borrowed read view. */
|
|
640
|
+
function cloneReadData(value) {
|
|
641
|
+
return structuredClone(value);
|
|
724
642
|
}
|
|
725
|
-
//#endregion
|
|
726
|
-
//#region src/services/context.ts
|
|
727
643
|
function diagnosticFromError(error) {
|
|
728
644
|
return {
|
|
729
645
|
code: error.code,
|
|
@@ -744,7 +660,7 @@ function createMeta(stage, startedAt, options) {
|
|
|
744
660
|
diagnostics: (options?.diagnostics ?? []).map((entry) => enrichBackendDiagnostic(entry, options?.sessionId)),
|
|
745
661
|
stage,
|
|
746
662
|
contractVersion: BACKEND_CONTRACT_VERSION,
|
|
747
|
-
capabilitySchemaVersion:
|
|
663
|
+
capabilitySchemaVersion: 12
|
|
748
664
|
};
|
|
749
665
|
}
|
|
750
666
|
function success(stage, startedAt, data, options) {
|
|
@@ -767,68 +683,6 @@ function failure(stage, startedAt, error, session, options) {
|
|
|
767
683
|
};
|
|
768
684
|
}
|
|
769
685
|
//#endregion
|
|
770
|
-
//#region src/services/session-project-writer.ts
|
|
771
|
-
function createWriterFileSystem(fileSystem, projectRoot, writtenPaths, failedPaths) {
|
|
772
|
-
const contained = async (targetPath, operation) => {
|
|
773
|
-
await assertProjectPathContained(fileSystem, projectRoot, targetPath);
|
|
774
|
-
return operation();
|
|
775
|
-
};
|
|
776
|
-
async function trackWrite(targetPath, write) {
|
|
777
|
-
try {
|
|
778
|
-
const result = await contained(targetPath, write);
|
|
779
|
-
writtenPaths.push(targetPath);
|
|
780
|
-
return result;
|
|
781
|
-
} catch (error) {
|
|
782
|
-
failedPaths.push(targetPath);
|
|
783
|
-
throw error;
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
return {
|
|
787
|
-
readFile: (path) => contained(path, () => fileSystem.readFile(path)),
|
|
788
|
-
readFileRaw: (path) => contained(path, () => fileSystem.readFileRaw(path)),
|
|
789
|
-
writeFile: (path, content) => trackWrite(path, async () => {
|
|
790
|
-
await fileSystem.mkdir(fileSystem.dirname(path), { recursive: true });
|
|
791
|
-
await fileSystem.writeFile(path, content);
|
|
792
|
-
}),
|
|
793
|
-
writeFileRaw: (path, data) => trackWrite(path, async () => {
|
|
794
|
-
await fileSystem.mkdir(fileSystem.dirname(path), { recursive: true });
|
|
795
|
-
await fileSystem.writeFileRaw(path, data);
|
|
796
|
-
}),
|
|
797
|
-
mkdir: (path) => contained(path, () => fileSystem.mkdir(path, { recursive: true })),
|
|
798
|
-
readdir: (path) => contained(path, () => fileSystem.readdir(path)),
|
|
799
|
-
async exists(path) {
|
|
800
|
-
await assertProjectPathContained(fileSystem, projectRoot, path);
|
|
801
|
-
try {
|
|
802
|
-
await fileSystem.stat(path);
|
|
803
|
-
return true;
|
|
804
|
-
} catch {
|
|
805
|
-
return false;
|
|
806
|
-
}
|
|
807
|
-
},
|
|
808
|
-
join: (...paths) => fileSystem.join(...paths),
|
|
809
|
-
dirname: (path) => fileSystem.dirname(path),
|
|
810
|
-
unlink: (path) => trackWrite(path, () => fileSystem.unlink(path)),
|
|
811
|
-
rmdir: (path) => trackWrite(path, () => fileSystem.rmdir(path))
|
|
812
|
-
};
|
|
813
|
-
}
|
|
814
|
-
async function writeSessionProject(input) {
|
|
815
|
-
const projectRoot = input.fileSystem.dirname(input.fairyPath);
|
|
816
|
-
const write = async (fileSystem) => {
|
|
817
|
-
await new _openfairygui_core_project_io.ProjectWriter(createWriterFileSystem(fileSystem, projectRoot, input.writtenPaths, input.failedPaths)).write(input.document, input.fairyPath, {
|
|
818
|
-
staleSourceFiles: input.staleSourceFiles,
|
|
819
|
-
staleResourceFolders: input.staleResourceFolders,
|
|
820
|
-
staleBranchDirectories: input.staleBranchDirectories
|
|
821
|
-
});
|
|
822
|
-
};
|
|
823
|
-
if (!input.fileSystem.runProjectWriteTransaction) return write(input.fileSystem);
|
|
824
|
-
try {
|
|
825
|
-
await input.fileSystem.runProjectWriteTransaction(projectRoot, write);
|
|
826
|
-
} catch (error) {
|
|
827
|
-
input.writtenPaths.length = 0;
|
|
828
|
-
throw error;
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
//#endregion
|
|
832
686
|
//#region src/services/session-utils.ts
|
|
833
687
|
function toSessionSnapshot(session, capabilities) {
|
|
834
688
|
return {
|
|
@@ -839,7 +693,7 @@ function toSessionSnapshot(session, capabilities) {
|
|
|
839
693
|
dirty: session.dirty,
|
|
840
694
|
uamFidelity: session.uamFidelity,
|
|
841
695
|
lockHeld: session.lockHeld,
|
|
842
|
-
capabilities:
|
|
696
|
+
capabilities: cloneReadData(capabilities)
|
|
843
697
|
};
|
|
844
698
|
}
|
|
845
699
|
function createSessionNotFoundError(sessionId) {
|
|
@@ -894,6 +748,22 @@ const BACKEND_RESOURCE_QUERY_FIELDS = [
|
|
|
894
748
|
"image",
|
|
895
749
|
"movieClip"
|
|
896
750
|
];
|
|
751
|
+
/** A failed staged write with an explicit outcome for the original project. */
|
|
752
|
+
var ProjectWriteTransactionError = class extends Error {
|
|
753
|
+
code = "project_write_transaction_failed";
|
|
754
|
+
/** Recognizes the same contract across separately bundled Node/root or ESM/CJS entries. */
|
|
755
|
+
static is(error) {
|
|
756
|
+
if (typeof error !== "object" || error === null) return false;
|
|
757
|
+
const value = error;
|
|
758
|
+
return value.code === "project_write_transaction_failed" && typeof value.message === "string" && typeof value.diskMayBePartiallyUpdated === "boolean" && Array.isArray(value.recoveryPaths) && value.recoveryPaths.every((path) => typeof path === "string");
|
|
759
|
+
}
|
|
760
|
+
constructor(cause, diskMayBePartiallyUpdated, recoveryPaths = []) {
|
|
761
|
+
super(cause instanceof Error ? cause.message : String(cause), { cause });
|
|
762
|
+
this.diskMayBePartiallyUpdated = diskMayBePartiallyUpdated;
|
|
763
|
+
this.recoveryPaths = recoveryPaths;
|
|
764
|
+
this.name = "ProjectWriteTransactionError";
|
|
765
|
+
}
|
|
766
|
+
};
|
|
897
767
|
//#endregion
|
|
898
768
|
//#region src/services/capture-project.ts
|
|
899
769
|
function createCaptureFileSystem(files, directories) {
|
|
@@ -1164,52 +1034,6 @@ function toBackendDiagnostics(error) {
|
|
|
1164
1034
|
opId: error.opId
|
|
1165
1035
|
}];
|
|
1166
1036
|
}
|
|
1167
|
-
function createCapabilityUnavailableError$1(message) {
|
|
1168
|
-
return {
|
|
1169
|
-
code: "capability_unavailable",
|
|
1170
|
-
message,
|
|
1171
|
-
capability: "fileSystem",
|
|
1172
|
-
requiredAdapter: "BackendFileSystem"
|
|
1173
|
-
};
|
|
1174
|
-
}
|
|
1175
|
-
function createUamFidelityUnsupportedError(session) {
|
|
1176
|
-
return {
|
|
1177
|
-
code: "uam_fidelity_unsupported",
|
|
1178
|
-
message: "The source project contains formal properties that the current UAM cannot preserve.",
|
|
1179
|
-
sessionId: session.sessionId,
|
|
1180
|
-
canonicalPathKey: session.canonicalPathKey
|
|
1181
|
-
};
|
|
1182
|
-
}
|
|
1183
|
-
function validationDiagnostics(sessionProject) {
|
|
1184
|
-
return (0, _openfairygui_core_uam.validateUamProject)(sessionProject).map((issue) => ({
|
|
1185
|
-
code: "materialize_validation_failed",
|
|
1186
|
-
message: issue.message,
|
|
1187
|
-
severity: "error",
|
|
1188
|
-
path: issue.path,
|
|
1189
|
-
operationKind: "materializeSession"
|
|
1190
|
-
}));
|
|
1191
|
-
}
|
|
1192
|
-
function toMaterializeSnapshot(session, capabilities, input) {
|
|
1193
|
-
return {
|
|
1194
|
-
...toSessionSnapshot(session, capabilities),
|
|
1195
|
-
mode: "fullProject",
|
|
1196
|
-
reason: input.reason,
|
|
1197
|
-
materializeRevision: session.revision,
|
|
1198
|
-
saveRevision: session.lastSavedRevision,
|
|
1199
|
-
writtenPaths: [...input.writtenPaths],
|
|
1200
|
-
skippedPaths: [...input.skippedPaths],
|
|
1201
|
-
diagnostics: input.diagnostics.map((diagnostic) => ({ ...diagnostic }))
|
|
1202
|
-
};
|
|
1203
|
-
}
|
|
1204
|
-
function storageCanonicalTarget(input) {
|
|
1205
|
-
const canonicalProjectPath = input.canonicalProjectPath ?? (input.fileSystem.dirname(input.fairyPath) || ".");
|
|
1206
|
-
return {
|
|
1207
|
-
fileSystem: input.fileSystem,
|
|
1208
|
-
fairyPath: input.fairyPath,
|
|
1209
|
-
canonicalProjectPath,
|
|
1210
|
-
canonicalPathKey: input.canonicalPathKey ?? normalizeComparablePath(canonicalProjectPath)
|
|
1211
|
-
};
|
|
1212
|
-
}
|
|
1213
1037
|
function detachSharedByteViews(value, seen = /* @__PURE__ */ new WeakSet()) {
|
|
1214
1038
|
if (!value || typeof value !== "object" || seen.has(value)) return;
|
|
1215
1039
|
seen.add(value);
|
|
@@ -1222,32 +1046,16 @@ function detachSharedByteViews(value, seen = /* @__PURE__ */ new WeakSet()) {
|
|
|
1222
1046
|
}
|
|
1223
1047
|
}
|
|
1224
1048
|
var AuthoringService = class {
|
|
1225
|
-
|
|
1226
|
-
constructor(context, cacheService, eventService) {
|
|
1049
|
+
constructor(context, cacheService, eventService, sessionOperations) {
|
|
1227
1050
|
this.context = context;
|
|
1228
1051
|
this.cacheService = cacheService;
|
|
1229
1052
|
this.eventService = eventService;
|
|
1230
|
-
|
|
1231
|
-
async runSessionExclusive(sessionId, operation) {
|
|
1232
|
-
const previous = this.sessionOperations.get(sessionId) ?? Promise.resolve();
|
|
1233
|
-
let release = () => void 0;
|
|
1234
|
-
const current = new Promise((resolve) => {
|
|
1235
|
-
release = resolve;
|
|
1236
|
-
});
|
|
1237
|
-
const tail = previous.then(() => current);
|
|
1238
|
-
this.sessionOperations.set(sessionId, tail);
|
|
1239
|
-
await previous;
|
|
1240
|
-
try {
|
|
1241
|
-
return await operation();
|
|
1242
|
-
} finally {
|
|
1243
|
-
release();
|
|
1244
|
-
if (this.sessionOperations.get(sessionId) === tail) this.sessionOperations.delete(sessionId);
|
|
1245
|
-
}
|
|
1053
|
+
this.sessionOperations = sessionOperations;
|
|
1246
1054
|
}
|
|
1247
1055
|
async preflightTransaction(input) {
|
|
1248
1056
|
const queuedInput = structuredClone(input);
|
|
1249
1057
|
detachSharedByteViews(queuedInput);
|
|
1250
|
-
return this.
|
|
1058
|
+
return this.sessionOperations.run(queuedInput.sessionId, async () => {
|
|
1251
1059
|
const startedAt = Date.now();
|
|
1252
1060
|
const session = this.context.sessions.get(queuedInput.sessionId);
|
|
1253
1061
|
if (!session || session.closed) return failure("authoring", startedAt, createSessionNotFoundError(queuedInput.sessionId));
|
|
@@ -1287,7 +1095,7 @@ var AuthoringService = class {
|
|
|
1287
1095
|
async applyTransaction(input) {
|
|
1288
1096
|
const queuedInput = structuredClone(input);
|
|
1289
1097
|
detachSharedByteViews(queuedInput);
|
|
1290
|
-
return this.
|
|
1098
|
+
return this.sessionOperations.run(queuedInput.sessionId, () => this.applyTransactionExclusive(queuedInput));
|
|
1291
1099
|
}
|
|
1292
1100
|
async applyTransactionExclusive(input) {
|
|
1293
1101
|
const startedAt = Date.now();
|
|
@@ -1348,6 +1156,200 @@ var AuthoringService = class {
|
|
|
1348
1156
|
revision: session.revision
|
|
1349
1157
|
});
|
|
1350
1158
|
}
|
|
1159
|
+
};
|
|
1160
|
+
//#endregion
|
|
1161
|
+
//#region src/path-policy.ts
|
|
1162
|
+
function normalizeComparablePath(value) {
|
|
1163
|
+
const normalized = value.replace(/[/\\]+$/, "").replace(/\\/g, "/");
|
|
1164
|
+
const driveMatch = normalized.match(/^([a-z]:)(?:\/(.*))?$/i);
|
|
1165
|
+
const drivePrefix = driveMatch?.[1].toLowerCase() ?? "";
|
|
1166
|
+
const remainder = driveMatch ? driveMatch[2] ?? "" : normalized;
|
|
1167
|
+
const hasRoot = driveMatch ? true : remainder.startsWith("/");
|
|
1168
|
+
const rawSegments = remainder.split("/").filter((segment) => segment.length > 0);
|
|
1169
|
+
const segments = [];
|
|
1170
|
+
for (const segment of rawSegments) {
|
|
1171
|
+
if (segment === ".") continue;
|
|
1172
|
+
if (segment === "..") {
|
|
1173
|
+
if (segments.length > 0 && segments[segments.length - 1] !== "..") segments.pop();
|
|
1174
|
+
else if (!hasRoot) segments.push("..");
|
|
1175
|
+
continue;
|
|
1176
|
+
}
|
|
1177
|
+
segments.push(segment);
|
|
1178
|
+
}
|
|
1179
|
+
const joined = segments.join("/");
|
|
1180
|
+
return (drivePrefix ? `${drivePrefix}/${joined}`.replace(/\/$/, "") : hasRoot ? `/${joined}`.replace(/\/$/, "") : joined || ".").toLowerCase();
|
|
1181
|
+
}
|
|
1182
|
+
function createRuntimePathPolicy() {
|
|
1183
|
+
return {
|
|
1184
|
+
canonicalization: "realpath+normalized-casefold",
|
|
1185
|
+
sessionIdentity: "project-root",
|
|
1186
|
+
saveTarget: "opened-project-only",
|
|
1187
|
+
outputTargets: "deferred",
|
|
1188
|
+
workspaceBoundary: "project-root-only"
|
|
1189
|
+
};
|
|
1190
|
+
}
|
|
1191
|
+
async function assertProjectPathContained(fileSystem, projectRoot, targetPath) {
|
|
1192
|
+
const [resolvedRoot, resolvedTarget] = await Promise.all([fileSystem.resolvePath(projectRoot), fileSystem.resolvePath(targetPath)]);
|
|
1193
|
+
const root = normalizeComparablePath(resolvedRoot);
|
|
1194
|
+
const target = normalizeComparablePath(resolvedTarget);
|
|
1195
|
+
if (root === "." && !target.startsWith("/") && !/^[a-z]:\//i.test(target)) return;
|
|
1196
|
+
if (target === root || target.startsWith(`${root}/`)) return;
|
|
1197
|
+
const error = /* @__PURE__ */ new Error(`Project path escapes the opened root: ${targetPath}`);
|
|
1198
|
+
error.code = "EACCES";
|
|
1199
|
+
throw error;
|
|
1200
|
+
}
|
|
1201
|
+
async function resolveFairyPath(fileSystem, input) {
|
|
1202
|
+
const resolvedInput = fileSystem.resolve(input);
|
|
1203
|
+
const stat = await fileSystem.stat(resolvedInput);
|
|
1204
|
+
if (stat.isFile() && resolvedInput.endsWith(".fairy")) return await fileSystem.resolvePath(resolvedInput);
|
|
1205
|
+
if (stat.isDirectory()) {
|
|
1206
|
+
const fairyFiles = (await fileSystem.readdir(resolvedInput)).filter((entry) => entry.endsWith(".fairy"));
|
|
1207
|
+
if (fairyFiles.length === 1) return await fileSystem.resolvePath(fileSystem.join(resolvedInput, fairyFiles[0]));
|
|
1208
|
+
if (fairyFiles.length > 1) throw new Error(`Multiple .fairy files found in ${resolvedInput}: ${fairyFiles.join(", ")}`);
|
|
1209
|
+
throw new Error(`No .fairy file found in ${resolvedInput}`);
|
|
1210
|
+
}
|
|
1211
|
+
throw new Error(`Input is not a .fairy file or directory: ${resolvedInput}`);
|
|
1212
|
+
}
|
|
1213
|
+
async function resolveCanonicalProjectRoot(fileSystem, input) {
|
|
1214
|
+
const fairyPath = await resolveFairyPath(fileSystem, input);
|
|
1215
|
+
const canonicalProjectPath = await fileSystem.resolvePath(fileSystem.dirname(fairyPath));
|
|
1216
|
+
return {
|
|
1217
|
+
fairyPath,
|
|
1218
|
+
canonicalProjectPath,
|
|
1219
|
+
canonicalPathKey: normalizeComparablePath(canonicalProjectPath)
|
|
1220
|
+
};
|
|
1221
|
+
}
|
|
1222
|
+
async function validateSaveTarget(fileSystem, openedFairyPath, targetPath) {
|
|
1223
|
+
if (!targetPath) return null;
|
|
1224
|
+
const attemptedPath = await fileSystem.resolvePath(fileSystem.resolve(targetPath));
|
|
1225
|
+
const allowedPath = await fileSystem.resolvePath(openedFairyPath);
|
|
1226
|
+
if (normalizeComparablePath(attemptedPath) === normalizeComparablePath(allowedPath)) return null;
|
|
1227
|
+
return {
|
|
1228
|
+
code: "path_policy_violation",
|
|
1229
|
+
message: `Save target is restricted to the originally opened project file: ${allowedPath}`,
|
|
1230
|
+
policy: "save_target",
|
|
1231
|
+
attemptedPath,
|
|
1232
|
+
allowedPath
|
|
1233
|
+
};
|
|
1234
|
+
}
|
|
1235
|
+
//#endregion
|
|
1236
|
+
//#region src/services/session-project-writer.ts
|
|
1237
|
+
function createWriterFileSystem(fileSystem, projectRoot, writtenPaths, failedPaths) {
|
|
1238
|
+
const contained = async (targetPath, operation) => {
|
|
1239
|
+
await assertProjectPathContained(fileSystem, projectRoot, targetPath);
|
|
1240
|
+
return operation();
|
|
1241
|
+
};
|
|
1242
|
+
async function trackWrite(targetPath, write) {
|
|
1243
|
+
try {
|
|
1244
|
+
const result = await contained(targetPath, write);
|
|
1245
|
+
writtenPaths.push(targetPath);
|
|
1246
|
+
return result;
|
|
1247
|
+
} catch (error) {
|
|
1248
|
+
failedPaths.push(targetPath);
|
|
1249
|
+
throw error;
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
return {
|
|
1253
|
+
resolvePath: (path) => contained(path, () => fileSystem.resolvePath(path)),
|
|
1254
|
+
readFile: (path) => contained(path, () => fileSystem.readFile(path)),
|
|
1255
|
+
readFileRaw: (path) => contained(path, () => fileSystem.readFileRaw(path)),
|
|
1256
|
+
writeFile: (path, content) => trackWrite(path, async () => {
|
|
1257
|
+
await fileSystem.mkdir(fileSystem.dirname(path), { recursive: true });
|
|
1258
|
+
await fileSystem.writeFile(path, content);
|
|
1259
|
+
}),
|
|
1260
|
+
writeFileRaw: (path, data) => trackWrite(path, async () => {
|
|
1261
|
+
await fileSystem.mkdir(fileSystem.dirname(path), { recursive: true });
|
|
1262
|
+
await fileSystem.writeFileRaw(path, data);
|
|
1263
|
+
}),
|
|
1264
|
+
mkdir: (path) => contained(path, () => fileSystem.mkdir(path, { recursive: true })),
|
|
1265
|
+
readdir: (path) => contained(path, () => fileSystem.readdir(path)),
|
|
1266
|
+
async exists(path) {
|
|
1267
|
+
await assertProjectPathContained(fileSystem, projectRoot, path);
|
|
1268
|
+
try {
|
|
1269
|
+
await fileSystem.stat(path);
|
|
1270
|
+
return true;
|
|
1271
|
+
} catch {
|
|
1272
|
+
return false;
|
|
1273
|
+
}
|
|
1274
|
+
},
|
|
1275
|
+
join: (...paths) => fileSystem.join(...paths),
|
|
1276
|
+
dirname: (path) => fileSystem.dirname(path),
|
|
1277
|
+
unlink: (path) => trackWrite(path, () => fileSystem.unlink(path)),
|
|
1278
|
+
rmdir: (path) => trackWrite(path, () => fileSystem.rmdir(path))
|
|
1279
|
+
};
|
|
1280
|
+
}
|
|
1281
|
+
async function writeSessionProject(input) {
|
|
1282
|
+
const projectRoot = input.fileSystem.dirname(input.fairyPath);
|
|
1283
|
+
const write = async (fileSystem) => {
|
|
1284
|
+
await new _openfairygui_core_project_io.ProjectWriter(createWriterFileSystem(fileSystem, projectRoot, input.writtenPaths, input.failedPaths)).write(input.document, input.fairyPath, {
|
|
1285
|
+
staleSourceFiles: input.staleSourceFiles,
|
|
1286
|
+
staleResourceFolders: input.staleResourceFolders,
|
|
1287
|
+
staleBranchDirectories: input.staleBranchDirectories
|
|
1288
|
+
});
|
|
1289
|
+
};
|
|
1290
|
+
if (!input.fileSystem.runProjectWriteTransaction) return write(input.fileSystem);
|
|
1291
|
+
try {
|
|
1292
|
+
await input.fileSystem.runProjectWriteTransaction(projectRoot, write);
|
|
1293
|
+
} catch (error) {
|
|
1294
|
+
if (ProjectWriteTransactionError.is(error) && !error.diskMayBePartiallyUpdated) input.writtenPaths.length = 0;
|
|
1295
|
+
throw error;
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
//#endregion
|
|
1299
|
+
//#region src/services/persistence-service.ts
|
|
1300
|
+
function createCapabilityUnavailableError$1(message) {
|
|
1301
|
+
return {
|
|
1302
|
+
code: "capability_unavailable",
|
|
1303
|
+
message,
|
|
1304
|
+
capability: "fileSystem",
|
|
1305
|
+
requiredAdapter: "BackendFileSystem"
|
|
1306
|
+
};
|
|
1307
|
+
}
|
|
1308
|
+
function createUamFidelityUnsupportedError(session) {
|
|
1309
|
+
return {
|
|
1310
|
+
code: "uam_fidelity_unsupported",
|
|
1311
|
+
message: "The source project contains formal properties that the current UAM cannot preserve.",
|
|
1312
|
+
sessionId: session.sessionId,
|
|
1313
|
+
canonicalPathKey: session.canonicalPathKey
|
|
1314
|
+
};
|
|
1315
|
+
}
|
|
1316
|
+
function validationDiagnostics(sessionProject) {
|
|
1317
|
+
return (0, _openfairygui_core_uam.validateUamProject)(sessionProject).map((issue) => ({
|
|
1318
|
+
code: "materialize_validation_failed",
|
|
1319
|
+
message: issue.message,
|
|
1320
|
+
severity: "error",
|
|
1321
|
+
path: issue.path,
|
|
1322
|
+
operationKind: "materializeSession"
|
|
1323
|
+
}));
|
|
1324
|
+
}
|
|
1325
|
+
function toMaterializeSnapshot(session, capabilities, input) {
|
|
1326
|
+
return {
|
|
1327
|
+
...toSessionSnapshot(session, capabilities),
|
|
1328
|
+
mode: "fullProject",
|
|
1329
|
+
reason: input.reason,
|
|
1330
|
+
materializeRevision: session.revision,
|
|
1331
|
+
saveRevision: session.lastSavedRevision,
|
|
1332
|
+
writtenPaths: [...input.writtenPaths],
|
|
1333
|
+
skippedPaths: [...input.skippedPaths],
|
|
1334
|
+
diagnostics: input.diagnostics.map((diagnostic) => ({ ...diagnostic }))
|
|
1335
|
+
};
|
|
1336
|
+
}
|
|
1337
|
+
function storageCanonicalTarget(input) {
|
|
1338
|
+
const canonicalProjectPath = input.canonicalProjectPath ?? (input.fileSystem.dirname(input.fairyPath) || ".");
|
|
1339
|
+
return {
|
|
1340
|
+
fileSystem: input.fileSystem,
|
|
1341
|
+
fairyPath: input.fairyPath,
|
|
1342
|
+
canonicalProjectPath,
|
|
1343
|
+
canonicalPathKey: input.canonicalPathKey ?? normalizeComparablePath(canonicalProjectPath)
|
|
1344
|
+
};
|
|
1345
|
+
}
|
|
1346
|
+
var PersistenceService = class {
|
|
1347
|
+
constructor(context, cacheService, eventService, sessionOperations) {
|
|
1348
|
+
this.context = context;
|
|
1349
|
+
this.cacheService = cacheService;
|
|
1350
|
+
this.eventService = eventService;
|
|
1351
|
+
this.sessionOperations = sessionOperations;
|
|
1352
|
+
}
|
|
1351
1353
|
async saveSession(input) {
|
|
1352
1354
|
if (input.force === true || input.mode === "materializeCleanSession") return this.materializeSession({
|
|
1353
1355
|
sessionId: input.sessionId,
|
|
@@ -1357,7 +1359,8 @@ var AuthoringService = class {
|
|
|
1357
1359
|
mode: "fullProject",
|
|
1358
1360
|
reason: "force_save"
|
|
1359
1361
|
});
|
|
1360
|
-
|
|
1362
|
+
const captured = { ...input };
|
|
1363
|
+
return this.sessionOperations.run(captured.sessionId, () => this.saveSessionExclusive(captured));
|
|
1361
1364
|
}
|
|
1362
1365
|
finishSuccessfulSave(session) {
|
|
1363
1366
|
session.lastSavedRevision = session.revision;
|
|
@@ -1449,7 +1452,8 @@ var AuthoringService = class {
|
|
|
1449
1452
|
lastSavedRevision: session.lastSavedRevision,
|
|
1450
1453
|
committedPaths,
|
|
1451
1454
|
failedPaths,
|
|
1452
|
-
diskMayBePartiallyUpdated: !
|
|
1455
|
+
diskMayBePartiallyUpdated: !ProjectWriteTransactionError.is(error) || error.diskMayBePartiallyUpdated,
|
|
1456
|
+
...ProjectWriteTransactionError.is(error) && error.recoveryPaths.length ? { recoveryPaths: error.recoveryPaths } : {}
|
|
1453
1457
|
}, toSessionSnapshot(session, this.context.capabilities), {
|
|
1454
1458
|
sessionId: session.sessionId,
|
|
1455
1459
|
revision: session.revision
|
|
@@ -1457,7 +1461,11 @@ var AuthoringService = class {
|
|
|
1457
1461
|
}
|
|
1458
1462
|
}
|
|
1459
1463
|
async materializeSession(input) {
|
|
1460
|
-
|
|
1464
|
+
const captured = {
|
|
1465
|
+
...input,
|
|
1466
|
+
storage: input.storage && { ...input.storage }
|
|
1467
|
+
};
|
|
1468
|
+
return this.sessionOperations.run(captured.sessionId, () => this.materializeSessionExclusive(captured));
|
|
1461
1469
|
}
|
|
1462
1470
|
async materializeSessionExclusive(input) {
|
|
1463
1471
|
const startedAt = Date.now();
|
|
@@ -1474,24 +1482,21 @@ var AuthoringService = class {
|
|
|
1474
1482
|
revision: session.revision
|
|
1475
1483
|
});
|
|
1476
1484
|
const fairyPath = storageTarget?.fairyPath ?? session.fairyPath;
|
|
1485
|
+
if (session.lockHeld && (fileSystem !== session.fileSystem || fairyPath !== session.fairyPath || storageTarget && (storageTarget.canonicalPathKey !== session.canonicalPathKey || storageTarget.canonicalProjectPath !== session.canonicalProjectPath))) return failure("authoring", startedAt, {
|
|
1486
|
+
code: "path_policy_violation",
|
|
1487
|
+
message: "A locked file-backed session must keep its opened storage binding.",
|
|
1488
|
+
policy: "save_target",
|
|
1489
|
+
attemptedPath: fairyPath,
|
|
1490
|
+
allowedPath: session.fairyPath
|
|
1491
|
+
}, toSessionSnapshot(session, this.context.capabilities), {
|
|
1492
|
+
sessionId: session.sessionId,
|
|
1493
|
+
revision: session.revision
|
|
1494
|
+
});
|
|
1477
1495
|
const targetViolation = await validateSaveTarget(fileSystem, fairyPath, input.targetPath);
|
|
1478
1496
|
if (targetViolation) return failure("authoring", startedAt, targetViolation, toSessionSnapshot(session, this.context.capabilities), {
|
|
1479
1497
|
sessionId: session.sessionId,
|
|
1480
1498
|
revision: session.revision
|
|
1481
1499
|
});
|
|
1482
|
-
if (storageTarget) {
|
|
1483
|
-
const holderSessionId = this.context.sessionsByPath.get(storageTarget.canonicalPathKey);
|
|
1484
|
-
if (holderSessionId && holderSessionId !== session.sessionId) return failure("authoring", startedAt, {
|
|
1485
|
-
code: "lock_conflict",
|
|
1486
|
-
kind: "in_process_session_exists",
|
|
1487
|
-
message: `Project is already open in this backend runtime: ${storageTarget.canonicalProjectPath}`,
|
|
1488
|
-
canonicalPathKey: storageTarget.canonicalPathKey,
|
|
1489
|
-
holderSessionId
|
|
1490
|
-
}, toSessionSnapshot(session, this.context.capabilities), {
|
|
1491
|
-
sessionId: session.sessionId,
|
|
1492
|
-
revision: session.revision
|
|
1493
|
-
});
|
|
1494
|
-
}
|
|
1495
1500
|
if (session.uamFidelity === "unsupported") return failure("authoring", startedAt, createUamFidelityUnsupportedError(session), toSessionSnapshot(session, this.context.capabilities), {
|
|
1496
1501
|
sessionId: session.sessionId,
|
|
1497
1502
|
revision: session.revision
|
|
@@ -1535,6 +1540,11 @@ var AuthoringService = class {
|
|
|
1535
1540
|
const writtenPaths = [];
|
|
1536
1541
|
const failedPaths = [];
|
|
1537
1542
|
const skippedPaths = [];
|
|
1543
|
+
const reservation = storageTarget ? this.context.sessions.reserve(session.sessionId, storageTarget) : null;
|
|
1544
|
+
if (reservation && "code" in reservation) return failure("authoring", startedAt, reservation, toSessionSnapshot(session, this.context.capabilities), {
|
|
1545
|
+
sessionId: session.sessionId,
|
|
1546
|
+
revision: session.revision
|
|
1547
|
+
});
|
|
1538
1548
|
this.eventService.emit({
|
|
1539
1549
|
kind: "save.started",
|
|
1540
1550
|
sessionId: session.sessionId,
|
|
@@ -1564,14 +1574,7 @@ var AuthoringService = class {
|
|
|
1564
1574
|
session.pendingStaleBranchDirectories.clear();
|
|
1565
1575
|
}
|
|
1566
1576
|
if (isSessionStorageTarget || storageTarget) (0, _openfairygui_core_uam.commitUamProjectSourcePaths)(session.project);
|
|
1567
|
-
|
|
1568
|
-
this.context.sessionsByPath.delete(session.canonicalPathKey);
|
|
1569
|
-
session.fileSystem = storageTarget.fileSystem;
|
|
1570
|
-
session.fairyPath = storageTarget.fairyPath;
|
|
1571
|
-
session.canonicalProjectPath = storageTarget.canonicalProjectPath;
|
|
1572
|
-
session.canonicalPathKey = storageTarget.canonicalPathKey;
|
|
1573
|
-
this.context.sessionsByPath.set(session.canonicalPathKey, session.sessionId);
|
|
1574
|
-
}
|
|
1577
|
+
reservation?.commit(session);
|
|
1575
1578
|
this.finishSuccessfulSave(session);
|
|
1576
1579
|
return success("authoring", startedAt, toMaterializeSnapshot(session, this.context.capabilities, {
|
|
1577
1580
|
reason: input.reason,
|
|
@@ -1609,12 +1612,15 @@ var AuthoringService = class {
|
|
|
1609
1612
|
failedPaths,
|
|
1610
1613
|
skippedPaths,
|
|
1611
1614
|
diagnostics: diagnosticsFromError,
|
|
1612
|
-
diskMayBePartiallyUpdated: !
|
|
1615
|
+
diskMayBePartiallyUpdated: !ProjectWriteTransactionError.is(error) || error.diskMayBePartiallyUpdated,
|
|
1616
|
+
...ProjectWriteTransactionError.is(error) && error.recoveryPaths.length ? { recoveryPaths: error.recoveryPaths } : {}
|
|
1613
1617
|
}, toSessionSnapshot(session, this.context.capabilities), {
|
|
1614
1618
|
sessionId: session.sessionId,
|
|
1615
1619
|
revision: session.revision,
|
|
1616
1620
|
diagnostics: diagnosticsFromError
|
|
1617
1621
|
});
|
|
1622
|
+
} finally {
|
|
1623
|
+
reservation?.release();
|
|
1618
1624
|
}
|
|
1619
1625
|
}
|
|
1620
1626
|
};
|
|
@@ -1637,14 +1643,16 @@ function createCacheEntry(session, valid) {
|
|
|
1637
1643
|
};
|
|
1638
1644
|
}
|
|
1639
1645
|
var CacheService = class {
|
|
1640
|
-
|
|
1641
|
-
|
|
1646
|
+
cacheBySession = /* @__PURE__ */ new Map();
|
|
1647
|
+
constructor(getSession, events) {
|
|
1648
|
+
this.getSession = getSession;
|
|
1649
|
+
this.events = events;
|
|
1642
1650
|
}
|
|
1643
1651
|
getCacheSnapshot(input) {
|
|
1644
1652
|
const startedAt = Date.now();
|
|
1645
|
-
const session = this.
|
|
1653
|
+
const session = this.getSession(input.sessionId);
|
|
1646
1654
|
if (!session || session.closed) return failure("read", startedAt, createSessionNotFoundError(input.sessionId));
|
|
1647
|
-
const entry = this.
|
|
1655
|
+
const entry = this.cacheBySession.get(input.sessionId);
|
|
1648
1656
|
return success("read", startedAt, {
|
|
1649
1657
|
cacheRevision: entry?.revision ?? session.revision,
|
|
1650
1658
|
entries: entry ? [structuredClone(entry)] : []
|
|
@@ -1653,47 +1661,145 @@ var CacheService = class {
|
|
|
1653
1661
|
revision: session.revision
|
|
1654
1662
|
});
|
|
1655
1663
|
}
|
|
1664
|
+
refreshCache(input) {
|
|
1665
|
+
const startedAt = Date.now();
|
|
1666
|
+
const session = this.getSession(input.sessionId);
|
|
1667
|
+
if (!session || session.closed) return failure("runtime", startedAt, createSessionNotFoundError(input.sessionId));
|
|
1668
|
+
const entry = this.refreshSession(session);
|
|
1669
|
+
this.events.emit({
|
|
1670
|
+
kind: "cache.updated",
|
|
1671
|
+
sessionId: session.sessionId,
|
|
1672
|
+
canonicalPathKey: session.canonicalPathKey,
|
|
1673
|
+
revision: session.revision,
|
|
1674
|
+
cacheRevision: entry.revision,
|
|
1675
|
+
payload: { reason: input.reason ?? "manual" }
|
|
1676
|
+
});
|
|
1677
|
+
return success("runtime", startedAt, {
|
|
1678
|
+
cacheRevision: entry.revision,
|
|
1679
|
+
entries: [structuredClone(entry)]
|
|
1680
|
+
}, {
|
|
1681
|
+
sessionId: session.sessionId,
|
|
1682
|
+
revision: session.revision
|
|
1683
|
+
});
|
|
1684
|
+
}
|
|
1656
1685
|
refreshSession(session) {
|
|
1657
1686
|
const entry = createCacheEntry(session, true);
|
|
1658
|
-
this.
|
|
1687
|
+
this.cacheBySession.set(session.sessionId, entry);
|
|
1659
1688
|
return entry;
|
|
1660
1689
|
}
|
|
1661
1690
|
invalidateSession(session) {
|
|
1662
1691
|
const entry = createCacheEntry(session, false);
|
|
1663
|
-
this.
|
|
1692
|
+
this.cacheBySession.set(session.sessionId, entry);
|
|
1664
1693
|
return entry;
|
|
1665
1694
|
}
|
|
1666
1695
|
removeSession(sessionId) {
|
|
1667
|
-
this.
|
|
1696
|
+
this.cacheBySession.delete(sessionId);
|
|
1697
|
+
}
|
|
1698
|
+
};
|
|
1699
|
+
//#endregion
|
|
1700
|
+
//#region src/services/session-registry.ts
|
|
1701
|
+
/** Owns live sessions and path claims, including claims made before asynchronous I/O. */
|
|
1702
|
+
var SessionRegistry = class {
|
|
1703
|
+
sessions = /* @__PURE__ */ new Map();
|
|
1704
|
+
paths = /* @__PURE__ */ new Map();
|
|
1705
|
+
opening = /* @__PURE__ */ new Set();
|
|
1706
|
+
get(sessionId) {
|
|
1707
|
+
return this.sessions.get(sessionId);
|
|
1708
|
+
}
|
|
1709
|
+
has(sessionId) {
|
|
1710
|
+
return this.sessions.has(sessionId) || this.opening.has(sessionId);
|
|
1711
|
+
}
|
|
1712
|
+
reserve(sessionId, target) {
|
|
1713
|
+
const key = target.canonicalPathKey;
|
|
1714
|
+
const previous = this.paths.get(key);
|
|
1715
|
+
if (previous && previous.sessionId !== sessionId) return {
|
|
1716
|
+
code: "lock_conflict",
|
|
1717
|
+
kind: "in_process_session_exists",
|
|
1718
|
+
message: `Project is already open in this backend runtime: ${target.canonicalProjectPath}`,
|
|
1719
|
+
canonicalPathKey: key,
|
|
1720
|
+
holderSessionId: previous.sessionId,
|
|
1721
|
+
lockFilePath: target.lockFilePath
|
|
1722
|
+
};
|
|
1723
|
+
const claim = previous ?? { sessionId };
|
|
1724
|
+
this.paths.set(key, claim);
|
|
1725
|
+
if (!this.sessions.has(sessionId)) this.opening.add(sessionId);
|
|
1726
|
+
let committed = false;
|
|
1727
|
+
return {
|
|
1728
|
+
commit: (session) => {
|
|
1729
|
+
if (session.sessionId !== sessionId || this.paths.get(key) !== claim) throw new Error("Session path reservation lost.");
|
|
1730
|
+
if (session.canonicalPathKey !== key) this.releasePath(session.canonicalPathKey, sessionId);
|
|
1731
|
+
Object.assign(session, target);
|
|
1732
|
+
this.sessions.set(sessionId, session);
|
|
1733
|
+
this.opening.delete(sessionId);
|
|
1734
|
+
committed = true;
|
|
1735
|
+
},
|
|
1736
|
+
release: () => {
|
|
1737
|
+
if (committed) return;
|
|
1738
|
+
if (!previous && this.paths.get(key) === claim) this.paths.delete(key);
|
|
1739
|
+
this.opening.delete(sessionId);
|
|
1740
|
+
}
|
|
1741
|
+
};
|
|
1742
|
+
}
|
|
1743
|
+
remove(session) {
|
|
1744
|
+
if (this.sessions.get(session.sessionId) !== session) return;
|
|
1745
|
+
this.sessions.delete(session.sessionId);
|
|
1746
|
+
this.releasePath(session.canonicalPathKey, session.sessionId);
|
|
1747
|
+
}
|
|
1748
|
+
releasePath(key, sessionId) {
|
|
1749
|
+
if (this.paths.get(key)?.sessionId === sessionId) this.paths.delete(key);
|
|
1750
|
+
}
|
|
1751
|
+
};
|
|
1752
|
+
//#endregion
|
|
1753
|
+
//#region src/services/session-operation-queue.ts
|
|
1754
|
+
/** Serializes lifecycle and authoring operations for one session, without blocking other sessions. */
|
|
1755
|
+
var SessionOperationQueue = class {
|
|
1756
|
+
operations = /* @__PURE__ */ new Map();
|
|
1757
|
+
async run(sessionId, operation) {
|
|
1758
|
+
const previous = this.operations.get(sessionId) ?? Promise.resolve();
|
|
1759
|
+
let release = () => void 0;
|
|
1760
|
+
const current = new Promise((resolve) => {
|
|
1761
|
+
release = resolve;
|
|
1762
|
+
});
|
|
1763
|
+
const tail = previous.then(() => current);
|
|
1764
|
+
this.operations.set(sessionId, tail);
|
|
1765
|
+
await previous;
|
|
1766
|
+
try {
|
|
1767
|
+
return await operation();
|
|
1768
|
+
} finally {
|
|
1769
|
+
release();
|
|
1770
|
+
if (this.operations.get(sessionId) === tail) this.operations.delete(sessionId);
|
|
1771
|
+
}
|
|
1668
1772
|
}
|
|
1669
1773
|
};
|
|
1670
1774
|
//#endregion
|
|
1671
1775
|
//#region src/services/event-service.ts
|
|
1672
1776
|
const DEFAULT_EVENT_RETENTION_LIMIT = 1e3;
|
|
1673
1777
|
var EventService = class {
|
|
1674
|
-
|
|
1675
|
-
|
|
1778
|
+
eventsBySession = /* @__PURE__ */ new Map();
|
|
1779
|
+
sequence = 0;
|
|
1780
|
+
constructor(getSession) {
|
|
1781
|
+
this.getSession = getSession;
|
|
1676
1782
|
}
|
|
1677
1783
|
emit(event) {
|
|
1678
1784
|
const emitted = {
|
|
1679
1785
|
...event,
|
|
1680
|
-
sequence: this.
|
|
1786
|
+
sequence: ++this.sequence,
|
|
1681
1787
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1682
1788
|
diagnostics: (event.diagnostics ?? []).map((entry) => enrichBackendDiagnostic(entry, event.sessionId))
|
|
1683
1789
|
};
|
|
1684
1790
|
const sessionId = event.sessionId;
|
|
1685
1791
|
if (!sessionId) return emitted;
|
|
1686
|
-
const events = this.
|
|
1792
|
+
const events = this.eventsBySession.get(sessionId) ?? [];
|
|
1687
1793
|
events.push(emitted);
|
|
1688
1794
|
while (events.length > DEFAULT_EVENT_RETENTION_LIMIT) events.shift();
|
|
1689
|
-
this.
|
|
1795
|
+
this.eventsBySession.set(sessionId, events);
|
|
1690
1796
|
return emitted;
|
|
1691
1797
|
}
|
|
1692
1798
|
getEvents(input) {
|
|
1693
1799
|
const startedAt = Date.now();
|
|
1694
|
-
const session = this.
|
|
1800
|
+
const session = this.getSession(input.sessionId);
|
|
1695
1801
|
if (!session || session.closed) return failure("runtime", startedAt, createSessionNotFoundError(input.sessionId));
|
|
1696
|
-
const events = this.
|
|
1802
|
+
const events = this.eventsBySession.get(input.sessionId) ?? [];
|
|
1697
1803
|
const oldestSequence = events[0]?.sequence ?? (events.length === 0 ? 1 : 0);
|
|
1698
1804
|
const currentSequence = events.at(-1)?.sequence ?? 0;
|
|
1699
1805
|
const after = input.after === void 0 ? 0 : Number(input.after);
|
|
@@ -1728,281 +1834,7 @@ var EventService = class {
|
|
|
1728
1834
|
});
|
|
1729
1835
|
}
|
|
1730
1836
|
removeSession(sessionId) {
|
|
1731
|
-
this.
|
|
1732
|
-
}
|
|
1733
|
-
};
|
|
1734
|
-
//#endregion
|
|
1735
|
-
//#region src/services/job-service.ts
|
|
1736
|
-
const COMPLETED_JOB_RETENTION_LIMIT = 100;
|
|
1737
|
-
const REFRESH_START_DELAY_MS = 0;
|
|
1738
|
-
const REFRESH_COMPLETE_DELAY_MS = 50;
|
|
1739
|
-
function isTerminal(status) {
|
|
1740
|
-
return status === "completed" || status === "failed" || status === "cancelled";
|
|
1741
|
-
}
|
|
1742
|
-
var JobService = class {
|
|
1743
|
-
constructor(context, cacheService, eventService) {
|
|
1744
|
-
this.context = context;
|
|
1745
|
-
this.cacheService = cacheService;
|
|
1746
|
-
this.eventService = eventService;
|
|
1747
|
-
}
|
|
1748
|
-
cancellationRequests = /* @__PURE__ */ new Set();
|
|
1749
|
-
getJobs(sessionId) {
|
|
1750
|
-
return this.context.jobsBySession.get(sessionId) ?? [];
|
|
1751
|
-
}
|
|
1752
|
-
setJobs(sessionId, jobs) {
|
|
1753
|
-
const retained = [];
|
|
1754
|
-
let terminalCount = 0;
|
|
1755
|
-
for (let index = jobs.length - 1; index >= 0; index -= 1) {
|
|
1756
|
-
const job = jobs[index];
|
|
1757
|
-
if (isTerminal(job.status)) {
|
|
1758
|
-
if (terminalCount >= COMPLETED_JOB_RETENTION_LIMIT) continue;
|
|
1759
|
-
terminalCount += 1;
|
|
1760
|
-
}
|
|
1761
|
-
retained.push(job);
|
|
1762
|
-
}
|
|
1763
|
-
this.context.jobsBySession.set(sessionId, retained.reverse());
|
|
1764
|
-
}
|
|
1765
|
-
refreshCache(input) {
|
|
1766
|
-
const startedAt = Date.now();
|
|
1767
|
-
const session = this.context.sessions.get(input.sessionId);
|
|
1768
|
-
if (!session || session.closed) return failure("runtime", startedAt, createSessionNotFoundError(input.sessionId));
|
|
1769
|
-
const jobId = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
|
1770
|
-
const job = {
|
|
1771
|
-
jobId,
|
|
1772
|
-
kind: "cache.refresh",
|
|
1773
|
-
status: "queued",
|
|
1774
|
-
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1775
|
-
sessionId: session.sessionId,
|
|
1776
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
1777
|
-
revision: session.revision,
|
|
1778
|
-
diagnostics: [],
|
|
1779
|
-
progress: {
|
|
1780
|
-
completed: 0,
|
|
1781
|
-
total: 1,
|
|
1782
|
-
message: input.reason ?? "manual"
|
|
1783
|
-
}
|
|
1784
|
-
};
|
|
1785
|
-
const jobs = this.getJobs(session.sessionId);
|
|
1786
|
-
jobs.push(job);
|
|
1787
|
-
this.setJobs(session.sessionId, jobs);
|
|
1788
|
-
this.eventService.emit({
|
|
1789
|
-
kind: "job.created",
|
|
1790
|
-
sessionId: session.sessionId,
|
|
1791
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
1792
|
-
revision: session.revision,
|
|
1793
|
-
jobId
|
|
1794
|
-
});
|
|
1795
|
-
this.scheduleRefreshJob(session.sessionId, jobId);
|
|
1796
|
-
return success("runtime", startedAt, structuredClone(job), {
|
|
1797
|
-
sessionId: session.sessionId,
|
|
1798
|
-
revision: session.revision
|
|
1799
|
-
});
|
|
1800
|
-
}
|
|
1801
|
-
getJob(input) {
|
|
1802
|
-
const startedAt = Date.now();
|
|
1803
|
-
const session = this.context.sessions.get(input.sessionId);
|
|
1804
|
-
if (!session || session.closed) return failure("runtime", startedAt, createSessionNotFoundError(input.sessionId));
|
|
1805
|
-
const job = this.getJobs(input.sessionId).find((item) => item.jobId === input.jobId);
|
|
1806
|
-
if (!job) return failure("runtime", startedAt, {
|
|
1807
|
-
code: "job_not_found",
|
|
1808
|
-
message: `Job was not found: ${input.jobId}`,
|
|
1809
|
-
sessionId: input.sessionId,
|
|
1810
|
-
jobId: input.jobId
|
|
1811
|
-
}, void 0, {
|
|
1812
|
-
sessionId: session.sessionId,
|
|
1813
|
-
revision: session.revision
|
|
1814
|
-
});
|
|
1815
|
-
return success("runtime", startedAt, structuredClone(job), {
|
|
1816
|
-
sessionId: session.sessionId,
|
|
1817
|
-
revision: session.revision
|
|
1818
|
-
});
|
|
1819
|
-
}
|
|
1820
|
-
listJobs(input) {
|
|
1821
|
-
const startedAt = Date.now();
|
|
1822
|
-
const session = this.context.sessions.get(input.sessionId);
|
|
1823
|
-
if (!session || session.closed) return failure("runtime", startedAt, createSessionNotFoundError(input.sessionId));
|
|
1824
|
-
let jobs = [...this.getJobs(input.sessionId)];
|
|
1825
|
-
if (input.kind) jobs = jobs.filter((job) => job.kind === input.kind);
|
|
1826
|
-
if (input.status) if (input.status === "active") jobs = jobs.filter((job) => !isTerminal(job.status));
|
|
1827
|
-
else if (input.status === "terminal") jobs = jobs.filter((job) => isTerminal(job.status));
|
|
1828
|
-
else jobs = jobs.filter((job) => job.status === input.status);
|
|
1829
|
-
if (input.limit !== void 0) jobs = jobs.slice(0, Math.max(0, input.limit));
|
|
1830
|
-
return success("runtime", startedAt, { jobs: jobs.map((job) => structuredClone(job)) }, {
|
|
1831
|
-
sessionId: session.sessionId,
|
|
1832
|
-
revision: session.revision
|
|
1833
|
-
});
|
|
1834
|
-
}
|
|
1835
|
-
cancelJob(input) {
|
|
1836
|
-
const startedAt = Date.now();
|
|
1837
|
-
const session = this.context.sessions.get(input.sessionId);
|
|
1838
|
-
if (!session || session.closed) return failure("runtime", startedAt, createSessionNotFoundError(input.sessionId));
|
|
1839
|
-
const job = this.getJobs(input.sessionId).find((item) => item.jobId === input.jobId);
|
|
1840
|
-
if (!job) return failure("runtime", startedAt, {
|
|
1841
|
-
code: "job_not_found",
|
|
1842
|
-
message: `Job was not found: ${input.jobId}`,
|
|
1843
|
-
sessionId: input.sessionId,
|
|
1844
|
-
jobId: input.jobId
|
|
1845
|
-
}, void 0, {
|
|
1846
|
-
sessionId: session.sessionId,
|
|
1847
|
-
revision: session.revision
|
|
1848
|
-
});
|
|
1849
|
-
if (isTerminal(job.status)) return failure("runtime", startedAt, {
|
|
1850
|
-
code: "job_not_cancellable",
|
|
1851
|
-
message: `Job is already terminal: ${input.jobId}`,
|
|
1852
|
-
sessionId: input.sessionId,
|
|
1853
|
-
jobId: input.jobId,
|
|
1854
|
-
status: job.status
|
|
1855
|
-
}, void 0, {
|
|
1856
|
-
sessionId: session.sessionId,
|
|
1857
|
-
revision: session.revision
|
|
1858
|
-
});
|
|
1859
|
-
this.cancellationRequests.add(job.jobId);
|
|
1860
|
-
this.eventService.emit({
|
|
1861
|
-
kind: "job.cancelRequested",
|
|
1862
|
-
sessionId: input.sessionId,
|
|
1863
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
1864
|
-
revision: session.revision,
|
|
1865
|
-
jobId: job.jobId
|
|
1866
|
-
});
|
|
1867
|
-
const cancelled = this.cancelRefreshJob(session.sessionId, job);
|
|
1868
|
-
return success("runtime", startedAt, structuredClone(cancelled), {
|
|
1869
|
-
sessionId: session.sessionId,
|
|
1870
|
-
revision: session.revision
|
|
1871
|
-
});
|
|
1872
|
-
}
|
|
1873
|
-
removeSession(sessionId) {
|
|
1874
|
-
for (const job of this.getJobs(sessionId)) this.cancellationRequests.delete(job.jobId);
|
|
1875
|
-
this.context.jobsBySession.delete(sessionId);
|
|
1876
|
-
}
|
|
1877
|
-
replaceJob(sessionId, nextJob) {
|
|
1878
|
-
const jobs = this.getJobs(sessionId).map((job) => job.jobId === nextJob.jobId ? nextJob : job);
|
|
1879
|
-
this.setJobs(sessionId, jobs);
|
|
1880
|
-
}
|
|
1881
|
-
scheduleRefreshJob(sessionId, jobId) {
|
|
1882
|
-
setTimeout(() => this.startRefreshJob(sessionId, jobId), REFRESH_START_DELAY_MS);
|
|
1883
|
-
}
|
|
1884
|
-
startRefreshJob(sessionId, jobId) {
|
|
1885
|
-
const session = this.context.sessions.get(sessionId);
|
|
1886
|
-
if (!session || session.closed) return;
|
|
1887
|
-
const job = this.getJobs(sessionId).find((item) => item.jobId === jobId);
|
|
1888
|
-
if (!job || isTerminal(job.status)) return;
|
|
1889
|
-
if (this.cancellationRequests.has(jobId)) {
|
|
1890
|
-
this.cancelRefreshJob(sessionId, job);
|
|
1891
|
-
return;
|
|
1892
|
-
}
|
|
1893
|
-
const running = {
|
|
1894
|
-
...job,
|
|
1895
|
-
status: "running",
|
|
1896
|
-
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1897
|
-
progress: {
|
|
1898
|
-
completed: 0,
|
|
1899
|
-
total: 1,
|
|
1900
|
-
message: "refreshing cache"
|
|
1901
|
-
}
|
|
1902
|
-
};
|
|
1903
|
-
this.replaceJob(sessionId, running);
|
|
1904
|
-
this.eventService.emit({
|
|
1905
|
-
kind: "job.started",
|
|
1906
|
-
sessionId,
|
|
1907
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
1908
|
-
revision: session.revision,
|
|
1909
|
-
jobId
|
|
1910
|
-
});
|
|
1911
|
-
this.eventService.emit({
|
|
1912
|
-
kind: "job.progress",
|
|
1913
|
-
sessionId,
|
|
1914
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
1915
|
-
revision: session.revision,
|
|
1916
|
-
jobId,
|
|
1917
|
-
payload: running.progress
|
|
1918
|
-
});
|
|
1919
|
-
setTimeout(() => this.completeRefreshJob(sessionId, jobId), REFRESH_COMPLETE_DELAY_MS);
|
|
1920
|
-
}
|
|
1921
|
-
completeRefreshJob(sessionId, jobId) {
|
|
1922
|
-
const session = this.context.sessions.get(sessionId);
|
|
1923
|
-
if (!session || session.closed) return;
|
|
1924
|
-
const job = this.getJobs(sessionId).find((item) => item.jobId === jobId);
|
|
1925
|
-
if (!job || isTerminal(job.status)) return;
|
|
1926
|
-
if (this.cancellationRequests.has(jobId)) {
|
|
1927
|
-
this.cancelRefreshJob(sessionId, job);
|
|
1928
|
-
return;
|
|
1929
|
-
}
|
|
1930
|
-
try {
|
|
1931
|
-
const entry = this.cacheService.refreshSession(session);
|
|
1932
|
-
const completed = {
|
|
1933
|
-
...job,
|
|
1934
|
-
status: "completed",
|
|
1935
|
-
finishedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1936
|
-
cacheRevision: entry.revision,
|
|
1937
|
-
progress: {
|
|
1938
|
-
completed: 1,
|
|
1939
|
-
total: 1,
|
|
1940
|
-
message: "cache refreshed"
|
|
1941
|
-
},
|
|
1942
|
-
result: { cacheRevision: entry.revision }
|
|
1943
|
-
};
|
|
1944
|
-
this.replaceJob(sessionId, completed);
|
|
1945
|
-
this.eventService.emit({
|
|
1946
|
-
kind: "job.completed",
|
|
1947
|
-
sessionId,
|
|
1948
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
1949
|
-
revision: session.revision,
|
|
1950
|
-
cacheRevision: entry.revision,
|
|
1951
|
-
jobId
|
|
1952
|
-
});
|
|
1953
|
-
this.eventService.emit({
|
|
1954
|
-
kind: "cache.updated",
|
|
1955
|
-
sessionId,
|
|
1956
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
1957
|
-
revision: session.revision,
|
|
1958
|
-
cacheRevision: entry.revision
|
|
1959
|
-
});
|
|
1960
|
-
} catch (error) {
|
|
1961
|
-
const failed = {
|
|
1962
|
-
...job,
|
|
1963
|
-
status: "failed",
|
|
1964
|
-
finishedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1965
|
-
error: {
|
|
1966
|
-
code: "cache_refresh_failed",
|
|
1967
|
-
message: error instanceof Error ? error.message : "Cache refresh failed",
|
|
1968
|
-
sessionId,
|
|
1969
|
-
jobId
|
|
1970
|
-
}
|
|
1971
|
-
};
|
|
1972
|
-
this.replaceJob(sessionId, failed);
|
|
1973
|
-
this.eventService.emit({
|
|
1974
|
-
kind: "job.failed",
|
|
1975
|
-
sessionId,
|
|
1976
|
-
canonicalPathKey: session.canonicalPathKey,
|
|
1977
|
-
revision: session.revision,
|
|
1978
|
-
jobId,
|
|
1979
|
-
diagnostics: failed.diagnostics
|
|
1980
|
-
});
|
|
1981
|
-
}
|
|
1982
|
-
}
|
|
1983
|
-
cancelRefreshJob(sessionId, job) {
|
|
1984
|
-
const session = this.context.sessions.get(sessionId);
|
|
1985
|
-
const cancelled = {
|
|
1986
|
-
...job,
|
|
1987
|
-
status: "cancelled",
|
|
1988
|
-
finishedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1989
|
-
error: {
|
|
1990
|
-
code: "job_cancelled",
|
|
1991
|
-
message: `Job was cancelled: ${job.jobId}`,
|
|
1992
|
-
sessionId,
|
|
1993
|
-
jobId: job.jobId
|
|
1994
|
-
}
|
|
1995
|
-
};
|
|
1996
|
-
this.replaceJob(sessionId, cancelled);
|
|
1997
|
-
this.cancellationRequests.delete(job.jobId);
|
|
1998
|
-
this.eventService.emit({
|
|
1999
|
-
kind: "job.cancelled",
|
|
2000
|
-
sessionId,
|
|
2001
|
-
canonicalPathKey: session?.canonicalPathKey ?? job.canonicalPathKey,
|
|
2002
|
-
revision: session?.revision ?? job.revision,
|
|
2003
|
-
jobId: job.jobId
|
|
2004
|
-
});
|
|
2005
|
-
return cancelled;
|
|
1837
|
+
this.eventsBySession.delete(sessionId);
|
|
2006
1838
|
}
|
|
2007
1839
|
};
|
|
2008
1840
|
//#endregion
|
|
@@ -2092,24 +1924,25 @@ function toProjectOutline(session) {
|
|
|
2092
1924
|
};
|
|
2093
1925
|
}
|
|
2094
1926
|
var ReadService = class {
|
|
2095
|
-
constructor(
|
|
2096
|
-
this.
|
|
1927
|
+
constructor(getSessionState, capabilities) {
|
|
1928
|
+
this.getSessionState = getSessionState;
|
|
1929
|
+
this.capabilities = capabilities;
|
|
2097
1930
|
}
|
|
2098
1931
|
getCapabilities() {
|
|
2099
|
-
return success("read", Date.now(),
|
|
1932
|
+
return success("read", Date.now(), cloneReadData(this.capabilities));
|
|
2100
1933
|
}
|
|
2101
1934
|
getSession(input) {
|
|
2102
1935
|
const startedAt = Date.now();
|
|
2103
|
-
const session = this.
|
|
1936
|
+
const session = this.getSessionState(input.sessionId);
|
|
2104
1937
|
if (!session || session.closed) return failure("read", startedAt, createSessionNotFoundError(input.sessionId));
|
|
2105
|
-
return success("read", startedAt, toSessionSnapshot(session, this.
|
|
1938
|
+
return success("read", startedAt, toSessionSnapshot(session, this.capabilities), {
|
|
2106
1939
|
sessionId: session.sessionId,
|
|
2107
1940
|
revision: session.revision
|
|
2108
1941
|
});
|
|
2109
1942
|
}
|
|
2110
1943
|
getProjectOutline(input) {
|
|
2111
1944
|
const startedAt = Date.now();
|
|
2112
|
-
const session = this.
|
|
1945
|
+
const session = this.getSessionState(input.sessionId);
|
|
2113
1946
|
if (!session || session.closed) return failure("read", startedAt, createSessionNotFoundError(input.sessionId));
|
|
2114
1947
|
return success("read", startedAt, toProjectOutline(session), {
|
|
2115
1948
|
sessionId: session.sessionId,
|
|
@@ -2118,7 +1951,7 @@ var ReadService = class {
|
|
|
2118
1951
|
}
|
|
2119
1952
|
queryEntity(input) {
|
|
2120
1953
|
const startedAt = Date.now();
|
|
2121
|
-
const session = this.
|
|
1954
|
+
const session = this.getSessionState(input.sessionId);
|
|
2122
1955
|
if (!session || session.closed) return failure("read", startedAt, createSessionNotFoundError(input.sessionId));
|
|
2123
1956
|
const meta = {
|
|
2124
1957
|
sessionId: session.sessionId,
|
|
@@ -2139,7 +1972,7 @@ var ReadService = class {
|
|
|
2139
1972
|
entity
|
|
2140
1973
|
};
|
|
2141
1974
|
const problem = queryResponseProblem(data);
|
|
2142
|
-
return problem ? reject(problem) : success("read", startedAt,
|
|
1975
|
+
return problem ? reject(problem) : success("read", startedAt, cloneReadData(data), meta);
|
|
2143
1976
|
};
|
|
2144
1977
|
if (target.kind === "project") {
|
|
2145
1978
|
if (Object.hasOwn(target, "selector")) return reject("invalid_query");
|
|
@@ -2233,7 +2066,7 @@ var ReadService = class {
|
|
|
2233
2066
|
}
|
|
2234
2067
|
resolveReadSession(input, startedAt) {
|
|
2235
2068
|
if (!input || typeof input.sessionId !== "string" || !input.sessionId.length || input.sessionId.length > 256 || input.expectedRevision !== void 0 && (!Number.isSafeInteger(input.expectedRevision) || input.expectedRevision < 0)) return readFailure(startedAt, typeof input?.sessionId === "string" ? input.sessionId : "", "invalid_query");
|
|
2236
|
-
const session = this.
|
|
2069
|
+
const session = this.getSessionState(input.sessionId);
|
|
2237
2070
|
if (!session || session.closed) return failure("read", startedAt, createSessionNotFoundError(input.sessionId));
|
|
2238
2071
|
const meta = {
|
|
2239
2072
|
sessionId: session.sessionId,
|
|
@@ -2277,7 +2110,7 @@ var ReadService = class {
|
|
|
2277
2110
|
}
|
|
2278
2111
|
};
|
|
2279
2112
|
const problem = queryResponseProblem(data, BACKEND_SESSION_READ_LIMITS.model);
|
|
2280
|
-
return problem ? readFailure(startedAt, session.sessionId, problem, session.revision) : success("read", startedAt,
|
|
2113
|
+
return problem ? readFailure(startedAt, session.sessionId, problem, session.revision) : success("read", startedAt, cloneReadData(data), meta);
|
|
2281
2114
|
}
|
|
2282
2115
|
readResourceBytes(input) {
|
|
2283
2116
|
const startedAt = Date.now();
|
|
@@ -2307,10 +2140,10 @@ var ReadService = class {
|
|
|
2307
2140
|
}
|
|
2308
2141
|
validateSession(input) {
|
|
2309
2142
|
const startedAt = Date.now();
|
|
2310
|
-
const session = this.
|
|
2143
|
+
const session = this.getSessionState(input.sessionId);
|
|
2311
2144
|
if (!session || session.closed) return failure("read", startedAt, createSessionNotFoundError(input.sessionId));
|
|
2312
|
-
const report = (0, _openfairygui_functions.validateProject)(session.project, {
|
|
2313
|
-
readDiagnostics: session.readDiagnostics,
|
|
2145
|
+
const report = (0, _openfairygui_functions.validateProject)(cloneReadData(session.project), {
|
|
2146
|
+
readDiagnostics: cloneReadData(session.readDiagnostics),
|
|
2314
2147
|
complete: session.readComplete,
|
|
2315
2148
|
validateSources: true
|
|
2316
2149
|
});
|
|
@@ -2349,6 +2182,7 @@ function createProjectReaderFileSystem(fileSystem, projectRoot) {
|
|
|
2349
2182
|
return operation();
|
|
2350
2183
|
};
|
|
2351
2184
|
return {
|
|
2185
|
+
stat: (path) => contained(path, () => fileSystem.stat(path)),
|
|
2352
2186
|
readFile(filePath) {
|
|
2353
2187
|
return contained(filePath, () => fileSystem.readFile(filePath));
|
|
2354
2188
|
},
|
|
@@ -2396,11 +2230,10 @@ async function hasFullUamFidelity(document, project) {
|
|
|
2396
2230
|
return capturedFilesEqual(sourceFiles, materializedFiles) && capturedDirectoriesEqual(sourceDirectories, materializedDirectories);
|
|
2397
2231
|
}
|
|
2398
2232
|
var RuntimeService = class {
|
|
2399
|
-
constructor(context, cacheService, eventService
|
|
2233
|
+
constructor(context, cacheService, eventService) {
|
|
2400
2234
|
this.context = context;
|
|
2401
2235
|
this.cacheService = cacheService;
|
|
2402
2236
|
this.eventService = eventService;
|
|
2403
|
-
this.jobService = jobService;
|
|
2404
2237
|
}
|
|
2405
2238
|
async openSession(input) {
|
|
2406
2239
|
const startedAt = Date.now();
|
|
@@ -2423,16 +2256,14 @@ var RuntimeService = class {
|
|
|
2423
2256
|
}
|
|
2424
2257
|
const { fairyPath, canonicalProjectPath, canonicalPathKey } = await resolveCanonicalProjectRoot(fileSystem, input.projectPath);
|
|
2425
2258
|
await fileSystem.validateProjectRoot?.(canonicalProjectPath);
|
|
2426
|
-
const existingSessionId = this.context.sessionsByPath.get(canonicalPathKey);
|
|
2427
2259
|
const lockFilePath = fileSystem.getSessionLockPath?.(canonicalProjectPath) ?? fileSystem.join(canonicalProjectPath, ".openfairygui.backend.lock");
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
kind: "in_process_session_exists",
|
|
2431
|
-
message: `Project is already open in this backend runtime: ${canonicalProjectPath}`,
|
|
2260
|
+
const sessionId = randomId();
|
|
2261
|
+
const reservation = this.context.sessions.reserve(sessionId, {
|
|
2432
2262
|
canonicalPathKey,
|
|
2433
|
-
|
|
2263
|
+
canonicalProjectPath,
|
|
2434
2264
|
lockFilePath
|
|
2435
2265
|
});
|
|
2266
|
+
if ("code" in reservation) return failure("runtime", startedAt, reservation);
|
|
2436
2267
|
let sessionLock = null;
|
|
2437
2268
|
try {
|
|
2438
2269
|
sessionLock = await fileSystem.acquireSessionLock(lockFilePath);
|
|
@@ -2448,7 +2279,6 @@ var RuntimeService = class {
|
|
|
2448
2279
|
if (!read.document) throw new Error(read.diagnostics[0]?.message ?? `Unable to read project: ${fairyPath}`);
|
|
2449
2280
|
const document = read.document;
|
|
2450
2281
|
const project = (0, _openfairygui_core_uam.liftDocumentToUamProject)(document);
|
|
2451
|
-
const sessionId = randomId();
|
|
2452
2282
|
const session = {
|
|
2453
2283
|
sessionId,
|
|
2454
2284
|
fairyPath,
|
|
@@ -2460,7 +2290,7 @@ var RuntimeService = class {
|
|
|
2460
2290
|
project,
|
|
2461
2291
|
readDiagnostics: read.diagnostics,
|
|
2462
2292
|
readComplete: read.complete,
|
|
2463
|
-
uamFidelity: await hasFullUamFidelity(document, project) ? "full" : "unsupported",
|
|
2293
|
+
uamFidelity: read.complete && await hasFullUamFidelity(document, project) ? "full" : "unsupported",
|
|
2464
2294
|
revision: 0,
|
|
2465
2295
|
lastSavedRevision: 0,
|
|
2466
2296
|
pendingStaleSourceFiles: /* @__PURE__ */ new Map(),
|
|
@@ -2470,8 +2300,7 @@ var RuntimeService = class {
|
|
|
2470
2300
|
lockHeld: true,
|
|
2471
2301
|
closed: false
|
|
2472
2302
|
};
|
|
2473
|
-
|
|
2474
|
-
this.context.sessionsByPath.set(canonicalPathKey, sessionId);
|
|
2303
|
+
reservation.commit(session);
|
|
2475
2304
|
this.cacheService.refreshSession(session);
|
|
2476
2305
|
this.eventService.emit({
|
|
2477
2306
|
kind: "session.opened",
|
|
@@ -2497,6 +2326,8 @@ var RuntimeService = class {
|
|
|
2497
2326
|
message: error instanceof Error ? error.message : String(error)
|
|
2498
2327
|
});
|
|
2499
2328
|
throw error;
|
|
2329
|
+
} finally {
|
|
2330
|
+
reservation.release();
|
|
2500
2331
|
}
|
|
2501
2332
|
}
|
|
2502
2333
|
openProjectSession(input) {
|
|
@@ -2511,14 +2342,12 @@ var RuntimeService = class {
|
|
|
2511
2342
|
const memoryProjectPath = `memory://${sessionId}`;
|
|
2512
2343
|
const canonicalProjectPath = storage?.canonicalProjectPath ?? input.canonicalProjectPath ?? (storage ? storage.fileSystem.dirname(storage.fairyPath) || "." : memoryProjectPath);
|
|
2513
2344
|
const canonicalPathKey = storage?.canonicalPathKey ?? input.canonicalPathKey ?? (storage ? normalizeComparablePath(canonicalProjectPath) : canonicalProjectPath.toLowerCase());
|
|
2514
|
-
const
|
|
2515
|
-
|
|
2516
|
-
code: "lock_conflict",
|
|
2517
|
-
kind: "in_process_session_exists",
|
|
2518
|
-
message: `Project is already open in this backend runtime: ${canonicalProjectPath}`,
|
|
2345
|
+
const project = (0, _openfairygui_core_uam.normalizeUamProject)(input.project);
|
|
2346
|
+
const reservation = this.context.sessions.reserve(sessionId, {
|
|
2519
2347
|
canonicalPathKey,
|
|
2520
|
-
|
|
2348
|
+
canonicalProjectPath
|
|
2521
2349
|
});
|
|
2350
|
+
if ("code" in reservation) return failure("runtime", startedAt, reservation);
|
|
2522
2351
|
const session = {
|
|
2523
2352
|
sessionId,
|
|
2524
2353
|
fairyPath: storage?.fairyPath ?? canonicalProjectPath,
|
|
@@ -2527,7 +2356,7 @@ var RuntimeService = class {
|
|
|
2527
2356
|
lockFilePath: "",
|
|
2528
2357
|
sessionLock: null,
|
|
2529
2358
|
fileSystem: storage?.fileSystem,
|
|
2530
|
-
project
|
|
2359
|
+
project,
|
|
2531
2360
|
readDiagnostics: [],
|
|
2532
2361
|
readComplete: true,
|
|
2533
2362
|
uamFidelity: "full",
|
|
@@ -2540,8 +2369,7 @@ var RuntimeService = class {
|
|
|
2540
2369
|
lockHeld: false,
|
|
2541
2370
|
closed: false
|
|
2542
2371
|
};
|
|
2543
|
-
|
|
2544
|
-
this.context.sessionsByPath.set(canonicalPathKey, sessionId);
|
|
2372
|
+
reservation.commit(session);
|
|
2545
2373
|
this.cacheService.refreshSession(session);
|
|
2546
2374
|
this.eventService.emit({
|
|
2547
2375
|
kind: "session.opened",
|
|
@@ -2564,14 +2392,24 @@ var RuntimeService = class {
|
|
|
2564
2392
|
canonicalPathKey: session.canonicalPathKey,
|
|
2565
2393
|
revision: session.revision
|
|
2566
2394
|
});
|
|
2567
|
-
|
|
2395
|
+
try {
|
|
2396
|
+
await session.sessionLock?.release();
|
|
2397
|
+
} catch (error) {
|
|
2398
|
+
return failure("runtime", startedAt, {
|
|
2399
|
+
code: "session_close_failed",
|
|
2400
|
+
message: error instanceof Error ? error.message : String(error),
|
|
2401
|
+
sessionId: session.sessionId,
|
|
2402
|
+
lockFilePath: session.lockFilePath
|
|
2403
|
+
}, toSessionSnapshot(session, this.context.capabilities), {
|
|
2404
|
+
sessionId: session.sessionId,
|
|
2405
|
+
revision: session.revision
|
|
2406
|
+
});
|
|
2407
|
+
}
|
|
2568
2408
|
session.sessionLock = null;
|
|
2569
2409
|
session.lockHeld = false;
|
|
2570
2410
|
session.closed = true;
|
|
2571
|
-
this.context.sessions.
|
|
2572
|
-
this.context.sessionsByPath.delete(session.canonicalPathKey);
|
|
2411
|
+
this.context.sessions.remove(session);
|
|
2573
2412
|
this.cacheService.removeSession(session.sessionId);
|
|
2574
|
-
this.jobService.removeSession(session.sessionId);
|
|
2575
2413
|
this.eventService.emit({
|
|
2576
2414
|
kind: "session.closed",
|
|
2577
2415
|
sessionId: session.sessionId,
|
|
@@ -2624,9 +2462,6 @@ const BACKEND_METHODS = [
|
|
|
2624
2462
|
"materializeSession",
|
|
2625
2463
|
"closeSession",
|
|
2626
2464
|
"getEvents",
|
|
2627
|
-
"getJob",
|
|
2628
|
-
"listJobs",
|
|
2629
|
-
"cancelJob",
|
|
2630
2465
|
"getCacheSnapshot",
|
|
2631
2466
|
"refreshCache"
|
|
2632
2467
|
];
|
|
@@ -2640,7 +2475,7 @@ const ARTIFACT_BRIDGE_CAPABILITY = {
|
|
|
2640
2475
|
function createCapabilities(atomicSave = false) {
|
|
2641
2476
|
return {
|
|
2642
2477
|
contractVersion: BACKEND_CONTRACT_VERSION,
|
|
2643
|
-
capabilitySchemaVersion:
|
|
2478
|
+
capabilitySchemaVersion: 12,
|
|
2644
2479
|
transactionKernelOwner: "@openfairygui/core",
|
|
2645
2480
|
appSeamOwner: "@openfairygui/functions",
|
|
2646
2481
|
runtimeOwner: "@openfairygui/backend",
|
|
@@ -2749,17 +2584,10 @@ function createCapabilities(atomicSave = false) {
|
|
|
2749
2584
|
retentionLimit: 1e3,
|
|
2750
2585
|
sequenceScope: "runtime"
|
|
2751
2586
|
},
|
|
2752
|
-
jobs: {
|
|
2753
|
-
inMemory: true,
|
|
2754
|
-
cooperativeCancel: true,
|
|
2755
|
-
persistent: false,
|
|
2756
|
-
supportedKinds: ["cache.refresh"],
|
|
2757
|
-
artifactJobs: false,
|
|
2758
|
-
completedRetentionLimit: 100
|
|
2759
|
-
},
|
|
2760
2587
|
cache: {
|
|
2761
2588
|
derivedReadOnly: true,
|
|
2762
|
-
keyedBy: "
|
|
2589
|
+
keyedBy: "sessionId",
|
|
2590
|
+
refreshMode: "synchronous",
|
|
2763
2591
|
sourceOfTruth: false,
|
|
2764
2592
|
refreshMethod: "refreshCache"
|
|
2765
2593
|
}
|
|
@@ -2771,19 +2599,15 @@ function createCapabilities(atomicSave = false) {
|
|
|
2771
2599
|
var BackendRuntime = class {
|
|
2772
2600
|
fileSystem;
|
|
2773
2601
|
capabilities;
|
|
2774
|
-
sessions =
|
|
2775
|
-
|
|
2776
|
-
eventsBySession = /* @__PURE__ */ new Map();
|
|
2777
|
-
jobsBySession = /* @__PURE__ */ new Map();
|
|
2778
|
-
cacheBySession = /* @__PURE__ */ new Map();
|
|
2779
|
-
eventSequence = 0;
|
|
2602
|
+
sessions = new SessionRegistry();
|
|
2603
|
+
sessionOperations = new SessionOperationQueue();
|
|
2780
2604
|
context;
|
|
2781
2605
|
readService;
|
|
2782
2606
|
runtimeService;
|
|
2783
2607
|
authoringService;
|
|
2608
|
+
persistenceService;
|
|
2784
2609
|
cacheService;
|
|
2785
2610
|
eventService;
|
|
2786
|
-
jobService;
|
|
2787
2611
|
constructor(options = {}) {
|
|
2788
2612
|
this.fileSystem = options.fileSystem;
|
|
2789
2613
|
this.capabilities = createCapabilities(Boolean(options.fileSystem?.runProjectWriteTransaction));
|
|
@@ -2792,22 +2616,15 @@ var BackendRuntime = class {
|
|
|
2792
2616
|
host: options.host,
|
|
2793
2617
|
allowedProjectRoots: options.allowedProjectRoots,
|
|
2794
2618
|
capabilities: this.capabilities,
|
|
2795
|
-
sessions: this.sessions
|
|
2796
|
-
sessionsByPath: this.sessionsByPath,
|
|
2797
|
-
eventsBySession: this.eventsBySession,
|
|
2798
|
-
jobsBySession: this.jobsBySession,
|
|
2799
|
-
cacheBySession: this.cacheBySession,
|
|
2800
|
-
nextEventSequence: () => {
|
|
2801
|
-
this.eventSequence += 1;
|
|
2802
|
-
return this.eventSequence;
|
|
2803
|
-
}
|
|
2619
|
+
sessions: this.sessions
|
|
2804
2620
|
};
|
|
2805
|
-
|
|
2806
|
-
this.
|
|
2807
|
-
this.
|
|
2808
|
-
this.
|
|
2809
|
-
this.runtimeService = new RuntimeService(this.context, this.cacheService, this.eventService
|
|
2810
|
-
this.authoringService = new AuthoringService(this.context, this.cacheService, this.eventService);
|
|
2621
|
+
const getSession = (sessionId) => this.sessions.get(sessionId);
|
|
2622
|
+
this.readService = new ReadService((sessionId) => readView(getSession(sessionId)), this.capabilities);
|
|
2623
|
+
this.eventService = new EventService(getSession);
|
|
2624
|
+
this.cacheService = new CacheService(getSession, this.eventService);
|
|
2625
|
+
this.runtimeService = new RuntimeService(this.context, this.cacheService, this.eventService);
|
|
2626
|
+
this.authoringService = new AuthoringService(this.context, this.cacheService, this.eventService, this.sessionOperations);
|
|
2627
|
+
this.persistenceService = new PersistenceService(this.context, this.cacheService, this.eventService, this.sessionOperations);
|
|
2811
2628
|
}
|
|
2812
2629
|
getCapabilities() {
|
|
2813
2630
|
return this.readService.getCapabilities();
|
|
@@ -2852,31 +2669,23 @@ var BackendRuntime = class {
|
|
|
2852
2669
|
return this.authoringService.applyTransaction(input);
|
|
2853
2670
|
}
|
|
2854
2671
|
async saveSession(input) {
|
|
2855
|
-
return this.
|
|
2672
|
+
return this.persistenceService.saveSession(input);
|
|
2856
2673
|
}
|
|
2857
2674
|
async materializeSession(input) {
|
|
2858
|
-
return this.
|
|
2675
|
+
return this.persistenceService.materializeSession(input);
|
|
2859
2676
|
}
|
|
2860
2677
|
async closeSession(input) {
|
|
2861
|
-
|
|
2678
|
+
const captured = { ...input };
|
|
2679
|
+
return this.sessionOperations.run(captured.sessionId, () => this.runtimeService.closeSession(captured));
|
|
2862
2680
|
}
|
|
2863
2681
|
getEvents(input) {
|
|
2864
2682
|
return this.eventService.getEvents(input);
|
|
2865
2683
|
}
|
|
2866
|
-
getJob(input) {
|
|
2867
|
-
return this.jobService.getJob(input);
|
|
2868
|
-
}
|
|
2869
|
-
listJobs(input) {
|
|
2870
|
-
return this.jobService.listJobs(input);
|
|
2871
|
-
}
|
|
2872
|
-
cancelJob(input) {
|
|
2873
|
-
return this.jobService.cancelJob(input);
|
|
2874
|
-
}
|
|
2875
2684
|
getCacheSnapshot(input) {
|
|
2876
2685
|
return this.cacheService.getCacheSnapshot(input);
|
|
2877
2686
|
}
|
|
2878
2687
|
refreshCache(input) {
|
|
2879
|
-
return this.
|
|
2688
|
+
return this.cacheService.refreshCache(input);
|
|
2880
2689
|
}
|
|
2881
2690
|
};
|
|
2882
2691
|
//#endregion
|
|
@@ -3059,6 +2868,7 @@ exports.BACKEND_ENTITY_QUERY_LIMITS = BACKEND_ENTITY_QUERY_LIMITS;
|
|
|
3059
2868
|
exports.BACKEND_SESSION_READ_LIMITS = BACKEND_SESSION_READ_LIMITS;
|
|
3060
2869
|
exports.BACKEND_TRANSACTION_PREVIEW_LIMITS = BACKEND_TRANSACTION_PREVIEW_LIMITS;
|
|
3061
2870
|
exports.BackendRuntime = BackendRuntime;
|
|
2871
|
+
exports.ProjectWriteTransactionError = ProjectWriteTransactionError;
|
|
3062
2872
|
exports.createBackendStorageFileSystem = createBackendStorageFileSystem;
|
|
3063
2873
|
exports.getBackendDiagnosticCatalog = getBackendDiagnosticCatalog;
|
|
3064
2874
|
exports.getBackendDiagnosticGuide = getBackendDiagnosticGuide;
|