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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'pathe';

import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { IAgentContextMemoryService } from '#/agent/contextMemory/contextMemory';
import type { ContextMessage } from '#/agent/contextMemory/types';
import { IAgentLoopService } from '#/agent/loop/loop';
import { IAgentProfileService } from '#/agent/profile/profile';
import { IAgentConversationUndoService } from '#/agent/undo/undo';
import { DEFAULT_AGENT_PROFILE_NAME } from '#/app/agentProfileCatalog/agentProfileCatalog';
import {
  AgentDateChangeService,
  IAgentDateChangeService,
} from '#/features/dateChange/dateChangeService';
import { IHostClock } from '#/os/interface/hostClock';
import { ISessionContext } from '#/session/sessionContext/sessionContext';

import {
  appService,
  createTestAgent,
  hostEnvironmentServices,
  InMemoryWireRecordPersistence,
  type TestAgentContext,
} from '../../harness';
import { runWillBeginStepHooks } from '../../agent/loop/stubs';

const TEST_TIME_ZONE = 'Asia/Shanghai';
const INITIAL_INSTANT = '2026-07-29T04:00:00.000Z';

interface TestHostClock extends IHostClock {
  set(iso: string): void;
}

function testHostClock(initialIso: string): TestHostClock {
  let current = new Date(initialIso);
  return {
    _serviceBrand: undefined,
    now: () => new Date(current),
    timeZone: () => TEST_TIME_ZONE,
    set: (iso) => {
      current = new Date(iso);
    },
  };
}

function systemPromptWithDate(iso: string): string {
  return [
    'You are a deterministic test agent.',
    '',
    `The current date and time in ISO format is \`${iso}\`. This was captured when the session started and does not update.`,
  ].join('\n');
}

function updateSystemPrompt(profile: IAgentProfileService, systemPrompt: string, cwd: string): void {
  profile.update({
    systemPrompt,
    environmentDisclosure: { cwd },
  });
}

function dateReminders(context: IAgentContextMemoryService): readonly ContextMessage[] {
  return context.get().filter((message) => {
    return message.origin?.kind === 'injection' && message.origin.variant === 'date_change';
  });
}

function messageText(message: ContextMessage): string {
  return message.content
    .map((part) => (part.type === 'text' ? part.text : ''))
    .join('');
}

describe('AgentDateChangeService', () => {
  let ctx: TestAgentContext;
  let context: IAgentContextMemoryService;
  let clock: TestHostClock;
  let loop: IAgentLoopService;
  let profile: IAgentProfileService;

  beforeEach(async () => {
    clock = testHostClock(INITIAL_INSTANT);
    ctx = createTestAgent({ autoConfigure: false }, appService(IHostClock, clock));
    context = ctx.get(IAgentContextMemoryService);
    loop = ctx.get(IAgentLoopService);
    profile = ctx.get(IAgentProfileService);
    await ctx.restorePersisted();
    ctx.configure();
  });

  afterEach(async () => {
    try {
      await ctx.expectResumeMatches();
    } finally {
      await ctx.dispose();
    }
  });

  it('injects on the first step even when the system prompt text states today\'s date', async () => {
    updateSystemPrompt(profile, systemPromptWithDate(INITIAL_INSTANT), ctx.get(ISessionContext).cwd);

    await runWillBeginStepHooks(loop);

    const reminders = dateReminders(context);
    expect(reminders).toHaveLength(1);
    expect(messageText(reminders[0] as ContextMessage)).toContain('2026-07-29');
  });

  it('discloses the current date and stays quiet when the system prompt text is stale', async () => {
    updateSystemPrompt(
      profile,
      systemPromptWithDate('2026-07-28T04:00:00.000Z'),
      ctx.get(ISessionContext).cwd,
    );

    await runWillBeginStepHooks(loop);

    const reminders = dateReminders(context);
    expect(reminders).toHaveLength(1);
    const first = reminders[0];
    expect(first).toBeDefined();
    const text = messageText(first as ContextMessage);
    expect(text).toContain("Today's date is 2026-07-29");
    expect(first?.origin).toMatchObject({
      kind: 'injection',
      variant: 'date_change',
      disclosure: {
        kind: 'date',
        renderGeneration: 2,
        localDate: '2026-07-29',
        timeZone: TEST_TIME_ZONE,
      },
    });

    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(1);
  });

  it('announces each date crossed by a long-lived session', async () => {
    updateSystemPrompt(profile, systemPromptWithDate(INITIAL_INSTANT), ctx.get(ISessionContext).cwd);
    await runWillBeginStepHooks(loop);

    clock.set('2026-07-30T04:00:00.000Z');
    await runWillBeginStepHooks(loop);

    let reminders = dateReminders(context);
    expect(reminders).toHaveLength(2);
    expect(messageText(reminders[1] as ContextMessage)).toContain('2026-07-30');

    clock.set('2026-07-31T04:00:00.000Z');
    await runWillBeginStepHooks(loop);

    reminders = dateReminders(context);
    expect(reminders).toHaveLength(3);
    expect(messageText(reminders[2] as ContextMessage)).toContain('2026-07-31');
    expect(reminders[2]?.origin).toMatchObject({
      disclosure: {
        kind: 'date',
        renderGeneration: 2,
        localDate: '2026-07-31',
      },
    });
  });

  it('injects on the first step when a persisted prompt crosses midnight before resume', async () => {
    const persistence = new InMemoryWireRecordPersistence();
    await ctx.dispose();
    ctx = createTestAgent({ persistence }, appService(IHostClock, clock));
    profile = ctx.get(IAgentProfileService);
    updateSystemPrompt(profile, systemPromptWithDate(INITIAL_INSTANT), ctx.get(ISessionContext).cwd);
    await ctx.wire.flush();
    await ctx.dispose();

    clock.set('2026-07-30T04:00:00.000Z');
    ctx = createTestAgent(
      { autoConfigure: false, persistence },
      appService(IHostClock, clock),
    );
    context = ctx.get(IAgentContextMemoryService);
    loop = ctx.get(IAgentLoopService);
    await ctx.restorePersisted();

    await runWillBeginStepHooks(loop);

    const reminders = dateReminders(context);
    expect(reminders).toHaveLength(1);
    expect(messageText(reminders[0] as ContextMessage)).toContain('2026-07-30');
  });

  it('discloses the current date after resuming a legacy profile without disclosure metadata', async () => {
    const persistence = new InMemoryWireRecordPersistence();
    await ctx.dispose();
    ctx = createTestAgent({ persistence }, appService(IHostClock, clock));
    profile = ctx.get(IAgentProfileService);
    profile.applyBindingSnapshot({
      modelAlias: 'mock-model',
      profileName: 'agent',
      thinkingLevel: 'off',
      systemPrompt: systemPromptWithDate(INITIAL_INSTANT),
      disallowedTools: [],
    });
    await ctx.wire.flush();
    const legacyBind = persistence.records.find((record) => record.type === 'profile.bind');
    expect(legacyBind?.['environmentDisclosure']).toBeUndefined();
    await ctx.dispose();

    clock.set('2026-07-30T04:00:00.000Z');
    ctx = createTestAgent(
      { autoConfigure: false, persistence },
      appService(IHostClock, clock),
    );
    context = ctx.get(IAgentContextMemoryService);
    loop = ctx.get(IAgentLoopService);
    await ctx.restorePersisted();

    await runWillBeginStepHooks(loop);
    const initial = dateReminders(context);
    expect(initial).toHaveLength(1);
    expect(messageText(initial[0] as ContextMessage)).toContain('2026-07-30');

    clock.set('2026-07-31T04:00:00.000Z');
    await runWillBeginStepHooks(loop);
    const reminders = dateReminders(context);
    expect(reminders).toHaveLength(2);
    expect(messageText(reminders[1] as ContextMessage)).toContain('2026-07-31');
  });

  it('announces a crossed midnight through a real bind rendered from the host clock', async () => {
    const homeDir = await mkdtemp(join(tmpdir(), 'kimi-date-bind-home-'));
    try {
      await ctx.dispose();
      ctx = createTestAgent(appService(IHostClock, clock), hostEnvironmentServices(homeDir));
      context = ctx.get(IAgentContextMemoryService);
      loop = ctx.get(IAgentLoopService);
      profile = ctx.get(IAgentProfileService);
        await ctx.restorePersisted();

      await profile.bind({ profile: DEFAULT_AGENT_PROFILE_NAME, model: 'mock-model' });

      await runWillBeginStepHooks(loop);
      const initial = dateReminders(context);
      expect(initial).toHaveLength(1);
      expect(messageText(initial[0] as ContextMessage)).toContain('2026-07-29');

      clock.set('2026-07-30T04:00:00.000Z');
      await runWillBeginStepHooks(loop);

      const reminders = dateReminders(context);
      expect(reminders).toHaveLength(2);
      expect(messageText(reminders[1] as ContextMessage)).toContain('2026-07-30');
    } finally {
      await rm(homeDir, { recursive: true, force: true });
    }
  });

  it('keeps the newer render-generation disclosure when an older metadata reminder appears later', async () => {
    updateSystemPrompt(
      profile,
      'You are a deterministic test agent.',
      ctx.get(ISessionContext).cwd,
    );

    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(1);

    context.append({
      role: 'user',
      content: [{ type: 'text', text: 'older metadata reminder' }],
      toolCalls: [],
      origin: {
        kind: 'injection',
        variant: 'date_change',
        disclosure: {
          kind: 'date',
          renderGeneration: 1,
          localDate: '2026-07-30',
          timeZone: TEST_TIME_ZONE,
        },
      },
    });

    await runWillBeginStepHooks(loop);

    expect(dateReminders(context)).toHaveLength(2);
  });

  it('keeps the structured reminder metadata exactly once across undo and the next step', async () => {
    updateSystemPrompt(
      profile,
      'You are a deterministic test agent.',
      ctx.get(ISessionContext).cwd,
    );
    context.append({
      role: 'user',
      content: [{ type: 'text', text: 'first turn' }],
      toolCalls: [],
      origin: { kind: 'user' },
    });
    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(1);

    await ctx.get(IAgentConversationUndoService).undo(1);
    expect(dateReminders(context)).toHaveLength(1);
    context.append({
      role: 'user',
      content: [{ type: 'text', text: 'replacement turn' }],
      toolCalls: [],
      origin: { kind: 'user' },
    });

    await runWillBeginStepHooks(loop);

    expect(dateReminders(context)).toHaveLength(1);
  });

  it('keeps the initial date disclosure across undo and the next step', async () => {
    updateSystemPrompt(
      profile,
      'You are a deterministic test agent.',
      ctx.get(ISessionContext).cwd,
    );
    context.append({
      role: 'user',
      content: [{ type: 'text', text: 'first turn' }],
      toolCalls: [],
      origin: { kind: 'user' },
    });
    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(1);

    await ctx.get(IAgentConversationUndoService).undo(1);
    expect(dateReminders(context)).toHaveLength(1);
    context.append({
      role: 'user',
      content: [{ type: 'text', text: 'replacement turn' }],
      toolCalls: [],
      origin: { kind: 'user' },
    });

    await runWillBeginStepHooks(loop);

    const reminders = dateReminders(context);
    expect(reminders).toHaveLength(1);
    expect(messageText(reminders[0] as ContextMessage)).toContain('2026-07-29');
  });

  it('discloses the current date on the first step and stays quiet', async () => {
    updateSystemPrompt(
      profile,
      'You are a deterministic test agent.',
      ctx.get(ISessionContext).cwd,
    );

    await runWillBeginStepHooks(loop);

    const reminders = dateReminders(context);
    expect(reminders).toHaveLength(1);
    expect(messageText(reminders[0] as ContextMessage)).toContain('2026-07-29');
    expect(reminders[0]?.origin).toMatchObject({
      kind: 'injection',
      variant: 'date_change',
      disclosure: {
        kind: 'date',
        renderGeneration: 2,
        localDate: '2026-07-29',
        timeZone: TEST_TIME_ZONE,
      },
    });

    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(1);
  });

  it('announces a crossed midnight after the initial disclosure', async () => {
    updateSystemPrompt(
      profile,
      'You are a deterministic test agent.',
      ctx.get(ISessionContext).cwd,
    );
    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(1);

    clock.set('2026-07-30T04:00:00.000Z');
    await runWillBeginStepHooks(loop);

    const reminders = dateReminders(context);
    expect(reminders).toHaveLength(2);
    expect(messageText(reminders[1] as ContextMessage)).toContain('2026-07-30');

    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(2);
  });

  it('discloses then announces when the snapshot cwd is empty', async () => {
    updateSystemPrompt(profile, 'You are a deterministic test agent.', '');
    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(1);

    clock.set('2026-07-30T04:00:00.000Z');
    await runWillBeginStepHooks(loop);

    const reminders = dateReminders(context);
    expect(reminders).toHaveLength(2);
    expect(messageText(reminders[1] as ContextMessage)).toContain('2026-07-30');
  });

  it('never injects when the snapshot belongs to a different cwd', async () => {
    updateSystemPrompt(profile, 'You are a deterministic test agent.', '/some/other/workspace');

    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(0);

    clock.set('2026-07-30T04:00:00.000Z');
    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(0);
  });

  it('keeps one provider registration across repeated restore', async () => {
    updateSystemPrompt(
      profile,
      'You are a deterministic test agent.',
      ctx.get(ISessionContext).cwd,
    );

    expect(ctx.get(IAgentDateChangeService)).toBeInstanceOf(AgentDateChangeService);
    await runWillBeginStepHooks(loop);
    expect(dateReminders(context)).toHaveLength(1);

    await ctx.restorePersisted();
    await ctx.restorePersisted();
    clock.set('2026-07-30T04:00:00.000Z');
    await runWillBeginStepHooks(loop);

    expect(dateReminders(context)).toHaveLength(2);
  });
});