@sema-agent/server 3.11.0 → 3.12.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
@@ -67,6 +67,24 @@ ANTHROPIC_MAX_RETRIES=10 # 云 Anthropic 腿
67
67
  - 断路器**只在配了 `MODEL_GATEWAY_FALLBACK_URLS`(≥2 路)时才有意义**——它的价值是"开路即快速失败 → failover 立刻切备";单网关下它是 no-op(启动日志 `circuitBreakerNoop` 会提示)。只 `network/server/rate_limit` 计入连败,`auth`/`invalid_request` 不计(坏 key 熔断整网关无意义)。备用网关(最后一路)不套断路器。
68
68
  - **重试上限**(2026-07-31):两个键**不设就不传给引擎** —— 引擎默认当家。以前这里是 server 侧硬编码 `2`(openai 腿甚至零配置出口),而**显式传参压过引擎默认**,于是引擎抬默认对 server 部署毫无效果;第三方限流 provider 下"重试两次就放弃"正是由此而来。现在:不设=继承引擎默认(抬默认那天自动跟上),设了=按设的走。钉在 `test/brain.test.ts` 的「网关腿重试次数」两条上。
69
69
  - 断路器状态:配了 `SESSION_BACKEND=tidb` 时自动用**跨副本共享态**(TiDB `circuit_breaker` 表,写穿+刷新最终一致),否则进程内 Map。启动日志 `breakerState` 字段回显 `shared(tidb)`/`in-process`/`off`。详见 `design/27`。
70
+
71
+ **可选 — WebSearch 后端(core 只留注入口,不自带任何 provider)**
72
+ ```bash
73
+ WEB_SEARCH_PROVIDER=brave|tavily|searxng # 缺席 = 不装配(WebSearch 工具根本不挂,不是挂了报错)
74
+ WEB_SEARCH_API_KEY=… # brave/tavily 必填;只进 backend 闭包,永不进模型 prompt 或工具参数
75
+ WEB_SEARCH_ENDPOINT=https://searx.example # searxng 必填;brave/tavily 下是可选的 base-URL 覆盖(代理/测试)
76
+ WEB_SEARCH_MAX_RESULTS=10 # 1..20,返给模型的条数上限
77
+ WEB_SEARCH_TIMEOUT_MS=10000 # 单次搜索墙钟
78
+ ```
79
+ - 结果是**不可信输入**:core 会 `delimitUntrusted` 围栏并**重新施加** `allowed_domains`/`blocked_domains`
80
+ 地板,所以 backend 遵不遵守 `opts` 是优化不是正确性要求 —— 换 provider(包括换成自建 SearXNG)
81
+ 不会削弱域名地板。
82
+ - 🔴 **`WEB_SEARCH_ENDPOINT` 的语义是「引擎可达的地址」,不是「用户机器上可达的地址」**
83
+ (2026-07-31 与 cli 对齐的跨机契约句):壳(TUI/桌面/web)与引擎**可以不在同一台机器**。壳在用户
84
+ 笔记本上起的 localhost SearXNG,云端 worker 够不着 —— 所以「本地 SearXNG 兜底」这一档**只适用
85
+ 壳自 spawn 本地引擎的同机形**;壳连接远程引擎时该档整级跳过,由运维在**引擎侧**配 `WEB_SEARCH_*`。
86
+ 用户手填一个集中式 SearXNG 地址是合法形,照常放行。
87
+
70
88
  - **大工具结果落盘**(core 1.47/1.49):单条工具结果 > ~20000 字符时 core 把全文移出上下文、只留预览+ref,模型用 `read_tool_result` 按需分页回取。配了 TiDB 时自动用**durable `tool_result` 表**(跨副本 wake 仍能取回全文;否则 core 进程内默认 = 跨副本 wake 取不到→降级到预览,不崩)。`TOOL_RESULT_TTL_SEC`(默认 86400)按 TTL 回收(要 ≥ run 可恢复期)。启动日志 `toolResultStore` 回显 `shared(tidb)`/`in-process`。
71
89
 
72
90
  **可选 — 成本计量 + 预算闸(core 1.37)**
@@ -9,6 +9,26 @@
9
9
  */
10
10
  import { type ToolPolicy } from "@sema-agent/core";
11
11
  import type { ApprovalStore } from "./plugins/store-backend.js";
12
+ /**
13
+ * 运维**是否表达了门意图** —— boot 的单用户 allow-all 基线只在"零门意图"时才允许铺开,
14
+ * 所以这个谓词漏一格 = 那一格的意图被 allow-all 静默吞掉。
15
+ *
16
+ * 🔴 由来(2026-07-31 缝合审):这判据本来是 `main.ts` 里的一行内联表达式,只枚举了
17
+ * requireApproval / deny / durableApproval **三格**,漏了 `approvalNeverAuto`。漏的根因不是手滑,
18
+ * 是 `config-types.ts` 当时把 neverAuto 描述成 "Subset of approvalRequire" —— 若真是子集,
19
+ * 枚举 requireApproval 就覆盖了它。但本文件 inv#2 的执行面逐字相反(neverAuto ⊄ requireApproval),
20
+ * 于是「单用户 + 只配 APPROVAL_NEVER_AUTO=Bash」这一**语义完全合理**的形被判成零门意图 ⇒
21
+ * 落进 `createAllowDenyPolicy({})` 全面放行 ⇒ 那张"永远问人"的表从未进入任何 policy,Bash 直接跑。
22
+ *
23
+ * 判定挪到**与 inv#2 同一个文件**就是为了这个:两者再想漂开,得有人同时改这两段。
24
+ * 新增任何"门意图"配置项时,这里必须同步加一格(下面的表驱动测试会点名漏的那格)。
25
+ */
26
+ export declare function hasOperatorGateIntent(config: {
27
+ approvalRequire: readonly string[];
28
+ approvalDeny: readonly string[];
29
+ approvalNeverAuto: readonly string[];
30
+ durableApproval: boolean;
31
+ }): boolean;
12
32
  /**
13
33
  * Durable approval policy (core 1.67 / design/45): returns three-state **`ask`** for gated tools instead of
14
34
  * polling. With `TaskSpec.durableApproval` set + a `checkpointStore` on the Runner, core turns that `ask` into
package/dist/approval.js CHANGED
@@ -8,6 +8,26 @@
8
8
  * dropped at the task deadline instead of holding the worker — design/06 F4 self-protection.
9
9
  */
10
10
  import { createApprovalPolicy, canonicalToolName, uuidv7 } from "@sema-agent/core";
11
+ /**
12
+ * 运维**是否表达了门意图** —— boot 的单用户 allow-all 基线只在"零门意图"时才允许铺开,
13
+ * 所以这个谓词漏一格 = 那一格的意图被 allow-all 静默吞掉。
14
+ *
15
+ * 🔴 由来(2026-07-31 缝合审):这判据本来是 `main.ts` 里的一行内联表达式,只枚举了
16
+ * requireApproval / deny / durableApproval **三格**,漏了 `approvalNeverAuto`。漏的根因不是手滑,
17
+ * 是 `config-types.ts` 当时把 neverAuto 描述成 "Subset of approvalRequire" —— 若真是子集,
18
+ * 枚举 requireApproval 就覆盖了它。但本文件 inv#2 的执行面逐字相反(neverAuto ⊄ requireApproval),
19
+ * 于是「单用户 + 只配 APPROVAL_NEVER_AUTO=Bash」这一**语义完全合理**的形被判成零门意图 ⇒
20
+ * 落进 `createAllowDenyPolicy({})` 全面放行 ⇒ 那张"永远问人"的表从未进入任何 policy,Bash 直接跑。
21
+ *
22
+ * 判定挪到**与 inv#2 同一个文件**就是为了这个:两者再想漂开,得有人同时改这两段。
23
+ * 新增任何"门意图"配置项时,这里必须同步加一格(下面的表驱动测试会点名漏的那格)。
24
+ */
25
+ export function hasOperatorGateIntent(config) {
26
+ return (config.approvalRequire.length > 0 ||
27
+ config.approvalDeny.length > 0 ||
28
+ config.approvalNeverAuto.length > 0 || // ← 漏了这格,见上面的由来
29
+ config.durableApproval);
30
+ }
11
31
  /**
12
32
  * Durable approval policy (core 1.67 / design/45): returns three-state **`ask`** for gated tools instead of
13
33
  * polling. With `TaskSpec.durableApproval` set + a `checkpointStore` on the Runner, core turns that `ask` into
@@ -493,9 +493,17 @@ export interface ServiceConfigFlat {
493
493
  * asks, the human only sees the rest". `APPROVAL_AUTO_BUDGET`. */
494
494
  approvalAutoBudget: number;
495
495
  /** design/80 D-E (inv#2 — safety asks are never budgetable): gated tools that ALWAYS require a human even when
496
- * the auto-budget is on — the "irreversible / never auto-approve" set. Subset of approvalRequire; empty = the
497
- * budget may auto-approve any required tool. (In-service every gated ask is gate.kind="human", so this set is
498
- * the ONLY way to keep an irreversible tool human-gated under an auto-budget.) `APPROVAL_NEVER_AUTO`. */
496
+ * the auto-budget is on — the "irreversible / never auto-approve" set. empty = the budget may auto-approve any
497
+ * required tool. (In-service every gated ask is gate.kind="human", so this set is the ONLY way to keep an
498
+ * irreversible tool human-gated under an auto-budget.) `APPROVAL_NEVER_AUTO`.
499
+ *
500
+ * 🔴 **NOT required to be a subset of `approvalRequire`**(2026-07-31 修口)。这里原来写的是
501
+ * "Subset of approvalRequire",而 `approval.ts` 的 inv#2 执行面逐字相反:
502
+ * 「even if it was not also listed in requireApproval — the footgun: neverAuto ⊄ requireApproval」。
503
+ * 两处文档互相矛盾**不是文字问题**:`main.ts` 的 `operatorGateIntent` 是照着"子集"那句写的
504
+ * ——若真是子集,枚举 `approvalRequire.length > 0` 就覆盖了它,漏掉看着无害。实际不是子集,于是
505
+ * 「只配 APPROVAL_NEVER_AUTO」这一形被判成"零门意图" ⇒ 落进单用户 allow-all 基线 ⇒ 那张表
506
+ * 从未进入任何 policy。判定现在单源在 `hasOperatorGateIntent()`(与 inv#2 同文件)。 */
499
507
  approvalNeverAuto: string[];
500
508
  /** design/80 D-G (HMAC = integrity, not identity; ✅ WIRED — the "SCAFFOLD, not yet wired" note here was STALE,
501
509
  * corrected 2026-07-25: consumed by `verifyDirectDoorProof` → `verifyApprovalHmac`, reached from the live decide
package/dist/main.js CHANGED
@@ -14,7 +14,7 @@ import { ensureChildSessionDurableWithPromotion } from "./plugins/session-store.
14
14
  import { ForkRoutingSessionStore } from "./plugins/fork-routing-session-store.js";
15
15
  import { webSearchConfigFromEnv, createWebSearchBackend, setWebSearchBadPayloadObserver } from "./plugins/web-search.js";
16
16
  import { createAuthorizer } from "./security.js";
17
- import { createDurableAskPolicy } from "./approval.js";
17
+ import { createDurableAskPolicy, hasOperatorGateIntent } from "./approval.js";
18
18
  import { loadSkills } from "./capabilities/skills.js";
19
19
  import { GiteaClient } from "./capabilities/repo-tools.js";
20
20
  import { buildScenarios, builtinScenarioDetails } from "./capabilities/scenarios.js";
@@ -30,6 +30,7 @@ import { performMemorySync } from "./memory-sync.js";
30
30
  import { startOtlpExporter } from "./observability/otel-exporter.js";
31
31
  import { HEARTBEAT_MS, backgroundAgentOutput, taskHandleOutput, taskHandleStop } from "./runs.js";
32
32
  import { SQL_BLOB_DEFAULT_MAX_BYTES } from "./plugins/blob-backend.js";
33
+ import { counterStoreLabel } from "./plugins/store-backend.js";
33
34
  import { composeHooks } from "./hooks/hook-runner.js";
34
35
  import { createHookLlm } from "./hooks/hook-llm.js";
35
36
  import { startFleetClientFromEnv } from "./fleet-client.js";
@@ -367,7 +368,8 @@ async function main() {
367
368
  // backing store is wired (e.g. DB_BACKEND=local → no approvalStore/checkpointStore ⇒ approvalEnabled/durableEnabled
368
369
  // false), that is a MISCONFIG — do NOT mask it with allow-all; leave toolPolicy undefined so core's UNGATED warning
369
370
  // still surfaces it. So the baseline needs single-user turnkey ∧ zero expressed gate intent.
370
- const operatorGateIntent = config.approvalRequire.length > 0 || config.approvalDeny.length > 0 || config.durableApproval;
371
+ // 单源在 approval.ts(与 inv#2 同文件)—— 这里原是内联的三格枚举,漏了 approvalNeverAuto,详见该函数顶注。
372
+ const operatorGateIntent = hasOperatorGateIntent(config);
371
373
  const singleUserAutoAcceptBaseline = config.requirePrincipal !== true && !operatorGateIntent;
372
374
  // design/45 durable-checkpoint (core 1.67): opt-in (DURABLE_APPROVAL + a pool). When on, F4 uses durable
373
375
  // suspend/resume instead of the poll gate — a gated `ask` suspends the task (status:"suspended" + checkpoint)
@@ -869,9 +871,9 @@ async function main() {
869
871
  memory: memoryEngine ? `engine(${memoryEngine.root})` : "off",
870
872
  instanceId,
871
873
  rateLimitPerMin: config.rateLimitPerMin,
872
- rateLimit: config.rateLimitPerMin > 0 ? (backend ? `shared()` : "in-process") : "off",
874
+ rateLimit: config.rateLimitPerMin > 0 ? counterStoreLabel(backend?.rateLimiter, backend?.kind) : "off",
873
875
  maxPrincipalCostUsd: config.maxPrincipalCostUsd || null,
874
- costQuota: config.maxPrincipalCostUsd > 0 ? (backend ? `shared()` : "in-process") : "off",
876
+ costQuota: config.maxPrincipalCostUsd > 0 ? counterStoreLabel(backend?.costQuota, backend?.kind) : "off",
875
877
  // durable offload store for large tool results (cross-replica wake); else core's in-process default
876
878
  toolResultStore: toolResultStore ? (backend?.kind === "local" ? "file(local)" : "shared(sql)") : "in-process",
877
879
  otel: config.otel ? config.otel.endpoint : null,
@@ -186,6 +186,19 @@ export declare function assertCloudSnapshotBlobPosture(kind: "mysql" | "pg" | "l
186
186
  * field the 20G-workspace case needed; more knobs when a case shows up. Exported for tests
187
187
  * (fractional-MB rounding + partial-merge semantics need a regression lock). */
188
188
  export declare function snapshotBoundsFromConfig(config: ServiceConfig): FileSnapshotBounds;
189
+ /**
190
+ * 启动日志里「跨副本 or 进程内」那一格的标签。
191
+ *
192
+ * 🔴 判据必须是**那个 store 本身在不在**,不是「有没有 backend」(2026-07-31 缝合审)。
193
+ * `LocalBackend` 明确省略 `costQuota`/`rateLimiter`(见它类尾那行 "intentionally absent"),
194
+ * 而 `DB_BACKEND=local` 是**裸 boot 的默认值** —— 原来 main.ts 那两行用 `backend ? "shared()" : …` 判,
195
+ * 于是单机默认形配了 `RATE_LIMIT_PER_MIN` 会打出 `shared()`,实际跑的是每副本一份的进程内限流器。
196
+ * 运维扩到 N 副本时读这行判定"限流是跨副本的",真实额度是 N 倍。
197
+ * (同一个说谎病族在紧邻的 `breakerState` 上修过一次——"was hard-coded 'shared(tidb)' — lied under
198
+ * DB_BACKEND=pg"——`session:` 也跟着修了,这两行没跟上。顺带:原来的 `shared()` 括号是空的,
199
+ * 而兄弟行都写 `shared(${kind})`。)
200
+ */
201
+ export declare function counterStoreLabel(store: unknown, kind: string | undefined): string;
189
202
  /** Build the durable-store backend for `config.dbBackend`. Does NOT connect (the pool connects lazily; the
190
203
  * reachability probe is the caller's first `ensureSchema`). Returns undefined when no DB is configured. */
191
204
  export declare function createStoreBackend(config: ServiceConfig): StoreBackend | undefined;
@@ -276,6 +276,21 @@ class LocalBackend {
276
276
  mysqlPool() { return undefined; }
277
277
  pgPool() { return undefined; }
278
278
  }
279
+ /**
280
+ * 启动日志里「跨副本 or 进程内」那一格的标签。
281
+ *
282
+ * 🔴 判据必须是**那个 store 本身在不在**,不是「有没有 backend」(2026-07-31 缝合审)。
283
+ * `LocalBackend` 明确省略 `costQuota`/`rateLimiter`(见它类尾那行 "intentionally absent"),
284
+ * 而 `DB_BACKEND=local` 是**裸 boot 的默认值** —— 原来 main.ts 那两行用 `backend ? "shared()" : …` 判,
285
+ * 于是单机默认形配了 `RATE_LIMIT_PER_MIN` 会打出 `shared()`,实际跑的是每副本一份的进程内限流器。
286
+ * 运维扩到 N 副本时读这行判定"限流是跨副本的",真实额度是 N 倍。
287
+ * (同一个说谎病族在紧邻的 `breakerState` 上修过一次——"was hard-coded 'shared(tidb)' — lied under
288
+ * DB_BACKEND=pg"——`session:` 也跟着修了,这两行没跟上。顺带:原来的 `shared()` 括号是空的,
289
+ * 而兄弟行都写 `shared(${kind})`。)
290
+ */
291
+ export function counterStoreLabel(store, kind) {
292
+ return store ? `shared(${kind ?? "unknown"})` : "in-process";
293
+ }
279
294
  /** Build the durable-store backend for `config.dbBackend`. Does NOT connect (the pool connects lazily; the
280
295
  * reachability probe is the caller's first `ensureSchema`). Returns undefined when no DB is configured. */
281
296
  export function createStoreBackend(config) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sema-agent/server",
3
- "version": "3.11.0",
3
+ "version": "3.12.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",