Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(helper): reject impossible calendar dates in history time filters

Zhao Dengfeng464c12267697
diff --git a/packages/lizi-mcps/src/__tests__/searchChatHistoryTool.test.ts b/packages/lizi-mcps/src/__tests__/searchChatHistoryTool.test.ts
index 5fe481a96f..2396cfdd9f 100644
--- a/packages/lizi-mcps/src/__tests__/searchChatHistoryTool.test.ts
+++ b/packages/lizi-mcps/src/__tests__/searchChatHistoryTool.test.ts
@@ -105,6 +105,19 @@ describe('search_chat_history tool', () => {
expect(searchChatHistory).not.toHaveBeenCalled();
});
+ it('日历上不存在的日期 → INVALID_ARGS, 不静默滚到下个月; 合法闰日照常查询', async () => {
+ const { registry, searchChatHistory } = setup(makeResult());
+ // new Date('2024-02-30') 会被归一化成 2024-03-01, 查询范围静默偏移。
+ const res = await registry.call('search_chat_history', { query: 'x', from: '2024-02-30' });
+ expect(parse(res)).toMatchObject({ ok: false, errorCode: 'INVALID_ARGS' });
+ expect(searchChatHistory).not.toHaveBeenCalled();
+
+ await registry.call('search_chat_history', { query: 'x', to: '2024-02-29T23:59:59Z' });
+ expect(searchChatHistory).toHaveBeenCalledWith(
+ expect.objectContaining({ toMs: Date.parse('2024-02-29T23:59:59Z') }),
+ );
+ });
+
it('payload 整形: createdAt→ISO, snippet/技术字段 omit-when-null, isHit, 诊断字段', async () => {
const ts = Date.parse('2026-05-01T08:00:00.000Z');
const result = makeResult({
diff --git a/packages/lizi-mcps/src/xdt-helper/_history_time.ts b/packages/lizi-mcps/src/xdt-helper/_history_time.ts
new file mode 100644
index 0000000000..6b6bb9cd1d
--- /dev/null
+++ b/packages/lizi-mcps/src/xdt-helper/_history_time.ts
@@ -0,0 +1,26 @@
+/**
+ * 历史工具 from / to 的时间解析(search_chat_history / get_chat_history /
+ * list_sessions 共用)。
+ *
+ * `new Date('2024-02-30')` 不报错, 而是静默滚到 2024-03-01 —— 模型推算
+ * 「月底」时很容易写出这种日期, 查询范围会悄悄偏移。这里在保留现有可接受
+ * 写法的前提下, 只额外拒绝日历上不存在的 `YYYY-MM-DD` 日期部分。
+ *
+ * - undefined / 空串 → null (不过滤)
+ * - 合法时间 → unix ms
+ * - 非法 → 'invalid' (调用方返 INVALID_ARGS)
+ */
+export function parseIsoMs(s: string | undefined): number | null | 'invalid' {
+ if (s === undefined || s === '') return null;
+ const t = new Date(s).getTime();
+ if (!Number.isFinite(t)) return 'invalid';
+ const date = /^(\d{4})-(\d{2})-(\d{2})(?![\d])/.exec(s);
+ if (date) {
+ const year = Number(date[1]);
+ const month = Number(date[2]);
+ const day = Number(date[3]);
+ const daysInMonth = new Date(Date.UTC(year, month, 0)).getUTCDate();
+ if (month < 1 || month > 12 || day < 1 || day > daysInMonth) return 'invalid';
+ }
+ return t;
+}
diff --git a/packages/lizi-mcps/src/xdt-helper/get_chat_history.ts b/packages/lizi-mcps/src/xdt-helper/get_chat_history.ts
index f58a1087a7..d208d99bd0 100644
--- a/packages/lizi-mcps/src/xdt-helper/get_chat_history.ts
+++ b/packages/lizi-mcps/src/xdt-helper/get_chat_history.ts
@@ -18,6 +18,7 @@ import { z } from 'zod';
import type { XdtHelperToolRegistry } from '../lizi_xdtHelperToolRegistry.js';
import type { XdtHelperHistoryDeps, HistoryRole } from './_history_types.js';
import { okPayload, errorPayload } from './_payload.js';
+import { parseIsoMs } from './_history_time.js';
import { stringArrayParam } from './_array-param.js';
import { encodeCursor, decodeCursor } from './_history_cursor.js';
import { resolveHistoryScope } from './_history_scope.js';
@@ -300,10 +301,3 @@ export function registerGetChatHistoryTool(
},
});
}
-
-function parseIsoMs(s: string | undefined): number | null | 'invalid' {
- if (s === undefined || s === '') return null;
- const t = new Date(s).getTime();
- if (!Number.isFinite(t)) return 'invalid';
- return t;
-}
diff --git a/packages/lizi-mcps/src/xdt-helper/list_sessions.ts b/packages/lizi-mcps/src/xdt-helper/list_sessions.ts
index 43121af459..0d53a95a54 100644
--- a/packages/lizi-mcps/src/xdt-helper/list_sessions.ts
+++ b/packages/lizi-mcps/src/xdt-helper/list_sessions.ts
@@ -17,6 +17,7 @@ import type { XdtHelperToolRegistry } from '../lizi_xdtHelperToolRegistry.js';
import type { XdtHelperHistoryDeps } from './_history_types.js';
import type { SessionQueueDeps } from './list_session_queue.js';
import { okPayload, errorPayload } from './_payload.js';
+import { parseIsoMs } from './_history_time.js';
import { encodeCursor, decodeCursor } from './_history_cursor.js';
import { resolveHistoryScope } from './_history_scope.js';
import { historyDeviceShape, HISTORY_DEVICE_DESCRIPTION, queryHistoryDevices } from './_history_devices.js';
@@ -182,16 +183,3 @@ export function registerListSessionsTool(
},
});
}
-
-/**
- * 把可选 ISO 8601 字符串解析成 unix ms。
- * - undefined / 空串 → null (不过滤)
- * - 合法时间 → number
- * - 非法 → 'invalid' (调用方返 INVALID_ARGS)
- */
-function parseIsoMs(s: string | undefined): number | null | 'invalid' {
- if (s === undefined || s === '') return null;
- const t = new Date(s).getTime();
- if (!Number.isFinite(t)) return 'invalid';
- return t;
-}
diff --git a/packages/lizi-mcps/src/xdt-helper/search_chat_history.ts b/packages/lizi-mcps/src/xdt-helper/search_chat_history.ts
index aef59eaf5e..657cc47cb5 100644
--- a/packages/lizi-mcps/src/xdt-helper/search_chat_history.ts
+++ b/packages/lizi-mcps/src/xdt-helper/search_chat_history.ts
@@ -29,6 +29,7 @@ import { z } from 'zod';
import type { XdtHelperToolRegistry } from '../lizi_xdtHelperToolRegistry.js';
import type { XdtHelperHistoryDeps, HistoryRole } from './_history_types.js';
import { okPayload, errorPayload } from './_payload.js';
+import { parseIsoMs } from './_history_time.js';
import { stringArrayParam } from './_array-param.js';
import { resolveHistoryScope } from './_history_scope.js';
import { historyDeviceShape, HISTORY_DEVICE_DESCRIPTION, queryHistoryDevices } from './_history_devices.js';
@@ -251,13 +252,6 @@ export function registerSearchChatHistoryTool(
});
}
-function parseIsoMs(s: string | undefined): number | null | 'invalid' {
- if (s === undefined || s === '') return null;
- const t = new Date(s).getTime();
- if (!Number.isFinite(t)) return 'invalid';
- return t;
-}
-
/**
* offset 游标编解码 —— search 的候选池是物化排名列表, 用简单 offset 翻页
* (区别于 list/get 工具的 (createdAt,id) keyset 游标, 故不复用 _history_cursor)。