fix(bridge): journal oversized reports without blocking replay

Signed-off-by: Tom Brow <tomb@block.xyz>
This commit is contained in:
Tom Brow
2026-10-15 18:31:33 -07:00
parent c0bb8e4b21
commit 9ab7732642
3 changed files with 37 additions and 6 deletions

View File

@@ -56,7 +56,10 @@ events and channel/author/kind scope, emits stored messages oldest-first, then
Supported message kinds are 9, 45001, and 45003. No human-name resolution is used.
The bridge journals a report before invoking Codex, and records successful queue
acceptance. Replayed event IDs already recorded as sent are ignored. Queue success
acceptance. Replayed event IDs already recorded as sent are ignored. Reports over 60,000 bytes
are recorded in a terminal `rejected` table and reported on stderr without stopping
other deliveries. Send a smaller summary as a new event; replay skips the rejected
event. Rejected records count toward the journal limit. Queue success
means accepted by Codex, not that an agent finished processing the report.
On timeout/crash between submission and recording success, the row stays `sending`.

View File

@@ -58,7 +58,8 @@ def open_db(path, cfg):
db.executescript('''CREATE TABLE IF NOT EXISTS config (digest TEXT NOT NULL, since INTEGER NOT NULL);
CREATE TABLE IF NOT EXISTS deliveries (
id TEXT PRIMARY KEY, channel TEXT NOT NULL, payload TEXT NOT NULL,
state TEXT NOT NULL CHECK(state IN ('pending','sending','sent')));''')
state TEXT NOT NULL CHECK(state IN ('pending','sending','sent')));
CREATE TABLE IF NOT EXISTS rejected (id TEXT PRIMARY KEY, channel TEXT NOT NULL, reason TEXT NOT NULL);''')
digest = hashlib.sha256(json.dumps(cfg, sort_keys=True).encode()).hexdigest()
row = db.execute('SELECT digest, since FROM config').fetchone()
if row and row[0] != digest:
@@ -78,13 +79,20 @@ def ingest(db, route, event):
raise ValueError('event outside authorized author/kind scope')
if ['h', route['channel']] not in [t[:2] for t in event['tags']]:
raise ValueError('event outside authorized channel')
if not isinstance(event['content'], str) or len(event['content'].encode()) > 60000:
raise ValueError('report exceeds 60000 bytes; agent must send a summary')
existing = db.execute('SELECT 1 FROM deliveries WHERE id=?', (event['id'],)).fetchone()
if not isinstance(event['content'], str):
raise ValueError('report content must be a string')
existing = db.execute('SELECT 1 FROM deliveries WHERE id=? UNION ALL SELECT 1 FROM rejected WHERE id=?',
(event['id'], event['id'])).fetchone()
if existing:
return
if db.execute('SELECT count(*) FROM deliveries').fetchone()[0] >= MAX_EVENTS:
if db.execute('SELECT (SELECT count(*) FROM deliveries) + (SELECT count(*) FROM rejected)').fetchone()[0] >= MAX_EVENTS:
raise RuntimeError('delivery journal reached 10000 events; archive this task before continuing')
if len(event['content'].encode()) > 60000:
reason = 'report exceeds 60000 bytes; send a smaller summary as a new event'
with db:
db.execute('INSERT INTO rejected VALUES (?,?,?)', (event['id'], route['channel'], reason))
print(json.dumps({'rejected': event['id'], 'channel': route['channel'], 'reason': reason}), file=sys.stderr, flush=True)
return
with db:
db.execute('INSERT INTO deliveries VALUES (?,?,?,?)',
(event['id'], route['channel'], json.dumps(event), 'pending'))

View File

@@ -47,6 +47,26 @@ class BridgeTest(unittest.TestCase):
finally:
reopened.close()
def test_oversized_report_is_journaled_and_does_not_block_replay_or_summary(self):
with patch('bridge.subprocess.run') as run:
run.return_value.returncode = 0
oversized = dict(self.event, content='x' * 60001)
bridge.ingest(self.db, self.route, oversized)
bridge.deliver(self.db, self.cfg)
run.assert_not_called()
self.assertEqual(self.db.execute('SELECT id FROM rejected').fetchone()[0], oversized['id'])
reopened = bridge.open_db(self.path, self.cfg)
try:
bridge.ingest(reopened, self.route, oversized)
summary = dict(self.event, id='c' * 64, content='short summary')
bridge.ingest(reopened, self.route, summary)
bridge.deliver(reopened, self.cfg)
self.assertEqual(run.call_count, 1)
self.assertIn('short summary', run.call_args.args[0][-1])
self.assertEqual(reopened.execute('SELECT count(*) FROM rejected').fetchone()[0], 1)
finally:
reopened.close()
def test_ambiguous_submission_is_not_retried(self):
bridge.ingest(self.db, self.route, self.event)
with patch('bridge.subprocess.run', side_effect=TimeoutError('queue timed out')):