Files
tiny-worlds/server/carry.mjs
T

98 lines
4.0 KiB
JavaScript

// server/carry.mjs -- the slidwin: the migrating live range across
// session boundaries.
//
// At stop, the closing session's tail MOVES into the next dormant
// session's carryover -- contiguous raw lines from the end, whole
// exchanges, nothing filtered (thinking, notes, draft variants all
// ride along; each line exists in exactly one jsonl, ever, no
// markers). At start the carryover is consumed into the new log
// behind the bundle; the model's view renders it as ordinary history
// under the standing policy, so the fresh contwin reads as the old
// one continuing and the seam is invisible from inside. The
// housekeeper of the closing session reads its log as it was left --
// the truncation at stop is the bound -- so the rings never overlap
// the carried prose.
//
// The walk is mechanically dumb while the cutoff is smart: only the
// prose the model will see in the new contwin counts against the
// budget. Invisible events stay in the carry but cost nothing.
import { effectiveContents } from './variants.mjs';
/** @typedef {import('./shapes.mjs').LogEvent} LogEvent */
// ~10% of a 256K-class contwin in token terms, grounded on Irene's
// session (84KB of turn content at ~23K prompt tokens). Substrate
// policy, not deployment taste: a constant, not config.
export const CARRY_BUDGET = 96 * 1024;
const STRIP_BLOCKS = /<(status|agenda|conflict)>[\s\S]*?<\/\1>/g;
// the prose the model will see for one log line in the new contwin
/** @param {LogEvent[]} lines @param {Map<number, string>} eff @param {number} i @returns {number} */
function visibleBytes(lines, eff, i) {
const e = lines[i];
if (e.t === 'msg') {
if (e.reminder) return String(e.m.content ?? '').length;
if (e.m.role === 'user' && !e.conflictOnly) {
// old GM turns elide their rides in the view
return String(e.m.content ?? '').replace(STRIP_BLOCKS, '').length;
}
if (e.m.role === 'assistant') {
// variant turns count only the chosen variant; drafts are
// invisible and cost nothing; thinking rides in the message
// and never renders
const c = eff.has(i) ? eff.get(i) : e.m.content;
return String(c ?? '').length;
}
return 0; // conflict-only traffic elides once the story moves on
}
if (e.t === 'tool') return String(e.m.content ?? '').length; // conservative: the full result, never the stub
return 0; // notes and bundle: machinery, never model prose
}
// The tail's start index: lines[start..] are the carryover, the head
// [0..start) stays in the log for the housekeeper. Whole exchanges
// from the end, most recent first, added while they fit the budget.
// A dangling GM turn (the session stopped before her reply) rides in
// the carry whenever any exchange does -- it is contiguous with the
// tail and costs nothing -- and that is load-bearing: a carryover
// ending on a GM turn auto-sends at start, so the exchange continues
// instead of leaving her cue behind in the dead log. When nothing
// fits (the degenerate empty tail) it stays behind and files
// normally.
/** @param {LogEvent[]} lines @returns {number} */
export function tailStart(lines) {
const eff = effectiveContents(lines);
const exchanges = [];
let i = lines.length - 1;
while (i >= 0) {
const e = lines[i];
if (e.t === 'msg' && e.m?.role === 'assistant' && !e.m.tool_calls?.length) {
// the exchange opens at the preceding real GM turn (a
// conflict heal belongs to the exchange it healed)
let j = i - 1;
while (j >= 0) {
const u = lines[j];
if (u.t === 'msg' && u.m?.role === 'user' && !u.conflictOnly && !u.reminder) break;
j--;
}
if (j >= 0) {
exchanges.push([j, i]);
i = j;
}
}
i--;
}
let bytes = 0;
let start = lines.length; // the empty tail is a valid degenerate case
for (const [a, b] of exchanges) {
let cost = 0;
for (let k = a; k <= b; k++) cost += visibleBytes(lines, eff, k);
if (bytes + cost > CARRY_BUDGET) break;
bytes += cost;
start = a;
}
return start;
}