holarchy-eidolon-adapter 0.1.1 → 0.2.1

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.
@@ -1,450 +0,0 @@
1
- import {
2
- HOLON_EXECUTION_BINDING_KIND,
3
- HOLON_EXECUTION_BINDING_KIND_DEFINITION_BYTES,
4
- HOLON_EXECUTION_BINDING_KIND_DEFINITION_FQN,
5
- parseHolonExecutionBindingBytes,
6
- validateHolonExecutionBindingGraph,
7
- type HolonExecutionBinding,
8
- type HolonExecutionResourceRef,
9
- } from "./HolonExecutionBinding"
10
- import {
11
- HOLON_EFFECTIVE_SNAPSHOT_KIND,
12
- HOLON_EFFECTIVE_SNAPSHOT_KIND_DEFINITION_BYTES,
13
- HOLON_EFFECTIVE_SNAPSHOT_KIND_DEFINITION_FQN,
14
- parseHolonEffectiveSnapshotBytes,
15
- parseHolonEffectiveSnapshotIssuanceReceiptBytes,
16
- type HolonEffectiveSnapshot,
17
- type HolonEffectiveSnapshotIssuanceReceipt,
18
- } from "holarchy-core-contract"
19
- import {
20
- buildResourceDependencySnapshot,
21
- sha256Digest,
22
- type EffectiveResourceRegistry,
23
- type ResourceContentIdentity,
24
- type ResourceRecord,
25
- } from "halfcode-compiler.xnl/resource-core"
26
- import type { AIWorkflowAgentResourceProjection } from "ai-workflow-contract"
27
-
28
- import { HolarchyEidolonAdapterError as EidolonResourceRegistryError } from "./HolarchyEidolonAdapterError"
29
-
30
- export interface EidolonHolonExecutionBindingProjection {
31
- readonly schemaVersion: "eidolon.holon-execution-binding-projection/v1"
32
- readonly resource: ResourceRecord
33
- readonly binding: HolonExecutionBinding
34
- readonly snapshotResource: ResourceRecord
35
- readonly snapshot: HolonEffectiveSnapshot
36
- readonly receipt: HolonEffectiveSnapshotIssuanceReceipt
37
- readonly bindingBytesDigest: `sha256:${string}`
38
- readonly receiptBytesDigest: `sha256:${string}`
39
- readonly closureResourceIds: readonly string[]
40
- readonly registryRevision: string
41
- }
42
-
43
- export interface EidolonHolonExecutionAgentProof {
44
- readonly agentDefinitionRef: HolonExecutionResourceRef
45
- readonly agentContentDigest: `sha256:${string}`
46
- readonly closureResourceIds: readonly string[]
47
- readonly snapshotRevision: `sha256:${string}`
48
- }
49
-
50
- export interface EidolonHolonExecutionBindingFreezeReceipt {
51
- readonly schemaVersion: "eidolon.holon-execution-binding-freeze/v1"
52
- readonly bindingRef: HolonExecutionResourceRef
53
- readonly snapshotRef: HolonExecutionResourceRef
54
- readonly snapshotTreeDigest: `sha256:${string}`
55
- readonly snapshotReceiptDigest: `sha256:${string}`
56
- readonly bindingBytesDigest: `sha256:${string}`
57
- readonly registryRevision: string
58
- readonly closure: readonly Readonly<{
59
- readonly resourceId: string
60
- readonly kind: string
61
- readonly authorityDigest: `sha256:${string}`
62
- readonly contentDigest: `sha256:${string}`
63
- }>[]
64
- readonly agentProofs: readonly EidolonHolonExecutionAgentProof[]
65
- readonly semanticFingerprint: `sha256:${string}`
66
- }
67
-
68
- export interface HolonExecutionBindingProjectionInput {
69
- readonly registry: EffectiveResourceRegistry
70
- readonly contentIdentities: ReadonlyMap<string, ResourceContentIdentity>
71
- readonly kindDefinitionAuthorityDigests: ReadonlyMap<string, `sha256:${string}`>
72
- readonly registryRevision: string
73
- }
74
-
75
- export interface HolonExecutionBindingFreezeInput {
76
- readonly registry: EffectiveResourceRegistry
77
- readonly contentIdentities: ReadonlyMap<string, ResourceContentIdentity>
78
- readonly registryRevision: string
79
- readonly projection: EidolonHolonExecutionBindingProjection
80
- readonly agentResources: AIWorkflowAgentResourceProjection
81
- }
82
-
83
- const authenticProjections = new WeakSet<object>()
84
- const authenticFreezeReceipts = new WeakSet<object>()
85
-
86
- export async function projectHolonExecutionBindings(
87
- input: HolonExecutionBindingProjectionInput,
88
- ): Promise<readonly EidolonHolonExecutionBindingProjection[]> {
89
- const resources = [...(input.registry.byKind.get(HOLON_EXECUTION_BINDING_KIND) ?? [])]
90
- .sort((left, right) => compareUtf16(left.resourceId, right.resourceId))
91
- if (resources.length === 0) return Object.freeze([])
92
- assertCanonicalKindDefinition(input, {
93
- resourceKind: HOLON_EXECUTION_BINDING_KIND,
94
- fqn: HOLON_EXECUTION_BINDING_KIND_DEFINITION_FQN,
95
- bytes: HOLON_EXECUTION_BINDING_KIND_DEFINITION_BYTES,
96
- code: "EIDOLON_HOLON_BINDING_KIND_DEFINITION_MISMATCH",
97
- })
98
- assertCanonicalKindDefinition(input, {
99
- resourceKind: HOLON_EFFECTIVE_SNAPSHOT_KIND,
100
- fqn: HOLON_EFFECTIVE_SNAPSHOT_KIND_DEFINITION_FQN,
101
- bytes: HOLON_EFFECTIVE_SNAPSHOT_KIND_DEFINITION_BYTES,
102
- code: "EIDOLON_HOLON_SNAPSHOT_KIND_DEFINITION_MISMATCH",
103
- })
104
-
105
- const decoded = resources.map((resource) => {
106
- const bindingBytes = canonicalBase64Property(resource, "bindingBytesBase64")
107
- const binding = parseHolonExecutionBindingBytes(bindingBytes)
108
- if (binding.bindingRef !== resourceRef(resource.resourceId)) {
109
- throw new EidolonResourceRegistryError(
110
- "EIDOLON_HOLON_BINDING_IDENTITY_MISMATCH",
111
- `HolonExecutionBinding '${resource.resourceId}' bytes select '${binding.bindingRef}'.`,
112
- )
113
- }
114
- return Object.freeze({ resource, binding, bindingBytes })
115
- })
116
- const bindingGraph = validateHolonExecutionBindingGraph(decoded.map(({ binding }) => binding))
117
- const decodedByRef = new Map(decoded.map((entry) => [entry.binding.bindingRef, entry]))
118
-
119
- const projections: EidolonHolonExecutionBindingProjection[] = []
120
- for (const binding of bindingGraph) {
121
- const decodedBinding = decodedByRef.get(binding.bindingRef)
122
- if (!decodedBinding) throw new EidolonResourceRegistryError(
123
- "EIDOLON_HOLON_BINDING_GRAPH_INTERNAL",
124
- `HolonExecutionBinding '${binding.bindingRef}' disappeared during projection.`,
125
- )
126
- const snapshotId = resourceId(binding.snapshotRef)
127
- const snapshotResource = exactResource(input.registry, snapshotId)
128
- if (snapshotResource.kind !== HOLON_EFFECTIVE_SNAPSHOT_KIND) {
129
- throw new EidolonResourceRegistryError(
130
- "EIDOLON_HOLON_SNAPSHOT_KIND_MISMATCH",
131
- `Resource '${snapshotId}' must be a canonical ${HOLON_EFFECTIVE_SNAPSHOT_KIND} Resource.`,
132
- )
133
- }
134
- const snapshotBytes = canonicalBase64Property(snapshotResource, "snapshotBytesBase64")
135
- const snapshot = await parseHolonEffectiveSnapshotBytes(snapshotBytes)
136
- const receiptBytes = canonicalBase64Property(snapshotResource, "issuanceReceiptBytesBase64")
137
- const receipt = parseHolonEffectiveSnapshotIssuanceReceiptBytes(receiptBytes, snapshot)
138
- assertTargetExists(binding, snapshot)
139
-
140
- const closureResourceIds = resolveBindingClosureResourceIds(binding, decodedByRef, input.registry)
141
- for (const resourceId of closureResourceIds) requiredIdentity(input.contentIdentities, resourceId)
142
- requiredIdentity(input.contentIdentities, snapshotResource.resourceId)
143
-
144
- const projection = Object.freeze({
145
- schemaVersion: "eidolon.holon-execution-binding-projection/v1",
146
- resource: decodedBinding.resource,
147
- binding,
148
- snapshotResource,
149
- snapshot,
150
- receipt,
151
- bindingBytesDigest: sha256Digest(decodedBinding.bindingBytes),
152
- receiptBytesDigest: sha256Digest(receiptBytes),
153
- closureResourceIds,
154
- registryRevision: input.registryRevision,
155
- })
156
- authenticProjections.add(projection)
157
- projections.push(projection)
158
- }
159
- return Object.freeze(projections)
160
- }
161
-
162
- export function freezeHolonExecutionBinding(
163
- input: HolonExecutionBindingFreezeInput,
164
- ): EidolonHolonExecutionBindingFreezeReceipt {
165
- if (!authenticProjections.has(input.projection as object)) {
166
- throw new EidolonResourceRegistryError(
167
- "EIDOLON_HOLON_BINDING_PROJECTION_UNTRUSTED",
168
- "Binding freeze requires the exact projection produced by the shared registry.",
169
- )
170
- }
171
- const closure = Object.freeze(input.projection.closureResourceIds.map((resourceId) => {
172
- const resource = exactResource(input.registry, resourceId)
173
- const identity = requiredIdentity(input.contentIdentities, resourceId)
174
- return Object.freeze({
175
- resourceId,
176
- kind: resource.kind,
177
- authorityDigest: exactSha256(identity.authorityDigest, `${resourceId}.authorityDigest`),
178
- contentDigest: exactSha256(identity.contentDigest, `${resourceId}.contentDigest`),
179
- })
180
- }))
181
- const agentProofs = Object.freeze(agentRefs(input.projection.binding, new Map(
182
- (input.registry.byKind.get(HOLON_EXECUTION_BINDING_KIND) ?? []).map((resource) => {
183
- const projected = resourceRef(resource.resourceId)
184
- return [projected, projected === input.projection.binding.bindingRef
185
- ? input.projection.binding
186
- : parseHolonExecutionBindingBytes(canonicalBase64Property(resource, "bindingBytesBase64"))]
187
- }),
188
- )).map((agentDefinitionRef) => {
189
- const agentId = resourceId(agentDefinitionRef)
190
- const agent = input.agentResources.agentDefinitions.find(
191
- (candidate) => candidate.resource.resourceId === agentId,
192
- )
193
- if (!agent) throw new EidolonResourceRegistryError(
194
- "EIDOLON_HOLON_BINDING_AGENT_PROJECTION_MISSING",
195
- `AIAgentDefinition '${agentDefinitionRef}' has no authentic shared-registry projection.`,
196
- )
197
- const roots = agentClosureResourceIds(agent)
198
- const dependencySnapshot = buildResourceDependencySnapshot({
199
- registry: input.registry,
200
- roots,
201
- edges: input.agentResources.dependencyEdges,
202
- contentIdentities: input.contentIdentities,
203
- })
204
- return Object.freeze({
205
- agentDefinitionRef,
206
- agentContentDigest: requiredIdentity(input.contentIdentities, agentId).contentDigest,
207
- closureResourceIds: Object.freeze(dependencySnapshot.closure.map(({ resourceId }) => resourceId)),
208
- snapshotRevision: dependencySnapshot.snapshotRevision,
209
- })
210
- }))
211
- const semanticFingerprint = sha256Digest(JSON.stringify({
212
- schemaVersion: "eidolon.holon-execution-binding-freeze-semantic/v1",
213
- bindingRef: input.projection.binding.bindingRef,
214
- snapshotRef: input.projection.binding.snapshotRef,
215
- snapshotTreeDigest: input.projection.snapshot.treeDigest,
216
- snapshotReceiptDigest: input.projection.receiptBytesDigest,
217
- bindingBytesDigest: input.projection.bindingBytesDigest,
218
- registryRevision: input.registryRevision,
219
- closure,
220
- agentProofs,
221
- }))
222
- const receipt = Object.freeze({
223
- schemaVersion: "eidolon.holon-execution-binding-freeze/v1" as const,
224
- bindingRef: input.projection.binding.bindingRef,
225
- snapshotRef: input.projection.binding.snapshotRef,
226
- snapshotTreeDigest: input.projection.snapshot.treeDigest,
227
- snapshotReceiptDigest: input.projection.receiptBytesDigest,
228
- bindingBytesDigest: input.projection.bindingBytesDigest,
229
- registryRevision: input.registryRevision,
230
- closure,
231
- agentProofs,
232
- semanticFingerprint,
233
- })
234
- authenticFreezeReceipts.add(receipt)
235
- return receipt
236
- }
237
-
238
- export function assertHolonExecutionBindingFreezeReceipt(
239
- value: EidolonHolonExecutionBindingFreezeReceipt,
240
- ): EidolonHolonExecutionBindingFreezeReceipt {
241
- if (!authenticFreezeReceipts.has(value as object)) {
242
- throw new EidolonResourceRegistryError(
243
- "EIDOLON_HOLON_BINDING_FREEZE_RECEIPT_UNTRUSTED",
244
- "Holon binding dispatch requires the authentic receipt produced by the shared registry freeze boundary.",
245
- )
246
- }
247
- return value
248
- }
249
-
250
- function agentRefs(
251
- root: HolonExecutionBinding,
252
- byRef: ReadonlyMap<HolonExecutionResourceRef, HolonExecutionBinding>,
253
- ): readonly HolonExecutionResourceRef[] {
254
- const refs = new Set<HolonExecutionResourceRef>()
255
- const visited = new Set<HolonExecutionResourceRef>()
256
- const visit = (binding: HolonExecutionBinding): void => {
257
- if (visited.has(binding.bindingRef)) return
258
- visited.add(binding.bindingRef)
259
- if (binding.adapter.kind === "ai-agent") refs.add(binding.adapter.agentDefinitionRef)
260
- if (binding.adapter.kind === "hybrid") {
261
- for (const candidateRef of binding.adapter.candidateBindingRefs) {
262
- const candidate = byRef.get(candidateRef)
263
- if (!candidate) throw new EidolonResourceRegistryError(
264
- "EIDOLON_HOLON_BINDING_CANDIDATE_MISSING",
265
- `Hybrid candidate '${candidateRef}' disappeared before freeze.`,
266
- )
267
- visit(candidate)
268
- }
269
- }
270
- }
271
- visit(root)
272
- return Object.freeze([...refs].sort(compareUtf16))
273
- }
274
-
275
- function agentClosureResourceIds(
276
- agent: AIWorkflowAgentResourceProjection["agentDefinitions"][number],
277
- ): readonly string[] {
278
- const ids = new Set<string>([agent.resource.resourceId])
279
- for (const message of agent.messages) {
280
- ids.add(message.prompt.resource.resourceId)
281
- if (message.schema) ids.add(message.schema.resource.resourceId)
282
- }
283
- if (agent.inputSchema) ids.add(agent.inputSchema.resource.resourceId)
284
- if (agent.outputSchema) ids.add(agent.outputSchema.resource.resourceId)
285
- if (agent.effectPolicy) ids.add(agent.effectPolicy.resource.resourceId)
286
- for (const tool of agent.tools) ids.add(tool.resource.resourceId)
287
- for (const port of agent.materialPorts) ids.add(port.resource.resourceId)
288
- return Object.freeze([...ids].sort(compareUtf16))
289
- }
290
-
291
- function assertCanonicalKindDefinition(
292
- input: HolonExecutionBindingProjectionInput,
293
- expected: {
294
- readonly resourceKind: string
295
- readonly fqn: string
296
- readonly bytes: Readonly<Uint8Array>
297
- readonly code: string
298
- },
299
- ): void {
300
- const kindDefinition = input.registry.kindDefinitions.get(expected.resourceKind)?.definition
301
- if (!kindDefinition || kindDefinition.resourceId !== expected.fqn) {
302
- throw new EidolonResourceRegistryError(
303
- expected.code,
304
- `Kind '${expected.resourceKind}' must be registered by canonical KindDefinition '${expected.fqn}'.`,
305
- )
306
- }
307
- const expectedDigest = sha256Digest(Uint8Array.from(expected.bytes))
308
- if (input.kindDefinitionAuthorityDigests.get(expected.resourceKind) !== expectedDigest) {
309
- throw new EidolonResourceRegistryError(
310
- expected.code,
311
- `KindDefinition '${expected.fqn}' bytes do not match the canonical owner bytes.`,
312
- )
313
- }
314
- }
315
-
316
- function canonicalBase64Property(resource: ResourceRecord, property: string): Uint8Array {
317
- const value = resource.node.properties[property]
318
- if (typeof value !== "string" || value.length === 0 || value !== value.trim()) {
319
- throw new EidolonResourceRegistryError(
320
- "EIDOLON_HOLON_RESOURCE_BYTES_MISSING",
321
- `Resource '${resource.resourceId}' requires exact '${property}' base64 bytes.`,
322
- )
323
- }
324
- let bytes: Uint8Array
325
- try {
326
- bytes = Uint8Array.from(Buffer.from(value, "base64"))
327
- } catch {
328
- throw new EidolonResourceRegistryError(
329
- "EIDOLON_HOLON_RESOURCE_BYTES_INVALID",
330
- `Resource '${resource.resourceId}' '${property}' is not valid base64.`,
331
- )
332
- }
333
- if (bytes.byteLength === 0 || Buffer.from(bytes).toString("base64") !== value) {
334
- throw new EidolonResourceRegistryError(
335
- "EIDOLON_HOLON_RESOURCE_BYTES_INVALID",
336
- `Resource '${resource.resourceId}' '${property}' is not canonical base64.`,
337
- )
338
- }
339
- return bytes
340
- }
341
-
342
- function assertTargetExists(binding: HolonExecutionBinding, snapshot: HolonEffectiveSnapshot): void {
343
- const expectedKind = binding.target.kind === "member" ? "Member" : "Role"
344
- const targetRef = binding.target.kind === "member" ? binding.target.memberRef : binding.target.roleRef
345
- if (!snapshot.records.some((record) => record.kind === expectedKind && record.id === targetRef)) {
346
- throw new EidolonResourceRegistryError(
347
- "EIDOLON_HOLON_BINDING_TARGET_MISSING",
348
- `${expectedKind} '${targetRef}' is not present in frozen snapshot '${snapshot.snapshotId}'.`,
349
- )
350
- }
351
- }
352
-
353
- function resolveBindingClosureResourceIds(
354
- root: HolonExecutionBinding,
355
- bindings: ReadonlyMap<HolonExecutionResourceRef, Readonly<{
356
- readonly resource: ResourceRecord
357
- readonly binding: HolonExecutionBinding
358
- readonly bindingBytes: Uint8Array
359
- }>>,
360
- registry: EffectiveResourceRegistry,
361
- ): readonly string[] {
362
- const selected = new Set<string>()
363
- const visited = new Set<HolonExecutionResourceRef>()
364
- const add = (ref: HolonExecutionResourceRef, expectedKind?: string): void => {
365
- const id = resourceId(ref)
366
- const resource = exactResource(registry, id)
367
- if (expectedKind && resource.kind !== expectedKind) {
368
- throw new EidolonResourceRegistryError(
369
- "EIDOLON_HOLON_BINDING_RESOURCE_KIND_MISMATCH",
370
- `Resource '${id}' must have kind '${expectedKind}', got '${resource.kind}'.`,
371
- )
372
- }
373
- selected.add(id)
374
- }
375
- const visit = (binding: HolonExecutionBinding): void => {
376
- if (visited.has(binding.bindingRef)) return
377
- visited.add(binding.bindingRef)
378
- add(binding.bindingRef, HOLON_EXECUTION_BINDING_KIND)
379
- add(binding.snapshotRef, HOLON_EFFECTIVE_SNAPSHOT_KIND)
380
- add(binding.policy.taskProfileRef)
381
- for (const ref of binding.policy.capabilityRefs) add(ref)
382
- for (const ref of binding.policy.toolRefs) add(ref)
383
- for (const ref of binding.policy.materialRefs) add(ref)
384
- if (binding.adapter.kind === "ai-agent") {
385
- add(binding.adapter.agentDefinitionRef, "AIAgentDefinition")
386
- add(binding.adapter.runtimeProfileRef)
387
- } else if (binding.adapter.kind === "human-endpoint") {
388
- add(binding.adapter.humanEndpointRef)
389
- add(binding.adapter.inboxProfileRef)
390
- } else if (binding.adapter.kind === "service") {
391
- add(binding.adapter.serviceAdapterRef)
392
- add(binding.adapter.runtimeProfileRef)
393
- } else {
394
- add(binding.adapter.policyRef)
395
- for (const candidateRef of binding.adapter.candidateBindingRefs) {
396
- const candidate = bindings.get(candidateRef)
397
- if (!candidate) throw new EidolonResourceRegistryError(
398
- "EIDOLON_HOLON_BINDING_CANDIDATE_MISSING",
399
- `Hybrid candidate '${candidateRef}' is not present in the admitted registry.`,
400
- )
401
- visit(candidate.binding)
402
- }
403
- }
404
- }
405
- visit(root)
406
- return Object.freeze([...selected].sort(compareUtf16))
407
- }
408
-
409
- function exactResource(registry: EffectiveResourceRegistry, resourceId: string): ResourceRecord {
410
- const resource = registry.byId.get(resourceId)?.resource
411
- if (!resource) {
412
- throw new EidolonResourceRegistryError(
413
- "EIDOLON_HOLON_BINDING_RESOURCE_MISSING",
414
- `Resource '${resourceId}' is not present in the admitted registry.`,
415
- )
416
- }
417
- return resource
418
- }
419
-
420
- function requiredIdentity(
421
- identities: ReadonlyMap<string, ResourceContentIdentity>,
422
- resourceId: string,
423
- ): ResourceContentIdentity {
424
- const identity = identities.get(resourceId)
425
- if (!identity) throw new EidolonResourceRegistryError(
426
- "EIDOLON_HOLON_BINDING_CONTENT_IDENTITY_MISSING",
427
- `Resource '${resourceId}' has no exact content identity.`,
428
- )
429
- return identity
430
- }
431
-
432
- function resourceId(ref: HolonExecutionResourceRef): string {
433
- return ref.slice("resource://".length)
434
- }
435
-
436
- function resourceRef(resourceId: string): HolonExecutionResourceRef {
437
- return `resource://${resourceId}`
438
- }
439
-
440
- function compareUtf16(left: string, right: string): number {
441
- return left < right ? -1 : left > right ? 1 : 0
442
- }
443
-
444
- function exactSha256(value: string, path: string): `sha256:${string}` {
445
- if (!/^sha256:[0-9a-f]{64}$/.test(value)) throw new EidolonResourceRegistryError(
446
- "EIDOLON_HOLON_BINDING_DIGEST_INVALID",
447
- `Expected one canonical SHA-256 digest at '${path}'.`,
448
- )
449
- return value as `sha256:${string}`
450
- }
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from "./HolarchyEidolonAdapterError"
2
- export * from "./HolonDeploymentDefinition"
3
- export * from "./HolonDeploymentRuntime"
4
- export * from "./HolonExecutionBinding"
5
- export * from "./HolonExecutionBindingProjection"