@mindot/will 0.6.0 → 0.7.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/dist/channels/discord.d.ts +1 -1
- package/dist/channels/whatsapp.d.ts +1 -1
- package/dist/cli.js +434 -11
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +434 -11
- package/dist/index.js.map +1 -1
- package/dist/mcp/effectors.d.ts +1 -1
- package/dist/{will-Bikuk4s2.d.ts → will-DAW0l-lY.d.ts} +42 -4
- package/package.json +1 -1
- package/src/cognition/agency/engines/action.selector.ts +41 -9
- package/src/cognition/agency/engines/affordance.synthesizer.ts +9 -0
- package/src/cognition/agency/engines/motor.schema.executor.ts +4 -0
- package/src/cognition/agency/engines/reafference.engine.ts +30 -5
- package/src/cognition/agency/reconcile.learning.ts +9 -0
- package/src/cognition/agency/schemas/repertoire.ts +107 -7
- package/src/cognition/agency/selection.scoring.ts +7 -1
- package/src/cognition/agency/types.ts +9 -0
- package/src/stem/index.ts +15 -0
- package/src/stem/policy/arbiter.ts +136 -0
- package/src/stem/policy/rule.table.ts +172 -0
- package/src/stem/policy/verdict.recorder.ts +0 -0
- package/src/stem/tracts/effector.controller.ts +379 -0
|
@@ -23,10 +23,73 @@
|
|
|
23
23
|
|
|
24
24
|
import { logger } from '#core/logger'
|
|
25
25
|
import { reconcileInvocation } from '#agency/reconcile.learning'
|
|
26
|
+
import { NULL_ARBITER, isNullArbiter } from '#stem/policy/arbiter'
|
|
27
|
+
import type { PolicyArbiter, PolicyInvocation, Verdict } from '#stem/policy/arbiter'
|
|
28
|
+
import {
|
|
29
|
+
getVerdictRecorder, getVerdictSource, type PolicyVerdictRecord,
|
|
30
|
+
} from '#stem/policy/verdict.recorder'
|
|
26
31
|
import type { effectorInvocation } from '#types'
|
|
27
32
|
import type { WillInstance } from '#stem/index'
|
|
28
33
|
|
|
34
|
+
/** A denial awaiting application as a refusal ack at the next tick boundary. */
|
|
35
|
+
interface PendingRefusal {
|
|
36
|
+
intentId: string
|
|
37
|
+
schema: string
|
|
38
|
+
reasonCode: string
|
|
39
|
+
finality: string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** How long an escalated intent is held awaiting a resolution before it degrades
|
|
43
|
+
* to a refusal — 2× the host-ack timeout, so a human has real time to answer. */
|
|
44
|
+
const ESCALATION_TTL_TICKS = 30
|
|
45
|
+
|
|
46
|
+
/** An escalation the Will has raised: the intent is held, the ask is voiced once,
|
|
47
|
+
* and the original payload is kept so an approval can dispatch it to the world. */
|
|
48
|
+
interface Escalation {
|
|
49
|
+
intentId: string
|
|
50
|
+
schema: string
|
|
51
|
+
reasonCode: string
|
|
52
|
+
/** The withheld invocation payload — replayed to the host on approval. */
|
|
53
|
+
payload: Record<string, unknown>
|
|
54
|
+
expiresAt: number
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A host's answer to an escalation, applied at the next tick boundary. */
|
|
58
|
+
interface PendingResolution {
|
|
59
|
+
intentId: string
|
|
60
|
+
approved: boolean
|
|
61
|
+
}
|
|
62
|
+
|
|
29
63
|
export class effectorController {
|
|
64
|
+
/** The Policy Decision Point consulted before an invocation reaches the world.
|
|
65
|
+
* Defaults to the no-op arbiter, so an unconfigured Will is byte-identical. */
|
|
66
|
+
private _arbiter: PolicyArbiter = NULL_ARBITER
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Denials queued during a step's flush, drained at the NEXT tick boundary
|
|
70
|
+
* (POLICY_REAFFERENCE P1). Keyed by willId — harness state, exactly like
|
|
71
|
+
* `pendingEffectorInvocations`; never simulation state, so it does not touch
|
|
72
|
+
* `simulation.step` determinism and is regenerated on any re-execution.
|
|
73
|
+
*/
|
|
74
|
+
private _pendingRefusals = new Map<string, PendingRefusal[]>()
|
|
75
|
+
|
|
76
|
+
/** Escalations awaiting their first application (mark intent + voice the ask). */
|
|
77
|
+
private _newEscalations = new Map<string, Escalation[]>()
|
|
78
|
+
/** Escalations currently held, keyed by intent id — the resolvable set. */
|
|
79
|
+
private _activeEscalations = new Map<string, Map<string, Escalation>>()
|
|
80
|
+
/** Host answers awaiting application at the next tick boundary. */
|
|
81
|
+
private _pendingResolutions = new Map<string, PendingResolution[]>()
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Install a Policy Decision Point (POLICY_REAFFERENCE P0). Passing null
|
|
85
|
+
* restores the no-op default. The arbiter sees only the proposed act — never
|
|
86
|
+
* simulation state — and its verdict decides whether the invocation is
|
|
87
|
+
* handed to the host at all.
|
|
88
|
+
*/
|
|
89
|
+
setArbiter( arbiter: PolicyArbiter | null ): void {
|
|
90
|
+
this._arbiter = arbiter ?? NULL_ARBITER
|
|
91
|
+
}
|
|
92
|
+
|
|
30
93
|
/**
|
|
31
94
|
* Update the set of allowed communication effectors at runtime via AccessGrants
|
|
32
95
|
* (the permission / sense gate the senses + reply path read).
|
|
@@ -43,6 +106,273 @@ export class effectorController {
|
|
|
43
106
|
* echoes it on its result-ack, and `confirmExecution` uses it to find the intent.
|
|
44
107
|
*/
|
|
45
108
|
bufferInvocation( instance: WillInstance, payload: Record<string, unknown> ): void {
|
|
109
|
+
const willId = instance.config.id
|
|
110
|
+
|
|
111
|
+
// Replay: a registered source re-feeds the recorded verdict instead of
|
|
112
|
+
// re-consulting a live (or absent) PDP — the arbiter is an external oracle,
|
|
113
|
+
// exactly like the LLM. Checked FIRST so replay never re-enters the arbiter.
|
|
114
|
+
const source = getVerdictSource( willId )
|
|
115
|
+
if( source ){
|
|
116
|
+
const invocation = toPolicyInvocation( instance, payload )
|
|
117
|
+
const record = source.verdictFor( invocation.tick, invocation.intentId )
|
|
118
|
+
// A miss means the live run had no verdict here (null arbiter at record
|
|
119
|
+
// time) — the invocation was simply buffered, so reproduce that.
|
|
120
|
+
if( record ) this._applyVerdict( instance, payload, invocation, recordToVerdict( record ) )
|
|
121
|
+
else this._buffer( instance, payload )
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Fast path: no policy configured ⇒ the seam does not exist. No allocation,
|
|
126
|
+
// no branch beyond this one — the byte-identical guarantee.
|
|
127
|
+
if( isNullArbiter( this._arbiter ) ){
|
|
128
|
+
this._buffer( instance, payload )
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const invocation = toPolicyInvocation( instance, payload )
|
|
133
|
+
let verdict: Verdict | Promise<Verdict>
|
|
134
|
+
|
|
135
|
+
// An arbiter that throws must never become an implicit allow.
|
|
136
|
+
try { verdict = this._arbiter.evaluate( invocation ) }
|
|
137
|
+
catch( err ){
|
|
138
|
+
logger.error(`[policy] arbiter "${this._arbiter.name}" threw for "${invocation.schema}" — failing closed:`, err )
|
|
139
|
+
return
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if( verdict instanceof Promise ){
|
|
143
|
+
// An external PDP resolves out of tick. Safe by construction: the executor
|
|
144
|
+
// holds the intent 'awaiting' for AWAIT_TIMEOUT (15 ticks), and the refusal
|
|
145
|
+
// queue drains each tick, so a verdict landing a few ticks late still lands.
|
|
146
|
+
void verdict.then(
|
|
147
|
+
v => this._recordAndApply( instance, payload, invocation, v ),
|
|
148
|
+
err => logger.error(`[policy] arbiter "${this._arbiter.name}" rejected for "${invocation.schema}" — failing closed:`, err ),
|
|
149
|
+
)
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
this._recordAndApply( instance, payload, invocation, verdict )
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Capture the verdict on the tape (if a recorder is attached), then enforce it. */
|
|
157
|
+
private _recordAndApply(
|
|
158
|
+
instance: WillInstance,
|
|
159
|
+
payload: Record<string, unknown>,
|
|
160
|
+
invocation: PolicyInvocation,
|
|
161
|
+
verdict: Verdict,
|
|
162
|
+
): void {
|
|
163
|
+
const sink = getVerdictRecorder( instance.config.id )
|
|
164
|
+
sink?.recordVerdict({
|
|
165
|
+
tick: invocation.tick,
|
|
166
|
+
willId: instance.config.id,
|
|
167
|
+
intentId: invocation.intentId,
|
|
168
|
+
schema: invocation.schema,
|
|
169
|
+
arbiter: this._arbiter.name,
|
|
170
|
+
decision: verdict.decision,
|
|
171
|
+
...( verdict.reasonCode ? { reasonCode: verdict.reasonCode } : {} ),
|
|
172
|
+
...( verdict.finality ? { finality: verdict.finality } : {} ),
|
|
173
|
+
...( verdict.counterfactual ? { counterfactual: verdict.counterfactual } : {} ),
|
|
174
|
+
timestamp: Date.now(),
|
|
175
|
+
})
|
|
176
|
+
this._applyVerdict( instance, payload, invocation, verdict )
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Enforce a verdict (POLICY_REAFFERENCE P1).
|
|
181
|
+
*
|
|
182
|
+
* • allow → hand the invocation to the world.
|
|
183
|
+
* • deny → queue a refusal ack, applied at the next tick boundary via
|
|
184
|
+
* `confirmExecution` — the same lifecycle as a host rejection,
|
|
185
|
+
* so the mind meets *world resistance*, not a permission dialog.
|
|
186
|
+
* • escalate → raise a held escalation (POLICY_REAFFERENCE P4): the intent is
|
|
187
|
+
* held (the executor stops timing it out), the Will voices a
|
|
188
|
+
* first-person ask once, and a host resolution later approves
|
|
189
|
+
* (dispatch) or denies (refuse). Unresolved, it degrades to a
|
|
190
|
+
* refusal at ESCALATION_TTL_TICKS.
|
|
191
|
+
*
|
|
192
|
+
* P1's refusal reconciles as a plain FAILURE — safe, but the wrong learning
|
|
193
|
+
* signal (forbidden ≠ unskilled). P2 routes it to affordance AVAILABILITY
|
|
194
|
+
* instead of competence.
|
|
195
|
+
*/
|
|
196
|
+
private _applyVerdict(
|
|
197
|
+
instance: WillInstance,
|
|
198
|
+
payload: Record<string, unknown>,
|
|
199
|
+
invocation: PolicyInvocation,
|
|
200
|
+
verdict: Verdict,
|
|
201
|
+
): void {
|
|
202
|
+
if( verdict.decision === 'allow'){
|
|
203
|
+
this._buffer( instance, payload )
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const cf = verdict.counterfactual
|
|
208
|
+
logger.info(
|
|
209
|
+
`[policy] ${verdict.decision.toUpperCase()} "${invocation.schema}" intent "${invocation.intentId}"` +
|
|
210
|
+
` — ${verdict.reasonCode ?? 'no reason code'}` +
|
|
211
|
+
( verdict.finality ? ` (${verdict.finality})` : '') +
|
|
212
|
+
( cf ? ` [${cf.field}: requested ${JSON.stringify( cf.requested )}, allowed ${JSON.stringify( cf.allowed )}]` : ''),
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
if( verdict.decision === 'deny'){
|
|
216
|
+
const queue = this._pendingRefusals.get( instance.config.id ) ?? []
|
|
217
|
+
queue.push({
|
|
218
|
+
intentId: invocation.intentId,
|
|
219
|
+
schema: invocation.schema,
|
|
220
|
+
reasonCode: verdict.reasonCode ?? 'POLICY_DENIED',
|
|
221
|
+
finality: verdict.finality ?? 'instance',
|
|
222
|
+
})
|
|
223
|
+
this._pendingRefusals.set( instance.config.id, queue )
|
|
224
|
+
return
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// 'escalate' — raise a held escalation, applied (marked + voiced) at the boundary.
|
|
228
|
+
const escalations = this._newEscalations.get( instance.config.id ) ?? []
|
|
229
|
+
escalations.push({
|
|
230
|
+
intentId: invocation.intentId,
|
|
231
|
+
schema: invocation.schema,
|
|
232
|
+
reasonCode: verdict.reasonCode ?? 'APPROVAL_REQUIRED',
|
|
233
|
+
payload,
|
|
234
|
+
expiresAt: 0, // stamped when applied (we don't have the current tick here)
|
|
235
|
+
})
|
|
236
|
+
this._newEscalations.set( instance.config.id, escalations )
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Record a host's answer to an escalation (POLICY_REAFFERENCE P4). Applied at
|
|
241
|
+
* the next tick boundary so every simulation-state write stays on the boundary:
|
|
242
|
+
* approve dispatches the held invocation to the world; deny refuses it. A
|
|
243
|
+
* no-op if the intent id is not (or no longer) an active escalation.
|
|
244
|
+
*/
|
|
245
|
+
resolveEscalation( instance: WillInstance, intentId: string, approved: boolean ): void {
|
|
246
|
+
const queue = this._pendingResolutions.get( instance.config.id ) ?? []
|
|
247
|
+
queue.push({ intentId, approved })
|
|
248
|
+
this._pendingResolutions.set( instance.config.id, queue )
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Apply queued policy refusals as failure acks (POLICY_REAFFERENCE P1).
|
|
253
|
+
* Called by the tick loop at the same boundary as inbound acks — BEFORE the
|
|
254
|
+
* step, stamped to this tick — so a denial reconciled here is the exact
|
|
255
|
+
* lifecycle of a host rejection that arrived between ticks.
|
|
256
|
+
*/
|
|
257
|
+
applyPolicyOutcomes( instance: WillInstance ): void {
|
|
258
|
+
const tick = instance.tickCount
|
|
259
|
+
this._applyResolutions( instance ) // host answers land first
|
|
260
|
+
this._expireEscalations( instance, tick ) // then time out the unanswered
|
|
261
|
+
this._applyNewEscalations( instance, tick ) // then raise + voice the newest
|
|
262
|
+
this._applyRefusals( instance ) // then the plain denials
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Drain queued refusals into failure acks (POLICY_REAFFERENCE P1). */
|
|
266
|
+
private _applyRefusals( instance: WillInstance ): void {
|
|
267
|
+
const queue = this._pendingRefusals.get( instance.config.id )
|
|
268
|
+
if( !queue || queue.length === 0 ) return
|
|
269
|
+
this._pendingRefusals.set( instance.config.id, [] )
|
|
270
|
+
|
|
271
|
+
for( const refusal of queue )
|
|
272
|
+
this.confirmExecution( instance, refusal.intentId, {
|
|
273
|
+
success: false,
|
|
274
|
+
refused: true,
|
|
275
|
+
finality: refusal.finality === 'class' ? 'class' : 'instance',
|
|
276
|
+
description: `refused by policy: ${refusal.reasonCode} (${refusal.finality})`,
|
|
277
|
+
} )
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Raise each newly-escalated intent (POLICY_REAFFERENCE P4): mark it held in
|
|
281
|
+
* simulation state, voice the ask ONCE, and move it to the resolvable set. */
|
|
282
|
+
private _applyNewEscalations( instance: WillInstance, tick: number ): void {
|
|
283
|
+
const pending = this._newEscalations.get( instance.config.id )
|
|
284
|
+
if( !pending || pending.length === 0 ) return
|
|
285
|
+
this._newEscalations.set( instance.config.id, [] )
|
|
286
|
+
|
|
287
|
+
const active = this._activeEscalations.get( instance.config.id ) ?? new Map<string, Escalation>()
|
|
288
|
+
for( const esc of pending ){
|
|
289
|
+
esc.expiresAt = tick + ESCALATION_TTL_TICKS
|
|
290
|
+
this._markEscalated( instance, esc.intentId, esc.expiresAt )
|
|
291
|
+
this._voiceEscalation( instance, esc )
|
|
292
|
+
active.set( esc.intentId, esc )
|
|
293
|
+
}
|
|
294
|
+
this._activeEscalations.set( instance.config.id, active )
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Apply host answers to active escalations (POLICY_REAFFERENCE P4). */
|
|
298
|
+
private _applyResolutions( instance: WillInstance ): void {
|
|
299
|
+
const queue = this._pendingResolutions.get( instance.config.id )
|
|
300
|
+
if( !queue || queue.length === 0 ) return
|
|
301
|
+
this._pendingResolutions.set( instance.config.id, [] )
|
|
302
|
+
|
|
303
|
+
const active = this._activeEscalations.get( instance.config.id )
|
|
304
|
+
for( const { intentId, approved } of queue ){
|
|
305
|
+
const esc = active?.get( intentId )
|
|
306
|
+
if( !esc ) continue // unknown / already resolved — ignore
|
|
307
|
+
active!.delete( intentId )
|
|
308
|
+
this._clearEscalated( instance, intentId ) // release the executor's hold
|
|
309
|
+
if( approved ){
|
|
310
|
+
this._buffer( instance, esc.payload ) // dispatch the held invocation now
|
|
311
|
+
logger.info(`[policy] escalation APPROVED → dispatching "${esc.schema}" intent "${intentId}"`)
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
this._queueRefusal( instance, esc.intentId, esc.schema, esc.reasonCode, 'class')
|
|
315
|
+
logger.info(`[policy] escalation DENIED → refusing "${esc.schema}" intent "${intentId}"`)
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** Degrade escalations no one answered in time into instance-refusals (P4). */
|
|
321
|
+
private _expireEscalations( instance: WillInstance, tick: number ): void {
|
|
322
|
+
const active = this._activeEscalations.get( instance.config.id )
|
|
323
|
+
if( !active || active.size === 0 ) return
|
|
324
|
+
for( const [ intentId, esc ] of active ){
|
|
325
|
+
if( tick < esc.expiresAt ) continue
|
|
326
|
+
active.delete( intentId )
|
|
327
|
+
this._clearEscalated( instance, intentId )
|
|
328
|
+
this._queueRefusal( instance, esc.intentId, esc.schema, 'ESCALATION_EXPIRED', 'instance')
|
|
329
|
+
logger.info(`[policy] escalation EXPIRED → refusing "${esc.schema}" intent "${intentId}"`)
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Push a refusal onto the queue drained by _applyRefusals this same tick. */
|
|
334
|
+
private _queueRefusal(
|
|
335
|
+
instance: WillInstance, intentId: string, schema: string, reasonCode: string, finality: 'class' | 'instance',
|
|
336
|
+
): void {
|
|
337
|
+
const queue = this._pendingRefusals.get( instance.config.id ) ?? []
|
|
338
|
+
queue.push({ intentId, schema, reasonCode, finality })
|
|
339
|
+
this._pendingRefusals.set( instance.config.id, queue )
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Mark the awaiting intent held: the executor stops timing it out (P4). */
|
|
343
|
+
private _markEscalated( instance: WillInstance, intentId: string, expiresAt: number ): void {
|
|
344
|
+
const intent = instance.simulation.stateManager.snapshot().entities.get( intentId )
|
|
345
|
+
if( !intent || intent.type !== 'agency.intent') return
|
|
346
|
+
instance.simulation.stateManager.setEntity({
|
|
347
|
+
id: intent.id,
|
|
348
|
+
type: intent.type,
|
|
349
|
+
metadata: { ...( intent.metadata ?? {} ), escalated: true, escalationExpiresAt: expiresAt },
|
|
350
|
+
})
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Release the hold so the executor resumes normal timeout for this intent. */
|
|
354
|
+
private _clearEscalated( instance: WillInstance, intentId: string ): void {
|
|
355
|
+
const intent = instance.simulation.stateManager.snapshot().entities.get( intentId )
|
|
356
|
+
if( !intent || intent.type !== 'agency.intent') return
|
|
357
|
+
const meta = { ...( intent.metadata ?? {} ) } as Record<string, unknown>
|
|
358
|
+
delete meta['escalated']; delete meta['escalationExpiresAt']
|
|
359
|
+
instance.simulation.stateManager.setEntity({ id: intent.id, type: intent.type, metadata: meta })
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Voice the escalation as a first-person broadcast ask — once, at raise time. */
|
|
363
|
+
private _voiceEscalation( instance: WillInstance, esc: Escalation ): void {
|
|
364
|
+
try {
|
|
365
|
+
instance.cognition.outboxWriter.enqueue({
|
|
366
|
+
targetEntityId: '*',
|
|
367
|
+
content: escalationAsk( esc.schema, esc.reasonCode ),
|
|
368
|
+
effectorName: 'broadcast',
|
|
369
|
+
})
|
|
370
|
+
}
|
|
371
|
+
catch( err ){ logger.warn(`[policy] escalation voice failed for "${esc.schema}": ${errMsg( err )}`) }
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Queue an approved invocation for the delivery layer. */
|
|
375
|
+
private _buffer( instance: WillInstance, payload: Record<string, unknown> ): void {
|
|
46
376
|
const intentId = ( payload.intentId as string ) ?? ''
|
|
47
377
|
instance.pendingEffectorInvocations.push({
|
|
48
378
|
id: intentId,
|
|
@@ -86,6 +416,10 @@ export class effectorController {
|
|
|
86
416
|
success: boolean
|
|
87
417
|
description: string
|
|
88
418
|
metrics?: Record<string, number>
|
|
419
|
+
/** POLICY_REAFFERENCE P2 — set when the ack is a policy refusal, so the
|
|
420
|
+
* ReafferenceEngine routes it to availability rather than competence. */
|
|
421
|
+
refused?: boolean
|
|
422
|
+
finality?: 'class' | 'instance'
|
|
89
423
|
},
|
|
90
424
|
): void {
|
|
91
425
|
const tick = instance.tickCount
|
|
@@ -142,3 +476,48 @@ export class effectorController {
|
|
|
142
476
|
function num( v: unknown, fallback: number ): number {
|
|
143
477
|
return typeof v === 'number' && Number.isFinite( v ) ? v : fallback
|
|
144
478
|
}
|
|
479
|
+
|
|
480
|
+
/** First-person ask for an escalated action, carrying the reason's MEANING (P4).
|
|
481
|
+
* Kept template-simple here; the facet-authored version is a later refinement. */
|
|
482
|
+
function escalationAsk( schema: string, reasonCode: string ): string {
|
|
483
|
+
const meaning = ESCALATION_MEANINGS[ reasonCode ] ?? 'I need your approval before I can do this'
|
|
484
|
+
return `I want to ${ schema }, but ${ meaning }. May I go ahead?`
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** reasonCode → human meaning. Unknown codes fall back to a generic phrase. */
|
|
488
|
+
const ESCALATION_MEANINGS: Record<string, string> = {
|
|
489
|
+
APPROVAL_REQUIRED: 'I need your approval before I can on my own',
|
|
490
|
+
WRITE_REQUIRES_APPROVAL: "it writes to the world and I shouldn't on my own",
|
|
491
|
+
PAYMENT_REQUIRES_APPROVAL: 'it moves money and I must not do that unattended',
|
|
492
|
+
DEPLOY_REQUIRES_APPROVAL: 'it ships something and needs a human to sign off',
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function errMsg( err: unknown ): string {
|
|
496
|
+
return err instanceof Error ? err.message : String( err )
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** Reconstruct an enforceable Verdict from a recorded verdict (replay path). */
|
|
500
|
+
function recordToVerdict( record: PolicyVerdictRecord ): Verdict {
|
|
501
|
+
return {
|
|
502
|
+
decision: record.decision,
|
|
503
|
+
...( record.reasonCode ? { reasonCode: record.reasonCode } : {} ),
|
|
504
|
+
...( record.finality ? { finality: record.finality } : {} ),
|
|
505
|
+
...( record.counterfactual ? { counterfactual: record.counterfactual } : {} ),
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Project the `agency.invocation` payload onto the policy boundary's view of a
|
|
511
|
+
* proposed act. Only the act crosses — no cognitive internals, no state handle.
|
|
512
|
+
*/
|
|
513
|
+
function toPolicyInvocation( instance: WillInstance, payload: Record<string, unknown> ): PolicyInvocation {
|
|
514
|
+
return {
|
|
515
|
+
willId: instance.config.id,
|
|
516
|
+
intentId: ( payload.intentId as string ) ?? '',
|
|
517
|
+
schema: ( payload.schema as string ) ?? '',
|
|
518
|
+
parameters: ( payload.parameters as Record<string, unknown> ) ?? {},
|
|
519
|
+
...( typeof payload.targetEntityId === 'string' ? { targetEntityId: payload.targetEntityId } : {} ),
|
|
520
|
+
...( typeof payload.description === 'string' ? { description: payload.description } : {} ),
|
|
521
|
+
tick: ( payload.tick as number ) ?? 0,
|
|
522
|
+
}
|
|
523
|
+
}
|