@x-otto/guard 0.1.0-alpha.43 → 0.3.0-beta.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.
@@ -0,0 +1,4 @@
1
+ {
2
+ "version": 1,
3
+ "revision": "d1d6cf1698f69575097849cee8a066a6"
4
+ }
package/dist/index.d.ts CHANGED
@@ -1,427 +1,124 @@
1
- import { HookRegistry, HookSpec } from "@x-otto/hooks";
2
- import { ApprovalRisk, HookTiming, PermissionMode, PermissionPolicySetting, PermissionRuleConfig, RuleEffect, RuleEffect as RuleEffect$1 } from "@x-otto/hook-contracts";
3
-
4
- //#region src/permission/types.d.ts
5
- interface PolicyScope {
6
- /** 适用的 agent 名称, '*' 或缺省表示所有 */
7
- agents?: string[];
8
- sessions?: string[];
9
- }
10
- interface PermissionContext {
11
- timing: HookTiming;
12
- toolName: string;
13
- args: Record<string, unknown>;
14
- agentName: string;
15
- sessionId: string;
16
- filePath?: string;
17
- metadata: Record<string, unknown>;
18
- }
19
- interface RuleMatch {
20
- tools?: string[];
21
- paths?: RegExp[];
22
- /** 必须同步。 */
23
- condition?: (context: PermissionContext) => boolean;
24
- }
25
- interface PermissionRule {
26
- name: string;
27
- effect: RuleEffect$1;
28
- match: RuleMatch;
29
- denyReason?: string;
30
- askPrompt?: string;
31
- /**
32
- * 兜底规则(C1/RFC-016):true 表示本规则仅在**没有任何非兜底规则命中**时生效
33
- * (如 strict 的 deny-unmatched、readonly 的 deny-writes 这类 match:{} 默认拒绝)。
34
- * 在 deny-wins 评估下,兜底 deny 不得盖过显式 allow(否则 strict/readonly 自破)。
35
- */
36
- fallthrough?: boolean;
37
- }
38
- interface PermissionPolicy {
39
- name: string;
40
- priority: number;
41
- enabled: boolean;
42
- scope: PolicyScope;
43
- rules: PermissionRule[];
44
- /**
45
- * 安全护栏标记(C1/RFC-016):true 表示本策略的 ask/deny 是安全护栏
46
- * (受保护路径、破坏性命令),alwaysAllow 绝不升级其 ask,bypass 也不能盖过。
47
- * 缺省 false(mode/user 策略的 ask 可被 alwaysAllow 升级)。
48
- */
49
- safety?: boolean;
50
- }
51
- interface PermissionDecision {
52
- effect: RuleEffect$1;
53
- policyName: string;
54
- ruleName: string;
55
- reason?: string;
56
- timestamp: number;
57
- evaluatedPolicies: number;
58
- /**
59
- * 命中策略的安全护栏位(透传自 PermissionPolicy.safety)——HITL 风险等级单一真源(RFC-077 M3)。
60
- * 取代 riskFromDecision 靠正则匹配 policyName 子串(destructive|credential|file-guard)反推风险的
61
- * 字符串考古:自定义安全策略名不含三魔词时不再被误判为 medium。仅 ask/deny(经 pick)携带;
62
- * allow/默认放行不参与风险评估故可空。
63
- */
64
- safety?: boolean;
65
- }
66
- interface PermissionAuditEntry {
67
- timestamp: number;
68
- sessionId: string;
69
- agentName: string;
70
- toolName: string;
71
- filePath?: string;
72
- decision: PermissionDecision;
73
- }
74
- //#endregion
75
- //#region src/permission/policies/helpers.d.ts
76
- interface PermissionToolDescriptor {
77
- name: string;
78
- readonly?: boolean;
79
- metadata?: {
80
- category?: string;
81
- };
82
- }
83
- interface PermissionToolSets {
84
- readonlyTools: Set<string>;
85
- writeTools: Set<string>;
86
- fileWriteTools: Set<string>;
87
- }
88
- interface PermissionToolSetsInput {
89
- readonlyTools?: Iterable<string>;
90
- writeTools?: Iterable<string>;
91
- fileWriteTools?: Iterable<string>;
92
- }
93
- declare function createPermissionToolSetsFromRegistry(tools: Iterable<PermissionToolDescriptor>): PermissionToolSets;
94
- //#endregion
95
- //#region src/permission/policies/file-guard.d.ts
96
- declare function createFileGuardPolicy(toolSets?: PermissionToolSetsInput): PermissionPolicy;
97
- //#endregion
98
- //#region src/permission/policies/destructive.d.ts
99
- declare const DESTRUCTIVE_COMMAND_POLICY: PermissionPolicy;
100
- //#endregion
101
- //#region src/permission/policies/credential.d.ts
102
- declare const CREDENTIAL_READ_POLICY: PermissionPolicy;
103
- //#endregion
104
- //#region src/permission/policies/otto-state.d.ts
105
- declare function createOttoStateDomainPolicy(toolSets?: PermissionToolSetsInput): PermissionPolicy;
106
- //#endregion
107
- //#region src/permission/policies/egress.d.ts
108
- declare function createEgressAllowlistPolicy(allowedHosts: readonly string[]): PermissionPolicy;
109
- //#endregion
110
- //#region src/permission/policies/disabled-tools.d.ts
111
- declare function createDisabledToolsPolicy(disabledTools: string[]): PermissionPolicy;
112
- //#endregion
113
- //#region src/permission/policies/mode.d.ts
114
- declare function createPermissionModePolicy(mode: PermissionMode, toolSets?: PermissionToolSetsInput): PermissionPolicy;
115
- //#endregion
116
- //#region src/permission/register-builtin.d.ts
117
- interface RegisterBuiltinPoliciesOptions {
118
- toolSets?: PermissionToolSetsInput;
119
- }
120
- //#endregion
121
- //#region src/permission/policy-registry.d.ts
122
- type PolicySource = 'builtin' | 'user' | 'plugin';
123
- declare class PermissionPolicyRegistry {
124
- private policies;
125
- /** 策略 → 来源标注(2026-08-15 锁死事故教训:排障时「这条规则哪来的」要一眼可见)。 */
126
- private readonly policySources;
127
- /**
128
- * 项目级「始终允许」的工具名(R8-B base)。语义:把该工具被评估出的 `ask` 升级为 `allow`,
129
- * 但绝不把 `deny` 改为 allow——安全 deny(受保护路径/破坏性命令)始终优先。
130
- * 来源 .otto/config.json,对工作区所有会话生效(手动配置的常驻放行基线)。
131
- */
132
- private alwaysAllow;
133
- /**
134
- * 会话级「始终允许」(弹窗里点「始终允许」的运行时授权):按 sessionId 隔离,
135
- * 跟随会话持久化(落盘进 session 记录,--continue 时回灌),互不串味——
136
- * 一次性的信任决策不再泄漏到项目其它会话/永久污染 config(修旧 R8-B 全局泄漏)。
137
- */
138
- private sessionAlwaysAllow;
139
- setAlwaysAllow(tools: readonly string[]): void;
140
- addAlwaysAllow(tool: string): void;
141
- getAlwaysAllow(): string[];
142
- /** 整体替换某会话的 always-allow 集合(会话恢复/回灌用)。空集合=移除该会话条目。 */
143
- setSessionAlwaysAllow(sessionId: string, tools: readonly string[]): void;
144
- addSessionAlwaysAllow(sessionId: string, tool: string): void;
145
- /** 不含项目级基线。 */
146
- getSessionAlwaysAllow(sessionId: string): string[];
147
- clearSessionAlwaysAllow(sessionId: string): void;
148
- register(policy: PermissionPolicy): void;
149
- /**
150
- * 带来源标注注册(2026-08-15 全 agent 锁死事故教训固化)。
151
- *
152
- * 事故中 user::repo-redlines 的 deny-git-stash 在项目代码里找不到来源,排障者
153
- * (含 agent 本人)反复误判为 AGENTS.md 文本/宿主 lessons/沙箱 denial。本方法把
154
- * 来源(builtin/user/plugin)记入 registry 内部映射,getAll() 输出可见——策略
155
- * 来源不再需要翻用户级 settings 才能确定。
156
- */
157
- registerWithSource(policy: PermissionPolicy, source: PolicySource): void;
158
- /** 来源标注类型:builtin 内置 / user 用户配置(~/.otto settings)/ plugin 插件注册。 */
159
- getSource(name: string): PolicySource | undefined;
160
- registerAll(policies: PermissionPolicy[]): void;
161
- unregister(name: string): boolean;
162
- has(name: string): boolean;
163
- /**
164
- * collect-all / deny-wins(C1/RFC-016,收集全部命中规则后按 effect 优先级裁决):
165
- * 收集所有命中规则,决策优先级 deny > ask > allow,**与命中/优先级顺序无关**——
166
- * priority 仅用于在同 effect 内择 reason。这样 `bypass` 的 allow-all 不再能盖过
167
- * 安全护栏的 deny/ask(first-match 旧语义的倒置缺口)。
168
- *
169
- * alwaysAllow(R8-B):仅升级**非安全护栏**的 ask→allow;安全护栏(file-guard/
170
- * destructive-guard,policy.safety=true)的 ask/deny 绝不被升级或盖过。
171
- */
172
- evaluate(context: PermissionContext): PermissionDecision;
173
- getEffective(agentName: string): PermissionPolicy[];
174
- getAll(): PermissionPolicy[];
175
- /** 带来源标注的策略视图(2026-08-15 锁死事故教训:排障/审计用——策略来源一眼可见)。 */
176
- getAllWithSource(): Array<PermissionPolicy & {
177
- source: PolicySource | undefined;
1
+ import * as _$_x_otto_kernel0 from "@x-otto/kernel";
2
+ import { CommandDeclaration, Id, PermissionMode, list } from "@x-otto/kernel";
3
+ import * as _$_x_otto_universal0 from "@x-otto/universal";
4
+ //#region src/settings/validation.d.ts
5
+ declare function parsePermissionListSetting(raw: unknown): _$_x_otto_kernel0.PermissionPolicySetting[] | undefined;
6
+ //#endregion
7
+ //#region src/component.d.ts
8
+ interface GuardOptions {
9
+ id: Id;
10
+ cwd?: string;
11
+ home?: string;
12
+ ottoHome?: string;
13
+ credentialRoots?: readonly string[];
14
+ }
15
+ declare const GUARD_FIELD_SPECS: Readonly<{
16
+ mode: Readonly<{
17
+ key: "permission.mode";
18
+ default: PermissionMode;
19
+ ownership: "trusted-project";
20
+ propagation: "on-restart";
21
+ parse: (raw: unknown) => PermissionMode | undefined;
22
+ legacy: {
23
+ aliases: string[];
24
+ };
178
25
  }>;
26
+ permissions: {
27
+ key: string;
28
+ default: never[];
29
+ ownership: "trusted-project";
30
+ parse: typeof parsePermissionListSetting;
31
+ };
32
+ alwaysAllow: {
33
+ key: string;
34
+ default: never[];
35
+ ownership: "trusted-project";
36
+ parse: typeof list;
37
+ legacy: {
38
+ aliases: string[];
39
+ };
40
+ };
41
+ offeredScopes: {
42
+ key: string;
43
+ default: _$_x_otto_kernel0.ApprovalScope[];
44
+ ownership: "trusted-project";
45
+ legacy: {
46
+ aliases: string[];
47
+ };
48
+ parse: (raw: unknown) => string[] | undefined;
49
+ };
50
+ }>;
51
+ declare const Guard: _$_x_otto_universal0.ComponentDefinition<GuardOptions>;
52
+ //#endregion
53
+ //#region src/policies/protected-resources.d.ts
54
+ type ProtectionTier = 'system' | 'credential' | 'runtime-state' | 'control-plane' | 'trust-store' | 'extension-decision-sources';
55
+ type ProtectionVerdict = 'allow' | 'ask' | 'deny';
56
+ interface ProtectionRule {
57
+ readonly tier: ProtectionTier;
58
+ readonly read: ProtectionVerdict;
59
+ readonly write: ProtectionVerdict;
60
+ }
61
+ declare const PROTECTION_RULES: Readonly<Record<ProtectionTier, ProtectionRule>>;
62
+ interface PathMatcher {
63
+ readonly tier: ProtectionTier;
64
+ readonly kind: 'absolute-prefix' | 'segment' | 'home-relative' | 'workspace-root-file' | 'basename' | 'suffix-under';
65
+ readonly value: string;
66
+ readonly within?: string;
67
+ readonly readAsk?: true;
68
+ }
69
+ declare const PROTECTED_RESOURCES: readonly PathMatcher[];
70
+ interface PathNormalizer {
71
+ readonly caseInsensitive: boolean;
72
+ normalize(path: string): string;
73
+ }
74
+ interface PathNormalizerOptions {
75
+ readonly platform: NodeJS.Platform;
76
+ readonly realpath?: (path: string) => string;
77
+ }
78
+ declare function createPathNormalizer(options: PathNormalizerOptions): PathNormalizer;
79
+ interface ProtectionContext {
80
+ readonly home?: string | undefined;
81
+ readonly ottoHome?: string | undefined;
82
+ readonly workspaceRoot?: string | undefined;
83
+ readonly credentialRoots?: readonly string[];
84
+ readonly normalizer: PathNormalizer;
85
+ }
86
+ interface ProtectionMatch {
87
+ readonly tier: ProtectionTier;
88
+ readonly verdict: ProtectionVerdict;
89
+ readonly matcher: string;
90
+ }
91
+ declare function classifyPath(target: string, access: 'read' | 'write', context: ProtectionContext): ProtectionMatch | undefined;
92
+ //#endregion
93
+ //#region src/libs/i18n.d.ts
94
+ declare const ruleKey: (ruleName: string) => string;
95
+ declare const askReasonText: (ruleName: string | undefined, fallback: string) => string;
96
+ //#endregion
97
+ //#region src/approval/hook.d.ts
98
+ interface RuntimePermissionOverridePort {
99
+ get(sessionId: string): PermissionMode | undefined;
100
+ set(sessionId: string, mode: PermissionMode | undefined): void;
179
101
  clear(): void;
180
102
  }
181
- declare function createPermissionRegistry(options?: RegisterBuiltinPoliciesOptions): PermissionPolicyRegistry;
182
- declare function compilePolicyFromSetting(setting: PermissionPolicySetting): PermissionPolicy;
183
103
  //#endregion
184
- //#region src/permission/audit-log.d.ts
185
- declare class PermissionAuditLog {
186
- private entries;
187
- private readonly maxEntries;
188
- constructor(maxEntries?: number);
189
- record(context: PermissionContext, decision: PermissionDecision): void;
190
- /**
191
- * RFC-380 T6:记录一次**非策略引擎**产生的自动裁决(语义审批层 semanticVerdict)。
192
- *
193
- * 语义审批发生在工具执行失败后的升级环节(tool-executor escalate-on-deny),不经
194
- * `PermissionPolicyRegistry.evaluate`,因而没有 `PermissionContext`——但它同样是
195
- * 「用户未点确认就放行/拒绝」的自动裁决,必须进审计,否则权限面板会漏掉
196
- * 恰恰最该被回看的那部分(全自动模式下的静默放行)。
197
- *
198
- * @param entry.policyName 裁决来源标识(如 `semantic::bypass-auto-allow`)。
199
- */
200
- recordAutoDecision(entry: {
201
- sessionId: string;
202
- agentName: string;
203
- toolName: string;
204
- effect: RuleEffect;
205
- policyName: string;
206
- reason?: string;
207
- timestamp?: number;
208
- }): void;
209
- getEntries(filter?: {
210
- sessionId?: string;
211
- effect?: RuleEffect;
212
- }): PermissionAuditEntry[];
213
- getDenyCount(sessionId?: string): number;
214
- getAskCount(sessionId?: string): number;
215
- clear(sessionId?: string): void;
216
- get size(): number;
104
+ //#region src/settings/migrate.d.ts
105
+ interface MigrationLeftover {
106
+ readonly policy: string;
107
+ readonly rule: string;
108
+ readonly field: 'paths' | 'commands';
109
+ readonly pattern: string;
217
110
  }
218
- //#endregion
219
- //#region src/permission/permission-guard.d.ts
220
- /**
221
- * 权限守卫 hook 工厂:`tool.execute.before`(priority 5)拦截,调 `PermissionPolicyRegistry.evaluate`
222
- * 并写审计日志。deny → `output.cancelled=true`+`decision='deny'`(tool-executor 据此拒绝);
223
- * ask → `decision='ask'`(引擎交审批端口);已有更严裁决不降级(fail-closed)。
224
- *
225
- * RFC-395:原 `PermissionGuardHook` class 是无状态、无多态的形式化封装(构造即固定 registry/auditLog,
226
- * 唯一方法是 handle lambda),已删除——纯工厂 + 闭包等价且更简。
227
- */
228
- declare function createPermissionGuardHook(registry: PermissionPolicyRegistry, auditLog?: PermissionAuditLog): HookSpec;
229
- //#endregion
230
- //#region src/permission/baseline-provisioning.d.ts
231
- /**
232
- * RFC-077 M3-B:bare-runtime(无工具注册表)的基线 `fileWriteTools`——`createFileGuardPolicy`
233
- * 的 protect-system-paths 规则按 `tools: [...fileWriteTools]` 过滤,无注册表派生集时须给默认,
234
- * 否则空集 → 该规则匹配不到任何工具 → protected-path 写不被拦。
235
- *
236
- * 与 standalone `createFileGuardHook` 的 `WRITE_TOOLS` 同源(canonical otto 文件写工具);bash 非
237
- * path-based 故不入 fileWriteTools(其命令面由 destructive/credential 命令策略覆盖)。**宿主有工具
238
- * 注册表时经 `createPermissionToolSetsFromRegistry` 派生集覆盖本默认**(coding 走该路径)。
239
- */
240
- declare const BASELINE_PERMISSION_TOOL_SETS: PermissionToolSetsInput;
241
- /**
242
- * RFC-077 M3-B(D6 §4.11「安全护栏先 provision 再删」):把权限策略引擎 + 基线安全策略
243
- * (file-guard / credential / destructive,经 `createPermissionRegistry` 注册)下沉为 bare-runtime
244
- * 的**默认提供**——注册 permission-guard hook(`tool.execute.before`),让无宿主 PermissionController
245
- * 的裸运行时也 safe-by-default(安全是引擎核心能力,非宿主可选项)。
246
- *
247
- * **幂等**:宿主(coding)已注册 permission-guard 时直接返回,绝不双注册——bare 分支与宿主
248
- * provision 二选一(createAgentRuntime 仅在自建 hookRegistry、未被宿主注入时调用本函数)。
249
- *
250
- * @returns 新建的基线 registry(宿主已 provision 时返回 undefined)。
251
- */
252
- declare function provisionBaselineSecurity(hookRegistry: HookRegistry): PermissionPolicyRegistry | undefined;
253
- //#endregion
254
- //#region src/permission/risk.d.ts
255
- /**
256
- * 从权限裁决的结构化安全位(decision.safety,透传自命中策略 PermissionPolicy.safety)派生 HITL
257
- * 显示风险等级。RFC-077 M3:取代原先靠正则匹配 policyName 子串
258
- * (destructive|credential|file-guard) 反推风险的字符串考古——那会把名字不含三魔词的自定义安全
259
- * 策略误判为 medium,且 builtin 策略一改名就静默退化。
260
- *
261
- * 仅作 UI 显示提示——不参与 allow/deny/ask 裁决。安全护栏(safety:true)→ high,其余 ask
262
- * (confirm 写 / egress 越界 / 自定义非安全策略)→ medium。
263
- *
264
- * 注意:always-allow 折叠后的非安全 ask 变成 effect==='allow',不会走到这里;到达 HITL 的 ask
265
- * 要么是安全护栏(safety:true→high),要么是未折叠的 mode/自定义 ask(safety 缺省→medium)。
266
- */
267
- declare function riskFromDecision(decision: PermissionDecision): ApprovalRisk;
268
- //#endregion
269
- //#region src/permission/always-allow.d.ts
270
- /**
271
- * always-allow 条目:`tool` 或 `tool(content)`。content 仅对 bash 解释为命令前缀。
272
- */
273
- interface AllowEntry {
274
- toolName: string;
275
- content?: string;
111
+ interface MigrationResult {
112
+ readonly permissions: unknown[];
113
+ readonly converted: number;
114
+ readonly leftovers: readonly MigrationLeftover[];
115
+ readonly changed: boolean;
276
116
  }
277
- /** 解析一条 always-allow 字符串。`bash` → 整工具(content=undefined);`bash(git status)` → 前缀条目。 */
278
- declare function parseAllowEntry(raw: string): AllowEntry;
279
- /**
280
- * 条目是否格式良好——供 `/permissions add` 校验,拒绝静默持久化危险/无意义条目。
281
- * 合法:`tool`(整工具)或 `tool(非空 content)`。
282
- * 非法:空串、空括号 `tool()`(读法似"无参"实为整工具,塌缩陷阱,RFC-073 P3)、
283
- * 括号不平衡/含元字符的垃圾(`bash(((`、`a b`)。
284
- */
285
- declare function isWellFormedAllowEntry(raw: string): boolean;
286
- declare function bashCommandCovered(command: string, prefixes: readonly string[]): boolean;
287
- /**
288
- * 某工具调用是否被 always-allow 条目覆盖(合并项目级+会话级条目)。
289
- * - 工具名条目(无 content)→ 整工具放行(保留旧语义)。
290
- * - bash(content) → 命令所有片段须被前缀覆盖。
291
- * - 其它工具的 content 条目暂不解释(保守:不匹配 → 仍 ask)。
292
- *
293
- * 注意:本函数只决定「是否升级非安全 ask→allow」,安全护栏(safety=true)的 ask/deny
294
- * 由调用方单独保留,绝不被此覆盖。
295
- */
296
- declare function toolCallCoveredByAllow(context: PermissionContext, entries: Iterable<string>): boolean;
297
- /**
298
- * 为「始终允许」按钮派生一个 bash 命令前缀(程序名 + 子命令两段),供 UI 显式呈现 +
299
- * 持久化为 `bash(<prefix>)`。复合/混淆/env 赋值命令返回 null → 调用方回退整工具放行
300
- * (此时安全护栏仍 bypass-immune,不会因整工具放行而漏过危险叶子)。
301
- *
302
- * 例:`git status -s`→`git status`、`npm run build`→`npm run`、`ls -la`→`ls`、
303
- * `cd /x && git status`→`git status`、`a && b`/`$()`/`FOO=1 cmd`→null。
304
- */
305
- declare function bashAlwaysAllowPrefix(command: string): string | null;
306
- //#endregion
307
- //#region src/permission/command-canonicalize.d.ts
308
- /**
309
- * 综合:返回一条命令的所有可执行片段(含原始串、去混淆形、拆分子命令、解包内层、替换内容、
310
- * here-string payload),去重。destructive/suspicious/interpreter 模式对每个片段独立检查。
311
- */
312
- declare function extractShellCommands(command: string): string[];
313
- declare function extractWriteTargets(command: string): string[];
314
- //#endregion
315
- //#region src/permission/protected-paths.d.ts
316
- declare const PROTECTED_PATH_PATTERNS: RegExp[];
117
+ declare function pathPatternToSpecifier(pattern: string): string | undefined;
118
+ declare function commandPatternToSpecifier(pattern: string): string | undefined;
119
+ declare function migratePermissions(value: unknown): MigrationResult;
317
120
  //#endregion
318
- //#region src/sandbox-safety.d.ts
319
- type SandboxVerdict = 'auto-allow' | 'ask' | 'deny';
320
- interface SandboxAssessment {
321
- verdict: SandboxVerdict;
322
- reason?: string;
323
- }
324
- interface SandboxOp {
325
- writePaths?: readonly string[];
326
- network?: boolean;
327
- }
328
- interface SandboxAssessContext {
329
- mode: PermissionMode;
330
- projectRoot: string;
331
- writableRoots: readonly string[];
332
- deniedRoots: readonly string[];
333
- networkAllowed: boolean;
334
- /**
335
- * bash 沙箱越界自动放行开关(opt-in,默认 false,见 SandboxConfig.autoAllowBashIfSandboxed)。
336
- * 本字段只是透传给 sandbox-guard hook 消费——assessSandboxSafety 本身不读取它(保持纯函数
337
- * 决策矩阵不变),由 sandbox-guard 在拿到 'ask' verdict 后结合此字段 + 权限层已有裁决
338
- * (output.decision)判断是否降级为自动放行。
339
- */
340
- autoAllowBashIfSandboxed?: boolean;
341
- }
342
- declare function assessSandboxSafety(op: SandboxOp, ctx: SandboxAssessContext): SandboxAssessment;
343
- declare function defaultDeniedRoots(workspaceDir: string, homeDir: string): string[];
344
- /**
345
- * 默认可写根:projectRoot + os.tmpdir() + 字面 `/tmp`。
346
- *
347
- * 参照 codex(openai/codex codex-rs/protocol/src/permissions.rs
348
- * legacy_runtime_file_system_policy_for_cwd):SlashTmp 与 Tmpdir 是两个独立的默认可写
349
- * special path,同时生效(`exclude_slash_tmp`/`exclude_tmpdir_env_var` 默认均 false)。
350
- * macOS 上 `os.tmpdir()` 落在 `/var/folders/...`(进程级临时目录),与用户/shell 习惯手写的
351
- * 字面 `/tmp`(→ 真实路径 `/private/tmp`,全系统共享临时目录)是两棵不同的目录树——只信
352
- * os.tmpdir() 会让 `mkdir -p /tmp/xxx` 这类零风险操作被误判越界触发审批(见 sandbox-guard
353
- * 越界升级事故复盘)。underAny() 的 realpath 归一化解决的是"同一路径不同书写形式",本条解决
354
- * 的是"两个本就不同的物理临时目录都该被信任"。
355
- */
356
- declare function computeSandboxRoots(mode: PermissionMode, projectRoot: string, tmpDir: string, extra?: readonly string[]): string[];
357
- //#endregion
358
- //#region src/observability/failure-signals.d.ts
359
- type FailureSignalKind = 'tool-error' | 'permission-denied';
360
- interface FailureSignal {
361
- kind: FailureSignalKind;
362
- /** 聚合签名(如 toolName)——同签名复发即累加。 */
363
- signature: string;
364
- count: number;
365
- firstSeen: number;
366
- lastSeen: number;
367
- /** 最近一次的简短细节(如错误首行,截断)。 */
368
- detail?: string;
369
- }
370
- /** 跨会话失败信号存储:按 (kind,signature) 聚合计数。诊断据此只认硬信号。 */
371
- declare class FailureSignalStore {
372
- private readonly signals;
373
- private readonly clock;
374
- constructor(clock?: () => number);
375
- record(kind: FailureSignalKind, signature: string, detail?: string): void;
376
- /** 复发达阈值(默认 ≥3)的信号,按计数降序——诊断只在这些硬信号上提案。 */
377
- recurring(minCount?: number): FailureSignal[];
378
- all(): FailureSignal[];
379
- clear(): void;
380
- }
381
- /** RFC-026 阶段3:tool.execute.after 把工具 isError 记成结构化失败信号。 */
382
- declare function createFailureSignalHook(store: FailureSignalStore): HookSpec;
383
- /**
384
- * RFC-067 M121-04a:把**权限拒绝**记成 `permission-denied` 失败信号(补 FailureSignalKind 的另一半,
385
- * 此前从无 caller 记录此 kind)。
386
- *
387
- * 为何在 tool.execute.before:denied 工具在 tool-executor 早返(errorKind:'permission'),**绕过
388
- * tool.execute.after**,故 after-hook(createFailureSignalHook)看不到它。权限/沙箱守卫在 before 层
389
- * 设 `output.decision='deny'`(如 sandbox-guard pri 6),所有 before-hook 跑完才判 deny 裁决——故本
390
- * recorder 用**晚 priority(90)**确保跑在守卫之后、读到累积的 decision。
391
- */
392
- declare function createPermissionDenyHook(store: FailureSignalStore): HookSpec;
393
- //#endregion
394
- //#region src/observability/diagnoser.d.ts
395
- interface Proposal {
396
- /** 稳定 id(= kind:signature,便于去重)。 */
397
- id: string;
398
- signature: string;
399
- kind: 'investigate-fix';
400
- /** 改进指令(L1:用户审后可 /job 执行;诊断器**不**自动跑)。 */
401
- directive: string;
402
- /** 来源证据(provenance):哪个信号、复发几次、细节。 */
403
- evidence: {
404
- signalKind: FailureSignal['kind'];
405
- count: number;
406
- detail?: string;
407
- };
408
- createdAt: number;
409
- }
410
- interface DiagnoserConfig {
411
- /** 复发阈值(信号 count ≥ 此才提案;默认 3)。 */
412
- minCount?: number;
413
- clock?: () => number;
414
- }
415
- declare class Diagnoser {
416
- private readonly signals;
417
- private readonly proposed;
418
- private readonly minCount;
419
- private readonly clock;
420
- constructor(signals: FailureSignalStore, config?: DiagnoserConfig);
421
- /** 扫硬信号(recurring ≥ minCount)→ **新**提案(已提过的签名跳过,防风暴)。 */
422
- propose(): Proposal[];
423
- /** 重置去重(用户处理完想重新评估时)。 */
424
- reset(): void;
425
- }
121
+ //#region src/commands/permissions-commands.d.ts
122
+ declare function createPermissionsCommands(): readonly CommandDeclaration[];
426
123
  //#endregion
427
- export { BASELINE_PERMISSION_TOOL_SETS, CREDENTIAL_READ_POLICY, DESTRUCTIVE_COMMAND_POLICY, Diagnoser, type DiagnoserConfig, type FailureSignal, type FailureSignalKind, FailureSignalStore, PROTECTED_PATH_PATTERNS, type PermissionAuditEntry, PermissionAuditLog, type PermissionContext, type PermissionDecision, type PermissionMode, type PermissionPolicy, PermissionPolicyRegistry, type PermissionPolicySetting, type PermissionRule, type PermissionRuleConfig, type PermissionToolDescriptor, type PermissionToolSets, type PermissionToolSetsInput, type PolicyScope, type Proposal, type RuleEffect, type RuleMatch, type SandboxAssessContext, type SandboxAssessment, type SandboxOp, type SandboxVerdict, assessSandboxSafety, bashAlwaysAllowPrefix, bashCommandCovered, compilePolicyFromSetting, computeSandboxRoots, createDisabledToolsPolicy, createEgressAllowlistPolicy, createFailureSignalHook, createFileGuardPolicy, createOttoStateDomainPolicy, createPermissionDenyHook, createPermissionGuardHook, createPermissionModePolicy, createPermissionRegistry, createPermissionToolSetsFromRegistry, defaultDeniedRoots, extractShellCommands, extractWriteTargets, isWellFormedAllowEntry, parseAllowEntry, provisionBaselineSecurity, riskFromDecision, toolCallCoveredByAllow };
124
+ export { GUARD_FIELD_SPECS, Guard, PROTECTED_RESOURCES, PROTECTION_RULES, type PathMatcher, type PathNormalizer, type PathNormalizerOptions, type ProtectionContext, type ProtectionMatch, type ProtectionRule, type ProtectionTier, type ProtectionVerdict, type RuntimePermissionOverridePort, askReasonText, classifyPath, commandPatternToSpecifier, createPathNormalizer, createPermissionsCommands, migratePermissions, pathPatternToSpecifier, ruleKey };