@arcforge/kernel 2.0.146 → 2.0.148
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 +8 -8
- package/src/capsule.ts +72 -5
- package/src/engine.ts +59 -23
- package/src/kernel.ts +185 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcforge/kernel",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.148",
|
|
4
4
|
"description": "The Axon kernel — ring 0. Owns resources and execution, enforces policy, and is the only constructor of the unprivileged capsule.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -15,13 +15,13 @@
|
|
|
15
15
|
"deploy": "echo \"This package is published ONLY by apps/tui/scripts/release.ts, which pins workspace:* deps to concrete versions first. Publishing it directly ships an unresolvable dependency.\" && exit 1"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@arcforge/air": "2.0.
|
|
19
|
-
"@arcforge/engines": "2.0.
|
|
20
|
-
"@arcforge/err": "2.0.
|
|
21
|
-
"@arcforge/session": "2.0.
|
|
22
|
-
"@arcforge/types": "2.0.
|
|
23
|
-
"@arclabs/cloud": "2.0.
|
|
24
|
-
"@axon/capsule": "2.0.
|
|
18
|
+
"@arcforge/air": "2.0.148",
|
|
19
|
+
"@arcforge/engines": "2.0.148",
|
|
20
|
+
"@arcforge/err": "2.0.148",
|
|
21
|
+
"@arcforge/session": "2.0.148",
|
|
22
|
+
"@arcforge/types": "2.0.148",
|
|
23
|
+
"@arclabs/cloud": "2.0.148",
|
|
24
|
+
"@axon/capsule": "2.0.148"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/bun": "latest",
|
package/src/capsule.ts
CHANGED
|
@@ -64,7 +64,16 @@ function defaultPolicy(blueprint: AxonBlueprint): CapsulePartialConfig["policy"]
|
|
|
64
64
|
// test harness. A ceiling applied only on the TUI's spawn path would be
|
|
65
65
|
// advisory, and the CLI is exactly where someone scripting would bypass it
|
|
66
66
|
// without meaning to.
|
|
67
|
-
|
|
67
|
+
// Both layers normalised to the KEYED shape before they meet.
|
|
68
|
+
//
|
|
69
|
+
// A bare `tools: "escalate"` is an authoring convenience; every rule below
|
|
70
|
+
// — the pairing, the wildcard lookup, the mediator — reads one shape. Doing
|
|
71
|
+
// it here rather than in each of them is the same discipline Blueprint()
|
|
72
|
+
// applies at the capsule seam.
|
|
73
|
+
const userPolicy = intersect(
|
|
74
|
+
blueprint.profilePolicy ? keyed(blueprint.profilePolicy) : undefined,
|
|
75
|
+
keyed(blueprint.config.policy ?? {}),
|
|
76
|
+
)
|
|
68
77
|
|
|
69
78
|
// The invariant: not setting a security policy means "I don't care" — the
|
|
70
79
|
// agent gets full access to the machine, no hassle, no dependencies. Setting
|
|
@@ -122,9 +131,9 @@ function defaultPolicy(blueprint: AxonBlueprint): CapsulePartialConfig["policy"]
|
|
|
122
131
|
* is not a ceiling.
|
|
123
132
|
*/
|
|
124
133
|
function intersect(
|
|
125
|
-
profile:
|
|
126
|
-
agent:
|
|
127
|
-
):
|
|
134
|
+
profile: NormalizedPolicy | undefined,
|
|
135
|
+
agent: NormalizedPolicy,
|
|
136
|
+
): NormalizedPolicy {
|
|
128
137
|
if (!profile) return agent
|
|
129
138
|
|
|
130
139
|
return {
|
|
@@ -176,10 +185,68 @@ function pairRules(
|
|
|
176
185
|
): Record<string, PolicyRule> {
|
|
177
186
|
const keys = new Set([...Object.keys(profile ?? {}), ...Object.keys(agent ?? {})])
|
|
178
187
|
const out: Record<string, PolicyRule> = {}
|
|
179
|
-
|
|
188
|
+
|
|
189
|
+
// A profile WILDCARD applies to every key, not only to `*`.
|
|
190
|
+
//
|
|
191
|
+
// Without this the ceiling leaks in exactly the case the wildcard exists
|
|
192
|
+
// for: a profile saying `tools: "escalate"` normalises to `{ "*":
|
|
193
|
+
// "escalate" }`, an agent's `tools: { fs: true }` keeps its own key, and
|
|
194
|
+
// the mediator's exact-match then hands back `true` — the profile's
|
|
195
|
+
// blanket never consulted. A ceiling a named grant can punch through is
|
|
196
|
+
// not a ceiling.
|
|
197
|
+
//
|
|
198
|
+
// Paired per key rather than replacing the agent's rule, so the profile's
|
|
199
|
+
// blanket and the agent's specific are BOTH carried to the mediator and
|
|
200
|
+
// the verdict can still name which layer decided.
|
|
201
|
+
const blanket = profile?.[POLICY_WILDCARD]
|
|
202
|
+
|
|
203
|
+
for (const key of keys) out[key] = pairRule(profile?.[key] ?? blanket, agent?.[key])
|
|
180
204
|
return out
|
|
181
205
|
}
|
|
182
206
|
|
|
207
|
+
|
|
208
|
+
/** The key a blanket rule normalises to — mirrors POLICY_WILDCARD in @axon/capsule. */
|
|
209
|
+
const POLICY_WILDCARD = "*"
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* One authored policy, with every bare surface rule expanded to `{ "*": rule }`.
|
|
213
|
+
*
|
|
214
|
+
* `tools: "escalate"` and `tools: { "*": "escalate" }` are the same statement;
|
|
215
|
+
* this makes them the same VALUE, so nothing downstream has to ask which was
|
|
216
|
+
* written. `process` is a fixed pair rather than an open bucket, so a bare rule
|
|
217
|
+
* there applies to both verbs instead of becoming a key no verb would match.
|
|
218
|
+
*/
|
|
219
|
+
function keyed(policy: NonNullable<CapsulePartialConfig["policy"]>): NormalizedPolicy {
|
|
220
|
+
const { tools, network, process: proc, ...rest } = policy
|
|
221
|
+
return {
|
|
222
|
+
...rest,
|
|
223
|
+
...(tools !== undefined ? { tools: bucket(tools) } : {}),
|
|
224
|
+
...(network !== undefined ? { network: bucket(network) } : {}),
|
|
225
|
+
...(proc !== undefined
|
|
226
|
+
? { process: isBareRule(proc) ? { spawn: proc, run: proc } : proc }
|
|
227
|
+
: {}),
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
type NormalizedPolicy = Omit<NonNullable<CapsulePartialConfig["policy"]>, "tools" | "network" | "process"> & {
|
|
232
|
+
tools?: Record<string, PolicyRule>
|
|
233
|
+
network?: Record<string, PolicyRule>
|
|
234
|
+
process?: Partial<CapsulePolicy["process"]>
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function bucket(value: NonNullable<CapsulePartialConfig["policy"]>["tools"]): Record<string, PolicyRule> {
|
|
238
|
+
if (value === undefined) return {}
|
|
239
|
+
return isBareRule(value) ? { [POLICY_WILDCARD]: value } : value
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** A rule with no keys of its own — `true`, `false`, `"escalate"`, or a glob object. */
|
|
243
|
+
function isBareRule(value: unknown): value is PolicyRule {
|
|
244
|
+
if (typeof value === "boolean" || value === "escalate") return true
|
|
245
|
+
if (typeof value !== "object" || value === null) return false
|
|
246
|
+
const keys = Object.keys(value)
|
|
247
|
+
return keys.length > 0 && keys.every(key => key === "allow" || key === "deny" || key === "escalate")
|
|
248
|
+
}
|
|
249
|
+
|
|
183
250
|
/** Resolve an fs policy's relative paths against the agent project root. */
|
|
184
251
|
function resolveFsPaths(fs: NonNullable<CapsulePolicy["fs"]>, root: string): CapsulePolicy["fs"] {
|
|
185
252
|
const abs = (p: string) => (isAbsolute(p) ? p : resolve(root, p))
|
package/src/engine.ts
CHANGED
|
@@ -8,7 +8,8 @@ import type {
|
|
|
8
8
|
AxonEngineResponse,
|
|
9
9
|
EngineCloud,
|
|
10
10
|
} from "@arcforge/types"
|
|
11
|
-
import { asEngineFault, EngineFailure
|
|
11
|
+
import { asEngineFault, EngineFailure } from "@arcforge/engines"
|
|
12
|
+
import type { EnginesT } from "@arcforge/engines/catalogue"
|
|
12
13
|
import { Air, type AirBlockEvent, type AirT } from "@arcforge/air"
|
|
13
14
|
import type { OutputDiagnostic } from "@arcforge/air/output"
|
|
14
15
|
import type { AxonSessionT } from "@arcforge/session"
|
|
@@ -27,6 +28,14 @@ type EngineOpts = {
|
|
|
27
28
|
* not see. Absent only where no reply exists to quote.
|
|
28
29
|
*/
|
|
29
30
|
fault(input: { code: string; message: string; excerpt?: string; rejected?: string; attempt?: number }): Promise<void>
|
|
31
|
+
/**
|
|
32
|
+
* The resolved inference roles, when the cognet declared any.
|
|
33
|
+
*
|
|
34
|
+
* Absent for an agent on the single-`engine:` path, which is every
|
|
35
|
+
* published agent today — this manager is what a call naming a role
|
|
36
|
+
* dispatches through, and nothing else changes when it is missing.
|
|
37
|
+
*/
|
|
38
|
+
engines?: EnginesT
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
type EngineContext = {
|
|
@@ -390,12 +399,37 @@ export function Engine(opts: EngineOpts) {
|
|
|
390
399
|
}
|
|
391
400
|
|
|
392
401
|
let blueprint = opts.blueprint
|
|
393
|
-
let current = build(blueprint)
|
|
394
402
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
403
|
+
/**
|
|
404
|
+
* The driver for one call.
|
|
405
|
+
*
|
|
406
|
+
* Always by ROLE. The agent's single `engine:` is gone, so there is no
|
|
407
|
+
* unnamed default to fall back to and no second selection path to keep in
|
|
408
|
+
* step: a call names what it needs, and resolution already decided what
|
|
409
|
+
* fills it.
|
|
410
|
+
*/
|
|
411
|
+
function select(role: string | undefined): { driver: AxonEngineDriver; name: string } | null {
|
|
412
|
+
if (!role || !opts.engines) return null
|
|
413
|
+
const bound = opts.engines.get(role)
|
|
414
|
+
|
|
415
|
+
// This manager IS the generate path — AIR parsing, the retry budget,
|
|
416
|
+
// the output contract. A transform or a session driver has none of
|
|
417
|
+
// that and cannot be fed a message array, so reaching here with one
|
|
418
|
+
// is a wiring fault rather than a model that failed. Resolution
|
|
419
|
+
// already matched the declared type, so the only way to arrive here
|
|
420
|
+
// is a provider whose create() disagreed with the capability it was
|
|
421
|
+
// handed — loud, not coerced.
|
|
422
|
+
if (bound.driver.kind !== undefined && bound.driver.kind !== "generate") {
|
|
423
|
+
throw new EngineFailure({
|
|
424
|
+
code: "INVALID_REQUEST",
|
|
425
|
+
message: `ENGINE_KIND_MISMATCH: role "${role}" is bound to a ${bound.driver.kind} driver, which cannot serve a generate call`,
|
|
426
|
+
retryable: false,
|
|
427
|
+
provider: bound.binding.capability.provider,
|
|
428
|
+
model: bound.binding.capability.id,
|
|
429
|
+
})
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return { driver: bound.driver as AxonEngineDriver, name: bound.binding.capability.provider }
|
|
399
433
|
}
|
|
400
434
|
|
|
401
435
|
/**
|
|
@@ -422,25 +456,22 @@ export function Engine(opts: EngineOpts) {
|
|
|
422
456
|
// from meta.durationMs, which is one attempt's provider latency. A
|
|
423
457
|
// call that succeeded on its third try shows both numbers honestly.
|
|
424
458
|
const callStarted = Date.now()
|
|
425
|
-
|
|
459
|
+
const selected = select(call.role)
|
|
460
|
+
if (!selected) {
|
|
426
461
|
const fault = {
|
|
427
462
|
code: "INVALID_REQUEST" as const,
|
|
428
|
-
message:
|
|
463
|
+
message: `NO_ENGINE: no engine bound for ${call.role ? `role "${call.role}"` : "this agent"}`,
|
|
429
464
|
retryable: false,
|
|
430
465
|
provider: "unconfigured",
|
|
431
466
|
}
|
|
432
|
-
await opts.session.commit("kernel:engine:start", { provider: fault.provider }, span)
|
|
433
|
-
await opts.session.commit("kernel:engine:input", {
|
|
434
|
-
messages: req.messages,
|
|
435
|
-
bytes: new TextEncoder().encode(JSON.stringify(req.messages)).byteLength,
|
|
436
|
-
}, span)
|
|
437
467
|
await opts.session.commit("kernel:engine:failed", { attempts: 0, fault, durationMs: Date.now() - callStarted }, span)
|
|
438
468
|
throw err("ENGINE_MISSING")
|
|
439
469
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
const
|
|
470
|
+
// What resolution actually bound, not what a config named — nothing
|
|
471
|
+
// names a model any more, and the binding is the only honest answer
|
|
472
|
+
// to "which model served this call".
|
|
473
|
+
const bound = call.role ? opts.engines?.get(call.role).binding.capability.id : undefined
|
|
474
|
+
const model = req.model ?? bound
|
|
444
475
|
const correlation = model ? { provider: selected.name, model } : { provider: selected.name }
|
|
445
476
|
|
|
446
477
|
await opts.session.commit("kernel:engine:start", correlation, span)
|
|
@@ -959,7 +990,7 @@ export function Engine(opts: EngineOpts) {
|
|
|
959
990
|
? err("OUTPUT_EMPTY", {
|
|
960
991
|
detail: `the model produced no <text> or <script> block in ${budget + 1} attempts — `
|
|
961
992
|
+ `its replies could not be used. Last attempt: ${lastFault}`,
|
|
962
|
-
context: { attempts: budget + 1,
|
|
993
|
+
context: { attempts: budget + 1, model: call.model },
|
|
963
994
|
})
|
|
964
995
|
: err("OUTPUT_UNSATISFIED", {
|
|
965
996
|
detail: `the model could not produce the required output shape after ${budget + 1} attempts. `
|
|
@@ -970,7 +1001,7 @@ export function Engine(opts: EngineOpts) {
|
|
|
970
1001
|
|
|
971
1002
|
return {
|
|
972
1003
|
get configured() {
|
|
973
|
-
return
|
|
1004
|
+
return (opts.engines?.resolution.bound.length ?? 0) > 0
|
|
974
1005
|
},
|
|
975
1006
|
|
|
976
1007
|
stream: stream,
|
|
@@ -983,11 +1014,16 @@ export function Engine(opts: EngineOpts) {
|
|
|
983
1014
|
throw err("ENGINE_NO_DONE")
|
|
984
1015
|
},
|
|
985
1016
|
|
|
1017
|
+
/**
|
|
1018
|
+
* Adopt a new blueprint on reload.
|
|
1019
|
+
*
|
|
1020
|
+
* Engine bindings are NOT rebuilt here: they are resolved once at the
|
|
1021
|
+
* Axon() seam against the user's providers, and a hot reload of the
|
|
1022
|
+
* agent's own config cannot change what a user has. Changing which
|
|
1023
|
+
* model serves a role is `Engines.rebind()`, a deliberate act on one
|
|
1024
|
+
* role, not a side effect of editing a file.
|
|
1025
|
+
*/
|
|
986
1026
|
update(next: AxonBlueprint) {
|
|
987
|
-
const nextCurrent = build(next)
|
|
988
|
-
// Assign after build succeeds: a bad engine ref never corrupts
|
|
989
|
-
// either the known-good driver or its metadata source.
|
|
990
|
-
current = nextCurrent
|
|
991
1027
|
blueprint = next
|
|
992
1028
|
},
|
|
993
1029
|
}
|
package/src/kernel.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { err } from "@arcforge/err"
|
|
2
|
-
import type { AxonBlueprint, AxonEngineCall, AxonEngineEvent, AxonEntry, AxonEntryEvent, AxonEventMap, AxonRunResult, CapsuleCommandOrigin, KernelAbi } from "@arcforge/types"
|
|
2
|
+
import type { AxonBlueprint, AxonDriver, AxonEngineCall, AxonEngineEvent, AxonEntry, AxonEntryEvent, AxonEventMap, AxonRunResult, CapsuleCommandOrigin, EngineSession, KernelAbi, TransformOptions } from "@arcforge/types"
|
|
3
3
|
import { EMPTY_CAPSULE_SCOPE } from "@arcforge/types"
|
|
4
4
|
import type { AxonCloudClient } from "@arclabs/cloud"
|
|
5
5
|
import type { KernelBus } from "./contracts"
|
|
6
6
|
import { Engine } from "./engine"
|
|
7
|
+
import { asEngineFault } from "@arcforge/engines"
|
|
8
|
+
import type { EnginesT } from "@arcforge/engines/catalogue"
|
|
7
9
|
import type { AxonSessionT } from "@arcforge/session"
|
|
8
10
|
import { AxonCapsule, type AxonCapsuleT } from "./capsule"
|
|
9
11
|
import { Scheduler } from "./scheduler"
|
|
@@ -56,6 +58,16 @@ type KernelOpts = {
|
|
|
56
58
|
* deny — the honest state for a headless run.
|
|
57
59
|
*/
|
|
58
60
|
escalate?: AxonEscalate
|
|
61
|
+
/**
|
|
62
|
+
* Resolved inference roles, when the cognet declared any.
|
|
63
|
+
*
|
|
64
|
+
* Built at the Axon() seam rather than here, for the same reason session
|
|
65
|
+
* and cloud are: gathering catalogues is network work against the user's
|
|
66
|
+
* declared providers, and construction inside ring 0 would put an fs/net
|
|
67
|
+
* dependency in the one place that must stay a pure syscall table.
|
|
68
|
+
* Absent for a cognet on the single-`engine:` path.
|
|
69
|
+
*/
|
|
70
|
+
engines?: EnginesT
|
|
59
71
|
}
|
|
60
72
|
|
|
61
73
|
type KernelInput = {
|
|
@@ -282,6 +294,7 @@ export async function Kernel(opts: KernelOpts) {
|
|
|
282
294
|
})
|
|
283
295
|
|
|
284
296
|
const engine = Engine({
|
|
297
|
+
...(opts.engines ? { engines: opts.engines } : {}),
|
|
285
298
|
blueprint: opts.blueprint,
|
|
286
299
|
session,
|
|
287
300
|
cloud: opts.cloud,
|
|
@@ -486,6 +499,161 @@ export async function Kernel(opts: KernelOpts) {
|
|
|
486
499
|
* business knowing a shape was asked for. A cognet renders context and
|
|
487
500
|
* reads facts; it never carries the caller's requirements.
|
|
488
501
|
*/
|
|
502
|
+
/**
|
|
503
|
+
* The ABI's inference surface — a role name in, a handle out.
|
|
504
|
+
*
|
|
505
|
+
* Callable with `has` attached, so using an engine reads as one verb and
|
|
506
|
+
* checking for one reads as a question. Every handle routes through the
|
|
507
|
+
* same recording path, so a percept call is as traced as a cortex call —
|
|
508
|
+
* an untraced second model would be exactly the observability hole this
|
|
509
|
+
* whole layer is supposed to close.
|
|
510
|
+
*/
|
|
511
|
+
const engineAbi = Object.assign(
|
|
512
|
+
(role: string) => {
|
|
513
|
+
const bound = opts.engines?.resolution.bound.find(entry => entry.role === role)
|
|
514
|
+
if (!bound) {
|
|
515
|
+
throw err("ENGINE_ROLE_UNBOUND", {
|
|
516
|
+
detail: `no engine bound to "${role}" — declare it in engines: and check kernel.engine.has() for optional roles`,
|
|
517
|
+
})
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// What the role ACTUALLY got, shared by every handle shape.
|
|
521
|
+
const facts = {
|
|
522
|
+
get context() { return bound.capability.context },
|
|
523
|
+
get modalities() { return { in: bound.capability.in, out: bound.capability.out } },
|
|
524
|
+
get slots() { return bound.slots },
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// The handle follows the DECLARED type, because that is what the
|
|
528
|
+
// cognet's code was written against: a loop calling `.transform()`
|
|
529
|
+
// cannot run against a `.stream()` handle however substitutable
|
|
530
|
+
// the model behind it is. Resolution already refused anything
|
|
531
|
+
// whose type disagrees, so this never has to reconcile them.
|
|
532
|
+
if (bound.requirement.type === "transform") {
|
|
533
|
+
return {
|
|
534
|
+
...facts,
|
|
535
|
+
type: "transform" as const,
|
|
536
|
+
transform: (input: unknown, transformOpts?: TransformOptions) =>
|
|
537
|
+
transformAndRecord(role, input, transformOpts),
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
if (bound.requirement.type === "stream") {
|
|
542
|
+
return {
|
|
543
|
+
...facts,
|
|
544
|
+
type: "stream" as const,
|
|
545
|
+
open: () => openSession(role),
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return {
|
|
550
|
+
...facts,
|
|
551
|
+
type: "generate" as const,
|
|
552
|
+
stream: (req: AxonEngineCall) => streamAndRecord({ ...req, role }),
|
|
553
|
+
async request(req: AxonEngineCall) {
|
|
554
|
+
for await (const event of streamAndRecord({ ...req, role })) {
|
|
555
|
+
if (event.type === "engine:done") return event.response
|
|
556
|
+
}
|
|
557
|
+
throw err("ENGINE_NO_DONE")
|
|
558
|
+
},
|
|
559
|
+
}
|
|
560
|
+
},
|
|
561
|
+
{
|
|
562
|
+
has: (role: string): boolean =>
|
|
563
|
+
opts.engines?.resolution.bound.some(entry => entry.role === role) ?? false,
|
|
564
|
+
},
|
|
565
|
+
)
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* The driver bound to a role, narrowed to the kind the role declared.
|
|
569
|
+
*
|
|
570
|
+
* Resolution already matched the declared type against the capability, so
|
|
571
|
+
* a mismatch here means a provider's create() disagreed with the
|
|
572
|
+
* capability it was handed — a wiring fault, and loud rather than coerced.
|
|
573
|
+
*/
|
|
574
|
+
function driverFor<K extends "transform" | "stream">(role: string, kind: K) {
|
|
575
|
+
const bound = opts.engines?.resolution.bound.find(entry => entry.role === role)
|
|
576
|
+
const driver = opts.engines?.get(role).driver
|
|
577
|
+
if (!driver || driver.kind !== kind) {
|
|
578
|
+
throw err("ENGINE_ROLE_UNBOUND", {
|
|
579
|
+
detail: `role "${role}" is not bound to a ${kind} engine`,
|
|
580
|
+
context: { role, kind, ...(bound ? { provider: bound.capability.provider } : {}) },
|
|
581
|
+
})
|
|
582
|
+
}
|
|
583
|
+
return { driver: driver as Extract<AxonDriver, { kind: K }>, bound }
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* One transform call, bracketed with the same span family a generate call
|
|
588
|
+
* uses.
|
|
589
|
+
*
|
|
590
|
+
* Traced for the reason every mediated resource is: an untraced second
|
|
591
|
+
* engine is exactly the observability hole this whole layer was supposed
|
|
592
|
+
* to close. A depth estimation that takes four seconds must be as visible
|
|
593
|
+
* in the flame graph as a model call that takes four seconds.
|
|
594
|
+
*
|
|
595
|
+
* The PAYLOAD is never logged — it is a tensor, an image, a waveform, and
|
|
596
|
+
* the log is a record of what happened rather than a copy of what moved.
|
|
597
|
+
*/
|
|
598
|
+
async function transformAndRecord(role: string, input: unknown, transformOpts?: TransformOptions): Promise<unknown> {
|
|
599
|
+
const run = scheduler.active()
|
|
600
|
+
const { driver, bound } = driverFor(role, "transform")
|
|
601
|
+
const span = { runId: run.runId, spanId: Bun.randomUUIDv7() }
|
|
602
|
+
const started = Date.now()
|
|
603
|
+
|
|
604
|
+
const correlation = {
|
|
605
|
+
role,
|
|
606
|
+
provider: bound?.capability.provider ?? "unknown",
|
|
607
|
+
...(bound?.capability.id ? { model: bound.capability.id } : {}),
|
|
608
|
+
}
|
|
609
|
+
await session.commit("kernel:transform:start", correlation, span)
|
|
610
|
+
|
|
611
|
+
try {
|
|
612
|
+
const result = await driver.transform(input, transformOpts)
|
|
613
|
+
await session.commit("kernel:transform:complete", { role, durationMs: Date.now() - started }, span)
|
|
614
|
+
return result
|
|
615
|
+
} catch (cause) {
|
|
616
|
+
const failure = err(cause)
|
|
617
|
+
await session.commit("kernel:transform:failed", {
|
|
618
|
+
role,
|
|
619
|
+
fault: asEngineFault(cause, { provider: correlation.provider }),
|
|
620
|
+
durationMs: Date.now() - started,
|
|
621
|
+
}, span)
|
|
622
|
+
throw failure
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Open one session on a stateful engine.
|
|
628
|
+
*
|
|
629
|
+
* Deliberately NOT traced per push: a VAD at 30Hz is 1800 pushes a
|
|
630
|
+
* minute, and committing a span for each would drown the log the way
|
|
631
|
+
* nothing else in this system does. The open and close are the events
|
|
632
|
+
* worth having — they bracket the resource's lifetime, which is the thing
|
|
633
|
+
* a reader actually wants to see.
|
|
634
|
+
*/
|
|
635
|
+
function openSession(role: string): EngineSession {
|
|
636
|
+
const { driver, bound } = driverFor(role, "stream")
|
|
637
|
+
const inner = driver.open()
|
|
638
|
+
|
|
639
|
+
const span = { runId: scheduler.active().runId, spanId: Bun.randomUUIDv7() }
|
|
640
|
+
const opened = Date.now()
|
|
641
|
+
void session.commit("kernel:transform:start", {
|
|
642
|
+
role,
|
|
643
|
+
provider: bound?.capability.provider ?? "unknown",
|
|
644
|
+
...(bound?.capability.id ? { model: bound.capability.id } : {}),
|
|
645
|
+
}, span)
|
|
646
|
+
|
|
647
|
+
return {
|
|
648
|
+
push: input => inner.push(input),
|
|
649
|
+
reset: () => inner.reset(),
|
|
650
|
+
close: () => {
|
|
651
|
+
inner.close()
|
|
652
|
+
void session.commit("kernel:transform:complete", { role, durationMs: Date.now() - opened }, span)
|
|
653
|
+
},
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
489
657
|
async function* streamAndRecord(req: Parameters<typeof engine.stream>[0]): AsyncGenerator<AxonEngineEvent> {
|
|
490
658
|
const run = scheduler.active()
|
|
491
659
|
// The wake's cancellation is applied here, not passed in: an
|
|
@@ -584,8 +752,8 @@ export async function Kernel(opts: KernelOpts) {
|
|
|
584
752
|
return session.commitEntry(type, data as AxonEntryEvent[typeof type], run).then(() => {})
|
|
585
753
|
},
|
|
586
754
|
|
|
587
|
-
// llm surface
|
|
588
|
-
|
|
755
|
+
// llm surface — by role, resolved at boot against the user's providers
|
|
756
|
+
engine: engineAbi,
|
|
589
757
|
|
|
590
758
|
/**
|
|
591
759
|
* A format violation the COGNET detected while rendering the model's
|
|
@@ -666,7 +834,6 @@ export async function Kernel(opts: KernelOpts) {
|
|
|
666
834
|
// hands the paths over. Frozen so a brain cannot mutate the map it
|
|
667
835
|
// was given, and empty (never undefined) so a cognet reads it without
|
|
668
836
|
// branching.
|
|
669
|
-
models: Object.freeze({ ...(opts.blueprint.cognet.models ?? {}) }),
|
|
670
837
|
}
|
|
671
838
|
|
|
672
839
|
// exec(): the kernel is the only loader. ABI compatibility is checked
|
|
@@ -712,6 +879,20 @@ export async function Kernel(opts: KernelOpts) {
|
|
|
712
879
|
*/
|
|
713
880
|
scope: () => capsule.current.scope,
|
|
714
881
|
|
|
882
|
+
/**
|
|
883
|
+
* How this agent's declared engine roles resolved, and what can be
|
|
884
|
+
* rebound.
|
|
885
|
+
*
|
|
886
|
+
* On the HOST surface, not the cognet's: a model picker has to render
|
|
887
|
+
* which role holds which model and point one somewhere else, and a
|
|
888
|
+
* brain must never see either — the whole indirection exists so
|
|
889
|
+
* cognition cannot learn what is behind a role. Undefined for an
|
|
890
|
+
* agent whose cognet declared none.
|
|
891
|
+
*/
|
|
892
|
+
get engines() {
|
|
893
|
+
return opts.engines
|
|
894
|
+
},
|
|
895
|
+
|
|
715
896
|
/** Invoke on a stimulus arrival — invocation-mode cognets only; throws for continuous-mode. */
|
|
716
897
|
stream: streamWithContract,
|
|
717
898
|
|