Fix workspace policy loading

This commit is contained in:
2026-08-13 23:24:45 +02:00
parent 10edca5a09
commit 25b2835e73
8 changed files with 92 additions and 17 deletions
+38 -3
View File
@@ -107,9 +107,44 @@ test.each([
expect(posted).toBe(false);
});
test("createSession preserves allowLegacy for a selected session workspace absent from registry summaries", async () => {
test("createSession replaces a stale browser workspace with the current installation default", async () => {
localStorage.setItem("thothii.workspace-registry.v1.preferences", JSON.stringify({
workspaceId: "retained-session-workspace", provider: "zai", model: "glm-5.2", thinking: "low",
workspaceId: "retired-workspace", provider: "zai", model: "glm-5.2", thinking: "low",
}));
let body: unknown;
server.use(
http.get("/api/settings", () => HttpResponse.json({ workspace: "psd-clinical" })),
http.get("/api/workspaces", () => HttpResponse.json([{
...workspaceSummaryFixture("psd-clinical", {
displayName: "PSD Clinical",
revision: workspaceRevisionFixture("psd-clinical"),
}),
}])),
http.get("/api/workspaces/psd-clinical", () => HttpResponse.json({
workspace: canonicalWorkspaceFixture("psd-clinical"),
revision: {
id: "psd-clinical", commit: "a".repeat(40), blob: "b".repeat(40), snapshotPath: "/snapshot",
},
})),
http.post("/api/sessions", async ({ request }) => {
body = await request.json();
return HttpResponse.json({ id: "s1" });
}),
);
await expect(createSession({ question: "q" })).resolves.toEqual({ id: "s1" });
expect(body).toEqual({
question: "q", workspaceId: "psd-clinical",
provider: "zai", model: "glm-5.2", thinking: "low",
});
expect(JSON.parse(localStorage.getItem("thothii.workspace-registry.v1.preferences")!)).toEqual({
workspaceId: "psd-clinical", provider: "zai", model: "glm-5.2", thinking: "low",
});
});
test("createSession preserves a local legacy selection when the registry is empty", async () => {
localStorage.setItem("thothii.workspace-registry.v1.preferences", JSON.stringify({
workspaceId: "legacy-workspace", provider: "zai", model: "glm-5.2", thinking: "low",
}));
let body: unknown;
server.use(
@@ -122,7 +157,7 @@ test("createSession preserves allowLegacy for a selected session workspace absen
await expect(createSession({ question: "q" })).resolves.toEqual({ id: "s1" });
expect(body).toEqual({
question: "q", workspaceId: "retained-session-workspace",
question: "q", workspaceId: "legacy-workspace",
provider: "zai", model: "glm-5.2", thinking: "low",
});
});
+28 -7
View File
@@ -50,23 +50,44 @@ async function ensureWorkspaceSelectionPolicy(): Promise<WorkspacePreference> {
const workspaceId = preferences.workspaceId;
if (!workspaceId) return preferences;
workspacePolicyGate.beginSummary(workspaceId);
let workspace;
let workspaces: Awaited<ReturnType<typeof listWorkspaces>>;
try {
workspace = (await listWorkspaces()).find((candidate) => candidate.id === workspaceId);
workspaces = await listWorkspaces();
} catch {
if (workspacePreferences.load().workspaceId !== workspaceId) continue;
workspacePolicyGate.rejectSummary(workspaceId);
throw new WorkspaceSelectionError(WORKSPACE_SUMMARY_ERROR);
}
if (workspacePreferences.load().workspaceId !== workspaceId) continue;
if (workspace && !workspace.revision) {
const workspace = workspaces.find((candidate) => candidate.id === workspaceId);
if (!workspace) {
if (workspaces.length === 0) {
workspacePolicyGate.allowLegacy(workspaceId);
return workspacePreferences.load();
}
let installationDefault: string | undefined;
try {
installationDefault = (await getSettings()).workspace;
} catch {
if (workspacePreferences.load().workspaceId !== workspaceId) continue;
workspacePolicyGate.rejectSummary(workspaceId);
throw new WorkspaceSelectionError(WORKSPACE_SUMMARY_ERROR);
}
if (workspacePreferences.load().workspaceId !== workspaceId) continue;
const replacement = workspaces.find(
(candidate) => candidate.id === installationDefault && candidate.revision,
) ?? workspaces.find((candidate) => candidate.revision);
if (!replacement) {
workspacePolicyGate.reject(workspaceId);
throw new WorkspaceSelectionError(WORKSPACE_POLICY_ERROR);
}
workspacePreferences.save({ ...preferences, workspaceId: replacement.id });
continue;
}
if (!workspace.revision) {
workspacePolicyGate.reject(workspaceId);
throw new WorkspaceSelectionError(WORKSPACE_POLICY_ERROR);
}
if (!workspace?.revision) {
workspacePolicyGate.allowLegacy(workspaceId);
return workspacePreferences.load();
}
workspacePolicyGate.select(workspaceId);
const outcome = await Promise.race([
getWorkspace(workspaceId).then(
+19
View File
@@ -121,6 +121,25 @@ test("accepts the atomic schema-v3 workspace revision contract without historica
await expect(getWorkspace("psd-clinical")).resolves.toEqual({ workspace, revision });
});
test("accepts a Qdrant collection using the canonical hyphenated workspace name", async () => {
const hyphenatedCollection = {
...workspace,
semantic_index: {
...workspace.semantic_index,
vector_store: { ...workspace.semantic_index.vector_store, collection: "psd-clinical" },
},
} satisfies CanonicalWorkspace;
server.use(http.get("/api/workspaces/psd-clinical", () => HttpResponse.json({
workspace: hyphenatedCollection,
revision,
})));
await expect(getWorkspace("psd-clinical")).resolves.toEqual({
workspace: hyphenatedCollection,
revision,
});
});
test("sanitizes read and validate responses while preserving directory-based Evidence", async () => {
server.use(
http.get("/api/workspaces/psd-clinical", () => HttpResponse.json({ workspace: evidenceWorkspace, revision })),