@zhushanwen/subagent-core 0.5.1 → 0.6.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.
Files changed (68) hide show
  1. package/dist/execution/engine/engines/zcode/reader.d.cts +1 -1
  2. package/dist/execution/engine/engines/zcode/reader.d.ts +1 -1
  3. package/dist/index.cjs +1122 -452
  4. package/dist/index.d.cts +188 -5
  5. package/dist/index.d.ts +188 -5
  6. package/dist/index.js +1133 -463
  7. package/dist/{types-Dv4QhSJ_.d.cts → types-C3fE3R9x.d.cts} +65 -0
  8. package/dist/{types-Dv4QhSJ_.d.ts → types-C3fE3R9x.d.ts} +65 -0
  9. package/dist.bundle/index.cjs +34933 -0
  10. package/package.json +2 -2
  11. package/src/execution/__tests__/agent-registry.test.ts +1 -1
  12. package/src/execution/__tests__/alive-store.test.ts +1 -1
  13. package/src/execution/__tests__/collect-budget.test.ts +291 -0
  14. package/src/execution/__tests__/collect-coordinator-service.test.ts +327 -0
  15. package/src/execution/__tests__/collect-coordinator.test.ts +371 -0
  16. package/src/execution/__tests__/collect-mixed-dispatch.test.ts +262 -0
  17. package/src/execution/__tests__/config-collect-sync.test.ts +120 -0
  18. package/src/execution/__tests__/config.test.ts +1 -1
  19. package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +1 -1
  20. package/src/execution/__tests__/finalized-marker.test.ts +1 -1
  21. package/src/execution/__tests__/gc-timer.test.ts +1 -1
  22. package/src/execution/__tests__/manifest-store-tmp-recovery.test.ts +1 -1
  23. package/src/execution/__tests__/notify-batch.test.ts +500 -0
  24. package/src/execution/__tests__/notify-ledger.test.ts +27 -0
  25. package/src/execution/__tests__/pi-invocation.test.ts +34 -2
  26. package/src/execution/__tests__/record-entry-collect.test.ts +102 -0
  27. package/src/execution/__tests__/relay-env.test.ts +12 -1
  28. package/src/execution/__tests__/session-file-gc.test.ts +1 -1
  29. package/src/execution/__tests__/spawn-event-adapter.test.ts +2 -2
  30. package/src/execution/__tests__/start-collect-guard.test.ts +255 -0
  31. package/src/execution/__tests__/start-sync-model-guard.test.ts +1 -1
  32. package/src/execution/__tests__/subagent-actions-core.test.ts +6 -0
  33. package/src/execution/__tests__/subagent-service-multiproc-guard.test.ts +13 -0
  34. package/src/execution/__tests__/sync-collect-recovery.test.ts +1117 -0
  35. package/src/execution/__tests__/tombstone-store.test.ts +1 -1
  36. package/src/execution/__tests__/worktree-git-ops.test.ts +1 -1
  37. package/src/execution/__tests__/worktree-reconcile-aging.test.ts +2 -2
  38. package/src/execution/__tests__/worktree-reconcile.integration.test.ts +1 -1
  39. package/src/execution/__tests__/worktree-registry.test.ts +1 -1
  40. package/src/execution/collect-coordinator.ts +200 -0
  41. package/src/execution/config.ts +77 -5
  42. package/src/execution/engine/__tests__/common/event-journal.test.ts +1 -1
  43. package/src/execution/engine/__tests__/common/pool-manager.test.ts +2 -2
  44. package/src/execution/engine/__tests__/conformance/contract.read-degradation.test.ts +1 -1
  45. package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +1 -1
  46. package/src/execution/engine/engines/zcode/__tests__/appserver-launcher.test.ts +1 -1
  47. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-degrade.test.ts +1 -1
  48. package/src/execution/execution-record.ts +7 -0
  49. package/src/execution/finalize-record.ts +15 -11
  50. package/src/execution/notifier.ts +234 -25
  51. package/src/execution/notify-host.ts +21 -1
  52. package/src/execution/record-entry.ts +16 -0
  53. package/src/execution/record-store.ts +161 -30
  54. package/src/execution/subagent-actions-core.ts +62 -6
  55. package/src/execution/subagent-service.ts +416 -7
  56. package/src/execution/sync-rebuild.ts +87 -0
  57. package/src/execution/types.ts +63 -0
  58. package/src/index.ts +1 -1
  59. package/src/orchestration/__tests__/config-loader.test.ts +1 -1
  60. package/src/orchestration/__tests__/file-run-store-prune.test.ts +1 -1
  61. package/src/orchestration/__tests__/file-run-store-throttle.test.ts +1 -1
  62. package/src/orchestration/__tests__/review-fix-loop-scriptpath-failfast.test.ts +1 -1
  63. package/src/orchestration/__tests__/script-generate.test.ts +5 -5
  64. package/src/orchestration/__tests__/skill-discovery.test.ts +2 -2
  65. package/src/orchestration/__tests__/workflow-files.test.ts +10 -10
  66. package/src/orchestration/__tests__/workflow-script-registry-impl.test.ts +1 -1
  67. package/src/shared/__tests__/atomic-write.test.ts +1 -1
  68. package/src/shared/__tests__/resource-discovery-host-roots.test.ts +5 -5
@@ -421,6 +421,14 @@ interface ExecutionRecord {
421
421
  journalPath?: string;
422
422
  poolKey: string;
423
423
  };
424
+ /**
425
+ * 同步收集模式标记(subagent-sync-collect 设计 §3.1.3,U1 foundation 契约)。
426
+ * 创建时确定不可变;undefined = async(缺省语义,旧记录零迁移)。持久化经
427
+ * subagent-record entry(record-entry.ts 序列化白名单,entry 唯一写点)。
428
+ * 消费方:U2 collectCoordinator 路由(sync→批缓冲)、U5 E1 重建投影。
429
+ * U2 接线点:service.createRecordForMode 从 ExecuteOptions.collect 读入 identity。
430
+ */
431
+ readonly collectMode?: "sync";
424
432
  status: ExecutionStatus;
425
433
  /** L2 关闭原因子枚举(仅 status="closed" 时有意义)。表达「为什么关闭」。
426
434
  * 由 tryTransition(record, "closed", reason) 写入;投影层按需派生对外语义。
@@ -432,6 +440,13 @@ interface ExecutionRecord {
432
440
  * record 无此字段,投影层按 projectOutcome 兜底(closed-legacy 语义)。
433
441
  */
434
442
  outcome?: ExecutionOutcome;
443
+ /**
444
+ * 离开批的终局标记(subagent-sync-collect 设计 §3.1.3,U1 foundation 契约)。
445
+ * 两出口统一落标:① 批闭合 flush 写账成功后;② E9 dispose 逐条转 async 写账后
446
+ * (均 appendEntry 持久化,U3/U5 写点)。undefined = 未离开批 / 旧记录零迁移。
447
+ * 消费方:U5 E1 重建扫描只收「collectMode=sync 且无本标记」的成员(防双重通知)。
448
+ */
449
+ batchFinalized?: boolean;
435
450
  /** 完整执行内容,按 turn 组织。createRecord 初始化为 [空 turn]。 */
436
451
  turns: Turn[];
437
452
  /** turn 计数(= turns.filter(closed).length,冗余存储供投影直接读)。 */
@@ -600,6 +615,17 @@ interface ExecuteOptions {
600
615
  * undefined/false = 一次性模式(默认,行为完全不变)。service.execute 透传到 createRecordForMode。
601
616
  */
602
617
  conversation?: boolean;
618
+ /**
619
+ * 同步收集模式(subagent-sync-collect 设计 §3.1.3,U1 foundation 契约)。
620
+ * undefined = config collectSync.default(缺省 "async",新 session 生效)。
621
+ * schema 层枚举限 "async"|"sync";运行时宽收 string 与 engine 字段同风格
622
+ * (非法值 ≠ "sync" 按 async 处理)。
623
+ * "sync" + conversation:true 组合在 startHandler E4 校验即拒
624
+ * (immediate throw,不产生半启动 record)。
625
+ * U2 接线点:service.createRecordForMode 读入 createRecord identity.collectMode
626
+ * (U1 打通类型与 startHandler 透传,record 落点归 U2)。
627
+ */
628
+ collect?: string;
603
629
  /**
604
630
  * 空闲超时毫秒数(仅 conversation 模式有意义)。覆盖默认 5min idle timeout。
605
631
  * 优先级:参数 > env XYZ_SUBAGENT_IDLE_TIMEOUT_MS > 默认 300000ms。
@@ -681,6 +707,16 @@ interface BgResponse {
681
707
  * 值语义由 U2(execution/notify-ledger.ts)兑现。
682
708
  */
683
709
  notifyContract: "ledger+at-least-once";
710
+ /**
711
+ * 同步收集登记回显段(subagent-sync-collect 设计 §3.1.1 交互样例,U1 foundation)。
712
+ * 仅 resolved 模式为 sync 时附带(async 响应字节零变化,G3):mode = 生效模式;
713
+ * pendingSyncCount = 当前未闭合批的 sync 成员总数(含本条;跨轮派发续累不重置,
714
+ * 与 D2 隐式批一致)。
715
+ */
716
+ collect?: {
717
+ mode: "sync";
718
+ pendingSyncCount: number;
719
+ };
684
720
  }
685
721
  /** list 的内层响应(挂在 SubagentToolResult.listResponse)。 */
686
722
  interface ListResponse {
@@ -850,6 +886,30 @@ interface SubagentRecord {
850
886
  journalPath?: string;
851
887
  poolKey: string;
852
888
  };
889
+ /**
890
+ * 同步收集模式标记(与 ExecutionRecord.collectMode 同源投影/重建,U1 foundation)。
891
+ * 缺省(存量 record)= async 投影,消费方零迁移。
892
+ */
893
+ collectMode?: "sync";
894
+ /**
895
+ * 离开批终局标记(与 ExecutionRecord.batchFinalized 同源投影/重建,U1 foundation)。
896
+ * 缺省 = 未离开批;U5 E1 重建扫描据此排除已离场成员。
897
+ */
898
+ batchFinalized?: boolean;
899
+ }
900
+ /**
901
+ * 同步收集(sync collect)配置节类型(subagent-sync-collect 设计 §3.1.3,U1 foundation)。
902
+ * 权威默认值在 config.ts DEFAULT_COLLECT_SYNC;坏值 sanitize 回默认不炸启动(E5,
903
+ * 与 maxConcurrent 同判)。类型定义于 types.ts(避免 config → types 反向依赖成环),
904
+ * config.ts re-export。
905
+ */
906
+ interface CollectSyncConfig {
907
+ /** start 未显式传 collect 时的缺省模式。新 session 生效(与 engine 配置时机一致)。 */
908
+ default: "async" | "sync";
909
+ /** 批通知单条目结果正文预算(字符):超出截断并接 session_read 指针行。flush 时热读。 */
910
+ perItemChars: number;
911
+ /** 批通知结果正文总量预算(字符):Σ 超限时统一收紧 effectivePerItem(U4 算法)。flush 时热读。 */
912
+ totalChars: number;
853
913
  }
854
914
  /**
855
915
  * 全局配置(~/.pi/agent/subagents/config.json)。
@@ -870,6 +930,11 @@ interface SubagentsGlobalConfig {
870
930
  engineRouting?: {
871
931
  strict: boolean;
872
932
  };
933
+ /**
934
+ * 同步收集配置节(subagent-sync-collect 设计 §3.1.3,U1 foundation)。
935
+ * 整节缺省 = DEFAULT_COLLECT_SYNC(config.ts);逐字段 sanitize 回默认(E5)。
936
+ */
937
+ collectSync?: CollectSyncConfig;
873
938
  }
874
939
  /**
875
940
  * Record 的只读视图。store.snapshot() 返回。
@@ -421,6 +421,14 @@ interface ExecutionRecord {
421
421
  journalPath?: string;
422
422
  poolKey: string;
423
423
  };
424
+ /**
425
+ * 同步收集模式标记(subagent-sync-collect 设计 §3.1.3,U1 foundation 契约)。
426
+ * 创建时确定不可变;undefined = async(缺省语义,旧记录零迁移)。持久化经
427
+ * subagent-record entry(record-entry.ts 序列化白名单,entry 唯一写点)。
428
+ * 消费方:U2 collectCoordinator 路由(sync→批缓冲)、U5 E1 重建投影。
429
+ * U2 接线点:service.createRecordForMode 从 ExecuteOptions.collect 读入 identity。
430
+ */
431
+ readonly collectMode?: "sync";
424
432
  status: ExecutionStatus;
425
433
  /** L2 关闭原因子枚举(仅 status="closed" 时有意义)。表达「为什么关闭」。
426
434
  * 由 tryTransition(record, "closed", reason) 写入;投影层按需派生对外语义。
@@ -432,6 +440,13 @@ interface ExecutionRecord {
432
440
  * record 无此字段,投影层按 projectOutcome 兜底(closed-legacy 语义)。
433
441
  */
434
442
  outcome?: ExecutionOutcome;
443
+ /**
444
+ * 离开批的终局标记(subagent-sync-collect 设计 §3.1.3,U1 foundation 契约)。
445
+ * 两出口统一落标:① 批闭合 flush 写账成功后;② E9 dispose 逐条转 async 写账后
446
+ * (均 appendEntry 持久化,U3/U5 写点)。undefined = 未离开批 / 旧记录零迁移。
447
+ * 消费方:U5 E1 重建扫描只收「collectMode=sync 且无本标记」的成员(防双重通知)。
448
+ */
449
+ batchFinalized?: boolean;
435
450
  /** 完整执行内容,按 turn 组织。createRecord 初始化为 [空 turn]。 */
436
451
  turns: Turn[];
437
452
  /** turn 计数(= turns.filter(closed).length,冗余存储供投影直接读)。 */
@@ -600,6 +615,17 @@ interface ExecuteOptions {
600
615
  * undefined/false = 一次性模式(默认,行为完全不变)。service.execute 透传到 createRecordForMode。
601
616
  */
602
617
  conversation?: boolean;
618
+ /**
619
+ * 同步收集模式(subagent-sync-collect 设计 §3.1.3,U1 foundation 契约)。
620
+ * undefined = config collectSync.default(缺省 "async",新 session 生效)。
621
+ * schema 层枚举限 "async"|"sync";运行时宽收 string 与 engine 字段同风格
622
+ * (非法值 ≠ "sync" 按 async 处理)。
623
+ * "sync" + conversation:true 组合在 startHandler E4 校验即拒
624
+ * (immediate throw,不产生半启动 record)。
625
+ * U2 接线点:service.createRecordForMode 读入 createRecord identity.collectMode
626
+ * (U1 打通类型与 startHandler 透传,record 落点归 U2)。
627
+ */
628
+ collect?: string;
603
629
  /**
604
630
  * 空闲超时毫秒数(仅 conversation 模式有意义)。覆盖默认 5min idle timeout。
605
631
  * 优先级:参数 > env XYZ_SUBAGENT_IDLE_TIMEOUT_MS > 默认 300000ms。
@@ -681,6 +707,16 @@ interface BgResponse {
681
707
  * 值语义由 U2(execution/notify-ledger.ts)兑现。
682
708
  */
683
709
  notifyContract: "ledger+at-least-once";
710
+ /**
711
+ * 同步收集登记回显段(subagent-sync-collect 设计 §3.1.1 交互样例,U1 foundation)。
712
+ * 仅 resolved 模式为 sync 时附带(async 响应字节零变化,G3):mode = 生效模式;
713
+ * pendingSyncCount = 当前未闭合批的 sync 成员总数(含本条;跨轮派发续累不重置,
714
+ * 与 D2 隐式批一致)。
715
+ */
716
+ collect?: {
717
+ mode: "sync";
718
+ pendingSyncCount: number;
719
+ };
684
720
  }
685
721
  /** list 的内层响应(挂在 SubagentToolResult.listResponse)。 */
686
722
  interface ListResponse {
@@ -850,6 +886,30 @@ interface SubagentRecord {
850
886
  journalPath?: string;
851
887
  poolKey: string;
852
888
  };
889
+ /**
890
+ * 同步收集模式标记(与 ExecutionRecord.collectMode 同源投影/重建,U1 foundation)。
891
+ * 缺省(存量 record)= async 投影,消费方零迁移。
892
+ */
893
+ collectMode?: "sync";
894
+ /**
895
+ * 离开批终局标记(与 ExecutionRecord.batchFinalized 同源投影/重建,U1 foundation)。
896
+ * 缺省 = 未离开批;U5 E1 重建扫描据此排除已离场成员。
897
+ */
898
+ batchFinalized?: boolean;
899
+ }
900
+ /**
901
+ * 同步收集(sync collect)配置节类型(subagent-sync-collect 设计 §3.1.3,U1 foundation)。
902
+ * 权威默认值在 config.ts DEFAULT_COLLECT_SYNC;坏值 sanitize 回默认不炸启动(E5,
903
+ * 与 maxConcurrent 同判)。类型定义于 types.ts(避免 config → types 反向依赖成环),
904
+ * config.ts re-export。
905
+ */
906
+ interface CollectSyncConfig {
907
+ /** start 未显式传 collect 时的缺省模式。新 session 生效(与 engine 配置时机一致)。 */
908
+ default: "async" | "sync";
909
+ /** 批通知单条目结果正文预算(字符):超出截断并接 session_read 指针行。flush 时热读。 */
910
+ perItemChars: number;
911
+ /** 批通知结果正文总量预算(字符):Σ 超限时统一收紧 effectivePerItem(U4 算法)。flush 时热读。 */
912
+ totalChars: number;
853
913
  }
854
914
  /**
855
915
  * 全局配置(~/.pi/agent/subagents/config.json)。
@@ -870,6 +930,11 @@ interface SubagentsGlobalConfig {
870
930
  engineRouting?: {
871
931
  strict: boolean;
872
932
  };
933
+ /**
934
+ * 同步收集配置节(subagent-sync-collect 设计 §3.1.3,U1 foundation)。
935
+ * 整节缺省 = DEFAULT_COLLECT_SYNC(config.ts);逐字段 sanitize 回默认(E5)。
936
+ */
937
+ collectSync?: CollectSyncConfig;
873
938
  }
874
939
  /**
875
940
  * Record 的只读视图。store.snapshot() 返回。