@sema-agent/server 7.98.0 → 7.99.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 (50) hide show
  1. package/MIGRATION.md +6 -0
  2. package/README.md +1 -1
  3. package/README.zh-CN.md +1 -1
  4. package/USAGE.md +12 -9
  5. package/deploy/sema-up/lib/envf.sh +110 -42
  6. package/deploy/sema-up/sema-up.sh +76 -34
  7. package/dist/boot/config-center.js +167 -22
  8. package/dist/boot/org-memory.d.ts +2 -1
  9. package/dist/boot/org-memory.js +9 -5
  10. package/dist/boot/runtime-caps.js +4 -4
  11. package/dist/capabilities/center-prompts.d.ts +3 -0
  12. package/dist/capabilities/center-prompts.js +1 -1
  13. package/dist/center-credential.d.ts +112 -0
  14. package/dist/center-credential.js +131 -0
  15. package/dist/config-catalog.js +3 -2
  16. package/dist/config-center/center-request.d.ts +60 -0
  17. package/dist/config-center/center-request.js +71 -0
  18. package/dist/config-center/http-client.d.ts +17 -9
  19. package/dist/config-center/http-client.js +27 -36
  20. package/dist/config-center/skills-mcp.d.ts +4 -1
  21. package/dist/config-center/skills-mcp.js +2 -2
  22. package/dist/config-invariants.d.ts +2 -2
  23. package/dist/config-invariants.js +0 -8
  24. package/dist/config-lkg.d.ts +47 -8
  25. package/dist/config-lkg.js +107 -20
  26. package/dist/config-provider.d.ts +8 -7
  27. package/dist/config-provider.js +5 -5
  28. package/dist/config-types.d.ts +8 -4
  29. package/dist/config.js +11 -13
  30. package/dist/fleet-client.d.ts +8 -8
  31. package/dist/fleet-client.js +7 -7
  32. package/dist/fleet-lease.d.ts +11 -11
  33. package/dist/fleet-lease.js +17 -13
  34. package/dist/hosted-posture.d.ts +49 -93
  35. package/dist/hosted-posture.js +1 -25
  36. package/dist/observability/secret-env-scrub.d.ts +2 -26
  37. package/dist/observability/secret-env-scrub.js +2 -2
  38. package/dist/plugins/remote-env-host.d.ts +3 -1
  39. package/dist/plugins/remote-env-host.js +3 -2
  40. package/dist/project-memory.d.ts +28 -1
  41. package/dist/project-memory.js +22 -22
  42. package/dist/run-local.js +0 -2
  43. package/dist/runtime-caps-resolver.d.ts +8 -8
  44. package/dist/runtime-caps-resolver.js +16 -14
  45. package/dist/sealed-key.js +3 -7
  46. package/dist/server-secret-env.d.ts +41 -0
  47. package/dist/server-secret-env.js +18 -0
  48. package/dist/tool-approval.d.ts +48 -1
  49. package/dist/tool-approval.js +10 -3
  50. package/package.json +4 -3
@@ -3,22 +3,22 @@ import { homedir } from "node:os";
3
3
  import { join, dirname } from "node:path";
4
4
  import { randomBytes } from "node:crypto";
5
5
  import { validatePromptsDomain } from "./prompts-domain-validate.js";
6
+ import { centerStateBin, pullTokenIdentity } from "./center-credential.js";
6
7
  export function defaultSkillCacheDir() {
7
8
  return join(process.env.SEMA_CONFIG_DIR ?? join(homedir(), ".sema"), "skill-cache");
8
9
  }
9
- export function defaultLkgPath(worker) {
10
+ export function defaultLkgPath(worker, identity) {
10
11
  const dir = process.env.SEMA_CONFIG_DIR ?? join(homedir(), ".sema");
11
- const key = (worker ?? "global").replace(/[^a-zA-Z0-9._-]/g, "_").slice(0, 64);
12
- return join(dir, `config-lkg-${key}.json`);
12
+ return join(dir, `config-lkg-${centerStateBin(worker, identity)}.json`);
13
13
  }
14
- export async function saveLkg(path, worker, effective, etag) {
15
- const body = { formatVersion: 1, savedAt: new Date().toISOString(), worker: worker ?? "global", ...(etag !== undefined ? { etag } : {}), effective };
14
+ export async function saveLkg(path, worker, identity, effective, etag) {
15
+ const body = { formatVersion: 2, savedAt: new Date().toISOString(), worker: worker ?? "global", identity, ...(etag !== undefined ? { etag } : {}), effective };
16
16
  const tmp = `${path}.${randomBytes(6).toString("hex")}.tmp`;
17
17
  await fs.mkdir(dirname(path), { recursive: true });
18
18
  await fs.writeFile(tmp, JSON.stringify(body), { mode: 0o600 });
19
19
  await fs.rename(tmp, path);
20
20
  }
21
- export async function loadLkg(path, worker) {
21
+ export async function loadLkg(path, worker, identity) {
22
22
  let text;
23
23
  try {
24
24
  text = await fs.readFile(path, "utf8");
@@ -36,38 +36,125 @@ export async function loadLkg(path, worker) {
36
36
  if (parsed === null || typeof parsed !== "object")
37
37
  return { reason: "bad envelope" };
38
38
  const p = parsed;
39
- if (p.formatVersion !== 1)
40
- return { reason: `unknown formatVersion ${String(p.formatVersion)}` };
39
+ const formatVersion = p.formatVersion;
40
+ if (formatVersion === 1)
41
+ return { reason: "pre-identity LKG (formatVersion 1) — not read back: it cannot prove whose credential fetched it (S-651); the next clean candidate rewrites it" };
42
+ if (formatVersion !== 2)
43
+ return { reason: `unknown formatVersion ${String(formatVersion)}` };
41
44
  if (typeof p.worker !== "string" || p.worker !== (worker ?? "global"))
42
45
  return { reason: `worker mismatch (file=${String(p.worker)})` };
43
- if (p.effective === null || typeof p.effective !== "object")
44
- return { reason: "bad effective" };
45
- if (p.etag !== undefined && typeof p.etag !== "string")
46
- return { reason: "bad etag" };
47
- const eff = p.effective;
46
+ if (typeof p.identity !== "string" || p.identity !== identity)
47
+ return { reason: "credential identity mismatch (the LKG was fetched under another credential identity — not read back)" };
48
+ const problem = lkgPayloadProblem(p.effective, p.etag);
49
+ return problem === undefined ? { lkg: p } : { reason: problem };
50
+ }
51
+ function lkgPayloadProblem(effective, etag) {
52
+ if (effective === null || typeof effective !== "object")
53
+ return "bad effective";
54
+ if (etag !== undefined && typeof etag !== "string")
55
+ return "bad etag";
56
+ const eff = effective;
48
57
  if (typeof eff.version !== "number")
49
- return { reason: "bad effective.version" };
58
+ return "bad effective.version";
50
59
  for (const [domain, arrayKey] of [["models", "models"], ["skills", "skills"], ["mcp", "servers"], ["scenarios", "scenarios"], ["runtime", null]]) {
51
60
  const v = eff[domain];
52
61
  if (v === undefined)
53
62
  continue;
54
63
  if (v === null || typeof v !== "object" || Array.isArray(v))
55
- return { reason: `bad effective.${domain}` };
64
+ return `bad effective.${domain}`;
56
65
  if (arrayKey !== null) {
57
66
  const arr = v[arrayKey];
58
67
  if (arr !== undefined && !Array.isArray(arr))
59
- return { reason: `bad effective.${domain}.${arrayKey}` };
68
+ return `bad effective.${domain}.${arrayKey}`;
60
69
  }
61
70
  }
62
71
  {
63
72
  const rt = eff.runtime;
64
73
  if (rt && (rt.autonomy !== undefined || rt.commandPolicy !== undefined || rt.approvalRequire !== undefined)) {
65
- return { reason: "legacy runtime governance keys (pre-0.7.1 mirror shape; no contract producer since LKG shipped) — refusing rather than silently dropping governance" };
74
+ return "legacy runtime governance keys (pre-0.7.1 mirror shape; no contract producer since LKG shipped) — refusing rather than silently dropping governance";
66
75
  }
67
76
  }
68
- const promptsRaw = p.effective.prompts;
77
+ const promptsRaw = eff.prompts;
69
78
  if (promptsRaw !== undefined && !validatePromptsDomain(promptsRaw).ok)
70
- return { reason: "prompts face fails re-validation" };
71
- return { lkg: p };
79
+ return "prompts face fails re-validation";
80
+ return undefined;
81
+ }
82
+ export function legacyV1LkgPath(worker) {
83
+ const dir = process.env.SEMA_CONFIG_DIR ?? join(homedir(), ".sema");
84
+ const key = (worker ?? "global").replace(/[^a-zA-Z0-9._-]/g, "_").slice(0, 64);
85
+ return join(dir, `config-lkg-${key}.json`);
86
+ }
87
+ export async function migrateV1Lkg(o) {
88
+ const refuse = (why) => ({
89
+ outcome: "not-migrated",
90
+ path: o.legacyPath,
91
+ reason: `pre-identity LKG (formatVersion 1) at ${o.legacyPath} not migrated: ${why}. Way out: delete that file — the next clean candidate from the center writes this credential identity's own LKG (${o.binPath})`,
92
+ });
93
+ const sameFile = o.legacyPath === o.binPath;
94
+ if (!sameFile) {
95
+ try {
96
+ await fs.access(o.binPath);
97
+ return { outcome: "none" };
98
+ }
99
+ catch (err) {
100
+ const code = errnoOf(err);
101
+ if (code !== "ENOENT")
102
+ return refuse(`this credential identity's bin ${o.binPath} could not be checked (${code})`);
103
+ }
104
+ }
105
+ let text;
106
+ try {
107
+ text = await fs.readFile(o.legacyPath, "utf8");
108
+ }
109
+ catch (err) {
110
+ const code = errnoOf(err);
111
+ return code === "ENOENT" || code === "ENOTDIR" || sameFile ? { outcome: "none" } : refuse(`it could not be read (${code})`);
112
+ }
113
+ let parsed;
114
+ try {
115
+ parsed = JSON.parse(text);
116
+ }
117
+ catch (err) {
118
+ return sameFile ? { outcome: "none" } : refuse(`it is not valid JSON (${err instanceof Error ? err.name : "parse error"})`);
119
+ }
120
+ if (parsed === null || typeof parsed !== "object" || !("formatVersion" in parsed) || parsed.formatVersion !== 1)
121
+ return { outcome: "none" };
122
+ const v1 = parsed;
123
+ if (v1.worker !== (o.worker ?? "global"))
124
+ return refuse(`it belongs to worker ${JSON.stringify(v1.worker)}`);
125
+ if (o.identity !== pullTokenIdentity(o.worker)) {
126
+ return refuse("its envelope records only the worker, and only a pull-token credential's identity (= the worker) can be derived from that — the current credential is not this worker's pull token, so the file cannot prove whose credential fetched it");
127
+ }
128
+ const problem = lkgPayloadProblem(v1.effective, v1.etag);
129
+ if (problem !== undefined)
130
+ return refuse(`its payload is unusable (${problem})`);
131
+ const retiredTo = `${o.legacyPath}.migrated-v1`;
132
+ let retireError;
133
+ if (sameFile) {
134
+ try {
135
+ await fs.copyFile(o.legacyPath, retiredTo);
136
+ }
137
+ catch (err) {
138
+ retireError = `the formatVersion 1 bytes could not be kept as ${retiredTo} (${errnoOf(err)})`;
139
+ }
140
+ }
141
+ try {
142
+ await saveLkg(o.binPath, o.worker, o.identity, v1.effective, typeof v1.etag === "string" ? v1.etag : undefined);
143
+ }
144
+ catch (err) {
145
+ return refuse(`writing it into this credential identity's bin failed (${String(err)}) — the old file is left in place and the migration retries on the next LKG read`);
146
+ }
147
+ if (!sameFile) {
148
+ try {
149
+ await fs.rename(o.legacyPath, retiredTo);
150
+ }
151
+ catch (err) {
152
+ retireError = `the old file could not be renamed to ${retiredTo} (${errnoOf(err)}) — harmless: the bin now exists, so the old file is never read again`;
153
+ }
154
+ }
155
+ return { outcome: "migrated", from: o.legacyPath, retiredTo, ...(retireError !== undefined ? { retireError } : {}) };
156
+ }
157
+ function errnoOf(err) {
158
+ return err instanceof Error && "code" in err && typeof err.code === "string" ? err.code : "unknown";
72
159
  }
73
160
  //# sourceMappingURL=config-lkg.js.map
@@ -1,6 +1,7 @@
1
1
  import { FileConfigStore } from "@sema-agent/settings-schema/node";
2
2
  import { type EffectiveConfig as AgentConfigEffective, type EffectiveReadWarning } from "@sema-agent/settings-schema";
3
3
  import { fetchEffective as remoteFetchEffective, fetchSkillContent as remoteFetchSkillContent, type EffectiveConfig } from "./config-center/facade.js";
4
+ import type { CenterEndpoint } from "./config-center/center-request.js";
4
5
  /** Result of a `fetchEffective` — EXACTLY the config-center's return: the effective config + its etag, or
5
6
  * `null` for "unchanged" (remote 304; local: the version matched the caller's prior etag).
6
7
  * `domainErrors`([ref] registry-core 0.10.12 tolerant seam,local lane only):catalog 域坏文件不再
@@ -17,6 +18,8 @@ import { fetchEffective as remoteFetchEffective, fetchSkillContent as remoteFetc
17
18
  * ——缺席/空两形并存就是又一处二义。
18
19
  * 🔐 原流的 `error` 未经 {@link redactConfigError}(那道脱敏挂在真正进日志流的 `domainErrors` 产出边界上),
19
20
  * 路由进日志/审计流的消费者必须自己先脱敏。 */
21
+ /** `credentialIdentity`(S-651 / [ref] E-15):远程腿恒在场 = 这次拉取所用凭证的身份(分箱键来源);本地腿
22
+ * 缺席(没有凭证 —— config.d 就在本机盘上)。 */
20
23
  export type FetchEffectiveResult = {
21
24
  effective: EffectiveConfig;
22
25
  etag?: string;
@@ -25,6 +28,7 @@ export type FetchEffectiveResult = {
25
28
  error: string;
26
29
  }>;
27
30
  readWarnings: readonly EffectiveReadWarning[];
31
+ credentialIdentity?: string;
28
32
  } | null;
29
33
  /** Sentinel returned by {@link raceBootFetch} when the boot pull outlived its budget — the caller
30
34
  * starts serving on the env fallback and the SAME promise settles in the background (never cancelled). */
@@ -67,9 +71,7 @@ export interface ConfigProviderInput {
67
71
  /** Set when SEMA_REGISTRY_URL (legacy CONFIG_CENTER_URL now boot-rejects, no dual-read) is configured (config.ts `configCenter`). `dryRun` is carried through so a
68
72
  * remote↔local swap via this seam does NOT drop the dry-run posture main.ts branches on (config.ts
69
73
  * `configCenter.dryRun`); the provider itself does not consume it (the caller still branches on it). */
70
- configCenter?: {
71
- baseUrl: string;
72
- token: string;
74
+ configCenter?: CenterEndpoint & {
73
75
  worker?: string;
74
76
  dryRun?: boolean;
75
77
  };
@@ -84,16 +86,15 @@ export interface ConfigProviderInput {
84
86
  }
85
87
  /**
86
88
  * RemoteConfigProvider — DELEGATES to the exported config-center HTTP functions. No HTTP is reimplemented;
87
- * baseUrl/token/worker are bound once and passed through. The two delegate fns are injectable (defaulting
89
+ * the center endpoint (baseUrl + credential SOURCE — S-651: read per request, never a bound token) and worker
90
+ * are passed through. The two delegate fns are injectable (defaulting
88
91
  * to the real config-center exports) so a unit test can assert delegation without module-mocking.
89
92
  */
90
93
  export declare class RemoteConfigProvider implements ConfigProvider {
91
94
  private readonly cc;
92
95
  private readonly deps;
93
96
  readonly kind: "remote";
94
- constructor(cc: {
95
- baseUrl: string;
96
- token: string;
97
+ constructor(cc: CenterEndpoint & {
97
98
  worker?: string;
98
99
  }, deps?: {
99
100
  fetchEffective?: typeof remoteFetchEffective;
@@ -2,7 +2,7 @@ import { readMcpSourceLabel } from "./mcp-source-label.js";
2
2
  import { createHmac, randomBytes } from "node:crypto";
3
3
  import { foldEffectiveReadWarning } from "./config-center/read-warnings.js";
4
4
  import { FileConfigStore } from "@sema-agent/settings-schema/node";
5
- import { findSkillContent, skillContentHash, refIntegrityIssues, createSiblingResolver, } from "@sema-agent/settings-schema";
5
+ import { findSkillContent, skillContentHash, refIntegrityIssues, createSiblingResolver, formatConfigPath, REF_CHECKED_DOMAINS, } from "@sema-agent/settings-schema";
6
6
  import { redactSecrets } from "./trace/redact.js";
7
7
  import { fetchEffective as remoteFetchEffective, fetchSkillContent as remoteFetchSkillContent, ConfigCenterHttpError, } from "./config-center/facade.js";
8
8
  import { createLogger } from "./observability/logger.js";
@@ -32,7 +32,7 @@ export class RemoteConfigProvider {
32
32
  }
33
33
  async fetchEffective(etag) {
34
34
  const fn = this.deps.fetchEffective ?? remoteFetchEffective;
35
- const r = await fn(this.cc.baseUrl, this.cc.token, etag, undefined, this.cc.worker);
35
+ const r = await fn(this.cc, etag, undefined, this.cc.worker);
36
36
  if (r === null || r.domainErrors === undefined)
37
37
  return r;
38
38
  return { ...r, domainErrors: r.domainErrors.map((de) => ({ domain: de.domain, error: redactConfigError(de.error) })) };
@@ -40,7 +40,7 @@ export class RemoteConfigProvider {
40
40
  async fetchSkillContent(contentHash) {
41
41
  const fn = this.deps.fetchSkillContent ?? remoteFetchSkillContent;
42
42
  try {
43
- return await fn(this.cc.baseUrl, this.cc.token, contentHash);
43
+ return await fn(this.cc, contentHash);
44
44
  }
45
45
  catch (err) {
46
46
  if (err instanceof ConfigCenterHttpError && err.status === 404)
@@ -77,10 +77,10 @@ export class LocalConfigProvider {
77
77
  return null;
78
78
  const refErrors = [];
79
79
  const resolve = createSiblingResolver(store, eff);
80
- for (const d of ["rosters", "workers", "models", "collab", "systems", "entitlement"]) {
80
+ for (const d of REF_CHECKED_DOMAINS) {
81
81
  const issues = await refIntegrityIssues(d, eff[d], resolve);
82
82
  if (issues.length > 0)
83
- refErrors.push({ domain: d, error: issues.map((i) => `${i.path.join(".")}: ${redactConfigError(i.message)}`).join("; ") });
83
+ refErrors.push({ domain: d, error: issues.map((i) => `${formatConfigPath(i.path)}: ${redactConfigError(i.message)}`).join("; ") });
84
84
  }
85
85
  const parseErrors = parseWarnings.map((w) => ({ domain: w.domain, error: redactConfigError(w.error) }));
86
86
  const errors = [...domainErrors.map((de) => ({ domain: de.domain, error: redactConfigError(de.error) })), ...parseErrors, ...refErrors];
@@ -15,6 +15,7 @@ import type { QuestionThrottle } from "./question.js";
15
15
  import type { UnattendedApprovalPolicy } from "./tool-approval.js";
16
16
  import type { InfraCostRates } from "./observability/cost-taxonomy.js";
17
17
  import type { Autonomy, CommandRule } from "./runtime-governance.js";
18
+ import type { CenterCredentialSource } from "./center-credential.js";
18
19
  import type { GitApiKind } from "./git-api-kind.js";
19
20
  /** [ref]:记忆向量面的 embedder 坐标(env `MEMORY_EMBEDDER_*` 解析结果;单一属主 = 本类型,
20
21
  * `plugins/memory-embedder.ts` 的工厂直接消费它,不复制形状)。语义与三问见
@@ -1240,8 +1241,8 @@ export interface ServiceConfigFlat {
1240
1241
  principalJwtAud?: string;
1241
1242
  /** S-591:registry SSO-JWT 的**显式** issuer(`AUTH_BRIDGE_ISSUER`)。
1242
1243
  * 🔴 **只是显式 env,不含 boot 期那条 `?? configCenter.baseUrl` 回退**(回退仍在
1243
- * `boot/stage-09-leader.ts` 的桥装配处)—— 本字段是「运维**点名了**一个外部签发方」这个事实的载体,
1244
- * 而那条回退的事实来源是「配了 center」= fleet 管理轴,不是租户轴(理由全文见 `hosted-posture.ts`)。 */
1244
+ * `boot/stage-09-leader.ts` 的桥装配处)。唯一消费方是桥;S-671([ref] ①)起它**不是**托管证据 ——
1245
+ * 显式与回退 issuer 在租户轴上同判(理由见 `hosted-posture.ts`)。 */
1245
1246
  authBridgeIssuer?: string;
1246
1247
  /** Max accepted principal-JWT lifetime (exp − now), seconds — caps the replay window past the short-lived mandate.
1247
1248
  * 0 = no cap. `PRINCIPAL_JWT_MAX_TTL_SEC` (default 120). */
@@ -1907,10 +1908,13 @@ export interface ServiceConfigFlat {
1907
1908
  * `dryRun` (SEMA_REGISTRY_DRY_RUN=true; legacy CONFIG_CENTER_DRY_RUN now boot-rejects, no dual-read): pull + LOG what the registry would change vs the env-derived
1908
1909
  * config, but do NOT apply it — a safe-rollout verification step before flipping to live.
1909
1910
  * `worker` (SEMA_REGISTRY_WORKER=<name>; legacy CONFIG_CENTER_WORKER now boot-rejects, no dual-read; injected per-worker by the reconciler): scopes the pull to
1910
- * that worker's roster via `/effective?worker=<name>`. Unset → the global roster (backward compatible). */
1911
+ * that worker's roster via `/effective?worker=<name>`. Unset → the global roster (backward compatible).
1912
+ * `credential`(S-651,BREAKING:取代 7.98.0 及以前的 `token: string` 启动快照):凭证来源活对象
1913
+ * (`center-credential.ts`),每条出站请求经它取一次值 —— `SEMA_REGISTRY_TOKEN`(值)或
1914
+ * `SEMA_REGISTRY_TOKEN_FILE`(每次读文件,轮换 / 换账号不重启)二选一。 */
1911
1915
  configCenter?: {
1912
1916
  baseUrl: string;
1913
- token: string;
1917
+ credential: CenterCredentialSource;
1914
1918
  dryRun: boolean;
1915
1919
  worker?: string;
1916
1920
  };
package/dist/config.js CHANGED
@@ -16,10 +16,10 @@ import { normalizeSshFingerprint } from "./ssh-host-key.js";
16
16
  import { readFaceForPosture } from "./read-face-posture.js";
17
17
  import { THINKING_LEVELS, THINKING_FORMATS, buildModelCompat, isThinkingFormat, isThinkingTier } from "./model-compat.js";
18
18
  import {} from "./posture-source.js";
19
- import { hostedPrincipalRefusal } from "./hosted-posture.js";
20
19
  import { CONTROL_AND_BIDI_STRIP_RE } from "./text-bidi.js";
21
20
  import { REMOTE_EXEC_PROVIDER_WORDS, executionLaneOf } from "./execution-lane-caps.js";
22
21
  import { AUTONOMY_MODES } from "./runtime-governance.js";
22
+ import { createCenterCredentialSource, readCenterCredentialDeclaration } from "./center-credential.js";
23
23
  import { modelProviderRefusal } from "./model-provider.js";
24
24
  import { modelRouteUrlRefusal } from "./model-route-url.js";
25
25
  import { modelRouteEndpointRefusal } from "./model-route-endpoint.js";
@@ -1965,19 +1965,21 @@ function parseIntegrationsDomain() {
1965
1965
  gitApiKind: parseGitApiKind(process.env.GIT_API_KIND, process.env.GIT_API_BASEURL),
1966
1966
  defaultScenario: env("DEFAULT_SCENARIO", "code"),
1967
1967
  skillsDir: env("SKILLS_DIR", defaultSkillsDir()),
1968
- configCenter: process.env.SEMA_REGISTRY_URL
1969
- ? {
1970
- baseUrl: env("SEMA_REGISTRY_URL"),
1971
- token: process.env.SEMA_REGISTRY_TOKEN || "",
1972
- dryRun: boolEnv("SEMA_REGISTRY_DRY_RUN", false),
1973
- worker: process.env.SEMA_REGISTRY_WORKER || undefined,
1974
- }
1975
- : undefined,
1968
+ configCenter: process.env.SEMA_REGISTRY_URL ? parseConfigCenter(env("SEMA_REGISTRY_URL")) : undefined,
1976
1969
  configProvider: process.env.CONFIG_PROVIDER || undefined,
1977
1970
  configLocalDir: process.env.CONFIG_LOCAL_DIR || undefined,
1978
1971
  a2aServe: parseA2aServe(),
1979
1972
  };
1980
1973
  }
1974
+ function parseConfigCenter(baseUrl) {
1975
+ const worker = process.env.SEMA_REGISTRY_WORKER || undefined;
1976
+ return {
1977
+ baseUrl,
1978
+ credential: createCenterCredentialSource(readCenterCredentialDeclaration(), worker),
1979
+ dryRun: boolEnv("SEMA_REGISTRY_DRY_RUN", false),
1980
+ worker,
1981
+ };
1982
+ }
1981
1983
  const A2A_SERVE_MAX_SKILLS = 64;
1982
1984
  const A2A_SERVE_MAX_ID_CHARS = 128;
1983
1985
  const A2A_SERVE_MAX_TEXT_CHARS = 2048;
@@ -2195,10 +2197,6 @@ export function loadConfig() {
2195
2197
  if (dgDurable && requirePrincipal && dgOperators.length === 0) {
2196
2198
  throw new Error("DURABLE_APPROVAL=true + REQUIRE_PRINCIPAL=true but OPERATOR_PRINCIPALS is empty — on a multi-tenant worker the empty operator set makes isOperator() true-for-all, so every authenticated tenant could read every other tenant's pending-approval queue (GET /v1/approvals & siblings). Set OPERATOR_PRINCIPALS, or unset REQUIRE_PRINCIPAL if this is a single-tenant deployment (#157-① boot invariant)");
2197
2199
  }
2198
- const hostedRefusal = hostedPrincipalRefusal({ requirePrincipal, principalJwtPubkeys, principalJwtIss, principalJwtAud, authBridgeIssuer: auth.authBridgeIssuer });
2199
- if (hostedRefusal !== undefined) {
2200
- throw new Error(hostedRefusal);
2201
- }
2202
2200
  if (imageBakesEnabled && dgOperators.length === 0) {
2203
2201
  throw new Error("IMAGE_BAKES_ENABLED=true but OPERATOR_PRINCIPALS is empty — the bake door runs build.sh on the privileged build host; an empty operator set makes isOperator() true-for-all (world-writable RCE). Set OPERATOR_PRINCIPALS or disable bakes (IMAGE-API-DESIGN.md §P2.4a boot invariant)");
2204
2202
  }
@@ -13,6 +13,7 @@
13
13
  * - 全部 fire-and-forget + swallow-guarded:fleet 面是运维旁路,绝不影响任务面;网络错误只 warn 一次/连败类。
14
14
  */
15
15
  import { UsageReport } from "@sema-agent/settings-schema/fleet";
16
+ import { type CenterEndpoint } from "./config-center/center-request.js";
16
17
  /** Bound on distinct principal×model keys per window — a runaway/multi-tenant burst collapses into
17
18
  * `__other__` instead of growing unbounded (same posture as budget.ts cardinalityGuard). */
18
19
  export declare const FLEET_USAGE_KEY_CAP = 1024;
@@ -55,9 +56,8 @@ export declare class FleetUsageAccumulator {
55
56
  get size(): number;
56
57
  }
57
58
  export interface FleetClientOpts {
58
- center: {
59
- baseUrl: string;
60
- token: string;
59
+ /** S-651:凭证是**来源**(每条公告 / 用量 / 注销请求各取一次值),不是启动期烧进 header 的字符串。 */
60
+ center: CenterEndpoint & {
61
61
  worker: string;
62
62
  };
63
63
  instanceId: string;
@@ -88,16 +88,16 @@ export interface FleetClient {
88
88
  */
89
89
  export declare function startFleetClient(opts: FleetClientOpts): FleetClient;
90
90
  /**
91
- * Env/config gate (D3 可选分层): start ONLY when the sema-registry lane is configured (baseUrl+token+worker)
91
+ * Env/config gate (D3 可选分层): start ONLY when the sema-registry lane is configured (baseUrl + a DECLARED credential
92
+ * + worker — S-651: declared, not "currently readable": a token file that is unreadable right now still starts the
93
+ * client, every request re-reads it)
92
94
  * AND the operator explicitly declared this instance's reachable address. Anything missing → undefined (zero
93
95
  * behavior change for non-fleet deployments); worker missing is warned when the rest is present (half-configured).
94
96
  */
95
97
  export declare function startFleetClientFromEnv(cfg: {
96
- configCenter?: {
97
- baseUrl: string;
98
- token: string;
98
+ configCenter?: (CenterEndpoint & {
99
99
  worker?: string;
100
- };
100
+ }) | undefined;
101
101
  }, opts: Omit<FleetClientOpts, "center" | "advertiseAddress"> & {
102
102
  advertiseAddress?: string;
103
103
  }): FleetClient | undefined;
@@ -1,5 +1,6 @@
1
1
  import { InstanceAnnouncement, UsageReport, DEFAULT_HEARTBEAT_MS } from "@sema-agent/settings-schema/fleet";
2
2
  import { buildDisplaySafeUrl } from "./trace/redact.js";
3
+ import { centerFetch } from "./config-center/center-request.js";
3
4
  export const FLEET_USAGE_KEY_CAP = 1024;
4
5
  export const FLEET_CLIENT_FETCH_TIMEOUT_MS = 5000;
5
6
  export class FleetUsageAccumulator {
@@ -77,8 +78,7 @@ export function startFleetClient(opts) {
77
78
  const f = opts.fetchImpl ?? fetch;
78
79
  const heartbeatMs = opts.heartbeatMs ?? DEFAULT_HEARTBEAT_MS;
79
80
  const usageWindowMs = opts.usageWindowMs ?? 60_000;
80
- const base = opts.center.baseUrl.replace(/\/+$/, "");
81
- const headers = { authorization: `Bearer ${opts.center.token}`, "content-type": "application/json" };
81
+ const send = (route, method, json) => centerFetch(opts.center, { kind: route }, { method, json, timeoutMs: FLEET_CLIENT_FETCH_TIMEOUT_MS, fetchImpl: f });
82
82
  let stopped = false;
83
83
  let failStreak = 0;
84
84
  const announceNow = async () => {
@@ -92,7 +92,7 @@ export function startFleetClient(opts) {
92
92
  heartbeatMs,
93
93
  };
94
94
  try {
95
- const res = await f(`${base}/api/fleet/instances`, { method: "POST", headers, body: JSON.stringify(body), signal: AbortSignal.timeout(FLEET_CLIENT_FETCH_TIMEOUT_MS) });
95
+ const { res } = await send("fleet-instances", "POST", body);
96
96
  if (!res.ok)
97
97
  throw new Error(`announce ${res.status}${await rejectionSnippet(res)}`);
98
98
  if (failStreak > 0)
@@ -117,7 +117,7 @@ export function startFleetClient(opts) {
117
117
  return;
118
118
  const report = { worker: opts.center.worker, instanceId: opts.instanceId, windowStartMs: start, windowEndMs, entries };
119
119
  try {
120
- const res = await f(`${base}/api/fleet/usage`, { method: "POST", headers, body: JSON.stringify({ reports: [report] }), signal: AbortSignal.timeout(FLEET_CLIENT_FETCH_TIMEOUT_MS) });
120
+ const { res } = await send("fleet-usage", "POST", { reports: [report] });
121
121
  if (!res.ok)
122
122
  throw new Error(`usage ${res.status}${await rejectionSnippet(res)}`);
123
123
  }
@@ -140,7 +140,7 @@ export function startFleetClient(opts) {
140
140
  clearInterval(uw);
141
141
  await flushUsage();
142
142
  try {
143
- await f(`${base}/api/fleet/instances`, { method: "DELETE", headers, body: JSON.stringify({ instanceId: opts.instanceId, worker: opts.center.worker }), signal: AbortSignal.timeout(FLEET_CLIENT_FETCH_TIMEOUT_MS) });
143
+ await send("fleet-instances", "DELETE", { instanceId: opts.instanceId, worker: opts.center.worker });
144
144
  }
145
145
  catch {
146
146
  }
@@ -150,7 +150,7 @@ export function startFleetClient(opts) {
150
150
  export function startFleetClientFromEnv(cfg, opts) {
151
151
  const cc = cfg.configCenter;
152
152
  const address = opts.advertiseAddress ?? process.env.FLEET_ADVERTISE_ADDRESS;
153
- if (!cc?.baseUrl || !cc.token || !address)
153
+ if (!cc?.baseUrl || !cc.credential.declared || !address)
154
154
  return undefined;
155
155
  let parsed;
156
156
  try {
@@ -166,6 +166,6 @@ export function startFleetClientFromEnv(cfg, opts) {
166
166
  opts.logger?.warn("fleet_client_no_worker_scope", { note: "FLEET_ADVERTISE_ADDRESS set but SEMA_REGISTRY_WORKER missing — announce needs the worker (dispatch key); fleet client not started" });
167
167
  return undefined;
168
168
  }
169
- return startFleetClient({ ...opts, center: { baseUrl: cc.baseUrl, token: cc.token, worker: cc.worker }, advertiseAddress: address });
169
+ return startFleetClient({ ...opts, center: { baseUrl: cc.baseUrl, credential: cc.credential, worker: cc.worker }, advertiseAddress: address });
170
170
  }
171
171
  //# sourceMappingURL=fleet-client.js.map
@@ -24,6 +24,7 @@
24
24
  * - 全部 swallow-guarded:lease 面绝不 fault 任务提交路径。
25
25
  */
26
26
  import { QuotaLease } from "@sema-agent/settings-schema/fleet";
27
+ import { type CenterEndpoint } from "./config-center/center-request.js";
27
28
  /** 负缓存 TTL:非 lease-enforced 的 principal 多久后再问一次 center(entitlement 升级为 lease 的收敛上界)。 */
28
29
  export declare const NOT_LEASE_TTL_MS: number;
29
30
  /** center 判 budget exhausted 后,多久内的重复提交直接拒绝而不再打 center(防 per-request 打爆)。 */
@@ -62,9 +63,8 @@ export type LeaseAdmission = {
62
63
  detail?: QuotaDenyDetail;
63
64
  };
64
65
  export interface FleetLeaseOpts {
65
- center: {
66
- baseUrl: string;
67
- token: string;
66
+ /** S-651:凭证是**来源**(每次协商取一次值),不是构造期烧进 header 的字符串。 */
67
+ center: CenterEndpoint & {
68
68
  worker: string;
69
69
  };
70
70
  fetchImpl?: typeof fetch;
@@ -82,8 +82,6 @@ export declare class FleetLeaseManager {
82
82
  private readonly state;
83
83
  private readonly inflight;
84
84
  private readonly f;
85
- private readonly base;
86
- private readonly headers;
87
85
  private readonly now;
88
86
  private failStreak;
89
87
  constructor(opts: FleetLeaseOpts);
@@ -108,21 +106,23 @@ export declare class FleetLeaseManager {
108
106
  }): void;
109
107
  /** Test/observability hook: the current state kind for a principal (never exposes the token). */
110
108
  peek(principal: string): PrincipalState["kind"] | undefined;
109
+ /** 🔴 复审 O2(Opus)的鉴权失败臂,S-651 起两个入口共用(中心 401/403/400;凭证文件不可读):运维配置事故,
110
+ * 不是可用性事件。仍 fail-open(凭证抖一下不许把租户锁在门外),但**每次**都 warn + 专用计数,且**不缓存**。 */
111
+ private authFailOpen;
111
112
  private set;
112
113
  private issueOrRenew;
113
114
  private issueOrRenewInner;
114
115
  }
115
116
  /**
116
117
  * Env/config gate(与 startFleetClientFromEnv 同 D3 分层姿势,但 lease 不需要 FLEET_ADVERTISE_ADDRESS ——
117
- * 它是出站消费面,不是入站可达性声明):sema-registry lane 配齐(baseUrl+token+worker)即启;任一缺失 →
118
- * undefined,非 fleet 部署零行为变化。
118
+ * 它是出站消费面,不是入站可达性声明):sema-registry lane 配齐(baseUrl + **声明了**凭证 + worker)即启;任一缺失 →
119
+ * undefined,非 fleet 部署零行为变化。S-651:按**声明**装、不按内容装 —— `SEMA_REGISTRY_TOKEN_FILE` 在场而
120
+ * 文件此刻不可读,管理器照样装上(每次协商取值,失败走响亮的鉴权失败臂;文件恢复即恢复)。
119
121
  */
120
122
  export declare function createFleetLeaseFromEnv(cfg: {
121
- configCenter?: {
122
- baseUrl: string;
123
- token: string;
123
+ configCenter?: (CenterEndpoint & {
124
124
  worker?: string;
125
- };
125
+ }) | undefined;
126
126
  }, opts: Omit<FleetLeaseOpts, "center">): FleetLeaseManager | undefined;
127
127
  export {};
128
128
  //# sourceMappingURL=fleet-lease.d.ts.map
@@ -1,4 +1,6 @@
1
1
  import { QuotaLease, leaseExhausted, shouldRenewLease } from "@sema-agent/settings-schema/fleet";
2
+ import { CenterCredentialUnavailableError } from "./center-credential.js";
3
+ import { centerFetch } from "./config-center/center-request.js";
2
4
  export const NOT_LEASE_TTL_MS = 10 * 60 * 1000;
3
5
  export const EXHAUSTED_RETRY_MS = 30 * 1000;
4
6
  export const LEASE_PRINCIPAL_CAP = 4096;
@@ -8,15 +10,11 @@ export class FleetLeaseManager {
8
10
  state = new Map();
9
11
  inflight = new Map();
10
12
  f;
11
- base;
12
- headers;
13
13
  now;
14
14
  failStreak = 0;
15
15
  constructor(opts) {
16
16
  this.opts = opts;
17
17
  this.f = opts.fetchImpl ?? fetch;
18
- this.base = opts.center.baseUrl.replace(/\/+$/, "");
19
- this.headers = { authorization: `Bearer ${opts.center.token}`, "content-type": "application/json" };
20
18
  this.now = opts.now ?? Date.now;
21
19
  }
22
20
  async admit(principal) {
@@ -46,6 +44,11 @@ export class FleetLeaseManager {
46
44
  peek(principal) {
47
45
  return this.state.get(principal)?.kind;
48
46
  }
47
+ authFailOpen(principal, status, note) {
48
+ this.opts.logger?.warn?.("fleet_lease_auth_error_fail_open", { status, principal, note });
49
+ this.opts.metrics?.inc("fleet_lease_auth_error_total", { status: String(status) });
50
+ return { ok: true };
51
+ }
49
52
  set(principal, st) {
50
53
  if (!this.state.has(principal) && this.state.size >= LEASE_PRINCIPAL_CAP) {
51
54
  const oldest = this.state.keys().next().value;
@@ -64,11 +67,11 @@ export class FleetLeaseManager {
64
67
  }
65
68
  async issueOrRenewInner(principal) {
66
69
  try {
67
- const res = await this.f(`${this.base}/api/fleet/leases`, {
70
+ const { res } = await centerFetch(this.opts.center, { kind: "fleet-leases" }, {
68
71
  method: "POST",
69
- headers: this.headers,
70
- body: JSON.stringify({ principal, worker: this.opts.center.worker }),
71
- signal: AbortSignal.timeout(LEASE_FETCH_TIMEOUT_MS),
72
+ json: { principal, worker: this.opts.center.worker },
73
+ timeoutMs: LEASE_FETCH_TIMEOUT_MS,
74
+ fetchImpl: this.f,
72
75
  });
73
76
  if (res.status === 200) {
74
77
  const body = (await res.json());
@@ -100,13 +103,14 @@ export class FleetLeaseManager {
100
103
  return { ok: true };
101
104
  }
102
105
  if (res.status === 401 || res.status === 403 || res.status === 400) {
103
- this.opts.logger?.warn?.("fleet_lease_auth_error_fail_open", { status: res.status, principal, note: "center rejected the lease request — check the service token / worker id / center URL (budget NOT enforced until fixed)" });
104
- this.opts.metrics?.inc("fleet_lease_auth_error_total", { status: String(res.status) });
105
- return { ok: true };
106
+ return this.authFailOpen(principal, res.status, "center rejected the lease request — check the service token / worker id / center URL (budget NOT enforced until fixed)");
106
107
  }
107
108
  throw new Error(`lease ${res.status}`);
108
109
  }
109
110
  catch (err) {
111
+ if (err instanceof CenterCredentialUnavailableError) {
112
+ return this.authFailOpen(principal, "credential-unreadable", `${err.reason} — no lease request sent (budget NOT enforced until the credential is readable)`);
113
+ }
110
114
  this.failStreak += 1;
111
115
  if (this.failStreak === 1)
112
116
  this.opts.logger?.warn?.("fleet_lease_unavailable_fail_open", { error: err instanceof Error ? err.message : String(err) });
@@ -117,8 +121,8 @@ export class FleetLeaseManager {
117
121
  }
118
122
  export function createFleetLeaseFromEnv(cfg, opts) {
119
123
  const cc = cfg.configCenter;
120
- if (!cc?.baseUrl || !cc.token || !cc.worker)
124
+ if (!cc?.baseUrl || !cc.credential.declared || !cc.worker)
121
125
  return undefined;
122
- return new FleetLeaseManager({ ...opts, center: { baseUrl: cc.baseUrl, token: cc.token, worker: cc.worker } });
126
+ return new FleetLeaseManager({ ...opts, center: { baseUrl: cc.baseUrl, credential: cc.credential, worker: cc.worker } });
123
127
  }
124
128
  //# sourceMappingURL=fleet-lease.js.map