feat: wire optional progress reminders to actual requests and verified readback
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
const hash = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
||||
const hash = value => createHash('sha256').update(typeof value === 'string' ? value : JSON.stringify(value)).digest('hex');
|
||||
const sha = value => /^[a-f0-9]{64}$/.test(value ?? '');
|
||||
const active = item => ['pending', 'delivered'].includes(item.status);
|
||||
const same = (a, b) => hash(a) === hash(b);
|
||||
@@ -114,14 +114,21 @@ export function selectReminder(previous, { scope, enabled = false, at, visibleTo
|
||||
|
||||
// Dispatch means included in a real submitted request. It does not assert that
|
||||
// an upstream received it, the model adopted it, or the advice caused progress.
|
||||
export function recordReminderDispatch(previous, { scope, at, message, requestId, requestSha256 }) {
|
||||
export function recordReminderDispatch(previous, { scope, at, message, requestId, requestSha256, attemptId = null, wireRequestSha256 = null }) {
|
||||
const state = queue(previous, scope, previous?.enabled, at), item = state.items.find(row => row.key === message?.key);
|
||||
if (!same(message?.scope, state.scope) || !sha(requestSha256) || typeof requestId !== 'string' || !requestId) throw new Error('Reminder request ownership is invalid.');
|
||||
if (!state.enabled || !item || !active(item)) return state;
|
||||
const expected = messageFor(state, item, message.tools ?? []);
|
||||
if (!expected || !same(expected, message)) throw new Error('Reminder dispatch differs from selected advice.');
|
||||
if (item.deliveries.some(row => row.requestId === requestId)) return state;
|
||||
item.deliveries.push({ requestId, requestSha256, messageSha256: message.sha256, ordinal: state.lastOrdinal, dispatchedAt: at });
|
||||
if (attemptId !== null && (typeof attemptId !== 'string' || !attemptId || !sha(wireRequestSha256))) throw new Error('Reminder wire attempt is invalid.');
|
||||
let delivery = item.deliveries.find(row => row.requestId === requestId);
|
||||
if (!delivery) {
|
||||
delivery = { requestId, requestSha256, messageSha256: message.sha256, ordinal: state.lastOrdinal, dispatchedAt: at };
|
||||
item.deliveries.push(delivery);
|
||||
}
|
||||
if (attemptId && !delivery.attempts?.some(row => row.attemptId === attemptId)) {
|
||||
delivery.attempts ??= []; delivery.attempts.push({ attemptId, wireRequestSha256, dispatchedAt: at });
|
||||
}
|
||||
item.status = 'delivered';
|
||||
return state;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user