@dreamforgestudiollc/emet 0.3.2 → 0.3.4

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/src/session.js CHANGED
@@ -38,6 +38,7 @@ import { LAYERS } from './layers.js';
38
38
  import { parseMetadata } from './metadata.js';
39
39
  import { applyCloseCap, scopeFor, escalateSpeak, CLOSE_CAP } from './lessons-to-checks.js';
40
40
  import { isBotTag, botHandoffDoc, botRecordDir, mainRecordIds, echoId } from './session-graph.js';
41
+ import { hasCarryForward, runCarryForwardClose, supersededStrikeSkipped, foreignContinuityProblems } from './carry-forward-close.js';
41
42
 
42
43
  const DOCS_COLLECTION = 'documents';
43
44
  const TRANSCRIPTS_COLLECTION = 'transcripts';
@@ -700,6 +701,7 @@ export async function sessionClose(options = {}) {
700
701
  try {
701
702
  const conn = await connect();
702
703
  client = conn.client;
704
+ if (hasCarryForward(options)) return await runCarryForwardClose(conn.db, options);
703
705
  // Baselines are bookkeeping on the document row, not a new revision.
704
706
  // A writeDoc here would archive a revision that changed nothing the user wrote.
705
707
  const cutoff = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
@@ -783,7 +785,7 @@ export async function sessionClose(options = {}) {
783
785
  const readDoc = async (docId) => {
784
786
  const found = await docs.findOne(
785
787
  { doc_id: docId },
786
- { projection: { doc_id: 1, version: 1, updated_at: 1, updated_by: 1, size: 1 } }
788
+ { projection: { doc_id: 1, version: 1, updated_at: 1, updated_by: 1, size: 1, session_id: 1 } }
787
789
  );
788
790
  const updatedMs = found ? toMillis(found.updated_at) : null;
789
791
  return {
@@ -792,13 +794,17 @@ export async function sessionClose(options = {}) {
792
794
  version: found ? found.version : null,
793
795
  updated_at: found && found.updated_at ? new Date(updatedMs).toISOString() : null,
794
796
  updated_by: found ? found.updated_by || null : null,
797
+ session_id: found && typeof found.session_id === 'string' ? found.session_id : null,
795
798
  in_window: updatedMs !== null && updatedMs >= startMs
796
799
  };
797
800
  };
798
801
  const documents = [];
802
+ const continuityWriters = new Map();
799
803
  const attributionWarnings = [];
800
804
  for (const docId of wanted) {
801
- const { in_window, ...d } = await readDoc(docId);
805
+ const read = await readDoc(docId);
806
+ continuityWriters.set(docId, read.session_id);
807
+ const { in_window, session_id: _writerSession, ...d } = read;
802
808
  if (bot) {
803
809
  // Revised this session = in the window AND by this session's tag.
804
810
  documents.push({ ...d, updated_this_session: in_window && d.updated_by === bot.tag, updated_in_window: in_window });
@@ -817,7 +823,8 @@ export async function sessionClose(options = {}) {
817
823
  const mainRecords = [];
818
824
  if (bot) {
819
825
  for (const docId of bot.main_records) {
820
- const { in_window, ...d } = await readDoc(docId);
826
+ const read = await readDoc(docId);
827
+ const { in_window, session_id: _writerSession, ...d } = read;
821
828
  mainRecords.push({ ...d, revised_by_session_tag: in_window && d.updated_by === bot.tag });
822
829
  }
823
830
  }
@@ -981,6 +988,17 @@ export async function sessionClose(options = {}) {
981
988
  ...(bot ? { bot } : {})
982
989
  });
983
990
  for (const p of receiptCheck.problems) missing.push(p);
991
+ const foreign = foreignContinuityProblems({
992
+ receipts,
993
+ writers: continuityWriters,
994
+ handoffDoc: sessionHandoff,
995
+ sessionBase: transcript && transcript.base_session_id ? transcript.base_session_id : null,
996
+ documents
997
+ });
998
+ for (const p of foreign) {
999
+ receiptCheck.problems.push(p);
1000
+ missing.push(p);
1001
+ }
984
1002
  if (uncorroborated) {
985
1003
  missing.push(
986
1004
  'transcript coverage is uncorroborated - no layer entry written this session cites the transcript. ' +
@@ -1003,9 +1021,19 @@ export async function sessionClose(options = {}) {
1003
1021
  const row = await attempts.findOne({ base_session_id: baseId }, { projection: { blocked_count: 1 } });
1004
1022
  priorBlocked = row && typeof row.blocked_count === 'number' ? row.blocked_count : 0;
1005
1023
  }
1006
- const capped = applyCloseCap(computed, priorBlocked);
1024
+ const strikeSkipped = computed === 'blocked' && supersededStrikeSkipped(receiptCheck.problems, {
1025
+ writers: continuityWriters,
1026
+ handoffDoc: sessionHandoff,
1027
+ sessionBase: baseId
1028
+ });
1029
+ // A later session advanced HANDOFF or STATE. The receipt close stays
1030
+ // blocked. It does not count, and it does not escalate, including when
1031
+ // an earlier retry of this same mismatch already filled the cap.
1032
+ const capped = strikeSkipped
1033
+ ? { state: 'blocked', blocked_count: priorBlocked, escalated: false, strike_skipped: true }
1034
+ : applyCloseCap(computed, priorBlocked);
1007
1035
  const state = capped.state;
1008
- if (baseId && computed === 'blocked' && capped.blocked_count !== priorBlocked) {
1036
+ if (baseId && computed === 'blocked' && !strikeSkipped && capped.blocked_count !== priorBlocked) {
1009
1037
  await attempts.updateOne(
1010
1038
  { base_session_id: baseId },
1011
1039
  { $set: { blocked_count: capped.blocked_count, last_state: state, updated_at: new Date() },
@@ -1027,11 +1055,21 @@ export async function sessionClose(options = {}) {
1027
1055
  });
1028
1056
 
1029
1057
  if (state === 'escalate') speak.push(...escalateSpeak(missing));
1058
+ if (strikeSkipped) {
1059
+ const note = 'HANDOFF or STATE was written by a later session, so this receipt close stays blocked and is not counted. Close with carry_forward. Do not retry this receipt close.';
1060
+ warnings.push(note);
1061
+ speak.push(note);
1062
+ }
1030
1063
 
1031
1064
  return {
1032
1065
  state,
1033
1066
  speak,
1034
- close_attempts: { blocked_count: capped.blocked_count, cap: CLOSE_CAP, escalated: state === 'escalate' },
1067
+ close_attempts: {
1068
+ blocked_count: capped.blocked_count,
1069
+ cap: CLOSE_CAP,
1070
+ escalated: state === 'escalate',
1071
+ ...(strikeSkipped ? { strike_skipped: true, counted: false } : {})
1072
+ },
1035
1073
  scope: scoped,
1036
1074
  window,
1037
1075
  checked: {
@@ -1075,6 +1113,11 @@ export async function sessionClose(options = {}) {
1075
1113
  'Say the `speak` lines, including the list, and wait for the user\'s word. The counter does not reset ' +
1076
1114
  'on a changed receipt; a close that would be complete is still accepted if the user directs the fix. ' +
1077
1115
  'Each line of `missing` has a `scope` in `scope`: touch only what it names.'
1116
+ : strikeSkipped
1117
+ ? 'The session is NOT saved. HANDOFF or STATE moved because a later session wrote them, so these receipts cannot match. ' +
1118
+ 'This blocked close was not counted, and it does not escalate. Do not retry this receipt close. ' +
1119
+ 'Close with carry_forward: the owner door, or the credential that wrote this session, cites the current records/ disposition and the later HANDOFF and STATE versions. ' +
1120
+ 'Transcripts stay. A declared source is not authority.'
1078
1121
  : state === 'blocked'
1079
1122
  ? 'The session is NOT saved. The store holds the artifacts, but the close is BLOCKED: a receipt is ' +
1080
1123
  'missing or disagrees with the store, the handoff was not the last write, or the transcript is not ' +
package/src/tools.js CHANGED
@@ -2243,6 +2243,36 @@ async function subkeyHandleForOpen(actor, base, now) {
2243
2243
  }
2244
2244
  }
2245
2245
 
2246
+ function exchangeIndexes(exchanges) {
2247
+ if (!Array.isArray(exchanges)) return [];
2248
+ const out = [];
2249
+ for (const row of exchanges) {
2250
+ const n = Number(row && row.exchange_index);
2251
+ if (Number.isInteger(n) && n >= 1) out.push(n);
2252
+ }
2253
+ return out;
2254
+ }
2255
+
2256
+ /** Indexes already stored under other ids of this session. The current part is not included. */
2257
+ async function exchangeIndexesOnOtherParts(col, sessionId) {
2258
+ const { partId, partNumber, baseFromPart } = await import('./transcript-session.js');
2259
+ const base = baseFromPart(sessionId);
2260
+ const ids = [];
2261
+ if (sessionId !== base) ids.push(base);
2262
+ const n = partNumber(sessionId);
2263
+ for (let i = 1; i < n; i++) {
2264
+ const id = partId(base, i);
2265
+ if (id !== sessionId) ids.push(id);
2266
+ }
2267
+ const found = [];
2268
+ for (const id of ids) {
2269
+ const doc = await col.findOne({ session_id: id, superseded_by: { $in: [null, undefined] } })
2270
+ || await col.findOne({ session_id: id });
2271
+ found.push(...exchangeIndexes(doc && doc.exchanges));
2272
+ }
2273
+ return found;
2274
+ }
2275
+
2246
2276
  export async function transcriptAppend(args = {}, logger = null) {
2247
2277
  const session_id = args.session_id;
2248
2278
  const user_message = args.user_message;
@@ -2304,20 +2334,32 @@ export async function transcriptAppend(args = {}, logger = null) {
2304
2334
  row.source_basis = 'subkey';
2305
2335
  }
2306
2336
 
2307
- // Build 1 (2026-09-27): the first append of a session must carry the
2308
- // measured machine line, or say there is no local shell. The rule lived in
2309
- // host instructions and was skipped twice on 2026-09-26; the server now
2310
- // refuses to start a transcript without it. Known names come from the
2311
- // user's install notes; with none, the check is reported as not enforced.
2337
+ // Build 1 (2026-09-27): the first successful append of a session must carry
2338
+ // the measured machine line, or say there is no local shell. The rule lived
2339
+ // in host instructions and was skipped twice on 2026-09-26. On 2026-11-03
2340
+ // the door ran only when the index was 1, so a refused exchange 1 was
2341
+ // followed by an accepted exchange 2 and the transcript opened without the
2342
+ // line. The check now runs on any append that opens the transcript, and on
2343
+ // any append that would become the lowest stored index. Known names come
2344
+ // from the user's install notes; with none, the check is reported as not
2345
+ // enforced.
2312
2346
  let machineCheck = null;
2313
- if (!live && partNumber(part) === 1 && idx === 1) {
2314
- const { knownMachinesFromInstallNotes, machineLineCheck } = await import('./lessons-to-checks.js');
2315
- const { installDoc } = await import('./setup.js');
2316
- const notes = await client.db(MONGODB_DB).collection('documents')
2317
- .findOne({ doc_id: installDoc(), retired: { $ne: true } }, { projection: { content: 1 } });
2318
- machineCheck = machineLineCheck(assistant_message, knownMachinesFromInstallNotes(notes && notes.content));
2319
- if (!machineCheck.ok) throw new ValidationError(machineCheck.reason, { code: 'EMET-MEM-018' });
2347
+ const here = exchangeIndexes(exchanges);
2348
+ const mightOpen = here.length === 0 || idx < Math.min(...here);
2349
+ if (mightOpen) {
2350
+ const { machineLineRequired, knownMachinesFromInstallNotes, machineLineCheck } = await import('./lessons-to-checks.js');
2351
+ const stored = here.concat(await exchangeIndexesOnOtherParts(col, part));
2352
+ if (machineLineRequired(stored, idx)) {
2353
+ const { installDoc } = await import('./setup.js');
2354
+ const notes = await client.db(MONGODB_DB).collection('documents')
2355
+ .findOne({ doc_id: installDoc(), retired: { $ne: true } }, { projection: { content: 1 } });
2356
+ machineCheck = machineLineCheck(assistant_message, knownMachinesFromInstallNotes(notes && notes.content));
2357
+ if (!machineCheck.ok) throw new ValidationError(machineCheck.reason, { code: 'EMET-MEM-018' });
2358
+ }
2320
2359
  }
2360
+ const machineLineField = () => (machineCheck
2361
+ ? { machine_line: machineCheck.enforced ? `checked: ${machineCheck.matched}` : machineCheck.reason }
2362
+ : {});
2321
2363
 
2322
2364
  if (!live) {
2323
2365
  const start = typeof session_start === 'number' ? session_start : Date.now() / 1000;
@@ -2334,7 +2376,7 @@ export async function transcriptAppend(args = {}, logger = null) {
2334
2376
  session_id: part, exchange_index: idx, exchange_count: 1, rolled: part !== session_id,
2335
2377
  grant_id: appendStamp.grant_id,
2336
2378
  ...(appendClaim.warning ? { claimed_tag_warning: appendClaim.warning } : {}),
2337
- ...(machineCheck ? { machine_line: machineCheck.enforced ? `checked: ${machineCheck.matched}` : machineCheck.reason } : {})
2379
+ ...machineLineField()
2338
2380
  };
2339
2381
  }
2340
2382
 
@@ -2350,7 +2392,8 @@ export async function transcriptAppend(args = {}, logger = null) {
2350
2392
  rolled: part !== session_id,
2351
2393
  next_part_after: EXCHANGES_PER_PART,
2352
2394
  grant_id: appendStamp.grant_id,
2353
- ...(appendClaim.warning ? { claimed_tag_warning: appendClaim.warning } : {})
2395
+ ...(appendClaim.warning ? { claimed_tag_warning: appendClaim.warning } : {}),
2396
+ ...machineLineField()
2354
2397
  };
2355
2398
  } finally {
2356
2399
  await client.close();
@@ -4317,7 +4360,7 @@ const TOOL_DEFINITIONS = [
4317
4360
  },
4318
4361
  {
4319
4362
  name: "emet_transcript_append",
4320
- description: "Append ONE exchange to the session transcript. Call after every reply. Idempotent on exchange_index. Rolls to a new part after 8 exchanges. After a complete close the session takes exactly ONE more append - its closing exchange, numbered next, within EMET_CLOSE_GRACE_MINUTES (default 5) of the close; it is written into the session's last part whatever session_id it names, and the result's `closing_exchange` says it was accepted and that the session is now locked. A retry of that exchange number is answered as already saved and writes nothing. exchange_index, when given, must be a whole number of 1 or more (a number or its text). Any other append to a closed session is refused and nothing is written; that exchange belongs in the next session you open the usual way. THE FIRST APPEND OF A SESSION IS CHECKED: its assistant_message must carry the measured machine line - a machine name from the install notes plus the clock reading, as the shell returned them - or the words `no local shell MCP`. Without one of those the append is refused and the transcript does not start.",
4363
+ description: "Append ONE exchange to the session transcript. Call after every reply. Idempotent on exchange_index. Rolls to a new part after 8 exchanges. After a complete close the session takes exactly ONE more append - its closing exchange, numbered next, within EMET_CLOSE_GRACE_MINUTES (default 5) of the close; it is written into the session's last part whatever session_id it names, and the result's `closing_exchange` says it was accepted and that the session is now locked. A retry of that exchange number is answered as already saved and writes nothing. exchange_index, when given, must be a whole number of 1 or more (a number or its text). Any other append to a closed session is refused and nothing is written; that exchange belongs in the next session you open the usual way. THE FIRST SUCCESSFUL APPEND OF A SESSION IS CHECKED, at whatever exchange_index opens the transcript or becomes the lowest stored index. A refused exchange 1 does not let a higher index start the transcript. Its assistant_message must carry the measured machine line - a machine name from the install notes plus the clock reading, as the shell returned them - or the words `no local shell MCP`. Without one of those the append is refused and nothing is written.",
4321
4364
  inputSchema: {
4322
4365
  type: "object",
4323
4366
  properties: {
@@ -4600,7 +4643,7 @@ const TOOL_DEFINITIONS = [
4600
4643
  },
4601
4644
  {
4602
4645
  name: "emet_session_close",
4603
- description: "Call this when the session is ending - the user says goodbye, asks you to wrap up, mentions restarting or opening a new session, or you are about to run out of room. An acknowledgement of the turn just completed - thanks, great, perfect - is not a session end; if it is ambiguous, ask rather than close. Checks whether this session's work has actually been written to the store and reports exactly what is missing. It does not write anything itself. RECEIPTS ARE REQUIRED: pass `receipts` naming what you wrote and read back for each close step - working documents, the episodic session record id, the board and its version, the transcript part ids and highest exchange index as query_transcripts returned them, and the handoff version (written last). Each receipt is compared with the store; omitted or mismatched receipts, a handoff that was not the last write, or a transcript no layer entry cites (derived_from) return state 'blocked'. Each missing line carries a `scope`: touch only what it names. THREE blocked closes on one session return state 'escalate': stop retrying, say the `speak` lines, and hand the user the list - the counter does not reset on a changed receipt, only with a new session. Say every line of the returned `speak` to the user, in order - a wrap that leaves it out is a failed close. A records/ session disposition is not a receipt and does not make this call complete. Never tell the user their session is saved unless this returns state 'complete'.",
4646
+ description: "Call this when the session is ending - the user says goodbye, asks you to wrap up, mentions restarting or opening a new session, or you are about to run out of room. An acknowledgement of the turn just completed - thanks, great, perfect - is not a session end; if it is ambiguous, ask rather than close. Checks whether this session's work has actually been written to the store and reports exactly what is missing. It does not write anything itself. RECEIPTS ARE REQUIRED: pass `receipts` naming what you wrote and read back for each close step - working documents, the episodic session record id, the board and its version, the transcript part ids and highest exchange index as query_transcripts returned them, and the handoff version (written last). Each receipt is compared with the store; omitted or mismatched receipts, a handoff that was not the last write, or a transcript no layer entry cites (derived_from) return state 'blocked'. Each missing line carries a `scope`: touch only what it names. THREE blocked closes on one session return state 'escalate': stop retrying, say the `speak` lines, and hand the user the list - the counter does not reset on a changed receipt, only with a new session. Say every line of the returned `speak` to the user, in order - a wrap that leaves it out is a failed close. A records/ session disposition is not a receipt and does not make this call complete. Pass carry_forward only for an old open session whose HANDOFF and STATE a later session already advanced. The owner door, or the credential that wrote the session, cites the current conforming records/ disposition (CARRIED_FORWARD, SUPERSEDED_BY_LATER_SESSION, or ABANDONED_NO_TRANSCRIPT) and those later versions. RETAINED_OPEN_CRITICAL stays open. A declared source is not authority. Transcripts are not deleted. A refused carry_forward is not counted. A receipt close blocked only because a later session holds HANDOFF or STATE stays blocked and is not counted. A same-session close is unchanged. Never tell the user their session is saved unless this returns state 'complete'.",
4604
4647
  inputSchema: {
4605
4648
  type: "object",
4606
4649
  properties: {
@@ -4622,7 +4665,26 @@ const TOOL_DEFINITIONS = [
4622
4665
  session_id: { type: "string", description: "The session_id used (or to be used) for this session's transcript, e.g. '2026-09-03_short-description'. Without it the transcript cannot be checked; receipts.transcript_session_id is used when this is omitted." },
4623
4666
  session_start: { type: "number", description: "Unix seconds when the session began. Defaults to 12 hours ago, which makes the answer approximate." },
4624
4667
  thread_docs: { type: "array", items: { type: "string" }, description: "Additional document ids this session should have updated, e.g. ['threads/some-project.md']." },
4625
- source: { type: "string", description: "Your host's source tag: the registered tag this session was written under. The close is never refused for it; an unregistered tag, one that did not write this session, or none (with no server default) gets a warning that the close is not attributed." }
4668
+ source: { type: "string", description: "Your host's source tag: the registered tag this session was written under. The close is never refused for it; an unregistered tag, one that did not write this session, or none (with no server default) gets a warning that the close is not attributed." },
4669
+ carry_forward: {
4670
+ type: "object",
4671
+ description: "Disposition-backed close for an old open session. Omit it for a normal receipt close. Not a receipt, and not authority by itself.",
4672
+ properties: {
4673
+ disposition_doc: { type: "string", description: "records/ document id of the current conforming session disposition." },
4674
+ disposition_version: { type: "number", description: "Version of that document now in force." },
4675
+ citations: {
4676
+ type: "array",
4677
+ description: "Later continuity records that superseded this session's receipts. A host session cites the current HANDOFF and STATE versions. A bot session cites its own handoff.",
4678
+ items: {
4679
+ type: "object",
4680
+ properties: {
4681
+ doc_id: { type: "string" },
4682
+ version: { type: "number" }
4683
+ }
4684
+ }
4685
+ }
4686
+ }
4687
+ }
4626
4688
  }
4627
4689
  }
4628
4690
  }
package/src/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  /** The server's version, in one place. tests/version.test.js keeps it equal to package.json. */
2
- export const EMET_VERSION = '0.3.2';
2
+ export const EMET_VERSION = '0.3.4';