@dreamforgestudiollc/emet 0.3.1 → 0.3.3
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/CHANGELOG.md +40 -0
- package/README.md +29 -6
- package/docs/BOOTSTRAP.md +2 -0
- package/docs/BOTS.md +8 -7
- package/docs/DEPLOY.md +6 -0
- package/docs/ERROR-CODES-SPEC.md +2 -2
- package/docs/ERROR-CODES.md +29 -0
- package/docs/GUIDED-SETUP.md +1 -1
- package/docs/PERMISSIONS.md +5 -5
- package/docs/SETUP.md +10 -4
- package/docs/SUBKEYS.md +4 -4
- package/package.json +2 -2
- package/server.json +8 -4
- package/src/aleph.js +34 -13
- package/src/annotations.js +6 -2
- package/src/approve-throttle.js +3 -0
- package/src/atlas-role.js +1 -1
- package/src/attribution.js +76 -28
- package/src/bound-door.js +170 -0
- package/src/break-glass.js +531 -0
- package/src/carry-forward-close.js +386 -0
- package/src/checkpoint.js +241 -0
- package/src/chooser.js +543 -0
- package/src/claimed-tag.js +140 -0
- package/src/conformance.js +269 -5
- package/src/deep-links.js +267 -0
- package/src/dispatch.js +112 -18
- package/src/error-codes.js +29 -0
- package/src/error-log.js +138 -26
- package/src/error-report.js +17 -5
- package/src/gaps.js +1 -1
- package/src/grants.js +230 -0
- package/src/http-remote.js +80 -26
- package/src/index.js +18 -2
- package/src/invites.js +47 -1
- package/src/layers.js +1 -1
- package/src/lessons-to-checks.js +1 -1
- package/src/logger.js +3 -1
- package/src/oauth-provider.js +89 -5
- package/src/owl.js +232 -0
- package/src/owner-approvals.js +24 -267
- package/src/owner-session.js +235 -0
- package/src/pages.js +1 -1
- package/src/persons.js +39 -6
- package/src/proof-run.js +70 -0
- package/src/redact.js +4 -0
- package/src/request-log.js +2 -2
- package/src/rotate.js +554 -0
- package/src/rune-dispatch.js +1 -1
- package/src/session-graph.js +44 -25
- package/src/session.js +52 -9
- package/src/setup.js +21 -12
- package/src/staged-write.js +522 -0
- package/src/startup-pages.js +2 -2
- package/src/startup-payload.js +3 -3
- package/src/subkeys.js +61 -0
- package/src/tools.js +290 -79
- package/src/transcript-session.js +23 -4
- package/src/validation.js +1 -1
- package/src/version.js +1 -1
- package/src/writer-approval.js +394 -0
- package/templates/AREA.md +1 -1
- package/templates/BOTHANDOFF.md +2 -2
- package/templates/GRANT.md +48 -0
- package/templates/INDEX.md +9 -7
- package/templates/PROCEDURE.md +1 -1
- package/templates/ROSTER.md +2 -2
- package/templates/SEED.md +2 -2
- package/templates/SESSIONDISPOSITION.md +59 -0
- package/templates/STAGING.md +1 -1
- package/templates/STANDARD.md +1 -1
- package/templates/THREAD.md +1 -1
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Carry-forward close.
|
|
3
|
+
*
|
|
4
|
+
* emet_session_close still compares this session's receipts with the store.
|
|
5
|
+
* A later session advances HANDOFF and STATE, so those receipts no longer
|
|
6
|
+
* match, the close stays blocked, and retries used to count toward the
|
|
7
|
+
* three-close cap.
|
|
8
|
+
*
|
|
9
|
+
* A records/ session disposition (templates/SESSIONDISPOSITION.md, #95)
|
|
10
|
+
* documents that fact. The row does not close the graph and it is not a
|
|
11
|
+
* receipt. This path closes the graph only when the owner door, or the
|
|
12
|
+
* credential that wrote the session, cites that current row and the later
|
|
13
|
+
* continuity versions that superseded the receipts. Transcripts stay.
|
|
14
|
+
* A declared source is not authority. A refused call is not a blocked close
|
|
15
|
+
* and is not counted.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { COLLECTION_PREFIX } from './database.js';
|
|
19
|
+
import { actorIsOwner } from './attribution.js';
|
|
20
|
+
import { rawActor, resolveTokenSubject } from './persons.js';
|
|
21
|
+
import {
|
|
22
|
+
parseDispositionRows,
|
|
23
|
+
SESSION_DISPOSITION_TEMPLATE
|
|
24
|
+
} from './conformance.js';
|
|
25
|
+
import { readDispositionCoverage } from './checkpoint.js';
|
|
26
|
+
import { isBotTag, botHandoffDoc, mainRecordIds } from './session-graph.js';
|
|
27
|
+
|
|
28
|
+
export const CARRY_FORWARD_DISPOSITIONS = Object.freeze([
|
|
29
|
+
'CARRIED_FORWARD',
|
|
30
|
+
'SUPERSEDED_BY_LATER_SESSION',
|
|
31
|
+
'ABANDONED_NO_TRANSCRIPT'
|
|
32
|
+
]);
|
|
33
|
+
const CLOSABLE = new Set(CARRY_FORWARD_DISPOSITIONS);
|
|
34
|
+
const PART_SUFFIX = /_(?:verbatim_)?part\d+$/i;
|
|
35
|
+
|
|
36
|
+
export function closeBaseId(sessionId) {
|
|
37
|
+
const id = typeof sessionId === 'string' ? sessionId.trim() : '';
|
|
38
|
+
if (!id) return null;
|
|
39
|
+
return id.replace(PART_SUFFIX, '') || null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function toMillis(value) {
|
|
43
|
+
if (value instanceof Date) {
|
|
44
|
+
const t = value.getTime();
|
|
45
|
+
return Number.isFinite(t) ? t : null;
|
|
46
|
+
}
|
|
47
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value > 1e11 ? value : value * 1000;
|
|
48
|
+
if (typeof value === 'string') {
|
|
49
|
+
const t = Date.parse(value);
|
|
50
|
+
return Number.isFinite(t) ? t : null;
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function hasCarryForward(options) {
|
|
56
|
+
return !!options && Object.prototype.hasOwnProperty.call(options, 'carry_forward') && options.carry_forward != null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function parseCarryForward(value) {
|
|
60
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) return { value, problem: null };
|
|
61
|
+
if (typeof value === 'string' && value.trim().startsWith('{')) {
|
|
62
|
+
try {
|
|
63
|
+
const parsed = JSON.parse(value);
|
|
64
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) return { value: parsed, problem: null };
|
|
65
|
+
} catch { /* text that does not parse is not a citation */ }
|
|
66
|
+
}
|
|
67
|
+
return { value: null, problem: 'carry_forward must be an object with disposition_doc, disposition_version, and citations' };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Owner door, or the credential that wrote the session. A declared source,
|
|
72
|
+
* a guest door, and a source named only on the call are not authority.
|
|
73
|
+
* No HTTP actor is the local stdio owner, the same rule as actorIsOwner.
|
|
74
|
+
*/
|
|
75
|
+
function credentialTag(actor) {
|
|
76
|
+
if (!actor || actor.sourceBasis === 'declared') return null;
|
|
77
|
+
if (actor.sourceBasis === 'subkey' || actor.sourceBasis === 'credential') {
|
|
78
|
+
const tag = typeof actor.effectiveTag === 'string' ? actor.effectiveTag.trim() : '';
|
|
79
|
+
return tag || null;
|
|
80
|
+
}
|
|
81
|
+
if (actor.sourceBasis === 'login') {
|
|
82
|
+
const tag = typeof actor.sourceTag === 'string' ? actor.sourceTag.trim() : '';
|
|
83
|
+
return tag || null;
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function carryForwardAuthority(actor, evidence = {}) {
|
|
89
|
+
const a = actor && typeof actor === 'object' ? actor : null;
|
|
90
|
+
if (!a || a.transport !== 'http') return { ok: true, basis: 'owner' };
|
|
91
|
+
if (a.door === 'guest') {
|
|
92
|
+
return { ok: false, basis: null, reason: 'a guest door cannot carry-forward close a session' };
|
|
93
|
+
}
|
|
94
|
+
if (a.sourceBasis === 'declared') {
|
|
95
|
+
return { ok: false, basis: null, reason: 'a declared source is not authority for a carry-forward close' };
|
|
96
|
+
}
|
|
97
|
+
if (actorIsOwner(a, a.ownerId) && (a.door === 'owner' || a.door == null)) {
|
|
98
|
+
return { ok: true, basis: 'owner' };
|
|
99
|
+
}
|
|
100
|
+
const opened = evidence.openedBy && typeof evidence.openedBy === 'object' ? evidence.openedBy : null;
|
|
101
|
+
if (opened && opened.person_id && a.personId && opened.person_id !== a.personId) {
|
|
102
|
+
return { ok: false, basis: null, reason: 'carry-forward close needs the owner door, or the credential that opened this session' };
|
|
103
|
+
}
|
|
104
|
+
const credTag = credentialTag(a);
|
|
105
|
+
const sameTag = !!credTag && !!evidence.sessionTag && credTag === evidence.sessionTag;
|
|
106
|
+
const sameClient = !!(opened && a.clientId && opened.client_id === a.clientId && a.personId && opened.person_id === a.personId);
|
|
107
|
+
const doorOk = a.door === 'member' || a.door === 'owner' || a.door == null;
|
|
108
|
+
if (doorOk && (sameClient || sameTag)) return { ok: true, basis: 'writer' };
|
|
109
|
+
return { ok: false, basis: null, reason: 'carry-forward close needs the owner door, or the credential that opened this session' };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function writerBase(sessionId) {
|
|
113
|
+
if (typeof sessionId !== 'string' || !sessionId.trim()) return null;
|
|
114
|
+
return sessionId.trim().replace(PART_SUFFIX, '');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function laterWriter(writers, docId, sessionBase) {
|
|
118
|
+
if (!writers || typeof writers.get !== 'function' || !sessionBase) return false;
|
|
119
|
+
const wrote = writerBase(writers.get(docId));
|
|
120
|
+
return !!wrote && wrote !== sessionBase;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* True when every receipt problem is HANDOFF or STATE having moved forward
|
|
125
|
+
* under a later session. A same-session mismatch, a missing receipt, or a
|
|
126
|
+
* working-document mismatch is false: those still count toward the cap.
|
|
127
|
+
* "Handoff was not the last write" counts as this case only together with
|
|
128
|
+
* one of those later-session mismatches.
|
|
129
|
+
*/
|
|
130
|
+
export function supersededStrikeSkipped(problems, { writers, handoffDoc, sessionBase } = {}) {
|
|
131
|
+
if (!Array.isArray(problems) || problems.length === 0 || !sessionBase) return false;
|
|
132
|
+
let sawLater = false;
|
|
133
|
+
for (const problem of problems) {
|
|
134
|
+
const handoff = /^handoff: receipt says v(\d+), store holds v(\d+)$/.exec(problem);
|
|
135
|
+
if (handoff) {
|
|
136
|
+
if (!(Number(handoff[2]) > Number(handoff[1]))) return false;
|
|
137
|
+
if (!laterWriter(writers, handoffDoc, sessionBase)) return false;
|
|
138
|
+
sawLater = true;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const board = /^board ([^:]+): receipt says v(\d+), store holds v(\d+)$/.exec(problem);
|
|
142
|
+
if (board) {
|
|
143
|
+
if (!(Number(board[3]) > Number(board[2]))) return false;
|
|
144
|
+
if (!laterWriter(writers, board[1], sessionBase)) return false;
|
|
145
|
+
sawLater = true;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (/^handoff v\d+ was written by session /.test(problem) || /^board .+ was written by session /.test(problem)) {
|
|
149
|
+
sawLater = true;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (/^handoff was not the last write /.test(problem)) continue;
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
return sawLater;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Receipt versions that match the store, written by a different session.
|
|
160
|
+
* Those numbers are the later continuity record, not this session's receipt.
|
|
161
|
+
*/
|
|
162
|
+
export function foreignContinuityProblems({ receipts, writers, handoffDoc, sessionBase, documents } = {}) {
|
|
163
|
+
if (!receipts || !sessionBase) return [];
|
|
164
|
+
const byId = new Map((documents || []).map((d) => [d.doc_id, d]));
|
|
165
|
+
const problems = [];
|
|
166
|
+
const handoff = handoffDoc ? byId.get(handoffDoc) : null;
|
|
167
|
+
if (handoff && handoff.exists && typeof receipts.handoff_version === 'number' && receipts.handoff_version === handoff.version
|
|
168
|
+
&& laterWriter(writers, handoffDoc, sessionBase)) {
|
|
169
|
+
const wrote = writerBase(writers.get(handoffDoc));
|
|
170
|
+
problems.push(`handoff v${handoff.version} was written by session ${wrote}, not ${sessionBase}; cite it with carry_forward`);
|
|
171
|
+
}
|
|
172
|
+
const boardId = typeof receipts.board_doc === 'string' ? receipts.board_doc : null;
|
|
173
|
+
const board = boardId ? byId.get(boardId) : null;
|
|
174
|
+
if (board && board.exists && typeof receipts.board_version === 'number' && receipts.board_version === board.version
|
|
175
|
+
&& laterWriter(writers, boardId, sessionBase)) {
|
|
176
|
+
const wrote = writerBase(writers.get(boardId));
|
|
177
|
+
problems.push(`board ${boardId} v${board.version} was written by session ${wrote}, not ${sessionBase}; cite it with carry_forward`);
|
|
178
|
+
}
|
|
179
|
+
return problems;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function assessCitations(citations, docs, { requiredIds = [], forbiddenIds = [], sessionBase, anchorMs } = {}) {
|
|
183
|
+
const problems = [];
|
|
184
|
+
const byId = docs instanceof Map ? docs : new Map((docs || []).map((d) => [d.doc_id, d]));
|
|
185
|
+
if (!Array.isArray(citations) || citations.length === 0) {
|
|
186
|
+
problems.push('carry_forward.citations must name the later continuity records, each as {doc_id, version}');
|
|
187
|
+
return problems;
|
|
188
|
+
}
|
|
189
|
+
const seen = new Set();
|
|
190
|
+
for (const cite of citations) {
|
|
191
|
+
const id = cite && typeof cite.doc_id === 'string' ? cite.doc_id.trim() : '';
|
|
192
|
+
const version = cite && typeof cite.version === 'number' ? cite.version : null;
|
|
193
|
+
if (!id || !Number.isInteger(version)) {
|
|
194
|
+
problems.push('each citation needs a doc_id and an integer version');
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (forbiddenIds.includes(id)) {
|
|
198
|
+
problems.push(`citation ${id} is a main record this bot session does not cite`);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
seen.add(id);
|
|
202
|
+
const doc = byId.get(id);
|
|
203
|
+
if (!doc || doc.exists === false) {
|
|
204
|
+
problems.push(`citation ${id} is not in the store`);
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (doc.version !== version) { // citation-version
|
|
208
|
+
problems.push(`citation ${id} says v${version}; the version in force is v${doc.version}`);
|
|
209
|
+
}
|
|
210
|
+
const wrote = writerBase(doc.session_id);
|
|
211
|
+
if (wrote && sessionBase && wrote === sessionBase) {
|
|
212
|
+
problems.push(`citation ${id} v${version} was written in this session; use a normal receipt close`);
|
|
213
|
+
}
|
|
214
|
+
const ms = toMillis(doc.updated_at);
|
|
215
|
+
if (anchorMs == null || ms == null || ms <= anchorMs) { // citation-later
|
|
216
|
+
problems.push(`citation ${id} v${version} is not later than this session`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
for (const id of requiredIds) {
|
|
220
|
+
if (!seen.has(id)) problems.push(`citation missing: ${id}`);
|
|
221
|
+
}
|
|
222
|
+
return problems;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function refused(problems, extra = {}) {
|
|
226
|
+
return {
|
|
227
|
+
state: 'refused',
|
|
228
|
+
speak: [
|
|
229
|
+
'Carry-forward close: refused',
|
|
230
|
+
...problems.map((p) => p),
|
|
231
|
+
'Transcript: retained',
|
|
232
|
+
'Close state: refused'
|
|
233
|
+
],
|
|
234
|
+
missing: problems,
|
|
235
|
+
warnings: [],
|
|
236
|
+
carry_forward: { closed: false, transcripts_deleted: false, ...extra },
|
|
237
|
+
close_attempts: { counted: false, escalated: false },
|
|
238
|
+
instructions:
|
|
239
|
+
'The session is NOT saved. The session graph stays open, and no transcript was deleted. ' +
|
|
240
|
+
'This refusal is not a blocked close and is not counted. ' +
|
|
241
|
+
'Cite the current records/ disposition and the later HANDOFF and STATE versions, ' +
|
|
242
|
+
'or use a normal receipt close when this session\'s receipts still match the store.'
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function anchorMillis(session, rows) {
|
|
247
|
+
const opened = toMillis(session && session.opened_at);
|
|
248
|
+
if (opened != null) return opened;
|
|
249
|
+
let max = null;
|
|
250
|
+
for (const doc of rows || []) {
|
|
251
|
+
const end = toMillis(doc && doc.session_end);
|
|
252
|
+
if (end != null && (max == null || end > max)) max = end;
|
|
253
|
+
for (const ex of (doc && doc.exchanges) || []) {
|
|
254
|
+
const t = toMillis(ex && ex.timestamp);
|
|
255
|
+
if (t != null && (max == null || t > max)) max = t;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return max;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export async function runCarryForwardClose(db, options = {}) {
|
|
262
|
+
const parsed = parseCarryForward(options.carry_forward);
|
|
263
|
+
if (parsed.problem) return refused([parsed.problem]);
|
|
264
|
+
const spec = parsed.value;
|
|
265
|
+
const requested = typeof options.session_id === 'string' ? options.session_id.trim() : '';
|
|
266
|
+
const base = closeBaseId(requested);
|
|
267
|
+
if (!base) return refused(['carry_forward requires session_id of the open session to close']);
|
|
268
|
+
|
|
269
|
+
const sessions = db.collection(`${COLLECTION_PREFIX}sessions`);
|
|
270
|
+
const session = await sessions.findOne({ _id: base });
|
|
271
|
+
if (!session || session.opened !== true) {
|
|
272
|
+
return refused([`session ${base} has no open session row`], { session_id: base });
|
|
273
|
+
}
|
|
274
|
+
if (session.closed === true) {
|
|
275
|
+
return refused([`session ${base} is already closed`], { session_id: base });
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const loaded = await (async () => {
|
|
279
|
+
const { sessionTagOf } = await import('./session.js');
|
|
280
|
+
const pattern = `^${String(base).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(_.*)?$`;
|
|
281
|
+
const rows = await db.collection('transcripts').find({ session_id: { $regex: pattern } }).toArray();
|
|
282
|
+
return { rows, tag: sessionTagOf(rows).tag };
|
|
283
|
+
})();
|
|
284
|
+
|
|
285
|
+
const actor = rawActor();
|
|
286
|
+
let ownerId = actor && actor.ownerId;
|
|
287
|
+
if (actor && actor.transport === 'http' && !ownerId) {
|
|
288
|
+
try { ownerId = await resolveTokenSubject(db); } catch { ownerId = null; }
|
|
289
|
+
}
|
|
290
|
+
const auth = carryForwardAuthority(actor ? { ...actor, ownerId: ownerId || actor.ownerId } : null, {
|
|
291
|
+
sessionTag: loaded.tag,
|
|
292
|
+
openedBy: session.opened_by || null
|
|
293
|
+
});
|
|
294
|
+
if (!auth.ok) return refused([auth.reason], { session_id: base });
|
|
295
|
+
|
|
296
|
+
const docId = typeof spec.disposition_doc === 'string' ? spec.disposition_doc.trim() : '';
|
|
297
|
+
const dispositionVersion = typeof spec.disposition_version === 'number' ? spec.disposition_version : null;
|
|
298
|
+
if (!docId || !docId.startsWith('records/') || !Number.isInteger(dispositionVersion)) {
|
|
299
|
+
return refused(['carry_forward.disposition_doc must be a records/ id and disposition_version its integer version in force'], { session_id: base });
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const covered = await readDispositionCoverage(db);
|
|
303
|
+
const exact = covered.get(requested) || covered.get(base);
|
|
304
|
+
if (!exact) {
|
|
305
|
+
const named = await db.collection('documents').findOne(
|
|
306
|
+
{ doc_id: docId },
|
|
307
|
+
{ projection: { content: 1, conformance: 1, retired: 1 } }
|
|
308
|
+
);
|
|
309
|
+
const rows = named ? parseDispositionRows(named.content) : [];
|
|
310
|
+
const transcriptOnly = rows.some((row) => (row.session_id === base || row.session_id === requested) && row.inserted_id);
|
|
311
|
+
const problems = transcriptOnly
|
|
312
|
+
? [`${docId} dispositions a transcript inserted_id only. That row does not close ${base}`]
|
|
313
|
+
: [`no current conforming session-level disposition covers ${base}`];
|
|
314
|
+
if (named && named.retired === true) problems.push(`${docId} is retired`);
|
|
315
|
+
if (named && named.conformance && named.conformance.template && named.conformance.template !== SESSION_DISPOSITION_TEMPLATE) {
|
|
316
|
+
problems.push(`${docId} is not a ${SESSION_DISPOSITION_TEMPLATE} record`);
|
|
317
|
+
}
|
|
318
|
+
return refused(problems, { session_id: base });
|
|
319
|
+
}
|
|
320
|
+
if (exact.doc_id !== docId || exact.version !== dispositionVersion) { // disposition-in-force
|
|
321
|
+
return refused([
|
|
322
|
+
`the disposition in force for ${base} is ${exact.doc_id} v${exact.version} (${exact.disposition}), not ${docId} v${dispositionVersion}`
|
|
323
|
+
], { session_id: base });
|
|
324
|
+
}
|
|
325
|
+
if (!CLOSABLE.has(exact.disposition)) { // closable-disposition
|
|
326
|
+
const why = exact.disposition === 'RETAINED_OPEN_CRITICAL'
|
|
327
|
+
? `${base} is RETAINED_OPEN_CRITICAL. That row keeps the session open.`
|
|
328
|
+
: `${base} is ${exact.disposition}. carry_forward closes only ${CARRY_FORWARD_DISPOSITIONS.join(', ')}. Use a normal receipt close for CLOSED_NORMAL.`;
|
|
329
|
+
return refused([why], { session_id: base, disposition: exact.disposition });
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const mains = mainRecordIds();
|
|
333
|
+
const bot = loaded.tag && isBotTag(loaded.tag);
|
|
334
|
+
const requiredIds = bot ? [botHandoffDoc(loaded.tag)] : mains;
|
|
335
|
+
const forbiddenIds = bot ? mains : [];
|
|
336
|
+
const citeIds = new Set(requiredIds);
|
|
337
|
+
for (const cite of Array.isArray(spec.citations) ? spec.citations : []) {
|
|
338
|
+
if (cite && typeof cite.doc_id === 'string' && cite.doc_id.trim()) citeIds.add(cite.doc_id.trim());
|
|
339
|
+
}
|
|
340
|
+
const found = new Map();
|
|
341
|
+
for (const id of citeIds) {
|
|
342
|
+
const row = await db.collection('documents').findOne(
|
|
343
|
+
{ doc_id: id },
|
|
344
|
+
{ projection: { doc_id: 1, version: 1, updated_at: 1, session_id: 1, retired: 1 } }
|
|
345
|
+
);
|
|
346
|
+
found.set(id, row && row.retired !== true
|
|
347
|
+
? { doc_id: id, exists: true, version: row.version, updated_at: row.updated_at, session_id: row.session_id || null }
|
|
348
|
+
: { doc_id: id, exists: false });
|
|
349
|
+
}
|
|
350
|
+
const anchorMs = anchorMillis(session, loaded.rows);
|
|
351
|
+
const citeProblems = assessCitations(spec.citations, found, {
|
|
352
|
+
requiredIds, forbiddenIds, sessionBase: base, anchorMs
|
|
353
|
+
});
|
|
354
|
+
if (citeProblems.length) return refused(citeProblems, { session_id: base, disposition: exact.disposition });
|
|
355
|
+
|
|
356
|
+
const citations = (spec.citations || []).map((cite) => ({ doc_id: cite.doc_id.trim(), version: cite.version }));
|
|
357
|
+
const citedText = citations.map((c) => `${c.doc_id} v${c.version}`).join(', ');
|
|
358
|
+
return {
|
|
359
|
+
state: 'complete',
|
|
360
|
+
speak: [
|
|
361
|
+
`Carry-forward close: ${base}`,
|
|
362
|
+
`Authority: ${auth.basis}`,
|
|
363
|
+
`Disposition: ${exact.doc_id} v${exact.version} ${exact.disposition}`,
|
|
364
|
+
`Later continuity: ${citedText}`,
|
|
365
|
+
'Transcript: retained',
|
|
366
|
+
'Close state: complete'
|
|
367
|
+
],
|
|
368
|
+
missing: [],
|
|
369
|
+
warnings: [],
|
|
370
|
+
carry_forward: {
|
|
371
|
+
closed: true,
|
|
372
|
+
authority: auth.basis,
|
|
373
|
+
disposition_doc: exact.doc_id,
|
|
374
|
+
disposition_version: exact.version,
|
|
375
|
+
disposition: exact.disposition,
|
|
376
|
+
citations,
|
|
377
|
+
transcripts_deleted: false,
|
|
378
|
+
session_id: base
|
|
379
|
+
},
|
|
380
|
+
close_attempts: { counted: false, escalated: false },
|
|
381
|
+
instructions:
|
|
382
|
+
'Say every line of `speak` to the user, in order. ' +
|
|
383
|
+
'This session is closed by carry-forward. The disposition and the later continuity versions were cited. ' +
|
|
384
|
+
'The transcript was kept. This is not a receipt close. After `speak`, tell the user the session is saved, in one line.'
|
|
385
|
+
};
|
|
386
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Continuous-chat checkpoint reminder (board 128, the owner 2026-10-16, resumed 2026-10-27).
|
|
3
|
+
*
|
|
4
|
+
* Context compression drops older turns, so a session closed days later cannot
|
|
5
|
+
* rebuild a word-for-word transcript. A bot saves verbatim exchanges at least
|
|
6
|
+
* nightly, under the open session or a daily `YYYY-MM-DD_chat-daily` session,
|
|
7
|
+
* and the close cites those saved parts.
|
|
8
|
+
*
|
|
9
|
+
* The server sees saved tool calls, not unsaved chat. This module only reads.
|
|
10
|
+
* It does not add a tool: emet_status always carries `checkpoint`, and any
|
|
11
|
+
* other tool result carries it when the call names an open session that is due.
|
|
12
|
+
* The save itself is still emet_transcript_append (one exchange) or
|
|
13
|
+
* save_transcript (the batch). emet_session_close cites the saved parts.
|
|
14
|
+
*
|
|
15
|
+
* A current conforming records/ session disposition covers a session for this
|
|
16
|
+
* listing. The session stays open. The disposition is not a close and not a
|
|
17
|
+
* receipt. A row that names inserted_id dispositions that transcript only.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { COLLECTION_PREFIX } from './database.js';
|
|
21
|
+
import { groupedUnder } from './session-graph.js';
|
|
22
|
+
import {
|
|
23
|
+
SESSION_DISPOSITION_PREFIX, SESSION_DISPOSITION_TEMPLATE, VERDICT,
|
|
24
|
+
dispositionResolvesOverdue, parseDispositionRows
|
|
25
|
+
} from './conformance.js';
|
|
26
|
+
|
|
27
|
+
/** At least nightly. Due when the anchor is this old or older. */
|
|
28
|
+
export const CHECKPOINT_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
29
|
+
|
|
30
|
+
export const SAVE_WITH = Object.freeze(['emet_transcript_append', 'save_transcript']);
|
|
31
|
+
export const CLOSE_WITH = 'emet_session_close';
|
|
32
|
+
|
|
33
|
+
/** New York calendar day. The daily id is this zone, not EMET_TIMEZONE. */
|
|
34
|
+
export function checkpointDay(date) {
|
|
35
|
+
return new Intl.DateTimeFormat('en-CA', {
|
|
36
|
+
timeZone: 'America/New_York', year: 'numeric', month: '2-digit', day: '2-digit'
|
|
37
|
+
}).format(date);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function saveWith() {
|
|
41
|
+
return [...SAVE_WITH];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A stored time as milliseconds. Dates and ISO strings are themselves.
|
|
46
|
+
* A number below 1e12 is Unix seconds (the exchange shape); a larger number
|
|
47
|
+
* is already milliseconds.
|
|
48
|
+
*/
|
|
49
|
+
export function checkpointMillis(value) {
|
|
50
|
+
if (value == null || value === '') return null;
|
|
51
|
+
if (value instanceof Date) {
|
|
52
|
+
const t = value.getTime();
|
|
53
|
+
return Number.isFinite(t) ? t : null;
|
|
54
|
+
}
|
|
55
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
56
|
+
return value < 1e12 ? value * 1000 : value; // checkpoint-unix
|
|
57
|
+
}
|
|
58
|
+
if (typeof value === 'string') {
|
|
59
|
+
const t = Date.parse(value);
|
|
60
|
+
return Number.isFinite(t) ? t : null;
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function iso(ms) {
|
|
66
|
+
return ms == null ? null : new Date(ms).toISOString();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function coverageGet(covered, sessionId) {
|
|
70
|
+
if (!covered || sessionId == null || sessionId === '') return null;
|
|
71
|
+
const row = covered instanceof Map ? covered.get(sessionId) : covered[sessionId];
|
|
72
|
+
if (!row || !dispositionResolvesOverdue(row.disposition)) return null;
|
|
73
|
+
return {
|
|
74
|
+
session_id: sessionId,
|
|
75
|
+
disposition: row.disposition,
|
|
76
|
+
doc_id: row.doc_id ?? null,
|
|
77
|
+
version: row.version ?? null
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Which open sessions owe a verbatim save. Pure. `rows` carry `opened`,
|
|
83
|
+
* `closed`, `opened_at`, and `last_saved_at` (the newest live transcript
|
|
84
|
+
* time, or null when nothing live is saved). The anchor is that save, else
|
|
85
|
+
* the open. No anchor is not due: an unreadable clock is not "fresh".
|
|
86
|
+
* `covered` maps a session id to a session-level disposition. A hit is not
|
|
87
|
+
* checkpoint-due. The session is not closed here.
|
|
88
|
+
*/
|
|
89
|
+
export function checkpointDecision(rows, now = new Date(), covered = null) {
|
|
90
|
+
const nowMs = checkpointMillis(now);
|
|
91
|
+
const due = [];
|
|
92
|
+
const dispositioned = [];
|
|
93
|
+
for (const row of rows || []) {
|
|
94
|
+
if (!row || row.opened !== true || row.closed === true) continue; // checkpoint-row
|
|
95
|
+
const saved = checkpointMillis(row.last_saved_at);
|
|
96
|
+
const opened = checkpointMillis(row.opened_at);
|
|
97
|
+
const anchor = saved != null ? saved : opened;
|
|
98
|
+
if (anchor == null || nowMs == null) continue;
|
|
99
|
+
const age = nowMs - anchor;
|
|
100
|
+
if (age < CHECKPOINT_INTERVAL_MS) continue; // checkpoint-night
|
|
101
|
+
const cover = coverageGet(covered, row.session_id);
|
|
102
|
+
if (cover) { dispositioned.push(cover); continue; } // disposition-cover
|
|
103
|
+
due.push({
|
|
104
|
+
session_id: row.session_id,
|
|
105
|
+
last_saved_at: saved != null ? iso(saved) : null,
|
|
106
|
+
opened_at: opened != null ? iso(opened) : null
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (!due.length) {
|
|
110
|
+
const quiet = { due: false, save_with: saveWith() };
|
|
111
|
+
if (dispositioned.length) quiet.dispositioned = dispositioned;
|
|
112
|
+
return quiet;
|
|
113
|
+
}
|
|
114
|
+
const day = checkpointDay(now instanceof Date ? now : new Date(nowMs));
|
|
115
|
+
const daily = `${day}_chat-daily`;
|
|
116
|
+
return {
|
|
117
|
+
due: true,
|
|
118
|
+
save_with: saveWith(),
|
|
119
|
+
close_with: CLOSE_WITH,
|
|
120
|
+
daily_session: daily,
|
|
121
|
+
sessions: due,
|
|
122
|
+
...(dispositioned.length ? { dispositioned } : {}),
|
|
123
|
+
text: `Checkpoint due. Save the verbatim exchanges with emet_transcript_append (one exchange) or save_transcript (the batch), under the open session or ${daily}. Do not reconstruct turns. Mark gaps as gaps. emet_session_close cites those saved parts.`
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function escapeRegex(text) {
|
|
128
|
+
return String(text).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // checkpoint-escape
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Live save time. A superseded part is not a save. session_end wins over an older exchange stamp. */
|
|
132
|
+
export function liveSaveMillis(doc) {
|
|
133
|
+
if (!doc || doc.superseded_by) return null; // checkpoint-live
|
|
134
|
+
const end = checkpointMillis(doc.session_end);
|
|
135
|
+
if (end != null) return end; // checkpoint-end
|
|
136
|
+
let max = null;
|
|
137
|
+
for (const ex of doc.exchanges || []) { // checkpoint-exchange
|
|
138
|
+
const t = checkpointMillis(ex && ex.timestamp);
|
|
139
|
+
if (t != null && (max == null || t > max)) max = t;
|
|
140
|
+
}
|
|
141
|
+
return max;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function docTime(doc) {
|
|
145
|
+
return checkpointMillis(doc && doc.updated_at) ?? 0;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Session-level covers from current conforming records/ documents.
|
|
150
|
+
* A row with inserted_id dispositions that transcript only and is not a cover.
|
|
151
|
+
* The latest write wins. A retired or nonconforming record does not cover.
|
|
152
|
+
* Throws when the store cannot be read.
|
|
153
|
+
*/
|
|
154
|
+
export async function readDispositionCoverage(db) {
|
|
155
|
+
const docs = await db.collection('documents').find({
|
|
156
|
+
doc_id: { $regex: `^${escapeRegex(SESSION_DISPOSITION_PREFIX)}` },
|
|
157
|
+
retired: { $ne: true }
|
|
158
|
+
}).toArray();
|
|
159
|
+
const ranked = [...(docs || [])].sort((a, b) => {
|
|
160
|
+
const dt = docTime(a) - docTime(b);
|
|
161
|
+
if (dt !== 0) return dt;
|
|
162
|
+
const dv = (Number(a && a.version) || 0) - (Number(b && b.version) || 0);
|
|
163
|
+
if (dv !== 0) return dv;
|
|
164
|
+
return String(a && a.doc_id).localeCompare(String(b && b.doc_id));
|
|
165
|
+
});
|
|
166
|
+
const best = new Map();
|
|
167
|
+
for (const doc of ranked) {
|
|
168
|
+
const conf = doc && doc.conformance;
|
|
169
|
+
if (!conf || conf.verdict !== VERDICT.CONFORMS) continue;
|
|
170
|
+
if (conf.template !== SESSION_DISPOSITION_TEMPLATE) continue;
|
|
171
|
+
const when = docTime(doc);
|
|
172
|
+
for (const row of parseDispositionRows(doc && doc.content)) {
|
|
173
|
+
if (row.inserted_id) continue; // disposition-transcript
|
|
174
|
+
if (!dispositionResolvesOverdue(row.disposition)) continue;
|
|
175
|
+
best.set(row.session_id, {
|
|
176
|
+
session_id: row.session_id,
|
|
177
|
+
disposition: row.disposition,
|
|
178
|
+
doc_id: doc.doc_id,
|
|
179
|
+
version: doc.version ?? null,
|
|
180
|
+
updated_ms: when
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return best;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Read open sessions and their live transcripts. Throws when the store cannot
|
|
189
|
+
* be read; the caller must not turn that into `due: false`.
|
|
190
|
+
*/
|
|
191
|
+
export async function checkpointStatus(dbManager, now = new Date()) {
|
|
192
|
+
const db = await dbManager.ensureClient();
|
|
193
|
+
const covered = await readDispositionCoverage(db);
|
|
194
|
+
const sessions = await db.collection(`${COLLECTION_PREFIX}sessions`)
|
|
195
|
+
.find({ opened: true, closed: { $ne: true } })
|
|
196
|
+
.toArray();
|
|
197
|
+
const rows = [];
|
|
198
|
+
for (const row of sessions) {
|
|
199
|
+
const id = row && row._id != null ? String(row._id) : '';
|
|
200
|
+
if (!id) continue;
|
|
201
|
+
const pattern = `^${escapeRegex(id)}(_.*)?$`; // checkpoint-span
|
|
202
|
+
const parts = await db.collection('transcripts').find({ session_id: { $regex: pattern } }).toArray();
|
|
203
|
+
let last = null;
|
|
204
|
+
for (const doc of parts) {
|
|
205
|
+
const ms = liveSaveMillis(doc);
|
|
206
|
+
if (ms != null && (last == null || ms > last)) last = ms;
|
|
207
|
+
}
|
|
208
|
+
rows.push({
|
|
209
|
+
session_id: id,
|
|
210
|
+
opened: row.opened === true,
|
|
211
|
+
closed: row.closed === true,
|
|
212
|
+
opened_at: row.opened_at ?? null,
|
|
213
|
+
last_saved_at: last
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return checkpointDecision(rows, now, covered);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Attach the reminder. emet_status always gets it, including `due: false`.
|
|
221
|
+
* Any other result gets it only when the call's session is grouped under a
|
|
222
|
+
* due open session (the base, or a part id `<base>_...`).
|
|
223
|
+
*/
|
|
224
|
+
export function applyCheckpoint(data, checkpoint, { always = false, sessionId = null } = {}) {
|
|
225
|
+
if (!data || typeof data !== 'object' || Array.isArray(data) || !checkpoint) return false;
|
|
226
|
+
if (always) {
|
|
227
|
+
data.checkpoint = checkpoint;
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
if (checkpoint.due !== true || sessionId == null || sessionId === '') return false;
|
|
231
|
+
const hit = (checkpoint.sessions || []).some((s) => groupedUnder(sessionId, s.session_id)); // checkpoint-group
|
|
232
|
+
if (!hit) return false;
|
|
233
|
+
data.checkpoint = checkpoint;
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** A failed read. Not `due: false`: that would claim the save is current. Only emet_status carries it. */
|
|
238
|
+
export function checkpointOnError(name) {
|
|
239
|
+
if (name !== 'emet_status') return null; // checkpoint-unread-gate
|
|
240
|
+
return { due: null, unread: true, save_with: saveWith() }; // checkpoint-unread-shape
|
|
241
|
+
}
|