oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1CREATE TABLE notification_outbox (sequence integer PRIMARY KEY AUTOINCREMENT, id text NOT NULL, team_id text NOT NULL, recipient text NOT NULL, event text NOT NULL, resource_id text NOT NULL, resource_revision integer NOT NULL, kind text, resource_type text, deep_link text, state text, created_at datetime, acknowledged_at datetime);
2CREATE UNIQUE INDEX notification_id ON notification_outbox(id);
3CREATE UNIQUE INDEX notification_event ON notification_outbox(team_id,recipient,event,resource_id,resource_revision);
4CREATE TABLE notification_subscriptions (id text PRIMARY KEY, team_id text, person text, device_id text, revision integer, ack_sequence integer, active numeric, created_at datetime, expires_at datetime);
5CREATE INDEX notification_device ON notification_subscriptions(device_id);
6CREATE TABLE notification_deliveries (subscription_id text, sequence integer, delivered_at datetime, acknowledged_at datetime, PRIMARY KEY(subscription_id,sequence));
7CREATE TABLE notification_history (team_id text, person text, pruned_through integer, PRIMARY KEY(team_id,person));
8CREATE TRIGGER notification_clarification_insert AFTER INSERT ON coordinator_questions BEGIN
9UPDATE notification_outbox SET state='superseded' WHERE resource_type='clarification' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state<>'pending');
10INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),b.team_id,NEW.person,'question',NEW.id,NEW.revision,'question','clarification','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM coordinator_bindings b WHERE b.id=NEW.binding_id AND NEW.state='pending' AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
11END;
12CREATE TRIGGER notification_question_insert AFTER INSERT ON questions BEGIN
13UPDATE notification_outbox SET state='superseded' WHERE resource_type='question' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state<>'pending');
14INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,t.requester,'question',NEW.id,NEW.revision,'question','question','/tasks/'||t.id||'/questions/'||NEW.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t WHERE t.id=NEW.task_id AND NEW.state='pending' AND EXISTS(SELECT 1 FROM members m WHERE m.address=t.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
15END;
16CREATE TRIGGER notification_task_insert AFTER INSERT ON tasks BEGIN
17UPDATE notification_outbox SET state='superseded' WHERE resource_type='task' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state IN ('cancelled','cancel_requested','merged','failed'));
18INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),NEW.team_id,NEW.requester,'task_ready.plan',NEW.id,NEW.revision,'task_ready','task','/tasks/'||NEW.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') WHERE NEW.state='proposed' AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
19END;
20CREATE TRIGGER notification_card_insert AFTER INSERT ON conversation_cards BEGIN
21UPDATE notification_outbox SET state='superseded' WHERE resource_type='card' AND resource_id=NEW.id AND state<>'superseded' AND resource_revision<>NEW.revision;
22INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),c.team_id,c.person,'task_ready.plan',NEW.resource_id,NEW.revision,'task_ready','task','/tasks/'||NEW.resource_id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM conversations c WHERE c.id=NEW.stream_id AND NEW.kind='task' AND EXISTS(SELECT 1 FROM members m WHERE m.address=c.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
23INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),c.team_id,c.person,'card',NEW.id,NEW.revision,'card','card','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM conversations c WHERE c.id=NEW.stream_id AND NEW.kind LIKE 'suggest_%' AND EXISTS(SELECT 1 FROM members m WHERE m.address=c.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
24END;
25CREATE TRIGGER notification_turn_insert AFTER INSERT ON coordinator_turns BEGIN
26UPDATE notification_outbox SET state='superseded' WHERE resource_type='coordinator' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state NOT IN ('uncertain','fenced','context_limit','context_unavailable'));
27INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),NEW.team_id,NEW.person,'interrupted',NEW.id,NEW.revision,'interrupted','coordinator','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') WHERE NEW.state IN ('uncertain','fenced','context_limit','context_unavailable') AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
28END;
29CREATE TRIGGER notification_clarification_update AFTER UPDATE ON coordinator_questions BEGIN
30UPDATE notification_outbox SET state='superseded' WHERE resource_type='clarification' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state<>'pending');
31INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),b.team_id,NEW.person,'question',NEW.id,NEW.revision,'question','clarification','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM coordinator_bindings b WHERE b.id=NEW.binding_id AND NEW.state='pending' AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
32END;
33CREATE TRIGGER notification_question_update AFTER UPDATE ON questions BEGIN
34UPDATE notification_outbox SET state='superseded' WHERE resource_type='question' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state<>'pending');
35INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,t.requester,'question',NEW.id,NEW.revision,'question','question','/tasks/'||t.id||'/questions/'||NEW.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t WHERE t.id=NEW.task_id AND NEW.state='pending' AND EXISTS(SELECT 1 FROM members m WHERE m.address=t.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
36END;
37CREATE TRIGGER notification_task_update AFTER UPDATE ON tasks BEGIN
38UPDATE notification_outbox SET state='superseded' WHERE resource_type='task' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state IN ('cancelled','cancel_requested','merged','failed'));
39INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),NEW.team_id,NEW.requester,'task_ready.plan',NEW.id,NEW.revision,'task_ready','task','/tasks/'||NEW.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') WHERE NEW.state='proposed' AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
40END;
41CREATE TRIGGER notification_card_update AFTER UPDATE ON conversation_cards BEGIN
42UPDATE notification_outbox SET state='superseded' WHERE resource_type='card' AND resource_id=NEW.id AND state<>'superseded' AND resource_revision<>NEW.revision;
43INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),c.team_id,c.person,'task_ready.plan',NEW.resource_id,NEW.revision,'task_ready','task','/tasks/'||NEW.resource_id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM conversations c WHERE c.id=NEW.stream_id AND NEW.kind='task' AND EXISTS(SELECT 1 FROM members m WHERE m.address=c.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
44INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),c.team_id,c.person,'card',NEW.id,NEW.revision,'card','card','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM conversations c WHERE c.id=NEW.stream_id AND NEW.kind LIKE 'suggest_%' AND EXISTS(SELECT 1 FROM members m WHERE m.address=c.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
45END;
46CREATE TRIGGER notification_turn_update AFTER UPDATE ON coordinator_turns BEGIN
47UPDATE notification_outbox SET state='superseded' WHERE resource_type='coordinator' AND resource_id=NEW.id AND state<>'superseded' AND (resource_revision<>NEW.revision OR NEW.state NOT IN ('uncertain','fenced','context_limit','context_unavailable'));
48INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),NEW.team_id,NEW.person,'interrupted',NEW.id,NEW.revision,'interrupted','coordinator','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') WHERE NEW.state IN ('uncertain','fenced','context_limit','context_unavailable') AND EXISTS(SELECT 1 FROM members m WHERE m.address=NEW.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
49END;
50CREATE TRIGGER notification_job_ready AFTER UPDATE ON jobs WHEN NEW.state='succeeded' AND OLD.state IS NOT NEW.state BEGIN
51INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,r.person,'task_ready.complete',t.id,t.revision,'task_ready','task','/tasks/'||t.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t JOIN (SELECT id AS task_id,requester AS person FROM tasks UNION SELECT t.id,o.address FROM tasks t JOIN decision_owners o ON o.project_id=t.project_id) r ON r.task_id=t.id WHERE t.job_id=NEW.id AND t.team_id=NEW.account AND EXISTS(SELECT 1 FROM members m WHERE m.address=r.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
52END;
53CREATE TRIGGER notification_job_decision AFTER UPDATE ON jobs WHEN NEW.wait='approval' AND (OLD.wait IS NOT NEW.wait OR OLD.revision IS NOT NEW.revision) BEGIN
54INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,r.person,'task_ready.decision.'||NEW.revision,t.id,t.revision,'task_ready','task','/tasks/'||t.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t JOIN (SELECT id AS task_id,requester AS person FROM tasks UNION SELECT t.id,o.address FROM tasks t JOIN decision_owners o ON o.project_id=t.project_id) r ON r.task_id=t.id WHERE t.job_id=NEW.id AND t.team_id=NEW.account AND EXISTS(SELECT 1 FROM members m WHERE m.address=r.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
55END;
56CREATE TRIGGER notification_job_interrupted AFTER UPDATE ON jobs WHEN NEW.wait IN ('uncertain_external_outcome','runner_unavailable','connection_unavailable') AND OLD.wait IS NOT NEW.wait BEGIN
57INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,t.requester,'interrupted.job.'||NEW.revision,t.id,t.revision,'interrupted','task','/tasks/'||t.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t WHERE t.job_id=NEW.id AND t.team_id=NEW.account AND EXISTS(SELECT 1 FROM members m WHERE m.address=t.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
58END;
59CREATE TRIGGER notification_device_revoke AFTER UPDATE ON person_devices WHEN NEW.revoked_at IS NOT NULL AND OLD.revoked_at IS NULL BEGIN
60UPDATE notification_subscriptions SET active=0,revision=revision+1 WHERE device_id=NEW.id AND active=1;
61END;
62CREATE TRIGGER notification_member_revoke AFTER UPDATE ON members WHEN NEW.active=0 OR NEW.session_epoch<>OLD.session_epoch BEGIN
63UPDATE notification_subscriptions SET active=0,revision=revision+1 WHERE person=NEW.address AND active=1;
64END;
65CREATE TRIGGER notification_binding_generation AFTER UPDATE ON coordinator_bindings WHEN NEW.generation<>OLD.generation BEGIN
66UPDATE notification_outbox SET state='superseded' WHERE resource_type IN ('clarification','coordinator') AND resource_id IN (SELECT id FROM coordinator_questions WHERE binding_id=NEW.id UNION SELECT id FROM coordinator_turns WHERE binding_id=NEW.id);
67END;
68
69CREATE INDEX notification_feed ON notification_outbox(team_id,recipient,sequence);
70
71-- Existing actionable work remains discoverable after upgrade.
72INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,t.requester,'task_ready.plan',t.id,t.revision,'task_ready','task','/tasks/'||t.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM tasks t WHERE t.state='proposed' AND EXISTS(SELECT 1 FROM members m WHERE m.address=t.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
73INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),b.team_id,q.person,'question',q.id,q.revision,'question','clarification','/conversation','unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM coordinator_questions q JOIN coordinator_bindings b ON b.id=q.binding_id WHERE q.state='pending' AND q.generation=b.generation AND EXISTS(SELECT 1 FROM members m WHERE m.address=q.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
74INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,t.requester,'question',q.id,q.revision,'question','question','/tasks/'||t.id||'/questions/'||q.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM questions q JOIN tasks t ON t.id=q.task_id WHERE q.state='pending' AND EXISTS(SELECT 1 FROM members m WHERE m.address=t.requester AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
75INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,r.person,'task_ready.complete',t.id,t.revision,'task_ready','task','/tasks/'||t.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM jobs j JOIN tasks t ON t.job_id=j.id AND t.team_id=j.account JOIN (SELECT id AS task_id,requester AS person FROM tasks UNION SELECT t.id,o.address FROM tasks t JOIN decision_owners o ON o.project_id=t.project_id) r ON r.task_id=t.id WHERE j.state='succeeded' AND t.state NOT IN ('merged','cancelled','cancel_requested','failed') AND EXISTS(SELECT 1 FROM members m WHERE m.address=r.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
76INSERT INTO notification_outbox(id,team_id,recipient,event,resource_id,resource_revision,kind,resource_type,deep_link,state,created_at) SELECT lower(hex(randomblob(16))),t.team_id,r.person,'task_ready.decision.'||j.revision,t.id,t.revision,'task_ready','task','/tasks/'||t.id,'unread',strftime('%Y-%m-%d %H:%M:%f','now') FROM jobs j JOIN tasks t ON t.job_id=j.id AND t.team_id=j.account JOIN (SELECT id AS task_id,requester AS person FROM tasks UNION SELECT t.id,o.address FROM tasks t JOIN decision_owners o ON o.project_id=t.project_id) r ON r.task_id=t.id WHERE j.state='waiting' AND j.wait='approval' AND t.state NOT IN ('merged','cancelled','cancel_requested','failed') AND EXISTS(SELECT 1 FROM members m WHERE m.address=r.person AND m.active=1) ON CONFLICT(team_id,recipient,event,resource_id,resource_revision) DO NOTHING;
77