Skip to content

test: let e2e scenarios move the local date through a test clock file - #5570

Merged
RealKai42 merged 2 commits into
mainfrom
test/e2e-cache-test-clock
Nov 26, 2026
Merged

RealKai42 merged 2 commits into
mainfrom
test/e2e-cache-test-clock

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

这是 tests/e2e/scenarios/cache 专题(#5526)清单里的 E5:会话进行到一半,用户机器上的日期变了,比如跨过了午夜。产品的承诺是保持缓存前缀:日期只在提醒里告诉模型,system 里不写日期,所以日期变化不该改写前缀。之前只有引擎测试覆盖这一项,E2E 没办法让产品看到的时间往前走,清单里记着缺口「需要一个产品经 IHostClock 读取的测试时钟」。这个 PR 加上这个测试时钟,并补上 E5 的 E2E 场景。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路:引擎的 IHostClock 是日期提醒取时间的唯一来源。设置了测试开关 KIMI_CODE_CLOCK_FILE 时,它从这个变量指向的文件里读当前时间;每次调用都重新读,所以测试改写文件,就相当于用户机器上的时间过去了。E2E 驱动只在场景给了 clock 选项时才写这个文件、设这个变量,其他场景启动产品的环境和原来一样。

flowchart LR
    S["场景:clock 选项给起点<br/>clock(时间) 改写时钟"] -->|写| F["测试 home 里的 clock.txt"]
    D["E2E 驱动 productEnv"] -->|文件存在时设置<br/>KIMI_CODE_CLOCK_FILE| P["产品进程<br/>TUI / ACP / Web / Desktop"]
    P --> C["IHostClock.now()<br/>每次调用都读文件"]
    F -.读.-> C
    C --> R["日期提醒<br/>今天的日期 / 日期变了"]
    R --> M["发给假模型的请求"]
Loading

IHostClock 只被日期提醒用到,它只决定告诉模型的本地日期。引擎里其他直接调用 Date.now()、new Date() 的地方(超时、时间戳等)不受影响,仍然是真实时间。

决策 选择 没选 理由
在哪里换掉时间 只改 IHostClock 假定时器、改整个进程的时间 专题对测试口子的原则是只替代外部世界、不干扰产品逻辑。IHostClock 只喂日期提醒,换掉它不会碰到超时和时间戳这些逻辑
时间从哪里来 文件,每次调用都读 环境变量里直接写时间 环境变量在进程启动时就固定了,没法模拟「会话进行中跨过午夜」。文件可以随时改,重启后产品读到的还是同一个时间
文件内容无效时 抛错,不退回真实时间 悄悄用真实时间 退回真实时间会让场景跑在一个它没设定的日期上。抛错之后,引擎的提醒服务记一条 context provider failed 日志,这次请求不带日期,断言日期的场景一定失败
什么时候设这个变量 只在场景给了 clock 选项、时钟文件存在时 所有场景都设 没用时钟的场景,产品的启动环境保持不变

E5 场景 date-change-keeps-the-prefix:起点 2030-01-01T12:00:00Z,第一回合之后把时钟拨到第二天,再聊一回合,重启,再聊一回合。断言:

  • 先证明操作生效:第一次请求的提醒里有第一天的本地日期,第二次请求的提醒里有第二天的本地日期。本地日期在场景里用 toLocaleDateString('en-CA') 算,不写死,因为不同时区的日期不同;两个时刻在任何时区下都正好差一天。
  • 三次请求的 system 完全相同。
  • 主请求一路延续,没有断点。

阅读顺序:

  1. packages/agent-core-v2/src/os/backends/node-local/hostClockService.ts 和它的单测;
  2. tests/e2e/surfaces/environment.ts:什么时候把变量交给产品;
  3. tests/e2e/support/scenario.ts:clock 选项和 clock(时间);
  4. tests/e2e/scenarios/cache/date-change-keeps-the-prefix.spec.ts。

可以略读:scripts/checks/e2e-boundary.json 里 testSwitches 的登记、清单 E5 的覆盖更新、tests/e2e/AGENTS.md「写场景」表新增的一行、步骤标题。

Behavior Changes and Affected Users

行为 之前 之后 谁依赖旧行为 退路
引擎的本机时钟,没设 KIMI_CODE_CLOCK_FILE 真实时间 真实时间,不变 所有用户:TUI、print 模式、Desktop、kimi web、ACP、SDK —
引擎的本机时钟,设了 KIMI_CODE_CLOCK_FILE 变量被忽略,读真实时间 读文件里的 ISO-8601 时间;文件读不了或内容不是时间时抛错,这次请求不带日期提醒,并记一条错误日志 无:这是新加的测试开关,只登记在 scripts/checks/e2e-boundary.json 的 testSwitches,不进用户文档 不设这个变量
E2E 驱动启动产品的环境 不设这个变量 场景给了 clock 选项时设置,否则不变 现有的所有 E2E 场景和驱动自测 不需要:不给 clock 选项时环境不变

受影响模块和覆盖:

  • 引擎时钟:hostClockService.test.ts 3 条,分别是不设变量时读真实时间、每次调用都重读文件、内容无效时抛错;日期提醒原有的 dateChange 单测继续通过。
  • E2E:E5 场景在 6 个 project 上跑;productEnv 是所有面启动产品的入口,改了它,所以重启相关的驱动自测也要跟着跑,交给本 PR 的 E2E 作业。

changeset:不带。引擎只多了一个测试开关,用户不设它时行为完全不变,维护者决定不写。

验证:见折叠块。E2E 没有在本地跑,交给本 PR 的 CI。

运行的命令和结果

基线是 main d5263af02,在 macOS 上跑。

  • pnpm lint:通过,e2e-cache-catalog 报 39 项、10 个 cache 场景都已登记、18 项写明了缺口(E5 的缺口去掉了)。
  • pnpm exec tsc -p packages/agent-core-v2/tsconfig.json --noEmit、pnpm exec tsc -p tests/e2e/tsconfig.json --noEmit:通过。
  • pnpm exec vitest run packages/agent-core-v2/test/os/backends/node-local/hostClockService.test.ts packages/agent-core-v2/test/features/dateChange:18 条通过。
  • node --test 'scripts/checks/*.test.mjs':130 条通过。
  • pnpm run check:duplication d5263af02:没有新增重复。
  • CLI、Web、Desktop 都构建通过,构建产物里带上了新开关。
  • 未在本地验证:E2E(E5 场景和重启自测,交给 CI 的 E2E 作业),以及「去掉读时钟文件、或改坏日期提醒后 E5 会失败」的破坏性检查。从断言看,第一次请求必须带 2030-01-01,产品读真实时间时不可能满足;第二次请求必须带 2030-01-02,只有日期提醒在时钟拨动后发出「日期变了」才能满足。
  • 路径只经过 path.join 和 readFileSync,内容先 trim(),CRLF 和 BOM 都会被去掉。只在 macOS 上核对过,Linux 和 Windows 交给 CI。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: d6c138d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T16:11:34.347013Z d6c138d New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@d6c138d
npx https://pkg.pr.new/@moonshot-ai/kimi-code@d6c138d

commit: d6c138d

@RealKai42
RealKai42 merged commit f94b9a2 into main Nov 26, 2026
27 of 28 checks passed
@RealKai42
RealKai42 deleted the test/e2e-cache-test-clock branch November 26, 2026 16:43
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 26, 2026
Port upstream f94b9a2: KIMI_CODE_CLOCK_FILE drives HostClockService.now(). The fork keeps the host/ path while upstream uses node-local/; the source blob matches the upstream post-image. The unit test covers the ISO file, invalid input and the wall-clock fallback.

The upstream e2e scenario and support files are test-only and remain unported; the source status is present. Ledger MoonshotAI#5570 pending -> ported.

Verified: host clock 3 pass; agent-core-v2 typecheck clean; check:no-comments clean.
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