@agenticprimitives/fabric 0.0.0-alpha.4 → 0.0.0-alpha.41
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/CLAUDE.md +28 -35
- package/README.md +18 -16
- package/dist/adoption.d.ts +79 -0
- package/dist/adoption.d.ts.map +1 -0
- package/dist/adoption.js +95 -0
- package/dist/adoption.js.map +1 -0
- package/dist/case-emissions.d.ts +95 -0
- package/dist/case-emissions.d.ts.map +1 -0
- package/dist/case-emissions.js +208 -0
- package/dist/case-emissions.js.map +1 -0
- package/dist/case-machine.d.ts +28 -2
- package/dist/case-machine.d.ts.map +1 -1
- package/dist/case-machine.js +34 -7
- package/dist/case-machine.js.map +1 -1
- package/dist/cloudflare/authority-verdict-cache.d.ts +28 -1
- package/dist/cloudflare/authority-verdict-cache.d.ts.map +1 -1
- package/dist/cloudflare/authority-verdict-cache.js +32 -1
- package/dist/cloudflare/authority-verdict-cache.js.map +1 -1
- package/dist/cloudflare/exchange-store.d.ts +20 -1
- package/dist/cloudflare/exchange-store.d.ts.map +1 -1
- package/dist/cloudflare/exchange-store.js +26 -4
- package/dist/cloudflare/exchange-store.js.map +1 -1
- package/dist/cloudflare/index.d.ts +7 -4
- package/dist/cloudflare/index.d.ts.map +1 -1
- package/dist/cloudflare/index.js +8 -2
- package/dist/cloudflare/index.js.map +1 -1
- package/dist/cloudflare/interaction-store.d.ts +81 -0
- package/dist/cloudflare/interaction-store.d.ts.map +1 -0
- package/dist/cloudflare/interaction-store.js +281 -0
- package/dist/cloudflare/interaction-store.js.map +1 -0
- package/dist/cloudflare/orchestration-runtime.d.ts +19 -5
- package/dist/cloudflare/orchestration-runtime.d.ts.map +1 -1
- package/dist/cloudflare/orchestration-runtime.js +11 -6
- package/dist/cloudflare/orchestration-runtime.js.map +1 -1
- package/dist/cloudflare/principal-gateway-do.d.ts +360 -30
- package/dist/cloudflare/principal-gateway-do.d.ts.map +1 -1
- package/dist/cloudflare/principal-gateway-do.js +580 -58
- package/dist/cloudflare/principal-gateway-do.js.map +1 -1
- package/dist/cloudflare/receipt-signer.d.ts.map +1 -1
- package/dist/cloudflare/receipt-signer.js +6 -3
- package/dist/cloudflare/receipt-signer.js.map +1 -1
- package/dist/cloudflare/sql-vault.d.ts.map +1 -1
- package/dist/cloudflare/sql-vault.js +56 -0
- package/dist/cloudflare/sql-vault.js.map +1 -1
- package/dist/cloudflare/vault-exchange-store.d.ts +47 -0
- package/dist/cloudflare/vault-exchange-store.d.ts.map +1 -0
- package/dist/cloudflare/vault-exchange-store.js +135 -0
- package/dist/cloudflare/vault-exchange-store.js.map +1 -0
- package/dist/cloudflare/vault-interaction-store.d.ts +44 -0
- package/dist/cloudflare/vault-interaction-store.d.ts.map +1 -0
- package/dist/cloudflare/vault-interaction-store.js +259 -0
- package/dist/cloudflare/vault-interaction-store.js.map +1 -0
- package/dist/communicative-act.d.ts +24 -1
- package/dist/communicative-act.d.ts.map +1 -1
- package/dist/communicative-act.js +28 -0
- package/dist/communicative-act.js.map +1 -1
- package/dist/delivery-envelope.d.ts.map +1 -1
- package/dist/delivery-envelope.js +0 -1
- package/dist/delivery-envelope.js.map +1 -1
- package/dist/email/index.d.ts +79 -0
- package/dist/email/index.d.ts.map +1 -0
- package/dist/email/index.js +91 -0
- package/dist/email/index.js.map +1 -0
- package/dist/enum-retirement.d.ts +43 -0
- package/dist/enum-retirement.d.ts.map +1 -0
- package/dist/enum-retirement.js +93 -0
- package/dist/enum-retirement.js.map +1 -0
- package/dist/exchange-record.d.ts +35 -4
- package/dist/exchange-record.d.ts.map +1 -1
- package/dist/exchange-record.js.map +1 -1
- package/dist/exchange-reducer.d.ts +32 -4
- package/dist/exchange-reducer.d.ts.map +1 -1
- package/dist/exchange-reducer.js +75 -1
- package/dist/exchange-reducer.js.map +1 -1
- package/dist/exchange-state.d.ts +30 -8
- package/dist/exchange-state.d.ts.map +1 -1
- package/dist/exchange-state.js +12 -10
- package/dist/exchange-state.js.map +1 -1
- package/dist/inbox-doc.d.ts +2 -2
- package/dist/inbox-doc.d.ts.map +1 -1
- package/dist/inbox-doc.js.map +1 -1
- package/dist/inbox-projection.d.ts +2 -2
- package/dist/inbox-projection.d.ts.map +1 -1
- package/dist/inbox-projection.js.map +1 -1
- package/dist/inbox-revision.d.ts +42 -0
- package/dist/inbox-revision.d.ts.map +1 -0
- package/dist/inbox-revision.js +57 -0
- package/dist/inbox-revision.js.map +1 -0
- package/dist/index.d.ts +26 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +40 -2
- package/dist/index.js.map +1 -1
- package/dist/interaction-machines.d.ts +136 -0
- package/dist/interaction-machines.d.ts.map +1 -0
- package/dist/interaction-machines.js +174 -0
- package/dist/interaction-machines.js.map +1 -0
- package/dist/interaction-profile.d.ts +136 -0
- package/dist/interaction-profile.d.ts.map +1 -0
- package/dist/interaction-profile.js +212 -0
- package/dist/interaction-profile.js.map +1 -0
- package/dist/interaction-record.d.ts +168 -0
- package/dist/interaction-record.d.ts.map +1 -0
- package/dist/interaction-record.js +108 -0
- package/dist/interaction-record.js.map +1 -0
- package/dist/interactions/grant-scopes.d.ts +21 -0
- package/dist/interactions/grant-scopes.d.ts.map +1 -0
- package/dist/interactions/grant-scopes.js +88 -0
- package/dist/interactions/grant-scopes.js.map +1 -0
- package/dist/interactions/index.d.ts +1 -0
- package/dist/interactions/index.d.ts.map +1 -1
- package/dist/interactions/index.js +3 -0
- package/dist/interactions/index.js.map +1 -1
- package/dist/interactions/machine.d.ts +2 -2
- package/dist/interactions/machine.d.ts.map +1 -1
- package/dist/interactions/machine.js +2 -2
- package/dist/interactions/machine.js.map +1 -1
- package/dist/interactions/open-case.d.ts +60 -0
- package/dist/interactions/open-case.d.ts.map +1 -0
- package/dist/interactions/open-case.js +83 -0
- package/dist/interactions/open-case.js.map +1 -0
- package/dist/interactions/store.d.ts +9 -0
- package/dist/interactions/store.d.ts.map +1 -1
- package/dist/interactions/store.js +57 -2
- package/dist/interactions/store.js.map +1 -1
- package/dist/messaging/board.d.ts +4 -4
- package/dist/messaging/board.d.ts.map +1 -1
- package/dist/messaging/board.js +2 -2
- package/dist/messaging/board.js.map +1 -1
- package/dist/messaging/consult-routing.d.ts +103 -23
- package/dist/messaging/consult-routing.d.ts.map +1 -1
- package/dist/messaging/consult-routing.js +157 -47
- package/dist/messaging/consult-routing.js.map +1 -1
- package/dist/messaging/consult.d.ts +4 -0
- package/dist/messaging/consult.d.ts.map +1 -1
- package/dist/messaging/consult.js +3 -1
- package/dist/messaging/consult.js.map +1 -1
- package/dist/messaging/conversation-interaction.d.ts +66 -0
- package/dist/messaging/conversation-interaction.d.ts.map +1 -0
- package/dist/messaging/conversation-interaction.js +139 -0
- package/dist/messaging/conversation-interaction.js.map +1 -0
- package/dist/messaging/conversation.d.ts +69 -1
- package/dist/messaging/conversation.d.ts.map +1 -1
- package/dist/messaging/conversation.js +131 -0
- package/dist/messaging/conversation.js.map +1 -1
- package/dist/messaging/delivery.d.ts +2 -2
- package/dist/messaging/delivery.d.ts.map +1 -1
- package/dist/messaging/delivery.js +8 -1
- package/dist/messaging/delivery.js.map +1 -1
- package/dist/messaging/envelope.d.ts +7 -3
- package/dist/messaging/envelope.d.ts.map +1 -1
- package/dist/messaging/envelope.js +18 -1
- package/dist/messaging/envelope.js.map +1 -1
- package/dist/messaging/hash.d.ts +8 -2
- package/dist/messaging/hash.d.ts.map +1 -1
- package/dist/messaging/hash.js +6 -0
- package/dist/messaging/hash.js.map +1 -1
- package/dist/messaging/inbox-assistant.d.ts +16 -4
- package/dist/messaging/inbox-assistant.d.ts.map +1 -1
- package/dist/messaging/inbox-assistant.js +27 -4
- package/dist/messaging/inbox-assistant.js.map +1 -1
- package/dist/messaging/index.d.ts +7 -3
- package/dist/messaging/index.d.ts.map +1 -1
- package/dist/messaging/index.js +11 -2
- package/dist/messaging/index.js.map +1 -1
- package/dist/messaging/outbound-message.d.ts +42 -0
- package/dist/messaging/outbound-message.d.ts.map +1 -0
- package/dist/messaging/outbound-message.js +64 -0
- package/dist/messaging/outbound-message.js.map +1 -0
- package/dist/messaging/projector.d.ts +3 -3
- package/dist/messaging/projector.d.ts.map +1 -1
- package/dist/messaging/projector.js.map +1 -1
- package/dist/messaging/receipts.d.ts +3 -3
- package/dist/messaging/receipts.d.ts.map +1 -1
- package/dist/messaging/receipts.js.map +1 -1
- package/dist/messaging/rules.d.ts +10 -3
- package/dist/messaging/rules.d.ts.map +1 -1
- package/dist/messaging/rules.js +8 -1
- package/dist/messaging/rules.js.map +1 -1
- package/dist/messaging/store.d.ts +4 -4
- package/dist/messaging/store.d.ts.map +1 -1
- package/dist/messaging/store.js +5 -3
- package/dist/messaging/store.js.map +1 -1
- package/dist/messaging/types.d.ts +30 -0
- package/dist/messaging/types.d.ts.map +1 -1
- package/dist/messaging/types.js +3 -0
- package/dist/messaging/types.js.map +1 -1
- package/dist/messaging/vault-store.d.ts +2 -2
- package/dist/messaging/vault-store.d.ts.map +1 -1
- package/dist/messaging/vault-store.js +8 -0
- package/dist/messaging/vault-store.js.map +1 -1
- package/dist/protocol-binding.d.ts +71 -0
- package/dist/protocol-binding.d.ts.map +1 -0
- package/dist/protocol-binding.js +0 -0
- package/dist/protocol-binding.js.map +1 -0
- package/dist/record-catalog.d.ts.map +1 -1
- package/dist/record-catalog.js +12 -0
- package/dist/record-catalog.js.map +1 -1
- package/package.json +28 -15
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
// DO-local InteractionStore (spec 340 W6) — durable homes for the records W2–W5 defined and nothing
|
|
3
|
+
// wrote. The `PrincipalGatewayDO` is the single writer for its principal, so no cross-writer races.
|
|
4
|
+
//
|
|
5
|
+
// Five tables, and the reason they are five rather than one wide row is the reason there are six state
|
|
6
|
+
// machines: an interaction's lifecycle, a decision, a commitment and a viewer's inbox state are
|
|
7
|
+
// answered by different parties at different times, and one row would make them one write.
|
|
8
|
+
//
|
|
9
|
+
// TWO INVARIANTS THIS LAYER OWNS, both about immutability rather than authority:
|
|
10
|
+
//
|
|
11
|
+
// 1. A ContextSnapshot is CONTENT-ADDRESSED, and the digest check IS the immutability guarantee.
|
|
12
|
+
// `putContextSnapshot` verifies the digest against the bytes before storing, so a party cannot
|
|
13
|
+
// rewrite what an evaluator saw while every Exchange still pins the same hash — the forged
|
|
14
|
+
// snapshot is refused at the door, not detected later.
|
|
15
|
+
//
|
|
16
|
+
// There is deliberately no separate `immutable-conflict` outcome. It was written first and was
|
|
17
|
+
// BOTH dead and wrong: dead, because content that differs necessarily fails the digest check
|
|
18
|
+
// already; and wrong, because it compared `JSON.stringify` output, so the same snapshot re-put
|
|
19
|
+
// with a different key insertion order would have been rejected as a conflicting overwrite. A
|
|
20
|
+
// verified digest with a row already at it means the content is the same — that is what
|
|
21
|
+
// content-addressing means — so the re-put is a no-op.
|
|
22
|
+
// 2. Every transition goes through its machine. The store never sets a state directly, so "the
|
|
23
|
+
// creditor discharges, not the debtor" cannot be bypassed by writing the row.
|
|
24
|
+
//
|
|
25
|
+
// It confers nothing. Persisting a commitment does not make it owed; persisting a decision does not
|
|
26
|
+
// authorize anything. Authority is `delegation` + `entitlements` + `tool-policy` at the moment of
|
|
27
|
+
// acting (ADR-0041, ADR-0043).
|
|
28
|
+
import { verifyContextSnapshot } from '../interaction-record.js';
|
|
29
|
+
import { applyInteractionTransition, applyDecisionTransition, applyCommitmentTransition, applyInboxTransition, TERMINAL_INTERACTION_STATES, TERMINAL_DECISION_STATES, TERMINAL_COMMITMENT_STATES, } from '../interaction-machines.js';
|
|
30
|
+
import { bindingKey, resolveInteractionByBinding, } from '../protocol-binding.js';
|
|
31
|
+
import { canonicalizeJson } from '@agenticprimitives/types';
|
|
32
|
+
const CREATE_TABLES = [
|
|
33
|
+
`CREATE TABLE IF NOT EXISTS interaction_records (
|
|
34
|
+
interaction_id TEXT PRIMARY KEY,
|
|
35
|
+
record_json TEXT NOT NULL,
|
|
36
|
+
state TEXT NOT NULL,
|
|
37
|
+
rev INTEGER NOT NULL,
|
|
38
|
+
updated_at TEXT NOT NULL
|
|
39
|
+
)`,
|
|
40
|
+
// Keyed by the binding, not by the interaction: the lookup direction is foreign → ours, and a
|
|
41
|
+
// duplicate key across two interactions is the AMBIGUITY the resolver must be able to see. A UNIQUE
|
|
42
|
+
// constraint here would silently drop the second one and manufacture a confident wrong answer.
|
|
43
|
+
`CREATE TABLE IF NOT EXISTS interaction_bindings (
|
|
44
|
+
binding_key TEXT NOT NULL,
|
|
45
|
+
interaction_id TEXT NOT NULL,
|
|
46
|
+
binding_json TEXT NOT NULL,
|
|
47
|
+
PRIMARY KEY (binding_key, interaction_id)
|
|
48
|
+
)`,
|
|
49
|
+
`CREATE TABLE IF NOT EXISTS context_snapshots (
|
|
50
|
+
digest TEXT PRIMARY KEY,
|
|
51
|
+
snapshot_json TEXT NOT NULL
|
|
52
|
+
)`,
|
|
53
|
+
`CREATE TABLE IF NOT EXISTS decision_records (
|
|
54
|
+
decision_id TEXT PRIMARY KEY,
|
|
55
|
+
record_json TEXT NOT NULL,
|
|
56
|
+
updated_at TEXT NOT NULL
|
|
57
|
+
)`,
|
|
58
|
+
`CREATE TABLE IF NOT EXISTS commitment_records (
|
|
59
|
+
commitment_id TEXT PRIMARY KEY,
|
|
60
|
+
record_json TEXT NOT NULL,
|
|
61
|
+
state TEXT NOT NULL,
|
|
62
|
+
updated_at TEXT NOT NULL
|
|
63
|
+
)`,
|
|
64
|
+
`CREATE TABLE IF NOT EXISTS inbox_entries (
|
|
65
|
+
viewer TEXT NOT NULL,
|
|
66
|
+
exchange_id TEXT NOT NULL,
|
|
67
|
+
record_json TEXT NOT NULL,
|
|
68
|
+
PRIMARY KEY (viewer, exchange_id)
|
|
69
|
+
)`,
|
|
70
|
+
];
|
|
71
|
+
/**
|
|
72
|
+
* A `put*` was offered over a record that has already ENDED (spec 445 §3.4, audit DATB-6).
|
|
73
|
+
*
|
|
74
|
+
* THE RULE: a put creates a record or replaces one that is still live; it never replaces one in a
|
|
75
|
+
* terminal state. A concluded interaction, a refused decision and a discharged commitment are facts
|
|
76
|
+
* about how something ended, and the machines refuse every transition out of them — a put that could
|
|
77
|
+
* write `active` over `discharged` would make that refusal a convention rather than a property.
|
|
78
|
+
*
|
|
79
|
+
* Thrown, not returned: the port's `put*` return `void`, and a refusal a caller can ignore by not
|
|
80
|
+
* looking is the silent overwrite again. Re-putting the IDENTICAL record is a no-op, not an error —
|
|
81
|
+
* a replayed import says nothing new. The reducers are unaffected: a transition is only ever applied
|
|
82
|
+
* to a non-terminal record, so their own writes never meet this guard.
|
|
83
|
+
*/
|
|
84
|
+
export class TerminalRecordError extends Error {
|
|
85
|
+
family;
|
|
86
|
+
id;
|
|
87
|
+
state;
|
|
88
|
+
reason = 'terminal-state';
|
|
89
|
+
constructor(family, id, state) {
|
|
90
|
+
super(`interaction-store: ${family} ${id} is ${state} (terminal); a put may not replace it`);
|
|
91
|
+
this.family = family;
|
|
92
|
+
this.id = id;
|
|
93
|
+
this.state = state;
|
|
94
|
+
this.name = 'TerminalRecordError';
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/** The guard both stores share, so "terminal" cannot mean one thing over DO-SQL and another over the
|
|
98
|
+
* vault. Returns `'skip'` when the offered record is identical to the stored terminal one. */
|
|
99
|
+
export function guardPutOverTerminal(family, id, existing, incoming, stateOf) {
|
|
100
|
+
if (!existing)
|
|
101
|
+
return 'write';
|
|
102
|
+
const terminal = family === 'interaction' ? TERMINAL_INTERACTION_STATES
|
|
103
|
+
: family === 'decision' ? TERMINAL_DECISION_STATES
|
|
104
|
+
: TERMINAL_COMMITMENT_STATES;
|
|
105
|
+
const state = stateOf(existing);
|
|
106
|
+
if (!terminal.includes(state))
|
|
107
|
+
return 'write';
|
|
108
|
+
if (canonicalizeJson(existing) === canonicalizeJson(incoming))
|
|
109
|
+
return 'skip';
|
|
110
|
+
throw new TerminalRecordError(family, id, state);
|
|
111
|
+
}
|
|
112
|
+
/** States an obligation is still live in. `discharged`/`breached`/`void` are ended, differently. */
|
|
113
|
+
const OPEN_COMMITMENT_STATES = ['proposed', 'active', 'fulfilment-claimed'];
|
|
114
|
+
export function createDurableObjectInteractionStore(sql) {
|
|
115
|
+
for (const ddl of CREATE_TABLES)
|
|
116
|
+
sql.exec(ddl);
|
|
117
|
+
const one = (query, ...binds) => {
|
|
118
|
+
const rows = sql.exec(query, ...binds).toArray();
|
|
119
|
+
return rows[0] ? JSON.parse(rows[0].record_json) : null;
|
|
120
|
+
};
|
|
121
|
+
const saveInteraction = (record) => {
|
|
122
|
+
sql.exec(`INSERT INTO interaction_records (interaction_id, record_json, state, rev, updated_at)
|
|
123
|
+
VALUES (?, ?, ?, ?, ?)
|
|
124
|
+
ON CONFLICT(interaction_id) DO UPDATE SET
|
|
125
|
+
record_json = excluded.record_json, state = excluded.state,
|
|
126
|
+
rev = excluded.rev, updated_at = excluded.updated_at`, record.interactionId, JSON.stringify(record), lifecycleStateOf(record), record.rev, record.updatedAt);
|
|
127
|
+
};
|
|
128
|
+
const saveCommitment = (record) => {
|
|
129
|
+
sql.exec(`INSERT INTO commitment_records (commitment_id, record_json, state, updated_at)
|
|
130
|
+
VALUES (?, ?, ?, ?)
|
|
131
|
+
ON CONFLICT(commitment_id) DO UPDATE SET
|
|
132
|
+
record_json = excluded.record_json, state = excluded.state, updated_at = excluded.updated_at`, record.commitmentId, JSON.stringify(record), record.state, record.updatedAt);
|
|
133
|
+
};
|
|
134
|
+
const store = {
|
|
135
|
+
async putInteraction(record) {
|
|
136
|
+
const existing = await this.getInteraction(record.interactionId);
|
|
137
|
+
if (guardPutOverTerminal('interaction', String(record.interactionId), existing, record, lifecycleStateOf) === 'skip')
|
|
138
|
+
return;
|
|
139
|
+
saveInteraction(record);
|
|
140
|
+
for (const b of record.protocolBindings ?? []) {
|
|
141
|
+
sql.exec(`INSERT INTO interaction_bindings (binding_key, interaction_id, binding_json) VALUES (?, ?, ?)
|
|
142
|
+
ON CONFLICT(binding_key, interaction_id) DO UPDATE SET binding_json = excluded.binding_json`, bindingKey(b.protocol, b.field, b.value), record.interactionId, JSON.stringify(b));
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
async getInteraction(id) {
|
|
146
|
+
return one('SELECT record_json FROM interaction_records WHERE interaction_id = ?', String(id));
|
|
147
|
+
},
|
|
148
|
+
async listInteractions() {
|
|
149
|
+
return sql
|
|
150
|
+
.exec('SELECT record_json FROM interaction_records ORDER BY updated_at DESC')
|
|
151
|
+
.toArray()
|
|
152
|
+
.map((r) => JSON.parse(r.record_json));
|
|
153
|
+
},
|
|
154
|
+
async transitionInteraction(id, event) {
|
|
155
|
+
const record = await this.getInteraction(id);
|
|
156
|
+
if (!record)
|
|
157
|
+
return null;
|
|
158
|
+
const result = applyInteractionTransition(lifecycleOf(record), event);
|
|
159
|
+
// Fail-closed: a rejection leaves the row exactly as it was. The store never sets state itself,
|
|
160
|
+
// so "concluding is custodial" cannot be bypassed by writing the row directly.
|
|
161
|
+
if (result.ok) {
|
|
162
|
+
saveInteraction({
|
|
163
|
+
...record,
|
|
164
|
+
lifecycleState: result.next.state,
|
|
165
|
+
updatedAt: event.at,
|
|
166
|
+
rev: record.rev + 1,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return result;
|
|
170
|
+
},
|
|
171
|
+
async bindProtocolTo(id, binding) {
|
|
172
|
+
sql.exec(`INSERT INTO interaction_bindings (binding_key, interaction_id, binding_json) VALUES (?, ?, ?)
|
|
173
|
+
ON CONFLICT(binding_key, interaction_id) DO UPDATE SET binding_json = excluded.binding_json`, bindingKey(binding.protocol, binding.field, binding.value), id, JSON.stringify(binding));
|
|
174
|
+
},
|
|
175
|
+
async resolveByBinding(protocol, field, value) {
|
|
176
|
+
const key = bindingKey(protocol, field, value);
|
|
177
|
+
const ids = sql
|
|
178
|
+
.exec('SELECT interaction_id FROM interaction_bindings WHERE binding_key = ?', key)
|
|
179
|
+
.toArray()
|
|
180
|
+
.map((r) => r.interaction_id);
|
|
181
|
+
// Reuses the in-memory resolver over a one-key index so SQL and non-SQL callers cannot disagree
|
|
182
|
+
// about what "ambiguous" means — one definition, two backing stores.
|
|
183
|
+
return resolveInteractionByBinding(new Map([[key, ids]]), protocol, field, value);
|
|
184
|
+
},
|
|
185
|
+
async putContextSnapshot(snapshot) {
|
|
186
|
+
// A snapshot whose digest does not match its bytes is not evidence, and storing it would let a
|
|
187
|
+
// later `verifyContextSnapshot` fail on a record that arrived corrupt rather than was tampered.
|
|
188
|
+
if (!(await verifyContextSnapshot(snapshot)))
|
|
189
|
+
return { ok: false, reason: 'digest-mismatch' };
|
|
190
|
+
const existing = sql
|
|
191
|
+
.exec('SELECT snapshot_json FROM context_snapshots WHERE digest = ?', snapshot.digest)
|
|
192
|
+
.toArray()[0];
|
|
193
|
+
// The digest verified and a row is already here, so the content is the same — two parties
|
|
194
|
+
// observing one context legitimately store it twice. A no-op, and the stored bytes win.
|
|
195
|
+
if (existing)
|
|
196
|
+
return { ok: true, stored: 'identical' };
|
|
197
|
+
sql.exec('INSERT INTO context_snapshots (digest, snapshot_json) VALUES (?, ?)', snapshot.digest, JSON.stringify(snapshot));
|
|
198
|
+
return { ok: true, stored: 'new' };
|
|
199
|
+
},
|
|
200
|
+
async getContextSnapshot(digest) {
|
|
201
|
+
const rows = sql
|
|
202
|
+
.exec('SELECT snapshot_json FROM context_snapshots WHERE digest = ?', digest)
|
|
203
|
+
.toArray();
|
|
204
|
+
return rows[0] ? JSON.parse(rows[0].snapshot_json) : null;
|
|
205
|
+
},
|
|
206
|
+
async putDecision(record) {
|
|
207
|
+
const existing = await this.getDecision(record.decisionId);
|
|
208
|
+
if (guardPutOverTerminal('decision', record.decisionId, existing, record, (r) => r.state) === 'skip')
|
|
209
|
+
return;
|
|
210
|
+
sql.exec(`INSERT INTO decision_records (decision_id, record_json, updated_at) VALUES (?, ?, ?)
|
|
211
|
+
ON CONFLICT(decision_id) DO UPDATE SET record_json = excluded.record_json, updated_at = excluded.updated_at`, record.decisionId, JSON.stringify(record), record.updatedAt);
|
|
212
|
+
},
|
|
213
|
+
async getDecision(id) {
|
|
214
|
+
return one('SELECT record_json FROM decision_records WHERE decision_id = ?', id);
|
|
215
|
+
},
|
|
216
|
+
async transitionDecision(id, event) {
|
|
217
|
+
const record = await this.getDecision(id);
|
|
218
|
+
if (!record)
|
|
219
|
+
return null;
|
|
220
|
+
const result = applyDecisionTransition(record, event);
|
|
221
|
+
if (result.ok)
|
|
222
|
+
await this.putDecision(result.next);
|
|
223
|
+
return result;
|
|
224
|
+
},
|
|
225
|
+
async putCommitment(record) {
|
|
226
|
+
const existing = await this.getCommitment(record.commitmentId);
|
|
227
|
+
if (guardPutOverTerminal('commitment', record.commitmentId, existing, record, (r) => r.state) === 'skip')
|
|
228
|
+
return;
|
|
229
|
+
saveCommitment(record);
|
|
230
|
+
},
|
|
231
|
+
async getCommitment(id) {
|
|
232
|
+
return one('SELECT record_json FROM commitment_records WHERE commitment_id = ?', id);
|
|
233
|
+
},
|
|
234
|
+
async listOpenCommitments() {
|
|
235
|
+
return sql
|
|
236
|
+
.exec(`SELECT record_json FROM commitment_records WHERE state IN (?, ?, ?) ORDER BY updated_at DESC`, ...OPEN_COMMITMENT_STATES)
|
|
237
|
+
.toArray()
|
|
238
|
+
.map((r) => JSON.parse(r.record_json));
|
|
239
|
+
},
|
|
240
|
+
async transitionCommitment(id, event) {
|
|
241
|
+
const record = await this.getCommitment(id);
|
|
242
|
+
if (!record)
|
|
243
|
+
return null;
|
|
244
|
+
const result = applyCommitmentTransition(record, event);
|
|
245
|
+
if (result.ok)
|
|
246
|
+
saveCommitment(result.next);
|
|
247
|
+
return result;
|
|
248
|
+
},
|
|
249
|
+
async putInboxEntry(entry) {
|
|
250
|
+
sql.exec(`INSERT INTO inbox_entries (viewer, exchange_id, record_json) VALUES (?, ?, ?)
|
|
251
|
+
ON CONFLICT(viewer, exchange_id) DO UPDATE SET record_json = excluded.record_json`, entry.viewer, entry.exchangeId, JSON.stringify(entry));
|
|
252
|
+
},
|
|
253
|
+
async getInboxEntry(viewer, exchangeId) {
|
|
254
|
+
return one('SELECT record_json FROM inbox_entries WHERE viewer = ? AND exchange_id = ?', viewer, exchangeId);
|
|
255
|
+
},
|
|
256
|
+
async transitionInbox(viewer, exchangeId, event) {
|
|
257
|
+
const entry = await this.getInboxEntry(viewer, exchangeId);
|
|
258
|
+
if (!entry)
|
|
259
|
+
return null;
|
|
260
|
+
const result = applyInboxTransition(entry, event);
|
|
261
|
+
if (result.ok)
|
|
262
|
+
await this.putInboxEntry(result.next);
|
|
263
|
+
return result;
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
return store;
|
|
267
|
+
}
|
|
268
|
+
/** The lifecycle facet of a stored interaction, for the machine to operate on. */
|
|
269
|
+
function lifecycleOf(record) {
|
|
270
|
+
return {
|
|
271
|
+
interactionId: record.interactionId,
|
|
272
|
+
state: lifecycleStateOf(record),
|
|
273
|
+
principal: record.principal,
|
|
274
|
+
participations: record.participations,
|
|
275
|
+
updatedAt: record.updatedAt,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
function lifecycleStateOf(record) {
|
|
279
|
+
return record.lifecycleState;
|
|
280
|
+
}
|
|
281
|
+
//# sourceMappingURL=interaction-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interaction-store.js","sourceRoot":"","sources":["../../src/cloudflare/interaction-store.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,oGAAoG;AACpG,oGAAoG;AACpG,EAAE;AACF,uGAAuG;AACvG,gGAAgG;AAChG,2FAA2F;AAC3F,EAAE;AACF,iFAAiF;AACjF,EAAE;AACF,mGAAmG;AACnG,oGAAoG;AACpG,gGAAgG;AAChG,4DAA4D;AAC5D,EAAE;AACF,oGAAoG;AACpG,kGAAkG;AAClG,oGAAoG;AACpG,mGAAmG;AACnG,6FAA6F;AAC7F,4DAA4D;AAC5D,iGAAiG;AACjG,mFAAmF;AACnF,EAAE;AACF,oGAAoG;AACpG,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AS,WAAW,CAAC,MAA2B;IAC9C,OAAO;QACL,aAAa,EAAE,MAAM,CAAC,aAAa;QACnC,KAAK,EAAE,gBAAgB,CAAC,MAAM,CAAC;QAC/B,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,cAAc,EAAE,MAAM,CAAC,cAAc;QACrC,SAAS,EAAE,MAAM,CAAC,SAAS;KAC5B,CAAC;AACJ,CAAC;AAED,SAAS,gBAAgB,CAAC,MAA2B;IACnD,OAAO,MAAM,CAAC,cAAc,CAAC;AAC/B,CAAC"}
|
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
import type { Vault } from '@agenticprimitives/vault';
|
|
2
2
|
import { type Intent, type Planner, type RunResult, type ToolInvoker, type ToolSpec } from '@agenticprimitives/orchestration';
|
|
3
|
-
import type { GatewayToolRegistry } from './gateway-tools.js';
|
|
3
|
+
import type { GatewayTool, GatewayToolRegistry } from './gateway-tools.js';
|
|
4
4
|
/** The co-resident tools as the planner's tool catalog (id + description; the plan may only reference these). */
|
|
5
5
|
export declare function gatewayToolSpecs(registry: GatewayToolRegistry): ToolSpec[];
|
|
6
|
+
/**
|
|
7
|
+
* The per-step gate (spec 445 §3.4, audit DATA-4). Called for EVERY planned step, with the concrete tool
|
|
8
|
+
* and the resolved arguments, BEFORE the tool runs; it throws to refuse. REQUIRED — the planner's input
|
|
9
|
+
* is by design a peer's text, so a step is authorized where it is used, never by having been planned.
|
|
10
|
+
* `PrincipalGatewayDO.orchestrate` supplies the §3a gate here (caveat pass B per step, request id for a
|
|
11
|
+
* write); a caller with no gate to offer has no business binding vault tools to a planner.
|
|
12
|
+
*/
|
|
13
|
+
export type GatewayStepGate = (tool: GatewayTool, args: Record<string, unknown>) => Promise<void>;
|
|
6
14
|
/** Bind the co-resident tool registry as an orchestration `ToolInvoker`: each planned step dispatches to a
|
|
7
|
-
* DO tool, run over `vault` under `principal
|
|
8
|
-
* the tool catalog too, but this is the last line). */
|
|
15
|
+
* DO tool, run over `vault` under `principal` — after `authorizeStep` has passed it. Fail-closed on an
|
|
16
|
+
* unknown tool (the plan is validated against the tool catalog too, but this is the last line). */
|
|
9
17
|
export declare function bindGatewayToolInvoker(opts: {
|
|
10
18
|
registry: GatewayToolRegistry;
|
|
11
19
|
vault: Vault;
|
|
12
20
|
principal: string;
|
|
21
|
+
authorizeStep: GatewayStepGate;
|
|
13
22
|
}): ToolInvoker;
|
|
14
23
|
/**
|
|
15
24
|
* Run a T2 intent to completion over the co-resident tools. `planner` is injected (LLM adapter in prod, the
|
|
16
|
-
* deterministic rule-based planner in tests). The loop is bounded (`maxSteps`)
|
|
17
|
-
*
|
|
25
|
+
* deterministic rule-based planner in tests). The loop is bounded (`maxSteps`). The loop itself decides no
|
|
26
|
+
* authority — `principal` is the VERIFIED verdict's principal and every step passes `authorizeStep`
|
|
27
|
+
* (ADR-0041; spec 445 §3.4). `tools` narrows the catalog the planner is shown (e.g. read tools only when
|
|
28
|
+
* the run carries no request id); the invoker's gate still stands behind it. Returns the orchestration
|
|
18
29
|
* `RunResult` (outcome + observations + composed result) the DO records onto the exchange stream.
|
|
19
30
|
*/
|
|
20
31
|
export declare function runGatewayOrchestration(intent: Intent, opts: {
|
|
@@ -22,6 +33,9 @@ export declare function runGatewayOrchestration(intent: Intent, opts: {
|
|
|
22
33
|
registry: GatewayToolRegistry;
|
|
23
34
|
vault: Vault;
|
|
24
35
|
principal: string;
|
|
36
|
+
authorizeStep: GatewayStepGate;
|
|
37
|
+
/** The subset of the registry offered to the planner. Default: all of it. */
|
|
38
|
+
tools?: ToolSpec[];
|
|
25
39
|
maxSteps?: number;
|
|
26
40
|
maxReplans?: number;
|
|
27
41
|
}): Promise<RunResult>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orchestration-runtime.d.ts","sourceRoot":"","sources":["../../src/cloudflare/orchestration-runtime.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACtD,OAAO,EAAa,KA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;IAC7E,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,GACA,OAAO,CAAC,SAAS,CAAC,CAQpB"}
|
|
@@ -7,27 +7,32 @@ export function gatewayToolSpecs(registry) {
|
|
|
7
7
|
}));
|
|
8
8
|
}
|
|
9
9
|
/** Bind the co-resident tool registry as an orchestration `ToolInvoker`: each planned step dispatches to a
|
|
10
|
-
* DO tool, run over `vault` under `principal
|
|
11
|
-
* the tool catalog too, but this is the last line). */
|
|
10
|
+
* DO tool, run over `vault` under `principal` — after `authorizeStep` has passed it. Fail-closed on an
|
|
11
|
+
* unknown tool (the plan is validated against the tool catalog too, but this is the last line). */
|
|
12
12
|
export function bindGatewayToolInvoker(opts) {
|
|
13
|
+
if (typeof opts.authorizeStep !== 'function')
|
|
14
|
+
throw new Error('orchestration: authorizeStep is required (no gate ⇒ no tools)');
|
|
13
15
|
return async (toolId, args) => {
|
|
14
16
|
const tool = opts.registry.get(toolId);
|
|
15
17
|
if (!tool)
|
|
16
18
|
throw new Error(`orchestration: unknown tool ${toolId}`);
|
|
19
|
+
await opts.authorizeStep(tool, args);
|
|
17
20
|
return tool.run({ principal: opts.principal, vault: opts.vault, args });
|
|
18
21
|
};
|
|
19
22
|
}
|
|
20
23
|
/**
|
|
21
24
|
* Run a T2 intent to completion over the co-resident tools. `planner` is injected (LLM adapter in prod, the
|
|
22
|
-
* deterministic rule-based planner in tests). The loop is bounded (`maxSteps`)
|
|
23
|
-
*
|
|
25
|
+
* deterministic rule-based planner in tests). The loop is bounded (`maxSteps`). The loop itself decides no
|
|
26
|
+
* authority — `principal` is the VERIFIED verdict's principal and every step passes `authorizeStep`
|
|
27
|
+
* (ADR-0041; spec 445 §3.4). `tools` narrows the catalog the planner is shown (e.g. read tools only when
|
|
28
|
+
* the run carries no request id); the invoker's gate still stands behind it. Returns the orchestration
|
|
24
29
|
* `RunResult` (outcome + observations + composed result) the DO records onto the exchange stream.
|
|
25
30
|
*/
|
|
26
31
|
export function runGatewayOrchestration(intent, opts) {
|
|
27
32
|
return runIntent(intent, {
|
|
28
33
|
planner: opts.planner,
|
|
29
|
-
tools: gatewayToolSpecs(opts.registry),
|
|
30
|
-
invoke: bindGatewayToolInvoker({ registry: opts.registry, vault: opts.vault, principal: opts.principal }),
|
|
34
|
+
tools: opts.tools ?? gatewayToolSpecs(opts.registry),
|
|
35
|
+
invoke: bindGatewayToolInvoker({ registry: opts.registry, vault: opts.vault, principal: opts.principal, authorizeStep: opts.authorizeStep }),
|
|
31
36
|
maxSteps: opts.maxSteps,
|
|
32
37
|
maxReplans: opts.maxReplans,
|
|
33
38
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orchestration-runtime.js","sourceRoot":"","sources":["../../src/cloudflare/orchestration-runtime.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,SAAS,EAA8E,MAAM,kCAAkC,CAAC;AAGzI,iHAAiH;AACjH,MAAM,UAAU,gBAAgB,CAAC,QAA6B;IAC5D,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACjC,EAAE,EAAE,CAAC,CAAC,IAAI;QACV,WAAW,EAAE,GAAG,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,2BAA2B,CAAC,CAAC,IAAI,GAAG;KAC3F,CAAC,CAAC,CAAC;AACN,CAAC;
|
|
1
|
+
{"version":3,"file":"orchestration-runtime.js","sourceRoot":"","sources":["../../src/cloudflare/orchestration-runtime.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,SAAS,EAA8E,MAAM,kCAAkC,CAAC;AAGzI,iHAAiH;AACjH,MAAM,UAAU,gBAAgB,CAAC,QAA6B;IAC5D,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC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