pi-auto-unattended 0.8.0 → 0.8.2

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 CHANGED
@@ -1,5 +1,20 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.8.2 (2026-12-05)
4
+
5
+ - 修噪音:会话自己 `off` 之后(当天已有 `retired`),手开会话分支不再弹「上下文 ≥ 阈值,该交接」的
6
+ `handoff-notify`(e2e 实测在收工瞬间弹了一句)。回归 33 → 34 条。
7
+ - 真会话 e2e(`PI_AUTO_HANDOFF_AT=1`,`env` 前缀传参):audit 依次
8
+ `session-start → continue → handoff-remind(percent 1.5324, stage 2) → off → session-end(reason=new) → session-start`,
9
+ 新会话接 kickoff 后立刻 off,无乒乓。
10
+
11
+ ## 0.8.1 (2026-12-05)
12
+
13
+ - 交接**冷却**:10 分钟内不重复发交接指令(`PI_AUTO_HANDOFF_COOLDOWN_MS` 可覆盖,0 = 关)。
14
+ 防的是「阈值配得比新会话基线还低」时的乒乓(交接出来的新会话一 settle 又超线 ⇒ 无限换会话);
15
+ 冷却期内按普通续轮走,audit `handoff-cooldown`。`handoffCoolingDown` 纯函数 + 4 条断言。
16
+ - 网络闸门(同日的 limbus-auto 交付,见那边仓):`tools/net-guard.py` 批前 `--check` + 批中 `--watch`。
17
+
3
18
  ## 0.8.0 (2026-12-05)
4
19
 
5
20
  - 修语义偏差(用户 2026-12-05):「off = 服务今天不会再拉起本目录」把 agent 的开关说成了**目录**级,
package/README.md CHANGED
@@ -162,7 +162,8 @@ auto_schedule({ action: "cancel", id: "sch-…" }) // 取消(id:"all"
162
162
  读数未知(刚压缩完)就什么都不做。回落到「阈值 −5」以下自动重新武装(audit `handoff-rearm`)。
163
163
  - `PI_AUTO_HANDOFF=off` 关掉;`remind`(默认)由模型写 brief,`auto` 由插件写 brief(要求 settings 里已装
164
164
  `pi-agent-handoff`,否则自动退回 `remind`)。
165
- - audit 关键字:`handoff-notify` / `handoff-remind` / `handoff-rearm` / `handoff-auto-pending` / `handoff-auto`。
165
+ - 冷却:10 分钟内不重复发交接(`PI_AUTO_HANDOFF_COOLDOWN_MS=0` 关掉)——防阈值低于新会话基线时的乒乓。
166
+ - audit 关键字:`handoff-notify` / `handoff-remind` / `handoff-rearm` / `handoff-cooldown` / `handoff-auto-pending` / `handoff-auto`。
166
167
  - 边界:交接由 `pi-agent-handoff` 完成会话替换(TUI-only);opencode 形态的 `--mode json` 会话不会被交接。
167
168
 
168
169
  ## 给 agent 的入口(三):`auto_timer` / `auto_bg`(等东西,但不空转)
@@ -43,6 +43,8 @@ export type AutoState = {
43
43
  handoffReminders?: number;
44
44
  /** 观测到的最高上下文百分比(审计用,便于事后核「为什么这时交接」)。 */
45
45
  contextPercentMax?: number | null;
46
+ /** 上次真正发交接指令的时刻(ISO):防「交接出来的新会话基线就超线」时的乒乓。 */
47
+ lastHandoffAt?: string | null;
46
48
  };
47
49
 
48
50
  const STATUS_KEY = "pi-auto";
@@ -748,6 +750,18 @@ export function handoffMode(env: NodeJS.ProcessEnv = process.env): HandoffMode {
748
750
 
749
751
  /** 百分比掉回「阈值 − 5」以下才重新武装(压缩后不该一直催交接)。 */
750
752
  export const HANDOFF_REARM_DROP = 5;
753
+ /**
754
+ * 交接冷却:10 分钟内不重复交接。防的是「阈值配得比新会话基线还低」时的乒乓
755
+ * (每次交接出来的新会话一 settle 就又超线 ⇒ 无限换会话)。
756
+ */
757
+ export const HANDOFF_COOLDOWN_MS = 10 * 60_000;
758
+
759
+ export function handoffCoolingDown(lastHandoffAt: string | null | undefined, now = Date.now(), cooldownMs = HANDOFF_COOLDOWN_MS): boolean {
760
+ if (!lastHandoffAt) return false;
761
+ const at = Date.parse(lastHandoffAt);
762
+ if (!Number.isFinite(at)) return false;
763
+ return now - at < cooldownMs;
764
+ }
751
765
 
752
766
  /** 本轮要不要发交接提醒:0 = 不用,1 = 首次提醒,2 = 提醒过仍未交接(升级)。 */
753
767
  export function handoffStage(percent: number | null, threshold: number, reminded: number): 0 | 1 | 2 {
@@ -1578,7 +1592,8 @@ export default function (pi: ExtensionAPI) {
1578
1592
  }
1579
1593
  if (!now.keepAlive) {
1580
1594
  // 手开会话:到线只提一句(不注入消息、不触发新一轮),同一条线只提醒一次
1581
- if (hMode !== "off" && pct !== null && pct >= hAt && (now.handoffStage ?? 0) === 0) {
1595
+ // 已经自己的收工的不再提醒(off 之后 keepAlive=false,别在收工后弹一句「该交接了」)
1596
+ if (hMode !== "off" && !retiredToday(now.cwd) && pct !== null && pct >= hAt && (now.handoffStage ?? 0) === 0) {
1582
1597
  touch(ctx, { handoffStage: 1, handoffReminders: 1 });
1583
1598
  try {
1584
1599
  ctx.ui?.notify?.(
@@ -1606,7 +1621,12 @@ export default function (pi: ExtensionAPI) {
1606
1621
  audit({ kind: "handoff-rearm", percent: pct, threshold: hAt, cwd: now.cwd });
1607
1622
  }
1608
1623
  const stage = handoffStage(pct, hAt, now.handoffReminders ?? 0);
1609
- if (stage > 0) {
1624
+ const cooldownRaw = Number(process.env.PI_AUTO_HANDOFF_COOLDOWN_MS ?? "");
1625
+ const cooldown = Number.isFinite(cooldownRaw) && cooldownRaw >= 0 ? cooldownRaw : HANDOFF_COOLDOWN_MS;
1626
+ if (stage > 0 && handoffCoolingDown(now.lastHandoffAt, Date.now(), cooldown)) {
1627
+ // 刚交接完(同一进程换会话)就别再交一次:本轮按普通续轮走
1628
+ audit({ kind: "handoff-cooldown", percent: pct, threshold: hAt, lastHandoffAt: now.lastHandoffAt, cwd: now.cwd });
1629
+ } else if (stage > 0) {
1610
1630
  const round = now.rounds + 1;
1611
1631
  const auto = hMode === "auto" && handoffExtensionInstalled();
1612
1632
  const next = touch(ctx, {
@@ -1615,6 +1635,7 @@ export default function (pi: ExtensionAPI) {
1615
1635
  handoffStage: stage,
1616
1636
  handoffReminders: (now.handoffReminders ?? 0) + 1,
1617
1637
  handoffPercent: pct,
1638
+ lastHandoffAt: new Date().toISOString(),
1618
1639
  });
1619
1640
  nudgeAttempts = 0;
1620
1641
  audit({ kind: "handoff-remind", percent: pct, threshold: hAt, stage, mode: hMode, auto, round, cwd: next.cwd });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-auto-unattended",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "Unattended Pi sessions: tool-side extensions (auto_switch keep-alive, auto_schedule timed runs, auto_today project-scoped daily brief, auto_timer/auto_bg wait-without-blocking, context-threshold handoff at idle) 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",
@@ -10,6 +10,7 @@ import register, {
10
10
  bgIndexPath,
11
11
  HANDOFF_DEFAULT_AT,
12
12
  handoffContinuationText,
13
+ handoffCoolingDown,
13
14
  handoffMode,
14
15
  handoffStage,
15
16
  handoffThreshold,
@@ -118,6 +119,7 @@ function createHost({ boot = true, cwd = "/tmp/proj-a", sessionId = "sess-1" } =
118
119
  delete process.env.PI_AUTO_BG_POLL_MS;
119
120
  delete process.env.PI_AUTO_HANDOFF;
120
121
  delete process.env.PI_AUTO_HANDOFF_AT;
122
+ delete process.env.PI_AUTO_HANDOFF_COOLDOWN_MS;
121
123
  delete process.env.PI_BOOT_SESSION_ID;
122
124
  delete process.env.PI_RUN_DIR;
123
125
  rmSync(dir, { recursive: true, force: true });
@@ -679,6 +681,10 @@ test("handoff 判据:阈值默认 35、可环境覆盖;stage 只到线才提
679
681
  assert.match(handoffContinuationText(3, 36, 35, 1), /交接线/);
680
682
  assert.match(handoffContinuationText(3, 36, 35, 1), /36\.0%/);
681
683
  assert.match(handoffContinuationText(3, 36, 35, 2), /升级/);
684
+ assert.equal(handoffCoolingDown(null), false);
685
+ assert.equal(handoffCoolingDown(new Date().toISOString()), true, "刚交接完在冷却里");
686
+ assert.equal(handoffCoolingDown(new Date(Date.now() - 3_600_000).toISOString()), false, "一小时后不再冷却");
687
+ assert.equal(handoffCoolingDown("不是时间"), false);
682
688
  });
683
689
 
684
690
  test("mechanicalBrief:只写仓内可核实事实(指针/提交/工作树),不编状态", () => {
@@ -853,3 +859,41 @@ test("收工标记绑的是会话+工单(不是目录):写 sessionId/scope
853
859
  host.cleanup();
854
860
  }
855
861
  });
862
+
863
+ test("交接冷却:刚交接完同一进程换会话不再乒乓(阈值低于新会话基线也不会无限换)", async () => {
864
+ const host = createHost({ boot: true });
865
+ try {
866
+ process.env.PI_AUTO_HANDOFF_COOLDOWN_MS = "600000";
867
+ await host.boot();
868
+ host.setPercent(36);
869
+ const first = await host.settle("completed");
870
+ assert.match(first.entries.at(-1).content, /交接线/);
871
+ assert.ok(host.state().lastHandoffAt, "要记下交接时刻");
872
+
873
+ // 交接出来的新会话:基线仍超线 ⇒ 冷却期内只走普通续轮,不再要求交接
874
+ const second = await host.settle("completed");
875
+ assert.match(second.entries.at(-1).content, /续轮/);
876
+ assert.doesNotMatch(second.entries.at(-1).content, /交接线/);
877
+ assert.match(host.audit(), /handoff-cooldown/);
878
+
879
+ // 冷却可以设 0(自测/调试口径)⇒ 立刻又能交接
880
+ process.env.PI_AUTO_HANDOFF_COOLDOWN_MS = "0";
881
+ const third = await host.settle("completed");
882
+ assert.match(third.entries.at(-1).content, /交接线/);
883
+ } finally {
884
+ host.cleanup();
885
+ }
886
+ });
887
+
888
+ test("收工后不再弹交接提醒(retired 当天 ⇒ 手开会话分支也不 notify)", async () => {
889
+ const host = createHost({ boot: false });
890
+ try {
891
+ await host.boot();
892
+ retire(host.ctx.cwd, "agent off", { sessionId: "sess-1", boot: true });
893
+ host.setPercent(60);
894
+ assert.equal(await host.settle("completed"), undefined);
895
+ assert.doesNotMatch(host.audit(), /handoff-notify/);
896
+ } finally {
897
+ host.cleanup();
898
+ }
899
+ });