Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/TitleGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,23 @@ export class TitleGenerator {
ephemeral: true,
});

const turnResult = await this.client.runTurn({
threadId: epThread.id,
input: [{
type: "text",
text: `${SYSTEM_PROMPT}\n\nUser's first message:\n${titleSource(userPromptText)}`,
text_elements: [],
}],
outputSchema: TITLE_OUTPUT_SCHEMA,
model: TITLE_MODEL,
});
let turnResult;
try {
turnResult = await this.client.runTurn({
threadId: epThread.id,
input: [{
type: "text",
text: `${SYSTEM_PROMPT}\n\nUser's first message:\n${titleSource(userPromptText)}`,
text_elements: [],
}],
outputSchema: TITLE_OUTPUT_SCHEMA,
model: TITLE_MODEL,
});
} finally {
// Codex keeps a subscribed thread loaded, with its MCP servers, and session/close
// releases only the main thread.
await this.client.threadUnsubscribe({threadId: epThread.id}).catch(() => {});
}

const title = extractTitle(turnResult.turn);
if (!title) return;
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/CodexACPAgent/session-fork-prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ describe("a prompt after session/fork", () => {
expect(forkId).toBe("fork-id");
await expect(promptWithin(fixture, forkId)).resolves.toMatchObject({stopReason: "end_turn"});
await expect(promptWithin(fixture, sourceId)).resolves.toMatchObject({stopReason: "end_turn"});
expect(requestsOf(fixture.current(), "thread/unsubscribe")).toEqual([]);
// Only the ephemeral title thread may be released; the fork and its source stay subscribed.
const unsubscribed = requestsOf(fixture.current(), "thread/unsubscribe") as {threadId: string}[];
expect(unsubscribed.filter(({threadId}) => threadId !== "title-thread")).toEqual([]);
expect(subscribed).toContain(forkId);
expect(subscribed).toContain(sourceId);
fixture.agent.sessionIndex.dispose();
Expand Down
61 changes: 61 additions & 0 deletions src/__tests__/TitleGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe("TitleGenerator.waitForIdle", () => {
threadStart: vi.fn().mockResolvedValue({thread: {id: "ephemeral"}}),
runTurn: vi.fn().mockReturnValue(turn.promise),
threadSetName,
threadUnsubscribe: vi.fn().mockResolvedValue({}),
} as unknown as Partial<CodexAppServerClient>);

generator.onTurnCompleted("hello");
Expand Down Expand Up @@ -51,6 +52,7 @@ describe("TitleGenerator.waitForIdle", () => {
threadStart: vi.fn().mockResolvedValue({thread: {id: "ephemeral"}}),
runTurn: vi.fn().mockReturnValue(new Promise(() => {})),
threadSetName: vi.fn(),
threadUnsubscribe: vi.fn().mockResolvedValue({}),
} as unknown as Partial<CodexAppServerClient>);

generator.onTurnCompleted("hello");
Expand All @@ -63,6 +65,7 @@ describe("TitleGenerator.waitForIdle", () => {
threadStart: vi.fn().mockRejectedValue(new Error("no ephemeral threads")),
runTurn: vi.fn(),
threadSetName: vi.fn(),
threadUnsubscribe: vi.fn().mockResolvedValue({}),
} as unknown as Partial<CodexAppServerClient>);

generator.onTurnCompleted("hello");
Expand All @@ -77,6 +80,7 @@ describe("TitleGenerator prompt", () => {
const generator = createGenerator({
threadStart: vi.fn().mockResolvedValue({thread: {id: "ephemeral"}}),
runTurn,
threadUnsubscribe: vi.fn().mockResolvedValue({}),
} as unknown as Partial<CodexAppServerClient>);

// The cut falls between the two halves of the emoji.
Expand All @@ -87,3 +91,60 @@ describe("TitleGenerator prompt", () => {
expect(text.endsWith(`User's first message:\n${"a".repeat(3_999)}`)).toBe(true);
});
});

describe("TitleGenerator ephemeral thread", () => {
const titleTurn = {turn: {items: [{type: "agentMessage", text: '{"title":"A short title"}'}]}};

function createClient(overrides: Record<string, unknown> = {}) {
return {
threadStart: vi.fn().mockResolvedValue({thread: {id: "ephemeral"}}),
runTurn: vi.fn().mockResolvedValue(titleTurn),
threadSetName: vi.fn().mockResolvedValue({}),
threadUnsubscribe: vi.fn().mockResolvedValue({}),
...overrides,
};
}

async function generate(client: ReturnType<typeof createClient>) {
const generator = createGenerator(client as unknown as Partial<CodexAppServerClient>);
generator.onTurnCompleted("hello");
// The rename echo never arrives in these tests; the thread must be released before that wait.
await generator.waitForIdle(20);
}

it("unsubscribes the ephemeral thread after the title turn", async () => {
const client = createClient();

await generate(client);

expect(client.threadUnsubscribe).toHaveBeenCalledTimes(1);
expect(client.threadUnsubscribe).toHaveBeenCalledWith({threadId: "ephemeral"});
expect(client.threadUnsubscribe.mock.invocationCallOrder[0])
.toBeLessThan(client.threadSetName.mock.invocationCallOrder[0]!);
});

it("unsubscribes the ephemeral thread when the title turn fails", async () => {
const client = createClient({runTurn: vi.fn().mockRejectedValue(new Error("model rejected"))});

await generate(client);

expect(client.threadUnsubscribe).toHaveBeenCalledWith({threadId: "ephemeral"});
expect(client.threadSetName).not.toHaveBeenCalled();
});

it("still writes the title when the unsubscribe fails", async () => {
const client = createClient({threadUnsubscribe: vi.fn().mockRejectedValue(new Error("not loaded"))});

await generate(client);

expect(client.threadSetName).toHaveBeenCalledWith({threadId: "thread-id", name: "A short title"});
});

it("does not unsubscribe when no ephemeral thread was started", async () => {
const client = createClient({threadStart: vi.fn().mockRejectedValue(new Error("no ephemeral threads"))});

await generate(client);

expect(client.threadUnsubscribe).not.toHaveBeenCalled();
});
});