@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.
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@capabilityhostprotocol/types",
3
+ "version": "0.1.0",
4
+ "description": "TypeScript protocol types for the Capability Host Protocol (CHP v0.1).",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ },
14
+ "./legacy": {
15
+ "types": "./dist/legacy.d.ts",
16
+ "import": "./dist/legacy.mjs",
17
+ "require": "./dist/legacy.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "src"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsup src/index.ts src/legacy.ts --format cjs,esm --dts",
26
+ "dev": "tsup src/index.ts src/legacy.ts --format cjs,esm --dts --watch",
27
+ "lint": "eslint src/",
28
+ "typecheck": "tsc --noEmit --strict --module NodeNext --moduleResolution NodeNext --target ES2022 examples/v0_1.ts",
29
+ "test": "vitest run",
30
+ "test:watch": "vitest"
31
+ },
32
+ "keywords": [
33
+ "chp",
34
+ "capability-host-protocol",
35
+ "governance",
36
+ "evidence",
37
+ "capabilities",
38
+ "typescript"
39
+ ],
40
+ "author": "Auxo",
41
+ "license": "Apache-2.0",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/capabilityhostprotocol/chp-core.git",
45
+ "directory": "packages/ts-types"
46
+ },
47
+ "engines": {
48
+ "node": ">=18.0.0"
49
+ },
50
+ "devDependencies": {
51
+ "tsup": "^8.5.1",
52
+ "typescript": "^5.9.3"
53
+ }
54
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Assurance Tier Types
3
+ *
4
+ * Three-tier assurance model for execution trust.
5
+ * Each tier provides increasing levels of confidence in execution evidence.
6
+ *
7
+ * @module assurance
8
+ */
9
+
10
+ /**
11
+ * Assurance tiers for execution trust.
12
+ *
13
+ * - S1: Observational - Execution was observed by the host
14
+ * - S2: Structural - Deterministic replay is possible
15
+ * - S3: Attested - Cryptographic proof with verified environment
16
+ */
17
+ export type AssuranceTier = "S1" | "S2" | "S3";
18
+
19
+ /**
20
+ * Assurance tier values as a const array.
21
+ */
22
+ export const ASSURANCE_TIERS = ["S1", "S2", "S3"] as const;
23
+
24
+ /**
25
+ * Human-readable names for assurance tiers.
26
+ */
27
+ export const ASSURANCE_TIER_DISPLAY_NAMES: Record<AssuranceTier, string> = {
28
+ S1: "Observational",
29
+ S2: "Structural",
30
+ S3: "Attested",
31
+ };
32
+
33
+ /**
34
+ * Assurance tier numeric order for comparison.
35
+ */
36
+ export const ASSURANCE_TIER_ORDER: Record<AssuranceTier, number> = {
37
+ S1: 1,
38
+ S2: 2,
39
+ S3: 3,
40
+ };
41
+
42
+ /**
43
+ * Compare two assurance tiers.
44
+ * @returns negative if a < b, 0 if equal, positive if a > b
45
+ */
46
+ export function compareAssuranceTier(a: AssuranceTier, b: AssuranceTier): number {
47
+ return ASSURANCE_TIER_ORDER[a] - ASSURANCE_TIER_ORDER[b];
48
+ }
49
+
50
+ /**
51
+ * Check if an assurance tier meets a minimum requirement.
52
+ */
53
+ export function meetsAssuranceTier(
54
+ actual: AssuranceTier,
55
+ required: AssuranceTier
56
+ ): boolean {
57
+ return ASSURANCE_TIER_ORDER[actual] >= ASSURANCE_TIER_ORDER[required];
58
+ }
59
+
60
+ /**
61
+ * Get the display name for an assurance tier.
62
+ */
63
+ export function getAssuranceTierDisplayName(tier: AssuranceTier): string {
64
+ return ASSURANCE_TIER_DISPLAY_NAMES[tier];
65
+ }
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Capability Declaration Types
3
+ *
4
+ * Capabilities are the units of authority in CHP.
5
+ * This module defines the canonical capability declaration structure.
6
+ *
7
+ * @module capability
8
+ */
9
+
10
+ import type { RiskClass } from "./risk.js";
11
+ import type { AssuranceTier } from "./assurance.js";
12
+ import type { DeclaredInvariant } from "./invariants.js";
13
+ import type { EvidenceType } from "./evidence.js";
14
+
15
+ /**
16
+ * Metadata for a declared capability.
17
+ *
18
+ * This is the canonical record of what a capability is, what it can do,
19
+ * and what governance applies to it.
20
+ */
21
+ export interface CapabilityDeclaration {
22
+ /** Unique capability identifier (e.g., "payment.process") */
23
+ name: string;
24
+
25
+ /** Semantic version string (e.g., "1.0.0") */
26
+ version: string;
27
+
28
+ /** Risk classification (INFORMATIONAL to CRITICAL) */
29
+ risk_class: RiskClass;
30
+
31
+ /** Human-readable description */
32
+ description: string;
33
+
34
+ /** Constraints that must hold for execution */
35
+ invariants: DeclaredInvariant[];
36
+
37
+ /** Evidence types this capability produces */
38
+ evidence_types: EvidenceType[];
39
+
40
+ /** Whether invocation requires authorization */
41
+ require_entitlement: boolean;
42
+
43
+ /** Minimum assurance tier for execution */
44
+ minimum_tier: AssuranceTier;
45
+
46
+ /** Team or individual responsible */
47
+ owner: string | null;
48
+
49
+ /** Categorization tags */
50
+ tags: string[];
51
+ }
52
+
53
+ /**
54
+ * Get the full capability identifier with version.
55
+ */
56
+ export function getCapabilityId(decl: CapabilityDeclaration): string {
57
+ return `${decl.name}:${decl.version}`;
58
+ }
59
+
60
+ /**
61
+ * Create a new capability declaration.
62
+ */
63
+ export function createCapabilityDeclaration(params: {
64
+ name: string;
65
+ version?: string;
66
+ risk_class?: RiskClass;
67
+ description?: string;
68
+ invariants?: DeclaredInvariant[];
69
+ evidence_types?: EvidenceType[];
70
+ require_entitlement?: boolean;
71
+ minimum_tier?: AssuranceTier;
72
+ owner?: string | null;
73
+ tags?: string[];
74
+ }): CapabilityDeclaration {
75
+ return {
76
+ name: params.name,
77
+ version: params.version ?? "1.0.0",
78
+ risk_class: params.risk_class ?? "medium",
79
+ description: params.description ?? "",
80
+ invariants: params.invariants ?? [],
81
+ evidence_types: params.evidence_types ?? [],
82
+ require_entitlement: params.require_entitlement ?? false,
83
+ minimum_tier: params.minimum_tier ?? "S1",
84
+ owner: params.owner ?? null,
85
+ tags: params.tags ?? [],
86
+ };
87
+ }
88
+
89
+ /**
90
+ * Validate that an object conforms to the CapabilityDeclaration interface.
91
+ */
92
+ export function isCapabilityDeclaration(
93
+ obj: unknown
94
+ ): obj is CapabilityDeclaration {
95
+ if (typeof obj !== "object" || obj === null) return false;
96
+
97
+ const c = obj as Record<string, unknown>;
98
+ return (
99
+ typeof c.name === "string" &&
100
+ typeof c.version === "string" &&
101
+ typeof c.risk_class === "string" &&
102
+ typeof c.require_entitlement === "boolean"
103
+ );
104
+ }
105
+
106
+ /**
107
+ * Supported invocation modes for a capability.
108
+ */
109
+ export type InvocationMode =
110
+ | "sync" // Synchronous request/response
111
+ | "async" // Asynchronous with callback/promise
112
+ | "stream" // Streaming response
113
+ | "fire_and_forget"; // No response expected
114
+
115
+ /**
116
+ * Invocation modes as a const array.
117
+ */
118
+ export const INVOCATION_MODES = [
119
+ "sync",
120
+ "async",
121
+ "stream",
122
+ "fire_and_forget",
123
+ ] as const;
124
+
125
+ /**
126
+ * Host identity for capability hosting.
127
+ * Identifies where capabilities are hosted.
128
+ */
129
+ export interface HostIdentity {
130
+ /** Unique host identifier */
131
+ host_id: string;
132
+
133
+ /** Host type (server, edge, agent, etc.) */
134
+ host_type: string;
135
+
136
+ /** Host version/build */
137
+ version: string;
138
+
139
+ /** Environment (production, staging, development) */
140
+ environment: string;
141
+
142
+ /** Additional host metadata */
143
+ metadata: Record<string, unknown>;
144
+ }
145
+
146
+ /**
147
+ * Create a new host identity.
148
+ */
149
+ export function createHostIdentity(params: {
150
+ host_id: string;
151
+ host_type?: string;
152
+ version?: string;
153
+ environment?: string;
154
+ metadata?: Record<string, unknown>;
155
+ }): HostIdentity {
156
+ return {
157
+ host_id: params.host_id,
158
+ host_type: params.host_type ?? "server",
159
+ version: params.version ?? "1.0.0",
160
+ environment: params.environment ?? "development",
161
+ metadata: params.metadata ?? {},
162
+ };
163
+ }
package/src/context.ts ADDED
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Context Types
3
+ *
4
+ * Context types for subject (user/agent) and invocation tracking.
5
+ * These provide the "who" and "what" for capability execution.
6
+ *
7
+ * @module context
8
+ */
9
+
10
+ /**
11
+ * Context about the subject (user/agent) invoking a capability.
12
+ *
13
+ * Subjects are the actors in the system - they invoke capabilities
14
+ * and are tracked for authorization and audit purposes.
15
+ */
16
+ export interface SubjectContext {
17
+ /** Unique identifier for the subject */
18
+ subject_id: string;
19
+
20
+ /** Type of subject (user, agent, service) */
21
+ subject_type: string;
22
+
23
+ /** List of entitlements the subject holds */
24
+ entitlements: string[];
25
+
26
+ /** Additional subject-specific data */
27
+ metadata: Record<string, unknown>;
28
+ }
29
+
30
+ /**
31
+ * Create a new subject context.
32
+ */
33
+ export function createSubjectContext(params: {
34
+ subject_id: string;
35
+ subject_type?: string;
36
+ entitlements?: string[];
37
+ metadata?: Record<string, unknown>;
38
+ }): SubjectContext {
39
+ return {
40
+ subject_id: params.subject_id,
41
+ subject_type: params.subject_type ?? "user",
42
+ entitlements: params.entitlements ?? [],
43
+ metadata: params.metadata ?? {},
44
+ };
45
+ }
46
+
47
+ /**
48
+ * Check if a subject has a specific entitlement.
49
+ */
50
+ export function hasEntitlement(
51
+ subject: SubjectContext,
52
+ entitlement: string
53
+ ): boolean {
54
+ return subject.entitlements.includes(entitlement);
55
+ }
56
+
57
+ /**
58
+ * Context for a capability invocation.
59
+ *
60
+ * Provides access to subject information, correlation tracking,
61
+ * and execution metadata during capability execution.
62
+ */
63
+ export interface InvocationContext {
64
+ /** Unique ID for this invocation */
65
+ invocation_id: string;
66
+
67
+ /** The capability being invoked */
68
+ capability_id: string;
69
+
70
+ /** The subject invoking the capability */
71
+ subject: SubjectContext;
72
+
73
+ /** For linking related invocations */
74
+ correlation_id: string | null;
75
+
76
+ /** W3C Trace Context trace ID */
77
+ trace_id: string | null;
78
+
79
+ /** If this is a child invocation */
80
+ parent_invocation_id: string | null;
81
+
82
+ /** ISO-8601 timestamp when the invocation started */
83
+ timestamp: string;
84
+
85
+ /** Additional context data */
86
+ metadata: Record<string, unknown>;
87
+ }
88
+
89
+ /**
90
+ * Create a new invocation context.
91
+ */
92
+ export function createInvocationContext(params: {
93
+ capability_id: string;
94
+ subject: SubjectContext;
95
+ correlation_id?: string | null;
96
+ trace_id?: string | null;
97
+ parent_invocation_id?: string | null;
98
+ metadata?: Record<string, unknown>;
99
+ }): InvocationContext {
100
+ return {
101
+ invocation_id: crypto.randomUUID(),
102
+ capability_id: params.capability_id,
103
+ subject: params.subject,
104
+ correlation_id: params.correlation_id ?? crypto.randomUUID(),
105
+ trace_id: params.trace_id ?? null,
106
+ parent_invocation_id: params.parent_invocation_id ?? null,
107
+ timestamp: new Date().toISOString(),
108
+ metadata: params.metadata ?? {},
109
+ };
110
+ }
111
+
112
+ /**
113
+ * Outcome of a capability execution.
114
+ *
115
+ * - SUCCESS: Execution completed successfully
116
+ * - FAILURE: Execution failed with an error
117
+ * - DENIED: Execution was denied (entitlement/invariant)
118
+ * - TIMEOUT: Execution exceeded time limit
119
+ * - ABORTED: Execution was aborted (e.g., circuit breaker)
120
+ */
121
+ export type ExecutionOutcome =
122
+ | "success"
123
+ | "failure"
124
+ | "denied"
125
+ | "timeout"
126
+ | "aborted";
127
+
128
+ /**
129
+ * Execution outcomes as a const array.
130
+ */
131
+ export const EXECUTION_OUTCOMES = [
132
+ "success",
133
+ "failure",
134
+ "denied",
135
+ "timeout",
136
+ "aborted",
137
+ ] as const;
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Evidence Types
3
+ *
4
+ * Evidence is the immutable record of capability execution.
5
+ * Every capability execution produces one or more Evidence records.
6
+ * These form the basis of the capability graph and audit trail.
7
+ *
8
+ * @module evidence
9
+ */
10
+
11
+ import type { AssuranceTier } from "./assurance.js";
12
+
13
+ /**
14
+ * Evidence types for capability execution tracking.
15
+ *
16
+ * Every capability execution produces evidence. The type indicates
17
+ * what kind of execution event occurred.
18
+ */
19
+ export type EvidenceType =
20
+ // Core Execution
21
+ | "execution_started"
22
+ | "execution_completed"
23
+ | "execution_failed"
24
+ | "execution_denied"
25
+ | "execution_aborted"
26
+ // Invocation Boundary
27
+ | "invocation_received"
28
+ | "invocation_validated"
29
+ | "invocation_rejected"
30
+ // Authorization
31
+ | "entitlement_checked"
32
+ | "entitlement_granted"
33
+ | "entitlement_denied"
34
+ // Invariants
35
+ | "invariant_checked"
36
+ | "invariant_passed"
37
+ | "invariant_failed"
38
+ // Resilience Primitives
39
+ | "retry_attempted"
40
+ | "retry_exhausted"
41
+ | "timeout_exceeded"
42
+ | "circuit_opened"
43
+ | "circuit_closed"
44
+ | "rate_limited"
45
+ // Assurance
46
+ | "assurance_derived"
47
+ | "assurance_degraded"
48
+ // Lineage
49
+ | "lineage_traced"
50
+ | "causal_link_created";
51
+
52
+ /**
53
+ * All evidence types as a const array for iteration.
54
+ */
55
+ export const EVIDENCE_TYPES = [
56
+ // Core Execution
57
+ "execution_started",
58
+ "execution_completed",
59
+ "execution_failed",
60
+ "execution_denied",
61
+ "execution_aborted",
62
+ // Invocation Boundary
63
+ "invocation_received",
64
+ "invocation_validated",
65
+ "invocation_rejected",
66
+ // Authorization
67
+ "entitlement_checked",
68
+ "entitlement_granted",
69
+ "entitlement_denied",
70
+ // Invariants
71
+ "invariant_checked",
72
+ "invariant_passed",
73
+ "invariant_failed",
74
+ // Resilience Primitives
75
+ "retry_attempted",
76
+ "retry_exhausted",
77
+ "timeout_exceeded",
78
+ "circuit_opened",
79
+ "circuit_closed",
80
+ "rate_limited",
81
+ // Assurance
82
+ "assurance_derived",
83
+ "assurance_degraded",
84
+ // Lineage
85
+ "lineage_traced",
86
+ "causal_link_created",
87
+ ] as const;
88
+
89
+ /**
90
+ * Immutable evidence record for capability execution.
91
+ *
92
+ * This is the canonical CHP evidence structure that must be
93
+ * faithfully serialized/deserialized across language boundaries.
94
+ */
95
+ export interface Evidence {
96
+ /** Unique identifier for this evidence */
97
+ evidence_id: string;
98
+
99
+ /** Type of evidence (from EvidenceType) */
100
+ evidence_type: EvidenceType;
101
+
102
+ /** The capability that produced this evidence */
103
+ capability_id: string;
104
+
105
+ /** ISO-8601 timestamp when the evidence was created */
106
+ timestamp: string;
107
+
108
+ /** The subject (user/agent) that triggered execution */
109
+ subject_id?: string | null;
110
+
111
+ /** Links related evidence across executions */
112
+ correlation_id?: string | null;
113
+
114
+ /** Trust level of this evidence */
115
+ assurance_tier: AssuranceTier;
116
+
117
+ /** Additional context-specific data */
118
+ payload: Record<string, unknown>;
119
+
120
+ /** W3C Trace Context trace ID for distributed tracing */
121
+ trace_id?: string | null;
122
+ }
123
+
124
+ /**
125
+ * Create a new evidence record.
126
+ *
127
+ * @param params - Evidence creation parameters
128
+ * @returns A new Evidence object
129
+ */
130
+ export function createEvidence(params: {
131
+ evidence_type: EvidenceType;
132
+ capability_id: string;
133
+ subject_id?: string | null;
134
+ correlation_id?: string | null;
135
+ assurance_tier?: AssuranceTier;
136
+ payload?: Record<string, unknown>;
137
+ trace_id?: string | null;
138
+ }): Evidence {
139
+ return {
140
+ evidence_id: crypto.randomUUID(),
141
+ evidence_type: params.evidence_type,
142
+ capability_id: params.capability_id,
143
+ timestamp: new Date().toISOString(),
144
+ subject_id: params.subject_id ?? null,
145
+ correlation_id: params.correlation_id ?? null,
146
+ assurance_tier: params.assurance_tier ?? "S1",
147
+ payload: params.payload ?? {},
148
+ trace_id: params.trace_id ?? null,
149
+ };
150
+ }
151
+
152
+ /**
153
+ * Validate that an object conforms to the Evidence interface.
154
+ */
155
+ export function isEvidence(obj: unknown): obj is Evidence {
156
+ if (typeof obj !== "object" || obj === null) return false;
157
+
158
+ const e = obj as Record<string, unknown>;
159
+ return (
160
+ typeof e.evidence_id === "string" &&
161
+ typeof e.evidence_type === "string" &&
162
+ typeof e.capability_id === "string" &&
163
+ typeof e.timestamp === "string" &&
164
+ typeof e.assurance_tier === "string" &&
165
+ typeof e.payload === "object"
166
+ );
167
+ }