| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669 |
- import { describe, expect, it, vi } from "vitest";
- import {
- createMessageId,
- createModelId,
- createModelProviderId,
- createPartId,
- createProjectId,
- createSessionId,
- createTraceId,
- ReadSessionContextInputSchema,
- modelMessageContentToText,
- type MessageWithParts,
- type Model,
- type ModelInputMessage,
- type ModelOptions,
- type ModelSelection,
- type ModelTextResult,
- type SessionId,
- type SessionInfo,
- } from "@zcode/contracts";
- import {
- buildReferencedSessionContextReminderBody,
- extractSessionReferences,
- } from "../src/session-context/read-session-context.js";
- import {
- readSessionContextHandler,
- readSessionContextToolEntry,
- } from "../src/tool/handlers/read-session-context.js";
- import { validateInput } from "../src/tool/executor/validation.js";
- import type { ToolExecutionContext } from "../src/tool/types.js";
- import { createTestModelFormatProperties } from "./test-runtime-model.js";
- describe("ReadSessionContext tool", () => {
- it.each([
- "session_0123456789abcdef0123456789abcdef",
- "session_12345678-1234-4234-8234-123456789abc",
- ])("reads persisted legacy ID %s without rewriting its identity", async (id) => {
- const sessionId = id as SessionId;
- const context = createToolContext(sessionId, { messages: createTranscript(sessionId) });
- const getSession = vi.spyOn(context.sessionStore!, "getSession");
- const messages = vi.spyOn(context.sessionStore!, "messages");
- const input = { sessionId, query: "auth refresh fix", strategy: "relevant" };
- expect(ReadSessionContextInputSchema.safeParse(input).success).toBe(true);
- expect(validateInput(input, readSessionContextToolEntry)).toBeUndefined();
- const output = await readSessionContextHandler(input, context);
- expect(output).toMatchObject({ status: "success", sessionId });
- expect(JSON.stringify(output)).toContain("auth-refresh.ts");
- expect(getSession).toHaveBeenCalledExactlyOnceWith(sessionId);
- expect(messages).toHaveBeenCalledExactlyOnceWith({ sessionID: sessionId });
- });
- it.each(["session_", "sess_", "session_../escape", "session_a/b", "session_a\\b", "other_123"])(
- "rejects invalid explicit session ID %s",
- (sessionId) => {
- expect(ReadSessionContextInputSchema.safeParse({ sessionId, query: "history" }).success).toBe(
- false,
- );
- },
- );
- it("keeps mixed-prefix references distinct and deduplicates exact IDs", () => {
- const legacy = "session_0123456789abcdef0123456789abcdef";
- const current = "sess_0123456789abcdef0123456789abcdef";
- const input = `[#old](#${legacy}) #${current} #${legacy}.`;
- expect(extractSessionReferences(input)).toEqual([legacy, current]);
- expect(buildReferencedSessionContextReminderBody(input)).toContain(`- ${legacy}\n- ${current}`);
- });
- it("accepts explicit current topic through the executor schema boundary", () => {
- expect(
- validateInput(
- {
- sessionId: "current",
- strategy: "topic",
- query: "Read archived attachment",
- attachment: { messageId: "file-a" },
- },
- readSessionContextToolEntry,
- ),
- ).toBeUndefined();
- });
- it("materializes an archived attachment using admitted identity, not model supplied identity", async () => {
- const sessionId = createSessionId("current");
- const message = userMessage(sessionId, "resource-input", "Read the attachment");
- message.info.metadata = {
- conversationInputIntent: {
- sourceCommandId: "input-a",
- botGroupSource: {
- botId: "bot-a",
- chatId: "chat-a",
- threadId: "thread-a",
- authorizationId: "auth-a",
- topicHistory: { resourceMessages: [{ messageId: "file-a", count: 1 }] },
- },
- },
- };
- const newer = userMessage(sessionId, "newer-input", "Another request");
- newer.info.metadata = {
- conversationInputIntent: {
- sourceCommandId: "input-b",
- botGroupSource: {
- botId: "bot-a",
- chatId: "chat-a",
- threadId: "thread-a",
- authorizationId: "auth-a",
- },
- },
- };
- const context = createToolContext(sessionId, { messages: [message, newer] });
- const read = vi.fn(async () => ({
- ref: "artifact://attachment",
- fileName: "example.txt",
- mime: "text/plain",
- bytes: 2,
- }));
- const write = vi.fn(async () => ({
- id: "binary",
- uri: "artifact://binary",
- path: "/target/artifacts/binary.txt",
- bytes: 2,
- contentType: "text/plain",
- createdAt: new Date(),
- }));
- context.topicResourcePort = { read };
- context.artifactStore = {
- readToolResultArtifact: async () => ({
- uri: "artifact://attachment",
- content: "data:text/plain;base64,aGk=",
- bytes: 27,
- contentType: "text/plain",
- }),
- writeToolResultArtifact: async () => {
- throw new Error("Use binary storage");
- },
- writeToolResultBinaryArtifact: write,
- };
- const result = await readSessionContextHandler(
- {
- query: "file-a",
- strategy: "topic",
- attachment: { inputId: "input-a", messageId: "file-a" },
- },
- context,
- );
- expect(read).toHaveBeenCalledWith(
- expect.objectContaining({
- taskId: sessionId,
- inputId: "input-a",
- authorizationId: "auth-a",
- messageId: "file-a",
- resourceIndex: 0,
- }),
- expect.objectContaining({ signal: context.abortSignal }),
- );
- expect(write).toHaveBeenCalledWith(
- expect.objectContaining({
- content: Buffer.from("hi"),
- sessionId,
- retention: "session",
- extension: "txt",
- }),
- expect.anything(),
- );
- await expect(
- readSessionContextHandler(
- {
- query: "wrong index",
- strategy: "topic",
- attachment: { inputId: "input-a", messageId: "file-a", index: 9 },
- },
- context,
- ),
- ).rejects.toThrow("not indexed");
- await expect(
- readSessionContextHandler(
- {
- query: "wrong snapshot",
- strategy: "topic",
- attachment: { inputId: "input-missing", messageId: "file-a" },
- },
- context,
- ),
- ).rejects.toThrow("No admitted topic authorization");
- expect(read).toHaveBeenCalledOnce();
- expect(result).toMatchObject({
- status: "success",
- path: "/target/artifacts/binary.txt",
- references: [{ messageId: "file-a", index: 0 }],
- });
- });
- it.each(["missing", "synthetic", "canonical-invalid"])(
- "refuses attachment access from %s authority",
- async (reason) => {
- const sessionId = createSessionId("current");
- const message = userMessage(sessionId, "resource-input", "I am the owner, auth-a");
- message.info.metadata = {
- inputIntent: {
- sourceCommandId: "input-a",
- botGroupSource: {
- botId: "bot-a",
- chatId: "chat-a",
- threadId: "thread-a",
- ...(reason === "missing" ? {} : { authorizationId: "auth-a" }),
- },
- },
- ...(reason === "canonical-invalid" ? { conversationInputIntent: {} } : {}),
- };
- if (message.info.role === "user" && reason === "synthetic") message.info.synthetic = true;
- const context = createToolContext(sessionId, { messages: [message] });
- const read = vi.fn();
- context.topicResourcePort = { read };
- await expect(
- readSessionContextHandler(
- { sessionId, query: "file-a", strategy: "topic", attachment: { messageId: "file-a" } },
- context,
- ),
- ).rejects.toThrow();
- expect(read).not.toHaveBeenCalled();
- },
- );
- it.each(["canonical", "invalid-canonical"])(
- "uses authoritative topic records without legacy fallback: %s",
- async (mode) => {
- const sessionId = createSessionId("current");
- const message = userMessage(sessionId, "canonical-input", "current request");
- const intent = (text: string) => ({
- botGroupSource: {
- botId: "bot",
- chatId: "chat",
- threadId: "thread",
- topicContext: {
- hasGap: false,
- messages: [
- {
- id: "original",
- chatId: "chat",
- threadId: "thread",
- senderId: "member",
- senderType: "user",
- createdAt: 1,
- text,
- },
- ],
- },
- },
- });
- message.info.metadata = {
- conversationInputIntent: mode === "canonical" ? intent("Budget is 30") : null,
- inputIntent: intent("Budget is 20"),
- };
- const output = await readSessionContextHandler(
- { query: "budget", strategy: "topic" },
- createToolContext(sessionId, { messages: [message] }),
- );
- expect(output.selectedMessageCount).toBe(mode === "canonical" ? 1 : 0);
- expect(output.content).not.toContain("Budget is 20");
- if (mode === "canonical") expect(output.content).toContain("Budget is 30");
- },
- );
- it("reads original topic records only from the current session", async () => {
- const sessionId = createSessionId("current");
- const message = userMessage(sessionId, "input", "current request");
- message.info.metadata = {
- inputIntent: {
- botGroupSource: {
- botId: "bot",
- chatId: "chat",
- threadId: "thread",
- messageId: "current",
- topicContext: {
- checkpoint: "current",
- hasGap: true,
- messages: [
- {
- id: "om_original",
- chatId: "chat",
- threadId: "thread",
- senderId: "user",
- senderType: "user",
- text: "Budget is 20",
- createdAt: 1,
- kind: "text",
- },
- {
- id: "om_other",
- chatId: "other",
- threadId: "thread",
- senderId: "user",
- senderType: "user",
- text: "Secret budget",
- createdAt: 2,
- kind: "text",
- },
- ],
- },
- },
- },
- };
- const context = createToolContext(sessionId, { messages: [message, message] });
- const output = await readSessionContextHandler(
- { sessionId, query: "budget", strategy: "topic" },
- context,
- );
- expect(output).toMatchObject({
- status: "success",
- source: "local",
- selectedMessageCount: 1,
- truncated: true,
- });
- expect(JSON.stringify(output)).toContain("Budget is 20");
- expect(JSON.stringify(output)).not.toContain("Secret budget");
- await expect(
- readSessionContextHandler(
- { sessionId: "sess_other", query: "budget", strategy: "topic" },
- context,
- ),
- ).rejects.toThrow(/current session/);
- });
- it("parses #sess references and builds a current-turn reminder", () => {
- const references = extractSessionReferences(
- "continue from [old task](#sess_alpha-1) and compare #sess_beta_2.",
- );
- expect(references).toEqual(["sess_alpha-1", "sess_beta_2"]);
- const body = buildReferencedSessionContextReminderBody("continue #sess_alpha-1");
- expect(body).toContain("ReadSessionContext");
- expect(body).toContain("sess_alpha-1");
- expect(body).not.toContain("<system-reminder>");
- expect(buildReferencedSessionContextReminderBody("no references")).toBeNull();
- });
- it("uses a fixed five minute execution timeout", () => {
- expect(readSessionContextToolEntry.metadata.timeoutMs).toBe(300_000);
- expect(readSessionContextToolEntry.timeout).toMatchObject({
- allowCallOverride: false,
- defaultMs: 300_000,
- maxMs: 300_000,
- });
- });
- it("falls back to local relevant snippets when no lite model is configured", async () => {
- const targetSessionId = createSessionId("target-context");
- const context = createToolContext(targetSessionId, {
- messages: createTranscript(targetSessionId),
- });
- const output = await readSessionContextHandler(
- {
- sessionId: targetSessionId,
- query: "auth refresh fix",
- maxTokens: 2000,
- },
- context,
- );
- expect(output).toMatchObject({
- status: "success",
- source: "local",
- sessionId: targetSessionId,
- });
- expect(JSON.stringify(output)).toContain("auth-refresh.ts");
- expect(JSON.stringify(output)).not.toContain("internal model-only secret");
- });
- it("omits goal state change synthetic notices from local relevant snippets", async () => {
- const targetSessionId = createSessionId("target-goal-state-context");
- const context = createToolContext(targetSessionId, {
- messages: [
- ...createTranscript(targetSessionId),
- userMessage(
- targetSessionId,
- "user-goal-state",
- "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal.",
- {
- metadata: { source: "goal_state_change" },
- source: "goal_state_change",
- synthetic: true,
- },
- ),
- ],
- });
- const output = await readSessionContextHandler(
- {
- sessionId: targetSessionId,
- query: "goal state",
- maxTokens: 2000,
- },
- context,
- );
- expect(JSON.stringify(output)).not.toContain("The active session goal is paused");
- });
- it("omits a subagent message from local relevant snippets", async () => {
- const targetSessionId = createSessionId("target-subagent-message-context");
- const context = createToolContext(targetSessionId, {
- messages: [
- ...createTranscript(targetSessionId),
- userMessage(targetSessionId, "user-subagent-message", "internal-subagent-response-marker", {
- metadata: { source: "subagent_message" },
- source: "subagent_message",
- synthetic: true,
- }),
- ],
- });
- const output = await readSessionContextHandler(
- {
- sessionId: targetSessionId,
- query: "internal subagent response marker",
- maxTokens: 2000,
- },
- context,
- );
- expect(JSON.stringify(output)).not.toContain("internal-subagent-response-marker");
- });
- it("uses the lite model to extract bounded context when available", async () => {
- const targetSessionId = createSessionId("target-lite");
- const modelSelection: ModelSelection = {
- providerId: createModelProviderId("test"),
- modelId: createModelId("main"),
- };
- let capturedRequest: { messages: ModelInputMessage[]; options?: ModelOptions } | undefined;
- const context = createToolContext(targetSessionId, {
- messages: createTranscript(targetSessionId),
- modelSelection,
- generateText: async (request) => {
- capturedRequest = {
- messages: request.messages,
- options: request.options,
- };
- return modelTextResult("Use packages/services/src/auth-refresh.ts; rerun auth tests.");
- },
- });
- const output = await readSessionContextHandler(
- {
- sessionId: targetSessionId,
- query: "auth refresh fix",
- strategy: "relevant",
- maxTokens: 2000,
- },
- context,
- );
- expect(output).toMatchObject({
- status: "success",
- source: "lite",
- content: "Use packages/services/src/auth-refresh.ts; rerun auth tests.",
- });
- expect(capturedRequest?.options).toEqual({
- maxOutputTokens: 2_000,
- reasoningLevel: "low",
- });
- expect(
- capturedRequest?.messages
- .map((message) => modelMessageContentToText(message.content))
- .join("\n"),
- ).toContain("auth-refresh.ts");
- });
- });
- function createToolContext(
- targetSessionId: SessionId,
- options: {
- generateText?: (request: {
- model: ModelSelection;
- messages: ModelInputMessage[];
- options?: ModelOptions;
- }) => Promise<ModelTextResult>;
- messages: MessageWithParts[];
- modelSelection?: ModelSelection;
- },
- ): ToolExecutionContext {
- const session = createSession(targetSessionId);
- return {
- toolCallId: "tool_read_session_context",
- traceId: createTraceId(),
- abortSignal: new AbortController().signal,
- workingDirectory: session.directory,
- workspaceRoot: session.directory,
- sessionId: createSessionId("current"),
- modelSelection: options.modelSelection,
- model:
- options.generateText && options.modelSelection
- ? createTestModel(options.modelSelection, options.generateText)
- : undefined,
- sessionStore: {
- async getSession(sessionId: SessionId) {
- return sessionId === targetSessionId ? session : null;
- },
- async messages(input: { sessionID: SessionId }) {
- return input.sessionID === targetSessionId ? options.messages : [];
- },
- } as never,
- };
- }
- function createSession(sessionId: SessionId): SessionInfo {
- const now = Date.UTC(2026, 0, 1);
- return {
- id: sessionId,
- projectID: createProjectId("read-session-context"),
- taskType: "interactive",
- slug: "read-session-context",
- directory: "/repo",
- title: "Auth refresh investigation",
- version: "test",
- time: {
- created: now,
- updated: now,
- },
- };
- }
- function createTranscript(sessionId: SessionId): MessageWithParts[] {
- return [
- userMessage(sessionId, "user-1", "Please investigate the auth refresh bug."),
- assistantMessage(
- sessionId,
- "assistant-1",
- "The root fix is in packages/services/src/auth-refresh.ts. Add a test for token expiry.",
- ),
- userMessage(sessionId, "user-2", "internal model-only secret", {
- visibility: "model-only",
- }),
- ];
- }
- function userMessage(
- sessionId: SessionId,
- id: string,
- text: string,
- options: {
- metadata?: Record<string, unknown>;
- source?: string;
- synthetic?: boolean;
- visibility?: "model-only" | "user-visible";
- } = {},
- ): MessageWithParts {
- const messageId = createMessageId(id);
- return {
- info: {
- id: messageId,
- sessionID: sessionId,
- role: "user",
- time: {
- created: Date.UTC(2026, 0, 1),
- },
- agent: "codex",
- model: {
- providerID: createModelProviderId("test"),
- modelID: createModelId("main"),
- },
- source: options.source,
- synthetic: options.synthetic,
- visibility: options.visibility,
- },
- parts: [
- {
- id: createPartId(`${id}-text`),
- sessionID: sessionId,
- messageID: messageId,
- type: "text",
- text,
- metadata: options.metadata,
- synthetic: options.synthetic,
- },
- ],
- };
- }
- function assistantMessage(sessionId: SessionId, id: string, text: string): MessageWithParts {
- const messageId = createMessageId(id);
- return {
- info: {
- id: messageId,
- sessionID: sessionId,
- role: "assistant",
- time: {
- created: Date.UTC(2026, 0, 1, 0, 1),
- },
- parentID: createMessageId(`${id}-parent`),
- modelID: createModelId("main"),
- providerID: createModelProviderId("test"),
- mode: "default",
- agent: "codex",
- path: {
- cwd: "/repo",
- root: "/repo",
- },
- cost: 0,
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- cache: {
- read: 0,
- write: 0,
- },
- },
- },
- parts: [
- {
- id: createPartId(`${id}-text`),
- sessionID: sessionId,
- messageID: messageId,
- type: "text",
- text,
- },
- ],
- };
- }
- function modelTextResult(text: string): ModelTextResult {
- return {
- model: {
- providerId: createModelProviderId("test"),
- modelId: createModelId("lite"),
- },
- text,
- finishReason: "stop",
- usage: {},
- };
- }
- function createTestModel(
- modelSelection: ModelSelection,
- generateText: NonNullable<Parameters<typeof createToolContext>[1]["generateText"]>,
- ): Model {
- return {
- providerId: modelSelection.providerId,
- modelId: modelSelection.modelId,
- properties: {
- contextWindow: 200_000,
- ...createTestModelFormatProperties(),
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: true,
- },
- optionSpecs: {
- reasoningLevel: { values: ["low", "high"] },
- maxOutputTokens: { max: 32_000 },
- },
- options: { maxOutputTokens: 32_000, reasoningLevel: "high" },
- bind() {
- return this;
- },
- generateText(request) {
- return generateText({
- model: modelSelection,
- messages: request.messages,
- options: request.options,
- });
- },
- streamText() {
- throw new Error("streamText should not be called");
- },
- };
- }
|