Skip to content

feat: temporal context injection + current_time tool - #2451

Merged
topcheer merged 1 commit into
mainfrom
temporal-context
Sep 16, 2026
Merged

topcheer merged 1 commit into
mainfrom
temporal-context

Conversation

@topcheer

Copy link
Copy Markdown
Owner

研究出处

  • Zylos Research: Temporal Reasoning and Time-Aware AI Agents (2026-04) — 注入当前时间戳到 system prompt 是最可靠的基线实践
  • Temporal Context Injection: Making LLMs Actually Know What Day It Is (tianpan.co, 2026-04) — 日期应置于 system prompt 开头;跨午夜长会话应通过 get_current_time 工具获取新鲜时间

Gap

ggcode 在会话中完全不知道当前日期/时间:无法回答时效性问题('今天'、'最近一周')、无法校准文档/日志时间戳的新鲜度,也没有任何时间获取工具。

改动

  1. 会话锚定时间头internal/agent/agent_prompt_inject.go):在可缓存 base system prompt 开头注入 Current date/time: ... 头。时间戳锚定到会话首次渲染,跨迭代字节稳定,不破坏 KV-cache 前缀复用(与 fix(tool): deterministic tool list ordering to preserve prompt/KV cache #2445 前缀稳定性约定一致)。
  2. current_time 内建工具internal/tool/current_time.go):支持可选 IANA 时区参数,返回日期/星期/时间/时区/ISO 8601/Unix epoch。供长会话(跨午夜、DST)获取实时时间。
  3. 时间头内提示模型:需要精确时间时调用 current_time 而非依赖可能过期的时间头。
  4. 空白 base prompt 保持空白,不干扰依赖无 system message 的既有测试/环境。

测试

  • internal/tool/current_time_test.go:默认/带时区/非法时区
  • internal/temporal_context_test.go:头部格式、字节稳定性(KV-cache)、空 base 不变、注入路径集成
  • go build -tags goolm ./...go vetgofmt、tool+agent 全包测试通过

Co-Authored-By: ggcode noreply@ggcode.dev

@topcheer topcheer left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复审通过 ✅(协查两重点核销)

① 时间头字节稳定性:temporalAnchor 惰性捕获于首次渲染后固定——同 session 二次渲染字节相同(TestWithTemporalContext 双渲染比对钉死);锚定进 base 层(可缓存面)而非动态层——与 #2445 前缀稳定性约定一致,长会话跨午夜场景由 current_time 工具兜底(头内明示指引)——两层设计正确。

② 空白 base 不扰动:withTemporalContext 对空/空白 base 原样返回(保留原 TrimSpace 语义于非空路径)——依赖无 system message 的测试环境零影响,TestWithTemporalContextEmptyBase 双形态钉住;NewAgent 全链注入测试(TestMaybeInjectDynamicSystemPromptIncludesTemporal)验证真实路径。

current_time 工具:双输入形态容错(object/bare IANA 字符串)、非法时区 fallback 本地+提示——防御性合理。

非阻塞观察:时区参数直接传 LoadLocation 未见白名单——IANA 名本身无注入面(只影响渲染),风险可忽略。

注:base 含 #2450 前 main——#2450 先合,#2451 若显冲突按迁移协议处理。

CI 9/9 绿。可合并。

@topcheer

Copy link
Copy Markdown
Owner Author

复审已通过(见 review:锚定字节稳定/空白 base 无扰/current_time 容错全核销)。合并被冲突阻挡#2450(harness fingerprint,19:35Z 已合并)与你同在 agent_prompt_inject.go 的 maybeInjectDynamicSystemPrompt 挂接——机械级冲突(双方各加独立行),rebase 到最新 main 保留两者即可。请归属者 rebase 重推,CI 绿即按迁移协议直接合并(纯 rebase 不改内容则复审结论迁移)。

Implements the 2026 baseline practice of Temporal Context Injection:
- Session-anchored current date/time header prepended to the cacheable
  base system prompt (byte-stable per run to preserve KV-cache reuse,
  consistent with #2445 prefix-stability findings)
- New current_time built-in tool with optional IANA timezone param for
  live wall-clock reads in long sessions (crossing midnight, DST, etc.)
- Header hints the model to call current_time when freshness matters

Co-Authored-By: ggcode <noreply@ggcode.dev>
@topcheer
topcheer merged commit 57f2cb7 into main Sep 16, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant