pi-auto-unattended 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/README.md +39 -6
- package/extensions/pi-auto.ts +184 -4
- package/lib.sh +27 -4
- package/package.json +2 -2
- package/pi-auto +46 -14
- package/systemd/pi-auto.service +5 -2
- package/test/lib.test.sh +25 -2
- package/test/pi-auto.test.mjs +91 -0
- package/test/schedule.test.sh +17 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 0.5.0 (2026-12-05)
|
|
4
|
+
|
|
5
|
+
- 新增工具 **`auto_today`**:agent 写今日补充(`today.prompt`)的唯一通道,作用域**默认 = 会话 cwd**
|
|
6
|
+
(每个会话天然隔离)。跨项目写 / 广播 / 覆盖别人当天的补充 **第一次只拦截并警告**(不落盘),
|
|
7
|
+
用同样参数第二次调用才生效(10 分钟内,按 `作用域+全文` 绑定)。
|
|
8
|
+
- `checkTodayWrite` 纯函数 + `todayHash`(与 lib.sh `pi_plan_today_hash` 同口径,互测);
|
|
9
|
+
写成功后按 CLI 语义清作用域内 enabled 的 `retired/`、重置 `deliveredTo`。
|
|
10
|
+
- 回归:`项目内直写 / 跨项目两次 / 广播两次 / 改字重新确认 / 覆盖拦截 / clear 两次`;
|
|
11
|
+
阴性对照(把守卫短路成放行)⇒ 2 条测试红。
|
|
12
|
+
|
|
13
|
+
## 0.4.1 (2026-12-05)
|
|
14
|
+
|
|
15
|
+
- 修 bug:`plan.json` 的 `today.prompt` 原先是全局字段,**单个项目的一次性简报会被当成全员补充
|
|
16
|
+
投进所有 enabled 会话**(2026-12-05 07:40 limbus-auto 的跑批简报投到了 y700-elden / pi-plugins /
|
|
17
|
+
lobcorp-remake-rs)。新增 `today.dir` 作用域:空 = 广播,非空 = 只投给该目录;
|
|
18
|
+
`hash` 覆盖 prompt+dir,改作用域同样触发重投。
|
|
19
|
+
- CLI 硬线:`pi-auto plan today` 必须显式给 `-C <目录>` 或 `--all`,缺了打印用法并退出 2
|
|
20
|
+
(不再默认广播);`plan list` / `status` / `sync -n` 都会显示作用域。
|
|
21
|
+
- `scripts/limbus-morning.sh` 改为 `plan today -C "$REPO"`;回归测试覆盖
|
|
22
|
+
「作用域外拿不到 / 改作用域重投 / 广播可用」。
|
|
23
|
+
- 服务单元 `KillMode=mixed → process`:tmux server 是 daemon 的子进程(同一 cgroup),mixed 在
|
|
24
|
+
`systemctl --user restart` 时会把整个 cgroup KILL,连正在跑批的会话一起杀掉(2026-12-05 09:33 实证)。
|
|
25
|
+
改成 process 只杀主进程;实测重启后 pane/pi pid 不变、批进程照跑。
|
|
26
|
+
|
|
3
27
|
## 0.4.0 (2026-12-05)
|
|
4
28
|
|
|
5
29
|
- 新增 agent 入口 **`auto_schedule`**(工具):`mode:"once"` 触发一次后 prompt 自动清理,
|
package/README.md
CHANGED
|
@@ -100,7 +100,33 @@ logs/ runner.log / alerts.log / <会话>.log
|
|
|
100
100
|
- 并发:daemon/CLI 与扩展都写 plan.json,统一用 `<plan>.lockdir`(mkdir 原子 + pid 判活)互斥,
|
|
101
101
|
写入一律临时文件 + rename。
|
|
102
102
|
|
|
103
|
-
## 给 agent
|
|
103
|
+
## 给 agent 的入口(一):`auto_today`(今日补充,默认只写本项目)
|
|
104
|
+
|
|
105
|
+
`auto_today` 是 agent 写 `today.prompt` 的唯一通道,**作用域默认 = 调用时的会话 cwd**:
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
auto_today({ action: "set", text: "今天把这批样本收掉" }) // 只给本项目(默认)
|
|
109
|
+
auto_today({ action: "set", text: "…", dir: "/home/x/repo-b" }) // 跨项目:第一次只警告
|
|
110
|
+
auto_today({ action: "set", text: "…", all: true }) // 广播:同样要两次
|
|
111
|
+
auto_today({ action: "show" }) // 看当前补充 + 作用域 + 已投递
|
|
112
|
+
auto_today({ action: "clear" }) // 清空(覆盖别人的也要两次)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
写盘闸门(防 agent 搞乱,`checkTodayWrite` 纯函数 + 单测):
|
|
116
|
+
|
|
117
|
+
| 情形 | 第一次调用 | 第二次同参数(10 分钟内) |
|
|
118
|
+
|---|---|---|
|
|
119
|
+
| `dir` 省略 / = cwd | 直接生效 | — |
|
|
120
|
+
| `dir` 是别的项目 | `isError` + 说明,**不落盘** | 生效 |
|
|
121
|
+
| `all: true`(广播) | `isError` + 说明,不落盘 | 生效 |
|
|
122
|
+
| 会覆盖别人当天的补充(即使写本项目) | `isError` + 说明,不落盘 | 生效 |
|
|
123
|
+
|
|
124
|
+
- 确认按 `作用域 + 全文` 绑定:改一个字就要重新确认;状态在会话内存里,不跨进程。
|
|
125
|
+
- hash 与 CLI 同口径:`sha1(prompt + "\n" + dir)`(`todayHash` / `pi_plan_today_hash` 互测)。
|
|
126
|
+
- 写成功会按 CLI 语义清掉作用域内 enabled 项目的 `retired/`,并重置 `deliveredTo` 让 daemon 下一 tick 投递。
|
|
127
|
+
- 需要「多个项目各自的任务」用 `auto_schedule`(每条带 `dir`),不要用广播的今日补充代替。
|
|
128
|
+
|
|
129
|
+
## 给 agent 的入口(二):`auto_schedule`
|
|
104
130
|
|
|
105
131
|
`extensions/pi-auto.ts` 注册工具 `auto_schedule`(描述/`promptGuidelines` 里写了调用口径,agent 自己会用):
|
|
106
132
|
|
|
@@ -135,11 +161,16 @@ pi-auto schedule on|off <id> # 暂停/恢复
|
|
|
135
161
|
|
|
136
162
|
```bash
|
|
137
163
|
pi-auto plan # $EDITOR 打开 plan.json
|
|
138
|
-
pi-auto plan list # 开关/目录/需求 +
|
|
139
|
-
pi-auto plan today "…"
|
|
164
|
+
pi-auto plan list # 开关/目录/需求 + 今日补充与作用域
|
|
165
|
+
pi-auto plan today -C <目录> "…" # 设今天的补充,只投给该项目的会话(明天自动清空)
|
|
166
|
+
pi-auto plan today --all "…" # 广播给所有 enabled 项目(慎用)
|
|
140
167
|
pi-auto plan today "" # 提前清空
|
|
141
168
|
```
|
|
142
169
|
|
|
170
|
+
> 2026-12-05 事故:`today.prompt` 原来是全局字段,单个项目的当日简报会被投进所有 enabled 会话
|
|
171
|
+
> (limbus 的跑批简报跑到 y700-elden)。所以 `plan today` 现在必须显式给作用域:
|
|
172
|
+
> `-C <目录>` 或 `--all`;不给就报用法并退出 2。手工编辑 plan.json 时同义字段是 `today.dir`。
|
|
173
|
+
|
|
143
174
|
## 定时/延迟条目
|
|
144
175
|
|
|
145
176
|
> 2026-12-05 起优先用 `schedules`(`auto_schedule` / `pi-auto schedule`)——不用手写 JSON,也不用装 timer。
|
|
@@ -150,13 +181,13 @@ daemon **没有**夜间时段门禁(见下节最后一条),`retired/` 也
|
|
|
150
181
|
|
|
151
182
|
1. plan.json 里照常写条目与 `prompt`(常驻需求),但 `enabled: false`;
|
|
152
183
|
2. 装**一次性** systemd user timer(`OnCalendar=<日期> <时刻>` + `Persistent=true`,不要用 `*-*-*` 每天),
|
|
153
|
-
到点跑一个脚本:`enabled=true` → `pi-auto plan today "<当日简报>"` → `pi-auto on -C <目录>`。
|
|
184
|
+
到点跑一个脚本:`enabled=true` → `pi-auto plan today -C <目录> "<当日简报>"` → `pi-auto on -C <目录>`。
|
|
154
185
|
|
|
155
186
|
现成例子(limbus-auto 真机跑批,2026-12-05 07:40):
|
|
156
187
|
|
|
157
188
|
| 文件 | 作用 |
|
|
158
189
|
|---|---|
|
|
159
|
-
| `scripts/limbus-morning.sh` | 日期守卫(只服务 2026-12-05)→ `limbus-boot.sh --status` 只读体检 → 开 `enabled` → 写 `today.prompt`
|
|
190
|
+
| `scripts/limbus-morning.sh` | 日期守卫(只服务 2026-12-05)→ `limbus-boot.sh --status` 只读体检 → 开 `enabled` → 写 `today.prompt`(`-C` 限定 limbus-auto)→ `pi-auto on` |
|
|
160
191
|
| `scripts/limbus-morning-brief.md` | 当日简报正文(前置/主任务/要收的样本/批尾三件套) |
|
|
161
192
|
| `systemd/pi-limbus-morning.{service,timer}` | 模板;实际装到 `~/.config/systemd/user/`(已 enable,`list-timers` 可见 NEXT) |
|
|
162
193
|
| 日志 | `~/.local/state/pi-auto/limbus-morning.log` |
|
|
@@ -221,7 +252,9 @@ systemctl --user status pi-auto.service
|
|
|
221
252
|
|
|
222
253
|
## 安装
|
|
223
254
|
|
|
224
|
-
npm 包:**`pi-auto-unattended
|
|
255
|
+
npm 包:**`pi-auto-unattended`**(版本 0.4.0 = latest,2026-12-05 过审;命令仍叫 `pi-auto` / `pi-auto-daemon`)。
|
|
256
|
+
本机现状:pi 的 `settings.json` 用 `npm:pi-auto-unattended`(实际加载 `~/.pi/agent/npm/node_modules/pi-auto-unattended/`),
|
|
257
|
+
服务 `ExecStart` 也指向该包;本仓是**源码 + 发布源**,改完要 `npm-pub publish` 才生效。
|
|
225
258
|
|
|
226
259
|
```bash
|
|
227
260
|
# 1) 插件 + 命令(pi 会自动加载 package.json 里的 pi.extensions)
|
package/extensions/pi-auto.ts
CHANGED
|
@@ -190,7 +190,8 @@ function enqueueMailbox(text: string): void {
|
|
|
190
190
|
//
|
|
191
191
|
// {
|
|
192
192
|
// "version": 2,
|
|
193
|
-
// "today": { date, prompt, hash, deliveredTo },
|
|
193
|
+
// "today": { date, dir, prompt, hash, deliveredTo }, // 一次性:当天补充;dir 为空 = 广播
|
|
194
|
+
// // 非空 = 只投给该目录(单项目简报必填),见 lib.sh 顶部注释
|
|
194
195
|
// "projects": [ { dir, enabled, prompt, until, oneoff, schedRevive } ],
|
|
195
196
|
// ^^^^^^^^^^^^^ 长期 prompt:不改就不变
|
|
196
197
|
// "schedules":[ { id, dir, mode, at, atEpoch, time, prompt, enabled,
|
|
@@ -229,7 +230,7 @@ export type PlanProject = {
|
|
|
229
230
|
|
|
230
231
|
export type PlanFile = {
|
|
231
232
|
version: number;
|
|
232
|
-
today: { date: string; prompt: string; hash: string; deliveredTo: string[] };
|
|
233
|
+
today: { date: string; dir?: string; prompt: string; hash: string; deliveredTo: string[] };
|
|
233
234
|
projects: PlanProject[];
|
|
234
235
|
schedules: Schedule[];
|
|
235
236
|
};
|
|
@@ -244,7 +245,7 @@ export function planFile(env: NodeJS.ProcessEnv = process.env): string {
|
|
|
244
245
|
}
|
|
245
246
|
|
|
246
247
|
function emptyPlan(): PlanFile {
|
|
247
|
-
return { version: 2, today: { date: "", prompt: "", hash: "", deliveredTo: [] }, projects: [], schedules: [] };
|
|
248
|
+
return { version: 2, today: { date: "", dir: "", prompt: "", hash: "", deliveredTo: [] }, projects: [], schedules: [] };
|
|
248
249
|
}
|
|
249
250
|
|
|
250
251
|
export function readPlan(env: NodeJS.ProcessEnv = process.env): PlanFile {
|
|
@@ -252,7 +253,7 @@ export function readPlan(env: NodeJS.ProcessEnv = process.env): PlanFile {
|
|
|
252
253
|
const raw = JSON.parse(fs.readFileSync(planFile(env), "utf8")) as Partial<PlanFile>;
|
|
253
254
|
return {
|
|
254
255
|
version: 2,
|
|
255
|
-
today: { date: "", prompt: "", hash: "", deliveredTo: [], ...(raw.today ?? {}) },
|
|
256
|
+
today: { date: "", dir: "", prompt: "", hash: "", deliveredTo: [], ...(raw.today ?? {}) },
|
|
256
257
|
projects: Array.isArray(raw.projects) ? raw.projects : [],
|
|
257
258
|
schedules: Array.isArray(raw.schedules) ? raw.schedules : [],
|
|
258
259
|
};
|
|
@@ -472,6 +473,52 @@ function describeSchedule(s: Schedule): string {
|
|
|
472
473
|
|
|
473
474
|
export { describeSchedule };
|
|
474
475
|
|
|
476
|
+
// ---------------------------------------------------------------- 今日补充(today)
|
|
477
|
+
//
|
|
478
|
+
// 2026-12-05 事故:today.prompt 原来没有作用域,单个项目的简报被投进所有会话。
|
|
479
|
+
// 现在的约束(与 lib.sh 同一份契约):
|
|
480
|
+
// - 作用域默认 = 会话 cwd(天然隔离),只有显式 all:true 才是广播;
|
|
481
|
+
// - 跨项目写(scope ≠ cwd)或广播,**首次调用只拦截并警告**,按同样参数第二次调用才生效;
|
|
482
|
+
// - 覆盖别人已占用的 today 也算跨项目写,同样两次确认;
|
|
483
|
+
// - hash 必须与 lib.sh 的 pi_plan_today_hash 一致:sha1(prompt + "\n" + dir)。
|
|
484
|
+
export function todayHash(prompt: string, dir: string): string {
|
|
485
|
+
return crypto.createHash("sha1").update(`${prompt}\n${dir}`).digest("hex");
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
export type TodayWriteCheck = { needsConfirm: boolean; reason?: string };
|
|
489
|
+
|
|
490
|
+
/** 纯函数:这次写 today 要不要二次确认(便于离线自测)。 */
|
|
491
|
+
export function checkTodayWrite(plan: PlanFile, scope: string, cwd: string, text: string, today = localDate(new Date())): TodayWriteCheck {
|
|
492
|
+
const sameAsNow = plan.today.date === today && plan.today.prompt === text && (plan.today.dir ?? "") === scope;
|
|
493
|
+
if (sameAsNow) return { needsConfirm: false };
|
|
494
|
+
const crossProject = scope === "" || resolveDir(scope) !== resolveDir(cwd);
|
|
495
|
+
const occupiesOthers = plan.today.date === today && (plan.today.prompt ?? "").trim() !== "" && (plan.today.dir ?? "") !== scope;
|
|
496
|
+
if (crossProject && occupiesOthers) {
|
|
497
|
+
const owner = plan.today.dir ? plan.today.dir : "所有 enabled 项目(广播)";
|
|
498
|
+
return {
|
|
499
|
+
needsConfirm: true,
|
|
500
|
+
reason:
|
|
501
|
+
scope === ""
|
|
502
|
+
? `广播给所有 enabled 项目,且会覆盖 ${owner} 今天的补充`
|
|
503
|
+
: `跨项目写(${resolveDir(scope)}),且会覆盖 ${owner} 今天的补充`,
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
if (crossProject) {
|
|
507
|
+
return {
|
|
508
|
+
needsConfirm: true,
|
|
509
|
+
reason:
|
|
510
|
+
scope === ""
|
|
511
|
+
? "广播给所有 enabled 项目(不是只给本会话)"
|
|
512
|
+
: `写的是别的项目(${resolveDir(scope)}),本会话 cwd=${resolveDir(cwd)}`,
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
if (occupiesOthers) {
|
|
516
|
+
const owner = plan.today.dir ? plan.today.dir : "所有 enabled 项目(广播)";
|
|
517
|
+
return { needsConfirm: true, reason: `会覆盖 ${owner} 今天的补充` };
|
|
518
|
+
}
|
|
519
|
+
return { needsConfirm: false };
|
|
520
|
+
}
|
|
521
|
+
|
|
475
522
|
export default function (pi: ExtensionAPI) {
|
|
476
523
|
if (inChildProcess()) return;
|
|
477
524
|
let state: AutoState | undefined;
|
|
@@ -479,6 +526,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
479
526
|
let nudgeTimer: ReturnType<typeof setTimeout> | undefined;
|
|
480
527
|
let nudgeAttempts = 0;
|
|
481
528
|
let lastOutcome = "completed";
|
|
529
|
+
// 今日补充的跨项目二次确认:key = scope\u0000text,值为过期时刻(会话内有效,不跨进程)
|
|
530
|
+
const pendingToday = new Map<string, number>();
|
|
531
|
+
const TODAY_CONFIRM_TTL_MS = 10 * 60_000;
|
|
482
532
|
|
|
483
533
|
const touch = (ctx: ExtensionContext, patch: Partial<AutoState> = {}): AutoState => {
|
|
484
534
|
const now = new Date().toISOString();
|
|
@@ -729,6 +779,136 @@ export default function (pi: ExtensionAPI) {
|
|
|
729
779
|
},
|
|
730
780
|
});
|
|
731
781
|
|
|
782
|
+
// ---------------------------------------------------------------- 今日补充入口
|
|
783
|
+
pi.registerTool({
|
|
784
|
+
name: "auto_today",
|
|
785
|
+
label: "今日补充(pi-auto)",
|
|
786
|
+
description:
|
|
787
|
+
"写 pi-auto 的今日补充(plan.json 的 today.prompt,当天会随首条消息/邮箱投给会话,跨天自动清空)。" +
|
|
788
|
+
"作用域默认 = 当前会话 cwd(天然隔离):只影响本项目的会话。" +
|
|
789
|
+
"要写别的项目或广播(all:true)会被拦一次——必须用同样参数再调一次才生效;" +
|
|
790
|
+
"覆盖别人已占用的今日补充也要二次确认。action=show 看当前补充与作用域,action=clear 清空。" +
|
|
791
|
+
"需要每个项目都有自己的一份时用 auto_schedule(schedules 是 per-dir,没有这个限制)。",
|
|
792
|
+
promptSnippet: "今日补充(pi-auto):auto_today({action:\"set\", text:\"…\"}) 默认只给本项目",
|
|
793
|
+
promptGuidelines: [
|
|
794
|
+
"本项目的当日补充 → `auto_today({action:\"set\", text})`,不用给 dir(默认本会话 cwd)。",
|
|
795
|
+
"跨项目广播/写别的仓会被拦一次并警告:确认确实要这样,再用同样参数调第二次(否则不会生效)。",
|
|
796
|
+
"要多个项目各自的到点任务 → `auto_schedule`(每条带 dir),不要用广播的今日补充代替。",
|
|
797
|
+
],
|
|
798
|
+
parameters: {
|
|
799
|
+
type: "object",
|
|
800
|
+
properties: {
|
|
801
|
+
action: { type: "string", enum: ["set", "show", "clear"], description: "set | show | clear" },
|
|
802
|
+
text: { type: "string", description: "今日补充正文(action=set 必填)" },
|
|
803
|
+
dir: { type: "string", description: "作用域目录(默认本会话 cwd)" },
|
|
804
|
+
all: { type: "boolean", description: "广播给所有 enabled 项目(需二次确认)" },
|
|
805
|
+
},
|
|
806
|
+
required: ["action"],
|
|
807
|
+
additionalProperties: false,
|
|
808
|
+
},
|
|
809
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
810
|
+
const p = (params ?? {}) as { action?: string; text?: string; dir?: string; all?: boolean };
|
|
811
|
+
const action = String(p.action ?? "show");
|
|
812
|
+
const file = planFile();
|
|
813
|
+
const fail = (text: string) => ({ content: [{ type: "text" as const, text }], isError: true });
|
|
814
|
+
const today = localDate(new Date());
|
|
815
|
+
|
|
816
|
+
if (action === "show") {
|
|
817
|
+
const plan = readPlan();
|
|
818
|
+
const t = plan.today;
|
|
819
|
+
const fresh = t.date === today && (t.prompt ?? "") !== "";
|
|
820
|
+
return {
|
|
821
|
+
content: [
|
|
822
|
+
{
|
|
823
|
+
type: "text" as const,
|
|
824
|
+
text: fresh
|
|
825
|
+
? `今日补充(${t.date}):作用域=${t.dir ? t.dir : "广播:所有 enabled 项目"}\n已投递:${(t.deliveredTo ?? []).join(", ") || "还没有"}\n正文:${t.prompt}`
|
|
826
|
+
: `今天(${today})还没有今日补充。plan.json:${file}`,
|
|
827
|
+
},
|
|
828
|
+
],
|
|
829
|
+
details: { file, today: t },
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// 作用域:all ⇒ 广播("");显式 dir ⇒ 那个目录;默认 = 本会话 cwd
|
|
834
|
+
const scope = p.all ? "" : resolveDir(String(p.dir ?? "") || ctx.cwd);
|
|
835
|
+
|
|
836
|
+
if (action === "clear") {
|
|
837
|
+
const plan = readPlan();
|
|
838
|
+
const check = checkTodayWrite(plan, scope, ctx.cwd, "", today);
|
|
839
|
+
if (check.needsConfirm) {
|
|
840
|
+
const key = `clear\u0000${scope}`;
|
|
841
|
+
const until = pendingToday.get(key) ?? 0;
|
|
842
|
+
if (until < Date.now()) {
|
|
843
|
+
pendingToday.set(key, Date.now() + TODAY_CONFIRM_TTL_MS);
|
|
844
|
+
audit({ kind: "today-guard", action: "clear", reason: check.reason, scope, cwd: ctx.cwd });
|
|
845
|
+
return fail(
|
|
846
|
+
`已拦截清空:${check.reason}。\n确认要清空就用同样参数再调一次(10 分钟内有效)。`,
|
|
847
|
+
);
|
|
848
|
+
}
|
|
849
|
+
pendingToday.delete(key);
|
|
850
|
+
}
|
|
851
|
+
updatePlan((draft) => {
|
|
852
|
+
draft.today = { date: "", dir: "", prompt: "", hash: "", deliveredTo: [] };
|
|
853
|
+
});
|
|
854
|
+
audit({ kind: "today-clear", scope, cwd: ctx.cwd });
|
|
855
|
+
return { content: [{ type: "text" as const, text: "今日补充已清空(今天不会再投递)" }], details: { file } };
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
if (action !== "set") return fail(`未知 action:${action}(set | show | clear)`);
|
|
859
|
+
const text = String(p.text ?? "").trim();
|
|
860
|
+
if (!text) return fail("set 需要 text:今天的补充正文");
|
|
861
|
+
|
|
862
|
+
const plan = readPlan();
|
|
863
|
+
const check = checkTodayWrite(plan, scope, ctx.cwd, text, today);
|
|
864
|
+
if (check.needsConfirm) {
|
|
865
|
+
const key = `${scope}\u0000${text}`;
|
|
866
|
+
const until = pendingToday.get(key) ?? 0;
|
|
867
|
+
if (until < Date.now()) {
|
|
868
|
+
pendingToday.set(key, Date.now() + TODAY_CONFIRM_TTL_MS);
|
|
869
|
+
audit({ kind: "today-guard", action: "set", reason: check.reason, scope, cwd: ctx.cwd });
|
|
870
|
+
return fail(
|
|
871
|
+
`已拦截写入:${check.reason}。\n` +
|
|
872
|
+
`本会话 cwd=${ctx.cwd},默认只能写本项目的今日补充。\n` +
|
|
873
|
+
`确实要这样,就用**完全相同的参数**再调一次 auto_today(10 分钟内有效,第二次直接生效)。`,
|
|
874
|
+
);
|
|
875
|
+
}
|
|
876
|
+
pendingToday.delete(key);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
const hash = todayHash(text, scope);
|
|
880
|
+
let planAfter: PlanFile;
|
|
881
|
+
try {
|
|
882
|
+
planAfter = updatePlan((draft) => {
|
|
883
|
+
draft.today = { date: today, dir: scope, prompt: text, hash, deliveredTo: [] };
|
|
884
|
+
});
|
|
885
|
+
} catch (error) {
|
|
886
|
+
return fail(`写 plan.json 失败:${String(error)}`);
|
|
887
|
+
}
|
|
888
|
+
// 与 CLI 同义:新的补充 = 重新授权,清掉作用域内 enabled 项目当天的 retired
|
|
889
|
+
for (const proj of planAfter.projects) {
|
|
890
|
+
const dir = proj.dir ? resolveDir(proj.dir) : "";
|
|
891
|
+
if (!dir || !proj.enabled) continue;
|
|
892
|
+
if (proj.until && proj.until < today) continue;
|
|
893
|
+
if (scope === "" || dir === scope) unretire(dir);
|
|
894
|
+
}
|
|
895
|
+
audit({ kind: "today-set", scope, len: text.length, confirmed: Boolean(check.needsConfirm), cwd: ctx.cwd });
|
|
896
|
+
return {
|
|
897
|
+
content: [
|
|
898
|
+
{
|
|
899
|
+
type: "text" as const,
|
|
900
|
+
text:
|
|
901
|
+
`今日补充已设置(${today},明天自动清空)\n` +
|
|
902
|
+
` 作用域:${scope ? scope : "广播:所有 enabled 项目"}\n` +
|
|
903
|
+
` hash:${hash}\n 正文:${text.length > 200 ? `${text.slice(0, 200)}…` : text}\n` +
|
|
904
|
+
` plan.json:${file}(daemon 下一 tick 投给作用域内的会话)`,
|
|
905
|
+
},
|
|
906
|
+
],
|
|
907
|
+
details: { file, today: planAfter.today },
|
|
908
|
+
};
|
|
909
|
+
},
|
|
910
|
+
});
|
|
911
|
+
|
|
732
912
|
pi.registerCommand("auto", {
|
|
733
913
|
description: "pi-auto 开关:/auto on|off|status(无人值守会话默认 on,手开会话默认 off)",
|
|
734
914
|
handler: async (args, ctx) => {
|
package/lib.sh
CHANGED
|
@@ -183,7 +183,8 @@ pi_retired_clear() { rm -f "$(pi_retired_file "$1")"; }
|
|
|
183
183
|
# ---- 计划文件 plan.json ----------------------------------------------------
|
|
184
184
|
# {
|
|
185
185
|
# "version": 2,
|
|
186
|
-
# "today": { "date": "2026-12-05", "
|
|
186
|
+
# "today": { "date": "2026-12-05", "dir": "/abs/dir", "prompt": "今天的补充",
|
|
187
|
+
# "hash": "…", "deliveredTo": ["slug"] },
|
|
187
188
|
# "projects": [ { "dir": "~/repo", "enabled": true, "prompt": "长期自动化需求",
|
|
188
189
|
# "until": "", "oneoff": false, "schedRevive": false } ],
|
|
189
190
|
# "schedules": [ { "id": "sch-20261205-1a2b3c", "dir": "/abs/dir", "mode": "once|recurring",
|
|
@@ -197,6 +198,9 @@ pi_retired_clear() { rm -f "$(pi_retired_file "$1")"; }
|
|
|
197
198
|
# 一次性(mode=once 的 schedules[].prompt、today.prompt)—— 投递/触发即消费,随后自动清理。
|
|
198
199
|
#
|
|
199
200
|
# today.prompt 只在当天有效;跨天由 pi_plan_tick 清空。hash 变化 ⇒ deliveredTo 重置,重新投递一次。
|
|
201
|
+
# today.dir 是「作用域」:空 = 广播给所有 enabled 项目;非空 = 只投给这一目录(2026-12-05 前的实现
|
|
202
|
+
# 没有它,单个项目的简报会被当成全局补充投进所有会话 —— 别再用 today.prompt 做跨项目广播之外的事)。
|
|
203
|
+
# hash 覆盖 prompt+dir,改作用域同样触发重新投递。
|
|
200
204
|
# schedules:mode=once 到点触发一次即删除(消费即清理);mode=recurring 每天 time 触发一次,
|
|
201
205
|
# prompt 长期保留。触发时把 dir 临时放进 projects(until=今天,一次性条目 oneoff=true,
|
|
202
206
|
# 到期由 tick 删除),当天崩溃仍由服务续接。
|
|
@@ -255,11 +259,12 @@ pi_plan_migrate() {
|
|
|
255
259
|
# 并让 today.hash 与 today.prompt 一致(prompt 被手改就重置 deliveredTo)
|
|
256
260
|
pi_plan_tick() {
|
|
257
261
|
pi_plan_has || return 0
|
|
258
|
-
local today d p h need
|
|
262
|
+
local today d p s h need
|
|
259
263
|
today="$(date +%F)"
|
|
260
264
|
d="$(jq -r '.today.date // ""' "$PI_AUTO_PLAN_FILE" 2>/dev/null)"
|
|
261
265
|
p="$(jq -r '.today.prompt // ""' "$PI_AUTO_PLAN_FILE" 2>/dev/null)"
|
|
262
|
-
|
|
266
|
+
s="$(jq -r '.today.dir // ""' "$PI_AUTO_PLAN_FILE" 2>/dev/null)"
|
|
267
|
+
h="$(pi_plan_today_hash "$p" "$s")"
|
|
263
268
|
need="$(jq -r --arg today "$today" --arg h "$h" '
|
|
264
269
|
[ ((.version // 0) < 2),
|
|
265
270
|
((.schedules | type) != "array"),
|
|
@@ -281,7 +286,7 @@ pi_plan_tick() {
|
|
|
281
286
|
else .enabled = false | del(.until) end
|
|
282
287
|
else . end))
|
|
283
288
|
| if (((.today.date // "") != "") and ((.today.date // "") != $today))
|
|
284
|
-
then .today = {date:"", prompt:"", hash:"", deliveredTo:[]}
|
|
289
|
+
then .today = {date:"", dir:"", prompt:"", hash:"", deliveredTo:[]}
|
|
285
290
|
elif (((.today.prompt // "") != "") and ((.today.hash // "") != $h))
|
|
286
291
|
then .today.date = $today | .today.hash = $h | .today.deliveredTo = []
|
|
287
292
|
elif (((.today.prompt // "") != "") and ((.today.date // "") == ""))
|
|
@@ -381,6 +386,24 @@ pi_plan_today_prompt() {
|
|
|
381
386
|
jq -r '.today.prompt // ""' "$PI_AUTO_PLAN_FILE" 2>/dev/null
|
|
382
387
|
}
|
|
383
388
|
|
|
389
|
+
pi_plan_today_scope() { # 今日补充的作用域目录;空 = 广播
|
|
390
|
+
pi_plan_has || return 0
|
|
391
|
+
jq -r '.today.dir // ""' "$PI_AUTO_PLAN_FILE" 2>/dev/null
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
pi_plan_today_hash() { # <prompt> [作用域目录];hash 必须与投递口径一致(含 dir)
|
|
395
|
+
pi_plan_hash "$(printf '%s\n%s' "${1:-}" "${2:-}")"
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
pi_plan_today_for() { # <目录> → 只有作用域为空或等于该目录时才输出今日补充
|
|
399
|
+
local dir="${1:-}" scope
|
|
400
|
+
scope="$(pi_plan_today_scope)"
|
|
401
|
+
if [ -n "$scope" ] && [ "$(pi_slug_for "$scope")" != "$(pi_slug_for "$dir")" ]; then
|
|
402
|
+
return 0
|
|
403
|
+
fi
|
|
404
|
+
pi_plan_today_prompt
|
|
405
|
+
}
|
|
406
|
+
|
|
384
407
|
pi_plan_delivered() { # <目录>;返回 0 = 今天的补充已投递
|
|
385
408
|
pi_plan_has || return 1
|
|
386
409
|
local slug; slug="$(pi_slug_for "$1")"
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-auto-unattended",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Unattended Pi sessions:
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Unattended Pi sessions: tool-side extensions (auto_switch keep-alive, auto_schedule timed runs, auto_today project-scoped daily brief) plus a pi-auto CLI and systemd daemon that keeps one agent alive per selected folder and nudges them through pi-mailbox.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
7
7
|
"pi-extension",
|
package/pi-auto
CHANGED
|
@@ -50,7 +50,7 @@ EOF
|
|
|
50
50
|
# ---- 拉起 / 注入 -----------------------------------------------------------
|
|
51
51
|
inject_today() { # <目录>
|
|
52
52
|
local dir="$1" p
|
|
53
|
-
p="$(
|
|
53
|
+
p="$(pi_plan_today_for "$dir")"
|
|
54
54
|
[ -n "$p" ] || return 0
|
|
55
55
|
pi_plan_delivered "$dir" && return 0
|
|
56
56
|
if pi_mailbox_send "$dir" "【pi-auto 今日补充】$p"; then
|
|
@@ -59,6 +59,13 @@ inject_today() { # <目录>
|
|
|
59
59
|
return 0
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
print_today() { # 今日补充 + 作用域(人看的输出统一走这里)
|
|
63
|
+
local scope
|
|
64
|
+
scope="$(pi_plan_today_scope)"
|
|
65
|
+
printf '今日补充:%s\n' "$(pi_plan_today_prompt || true)"
|
|
66
|
+
printf '今日补充作用域:%s\n' "${scope:-(广播:所有 enabled 项目)}"
|
|
67
|
+
}
|
|
68
|
+
|
|
62
69
|
ensure_one() { # <目录> <常驻需求> [force] [一次性请求]
|
|
63
70
|
local dir="$1" req="${2:-}" force="${3:-0}" extra="${4:-}" live sid launches today_prompt prompt
|
|
64
71
|
if [ ! -d "$dir" ]; then
|
|
@@ -114,7 +121,7 @@ ensure_one() { # <目录> <常驻需求> [force] [一次性请求]
|
|
|
114
121
|
return 1
|
|
115
122
|
fi
|
|
116
123
|
|
|
117
|
-
today_prompt="$(
|
|
124
|
+
today_prompt="$(pi_plan_today_for "$PI_AUTO_DIR")"
|
|
118
125
|
prompt="$(pi_prompt_build "$req" "$today_prompt")"
|
|
119
126
|
[ -n "$extra" ] && prompt+=$'\n\n【本次请求】'"$extra"
|
|
120
127
|
|
|
@@ -177,7 +184,7 @@ cmd_sync() {
|
|
|
177
184
|
if [ "$dry" = "1" ]; then
|
|
178
185
|
printf '%-4s %-44s %s\n' 开关 目录 需求
|
|
179
186
|
pi_projects_list | awk -F'\t' '{printf "%-4s %-44s %s\n", ($1=="1"?"on":"off"), $2, ($3==""?"-":$3)}'
|
|
180
|
-
|
|
187
|
+
print_today
|
|
181
188
|
printf '到点预约:\n'
|
|
182
189
|
pi_plan_sched_due | awk -F'\t' '{printf " %-24s %-9s %s\n", $1, $2, $3}'
|
|
183
190
|
return 0
|
|
@@ -324,7 +331,7 @@ cmd_status() {
|
|
|
324
331
|
echo
|
|
325
332
|
echo "enabled 项目:"
|
|
326
333
|
pi_projects_list | awk -F'\t' '$1=="1"{printf " %-44s %s\n", $2, ($3==""?"-":$3)}'
|
|
327
|
-
|
|
334
|
+
print_today
|
|
328
335
|
echo
|
|
329
336
|
echo "预约:"
|
|
330
337
|
if [ -z "$(pi_plan_sched_list)" ]; then echo " (没有预约)"; else
|
|
@@ -342,7 +349,7 @@ cmd_plan() {
|
|
|
342
349
|
list|ls)
|
|
343
350
|
printf '%-6s %-44s %s\n' 开关 目录 需求
|
|
344
351
|
pi_projects_list | awk -F'\t' '{printf "%-6s %-44s %s\n", ($1=="1"?"on":"off"), $2, ($3==""?"-":$3)}'
|
|
345
|
-
|
|
352
|
+
print_today
|
|
346
353
|
printf '计划文件:%s\n' "$PI_AUTO_PLAN_FILE"
|
|
347
354
|
;;
|
|
348
355
|
edit|show|open|"")
|
|
@@ -353,28 +360,53 @@ cmd_plan() {
|
|
|
353
360
|
esac ;;
|
|
354
361
|
today)
|
|
355
362
|
shift
|
|
356
|
-
local text="
|
|
363
|
+
local text="" scope="" scope_set=0 tmp dir
|
|
364
|
+
# 作用域必填:默认广播会把单个项目的简报投进所有会话(2026-12-05 事故)
|
|
365
|
+
while [ "$#" -gt 0 ]; do
|
|
366
|
+
case "$1" in
|
|
367
|
+
-C|--dir) [ -n "${2:-}" ] || die "$1 需要目录"; scope="$(realpath -m -- "$2")"; scope_set=1; shift 2 ;;
|
|
368
|
+
--all|-A) scope=""; scope_set=1; shift ;;
|
|
369
|
+
--) shift; break ;;
|
|
370
|
+
-*) die "未知参数:$1(plan today [-C 目录|--all] 文本)" ;;
|
|
371
|
+
*) break ;;
|
|
372
|
+
esac
|
|
373
|
+
done
|
|
374
|
+
text="${*:-}"
|
|
357
375
|
tmp="$PI_AUTO_PLAN_FILE.tmp.$$"
|
|
376
|
+
if [ -n "$text" ] && [ "$scope_set" = 0 ]; then
|
|
377
|
+
cat >&2 <<'EOF'
|
|
378
|
+
用法:pi-auto plan today -C <目录> "文本" # 只投给该项目的会话
|
|
379
|
+
pi-auto plan today --all "文本" # 广播给所有 enabled 项目(慎用)
|
|
380
|
+
pi-auto plan today "" # 清空
|
|
381
|
+
EOF
|
|
382
|
+
return 2
|
|
383
|
+
fi
|
|
358
384
|
if [ -n "$text" ]; then
|
|
359
|
-
jq --arg d "$(date +%F)" --arg p "$text" --arg h "$(
|
|
360
|
-
'.today={
|
|
385
|
+
jq --arg d "$(date +%F)" --arg dir "$scope" --arg p "$text" --arg h "$(pi_plan_today_hash "$text" "$scope")" \
|
|
386
|
+
'.today={date:$d,dir:$dir,prompt:$p,hash:$h,deliveredTo:[]}' "$PI_AUTO_PLAN_FILE" >"$tmp" \
|
|
361
387
|
&& mv "$tmp" "$PI_AUTO_PLAN_FILE"
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
388
|
+
if [ -n "$scope" ]; then
|
|
389
|
+
echo "今日补充已设置(作用域:$scope;明天自动清空):$text"
|
|
390
|
+
! pi_projects_list | awk -F'\t' -v d="$scope" '$2==d{f=1} END{exit !f}' \
|
|
391
|
+
&& pi_alert NOTE "今日补充的作用域 $scope 不在 plan.json 的 projects 里(不会投递)"
|
|
392
|
+
else
|
|
393
|
+
echo "今日补充已设置(广播:所有 enabled 项目;明天自动清空):$text"
|
|
394
|
+
fi
|
|
395
|
+
# 新的一天/新的补充 = 重新授权:只清作用域内(或广播时全部)enabled 项目当天的 retired
|
|
396
|
+
local en req
|
|
365
397
|
# 2026-12-04:末尾条目 enabled=false 时 `[ ... ] && cmd` 会让整个函数返回 1(假失败),
|
|
366
398
|
# 调用方(scripts/limbus-morning.sh)会把它当「写今日补充失败」⇒ 改成 if 语义。
|
|
367
399
|
while IFS=$'\t' read -r en dir req; do
|
|
368
|
-
if [ "$en" = "1" ]; then pi_retired_clear "$dir"; fi
|
|
400
|
+
if [ "$en" = "1" ] && { [ -z "$scope" ] || [ "$dir" = "$scope" ]; }; then pi_retired_clear "$dir"; fi
|
|
369
401
|
done < <(pi_projects_list)
|
|
370
402
|
else
|
|
371
|
-
jq '.today={"date":"","prompt":"","hash":"","deliveredTo":[]}' "$PI_AUTO_PLAN_FILE" >"$tmp" \
|
|
403
|
+
jq '.today={"date":"","dir":"","prompt":"","hash":"","deliveredTo":[]}' "$PI_AUTO_PLAN_FILE" >"$tmp" \
|
|
372
404
|
&& mv "$tmp" "$PI_AUTO_PLAN_FILE"
|
|
373
405
|
echo "今日补充已清空"
|
|
374
406
|
fi ;;
|
|
375
407
|
migrate)
|
|
376
408
|
echo "已确保 $PI_AUTO_PLAN_FILE 存在" ;;
|
|
377
|
-
*) echo "用法:pi-auto plan [list|edit|today \"文本\"|migrate]" >&2; exit 2 ;;
|
|
409
|
+
*) echo "用法:pi-auto plan [list|edit|today [-C 目录|--all] \"文本\"|migrate]" >&2; exit 2 ;;
|
|
378
410
|
esac
|
|
379
411
|
}
|
|
380
412
|
|
package/systemd/pi-auto.service
CHANGED
|
@@ -10,8 +10,11 @@ Environment=PI_AUTO_TICK=60
|
|
|
10
10
|
ExecStart=@EXECSTART@
|
|
11
11
|
Restart=on-failure
|
|
12
12
|
RestartSec=30
|
|
13
|
-
#
|
|
14
|
-
KillMode=mixed
|
|
13
|
+
# 退出/重启服务时,tmux 里已拉起的 pi 不应被连带杀掉。
|
|
14
|
+
# 注意:tmux server 本身就是 daemon 的子进程(同一 cgroup),KillMode=mixed 会先 TERM 主进程、
|
|
15
|
+
# 再 KILL 整个 cgroup ⇒ 2026-12-05 09:33 一次 `systemctl --user restart` 把正在跑批的
|
|
16
|
+
# limbus 会话杀掉了(批进程在 systemd-run scope 里才幸存)。process = 只杀主进程。
|
|
17
|
+
KillMode=process
|
|
15
18
|
TimeoutStopSec=60
|
|
16
19
|
|
|
17
20
|
[Install]
|
package/test/lib.test.sh
CHANGED
|
@@ -31,8 +31,8 @@ pi_plan_tick >/dev/null
|
|
|
31
31
|
|
|
32
32
|
# plan: 设置今天的补充 → hash 落盘、未投递
|
|
33
33
|
today="$(date +%F)"
|
|
34
|
-
jq --arg d "$today" --arg p "今天补一件事" --arg h "$(
|
|
35
|
-
'.today={date:$d,prompt:$p,hash:$h,deliveredTo:[]}' "$PI_AUTO_PLAN_FILE" >"$PI_AUTO_PLAN_FILE.t" && mv "$PI_AUTO_PLAN_FILE.t" "$PI_AUTO_PLAN_FILE"
|
|
34
|
+
jq --arg d "$today" --arg p "今天补一件事" --arg h "$(pi_plan_today_hash "今天补一件事")" \
|
|
35
|
+
'.today={date:$d,dir:"",prompt:$p,hash:$h,deliveredTo:[]}' "$PI_AUTO_PLAN_FILE" >"$PI_AUTO_PLAN_FILE.t" && mv "$PI_AUTO_PLAN_FILE.t" "$PI_AUTO_PLAN_FILE"
|
|
36
36
|
pi_plan_tick
|
|
37
37
|
[ "$(pi_plan_today_prompt)" = "今天补一件事" ] || fail "today prompt"
|
|
38
38
|
pi_plan_delivered "$HOME/repo" && fail "should not be delivered yet"
|
|
@@ -43,6 +43,29 @@ jq '.today.prompt="换了一件事"' "$PI_AUTO_PLAN_FILE" >"$PI_AUTO_PLAN_FILE.t
|
|
|
43
43
|
pi_plan_tick
|
|
44
44
|
pi_plan_delivered "$HOME/repo" && fail "prompt change should reset deliveredTo"
|
|
45
45
|
|
|
46
|
+
# ---- 今日补充作用域:单个项目的简报不许投给别的会话(2026-12-05 误投事故回归) ----
|
|
47
|
+
today="$(date +%F)"
|
|
48
|
+
set_today() { # <作用域目录|空字符串> <文本>
|
|
49
|
+
jq --arg d "$today" --arg dir "$1" --arg p "$2" --arg h "$(pi_plan_today_hash "$2" "$1")" \
|
|
50
|
+
'.today={date:$d,dir:$dir,prompt:$p,hash:$h,deliveredTo:[]}' "$PI_AUTO_PLAN_FILE" >"$PI_AUTO_PLAN_FILE.t" \
|
|
51
|
+
&& mv "$PI_AUTO_PLAN_FILE.t" "$PI_AUTO_PLAN_FILE"
|
|
52
|
+
}
|
|
53
|
+
set_today "$HOME/repo" "只给 repo 的简报"
|
|
54
|
+
pi_plan_tick
|
|
55
|
+
[ "$(pi_plan_today_scope)" = "$HOME/repo" ] || fail "today.dir 应落盘"
|
|
56
|
+
[ "$(pi_plan_today_for "$HOME/repo")" = "只给 repo 的简报" ] || fail "作用域内应拿到今日补充"
|
|
57
|
+
[ -z "$(pi_plan_today_for "$HOME/other")" ] || fail "作用域外的目录不许拿到今日补充"
|
|
58
|
+
# 作用域改动 = 重新授权(hash 覆盖 dir)
|
|
59
|
+
pi_plan_mark_delivered "$HOME/repo"
|
|
60
|
+
set_today "$HOME/other" "改投 other"
|
|
61
|
+
pi_plan_tick
|
|
62
|
+
[ "$(jq -r '.today.deliveredTo|length' "$PI_AUTO_PLAN_FILE")" = 0 ] || fail "改作用域应重置 deliveredTo"
|
|
63
|
+
[ -z "$(pi_plan_today_for "$HOME/repo")" ] || fail "改作用域后 repo 不应再收到"
|
|
64
|
+
set_today "" "广播补充"
|
|
65
|
+
pi_plan_tick
|
|
66
|
+
[ "$(pi_plan_today_for "$HOME/repo")" = "广播补充" ] || fail "广播应投给 repo"
|
|
67
|
+
[ "$(pi_plan_today_for "$HOME/other")" = "广播补充" ] || fail "广播应投给 other"
|
|
68
|
+
|
|
46
69
|
# projects 解析(~/ 展开 + enabled 过滤;plan.json 优先,这里直接测 legacy 解析器)
|
|
47
70
|
printf 'true|~/repo|常驻\ntrue|~/other|B\nfalse|~/off|C\n' >"$PI_AUTO_PROJECTS_FILE"
|
|
48
71
|
out="$(pi_projects_list_legacy)"
|
package/test/pi-auto.test.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import { join } from "node:path";
|
|
|
7
7
|
import test from "node:test";
|
|
8
8
|
|
|
9
9
|
import register, {
|
|
10
|
+
checkTodayWrite,
|
|
10
11
|
continuationText,
|
|
11
12
|
describeSchedule,
|
|
12
13
|
inChildProcess,
|
|
@@ -19,6 +20,7 @@ import register, {
|
|
|
19
20
|
retiredToday,
|
|
20
21
|
slugFor,
|
|
21
22
|
statePath,
|
|
23
|
+
todayHash,
|
|
22
24
|
} from "../extensions/pi-auto.ts";
|
|
23
25
|
|
|
24
26
|
function createHost({ boot = true, cwd = "/tmp/proj-a", sessionId = "sess-1" } = {}) {
|
|
@@ -394,3 +396,92 @@ test("describeSchedule / 备注:recurring 显示每天时刻,once 显示绝
|
|
|
394
396
|
assert.match(rec, /recurring\/on/);
|
|
395
397
|
assert.match(rec, /每天 07:40/);
|
|
396
398
|
});
|
|
399
|
+
|
|
400
|
+
// ---------------------------------------------------------------- 今日补充(auto_today)
|
|
401
|
+
test("todayHash 与 lib.sh 的 pi_plan_today_hash 同口径(sha1(prompt + \\n + dir))", () => {
|
|
402
|
+
// printf '%s\n%s' "只给本项目" "/tmp/proj-a" | sha1sum
|
|
403
|
+
assert.equal(todayHash("只给本项目", "/tmp/proj-a"), "8fbeabd85fa97769ab7e17e501e66488b5ab221c");
|
|
404
|
+
assert.notEqual(todayHash("只给本项目", "/tmp/proj-a"), todayHash("只给本项目", "/tmp/proj-b"));
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
test("checkTodayWrite:本项目直接写;跨项目/广播/覆盖别人要二次确认", () => {
|
|
408
|
+
const today = "2026-12-05";
|
|
409
|
+
const base = { version: 2, today: { date: "", dir: "", prompt: "", hash: "", deliveredTo: [] }, projects: [], schedules: [] };
|
|
410
|
+
assert.equal(checkTodayWrite(base, "/tmp/proj-a", "/tmp/proj-a", "x", today).needsConfirm, false);
|
|
411
|
+
const cross = checkTodayWrite(base, "/tmp/proj-b", "/tmp/proj-a", "x", today);
|
|
412
|
+
assert.equal(cross.needsConfirm, true);
|
|
413
|
+
assert.match(cross.reason, /别的项目/);
|
|
414
|
+
const cast = checkTodayWrite(base, "", "/tmp/proj-a", "x", today);
|
|
415
|
+
assert.equal(cast.needsConfirm, true);
|
|
416
|
+
assert.match(cast.reason, /广播/);
|
|
417
|
+
|
|
418
|
+
const busy = {
|
|
419
|
+
version: 2,
|
|
420
|
+
today: { date: today, dir: "/tmp/proj-b", prompt: "别人的补充", hash: "h", deliveredTo: [] },
|
|
421
|
+
projects: [],
|
|
422
|
+
schedules: [],
|
|
423
|
+
};
|
|
424
|
+
const steal = checkTodayWrite(busy, "/tmp/proj-a", "/tmp/proj-a", "x", today);
|
|
425
|
+
assert.equal(steal.needsConfirm, true, "本项目也要拦:会覆盖别的项目今天的补充");
|
|
426
|
+
assert.match(steal.reason, /覆盖/);
|
|
427
|
+
// 同样内容重复写 = 幂等,不用再确认
|
|
428
|
+
assert.equal(checkTodayWrite(busy, "/tmp/proj-b", "/tmp/proj-a", "别人的补充", today).needsConfirm, false);
|
|
429
|
+
// 跨天的旧补充不算占用
|
|
430
|
+
assert.equal(checkTodayWrite({ ...busy, today: { ...busy.today, date: "2020-01-01" } }, "/tmp/proj-a", "/tmp/proj-a", "x", today).needsConfirm, false);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
test("auto_today:默认作用域=本会话 cwd;跨项目/广播第一次拦截,第二次同参数才生效", async () => {
|
|
434
|
+
const host = createHost({ boot: false, cwd: "/tmp/proj-today" });
|
|
435
|
+
const read = () => JSON.parse(readFileSync(planFile(), "utf8"));
|
|
436
|
+
try {
|
|
437
|
+
await host.boot();
|
|
438
|
+
|
|
439
|
+
const own = await host.callTool("auto_today", { action: "set", text: "本项目的补充" });
|
|
440
|
+
assert.equal(own.isError, undefined);
|
|
441
|
+
let plan = read();
|
|
442
|
+
assert.equal(plan.today.dir, "/tmp/proj-today");
|
|
443
|
+
assert.equal(plan.today.prompt, "本项目的补充");
|
|
444
|
+
assert.equal(plan.today.hash, todayHash("本项目的补充", "/tmp/proj-today"));
|
|
445
|
+
assert.deepEqual(plan.today.deliveredTo, []);
|
|
446
|
+
|
|
447
|
+
// 本项目换文案:不用确认
|
|
448
|
+
assert.equal((await host.callTool("auto_today", { action: "set", text: "本项目第二版" })).isError, undefined);
|
|
449
|
+
|
|
450
|
+
// 跨项目:第一次只拦截,不许落盘
|
|
451
|
+
const cross1 = await host.callTool("auto_today", { action: "set", text: "替别的仓写", dir: "/tmp/proj-other" });
|
|
452
|
+
assert.equal(cross1.isError, true);
|
|
453
|
+
assert.match(cross1.content[0].text, /已拦截/);
|
|
454
|
+
assert.equal(read().today.prompt, "本项目第二版", "拦截时不许写盘");
|
|
455
|
+
|
|
456
|
+
// 第二次同参数:生效
|
|
457
|
+
const cross2 = await host.callTool("auto_today", { action: "set", text: "替别的仓写", dir: "/tmp/proj-other" });
|
|
458
|
+
assert.equal(cross2.isError, undefined);
|
|
459
|
+
assert.equal(read().today.dir, "/tmp/proj-other");
|
|
460
|
+
|
|
461
|
+
// 改一个字就不认那次确认(确认按 scope+全文绑定)
|
|
462
|
+
const other1 = await host.callTool("auto_today", { action: "set", text: "替别的仓写(改一个字)", dir: "/tmp/proj-other" });
|
|
463
|
+
assert.equal(other1.isError, true, "内容变了要重新确认");
|
|
464
|
+
|
|
465
|
+
// 广播同样两次
|
|
466
|
+
const all1 = await host.callTool("auto_today", { action: "set", text: "全体注意", all: true });
|
|
467
|
+
assert.equal(all1.isError, true);
|
|
468
|
+
assert.match(all1.content[0].text, /广播/);
|
|
469
|
+
assert.equal((await host.callTool("auto_today", { action: "set", text: "全体注意", all: true })).isError, undefined);
|
|
470
|
+
plan = read();
|
|
471
|
+
assert.equal(plan.today.dir, "");
|
|
472
|
+
assert.equal(plan.today.prompt, "全体注意");
|
|
473
|
+
|
|
474
|
+
const show = await host.callTool("auto_today", { action: "show" });
|
|
475
|
+
assert.match(show.content[0].text, /广播/);
|
|
476
|
+
assert.match(show.content[0].text, /全体注意/);
|
|
477
|
+
|
|
478
|
+
// clear 清空广播(本项目视角下会覆盖广播 ⇒ 要确认一次),第二次生效
|
|
479
|
+
assert.equal((await host.callTool("auto_today", { action: "clear" })).isError, true);
|
|
480
|
+
assert.equal((await host.callTool("auto_today", { action: "clear" })).isError, undefined);
|
|
481
|
+
assert.equal(read().today.prompt, "");
|
|
482
|
+
assert.match(host.audit(), /today-set/);
|
|
483
|
+
assert.match(host.audit(), /today-guard/);
|
|
484
|
+
} finally {
|
|
485
|
+
host.cleanup();
|
|
486
|
+
}
|
|
487
|
+
});
|
package/test/schedule.test.sh
CHANGED
|
@@ -91,6 +91,23 @@ grep -q 'dry-launch' "$PI_AUTO_LOG_DIR/runner.log" || fail "recurring 到点应
|
|
|
91
91
|
"$PA" sync >/dev/null 2>&1
|
|
92
92
|
[ "$(grep -c '预约到点' "$PI_AUTO_LOG_DIR/runner.log")" = 1 ] || fail "recurring 当天不应重复触发"
|
|
93
93
|
|
|
94
|
+
# 5b) 今日补充作用域:无作用域必须报用法(防单个项目简报广播);-C / --all 落盘正确
|
|
95
|
+
out="$("$PA" plan today "无作用域文本" 2>&1)"; rc=$?
|
|
96
|
+
[ "$rc" = 2 ] || fail "plan today 无作用域应退出 2(实际 $rc)"
|
|
97
|
+
printf '%s' "$out" | grep -q '用法' || fail "应打印用法"
|
|
98
|
+
[ -z "$(jq -r '.today.prompt' "$PI_AUTO_PLAN_FILE")" ] || fail "无作用域不许写盘"
|
|
99
|
+
"$PA" plan today -C "$HOME/repo" "只给 repo" >/dev/null || fail "plan today -C"
|
|
100
|
+
[ "$(jq -r '.today.dir' "$PI_AUTO_PLAN_FILE")" = "$HOME/repo" ] || fail "today.dir 未落盘"
|
|
101
|
+
# shellcheck source=/dev/null
|
|
102
|
+
. "$ROOT/lib.sh"
|
|
103
|
+
[ "$(pi_plan_today_for "$HOME/repo")" = "只给 repo" ] || fail "作用域内应收到"
|
|
104
|
+
[ -z "$(pi_plan_today_for "$HOME/other")" ] || fail "作用域外不许收到"
|
|
105
|
+
"$PA" plan today --all "全体" >/dev/null || fail "plan today --all"
|
|
106
|
+
[ "$(jq -r '.today.dir' "$PI_AUTO_PLAN_FILE")" = "" ] || fail "--all 应清作用域"
|
|
107
|
+
[ "$(pi_plan_today_for "$HOME/other")" = "全体" ] || fail "广播应投给所有项目"
|
|
108
|
+
"$PA" plan today "" >/dev/null || fail "plan today 清空"
|
|
109
|
+
[ -z "$(jq -r '.today.prompt' "$PI_AUTO_PLAN_FILE")" ] || fail "清空后 prompt 应为空"
|
|
110
|
+
|
|
94
111
|
# 6) 临时条目跨天自清:把 until 设成过期 → tick 删除条目并保留长期条目
|
|
95
112
|
jq '.projects += [{"dir":"'"$HOME"'/longterm","enabled":true,"prompt":"长期"},{"dir":"'"$HOME"'/revive","enabled":false,"prompt":"被预约临时打开"}]' \
|
|
96
113
|
"$PI_AUTO_PLAN_FILE" >"$PI_AUTO_PLAN_FILE.t" && mv "$PI_AUTO_PLAN_FILE.t" "$PI_AUTO_PLAN_FILE"
|