fix: wait for workspace policy before creation
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { apiFetch } from "./client";
|
import { apiFetch } from "./client";
|
||||||
import { getSettings } from "./settings";
|
import { getSettings } from "./settings";
|
||||||
import { workspacePreferences, type WorkspacePreference } from "../workspaces/drafts";
|
import { workspacePolicyGate, workspacePreferences, type WorkspacePreference } from "../workspaces/drafts";
|
||||||
import type {
|
import type {
|
||||||
Principal, ResumeSessionResult, SessionScope, SessionSummary, SessionDocument, UiResponse,
|
Principal, ResumeSessionResult, SessionScope, SessionSummary, SessionDocument, UiResponse,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
@@ -24,9 +24,11 @@ async function selectedPreferences(): Promise<WorkspacePreference> {
|
|||||||
|
|
||||||
export async function createSession(i: NewSessionInput) {
|
export async function createSession(i: NewSessionInput) {
|
||||||
const preferences = await selectedPreferences();
|
const preferences = await selectedPreferences();
|
||||||
|
await workspacePolicyGate.waitFor(i.workspaceId ?? preferences.workspaceId);
|
||||||
|
const selection = workspacePreferences.load();
|
||||||
return apiFetch<{ id: string }>("/sessions", {
|
return apiFetch<{ id: string }>("/sessions", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({ ...preferences, ...i }),
|
body: JSON.stringify({ ...selection, ...i }),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,10 +21,12 @@ test("new sessions send the browser-selected workspace, model, provider, and thi
|
|||||||
localStorage.setItem("thothii.workspace-registry.v1.preferences", JSON.stringify({
|
localStorage.setItem("thothii.workspace-registry.v1.preferences", JSON.stringify({
|
||||||
workspaceId: "psd-clinical", provider: "zai", model: "glm-5.2", thinking: "high",
|
workspaceId: "psd-clinical", provider: "zai", model: "glm-5.2", thinking: "high",
|
||||||
}));
|
}));
|
||||||
server.use(http.post("http://localhost:8787/sessions", async ({ request }) => {
|
server.use(
|
||||||
body = await request.json();
|
http.post("http://localhost:8787/sessions", async ({ request }) => {
|
||||||
return HttpResponse.json({ id: "s1" });
|
body = await request.json();
|
||||||
}));
|
return HttpResponse.json({ id: "s1" });
|
||||||
|
}),
|
||||||
|
);
|
||||||
render(<SteerInput sessionId={null} />);
|
render(<SteerInput sessionId={null} />);
|
||||||
|
|
||||||
await userEvent.type(screen.getByRole("textbox", { name: /new question/i }), "q");
|
await userEvent.type(screen.getByRole("textbox", { name: /new question/i }), "q");
|
||||||
@@ -231,6 +233,62 @@ test("switching workspaces replaces an out-of-policy model before session creati
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("immediate submit waits for a switched workspace policy before creating a session", async () => {
|
||||||
|
let body: unknown;
|
||||||
|
let releasePolicy!: () => void;
|
||||||
|
let policyRequestStarted = false;
|
||||||
|
const policyMayFinish = new Promise<void>((resolve) => { releasePolicy = resolve; });
|
||||||
|
localStorage.setItem("thothii.workspace-registry.v1.preferences", JSON.stringify({
|
||||||
|
workspaceId: "research", provider: "deepseek", model: "deepseek-v4-pro", thinking: "medium",
|
||||||
|
}));
|
||||||
|
const revision = (id: string) => ({
|
||||||
|
id, commit: "a".repeat(40), blob: "b".repeat(40), snapshotPath: "/snapshot", state: "operational" as const,
|
||||||
|
});
|
||||||
|
server.use(
|
||||||
|
http.get("http://localhost:8787/settings", () => HttpResponse.json({ workspace: "research" })),
|
||||||
|
http.get("http://localhost:8787/workspaces", () => HttpResponse.json([
|
||||||
|
{ id: "research", name: "research", file: "research.yaml", displayName: "Research", revision: revision("research") },
|
||||||
|
{ id: "psd-clinical", name: "psd-clinical", file: "psd-clinical.yaml", displayName: "PSD Clinical", revision: revision("psd-clinical") },
|
||||||
|
])),
|
||||||
|
http.get("http://localhost:8787/workspaces/research", () => HttpResponse.json({
|
||||||
|
workspace: { llm_policy: { allowed: ["deepseek/deepseek-v4-pro"] } }, revision: revision("research"),
|
||||||
|
})),
|
||||||
|
http.get("http://localhost:8787/workspaces/psd-clinical", async () => {
|
||||||
|
policyRequestStarted = true;
|
||||||
|
await policyMayFinish;
|
||||||
|
return HttpResponse.json({
|
||||||
|
workspace: { llm_policy: { default: "zai/glm-5.2", allowed: ["zai/glm-5.2"] } },
|
||||||
|
revision: revision("psd-clinical"),
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
http.get("http://localhost:8787/models", () => HttpResponse.json({ models: [
|
||||||
|
{ provider: "zai", id: "glm-5.2", name: "GLM-5.2", reasoning: true },
|
||||||
|
{ provider: "deepseek", id: "deepseek-v4-pro", name: "DeepSeek V4 Pro", reasoning: true },
|
||||||
|
] })),
|
||||||
|
http.post("http://localhost:8787/sessions", async ({ request }) => {
|
||||||
|
body = await request.json();
|
||||||
|
return HttpResponse.json({ id: "s1" });
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||||
|
render(<QueryClientProvider client={client}><ComposerFooter /><SteerInput sessionId={null} /></QueryClientProvider>);
|
||||||
|
|
||||||
|
const workspaceSelector = await screen.findByRole("combobox", { name: "Workspace" });
|
||||||
|
await waitFor(() => expect(workspaceSelector).toHaveTextContent("psd-clinical"));
|
||||||
|
await userEvent.selectOptions(workspaceSelector, "psd-clinical");
|
||||||
|
await waitFor(() => expect(policyRequestStarted).toBe(true));
|
||||||
|
await userEvent.type(screen.getByRole("textbox", { name: /new question/i }), "q");
|
||||||
|
await userEvent.click(screen.getByRole("button", { name: /send/i }));
|
||||||
|
|
||||||
|
expect(body).toBeUndefined();
|
||||||
|
expect(screen.getByRole("button", { name: /send/i })).toBeDisabled();
|
||||||
|
releasePolicy();
|
||||||
|
|
||||||
|
await waitFor(() => expect(body).toEqual({
|
||||||
|
question: "q", workspaceId: "psd-clinical", provider: "zai", model: "glm-5.2", thinking: "medium",
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
test("pulses the stop dot only while the harness is working", () => {
|
test("pulses the stop dot only while the harness is working", () => {
|
||||||
const { rerender } = render(<SteerInput sessionId="s1" working />);
|
const { rerender } = render(<SteerInput sessionId="s1" working />);
|
||||||
const dot = () =>
|
const dot = () =>
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { getSettings } from "../api/settings";
|
|||||||
import { getWorkspace, listWorkspaces } from "../api/workspaces";
|
import { getWorkspace, listWorkspaces } from "../api/workspaces";
|
||||||
import { listModels } from "../api/models";
|
import { listModels } from "../api/models";
|
||||||
import { useSessionStore } from "../store/sessionStore";
|
import { useSessionStore } from "../store/sessionStore";
|
||||||
import { workspacePreferences, type WorkspacePreference } from "../workspaces/drafts";
|
import { workspacePolicyGate, workspacePreferences, type WorkspacePreference } from "../workspaces/drafts";
|
||||||
|
|
||||||
const THINKING_LEVELS = ["low", "medium", "high"] as const;
|
const THINKING_LEVELS = ["low", "medium", "high"] as const;
|
||||||
|
|
||||||
@@ -84,6 +84,7 @@ export function SteerInput({
|
|||||||
setLastUserEntry({ kind: "input", text: trimmed });
|
setLastUserEntry({ kind: "input", text: trimmed });
|
||||||
onSessionCreating?.(trimmed);
|
onSessionCreating?.(trimmed);
|
||||||
try {
|
try {
|
||||||
|
await workspacePolicyGate.waitFor(workspacePreferences.load().workspaceId);
|
||||||
const { id } = await createSession({ question: trimmed, ...workspacePreferences.load() });
|
const { id } = await createSession({ question: trimmed, ...workspacePreferences.load() });
|
||||||
onSessionCreated?.(id);
|
onSessionCreated?.(id);
|
||||||
setText("");
|
setText("");
|
||||||
@@ -174,7 +175,7 @@ export function ComposerFooter() {
|
|||||||
|
|
||||||
const workspace = preferences.workspaceId ?? settings?.workspace ?? "";
|
const workspace = preferences.workspaceId ?? settings?.workspace ?? "";
|
||||||
const selectedWorkspace = workspaces.find((candidate) => candidate.id === workspace);
|
const selectedWorkspace = workspaces.find((candidate) => candidate.id === workspace);
|
||||||
const { data: workspaceRecord } = useQuery({
|
const { data: workspaceRecord, isError: workspacePolicyError } = useQuery({
|
||||||
queryKey: ["workspace", workspace],
|
queryKey: ["workspace", workspace],
|
||||||
queryFn: () => getWorkspace(workspace),
|
queryFn: () => getWorkspace(workspace),
|
||||||
// Legacy metadata responses do not carry a registry revision, so retain the
|
// Legacy metadata responses do not carry a registry revision, so retain the
|
||||||
@@ -210,7 +211,18 @@ export function ComposerFooter() {
|
|||||||
setPreferences(next);
|
setPreferences(next);
|
||||||
}, [allowedModels, preferences, workspaceRecord]);
|
}, [allowedModels, preferences, workspaceRecord]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!selectedWorkspace?.revision) return;
|
||||||
|
if (workspaceRecord) workspacePolicyGate.resolve(workspace);
|
||||||
|
else if (workspacePolicyError) workspacePolicyGate.reject(workspace);
|
||||||
|
}, [selectedWorkspace, workspace, workspacePolicyError, workspaceRecord]);
|
||||||
|
|
||||||
function update(patch: WorkspacePreference) {
|
function update(patch: WorkspacePreference) {
|
||||||
|
if (
|
||||||
|
patch.workspaceId
|
||||||
|
&& patch.workspaceId !== workspace
|
||||||
|
&& workspaces.some((candidate) => candidate.id === patch.workspaceId && candidate.revision)
|
||||||
|
) workspacePolicyGate.begin(patch.workspaceId);
|
||||||
const next = workspacePreferences.save({ ...preferences, ...patch });
|
const next = workspacePreferences.save({ ...preferences, ...patch });
|
||||||
setPreferences(next);
|
setPreferences(next);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,44 @@ export interface WorkspaceDraft {
|
|||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let pendingPolicy: { workspaceId: string; promise: Promise<void>; resolve: () => void } | undefined;
|
||||||
|
let failedPolicy: { workspaceId: string; error: Error } | undefined;
|
||||||
|
|
||||||
|
/** In-memory handoff between the footer's policy query and all session entry points. */
|
||||||
|
export const workspacePolicyGate = {
|
||||||
|
begin(workspaceId: string): void {
|
||||||
|
if (pendingPolicy?.workspaceId === workspaceId) return;
|
||||||
|
if (failedPolicy?.workspaceId === workspaceId) failedPolicy = undefined;
|
||||||
|
let resolve!: () => void;
|
||||||
|
const promise = new Promise<void>((onResolve) => {
|
||||||
|
resolve = onResolve;
|
||||||
|
});
|
||||||
|
pendingPolicy = { workspaceId, promise, resolve };
|
||||||
|
},
|
||||||
|
|
||||||
|
resolve(workspaceId: string): void {
|
||||||
|
if (pendingPolicy?.workspaceId === workspaceId) {
|
||||||
|
pendingPolicy.resolve();
|
||||||
|
pendingPolicy = undefined;
|
||||||
|
}
|
||||||
|
if (failedPolicy?.workspaceId === workspaceId) failedPolicy = undefined;
|
||||||
|
},
|
||||||
|
|
||||||
|
reject(workspaceId: string): void {
|
||||||
|
if (pendingPolicy?.workspaceId !== workspaceId) return;
|
||||||
|
pendingPolicy.resolve();
|
||||||
|
pendingPolicy = undefined;
|
||||||
|
failedPolicy = { workspaceId, error: new Error("Selected workspace policy could not be loaded.") };
|
||||||
|
},
|
||||||
|
|
||||||
|
waitFor(workspaceId: string | undefined): Promise<void> {
|
||||||
|
const failed = failedPolicy;
|
||||||
|
if (failed && failed.workspaceId === workspaceId) return Promise.reject(failed.error);
|
||||||
|
const pending = pendingPolicy;
|
||||||
|
return pending && pending.workspaceId === workspaceId ? pending.promise : Promise.resolve();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const PREFIX = "thothii.workspace-registry.v1";
|
const PREFIX = "thothii.workspace-registry.v1";
|
||||||
const DRAFT_PREFIX = `${PREFIX}.draft.`;
|
const DRAFT_PREFIX = `${PREFIX}.draft.`;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user