@dreamforgestudiollc/emet 0.3.0 → 0.3.2

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.
Files changed (73) hide show
  1. package/.env.example +2 -2
  2. package/CHANGELOG.md +37 -0
  3. package/README.md +33 -8
  4. package/docs/BOOTSTRAP.md +2 -0
  5. package/docs/BOTS.md +8 -7
  6. package/docs/DEPLOY.md +6 -0
  7. package/docs/DEVELOPMENT-PRACTICES.md +26 -0
  8. package/docs/ERROR-CODES-SPEC.md +1 -1
  9. package/docs/ERROR-CODES.md +29 -0
  10. package/docs/GUIDED-SETUP.md +1 -1
  11. package/docs/PERMISSIONS.md +5 -5
  12. package/docs/SETUP.md +10 -4
  13. package/docs/SUBKEYS.md +4 -4
  14. package/package.json +3 -2
  15. package/server.json +8 -4
  16. package/src/aleph.js +34 -13
  17. package/src/annotations.js +6 -2
  18. package/src/approve-throttle.js +3 -0
  19. package/src/atlas-role.js +1 -1
  20. package/src/attribution.js +76 -28
  21. package/src/bound-door.js +170 -0
  22. package/src/break-glass.js +531 -0
  23. package/src/checkpoint.js +241 -0
  24. package/src/chooser.js +543 -0
  25. package/src/claimed-tag.js +140 -0
  26. package/src/conformance.js +269 -5
  27. package/src/deep-links.js +267 -0
  28. package/src/dispatch.js +112 -18
  29. package/src/error-codes.js +29 -0
  30. package/src/error-log.js +138 -26
  31. package/src/error-report.js +17 -5
  32. package/src/gaps.js +1 -1
  33. package/src/grants.js +230 -0
  34. package/src/http-remote.js +80 -26
  35. package/src/index.js +18 -2
  36. package/src/invites.js +47 -1
  37. package/src/layers.js +1 -1
  38. package/src/lessons-to-checks.js +1 -1
  39. package/src/logger.js +3 -1
  40. package/src/oauth-provider.js +89 -5
  41. package/src/owl.js +232 -0
  42. package/src/owner-approvals.js +24 -267
  43. package/src/owner-session.js +174 -0
  44. package/src/pages.js +1 -1
  45. package/src/persons.js +39 -6
  46. package/src/proof-run.js +70 -0
  47. package/src/redact.js +4 -0
  48. package/src/request-log.js +2 -2
  49. package/src/rotate.js +554 -0
  50. package/src/rune-dispatch.js +1 -1
  51. package/src/session-graph.js +44 -25
  52. package/src/session.js +3 -3
  53. package/src/setup.js +21 -12
  54. package/src/staged-write.js +522 -0
  55. package/src/startup-pages.js +2 -2
  56. package/src/startup-payload.js +3 -3
  57. package/src/subkeys.js +61 -0
  58. package/src/tools.js +270 -78
  59. package/src/transcript-session.js +23 -4
  60. package/src/validation.js +1 -1
  61. package/src/version.js +1 -1
  62. package/src/writer-approval.js +394 -0
  63. package/templates/AREA.md +1 -1
  64. package/templates/BOTHANDOFF.md +2 -2
  65. package/templates/GRANT.md +48 -0
  66. package/templates/INDEX.md +9 -7
  67. package/templates/PROCEDURE.md +1 -1
  68. package/templates/ROSTER.md +2 -2
  69. package/templates/SEED.md +2 -2
  70. package/templates/SESSIONDISPOSITION.md +59 -0
  71. package/templates/STAGING.md +1 -1
  72. package/templates/STANDARD.md +1 -1
  73. package/templates/THREAD.md +1 -1
@@ -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
+ }