Improve historical context tense handling

This commit is contained in:
Meta Vens 2026-05-02 20:57:10 +01:00
commit f90376708b
5 changed files with 145 additions and 7 deletions

View file

@ -347,6 +347,7 @@ export function formatHistoryResults(
authorId: result.metadata.authorId,
authorName: result.metadata.authorName,
timestamp: result.metadata.timestamp,
timeContext: formatTimeContext(result.metadata.timestamp),
jumpUrl: result.metadata.jumpUrl,
threadId: result.metadata.threadId,
}));
@ -378,6 +379,7 @@ export function formatArchivedMessageContext(
id: message.id,
isAnchor: message.id === anchorMessageId,
timestamp: message.timestamp,
timeContext: formatTimeContext(message.timestamp),
authorId: message.authorId,
authorName: message.authorName,
channelId: message.channelId,
@ -566,6 +568,51 @@ function normalizeForMatching(value: string): string {
.trim();
}
function formatTimeContext(timestamp: string | undefined): string | undefined {
if (!timestamp) {
return undefined;
}
const parsed = new Date(timestamp);
if (Number.isNaN(parsed.getTime())) {
return undefined;
}
const absolute = formatUtcTimestamp(timestamp);
const now = new Date();
const deltaMs = now.getTime() - parsed.getTime();
const absDeltaMs = Math.abs(deltaMs);
const suffix = deltaMs >= 0 ? "ago" : "from now";
if (absDeltaMs < 60_000) {
return `${absolute} (just now)`;
}
const minuteMs = 60_000;
const hourMs = 60 * minuteMs;
const dayMs = 24 * hourMs;
const yearMs = 365 * dayMs;
if (absDeltaMs < hourMs) {
const minutes = Math.round(absDeltaMs / minuteMs);
return `${absolute} (${minutes} minute${minutes === 1 ? "" : "s"} ${suffix})`;
}
if (absDeltaMs < dayMs) {
const hours = Math.round(absDeltaMs / hourMs);
return `${absolute} (${hours} hour${hours === 1 ? "" : "s"} ${suffix})`;
}
if (absDeltaMs < yearMs) {
const days = Math.round(absDeltaMs / dayMs);
return `${absolute} (${days} day${days === 1 ? "" : "s"} ${suffix})`;
}
const years = Math.round(absDeltaMs / yearMs);
return `${absolute} (${years} year${years === 1 ? "" : "s"} ${suffix})`;
}
function isAmbientVoidReference(
message: Message,
botUserId: string | undefined,

View file

@ -573,6 +573,7 @@ function formatHistoryResults(
authorId: result.metadata.authorId,
authorName: result.metadata.authorName,
timestamp: result.metadata.timestamp,
timeContext: formatTimeContext(result.metadata.timestamp),
jumpUrl: result.metadata.jumpUrl,
threadId: result.metadata.threadId,
}));
@ -604,6 +605,7 @@ function formatArchivedMessageContext(
id: message.id,
isAnchor: message.id === anchorMessageId,
timestamp: message.timestamp,
timeContext: formatTimeContext(message.timestamp),
authorId: message.authorId,
authorName: message.authorName,
channelId: message.channelId,
@ -614,3 +616,58 @@ function formatArchivedMessageContext(
editedAt: message.editedAt,
};
}
function formatTimeContext(timestamp: string | undefined): string | undefined {
if (!timestamp) {
return undefined;
}
const parsed = new Date(timestamp);
if (Number.isNaN(parsed.getTime())) {
return undefined;
}
const absolute = formatUtcTimestamp(timestamp);
const now = new Date();
const deltaMs = now.getTime() - parsed.getTime();
const absDeltaMs = Math.abs(deltaMs);
const suffix = deltaMs >= 0 ? "ago" : "from now";
if (absDeltaMs < 60_000) {
return `${absolute} (just now)`;
}
const minuteMs = 60_000;
const hourMs = 60 * minuteMs;
const dayMs = 24 * hourMs;
const yearMs = 365 * dayMs;
if (absDeltaMs < hourMs) {
const minutes = Math.round(absDeltaMs / minuteMs);
return `${absolute} (${minutes} minute${minutes === 1 ? "" : "s"} ${suffix})`;
}
if (absDeltaMs < dayMs) {
const hours = Math.round(absDeltaMs / hourMs);
return `${absolute} (${hours} hour${hours === 1 ? "" : "s"} ${suffix})`;
}
if (absDeltaMs < yearMs) {
const days = Math.round(absDeltaMs / dayMs);
return `${absolute} (${days} day${days === 1 ? "" : "s"} ${suffix})`;
}
const years = Math.round(absDeltaMs / yearMs);
return `${absolute} (${years} year${years === 1 ? "" : "s"} ${suffix})`;
}
function formatUtcTimestamp(timestamp: string): string {
const date = new Date(timestamp);
const year = date.getUTCFullYear();
const month = `${date.getUTCMonth() + 1}`.padStart(2, "0");
const day = `${date.getUTCDate()}`.padStart(2, "0");
const hours = `${date.getUTCHours()}`.padStart(2, "0");
const minutes = `${date.getUTCMinutes()}`.padStart(2, "0");
return `${year}-${month}-${day} ${hours}:${minutes} UTC`;
}

View file

@ -34,6 +34,8 @@ export function buildSystemPrompt(context: ContextBundle): string {
"If the user is asking about a real incident, personal history, prior server drama, something somebody said here, or a historical event discussed in Discord, start with search_history.",
"Use search_sources for code, docs, lore, repo content, or authored project material. Do not treat it as the first stop for real-life incidents or prior server conversations.",
"If a history search gives you echoes of the current question or other repeated ask-lines, ignore those and look for earlier substantive messages, links, or fetch surrounding context with get_message_context.",
"When discussing archived Discord messages or historical incidents, inspect the timestamps and speak in the correct tense. Do not describe old events as if they are happening right now. Prefer explicit dates or time markers when they matter.",
"Do not guess anyone's pronouns from a name alone. If explicit pronouns were not provided in the attached context, prefer the person's name or neutral phrasing.",
"If you need to target a specific indexed repo and do not know the valid repo names yet, call list_indexed_repos before search_sources.",
renderSourceGroundingInstructions(context, false),
"Do not claim to have performed searches or tool calls beyond the material actually executed in this run.",
@ -58,8 +60,18 @@ export function buildPrompt(context: ContextBundle): string {
const retrievedContext = context.retrieval.length
? context.retrieval
.map(
(result) =>
`- score=${result.score.toFixed(2)} source=${result.sourceId} kind=${result.sourceKind} text=${result.text}`,
(result) => {
const timeContext =
result.sourceKind === "discord_message" && result.metadata.timestamp
? ` time=${result.metadata.timestamp}`
: "";
const authorName =
result.sourceKind === "discord_message" && result.metadata.authorName
? ` author=${result.metadata.authorName}`
: "";
return `- score=${result.score.toFixed(2)} source=${result.sourceId} kind=${result.sourceKind}${authorName}${timeContext} text=${result.text}`;
},
)
.join("\n")
: "- No archived retrieval snippets were attached.";

View file

@ -40,7 +40,18 @@ export function buildDiscordReplyPrompt(
const retrieval = context.retrieval.length
? context.retrieval
.map((result) => `- (${result.score.toFixed(2)}) ${result.text}`)
.map((result) => {
const timeContext =
result.sourceKind === "discord_message" && result.metadata.timestamp
? ` [${result.metadata.timestamp}]`
: "";
const authorName =
result.sourceKind === "discord_message" && result.metadata.authorName
? ` ${result.metadata.authorName}:`
: "";
return `- (${result.score.toFixed(2)})${timeContext}${authorName} ${result.text}`.trimEnd();
})
.join("\n")
: "- No retrieval results attached.";
const stylePackInstructions =
@ -90,6 +101,8 @@ export function buildDiscordReplyPrompt(
"- You may inspect the workspace and use safe read-only commands if needed.",
"- For questions about Discord history, prior discussion, or user preferences, use search_history and get_message_context instead of filesystem inspection.",
"- If a history search gives you echoes of the current question or other repeated ask-lines, ignore those and look for earlier substantive messages, links, or fetch surrounding context with get_message_context.",
"- When discussing archived Discord messages or historical incidents, inspect timestamps and use the correct tense. Do not narrate old events as if they are unfolding right now. Use explicit dates or time markers when they matter.",
"- Do not guess anyone's pronouns from a name alone. If explicit pronouns were not provided in the attached context, prefer the person's name or neutral phrasing.",
"- For questions about indexed repos, source trees, repo-local docs, or indexed lore collections, use search_sources and get_source_context before broad workspace scans.",
"- If you want to narrow source search to a specific repo but do not know the valid repo names yet, call list_indexed_repos first.",
sourceGroundingInstructions,
@ -138,10 +151,18 @@ export function renderMarkdownBundle(context: ContextBundle): string {
const retrieval = context.retrieval.length
? context.retrieval
.map(
(result) =>
`- score=${result.score.toFixed(2)} source=${result.sourceId} text=${result.text}`,
)
.map((result) => {
const timeContext =
result.sourceKind === "discord_message" && result.metadata.timestamp
? ` time=${result.metadata.timestamp}`
: "";
const authorName =
result.sourceKind === "discord_message" && result.metadata.authorName
? ` author=${result.metadata.authorName}`
: "";
return `- score=${result.score.toFixed(2)} source=${result.sourceId}${authorName}${timeContext} text=${result.text}`;
})
.join("\n")
: "- No retrieval results attached.";

View file

@ -24,3 +24,4 @@
{"ts":"2026-05-02T18:58:38Z","type":"bugfix","status":"ok","note":"Local LLM could choose search_sources for real-life incident questions and get Ghostlight narrative hits; prompt/tool guidance now biases prior server drama and personal-history questions toward search_history before repo/lore search."}
{"ts":"2026-05-02T19:11:44Z","type":"bugfix","status":"ok","note":"Public local replies were too free to surface internal user-analysis language and too vulnerable to prompt-echo history results; interaction memory scaffolding is now slimmer/private and local history tool results filter echoed current asks before the model sees them."}
{"ts":"2026-05-02T20:25:00+01:00","type":"architecture","status":"ok","note":"Ephemeral room-reading now comes from a separate `think=false` local Ollama sidecar pass over recent room context instead of hand-built phrase heuristics, and the inferred reads are stored as audit events so they can later be aggregated into richer profiles without immediately hardening every passing vibe into durable identity truth."}
{"ts":"2026-05-02T21:05:00+01:00","type":"bugfix","status":"ok","note":"Archived-history answers should not make the model infer time sense from bare ISO strings alone; history tool results now carry human-readable time context, and both reply lanes are explicitly told to narrate old events in the correct tense and avoid guessing pronouns when the supplied context does not state them."}