fix: keep numeric session thread ids in sessions list

This commit is contained in:
Tak Hoffman 2026-03-27 20:08:58 -05:00
parent f0d5d7a33a
commit 1b16a112e7
No known key found for this signature in database
3 changed files with 76 additions and 3 deletions

View File

@ -44,7 +44,7 @@ export type SessionListDeliveryContext = {
channel?: string;
to?: string;
accountId?: string;
threadId?: string;
threadId?: string | number;
};
export type SessionRunStatus = "running" | "done" | "failed" | "killed" | "timeout";

View File

@ -74,6 +74,75 @@ describe("sessions-list-tool", () => {
});
});
it("keeps numeric deliveryContext.threadId in sessions_list results", async () => {
const gatewayCallMock = vi.fn(async (opts: unknown) => {
const request = opts as { method?: string };
if (request.method === "sessions.list") {
return {
path: "/tmp/sessions.json",
sessions: [
{
key: "agent:main:telegram:group:-100123:topic:99",
kind: "group",
sessionId: "sess-telegram-topic",
deliveryContext: {
channel: "telegram",
to: "-100123",
accountId: "acct-1",
threadId: 99,
},
},
],
};
}
return {};
});
vi.doMock("../../gateway/call.js", () => ({
callGateway: gatewayCallMock,
}));
vi.doMock("./sessions-helpers.js", async () => {
const actual =
await vi.importActual<typeof import("./sessions-helpers.js")>("./sessions-helpers.js");
return {
...actual,
createAgentToAgentPolicy: () => ({}),
createSessionVisibilityGuard: async () => ({
check: () => ({ allowed: true }),
}),
resolveEffectiveSessionToolsVisibility: () => "all",
resolveSandboxedSessionToolContext: () => ({
mainKey: "main",
alias: "main",
requesterInternalKey: undefined,
restrictToSpawned: false,
}),
};
});
const { createSessionsListTool } = await import("./sessions-list-tool.js");
const tool = createSessionsListTool({ config: {} as never });
const result = await tool.execute("call-2", {});
const details = result.details as {
sessions?: Array<{
deliveryContext?: {
channel?: string;
to?: string;
accountId?: string;
threadId?: string | number;
};
}>;
};
expect(details.sessions?.[0]?.deliveryContext).toEqual({
channel: "telegram",
to: "-100123",
accountId: "acct-1",
threadId: 99,
});
});
it("keeps live session setting metadata in sessions_list results", async () => {
const gatewayCallMock = vi.fn(async (opts: unknown) => {
const request = opts as { method?: string };
@ -123,7 +192,7 @@ describe("sessions-list-tool", () => {
const { createSessionsListTool } = await import("./sessions-list-tool.js");
const tool = createSessionsListTool({ config: {} as never });
const result = await tool.execute("call-2", {});
const result = await tool.execute("call-3", {});
const details = result.details as {
sessions?: Array<{
thinkingLevel?: string;

View File

@ -153,7 +153,11 @@ export function createSessionsListTool(opts?: {
const deliveryAccountId =
typeof deliveryContext?.accountId === "string" ? deliveryContext.accountId : undefined;
const deliveryThreadId =
typeof deliveryContext?.threadId === "string" ? deliveryContext.threadId : undefined;
typeof deliveryContext?.threadId === "string" ||
(typeof deliveryContext?.threadId === "number" &&
Number.isFinite(deliveryContext.threadId))
? deliveryContext.threadId
: undefined;
const lastChannel =
deliveryChannel ??
(typeof entry.lastChannel === "string" ? entry.lastChannel : undefined);