feat(gateway): inject timestamps into agent handler messages

Messages arriving through the gateway agent method (TUI, web, spawned
subagents, sessions_send, heartbeats) now get a timestamp prefix
automatically. This gives all agent contexts date/time awareness
without modifying the system prompt (which is cached for stability).

Channel messages (Discord, Telegram, etc.) already have timestamps
via envelope formatting in a separate code path and never reach
the agent handler, so there is no double-stamping risk.

Cron jobs also inject their own 'Current time:' prefix and are
detected and skipped.

Extracted as a pure function (injectTimestamp) with 12 unit tests
covering: timezone handling, 12/24h format, midnight boundaries,
envelope detection, cron detection, and empty messages.

Integration test verifies the agent handler wires it in correctly.

Closes #3658
Refs: #1897, #1928, #2108
This commit is contained in:
Conroy Whitney
2026-01-28 21:31:08 -05:00
committed by Tak Hoffman
parent 83e64c1ac9
commit 582a4e261a
4 changed files with 270 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ const mocks = vi.hoisted(() => ({
updateSessionStore: vi.fn(),
agentCommand: vi.fn(),
registerAgentRunContext: vi.fn(),
loadConfigReturn: {} as Record<string, unknown>,
}));
vi.mock("../session-utils.js", () => ({
@@ -32,7 +33,7 @@ vi.mock("../../commands/agent.js", () => ({
}));
vi.mock("../../config/config.js", () => ({
loadConfig: () => ({}),
loadConfig: () => mocks.loadConfigReturn,
}));
vi.mock("../../agents/agent-scope.js", () => ({
@@ -115,6 +116,62 @@ describe("gateway agent handler", () => {
expect(capturedEntry?.claudeCliSessionId).toBe(existingClaudeCliSessionId);
});
it("injects a timestamp into the message passed to agentCommand", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-01-29T01:30:00.000Z")); // Wed Jan 28, 8:30 PM EST
mocks.agentCommand.mockReset();
mocks.loadConfigReturn = {
agents: {
defaults: {
userTimezone: "America/New_York",
timeFormat: "12",
},
},
};
mocks.loadSessionEntry.mockReturnValue({
cfg: mocks.loadConfigReturn,
storePath: "/tmp/sessions.json",
entry: {
sessionId: "existing-session-id",
updatedAt: Date.now(),
},
canonicalKey: "agent:main:main",
});
mocks.updateSessionStore.mockResolvedValue(undefined);
mocks.agentCommand.mockResolvedValue({
payloads: [{ text: "ok" }],
meta: { durationMs: 100 },
});
const respond = vi.fn();
await agentHandlers.agent({
params: {
message: "Is it the weekend?",
agentId: "main",
sessionKey: "agent:main:main",
idempotencyKey: "test-timestamp-inject",
},
respond,
context: makeContext(),
req: { type: "req", id: "ts-1", method: "agent" },
client: null,
isWebchatConnect: () => false,
});
// Wait for the async agentCommand call
await vi.waitFor(() => expect(mocks.agentCommand).toHaveBeenCalled());
const callArgs = mocks.agentCommand.mock.calls[0][0];
expect(callArgs.message).toMatch(
/^\[.*Wednesday.*January 28.*2026.*8:30 PM.*\] Is it the weekend\?$/,
);
mocks.loadConfigReturn = {};
vi.useRealTimers();
});
it("handles missing cliSessionIds gracefully", async () => {
mocks.loadSessionEntry.mockReturnValue({
cfg: {},