teamsmith 0.4.6 → 0.4.8

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.
Files changed (91) hide show
  1. package/README.md +6 -6
  2. package/package.json +1 -1
  3. package/skills/teamsmith/CHANGELOG.md +35 -0
  4. package/skills/teamsmith/SKILL.md +43 -30
  5. package/skills/teamsmith/extension/team-bg.ts +5 -3
  6. package/skills/teamsmith/extension/team-inbox-watch.ts +14 -3
  7. package/skills/teamsmith/extension/team-notify.ts +20 -13
  8. package/skills/teamsmith/extension/team-remind.ts +189 -0
  9. package/skills/teamsmith/extension/team-reply.ts +4 -7
  10. package/skills/teamsmith/references/config.md +11 -12
  11. package/skills/teamsmith/references/migration.md +101 -0
  12. package/skills/teamsmith/references/openspec.md +43 -27
  13. package/skills/teamsmith/references/protocol.md +26 -15
  14. package/skills/teamsmith/references/reminders.md +153 -161
  15. package/skills/teamsmith/references/troubleshooting.md +12 -7
  16. package/skills/teamsmith/references/workflows.md +84 -31
  17. package/skills/teamsmith/scripts/lib/cmd-agents.sh +10 -4
  18. package/skills/teamsmith/scripts/lib/cmd-project.sh +88 -6
  19. package/skills/teamsmith/scripts/lib/cmd-remind.sh +88 -2
  20. package/skills/teamsmith/scripts/lib/cmd-review.sh +61 -35
  21. package/skills/teamsmith/scripts/lib/cmd-watch.sh +48 -11
  22. package/skills/teamsmith/scripts/lib/common.sh +83 -13
  23. package/skills/teamsmith/scripts/panel/README.md +35 -33
  24. package/skills/teamsmith/scripts/panel/panel.js +217 -199
  25. package/skills/teamsmith/scripts/panel/src/App.tsx +221 -66
  26. package/skills/teamsmith/scripts/panel/src/data.ts +71 -15
  27. package/skills/teamsmith/scripts/panel/src/layout.ts +159 -310
  28. package/skills/teamsmith/scripts/panel/src/main.tsx +83 -21
  29. package/skills/teamsmith/scripts/panel/src/project-settings.ts +46 -0
  30. package/skills/teamsmith/scripts/panel/src/strings/en.ts +71 -205
  31. package/skills/teamsmith/scripts/panel/src/strings/index.ts +3 -5
  32. package/skills/teamsmith/scripts/panel/src/strings/zh.ts +72 -204
  33. package/skills/teamsmith/scripts/panel/src/types.ts +23 -4
  34. package/skills/teamsmith/scripts/remind-capability.mjs +261 -0
  35. package/skills/teamsmith/scripts/remind-store.mjs +55 -24
  36. package/skills/teamsmith/scripts/remind-tick.mjs +5 -2
  37. package/skills/teamsmith/scripts/reply-capture.mjs +8 -9
  38. package/skills/teamsmith/scripts/state-authority.mjs +72 -6
  39. package/skills/teamsmith/scripts/strings/commands.tsv +1 -1
  40. package/skills/teamsmith/scripts/team +1 -0
  41. package/skills/teamsmith/templates/AGENTS.section.md.tmpl +18 -8
  42. package/skills/teamsmith/templates/DECISIONS.md.tmpl +2 -2
  43. package/skills/teamsmith/templates/OWNERSHIP.md.tmpl +3 -3
  44. package/skills/teamsmith/templates/PROTOCOL.md.tmpl +19 -14
  45. package/skills/teamsmith/templates/memory-seed.md.tmpl +2 -2
  46. package/skills/teamsmith/templates/pm-prompt.md.tmpl +41 -25
  47. package/skills/teamsmith/templates/report.md.tmpl +33 -0
  48. package/skills/teamsmith/tests/bg-ledger-harness.mjs +217 -33
  49. package/skills/teamsmith/tests/bg-ledger.sh +24 -10
  50. package/skills/teamsmith/tests/checkout-shape-probe.sh +7 -6
  51. package/skills/teamsmith/tests/fixtures/remind-adversarial-delay.ts +27 -0
  52. package/skills/teamsmith/tests/fixtures/remind-tool-provider.ts +65 -0
  53. package/skills/teamsmith/tests/help-surface.sh +78 -12
  54. package/skills/teamsmith/tests/install-shape.sh +7 -7
  55. package/skills/teamsmith/tests/isolation-ledger.sh +6 -2
  56. package/skills/teamsmith/tests/key-replies-ui.sh +64 -5
  57. package/skills/teamsmith/tests/key-replies.sh +307 -106
  58. package/skills/teamsmith/tests/lib/main-team-fingerprint.sh +2 -0
  59. package/skills/teamsmith/tests/lib/model-routing-boundary.sh +255 -90
  60. package/skills/teamsmith/tests/lib/model-routing-expansion.sh +181 -61
  61. package/skills/teamsmith/tests/lib/model-routing-roles.sh +125 -67
  62. package/skills/teamsmith/tests/lib/reply-fixture.sh +14 -9
  63. package/skills/teamsmith/tests/lib/routes-pages.sh +243 -0
  64. package/skills/teamsmith/tests/lib/tmp-root.sh +1 -1
  65. package/skills/teamsmith/tests/loop-inventory.tsv +3 -0
  66. package/skills/teamsmith/tests/panel-b2.sh +91 -31
  67. package/skills/teamsmith/tests/panel-b3-stub.sh +9 -2
  68. package/skills/teamsmith/tests/panel-p21.sh +607 -1162
  69. package/skills/teamsmith/tests/panel-strings.mjs +21 -113
  70. package/skills/teamsmith/tests/remind-adversarial-faults.mjs +190 -0
  71. package/skills/teamsmith/tests/remind-adversarial-faults.sh +78 -0
  72. package/skills/teamsmith/tests/remind-adversarial-real.sh +28 -0
  73. package/skills/teamsmith/tests/remind-cli.sh +108 -3
  74. package/skills/teamsmith/tests/remind-delivery-node.mjs +13 -0
  75. package/skills/teamsmith/tests/remind-delivery.sh +123 -35
  76. package/skills/teamsmith/tests/remind-lifecycle.sh +197 -0
  77. package/skills/teamsmith/tests/remind-pulse.sh +199 -0
  78. package/skills/teamsmith/tests/remind-recovery-real.sh +5 -0
  79. package/skills/teamsmith/tests/remind-recovery-unit.sh +118 -0
  80. package/skills/teamsmith/tests/remind-tool-node.mjs +159 -0
  81. package/skills/teamsmith/tests/remind-tool-real.mjs +801 -0
  82. package/skills/teamsmith/tests/remind-tool-real.sh +180 -0
  83. package/skills/teamsmith/tests/reply-capture-real.mjs +93 -2
  84. package/skills/teamsmith/tests/routes.sh +213 -293
  85. package/skills/teamsmith/tests/section-paths.tsv +19 -17
  86. package/skills/teamsmith/tests/smoke.sh +583 -133
  87. package/skills/teamsmith/tests/state-authority.sh +152 -8
  88. package/skills/teamsmith/tests/task-model-routing.sh +57 -26
  89. package/skills/teamsmith/tests/tmp-hygiene.sh +1 -1
  90. package/skills/teamsmith-init/SKILL.md +5 -5
  91. package/skills/teamsmith/tests/fixtures/delivery-truth-real/__pycache__/judge-second.cpython-313.pyc +0 -0
@@ -74,6 +74,10 @@ if (group === 'content') {
74
74
  check('content 标出补发与来源/任务/key', text.includes('overdue: the patrol queued it late') &&
75
75
  text.includes('source cli') && text.includes('task P423') && text.includes('key check'))
76
76
  check('content 不截断:整条正文长度原样留在消息里', (text.match(new RegExp(MESSAGE.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g')) ?? []).length === 1)
77
+ const large = `${'x'.repeat(64 * 1024)}\n`
78
+ const largeText = MOD.reminderContent(batchFixture({ members: [{ ...batch.members[0], message: large }] }))
79
+ check('content 64 KiB + 末尾换行逐字进入投递消息', Buffer.byteLength(large, 'utf8') === 65537 &&
80
+ largeText.includes(`${large}\n\n---\n`))
77
81
  const details = MOD.reminderDetails(batch)
78
82
  check('details 带批次身份与成员(模型看不见的那一半)', details.batchId === batch.id && details.nonce === 'nonce-abc' &&
79
83
  details.members.join(',') === 'rem-x-1,rem-x-2')
@@ -148,6 +152,15 @@ check('watcher 先写 intent 再发(快照里已有该批次)', (() => {
148
152
  const intentBatch = snapshot().batches.find(b => b.id === first?.message?.details?.batchId)
149
153
  check('watcher 的意图绑定了当前待命代次', intentBatch?.generation === 1, `generation=${intentBatch?.generation}`)
150
154
 
155
+ // A real event with the batch id and nonce but a different frozen member set is not this message.
156
+ // It must not spend the right or clear Standby before the genuine observation arrives.
157
+ await handlers.message_start[0]({ message: { role: 'custom', customType: first.message.customType,
158
+ details: { ...first.message.details, members: [] } } })
159
+ const mismatched = snapshot()
160
+ check('watcher 批次和 nonce 相同但成员集不同 → 拒绝,权利与待命都不动',
161
+ mismatched.batches.find(b => b.id === first.message.details.batchId)?.phase === 'intent' &&
162
+ mismatched.batches.find(b => b.id === first.message.details.batchId)?.right === 'unspent' && mismatched.standby.on === true)
163
+
151
164
  // the observation: Pi's own message_start for that very message
152
165
  await handlers.message_start[0]({ message: { role: 'custom', customType: first.message.customType, details: first.message.details } })
153
166
  const after = snapshot()
@@ -7,11 +7,11 @@
7
7
  #
8
8
  # A 到期入队 不到点不入队;到点的只入一次(重复巡检不加第二条);取消的项不入队;
9
9
  # overdue 的两种形状(到期早于本巡检实例 / 入队滞后超 30s),正常轮询延迟不标 overdue
10
- # B 冻结批次 按入队顺序最多 8 条冻结成一批;已有一批在飞时拒绝再冻(不重复送);
11
- # 5 秒间隔;成员记录带批次 id
10
+ # B 冻结批次 按入队顺序把当前成员全部冻结成一批;已有一批在飞时拒绝再冻(不重复送);
11
+ # 观察后下一批立即可发;成员记录带批次 id
12
12
  # C 观察与待命 匹配的回执 → observed + 花掉唯一 right + 自动退当前代次 + 一条点名全部 id/来源的审计;
13
13
  # 手动已 off → already-off;更新的代次 → 保留并报 ambiguous;重复事件不产生第二次 off;
14
- # nonce/会话不符 → 拒绝且不消耗 right
14
+ # nonce/会话/成员不符(含同长度乱序)→ 拒绝且不消耗 right
15
15
  # D 未送达 queued / intent / failed-unknown 都不清待命;只有真回执才清
16
16
  # E 失败与确认 API 拒绝 → unknown(可见、不重发、attempts 记账);unknown 挡住下一批;
17
17
  # resolve 必须显式 --ack-unknown;确认后成员 resolved、right 烧掉、下一批可以再冻
@@ -25,6 +25,7 @@
25
25
  # --flip=admit-any 入队不看到期时间 → A 的「不到点不入队」必须红
26
26
  # --flip=intent-multi 不看已有一批在飞 → B 的「拒绝再冻」必须红
27
27
  # --flip=observe-nogen 观察不再核对待命代次 → C 的「更新代次必须保留」必须红
28
+ # --flip=members-length 成员只比长度不比顺序 → C 的「同长度乱序必须拒绝」必须红
28
29
  # --flip=off-on-queue 入队就把待命关掉 → D 的「未送达不得清待命」必须红
29
30
  # --flip=unknown-resend unknown 不再挡住下一批 → E 的「unknown 挡住下一批」必须红
30
31
  # 红侧跑在 scratch 副本上(只复制 state-authority.mjs + remind-store.mjs,import 关系与产品相同),
@@ -43,7 +44,7 @@ for arg in "$@"; do
43
44
  case "$arg" in
44
45
  --flip=*)
45
46
  case "${arg#--flip=}" in
46
- admit-any|intent-multi|observe-nogen|off-on-queue|unknown-resend) FLIPS+=("${arg#--flip=}") ;;
47
+ admit-any|intent-multi|observe-nogen|members-length|off-on-queue|unknown-resend|batch-cap|batch-spacing) FLIPS+=("${arg#--flip=}") ;;
47
48
  *) printf 'remind-delivery:不认识的 --flip 值 [%s]\n' "${arg#--flip=}" >&2; exit 2 ;;
48
49
  esac ;;
49
50
  -h|--help) sed -n '2,33p' "$0"; exit 0 ;;
@@ -119,6 +120,9 @@ fs.writeFileSync(file + '.edit', JSON.stringify(snap) + '\n');
119
120
  fs.renameSync(file + '.edit', file);
120
121
  JS
121
122
  jfield() { "$RUNNER" "$ROOT/json-get.mjs" "$1" "$2"; }
123
+ reversed_members() { # <intent.json> → 同长度、逆序的成员 id JSON(不改写持久快照)
124
+ "$RUNNER" -e 'const fs=require("fs");const r=JSON.parse(fs.readFileSync(process.argv[1],"utf8"));process.stdout.write(JSON.stringify(r.batch.members.map(m=>m.id).reverse()))' "$1"
125
+ }
122
126
  snapshot_of() { printf '%s/.pi/team/state/reminders/state.json\n' "$1"; }
123
127
  edit_snapshot() { # <repo> <script-file>
124
128
  "$RUNNER" "$ROOT/snapshot-edit.mjs" "$(snapshot_of "$1")" "$2"
@@ -292,42 +296,58 @@ assert_eq "B 批次绑定了当时的待命状态(off)" "$(jfield "$ROOT/out
292
296
  authority "$D" "$ROOT/out/intent2.txt" remind-intent --session sess-D --pid 111
293
297
  assert_eq "B 已有一批在飞:第二次冻结被拒(wait,不产生第二批)" "$(jfield "$ROOT/out/intent2.txt" 'action')" "wait"
294
298
  assert_eq "B 快照里只有一批" "$(jfield "$SD" 'batches.length')" "1"
295
- # 8 条上限:一个批次最多 8 个成员(第 9 条留到下一批)
299
+ # 九条同批:没有成员数上限;观察结束后新成员立即进入下一批,不等批次间隔。
296
300
  E="$(new_repo E)"; SE="$(snapshot_of "$E")"
297
- for i in 1 2 3 4 5 6 7 8 9; do add_due "$E" "cap member $i" "cap$i" >/dev/null; done
301
+ for i in 1 2 3 4 5 6 7 8 9; do add_due "$E" "uncapped member $i" "uncapped$i" >/dev/null; done
298
302
  tick "$E" "$(iso_shift -10)" "$ROOT/out/tick.txt"
299
303
  authority "$E" "$ROOT/out/intent.txt" remind-intent --session sess-E --pid 222
300
- assert_eq "B 单批最多 8 条" "$(jfield "$ROOT/out/intent.txt" 'batch.members.length')" "8"
301
- assert_eq "B 第 9 条仍是 queued(留给下一批)" "$(jfield "$SE" 'reminders.8.phase')" "queued"
302
- # 5 秒间隔:刚冻结完立刻再冻(先确认上一批已离开 outstanding)
304
+ assert_eq "B 九条全部进入同一批" "$(jfield "$ROOT/out/intent.txt" 'batch.members.length')" "9"
305
+ assert_eq "B 第九条不再滞留 queued" "$(jfield "$SE" 'reminders.8.phase')" "intent"
303
306
  authority "$E" "$ROOT/out/obs.txt" remind-observe --id "$(jfield "$ROOT/out/intent.txt" 'batch.id')" \
304
- --nonce "$(jfield "$ROOT/out/intent.txt" 'batch.nonce')" --session sess-E
307
+ --nonce "$(jfield "$ROOT/out/intent.txt" 'batch.nonce')" --session sess-E \
308
+ --members "$(jfield "$ROOT/out/intent.txt" 'batch.members')"
309
+ add_due "$E" 'immediately eligible after observation' >/dev/null
310
+ tick "$E" "$(iso_shift -10)" "$ROOT/out/tick.txt"
305
311
  authority "$E" "$ROOT/out/intent2.txt" remind-intent --session sess-E --pid 222
306
- assert_eq "B 5 秒内不再提交下一批(spacing)" "$(jfield "$ROOT/out/intent2.txt" 'action')" "spacing"
312
+ assert_eq "B 观察后下一批立即可发(无 spacing)" "$(jfield "$ROOT/out/intent2.txt" 'action')" "send"
313
+ assert_eq "B 新批次就是刚入队的那一条" "$(jfield "$ROOT/out/intent2.txt" 'batch.members.0.message')" 'immediately eligible after observation'
307
314
 
308
315
  # ---------------------------------------------------------------- C. 观察与待命
309
316
  F="$(new_repo F)"; SF="$(snapshot_of "$F")"
310
317
  add_due "$F" 'standby wake' wake >/dev/null
318
+ add_due "$F" 'standby wake two' wake-two >/dev/null
311
319
  authority "$F" "$ROOT/out/sb.txt" standby-on --reason "user away" --json
312
320
  assert_eq "C 待命开了(代次 1)" "$(jfield "$ROOT/out/sb.txt" 'generation')" "1"
313
321
  tick "$F" "$(iso_shift -10)" "$ROOT/out/tick.txt"
314
322
  authority "$F" "$ROOT/out/intent.txt" remind-intent --session sess-F --pid 333
315
323
  FB="$(jfield "$ROOT/out/intent.txt" 'batch.id')"; FN="$(jfield "$ROOT/out/intent.txt" 'batch.nonce')"
316
324
  assert_eq "C 意图绑定当前代次 1" "$(jfield "$ROOT/out/intent.txt" 'batch.generation')" "1"
317
- authority "$F" "$ROOT/out/bad.txt" remind-observe --id "$FB" --nonce "wrong" --session sess-F
325
+ authority "$F" "$ROOT/out/bad.txt" remind-observe --id "$FB" --nonce "wrong" --session sess-F \
326
+ --members "$(jfield "$ROOT/out/intent.txt" 'batch.members')"
318
327
  assert_eq "C nonce 不符:拒绝且不消耗 right" "$(jfield "$ROOT/out/bad.txt" 'refusal')" "nonce-mismatch"
319
328
  assert_eq "C nonce 不符后 right 仍未花" "$(jfield "$SF" 'batches.0.right')" "unspent"
320
- authority "$F" "$ROOT/out/bad.txt" remind-observe --id "$FB" --nonce "$FN" --session other
329
+ authority "$F" "$ROOT/out/bad.txt" remind-observe --id "$FB" --nonce "$FN" --session other \
330
+ --members "$(jfield "$ROOT/out/intent.txt" 'batch.members')"
321
331
  assert_eq "C 会话不符:拒绝" "$(jfield "$ROOT/out/bad.txt" 'refusal')" "session-mismatch"
322
- authority "$F" "$ROOT/out/obs.txt" remind-observe --id "$FB" --nonce "$FN" --session sess-F
332
+ authority "$F" "$ROOT/out/bad.txt" remind-observe --id "$FB" --nonce "$FN" --session sess-F --members '[]'
333
+ assert_eq "C 成员集不符:拒绝且不消耗 right" "$(jfield "$ROOT/out/bad.txt" 'refusal')" "member-mismatch"
334
+ assert_eq "C 成员集不符后 right 仍未花" "$(jfield "$SF" 'batches.0.right')" "unspent"
335
+ authority "$F" "$ROOT/out/bad.txt" remind-observe --id "$FB" --nonce "$FN" --session sess-F \
336
+ --members "$(reversed_members "$ROOT/out/intent.txt")"
337
+ assert_eq "C 同长度乱序成员:拒绝" "$(jfield "$ROOT/out/bad.txt" 'refusal')" "member-mismatch"
338
+ assert_eq "C 同长度乱序成员:right 仍未花" "$(jfield "$SF" 'batches.0.right')" "unspent"
339
+ assert_eq "C 同长度乱序成员:Standby 仍开" "$(jfield "$SF" 'standby.on')" "true"
340
+ authority "$F" "$ROOT/out/obs.txt" remind-observe --id "$FB" --nonce "$FN" --session sess-F \
341
+ --members "$(jfield "$ROOT/out/intent.txt" 'batch.members')"
323
342
  assert_has "C 匹配事件 → observed" "$(cat "$ROOT/out/obs.txt")" '"outcome":"observed"'
324
343
  assert_has "C 观察原子地自动退代次 1" "$(cat "$ROOT/out/obs.txt")" '"standby":"auto-off"'
325
344
  assert_eq "C 待命已关" "$(jfield "$SF" 'standby.on')" "false"
326
345
  assert_eq "C right 已花(wake)" "$(jfield "$SF" 'batches.0.rightSpentBy')" "wake"
327
346
  assert_eq "C 审计一条、点名批次" "$(jfield "$SF" 'audit.-1.batch')" "$FB"
328
- assert_eq "C 审计带每个提醒 id" "$(jfield "$SF" 'audit.-1.ids.length')" "1"
347
+ assert_eq "C 审计带每个提醒 id" "$(jfield "$SF" 'audit.-1.ids.length')" "2"
329
348
  assert_eq "C 审计带来源" "$(jfield "$SF" 'audit.-1.sources.0')" "cli"
330
- authority "$F" "$ROOT/out/obs.txt" remind-observe --id "$FB" --nonce "$FN" --session sess-F
349
+ authority "$F" "$ROOT/out/obs.txt" remind-observe --id "$FB" --nonce "$FN" --session sess-F \
350
+ --members "$(jfield "$ROOT/out/intent.txt" 'batch.members')"
331
351
  assert_has "C 重复事件 → duplicate(不再退待命)" "$(cat "$ROOT/out/obs.txt")" '"outcome":"duplicate"'
332
352
  assert_eq "C 重复事件不产生第二条观察审计" \
333
353
  "$(node -e 'const s=require("'"$SF"'");console.log(s.audit.filter(a=>a.op==="remind-observed").length)')" "1"
@@ -340,7 +360,8 @@ tick "$G" "$(iso_shift -10)" "$ROOT/out/tick.txt"
340
360
  authority "$G" "$ROOT/out/intent.txt" remind-intent --session sess-G --pid 444
341
361
  authority "$G" "$ROOT/out/sb.txt" standby-off >/dev/null
342
362
  authority "$G" "$ROOT/out/obs.txt" remind-observe --id "$(jfield "$ROOT/out/intent.txt" 'batch.id')" \
343
- --nonce "$(jfield "$ROOT/out/intent.txt" 'batch.nonce')" --session sess-G
363
+ --nonce "$(jfield "$ROOT/out/intent.txt" 'batch.nonce')" --session sess-G \
364
+ --members "$(jfield "$ROOT/out/intent.txt" 'batch.members')"
344
365
  assert_has "C 手动已关 → already-off(不虚构自动退出)" "$(cat "$ROOT/out/obs.txt")" '"standby":"already-off"'
345
366
 
346
367
  # 更新的代次:保留,报 ambiguous
@@ -353,7 +374,8 @@ authority "$H" "$ROOT/out/sb.txt" standby-off >/dev/null
353
374
  authority "$H" "$ROOT/out/sb.txt" standby-on --reason "g2" --json >/dev/null
354
375
  assert_eq "C 现在的代次是 2" "$(jfield "$ROOT/out/sb.txt" 'generation')" "2"
355
376
  authority "$H" "$ROOT/out/obs.txt" remind-observe --id "$(jfield "$ROOT/out/intent.txt" 'batch.id')" \
356
- --nonce "$(jfield "$ROOT/out/intent.txt" 'batch.nonce')" --session sess-H
377
+ --nonce "$(jfield "$ROOT/out/intent.txt" 'batch.nonce')" --session sess-H \
378
+ --members "$(jfield "$ROOT/out/intent.txt" 'batch.members')"
357
379
  assert_has "C 更新代次 → 保留并报 generation-ambiguous" "$(cat "$ROOT/out/obs.txt")" '"standby":"generation-ambiguous"'
358
380
  assert_eq "C 更新代次没有被清掉" "$(jfield "$SH" 'standby.on')" "true"
359
381
  assert_eq "C 更新代次仍是 2" "$(jfield "$SH" 'standby.generation')" "2"
@@ -387,11 +409,7 @@ assert_eq "E 成员一起关闭" "$(jfield "$SI" 'reminders.0.phase')" "resolved
387
409
  assert_eq "E right 由确认烧掉" "$(jfield "$SI" 'batches.0.rightSpentBy')" "resolve"
388
410
  assert_eq "E 确认不声称送达(没有 remind-observed)" \
389
411
  "$(node -e 'const s=require("'"$SI"'");console.log(s.audit.filter(a=>a.op==="remind-observed").length)')" "0"
390
- # 确认后下一批可以再冻结(把上一批的 intentAt 放到 5 秒外,不睡)
391
- cat > "$ROOT/script/space.js" <<'JS'
392
- for (const batch of snap.batches) batch.intentAt = iso(Date.now() - 10000);
393
- JS
394
- edit_snapshot "$I" "$ROOT/script/space.js"
412
+ # 确认后下一批可以再冻结,不需要等待或改写上一批的时间。
395
413
  add_due "$I" 'after resolve' >/dev/null
396
414
  tick "$I" "$(iso_shift -10)" "$ROOT/out/tick.txt"
397
415
  authority "$I" "$ROOT/out/intent3.txt" remind-intent --session sess-I --pid 666
@@ -408,7 +426,8 @@ authority "$J" "$ROOT/out/intent.txt" remind-intent --session sess-J --pid 777
408
426
  assert_eq "F 重启后 queued 被冻结成批次" "$(jfield "$ROOT/out/intent.txt" 'action')" "send"
409
427
  assert_has "F 正文仍是原文" "$(cat "$ROOT/out/intent.txt")" 'restart catch-up'
410
428
  authority "$J" "$ROOT/out/obs.txt" remind-observe --id "$(jfield "$ROOT/out/intent.txt" 'batch.id')" \
411
- --nonce "$(jfield "$ROOT/out/intent.txt" 'batch.nonce')" --session sess-J
429
+ --nonce "$(jfield "$ROOT/out/intent.txt" 'batch.nonce')" --session sess-J \
430
+ --members "$(jfield "$ROOT/out/intent.txt" 'batch.members')"
412
431
  tick "$J" "$(iso_shift -10)" "$ROOT/out/tick.txt"
413
432
  assert_eq "F 已送达的不再入队(admitted 为空)" "$(jfield "$ROOT/out/tick.txt" 'admitted.length')" "0"
414
433
  assert_eq "F 已送达记录仍是 observed" "$(jfield "$SJ" 'reminders.0.phase')" "observed"
@@ -523,12 +542,8 @@ JS
523
542
  for i in 1 2 3 4 5 6 7 8 9; do add_due "$repo" "flip member $i" "fm$i" >/dev/null; done
524
543
  run_helper "$helper" "$repo" "$ROOT/out/flip.txt" remind-admit --pulse-started "$(iso_shift -10)"
525
544
  run_helper "$helper" "$repo" "$ROOT/out/f1.txt" remind-intent --session s --pid 1
526
- # Move the first batch out of the 5-second spacing window, so the ONLY thing that could stop the
527
- # second freeze is the outstanding-batch rule the mutation removed.
528
- cat > "$ROOT/script/space3.js" <<'JS'
529
- for (const batch of snap.batches) batch.intentAt = iso(Date.now() - 10000);
530
- JS
531
- edit_snapshot "$repo" "$ROOT/script/space3.js"
545
+ add_due "$repo" 'queued while another batch is in flight' >/dev/null
546
+ run_helper "$helper" "$repo" "$ROOT/out/flip.txt" remind-admit --pulse-started "$(iso_shift -10)"
532
547
  run_helper "$helper" "$repo" "$ROOT/out/f2.txt" remind-intent --session s --pid 1
533
548
  if [ "$(jfield "$ROOT/out/f2.txt" 'action')" = "send" ]; then
534
549
  ok "flip(不看已有一批在飞):同一批被冻了两次 —— B 的「拒绝再冻」会红"
@@ -552,7 +567,8 @@ JS
552
567
  run_helper "$helper" "$repo" "$ROOT/out/f.txt" standby-off
553
568
  run_helper "$helper" "$repo" "$ROOT/out/f.txt" standby-on --reason g2
554
569
  run_helper "$helper" "$repo" "$ROOT/out/f2.txt" remind-observe --id "$(jfield "$ROOT/out/f1.txt" 'batch.id')" \
555
- --nonce "$(jfield "$ROOT/out/f1.txt" 'batch.nonce')" --session s
570
+ --nonce "$(jfield "$ROOT/out/f1.txt" 'batch.nonce')" --session s \
571
+ --members "$(jfield "$ROOT/out/f1.txt" 'batch.members')"
556
572
  if [ "$(jfield "$ROOT/out/f2.txt" 'standby')" = "auto-off" ]; then
557
573
  ok "flip(观察不核对代次):新代次被清掉 —— C 的「更新代次必须保留」会红"
558
574
  else
@@ -560,6 +576,35 @@ JS
560
576
  fi
561
577
  fi
562
578
 
579
+ if arm_selected members-length; then
580
+ helper="$(helper_copy members-length)"
581
+ if mutate "$(dirname "$helper")/remind-store.mjs" \
582
+ "request.members.length !== batch.members.length ||
583
+ request.members.some((id, index) => id !== batch.members[index])" \
584
+ "request.members.length !== batch.members.length"; then
585
+ ok "红侧 members-length:变异已落地(成员只比长度,不再逐项比身份与顺序)"
586
+ else
587
+ bad "红侧 members-length:变异锚点没落地"
588
+ fi
589
+ FLIPS_RUN="${FLIPS_RUN:+$FLIPS_RUN,}members-length"
590
+ repo="$(new_repo FLIP_MEMBERS)"
591
+ add_due "$repo" 'member order one' order-one >/dev/null
592
+ add_due "$repo" 'member order two' order-two >/dev/null
593
+ run_helper "$helper" "$repo" "$ROOT/out/f.txt" standby-on --reason g1
594
+ run_helper "$helper" "$repo" "$ROOT/out/flip.txt" remind-admit --pulse-started "$(iso_shift -10)"
595
+ run_helper "$helper" "$repo" "$ROOT/out/f1.txt" remind-intent --session s --pid 1
596
+ run_helper "$helper" "$repo" "$ROOT/out/f2.txt" remind-observe --id "$(jfield "$ROOT/out/f1.txt" 'batch.id')" \
597
+ --nonce "$(jfield "$ROOT/out/f1.txt" 'batch.nonce')" --session s \
598
+ --members "$(reversed_members "$ROOT/out/f1.txt")"
599
+ if [ "$(jfield "$ROOT/out/f2.txt" 'outcome')" = "observed" ] && \
600
+ [ "$(jfield "$ROOT/out/f2.txt" 'standby')" = "auto-off" ] && \
601
+ [ "$(jfield "$(snapshot_of "$repo")" 'standby.on')" = "false" ]; then
602
+ ok "flip(成员只比长度):同长度乱序被误收并清了 Standby —— C 的乱序拒绝会红;完整比较恢复后拒绝"
603
+ else
604
+ bad "flip(成员只比长度):变异没有误收同长度乱序(红侧无效)"
605
+ fi
606
+ fi
607
+
563
608
  if arm_selected off-on-queue; then
564
609
  helper="$(helper_copy off-on-queue)"
565
610
  mutate "$(dirname "$helper")/remind-store.mjs" \
@@ -592,10 +637,6 @@ JS
592
637
  run_helper "$helper" "$repo" "$ROOT/out/f1.txt" remind-intent --session s --pid 1
593
638
  run_helper "$helper" "$repo" "$ROOT/out/f.txt" remind-fail --id "$(jfield "$ROOT/out/f1.txt" 'batch.id')" \
594
639
  --nonce "$(jfield "$ROOT/out/f1.txt" 'batch.nonce')" --reason "refused"
595
- cat > "$ROOT/script/space2.js" <<'JS'
596
- for (const batch of snap.batches) batch.intentAt = iso(Date.now() - 10000);
597
- JS
598
- edit_snapshot "$repo" "$ROOT/script/space2.js"
599
640
  add_due "$repo" 'flip next batch' >/dev/null
600
641
  run_helper "$helper" "$repo" "$ROOT/out/f.txt" remind-admit --pulse-started "$(iso_shift -10)"
601
642
  run_helper "$helper" "$repo" "$ROOT/out/f2.txt" remind-intent --session s --pid 1
@@ -605,6 +646,53 @@ JS
605
646
  bad "flip(unknown 不再挡住下一批):变异没有改变答案(红侧无效)"
606
647
  fi
607
648
  fi
649
+
650
+ if arm_selected batch-cap; then
651
+ helper="$(helper_copy batch-cap)"
652
+ mutate "$(dirname "$helper")/remind-store.mjs" \
653
+ " .sort((a, b) => ((a.queueSeq ?? 0) - (b.queueSeq ?? 0)) || (a.seq - b.seq));" \
654
+ " .sort((a, b) => ((a.queueSeq ?? 0) - (b.queueSeq ?? 0)) || (a.seq - b.seq))
655
+ .slice(0, 8);"
656
+ ok "红侧 batch-cap:变异已落地(恢复单批 8 条上限)"
657
+ FLIPS_RUN="${FLIPS_RUN:+$FLIPS_RUN,}batch-cap"
658
+ repo="$(new_repo FLIP6)"
659
+ for i in 1 2 3 4 5 6 7 8 9; do add_due "$repo" "flip cap $i" "fc$i" >/dev/null; done
660
+ run_helper "$helper" "$repo" "$ROOT/out/flip.txt" remind-admit --pulse-started "$(iso_shift -10)"
661
+ run_helper "$helper" "$repo" "$ROOT/out/f1.txt" remind-intent --session s --pid 1
662
+ if [ "$(jfield "$ROOT/out/f1.txt" 'batch.members.length')" = "8" ]; then
663
+ ok "flip(恢复单批 8 条上限):第九条被留下 —— B 的「九条同批」会红"
664
+ else
665
+ bad "flip(恢复单批 8 条上限):第九条仍同批,这条红侧是橡皮章"
666
+ fi
667
+ fi
668
+
669
+ if arm_selected batch-spacing; then
670
+ helper="$(helper_copy batch-spacing)"
671
+ mutate "$(dirname "$helper")/remind-store.mjs" \
672
+ " const queued = snapshot.reminders" \
673
+ " const last = snapshot.batches[snapshot.batches.length - 1];
674
+ if (last && Number.isFinite(msOf(last.intentAt)) && nowMs - msOf(last.intentAt) < 5000) {
675
+ return { ok: true, action: 'spacing', now: iso(nowMs), swept };
676
+ }
677
+ const queued = snapshot.reminders"
678
+ ok "红侧 batch-spacing:变异已落地(恢复五秒批次间隔)"
679
+ FLIPS_RUN="${FLIPS_RUN:+$FLIPS_RUN,}batch-spacing"
680
+ repo="$(new_repo FLIP7)"
681
+ add_due "$repo" 'flip spacing first' >/dev/null
682
+ run_helper "$helper" "$repo" "$ROOT/out/flip.txt" remind-admit --pulse-started "$(iso_shift -10)"
683
+ run_helper "$helper" "$repo" "$ROOT/out/f1.txt" remind-intent --session s --pid 1
684
+ run_helper "$helper" "$repo" "$ROOT/out/obs.txt" remind-observe --id "$(jfield "$ROOT/out/f1.txt" 'batch.id')" \
685
+ --nonce "$(jfield "$ROOT/out/f1.txt" 'batch.nonce')" --session s \
686
+ --members "$(jfield "$ROOT/out/f1.txt" 'batch.members')"
687
+ add_due "$repo" 'flip spacing second' >/dev/null
688
+ run_helper "$helper" "$repo" "$ROOT/out/flip.txt" remind-admit --pulse-started "$(iso_shift -10)"
689
+ run_helper "$helper" "$repo" "$ROOT/out/f2.txt" remind-intent --session s --pid 1
690
+ if [ "$(jfield "$ROOT/out/f2.txt" 'action')" = "spacing" ]; then
691
+ ok "flip(恢复五秒批次间隔):下一批被推迟 —— B 的「立即可发」会红"
692
+ else
693
+ bad "flip(恢复五秒批次间隔):下一批仍立即可发,这条红侧是橡皮章"
694
+ fi
695
+ fi
608
696
  }
609
697
  run_flips
610
698
 
@@ -0,0 +1,197 @@
1
+ #!/usr/bin/env bash
2
+ # P443 · Reminder lifecycle: real concurrent creates, cancel/intent order and late events after pruning.
3
+ # All writes stay in private fixture repositories; --flip runs against scratch module copies.
4
+ set -uo pipefail
5
+ HERE="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6
+ SKILL="$(cd -P "$HERE/.." && pwd)"
7
+ TEAM_CLI="$SKILL/scripts/team"
8
+ FLIPS=()
9
+ for arg in "$@"; do
10
+ case "$arg" in
11
+ --flip=key-race|--flip=prune-active) FLIPS+=("${arg#--flip=}") ;;
12
+ -h|--help) sed -n '1,12p' "$0"; exit 0 ;;
13
+ *) printf 'remind-lifecycle:不认识的参数 [%s]\n' "$arg" >&2; exit 2 ;;
14
+ esac
15
+ done
16
+ arm() { [ "${#FLIPS[@]}" -eq 0 ] && return 0; local f; for f in "${FLIPS[@]}"; do [ "$f" = "$1" ] && return 0; done; return 1; }
17
+ # shellcheck source=lib/tmp-root.sh
18
+ . "$HERE/lib/tmp-root.sh"
19
+ # shellcheck source=lib/main-team-fingerprint.sh
20
+ . "$HERE/lib/main-team-fingerprint.sh"
21
+ RUNNER="$(team_js_runner)"
22
+ [ -n "$RUNNER" ] || { printf 'remind-lifecycle:前提缺失:没有 JS 运行时\n' >&2; exit 3; }
23
+ command -v flock >/dev/null 2>&1 || { printf 'remind-lifecycle:前提缺失:没有 flock\n' >&2; exit 3; }
24
+ ROOT="$(tmp_root_create remind-lifecycle)" || exit 3
25
+ trap 'tmp_root_reap_all' EXIT
26
+ mkdir -p "$ROOT/home" "$ROOT/tmp" "$ROOT/fx" "$ROOT/out" "$ROOT/msg" "$ROOT/script"
27
+ export HOME="$ROOT/home"
28
+ unset TEAM_ROOT TEAM_MAIN_ROOT TEAM_PROJECT TEAM_SESSION TEAM_SESSION_FROM TEAM_STATE_DIR TEAM_CONFIG_FILE TMUX TMUX_PANE 2>/dev/null || true
29
+ PASS=0; FAIL=0; FLIPS_RUN=""
30
+ ok() { printf ' \033[32m✓\033[0m %s\n' "$1"; PASS=$((PASS+1)); }
31
+ bad() { printf ' \033[31m✗\033[0m %s\n' "$1"; FAIL=$((FAIL+1)); }
32
+ assert_eq() { [ "$2" = "$3" ] && ok "$1" || bad "$1(期望 [$3],实际 [$2])"; }
33
+ assert_rc() { [ "$1" = "$2" ] && ok "$3" || bad "$3(rc=$1,期望 $2)"; }
34
+ result() { printf '\nflip 臂:%s\n== remind-lifecycle 结果 == ✓%d ✗%d SKIP0\n' "${FLIPS_RUN:-none}" "$PASS" "$FAIL"; [ "$FAIL" -eq 0 ]; }
35
+ printf 'full text — 中文 100%%\nsecond line\n\n' > "$ROOT/msg/full.txt"
36
+ : > "$ROOT/msg/empty"
37
+ new_repo() { local d="$ROOT/fx/$1"; rm -rf "$d"; mkdir -p "$d"; git -C "$d" init -q && git -C "$d" -c user.email=fixture@example.invalid -c user.name=Fixture commit -q --allow-empty -m fixture && printf '%s\n' "$d"; }
38
+ child_env() { local repo="$1"; shift; (cd "$repo" && env -u TEAM_ROOT -u TEAM_MAIN_ROOT -u TEAM_PROJECT -u TEAM_SESSION -u TEAM_SESSION_FROM -u TEAM_STATE_DIR -u TEAM_CONFIG_FILE -u TMUX -u TMUX_PANE HOME="$ROOT/home" TMPDIR="$ROOT/tmp" TEAM_STATE_DIR="$repo/.pi/team/state" "$@"); }
39
+ run_team() { local repo="$1" out="$2"; shift 2; child_env "$repo" bash "$TEAM_CLI" --root "$repo" "$@" </dev/null >"$out" 2>&1; RC=$?; }
40
+ run_team_stdin() { local repo="$1" input="$2" out="$3"; shift 3; child_env "$repo" bash "$TEAM_CLI" --root "$repo" "$@" <"$input" >"$out" 2>&1; RC=$?; }
41
+ authority() { local repo="$1" out="$2"; shift 2; child_env "$repo" "$RUNNER" "$SKILL/scripts/state-authority.mjs" --cli "$repo" "$repo/.pi/team/state" "$@" </dev/null >"$out" 2>&1; RC=$?; }
42
+ tick() { local repo="$1" out="$2" started; started="$(date -u -d "@$(( $(date +%s) - 10 ))" +%Y-%m-%dT%H:%M:%SZ)"; child_env "$repo" "$RUNNER" "$SKILL/scripts/remind-tick.mjs" --root "$repo" --state-dir "$repo/.pi/team/state" --pulse-started "$started" --json </dev/null >"$out" 2>&1; RC=$?; }
43
+ snapshot_of() { printf '%s/.pi/team/state/reminders/state.json\n' "$1"; }
44
+ cat > "$ROOT/json-get.mjs" <<'JS'
45
+ import fs from 'node:fs';
46
+ let value = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
47
+ for (const key of process.argv[3].split('.')) {
48
+ if (value == null) { value = undefined; break; }
49
+ if (Array.isArray(value) && /^-[0-9]+$/.test(key)) value = value[value.length + Number(key)];
50
+ else value = value[key];
51
+ }
52
+ if (value === undefined) process.exit(3);
53
+ process.stdout.write(typeof value === 'string' ? value : JSON.stringify(value));
54
+ JS
55
+ jfield() { "$RUNNER" "$ROOT/json-get.mjs" "$1" "$2"; }
56
+ edit_snapshot() { "$RUNNER" -e '
57
+ import fs from "node:fs"; const file = process.argv[1], script = process.argv[2];
58
+ const snap = JSON.parse(fs.readFileSync(file, "utf8"));
59
+ new Function("snap", "iso", fs.readFileSync(script, "utf8"))(snap, ms => new Date(ms).toISOString());
60
+ fs.writeFileSync(file + ".edit", JSON.stringify(snap) + "\n"); fs.renameSync(file + ".edit", file);
61
+ ' "$(snapshot_of "$1")" "$2"; }
62
+ add() { local repo="$1" msg="$2" key="${3:-}" after="${4:-2h}"; local args=(remind add --after "$after" --message "$msg" --json); [ -n "$key" ] && args+=(--key "$key"); run_team "$repo" "$ROOT/out/add.json" "${args[@]}"; [ "$RC" -eq 0 ] || return 1; jfield "$ROOT/out/add.json" reminder.id; }
63
+ make_due() { printf 'for (const r of snap.reminders) if (r.phase === "scheduled") r.dueAt = iso(Date.now() - 1000);\n' > "$ROOT/script/due.js"; edit_snapshot "$1" "$ROOT/script/due.js"; }
64
+ mutate() { python3 - "$1" "$2" "$3" <<'PY'
65
+ import sys
66
+ p, old, new = sys.argv[1:]
67
+ s = open(p, encoding='utf8').read()
68
+ if old not in s: sys.exit('mutation anchor missing')
69
+ open(p, 'w', encoding='utf8').write(s.replace(old, new, 1))
70
+ PY
71
+ }
72
+ REAL_MAIN="$(main_team_project_root "$SKILL")"
73
+ main_team_snapshot "${REAL_MAIN:-/nonexistent}/.pi/team" > "$ROOT/real-before.txt"
74
+
75
+ # A · eight real writers race one key; conflicting writers cannot replace it; cancellation releases it.
76
+ A="$(new_repo concurrent)" || { bad 'A 夹具仓库建不出来'; result; exit 1; }
77
+ pids=()
78
+ for i in 1 2 3 4 5 6 7 8; do
79
+ (run_team_stdin "$A" "$ROOT/msg/full.txt" "$ROOT/out/a-$i.json" remind add --after 2h --message-file "$ROOT/msg/full.txt" --key shared --json; echo "$RC" > "$ROOT/out/a-$i.rc") & pids+=("$!")
80
+ done
81
+ for p in "${pids[@]}"; do wait "$p"; done
82
+ assert_eq 'A 八个并发创建都成功返回' "$(cat "$ROOT/out"/a-*.rc | sort -u | tr '\n' ' ')" '0 '
83
+ A_ID="$(jfield "$ROOT/out/a-1.json" reminder.id)"; A_DUE="$(jfield "$ROOT/out/a-1.json" reminder.dueAt)"
84
+ assert_eq 'A 同 key 并发只保留一个 ID' "$(for f in "$ROOT/out"/a-*.json; do jfield "$f" reminder.id; done | sort -u | wc -l | tr -d ' ')" '1'
85
+ assert_eq 'A 同 key 并发固定第一次 due' "$(for f in "$ROOT/out"/a-*.json; do jfield "$f" reminder.dueAt; done | sort -u | wc -l | tr -d ' ')" '1'
86
+ assert_eq 'A 快照只有一条活跃记录' "$(jfield "$(snapshot_of "$A")" reminders.length)" '1'
87
+ "$RUNNER" -e 'import fs from "node:fs"; const s=JSON.parse(fs.readFileSync(process.argv[1],"utf8"));fs.writeFileSync(process.argv[2],s.reminders[0].message)' "$(snapshot_of "$A")" "$ROOT/out/a-message.txt"
88
+ cmp -s "$ROOT/msg/full.txt" "$ROOT/out/a-message.txt" && ok 'A 并发创建保留完整正文和结尾换行' || bad 'A 并发创建改变了正文'
89
+ pids=()
90
+ for i in 1 2 3 4; do (run_team "$A" "$ROOT/out/ac-$i.json" remind add --after 2h --message "conflict $i" --key shared --json; echo "$RC" > "$ROOT/out/ac-$i.rc") & pids+=("$!"); done
91
+ for p in "${pids[@]}"; do wait "$p"; done
92
+ assert_eq 'A 并发冲突请求全部拒绝' "$(cat "$ROOT/out"/ac-*.rc | sort -u | tr '\n' ' ')" '1 '
93
+ assert_eq 'A 冲突是 key-conflict' "$(jfield "$ROOT/out/ac-1.json" refusal)" 'key-conflict'
94
+ assert_eq 'A 冲突没有分配新 ID' "$(jfield "$(snapshot_of "$A")" reminders.length)" '1'
95
+ run_team "$A" "$ROOT/out/a-cancel.json" remind cancel "$A_ID" --json
96
+ pids=()
97
+ for i in 1 2 3 4; do (run_team_stdin "$A" "$ROOT/msg/full.txt" "$ROOT/out/ar-$i.json" remind add --after 3h --message-file "$ROOT/msg/full.txt" --key shared --json; echo "$RC" > "$ROOT/out/ar-$i.rc") & pids+=("$!"); done
98
+ for p in "${pids[@]}"; do wait "$p"; done
99
+ A_NEW="$(jfield "$ROOT/out/ar-1.json" reminder.id)"
100
+ assert_eq 'A 取消后的并发 key 复用也只有一个新 ID' "$(for f in "$ROOT/out"/ar-*.json; do jfield "$f" reminder.id; done | sort -u | wc -l | tr -d ' ')" '1'
101
+ [ "$A_NEW" != "$A_ID" ] && ok "A key 复用使用新单调 ID($A_ID → $A_NEW)" || bad 'A key 复用错误返回了旧 ID'
102
+ assert_eq 'A 旧 cancelled 保留、新 scheduled 只有一条' "$(jfield "$(snapshot_of "$A")" reminders.length)" '2'
103
+
104
+ # B · both serializations of cancel versus intent, with whole-batch membership as the oracle.
105
+ B="$(new_repo cancel-intent)"; B1="$(add "$B" member-A)"; B2="$(add "$B" member-B)"; make_due "$B"; tick "$B" "$ROOT/out/b-tick.json"
106
+ run_team "$B" "$ROOT/out/b-cancel.json" remind cancel "$B2" --json
107
+ authority "$B" "$ROOT/out/b-intent.json" remind-intent --session sess-B --pid 10
108
+ assert_eq 'B cancel 先于 intent:B 已取消' "$(jfield "$ROOT/out/b-cancel.json" outcome)" 'cancelled'
109
+ assert_eq 'B cancel 先于 intent:批次只剩 A' "$(jfield "$ROOT/out/b-intent.json" batch.members.length)" '1'
110
+ assert_eq 'B cancel 先于 intent:消息不含 B 正文' "$(jfield "$ROOT/out/b-intent.json" batch.members.0.message)" 'member-A'
111
+ authority "$B" "$ROOT/out/b-obs.json" remind-observe --id "$(jfield "$ROOT/out/b-intent.json" batch.id)" --nonce "$(jfield "$ROOT/out/b-intent.json" batch.nonce)" --session sess-B --members "$(jfield "$ROOT/out/b-intent.json" batch.members)"
112
+ B3="$(add "$B" member-C)"; B4="$(add "$B" member-D)"; make_due "$B"; tick "$B" "$ROOT/out/b-tick2.json"
113
+ authority "$B" "$ROOT/out/b-intent2.json" remind-intent --session sess-B --pid 10
114
+ BB="$(jfield "$ROOT/out/b-intent2.json" batch.id)"
115
+ run_team "$B" "$ROOT/out/b-cancel2.json" remind cancel "$B4" --json
116
+ assert_eq 'B intent 先于 cancel:拒绝撤回并说明 in-flight' "$(jfield "$ROOT/out/b-cancel2.json" refusal)" 'in-flight'
117
+ assert_eq 'B intent 先于 cancel:D 仍在原批次' "$(jfield "$(snapshot_of "$B")" reminders.3.phase)" 'intent'
118
+ assert_eq 'B intent 先于 cancel:批次仍含两个成员' "$(jfield "$(snapshot_of "$B")" batches.1.members.length)" '2'
119
+ assert_eq 'B cancel 拒绝不消耗批次权利' "$(jfield "$(snapshot_of "$B")" batches.1.right)" 'unspent'
120
+
121
+ # C · retention keeps an old unknown and its right; a pruned batch's late event cannot recreate anything.
122
+ fill_history() { # <repo> <keep-unknown 0|1>
123
+ cat > "$ROOT/script/fill-$2.js" <<'JS'
124
+ const oldUnknown = snap.reminders.find(r => r.phase === 'unknown');
125
+ const oldBatch = snap.batches.find(b => b.phase === 'unknown');
126
+ const baseR = oldUnknown ? oldUnknown.seq : 1, baseB = oldBatch ? oldBatch.seq : 1;
127
+ snap.reminders = oldUnknown && 'KEEP' === '1' ? [oldUnknown] : [];
128
+ snap.batches = oldBatch && 'KEEP' === '1' ? [oldBatch] : [];
129
+ for (let i = 1; i <= 1005; i++) {
130
+ const rs = baseR + i, bs = baseB + i, rid = 'rem-' + snap.project.id + '-' + rs, bid = 'bat-' + snap.project.id + '-' + bs;
131
+ const at = iso(Date.now() + i * 60000);
132
+ snap.reminders.push({ id: rid, seq: rs, owner: 'pm', source: 'cli', task: null, key: null, phase: 'observed', createdAt: at,
133
+ request: { form: 'after', value: '2h' }, dueAt: at, message: 'terminal ' + rs, endedAt: at, batchId: bid, observedAt: at });
134
+ snap.batches.push({ id: bid, seq: bs, nonce: 'history-' + bs, phase: 'observed', intentAt: at, generation: 'off',
135
+ target: { session: 'history', pid: 1 }, members: [rid], right: 'spent', rightSpentBy: 'wake', attempts: 1,
136
+ lastError: null, observedAt: at, unknownAt: null, resolvedAt: null, outcome: { standby: 'already-off' } });
137
+ }
138
+ snap.sequences.reminder = baseR + 1006; snap.sequences.batch = baseB + 1006; snap.pruned = 0; snap.audit = [];
139
+ JS
140
+ sed -i "s/'KEEP'/'$2'/" "$ROOT/script/fill-$2.js"
141
+ edit_snapshot "$1" "$ROOT/script/fill-$2.js"
142
+ }
143
+ C="$(new_repo old-unknown)"; CID="$(add "$C" old-unknown)"; make_due "$C"; tick "$C" "$ROOT/out/c-tick.json"
144
+ authority "$C" "$ROOT/out/c-intent.json" remind-intent --session sess-C --pid 20
145
+ CB="$(jfield "$ROOT/out/c-intent.json" batch.id)"; CN="$(jfield "$ROOT/out/c-intent.json" batch.nonce)"
146
+ authority "$C" "$ROOT/out/c-fail.json" remind-fail --id "$CB" --nonce "$CN" --reason controlled-loss
147
+ fill_history "$C" 1
148
+ add "$C" trigger-compaction >/dev/null
149
+ assert_eq 'C 修剪后旧 unknown 仍在' "$(jfield "$(snapshot_of "$C")" reminders.0.phase)" 'unknown'
150
+ assert_eq 'C 旧 unknown 批次和未花权利仍在' "$(jfield "$(snapshot_of "$C")" batches.0.right)" 'unspent'
151
+ assert_eq 'C 终态批次有界且旧活跃批次不计入淘汰' "$(jfield "$(snapshot_of "$C")" batches.length)" '1001'
152
+ authority "$C" "$ROOT/out/c-late.json" remind-observe --id "$CB" --nonce "$CN" --session sess-C --members "$(jfield "$ROOT/out/c-intent.json" batch.members)"
153
+ assert_eq 'C 旧 unknown 的迟到事件仍能正式观察' "$(jfield "$ROOT/out/c-late.json" outcome)" 'observed'
154
+ authority "$C" "$ROOT/out/c-dup.json" remind-observe --id "$CB" --nonce "$CN" --session sess-C --members "$(jfield "$ROOT/out/c-intent.json" batch.members)"
155
+ assert_eq 'C 迟到事件重放只报 duplicate' "$(jfield "$ROOT/out/c-dup.json" outcome)" 'duplicate'
156
+ assert_eq 'C 迟到事件没有合成新提醒' "$(jfield "$(snapshot_of "$C")" sequences.reminder)" '1008'
157
+
158
+ D="$(new_repo pruned-late)"; DID="$(add "$D" prune-this-batch)"; make_due "$D"; tick "$D" "$ROOT/out/d-tick.json"
159
+ authority "$D" "$ROOT/out/d-intent.json" remind-intent --session sess-D --pid 30
160
+ DB="$(jfield "$ROOT/out/d-intent.json" batch.id)"; DN="$(jfield "$ROOT/out/d-intent.json" batch.nonce)"
161
+ authority "$D" "$ROOT/out/d-obs.json" remind-observe --id "$DB" --nonce "$DN" --session sess-D --members "$(jfield "$ROOT/out/d-intent.json" batch.members)"
162
+ fill_history "$D" 0
163
+ add "$D" trigger-pruned-batch >/dev/null
164
+ run_team "$D" "$ROOT/out/d-show.json" remind show "$DID" --json
165
+ assert_eq 'C 被修剪提醒报 history-pruned' "$(jfield "$ROOT/out/d-show.json" refusal)" 'history-pruned'
166
+ authority "$D" "$ROOT/out/d-late.json" remind-observe --id "$DB" --nonce "$DN" --session sess-D --members "$(jfield "$ROOT/out/d-intent.json" batch.members)"
167
+ assert_eq 'C 被修剪批次的迟到事件报 history-pruned' "$(jfield "$ROOT/out/d-late.json" refusal)" 'history-pruned'
168
+ assert_eq 'C 被修剪批次没有复活' "$(jfield "$(snapshot_of "$D")" batches.length)" '1000'
169
+ assert_eq 'C 迟到事件没有新增提醒或批次序号' "$(jfield "$(snapshot_of "$D")" sequences.reminder)" '1008'
170
+ assert_eq 'C 新写入的提醒仍然有效' "$(jfield "$(snapshot_of "$D")" reminders.-1.phase)" 'scheduled'
171
+
172
+ # D · red sides run the same authority entry against one-defect scratch copies.
173
+ helper_copy() { local d="$ROOT/flip/$1"; rm -rf "$d"; mkdir -p "$d"; cp "$SKILL/scripts/state-authority.mjs" "$d/"; cp "$SKILL/scripts/remind-store.mjs" "$d/"; printf '%s\n' "$d/state-authority.mjs"; }
174
+ run_helper() { local helper="$1" repo="$2" input="$3" out="$4"; shift 4; child_env "$repo" "$RUNNER" "$helper" --cli "$repo" "$repo/.pi/team/state" "$@" <"$input" >"$out" 2>&1; RC=$?; }
175
+ if arm key-race; then
176
+ FLIPS_RUN="${FLIPS_RUN:+$FLIPS_RUN,}key-race"; H="$(helper_copy key-race)"
177
+ mutate "$(dirname "$H")/remind-store.mjs" ' if (key.key) {' ' if (false) {'
178
+ R="$(new_repo flip-key)"; run_helper "$H" "$R" "$ROOT/msg/full.txt" "$ROOT/out/fk1.json" remind-add --after 2h --key shared --json
179
+ run_helper "$H" "$R" "$ROOT/msg/full.txt" "$ROOT/out/fk2.json" remind-add --after 2h --key shared --json
180
+ [ "$(jfield "$ROOT/out/fk1.json" reminder.id)" != "$(jfield "$ROOT/out/fk2.json" reminder.id)" ] \
181
+ && ok 'flip(去掉活跃 key 检查):同 key 分出两个 ID —— A 的并发断言会红' || bad 'flip key-race 没有改变结果'
182
+ fi
183
+ if arm prune-active; then
184
+ FLIPS_RUN="${FLIPS_RUN:+$FLIPS_RUN,}prune-active"; H="$(helper_copy prune-active)"
185
+ mutate "$(dirname "$H")/remind-store.mjs" 'function pruneHistory(snapshot, nowMs) {' $'function pruneHistory(snapshot, nowMs) {\n snapshot.reminders = snapshot.reminders.filter(record => record.phase !== "unknown");'
186
+ R="$(new_repo flip-prune)"; RID="$(add "$R" old-unknown)"; make_due "$R"; tick "$R" "$ROOT/out/fp-tick.json"
187
+ run_helper "$H" "$R" "$ROOT/msg/empty" "$ROOT/out/fp-intent.json" remind-intent --session sess-F --pid 40
188
+ run_helper "$H" "$R" "$ROOT/msg/empty" "$ROOT/out/fp-fail.json" remind-fail --id "$(jfield "$ROOT/out/fp-intent.json" batch.id)" --nonce "$(jfield "$ROOT/out/fp-intent.json" batch.nonce)" --reason lost
189
+ run_helper "$H" "$R" "$ROOT/msg/full.txt" "$ROOT/out/fp-add.json" remind-add --after 2h --json
190
+ [ "$(jfield "$(snapshot_of "$R")" reminders.0.phase)" != 'unknown' ] \
191
+ && ok 'flip(修剪丢掉 unknown):旧活跃记录被删掉 —— C 的保留断言会红' || bad 'flip prune-active 没有删掉 unknown'
192
+ fi
193
+
194
+ main_team_snapshot "${REAL_MAIN:-/nonexistent}/.pi/team" > "$ROOT/real-after.txt"
195
+ VIOL="$(main_team_violations "$ROOT/real-before.txt" "$ROOT/real-after.txt")"
196
+ [ -z "$VIOL" ] && ok 'E 主项目 .pi/team 非白名单路径零变化' || bad "E 主项目被夹具写入:$VIOL"
197
+ result