@capabilityhostprotocol/types 0.1.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,252 @@
1
+ /**
2
+ * Governance Types
3
+ *
4
+ * Governance layer types for capability execution control.
5
+ * These determine how governance violations are handled.
6
+ *
7
+ * @module governance
8
+ */
9
+
10
+ import type { AssuranceTier } from "./assurance.js";
11
+ import type { RiskClass } from "./risk.js";
12
+ import type { DeclaredInvariant } from "./invariants.js";
13
+ import type { SubjectContext } from "./context.js";
14
+ import type { Evidence, EvidenceType } from "./evidence.js";
15
+ import { createEvidence } from "./evidence.js";
16
+
17
+ /**
18
+ * Mode of governance enforcement.
19
+ *
20
+ * Determines how governance violations are handled.
21
+ *
22
+ * - ENFORCE: Violations block execution (production)
23
+ * - AUDIT: Violations are logged but allowed (testing)
24
+ * - DISABLED: No governance checks (development only)
25
+ * - SHADOW: Parallel check without blocking (migration)
26
+ */
27
+ export type GovernanceMode = "enforce" | "audit" | "disabled" | "shadow";
28
+
29
+ /**
30
+ * Governance modes as a const array.
31
+ */
32
+ export const GOVERNANCE_MODES = [
33
+ "enforce",
34
+ "audit",
35
+ "disabled",
36
+ "shadow",
37
+ ] as const;
38
+
39
+ /**
40
+ * Check if a governance mode blocks execution on violations.
41
+ */
42
+ export function blocksExecution(mode: GovernanceMode): boolean {
43
+ return mode === "enforce";
44
+ }
45
+
46
+ /**
47
+ * Check if a governance mode emits evidence.
48
+ */
49
+ export function emitsEvidence(mode: GovernanceMode): boolean {
50
+ return mode !== "disabled";
51
+ }
52
+
53
+ /**
54
+ * Execution context for governed capabilities.
55
+ *
56
+ * Provides access to subject information, governance state, evidence emission,
57
+ * and execution metadata during capability execution.
58
+ */
59
+ export interface GovernedContext {
60
+ /** Unique ID for this invocation */
61
+ invocation_id: string;
62
+
63
+ /** The capability being invoked */
64
+ capability_id: string;
65
+
66
+ /** The subject (user/agent) invoking */
67
+ subject: SubjectContext;
68
+
69
+ /** For linking related invocations */
70
+ correlation_id: string;
71
+
72
+ /** W3C Trace Context trace ID */
73
+ trace_id: string | null;
74
+
75
+ /** Active enforcement mode */
76
+ governance_mode: GovernanceMode;
77
+
78
+ /** Risk classification of the capability */
79
+ risk_class: RiskClass;
80
+
81
+ /** Required assurance tier */
82
+ minimum_tier: AssuranceTier;
83
+
84
+ /** Active invariants */
85
+ invariants: DeclaredInvariant[];
86
+
87
+ /** Evidence emitted during execution */
88
+ evidence: Evidence[];
89
+
90
+ /** Additional context data */
91
+ metadata: Record<string, unknown>;
92
+
93
+ /** ISO-8601 timestamp when context was created */
94
+ started_at: string;
95
+ }
96
+
97
+ /**
98
+ * Create a new governed context.
99
+ */
100
+ export function createGovernedContext(params: {
101
+ capability_id: string;
102
+ subject: SubjectContext;
103
+ governance_mode?: GovernanceMode;
104
+ risk_class?: RiskClass;
105
+ minimum_tier?: AssuranceTier;
106
+ correlation_id?: string | null;
107
+ trace_id?: string | null;
108
+ invariants?: DeclaredInvariant[];
109
+ metadata?: Record<string, unknown>;
110
+ }): GovernedContext {
111
+ return {
112
+ invocation_id: crypto.randomUUID(),
113
+ capability_id: params.capability_id,
114
+ subject: params.subject,
115
+ correlation_id: params.correlation_id ?? crypto.randomUUID(),
116
+ trace_id: params.trace_id ?? null,
117
+ governance_mode: params.governance_mode ?? "enforce",
118
+ risk_class: params.risk_class ?? "medium",
119
+ minimum_tier: params.minimum_tier ?? "S1",
120
+ invariants: params.invariants ?? [],
121
+ evidence: [],
122
+ metadata: params.metadata ?? {},
123
+ started_at: new Date().toISOString(),
124
+ };
125
+ }
126
+
127
+ /**
128
+ * Emit evidence within a governed context.
129
+ * Mutates the context by appending to evidence array.
130
+ */
131
+ export function emitEvidence(
132
+ ctx: GovernedContext,
133
+ evidence_type: EvidenceType,
134
+ payload?: Record<string, unknown>,
135
+ assurance_tier?: AssuranceTier
136
+ ): Evidence {
137
+ const evidence = createEvidence({
138
+ evidence_type,
139
+ capability_id: ctx.capability_id,
140
+ subject_id: ctx.subject.subject_id,
141
+ correlation_id: ctx.correlation_id,
142
+ assurance_tier: assurance_tier ?? ctx.minimum_tier,
143
+ payload: payload ?? {},
144
+ trace_id: ctx.trace_id,
145
+ });
146
+ ctx.evidence.push(evidence);
147
+ return evidence;
148
+ }
149
+
150
+ /**
151
+ * Create a child context for invoking another capability.
152
+ * Maintains correlation and trace context.
153
+ */
154
+ export function createChildContext(
155
+ parent: GovernedContext,
156
+ capability_id: string
157
+ ): GovernedContext {
158
+ return {
159
+ invocation_id: crypto.randomUUID(),
160
+ capability_id,
161
+ subject: parent.subject,
162
+ correlation_id: parent.correlation_id,
163
+ trace_id: parent.trace_id,
164
+ governance_mode: parent.governance_mode,
165
+ risk_class: parent.risk_class,
166
+ minimum_tier: parent.minimum_tier,
167
+ invariants: [],
168
+ evidence: [],
169
+ metadata: {
170
+ ...parent.metadata,
171
+ parent_invocation_id: parent.invocation_id,
172
+ },
173
+ started_at: new Date().toISOString(),
174
+ };
175
+ }
176
+
177
+ /**
178
+ * Calculate elapsed time in milliseconds.
179
+ */
180
+ export function elapsedMs(ctx: GovernedContext): number {
181
+ const started = new Date(ctx.started_at).getTime();
182
+ return Date.now() - started;
183
+ }
184
+
185
+ /**
186
+ * Configuration for governance behavior.
187
+ */
188
+ export interface GovernanceConfig {
189
+ /** Default enforcement mode */
190
+ default_mode: GovernanceMode;
191
+
192
+ /** Whether a subject is always required */
193
+ require_subject: boolean;
194
+
195
+ /** Whether to emit evidence */
196
+ emit_evidence: boolean;
197
+
198
+ /** Whether to emit evidence for denials */
199
+ audit_denials: boolean;
200
+
201
+ /** Default assurance tier */
202
+ default_tier: AssuranceTier;
203
+ }
204
+
205
+ /**
206
+ * Create default governance configuration.
207
+ */
208
+ export function createGovernanceConfig(
209
+ overrides?: Partial<GovernanceConfig>
210
+ ): GovernanceConfig {
211
+ return {
212
+ default_mode: "enforce",
213
+ require_subject: true,
214
+ emit_evidence: true,
215
+ audit_denials: true,
216
+ default_tier: "S1",
217
+ ...overrides,
218
+ };
219
+ }
220
+
221
+ /**
222
+ * Development environment configuration.
223
+ */
224
+ export const DEVELOPMENT_CONFIG: GovernanceConfig = {
225
+ default_mode: "audit",
226
+ require_subject: false,
227
+ emit_evidence: true,
228
+ audit_denials: true,
229
+ default_tier: "S1",
230
+ };
231
+
232
+ /**
233
+ * Production environment configuration.
234
+ */
235
+ export const PRODUCTION_CONFIG: GovernanceConfig = {
236
+ default_mode: "enforce",
237
+ require_subject: true,
238
+ emit_evidence: true,
239
+ audit_denials: true,
240
+ default_tier: "S2",
241
+ };
242
+
243
+ /**
244
+ * Testing environment configuration.
245
+ */
246
+ export const TESTING_CONFIG: GovernanceConfig = {
247
+ default_mode: "disabled",
248
+ require_subject: false,
249
+ emit_evidence: false,
250
+ audit_denials: false,
251
+ default_tier: "S1",
252
+ };
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Public TypeScript types for CHP v0.1.
3
+ *
4
+ * The root export is intentionally limited to the open-source protocol surface
5
+ * described by `spec/chp-v0.1.md` and the JSON Schemas in `schemas/`.
6
+ *
7
+ * Internal legacy mesh/governance helpers are available from
8
+ * `@auxo/ts-types/legacy`.
9
+ *
10
+ * @packageDocumentation
11
+ */
12
+
13
+ export * from "./v0_1.js";
14
+
15
+ export const CHP_VERSION = "0.1";
16
+ export const VERSION = "0.1.0";
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Invariant Types
3
+ *
4
+ * Invariants are constraints that must hold for a capability to execute.
5
+ * They are checked at the invocation boundary or during execution.
6
+ *
7
+ * @module invariants
8
+ */
9
+
10
+ /**
11
+ * Classification of invariant constraints.
12
+ *
13
+ * Determines when and how invariants are checked and enforced.
14
+ *
15
+ * - STRUCTURAL: Code/schema correctness (compile-time checkable)
16
+ * - ENVIRONMENTAL: Runtime environment constraints
17
+ * - DATA: Input/output data validation
18
+ * - TEMPORAL: Time-based constraints (deadlines, ordering)
19
+ * - CAUSAL: Dependency and causality constraints
20
+ */
21
+ export type InvariantClass =
22
+ | "structural"
23
+ | "environmental"
24
+ | "data"
25
+ | "temporal"
26
+ | "causal";
27
+
28
+ /**
29
+ * Invariant classes as a const array.
30
+ */
31
+ export const INVARIANT_CLASSES = [
32
+ "structural",
33
+ "environmental",
34
+ "data",
35
+ "temporal",
36
+ "causal",
37
+ ] as const;
38
+
39
+ /**
40
+ * Who is responsible for enforcing an invariant.
41
+ *
42
+ * - HOST: The capability host enforces at invocation boundary
43
+ * - RUNTIME: Enforced during capability execution
44
+ * - SUBSTRATE: Enforced by underlying infrastructure (e.g., Zenoh)
45
+ * - DECLARATIVE: Declared but not actively enforced
46
+ */
47
+ export type EnforcementResponsibility =
48
+ | "host"
49
+ | "runtime"
50
+ | "substrate"
51
+ | "declarative";
52
+
53
+ /**
54
+ * What happens when an invariant fails.
55
+ *
56
+ * - DENY: Prevent execution entirely
57
+ * - ABORT: Stop execution and rollback if possible
58
+ * - WARN: Log warning but allow execution
59
+ * - DEGRADE: Continue with reduced assurance
60
+ */
61
+ export type FailureBehavior = "deny" | "abort" | "warn" | "degrade";
62
+
63
+ /**
64
+ * A declared invariant constraint for a capability.
65
+ *
66
+ * This is the canonical CHP invariant structure that must be
67
+ * faithfully serialized/deserialized across language boundaries.
68
+ */
69
+ export interface DeclaredInvariant {
70
+ /** Unique identifier for this invariant */
71
+ invariant_id: string;
72
+
73
+ /** Type of invariant (structural, temporal, etc.) */
74
+ invariant_class: InvariantClass;
75
+
76
+ /** Who enforces this invariant */
77
+ enforcement: EnforcementResponsibility;
78
+
79
+ /** What happens on failure */
80
+ failure_behavior: FailureBehavior;
81
+
82
+ /** Human-readable description */
83
+ description: string;
84
+
85
+ /** Invariant-specific configuration */
86
+ parameters: Record<string, unknown>;
87
+ }
88
+
89
+ /**
90
+ * Create a new declared invariant.
91
+ */
92
+ export function createDeclaredInvariant(params: {
93
+ invariant_id: string;
94
+ invariant_class: InvariantClass;
95
+ enforcement?: EnforcementResponsibility;
96
+ failure_behavior?: FailureBehavior;
97
+ description?: string;
98
+ parameters?: Record<string, unknown>;
99
+ }): DeclaredInvariant {
100
+ return {
101
+ invariant_id: params.invariant_id,
102
+ invariant_class: params.invariant_class,
103
+ enforcement: params.enforcement ?? "runtime",
104
+ failure_behavior: params.failure_behavior ?? "deny",
105
+ description: params.description ?? "",
106
+ parameters: params.parameters ?? {},
107
+ };
108
+ }
109
+
110
+ /**
111
+ * Validate that an object conforms to the DeclaredInvariant interface.
112
+ */
113
+ export function isDeclaredInvariant(obj: unknown): obj is DeclaredInvariant {
114
+ if (typeof obj !== "object" || obj === null) return false;
115
+
116
+ const i = obj as Record<string, unknown>;
117
+ return (
118
+ typeof i.invariant_id === "string" &&
119
+ typeof i.invariant_class === "string" &&
120
+ typeof i.enforcement === "string" &&
121
+ typeof i.failure_behavior === "string"
122
+ );
123
+ }
package/src/legacy.ts ADDED
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Legacy/internal TypeScript types for pre-v0.1 CHP work.
3
+ *
4
+ * These exports are retained for internal packages that still use the older
5
+ * mesh/governance model. They are not the public CHP v0.1 protocol surface.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+
10
+ export {
11
+ type RiskClass,
12
+ RISK_CLASSES,
13
+ RISK_CLASS_ORDER,
14
+ compareRiskClass,
15
+ isRiskAtLeast,
16
+ } from "./risk.js";
17
+
18
+ export {
19
+ type AssuranceTier,
20
+ ASSURANCE_TIERS,
21
+ ASSURANCE_TIER_DISPLAY_NAMES,
22
+ ASSURANCE_TIER_ORDER,
23
+ compareAssuranceTier,
24
+ meetsAssuranceTier,
25
+ getAssuranceTierDisplayName,
26
+ } from "./assurance.js";
27
+
28
+ export {
29
+ type EvidenceType,
30
+ type Evidence,
31
+ EVIDENCE_TYPES,
32
+ createEvidence,
33
+ isEvidence,
34
+ } from "./evidence.js";
35
+
36
+ export {
37
+ type InvariantClass,
38
+ type EnforcementResponsibility,
39
+ type FailureBehavior,
40
+ type DeclaredInvariant,
41
+ INVARIANT_CLASSES,
42
+ createDeclaredInvariant,
43
+ isDeclaredInvariant,
44
+ } from "./invariants.js";
45
+
46
+ export {
47
+ type SubjectContext,
48
+ type InvocationContext,
49
+ type ExecutionOutcome,
50
+ EXECUTION_OUTCOMES,
51
+ createSubjectContext,
52
+ createInvocationContext,
53
+ hasEntitlement,
54
+ } from "./context.js";
55
+
56
+ export {
57
+ type GovernanceMode,
58
+ type GovernedContext,
59
+ type GovernanceConfig,
60
+ GOVERNANCE_MODES,
61
+ DEVELOPMENT_CONFIG,
62
+ PRODUCTION_CONFIG,
63
+ TESTING_CONFIG,
64
+ blocksExecution,
65
+ emitsEvidence,
66
+ createGovernedContext,
67
+ createGovernanceConfig,
68
+ createChildContext,
69
+ emitEvidence,
70
+ elapsedMs,
71
+ } from "./governance.js";
72
+
73
+ export {
74
+ type CapabilityDeclaration,
75
+ type InvocationMode,
76
+ type HostIdentity,
77
+ INVOCATION_MODES,
78
+ getCapabilityId,
79
+ createCapabilityDeclaration,
80
+ isCapabilityDeclaration,
81
+ createHostIdentity,
82
+ } from "./capability.js";
83
+
84
+ export const LEGACY_CHP_VERSION = "1.0";
package/src/risk.ts ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Risk Classification Types
3
+ *
4
+ * Risk classes determine the level of governance scrutiny applied to a capability.
5
+ * Higher risk classes require stronger assurance and more evidence.
6
+ *
7
+ * @module risk
8
+ */
9
+
10
+ /**
11
+ * Risk classification for capabilities.
12
+ *
13
+ * Ordered from lowest to highest risk:
14
+ * - INFORMATIONAL: Read-only, no side effects
15
+ * - LOW: Minor side effects, easily reversible
16
+ * - MEDIUM: Moderate side effects, may require coordination
17
+ * - HIGH: Significant side effects, requires authorization
18
+ * - CRITICAL: System-wide impact, requires multi-party approval
19
+ */
20
+ export type RiskClass =
21
+ | "informational"
22
+ | "low"
23
+ | "medium"
24
+ | "high"
25
+ | "critical";
26
+
27
+ /**
28
+ * Risk class values as a const array for iteration.
29
+ */
30
+ export const RISK_CLASSES = [
31
+ "informational",
32
+ "low",
33
+ "medium",
34
+ "high",
35
+ "critical",
36
+ ] as const;
37
+
38
+ /**
39
+ * Risk class numeric order for comparison.
40
+ */
41
+ export const RISK_CLASS_ORDER: Record<RiskClass, number> = {
42
+ informational: 0,
43
+ low: 1,
44
+ medium: 2,
45
+ high: 3,
46
+ critical: 4,
47
+ };
48
+
49
+ /**
50
+ * Compare two risk classes.
51
+ * @returns negative if a < b, 0 if equal, positive if a > b
52
+ */
53
+ export function compareRiskClass(a: RiskClass, b: RiskClass): number {
54
+ return RISK_CLASS_ORDER[a] - RISK_CLASS_ORDER[b];
55
+ }
56
+
57
+ /**
58
+ * Check if a risk class is at least as high as another.
59
+ */
60
+ export function isRiskAtLeast(actual: RiskClass, required: RiskClass): boolean {
61
+ return RISK_CLASS_ORDER[actual] >= RISK_CLASS_ORDER[required];
62
+ }
package/src/v0_1.ts ADDED
@@ -0,0 +1,166 @@
1
+ /**
2
+ * CHP v0.1 protocol types.
3
+ *
4
+ * These interfaces mirror the JSON Schemas in the repository root `schemas/`
5
+ * directory. They are intentionally transport-neutral and do not depend on the
6
+ * older mesh/governance TypeScript model in this package.
7
+ */
8
+
9
+ export const CHP_V0_1_VERSION = "0.1" as const;
10
+
11
+ export type JsonPrimitive = string | number | boolean | null;
12
+ export type JsonValue = JsonPrimitive | JsonValue[] | JsonObject;
13
+ export interface JsonObject {
14
+ [key: string]: JsonValue;
15
+ }
16
+
17
+ export type CapabilityRisk = "low" | "medium" | "high" | "critical";
18
+ export type ChpV01InvocationMode = "sync" | "async" | "stream" | "fire_and_forget";
19
+ export type ChpV01ExecutionOutcome = "success" | "failure" | "denied" | "skipped";
20
+ export type AssuranceLevel = "S1" | "S2" | "S3";
21
+ export type InvariantEnforcement = "declarative" | "host" | "runtime";
22
+ export type InvariantFailureBehavior = "deny" | "warn" | "degrade";
23
+
24
+ export const CHP_V0_1_OUTCOMES = [
25
+ "success",
26
+ "failure",
27
+ "denied",
28
+ "skipped",
29
+ ] as const satisfies readonly ChpV01ExecutionOutcome[];
30
+
31
+ export const CHP_V0_1_CORE_EVIDENCE_TYPES = [
32
+ "execution_started",
33
+ "execution_completed",
34
+ "execution_failed",
35
+ "execution_denied",
36
+ "execution_skipped",
37
+ ] as const;
38
+
39
+ export interface AssuranceMetadata {
40
+ level: AssuranceLevel;
41
+ evidence_policy?: string;
42
+ notes?: string[];
43
+ }
44
+
45
+ export interface InvariantDescriptor {
46
+ id: string;
47
+ kind: string;
48
+ description?: string;
49
+ enforcement?: InvariantEnforcement;
50
+ failure_behavior?: InvariantFailureBehavior;
51
+ parameters?: JsonObject;
52
+ }
53
+
54
+ export interface CapabilityDescriptor {
55
+ id: string;
56
+ version: string;
57
+ capability_uri?: string;
58
+ description: string;
59
+ modes: ChpV01InvocationMode[];
60
+ input_schema?: JsonObject;
61
+ output_schema?: JsonObject;
62
+ invariants?: InvariantDescriptor[];
63
+ emits: string[];
64
+ owner?: string | null;
65
+ tags?: string[];
66
+ risk?: CapabilityRisk;
67
+ assurance?: AssuranceMetadata;
68
+ metadata?: JsonObject;
69
+ }
70
+
71
+ export interface HostDescriptor {
72
+ id: string;
73
+ version: string;
74
+ protocol_version: typeof CHP_V0_1_VERSION;
75
+ kind: string;
76
+ capabilities: CapabilityDescriptor[];
77
+ evidence: {
78
+ store: string;
79
+ append_only: boolean;
80
+ [key: string]: JsonValue;
81
+ };
82
+ metadata?: JsonObject;
83
+ }
84
+
85
+ export interface CorrelationContext {
86
+ correlation_id: string;
87
+ causation_id?: string | null;
88
+ parent_correlation_id?: string | null;
89
+ trace_id?: string | null;
90
+ baggage?: Record<string, JsonPrimitive>;
91
+ }
92
+
93
+ export interface InvocationEnvelope {
94
+ invocation_id: string;
95
+ capability_id: string;
96
+ version?: string | null;
97
+ mode: ChpV01InvocationMode;
98
+ correlation: CorrelationContext;
99
+ subject: JsonObject;
100
+ payload: JsonObject;
101
+ requested_at: string;
102
+ metadata?: JsonObject;
103
+ }
104
+
105
+ export interface DenialReason {
106
+ code: string;
107
+ message: string;
108
+ invariant_id?: string | null;
109
+ retryable: boolean;
110
+ details?: JsonObject;
111
+ }
112
+
113
+ export interface InvocationResult {
114
+ invocation_id: string;
115
+ capability_id: string;
116
+ capability_version?: string | null;
117
+ correlation: CorrelationContext;
118
+ outcome: ChpV01ExecutionOutcome;
119
+ success: boolean;
120
+ data?: JsonValue;
121
+ error?: JsonObject | null;
122
+ denial?: DenialReason | null;
123
+ evidence_ids: string[];
124
+ started_at?: string | null;
125
+ completed_at: string;
126
+ }
127
+
128
+ export interface ExecutionEvidence {
129
+ event_id: string;
130
+ event_type: string;
131
+ invocation_id: string;
132
+ capability_id: string;
133
+ capability_version?: string | null;
134
+ host_id: string;
135
+ correlation: CorrelationContext;
136
+ timestamp: string;
137
+ sequence: number;
138
+ outcome?: ChpV01ExecutionOutcome | null;
139
+ payload: JsonObject;
140
+ redacted: boolean;
141
+ error?: JsonObject | null;
142
+ denial?: DenialReason | null;
143
+ assurance: AssuranceMetadata;
144
+ }
145
+
146
+ export interface ReplayQuery {
147
+ correlation_id: string;
148
+ limit?: number | null;
149
+ since_sequence?: number | null;
150
+ include_payloads?: boolean;
151
+ }
152
+
153
+ export interface ReplayResult {
154
+ correlation_id: string;
155
+ events: ExecutionEvidence[];
156
+ event_count: number;
157
+ replayed_at: string;
158
+ }
159
+
160
+ export function capabilityUri(descriptor: Pick<CapabilityDescriptor, "id" | "version">): string {
161
+ return `${descriptor.id}:${descriptor.version}`;
162
+ }
163
+
164
+ export function isChpV01Outcome(value: string): value is ChpV01ExecutionOutcome {
165
+ return (CHP_V0_1_OUTCOMES as readonly string[]).includes(value);
166
+ }