Q12: read_messages reads threads, pages back, and searches

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 03:59:00 -04:00
1 parent 22632a4695
commit f15e47eda1
4 files changed
+138 -29

No files matched your search

+1 -1
View File
@@ -153,6 +153,6 @@ describe("agent direct messages", () => {
dispatchAgentTool(deps, h.ctx, call("send_message", { to: "Dana", body: "Morning - priorities for today are in the doc." }));
const readBack = dispatchAgentTool(deps, h.ctx, call("read_messages", { with: "Dana" }));
expect(readBack).toContain("priorities for today");
expect(readBack).toContain("[CEO]");
expect(readBack).toMatch(/\] CEO \(thread=/);
});
});
+60
View File
@@ -144,3 +144,63 @@ describe("mention resolution", () => {
expect(dispatchAgentTool(deps, danaCtx, call("read_messages", {}))).toContain("status on the rollback");
});
});
describe("read_messages history access", () => {
test("reads one thread, pages back with before, and searches with query", () => {
const connection = new DatabaseConnection(join(tmpdir(), `ac-history-${randomUUID()}.db`));
const organization = new OrganizationService(connection.db);
let orgId = "";
let ceoId = "";
connection.transaction(() => {
orgId = organization.createOrganization({ organizationName: "HistCorp", employeeName: "Yang", mission: "m", goal: "g" });
const providerId = (connection.db.prepare("select id from providers where org_id = ? and provider_key = 'openai'").get(orgId) as { id: string }).id;
ceoId = organization.createAgent({
orgId, name: "CEO", role: "CEO", department: "Executive",
modelProviderId: providerId, modelId: "gpt-5.4", budgetCents: 25000, managerAgentId: null,
});
});
const deps = { db: connection.db, organization, chat: new ChatService(connection.db) };
const ctx: AgentToolContext = {
orgId, agent: { id: ceoId, modelProviderId: null, modelId: null },
taskProjectId: null, taskGoalId: null, taskId: null, sourceThreadId: null, workspace: null,
};
const first = dispatchAgentTool(deps, ctx, call("send_message", { channel: "general", body: "Decision 0: use Postgres" }));
const threadId = /thread_id="([^"]+)"/.exec(first)![1];
for (let index = 1; index <= 14; index += 1) {
dispatchAgentTool(deps, ctx, call("send_message", { channel: "general", body: `Decision ${index}`, thread_id: threadId }));
}
dispatchAgentTool(deps, ctx, call("send_message", { channel: "general", body: "Unrelated chatter" }));
connection.db.prepare("update chat_messages set created_at = '2026-01-01T00:00:' || printf('%02d', rowid) || '.000Z'").run();
const page = dispatchAgentTool(deps, ctx, call("read_messages", { thread_id: threadId, limit: 5 }));
expect(page).not.toContain("Unrelated chatter");
expect(page).toContain("Decision 14");
expect(page).not.toContain("Decision 9");
const before = /before="([^"]+)"/.exec(page)![1];
const older = dispatchAgentTool(deps, ctx, call("read_messages", { thread_id: threadId, limit: 5, before }));
expect(older).toContain("Decision 9");
expect(older).not.toContain("Decision 10");
const found = dispatchAgentTool(deps, ctx, call("read_messages", { channel: "general", query: "postgres" }));
expect(found).toContain("Decision 0: use Postgres");
expect(found).not.toContain("Decision 1");
expect(dispatchAgentTool(deps, ctx, call("read_messages", { thread_id: threadId, channel: "general" }))).toContain('Pass only one of');
for (const bad of ["yesterday", "2026-02-30T00:00:00.000Z", "2026-01-01 00:00:00", "2026-01-01T00:00:00+08:00"]) {
expect(dispatchAgentTool(deps, ctx, call("read_messages", { channel: "general", before: bad }))).toContain('"before" must be a message_id from a previous page or a UTC ISO timestamp');
}
expect(dispatchAgentTool(deps, ctx, call("read_messages", { thread_id: threadId, before: "2026-01-01T00:00:03Z" }))).toContain("Decision 1");
// Messages sharing one timestamp still page through completely.
connection.db.prepare("update chat_messages set created_at = '2026-01-01T00:00:00.000Z' where thread_id = ?").run(threadId);
const seen = new Set<string>();
let cursor: string | undefined;
for (let pageIndex = 0; pageIndex < 5; pageIndex += 1) {
const result = dispatchAgentTool(deps, ctx, call("read_messages", { thread_id: threadId, limit: 4, ...(cursor ? { before: cursor } : {}) }));
for (const match of result.matchAll(/: (Decision \d+)/g)) seen.add(match[1].split(":")[0]);
cursor = /before="([^"]+)"/.exec(result)?.[1];
if (!cursor) break;
}
expect(seen.size).toBe(15);
connection.close();
});
});