The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as SqlClient from "effect/sql/SqlClient"; |
| 3 | |
| 4 | // Bootstrap uses projections and cursors, so repair projections only. |
| 5 | // Matching settledAt and occurred_at identifies automatic settlements from the decider's shared clock read. |
| 6 | export default Effect.gen(function* () { |
| 7 | const sql = yield* SqlClient.SqlClient; |
| 8 | |
| 9 | yield* sql` |
| 10 | WITH activity_timestamps AS ( |
| 11 | SELECT thread_id, created_at AS activity_at |
| 12 | FROM projection_thread_messages |
| 13 | WHERE role = 'user' |
| 14 | UNION ALL |
| 15 | SELECT thread_id, requested_at |
| 16 | FROM projection_turns |
| 17 | UNION ALL |
| 18 | SELECT thread_id, started_at |
| 19 | FROM projection_turns |
| 20 | WHERE started_at IS NOT NULL |
| 21 | UNION ALL |
| 22 | SELECT thread_id, completed_at |
| 23 | FROM projection_turns |
| 24 | WHERE completed_at IS NOT NULL |
| 25 | ), |
| 26 | automatic_settlements AS ( |
| 27 | SELECT |
| 28 | stream_id AS thread_id, |
| 29 | occurred_at, |
| 30 | json_extract(payload_json, '$.settledAt') AS settled_at |
| 31 | FROM orchestration_events |
| 32 | WHERE aggregate_kind = 'thread' |
| 33 | AND event_type = 'thread.settled' |
| 34 | AND actor_kind = 'server' |
| 35 | AND command_id LIKE 'server:auto-settle:%' |
| 36 | AND json_type(payload_json, '$.settledAt') = 'text' |
| 37 | AND json_extract(payload_json, '$.settledAt') = occurred_at |
| 38 | ) |
| 39 | UPDATE projection_threads AS thread |
| 40 | SET settled_at = ( |
| 41 | SELECT COALESCE( |
| 42 | ( |
| 43 | SELECT activity.activity_at |
| 44 | FROM activity_timestamps AS activity |
| 45 | WHERE activity.thread_id = thread.thread_id |
| 46 | AND julianday(activity.activity_at) IS NOT NULL |
| 47 | AND julianday(activity.activity_at) <= julianday(automatic.occurred_at) |
| 48 | ORDER BY julianday(activity.activity_at) DESC |
| 49 | LIMIT 1 |
| 50 | ), |
| 51 | thread.created_at |
| 52 | ) |
| 53 | FROM automatic_settlements AS automatic |
| 54 | WHERE automatic.thread_id = thread.thread_id |
| 55 | AND automatic.settled_at = thread.settled_at |
| 56 | LIMIT 1 |
| 57 | ) |
| 58 | WHERE thread.settled_override = 'settled' |
| 59 | AND EXISTS ( |
| 60 | SELECT 1 |
| 61 | FROM automatic_settlements AS automatic |
| 62 | WHERE automatic.thread_id = thread.thread_id |
| 63 | AND automatic.settled_at = thread.settled_at |
| 64 | ) |
| 65 | `; |
| 66 | }); |