@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
@@ -34,6 +34,7 @@ import type {
34
34
  SubagentRecord,
35
35
  } from "./types.ts";
36
36
  import { ResurrectDeniedError } from "./types.ts";
37
+ import { COLLECT_SCAN_LIMIT } from "./collect-coordinator.ts";
37
38
 
38
39
  // ============================================================
39
40
  // 常量
@@ -105,6 +106,14 @@ export interface StartHandlerInput {
105
106
  idleTimeoutMs?: number;
106
107
  /** 执行引擎(三层路由第一层:本参数 > agent frontmatter engine > config defaultEngine)。 */
107
108
  engine?: string;
109
+ /**
110
+ * 同步收集模式(subagent-sync-collect U1 foundation)。undefined = config
111
+ * collectSync.default(缺省 "async")。schema 层枚举限 "async"|"sync";运行时
112
+ * 宽收 string 与 engine 字段同风格(pi 工具框架把 schema Static 解析为 string;
113
+ * 非法值 ≠ "sync" 按 async 处理,E4 守卫用精确 "sync" 判定)。透传
114
+ * service.execute(ExecuteOptions.collect;record.collectMode 落点归 U2 接线)。
115
+ */
116
+ collect?: string;
108
117
  }
109
118
 
110
119
  /** start 领域对象(宿主 adapter 包成 bg 工具结果)。 */
@@ -340,6 +349,21 @@ export async function startHandler(
340
349
  );
341
350
  if (slug.length > SLUG_MAX_LENGTH) throw new Error(`slug must be ≤${SLUG_MAX_LENGTH} chars (got ${slug.length}). Shorten to a kebab-case label, e.g. "fix-login", "extract-urls".`);
342
351
 
352
+ // ── collect 解析(subagent-sync-collect)──
353
+ // resolved = 显式参数 ?? config collectSync.default(U2 偏差#3 接线:经 service
354
+ // 公开访问器读真实 config,内部 DEFAULT 兑底——config 未配/读失败不炸)。
355
+ // E4 守卫放在 resolved 之后:显式 collect:"sync" + conversation 即拒;config 默认
356
+ // sync + conversation 同样被拦(同一守卫,无需改判定)。
357
+ const resolvedCollect = input.collect ?? service.getCollectSyncDefault();
358
+ // E4(设计 §3.1.5):sync 仅支持 one-shot。immediate throw——校验先于 service.execute,
359
+ // 不产生半启动 record(与 skillPath 路径守卫同风格:参数语义校验前置)。
360
+ if (input.conversation === true && resolvedCollect === "sync") {
361
+ throw new Error(
362
+ 'collect:"sync" only supports one-shot subagents — it cannot be combined with conversation:true. ' +
363
+ 'Remove either conversation or collect (use collect:"async", or omit it, for conversational subagents).',
364
+ );
365
+ }
366
+
343
367
  const handle = await service.execute({
344
368
  task,
345
369
  slug,
@@ -357,11 +381,31 @@ export async function startHandler(
357
381
  conversation: input.conversation,
358
382
  idleTimeoutMs: input.idleTimeoutMs,
359
383
  engine: input.engine,
384
+ // B1(code-simplify 审查发现的行为缺口):config collectSync.default=sync 且调用方
385
+ // 省略 collect 时,record 本体也要落 sync(设计 §3.1.3「缺省 = config 默认」作用于
386
+ // record,而非仅回显)——createRecordForMode 只认 opts.collect==="sync",原样透传
387
+ // input.collect 会让 record 走 async 逐条通知而响应声称已入批。仅 sync 落值:
388
+ // async/缺省路径传 undefined 语义(旧 record 零迁移)字节不变。
389
+ collect: resolvedCollect === "sync" ? "sync" : input.collect,
360
390
  ctxModel,
361
391
  signal,
362
392
  // background detached 运行,完成由 notify 驱动新 turn。
363
393
  });
364
394
 
395
+ const response: BgResponse = {
396
+ status: "running",
397
+ mode: "background",
398
+ message: BG_MESSAGE,
399
+ notifyContract: NOTIFY_CONTRACT,
400
+ };
401
+ // 同步收集登记回显段(设计 §3.1.1):仅 resolved 为 sync 时附段——async 响应
402
+ // 字节零变化(G3)。pendingSyncCount = 未闭合批 sync 成员总数(含本条,跨轮续累)。
403
+ // 本条 record 已由 createRecordForMode 落 collectMode(U2 偏差#4 接线),
404
+ // 枚举天然含本条,无需补偿。
405
+ if (resolvedCollect === "sync") {
406
+ response.collect = { mode: "sync", pendingSyncCount: countPendingSyncRecords(service) };
407
+ }
408
+
365
409
  return {
366
410
  kind: "bg",
367
411
  subagentId: handle.subagentId,
@@ -369,15 +413,27 @@ export async function startHandler(
369
413
  slug: handle.details.slug,
370
414
  // registry 全等回显:record.model 由 resolved(裁决放行条目)拼接,原样透出。
371
415
  model: handle.details.model,
372
- response: {
373
- status: "running",
374
- mode: "background",
375
- message: BG_MESSAGE,
376
- notifyContract: NOTIFY_CONTRACT,
377
- },
416
+ response,
378
417
  };
379
418
  }
380
419
 
420
+ /**
421
+ * 当前未闭合批的 sync 成员计数(pendingSyncCount 口径,设计 §3.1.3):本进程全部
422
+ * record(含已终态未 flush 的缓冲成员,故 statusFilter="all")中 collectMode="sync"
423
+ * 且无 batchFinalized 标记的数量。含调用方刚启动的本条(record 已带 collectMode 入
424
+ * 枚举——U2 偏差#4 接线)。
425
+ *
426
+ * 扫描上限与 service 冷路径全扫兑底同量级;常量用本 feature 已导出的
427
+ * COLLECT_SCAN_LIMIT(collect 域扫描上限单点定义,S4 code-simplify)。
428
+ */
429
+ function countPendingSyncRecords(service: SubagentService): number {
430
+ let count = 0;
431
+ for (const r of service.queries.collectRecords(COLLECT_SCAN_LIMIT, "all")) {
432
+ if (r.collectMode === "sync" && r.batchFinalized !== true) count += 1;
433
+ }
434
+ return count;
435
+ }
436
+
381
437
  // ============================================================
382
438
  // list handler
383
439
  // ============================================================