fix(sessions): preserve cross-session input ownership and history (#6613)
* fix(webui): reconcile cross-session history by message ID * fix(sessions): preserve target authority and input ownership Resolve cross-session follow-ups through the target route, carry stable input IDs through admission and recovery, and associate live and replayed answers with the inputs actually consumed. Keep ordinary input scope isolation and omit historical anomaly repair. Resolves NAN-430. * fix(sessions): complete input identity integration across boundaries Carry input identity explicitly in projected input events, keep runner annotations out of public history, and reuse the canonical WebUI session parser during recovery. Type validated recovery metadata and align existing channel and subagent regressions with explicit response ownership. All nine failed compatibility cases and adjacent recovery coverage pass in a focused 58-test run. * fix(sessions): unify input recording and block remote commands * fix(webui): keep input ownership checks page-bounded Look up referenced inputs through the existing identity index before invoking transcript backfill. Complete pages no longer scan the full prompt history, including answers whose inputs live on earlier pages. Cover cold-cache pagination and cross-turn ownership in existing regressions. Focused pagination, replay and follow-up recovery checks: 88 passed. * fix(webui): preserve response ownership during history replay * fix: resolve strict typing in response replay * fix: preserve continuation ownership during recovery * test: diagnose stuck gateway asyncio shutdown * test: retain gateway thread stack diagnostics * test(webui): stabilize native autoscroll browser setup
This commit is contained in:
1 parent
0ce335bf12
commit
394a78e82d
51 files changed
+1412
-334
No files matched your search
@@ -134,7 +134,7 @@ describe("webui API helpers", () => {
|
||||
});
|
||||
|
||||
it("revalidates prompt summaries and replaces only the changed suffix", async () => {
|
||||
const prompt = { id: "u1", preview: "question", answerPreview: "", cursor: "cursor", turnId: "t1", createdAt: 1 };
|
||||
const prompt = { id: "u1", inputId: "user:t1", preview: "question", answerPreview: "", cursor: "cursor", turnId: "t1", createdAt: 1 };
|
||||
const cached = { revision: "first", prompts: [prompt] };
|
||||
const signal = new AbortController().signal;
|
||||
vi.mocked(fetch).mockResolvedValueOnce({ status: 304 } as Response);
|
||||
@@ -146,6 +146,7 @@ describe("webui API helpers", () => {
|
||||
}) } as Response);
|
||||
const appended = await fetchPromptIndex("tok", "websocket:chat-1", signal, cached);
|
||||
expect(appended.prompts.map((item) => item.id)).toEqual(["u1", "u2"]);
|
||||
expect(appended.prompts[1].inputId).toBe("user:t1");
|
||||
expect(appended.prompts[0]).toBe(prompt);
|
||||
vi.mocked(fetch).mockResolvedValueOnce({ ok: true, json: async () => ({ revision: "third", replaceFrom: 1, prompts: [] }) } as Response);
|
||||
expect((await fetchPromptIndex("tok", "websocket:chat-1", signal, appended)).prompts).toEqual([prompt]);
|
||||
@@ -259,7 +260,7 @@ describe("webui API helpers", () => {
|
||||
[{ provider: "xai", model: "grok", preset: "snapshot", fallback: true }],
|
||||
[{ provider: "xai", model: "grok", preset: "legacy snapshot" }],
|
||||
])("accepts optional recorded response sources: %j", async (response_sources) => {
|
||||
const event = { event: "stream_end", chat_id: "chat-1", text: "Reply", response_sources };
|
||||
const event = { event: "stream_end", chat_id: "chat-1", text: "Reply", response_sources, response_input_ids: ["session-message:received"] };
|
||||
vi.mocked(fetch).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
|
||||
@@ -167,14 +167,15 @@ describe("canonical thread event projection", () => {
|
||||
expect(messages[0]).toMatchObject({ content: "Complete answer", projectionStartSeq: 1, turnSeq: 3 });
|
||||
});
|
||||
|
||||
it.each(["message", "stream_end"] as const)("preserves recorded sources in %s replay", (event) => {
|
||||
it.each(["message", "stream_end"] as const)("preserves recorded response metadata in %s replay", (event) => {
|
||||
const source = { provider: "xai", model: "grok", preset: "saved backup", fallback: true };
|
||||
const messages = projectThreadEvents([
|
||||
{ event, chat_id: "chat", text: "Recovered", response_sources: [source] },
|
||||
{ event, chat_id: "chat", text: "Recovered", response_sources: [source], response_input_ids: ["session-message:received"] },
|
||||
{ event: "turn_end", chat_id: "chat" },
|
||||
{ event: "message", chat_id: "chat", text: "Legacy response" },
|
||||
]);
|
||||
expect(messages[0].responseSources).toEqual([source]);
|
||||
expect(messages[0].responseInputIds).toEqual(["session-message:received"]);
|
||||
expect(messages[1].responseSources).toBeUndefined();
|
||||
});
|
||||
|
||||
|
||||
@@ -41,6 +41,18 @@ function receive(thread: ThreadTranscript, ...events: ThreadProjectionEvent[]):
|
||||
}
|
||||
|
||||
describe("ThreadTranscript admission", () => {
|
||||
it("updates a represented live input's final image URL without duplicating it", () => {
|
||||
const thread = new ThreadTranscript();
|
||||
const first: ThreadProjectionEvent = { ...at("upload", 1), event: "user_message", text: "look", starts_turn: true,
|
||||
media_urls: [{ kind: "image", url: "data:image/png;base64,AAAA", name: "image.png" }] };
|
||||
receive(thread, first);
|
||||
receive(thread, { ...first, media_urls: [{ kind: "image", url: "/api/media/final", name: "image.png" }] });
|
||||
expect(thread.messages).toHaveLength(1);
|
||||
expect(thread.messages[0]).toMatchObject({ media: [{ url: "/api/media/final" }], images: [{ url: "/api/media/final" }] });
|
||||
const restored = new ThreadTranscript([], thread.save());
|
||||
expect(restored.messages[0].media?.[0].url).toBe("/api/media/final");
|
||||
});
|
||||
|
||||
it("keeps the history boundary unknown through live delivery and a failed first read", () => {
|
||||
const thread = new ThreadTranscript();
|
||||
const read = thread.begin("initial");
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { ReactNode } from "react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { useNanobotStream } from "@/hooks/useNanobotStream";
|
||||
import { userPromptAnchors } from "@/components/thread/promptNavigation";
|
||||
import { ThreadVisibilityContext } from "@/hooks/useThreadVisibility";
|
||||
import { normalizeActivityTimeline } from "@/lib/activity-timeline";
|
||||
import { composerUsageSnapshots } from "@/lib/model-request";
|
||||
@@ -2556,16 +2557,32 @@ describe("useNanobotStream", () => {
|
||||
expect(result.current.runStartedAt).toBe(1_700_000_000);
|
||||
});
|
||||
|
||||
it("projects a cross-session input with its public handle exactly once", () => {
|
||||
it("keeps an existing answer stream open when an ordinary input arrives", () => {
|
||||
const fake = fakeClient();
|
||||
const { result } = renderHook(() => useNanobotStream("chat-live-input", EMPTY_MESSAGES),
|
||||
{ wrapper: wrap(fake.client) });
|
||||
act(() => {
|
||||
fake.emit("chat-live-input", { event: "delta", chat_id: "chat-live-input", stream_id: "answer", text: "first" });
|
||||
fake.emit("chat-live-input", { event: "user_message", chat_id: "chat-live-input", turn_id: "queued", text: "next", starts_turn: false });
|
||||
fake.emit("chat-live-input", { event: "delta", chat_id: "chat-live-input", stream_id: "answer", text: " second" });
|
||||
fake.emit("chat-live-input", { event: "stream_end", chat_id: "chat-live-input", stream_id: "answer", resuming: true });
|
||||
});
|
||||
expect(result.current.messages.filter((row) => row.role === "assistant").map((row) => row.content)).toEqual(["first second"]);
|
||||
expect(result.current.messages.filter((row) => row.role === "user").map((row) => row.content)).toEqual(["next"]);
|
||||
});
|
||||
|
||||
it("projects a cross-session input once without interrupting the answer being streamed", () => {
|
||||
const fake = fakeClient();
|
||||
const original: UIMessage[] = [{ id: "original", inputId: "user:original", role: "user", content: "original", createdAt: 1 }];
|
||||
const { result } = renderHook(
|
||||
() => useNanobotStream("chat-target", EMPTY_MESSAGES),
|
||||
() => useNanobotStream("chat-target", original),
|
||||
{ wrapper: wrap(fake.client) },
|
||||
);
|
||||
const event: InboundEvent = {
|
||||
event: "user_message",
|
||||
chat_id: "chat-target",
|
||||
text: "Please review this.",
|
||||
input_id: "session-message:message-1",
|
||||
created_at_ms: 1_700_000_000_123,
|
||||
starts_turn: false,
|
||||
provenance: {
|
||||
@@ -2580,18 +2597,29 @@ describe("useNanobotStream", () => {
|
||||
};
|
||||
|
||||
act(() => {
|
||||
fake.emit("chat-target", { event: "delta", chat_id: "chat-target", text: "Original",
|
||||
stream_id: "original-answer", response_input_ids: ["user:original"] });
|
||||
fake.emit("chat-target", event);
|
||||
fake.emit("chat-target", event);
|
||||
fake.emit("chat-target", { event: "delta", chat_id: "chat-target", text: " answer",
|
||||
stream_id: "original-answer", response_input_ids: ["user:original"] });
|
||||
fake.emit("chat-target", { event: "stream_end", chat_id: "chat-target", stream_id: "original-answer",
|
||||
response_input_ids: ["user:original"], resuming: true });
|
||||
});
|
||||
|
||||
expect(result.current.messages).toEqual([expect.objectContaining({
|
||||
expect(userPromptAnchors(result.current.messages).map((prompt) => prompt.answerPreview)).toEqual(["Original answer", ""]);
|
||||
expect(result.current.messages.filter((row) => row.role === "assistant")).toHaveLength(1);
|
||||
expect(result.current.messages.filter((row) => row.sessionMessage)).toEqual([expect.objectContaining({
|
||||
id: "session-message:message-1",
|
||||
inputId: "session-message:message-1",
|
||||
role: "user",
|
||||
content: "Please review this.",
|
||||
createdAt: 1_700_000_000_123,
|
||||
sessionMessage: event.provenance?.session_message,
|
||||
})]);
|
||||
expect(result.current.isStreaming).toBe(true);
|
||||
act(() => fake.emit("chat-target", { event: "message", chat_id: "chat-target", text: "Review answer",
|
||||
response_input_ids: ["session-message:message-1"] }));
|
||||
expect(userPromptAnchors(result.current.messages).map((prompt) => prompt.answerPreview)).toEqual(["Original answer", "Review answer"]);
|
||||
});
|
||||
|
||||
it("marks only the optimistic turn named by a correlated rejection as failed", () => {
|
||||
|
||||
@@ -221,3 +221,31 @@ it("keeps the request operation with failures and retries", async () => {
|
||||
act(() => result.current.cancel());
|
||||
expect(result.current.operation).toBeNull();
|
||||
});
|
||||
|
||||
|
||||
it("associates live answers and merges the persisted directory by input ID across turns", async () => {
|
||||
vi.mocked(api.fetchPromptIndex).mockResolvedValue({ prompts: [], revision: "empty" });
|
||||
const users: UIMessage[] = [
|
||||
{ id: "local-original", inputId: "user:original", role: "user", content: "same", createdAt: 1 },
|
||||
{ id: "local-session", inputId: "session-message:received", role: "user", content: "same", createdAt: 2 },
|
||||
];
|
||||
const { result, rerender } = renderHook(({ messages, version }) => usePromptHistory("a", version, messages), {
|
||||
wrapper: wrapper(), initialProps: { messages: users, version: 1 },
|
||||
});
|
||||
await waitFor(() => expect(result.current.prompts).toHaveLength(2));
|
||||
const messages: UIMessage[] = [...users,
|
||||
{ id: "original-answer", role: "assistant", content: "Original answer", createdAt: 3, responseInputIds: ["user:original"] },
|
||||
];
|
||||
rerender({ messages, version: 1 });
|
||||
expect(result.current.prompts.map((prompt) => prompt.answerPreview)).toEqual(["Original answer", ""]);
|
||||
messages.push({ id: "session-answer", role: "assistant", content: "Session answer", createdAt: 4,
|
||||
responseInputIds: ["session-message:received"] });
|
||||
rerender({ messages: [...messages], version: 1 });
|
||||
expect(result.current.prompts.map((prompt) => prompt.answerPreview)).toEqual(["Original answer", "Session answer"]);
|
||||
const persisted = result.current.prompts.map((prompt, index) => ({ ...prompt, id: `persisted-${index}`,
|
||||
cursor: String(index), turnId: `execution-${index}` }));
|
||||
vi.mocked(api.fetchPromptIndex).mockResolvedValue({ prompts: persisted, revision: "persisted" });
|
||||
rerender({ messages, version: 2 });
|
||||
await waitFor(() => expect(result.current.prompts.map((prompt) => prompt.id)).toEqual(["persisted-0", "persisted-1"]));
|
||||
expect(result.current.prompts.map((prompt) => prompt.answerPreview)).toEqual(["Original answer", "Session answer"]);
|
||||
});
|
||||
Reference in new issue
Block a user