@carfiedli/runtime-guardrail 0.1.19

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 (86) hide show
  1. package/README.fe.md +256 -0
  2. package/README.hooks-security.md +1017 -0
  3. package/README.md +1316 -0
  4. package/dist/adapters/index.d.ts +1 -0
  5. package/dist/adapters/persistence/file-store.d.ts +18 -0
  6. package/dist/adapters/persistence/index.d.ts +4 -0
  7. package/dist/adapters/persistence/json-event-log.d.ts +31 -0
  8. package/dist/adapters/persistence/queue-store.d.ts +19 -0
  9. package/dist/adapters/persistence/snapshot-store.d.ts +14 -0
  10. package/dist/approval/approval-service.d.ts +27 -0
  11. package/dist/approval/approval-state-machine.d.ts +5 -0
  12. package/dist/approval/hitl/hitl-connector.d.ts +9 -0
  13. package/dist/approval/index.d.ts +4 -0
  14. package/dist/approval/run-hold-service.d.ts +16 -0
  15. package/dist/audit/audit-event-store.d.ts +12 -0
  16. package/dist/audit/audit-read-model-builder.d.ts +17 -0
  17. package/dist/audit/audit-service.d.ts +18 -0
  18. package/dist/audit/incident-query-service.d.ts +7 -0
  19. package/dist/audit/index.d.ts +5 -0
  20. package/dist/audit/metrics-projection.d.ts +10 -0
  21. package/dist/bootstrap/create-runtime-guardrail-plugin.d.ts +3 -0
  22. package/dist/bootstrap/dependency-container.d.ts +2 -0
  23. package/dist/bootstrap/index.d.ts +3 -0
  24. package/dist/bootstrap/runtime-facade.d.ts +31 -0
  25. package/dist/compat/index.d.ts +1 -0
  26. package/dist/compat/legacy-types.d.ts +29 -0
  27. package/dist/contracts/core.d.ts +277 -0
  28. package/dist/contracts/events.d.ts +35 -0
  29. package/dist/contracts/host.d.ts +239 -0
  30. package/dist/contracts/index.d.ts +6 -0
  31. package/dist/contracts/operator.d.ts +110 -0
  32. package/dist/execution/egress-mediator.d.ts +7 -0
  33. package/dist/execution/execution-broker.d.ts +13 -0
  34. package/dist/execution/execution-plan-builder.d.ts +12 -0
  35. package/dist/execution/index.d.ts +4 -0
  36. package/dist/execution/model-governance-service.d.ts +7 -0
  37. package/dist/index.d.ts +29 -0
  38. package/dist/index.js +23 -0
  39. package/dist/openclaw/hooks/egress-adapter.d.ts +9 -0
  40. package/dist/openclaw/hooks/hook-registry.d.ts +21 -0
  41. package/dist/openclaw/hooks/hook-result-mapper.d.ts +43 -0
  42. package/dist/openclaw/hooks/hook-types.d.ts +31 -0
  43. package/dist/openclaw/hooks/index.d.ts +8 -0
  44. package/dist/openclaw/hooks/ingress-adapter.d.ts +14 -0
  45. package/dist/openclaw/hooks/llm-request-adapter.d.ts +9 -0
  46. package/dist/openclaw/hooks/persist-adapter.d.ts +30 -0
  47. package/dist/openclaw/hooks/tool-call-adapter.d.ts +7 -0
  48. package/dist/openclaw/index.d.ts +4 -0
  49. package/dist/openclaw/plugin-runtime.d.ts +103 -0
  50. package/dist/openclaw/rpc-handlers.d.ts +20 -0
  51. package/dist/openclaw/skills-availability.d.ts +10 -0
  52. package/dist/openclaw/skills-upload.d.ts +17 -0
  53. package/dist/openclaw/testing/index.d.ts +1 -0
  54. package/dist/openclaw/testing/mock-openclaw-api.d.ts +74 -0
  55. package/dist/operator/cli/register-cli.d.ts +4 -0
  56. package/dist/operator/command-service.d.ts +15 -0
  57. package/dist/operator/index.d.ts +5 -0
  58. package/dist/operator/query-service.d.ts +21 -0
  59. package/dist/operator/reporting/report-service.d.ts +9 -0
  60. package/dist/operator/rpc/register-rpc.d.ts +5 -0
  61. package/dist/policy/detectors/detector-port.d.ts +23 -0
  62. package/dist/policy/finding-normalizer.d.ts +3 -0
  63. package/dist/policy/index.d.ts +4 -0
  64. package/dist/policy/policy-engine.d.ts +8 -0
  65. package/dist/policy/stage-resolver.d.ts +7 -0
  66. package/dist/runtime-core/device-id.d.ts +15 -0
  67. package/dist/runtime-core/evaluate-service.d.ts +91 -0
  68. package/dist/runtime-core/index.d.ts +10 -0
  69. package/dist/runtime-core/memory-audit-logger.d.ts +55 -0
  70. package/dist/runtime-core/memory-store.d.ts +141 -0
  71. package/dist/runtime-core/remote-guard-request-builder.d.ts +15 -0
  72. package/dist/runtime-core/remote-guard-transport.d.ts +79 -0
  73. package/dist/runtime-core/remote-guard-types.d.ts +183 -0
  74. package/dist/runtime-core/remote-policy-evaluator.d.ts +51 -0
  75. package/dist/runtime-core/skill-name-resolver.d.ts +31 -0
  76. package/dist/runtime-core/sync-remote-evaluate.d.ts +29 -0
  77. package/dist/runtime-core/sync-remote-worker.d.ts +14 -0
  78. package/dist/runtime-core/sync-remote-worker.js +2 -0
  79. package/dist/runtime-core/telemetry-service.d.ts +94 -0
  80. package/dist/runtime-core/telemetry-types.d.ts +181 -0
  81. package/dist/types.d.ts +224 -0
  82. package/dist/version.d.ts +1 -0
  83. package/openclaw.plugin.json +76 -0
  84. package/package.json +71 -0
  85. package/remote-guard-config.json +30 -0
  86. package/scripts/runtime-guardrailctl.mjs +864 -0
@@ -0,0 +1 @@
1
+ export * from "./persistence";
@@ -0,0 +1,18 @@
1
+ export type FileStoreEntry = {
2
+ key: string;
3
+ content: string;
4
+ updatedAt: number;
5
+ };
6
+ export interface FileStore {
7
+ read(key: string): Promise<string | undefined>;
8
+ write(key: string, content: string): Promise<void>;
9
+ delete(key: string): Promise<void>;
10
+ list(prefix?: string): Promise<FileStoreEntry[]>;
11
+ }
12
+ export declare class InMemoryFileStore implements FileStore {
13
+ private readonly files;
14
+ read(key: string): Promise<string | undefined>;
15
+ write(key: string, content: string): Promise<void>;
16
+ delete(key: string): Promise<void>;
17
+ list(prefix?: string): Promise<FileStoreEntry[]>;
18
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./file-store";
2
+ export * from "./json-event-log";
3
+ export * from "./snapshot-store";
4
+ export * from "./queue-store";
@@ -0,0 +1,31 @@
1
+ import type { FileStore } from "./file-store";
2
+ export type EventLogCorruptionRecord = {
3
+ key: string;
4
+ detectedAt: number;
5
+ reason: string;
6
+ rawContent?: string;
7
+ };
8
+ export interface EventLogCorruptionSink {
9
+ record(record: EventLogCorruptionRecord): Promise<void>;
10
+ list(): Promise<EventLogCorruptionRecord[]>;
11
+ }
12
+ export declare class InMemoryEventLogCorruptionSink implements EventLogCorruptionSink {
13
+ private readonly records;
14
+ record(record: EventLogCorruptionRecord): Promise<void>;
15
+ list(): Promise<EventLogCorruptionRecord[]>;
16
+ }
17
+ export interface EventLog<T> {
18
+ append(entry: T): Promise<void>;
19
+ list(): Promise<T[]>;
20
+ replace(entries: T[]): Promise<void>;
21
+ }
22
+ export declare class JsonEventLog<T> implements EventLog<T> {
23
+ private readonly fileStore;
24
+ private readonly key;
25
+ private readonly corruptionSink;
26
+ constructor(fileStore: FileStore, key: string, corruptionSink?: EventLogCorruptionSink);
27
+ append(entry: T): Promise<void>;
28
+ list(): Promise<T[]>;
29
+ replace(entries: T[]): Promise<void>;
30
+ private readEntries;
31
+ }
@@ -0,0 +1,19 @@
1
+ import type { FileStore } from "./file-store";
2
+ export type PersistedQueueItem<T> = {
3
+ id: string;
4
+ createdAt: number;
5
+ payload: T;
6
+ };
7
+ export interface QueueStore<T> {
8
+ enqueue(payload: T): Promise<PersistedQueueItem<T>>;
9
+ list(): Promise<Array<PersistedQueueItem<T>>>;
10
+ remove(id: string): Promise<void>;
11
+ }
12
+ export declare class JsonQueueStore<T> implements QueueStore<T> {
13
+ private readonly fileStore;
14
+ private readonly key;
15
+ constructor(fileStore: FileStore, key: string);
16
+ enqueue(payload: T): Promise<PersistedQueueItem<T>>;
17
+ list(): Promise<Array<PersistedQueueItem<T>>>;
18
+ remove(id: string): Promise<void>;
19
+ }
@@ -0,0 +1,14 @@
1
+ import type { FileStore } from "./file-store";
2
+ export interface SnapshotStore<T> {
3
+ load(): Promise<T | undefined>;
4
+ save(snapshot: T): Promise<void>;
5
+ clear(): Promise<void>;
6
+ }
7
+ export declare class JsonSnapshotStore<T> implements SnapshotStore<T> {
8
+ private readonly fileStore;
9
+ private readonly key;
10
+ constructor(fileStore: FileStore, key: string);
11
+ load(): Promise<T | undefined>;
12
+ save(snapshot: T): Promise<void>;
13
+ clear(): Promise<void>;
14
+ }
@@ -0,0 +1,27 @@
1
+ import type { ApprovalListQuery, ApprovalTicket, GuardrailDecision } from "../contracts";
2
+ import { ApprovalStateMachine } from "./approval-state-machine";
3
+ import type { HitlConnector } from "./hitl/hitl-connector";
4
+ export type ResolveApprovalInput = {
5
+ approvalId: string;
6
+ decision: "approved" | "rejected";
7
+ resolvedBy?: string;
8
+ note?: string;
9
+ source?: string;
10
+ externalApprovalId?: string;
11
+ };
12
+ export interface ApprovalService {
13
+ createFromDecision(decision: GuardrailDecision): Promise<ApprovalTicket[]>;
14
+ getById(approvalId: string): Promise<ApprovalTicket | undefined>;
15
+ list(query?: ApprovalListQuery): Promise<ApprovalTicket[]>;
16
+ resolve(input: ResolveApprovalInput): Promise<ApprovalTicket>;
17
+ }
18
+ export declare class InMemoryApprovalService implements ApprovalService {
19
+ private readonly stateMachine;
20
+ private readonly hitlConnector;
21
+ private readonly approvals;
22
+ constructor(stateMachine: ApprovalStateMachine, hitlConnector: HitlConnector);
23
+ createFromDecision(decision: GuardrailDecision): Promise<ApprovalTicket[]>;
24
+ getById(approvalId: string): Promise<ApprovalTicket | undefined>;
25
+ list(query?: ApprovalListQuery): Promise<ApprovalTicket[]>;
26
+ resolve(input: ResolveApprovalInput): Promise<ApprovalTicket>;
27
+ }
@@ -0,0 +1,5 @@
1
+ import type { ApprovalStatus } from "../contracts";
2
+ export declare class ApprovalStateMachine {
3
+ canTransition(from: ApprovalStatus, to: ApprovalStatus): boolean;
4
+ transition(from: ApprovalStatus, to: ApprovalStatus): ApprovalStatus;
5
+ }
@@ -0,0 +1,9 @@
1
+ import type { ApprovalTicket } from "../../contracts";
2
+ export interface HitlConnector {
3
+ enqueueApproval(ticket: ApprovalTicket): Promise<void>;
4
+ notifyResolution(ticket: ApprovalTicket): Promise<void>;
5
+ }
6
+ export declare class NoopHitlConnector implements HitlConnector {
7
+ enqueueApproval(): Promise<void>;
8
+ notifyResolution(): Promise<void>;
9
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./approval-state-machine";
2
+ export * from "./approval-service";
3
+ export * from "./run-hold-service";
4
+ export * from "./hitl/hitl-connector";
@@ -0,0 +1,16 @@
1
+ import type { GuardrailDecision, RunHoldListQuery, RunHoldTicket } from "../contracts";
2
+ export interface RunHoldService {
3
+ createFromDecision(decision: GuardrailDecision, approvalIds?: string[]): Promise<RunHoldTicket[]>;
4
+ getById(holdId: string): Promise<RunHoldTicket | undefined>;
5
+ list(query?: RunHoldListQuery): Promise<RunHoldTicket[]>;
6
+ resume(holdId: string): Promise<RunHoldTicket>;
7
+ cancel(holdId: string): Promise<RunHoldTicket>;
8
+ }
9
+ export declare class InMemoryRunHoldService implements RunHoldService {
10
+ private readonly holds;
11
+ createFromDecision(decision: GuardrailDecision, approvalIds?: string[]): Promise<RunHoldTicket[]>;
12
+ getById(holdId: string): Promise<RunHoldTicket | undefined>;
13
+ list(query?: RunHoldListQuery): Promise<RunHoldTicket[]>;
14
+ resume(holdId: string): Promise<RunHoldTicket>;
15
+ cancel(holdId: string): Promise<RunHoldTicket>;
16
+ }
@@ -0,0 +1,12 @@
1
+ import type { EventLog } from "../adapters";
2
+ import type { AuditEvent } from "../contracts";
3
+ export interface AuditEventStore {
4
+ append(event: AuditEvent): Promise<void>;
5
+ list(): Promise<AuditEvent[]>;
6
+ }
7
+ export declare class JsonAuditEventStore implements AuditEventStore {
8
+ private readonly eventLog;
9
+ constructor(eventLog: EventLog<AuditEvent>);
10
+ append(event: AuditEvent): Promise<void>;
11
+ list(): Promise<AuditEvent[]>;
12
+ }
@@ -0,0 +1,17 @@
1
+ import type { SnapshotStore } from "../adapters";
2
+ import type { ShieldIncident } from "../types";
3
+ import type { AuditEventStore } from "./audit-event-store";
4
+ import { type MetricsProjection } from "./metrics-projection";
5
+ export type AuditReadModel = {
6
+ schemaVersion: 1;
7
+ generatedAt: number;
8
+ incidents: ShieldIncident[];
9
+ metrics: MetricsProjection;
10
+ };
11
+ export declare class AuditReadModelBuilder {
12
+ private readonly auditEventStore;
13
+ private readonly snapshotStore;
14
+ constructor(auditEventStore: AuditEventStore, snapshotStore: SnapshotStore<AuditReadModel>);
15
+ getCurrent(): Promise<AuditReadModel>;
16
+ refresh(): Promise<AuditReadModel>;
17
+ }
@@ -0,0 +1,18 @@
1
+ import type { AuditEvent, GuardrailDecision } from "../contracts";
2
+ import type { AuditLogger } from "../runtime-core";
3
+ import type { AuditReadModelBuilder } from "./audit-read-model-builder";
4
+ import type { AuditEventStore } from "./audit-event-store";
5
+ export interface AuditService extends AuditLogger {
6
+ append(event: AuditEvent): Promise<void>;
7
+ appendDecision(decision: GuardrailDecision): Promise<void>;
8
+ listEvents(): Promise<AuditEvent[]>;
9
+ }
10
+ export declare class DefaultAuditService implements AuditService {
11
+ private readonly eventStore;
12
+ private readonly readModelBuilder?;
13
+ constructor(eventStore: AuditEventStore, readModelBuilder?: AuditReadModelBuilder | undefined);
14
+ append(event: AuditEvent): Promise<void>;
15
+ appendDecision(decision: GuardrailDecision): Promise<void>;
16
+ logDecision(decision: GuardrailDecision): Promise<void>;
17
+ listEvents(): Promise<AuditEvent[]>;
18
+ }
@@ -0,0 +1,7 @@
1
+ import type { ShieldIncident } from "../types";
2
+ import type { AuditReadModelBuilder } from "./audit-read-model-builder";
3
+ export declare class IncidentQueryService {
4
+ private readonly readModelBuilder;
5
+ constructor(readModelBuilder: AuditReadModelBuilder);
6
+ listIncidents(): Promise<ShieldIncident[]>;
7
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./audit-event-store";
2
+ export * from "./audit-service";
3
+ export * from "./incident-query-service";
4
+ export * from "./metrics-projection";
5
+ export * from "./audit-read-model-builder";
@@ -0,0 +1,10 @@
1
+ import type { AuditEvent } from "../contracts";
2
+ import type { ShieldSeverity } from "../types";
3
+ export type MetricsProjection = {
4
+ totalEvents: number;
5
+ eventTypeCounts: Record<string, number>;
6
+ blockedDecisionCount: number;
7
+ decisionCount: number;
8
+ severityCounts: Record<ShieldSeverity, number>;
9
+ };
10
+ export declare const buildMetricsProjection: (events: AuditEvent[]) => MetricsProjection;
@@ -0,0 +1,3 @@
1
+ import type { OpenClawApi } from "../contracts";
2
+ import { PluginRuntime, type PluginRuntimeOptions } from "../openclaw/plugin-runtime";
3
+ export declare const createRuntimeGuardrailPlugin: (api: OpenClawApi, options?: PluginRuntimeOptions) => Promise<PluginRuntime>;
@@ -0,0 +1,2 @@
1
+ import type { RuntimeBootstrapOptions, RuntimeFacade } from "./runtime-facade";
2
+ export declare const createRuntimeFacade: (options?: RuntimeBootstrapOptions) => RuntimeFacade;
@@ -0,0 +1,3 @@
1
+ export * from "./runtime-facade";
2
+ export * from "./dependency-container";
3
+ export * from "./create-runtime-guardrail-plugin";
@@ -0,0 +1,31 @@
1
+ import type { FileStore } from "../adapters";
2
+ import type { ApprovalService, RunHoldService } from "../approval";
3
+ import type { AuditEventStore, AuditReadModelBuilder, AuditService, IncidentQueryService } from "../audit";
4
+ import type { EgressMediator, ExecutionBroker, ExecutionPlanBuilder, ModelGovernanceService } from "../execution";
5
+ import type { OperatorCommandService, OperatorQueryService, ReportService } from "../operator";
6
+ import type { AuditLogger, EvaluateService, PolicyEvaluator, ShieldRuntimeConfig } from "../runtime-core";
7
+ export type RuntimeBootstrapOptions = {
8
+ config?: Partial<ShieldRuntimeConfig>;
9
+ policyEvaluator?: PolicyEvaluator;
10
+ auditLogger?: AuditLogger;
11
+ };
12
+ export interface RuntimeFacade {
13
+ runtimeConfig: ShieldRuntimeConfig;
14
+ fileStore: FileStore;
15
+ policyEvaluator: PolicyEvaluator;
16
+ auditLogger: AuditLogger;
17
+ evaluateService: EvaluateService;
18
+ auditEventStore: AuditEventStore;
19
+ auditReadModelBuilder: AuditReadModelBuilder;
20
+ auditService: AuditService;
21
+ incidentQueryService: IncidentQueryService;
22
+ approvalService: ApprovalService;
23
+ runHoldService: RunHoldService;
24
+ modelGovernanceService: ModelGovernanceService;
25
+ egressMediator: EgressMediator;
26
+ executionPlanBuilder: ExecutionPlanBuilder;
27
+ executionBroker: ExecutionBroker;
28
+ operatorQueryService: OperatorQueryService;
29
+ operatorCommandService: OperatorCommandService;
30
+ reportService: ReportService;
31
+ }
@@ -0,0 +1 @@
1
+ export * from "./legacy-types";
@@ -0,0 +1,29 @@
1
+ import type { ApprovalTicket, AuditEvent, GuardrailDecision, RunHoldTicket } from "../contracts";
2
+ import type { ShieldApprovalRequest, ShieldPolicyResult, ShieldRunHold, ShieldStage } from "../types";
3
+ /**
4
+ * 【兼容层】将旧版策略评估结果映射为新版护栏决策
5
+ * 用于在旧的 ShieldPolicyResult 格式和新的 GuardrailDecision 格式之间做转换
6
+ * 处理流程:
7
+ * 1. 生成决策 ID
8
+ * 2. 标准化发现列表(findings)
9
+ * 3. 根据 effectiveAction、审批请求、运行暂停等生成效果(effects)列表
10
+ * 4. 组装完整决策对象
11
+ */
12
+ export declare const mapPolicyResultToDecision: (policyResult: ShieldPolicyResult, envelopeRef: GuardrailDecision["envelopeRef"], stage: ShieldStage, simulated: boolean) => GuardrailDecision;
13
+ /**
14
+ * 【兼容层】将旧版审批请求映射为新版审批工单
15
+ * ShieldApprovalRequest → ApprovalTicket 的格式转换
16
+ */
17
+ export declare const mapApprovalRequestToTicket: (request: ShieldApprovalRequest) => ApprovalTicket;
18
+ /**
19
+ * 【兼容层】将旧版运行暂停记录映射为新版暂停工单
20
+ * ShieldRunHold → RunHoldTicket 的格式转换
21
+ */
22
+ export declare const mapRunHoldToTicket: (hold: ShieldRunHold) => RunHoldTicket;
23
+ /**
24
+ * 构建决策审计事件
25
+ * 将护栏决策转换为审计事件格式,用于审计日志记录
26
+ * 如果决策中包含 block 效果,事件类型为 "decision.blocked"
27
+ * 否则为 "decision.made"
28
+ */
29
+ export declare const buildDecisionAuditEvent: (decision: GuardrailDecision) => AuditEvent;
@@ -0,0 +1,277 @@
1
+ import type { ShieldApprovalCategory, ShieldFailMode, ShieldSeverity, ShieldStage, ShieldSurface } from "../types";
2
+ /** 统一钩子名称枚举:对应 Agent 生命周期中的各个检测点 */
3
+ export type ShieldCanonicalHookName = "message_received" | "before_llm_request" | "before_tool_call" | "message_sending" | "before_message_write" | "tool_result_persist";
4
+ /**
5
+ * 兼容别名:历史代码使用 ShieldHookName,等价于统一钩子名
6
+ * 后续建议优先使用 ShieldCanonicalHookName
7
+ */
8
+ export type ShieldHookName = ShieldCanonicalHookName;
9
+ /** 宿主类型 */
10
+ export type HostType = "openclaw" | "langchain" | "custom";
11
+ /** 宿主信息(用于跨宿主接入时标识来源) */
12
+ export type HostInfo = {
13
+ hostType: HostType;
14
+ hostVersion?: string;
15
+ };
16
+ /** 钩子信封基础类型:所有钩子事件的公共字段 */
17
+ export type HookEnvelopeBase = {
18
+ /** 统一钩子名:评估引擎优先依赖该字段 */
19
+ canonicalHook: ShieldCanonicalHookName;
20
+ /** 宿主原始钩子名(不同宿主可不一致) */
21
+ hostHookName: string;
22
+ surface: ShieldSurface;
23
+ timestamp: number;
24
+ agentId?: string;
25
+ runId?: string;
26
+ sessionId?: string;
27
+ sessionKey?: string;
28
+ hostInfo?: HostInfo;
29
+ /** 宿主平台传递的上下文信息 */
30
+ hostContext: Record<string, unknown>;
31
+ };
32
+ /** 入口钩子信封:用户消息到达时触发 */
33
+ export type IngressEnvelope = HookEnvelopeBase & {
34
+ canonicalHook: "message_received";
35
+ hostHookName: "message_received" | "before_prompt_build";
36
+ surface: "ingress";
37
+ payload: {
38
+ message: unknown;
39
+ };
40
+ };
41
+ /** LLM 请求钩子信封:向大模型发送请求前触发 */
42
+ export type LlmRequestEnvelope = HookEnvelopeBase & {
43
+ canonicalHook: "before_llm_request";
44
+ hostHookName: "llm_input";
45
+ surface: "llm";
46
+ payload: {
47
+ provider: string;
48
+ model: string;
49
+ prompt: string;
50
+ systemPrompt?: string;
51
+ historyMessages: unknown[];
52
+ skillNames?: string[];
53
+ };
54
+ };
55
+ /** 工具调用钩子信封:执行工具调用前触发 */
56
+ export type ToolCallEnvelope = HookEnvelopeBase & {
57
+ canonicalHook: "before_tool_call";
58
+ hostHookName: "before_tool_call";
59
+ surface: "tool";
60
+ payload: {
61
+ toolName: string;
62
+ toolParams: Record<string, unknown>;
63
+ };
64
+ };
65
+ /** 出口钩子信封:向用户发送消息前触发 */
66
+ export type EgressEnvelope = HookEnvelopeBase & {
67
+ canonicalHook: "message_sending";
68
+ hostHookName: "message_sending";
69
+ surface: "egress";
70
+ payload: {
71
+ message: unknown;
72
+ channel?: string;
73
+ };
74
+ };
75
+ /** 持久化钩子信封:写入消息或工具结果前触发 */
76
+ export type PersistEnvelope = HookEnvelopeBase & {
77
+ canonicalHook: "before_message_write" | "tool_result_persist";
78
+ hostHookName: "before_message_write" | "tool_result_persist";
79
+ surface: "persist";
80
+ payload: {
81
+ message?: unknown;
82
+ toolName?: string;
83
+ toolResult?: unknown;
84
+ };
85
+ };
86
+ /** 钩子信封联合类型:所有钩子信封的联合 */
87
+ export type HookEnvelope = IngressEnvelope | LlmRequestEnvelope | ToolCallEnvelope | EgressEnvelope | PersistEnvelope;
88
+ /** 原因代码:标识安全问题的分类 */
89
+ export type ReasonCode = "prompt_injection" | "retrieval_poisoning" | "sensitive_action" | "egress_exfiltration" | "mcp_allowlist_violation" | "model_governance_violation" | "approval_required" | "unknown";
90
+ /** 标准化的检测发现:将各种来源的发现统一为标准格式 */
91
+ export type NormalizedFinding = {
92
+ findingId: string;
93
+ detector: string;
94
+ reasonCode: ReasonCode;
95
+ severity: ShieldSeverity;
96
+ summary: string;
97
+ tags: string[];
98
+ evidence?: Record<string, unknown>;
99
+ matchedRuleIds?: string[];
100
+ };
101
+ /** 审批工单草案:创建审批请求时的初始数据 */
102
+ export type ApprovalTicketDraft = {
103
+ category: ShieldApprovalCategory;
104
+ reason: string;
105
+ severity: ShieldSeverity;
106
+ tags?: string[];
107
+ agentId?: string;
108
+ runId?: string;
109
+ sessionId?: string;
110
+ toolName?: string;
111
+ /** 审批来源绑定(关联外部审批系统) */
112
+ sourceBinding?: {
113
+ source: string;
114
+ externalApprovalId?: string;
115
+ };
116
+ /** 相关资源信息 */
117
+ resource?: {
118
+ toolName?: string;
119
+ provider?: string;
120
+ model?: string;
121
+ };
122
+ };
123
+ /** 运行暂停草案:创建运行暂停时的初始数据 */
124
+ export type RunHoldDraft = {
125
+ toolName: string;
126
+ toolParams?: Record<string, unknown>;
127
+ /** 暂停时所处的护盾阶段 */
128
+ stageAtHold: ShieldStage;
129
+ /** 暂停时的失败模式 */
130
+ failModeAtHold: ShieldFailMode;
131
+ category?: ShieldApprovalCategory;
132
+ severity?: ShieldSeverity;
133
+ reason?: string;
134
+ agentId?: string;
135
+ runId?: string;
136
+ sessionId?: string;
137
+ };
138
+ /** 执行计划草案:描述即将执行的操作类型和要求 */
139
+ export type ExecutionPlanDraft = {
140
+ /** 操作类型:llm_request=LLM请求 | tool_call=工具调用 | message_send=消息发送 */
141
+ actionType: "llm_request" | "tool_call" | "message_send";
142
+ /** 要求的后端环境 */
143
+ requiredBackend?: "local" | "sandbox" | "remote_controlled";
144
+ /** 沙箱配置 */
145
+ sandboxProfile?: string;
146
+ /** 审批模式 */
147
+ approvalMode?: "none" | "required" | "already_bound";
148
+ /** 预算配置 */
149
+ budgetProfile?: string;
150
+ };
151
+ /**
152
+ * 决策效果联合类型:描述护栏决策产生的各种效果
153
+ * - allow: 放行
154
+ * - redact: 脱敏(指定字段和替换值)
155
+ * - block: 拦截(附带原因)
156
+ * - queue_approval: 排队等待审批
157
+ * - create_run_hold: 暂停运行
158
+ * - require_model_profile: 要求使用指定模型配置
159
+ * - restrict_model: 限制可用的模型/提供商
160
+ * - route_execution: 路由到指定执行计划
161
+ * - emit_audit: 发出审计事件
162
+ */
163
+ export type DecisionEffect = {
164
+ type: "allow";
165
+ } | {
166
+ type: "redact";
167
+ targets: Array<{
168
+ field: string;
169
+ replacement: string;
170
+ ruleId?: string;
171
+ value?: unknown;
172
+ }>;
173
+ } | {
174
+ type: "block";
175
+ reason: string;
176
+ } | {
177
+ type: "queue_approval";
178
+ ticket: ApprovalTicketDraft;
179
+ } | {
180
+ type: "create_run_hold";
181
+ hold: RunHoldDraft;
182
+ } | {
183
+ type: "require_model_profile";
184
+ profileId: string;
185
+ } | {
186
+ type: "restrict_model";
187
+ allowedProviderIds?: string[];
188
+ allowedModelIds?: string[];
189
+ } | {
190
+ type: "route_execution";
191
+ plan: ExecutionPlanDraft;
192
+ } | {
193
+ type: "emit_audit";
194
+ eventType: string;
195
+ };
196
+ /** 护栏决策:一次完整的安全评估决策结果 */
197
+ export type GuardrailDecision = {
198
+ decisionId: string;
199
+ /** 触发决策的钩子信封引用 */
200
+ envelopeRef: {
201
+ /** 统一钩子名 */
202
+ canonicalHook: ShieldCanonicalHookName;
203
+ /** 宿主原始钩子名(可选,跨宿主时可能与 canonicalHook 不同) */
204
+ hostHookName?: string;
205
+ surface: ShieldSurface;
206
+ agentId?: string;
207
+ runId?: string;
208
+ sessionId?: string;
209
+ };
210
+ /** 当前护盾阶段 */
211
+ stage: ShieldStage;
212
+ severity: ShieldSeverity;
213
+ /** 标准化检测发现列表 */
214
+ findings: NormalizedFinding[];
215
+ /** 决策产生的效果列表 */
216
+ effects: DecisionEffect[];
217
+ /** 原因代码列表 */
218
+ reasonCodes: ReasonCode[];
219
+ tags: string[];
220
+ /** 是否为模拟模式 */
221
+ simulated: boolean;
222
+ /** 评估耗时(毫秒) */
223
+ durationMs: number;
224
+ };
225
+ /** 审批状态 */
226
+ export type ApprovalStatus = "pending" | "approved" | "rejected" | "expired" | "cancelled";
227
+ /** 审批工单(完整版):在草案基础上增加了 ID、状态和时间戳等运行时字段 */
228
+ export type ApprovalTicket = ApprovalTicketDraft & {
229
+ approvalId: string;
230
+ incidentId?: string;
231
+ status: ApprovalStatus;
232
+ createdAt: number;
233
+ resolvedAt?: number;
234
+ resolvedBy?: string;
235
+ note?: string;
236
+ };
237
+ /** 运行暂停状态 */
238
+ export type RunHoldStatus = "pending" | "approved_waiting_resume" | "resumed" | "rejected" | "expired" | "cancelled";
239
+ /** 运行暂停工单(完整版):在草案基础上增加了 ID、状态和时间戳 */
240
+ export type RunHoldTicket = RunHoldDraft & {
241
+ holdId: string;
242
+ approvalId: string;
243
+ status: RunHoldStatus;
244
+ createdAt: number;
245
+ resumedAt?: number;
246
+ cancelledAt?: number;
247
+ };
248
+ /** 执行计划(完整版):在草案基础上增加了模型限制和出口策略等 */
249
+ export type ExecutionPlan = ExecutionPlanDraft & {
250
+ planId: string;
251
+ allowedProviderIds?: string[];
252
+ allowedModelIds?: string[];
253
+ requiredModelProfile?: string;
254
+ /** 出口策略:限制目标地址和渠道 */
255
+ egressPolicy?: {
256
+ destinationAllowlist?: string[];
257
+ channelAllowlist?: string[];
258
+ };
259
+ };
260
+ /** 审计事件类型枚举 */
261
+ export type AuditEventType = "decision.made" | "decision.blocked" | "approval.queued" | "approval.resolved" | "run_hold.created" | "run_hold.resumed" | "hitl.deadletter.queued" | "hitl.deadletter.resolved";
262
+ /** 审计事件:记录系统中发生的每一个重要安全事件 */
263
+ export type AuditEvent = {
264
+ eventId: string;
265
+ eventType: AuditEventType;
266
+ timestamp: number;
267
+ decisionId?: string;
268
+ approvalId?: string;
269
+ holdId?: string;
270
+ agentId?: string;
271
+ runId?: string;
272
+ sessionId?: string;
273
+ /** 事件的详细数据 */
274
+ payload: Record<string, unknown>;
275
+ /** Schema 版本号 */
276
+ schemaVersion: 1;
277
+ };
@@ -0,0 +1,35 @@
1
+ /** 护盾网关事件常量:定义所有可以通过网关发布的事件名称 */
2
+ export declare const SHIELD_GATEWAY_EVENTS: {
3
+ readonly decision: "shield.decision";
4
+ readonly blocked: "shield.blocked";
5
+ readonly stageChanged: "shield.stage.changed";
6
+ readonly approvalQueued: "shield.approval.queued";
7
+ readonly approvalResolved: "shield.approval.resolved";
8
+ readonly hitlDeadLetterQueued: "shield.hitl.deadletter.queued";
9
+ readonly hitlDeadLetterResolved: "shield.hitl.deadletter.resolved";
10
+ };
11
+ /** 网关事件的通用包装类型 */
12
+ export type GatewayEvent<T = unknown> = {
13
+ eventName: string;
14
+ data: T;
15
+ };
16
+ /** 决策事件数据:决策做出时附带的信息 */
17
+ export type DecisionEventData = {
18
+ decisionId: string;
19
+ hook: string;
20
+ surface: string;
21
+ stage: string;
22
+ severity: string;
23
+ effects: unknown[];
24
+ reasonCodes: string[];
25
+ simulated: boolean;
26
+ durationMs: number;
27
+ };
28
+ /** 拦截事件数据:请求被拦截时附带的信息 */
29
+ export type BlockedEventData = {
30
+ decisionId: string;
31
+ hook: string;
32
+ surface: string;
33
+ reason: string;
34
+ findings: unknown[];
35
+ };