You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maka.apache.org by li...@apache.org on 2026/09/24 06:33:13 UTC

(maka) branch main updated: fix(runtime): report the Host clock from ScheduledTask (#5667)

This is an automated email from the ASF dual-hosted git repository.

liugddx pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/maka.git


The following commit(s) were added to refs/heads/main by this push:
     new 4d1e35898f fix(runtime): report the Host clock from ScheduledTask (#5667)
4d1e35898f is described below

commit 4d1e35898fa27bb32408a2bee7746e09ad2f85a9
Author: Guangdong Liu <li...@gmail.com>
AuthorDate: Thu Sep 24 14:33:08 2026 +0800

    fix(runtime): report the Host clock from ScheduledTask (#5667)
    
    ScheduledTask takes epoch milliseconds, but nothing in the prompt or the
    tool tells the model the current time or time zone. To schedule "today
    at 10:00" the model reached for a shell (`Get-Date`), which fails closed
    on Windows whenever the permission profile requires a command sandbox, so
    the task was never created.
    
    Every ScheduledTask result now ends with the Host's current time and
    zone, and nextFireAt carries its local rendering. The description tells
    the model to read it via mode=list. The system prompt is untouched, so
    prompt caching is unaffected.
    
    Generated-by: Claude Code
    
    Co-authored-by: Claude Opus 5.5 (1M context) <no...@anthropic.com>
---
 .../src/__tests__/scheduled-task-tools.test.ts     | 97 ++++++++++++++++++++++
 packages/runtime/src/scheduled-task-tools.ts       | 61 +++++++++++---
 2 files changed, 147 insertions(+), 11 deletions(-)

diff --git a/packages/runtime/src/__tests__/scheduled-task-tools.test.ts b/packages/runtime/src/__tests__/scheduled-task-tools.test.ts
new file mode 100644
index 0000000000..1a01f5f4b6
--- /dev/null
+++ b/packages/runtime/src/__tests__/scheduled-task-tools.test.ts
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import assert from 'node:assert/strict';
+import { test } from 'node:test';
+import type { ScheduledTask } from '@maka/core/scheduled-task';
+import {
+  buildScheduledTaskTool,
+  formatScheduledTaskLocalTime,
+  type ScheduledTaskToolAuthority,
+} from '../scheduled-task-tools.js';
+import type { MakaToolContext } from '../tool-runtime.js';
+
+// 2026-09-24T09:42:45+08:00
+const NOW = Date.UTC(2026, 8, 24, 1, 42, 45);
+const TEN_AM_SHANGHAI = Date.UTC(2026, 8, 24, 2, 0, 0);
+
+function authority(created: Partial<ScheduledTask> = {}): ScheduledTaskToolAuthority {
+  return {
+    create: async (input) =>
+      ({
+        id: 'task-1',
+        title: input.title,
+        nextFireAt: input.schedule.kind === 'once' ? input.schedule.runAt : null,
+        effect: { kind: 'notify', channel: 'local' },
+        ...created,
+      }) as ScheduledTask,
+    list: async () => [],
+    pause: async () => ({ error: 'unused' }),
+    resume: async () => ({ error: 'unused' }),
+    remove: async () => ({ error: 'unused' }),
+  };
+}
+
+const ctx = { sessionId: 'session-1' } as MakaToolContext;
+
+test('formats Host time with an explicit offset and zone', () => {
+  assert.equal(
+    formatScheduledTaskLocalTime(NOW, 'Asia/Shanghai'),
+    '2026-09-24T09:42:45+08:00 (Asia/Shanghai)',
+  );
+  assert.equal(formatScheduledTaskLocalTime(NOW, 'UTC'), '2026-09-24T01:42:45+00:00 (UTC)');
+});
+
+test('list reports the current Host time so runAt needs no shell', async () => {
+  const tool = buildScheduledTaskTool({
+    authority: authority(),
+    now: () => NOW,
+    timeZone: 'Asia/Shanghai',
+  });
+  assert.equal(
+    await tool.impl({ mode: 'list' }, ctx),
+    `No scheduled tasks.\nnow=${NOW} (2026-09-24T09:42:45+08:00 (Asia/Shanghai))`,
+  );
+});
+
+test('create echoes the local fire time next to the epoch', async () => {
+  const tool = buildScheduledTaskTool({
+    authority: authority(),
+    now: () => NOW,
+    timeZone: 'Asia/Shanghai',
+  });
+  const result = String(
+    await tool.impl(
+      {
+        mode: 'create',
+        title: 'Tidy Downloads',
+        intentBody: 'Archive screenshots, installers, and temporary documents.',
+        schedule: { kind: 'once', runAt: TEN_AM_SHANGHAI },
+        effect: 'agent_run',
+      },
+      ctx,
+    ),
+  );
+  assert.deepEqual(result.split('\n'), [
+    'Scheduled task created: Tidy Downloads (task-1)',
+    `nextFireAt=${TEN_AM_SHANGHAI} (2026-09-24T10:00:00+08:00 (Asia/Shanghai))`,
+    'effect=notify',
+    `now=${NOW} (2026-09-24T09:42:45+08:00 (Asia/Shanghai))`,
+  ]);
+});
diff --git a/packages/runtime/src/scheduled-task-tools.ts b/packages/runtime/src/scheduled-task-tools.ts
index 7706adcb2d..9052bdeac7 100644
--- a/packages/runtime/src/scheduled-task-tools.ts
+++ b/packages/runtime/src/scheduled-task-tools.ts
@@ -96,31 +96,70 @@ const schema = z.object({
   maxFires: z.number().int().min(1).max(10_000).optional(),
 });
 
-export function buildScheduledTaskTool(deps: { authority: ScheduledTaskToolAuthority }): MakaTool {
+/**
+ * Renders an epoch in the Host's local time zone with an explicit offset, so a
+ * model can turn "today at 10:00" into `runAt` without a shell round trip.
+ */
+export function formatScheduledTaskLocalTime(epochMs: number, timeZone?: string): string {
+  const zone = timeZone ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
+  const parts = Object.fromEntries(
+    new Intl.DateTimeFormat('en-CA', {
+      timeZone: zone,
+      year: 'numeric',
+      month: '2-digit',
+      day: '2-digit',
+      hour: '2-digit',
+      minute: '2-digit',
+      second: '2-digit',
+      hourCycle: 'h23',
+      timeZoneName: 'longOffset',
+    })
+      .formatToParts(epochMs)
+      .map((part) => [part.type, part.value]),
+  );
+  const offset = parts.timeZoneName === 'GMT' ? '+00:00' : parts.timeZoneName?.replace('GMT', '');
+  return `${parts.year}-${parts.month}-${parts.day}T${parts.hour}:${parts.minute}:${parts.second}${offset} (${zone})`;
+}
+
+export function buildScheduledTaskTool(deps: {
+  authority: ScheduledTaskToolAuthority;
+  now?: () => number;
+  timeZone?: string;
+}): MakaTool {
+  const now = deps.now ?? Date.now;
+  const clockLine = () => {
+    const at = now();
+    return `now=${at} (${formatScheduledTaskLocalTime(at, deps.timeZone)})`;
+  };
+  const fireLine = (epochMs: number | null) =>
+    epochMs === null ? '—' : `${epochMs} (${formatScheduledTaskLocalTime(epochMs, deps.timeZone)})`;
   return {
     name: SCHEDULED_TASK_TOOL_NAME,
     displayName: 'ScheduledTask',
     description:
       'Create and manage global scheduled tasks (定时任务). ' +
       'Use for every recurring or one-shot task. All tasks appear in the desktop Scheduled tasks page. ' +
-      'The default session_resume effect continues this conversation; use agent_run for independent work.',
+      'The default session_resume effect continues this conversation; use agent_run for independent work. ' +
+      'Times are epoch milliseconds. Every result reports the current Host time and time zone; ' +
+      'call mode=list first when you need it to compute runAt or startAt.',
     parameters: schema,
     impl: async (raw, ctx) => {
       const input = schema.parse(raw);
       const sessionId = ctx.sessionId;
       if (input.mode === 'list') {
         const tasks = await deps.authority.list();
-        if (tasks.length === 0) return 'No scheduled tasks.';
-        return tasks
-          .map(
+        if (tasks.length === 0) return `No scheduled tasks.\n${clockLine()}`;
+        return [
+          ...tasks.map(
             (task) =>
-              `- ${task.id} | ${task.title} | ${task.status} | next=${task.nextFireAt ?? '—'} | effect=${task.effect.kind}`,
-          )
-          .join('\n');
+              `- ${task.id} | ${task.title} | ${task.status} | next=${fireLine(task.nextFireAt)} | effect=${task.effect.kind}`,
+          ),
+          clockLine(),
+        ].join('\n');
       }
       if (input.mode === 'create') {
         if (!input.title || !input.intentBody || !input.schedule) {
-          return 'create requires title, intentBody, and schedule';
+          return `create requires title, intentBody, and schedule\n${clockLine()}`;
         }
         const result = await deps.authority.create({
           title: input.title,
@@ -130,8 +169,8 @@ export function buildScheduledTaskTool(deps: { authority: ScheduledTaskToolAutho
           sessionId,
           ...(input.maxFires !== undefined ? { maxFires: input.maxFires } : {}),
         });
-        if ('error' in result) return result.error;
-        return `Scheduled task created: ${result.title} (${result.id})\nnextFireAt=${result.nextFireAt}\neffect=${result.effect.kind}`;
+        if ('error' in result) return `${result.error}\n${clockLine()}`;
+        return `Scheduled task created: ${result.title} (${result.id})\nnextFireAt=${fireLine(result.nextFireAt)}\neffect=${result.effect.kind}\n${clockLine()}`;
       }
       if (!input.id) return `${input.mode} requires id`;
       if (input.mode === 'pause') {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org