feat(schedule): create 加 script 可选标记——触发器化的脚本化暗示,零校验零强制

设计定稿(用户拍板):定时任务每次从全新回合跑,理想是首趟趟平的路沉淀成脚本、
以后触发只跑脚本;但封装与否、bash 还是 python、prompt 怎么引用,都由当回合的
模型自决——工具侧只放一个可选参数制造「该不该脚本化」的潜意识提示。

- scheduled_tasks 加 script 列(与 brief 同构迁移,默认空串);createTask 收参落库,
  update 不动它(与 brief 同为身份性字段);
- get 详情显示 script=xxx(肌肉记忆长在哪);list 保持紧凑不带;create 回执带一行
  script=(仅当提供);
- 不校验:create/触发均不查脚本存在——错误原文天然回给触发回合,prompt 自身指示
  就是回退面;schema 参数描述明说这层语义,防模型误当契约。

探针 7 项全过(回执/get 展示、list 紧凑、update 保值、默认空串、无标记不显示、
迁移幂等);agent-core 已重启应用。
This commit is contained in:
2026-09-29 17:54:59 +08:00
parent 8275750600
commit 337f79c653
4 changed files with 19 additions and 6 deletions
+4
View File
@@ -185,6 +185,10 @@ function initDb(dbPath) {
// brief 直接充当会话的日记碎片底子,零 LLM 成本)。默认空串:存量任务无 brief,其下班碎片降级用 prompt 首行。
const schedCols = db.prepare('PRAGMA table_info(scheduled_tasks)').all();
if (!schedCols.some(c => c.name === 'brief')) db.exec("ALTER TABLE scheduled_tasks ADD COLUMN brief TEXT NOT NULL DEFAULT ''");
// 升级老库:scheduled_tasks 补 script 列(可选标记:任务脚本的文件名)。纯标记——创建不验存在、
// 触发不验存在(参数存在本身就是对 AI 的脚本化暗示,怎么写进 prompt、用 bash 还是 python 由它自定);
// get 详情里展示这个「肌肉记忆长在哪」。与 brief 同构,新库建表后也靠本迁移补列。
if (!schedCols.some(c => c.name === 'script')) db.exec("ALTER TABLE scheduled_tasks ADD COLUMN script TEXT NOT NULL DEFAULT ''");
// 迁移:model_slot 记会话粘住的具体池 key 槽位(「平台#key下标」,key 为共享资源不再独占),
// 跨结版/重启保持(只有手动换绑或模型失效改选会动),model_id 存模型 id 供展示/统计(见 engine.applyBinding)。
if (!cols.some(c => c.name === 'model_slot')) db.exec('ALTER TABLE sessions ADD COLUMN model_slot TEXT');
+4 -4
View File
@@ -60,15 +60,15 @@ function validateCron(cronExpr) {
// sessionId 由调用方从工具 ctx 取(AI 无法指定别的会话)。校验顺序:
// validateCron → 本会话尚无任务 → INSERT(主键冲突兜底)。
function createTask(db, sessionId, cronExpr, prompt, brief) {
function createTask(db, sessionId, cronExpr, prompt, brief, script) {
if (prompt.length > PROMPT_MAX_LEN) throw new Error('prompt 过长(上限 ' + PROMPT_MAX_LEN + ' 字符)');
const first = validateCron(cronExpr);
const exist = db.prepare('SELECT cron_expr FROM scheduled_tasks WHERE session_id = ?').get(sessionId);
if (exist) throw new Error('本会话已有定时任务(' + exist.cron_expr + ')。改频次或提示词用 op=update;确要换新任务先 op=delete 再 op=create');
try {
db.prepare(
'INSERT INTO scheduled_tasks (session_id, cron_expr, prompt, brief, next_run_at, created_at) VALUES (?,?,?,?,?,?)'
).run(sessionId, cronExpr, prompt, String(brief || '').slice(0, 80), first, Date.now());
'INSERT INTO scheduled_tasks (session_id, cron_expr, prompt, brief, script, next_run_at, created_at) VALUES (?,?,?,?,?,?,?)'
).run(sessionId, cronExpr, prompt, String(brief || '').slice(0, 80), String(script || '').replace(/\s+/g, ' ').trim().slice(0, 80), first, Date.now());
} catch (_) {
// 单进程同步 API 下正常到不了这里(上一步已查重),仅作主键约束重复报错的兜底
const ex = db.prepare('SELECT cron_expr FROM scheduled_tasks WHERE session_id = ?').get(sessionId);
@@ -96,7 +96,7 @@ function listUserTasks(db, user) {
function getTask(db, sessionId) {
return db.prepare(
'SELECT cron_expr, prompt, brief, next_run_at, last_run_at, last_status, last_note FROM scheduled_tasks WHERE session_id = ?'
'SELECT cron_expr, prompt, brief, script, next_run_at, last_run_at, last_status, last_note FROM scheduled_tasks WHERE session_id = ?'
).get(sessionId) || null;
}
+5 -1
View File
@@ -292,7 +292,7 @@
"type": "function",
"function": {
"name": "schedule",
"description": "Manage your scheduled tasks (alarms). Each task lives on exactly one session (at most one per session) and is addressed by its home session id — the same id op=list shows (e.g. 'task-20260929-003'); omit id (or '0') to act on THIS session's task. At each trigger the prompt is delivered to the task's session as a new user message. Schedule times are interpreted in Asia/Shanghai (UTC+8). Call this ONLY when the user explicitly asks for something to run periodically or at a specific time.\n\nop=get Optional id (default: this session).\nop=list No arguments. Every task of the current user across all sessions, each row starting with its id — hand that id to get/update/delete to act on any task directly; never ask the user to switch sessions.\nop=create Always targets THIS session (id is rejected). Requires cron_expr, prompt and brief. After creating, this session retires into the task's dedicated home (archived; the user's next message starts a fresh session).\nop=update Optional id. Requires at least one of cron_expr, prompt; only given fields change.\nop=delete Optional id. Cancels that task.",
"description": "Manage your scheduled tasks (alarms). Each task lives on exactly one session (at most one per session) and is addressed by its home session id — the same id op=list shows (e.g. 'task-20260929-003'); omit id (or '0') to act on THIS session's task. At each trigger the prompt is delivered to the task's session as a new user message. Schedule times are interpreted in Asia/Shanghai (UTC+8). Call this ONLY when the user explicitly asks for something to run periodically or at a specific time.\n\nop=get Optional id (default: this session).\nop=list No arguments. Every task of the current user across all sessions, each row starting with its id — hand that id to get/update/delete to act on any task directly; never ask the user to switch sessions.\nop=create Always targets THIS session (id is rejected). Requires cron_expr, prompt and brief; optional script. After creating, this session retires into the task's dedicated home (archived; the user's next message starts a fresh session).\nop=update Optional id. Requires at least one of cron_expr, prompt; only given fields change.\nop=delete Optional id. Cancels that task.",
"parameters": {
"type": "object",
"required": [
@@ -325,6 +325,10 @@
"brief": {
"type": "string",
"description": "One short plain-language line (<= 40 chars) saying what this task actually accomplishes, e.g. 'collect and produce a daily news PDF report'. It is written into the assistant's long-term memory diary so the recurring job can be recalled later. Required with op=create."
},
"script": {
"type": "string",
"description": "Optional marker for op=create: a script filename (relative to the session working dir) meant to carry this task's routine, so triggers reuse muscle memory instead of redoing everything. Supplying it signals you should distill the workflow into that script (language, invocation and how the prompt wires it are yours to decide). Existence is never validated — not at create, not at trigger."
}
}
}
+6 -1
View File
@@ -916,6 +916,7 @@ function schedDetail(t) {
const lines = ['id=' + t.session_id + (t.current ? '(本会话)' : '')];
if (t.title) lines.push('name=' + t.title);
if (t.brief) lines.push('brief=' + t.brief);
if (t.script) lines.push('script=' + t.script);
lines.push('cron="' + t.cron_expr + '"');
lines.push('next=' + scheduler.fmtIso(t.next_run_at));
if (t.last_run_at) lines.push('last=' + scheduler.fmtIso(t.last_run_at) + ' last_status=' + (t.last_status || '-'));
@@ -959,10 +960,14 @@ function toolScheduleTask(ctx, args) {
if (!cronExpr) throw new Error('op=create 缺少必填参数 cron_expr');
if (!prompt) throw new Error('op=create 缺少必填参数 prompt');
const brief = String(args.brief || '').replace(/\s+/g, ' ').trim().slice(0, 80);
const t = scheduler.createTask(db, ctx.sessionId, cronExpr, prompt, brief);
// script 纯标记:存在即暗示模型把流程沉淀成脚本(怎么写、用什么跑、prompt 怎么引用由它自定);
// 创建与触发均不校验——缺脚本时 prompt 自身的指示就是回退面。
const script = String(args.script || '').replace(/\s+/g, ' ').trim().slice(0, 80);
const t = scheduler.createTask(db, ctx.sessionId, cronExpr, prompt, brief, script);
// 定时即下班:标记本回合收尾时由 engine 收口(brief 碎片 + 归档 + 通道解绑,见 engine.taskClockOut)。
if (ctx.onTaskCreated) ctx.onTaskCreated();
return '已创建 id=' + ctx.sessionId + '\ncron="' + t.cron_expr + '"\nnext=' + scheduler.fmtIso(t.next_run_at)
+ (script ? '\nscript=' + script : '')
+ '\n本会话即收口为该任务专柜(用户无感知,下条消息自动进新会话)。';
}
case 'update': {