nx-kv 0.65.0 → 0.65.3

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 (155) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/package.json +1 -1
  3. package/src/core/db/seed-templates.ts +2 -86
  4. package/src/core/db/settings.ts +1 -1
  5. package/src/core/instruction-template.ts +1 -1
  6. package/src/core/workflow/plugin-registry.ts +1 -1
  7. package/src/core/workflow/types.ts +4 -4
  8. package/src/modules/claim/actions/_manifest.generated.ts +23 -0
  9. package/src/modules/{localtask → claim}/actions/localtask-claim.ts +4 -4
  10. package/src/modules/{localtask → claim}/actions/localtask-preview.ts +4 -4
  11. package/src/modules/{localtask → claim}/actions/localtask-wait.ts +4 -4
  12. package/src/modules/{tasktool → claim}/get.ts +3 -3
  13. package/src/modules/claim/index.ts +25 -0
  14. package/src/modules/{tasktool → claim}/panel-escort.ts +2 -2
  15. package/src/modules/{tasktool → claim}/params.ts +1 -1
  16. package/src/modules/claim/wait.ts +305 -0
  17. package/src/modules/{tasktool/plugins → claim}/waitgoal-config.ts +4 -28
  18. package/src/modules/delivery/assemble.ts +151 -0
  19. package/src/modules/{tasktool → delivery}/attach-resource.ts +2 -2
  20. package/src/modules/{tasktool → delivery}/attach-template.ts +1 -1
  21. package/src/modules/{tasktool → delivery}/attach.ts +1 -1
  22. package/src/modules/{tasktool → delivery}/delivery-text.ts +2 -2
  23. package/src/modules/{tasktool → delivery}/doc-inline.ts +1 -1
  24. package/src/modules/{tasktool → delivery}/groupctx.ts +1 -1
  25. package/src/modules/delivery/host.ts +231 -0
  26. package/src/modules/delivery/index.ts +18 -0
  27. package/src/modules/{tasktool → delivery}/preview.ts +5 -6
  28. package/src/modules/{tasktool → goals}/actions/timegoal-check.ts +1 -1
  29. package/src/modules/{tasktool → goals}/actions/timegoal-clear.ts +1 -1
  30. package/src/modules/{tasktool → goals}/actions/timegoal-list.ts +1 -1
  31. package/src/modules/{tasktool → goals}/actions/timegoal-log.ts +1 -1
  32. package/src/modules/{tasktool → goals}/actions/timegoal-set.ts +1 -1
  33. package/src/modules/{tasktool → goals}/actions/timegoal-show.ts +1 -1
  34. package/src/modules/{tasktool → goals}/actions/timegoal-update.ts +1 -1
  35. package/src/modules/goals/index.ts +25 -0
  36. package/src/modules/{tasktool → goals}/plugins/discover.ts +3 -3
  37. package/src/modules/{tasktool → goals}/plugins/index.ts +0 -2
  38. package/src/modules/{tasktool → goals}/plugins/taskgoal-store.ts +3 -3
  39. package/src/modules/{tasktool → goals}/plugins/timegoal-task-store.ts +2 -2
  40. package/src/modules/localtask/action-render.ts +1 -1
  41. package/src/modules/localtask/actions/_manifest.generated.ts +0 -9
  42. package/src/modules/localtask/actions/localtask-add.ts +2 -2
  43. package/src/modules/localtask/actions/localtask-attach-note.ts +1 -1
  44. package/src/modules/localtask/actions/localtask-attach.ts +1 -1
  45. package/src/modules/localtask/actions/localtask-attachments.ts +1 -1
  46. package/src/modules/localtask/actions/localtask-detach.ts +1 -1
  47. package/src/modules/localtask/actions/localtask-get.ts +1 -1
  48. package/src/modules/localtask/actions/localtask-reopen.ts +1 -1
  49. package/src/modules/localtask/actions/localtask-timegoalLog.ts +1 -1
  50. package/src/modules/localtask/actions/localtask-update.ts +2 -2
  51. package/src/modules/{tasktool → localtask}/add.ts +3 -3
  52. package/src/modules/localtask/engine.ts +1 -1
  53. package/src/modules/{tasktool → localtask}/group-store.ts +2 -2
  54. package/src/modules/localtask/mutator/index.ts +1 -1
  55. package/src/modules/localtask/panel-presence.ts +2 -2
  56. package/src/modules/localtask/scheduler/state-drive.ts +1 -1
  57. package/src/modules/{tasktool → localtask}/support.ts +1 -1
  58. package/src/modules/localtask/task-lifecycle.ts +5 -5
  59. package/src/modules/localtask/task-ops.ts +1 -1
  60. package/src/modules/localtask/task-templates.ts +1 -1
  61. package/src/modules/localtask/task-view.ts +1 -1
  62. package/src/modules/{tasktool → localtask}/update.ts +1 -1
  63. package/src/modules/localtask/waiters.ts +1 -1
  64. package/src/modules/localtask/writes.ts +1 -1
  65. package/src/modules/promptplug/factory.ts +5 -5
  66. package/src/modules/promptplug/index.ts +5 -5
  67. package/src/modules/promptplug/plugins/attachment.ts +1 -1
  68. package/src/modules/promptplug/plugins/context.ts +1 -1
  69. package/src/modules/promptplug/plugins/getloop.ts +8 -7
  70. package/src/modules/{tasktool/plugins/goal-template.ts → promptplug/plugins/goal-prompt.ts} +4 -3
  71. package/src/modules/repos/service/crud.ts +1 -1
  72. package/src/modules/templates/actions/templates-default.ts +2 -5
  73. package/src/modules/templates/actions/templates-update.ts +1 -3
  74. package/src/modules/templates/service.ts +11 -13
  75. package/src/modules/templates/support.ts +0 -15
  76. package/src/runtime/registry.ts +6 -2
  77. package/src/web/frontend/App.tsx +0 -12
  78. package/src/web/frontend/api/cli-tree.json +138 -138
  79. package/src/web/frontend/api/client.generated.ts +90 -88
  80. package/src/web/frontend/components/md/live-block.tsx +1 -5
  81. package/src/web/frontend/components/md/md-panes-root.tsx +0 -4
  82. package/src/web/frontend/components/md/md-panes.tsx +1 -11
  83. package/src/web/frontend/modules/md/styles/docs.css +0 -7
  84. package/src/web/frontend/modules/md/styles/editor.css +0 -17
  85. package/src/web/frontend/modules/md/use-templates.ts +3 -13
  86. package/src/web/frontend/page/dir/index.tsx +3 -15
  87. package/src/web/frontend/page/dir/task-actions.ts +1 -16
  88. package/src/web/frontend/page/dir/task.tsx +2 -16
  89. package/src/web/frontend/page/group/index.tsx +6 -19
  90. package/src/web/frontend/page/settings/index.tsx +1 -1
  91. package/src/web/frontend/page/templates/index.tsx +5 -54
  92. package/src/web/frontend/page/templates/manage.tsx +1 -5
  93. package/src/web/frontend/page/templates/style.css +1 -43
  94. package/src/web/frontend/page/templates/template-list.tsx +7 -15
  95. package/src/web/frontend/routes/route-eq.ts +2 -3
  96. package/src/web/frontend/routes/templates.ts +0 -10
  97. package/src/web/frontend/styles-v2/shell.css +7 -0
  98. package/src/web/frontend/types.ts +2 -1
  99. package/src/web/public/assets/{bar-csH6ZW39.js → bar-Dc5Tq1eE.js} +1 -1
  100. package/src/web/public/assets/{composer-launch-Ba9cBxhb.js → composer-launch-fp2IzGzQ.js} +1 -1
  101. package/src/web/public/assets/{composer-panel-7SHKllt5.js → composer-panel-Dh681iO6.js} +1 -1
  102. package/src/web/public/assets/dir-d16q1GLe.js +3 -0
  103. package/src/web/public/assets/{doc-BmGb9F-k.js → doc-DRSKD0Re.js} +1 -1
  104. package/src/web/public/assets/group-Dc3ZgW24.js +1 -0
  105. package/src/web/public/assets/{index-D1wpt-oL.css → index-BIVxGd6t.css} +1 -1
  106. package/src/web/public/assets/index-Bg0m8gbJ.js +2 -0
  107. package/src/web/public/assets/{md-history-DVI47hu4.js → md-history-DNtgaIdH.js} +6 -6
  108. package/src/web/public/assets/{refs-P8HrKv4T.js → refs-TzxZYy7T.js} +1 -1
  109. package/src/web/public/assets/{repo-form-modal-BiBZ63DZ.js → repo-form-modal-Dg3hJWUV.js} +1 -1
  110. package/src/web/public/assets/{repos-B9D8q6U7.js → repos-uNqSR5YB.js} +1 -1
  111. package/src/web/public/assets/task-DuFOXLPC.js +11 -0
  112. package/src/web/public/assets/{task-utils-DPGXUIOk.js → task-utils-DAPxKm4c.js} +1 -1
  113. package/src/web/public/assets/templates-CwYDIBYn.css +1 -0
  114. package/src/web/public/assets/templates-DNiGvmdq.js +10 -0
  115. package/src/web/public/assets/{use-rail-drag-TGx8CLcY.js → use-rail-drag-DxIYcszo.js} +1 -1
  116. package/src/web/public/index.html +6 -7
  117. package/src/core/workflow/edges.ts +0 -28
  118. package/src/core/workflow/layout.ts +0 -118
  119. package/src/modules/tasktool/facade/get.ts +0 -54
  120. package/src/modules/tasktool/facade/index.ts +0 -4
  121. package/src/modules/tasktool/facade/submit.ts +0 -61
  122. package/src/modules/tasktool/facade/wait.ts +0 -3
  123. package/src/modules/tasktool/getloop.ts +0 -2
  124. package/src/modules/tasktool/index.ts +0 -24
  125. package/src/modules/tasktool/plugins/runtime.ts +0 -73
  126. package/src/modules/tasktool/plugins/waitgoal.ts +0 -75
  127. package/src/modules/tasktool/wait.ts +0 -572
  128. package/src/web/frontend/modules/md/fill-session.ts +0 -36
  129. package/src/web/frontend/modules/md/new-task-from-template.ts +0 -22
  130. package/src/web/frontend/modules/md/task-intent.ts +0 -29
  131. package/src/web/frontend/modules/prompts/render-template-body.ts +0 -14
  132. package/src/web/frontend/modules/prompts/template-preview.ts +0 -62
  133. package/src/web/frontend/page/templates/fill.tsx +0 -126
  134. package/src/web/public/assets/dir-D5AImCiy.js +0 -3
  135. package/src/web/public/assets/fill-session-fHxQHFaD.js +0 -1
  136. package/src/web/public/assets/group-CTktws14.js +0 -1
  137. package/src/web/public/assets/index-Byb7MFTl.js +0 -2
  138. package/src/web/public/assets/new-task-from-template-DDWu4yR4.js +0 -1
  139. package/src/web/public/assets/render-template-body-bXdedFig.js +0 -1
  140. package/src/web/public/assets/task-BBk0TQZi.js +0 -11
  141. package/src/web/public/assets/task-intent-CX5DZccN.js +0 -1
  142. package/src/web/public/assets/templates-BN9i9-6c.js +0 -11
  143. package/src/web/public/assets/templates-plA8R7jJ.css +0 -1
  144. /package/src/modules/{tasktool → goals}/actions/_manifest.generated.ts +0 -0
  145. /package/src/modules/{tasktool → goals}/actions/timegoal-prune.ts +0 -0
  146. /package/src/modules/{tasktool → goals}/plugins/base.ts +0 -0
  147. /package/src/modules/{tasktool → goals}/plugins/blockgoal.ts +0 -0
  148. /package/src/modules/{tasktool → goals}/plugins/container.ts +0 -0
  149. /package/src/modules/{tasktool → goals}/plugins/taskgoal.ts +0 -0
  150. /package/src/modules/{tasktool → goals}/plugins/timegoal-core-store.ts +0 -0
  151. /package/src/modules/{tasktool → goals}/plugins/timegoal-cwd-store.ts +0 -0
  152. /package/src/modules/{tasktool → goals}/plugins/timegoal-store.ts +0 -0
  153. /package/src/modules/{tasktool → goals}/plugins/timegoal.ts +0 -0
  154. /package/src/modules/{tasktool → goals}/taskgoal.ts +0 -0
  155. /package/src/modules/{tasktool → goals}/timegoal.ts +0 -0
@@ -0,0 +1,305 @@
1
+ // claim/wait.ts —— wait/get 的阻塞编排(从 localtask/service.js 整体迁入)。
2
+ //
3
+ // 边界(claim = 领取域,localtask = 队列本体):
4
+ // - 队列读写、唤醒注册表、presence/面板注册表都在 localtask(serve 路由与
5
+ // panel-guard 的一半也长在那边),这里只做**队列编排**:快路径领取 → 阻塞窗口
6
+ // (跨进程轮询 / goal 到点收尾 / 用户关闭)。
7
+ // - 交付组装已拆到 delivery.ts(宿主取数 → promptplug 的 assembleDelivery)——
8
+ // 本文件在拿到任务的那一刻调 buildTaskDelivery,不碰模板与渲染。
9
+ // - 面板随行(确保主面板 / 深链 / 窗口重开防风暴)已拆到 panel-escort.js——
10
+ // 这里只经 escortPanel({ stop() }) 一点相接。
11
+ // - 与队列的耦合全部走 localtask/service.js 的导出面;与计时目标的耦合走
12
+ // 同模块的 timegoal.js(探针只读;goal 到点自动收尾本轮)。
13
+ import { DEFAULT_PORT } from '../../core/paths.js';
14
+ import {
15
+ clampTimeoutSec,
16
+ clearWaiting,
17
+ consumeCancelRequest,
18
+ consumeFirstPending,
19
+ getSettings,
20
+ GLOBAL_KEY,
21
+ isGlobalKey,
22
+ normalizeCwd,
23
+ normalizeSummary,
24
+ probeEffectivePresence,
25
+ registerGlobalWaiter,
26
+ registerTopicWaiter,
27
+ repoContextIndex,
28
+ repoCtxOf,
29
+ setWaiting,
30
+ unregisterTopicWaiter,
31
+ } from '../localtask/service.js';
32
+ import { ensureMainPanel, deepLinkUrl, escortPanel } from './panel-escort.js';
33
+ import { buildTaskDelivery } from '../delivery/host.js';
34
+ import * as timegoal from '../goals/timegoal.js';
35
+
36
+ // 跨进程兜底轮询间隔(只定这里的读取节奏;队列写入节奏与它无关)
37
+ const POLL_MS = 500;
38
+
39
+ // ─── 时间目标探针(阻塞期间到点自动收尾本轮) ────────────────────────
40
+ //
41
+ // 与 localtask wait 的融合(fail-open:timegoal 坏了绝不拖垮 wait):
42
+ // - 阻塞期间到点 → 本轮立刻收尾(goalPoll,见 waitForTask);
43
+ // - 返回时刻把目标状态渲染进 instruction(goal 段)——那半边在 delivery.ts。
44
+ // 探针按 key 服务于 500ms 轮询。读取时机是**阻塞开始**不是返回时刻:
45
+ // 只认「等待开始时还在跑」的目标,已到点的不参与自动取消。
46
+
47
+ // 等待期到点探针(只读、不写盘):返回该目录目标的截止 epoch ms;已到点/无目标 → 0
48
+ async function readGoalDeadlineMs(key: unknown): Promise<number> {
49
+ if (!key || isGlobalKey(key)) return 0;
50
+ try {
51
+ const g = await timegoal.probeGoalExpiry(key);
52
+ if (!g || g.reached) return 0; // 已经到点的:不参与自动取消
53
+ const ms = Number(g.deadlineMs);
54
+ return Number.isFinite(ms) && ms > 0 ? ms : 0;
55
+ } catch {
56
+ return 0;
57
+ }
58
+ }
59
+
60
+ // ─── 阻塞等待:核心入口 ─────────────────────────────────────────────
61
+
62
+ export interface WaitForTaskOptions {
63
+ /** 工作目录(主题按目录名识别);缺省 → 进程当前 cwd。显式传 cwd **不会**关闭全局兜底 */
64
+ cwd?: unknown;
65
+ /** 当前现状(#171 起不再必填:面板上没有对应输入界面) */
66
+ summary?: unknown;
67
+ /** 阻塞上限(秒);缺省 → 页面配置(推荐 180,最大 600) */
68
+ timeoutSec?: number;
69
+ /** 兼容旧参数(毫秒) */
70
+ timeoutMs?: number;
71
+ /** 是否自动打开浏览器弹窗(测试可关) */
72
+ open?: boolean;
73
+ /** 服务器就绪回调 {addr,key,cwd,name,summary}(测试/集成钩子) */
74
+ onReady?: (info: { addr: string; key: string; cwd: string; name: string; summary: string }) => unknown | Promise<unknown>;
75
+ /** 外部参数(平面扩展):进交付模板的 ctx(--param k=v) */
76
+ params?: Record<string, string>;
77
+ /** get 的「直接领」形态:没有 pending 立即空手而回,不开窗不阻塞 */
78
+ instant?: boolean;
79
+ /** 项目级「持续 get」开关(仅 get/preview 传;wait 不传 → 不出现该区块) */
80
+ getLoopEnabled?: boolean;
81
+ /** 项目级阻塞窗口(分钟):进 getloop 模板 ctx,让 agent 知道每轮会等多久 */
82
+ getLoopWaitMinutes?: number;
83
+ }
84
+
85
+ /** 阻塞等待的唤醒结果(三个生产者 + 超时) */
86
+ type WaitOutcome =
87
+ | { kind: 'task'; task: any; delivered: boolean; source?: string }
88
+ | { kind: 'timeout' }
89
+ | { kind: 'cancelled'; reason?: string };
90
+
91
+ /**
92
+ * 针对某主题(工作目录)等待用户回填下一条任务。
93
+ * 领取顺序:本目录队列 → 全局主题(共享池)→ 阻塞(两个来源任一到达即唤醒)。
94
+ * 返回带 source: 'local' | 'global' 标明任务来自哪个队列。
95
+ */
96
+ export async function waitForTask({
97
+ cwd: rawCwd,
98
+ summary: rawSummary,
99
+ timeoutSec,
100
+ timeoutMs,
101
+ open = true,
102
+ onReady,
103
+ params = {}, // 外部参数(平面扩展):进交付模板的 ctx(--param k=v)
104
+ instant = false, // get 的「直接领」形态:没有 pending 立即空手而回,不开窗不阻塞
105
+ getLoopEnabled = false, // 项目级「持续 get」开关(仅 get/preview 传;wait 不传 → 不出现该区块)
106
+ getLoopWaitMinutes = 0, // 项目级阻塞窗口(分钟):进 getloop 模板 ctx,让 agent 知道每轮会等多久
107
+ }: WaitForTaskOptions = {}): Promise<Record<string, any>> {
108
+ // paths.normalizeCwd 的 @returns 标注漏了 name(运行时恒返回 {cwd, name, key})
109
+ const { key, cwd, name } = normalizeCwd(rawCwd as string) as { cwd: string; key: string; name: string };
110
+ const summary = normalizeSummary(rawSummary, { optional: true }); // #171:现状不再必填
111
+ // 领取方身份:记进 task.claimedBy(事后能看出"这条被谁领的")
112
+ const by = { key, cwd, name };
113
+ // 仓库上下文:这个目录属于哪个已登记仓库、周围关联哪些看板。
114
+ // 跟着 wait 的结果一起给出去 —— agent 领到任务就知道自己的" surroundings"。
115
+ const repoIndex = await repoContextIndex();
116
+ const repoCtx = repoCtxOf(repoIndex, cwd);
117
+
118
+ // 超时:显式 --timeout(秒/毫秒)优先;否则用页面配置;统一夹到 1-600
119
+ // T03:设置走 settings 表(repo/settings),不再有 JSON store 对象
120
+ const startSettings = await getSettings();
121
+ let sec: unknown;
122
+ if (timeoutSec !== undefined && timeoutSec !== null) sec = timeoutSec;
123
+ else if (timeoutMs !== undefined && timeoutMs !== null) sec = Number(timeoutMs) / 1000;
124
+ else sec = startSettings.timeoutSec;
125
+ const tmo = clampTimeoutSec(sec) * 1000;
126
+
127
+ // 1. 主题队列已有 pending → 立即取走,不开窗、不阻塞;记录本轮完成总结
128
+ const queued = await consumeFirstPending(key, summary, by);
129
+ if (queued) {
130
+ const delivery = await buildTaskDelivery(queued, { source: 'local', summary, cwd, name, topicKey: key, params, getLoopEnabled, getLoopWaitMinutes });
131
+ return { status: 'ok', key, cwd, name, summary, ...delivery, queued: true, source: 'local', repo: repoCtx };
132
+ }
133
+
134
+ // 1b. 本地空 → 去全局主题(共享池)领。这是所有 wait 的**无条件默认兜底**:
135
+ // 与是否显式传 --cwd 无关——本地取不到就一定来全局扫一眼,全局也没有才阻塞。
136
+ // (防止调用方习惯性传 cwd 导致共享池永远无人领取。)
137
+ // instant(localtask get 默认形态):两处快路径都没货 → 立即返回,不进阻塞窗口。
138
+ // 「无任务时唤起阻塞等待」是 waitgoal(项目 getLoop)的语义,见 claim/get.ts。
139
+ if (instant) {
140
+ return { status: 'timeout', key, cwd, name, summary, timeoutMs: 0, instant: true };
141
+ }
142
+
143
+ const fromGlobal = await consumeFirstPending(GLOBAL_KEY, summary, by);
144
+ if (fromGlobal) {
145
+ // 全局池投递渲染的仍是**等待方目录**的 goal——目标绑定在等待中的 agent 目录上,
146
+ // 与任务来自哪个队列无关。
147
+ const delivery = await buildTaskDelivery(fromGlobal, { source: 'global', summary, cwd, name, topicKey: key, params, getLoopEnabled, getLoopWaitMinutes });
148
+ return {
149
+ status: 'ok',
150
+ key,
151
+ cwd,
152
+ name,
153
+ summary,
154
+ ...delivery,
155
+ queued: true,
156
+ source: 'global',
157
+ repo: repoCtx,
158
+ };
159
+ }
160
+
161
+ // 1c. 这个目录的时间目标**还在跑** → 记下它的截止时刻。
162
+ // 等待期间一旦越过这个时刻,本轮立刻收尾(见下方 goalPoll)。
163
+ // 目标早就过期的(reached)不记:那是"这一局已经结束了",
164
+ // 该由 agent 自己的 `timegoal check` 去处理,不该把 wait 焊死在这个目录上。
165
+ const goalDeadlineMs = await readGoalDeadlineMs(key);
166
+
167
+ // 2. 没有 pending → 复用常驻主面板(不再起随机端口临时弹窗):
168
+ // a) 确保主面板在跑(不存在就 detached 拉起 serve --no-open --auto-exit);
169
+ // b) 探测活动 SSE 连接:有活标签就不开新页(避免 Edge 重复开标签);
170
+ // 没活标签就 openBrowser 深链到当前主题。
171
+ // 失败只降级为纯阻塞——绝不抛错中断 wait。
172
+ // --no-open 模式(冒烟/CI)不发起任何面板相关动作:不开浏览器、不拉起 serve、不开 SSE。
173
+ // 这样可避免在测试里 spawn 出一堆孤儿进程占着 7877 端口。
174
+ const panelInfo = open
175
+ ? await ensureMainPanel()
176
+ : { running: false, started: false, port: 0, url: '' };
177
+ // 活性判据:「本地任务」页的活动 SSE 标签,或面板停在任意页(App 壳 ping 兜底)。
178
+ // 只认 SSE 的旧口径在用户切到别的页时会误判「没开」,于是每次 wait 都重开一个标签。
179
+ const presence = panelInfo.running ? await probeEffectivePresence() : 0;
180
+ const deepLink = deepLinkUrl(key, panelInfo.port || DEFAULT_PORT);
181
+
182
+ // 3. 结果承诺:三个生产者(同进程本地 waiter / 同进程全局 FIFO / 跨进程轮询)+ 超时
183
+ let resolveOutcome!: (r: WaitOutcome) => void;
184
+ const outcome = new Promise<WaitOutcome>((r) => {
185
+ resolveOutcome = r;
186
+ });
187
+ registerTopicWaiter(key, {
188
+ resolve: (task: any) => resolveOutcome({ kind: 'task', task, delivered: true }),
189
+ });
190
+ // 全局兜底:同进程有人 addTask({global:true}) 时按 FIFO 唤醒本 wait
191
+ const unregisterGlobal = registerGlobalWaiter(
192
+ (task: any) => resolveOutcome({ kind: 'task', task, delivered: true, source: 'global' }),
193
+ summary,
194
+ // by 承载 {key,cwd,name} 对象(领取后记进 task.claimedBy,见 task-ops 的 gw.by 消费);
195
+ // waiters 层的 by 已按 WaiterBy 标注(曾误标 string),此处直接透传
196
+ by
197
+ );
198
+
199
+ const timer = setTimeout(() => resolveOutcome({ kind: 'timeout' }), tmo);
200
+
201
+ // 三个 500ms 节拍的轮询合一为一个 ticker(原 poll/cancelPoll/goalPoll 三个 setInterval):
202
+ // ① 跨进程兜底轮询:先查本地,再查全局(本地优先;同轮至多 resolve 一次)
203
+ // ② 关闭轮询:用户在面板点了「关闭」→ 立即收尾本轮(走与超时同一条出口,只多带
204
+ // cancelled 标记,agent 不必认识新状态,照常结束本轮)
205
+ // ③ 时间目标到点 → 立刻收尾本轮(等的是"下一条任务",预算用完了就没必要等)。
206
+ // 与②同一条出口:status:"timeout" + cancelled:true。
207
+ // 只认"等待开始时还在跑"的目标(goalDeadlineMs>0)——已经过期的目标不参与。
208
+ // 同拍内①②各自异步,谁先 resolve 谁赢(resolveOutcome 幂等)——拆分前多个独立
209
+ // setInterval 的同拍相对顺序本就不保证,语义等价。unref 收尾:进程存活由 ref 的
210
+ // timeout timer 兜着,ticker 不必单独撑事件循环(面板窗口复查的 5s 节拍在
211
+ // panel-escort.js,自带起停,不并入)。
212
+ const ticker = setInterval(() => {
213
+ if (goalDeadlineMs && Date.now() >= goalDeadlineMs) {
214
+ resolveOutcome({ kind: 'cancelled', reason: 'goal_expired' });
215
+ }
216
+ consumeFirstPending(key, undefined, by)
217
+ .then((task: any) => {
218
+ if (task) {
219
+ resolveOutcome({ kind: 'task', task, delivered: false, source: 'local' });
220
+ return null;
221
+ }
222
+ return consumeFirstPending(GLOBAL_KEY, undefined, by);
223
+ })
224
+ .then((task: any) => {
225
+ if (task) resolveOutcome({ kind: 'task', task, delivered: false, source: 'global' });
226
+ })
227
+ .catch(() => {
228
+ /* 瞬时读写错误,下一轮再试 */
229
+ });
230
+ consumeCancelRequest(key)
231
+ .then((yes: any) => {
232
+ if (yes) resolveOutcome({ kind: 'cancelled' });
233
+ })
234
+ .catch(() => {});
235
+ }, POLL_MS);
236
+ ticker.unref?.();
237
+
238
+ // 登记「等待中」(含截止时刻与现状),面板据此显示倒计时
239
+ await setWaiting(key, cwd, summary, true, new Date(Date.now() + tmo));
240
+
241
+ // 登记之后再查一次关闭标记:setWaiting 刚清掉上一轮残留的标记,因此这里若有值
242
+ // 一定是新一次关闭请求——立刻收尾,不让用户白等。
243
+ if (await consumeCancelRequest(key).catch(() => false)) {
244
+ resolveOutcome({ kind: 'cancelled' });
245
+ }
246
+
247
+ // waiter/轮询就位后再回调,保证 onReady 里立刻回填也能被接住
248
+ if (typeof onReady === 'function') {
249
+ try {
250
+ await onReady({ addr: deepLink, key, cwd, name, summary });
251
+ } catch {
252
+ /* 钩子异常不得影响 wait */
253
+ }
254
+ }
255
+
256
+ // 没活标签且主面板在跑 → 开浏览器深链 + 周期复查重开(含初次开页判定)。
257
+ // 全部细节(节流/冷却/上限/降级)在 panel-escort.js;这里只管起停。
258
+ const escort = escortPanel({ open, panelInfo, deepLink, initialPresence: presence });
259
+
260
+ let result: Record<string, any>;
261
+ try {
262
+ const r = await outcome;
263
+ // 交付指令在**返回时刻**组装(此刻的 goal 状态才是真实状态;阻塞期间到点会渲染出已到点)。
264
+ // 只对拿到任务的结果生效——timeout/goal_expired/cancelled 的返回形状是钉死的契约,不动。
265
+ const delivery =
266
+ r.kind === 'task'
267
+ ? await buildTaskDelivery(r.task, { source: r.source || 'local', summary, cwd, name, topicKey: key, params, getLoopEnabled, getLoopWaitMinutes })
268
+ : null;
269
+ result =
270
+ r.kind === 'timeout'
271
+ ? { status: 'timeout', key, cwd, summary, timeoutMs: tmo }
272
+ : r.kind === 'cancelled'
273
+ ? {
274
+ status: 'timeout',
275
+ key,
276
+ cwd,
277
+ summary,
278
+ timeoutMs: tmo,
279
+ cancelled: true,
280
+ // 为什么取消:'goal_expired' = 时间目标到点、这一局预算用完;
281
+ // 缺省 = 用户在面板点了「关闭等待」。
282
+ ...(r.reason ? { reason: r.reason } : {}),
283
+ }
284
+ : {
285
+ status: 'ok',
286
+ key,
287
+ cwd,
288
+ name,
289
+ summary,
290
+ ...(delivery || { task: r.task }),
291
+ delivered: r.delivered,
292
+ queued: false,
293
+ source: r.source || 'local',
294
+ repo: repoCtx,
295
+ };
296
+ } finally {
297
+ clearInterval(ticker);
298
+ escort.stop();
299
+ clearTimeout(timer);
300
+ unregisterTopicWaiter(key);
301
+ unregisterGlobal();
302
+ await clearWaiting(key);
303
+ }
304
+ return result;
305
+ }
@@ -1,4 +1,4 @@
1
- // tasktool/getloop.js —— 项目级「持续 get」(waitgoal)配置的读取。
1
+ // claim/waitgoal-config.ts —— 项目级「持续 get」(waitgoal)配置的读取。
2
2
  //
3
3
  // 落点:repos.json 的 repo 记录字段 getLoop = { enabled, waitMinutes }。
4
4
  // **#182 起仓库级配置只剩「开关 + 阻塞分钟」**:交付里那段「继续 get」的**文案**
@@ -16,20 +16,9 @@
16
16
  // 且交付文本带「不断调用 get,直到 get 超时」那段指令(提示词增强,文案见全局区块)。
17
17
  //
18
18
  // repos 在模块互依禁列里 → 动态 import(与 localtask→timegoal 探针同一惯例)。
19
- import { badInput } from '../../../core/errors.js';
20
-
21
- /**
22
- * 持续 get 内文的默认文案(#152 起模板正文的唯一真相是插件类:getloop 插件的内置
23
- * 模板 = `{{#if getloop}}\n<本段>\n{{/if}}`,这里是其中的**内文**,由
24
- * getLoopInstruction() 渲染后作为 loopText 供给插件)。改文案请改插件类。
25
- */
26
- export const DEFAULT_GETLOOP_INSTRUCTION =
27
- '{{#if task}}不要直接结束本轮:完成上面的任务({{task.id}})之后,继续调用 nx-kv localtask get 领取下一条任务。队列里有活会立即返回;{{#if (gt getloop.waitMinutes 0)}}没活时每次最多阻塞 {{getloop.waitMinutes}} 分钟,直到 get 返回 timeout 为止。{{/if}}{{/if}}';
28
-
29
- // 持续 get 内文的默认文案:**唯一来源在本文件**(hook 侧注入 getloop 插件的 loopText),
30
- // 全局「提示词模板 → 持续 get」区块的内置模板与它逐字同源(单测钉住)。
31
- export const DEFAULT_GET_LOOP_TEMPLATE = DEFAULT_GETLOOP_INSTRUCTION;
19
+ import { badInput } from '../../core/errors.js';
32
20
 
21
+ /** 阻塞窗口默认 10 分钟(上限仍受 wait 的 600s 夹制)。#230 起循环文案唯一真相在 getloop 插件类。 */
33
22
  export const GET_LOOP_DEFAULT_MINUTES = 10; // 阻塞窗口默认 10 分钟(上限仍受 wait 的 600s 夹制)
34
23
 
35
24
  /** getLoopForCwd 返回的项目级配置形态(#182 起没有 template:文案归全局区块)。 */
@@ -38,23 +27,10 @@ export interface GetLoopState {
38
27
  waitMinutes?: number;
39
28
  }
40
29
 
41
- /**
42
- * 项目开启「持续 get」→ 交出那段循环指令(交给交付编排当 getloop 区块的内文)。
43
- *
44
- * #182 起**不再读仓库里的自定义模板**:文案只有共享层那一份,用户要改就到
45
- * 设置 → 提示词模板 → 持续 get 区块改(那里是唯一出口)。
46
- *
47
- * @param loop 项目级 getLoop 配置
48
- * @returns 可渲染的模板文本;未开启时返回空串(该区块整段不出现)
49
- */
50
- export function getLoopInstruction(loop: { enabled?: unknown } | null | undefined): string {
51
- if (!loop || !loop.enabled) return '';
52
- return DEFAULT_GET_LOOP_TEMPLATE;
53
- }
54
30
 
55
31
  let reposMod: any = null;
56
32
  async function reposStore(): Promise<any> {
57
- if (!reposMod) reposMod = await import('../../repos/service.js');
33
+ if (!reposMod) reposMod = await import('../repos/service.js');
58
34
  return reposMod;
59
35
  }
60
36
 
@@ -0,0 +1,151 @@
1
+ // promptplug/delivery.ts —— 交付组装(assembleDelivery):seed 构造 + 三链合并 + compose。
2
+ //
3
+ // 这是「一条交付提示词怎么拼出来」的**唯一实现**(#227 重构:原样从 claim/wait.ts
4
+ // 的 buildTaskDelivery 下半段收编进来)。宿主(delivery/host.ts)完成领域取数
5
+ // (附件标注 / 时间目标起表 / 组上下文 / 队列余量)后,把取好的数据装进
6
+ // DeliveryHostData 交到这里;本文件不再碰任何业务模块——promptplug 不 import
7
+ // 其他模块的边界保持不变。
8
+ //
9
+ // #230 清理:原「getLoopTpl 预渲染 → data.getloop.text」残留链路已删——#182 起项目级
10
+ // template 口子下线,getloop 文案唯一真相是插件内置模板,本文件只给 enabled 开关与分钟数。
11
+ // 之所以收进 promptplug:组装配方(平面扩展的键保护 / 三链合并 / coversBody 判定)
12
+ // 是提示词域的语义,不是队列域的。
13
+ // 之前它埋在 wait.ts(阻塞编排文件)里,测试只能各自手工复刻(prompt-golden /
14
+ // delivery-template 两个测试都有「与 wait.ts 同构」的最小组装)——现在它们可以
15
+ // 直接调 assembleDelivery。
16
+ import { reorderRoundsForDelivery } from '../../shared/feedback-text.js';
17
+ import { compose, type RenderTraceEntry, type RenderWarning, type SegmentMeta } from '../promptplug/compose.js';
18
+ import { buildPlugins } from '../promptplug/factory.js';
19
+
20
+ /** compose 的分段元信息(面板「这段是哪来的」的展示标签),交付组装的固定清单。 */
21
+ export const DELIVERY_SEGMENTS: SegmentMeta[] = [
22
+ { id: 'context', label: '组 / 项目上下文(模板)', traceId: 'delivery' },
23
+ { id: 'attachment', label: '附件段(清单 / 本地路径)', traceId: 'attach' },
24
+ { id: 'timegoal', label: '时间目标启动(介绍 / 每轮怎么做)', traceId: 'goal' },
25
+ { id: 'doneguard', label: '完成门禁提示(timegoal)', traceId: 'doneguard' },
26
+ { id: 'ext', label: '待领取队列(ext)', traceId: 'ext' },
27
+ { id: 'ack', label: 'ack 完成总结提示词', traceId: 'ack' },
28
+ { id: 'getloop', label: '持续 get 指令(项目级)', traceId: 'getloop' },
29
+ ];
30
+
31
+ // 平面扩展的**内建键保护名单**:外部参数(--param k=v)顶层直达 {{k}},
32
+ // 但不许覆盖 seed 的内建键(ctx 形状是插件 dataSchema 白名单的前提)。
33
+ const SEED_PROTECTED_KEYS = ['task', 'goal', 'cwd', 'source', 'summary', 'name', 'repo', 'group', 'refRepos', 'groups'];
34
+
35
+ /** 外部参数 → 平面扩展表({{k}} 顶层直达;内建键被保护不被覆盖;{{params.k}} 永远可用)。 */
36
+ export function flatParamsOf(params?: Record<string, string>): Record<string, unknown> {
37
+ const flat: Record<string, unknown> = {};
38
+ for (const [k, v] of Object.entries(params || {})) {
39
+ if (SEED_PROTECTED_KEYS.includes(k)) continue;
40
+ flat[k] = v;
41
+ }
42
+ return flat;
43
+ }
44
+
45
+ /** 宿主取好的领域数据(取数细节见 delivery/host.ts——本文件不做任何 IO)。 */
46
+ export interface DeliveryHostData {
47
+ /** 标注后的任务行(id / text);无任务 → null(指令段也照常渲染) */
48
+ task: any | null;
49
+ /** 附件段 ctx(attachTemplateCtx 产物);无附件 → null */
50
+ attachCtx: any | null;
51
+ /** 最终 goal ctx(enrichGoalTiming + 任务级/目录级合并后);无目标 → null */
52
+ goalCtx: any | null;
53
+ /** 组上下文(readGroupContext 产物);非组任务 → null */
54
+ groupCtx: any | null;
55
+ /** 队列余量(pendingQueueInfo 产物,count>0 才给);null → ext 段不出 */
56
+ queueInfo: { topic: string; count: number; list: { id: number; title: string }[] } | null;
57
+ /** 项目级「持续 get」开关(宿主从 waitgoal 配置读出);false = 段不出 */
58
+ getLoopEnabled?: boolean;
59
+ /** 项目级阻塞窗口(分钟):进 getloop 模板 ctx */
60
+ getLoopWaitMinutes?: number;
61
+ /** 外部参数(--param k=v 解析产物):{{params.k}} + 平面扩展 {{k}} */
62
+ params?: Record<string, string>;
63
+ /** 设置里的追加模式:'plain' 直拼文本,其余(含缺省)包 XML 标签 */
64
+ appendMode?: unknown;
65
+ /** seed 原样透传的上下文键(保持 unknown:宿主传什么 seed 就是什么) */
66
+ source?: unknown;
67
+ summary?: unknown;
68
+ cwd?: unknown;
69
+ name?: unknown;
70
+ /** 宿主侧告警(如 ext 读取失败),并入 renderWarnings(排在本文件告警之后) */
71
+ hostWarnings?: RenderWarning[];
72
+ /** 逐段来源追踪(面板预览用;真实领取不传) */
73
+ trace?: RenderTraceEntry[];
74
+ }
75
+
76
+ export interface AssembledDelivery {
77
+ /** 拼好的完整指令;空串 = 没有任何段输出(宿主按「只回任务」降级) */
78
+ instruction: string;
79
+ /** 任一模板含 {{task.text}} → 指令自包含任务正文(服务端判定,CLI 不做子串猜测) */
80
+ coversBody: boolean;
81
+ /** 渲染告警(段渲染失败但交付继续的口径),已并宿主告警 */
82
+ renderWarnings: RenderWarning[];
83
+ }
84
+
85
+ /**
86
+ * 组装一次交付提示词。**单一路径**:所有段落(上下文 / 附件 / 时间目标 / ack /
87
+ * 持续 get / done 门禁)合并成一条 instruction;task.text 始终是库存原文,
88
+ * 绝不把系统提示词拼进正文。
89
+ *
90
+ * 交付一次给全三条链(get → check → done,合并后按各段 order 全局排序):
91
+ * check(时间目标)与 done(完成门禁)没有独立的注入点——它们本来就是
92
+ * 「领取时告诉 agent 每轮怎么检查、什么时候才能收口」,等 agent 真去 done 再给
93
+ * 已经晚了;三者链内都是条件渲染,无数据即不出段。
94
+ */
95
+ export async function assembleDelivery(host: DeliveryHostData): Promise<AssembledDelivery> {
96
+ const {
97
+ task,
98
+ attachCtx: attach,
99
+ goalCtx: goal,
100
+ groupCtx,
101
+ queueInfo,
102
+ getLoopEnabled = false,
103
+ getLoopWaitMinutes = 0,
104
+ params,
105
+ appendMode,
106
+ trace,
107
+ } = host;
108
+ const renderWarnings: RenderWarning[] = [];
109
+
110
+ const flat = flatParamsOf(params);
111
+ const seedCtx: Record<string, any> = {
112
+ task: task ? { id: task.id, text: reorderRoundsForDelivery(String(task.text || '')) } : null,
113
+ goal: goal ?? null,
114
+ source: host.source || '',
115
+ summary: host.summary || '',
116
+ cwd: host.cwd || '',
117
+ name: host.name || '',
118
+ params: { ...(params || {}) },
119
+ ...flat,
120
+ };
121
+
122
+ const mode: 'xml' | 'plain' = appendMode === 'plain' ? 'plain' : 'xml';
123
+ // #152:模板是插件类的内置正文(不再从 settings 解析)。
124
+ // 宿主取数以 Supply 注入(惰性:不消费就不算)。
125
+ const plugins = [
126
+ ...buildPlugins('get', {
127
+ groupCtx: () => groupCtx,
128
+ attach: () => attach,
129
+ queueInfo: () => queueInfo,
130
+ enabled: () => !!getLoopEnabled,
131
+ waitMinutes: () => Number(getLoopWaitMinutes) || 0,
132
+ }),
133
+ ...buildPlugins('check'),
134
+ ...buildPlugins('done'),
135
+ ];
136
+ const full = await compose({
137
+ plugins,
138
+ groups: ['get', 'check', 'done'],
139
+ seed: seedCtx,
140
+ mode,
141
+ trace,
142
+ segments: DELIVERY_SEGMENTS,
143
+ });
144
+ // coversBody = 本次交付链的任一模板含 {{task.text}}(instruction 自包含任务正文)。
145
+ // 由服务端判定,避免 CLI 端做子串猜测。
146
+ return {
147
+ instruction: full,
148
+ coversBody: plugins.some((p) => /\{\{\s*task\.text\s*\}\}/.test(p.get())),
149
+ renderWarnings: [...renderWarnings, ...(host.hostWarnings || [])],
150
+ };
151
+ }
@@ -1,4 +1,4 @@
1
- // tasktool/attach-resource.js —— 附件的「资源视图模型」:关系表行 → 展示/取用信息。
1
+ // delivery/attach-resource.ts —— 附件的「资源视图模型」:关系表行 → 展示/取用信息。
2
2
  //
3
3
  // 资源类型只有五种(用户口径):file(含图片)/ url / task / ref / mdfile。
4
4
  // 旧数据里 kind='image' 或没有 kind 的一律按 file 处理(向后兼容)。
@@ -338,7 +338,7 @@ export function resourceOf(row: any, lookups: ResourceLookups = {}): any {
338
338
 
339
339
  // ref:文档库里的文档(v7 起 ref 是 DB 实体,没有物理路径)。
340
340
  // 标题口径(#168):**文档标题**(正文首行的 # 标题)优先 —— 服务端在标注层
341
- // (tasktool/attach.js 的 annotateRefTitles)读库补上并覆盖 title/name;
341
+ // (delivery/attach.ts 的 annotateRefTitles)读库补上并覆盖 title/name;
342
342
  // 这里先给 desc / 名字占位,读不到标题时前端回落它。
343
343
  const info = lookups.refs && lookups.refs.get(String(meta.refId || row.targetKey || ''));
344
344
  const refId = String(meta.refId || row.targetKey || (info && info.refId) || '');
@@ -1,4 +1,4 @@
1
- // tasktool/attach-template.js —— 交付 instruction〔附件〕段的模板 ctx 与渲染。
1
+ // delivery/attach-template.ts —— 交付 instruction〔附件〕段的模板 ctx 与渲染。
2
2
  //
3
3
  // 输出由模板渲染(内置 DEFAULT_ATTACH_TEMPLATE 或配置模板),保证「配置模板」与
4
4
  // 「内置默认」走同一份 ctx、同一套口径;渲染失败时用最朴素的逐条文本兜底
@@ -1,4 +1,4 @@
1
- // tasktool/attach.js —— 本地任务链路的附件编排(标注 / 追加 / 清单)。
1
+ // delivery/attach.ts —— 本地任务链路的附件编排(标注 / 追加 / 清单)。
2
2
  //
3
3
  // 背景(任务 #15「localtask 附件功能未实现」):web 编辑器已把附件以
4
4
  // `![名](fileId)` 嵌进正文,但链路的 **agent 侧**对不上——get/wait 交付的
@@ -1,8 +1,8 @@
1
- // tasktool/delivery-text.js —— 交付载荷的人读正文(**唯一出处**)。
1
+ // delivery/delivery-text.ts —— 交付载荷的人读正文(**唯一出处**)。
2
2
  //
3
3
  // 谁会用到:
4
4
  // - CLI `localtask get/wait` 的人读输出(localtask/index.js 的 renderWait)
5
- // - CLI `localtask preview` 与面板「预览提示词」(tasktool/preview.js)
5
+ // - CLI `localtask preview` 与面板「预览提示词」(delivery/preview.ts)
6
6
  //
7
7
  // 为什么单独抽出来:预览的全部价值在于「所见即所得」——如果面板自己拼一遍排版,
8
8
  // 迟早与 CLI 漂移,用户看到的就不是 agent 收到的那一份。同一份代码 = 同一份文本。
@@ -1,4 +1,4 @@
1
- // tasktool/doc-inline.js —— 任务级文档绑定的「进原文」(#162)。
1
+ // delivery/doc-inline.ts —— 任务级文档绑定的「进原文」(#162)。
2
2
  //
3
3
  // role='doc' 的 kind=ref 附件 = **文档资料绑定**:交付时把文档全文附进 instruction,
4
4
  // 而不是只给一条 `docs get` 命令——agent 少跑一轮 nx-kv(任务原文口径:
@@ -1,4 +1,4 @@
1
- // tasktool/groupctx.js —— **项目组上下文封装**(交付层)。
1
+ // delivery/groupctx.ts —— **项目组上下文封装**(交付层)。
2
2
  //
3
3
  // todo.md 的原文口径:
4
4
  // 「如果是获得在项目组的仓库的一个任务,该对返回的提示词进行封装,就比如说返回