flowstatemcp 2.5.0__py3-none-any.whl

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.
Files changed (143) hide show
  1. flowstate/__init__.py +34 -0
  2. flowstate/__main__.py +7 -0
  3. flowstate/activity.py +224 -0
  4. flowstate/agent_guide.py +251 -0
  5. flowstate/app_keys.py +141 -0
  6. flowstate/approvals.py +974 -0
  7. flowstate/approve_page.py +565 -0
  8. flowstate/ask.py +107 -0
  9. flowstate/autosession.py +480 -0
  10. flowstate/backup.py +201 -0
  11. flowstate/brief.py +420 -0
  12. flowstate/card_offer.py +208 -0
  13. flowstate/card_taps.py +165 -0
  14. flowstate/card_trust.py +127 -0
  15. flowstate/claims.py +461 -0
  16. flowstate/cleanup.py +619 -0
  17. flowstate/cli.py +728 -0
  18. flowstate/cli_data.py +434 -0
  19. flowstate/cli_more.py +302 -0
  20. flowstate/cli_review.py +459 -0
  21. flowstate/cloudflared_check.py +49 -0
  22. flowstate/conflict_card_ui.py +286 -0
  23. flowstate/conflict_cards.py +1193 -0
  24. flowstate/conflict_reopen.py +320 -0
  25. flowstate/conflicts.py +420 -0
  26. flowstate/connections.py +33 -0
  27. flowstate/database.py +2191 -0
  28. flowstate/dbconn.py +308 -0
  29. flowstate/deletes.py +860 -0
  30. flowstate/embeddings.py +354 -0
  31. flowstate/erase.py +1047 -0
  32. flowstate/extras.py +71 -0
  33. flowstate/git_capture.py +157 -0
  34. flowstate/guard.py +224 -0
  35. flowstate/handoffs.py +168 -0
  36. flowstate/history.py +755 -0
  37. flowstate/history_page.py +276 -0
  38. flowstate/http_app.py +204 -0
  39. flowstate/identity.py +387 -0
  40. flowstate/install_name.py +140 -0
  41. flowstate/ledger.py +272 -0
  42. flowstate/legacy_args.py +65 -0
  43. flowstate/links.py +609 -0
  44. flowstate/markdown_io.py +242 -0
  45. flowstate/meaning.py +398 -0
  46. flowstate/memory_import.py +92 -0
  47. flowstate/migrations/001_auto_sessions.sql +16 -0
  48. flowstate/migrations/002_review_queue.sql +33 -0
  49. flowstate/migrations/003_links.sql +47 -0
  50. flowstate/migrations/004_review_mode.sql +24 -0
  51. flowstate/migrations/005_history.sql +21 -0
  52. flowstate/migrations/006_activity.sql +26 -0
  53. flowstate/migrations/007_meaning_index.sql +16 -0
  54. flowstate/migrations/008_git_capture.sql +12 -0
  55. flowstate/migrations/009_hard_rules.sql +13 -0
  56. flowstate/migrations/010_handoffs.sql +14 -0
  57. flowstate/migrations/011_record_meta.sql +13 -0
  58. flowstate/migrations/012_accountability.sql +71 -0
  59. flowstate/migrations/013_todo_claims.sql +36 -0
  60. flowstate/migrations/014_decision_conflicts.sql +41 -0
  61. flowstate/migrations/015_session_runs.sql +17 -0
  62. flowstate/migrations/016_write_receipts.sql +35 -0
  63. flowstate/migrations/017_run_views.sql +26 -0
  64. flowstate/migrations/018_run_views_upto.sql +4 -0
  65. flowstate/migrations/019_approvals.sql +47 -0
  66. flowstate/migrations/020_source_stamps.sql +10 -0
  67. flowstate/migrations/021_review_links.sql +11 -0
  68. flowstate/migrations/022_review_start_tickets.sql +10 -0
  69. flowstate/migrations/023_conflict_cards.sql +68 -0
  70. flowstate/migrations/024_rule_confirmations.sql +7 -0
  71. flowstate/migrations/025_confirmed_hash.sql +7 -0
  72. flowstate/migrations/__init__.py +149 -0
  73. flowstate/models.py +307 -0
  74. flowstate/names.py +157 -0
  75. flowstate/offsite.py +386 -0
  76. flowstate/offsite_crypto.py +272 -0
  77. flowstate/paths.py +265 -0
  78. flowstate/popup.py +238 -0
  79. flowstate/priority.py +66 -0
  80. flowstate/privatefs.py +476 -0
  81. flowstate/project_history.py +710 -0
  82. flowstate/provenance.py +696 -0
  83. flowstate/receipts.py +377 -0
  84. flowstate/relay/__init__.py +72 -0
  85. flowstate/relay/acme.py +271 -0
  86. flowstate/relay/api.py +164 -0
  87. flowstate/relay/cli.py +344 -0
  88. flowstate/relay/crypto.py +137 -0
  89. flowstate/relay/ct.py +118 -0
  90. flowstate/relay/runner.py +177 -0
  91. flowstate/relay/store.py +191 -0
  92. flowstate/relay/tunnel.py +413 -0
  93. flowstate/release_signers +14 -0
  94. flowstate/remote_auth.py +1219 -0
  95. flowstate/review.py +320 -0
  96. flowstate/review_apply.py +140 -0
  97. flowstate/review_card_ui.py +241 -0
  98. flowstate/review_cards.py +564 -0
  99. flowstate/review_mode.py +274 -0
  100. flowstate/review_page.py +1361 -0
  101. flowstate/review_phone.py +465 -0
  102. flowstate/review_tunnel.py +338 -0
  103. flowstate/routing.py +392 -0
  104. flowstate/rules.py +418 -0
  105. flowstate/safety.py +98 -0
  106. flowstate/saves.py +83 -0
  107. flowstate/scheduler.py +160 -0
  108. flowstate/schema.sql +713 -0
  109. flowstate/search.py +355 -0
  110. flowstate/secret_store.py +271 -0
  111. flowstate/secrets_scan.py +135 -0
  112. flowstate/selfupdate.py +1020 -0
  113. flowstate/server.py +1299 -0
  114. flowstate/session_contents.py +382 -0
  115. flowstate/session_records.py +629 -0
  116. flowstate/site_style.py +46 -0
  117. flowstate/stored.py +158 -0
  118. flowstate/story.py +442 -0
  119. flowstate/surface.py +173 -0
  120. flowstate/tasks.py +231 -0
  121. flowstate/term.py +40 -0
  122. flowstate/tool_labels.py +104 -0
  123. flowstate/tooldefs.py +383 -0
  124. flowstate/tools/__init__.py +118 -0
  125. flowstate/tools/core.py +545 -0
  126. flowstate/tools/files.py +416 -0
  127. flowstate/tools/git_ops.py +1081 -0
  128. flowstate/tools/intelligence.py +376 -0
  129. flowstate/tools/learning.py +282 -0
  130. flowstate/tools/problems.py +159 -0
  131. flowstate/tools/sessions.py +266 -0
  132. flowstate/tools/story.py +48 -0
  133. flowstate/tools/todos.py +79 -0
  134. flowstate/tools/utils.py +71 -0
  135. flowstate/tools/variables.py +345 -0
  136. flowstate/updates.py +482 -0
  137. flowstate/urls.py +7 -0
  138. flowstate/waiting.py +828 -0
  139. flowstate/who.py +302 -0
  140. flowstatemcp-2.5.0.dist-info/METADATA +88 -0
  141. flowstatemcp-2.5.0.dist-info/RECORD +143 -0
  142. flowstatemcp-2.5.0.dist-info/WHEEL +4 -0
  143. flowstatemcp-2.5.0.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,710 @@
1
+ """Project history (task 73): one project's whole path in time order, for the local review
2
+ page's "Project history" view (history_page.py). Read-only. No MCP tool, no relay route and
3
+ no tunnel route use it.
4
+
5
+ Entries come from who.py's sources (handoffs, hard rules, review-mode changes, todo claims)
6
+ and from these:
7
+ - every edit-history row: created, edited, deleted, undone (kept until `flowstate erase`);
8
+ - records the edit history doesn't know were created: older ones (before 2.4.0, "not
9
+ recorded" unless the activity log still knows the app) and ones only the activity log
10
+ names (deleted before the edit history knew them, or erased);
11
+ - review items queued and decided, links added and removed (a decision replacing another
12
+ is a `supersedes` link), possible conflicts, disagreement cards, automatic sessions;
13
+ - a todo claim that ran out with nobody releasing it (worked out from the claim events).
14
+
15
+ Every entry has its time, app, model (only when the app gave it), machine and how the app
16
+ signed in, and names a record (`record_type`, `record_id`) and sometimes a second one (the
17
+ record a link points to, the record an approved review item became).
18
+
19
+ Erased records (`flowstate erase`): their rows and their edit history are gone. An entry
20
+ that still names one shows `[erased]` as its text; search and the record view find nothing
21
+ of it. The activity log and the session ledger fade after FLOWSTATE_ACTIVITY_DAYS (90):
22
+ `faded` says when the shown range reaches past that.
23
+ """
24
+
25
+ import json
26
+ import re
27
+ import sqlite3
28
+ from datetime import datetime, timedelta, timezone
29
+ from pathlib import Path
30
+ from typing import Optional
31
+
32
+ from . import activity, dbconn, history, identity, who
33
+
34
+ PER_PAGE = 50
35
+ MAX_PER_PAGE = 100
36
+ MAX_EDITS = 200 # edit-history rows shown on one record's page
37
+ FIELD_WIDTH = 2000 # longest value shown for one field, before and after
38
+ ERASED = "[erased]"
39
+ _TYPE_OF = dict(history.TYPE_OF, decision_conflicts="conflict", review_items="review_item")
40
+ _SIDE = tuple(history.SIDE)
41
+ # created by their own source below (who.py's, or the links source): not again from history
42
+ _OWN_CREATE = ("record_links", "project_rules", "handoff_notes")
43
+ # records with a creation time of their own: (type, table, project expression, join)
44
+ _CREATED = (("todo", "todos", "x.project_id", ""),
45
+ ("decision", "conversations", "x.project_id", ""),
46
+ ("learning", "learnings", "x.project_id", ""),
47
+ ("component", "components", "x.project_id", ""),
48
+ ("change", "changes", "c.project_id", "JOIN components c ON c.id = x.component_id"),
49
+ ("problem", "problems", "c.project_id", "JOIN components c ON c.id = x.component_id"))
50
+ _OWN_TYPES = ("project", "rule", "handoff", "link")
51
+ # the text that names a record (live row or a stored copy in the edit history)
52
+ _LABEL = {"todos": ("title",), "conversations": ("key_decisions", "user_prompt_summary"),
53
+ "changes": ("field_name",), "problems": ("title",), "learnings": ("insight",),
54
+ "components": ("name",), "projects": ("name",), "project_rules": ("rule",),
55
+ "handoff_notes": ("where_i_was",), "solutions": ("summary", "description"),
56
+ "solution_attempts": ("description",), "attachments": ("file_name", "name"),
57
+ "project_variables": ("name",), "project_methods": ("name",),
58
+ "review_items": ("summary",)}
59
+ # searched in live rows: table -> (columns, project expression, join)
60
+ _SEARCH = {"todos": (("title", "description"), "x.project_id", ""),
61
+ "conversations": (("user_prompt_summary", "assistant_response_summary",
62
+ "key_decisions"), "x.project_id", ""),
63
+ "learnings": (("insight", "context"), "x.project_id", ""),
64
+ "components": (("name", "description"), "x.project_id", ""),
65
+ "changes": (("field_name", "reason", "old_value", "new_value"), "c.project_id",
66
+ "JOIN components c ON c.id = x.component_id"),
67
+ "problems": (("title", "description"), "c.project_id",
68
+ "JOIN components c ON c.id = x.component_id"),
69
+ "project_rules": (("rule",), "x.project_id", ""),
70
+ "handoff_notes": (("where_i_was", "next_step"), "x.project_id", ""),
71
+ "review_items": (("summary", "details"), "x.project_id", "")}
72
+ TODO_OPEN = ("pending", "in_progress", "blocked")
73
+ TODO_STATUSES = ("open", "done", "cancelled", "deleted", "all")
74
+ _MINUTES = re.compile(r"(?:for |\()(\d{1,4})m(?:\)|\b)")
75
+
76
+
77
+ class HistoryPageError(Exception):
78
+ def __init__(self, code: str, message: str):
79
+ super().__init__(message)
80
+ self.payload = {"error": code, "message": message}
81
+
82
+
83
+ def _now() -> str:
84
+ return datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
85
+
86
+
87
+ def _claim_minutes(summary) -> Optional[int]:
88
+ """How long a claim or renewal was for, from its event text ("... for 15m", "(15m)")."""
89
+ m = _MINUTES.findall(str(summary or ""))
90
+ return int(m[-1]) if m else None
91
+
92
+
93
+ def _connect(db_path: Path) -> sqlite3.Connection:
94
+ conn = dbconn.connect(str(db_path), timeout=5.0)
95
+ conn.row_factory = sqlite3.Row
96
+ conn.execute("PRAGMA busy_timeout = 5000")
97
+ conn.create_function("claim_minutes", 1, _claim_minutes, deterministic=True)
98
+ if "record_type" in who._tables(conn).get("activity_log", ()):
99
+ # who created what, while the activity log keeps it: one scan, then indexed (a temp
100
+ # table: this connection's own, never in the database file)
101
+ conn.execute("CREATE TEMP TABLE created_by AS SELECT record_type, record_id, MIN(id) "
102
+ "AS id FROM activity_log WHERE record_type IS NOT NULL AND ok = 1 GROUP BY "
103
+ "record_type, record_id")
104
+ conn.execute("CREATE INDEX temp.created_by_rec ON created_by(record_type, record_id)")
105
+ conn.execute("PRAGMA query_only = ON") # this page never writes
106
+ return conn
107
+
108
+
109
+ def _type_case(col: str) -> str:
110
+ return "CASE " + col + " " + " ".join(f"WHEN '{t}' THEN '{k}'" for t, k in _TYPE_OF.items()) \
111
+ + f" ELSE {col} END"
112
+
113
+
114
+ def _table_case(col: str) -> str:
115
+ return "CASE " + col + " " + " ".join(f"WHEN '{k}' THEN '{t}'" for k, t in history.TABLES.items()) \
116
+ + f" ELSE {col} END"
117
+
118
+
119
+ def _ids(cols: set, prefix: str = "", fallback: str = "NULL") -> str:
120
+ return who._id_cols(cols, prefix, fallback)
121
+
122
+
123
+ _NO2 = "NULL AS rt2, NULL AS rid2, NULL AS pid2"
124
+
125
+
126
+ def _sources(conn, t: dict) -> list[str]:
127
+ """One SELECT per kind of entry, all with the same columns: at, app, model, machine,
128
+ identity_source, project_id, record_type, record_id, what, kind, ref, extra, rt2, rid2,
129
+ pid2 (a second record and project the entry is about)."""
130
+ q = [f"SELECT w.*, {_NO2} FROM ({s}) w" for s in who._sources(
131
+ {k: v for k, v in t.items() if k in ("handoff_notes", "project_rules", "review_mode_log",
132
+ "todo_claim_events")})]
133
+ if "record_history" in t:
134
+ c = t["record_history"]
135
+ json_of = "CASE WHEN json_valid(COALESCE(h.new_row, h.old_row)) THEN json_extract(" \
136
+ "COALESCE(h.new_row, h.old_row), '$.{}') END"
137
+ side = ", ".join(f"'{s}'" for s in _SIDE)
138
+ q.append(
139
+ f"SELECT h.created_at AS at, {_ids(c, fallback='client')}, "
140
+ f"h.project_id, CASE WHEN h.table_name IN ({side}) THEN {json_of.format('record_type')} "
141
+ f"ELSE {_type_case('h.table_name')} END AS record_type, "
142
+ f"CASE WHEN h.table_name IN ({side}) THEN {json_of.format('record_id')} ELSE h.record_id "
143
+ f"END AS record_id, h.tool AS what, CASE WHEN h.old_row IS NULL AND h.tool != 'history/undo' "
144
+ f"AND h.table_name NOT IN ({side}) THEN 'edit_created' ELSE 'edit' END AS kind, h.id AS ref, h.undone_at AS extra, "
145
+ f"{_NO2} FROM record_history h WHERE NOT (h.old_row IS NULL AND h.table_name IN "
146
+ f"({', '.join(repr(x) for x in _OWN_CREATE)}))")
147
+ rh = "record_history" in t
148
+ made = ("EXISTS (SELECT 1 FROM record_history h WHERE h.table_name = '{table}' AND "
149
+ "h.record_id = {rid} AND h.old_row IS NULL)") if rh else "0"
150
+ act = "activity_log" in t and "record_type" in t["activity_log"]
151
+ ac = t["activity_log"] if act else set() # (created_by: _connect)
152
+ who_cols = ", ".join(f"a.{f} AS {f}" if f in ac else f"NULL AS {f}" for f in who._ID)
153
+ for rtype, table, proj, join in _CREATED:
154
+ if table not in t or "created_at" not in t[table]:
155
+ continue
156
+ q.append(
157
+ f"SELECT x.created_at AS at, {who_cols}, {proj} AS project_id, '{rtype}' AS record_type, "
158
+ f"x.id AS record_id, 'created {rtype} #' || x.id AS what, 'created' AS kind, x.id AS ref, "
159
+ f"NULL AS extra, {_NO2} FROM {table} x {join} "
160
+ + (f"LEFT JOIN created_by cb ON cb.record_type = '{rtype}' AND cb.record_id = x.id "
161
+ f"LEFT JOIN activity_log a ON a.id = cb.id " if act else "")
162
+ + f"WHERE x.created_at IS NOT NULL AND NOT {made.format(table=table, rid='x.id')}")
163
+ if act:
164
+ # creations only the activity log names: the record is gone (deleted before the edit
165
+ # history knew it, or erased) or has no creation time of its own
166
+ mapped = {r: tb for r, tb, _p, _j in _CREATED if tb in t}
167
+ gone = " AND ".join(
168
+ f"NOT (a.record_type = '{r}' AND EXISTS (SELECT 1 FROM {tb} z WHERE z.id = a.record_id))"
169
+ for r, tb in mapped.items()) or "1"
170
+ hist = (f"NOT EXISTS (SELECT 1 FROM record_history h WHERE h.table_name = "
171
+ f"{_table_case('a.record_type')} AND h.record_id = a.record_id AND h.old_row IS NULL)"
172
+ if rh else "1")
173
+ q.append(
174
+ f"SELECT a.at, {who_cols}, "
175
+ f"a.project_id, a.record_type, a.record_id, 'created ' || a.record_type || ' #' || "
176
+ f"a.record_id AS what, 'created' AS kind, a.id AS ref, NULL AS extra, {_NO2} "
177
+ f"FROM created_by cb JOIN activity_log a ON a.id = cb.id WHERE a.record_type NOT IN "
178
+ f"({', '.join(repr(x) for x in _OWN_TYPES)}) AND {gone} AND {hist}")
179
+ if "projects" in t:
180
+ q.append(
181
+ f"SELECT p.created_at AS at, {who_cols}"
182
+ ", p.id AS project_id, 'project' AS record_type, p.id AS record_id, 'project created' "
183
+ f"AS what, 'created' AS kind, p.id AS ref, NULL AS extra, {_NO2} FROM projects p "
184
+ + ("LEFT JOIN created_by cb ON cb.record_type = 'project' AND cb.record_id = p.id "
185
+ "LEFT JOIN activity_log a ON a.id = cb.id" if act else ""))
186
+ if "review_items" in t:
187
+ c = t["review_items"]
188
+ res = (f"{_type_case('result_table')}", "result_id")
189
+ q.append(
190
+ f"SELECT created_at AS at, {_ids(c, fallback='source')}, project_id, 'review_item' AS "
191
+ f"record_type, id AS record_id, 'queued ' || kind || ' for review: ' || "
192
+ f"substr(summary, 1, 80) AS what, 'queued' AS kind, id AS ref, NULL AS extra, "
193
+ f"CASE WHEN kind = 'delete' AND json_valid(payload) THEN json_extract(payload, "
194
+ f"'$.record_type') END AS rt2, CASE WHEN kind = 'delete' AND json_valid(payload) THEN "
195
+ f"json_extract(payload, '$.record_id') END AS rid2, NULL AS pid2 FROM review_items")
196
+ q.append(
197
+ f"SELECT decided_at AS at, {_ids(c, 'decided_', 'decided_by')}, project_id, "
198
+ f"'review_item' AS record_type, id AS record_id, status || ' review item #' || id || "
199
+ f"' (' || kind || ')' || CASE WHEN result_table IS NOT NULL AND result_id IS NOT NULL "
200
+ f"THEN ', became ' || {res[0]} || ' #' || result_id ELSE '' END AS what, 'decided' AS "
201
+ f"kind, id AS ref, NULL AS extra, {res[0]} AS rt2, {res[1]} AS rid2, NULL AS pid2 "
202
+ f"FROM review_items WHERE decided_at IS NOT NULL")
203
+ if "record_links" in t:
204
+ c = t["record_links"]
205
+ target = "COALESCE(url, to_type || ' #' || to_id)"
206
+ q.append(
207
+ f"SELECT created_at AS at, {_ids(c, fallback='created_by')}, from_project_id AS "
208
+ f"project_id, from_type AS record_type, from_id AS record_id, from_type || ' #' || "
209
+ f"from_id || ' ' || link_type || ' ' || {target} AS what, 'link_added' AS kind, id AS "
210
+ f"ref, link_type AS extra, to_type AS rt2, to_id AS rid2, to_project_id AS pid2 "
211
+ f"FROM record_links")
212
+ q.append(
213
+ f"SELECT removed_at AS at, {_ids(c, 'removed_', 'removed_by')}, from_project_id AS "
214
+ f"project_id, from_type AS record_type, from_id AS record_id, 'removed link: ' || "
215
+ f"from_type || ' #' || from_id || ' ' || link_type || ' ' || {target} AS what, "
216
+ f"'link_removed' AS kind, id AS ref, link_type AS extra, to_type AS rt2, to_id AS rid2, "
217
+ f"to_project_id AS pid2 FROM record_links WHERE removed_at IS NOT NULL")
218
+ if "decision_conflicts" in t:
219
+ c = t["decision_conflicts"]
220
+ pair = "'decision #' || new_id || ' and decision #' || old_id"
221
+ q.append(
222
+ f"SELECT created_at AS at, {_ids(c)}, project_id, 'decision' AS record_type, new_id AS "
223
+ f"record_id, 'possible conflict flagged: ' || {pair} AS what, 'conflict_flagged' AS "
224
+ f"kind, id AS ref, NULL AS extra, 'decision' AS rt2, old_id AS rid2, NULL AS pid2 "
225
+ f"FROM decision_conflicts")
226
+ q.append(
227
+ f"SELECT resolved_at AS at, {_ids(c, 'resolved_')}, project_id, 'decision' AS "
228
+ f"record_type, new_id AS record_id, 'possible conflict ' || {pair} || CASE status WHEN "
229
+ f"'resolved_replaced' THEN ' resolved: decision #' || resolved_by_decision || "
230
+ f"' replaced one' WHEN 'confirmed' THEN ' confirmed by the user' ELSE ' dismissed: the "
231
+ f"user said they are compatible' END AS what, 'conflict_resolved' AS kind, id AS ref, "
232
+ f"NULL AS extra, 'decision' AS rt2, old_id AS rid2, NULL AS pid2 FROM "
233
+ f"decision_conflicts WHERE resolved_at IS NOT NULL")
234
+ if "conflict_cards" in t:
235
+ c = t["conflict_cards"]
236
+ pair = "a_type || ' #' || a_id || ' and ' || b_type || ' #' || b_id"
237
+ q.append(
238
+ f"SELECT created_at AS at, {_ids(c)}, project_id, a_type AS record_type, a_id AS "
239
+ f"record_id, 'disagreement flagged: ' || {pair} || ' (' || topic || ')' AS what, "
240
+ f"'card_flagged' AS kind, id AS ref, NULL AS extra, b_type AS rt2, b_id AS rid2, NULL "
241
+ f"AS pid2 FROM conflict_cards")
242
+ q.append(
243
+ f"SELECT resolved_at AS at, {_ids(c, 'resolved_')}, project_id, a_type AS record_type, "
244
+ f"a_id AS record_id, 'disagreement ' || {pair} || CASE status WHEN 'kept_a' THEN "
245
+ f"': kept the older one' WHEN 'kept_b' THEN ': kept the newer one' WHEN 'kept_both' "
246
+ f"THEN ': kept both' ELSE ': dismissed' END || COALESCE(' (' || resolved_path || ')', "
247
+ f"'') AS what, 'card_resolved' AS kind, id AS ref, NULL AS extra, b_type AS rt2, b_id "
248
+ f"AS rid2, NULL AS pid2 FROM conflict_cards WHERE resolved_at IS NOT NULL")
249
+ if "todo_claim_events" in t:
250
+ c = t["todo_claim_events"]
251
+ ends = "datetime(x.at, '+' || x.mins || ' minutes')"
252
+ q.append(
253
+ f"SELECT {ends} AS at, {', '.join(f'x.{f} AS {f}' if f in c else f'NULL AS {f}' for f in who._ID)}, "
254
+ f"x.project_id, 'todo' AS record_type, x.todo_id AS record_id, 'claim on todo #' || "
255
+ f"x.todo_id || ' expired (nobody renewed or released it)' AS what, 'todo_expired' AS "
256
+ f"kind, x.id AS ref, NULL AS extra, {_NO2} FROM (SELECT e.*, claim_minutes(e.summary) "
257
+ f"AS mins, LEAD(e.at) OVER (PARTITION BY e.todo_id ORDER BY e.at, e.id) AS next_at, "
258
+ f"LEAD(e.event) OVER (PARTITION BY e.todo_id ORDER BY e.at, e.id) AS next_event "
259
+ f"FROM todo_claim_events e) x WHERE x.event IN ('claim', 'renew') AND x.mins IS NOT "
260
+ f"NULL AND {ends} <= '{_now()}' AND (x.next_at IS NULL OR x.next_at > {ends} OR "
261
+ f"x.next_event = 'claim')")
262
+ if "auto_sessions" in t:
263
+ runs = "session_runs" in t
264
+ ids = ("COALESCE(r.app, s.client) AS app, r.model AS model, r.machine AS machine, "
265
+ "r.identity_source AS identity_source" if runs else
266
+ "s.client AS app, NULL AS model, NULL AS machine, NULL AS identity_source")
267
+ join = "LEFT JOIN session_runs r ON r.session_id = s.session_id" if runs else ""
268
+ q.append(
269
+ f"SELECT s.opened_at AS at, {ids}, s.project_id, 'session' AS record_type, "
270
+ f"s.session_id AS record_id, 'session #' || s.session_id || ' opened' AS what, "
271
+ f"'session_opened' AS kind, s.session_id AS ref, NULL AS extra, {_NO2} "
272
+ f"FROM auto_sessions s {join}")
273
+ q.append(
274
+ f"SELECT s.closed_at AS at, {ids}, s.project_id, 'session' AS record_type, "
275
+ f"s.session_id AS record_id, 'session #' || s.session_id || ' closed (' || "
276
+ f"COALESCE(s.close_reason, 'closed') || ', ' || s.call_count || ' calls)' AS what, "
277
+ f"'session_closed' AS kind, s.session_id AS ref, NULL AS extra, {_NO2} "
278
+ f"FROM auto_sessions s {join} WHERE s.closed_at IS NOT NULL")
279
+ return q
280
+
281
+
282
+ _CAT = ("CASE WHEN u.kind IN ('conflict_flagged', 'conflict_resolved', 'card_flagged', "
283
+ "'card_resolved') THEN 'conflict' WHEN u.kind IN ('queued', 'decided') THEN 'review' "
284
+ "WHEN u.kind IN ('link_added', 'link_removed') THEN 'link' ELSE u.record_type END")
285
+ TYPE_WORDS = {"decision": "Decisions", "todo": "Todos", "change": "Changes", "problem": "Problems",
286
+ "learning": "Learnings", "component": "Components", "project": "The project",
287
+ "review": "Review items", "link": "Links (and what replaced what)",
288
+ "conflict": "Conflicts", "session": "Sessions", "rule": "Hard rules",
289
+ "handoff": "Handoffs", "review_mode_change": "Review mode",
290
+ "attempt": "Attempts", "solution": "Solutions", "attachment": "Files"}
291
+
292
+
293
+ def _where(project_id: int, rtype, app, since_u, until_u, keys, record) -> tuple:
294
+ where, params = ["u.at IS NOT NULL", "(u.project_id = ? OR u.pid2 = ?)"], [project_id] * 2
295
+ if rtype:
296
+ where.append(f"({_CAT} = ? OR u.record_type = ?)")
297
+ params += [rtype, rtype]
298
+ if app not in (None, ""):
299
+ where.append("(instr(lower(COALESCE(u.app, '')), lower(?)) > 0 OR instr(lower("
300
+ "COALESCE(u.machine, '')), lower(?)) > 0)")
301
+ params += [str(app).strip()] * 2
302
+ if since_u:
303
+ where.append("u.at >= ?")
304
+ params.append(since_u)
305
+ if until_u:
306
+ where.append("u.at <= ?")
307
+ params.append(until_u)
308
+ if keys is not None:
309
+ where.append("(instr(lower(COALESCE(u.what, '')), lower(?)) > 0 OR (u.record_type || ':' || "
310
+ "u.record_id) IN (SELECT value FROM json_each(?)) OR (u.rt2 || ':' || u.rid2) "
311
+ "IN (SELECT value FROM json_each(?)))")
312
+ params += [keys[0], json.dumps(sorted(keys[1])), json.dumps(sorted(keys[1]))]
313
+ if record:
314
+ where.append("((u.record_type = ? AND u.record_id = ?) OR (u.rt2 = ? AND u.rid2 = ?))")
315
+ params += [record[0], int(record[1]), record[0], int(record[1])]
316
+ return " AND ".join(where), params
317
+
318
+
319
+ def _tables(conn) -> dict:
320
+ return who._tables(conn)
321
+
322
+
323
+ def _matches(conn, t: dict, project_id: int, text: str) -> set:
324
+ """"type:id" of every record of the project whose live text or a stored copy in its edit
325
+ history contains `text` (any case). An erased record has neither, so it never matches."""
326
+ needle = text.lower()
327
+ escaped = json.dumps(text, ensure_ascii=True)[1:-1].lower()
328
+ out = set()
329
+ for table, (cols, proj, join) in _SEARCH.items():
330
+ if table not in t:
331
+ continue
332
+ have = [c for c in cols if c in t[table]]
333
+ if not have:
334
+ continue
335
+ test = " OR ".join(f"instr(lower(COALESCE(x.{c}, '')), ?) > 0" for c in have)
336
+ for r in conn.execute(f"SELECT x.id FROM {table} x {join} WHERE {proj} = ? AND ({test})",
337
+ (project_id, *[needle] * len(have))):
338
+ out.add(f"{_TYPE_OF.get(table, table)}:{r[0]}")
339
+ if "record_history" in t:
340
+ for r in conn.execute(
341
+ "SELECT table_name, record_id, old_row, new_row FROM record_history WHERE "
342
+ "project_id = ? AND (instr(lower(COALESCE(old_row, '')), ?) > 0 OR instr(lower("
343
+ "COALESCE(new_row, '')), ?) > 0 OR instr(lower(COALESCE(old_row, '')), ?) > 0 OR "
344
+ "instr(lower(COALESCE(new_row, '')), ?) > 0)",
345
+ (project_id, needle, needle, escaped, escaped)):
346
+ if r["table_name"] in _SIDE:
347
+ row = _load(r["new_row"]) or _load(r["old_row"]) or {}
348
+ if row.get("record_type") and row.get("record_id") is not None:
349
+ out.add(f"{row['record_type']}:{row['record_id']}")
350
+ else:
351
+ out.add(f"{_TYPE_OF.get(r['table_name'], r['table_name'])}:{r['record_id']}")
352
+ return out
353
+
354
+
355
+ def _load(v) -> Optional[dict]:
356
+ if not v:
357
+ return None
358
+ try:
359
+ d = json.loads(v)
360
+ except (TypeError, ValueError):
361
+ return None
362
+ return d if isinstance(d, dict) else None
363
+
364
+
365
+ def _label_of(table: str, row: Optional[dict]) -> str:
366
+ if not row:
367
+ return ""
368
+ for col in _LABEL.get(table, ()):
369
+ v = row.get(col)
370
+ if col == "key_decisions" and isinstance(v, str):
371
+ try:
372
+ v = json.loads(v)
373
+ except ValueError:
374
+ pass
375
+ if isinstance(v, list):
376
+ v = v[0] if v else None
377
+ if v:
378
+ s = " ".join(str(v).split())
379
+ return s[len("Decision: "):] if s.startswith("Decision: ") else s
380
+ return ""
381
+
382
+
383
+ def record_state(conn, t: dict, rtype: str, rid) -> dict:
384
+ """{"state": live | deleted | erased | "", "label": text, "row": the live row or None}.
385
+ Erased: no live row and no copy in the edit history; nothing of its text is left."""
386
+ table = history.TABLES.get(rtype) or {"review_item": "review_items",
387
+ "conflict": "decision_conflicts"}.get(rtype)
388
+ if not table or table not in t or rid is None:
389
+ return {"state": "", "label": "", "row": None}
390
+ row = conn.execute(f"SELECT * FROM {table} WHERE id = ?", (rid,)).fetchone()
391
+ if row is not None:
392
+ d = dict(row)
393
+ label = _label_of(table, d)
394
+ if table == "decision_conflicts":
395
+ label = f"decision #{d.get('new_id')} and decision #{d.get('old_id')}"
396
+ return {"state": "live", "label": label, "row": d}
397
+ if "record_history" in t:
398
+ h = conn.execute("SELECT old_row, new_row FROM record_history WHERE table_name = ? AND "
399
+ "record_id = ? ORDER BY id DESC LIMIT 1", (table, rid)).fetchone()
400
+ if h is not None:
401
+ stored = _load(h["new_row"]) or _load(h["old_row"])
402
+ return {"state": "deleted", "label": _label_of(table, stored), "row": None}
403
+ return {"state": "erased", "label": ERASED, "row": None}
404
+
405
+
406
+ def _short(v, width: int = 80) -> str:
407
+ s = " ".join(("" if v is None else str(v)).split())
408
+ return s if len(s) <= width else s[: width - 3] + "..."
409
+
410
+
411
+ def describe_edit(table: str, old: Optional[dict], new: Optional[dict], tool: str,
412
+ undone: bool = False) -> str:
413
+ """One plain line for one edit-history row: created, deleted, a todo completed or
414
+ reopened, which fields changed; an undo and an undone edit say so."""
415
+ tool = tool or ""
416
+ if table in _SIDE:
417
+ name = history.SIDE[table]
418
+ cur = new or old or {}
419
+ if table == "entry_standing":
420
+ out = (f"marked {cur.get('standing')}" + (f" (by {cur.get('by_type')} #{cur.get('by_id')})"
421
+ if cur.get("by_id") else "")
422
+ if new else "standing cleared")
423
+ else:
424
+ out = f"{name} " + ("added" if old is None else "removed" if new is None else "changed")
425
+ elif old is None:
426
+ out = "brought back (undo of a delete)" if tool == "history/undo" else "created"
427
+ elif new is None:
428
+ out = "taken back (undo of its creation)" if tool == "history/undo" else "deleted"
429
+ else:
430
+ ch = history._changed_fields(old, new)
431
+ parts = []
432
+ if table == "todos" and "status" in ch:
433
+ a, b = old.get("status"), new.get("status")
434
+ if b == "done":
435
+ parts.append(f"completed ({a} -> done)")
436
+ elif b == "cancelled":
437
+ parts.append(f"cancelled ({a} -> cancelled)")
438
+ elif a in ("done", "cancelled"):
439
+ parts.append(f"reopened ({a} -> {b})")
440
+ else:
441
+ parts.append(f"status {a} -> {b}")
442
+ rest = sorted(k for k in ch if not (table == "todos" and k in ("status", "completed_at")))
443
+ if rest:
444
+ parts.append("edited " + ", ".join(rest))
445
+ out = "; ".join(parts) or "no field changes"
446
+ if tool == "history/undo":
447
+ out = "undo: " + out
448
+ if tool.startswith("queue/"):
449
+ out = f"{history.QUEUE.get(table, table)} {out}"
450
+ return out + (" (later undone)" if undone else "")
451
+
452
+
453
+ def _decorate(conn, t: dict, rows: list) -> list:
454
+ out, states = [], {}
455
+
456
+ def state(rt, rid):
457
+ if (rt, rid) not in states:
458
+ states[(rt, rid)] = record_state(conn, t, rt, rid)
459
+ return states[(rt, rid)]
460
+ for r in rows:
461
+ what = r["what"] or ""
462
+ if r["kind"] in ("edit", "edit_created"):
463
+ h = conn.execute("SELECT table_name, old_row, new_row, tool, undone_at FROM "
464
+ "record_history WHERE id = ?", (r["ref"],)).fetchone()
465
+ if h is not None:
466
+ what = describe_edit(h["table_name"], _load(h["old_row"]), _load(h["new_row"]),
467
+ h["tool"], bool(h["undone_at"]))
468
+ st = state(r["record_type"], r["record_id"])
469
+ other = state(r["rt2"], r["rid2"]) if r["rt2"] and r["rid2"] is not None else None
470
+ if r["kind"] in ("link_added", "link_removed") and r["extra"] == "supersedes":
471
+ what = (("removed: " if r["kind"] == "link_removed" else "")
472
+ + f"{r['record_type']} #{r['record_id']} replaces {r['rt2']} #{r['rid2']}")
473
+ out.append({
474
+ "at": who.local_time(r["at"]), "at_utc": r["at"], "event": r["kind"],
475
+ "type": r["cat"], "what": " ".join(what.split())[:200],
476
+ "app": r["app"], "model": r["model"], "machine": r["machine"],
477
+ "identity_source": r["identity_source"],
478
+ "identity": identity.label(r["identity_source"]),
479
+ "who": identity.shown_row(r, fallback="not recorded"),
480
+ **({"via": v} if (v := identity.via(r["machine"], r["identity_source"])) else {}),
481
+ "record_type": r["record_type"], "record_id": r["record_id"],
482
+ "label": st["label"], "state": st["state"],
483
+ **({"other": {"record_type": r["rt2"], "record_id": r["rid2"],
484
+ "label": other["label"], "state": other["state"]}} if other else {})})
485
+ return out
486
+
487
+
488
+ def _range(since, until):
489
+ try:
490
+ return who.parse_time(since), who.parse_time(until, end=True)
491
+ except who.WhoError as e:
492
+ raise HistoryPageError("bad_time", e.payload["message"]) from e
493
+
494
+
495
+ def cutoff() -> str:
496
+ """Before this (UTC), the activity log and the session ledger have faded."""
497
+ return (datetime.now(timezone.utc) - timedelta(days=activity.keep_days())).strftime(
498
+ "%Y-%m-%d %H:%M:%S")
499
+
500
+
501
+ def timeline(db_path: Path, project_id: int, rtype: Optional[str] = None,
502
+ app: Optional[str] = None, since=None, until=None, q: Optional[str] = None,
503
+ record: Optional[tuple] = None, page: int = 1, per_page: int = PER_PAGE,
504
+ newest_first: bool = False) -> dict:
505
+ """One page of a project's entries, oldest first (or newest first). Filters: type (a
506
+ record type, or review / link / conflict / session), app (app or machine contains, any
507
+ case), since / until (this computer's local time), q (search: the entry's line, or the
508
+ live text or any stored copy of a record it names), record = (type, id): that record's
509
+ entries only. {"rows", "total", "page", "pages", "per_page", "types", "faded"}."""
510
+ page = max(1, int(page or 1))
511
+ per_page = max(1, min(int(per_page or PER_PAGE), MAX_PER_PAGE))
512
+ since_u, until_u = _range(since, until)
513
+ conn = _connect(db_path)
514
+ try:
515
+ t = _tables(conn)
516
+ parts = _sources(conn, t)
517
+ empty = {"rows": [], "total": 0, "page": 1, "pages": 1, "per_page": per_page,
518
+ "types": [], "faded": None}
519
+ if not parts:
520
+ return empty
521
+ union = " UNION ALL ".join(parts)
522
+ q = (q or "").strip()
523
+ keys = (q, _matches(conn, t, project_id, q)) if q else None
524
+ cond, params = _where(project_id, rtype, app, since_u, until_u, keys, record)
525
+ base = f"SELECT u.*, {_CAT} AS cat FROM ({union}) u WHERE {cond}"
526
+ total = conn.execute(f"SELECT COUNT(*) FROM ({base})", params).fetchone()[0]
527
+ order = "DESC" if newest_first else "ASC"
528
+ rows = [dict(r) for r in conn.execute(
529
+ f"SELECT * FROM ({base}) ORDER BY at {order}, CASE WHEN record_type = 'project' AND "
530
+ f"kind = 'created' THEN 0 WHEN kind IN ('session_opened', 'created', 'edit_created') "
531
+ f"THEN 1 ELSE 2 END {order}, ref {order} LIMIT ? OFFSET ?",
532
+ (*params, per_page, (page - 1) * per_page))]
533
+ all_cond, all_params = _where(project_id, None, None, None, None, None, record)
534
+ span = conn.execute(f"SELECT MIN(u.at), group_concat(DISTINCT {_CAT}) FROM ({union}) u "
535
+ f"WHERE {all_cond}", all_params).fetchone()
536
+ types = sorted(x for x in (span[1] or "").split(",") if x)
537
+ cut = cutoff()
538
+ faded = None
539
+ if span[0] and span[0] < cut and (not since_u or since_u < cut):
540
+ faded = {"before": who.local_time(cut)[:10], "days": activity.keep_days()}
541
+ return {"rows": _decorate(conn, t, rows), "total": total, "page": page,
542
+ "per_page": per_page, "pages": max(1, -(-total // per_page)), "types": types,
543
+ "faded": faded}
544
+ finally:
545
+ conn.close()
546
+
547
+
548
+ def record_projects(db_path: Path, rtype: str, rid: int) -> Optional[set]:
549
+ """The projects a record belongs to (a link: both of its ends), from its live row, else
550
+ from the edit history's newest copy. None: not known here (an erased record, an unknown
551
+ type or id), so the page goes by what the project's own history names (task 75)."""
552
+ table = history.TABLES.get(rtype) or {"review_item": "review_items",
553
+ "conflict": "decision_conflicts"}.get(rtype)
554
+ conn = dbconn.connect(str(db_path), timeout=5.0)
555
+ conn.row_factory = sqlite3.Row
556
+ try:
557
+ conn.execute("PRAGMA query_only = ON")
558
+ t = _tables(conn)
559
+ if not table or table not in t or rid is None:
560
+ return None
561
+ row = conn.execute(f"SELECT * FROM {table} WHERE id = ?", (rid,)).fetchone()
562
+ if row is not None:
563
+ row = dict(row)
564
+ own = history._project_of(conn, table, row)
565
+ elif "record_history" in t:
566
+ h = conn.execute("SELECT project_id, old_row, new_row FROM record_history WHERE "
567
+ "table_name = ? AND record_id = ? ORDER BY id DESC LIMIT 1",
568
+ (table, rid)).fetchone()
569
+ if h is None:
570
+ return None
571
+ row = _load(h["new_row"]) or _load(h["old_row"]) or {}
572
+ own = h["project_id"]
573
+ else:
574
+ return None
575
+ out = {x for x in (own, row.get("to_project_id") if table == "record_links" else None)
576
+ if isinstance(x, int)}
577
+ return out or None
578
+ finally:
579
+ conn.close()
580
+
581
+
582
+ def record_detail(db_path: Path, project_id: int, rtype: str, rid: int) -> dict:
583
+ """One record: its state and label, its live fields, and its whole edit history (each
584
+ edit with who and every changed field before and after). An erased record: state
585
+ "erased", label "[erased]", no fields, no history."""
586
+ conn = _connect(db_path)
587
+ try:
588
+ t = _tables(conn)
589
+ st = record_state(conn, t, rtype, rid)
590
+ out = {"record_type": rtype, "record_id": rid, "state": st["state"],
591
+ "label": st["label"], "fields": None, "edits": [], "more_edits": 0}
592
+ if st["state"] == "live" and st["row"] is not None:
593
+ out["fields"] = {k: v for k, v in st["row"].items()}
594
+ table = history.TABLES.get(rtype) or {"review_item": "review_items",
595
+ "conflict": "decision_conflicts"}.get(rtype)
596
+ if st["state"] == "erased" or "record_history" not in t or not table:
597
+ return out
598
+ where = "(table_name = ? AND record_id = ?)"
599
+ params = [table, rid]
600
+ for side in _SIDE:
601
+ where += f" OR ({history._SIDE_OF})"
602
+ params += [side, rtype, rid]
603
+ total = conn.execute(f"SELECT COUNT(*) FROM record_history WHERE {where}", params).fetchone()[0]
604
+ rows = conn.execute(f"SELECT * FROM record_history WHERE {where} ORDER BY id LIMIT ?",
605
+ (*params, MAX_EDITS)).fetchall()
606
+ out["more_edits"] = max(0, total - MAX_EDITS)
607
+ for r in rows:
608
+ old, new = _load(r["old_row"]), _load(r["new_row"])
609
+ keys = r.keys()
610
+ changes = {}
611
+ if old is not None and new is not None:
612
+ changes = {k: {"old": _short(old.get(k), FIELD_WIDTH), "new": _short(new.get(k), FIELD_WIDTH)}
613
+ for k in sorted(set(old) | set(new)) if old.get(k) != new.get(k)}
614
+ elif (old or new) is not None:
615
+ whole = new if new is not None else old
616
+ changes = {k: {"old": "" if new is not None else _short(v, FIELD_WIDTH),
617
+ "new": _short(v, FIELD_WIDTH) if new is not None else ""}
618
+ for k, v in whole.items() if v not in (None, "")}
619
+ out["edits"].append({
620
+ "at": who.local_time(r["created_at"]), "at_utc": r["created_at"],
621
+ "what": describe_edit(r["table_name"], old, new, r["tool"], bool(r["undone_at"])),
622
+ "tool": r["tool"], "changes": changes,
623
+ **{k: (r[k] if k in keys else None) for k in identity.FIELDS},
624
+ "identity": identity.label(r["identity_source"] if "identity_source" in keys else None),
625
+ "who": identity.shown_row({**dict(r), "app": (r["app"] if "app" in keys else None)
626
+ or r["client"]}, fallback="not recorded")})
627
+ return out
628
+ finally:
629
+ conn.close()
630
+
631
+
632
+ def todos(db_path: Path, project_id: int, status: str = "all", page: int = 1,
633
+ per_page: int = PER_PAGE) -> dict:
634
+ """A project's todos (live ones, and deleted ones the edit history still has), each with
635
+ who created it (app, model, machine, sign-in; "not recorded" before 2.4.0 once the
636
+ activity log has faded), its current status, any live claim and its last entry."""
637
+ status = status if status in TODO_STATUSES else "all"
638
+ page = max(1, int(page or 1))
639
+ per_page = max(1, min(int(per_page or PER_PAGE), MAX_PER_PAGE))
640
+ conn = _connect(db_path)
641
+ try:
642
+ t = _tables(conn)
643
+ if "todos" not in t:
644
+ return {"todos": [], "total": 0, "page": 1, "pages": 1, "per_page": per_page,
645
+ "counts": {}}
646
+ rh = "record_history" in t
647
+ deleted = ("SELECT DISTINCT h.record_id AS id FROM record_history h WHERE h.table_name = "
648
+ "'todos' AND h.project_id = ? AND NOT EXISTS (SELECT 1 FROM todos z WHERE z.id = "
649
+ "h.record_id)") if rh else "SELECT NULL AS id WHERE 0"
650
+ allq = (f"SELECT id, status, created_at, 0 AS gone FROM todos WHERE project_id = ? UNION ALL "
651
+ f"SELECT d.id, 'deleted', NULL, 1 FROM ({deleted}) d")
652
+ counts = {r[0]: r[1] for r in conn.execute(
653
+ f"SELECT CASE WHEN status IN ({', '.join('?' * len(TODO_OPEN))}) THEN 'open' ELSE status "
654
+ f"END, COUNT(*) FROM ({allq}) GROUP BY 1", (*TODO_OPEN, project_id, project_id))}
655
+ cond, params = "1", []
656
+ if status == "open":
657
+ cond, params = f"status IN ({', '.join('?' * len(TODO_OPEN))})", list(TODO_OPEN)
658
+ elif status != "all":
659
+ cond, params = "status = ?", [status]
660
+ total = conn.execute(f"SELECT COUNT(*) FROM ({allq}) WHERE {cond}",
661
+ (project_id, project_id, *params)).fetchone()[0]
662
+ ids = conn.execute(f"SELECT id, gone FROM ({allq}) WHERE {cond} ORDER BY gone, id DESC "
663
+ f"LIMIT ? OFFSET ?", (project_id, project_id, *params, per_page,
664
+ (page - 1) * per_page)).fetchall()
665
+ claims = "todo_claims" in t
666
+ out = []
667
+ for i in ids:
668
+ tid = i["id"]
669
+ st = record_state(conn, t, "todo", tid)
670
+ row = st["row"] or {}
671
+ maker = None
672
+ if rh:
673
+ maker = conn.execute("SELECT * FROM record_history WHERE table_name = 'todos' AND "
674
+ "record_id = ? AND old_row IS NULL ORDER BY id LIMIT 1",
675
+ (tid,)).fetchone()
676
+ if maker is None and "activity_log" in t and "record_type" in t["activity_log"]:
677
+ maker = conn.execute("SELECT * FROM activity_log WHERE record_type = 'todo' AND "
678
+ "record_id = ? AND ok = 1 ORDER BY id LIMIT 1", (tid,)).fetchone()
679
+ m = dict(maker) if maker is not None else {}
680
+ if m and not m.get("app"):
681
+ m["app"] = m.get("client")
682
+ created = m.get("created_at") or m.get("at") or row.get("created_at")
683
+ claim = None
684
+ if claims and st["state"] == "live":
685
+ c = conn.execute("SELECT * FROM todo_claims WHERE todo_id = ? AND expires_at > ?",
686
+ (tid, _now())).fetchone()
687
+ if c is not None:
688
+ claim = {"by": identity.shown_row(c), "until": who.local_time(c["expires_at"])}
689
+ last = None
690
+ if rh:
691
+ last = conn.execute("SELECT MAX(created_at) FROM record_history WHERE table_name = "
692
+ "'todos' AND record_id = ?", (tid,)).fetchone()[0]
693
+ if "todo_claim_events" in t:
694
+ ev = conn.execute("SELECT MAX(at) FROM todo_claim_events WHERE todo_id = ?",
695
+ (tid,)).fetchone()[0]
696
+ last = max(x for x in (last, ev) if x) if (last or ev) else None
697
+ out.append({"id": tid, "title": st["label"], "state": st["state"],
698
+ "status": row.get("status") if st["state"] == "live" else st["state"],
699
+ "priority": row.get("priority"),
700
+ "created_at": who.local_time(created) if created else "",
701
+ "creator": {"who": identity.shown_row(m, fallback="not recorded") if m
702
+ else "not recorded", "app": m.get("app"),
703
+ "model": m.get("model"), "machine": m.get("machine"),
704
+ "identity_source": m.get("identity_source"),
705
+ "identity": identity.label(m.get("identity_source"))},
706
+ "claim": claim, "last_at": who.local_time(last) if last else ""})
707
+ return {"todos": out, "total": total, "page": page, "per_page": per_page,
708
+ "pages": max(1, -(-total // per_page)), "counts": counts, "status": status}
709
+ finally:
710
+ conn.close()