feat: add scoped history and artifacts
Generic Container CI/CD / test-build-publish (push) Failing after 8m25s
Agent CI/CD / build-test-publish-and-deploy (push) Failing after 8m25s

This commit is contained in:
2026-10-22 18:40:20 +08:00
parent bc4e9834e5
commit 7d47a2df86
48 changed files with 2632 additions and 78 deletions
+52 -1
View File
@@ -24,6 +24,7 @@ let aborts: number;
let gate: Promise<void> | undefined;
let release: () => void = () => {};
let bridge: ChatSessionBridge;
let states: SessionUiStateStore;
const project = "10000000-0000-4000-8000-000000000001";
beforeEach(async () => {
fail = false; prompted = []; pendingInputs = []; aborts = 0; gate = undefined;
@@ -34,7 +35,7 @@ beforeEach(async () => {
})) as typeof fetch;
const workspaces = new SessionWorkspaceStore(join(root, "workspaces"));
const sessions = new SessionMetadataStore(join(root, "metadata"), workspaces);
const states = new SessionUiStateStore(join(root, "ui"), workspaces);
states = new SessionUiStateStore(join(root, "ui"), workspaces);
await sessions.initialize();
const context = { actorKey: toActorKey("product:owner"), projectKey: toProjectKey(`product:${project}`), projectId: project, userId: "owner", sessionId: "owned" };
await sessions.ensure(context);
@@ -44,6 +45,12 @@ beforeEach(async () => {
messages: async () => [], abortSession: async () => { aborts++; }, waitForSessionIdle: async () => {},
listPendingUserInputs: async () => pendingInputs.map((input) => ({ ...input })),
cancelPendingUserInputs: async () => { pendingInputs = []; },
cancelPendingUserInput: async (_sessionId: string, id: string) => {
const item = pendingInputs.find((input) => input.id === id);
if (!item) throw Object.assign(new Error("Consumed"), { status: 409 });
pendingInputs = pendingInputs.filter((input) => input.id !== id);
return { ...item, status: "cancelled" };
},
enqueueUserInput: async (_sessionId: string, input: ResponseInput) => { pendingInputs.push({ ...input }); },
updatePendingUserInput: async (_sessionId: string, id: string, delivery: "steer" | "queue") => {
const item = pendingInputs.find((input) => input.id === id);
@@ -144,6 +151,31 @@ it("rejects cross-user conversations, legacy requests and retired URLs before ge
expect((await call({ input: "核查压力" }, "owner", "/api/v1/agent/chat/stream")).status).toBe(404);
expect(prompted).toEqual([]);
});
it("withdraws only owned pending input without stopping generation or persisting a sent message", async () => {
gate = new Promise<void>((resolve) => { release = resolve; });
const initial = await call({ input: "原始问题", conversation: "owned", stream: true });
const id = `msg_${"3".repeat(32)}`;
await call({ input: "撤回后重新编辑", conversation: "owned", tjwater: { delivery: "queue", input_id: id } });
const headers = { Authorization: "Bearer owner", "X-Project-Id": project };
const url = `${base}/api/v1/session/owned/inputs/${id}`;
const baselineAborts = aborts;
expect((await http(url, { method: "DELETE", headers: { ...headers, Authorization: "Bearer other" } })).status).toBe(404);
expect(pendingInputs).toHaveLength(1);
const withdrawn = await http(url, { method: "DELETE", headers });
expect(withdrawn.status).toBe(200);
expect(await withdrawn.json()).toMatchObject({ id, text: "撤回后重新编辑", delivery: "queue", status: "cancelled" });
expect(pendingInputs).toHaveLength(0);
expect((await http(url, { method: "DELETE", headers })).status).toBe(409);
expect(aborts).toBe(baselineAborts);
release();
const output = await initial.text();
expect(output).toContain('"status":"cancelled"');
expect(output).toContain("event: response.completed");
const history = await http(`${base}/api/v1/session/owned`, { headers });
const messages = (await history.json()).messages as Array<{ role: string }>;
expect(JSON.stringify(messages.filter((message) => message.role === "user"))).not.toContain("撤回后重新编辑");
});
it("returns response.failed when the runtime fails", async () => {
fail = true;
const response = await call({ input: "核查压力", conversation: "owned", stream: true });
@@ -183,3 +215,22 @@ it("marks an explicitly aborted response incomplete and preserves scoped history
expect(text).toContain("event: response.incomplete");
expect(text).not.toContain("event: response.completed");
});
it("continues a paged conversation from the complete saved history and preserves client message IDs", async () => {
const context = { actorKey: toActorKey("product:owner"), projectKey: toProjectKey(`product:${project}`), sessionId: "owned" };
const history = Array.from({ length: 105 }, (_, index) => ({ id: `old-${index}`, role: index % 2 ? "assistant" : "user",
parts: [{ type: "text", text: `历史证据 ${index}` }] }));
await states.write(context, { sessionId: "owned", isTitleManuallyEdited: true, messages: history });
const headers = { Authorization: "Bearer owner", "X-Project-Id": project };
const first = await http(`${base}/api/v1/session/owned?message_limit=40`, { headers });
expect((await first.json()).messages).toHaveLength(40);
const response = await call({ input: "继续核查", conversation: "owned", tjwater: { client_message_id: "client-turn-a" } });
expect(response.status).toBe(200);
await response.json();
const saved = (await states.read(context))!.messages as Array<{ id: string }>;
expect(saved.slice(0, 105)).toEqual(history);
expect(saved).toHaveLength(107);
expect(saved[105]?.id).toBe("client-turn-a");
expect((await call({ input: "重复提交", conversation: "owned", tjwater: { client_message_id: "client-turn-a" } })).status).toBe(409);
expect(prompted).toHaveLength(1);
});