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,414 +0,0 @@
1
- export const HOLON_EXECUTION_BINDING_KIND = "HolonExecutionBinding" as const
2
- export const HOLON_EXECUTION_BINDING_KIND_DEFINITION_FQN =
3
- "Eidolon.AI.KindDefinition.HolonExecutionBinding" as const
4
- export const HOLON_EXECUTION_BINDING_API_VERSION = "eidolon.ai/v1" as const
5
- export const HOLON_EXECUTION_BINDING_KIND_DEFINITION_VERSION = "1.0.0" as const
6
-
7
- export const HOLON_EXECUTION_BINDING_KIND_DEFINITION_SOURCE =
8
- `<KindDefinition #Eidolon.AI.KindDefinition.HolonExecutionBinding apiVersion="halfcode.resources/v1" version="1.0.0" {
9
- lifecycle = "Stable"
10
- resourceKind = "HolonExecutionBinding"
11
- sourceShapes = ["single-file"]
12
- currentApiVersion = "eidolon.ai/v1"
13
- supportedApiVersions = ["eidolon.ai/v1"]
14
- description = "Maps one frozen Holon Member or Role identity to an exact Eidolon execution adapter closure."
15
- }>
16
- ` as const
17
-
18
- export const HOLON_EXECUTION_BINDING_KIND_DEFINITION_BYTES: Readonly<Uint8Array> =
19
- new TextEncoder().encode(HOLON_EXECUTION_BINDING_KIND_DEFINITION_SOURCE)
20
-
21
- export const HOLON_MEMBER_EXECUTION_ADAPTER_KINDS = Object.freeze([
22
- "ai-agent",
23
- "human-endpoint",
24
- "service",
25
- "hybrid",
26
- ] as const)
27
-
28
- export type HolonExecutionResourceRef = `resource://${string}`
29
-
30
- export type HolonExecutionTarget =
31
- | Readonly<{ readonly kind: "member"; readonly memberRef: string }>
32
- | Readonly<{ readonly kind: "role"; readonly roleRef: string }>
33
-
34
- export type HolonMemberExecutionAdapter =
35
- | Readonly<{
36
- readonly kind: "ai-agent"
37
- readonly agentDefinitionRef: HolonExecutionResourceRef
38
- readonly runtimeProfileRef: HolonExecutionResourceRef
39
- }>
40
- | Readonly<{
41
- readonly kind: "human-endpoint"
42
- readonly humanEndpointRef: HolonExecutionResourceRef
43
- readonly inboxProfileRef: HolonExecutionResourceRef
44
- }>
45
- | Readonly<{
46
- readonly kind: "service"
47
- readonly serviceAdapterRef: HolonExecutionResourceRef
48
- readonly runtimeProfileRef: HolonExecutionResourceRef
49
- }>
50
- | Readonly<{
51
- readonly kind: "hybrid"
52
- readonly policyRef: HolonExecutionResourceRef
53
- readonly candidateBindingRefs: readonly [HolonExecutionResourceRef, ...HolonExecutionResourceRef[]]
54
- }>
55
-
56
- export type HolonExecutionRuntimeIntent =
57
- | Readonly<{ readonly mode: "shared-member-runtime" }>
58
- | Readonly<{ readonly mode: "isolated-task-runtime" }>
59
-
60
- export interface HolonExecutionPolicyV1 {
61
- readonly version: "1"
62
- readonly runtime: HolonExecutionRuntimeIntent
63
- readonly taskProfileRef: HolonExecutionResourceRef
64
- readonly capabilityRefs: readonly HolonExecutionResourceRef[]
65
- readonly toolRefs: readonly HolonExecutionResourceRef[]
66
- readonly materialRefs: readonly HolonExecutionResourceRef[]
67
- }
68
-
69
- export interface HolonExecutionBinding {
70
- readonly apiVersion: typeof HOLON_EXECUTION_BINDING_API_VERSION
71
- readonly kind: typeof HOLON_EXECUTION_BINDING_KIND
72
- readonly bindingRef: HolonExecutionResourceRef
73
- readonly snapshotRef: HolonExecutionResourceRef
74
- readonly target: HolonExecutionTarget
75
- readonly adapter: HolonMemberExecutionAdapter
76
- readonly policy: HolonExecutionPolicyV1
77
- }
78
-
79
- export interface HolonExecutionInvocation {
80
- readonly apiVersion: typeof HOLON_EXECUTION_BINDING_API_VERSION
81
- readonly kind: "HolonExecutionInvocation"
82
- readonly taskSpaceRef: string
83
- readonly taskRef: string
84
- readonly claimRef: string
85
- readonly invocationRef: string
86
- readonly targetBindingRef: HolonExecutionResourceRef
87
- readonly input: ClosedValue
88
- readonly materialRefs: readonly HolonExecutionResourceRef[]
89
- readonly resultContractRef: HolonExecutionResourceRef
90
- }
91
-
92
- interface ClosedArray extends ReadonlyArray<ClosedValue> {}
93
- interface ClosedRecord { readonly [key: string]: ClosedValue }
94
- export type ClosedValue = null | string | number | boolean | ClosedArray | ClosedRecord
95
-
96
- export class HolonExecutionBindingContractError extends Error {
97
- constructor(readonly path: string, message: string) {
98
- super(`${path}: ${message}`)
99
- this.name = "HolonExecutionBindingContractError"
100
- }
101
- }
102
-
103
- const invalid = (path: string, message: string): never => {
104
- throw new HolonExecutionBindingContractError(path, message)
105
- }
106
-
107
- const compareUtf16CodeUnits = (left: string, right: string): number =>
108
- left < right ? -1 : left > right ? 1 : 0
109
-
110
- function closedObject(value: unknown, path: string): Readonly<Record<string, unknown>> {
111
- if (value === null || typeof value !== "object" || Array.isArray(value)) {
112
- return invalid(path, "Expected a plain object")
113
- }
114
- const prototype = Object.getPrototypeOf(value)
115
- if (prototype !== Object.prototype && prototype !== null) {
116
- return invalid(path, "Expected a plain object")
117
- }
118
- const descriptors = Object.getOwnPropertyDescriptors(value)
119
- const keys = Reflect.ownKeys(descriptors)
120
- if (keys.some((key) => typeof key !== "string")) {
121
- return invalid(path, "Symbol properties are not allowed")
122
- }
123
- const result: Record<string, unknown> = Object.create(null)
124
- for (const key of (keys as string[]).sort(compareUtf16CodeUnits)) {
125
- const descriptor = descriptors[key]
126
- if (!descriptor || !("value" in descriptor) || descriptor.enumerable !== true) {
127
- return invalid(`${path}.${key}`, "Expected one enumerable own-data property")
128
- }
129
- result[key] = descriptor.value
130
- }
131
- return result
132
- }
133
-
134
- function exactKeys(
135
- value: Readonly<Record<string, unknown>>,
136
- expected: readonly string[],
137
- path: string,
138
- ): void {
139
- const expectedSet = new Set(expected)
140
- const extra = Object.keys(value).find((key) => !expectedSet.has(key))
141
- if (extra !== undefined) invalid(`${path}.${extra}`, "Unsupported field")
142
- const missing = expected.find((key) => !Object.prototype.hasOwnProperty.call(value, key))
143
- if (missing !== undefined) invalid(`${path}.${missing}`, "Required field is missing")
144
- }
145
-
146
- function exactString(value: unknown, path: string): string {
147
- if (typeof value !== "string" || value.length === 0 || value !== value.trim()) {
148
- return invalid(path, "Expected an exact non-empty string")
149
- }
150
- return value
151
- }
152
-
153
- function exactLiteral<T extends string>(value: unknown, expected: T, path: string): T {
154
- if (value !== expected) invalid(path, `Expected '${expected}'`)
155
- return expected
156
- }
157
-
158
- function resourceRef(value: unknown, path: string): HolonExecutionResourceRef {
159
- const exact = exactString(value, path)
160
- const prefix = "resource://"
161
- if (!exact.startsWith(prefix)) invalid(path, "Expected an exact resource:// reference")
162
- const identity = exact.slice(prefix.length)
163
- if (!identity || identity.includes("://")) invalid(path, "Expected one exact Resource identity")
164
- return exact as HolonExecutionResourceRef
165
- }
166
-
167
- function denseArray(value: unknown, path: string): readonly unknown[] {
168
- if (!Array.isArray(value) || Object.getPrototypeOf(value) !== Array.prototype) {
169
- return invalid(path, "Expected a plain dense array")
170
- }
171
- const descriptors = Object.getOwnPropertyDescriptors(value)
172
- for (let index = 0; index < value.length; index += 1) {
173
- const descriptor = descriptors[String(index)]
174
- if (!descriptor || !("value" in descriptor) || descriptor.enumerable !== true) {
175
- return invalid(`${path}[${index}]`, "Expected a dense enumerable own-data entry")
176
- }
177
- }
178
- for (const key of Reflect.ownKeys(descriptors)) {
179
- if (key === "length") continue
180
- if (typeof key !== "string" || !/^(0|[1-9][0-9]*)$/.test(key) || Number(key) >= value.length) {
181
- return invalid(path, "Symbol and extra array properties are not allowed")
182
- }
183
- }
184
- return value
185
- }
186
-
187
- function resourceRefs(
188
- value: unknown,
189
- path: string,
190
- options: { readonly nonEmpty?: boolean; readonly sort?: boolean } = {},
191
- ): readonly HolonExecutionResourceRef[] {
192
- const refs = denseArray(value, path).map((entry, index) => resourceRef(entry, `${path}[${index}]`))
193
- if (options.nonEmpty && refs.length === 0) invalid(path, "Expected at least one Resource reference")
194
- if (new Set(refs).size !== refs.length) invalid(path, "Resource reference list contains a duplicate")
195
- if (options.sort) refs.sort(compareUtf16CodeUnits)
196
- return Object.freeze(refs)
197
- }
198
-
199
- function target(value: unknown): HolonExecutionTarget {
200
- const record = closedObject(value, "$.target")
201
- const kind = exactString(record.kind, "$.target.kind")
202
- if (kind === "member") {
203
- exactKeys(record, ["kind", "memberRef"], "$.target")
204
- return Object.freeze({ kind, memberRef: exactString(record.memberRef, "$.target.memberRef") })
205
- }
206
- if (kind === "role") {
207
- exactKeys(record, ["kind", "roleRef"], "$.target")
208
- return Object.freeze({ kind, roleRef: exactString(record.roleRef, "$.target.roleRef") })
209
- }
210
- return invalid("$.target.kind", "Expected 'member' or 'role'")
211
- }
212
-
213
- function adapter(value: unknown): HolonMemberExecutionAdapter {
214
- const record = closedObject(value, "$.adapter")
215
- const kind = exactString(record.kind, "$.adapter.kind")
216
- if (kind === "ai-agent") {
217
- exactKeys(record, ["kind", "agentDefinitionRef", "runtimeProfileRef"], "$.adapter")
218
- return Object.freeze({
219
- kind,
220
- agentDefinitionRef: resourceRef(record.agentDefinitionRef, "$.adapter.agentDefinitionRef"),
221
- runtimeProfileRef: resourceRef(record.runtimeProfileRef, "$.adapter.runtimeProfileRef"),
222
- })
223
- }
224
- if (kind === "human-endpoint") {
225
- exactKeys(record, ["kind", "humanEndpointRef", "inboxProfileRef"], "$.adapter")
226
- return Object.freeze({
227
- kind,
228
- humanEndpointRef: resourceRef(record.humanEndpointRef, "$.adapter.humanEndpointRef"),
229
- inboxProfileRef: resourceRef(record.inboxProfileRef, "$.adapter.inboxProfileRef"),
230
- })
231
- }
232
- if (kind === "service") {
233
- exactKeys(record, ["kind", "serviceAdapterRef", "runtimeProfileRef"], "$.adapter")
234
- return Object.freeze({
235
- kind,
236
- serviceAdapterRef: resourceRef(record.serviceAdapterRef, "$.adapter.serviceAdapterRef"),
237
- runtimeProfileRef: resourceRef(record.runtimeProfileRef, "$.adapter.runtimeProfileRef"),
238
- })
239
- }
240
- if (kind === "hybrid") {
241
- exactKeys(record, ["kind", "policyRef", "candidateBindingRefs"], "$.adapter")
242
- const refs = resourceRefs(record.candidateBindingRefs, "$.adapter.candidateBindingRefs", { nonEmpty: true })
243
- return Object.freeze({
244
- kind,
245
- policyRef: resourceRef(record.policyRef, "$.adapter.policyRef"),
246
- candidateBindingRefs: refs as readonly [HolonExecutionResourceRef, ...HolonExecutionResourceRef[]],
247
- })
248
- }
249
- return invalid("$.adapter.kind", "Expected one closed execution adapter kind")
250
- }
251
-
252
- function policy(value: unknown): HolonExecutionPolicyV1 {
253
- const record = closedObject(value, "$.policy")
254
- exactKeys(record, [
255
- "version",
256
- "runtime",
257
- "taskProfileRef",
258
- "capabilityRefs",
259
- "toolRefs",
260
- "materialRefs",
261
- ], "$.policy")
262
- exactLiteral(record.version, "1", "$.policy.version")
263
- const runtime = closedObject(record.runtime, "$.policy.runtime")
264
- exactKeys(runtime, ["mode"], "$.policy.runtime")
265
- const mode = exactString(runtime.mode, "$.policy.runtime.mode")
266
- if (mode !== "shared-member-runtime" && mode !== "isolated-task-runtime") {
267
- invalid("$.policy.runtime.mode", "Expected one closed runtime intent")
268
- }
269
- const normalizedMode = mode as HolonExecutionRuntimeIntent["mode"]
270
- return Object.freeze({
271
- version: "1",
272
- runtime: Object.freeze({ mode: normalizedMode }),
273
- taskProfileRef: resourceRef(record.taskProfileRef, "$.policy.taskProfileRef"),
274
- capabilityRefs: resourceRefs(record.capabilityRefs, "$.policy.capabilityRefs", { sort: true }),
275
- toolRefs: resourceRefs(record.toolRefs, "$.policy.toolRefs", { sort: true }),
276
- materialRefs: resourceRefs(record.materialRefs, "$.policy.materialRefs", { sort: true }),
277
- })
278
- }
279
-
280
- export function normalizeHolonExecutionBinding(value: unknown): HolonExecutionBinding {
281
- const record = closedObject(value, "$")
282
- exactKeys(record, [
283
- "apiVersion",
284
- "kind",
285
- "bindingRef",
286
- "snapshotRef",
287
- "target",
288
- "adapter",
289
- "policy",
290
- ], "$")
291
- return Object.freeze({
292
- apiVersion: exactLiteral(record.apiVersion, HOLON_EXECUTION_BINDING_API_VERSION, "$.apiVersion"),
293
- kind: exactLiteral(record.kind, HOLON_EXECUTION_BINDING_KIND, "$.kind"),
294
- bindingRef: resourceRef(record.bindingRef, "$.bindingRef"),
295
- snapshotRef: resourceRef(record.snapshotRef, "$.snapshotRef"),
296
- target: target(record.target),
297
- adapter: adapter(record.adapter),
298
- policy: policy(record.policy),
299
- })
300
- }
301
-
302
- function sameBytes(left: Uint8Array, right: Uint8Array): boolean {
303
- if (left.byteLength !== right.byteLength) return false
304
- for (let index = 0; index < left.byteLength; index += 1) {
305
- if (left[index] !== right[index]) return false
306
- }
307
- return true
308
- }
309
-
310
- export function canonicalHolonExecutionBindingBytes(value: unknown): Uint8Array {
311
- return new TextEncoder().encode(JSON.stringify(normalizeHolonExecutionBinding(value)))
312
- }
313
-
314
- export function parseHolonExecutionBindingBytes(bytes: Uint8Array): HolonExecutionBinding {
315
- let decoded: string
316
- try {
317
- decoded = new TextDecoder("utf-8", { fatal: true }).decode(bytes)
318
- } catch {
319
- return invalid("$bytes", "Binding bytes are not valid UTF-8")
320
- }
321
- let value: unknown
322
- try {
323
- value = JSON.parse(decoded)
324
- } catch {
325
- return invalid("$bytes", "Binding bytes are not valid JSON")
326
- }
327
- const binding = normalizeHolonExecutionBinding(value)
328
- if (!sameBytes(bytes, canonicalHolonExecutionBindingBytes(binding))) {
329
- return invalid("$bytes", "Binding bytes are not canonical")
330
- }
331
- return binding
332
- }
333
-
334
- export function validateHolonExecutionBindingGraph(
335
- values: readonly HolonExecutionBinding[],
336
- ): readonly HolonExecutionBinding[] {
337
- const bindings = values.map(normalizeHolonExecutionBinding)
338
- const byRef = new Map<HolonExecutionResourceRef, HolonExecutionBinding>()
339
- for (const binding of bindings) {
340
- if (byRef.has(binding.bindingRef)) {
341
- invalid("$.bindings", `Duplicate binding identity '${binding.bindingRef}'`)
342
- }
343
- byRef.set(binding.bindingRef, binding)
344
- }
345
- const visiting = new Set<HolonExecutionResourceRef>()
346
- const visited = new Set<HolonExecutionResourceRef>()
347
- const visit = (bindingRef: HolonExecutionResourceRef): void => {
348
- if (visited.has(bindingRef)) return
349
- if (visiting.has(bindingRef)) invalid("$.bindings", `Hybrid binding cycle includes '${bindingRef}'`)
350
- const binding = byRef.get(bindingRef)
351
- ?? invalid("$.bindings", `Hybrid candidate '${bindingRef}' is unresolved`)
352
- visiting.add(bindingRef)
353
- if (binding.adapter.kind === "hybrid") {
354
- for (const candidateRef of binding.adapter.candidateBindingRefs) visit(candidateRef)
355
- }
356
- visiting.delete(bindingRef)
357
- visited.add(bindingRef)
358
- }
359
- for (const binding of bindings) visit(binding.bindingRef)
360
- return Object.freeze(bindings)
361
- }
362
-
363
- function cloneClosedValue(value: unknown, path: string, active = new Set<object>()): ClosedValue {
364
- if (value === null || typeof value === "string" || typeof value === "boolean") return value
365
- if (typeof value === "number") {
366
- if (!Number.isFinite(value)) invalid(path, "Expected a finite number")
367
- return value
368
- }
369
- if (typeof value !== "object") return invalid(path, "Expected JSON-compatible plain data")
370
- if (active.has(value)) return invalid(path, "Cycles are not allowed")
371
- active.add(value)
372
- try {
373
- if (Array.isArray(value)) {
374
- return Object.freeze(denseArray(value, path).map((entry, index) =>
375
- cloneClosedValue(entry, `${path}[${index}]`, active)))
376
- }
377
- const record = closedObject(value, path)
378
- const output: Record<string, ClosedValue> = Object.create(null)
379
- for (const key of Object.keys(record).sort(compareUtf16CodeUnits)) {
380
- output[key] = cloneClosedValue(record[key], `${path}.${key}`, active)
381
- }
382
- return Object.freeze(output)
383
- } finally {
384
- active.delete(value)
385
- }
386
- }
387
-
388
- export function normalizeHolonExecutionInvocation(value: unknown): HolonExecutionInvocation {
389
- const record = closedObject(value, "$")
390
- exactKeys(record, [
391
- "apiVersion",
392
- "kind",
393
- "taskSpaceRef",
394
- "taskRef",
395
- "claimRef",
396
- "invocationRef",
397
- "targetBindingRef",
398
- "input",
399
- "materialRefs",
400
- "resultContractRef",
401
- ], "$")
402
- return Object.freeze({
403
- apiVersion: exactLiteral(record.apiVersion, HOLON_EXECUTION_BINDING_API_VERSION, "$.apiVersion"),
404
- kind: exactLiteral(record.kind, "HolonExecutionInvocation", "$.kind"),
405
- taskSpaceRef: exactString(record.taskSpaceRef, "$.taskSpaceRef"),
406
- taskRef: exactString(record.taskRef, "$.taskRef"),
407
- claimRef: exactString(record.claimRef, "$.claimRef"),
408
- invocationRef: exactString(record.invocationRef, "$.invocationRef"),
409
- targetBindingRef: resourceRef(record.targetBindingRef, "$.targetBindingRef"),
410
- input: cloneClosedValue(record.input, "$.input"),
411
- materialRefs: resourceRefs(record.materialRefs, "$.materialRefs", { sort: true }),
412
- resultContractRef: resourceRef(record.resultContractRef, "$.resultContractRef"),
413
- })
414
- }