@volter/teams 0.1.34 → 0.1.35

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/teams",
3
- "version": "0.1.34",
3
+ "version": "0.1.35",
4
4
  "type": "module",
5
5
  "description": "Volter Teams, the neutral team plane: machine identity and daemon core, the local door and server link, admission from machine grants, product doors, and the Teams server's neutral routes (company RFC 0008).",
6
6
  "license": "MIT",
package/server/core.mjs CHANGED
@@ -64,8 +64,8 @@ export async function maintain(api){const now=api.clock.nowMs();
64
64
  // they go (a backlog of them filled the database).
65
65
  const parts=await api.store.query(`SELECT p.team_id,p.capture_id,p.part_index FROM capture_parts p JOIN captures c ON c.team_id=p.team_id AND c.id=p.capture_id WHERE c.state='committed' LIMIT ${PART_PRUNE_BATCH}`,[]);
66
66
  for(let at=0;at<parts.length;at+=50)await api.store.transaction(parts.slice(at,at+50).map(({team_id,capture_id,part_index})=>({sql:'DELETE FROM capture_parts WHERE team_id=? AND capture_id=? AND part_index=?',params:[team_id,capture_id,part_index]})));
67
- // Every capture writes its session a new version with a full copy of its messages, so versions pile up (hundreds
68
- // per session) until listing them outgrew a Worker's memory. Kept: each session's current version, the newest
67
+ // Every capture writes its session a new version (its messages are carried, not copied: schema 9), so versions pile
68
+ // up (hundreds per session) until listing them outgrew a Worker's memory. Kept: each session's current version, the newest
69
69
  // version from each of its sources (the next capture and a correlation read that one), and every version an
70
70
  // unexpired list, search or history cursor names. The rest go, a bounded batch per run.
71
71
  const stale=await api.store.query(`SELECT v.team_id,v.session_id,v.revision FROM session_versions v JOIN sessions s ON s.team_id=v.team_id AND s.id=v.session_id
@@ -74,7 +74,12 @@ export async function maintain(api){const now=api.clock.nowMs();
74
74
  AND NOT EXISTS (SELECT 1 FROM cursors c, json_each(c.snapshot_json) j WHERE c.team_id=v.team_id AND c.kind IN ('list','search') AND c.expires_at>? AND json_extract(j.value,'$[0]')=v.session_id AND json_extract(j.value,'$[1]')=v.revision)
75
75
  AND NOT EXISTS (SELECT 1 FROM cursors c WHERE c.team_id=v.team_id AND c.kind='history' AND c.expires_at>? AND json_extract(c.snapshot_json,'$.session_id')=v.session_id AND json_extract(c.snapshot_json,'$.revision')=v.revision)
76
76
  LIMIT ${VERSION_PRUNE_BATCH}`,[now,now]);
77
- for(const {team_id,session_id,revision} of stale)await api.store.transaction(['messages','session_artifacts','session_versions'].map(table=>({sql:`DELETE FROM ${table} WHERE team_id=? AND session_id=? AND revision=?`,params:[team_id,session_id,revision]})));
77
+ // A message row lives from the version that wrote it until one that changed it (schema 9), so a row a later version
78
+ // still holds moves to the next version that holds it before its own goes; the rest go with the version.
79
+ for(const {team_id,session_id,revision} of stale)await api.store.transaction([
80
+ {sql:'UPDATE messages SET revision=(SELECT MIN(k.revision) FROM session_versions k WHERE k.team_id=messages.team_id AND k.session_id=messages.session_id AND k.revision>messages.revision AND k.revision<COALESCE(messages.until_revision,messages.revision+1)) WHERE team_id=? AND session_id=? AND revision=? AND EXISTS (SELECT 1 FROM session_versions k WHERE k.team_id=messages.team_id AND k.session_id=messages.session_id AND k.revision>messages.revision AND k.revision<COALESCE(messages.until_revision,messages.revision+1))',params:[team_id,session_id,revision]},
81
+ ...['messages','session_artifacts','session_versions'].map(table=>({sql:`DELETE FROM ${table} WHERE team_id=? AND session_id=? AND revision=?`,params:[team_id,session_id,revision]})),
82
+ ]);
78
83
  await api.store.transaction([
79
84
  {sql:'DELETE FROM cursors WHERE expires_at<=?',params:[now]},
80
85
  {sql:'DELETE FROM global_cursors WHERE expires_at<=?',params:[now]},
@@ -0,0 +1,8 @@
1
+ // Schema migration 009 (SQLite DDL), applied in order by store-core migrate().
2
+ export default `-- Schema 9: a message row is written once and read by every version it belongs to. A row is part of each version
3
+ -- of its session from \`revision\` up to, not including, \`until_revision\`: open (OPEN_REVISION) while the session's
4
+ -- current version still holds it, closed at the revision of the version that changed or dropped it. A row without one
5
+ -- (every row written before schema 9) belongs to its own revision only. A capture writes only the messages that
6
+ -- changed; a version that changes no message writes none.
7
+ ALTER TABLE messages ADD COLUMN until_revision INTEGER;
8
+ `;
@@ -7,4 +7,9 @@ import m005 from './005.mjs';
7
7
  import m006 from './006.mjs';
8
8
  import m007 from './007.mjs';
9
9
  import m008 from './008.mjs';
10
- export const MIGRATIONS = [m001, m002, m003, m004, m005, m006, m007, m008];
10
+ import m009 from './009.mjs';
11
+ export const MIGRATIONS = [m001, m002, m003, m004, m005, m006, m007, m008, m009];
12
+ /** A message row's `until_revision` while its session's current version still holds it (schema 9). */
13
+ export const OPEN_REVISION = Number.MAX_SAFE_INTEGER;
14
+ /** SQL: message row `m` belongs to the version at revision ?1 (bind the revision twice). */
15
+ export const MESSAGE_AT = (m = 'm') => `${m}.revision<=? AND ?<COALESCE(${m}.until_revision,${m}.revision+1)`;