feat: add "Current Date:" label to timestamp prefix

Changes [Wed 2026-01-28 20:30 EST] to [Current Date: Wed 2026-01-28 20:30 EST].

Tested with qwen3-1.7B: even with DOW in the timestamp, the model
ignored it and tried to compute the day using Zeller's Congruence.
The "Current Date:" semantic label is widely present in training data
and gives small models the best chance of recognizing the timestamp
as authoritative context rather than metadata to parse.

Cost: ~18 tokens per message. Prevents hallucination spirals that
burn hundreds or thousands of tokens on date derivation.
This commit is contained in:
Conroy Whitney
2026-01-28 22:28:48 -05:00
committed by Tak Hoffman
parent a6c68e8690
commit b6c8c1e89d
3 changed files with 15 additions and 14 deletions

View File

@@ -18,7 +18,7 @@ describe("injectTimestamp", () => {
timezone: "America/New_York",
});
expect(result).toMatch(/^\[Wed 2026-01-28 20:30 EST\] Is it the weekend\?$/);
expect(result).toMatch(/^\[Current Date: Wed 2026-01-28 20:30 EST\] Is it the weekend\?$/);
});
it("uses channel envelope format with DOW prefix", () => {
@@ -28,7 +28,7 @@ describe("injectTimestamp", () => {
const result = injectTimestamp("hello", { timezone: "America/New_York" });
// DOW prefix + formatZonedTimestamp format
expect(result).toBe(`[Wed ${expected}] hello`);
expect(result).toBe(`[Current Date: Wed ${expected}] hello`);
});
it("always uses 24-hour format", () => {
@@ -43,14 +43,14 @@ describe("injectTimestamp", () => {
const result = injectTimestamp("hello", { timezone: "America/Chicago" });
// 8:30 PM EST = 7:30 PM CST = 19:30
expect(result).toMatch(/^\[Wed 2026-01-28 19:30 CST\]/);
expect(result).toMatch(/^\[Current Date: Wed 2026-01-28 19:30 CST\]/);
});
it("defaults to UTC when no timezone specified", () => {
const result = injectTimestamp("hello", {});
// 2026-01-29T01:30:00Z
expect(result).toMatch(/^\[Thu 2026-01-29 01:30/);
expect(result).toMatch(/^\[Current Date: Thu 2026-01-29 01:30/);
});
it("returns empty/whitespace messages unchanged", () => {
@@ -66,7 +66,7 @@ describe("injectTimestamp", () => {
});
it("does NOT double-stamp messages already injected by us", () => {
const alreadyStamped = "[Wed 2026-01-28 20:30 EST] hello there";
const alreadyStamped = "[Current Date: Wed 2026-01-28 20:30 EST] hello there";
const result = injectTimestamp(alreadyStamped, { timezone: "America/New_York" });
expect(result).toBe(alreadyStamped);
@@ -85,7 +85,7 @@ describe("injectTimestamp", () => {
const result = injectTimestamp("hello", { timezone: "America/New_York" });
expect(result).toMatch(/^\[Sun 2026-02-01 00:00 EST\]/);
expect(result).toMatch(/^\[Current Date: Sun 2026-02-01 00:00 EST\]/);
});
it("handles date boundaries (just before midnight)", () => {
@@ -93,19 +93,19 @@ describe("injectTimestamp", () => {
const result = injectTimestamp("hello", { timezone: "America/New_York" });
expect(result).toMatch(/^\[Sat 2026-01-31 23:59 EST\]/);
expect(result).toMatch(/^\[Current Date: Sat 2026-01-31 23:59 EST\]/);
});
it("handles DST correctly (same UTC hour, different local time)", () => {
// EST (winter): UTC-5 → 2026-01-15T05:00Z = midnight Jan 15
vi.setSystemTime(new Date("2026-01-15T05:00:00.000Z"));
const winter = injectTimestamp("winter", { timezone: "America/New_York" });
expect(winter).toMatch(/^\[Thu 2026-01-15 00:00 EST\]/);
expect(winter).toMatch(/^\[Current Date: Thu 2026-01-15 00:00 EST\]/);
// EDT (summer): UTC-4 → 2026-07-15T04:00Z = midnight Jul 15
vi.setSystemTime(new Date("2026-07-15T04:00:00.000Z"));
const summer = injectTimestamp("summer", { timezone: "America/New_York" });
expect(summer).toMatch(/^\[Wed 2026-07-15 00:00 EDT\]/);
expect(summer).toMatch(/^\[Current Date: Wed 2026-07-15 00:00 EDT\]/);
});
it("accepts a custom now date", () => {
@@ -116,7 +116,7 @@ describe("injectTimestamp", () => {
now: customDate,
});
expect(result).toMatch(/^\[Fri 2025-07-04 12:00 EDT\]/);
expect(result).toMatch(/^\[Current Date: Fri 2025-07-04 12:00 EDT\]/);
});
});

View File

@@ -53,13 +53,14 @@ export function injectTimestamp(message: string, opts?: TimestampInjectionOption
const formatted = formatZonedTimestamp(now, timezone);
if (!formatted) return message;
// Add 3-letter day-of-week for smaller models that can't derive DOW
// from a date. Costs ~1 token, cheap insurance.
// "Current Date:" label is unambiguous even for tiny models (1.7B+).
// 3-letter DOW included because small models can't derive it from a date.
// Total cost: ~18 tokens — saves thousands when it prevents hallucination.
const dow = new Intl.DateTimeFormat("en-US", { timeZone: timezone, weekday: "short" }).format(
now,
);
return `[${dow} ${formatted}] ${message}`;
return `[Current Date: ${dow} ${formatted}] ${message}`;
}
/**

View File

@@ -163,7 +163,7 @@ describe("gateway agent handler", () => {
await vi.waitFor(() => expect(mocks.agentCommand).toHaveBeenCalled());
const callArgs = mocks.agentCommand.mock.calls[0][0];
expect(callArgs.message).toBe("[Wed 2026-01-28 20:30 EST] Is it the weekend?");
expect(callArgs.message).toBe("[Current Date: Wed 2026-01-28 20:30 EST] Is it the weekend?");
mocks.loadConfigReturn = {};
vi.useRealTimers();