@sema-agent/server 7.49.0 → 7.50.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.
package/USAGE.md CHANGED
@@ -554,6 +554,45 @@ SEMA_PARENT_PID=$$ # 壳把自己的 pid 传给它 spawn 出来的引擎
554
554
  PID 1 是典型;终端 / launchd / systemd 不在此列),pid 表项仍在,探活同样判"活着"。两条同源:
555
555
  `kill(pid, 0)` 是**弱身份**判据。这条腿是**尽力自愈**,不是强一致的父子生命周期绑定。
556
556
 
557
+ **可选 — 附着租约自退(`ENGINE_AUTO_EXIT`,默认关)**
558
+
559
+ 上一节那条腿盯的是「**生我的那个 pid** 还在吗」。多个壳会话共享**同一只**引擎时它问错了问题:首壳退了、
560
+ peer 还在用。这条腿问的是那个真正的问题 —— **还有没有人附着**。
561
+
562
+ ```bash
563
+ ENGINE_AUTO_EXIT=true # 壳 spawn 引擎时注入;服务器部署**不要**设
564
+ ENGINE_LINGER_MS=60000 # 可选,默认 60s,下界 1000
565
+ ```
566
+
567
+ - **默认 `false` = 整件不装配**,存量部署逐字零变化。一台 k8s 上的 replica 半夜没人用是**正常低谷**,
568
+ 不是「该退了」;自退只对**壳自 spawn 的本机引擎**有意义,所以由壳在 spawn 时显式打开。
569
+ - **自退判据 = 三合取,且必须连续满 `ENGINE_LINGER_MS`**:① 没有附着的 SSE 流;② 没有在飞的 run
570
+ (与 `/health` 的 `inflight` **同一个读数**);③ 没有非探针 HTTP 触点(`/health`、`/metrics*` 不算 ——
571
+ 探针不是「有人在用」),**且**最后一条附着流断开也已满窗。任何一条不满足即把计时**清零**。
572
+ (最后那半句不是修辞:一条活得比 15s 拍频还短的流整个生命周期都落在两拍之间,自查腿从没看见过它 ——
573
+ 只有在**关闭点**记一笔离场时刻,「已经没人附着满 N 秒」才是精确的。)
574
+ - **「run 在跑就不退」是结构性保护,没有旋钮可以关掉它** —— 最后一个壳关掉、后台 run 还在烧的场景里,
575
+ 引擎必须活到那条 run 落地。
576
+ - **`ENGINE_LINGER_MS` 防的是壳重启窗**:壳崩了/重启的 1-3s 里附着数确实是 0,窗太短就在用户眼皮底下
577
+ 把引擎杀了。窗内任何一次重连即清零重新起算。
578
+ - **实际等待 = `[ENGINE_LINGER_MS, ENGINE_LINGER_MS + 15s)`**:自查拍频固定 15s(不开旋钮),最后一条
579
+ 租约灭在两拍之间时要到下一拍才被看见。方向是**保守**的(只会等更久)。
580
+ - 判死后走的是**和 `SIGTERM` 逐字同一条**优雅排空腿:拒新提交(503 + `Retry-After`)→ 候在途腿跑完 →
581
+ 停机。in-flight 的 run 照常结算 / park,**不是**裸退出;而且因为是受控退,`crash-last.json` 会被清掉
582
+ ⇒ 下次启动**不会**把这次自退误读成崩溃。
583
+ - 日志:装配时一行 `engine_lease_armed`(带 `lingerMs`),自退时一行 `engine_lease_auto_exit`
584
+ (带 `attached` / `inflight` / `idleForMs` 三个读数),随后就是那条 `draining_started`。
585
+ - **坏值拒启**:`ENGINE_LINGER_MS` 必须是落在 **`[1000, 86400000]`(1s…24h)** 的整数毫秒,**两端都承重**:
586
+ 下界防 `ENGINE_LINGER_MS=60`(以为单位是秒)—— 60ms 的窗等于把整条防误杀腿静默摘掉;上界防 `1e100`
587
+ 这类值 —— 它能过朴素的「是正整数吗」检查,而计时**永远追不上它**,于是配置看着好好的、自退其实整件
588
+ 失效。两端一律**拒启并指路**而不是夹取。本键**没有**「关掉」的取值 —— 关自退的唯一开关是
589
+ `ENGINE_AUTO_EXIT=false`。校验**不看** `ENGINE_AUTO_EXIT` 的表态:否则手滑值可以在一台今天关着自退的
590
+ 机器上一直躺着,等哪天有人打开时当场生效。
591
+ - 🔴 **已知收窄(成文,现版不收)**:**parked-待赎回** 的 run 不在判据里,只看 running。数 parked 要发
592
+ SQL,而这是一只每 15s 无限期跑下去的定时器 —— 不值得为它引入周期性 SQL 轮询。parked 行是 **durable**
593
+ 的,引擎退掉不丢:下次起来(壳的下一次 spawn)仍可赎回,伤害面是**赎回被推迟**而不是丢失。
594
+ - 与 `SEMA_PARENT_PID` 是**两件**,可以同时设,各走各的判据。
595
+
557
596
  **布尔旋钮的取值与极性(运维必读)**
558
597
 
559
598
  布尔 env **只认 `true` / `false` 两个字面量**。写成 `1` / `yes` / `TRUE` ⇒ 该旋钮退回自己的缺省值,并在启动时
@@ -0,0 +1,95 @@
1
+ /**
2
+ * #118(黑板 [5371]②,cli [5394] 认领消费)—— 引擎**附着租约自退**。
3
+ *
4
+ * ## 防的是什么
5
+ *
6
+ * 壳自 spawn 引擎的本机形里,壳正常退出之后引擎**继续活着**:占端口、占库连接池、占模型配额,而且
7
+ * 用户已经离开了、再没有人会关心它。#219 的 parent 监视只覆盖「生我的那个 pid 没了」这一形,而
8
+ * **多壳共享一只引擎**时它结构性地问错了问题 —— 首壳退了 peer 还在用,监视父 pid 要么误杀要么永不触发。
9
+ * 本件问的是那个真正的问题:**还有没有人附着**。
10
+ *
11
+ * ## 机制(逐条都是契约)
12
+ *
13
+ * 1. **opt-in**:`ENGINE_AUTO_EXIT` 默认 **false** ⇒ 整件不装配(`boot/shutdown.ts` 连本函数都不调)。
14
+ * 服务器部署形逐字零变化 —— 一台 k8s 上的 replica 半夜没人用不是「该退了」,是**正常的低谷**。
15
+ * 壳 spawn 形由壳在 spawn 时显式注入 `true`。**部署级旋钮无条件生效**:它不派生自任何客户端表态
16
+ * (本仓 operator-knob 铁律,#153 同病两犯史),`ENGINE_LINGER_MS` 的坏值校验也不挂在本键的表态上。
17
+ * 2. **自退判据 = 三合取,且必须持续满 `lingerMs`**:
18
+ * · 附着租约集空(`attachedStreams() === 0`);
19
+ * · 无在飞 run(`inflight() === 0` —— **与 /health 的 `inflight` 同一只 getter**,不另立第二判据);
20
+ * · HTTP 触点静默(`now() - lastActivityAt() >= lingerMs`),**且**最后一条附着流的**离场**
21
+ * 也已满窗(`now() - lastAttachEndedAt() >= lingerMs` —— codex R1-F1,见该 dep 的注)。
22
+ * 任何一条不满足即把计时清零(连续性判据,不是累计)。
23
+ * 3. **为什么 HTTP 触点是独立的第三条**(不是冗余):纯轮询壳(零 SSE,每 2s 一次 `GET /v1/sessions`)
24
+ * 的请求是毫秒级的,而本腿 15s 采样一拍 —— 采样点几乎必然落在两次轮询之间,`attached` 与
25
+ * `inflight` **同时读到 0**。只看那两条的实现会在一只**正被使用**的引擎底下把它关掉。租约的
26
+ * 建立面按设计就是「首个 HTTP/SSE 接触」,不只是 SSE。
27
+ * 4. **退出 = 走 `drain`**,即 `boot/shutdown.ts` 里 SIGTERM 用的那一条 `drainThenShutdown`:拒新提交
28
+ * (503+Retry-After)→ 候在途腿跑完 → `hardShutdown`。in-flight run 的结算 / park 语义与人工停机
29
+ * **逐字一致**。本模块自己**从不** `process.exit`。
30
+ * 5. **crash-last 判别位**([5371] ④「flush 判别位标 auto-exit 非崩溃」):走上面那条受控退,
31
+ * `hardShutdown` 一进来就 `clearCrashLast` ⇒ 遗言文件被删 ⇒「文件在场 ⇔ 上次退出=崩溃」这条契约
32
+ * 对自退**自动成立**。刻意**不**另写一条 `reason:"auto-exit"` 的遗言行:那会把该文件的语义从
33
+ * 「崩溃判别位」稀释成「上次退出记录」,而运维读它就是为了判前者(见 `crash-last.ts` 顶注)。
34
+ * 自退的可观察面是 `engine_lease_auto_exit` + 随后的 `draining_started` 两行日志。
35
+ * 6. **interval 必须 unref**:自退检查腿不得成为「进程本该退出却退不掉」的理由(与 drain tick 刻意
36
+ * 不 unref 的取向相反 —— 那一只 tick 就是停机驱动,这一只只是观察者)。
37
+ * 7. **装配即起算**:armed 那一刻若已空闲,计时**当场**开始。覆盖的是「壳 spawn 了引擎,自己却在
38
+ * 第一次接触之前就崩了」这一形 —— 那只引擎一辈子没有过租约,正是本件要收的孤儿。
39
+ * 8. **实际窗 = `[lingerMs, lingerMs + tickMs)`**(采样腿的固有滞后,成文而非隐藏):最后一条租约是在
40
+ * **两拍之间**灭的,本腿要到下一拍才看见,空闲计时从**那一拍**起表。⇒ 真实等待比 `lingerMs` 多出
41
+ * 0~1 拍。方向是**保守**的(等得更久,不会更早),所以不修 —— 要修就得让 SSE 关闭点反向通知本腿,
42
+ * 那是把一只自洽的观察者改成有状态的被通知者,不值当。运维读数以 `engine_lease_auto_exit.idleForMs`
43
+ * 为准(它报的是真实空闲时长,不是 `lingerMs`)。
44
+ *
45
+ * ## 已知收窄(成文,现版不收)
46
+ *
47
+ * · **parked-待赎回 run 不入判据**:设计稿 ② 原文含「无 running/parked-待赎回 run」,本版**只看
48
+ * running**。理由 = 廉价读口不存在:parked 是 store 里的**durable 行**,数它必须发 SQL,而本腿是一只
49
+ * 每 15s 无限期跑下去的定时器 —— 为它引入周期性 SQL 轮询,代价压过收益。**bg run 在跑不退**那一臂
50
+ * 已由 `inflight()` 结构性守住;parked 行是 durable 的,引擎退掉不丢:下次起来仍可赎回(spawn 形里
51
+ * 「下次」就是壳的下一次 spawn)。伤害面 = 一条 parked run 的赎回被推迟到下次启动,不是丢失。
52
+ * 这是**允许的收窄,不是静默**:有了廉价 parked 计数口(或本腿改成事件驱动)之后应当补上。
53
+ * · **`/health` 探针不算触点**:`lastActivityAt` 的属主(`http/server.ts` 的 `noteActivity`)按既有契约
54
+ * 把 `/health` 与 `/metrics*` 排除在活性时钟之外(常态采样噪音不得把空闲窗永远撑开)。⇒ 一只**只**
55
+ * 轮询 `/health` 的壳不构成租约。这与 SIGHUP 空闲窗的判据同源,刻意保持一致而非在本腿另开一份。
56
+ */
57
+ /** 自退检查拍频。**固定值,不开 env 旋钮**(与 `PARENT_WATCH_INTERVAL_MS` 同纪律:有真需求再议,防旋钮增殖)。 */
58
+ export declare const ENGINE_LEASE_TICK_MS = 15000;
59
+ export interface EngineLeaseDeps {
60
+ /** 活附着数 = 本副本当前**打开着的 SSE 流**条数(`drainState.attachedStreams`,由 `createHttpServer` 自赋)。 */
61
+ attachedStreams: () => number;
62
+ /** 在飞腿数 —— **`/health` 的 `inflight` 同一只 getter**(`drainState.inflight`)。不自建第二套计数:
63
+ * 两套计数迟早分叉,而分叉的那一侧恰好是「以为没 run 了」就把引擎关掉。 */
64
+ inflight: () => number;
65
+ /** 最后一次**非探针** HTTP 触点的单调时刻(`drainState.lastActivityAt`)。
66
+ * ⚠️ 时基契约:必须与本模块的 `now` 同为 `performance.now()` 单调域(`server.ts` 的打点即该域)——
67
+ * 混入 `Date.now()` 会让 `now() - lastActivityAt` 差出 epoch 量级,静默判据恒真。 */
68
+ lastActivityAt: () => number;
69
+ /** 最后一条**附着流断开**的单调时刻(`drainState.lastAttachEndedAt`)。
70
+ *
71
+ * 🔴 codex 对抗复审 R1-F1(验真后修):`attachedStreams` 是**采样**面,`lastActivityAt` 记的是
72
+ * **入场**(admission)时刻 —— 两者合起来仍漏一个形:一条**整个生命周期都落在两拍之间**的流
73
+ * (活得比拍频短)从没被任何一拍看见,于是 `idleSince` 保持着这条流出现**之前**的旧值,而
74
+ * 「已经没人附着满 lingerMs」这句承诺可以提前至多一整拍兑现。补这条**离场**触点之后,判据变成
75
+ * 精确的「距最后一次附着结束已满 lingerMs」。缺席(0)= 本进程还没有过附着流结束。 */
76
+ lastAttachEndedAt: () => number;
77
+ /** 已有停机属主(`closing || draining`)时本腿站下:一次正在进行的优雅 drain 不该被「也没人附着了」
78
+ * 升级成第二信号硬停(那会斩掉 in-flight leg)。 */
79
+ isStopped: () => boolean;
80
+ /** 自退出口 = SIGTERM 那条 graceful drain(见顶注 4/5)。 */
81
+ drain: () => void;
82
+ /** 三合取**持续**满这么久才触发(`config.engineLingerMs`,默认 60s)。 */
83
+ lingerMs: number;
84
+ log: (event: string, fields: Record<string, unknown>) => void;
85
+ /** 检查拍频 seam(测试)。缺省 {@link ENGINE_LEASE_TICK_MS}。 */
86
+ tickMs?: number;
87
+ /** 时钟 seam,缺省 `() => performance.now()`(**单调**域)。用 `Date.now()` 会让 NTP/手动跳变吞掉或
88
+ * 拉长 linger 窗 —— 前跳把窗直接跳过去 = 在一只还有人用的引擎底下提前关停。 */
89
+ now?: () => number;
90
+ }
91
+ /** 装配并**立刻**起自退检查(装配即 `engine_lease_armed`)。返回停机链用的 `stop()`。 */
92
+ export declare function createEngineLeaseTracker(deps: EngineLeaseDeps): {
93
+ stop(): void;
94
+ };
95
+ //# sourceMappingURL=engine-lease.d.ts.map
@@ -0,0 +1,56 @@
1
+ export const ENGINE_LEASE_TICK_MS = 15_000;
2
+ export function createEngineLeaseTracker(deps) {
3
+ const tickMs = deps.tickMs ?? ENGINE_LEASE_TICK_MS;
4
+ const now = deps.now ?? (() => performance.now());
5
+ let stopped = false;
6
+ let idleSince = now();
7
+ deps.log("engine_lease_armed", {
8
+ lingerMs: deps.lingerMs,
9
+ tickMs,
10
+ note: "engine self-exits once NO shell session is attached AND no run is in flight AND no HTTP touch, " +
11
+ "continuously for lingerMs — via the same graceful drain as SIGTERM. Set ENGINE_AUTO_EXIT=false to disable.",
12
+ });
13
+ const timer = setInterval(() => {
14
+ if (stopped)
15
+ return;
16
+ if (deps.isStopped()) {
17
+ stop();
18
+ return;
19
+ }
20
+ const attached = deps.attachedStreams();
21
+ const inflight = deps.inflight();
22
+ if (attached > 0 || inflight > 0) {
23
+ idleSince = null;
24
+ return;
25
+ }
26
+ const t = now();
27
+ if (idleSince === null) {
28
+ idleSince = t;
29
+ return;
30
+ }
31
+ if (t - deps.lastActivityAt() < deps.lingerMs)
32
+ return;
33
+ if (t - deps.lastAttachEndedAt() < deps.lingerMs)
34
+ return;
35
+ if (t - idleSince < deps.lingerMs)
36
+ return;
37
+ stop();
38
+ deps.log("engine_lease_auto_exit", {
39
+ attached,
40
+ inflight,
41
+ lingerMs: deps.lingerMs,
42
+ idleForMs: Math.round(t - Math.max(idleSince, deps.lastActivityAt(), deps.lastAttachEndedAt())),
43
+ note: "no attached shell session, no in-flight run and no HTTP touch for the full linger window — entering the SIGTERM graceful drain path",
44
+ });
45
+ deps.drain();
46
+ }, tickMs);
47
+ timer.unref?.();
48
+ function stop() {
49
+ if (stopped)
50
+ return;
51
+ stopped = true;
52
+ clearInterval(timer);
53
+ }
54
+ return { stop };
55
+ }
56
+ //# sourceMappingURL=engine-lease.js.map
@@ -64,6 +64,8 @@ export interface ShutdownCtx {
64
64
  since?: number;
65
65
  inflight?: () => number;
66
66
  lastActivityAt?: () => number;
67
+ attachedStreams?: () => number;
68
+ lastAttachEndedAt?: () => number;
67
69
  };
68
70
  /** #131-2:store 活体探针(main 建)——不停的话 hardShutdown 后仍对正在关闭的池发探针,刷假
69
71
  * store_probe_dead 告警(文件头契约 3 的同族漏网)。缺席 = 该部署形没建探针。 */
@@ -2,6 +2,7 @@ import { Runner, defaultTaskRegistry } from "@sema-agent/core";
2
2
  import { writeCrashLast, clearCrashLast, readCrashLast } from "./crash-last.js";
3
3
  import { createSighupIdleHandler } from "../sighup-idle.js";
4
4
  import { createParentWatch } from "../parent-watch.js";
5
+ import { createEngineLeaseTracker } from "./engine-lease.js";
5
6
  import { isScriptRealmRejection, describeRejectionReason } from "../orchestration/hardened-vm-runner.js";
6
7
  export function installShutdownHandlers(ctx) {
7
8
  const { config, logger, server, reaper, fleetReconcile, retentionLane, releaseRetentionLease, otelExporter, breakerState, costQuota, rateLimiter, runner, subRunner, lspManager, workflowNotifyJournal, fleetClient, backend, drainState, storeLiveProbe, configCenter, version, } = ctx;
@@ -45,6 +46,7 @@ export function installShutdownHandlers(ctx) {
45
46
  process.on("unhandledRejection", onUnhandledRejection);
46
47
  let closing = false;
47
48
  let parentWatch;
49
+ let engineLease;
48
50
  const hardShutdown = () => {
49
51
  if (closing)
50
52
  return;
@@ -62,6 +64,7 @@ export function installShutdownHandlers(ctx) {
62
64
  .catch(() => undefined);
63
65
  fleetReconcile.reconciler.stop();
64
66
  parentWatch?.stop();
67
+ engineLease?.stop();
65
68
  storeLiveProbe?.stop();
66
69
  configCenter?.stopRefreshLoop();
67
70
  otelExporter?.stop();
@@ -145,6 +148,18 @@ export function installShutdownHandlers(ctx) {
145
148
  log: (event, fields) => logger.info(event, fields),
146
149
  });
147
150
  }
151
+ if (config.engineAutoExit) {
152
+ engineLease = createEngineLeaseTracker({
153
+ attachedStreams: () => drainState.attachedStreams?.() ?? 0,
154
+ inflight: () => drainState.inflight?.() ?? 0,
155
+ lastActivityAt: () => drainState.lastActivityAt?.() ?? 0,
156
+ lastAttachEndedAt: () => drainState.lastAttachEndedAt?.() ?? 0,
157
+ isStopped: () => closing || draining,
158
+ drain: drainThenShutdown,
159
+ lingerMs: config.engineLingerMs,
160
+ log: (event, fields) => logger.info(event, fields),
161
+ });
162
+ }
148
163
  let disposed = false;
149
164
  return {
150
165
  dispose: () => {
@@ -162,6 +177,7 @@ export function installShutdownHandlers(ctx) {
162
177
  drainTick = undefined;
163
178
  }
164
179
  parentWatch?.stop();
180
+ engineLease?.stop();
165
181
  },
166
182
  };
167
183
  }
@@ -1223,6 +1223,25 @@ export interface ServiceConfigFlat {
1223
1223
  * 已知残余(成文,不收窄):两拍窗口内 pid 被系统复用 ⇒ 探活看到的是**新**进程,监视判"父还活着"
1224
1224
  * 而不自退。监视是**尽力自愈**、不是强一致的父子生命周期绑定。 Env: `SEMA_PARENT_PID`。 */
1225
1225
  parentPid?: number;
1226
+ /** #118(黑板 [5371]②):引擎**附着租约自退**总开关。壳自 spawn 引擎的本机形里,壳退出之后引擎
1227
+ * 继续活着 = 占端口/占库连接/占模型配额的孤儿。设了本键 = 引擎每 15s 自查一次「还有没有人附着」,
1228
+ * **三合取**(无附着 SSE 流 ∧ 无在飞 run ∧ 无非探针 HTTP 触点)**持续**满 {@link engineLingerMs}
1229
+ * 才自退,且自退走的是与 SIGTERM **逐字同一条** graceful drain 腿(不是裸 `process.exit`)。
1230
+ * **默认 false** —— 服务器部署形永不自退(k8s replica 半夜没人用是正常低谷,不是「该退了」);
1231
+ * 壳 spawn 形由壳显式注入 `true`。**部署级旋钮无条件生效**,不派生自任何客户端表态。
1232
+ * 与 {@link parentPid} 是**两件**:那条问「生我的 pid 还在吗」(多壳共享一引擎时结构性问错),
1233
+ * 这条问「还有没有人附着」。两条可同时装,各走各的判据。 Env: `ENGINE_AUTO_EXIT`(默认 off)。 */
1234
+ engineAutoExit: boolean;
1235
+ /** #118:自退前三合取必须**持续**成立的静默窗。默认 60s —— 防的是**壳重启窗**:壳崩了/重启的 1-3s
1236
+ * 里附着数确实是 0,窗太短就在用户眼皮底下把引擎杀了,窗内任何一次重连即把计时清零。
1237
+ * {@link engineAutoExit} 关着时本值不生效,但**照样解析、坏值照样拒启**(部署级旋钮的解析不挂在
1238
+ * 另一个旋钮的表态上 —— 否则手滑值躺在配置里,等哪天有人打开自退时当场生效且从没人报过警)。
1239
+ * **坏值拒启**(#210 A 档):非安全整数 / 落在 `[1000, 86400000]`(1s…24h)之外一律 boot 期 throw
1240
+ * 带指路 —— 「把 60(秒)填进一个毫秒键」是这类旋钮最常见的手滑,而 60ms 的 linger 等于把整条防误杀
1241
+ * 腿静默摘掉;上界那一侧同样承重(codex R1-F2):`1e100` 能过朴素的「正整数吗」检查,而 elapsed
1242
+ * **永远追不上它** ⇒ 看着配好了、自退其实整件失效。要「永不自退」请用 `ENGINE_AUTO_EXIT=false`。
1243
+ * Env: `ENGINE_LINGER_MS`(默认 60000,闭区间 [1000, 86400000])。 */
1244
+ engineLingerMs: number;
1226
1245
  /** E23 (shell-host contract): inbound MCP elicitation HITL. When enabled, the service mounts `RunnerDeps.onElicit` + the
1227
1246
  * `POST /v1/elicitations/:id/respond` route so an opted-in MCP server (`McpServerSpec.elicitation`, itself default
1228
1247
  * OFF) can ask the END USER for input mid-tool-call. LIVE-ONLY (no durable suspend; lost on crash/replica-change).
@@ -1496,7 +1515,7 @@ export type ServiceAuthConfig = Pick<ServiceConfigFlat, "authToken" | "authToken
1496
1515
  /** 组:orchestration(编排 + 执行车道 + 沙箱面 + workflow/后台 agent 存留)。 */
1497
1516
  export type ServiceOrchestrationConfig = Pick<ServiceConfigFlat, "remoteExec" | "worktreeIsolation" | "leaderEnabled" | "leaderFanoutEnabled" | "routerEnabled" | "selfOrchestrationEnabled" | "selfOrchestrationModels" | "selfOrchestrationWorkerIsolation" | "forkEnabled" | "experimentalObserverAgents" | "schedulerEnabled" | "schedulerSessionWakeup" | "schedulerSessionLifetime" | "schedulerStorePath" | "planModeEnabled" | "hooksTimeoutMs" | "workflowRunStoreBackend" | "workflowOrphanGraceMs" | "workflowJournalRetentionMs" | "workflowRunRetentionMs" | "usageWindows" | "workflowAgentsReadOnly" | "workflowSizeGuideline" | "backgroundAgentRetentionMs" | "backgroundAgentStaleRunningMs" | "backgroundAgentParkClaimStaleMs" | "delegationEntryCaps" | "rosterRetentionMs" | "leaderRunStaleMs" | "scratchpadSweepTtlMs" | "sandboxPkgSource" | "sessionAutoTitle" | "selectEnvironmentTool" | "envFactsEnabled" | "toolDeferLongtail" | "lspEnabled" | "lspHostEnabled" | "imageBakes" | "readFace" | "readDenyPatterns" | "readDenyBuiltinTiers" | "readDenyBuiltinExclude">;
1498
1517
  /** 组:limitsHttp(HTTP 面 + 各类上限/配额/回收窗)。 */
1499
- export type ServiceLimitsHttpConfig = Pick<ServiceConfigFlat, "port" | "attachmentOrphanGraceMs" | "workspaceFileMaxBytes" | "attachmentMaxBytes" | "attachmentMimeAllowlist" | "attachmentUnboundTtlMs" | "infraCostRates" | "drainGraceMs" | "sighupIdleGraceMs" | "parentPid" | "rateLimitPerMin" | "maxTaskCostUsd" | "maxTaskTokens" | "maxPrincipalCostUsd" | "costQuotaWindowSec" | "reapIntervalSec" | "runStaleSec" | "toolResultTtlSec">;
1518
+ export type ServiceLimitsHttpConfig = Pick<ServiceConfigFlat, "port" | "attachmentOrphanGraceMs" | "workspaceFileMaxBytes" | "attachmentMaxBytes" | "attachmentMimeAllowlist" | "attachmentUnboundTtlMs" | "infraCostRates" | "drainGraceMs" | "sighupIdleGraceMs" | "parentPid" | "engineAutoExit" | "engineLingerMs" | "rateLimitPerMin" | "maxTaskCostUsd" | "maxTaskTokens" | "maxPrincipalCostUsd" | "costQuotaWindowSec" | "reapIntervalSec" | "runStaleSec" | "toolResultTtlSec">;
1500
1519
  /** 组:observability(可观测)。 */
1501
1520
  export type ServiceObservabilityConfig = Pick<ServiceConfigFlat, "metricsToken" | "traceToken" | "toolTrace" | "traceThinking" | "logLevel" | "otel">;
1502
1521
  /** 组:integrations(外部集成)。`mcpServers` / `a2aPeers` 由 sema-registry 适配器填(无 env 腿),归本组。 */
package/dist/config.js CHANGED
@@ -962,6 +962,24 @@ function parentPidEnv() {
962
962
  }
963
963
  return { parentPid: n };
964
964
  }
965
+ const ENGINE_LINGER_FLOOR_MS = 1_000;
966
+ const ENGINE_LINGER_CEIL_MS = 24 * 60 * 60 * 1_000;
967
+ function engineLingerMsEnv() {
968
+ const raw = process.env.ENGINE_LINGER_MS;
969
+ if (raw === undefined || raw.trim() === "")
970
+ return 60_000;
971
+ const n = parseNumOrFail("ENGINE_LINGER_MS", raw);
972
+ if (!Number.isSafeInteger(n) || n < ENGINE_LINGER_FLOOR_MS || n > ENGINE_LINGER_CEIL_MS) {
973
+ throw new Error(`env ENGINE_LINGER_MS="${raw}" must be an INTEGER number of MILLISECONDS in ` +
974
+ `[${ENGINE_LINGER_FLOOR_MS}, ${ENGINE_LINGER_CEIL_MS}] (1s … 24h) — it is how long ` +
975
+ `"nobody attached, no run in flight, no HTTP touch" must hold CONTINUOUSLY before the engine self-exits. ` +
976
+ `A value like "60" is the seconds-for-milliseconds slip: a 60ms window kills the engine inside a shell's own ` +
977
+ `restart gap. A value like "1e100" passes a naive "is it a positive integer" check but the elapsed clock can ` +
978
+ `NEVER reach it, which silently turns self-exit off while looking configured. ` +
979
+ `There is no "disable" value here — set ENGINE_AUTO_EXIT=false to turn self-exit off entirely.`);
980
+ }
981
+ return n;
982
+ }
965
983
  function parseMemoryEmbedder(ctx) {
966
984
  const endpoint = process.env.MEMORY_EMBEDDER_ENDPOINT || undefined;
967
985
  const model = process.env.MEMORY_EMBEDDER_MODEL || undefined;
@@ -1472,6 +1490,8 @@ function parseLimitsHttpDomain() {
1472
1490
  drainGraceMs: clampEnvWithWarn("DRAIN_GRACE_MS", String(10 * 60 * 1000), LIMITS_HTTP_FLOORS.drainGraceMs),
1473
1491
  sighupIdleGraceMs: clampEnvWithWarn("SIGHUP_IDLE_GRACE_MS", String(120_000), 5_000),
1474
1492
  ...parentPidEnv(),
1493
+ engineAutoExit: boolEnv("ENGINE_AUTO_EXIT", false),
1494
+ engineLingerMs: engineLingerMsEnv(),
1475
1495
  rateLimitPerMin: numEnv("RATE_LIMIT_RPM", "0"),
1476
1496
  maxTaskCostUsd: numEnv("MAX_TASK_COST_USD", "0"),
1477
1497
  maxTaskTokens: numEnv("MAX_TASK_TOKENS", "0"),
@@ -1667,7 +1687,9 @@ const ORCHESTRATION_GROUP_KEYS = [
1667
1687
  ];
1668
1688
  const LIMITS_HTTP_GROUP_KEYS = [
1669
1689
  "port", "attachmentOrphanGraceMs", "workspaceFileMaxBytes", "attachmentMaxBytes", "attachmentMimeAllowlist",
1670
- "attachmentUnboundTtlMs", "infraCostRates", "drainGraceMs", "sighupIdleGraceMs", "parentPid", "rateLimitPerMin", "maxTaskCostUsd",
1690
+ "attachmentUnboundTtlMs", "infraCostRates", "drainGraceMs", "sighupIdleGraceMs", "parentPid",
1691
+ "engineAutoExit", "engineLingerMs",
1692
+ "rateLimitPerMin", "maxTaskCostUsd",
1671
1693
  "maxTaskTokens", "maxPrincipalCostUsd", "costQuotaWindowSec", "reapIntervalSec", "runStaleSec", "toolResultTtlSec",
1672
1694
  ];
1673
1695
  const OBSERVABILITY_GROUP_KEYS = ["metricsToken", "traceToken", "toolTrace", "traceThinking", "logLevel", "otel"];
@@ -7,7 +7,7 @@ export type SubagentTailFrame = Record<string, unknown> & {
7
7
  type: string;
8
8
  };
9
9
  /** core forward 事件 → wire 投影(sync 主流五分支同源;不认识的帧类型=null 不发——tail 面只承诺
10
- * content 五类,新 core 帧类型先经这里显式收编再出 wire)。 */
10
+ * content 六类,新 core 帧类型先经这里显式收编再出 wire)。 */
11
11
  export declare function projectTailFrame(e: Record<string, unknown> & {
12
12
  type?: string;
13
13
  }): SubagentTailFrame | null;
@@ -18,6 +18,16 @@ export function projectTailFrame(e) {
18
18
  const td = e;
19
19
  return { type: "text_delta", delta: td.delta, ...(td.eventId ? { eventId: td.eventId } : {}), ...(td.parentToolCallId ? { parentToolCallId: td.parentToolCallId } : {}) };
20
20
  }
21
+ if (t === "text_end") {
22
+ if (typeof e.content !== "string" || e.content.length === 0)
23
+ return null;
24
+ return {
25
+ type: "text_end",
26
+ content: e.content,
27
+ ...(typeof e.eventId === "string" ? { eventId: e.eventId } : {}),
28
+ ...(typeof e.parentToolCallId === "string" ? { parentToolCallId: e.parentToolCallId } : {}),
29
+ };
30
+ }
21
31
  if (t === "reasoning_delta") {
22
32
  const rd = e;
23
33
  return { type: "reasoning_delta", delta: redactSecrets(rd.delta), ...(rd.eventId ? { eventId: rd.eventId } : {}), ...(rd.parentToolCallId ? { parentToolCallId: rd.parentToolCallId } : {}) };
@@ -60,7 +60,7 @@ async function decideIdempotentReplay(cs, args) {
60
60
  return undefined;
61
61
  }
62
62
  }
63
- if (row.boundInputHash !== (binding.boundInputHash ?? null))
63
+ if (binding.boundInputHash !== undefined && row.boundInputHash !== binding.boundInputHash)
64
64
  return undefined;
65
65
  if (row.decision !== decision)
66
66
  return undefined;
@@ -366,7 +366,7 @@ async function handleSessionsBody(req, res, url, ctx, miss) {
366
366
  sendError(res, 503, "state.sse_probe_cap", "session-events probe cap reached — fall back to /head polling");
367
367
  return;
368
368
  }
369
- res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache, no-transform", connection: "keep-alive", "x-accel-buffering": "no" });
369
+ sseHeaders(res, { "cache-control": "no-cache, no-transform", "x-accel-buffering": "no" });
370
370
  writeRaw(": connected\n\n");
371
371
  started = true;
372
372
  if (preVal !== undefined)
@@ -331,6 +331,10 @@ async function handleTasksBody(req, res, url, ctx, miss) {
331
331
  const td = e;
332
332
  sseData(res, { type: "text_delta", delta: td.delta, ...(td.eventId ? { eventId: td.eventId } : {}), ...(td.parentToolCallId ? { parentToolCallId: td.parentToolCallId } : {}) });
333
333
  }
334
+ else if (t === "text_end") {
335
+ const tend = e;
336
+ sseData(res, { type: "text_end", content: tend.content, ...(tend.eventId ? { eventId: tend.eventId } : {}), ...(tend.parentToolCallId ? { parentToolCallId: tend.parentToolCallId } : {}) });
337
+ }
334
338
  else if (t === "reasoning_delta") {
335
339
  const rd = e;
336
340
  sseData(res, { type: "reasoning_delta", delta: redactSecrets(rd.delta), ...(rd.eventId ? { eventId: rd.eventId } : {}), ...(rd.parentToolCallId ? { parentToolCallId: rd.parentToolCallId } : {}) });
@@ -459,7 +463,7 @@ async function handleTasksBody(req, res, url, ctx, miss) {
459
463
  else if (ev.type === "human_input") {
460
464
  sseData(res, { type: "human_input", ...humanInputEventData(ev) });
461
465
  }
462
- else if (ev.type === "text_delta" || ev.type === "turn_end" || ev.type === "message_committed" || ev.type === "context_usage") {
466
+ else if (ev.type === "text_delta" || ev.type === "text_end" || ev.type === "turn_end" || ev.type === "message_committed" || ev.type === "context_usage") {
463
467
  const e = ev;
464
468
  const ident = {
465
469
  ...(e.eventId !== undefined ? { eventId: e.eventId } : {}),
@@ -469,11 +473,13 @@ async function handleTasksBody(req, res, url, ctx, miss) {
469
473
  };
470
474
  const arm = ev.type === "text_delta"
471
475
  ? { type: "text_delta", delta: e.delta, ...ident }
472
- : ev.type === "turn_end"
473
- ? { type: "turn_end", ...(e.usage !== undefined ? { usage: e.usage } : {}), ...(e.usageMissing !== undefined ? { usageMissing: e.usageMissing } : {}), ...(e.stopReason !== undefined ? { stopReason: e.stopReason } : {}), ...ident }
474
- : ev.type === "message_committed"
475
- ? { type: "message_committed", entryId: e.entryId, role: e.role, ...(e.toolCallId !== undefined ? { toolCallId: e.toolCallId } : {}), ...ident }
476
- : { type: "context_usage", ...contextUsageEventData(ev), ...ident };
476
+ : ev.type === "text_end"
477
+ ? { type: "text_end", content: e.content, ...ident }
478
+ : ev.type === "turn_end"
479
+ ? { type: "turn_end", ...(e.usage !== undefined ? { usage: e.usage } : {}), ...(e.usageMissing !== undefined ? { usageMissing: e.usageMissing } : {}), ...(e.stopReason !== undefined ? { stopReason: e.stopReason } : {}), ...ident }
480
+ : ev.type === "message_committed"
481
+ ? { type: "message_committed", entryId: e.entryId, role: e.role, ...(e.toolCallId !== undefined ? { toolCallId: e.toolCallId } : {}), ...ident }
482
+ : { type: "context_usage", ...contextUsageEventData(ev), ...ident };
477
483
  sseData(res, arm);
478
484
  }
479
485
  else {
@@ -8,6 +8,20 @@
8
8
  * now imports them from here, so all ~500 existing `sendJson(...)` call sites are unchanged text.
9
9
  */
10
10
  import type { ServerResponse } from "node:http";
11
+ /**
12
+ * #118(黑板 [5371]②)—— 一条响应「是 SSE 流」的**在场标记**。
13
+ *
14
+ * 引擎附着租约把**打开着的 SSE 流**当作壳的附着租约来数,于是 `handle()` 需要判「这条响应是不是流」。
15
+ * 🔴 **不能靠读回 content-type**(本机 node v24.2.0 实测):`res.writeHead(status, headersObject)` 直接
16
+ * 把头写进网络缓冲,`res.getHeader("content-type")` / `res.hasHeader(...)` 在那之后**仍然**回
17
+ * `undefined` / `false`。照那条路写会得到一个恒为 0 的附着计数 —— 而恒 0 的方向恰好是「以为没人附着」
18
+ * ⇒ 在一只有人用的引擎底下触发自退。所以判据必须是我们自己在开流那一刻盖的这个章。
19
+ *
20
+ * symbol 键:不上 wire、不进 `JSON.stringify`、不与任何应用属性撞名。
21
+ */
22
+ export declare const SSE_RESPONSE_MARK: unique symbol;
23
+ /** 这条响应是否已开成 SSE 流(= 走过 {@link sseHeaders})。 */
24
+ export declare function isSseResponse(res: ServerResponse): boolean;
11
25
  export declare function sseHeaders(res: ServerResponse, extra?: Record<string, string>): void;
12
26
  /**
13
27
  * 一条 SSE 连接的墙钟上限。到点发一帧 `error`/`STREAM_MAX_DURATION`(**不是**静默关闭 —— 客户端分不出
package/dist/http/send.js CHANGED
@@ -1,4 +1,9 @@
1
+ export const SSE_RESPONSE_MARK = Symbol("sema.http.sse-response");
2
+ export function isSseResponse(res) {
3
+ return res[SSE_RESPONSE_MARK] === true;
4
+ }
1
5
  export function sseHeaders(res, extra) {
6
+ res[SSE_RESPONSE_MARK] = true;
2
7
  res.writeHead(200, {
3
8
  "content-type": "text/event-stream",
4
9
  "cache-control": "no-cache",
@@ -513,6 +513,9 @@ export interface ServiceDeploymentDeps {
513
513
  * • `inflight` is ASSIGNED BY createServer (a live union of this replica's in-flight legs: durable bg/resume
514
514
  * `inflightRuns` + live sync/resume streams `steerableRuns`) — main.ts polls it to know when drain is done.
515
515
  * • /health carries `draining:true` (k8s readiness 摘流信号) + `version` (build self-description).
516
+ * • #118:`attachedStreams` 同样 ASSIGNED BY createServer —— 本副本当前打开着的 SSE 流条数
517
+ * (= 引擎附着租约集的大小)。消费方是 `boot/engine-lease.ts` 的自退判据;与 `inflight` 同族,
518
+ * 同一个共享盒子,不另开第二条 main↔server 的通道。
516
519
  */
517
520
  drainState?: {
518
521
  draining: boolean;
@@ -520,6 +523,8 @@ export interface ServiceDeploymentDeps {
520
523
  reason?: string;
521
524
  inflight?: () => number;
522
525
  lastActivityAt?: () => number;
526
+ attachedStreams?: () => number;
527
+ lastAttachEndedAt?: () => number;
523
528
  };
524
529
  /** boot ready 门(b):false = registry 部署无显式 env 模型且首次 effective pull 尚未落 roster
525
530
  * (worker 只有占位模型)。计费提交 503 + /health 加性 `ready:false`。absent = 恒 ready(env 模型在/非
@@ -70,7 +70,7 @@ import { cascadeConfig } from "./run-meta.js";
70
70
  export { cascadeConfig };
71
71
  import { handleImages, createImagesLocal, coarseStatusForState, errorCodeForExit } from "./routes/images.js";
72
72
  export { coarseStatusForState, errorCodeForExit };
73
- import { sendJson, sendError, httpErrorCode, msg } from "./send.js";
73
+ import { sendJson, sendError, httpErrorCode, msg, isSseResponse } from "./send.js";
74
74
  import { authorized, systemFor, gatedPrincipal, explicitOperatorOk, isOperator } from "./principal-gate.js";
75
75
  import { buildActiveRunConflict, resumeEntryForGate } from "./active-run-conflict.js";
76
76
  export { explicitOperatorOk, isOperator };
@@ -179,6 +179,18 @@ export function createHttpServer(rawDeps) {
179
179
  };
180
180
  if (deps.drainState)
181
181
  deps.drainState.lastActivityAt = () => lastBillableActivityAt;
182
+ const openResponses = new Set();
183
+ let lastAttachEndedAt = 0;
184
+ if (deps.drainState) {
185
+ deps.drainState.attachedStreams = () => {
186
+ let n = 0;
187
+ for (const r of openResponses)
188
+ if (isSseResponse(r))
189
+ n++;
190
+ return n;
191
+ };
192
+ deps.drainState.lastAttachEndedAt = () => lastAttachEndedAt;
193
+ }
182
194
  if (deps.hookWakeBus) {
183
195
  deps.hookWakeBus.deliver = async (sessionId, text) => {
184
196
  try {
@@ -247,6 +259,8 @@ export function createHttpServer(rawDeps) {
247
259
  const admitted = method !== "OPTIONS" && url !== "/health" && !url.startsWith("/metrics");
248
260
  if (admitted)
249
261
  counters.admittedInflight++;
262
+ if (admitted)
263
+ openResponses.add(res);
250
264
  let logged = false;
251
265
  const reqState = { streamTaskId: undefined, streamDetached: false, source: null };
252
266
  const ctx = { deps: routeCtxBase.deps, registry: routeCtxBase.registry, helpers: routeCtxBase.helpers, local: routeCtxBase.local, legs: routeCtxBase.legs, req: reqState };
@@ -256,6 +270,8 @@ export function createHttpServer(rawDeps) {
256
270
  logged = true;
257
271
  if (admitted)
258
272
  counters.admittedInflight--;
273
+ if (openResponses.delete(res) && isSseResponse(res))
274
+ lastAttachEndedAt = performance.now();
259
275
  const route = routeLabel(method, url);
260
276
  const seconds = (Date.now() - startedAt) / 1000;
261
277
  const aborted = viaClose && !res.writableEnded;
@@ -216,7 +216,7 @@ export interface TaskRequestBody {
216
216
  skillsListing?: boolean;
217
217
  };
218
218
  /** C1 (core 1.219, subagent viewing pane): opt-in — forward a delegated child's live CONTENT events
219
- * (text_delta/reasoning_delta/tool_start/tool_end, each stamped `parentToolCallId`) onto this run's stream/durable
219
+ * (text_delta/text_end/reasoning_delta/tool_start/tool_end, each stamped `parentToolCallId`) onto this run's stream/durable
220
220
  * log, so a shell can render the child's transcript live ("enter 看详情"). Default OFF = progress-only (prior
221
221
  * behavior). Purely a render channel — core never merges the child stream into the parent's model context. */
222
222
  forwardSubagentEvents?: boolean;
@@ -30,13 +30,13 @@ export declare function ensurePgUsageWindowSchema(q: (text: string, params?: unk
30
30
  export declare class TiDBUsageWindowStore implements UsageWindowStore {
31
31
  private readonly pool;
32
32
  constructor(pool: MySqlPool);
33
- charge(key: string, tokens: number, at: number, windows: readonly UsageWindow[]): Promise<void>;
33
+ charge(key: string, tokens: number, at: number, windows: readonly UsageWindow[], costMicroUsd?: number | null): Promise<void>;
34
34
  read(key: string, windows: readonly UsageWindow[], now: number): Promise<readonly UsageWindowReading[]>;
35
35
  }
36
36
  export declare class PgUsageWindowStore implements UsageWindowStore {
37
37
  private readonly pool;
38
38
  constructor(pool: PgPool);
39
- charge(key: string, tokens: number, at: number, windows: readonly UsageWindow[]): Promise<void>;
39
+ charge(key: string, tokens: number, at: number, windows: readonly UsageWindow[], costMicroUsd?: number | null): Promise<void>;
40
40
  read(key: string, windows: readonly UsageWindow[], now: number): Promise<readonly UsageWindowReading[]>;
41
41
  }
42
42
  //# sourceMappingURL=usage-window-store-sql.d.ts.map
@@ -14,13 +14,24 @@ function parseRecord(raw, key) {
14
14
  }
15
15
  const badShape = (detail) => new Error(`usage_window record for key "${key}" has an invalid shape (${detail}) — refusing to mis-count a governance window`);
16
16
  const finite = (x) => typeof x === "number" && Number.isFinite(x);
17
+ const costKeys = (e) => {
18
+ if (e.costMicroUsd !== undefined && (typeof e.costMicroUsd !== "number" || !Number.isSafeInteger(e.costMicroUsd) || e.costMicroUsd < 0)) {
19
+ throw badShape("`costMicroUsd` present but not a non-negative safe integer");
20
+ }
21
+ if (e.costUnknown !== undefined && e.costUnknown !== true)
22
+ throw badShape("`costUnknown` present but not literal true");
23
+ return {
24
+ ...(e.costMicroUsd !== undefined ? { costMicroUsd: e.costMicroUsd } : {}),
25
+ ...(e.costUnknown === true ? { costUnknown: true } : {}),
26
+ };
27
+ };
17
28
  const slots = r.slots.map((s) => {
18
29
  if (s === null || typeof s !== "object")
19
30
  throw badShape("`slots` entry is not an object");
20
31
  const { at, tokens } = s;
21
32
  if (!finite(at) || !finite(tokens))
22
33
  throw badShape("`slots` entry has a non-finite-numeric `at`/`tokens`");
23
- return { at, tokens };
34
+ return { at, tokens, ...costKeys(s) };
24
35
  });
25
36
  const buckets = r.buckets.map((b) => {
26
37
  if (b === null || typeof b !== "object")
@@ -29,7 +40,7 @@ function parseRecord(raw, key) {
29
40
  if (!finite(windowMs) || windowMs <= 0 || !finite(openedAt) || !finite(tokens)) {
30
41
  throw badShape("`buckets` entry has a non-finite-numeric `windowMs`/`openedAt`/`tokens` (windowMs must be > 0)");
31
42
  }
32
- return { windowMs, openedAt, tokens };
43
+ return { windowMs, openedAt, tokens, ...costKeys(b) };
33
44
  });
34
45
  return { slots, buckets };
35
46
  }
@@ -54,7 +65,7 @@ export class TiDBUsageWindowStore {
54
65
  constructor(pool) {
55
66
  this.pool = pool;
56
67
  }
57
- async charge(key, tokens, at, windows) {
68
+ async charge(key, tokens, at, windows, costMicroUsd) {
58
69
  const c = await this.pool.getConnection();
59
70
  try {
60
71
  await c.beginTransaction();
@@ -68,7 +79,7 @@ export class TiDBUsageWindowStore {
68
79
  throw new Error(`usage_window row for key "${key}" vanished between ensure and lock — refusing to drop a governance charge`);
69
80
  }
70
81
  const record = parseRecord(rows[0].record, key);
71
- const next = chargeUsageRecord(record, tokens, at, windows);
82
+ const next = chargeUsageRecord(record, tokens, at, windows, costMicroUsd);
72
83
  await c.query(`UPDATE ${USAGE_WINDOW_TABLE} SET record = ?, updated_at_ms = ? WHERE usage_key = ?`, [JSON.stringify(next), at, key]);
73
84
  await c.commit();
74
85
  }
@@ -91,7 +102,7 @@ export class PgUsageWindowStore {
91
102
  constructor(pool) {
92
103
  this.pool = pool;
93
104
  }
94
- async charge(key, tokens, at, windows) {
105
+ async charge(key, tokens, at, windows, costMicroUsd) {
95
106
  const c = await this.pool.connect();
96
107
  try {
97
108
  await c.query("BEGIN");
@@ -105,7 +116,7 @@ export class PgUsageWindowStore {
105
116
  throw new Error(`usage_window row for key "${key}" vanished between ensure and lock — refusing to drop a governance charge`);
106
117
  }
107
118
  const record = parseRecord(rows[0].record, key);
108
- const next = chargeUsageRecord(record, tokens, at, windows);
119
+ const next = chargeUsageRecord(record, tokens, at, windows, costMicroUsd);
109
120
  await c.query(`UPDATE ${USAGE_WINDOW_TABLE} SET record = $1, updated_at_ms = $2 WHERE usage_key = $3`, [JSON.stringify(next), at, key]);
110
121
  await c.query("COMMIT");
111
122
  }
@@ -27,9 +27,9 @@ type _GuardBgNotif = AssertAllKeysHandled<Exclude<keyof BackgroundChildEvent, Bg
27
27
  type RosterProjected = "name" | "agentId" | "sessionId" | "toolUseId" | "owner" | "scope" | "sessionScoped" | "rootSessionId" | "model" | "modelFallback" | "createdAt";
28
28
  type _GuardRoster = AssertAllKeysHandled<Exclude<keyof RosterEntry, RosterProjected>>;
29
29
  type AskProjected = "toolName" | "toolCallId" | "args" | "message" | "sourceTaskId" | "fromSubagent" | "sourceAgentName" | "delegation" | "ruleOffers" | "persistedRuleShadowed" | "probeCause" | "ruleEvidence" | "requiresRealApproval";
30
- type AskExcluded = "preview" | "principal" | "riskAxes" | "boundInputHash" | "isDelegatedChild" | "probeReason" | "hasBidiControls";
30
+ type AskExcluded = "preview" | "principal" | "riskAxes" | "boundInputHash" | "isDelegatedChild" | "probeReason" | "hasBidiControls" | "previewWithheld";
31
31
  type _GuardAsk = AssertAllKeysHandled<Exclude<keyof AskRequest, AskProjected | AskExcluded>>;
32
- type TaskEventHandled = "text_delta" | "reasoning_delta" | "tool_start" | "tool_end" | "turn_end" | "compacted" | "diagnostics" | "message_committed" | "status" | "task_notification" | "task_progress" | "steering_injected" | "workspace_changed" | "done" | "context_usage" | "compaction_outcome" | "human_input" | "wiring_manifest";
32
+ type TaskEventHandled = "text_delta" | "reasoning_delta" | "tool_start" | "tool_end" | "turn_end" | "compacted" | "diagnostics" | "message_committed" | "status" | "task_notification" | "task_progress" | "steering_injected" | "workspace_changed" | "done" | "context_usage" | "compaction_outcome" | "human_input" | "wiring_manifest" | "text_end";
33
33
  type _GuardTaskEvent = AssertAllKeysHandled<Exclude<TaskEvent["type"], TaskEventHandled>>;
34
34
  type MailboxProjected = "seq" | "from" | "content" | "sentAt" | "hopChain";
35
35
  type _GuardMailbox = AssertAllKeysHandled<Exclude<keyof MailboxMessage, MailboxProjected>>;
@@ -45,7 +45,7 @@ type _GuardPermissionTraceReverse = AssertAllKeysHandled<Exclude<PermissionTrace
45
45
  type AssertNonEmpty<T> = [T] extends [never] ? "EMPTY" : never;
46
46
  type _GuardPermissionTraceNonEmpty = AssertAllKeysHandled<AssertNonEmpty<PermissionTraceKind>>;
47
47
  type SummaryProjected = "sessionId" | "scope" | "createdAt" | "gateKind" | "severity" | "spentMicroUsd" | "deadline" | "hasBidiControls";
48
- type SummaryExcluded = "token" | "toolInput" | "toolName" | "toolCallId" | "preview" | "principal" | "sourceTaskId" | "contentKind" | "restoreMode" | "checkpointId";
48
+ type SummaryExcluded = "token" | "toolInput" | "toolName" | "toolCallId" | "preview" | "principal" | "sourceTaskId" | "contentKind" | "restoreMode" | "checkpointId" | "previewWithheld";
49
49
  type _GuardInboxSummary = AssertAllKeysHandled<Exclude<keyof CheckpointSummary, SummaryProjected | SummaryExcluded>>;
50
50
  export type CoreKeysetGuards = [
51
51
  _GuardNotification,
@@ -14,7 +14,7 @@
14
14
  * 同文件 G1 e2e 的全码扇出夹具、以及契约文档附录 D.3 那张表(含**小节标题里的码数** —— 那道门本批
15
15
  * 刚立,立完就在下一次加码时自己咬住了)。
16
16
  */
17
- export declare const ENGINE_NOTICE_WIRE_CODES: readonly ["memory.session_polluted", "memory.harvest_quarantined", "memory.delegation_static_mark_waived", "memory.hold_opened", "memory.hold_released", "memory.hold_disposed", "task.turn_interrupted", "task.user_steer_undrained", "task.user_followup_undrained"];
17
+ export declare const ENGINE_NOTICE_WIRE_CODES: readonly ["memory.session_polluted", "memory.harvest_quarantined", "memory.delegation_static_mark_waived", "memory.hold_opened", "memory.hold_released", "memory.hold_disposed", "task.turn_interrupted", "steering.parked_input_blocked", "task.user_steer_undrained", "task.user_followup_undrained"];
18
18
  export type EngineNoticeWireCode = (typeof ENGINE_NOTICE_WIRE_CODES)[number];
19
19
  /** 白名单谓词(单点):路由与门都读这一个,不许第二处手抄码串。 */
20
20
  export declare function isEngineNoticeWireCode(code: string): code is EngineNoticeWireCode;
@@ -8,6 +8,7 @@ export const ENGINE_NOTICE_WIRE_CODES = [
8
8
  "memory.hold_released",
9
9
  "memory.hold_disposed",
10
10
  "task.turn_interrupted",
11
+ "steering.parked_input_blocked",
11
12
  "task.user_steer_undrained",
12
13
  "task.user_followup_undrained",
13
14
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sema-agent/server",
3
- "version": "7.49.0",
3
+ "version": "7.50.0",
4
4
  "description": "Sema Server — the server/API implementation layer for Sema, wiring core, registry, model providers, and cloud agent execution. Built on @sema-agent/core.",
5
5
  "type": "module",
6
6
  "license": "BUSL-1.1",
@@ -54,8 +54,8 @@
54
54
  "build:binary:run-local:darwin-arm64": "bun build --compile --target=bun-darwin-arm64 src/run-local.ts --outfile dist/run-local-darwin-arm64"
55
55
  },
56
56
  "dependencies": {
57
- "@sema-agent/core": "^5.61.0",
58
- "@sema-agent/settings-schema": "^1.0.0",
57
+ "@sema-agent/core": "5.63.0",
58
+ "@sema-agent/settings-schema": "1.0.0",
59
59
  "e2b": "^2.28.0",
60
60
  "libsodium-wrappers": "^0.8.4",
61
61
  "mysql2": "^3.22.4",