@dreamforgestudiollc/emet 0.2.4 → 0.3.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.
Files changed (116) hide show
  1. package/.env.example +63 -0
  2. package/CHANGELOG.md +84 -3
  3. package/CHARTER.md +19 -7
  4. package/README.md +130 -21
  5. package/areas/emet.md +18 -0
  6. package/areas/garden.md +18 -0
  7. package/areas/health.md +18 -0
  8. package/areas/home.md +18 -0
  9. package/areas/learning.md +18 -0
  10. package/areas/state.md +18 -0
  11. package/docs/AUTHORSHIP.md +3 -3
  12. package/docs/BOTS.md +4 -0
  13. package/docs/COVERAGE.md +2 -2
  14. package/docs/DEPLOY.md +41 -5
  15. package/docs/ERROR-CODES-SPEC.md +43 -0
  16. package/docs/ERROR-CODES.md +332 -0
  17. package/docs/GUARDRAILS-AND-MONORAILS.md +2 -1
  18. package/docs/GUIDED-SETUP.md +24 -3
  19. package/docs/PERMISSIONS.md +13 -7
  20. package/docs/ROADMAP.md +18 -15
  21. package/docs/SETUP.md +123 -14
  22. package/docs/SUBKEYS.md +59 -0
  23. package/gems/artifact-index.json +12 -0
  24. package/gems/bot-team-backup/floor.json +15 -0
  25. package/gems/bot-team-backup/gem.json +43 -0
  26. package/gems/bot-team-backup/guides/BOT_TEAM_BACKUP.md +27 -0
  27. package/gems/bot-team-backup/guides/BOT_TEAM_RECOVERY.md +23 -0
  28. package/gems/fieldnote/floor.json +14 -0
  29. package/gems/fieldnote/gem.json +70 -0
  30. package/gems/fieldnote/runes/fieldnote.js +4 -0
  31. package/gems/lean-six-sigma/CHANGELOG.md +10 -0
  32. package/gems/lean-six-sigma/README.md +92 -0
  33. package/gems/lean-six-sigma/floor.json +31 -0
  34. package/gems/lean-six-sigma/gem.json +1296 -0
  35. package/gems/lean-six-sigma/guides/ASSUMPTIONS.md +45 -0
  36. package/gems/lean-six-sigma/guides/BELTS.md +52 -0
  37. package/gems/lean-six-sigma/guides/DMADV.md +56 -0
  38. package/gems/lean-six-sigma/guides/DMAIC.md +127 -0
  39. package/gems/lean-six-sigma/guides/FORMULAS.md +330 -0
  40. package/gems/lean-six-sigma/guides/LEAN.md +97 -0
  41. package/gems/lean-six-sigma/guides/TEMPLATES.md +279 -0
  42. package/gems/lean-six-sigma/runes/lss_capability.js +132 -0
  43. package/gems/lean-six-sigma/runes/lss_control_limits.js +240 -0
  44. package/gems/lean-six-sigma/runes/lss_dpmo_sigma.js +81 -0
  45. package/gems/lean-six-sigma/runes/lss_hypothesis.js +298 -0
  46. package/gems/lean-six-sigma/runes/lss_sample_size.js +81 -0
  47. package/gems/neurendipity/floor.json +65 -0
  48. package/gems/neurendipity/gem.json +42 -0
  49. package/gems/neurendipity/guides/AUTISTIC_BURNOUT.md +81 -0
  50. package/{guides → gems/neurendipity/guides}/NEURODIVERGENT_SUPPORT.md +17 -8
  51. package/guides/README.md +3 -0
  52. package/package.json +12 -5
  53. package/server.json +48 -4
  54. package/src/aleph.js +18 -12
  55. package/src/annotations.js +45 -5
  56. package/src/atlas-role.js +9 -4
  57. package/src/attribution.js +555 -0
  58. package/src/conformance.js +7 -5
  59. package/src/database.js +34 -17
  60. package/src/dispatch.js +136 -16
  61. package/src/env.js +54 -9
  62. package/src/error-codes.js +394 -0
  63. package/src/error-log.js +273 -0
  64. package/src/error-report.js +245 -0
  65. package/src/floor.js +84 -41
  66. package/src/gaps.js +41 -8
  67. package/src/gem-check.js +1360 -0
  68. package/src/guides.js +31 -4
  69. package/src/host-reach.js +23 -0
  70. package/src/http-remote.js +47 -2
  71. package/src/index.js +10 -2
  72. package/src/invites.js +6 -5
  73. package/src/layers.js +9 -8
  74. package/src/lessons-to-checks.js +3 -3
  75. package/src/logger.js +2 -1
  76. package/src/mongo-connect.js +50 -2
  77. package/src/oauth-provider.js +30 -29
  78. package/src/owner-approvals.js +281 -0
  79. package/src/pages.js +806 -0
  80. package/src/persons.js +22 -1
  81. package/src/provenance.js +10 -10
  82. package/src/redact.js +10 -4
  83. package/src/request-log.js +1 -1
  84. package/src/rune-dispatch.js +360 -0
  85. package/src/rune-registry.js +71 -0
  86. package/src/rune-scope.js +42 -0
  87. package/src/scopes.js +3 -0
  88. package/src/session-graph.js +15 -12
  89. package/src/session.js +10 -10
  90. package/src/setup.js +307 -36
  91. package/src/socket.js +545 -0
  92. package/src/startup-pages.js +26 -8
  93. package/src/startup-payload.js +12 -8
  94. package/src/subkey-store.js +358 -0
  95. package/src/subkeys.js +947 -0
  96. package/src/tools.js +518 -204
  97. package/src/transcript-session.js +1 -1
  98. package/src/validation.js +85 -52
  99. package/src/version.js +1 -1
  100. package/templates/AREA.md +35 -0
  101. package/templates/BOARD.md +2 -2
  102. package/templates/BOTHANDOFF.md +2 -2
  103. package/templates/CHARACTER.md +1 -1
  104. package/templates/HANDOFF.md +1 -1
  105. package/templates/IDENTITY.md +1 -1
  106. package/templates/INDEX.md +21 -20
  107. package/templates/INSTALL.md +1 -1
  108. package/templates/PROCEDURE.md +1 -1
  109. package/templates/ROSTER.md +2 -2
  110. package/templates/SEED.md +2 -2
  111. package/templates/STAGING.md +2 -2
  112. package/templates/STANDARD.md +5 -5
  113. package/templates/THREAD.md +1 -1
  114. package/templates/USER.md +4 -1
  115. package/verification/load-gate/corpus.json +1765 -0
  116. package/verification/load-gate/load-gate.mjs +1246 -0
@@ -0,0 +1,273 @@
1
+ /**
2
+ * Operational error log. Not a controlled document and not a memory.
3
+ * Entries are capped and rotated. Nothing here stores message text,
4
+ * arguments, document ids, names, or secrets.
5
+ */
6
+
7
+ import { readySharedClient } from './mongo-connect.js';
8
+ import { MONGODB_URI, MONGODB_DB, ErrorCodes } from './database.js';
9
+ import { redactText } from './redact.js';
10
+ import { currentActor, getPerson } from './persons.js';
11
+ import { registeredSourceTags } from './setup.js';
12
+ import { EMET_VERSION } from './version.js';
13
+ import { CodedError, EMET_CODE_RE, EMET_INT_001, lookupEmetCode, newCorrelationId } from './error-codes.js';
14
+
15
+ export const ERROR_LOG_COLLECTION = 'error_log';
16
+ export const ERROR_LOG_SUMMARY_COLLECTION = 'error_log_summary';
17
+ export const ERROR_LOG_MAX = 10000;
18
+ export const ERROR_LOG_DAYS = 90;
19
+
20
+ const ENTRY_KEYS = Object.freeze([
21
+ '_id', 't', 'emet_code', 'class_code', 'tool', 'version', 'transport', 'caller', 'role', 'correlation_id', 'count'
22
+ ]);
23
+ const SUMMARY_KEYS = Object.freeze(['day', 'emet_code', 'tool', 'version', 'count']);
24
+ const CLASS_CODES = new Set(Object.values(ErrorCodes));
25
+
26
+ let backend = null;
27
+ let indexed = false;
28
+ let chain = Promise.resolve();
29
+
30
+ /** Test seam. Pass null to use Mongo again. */
31
+ export function setErrorLogBackend(store) {
32
+ backend = store;
33
+ }
34
+
35
+ export function useMemoryErrorLog() {
36
+ const store = { entries: [], summaries: [], fail: false };
37
+ backend = store;
38
+ return store;
39
+ }
40
+
41
+ export function flushErrorLog() {
42
+ return chain;
43
+ }
44
+
45
+ function scrub(value) {
46
+ return redactText(String(value ?? ''));
47
+ }
48
+
49
+ function safeCode(code) {
50
+ return typeof code === 'string' && EMET_CODE_RE.test(code) && lookupEmetCode(code) ? code : EMET_INT_001;
51
+ }
52
+
53
+ function safeClass(code) {
54
+ return CLASS_CODES.has(code) ? code : ErrorCodes.INTERNAL_ERROR;
55
+ }
56
+
57
+ function safeTool(name) {
58
+ return typeof name === 'string' && /^[a-z0-9_]{1,80}$/.test(name) ? name : 'unknown';
59
+ }
60
+
61
+ function safeCorrelation(id) {
62
+ return typeof id === 'string' && /^[0-9a-f]{16}$/.test(id) ? id : newCorrelationId();
63
+ }
64
+
65
+ function callerTag() {
66
+ let raw = null;
67
+ try {
68
+ const actor = currentActor();
69
+ if (actor && typeof actor.sourceTag === 'string' && actor.sourceTag) raw = actor.sourceTag;
70
+ } catch { raw = null; }
71
+ const tags = registeredSourceTags();
72
+ if (typeof raw !== 'string' || !tags.includes(raw)) return 'unregistered';
73
+ const clean = scrub(raw);
74
+ if (clean !== raw) return 'unregistered';
75
+ return raw;
76
+ }
77
+
78
+ async function roleOf(db) {
79
+ try {
80
+ const actor = currentActor();
81
+ if (actor.transport === 'stdio') return 'owner';
82
+ if (actor.transport !== 'http' || !actor.personId || !db) return 'guest';
83
+ const person = await getPerson(db, actor.personId);
84
+ return person && person.role === 'owner' ? 'owner' : 'guest';
85
+ } catch {
86
+ return 'guest';
87
+ }
88
+ }
89
+
90
+ function buildEntry(fields) {
91
+ const doc = {
92
+ _id: fields._id || newCorrelationId(),
93
+ t: typeof fields.t === 'string' && fields.t ? fields.t : new Date().toISOString(),
94
+ emet_code: safeCode(fields.emet_code),
95
+ class_code: safeClass(fields.class_code),
96
+ tool: safeTool(fields.tool),
97
+ version: EMET_VERSION,
98
+ transport: fields.transport === 'http' ? 'http' : 'stdio',
99
+ caller: fields.caller === 'unregistered' || typeof fields.caller !== 'string' ? 'unregistered' : (scrub(fields.caller) === fields.caller ? fields.caller : 'unregistered'),
100
+ role: fields.role === 'owner' ? 'owner' : 'guest',
101
+ correlation_id: safeCorrelation(fields.correlation_id),
102
+ count: 1
103
+ };
104
+ const out = {};
105
+ for (const key of ENTRY_KEYS) out[key] = doc[key];
106
+ return out;
107
+ }
108
+
109
+ function dbHandle() {
110
+ if (!MONGODB_URI) return null;
111
+ const client = readySharedClient();
112
+ if (!client) return null;
113
+ return client.db(MONGODB_DB);
114
+ }
115
+
116
+ async function logCollection() {
117
+ const db = await dbHandle();
118
+ if (!db) return null;
119
+ const col = db.collection(ERROR_LOG_COLLECTION);
120
+ if (!indexed) {
121
+ indexed = true;
122
+ await col.createIndex({ t: -1 });
123
+ await col.createIndex({ emet_code: 1, t: -1 });
124
+ await col.createIndex({ correlation_id: 1 }, { unique: true });
125
+ await db.collection(ERROR_LOG_SUMMARY_COLLECTION).createIndex(
126
+ { day: 1, emet_code: 1, tool: 1, version: 1 },
127
+ { unique: true }
128
+ );
129
+ }
130
+ return col;
131
+ }
132
+
133
+ export async function insertErrorLog(fields) {
134
+ const doc = buildEntry(fields);
135
+ if (backend) {
136
+ if (backend.fail) throw new CodedError('error log down', { code: 'EMET-INT-001' });
137
+ backend.entries.push(doc);
138
+ return doc;
139
+ }
140
+ const col = await logCollection();
141
+ if (!col) return doc;
142
+ await col.insertOne(doc);
143
+ return doc;
144
+ }
145
+
146
+ export async function upsertErrorSummary(row) {
147
+ const summary = {
148
+ day: String(row.day || '').slice(0, 10),
149
+ emet_code: safeCode(row.emet_code),
150
+ tool: safeTool(row.tool),
151
+ version: typeof row.version === 'string' ? row.version : EMET_VERSION,
152
+ count: Number(row.count) || 0
153
+ };
154
+ const stored = {};
155
+ for (const key of SUMMARY_KEYS) stored[key] = summary[key];
156
+ if (backend) {
157
+ if (backend.fail) throw new CodedError('error log down', { code: 'EMET-INT-001' });
158
+ const found = backend.summaries.find((s) => s.day === stored.day && s.emet_code === stored.emet_code && s.tool === stored.tool && s.version === stored.version);
159
+ if (found) found.count += stored.count;
160
+ else backend.summaries.push({ ...stored });
161
+ return stored;
162
+ }
163
+ const db = await dbHandle();
164
+ if (!db) return stored;
165
+ await db.collection(ERROR_LOG_SUMMARY_COLLECTION).updateOne(
166
+ { day: stored.day, emet_code: stored.emet_code, tool: stored.tool, version: stored.version },
167
+ { $inc: { count: stored.count } },
168
+ { upsert: true }
169
+ );
170
+ return stored;
171
+ }
172
+
173
+ async function loadRows() {
174
+ if (backend) return backend.entries.map((row) => ({ ...row }));
175
+ const col = await logCollection();
176
+ if (!col) return [];
177
+ return col.find({}, { projection: { t: 1, emet_code: 1, tool: 1, version: 1, count: 1 } }).toArray();
178
+ }
179
+
180
+ export async function rotateErrorLog(options = {}) {
181
+ const max = options.maxEntries ?? ERROR_LOG_MAX;
182
+ const days = options.maxAgeDays ?? ERROR_LOG_DAYS;
183
+ const now = options.now instanceof Date ? options.now : new Date();
184
+ const ageCutoff = new Date(now.getTime() - days * 86400000).toISOString();
185
+ const rows = await loadRows();
186
+ const sorted = [...rows].sort((a, b) => (a.t < b.t ? 1 : a.t > b.t ? -1 : String(a._id) < String(b._id) ? 1 : -1));
187
+ const keep = new Set(sorted.slice(0, max).map((row) => row._id));
188
+ const removed = sorted.filter((row) => row.t < ageCutoff || !keep.has(row._id));
189
+ if (!removed.length) return { removed: 0 };
190
+ let day = ageCutoff.slice(0, 10);
191
+ if (removed.some((row) => row.t >= ageCutoff)) {
192
+ const kept = sorted.filter((row) => !removed.includes(row));
193
+ const oldest = kept[kept.length - 1];
194
+ if (oldest) day = String(oldest.t).slice(0, 10);
195
+ }
196
+ const groups = new Map();
197
+ for (const row of removed) {
198
+ const key = `${String(row.t).slice(0, 10)}\0${row.emet_code}\0${row.tool}\0${row.version}`;
199
+ const group = groups.get(key) || {
200
+ day: String(row.t).slice(0, 10),
201
+ emet_code: row.emet_code,
202
+ tool: row.tool,
203
+ version: row.version,
204
+ count: 0
205
+ };
206
+ group.count += Number(row.count) || 1;
207
+ groups.set(key, group);
208
+ }
209
+ for (const group of groups.values()) await upsertErrorSummary(group);
210
+ const ids = removed.map((row) => row._id);
211
+ if (backend) {
212
+ const drop = new Set(ids);
213
+ backend.entries = backend.entries.filter((row) => !drop.has(row._id));
214
+ } else {
215
+ const col = await logCollection();
216
+ if (col) await col.deleteMany({ _id: { $in: ids } });
217
+ }
218
+ console.error(`rotated ${removed.length} entries older than ${day}`);
219
+ return { removed: removed.length, day };
220
+ }
221
+
222
+ export async function queryErrorLog({ since, correlation_id, codes } = {}) {
223
+ let rows;
224
+ if (backend) rows = backend.entries.map((row) => ({ ...row }));
225
+ else {
226
+ const col = await logCollection();
227
+ rows = col ? await col.find({}).toArray() : [];
228
+ }
229
+ if (correlation_id) rows = rows.filter((row) => row.correlation_id === correlation_id);
230
+ else {
231
+ if (since) rows = rows.filter((row) => row.t >= since);
232
+ if (Array.isArray(codes) && codes.length) {
233
+ const wanted = new Set(codes);
234
+ rows = rows.filter((row) => wanted.has(row.emet_code));
235
+ }
236
+ }
237
+ rows.sort((a, b) => (a.t < b.t ? 1 : a.t > b.t ? -1 : 0));
238
+ return rows;
239
+ }
240
+
241
+ export async function recordError(input = {}) {
242
+ const job = (async () => {
243
+ try {
244
+ const db = backend ? null : await dbHandle();
245
+ const actor = currentActor();
246
+ await insertErrorLog({
247
+ t: new Date().toISOString(),
248
+ emet_code: input.emet_code,
249
+ class_code: input.class_code,
250
+ tool: input.tool,
251
+ correlation_id: input.correlation_id,
252
+ transport: actor.transport === 'http' ? 'http' : 'stdio',
253
+ caller: callerTag(),
254
+ role: await roleOf(db)
255
+ });
256
+ } catch {
257
+ console.error('error log write failed');
258
+ }
259
+ })();
260
+ chain = chain.then(() => job, () => job);
261
+ return job;
262
+ }
263
+
264
+ export function startErrorLogRotation() {
265
+ const run = () => {
266
+ rotateErrorLog().catch(() => { console.error('error log write failed'); });
267
+ };
268
+ const kick = setTimeout(run, 1000);
269
+ if (kick.unref) kick.unref();
270
+ const timer = setInterval(run, 24 * 60 * 60 * 1000);
271
+ if (timer.unref) timer.unref();
272
+ return timer;
273
+ }
@@ -0,0 +1,245 @@
1
+ /**
2
+ * Owner-only error report. This module does not send mail and does not
3
+ * open a network connection. A calling assistant may pass the structured
4
+ * email to its own mail tool only after the user has seen the report and agreed.
5
+ */
6
+
7
+ import crypto from 'node:crypto';
8
+ import { ValidationError } from './validation.js';
9
+ import { currentActor, getPerson } from './persons.js';
10
+ import { EMET_VERSION } from './version.js';
11
+ import { ERROR_CODES, EMET_CODE_RE } from './error-codes.js';
12
+ import { redactText } from './redact.js';
13
+ import { queryErrorLog } from './error-log.js';
14
+
15
+ export const SEND_INSTRUCTION = 'The calling assistant may send this report only after showing the report to its user and getting their OK.';
16
+ export const DEFAULT_REPORT_EMAIL = 'reports@example.com';
17
+ export const MAILTO_MAX = 1800;
18
+
19
+ /**
20
+ * One plain address: exactly one @. The local part is letters, digits, and
21
+ * . _ + - , and it does not start or end with a dot or contain "..".
22
+ * The domain is two or more labels separated by dots. A label is letters,
23
+ * digits, and -, and it does not start or end with -.
24
+ */
25
+ export function plainReportAddress(value) {
26
+ if (typeof value !== 'string' || value.length === 0) return false;
27
+ const at = value.indexOf('@');
28
+ if (at < 1 || at !== value.lastIndexOf('@') || at === value.length - 1) return false;
29
+ const local = value.slice(0, at);
30
+ const domain = value.slice(at + 1);
31
+ if (!/^[A-Za-z0-9._+-]+$/.test(local)) return false;
32
+ if (local.startsWith('.') || local.endsWith('.') || local.includes('..')) return false;
33
+ const labels = domain.split('.');
34
+ if (labels.length < 2) return false;
35
+ const labelOk = /^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/;
36
+ if (labels.some((label) => !labelOk.test(label))) return false;
37
+ return true;
38
+ }
39
+
40
+ export function reportEmailAddress() {
41
+ if (!plainReportAddress(DEFAULT_REPORT_EMAIL)) return '';
42
+ if (process.env.EMET_REPORT_EMAIL == null) return DEFAULT_REPORT_EMAIL;
43
+ const raw = process.env.EMET_REPORT_EMAIL;
44
+ if (raw === '' || !plainReportAddress(raw)) return '';
45
+ return raw;
46
+ }
47
+
48
+ export async function assertReportOwner(db) {
49
+ const actor = currentActor();
50
+ if (actor.transport === 'stdio') return;
51
+ const person = actor.transport === 'http' && actor.personId && db ? await getPerson(db, actor.personId) : null;
52
+ if (!person || person.role !== 'owner') {
53
+ throw new ValidationError('The error report is owner-only.', { code: 'EMET-AUTH-038' });
54
+ }
55
+ }
56
+
57
+ export function validateReportArgs(args = {}, now = new Date()) {
58
+ const out = { since: null, codes: null, correlation_id: null, limit: 50, now };
59
+ if (args.correlation_id != null && args.correlation_id !== '') {
60
+ if (typeof args.correlation_id !== 'string' || !/^[0-9a-f]{16}$/.test(args.correlation_id)) {
61
+ throw new ValidationError('correlation_id must be 16 hex characters.', { code: 'EMET-TOOL-013' });
62
+ }
63
+ out.correlation_id = args.correlation_id;
64
+ }
65
+ if (args.limit != null) {
66
+ if (typeof args.limit !== 'number' || !Number.isInteger(args.limit) || args.limit < 1 || args.limit > 200) {
67
+ throw new ValidationError('limit must be a whole number from 1 to 200.', { code: 'EMET-TOOL-012' });
68
+ }
69
+ out.limit = args.limit;
70
+ }
71
+ if (args.codes != null) {
72
+ if (!Array.isArray(args.codes) || args.codes.some((code) => typeof code !== 'string' || !EMET_CODE_RE.test(code))) {
73
+ throw new ValidationError('codes must be a list of EMET codes.', { code: 'EMET-TOOL-011' });
74
+ }
75
+ out.codes = args.codes;
76
+ }
77
+ if (!out.correlation_id) {
78
+ let since;
79
+ if (args.since == null || args.since === '') {
80
+ since = new Date(now.getTime() - 7 * 86400000);
81
+ } else if (typeof args.since !== 'string' || !/^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z)?$/.test(args.since)) {
82
+ throw new ValidationError('since must be an ISO date.', { code: 'EMET-TOOL-009' });
83
+ } else {
84
+ since = new Date(args.since.length === 10 ? `${args.since}T00:00:00.000Z` : args.since);
85
+ if (Number.isNaN(since.getTime())) throw new ValidationError('since must be an ISO date.', { code: 'EMET-TOOL-009' });
86
+ }
87
+ const oldest = now.getTime() - 90 * 86400000;
88
+ if (since.getTime() < oldest - 1000) {
89
+ throw new ValidationError('since is older than 90 days.', { code: 'EMET-TOOL-010' });
90
+ }
91
+ out.since = since.toISOString();
92
+ }
93
+ return out;
94
+ }
95
+
96
+ function shown(entry) {
97
+ return {
98
+ t: redactText(String(entry.t || '')),
99
+ emet_code: redactText(String(entry.emet_code || '')),
100
+ tool: redactText(String(entry.tool || '')),
101
+ transport: entry.transport === 'http' ? 'http' : 'stdio',
102
+ caller: redactText(String(entry.caller || 'unregistered')),
103
+ role: entry.role === 'owner' ? 'owner' : 'guest',
104
+ correlation_id: redactText(String(entry.correlation_id || ''))
105
+ };
106
+ }
107
+
108
+ function totalsOf(rows) {
109
+ const map = new Map();
110
+ for (const row of rows) {
111
+ const code = row.emet_code;
112
+ const cur = map.get(code) || { emet_code: code, count: 0, tools: new Map() };
113
+ cur.count += 1;
114
+ cur.tools.set(row.tool, (cur.tools.get(row.tool) || 0) + 1);
115
+ map.set(code, cur);
116
+ }
117
+ return [...map.values()]
118
+ .sort((a, b) => b.count - a.count || (a.emet_code < b.emet_code ? -1 : 1))
119
+ .map((row) => {
120
+ let tool = 'unknown';
121
+ let best = -1;
122
+ for (const [name, count] of row.tools) {
123
+ if (count > best) { tool = name; best = count; }
124
+ }
125
+ const known = ERROR_CODES[row.emet_code];
126
+ return { emet_code: row.emet_code, summary: known ? known.summary : row.emet_code, count: row.count, tool };
127
+ });
128
+ }
129
+
130
+ function assemble({ version, shortId, generated, windowStart, windowEnd, nodeVersion, totals, recent, notes }) {
131
+ const lines = [];
132
+ lines.push(`EMET error report ${version} ${shortId}`);
133
+ lines.push(`Generated: ${generated} EMET ${version} Node ${nodeVersion}`);
134
+ lines.push(`Window: ${windowStart} to ${windowEnd} (UTC)`);
135
+ lines.push('');
136
+ lines.push('Totals');
137
+ if (!totals.length) lines.push(' (none)');
138
+ for (const row of totals) lines.push(` ${row.emet_code} ${row.summary} ${row.count} ${row.tool}`);
139
+ for (const note of notes) lines.push(note);
140
+ lines.push('');
141
+ lines.push('Most recent');
142
+ if (!recent.length) lines.push(' (none)');
143
+ for (const entry of recent) {
144
+ lines.push(` ${entry.t} ${entry.emet_code} ${entry.tool} ${entry.transport} ${entry.caller} ${entry.role} ${entry.correlation_id}`);
145
+ }
146
+ lines.push('');
147
+ lines.push('Contains: codes, times, tool names, versions, caller tags, ids.');
148
+ lines.push('Does not contain: memory, documents, arguments, names, tokens.');
149
+ lines.push('You are sending this yourself. EMET has not sent it anywhere.');
150
+ return lines.join('\n');
151
+ }
152
+
153
+ function mailtoLink(to, subject, body) {
154
+ const text = String(body).replace(/\r?\n/g, '\r\n');
155
+ return `mailto:${to}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(text)}`;
156
+ }
157
+
158
+ export function fitReportEmail({ to, subject, base }) {
159
+ const full = assemble({ ...base, notes: [] });
160
+ const fullLink = mailtoLink(to, subject, full);
161
+ if (fullLink.length <= MAILTO_MAX) {
162
+ return { body: full, mailto: fullLink, truncated: false, entries_in_link: base.recent.length, report: full };
163
+ }
164
+ const listed = base.recent.length;
165
+ for (let count = listed - 1; count >= 1; count--) {
166
+ const notes = [`Truncated: showing the ${count} most recent of ${listed} entries. The full report is in the tool output above.`];
167
+ const body = assemble({ ...base, recent: base.recent.slice(0, count), notes });
168
+ const link = mailtoLink(to, subject, body);
169
+ if (link.length <= MAILTO_MAX) return { body, mailto: link, truncated: true, entries_in_link: count, report: full };
170
+ }
171
+ for (let keep = base.totals.length; keep >= 0; keep--) {
172
+ const omitted = base.totals.length - keep;
173
+ const notes = [];
174
+ if (listed) notes.push(`Truncated: showing none of the ${listed} most recent entries. The full report is in the tool output above.`);
175
+ if (omitted) notes.push(`Truncated: totals omit ${omitted} codes. The full report is in the tool output above.`);
176
+ const body = assemble({ ...base, totals: base.totals.slice(0, keep), recent: [], notes });
177
+ const link = mailtoLink(to, subject, body);
178
+ if (link.length <= MAILTO_MAX) return { body, mailto: link, truncated: true, entries_in_link: 0, report: full };
179
+ }
180
+ const body = assemble({ ...base, totals: [], recent: [], notes: ['Truncated: the full report is in the tool output above.'] });
181
+ const link = mailtoLink(to, subject, body);
182
+ if (link.length <= MAILTO_MAX) {
183
+ return { body, mailto: link, truncated: true, entries_in_link: 0, report: full };
184
+ }
185
+ return {
186
+ body,
187
+ mailto: null,
188
+ reason: 'The mailto link would exceed 1800 characters.',
189
+ truncated: true,
190
+ entries_in_link: 0,
191
+ report: full
192
+ };
193
+ }
194
+
195
+ function windowLabel(rows, since, now) {
196
+ if (rows.length) {
197
+ const oldest = rows[rows.length - 1].t.slice(0, 10);
198
+ const newest = rows[0].t.slice(0, 10);
199
+ return [oldest, newest];
200
+ }
201
+ const end = now.toISOString().slice(0, 10);
202
+ const start = since ? since.slice(0, 10) : end;
203
+ return [start, end];
204
+ }
205
+
206
+ export function composeErrorReport(rows, { since, now, shortId, limit = rows.length, nodeVersion = process.versions.node } = {}) {
207
+ const version = EMET_VERSION;
208
+ const generated = now.toISOString().replace(/\.\d{3}Z$/, 'Z');
209
+ const [windowStart, windowEnd] = windowLabel(rows, since, now);
210
+ const shownRows = rows.map(shown);
211
+ const recent = shownRows.slice(0, limit);
212
+ const totals = totalsOf(shownRows);
213
+ const base = { version, shortId, generated, windowStart, windowEnd, nodeVersion, totals, recent };
214
+ const report = assemble({ ...base, notes: [] });
215
+ const to = reportEmailAddress();
216
+ const subject = `EMET error report ${version} ${shortId}`;
217
+ if (to === '') {
218
+ return { report, email: null, instruction: SEND_INSTRUCTION };
219
+ }
220
+ const fitted = fitReportEmail({ to, subject, base });
221
+ const email = {
222
+ to,
223
+ subject,
224
+ body_text: fitted.report,
225
+ short_id: shortId,
226
+ version,
227
+ body: fitted.body,
228
+ mailto: fitted.mailto,
229
+ truncated: fitted.truncated,
230
+ entries_in_link: fitted.entries_in_link,
231
+ entries_total: shownRows.length
232
+ };
233
+ if (fitted.mailto == null) email.reason = fitted.reason;
234
+ return { report: fitted.report, email, instruction: SEND_INSTRUCTION };
235
+ }
236
+
237
+ export async function runErrorReport(args = {}, dbManager = null) {
238
+ const db = dbManager && dbManager.db ? dbManager.db : (dbManager && dbManager.collection ? dbManager : null);
239
+ await assertReportOwner(db);
240
+ const now = args.now instanceof Date ? args.now : new Date();
241
+ const query = validateReportArgs(args, now);
242
+ const rows = await queryErrorLog(query);
243
+ const shortId = crypto.randomBytes(3).toString('hex');
244
+ return composeErrorReport(rows, { since: query.since, now, shortId, limit: query.limit });
245
+ }