salvage 2026-11-06: uncommitted work from src/mosaic-stack-rocko-49 (disk cleanup #824)
This commit is contained in:
1 parent
7b166a0f19
commit
bb99a8448c
10 files changed
+211
-52
No files matched your search
+39
-7
@@ -447,15 +447,47 @@ scope probes and rotation/expiry notifications; S2 never probes real services.
|
||||
|
||||
## Schema and verification
|
||||
|
||||
`schema.sql` is unchanged v3b, SHA-256
|
||||
`179ffe356d4ff19a49b5ebad39b6c6bfd7771deb8e1b7c55b5e746f039d69e65`.
|
||||
It has eight tables, **36** triggers and five views. The prototype's displayed
|
||||
35 is after deliberately dropping a guard. Source digest and a trusted full
|
||||
sqlite_master digest are checked at open, alongside schema version `3b`, WAL,
|
||||
`schema.sql` is v3c, SHA-256
|
||||
`7587ac1df813e5148fdb73dc4733ec8064e6a142e4647f0da71be009a73eebdb`.
|
||||
It has nine tables, 40 triggers and five views. Source digest and a trusted full
|
||||
sqlite_master digest are checked at open, alongside schema version `3c`, WAL,
|
||||
quick_check and foreign-key integrity. Missing metadata refuses; it never
|
||||
reinitializes an existing file. Full synchronous WAL transactions either commit
|
||||
or roll back. SQL UPDATE, DELETE and INSERT OR REPLACE refuse on every table.
|
||||
No migration or pruning command ships here.
|
||||
or roll back. SQL UPDATE, DELETE and INSERT OR REPLACE refuse on evidence tables.
|
||||
|
||||
`task_comment_cursors` holds append-only `(business, task_id, last_comment)` rows.
|
||||
The latest sequence wins, and a database guard requires the ID to increase.
|
||||
Trusted sync commits a new cursor and its comment event in one transaction;
|
||||
agent snapshots and the public task digest/projection are unchanged. An empty
|
||||
poll writes no cursor. Growth is at most one row per observed new comment, and
|
||||
usually one per batch. IDs include bots, but bot comments never contribute to
|
||||
the external-comment count. Task IDs survive project moves within a business.
|
||||
|
||||
Normal startup refuses a 3b database. The explicit offline command is:
|
||||
|
||||
```sh
|
||||
node packages/bus/src/migrate-schema.mjs /absolute/dataRoot
|
||||
```
|
||||
|
||||
Mos runs it only with the bus stopped, in an approved downtime/re-pin window,
|
||||
after a consistent backup. Development seats run it only against disposable
|
||||
fixtures. It takes the same exclusive writer lock, checks the pinned source
|
||||
schema and metadata, WAL, timestamps and integrity, then runs named migration
|
||||
steps in order. Each step atomically adds its DDL, changes only schema version
|
||||
and digest metadata, restores the metadata update guard, and verifies its target
|
||||
digest. The 3b→3c step also records an immutable `comment_cursor_epoch`. Prior
|
||||
steps stay pinned under `src/migrations/`; later versions append a step. A completed
|
||||
step remains valid if a later step refuses; rerunning resumes from that version.
|
||||
Already-current migration is a validated no-op. Missing stores refuse.
|
||||
|
||||
Before a task has a cursor, comments at or after the epoch minus 60 seconds are
|
||||
eligible (rounded down to tracker seconds). This deliberately favors a possible
|
||||
cutover duplicate over dropping comments with up to one minute of tracker clock
|
||||
lag. Older historical comments are baselined without events; a tracker clock
|
||||
more than a minute behind may still lose initial comments, and a clock ahead can
|
||||
replay already reported comments once at migration. Once an ID is stored, only
|
||||
IDs govern freshness, including after a long outage. No other event semantics,
|
||||
tracker client, or pruning command changes here.
|
||||
|
||||
Run `node --test packages/bus/tests/*.test.mjs`. Tests use private disposable
|
||||
directories, fixture token files, fake process tables, real local child brokers,
|
||||
|
||||
@@ -209,3 +209,21 @@ CREATE VIEW urgent_inbox AS
|
||||
FROM decisions d
|
||||
WHERE d.class = 'gated' AND d.blocking = 1
|
||||
AND NOT EXISTS (SELECT 1 FROM decision_events x WHERE x.decision = d.id AND x.op IN ('resolved','withdrawn','expired'));
|
||||
CREATE TABLE task_comment_cursors (
|
||||
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
business TEXT NOT NULL,
|
||||
task_id INTEGER NOT NULL CHECK (task_id > 0),
|
||||
last_comment INTEGER NOT NULL CHECK (last_comment > 0)
|
||||
) STRICT;
|
||||
CREATE INDEX task_comment_cursors_latest ON task_comment_cursors (business, task_id, seq);
|
||||
CREATE TRIGGER task_comment_cursors_no_update BEFORE UPDATE ON task_comment_cursors
|
||||
BEGIN SELECT RAISE(ABORT, 'task_comment_cursors is append-only'); END;
|
||||
CREATE TRIGGER task_comment_cursors_no_delete BEFORE DELETE ON task_comment_cursors
|
||||
BEGIN SELECT RAISE(ABORT, 'task_comment_cursors is append-only'); END;
|
||||
CREATE TRIGGER task_comment_cursors_no_replace BEFORE INSERT ON task_comment_cursors
|
||||
WHEN EXISTS (SELECT 1 FROM task_comment_cursors WHERE seq = NEW.seq)
|
||||
BEGIN SELECT RAISE(ABORT, 'task_comment_cursors is append-only'); END;
|
||||
CREATE TRIGGER task_comment_cursors_forward BEFORE INSERT ON task_comment_cursors
|
||||
WHEN NEW.last_comment <= COALESCE((SELECT last_comment FROM task_comment_cursors
|
||||
WHERE business = NEW.business AND task_id = NEW.task_id ORDER BY seq DESC LIMIT 1), 0)
|
||||
BEGIN SELECT RAISE(ABORT, 'comment cursor must advance'); END;
|
||||
+32
-12
@@ -599,23 +599,35 @@ export class Broker {
|
||||
}
|
||||
// Trusted S3 adapter only. With a cap it records a role's own completed write: the external call
|
||||
// already happened, so this records and does not re-authorize. Without one it records sync reads.
|
||||
recordTask({ cap = null, business, snapshots = [], events = [] }) {
|
||||
recordTask({ cap = null, business, snapshots = [], events = [], commentCursors = [] }) {
|
||||
const s = cap === null ? { business, role: null, run: null } : this.#session(cap);
|
||||
this.#business(business);
|
||||
if (cap !== null && (s.human || s.reader || s.business !== business)) fail('agent-required');
|
||||
if (!Array.isArray(snapshots) || !Array.isArray(events)) fail('invalid-request');
|
||||
if (!Array.isArray(snapshots) || !Array.isArray(events) || !Array.isArray(commentCursors)) fail('invalid-request');
|
||||
if (cap !== null && commentCursors.length) fail('agent-required');
|
||||
const kinds = cap === null ? POLL_KINDS : SELF_KINDS;
|
||||
this.#secretCheck({ snapshots, events });
|
||||
return this.#store.transaction(() => ({
|
||||
snapshots: snapshots.map((x) => this.#snapshot(s, x)),
|
||||
events: events.map((e) => {
|
||||
keys(e, ['kind', 'body', 'subject'], ['kind', 'body']);
|
||||
if (!kinds.has(e.kind)) fail('reserved-event');
|
||||
if (!object(e.body)) fail('invalid-request');
|
||||
return this.#event(s, e.kind, e.body, e.subject ?? null);
|
||||
}),
|
||||
}));
|
||||
this.#secretCheck({ snapshots, events, commentCursors });
|
||||
return this.#store.transaction(() => {
|
||||
for (const x of commentCursors) {
|
||||
keys(x, ['task_id', 'last_comment'], ['task_id', 'last_comment']);
|
||||
if (![x.task_id, x.last_comment].every((n) => Number.isSafeInteger(n) && n > 0)) fail('invalid-request');
|
||||
this.#store.run(
|
||||
'INSERT INTO task_comment_cursors(business,task_id,last_comment) VALUES(?,?,?)',
|
||||
business, x.task_id, x.last_comment,
|
||||
);
|
||||
}
|
||||
return {
|
||||
snapshots: snapshots.map((x) => this.#snapshot(s, x)),
|
||||
events: events.map((e) => {
|
||||
keys(e, ['kind', 'body', 'subject'], ['kind', 'body']);
|
||||
if (!kinds.has(e.kind)) fail('reserved-event');
|
||||
if (!object(e.body)) fail('invalid-request');
|
||||
return this.#event(s, e.kind, e.body, e.subject ?? null);
|
||||
}),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
#snapshot(s, x) {
|
||||
const poll = s.role === null;
|
||||
keys(
|
||||
@@ -649,6 +661,14 @@ export class Broker {
|
||||
// priority and requirement (#taskRows).
|
||||
taskView(business, view, arg = null) {
|
||||
this.#business(business);
|
||||
if (view === 'comments' && arg === null) return {
|
||||
epoch: this.#store.get("SELECT value FROM meta WHERE key='comment_cursor_epoch'").value,
|
||||
cursors: this.#store.all(
|
||||
`SELECT task_id,last_comment FROM task_comment_cursors c WHERE business=? AND seq=(
|
||||
SELECT max(seq) FROM task_comment_cursors WHERE business=c.business AND task_id=c.task_id
|
||||
) ORDER BY task_id`, business,
|
||||
),
|
||||
};
|
||||
const parse = (r) => ({ ...r, fields: JSON.parse(r.fields) });
|
||||
if (view === 'current' && arg === null)
|
||||
return this.#store.all('SELECT * FROM task_current WHERE business=? ORDER BY task_ref', business).map(parse);
|
||||
|
||||
@@ -13,7 +13,19 @@ import { join, isAbsolute } from 'node:path';
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
const ddl = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8');
|
||||
export const SCHEMA_SHA256 = '179ffe356d4ff19a49b5ebad39b6c6bfd7771deb8e1b7c55b5e746f039d69e65';
|
||||
export const SCHEMA_SHA256 = '7587ac1df813e5148fdb73dc4733ec8064e6a142e4647f0da71be009a73eebdb';
|
||||
export const SCHEMA_VERSION = '3c';
|
||||
// Ordered, named offline steps. New schema changes append a step and keep prior DDL pinned.
|
||||
const legacyDDL = readFileSync(new URL('./migrations/3b.sql', import.meta.url), 'utf8');
|
||||
const LEGACY_SHA256 = '179ffe356d4ff19a49b5ebad39b6c6bfd7771deb8e1b7c55b5e746f039d69e65';
|
||||
const migrations = [{
|
||||
from: '3b',
|
||||
to: '3c',
|
||||
sql: readFileSync(new URL('./migrations/3b-3c.sql', import.meta.url), 'utf8'),
|
||||
initialize: (store) => store.run(
|
||||
'INSERT INTO meta(key,value) VALUES (?,?)', 'comment_cursor_epoch', new Date().toISOString(),
|
||||
),
|
||||
}];
|
||||
const timedTables = [
|
||||
'events',
|
||||
'role_claims',
|
||||
@@ -37,6 +49,19 @@ const reference = new DatabaseSync(':memory:');
|
||||
reference.exec(ddl);
|
||||
const EXPECTED_DIGEST = schemaDigest(reference);
|
||||
reference.close();
|
||||
const legacy = new DatabaseSync(':memory:');
|
||||
if (hex(legacyDDL) !== LEGACY_SHA256) throw Error('legacy-schema-source-mismatch');
|
||||
legacy.exec(legacyDDL);
|
||||
for (let i = 0; i < migrations.length; i++) {
|
||||
const step = migrations[i];
|
||||
if (i && migrations[i - 1].to !== step.from) throw Error('migration-chain-mismatch');
|
||||
step.fromDigest = schemaDigest(legacy);
|
||||
legacy.exec(step.sql);
|
||||
step.toDigest = schemaDigest(legacy);
|
||||
}
|
||||
if (migrations.at(-1).to !== SCHEMA_VERSION || schemaDigest(legacy) !== EXPECTED_DIGEST)
|
||||
throw Error('migration-target-mismatch');
|
||||
legacy.close();
|
||||
function exists(path) {
|
||||
try {
|
||||
return lstatSync(path);
|
||||
@@ -64,7 +89,7 @@ export class Store {
|
||||
#lockStat;
|
||||
#closed = false;
|
||||
#inTransaction = false;
|
||||
constructor(dataRoot) {
|
||||
constructor(dataRoot, { migrateSchema = false } = {}) {
|
||||
if (typeof dataRoot !== 'string' || !isAbsolute(dataRoot)) throw Error('unsafe-path');
|
||||
const rootStat = lstatSync(dataRoot);
|
||||
if (!rootStat.isDirectory() || rootStat.isSymbolicLink() || rootStat.uid !== process.getuid())
|
||||
@@ -94,6 +119,7 @@ export class Store {
|
||||
}
|
||||
this.path = join(this.directory, 'bus.sqlite');
|
||||
const fresh = !exists(this.path);
|
||||
if (fresh && migrateSchema) throw Error('migration-source-missing');
|
||||
if (fresh) closeSync(openSync(this.path, 'wx', 0o600));
|
||||
else safe(this.path);
|
||||
for (const suffix of ['-wal', '-shm']) if (exists(this.path + suffix)) safe(this.path + suffix);
|
||||
@@ -104,13 +130,15 @@ export class Store {
|
||||
this.#db.exec('PRAGMA journal_mode=WAL');
|
||||
this.transaction(() => {
|
||||
this.#db.exec(ddl.replace('PRAGMA journal_mode = WAL;', ''));
|
||||
this.run('INSERT INTO meta(key,value) VALUES (?,?)', 'schema_version', '3b');
|
||||
this.run('INSERT INTO meta(key,value) VALUES (?,?)', 'schema_version', SCHEMA_VERSION);
|
||||
this.run('INSERT INTO meta(key,value) VALUES (?,?)', 'schema_digest', EXPECTED_DIGEST);
|
||||
this.run('INSERT INTO meta(key,value) VALUES (?,?)', 'comment_cursor_epoch', new Date().toISOString());
|
||||
});
|
||||
}
|
||||
if (!fresh && migrateSchema) this.#migrateSchema();
|
||||
if (
|
||||
schemaDigest(this.#db) !== EXPECTED_DIGEST ||
|
||||
this.get("SELECT value FROM meta WHERE key='schema_version'")?.value !== '3b' ||
|
||||
this.get("SELECT value FROM meta WHERE key='schema_version'")?.value !== SCHEMA_VERSION ||
|
||||
this.get("SELECT value FROM meta WHERE key='schema_digest'")?.value !== EXPECTED_DIGEST ||
|
||||
this.get('PRAGMA journal_mode').journal_mode !== 'wal'
|
||||
)
|
||||
@@ -118,12 +146,41 @@ export class Store {
|
||||
if (this.get('PRAGMA quick_check').quick_check !== 'ok' || this.all('PRAGMA foreign_key_check').length)
|
||||
throw Error('integrity-failed');
|
||||
this.#verifyTimes();
|
||||
const epoch = this.get("SELECT value FROM meta WHERE key='comment_cursor_epoch'")?.value;
|
||||
if (!epoch || !Number.isFinite(Date.parse(epoch)) || new Date(epoch).toISOString() !== epoch)
|
||||
throw Error('invalid-comment-epoch');
|
||||
} catch (e) {
|
||||
if (fd !== undefined) closeSync(fd);
|
||||
this.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
// Explicit offline operation only. Normal startup refuses old versions without changing them.
|
||||
#migrateSchema() {
|
||||
let version = this.get("SELECT value FROM meta WHERE key='schema_version'")?.value;
|
||||
while (version !== SCHEMA_VERSION) {
|
||||
const step = migrations.find((m) => m.from === version);
|
||||
if (
|
||||
!step || schemaDigest(this.#db) !== step.fromDigest ||
|
||||
this.get("SELECT value FROM meta WHERE key='schema_digest'")?.value !== step.fromDigest ||
|
||||
this.get('PRAGMA journal_mode').journal_mode !== 'wal'
|
||||
) throw Error('schema-mismatch');
|
||||
if (this.get('PRAGMA quick_check').quick_check !== 'ok' || this.all('PRAGMA foreign_key_check').length)
|
||||
throw Error('integrity-failed');
|
||||
this.#verifyTimes();
|
||||
this.transaction(() => {
|
||||
this.#db.exec(step.sql);
|
||||
// Only version metadata changes. The guard is restored before this step commits.
|
||||
this.#db.exec('DROP TRIGGER meta_no_update');
|
||||
this.run("UPDATE meta SET value=? WHERE key='schema_version'", step.to);
|
||||
this.run("UPDATE meta SET value=? WHERE key='schema_digest'", step.toDigest);
|
||||
step.initialize?.(this);
|
||||
this.#db.exec("CREATE TRIGGER meta_no_update BEFORE UPDATE ON meta BEGIN SELECT RAISE(ABORT, 'meta is append-only'); END");
|
||||
if (schemaDigest(this.#db) !== step.toDigest) throw Error('schema-mismatch');
|
||||
});
|
||||
version = step.to;
|
||||
}
|
||||
}
|
||||
get(sql, ...args) {
|
||||
return this.#db.prepare(sql).get(...args);
|
||||
}
|
||||
|
||||
@@ -516,7 +516,7 @@ test('v3b prototype refusals, views and append-only mutations', (t) => {
|
||||
|
||||
const count = (type) => db.prepare('SELECT count(*) n FROM sqlite_master WHERE type = ?').get(type).n;
|
||||
for (const label of refused) assert.ok(tested.has(label), label);
|
||||
assert.equal(count('table') - 1, 8);
|
||||
assert.equal(count('trigger'), 35);
|
||||
assert.equal(count('table') - 1, 9);
|
||||
assert.equal(count('trigger'), 39);
|
||||
assert.equal(count('view'), 5);
|
||||
});
|
||||
@@ -427,3 +427,22 @@ run use a scratch container on 127.0.0.1, never a shared instance.
|
||||
|
||||
`deploy/vikunja/` has the compose file for installs without their own
|
||||
Vikunja. See its README.
|
||||
|
||||
### Durable comment cursor (row 49)
|
||||
|
||||
The bus stores each business/task's highest observed comment ID separately from
|
||||
`task_current` and its digest. Adapter startup restores those IDs before its
|
||||
first reconcile. The first read of each task's comment list after startup catches
|
||||
outage comments, even outside the normal `updated` window. A reconcile also
|
||||
checks comment lists when the expanded count changes. Bot IDs advance the cursor
|
||||
but do not produce external-comment counts. The cursor and event commit together;
|
||||
in-memory ID, count and timestamp caches advance only after a successful commit.
|
||||
A crash after commit reloads the durable cursor and does not repeat the event.
|
||||
|
||||
No cursor row is written until a new ID is observed. Empty tasks implicitly have
|
||||
cursor zero; initial timestamps use the database's creation/migration epoch minus
|
||||
a 60-second skew margin. This can duplicate comments at the first migration if
|
||||
the tracker clock is ahead, and clocks more than a minute behind remain outside
|
||||
the guarantee. Once a cursor exists, timestamps do not govern freshness.
|
||||
See the bus README for the explicit offline schema migration and Mos's separate
|
||||
approved downtime/re-pin procedure. No development seat migrates the live store.
|
||||
@@ -68,6 +68,7 @@ export function tasksAdapter({
|
||||
const call = client({ baseUrl: s.baseUrl, fetch });
|
||||
const use = (instance) => (method, path, opts) =>
|
||||
credentials.use(`${name}/${instance}`, 'vikunja', (token) => call(token, method, path, opts));
|
||||
const comments = broker.taskView(name, 'comments');
|
||||
const ctx = {
|
||||
...s,
|
||||
broker,
|
||||
@@ -86,7 +87,8 @@ export function tasksAdapter({
|
||||
pending: 0,
|
||||
lastStart: null,
|
||||
lastReconcile: null,
|
||||
lastComment: new Map(),
|
||||
lastComment: new Map(comments.cursors.map((c) => [c.task_id, c.last_comment])),
|
||||
commentEpoch: Date.parse(comments.epoch),
|
||||
commentsAt: new Map(),
|
||||
commentCount: new Map(),
|
||||
files: Object.fromEntries(Object.entries(files(businesses[name])).map(([i, f]) => [i, { file: f, stamp: stamp(f) }])),
|
||||
|
||||
+18
-14
@@ -79,12 +79,14 @@ export async function walk(ctx, filter, query = {}, project = ctx.project) {
|
||||
const changed = (before, after) =>
|
||||
Object.keys(after).filter((k) => JSON.stringify(before?.[k]) !== JSON.stringify(after[k]));
|
||||
// Comments by anyone but this business's bots, newer than the last one seen. The first look at a
|
||||
// task counts only comments created inside the window, so a restart doesn't replay old ones.
|
||||
// task uses the database epoch minus the skew margin; later reads use durable IDs.
|
||||
// `created` is to the second, so the bound is too.
|
||||
async function foreignComments(ctx, id, sinceMs) {
|
||||
async function foreignComments(ctx, id, out) {
|
||||
const r = await all(ctx.syncCall, null, `/tasks/${id}/comments`, {}, ctx.per);
|
||||
if (!r.ok) throw new BusError(refusal(r.r));
|
||||
const since = Math.floor(sinceMs / 1000) * 1000;
|
||||
// No cursor yet: allow one minute of tracker clock skew around creation/migration.
|
||||
// Once an ID is durable, tracker timestamps never affect freshness.
|
||||
const since = Math.floor((ctx.commentEpoch - WINDOW_MS) / 1000) * 1000;
|
||||
const last = ctx.lastComment.get(id);
|
||||
let n = 0,
|
||||
top = last ?? 0;
|
||||
@@ -94,7 +96,7 @@ async function foreignComments(ctx, id, sinceMs) {
|
||||
const fresh = last === undefined ? Date.parse(c.created) >= since : c.id > last;
|
||||
if (fresh && !ctx.bots.has(c.author.id)) n++;
|
||||
}
|
||||
ctx.lastComment.set(id, top);
|
||||
if (top > (last ?? 0)) out.commentCursors.push({ task_id: id, last_comment: top });
|
||||
return n;
|
||||
}
|
||||
// One task read. Records nothing that a newer self snapshot already covers, and nothing that
|
||||
@@ -163,7 +165,9 @@ async function missing(ctx, current, refs, out) {
|
||||
}
|
||||
}
|
||||
const record = (ctx, out) => {
|
||||
if (out.snapshots.length || out.events.length) ctx.broker.recordTask({ business: ctx.business, ...out });
|
||||
if (out.snapshots.length || out.events.length || out.commentCursors.length)
|
||||
ctx.broker.recordTask({ business: ctx.business, ...out });
|
||||
for (const c of out.commentCursors) ctx.lastComment.set(c.task_id, c.last_comment);
|
||||
};
|
||||
// A child that answers 403 or 404 may have left the tree after the last reconcile: no longer
|
||||
// shared, moved under a project the bot can't read, or deleted. A 404 can also be the child's kanban
|
||||
@@ -191,17 +195,16 @@ export async function tick(ctx) {
|
||||
}
|
||||
}
|
||||
const current = new Map(ctx.broker.taskView(ctx.business, 'current').map((x) => [x.task_ref, x]));
|
||||
const out = { snapshots: [], events: [] };
|
||||
const out = { snapshots: [], events: [], commentCursors: [] };
|
||||
// Refs some read this tick placed. Keyed by ref, not id: a task moved between two projects of the
|
||||
// tree shows on the new one's board under the same id, and the old ref still needs its tombstone.
|
||||
const placed = new Set();
|
||||
for (const { project, view, open, hits } of reads) {
|
||||
const comments = new Map();
|
||||
for (const [id, t] of hits) {
|
||||
const key = normal(t.updated);
|
||||
if (ctx.commentsAt.get(id) === key) continue;
|
||||
comments.set(id, await foreignComments(ctx, id, sinceMs));
|
||||
ctx.commentsAt.set(id, key);
|
||||
if (ctx.commentsAt.get(id) === normal(t.updated)) continue;
|
||||
comments.set(id, await foreignComments(ctx, id, out));
|
||||
// Cache advances only after the database transaction commits.
|
||||
}
|
||||
for (const [id, { task, bucket }] of open) {
|
||||
const hit = hits.get(id);
|
||||
@@ -224,6 +227,7 @@ export async function tick(ctx) {
|
||||
.filter((r) => !placed.has(r));
|
||||
await missing(ctx, current, gone, out);
|
||||
record(ctx, out);
|
||||
for (const { hits } of reads) for (const [id, t] of hits) ctx.commentsAt.set(id, normal(t.updated));
|
||||
ctx.lastStart = startMs;
|
||||
return { snapshots: out.snapshots.length, events: out.events.length };
|
||||
}
|
||||
@@ -234,13 +238,12 @@ export async function tick(ctx) {
|
||||
export async function reconcile(ctx) {
|
||||
const startMs = ctx.clock();
|
||||
const readAt = iso(startMs);
|
||||
const sinceMs = ctx.lastReconcile ?? startMs;
|
||||
if (ctx.descendants) await discover(ctx);
|
||||
const reads = [];
|
||||
for (const [project, view] of ctx.tree)
|
||||
reads.push({ project, view, open: await openTasks(ctx, project, view), list: await walk(ctx, '', { expand: 'comment_count' }, project) });
|
||||
const current = new Map(ctx.broker.taskView(ctx.business, 'current').map((x) => [x.task_ref, x]));
|
||||
const out = { snapshots: [], events: [] };
|
||||
const out = { snapshots: [], events: [], commentCursors: [] };
|
||||
const listed = new Set();
|
||||
for (const { project, view, open, list } of reads)
|
||||
for (const t of list) {
|
||||
@@ -251,8 +254,7 @@ export async function reconcile(ctx) {
|
||||
const count = t.comment_count;
|
||||
if (count !== undefined && !Number.isSafeInteger(count)) shape();
|
||||
const before = ctx.commentCount.get(t.id);
|
||||
if (count !== undefined) ctx.commentCount.set(t.id, count);
|
||||
if (before !== undefined && count !== before) comments = await foreignComments(ctx, t.id, sinceMs);
|
||||
if (before === undefined || count !== before) comments = await foreignComments(ctx, t.id, out);
|
||||
consider(ctx, current, project, { task: t, bucket, via: 'reconcile', readAt, comments }, out);
|
||||
}
|
||||
const gone = [...current.values()]
|
||||
@@ -260,6 +262,8 @@ export async function reconcile(ctx) {
|
||||
.map((x) => x.task_ref);
|
||||
await missing(ctx, current, gone, out);
|
||||
record(ctx, out);
|
||||
for (const { list } of reads)
|
||||
for (const t of list) if (t.comment_count !== undefined) ctx.commentCount.set(t.id, t.comment_count);
|
||||
ctx.lastReconcile = startMs;
|
||||
ctx.lastStart ??= startMs;
|
||||
return { snapshots: out.snapshots.length, events: out.events.length };
|
||||
|
||||
@@ -104,8 +104,8 @@ export async function world(t, edit = {}) {
|
||||
for (const [r, x] of Object.entries(roles)) if (x.credentials) references['demo/' + r] = x.credentials;
|
||||
if (businesses.demo.tracker.sync.credentials) references['demo/@sync'] = businesses.demo.tracker.sync.credentials;
|
||||
const credentials = new Credentials({ references, env, dataRoot: root });
|
||||
const store = new Store(root);
|
||||
const broker = new Broker({ store, businesses, secretCheck: (v) => credentials.assertClean(v) });
|
||||
let store = new Store(root);
|
||||
let broker = new Broker({ store, businesses, secretCheck: (v) => credentials.assertClean(v) });
|
||||
const log = [];
|
||||
const factory = tasksAdapter({
|
||||
trackers: { demo: { baseUrl: 'http://vikunja.test', project, ...edit.tracker } },
|
||||
@@ -114,7 +114,7 @@ export async function world(t, edit = {}) {
|
||||
autostart: false,
|
||||
...edit.adapter,
|
||||
});
|
||||
const adapter = await factory({ broker, credentials, businesses });
|
||||
let adapter = await factory({ broker, credentials, businesses });
|
||||
t.after(async () => {
|
||||
await adapter.close();
|
||||
store.close();
|
||||
@@ -145,6 +145,17 @@ export async function world(t, edit = {}) {
|
||||
store.all('SELECT * FROM task_snapshots WHERE task_ref=? ORDER BY seq', taskRef).map((s) => ({ ...s, fields: JSON.parse(s.fields) }));
|
||||
const call = (cap, verb, args) => broker.requestTask(cap, { verb, args }, adapter.handle);
|
||||
return {
|
||||
async stop() {
|
||||
await adapter.close();
|
||||
store.close();
|
||||
},
|
||||
async restart() {
|
||||
store = new Store(root);
|
||||
broker = new Broker({ store, businesses, secretCheck: (v) => credentials.assertClean(v) });
|
||||
adapter = await factory({ broker, credentials, businesses });
|
||||
await adapter.start('demo');
|
||||
if (adapter.status()[0].state !== 'ready') throw Error(adapter.status()[0].refused);
|
||||
},
|
||||
root,
|
||||
fake,
|
||||
owner,
|
||||
@@ -155,9 +166,9 @@ export async function world(t, edit = {}) {
|
||||
tokens,
|
||||
credentials,
|
||||
businesses,
|
||||
store,
|
||||
broker,
|
||||
adapter,
|
||||
get store() { return store; },
|
||||
get broker() { return broker; },
|
||||
get adapter() { return adapter; },
|
||||
log,
|
||||
caps,
|
||||
agent,
|
||||
|
||||
@@ -10,18 +10,14 @@
|
||||
// "refuse<TAB>reason<TAB>detail" line and exit 2. A missing or unsafe
|
||||
// database or business file exits 3.
|
||||
//
|
||||
// Kept in step with packages/bus/src/store.mjs, which this imports from:
|
||||
// the schema_version literal '3b' below is a copy of the Store's. A schema
|
||||
// bump changes it there, and until it changes here every read refuses
|
||||
// bus-schema.
|
||||
// Schema version and digest follow the Store exports; a schema bump has one authority.
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { readFileSync, lstatSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { schemaDigest, SCHEMA_SHA256 } from '../../packages/bus/src/store.mjs';
|
||||
import { schemaDigest, SCHEMA_SHA256, SCHEMA_VERSION } from '../../packages/bus/src/store.mjs';
|
||||
|
||||
const SCHEMA_VERSION = '3b';
|
||||
const MERGE = 'git.merge.protected';
|
||||
const WORK_REF = /^work\/vikunja-([1-9][0-9]*)-([1-9][0-9]*)$/;
|
||||
const ANY_WORK_REF = /\bwork\/[A-Za-z0-9._/-]*[A-Za-z0-9_]/g;
|
||||
|
||||
Reference in new issue
Block a user