Repository navigation
Expand file tree
/
Copy pathtask_server.py
More file actions
1371 lines (1222 loc) · 50.9 KB
/
Copy pathtask_server.py
File metadata and controls
1371 lines (1222 loc) · 50.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Thin MCP server exposing only task management tools.
Shares the same SQLite database as the main sqlite-kb server and keeps the task
surface independently deployable; ``unified_server.py`` also mounts it.
"""
from __future__ import annotations
import json
import sqlite3
import threading
import uuid
from typing import Any
from fastmcp_compat import FastMCP
from db_utils import (
get_conn as _get_conn,
get_conn_immediate as _get_conn_immediate,
fts_query as _fts_query,
TaskDAO,
TASK_ACTIVE_EXCLUSIONS as _TASK_ACTIVE_EXCLUSIONS,
TASK_PRIORITIES as _TASK_PRIORITIES,
validate_task_fields as _validate_task_fields,
build_priority_order_sql,
now_iso as _now,
setup_logger,
apply_task_mutation as _apply_task_mutation,
create_task_with_ledger as _create_task_with_ledger,
DB_PATH as _DB_PATH,
)
from retrieval_contract import (
RETRIEVAL_CONTRACT_VERSION,
classify_lookup_confidence,
is_visible_lookup_match,
order_surface_hits,
normalize_lookup_text,
score_lookup_surface,
)
from premium_runtime import maybe_mount_premium_extensions
from task_search import TaskSearchEngine
from smart_retrieval import (
READY_CONTEXT_CONTRACT_VERSION as _READY_CONTEXT_CONTRACT_VERSION,
prime_context as _prime_context,
ready_context as _ready_context,
suggested_ready as _suggested_ready,
)
# Explicit injection seam for authoritative task mutations. Production writes
# reserve the SQLite writer slot before their read/modify/write sequence; tests
# that replace the connection factory must replace this seam as well so an
# isolated fixture can never fall through to the live database.
_get_write_conn = _get_conn_immediate
# Pre-built SQL for active-task exclusion
_EXCL_PH = ",".join("?" for _ in _TASK_ACTIVE_EXCLUSIONS)
# ── Logging (file-only, NEVER stdout — breaks MCP stdio) ────────────────
logger = setup_logger("sqlite-tasks", "task_server.log")
_search_engine: TaskSearchEngine | None = None
def _get_search_engine() -> TaskSearchEngine:
global _search_engine
if _search_engine is None:
_search_engine = TaskSearchEngine()
return _search_engine
class _TaskEmbeddingScheduler:
"""Coalesce optional embedding refreshes onto one daemon worker."""
def __init__(self) -> None:
self._lock = threading.Lock()
self._pending: dict[str, None] = {}
self._running = False
def request(self, task_id: str) -> None:
with self._lock:
self._pending[task_id] = None
if self._running:
return
self._running = True
threading.Thread(
target=self._run,
name="sqlite-task-embedding",
daemon=True,
).start()
def _run(self) -> None:
while True:
with self._lock:
if not self._pending:
self._running = False
return
task_id = next(iter(self._pending))
self._pending.pop(task_id, None)
try:
from vec_search import vec_sync_task_detached
vec_sync_task_detached(_DB_PATH, task_id)
except Exception as exc:
logger.debug(
"detached vec_sync_task(%s) skipped: %s",
task_id,
exc,
exc_info=True,
)
_task_embedding_scheduler = _TaskEmbeddingScheduler()
def _vec_sync_task_safe(task_id: str) -> None:
"""Schedule derived embedding work after the authoritative commit."""
try:
_task_embedding_scheduler.request(task_id)
except Exception as exc:
# The authoritative task is already committed. Optional cache
# scheduling must never turn that success into a retry/duplicate.
logger.debug(
"task embedding scheduling skipped for %s: %s",
task_id,
exc,
exc_info=True,
)
def _normalize_title_key(value: str | None) -> str:
return " ".join((value or "").casefold().split())
def _find_note_by_title_project(
conn, *, title: str, project: str | None
) -> dict[str, Any] | None:
title_key = _normalize_title_key(title)
if not title_key:
return None
project_predicate = "project IS NULL" if project is None else "project = ?"
params = () if project is None else (project,)
rows = conn.execute(
f"SELECT id, title FROM tasks WHERE type = 'note' AND {project_predicate}",
params,
).fetchall()
for row in rows:
if _normalize_title_key(row["title"]) == title_key:
return dict(row)
return None
# ── FastMCP app ──────────────────────────────────────────────────────────
mcp = FastMCP(
"sqlite-tasks",
instructions=(
"Task management tools for SQLite-backed persistent memory. "
"Create, update, query, and digest tasks. "
"query_tasks returns live work by default: done/archived/cancelled rows are "
"excluded unless you pass an explicit status or include_completed=True. "
"Use find_by_title when only a remembered phrase is known: it searches tasks, notes, "
"and entities across title/name, description, notes, observations, and project "
"regardless of section, type, or project filters, using retrieval contract "
f"{RETRIEVAL_CONTRACT_VERSION} with confidence gating; finished rows stay "
"reachable but always rank below live work. "
"Use upsert_note_by_title_project for idempotent research/decision notes "
"when a repeated agent run must update an existing title/project instead of "
"creating duplicates. "
"Use description as the default primary body for task/note content; "
"use notes only for auxiliary/internal metadata. Shares DB with sqlite-kb."
),
)
# ═══════════════════════════════════════════════════════════════════════════
# Tool 1: create_task_or_note
# ═══════════════════════════════════════════════════════════════════════════
@mcp.tool()
def create_task_or_note(
title: str,
type: str = "task",
description: str = "",
section: str = "inbox",
priority: str = "medium",
due_date: str = "",
project: str = "",
parent_id: str = "",
notes: str = "",
recurring: str = "",
reminder_at: str = "",
) -> str:
"""Create a new task or note. Returns the UUID.
Put the main long-form task/note text in ``description`` by default.
Use ``notes`` only for auxiliary, internal, or machine-readable metadata.
Args:
title: Task title (required).
type: task | note.
description: Primary task/note body and main long-form content.
section: inbox | today | next | someday | waiting.
priority: low | medium | high | critical.
due_date: YYYY-MM-DD format or empty to skip.
project: Project tag for grouping.
parent_id: UUID of parent task (for subtasks).
notes: Secondary/internal notes or machine-readable metadata.
recurring: JSON config for recurrence (e.g. '{"every":"week","day":"monday"}').
reminder_at: ISO datetime for reminder (e.g. '2026-03-15T14:00:00').
"""
# Normalize empty strings to None
description = description or None
due_date = due_date or None
project = project or None
parent_id = parent_id or None
notes = notes or None
recurring = recurring or None
reminder_at = reminder_at or None
task_id = str(uuid.uuid4())
now = _now()
if err := _validate_task_fields(
section=section,
priority=priority,
type=type,
due_date=due_date,
recurring=recurring,
reminder_at=reminder_at,
):
return json.dumps({"error": err}, ensure_ascii=False)
with _get_write_conn() as conn:
if parent_id:
if not TaskDAO.exists(conn, parent_id):
return json.dumps(
{"error": f"Parent task {parent_id} not found"}, ensure_ascii=False
)
_create_task_with_ledger(
conn,
task_id,
title,
now,
description=description,
status="not_started",
priority=priority,
section=section,
due_date=due_date,
project=project,
parent_id=parent_id,
notes=notes,
recurring=recurring,
reminder_at=reminder_at,
type=type,
tool_name="sqlite-tasks.create_task_or_note",
)
_vec_sync_task_safe(task_id)
logger.info("create_task_or_note: %s (%s)", title, task_id)
return json.dumps(
{"task_id": task_id, "title": title, "type": type, "status": "not_started"},
ensure_ascii=False,
)
# ═══════════════════════════════════════════════════════════════════════════
# Tool 1b: upsert_note_by_title_project
# ═══════════════════════════════════════════════════════════════════════════
@mcp.tool()
def upsert_note_by_title_project(
title: str,
project: str = "",
description: str = "",
notes: str = "",
section: str = "",
priority: str = "",
update_if_found: bool = True,
) -> str:
"""Create or update a note by exact normalized title + project.
This is the idempotent write surface for durable research/decision notes.
It prevents repeated agent retries from creating near-duplicate note rows.
Matching rule:
- exact normalized title + project;
- no fuzzy overwrite;
- updates go through the task mutation ledger.
Args:
title: Note title (required).
project: Project tag for matching and grouping.
description: Primary long-form note body.
notes: Optional auxiliary/internal metadata.
section: Section for new notes; updates only when explicitly set.
priority: Priority for new notes; updates only when explicitly set.
update_if_found: When false, return the existing row without mutation.
"""
title = (title or "").strip()
if not title:
return json.dumps({"error": "title is required"}, ensure_ascii=False)
project_value = project.strip() if project else None
create_section = section or "next"
create_priority = priority or "medium"
if err := _validate_task_fields(
section=create_section,
priority=create_priority,
type="note",
):
return json.dumps({"error": err}, ensure_ascii=False)
update_validation = {
key: value
for key, value in {"section": section, "priority": priority}.items()
if value
}
if update_validation and (err := _validate_task_fields(**update_validation)):
return json.dumps({"error": err}, ensure_ascii=False)
now = _now()
embed_task_id: str | None = None
response: dict[str, Any]
with _get_write_conn() as conn:
existing = _find_note_by_title_project(conn, title=title, project=project_value)
if existing:
if not update_if_found:
return json.dumps(
{
"task_id": existing["id"],
"title": existing["title"],
"type": "note",
"action": "existing",
"matched_on": "normalized_title_project",
},
ensure_ascii=False,
)
updates: dict[str, Any] = {}
if description:
updates["description"] = description
if notes:
updates["notes"] = notes
if section:
updates["section"] = section
if priority:
updates["priority"] = priority
if not updates:
return json.dumps(
{
"task_id": existing["id"],
"title": existing["title"],
"type": "note",
"action": "existing",
"matched_on": "normalized_title_project",
},
ensure_ascii=False,
)
result = _apply_task_mutation(
conn,
existing["id"],
updates,
timestamp=now,
tool_name="sqlite-tasks.upsert_note_by_title_project",
)
if {"title", "description", "notes"} & set(
result.get("changed_fields", ())
):
embed_task_id = existing["id"]
response = {
"task_id": existing["id"],
"title": title,
"type": "note",
"action": "updated",
"fields": result.get("changed_fields", []),
"matched_on": "normalized_title_project",
}
else:
task_id = str(uuid.uuid4())
_create_task_with_ledger(
conn,
task_id,
title,
now,
description=description or None,
status="not_started",
priority=create_priority,
section=create_section,
project=project_value,
notes=notes or None,
type="note",
tool_name="sqlite-tasks.upsert_note_by_title_project",
)
embed_task_id = task_id
response = {
"task_id": task_id,
"title": title,
"type": "note",
"status": "not_started",
"action": "created",
"matched_on": "normalized_title_project",
}
if embed_task_id:
_vec_sync_task_safe(embed_task_id)
logger.info(
"upsert_note_by_title_project: %s (%s)",
title,
response["task_id"],
)
return json.dumps(response, ensure_ascii=False)
# ═══════════════════════════════════════════════════════════════════════════
# Tool 2: update_task
# ═══════════════════════════════════════════════════════════════════════════
@mcp.tool()
def update_task(
task_id: str,
title: str = "",
description: str = "",
status: str = "",
priority: str = "",
section: str = "",
due_date: str = "",
project: str = "",
parent_id: str = "",
notes: str = "",
recurring: str = "",
reminder_at: str = "",
type: str = "",
) -> str:
"""Update a task's fields. Only non-empty fields are changed.
Pass special value "CLEAR" to set a field to NULL.
Explicitly confirming an archived/cancelled status whose row conflicts with
visible field/event authority records that confirmation once. Repeating a
consistent same-status command remains a no-op.
``description`` is the primary body field for task/note text.
``notes`` is reserved for secondary/internal metadata.
Args:
task_id: UUID of the task to update (required).
title: New title.
description: New main task/note body.
status: not_started | in_progress | done | archived | cancelled.
priority: low | medium | high | critical.
section: inbox | today | next | someday | waiting.
due_date: YYYY-MM-DD or "CLEAR" to remove.
project: Project tag or "CLEAR" to remove.
parent_id: Parent UUID or "CLEAR" to remove.
notes: New auxiliary/internal notes or "CLEAR" to remove.
recurring: JSON config or "CLEAR" to remove.
reminder_at: ISO datetime or "CLEAR" to remove.
type: task | note.
"""
fields = {
"title": title,
"description": description,
"status": status,
"priority": priority,
"section": section,
"due_date": due_date,
"project": project,
"parent_id": parent_id,
"notes": notes,
"recurring": recurring,
"reminder_at": reminder_at,
"type": type,
}
updates = {}
for k, v in fields.items():
if v == "CLEAR":
updates[k] = None
elif v: # non-empty string = update
updates[k] = v
if not updates:
return json.dumps(
{"error": "No fields to update. Pass non-empty values."}, ensure_ascii=False
)
val_fields = {
k: v
for k, v in updates.items()
if k
in (
"status",
"section",
"priority",
"type",
"due_date",
"recurring",
"reminder_at",
)
and v is not None
}
if err := _validate_task_fields(**val_fields):
return json.dumps({"error": err}, ensure_ascii=False)
updates["updated_at"] = _now()
reembed = False
with _get_write_conn() as conn:
result = _apply_task_mutation(
conn,
task_id,
{k: v for k, v in updates.items() if k != "updated_at"},
timestamp=updates["updated_at"],
tool_name="sqlite-tasks.update_task",
explicit_clear_fields=("parent_id",) if parent_id == "CLEAR" else (),
)
if result.get("updated", 0) == 0 and result.get("missing"):
return json.dumps(
{"error": f"Task {task_id} not found"}, ensure_ascii=False
)
# Re-embed if content fields changed
if {"title", "description", "notes"} & set(result.get("changed_fields", ())):
reembed = True
if reembed:
_vec_sync_task_safe(task_id)
logger.info("update_task: %s updated %s", task_id, list(updates.keys()))
return json.dumps(
{"updated": task_id, "fields": list(updates.keys())}, ensure_ascii=False
)
# ═══════════════════════════════════════════════════════════════════════════
# Tool 3: query_tasks
# ═══════════════════════════════════════════════════════════════════════════
# Allowlist: maps a caller-supplied sort key to a hardcoded SQL expression.
# Values are NEVER derived from caller input — this is the SQL-injection guard.
# "priority" maps to the enum CASE (critical-first), not the raw TEXT column.
_QUERY_TASKS_SORT_COLUMNS: dict[str, str] = {
"created_at": "t.created_at",
"updated_at": "t.updated_at",
"due_date": "t.due_date",
"priority": build_priority_order_sql("t."),
"status": "t.status",
"title": "t.title",
"project": "t.project",
"section": "t.section",
"type": "t.type",
}
# Per-field default direction when sort_order is omitted. Dates/recency and
# priority lead with the most actionable rows; text fields are alphabetical.
# For "priority" the CASE is critical=0..low=3, so "desc" (highest first)
# means CASE ASC — handled explicitly in the builder below.
_QUERY_TASKS_SORT_DEFAULT_DIR: dict[str, str] = {
"created_at": "desc",
"updated_at": "desc",
"due_date": "asc",
"priority": "desc",
"status": "asc",
"title": "asc",
"project": "asc",
"section": "asc",
"type": "asc",
}
# Default ORDER BY when sort_by is empty — byte-for-byte the historical
# behavior (priority critical-first, due_date ASC NULLS LAST, created_at ASC).
_QUERY_TASKS_DEFAULT_ORDER = (
f"{build_priority_order_sql('t.')}, t.due_date ASC NULLS LAST, t.created_at ASC"
)
def _build_query_tasks_order(sort_by: str, sort_order: str) -> tuple[str, str | None]:
"""Build a validated ORDER BY clause for query_tasks.
Returns (order_clause, error). When sort_by is empty, returns the
historical default and no error. On invalid sort_by/sort_order, returns
("", error_message). The SQL column is taken only from the hardcoded
allowlist — caller input is never interpolated into SQL.
"""
if not sort_by:
return _QUERY_TASKS_DEFAULT_ORDER, None
col = _QUERY_TASKS_SORT_COLUMNS.get(sort_by)
if col is None:
allowed = ", ".join(sorted(_QUERY_TASKS_SORT_COLUMNS))
return "", f"invalid_sort_by: {sort_by}; allowed: [{allowed}]"
if sort_order:
direction = sort_order.lower()
if direction not in ("asc", "desc"):
return "", f"invalid_sort_order: {sort_order}; allowed: [asc, desc]"
else:
direction = _QUERY_TASKS_SORT_DEFAULT_DIR[sort_by]
if sort_by == "priority":
# CASE is critical=0..low=3; highest-first ("desc") == CASE ASC.
sql_dir = "ASC" if direction == "desc" else "DESC"
primary = f"{col} {sql_dir}"
elif sort_by == "due_date":
# NULLS LAST so undated tasks never crowd the top of an ASC sort.
sql_dir = "ASC" if direction == "asc" else "DESC"
primary = f"{col} {sql_dir} NULLS LAST"
else:
sql_dir = "ASC" if direction == "asc" else "DESC"
primary = f"{col} {sql_dir}"
# Deterministic tie-breaker so equal keys never reorder between calls.
return f"{primary}, t.created_at DESC, t.id ASC", None
@mcp.tool()
def query_tasks(
section: str = "",
status: str = "",
priority: str = "",
project: str = "",
parent_id: str = "",
type: str = "",
overdue_only: bool = False,
search: str = "",
summary_only: bool = False,
offset: int = 0,
limit: int = 50,
sort_by: str = "",
sort_order: str = "",
include_completed: bool = False,
) -> str:
"""Query tasks with optional filters. Returns markdown table.
Filters are combined with AND. Leave empty to skip a filter.
overdue_only=True shows only tasks past due_date.
search: full-text search across title, description, notes.
summary_only=True omits description/notes (faster).
Liveness default: finished rows (done/archived/cancelled) keep their
due_date, so an unfiltered query would otherwise be dominated by
completed work under the priority/due_date default order. Unless the
caller names a ``status`` explicitly, or sets include_completed=True,
finished rows are excluded. ``overdue_only`` keeps its historical
unconditional exclusion — finished work is never "overdue".
include_completed: opt back in to done/archived/cancelled rows when no
explicit ``status`` filter is given (e.g. for history/audit views).
sort_by: optional column to sort by. Empty ("") preserves the default
ordering (priority critical-first, then due_date ASC NULLS LAST,
then created_at ASC). Allowed: created_at, updated_at, due_date,
priority, status, title, project, section, type. Any other value
returns an error (no silent fallback).
sort_order: "asc" or "desc" (case-insensitive). Empty ("") uses a
sensible per-field default: due_date ASC (soonest first);
created_at/updated_at DESC (most recent first); priority DESC
(highest first); title/project/section/status/type ASC. Ignored
when sort_by is empty. Any other value returns an error.
Note: when ``search`` is set, results are re-ranked by the search engine,
so ``sort_by`` has little visible effect on that path.
"""
order_clause, sort_err = _build_query_tasks_order(sort_by, sort_order)
if sort_err is not None:
return json.dumps({"error": sort_err}, ensure_ascii=False)
conditions: list[str] = []
params: list[Any] = []
if section:
conditions.append("t.section = ?")
params.append(section)
if status:
conditions.append("t.status = ?")
params.append(status)
if priority:
conditions.append("t.priority = ?")
params.append(priority)
if project:
conditions.append("t.project = ?")
params.append(project)
if parent_id:
conditions.append("t.parent_id = ?")
params.append(parent_id)
if type:
conditions.append("t.type = ?")
params.append(type)
if overdue_only:
conditions.append("t.due_date < date('now')")
# Liveness guard. Without it the caller-argument loop above can leave
# ``WHERE 1=1``, and because completed tasks retain their due_date the
# default order (priority, then due_date ASC NULLS LAST) buries live work
# behind finished rows. An explicit ``status`` means the caller already
# named the lifecycle they want, so it is left untouched.
if overdue_only or not (status or include_completed):
conditions.append(f"t.status NOT IN ({_EXCL_PH})")
params.extend(_TASK_ACTIVE_EXCLUSIONS)
if summary_only:
cols = "t.id, t.title, t.status, t.priority, t.section, t.due_date, t.project, t.parent_id, t.notes, t.created_at, t.updated_at"
else:
cols = "t.id, t.title, t.description, t.notes, t.status, t.priority, t.section, t.due_date, t.project, t.parent_id, t.created_at, t.updated_at"
from_clause = "tasks t"
where = " AND ".join(conditions) if conditions else "1=1"
with _get_conn() as conn:
if search:
# FTS5 pre-filter: narrow rows before search engine re-ranks
fts_tokens = search.split()
fts_where = where
fts_params = list(params)
if fts_tokens:
# Prefix match (token*) so pre-filter is broader than
# the search engine's fuzzy/substring matching
escaped = []
for t in fts_tokens:
clean = "".join(c for c in t if c.isalnum() or c == "_")
if clean:
escaped.append(clean + "*")
fts_match = " OR ".join(escaped) if escaped else None
if fts_match:
try:
# Verify FTS5 query is valid before using it
conn.execute(
"SELECT 1 FROM tasks_fts WHERE tasks_fts MATCH ? LIMIT 1",
(fts_match,),
)
fts_where = (
f"{where} AND t.rowid IN "
f"(SELECT rowid FROM tasks_fts WHERE tasks_fts MATCH ?)"
)
fts_params.append(fts_match)
except sqlite3.Error as e:
logger.debug(
"Task FTS5 prefilter failed for %r: %s",
search,
e,
exc_info=True,
)
sql = (
f"SELECT {cols} FROM {from_clause} WHERE {fts_where} "
f"ORDER BY {order_clause}"
)
all_rows = conn.execute(sql, fts_params).fetchall()
results = _get_search_engine().search(
search, [dict(r) for r in all_rows], conn=conn
)
total = len(results)
rows = results[offset : offset + limit]
else:
sql = (
f"SELECT {cols} FROM {from_clause} WHERE {where} "
f"ORDER BY {order_clause} "
f"LIMIT ? OFFSET ?"
)
rows = conn.execute(sql, params + [limit, offset]).fetchall()
count_sql = f"SELECT COUNT(*) FROM {from_clause} WHERE {where}"
total = conn.execute(count_sql, params).fetchone()[0]
rows = [dict(r) if not isinstance(r, dict) else r for r in rows] if rows else []
if not rows:
return json.dumps(
{"tasks": [], "count": 0, "total": total, "message": "No tasks match"},
ensure_ascii=False,
)
lines = [
"| # | Title | Status | Priority | Section | Due | Project | Created | Notes |",
"|---|-------|--------|----------|---------|-----|---------|---------|-------|",
]
for i, r in enumerate(rows, 1):
due = r["due_date"] or "—"
proj = r["project"] or "—"
created = (r.get("created_at") or "—")[:16]
notes = (r["notes"] or "—")[:80]
lines.append(
f"| {i + offset} | {r['title']} | {r['status']} | {r['priority']} "
f"| {r['section']} | {due} | {proj} | {created} | {notes} |"
)
result = {
"tasks": rows,
"count": len(rows),
"total": total,
"offset": offset,
"limit": limit,
"markdown": "\n".join(lines),
}
if total > offset + limit:
result["has_more"] = True
result["next_offset"] = offset + limit
return json.dumps(result, ensure_ascii=False)
# ═══════════════════════════════════════════════════════════════════════════
# Tool 3b: find_by_title
# ═══════════════════════════════════════════════════════════════════════════
_LOOKUP_TASK_COLUMNS = (
"id, title, description, notes, type, status, section, priority, due_date, "
"project, updated_at, created_at"
)
_LOOKUP_ENTITY_COLUMNS = "id, name, entity_type, project, updated_at, created_at"
def _lookup_prefilter_ids(
conn: sqlite3.Connection, query: str, candidate_limit: int
) -> tuple[list[int], list[int]] | None:
"""Return broad FTS/project candidates, or None for exact full-scan fallback."""
try:
fts_q = _fts_query(query)
task_ids = {
row["rowid"]
for row in conn.execute(
"SELECT rowid FROM tasks_fts WHERE tasks_fts MATCH ? "
"ORDER BY rank LIMIT ?",
(fts_q, candidate_limit),
)
}
entity_ids = {
row["rowid"]
for row in conn.execute(
"SELECT rowid FROM memory_fts WHERE memory_fts MATCH ? "
"ORDER BY rank LIMIT ?",
(fts_q, candidate_limit),
)
}
# Project is part of the retrieval contract but is not indexed by either
# FTS table. Include normalized project phrase hits so the prefilter does
# not erase project-only recall (covered by the locked eval corpus).
normalized = normalize_lookup_text(query)
if normalized and normalized.isascii():
project_expr = (
"lower(replace(replace(coalesce(project, ''), '-', ' '), '_', ' '))"
)
task_ids.update(
row["rowid"]
for row in conn.execute(
f"SELECT rowid FROM tasks WHERE instr({project_expr}, ?) > 0 "
"LIMIT ?",
(normalized, candidate_limit),
)
)
entity_ids.update(
row["id"]
for row in conn.execute(
f"SELECT id FROM entities WHERE instr({project_expr}, ?) > 0 "
"LIMIT ?",
(normalized, candidate_limit),
)
)
except sqlite3.Error:
return None
if not task_ids and not entity_ids:
return None
return sorted(task_ids), sorted(entity_ids)
def _lookup_rows(
conn: sqlite3.Connection,
query: str,
limit: int,
*,
force_full_scan: bool = False,
) -> tuple[list[sqlite3.Row], list[sqlite3.Row], dict[int, list[str]], str]:
"""Load only lookup candidates; retain an exact full-scan fallback."""
candidate_ids = None
if not force_full_scan:
# Stay below SQLite's historical 999 host-parameter ceiling because
# candidate IDs are consumed by one IN (...) query per surface.
candidate_limit = min(900, max(200, limit * 10))
candidate_ids = _lookup_prefilter_ids(conn, query, candidate_limit)
if candidate_ids is None:
# No status filter here, deliberately. The liveness sort key already
# guarantees a finished row never outranks a live one, so excluding
# them buys nothing for ranking. It would only change behaviour in the
# one case where there is no live answer at all — turning a
# low-confidence closed match into "no result". This is the last-resort
# recall path of a system whose job is remembering past work, so
# dropping finished rows from it costs more than it returns.
task_rows = conn.execute(f"SELECT {_LOOKUP_TASK_COLUMNS} FROM tasks").fetchall()
entity_rows = conn.execute(
f"SELECT {_LOOKUP_ENTITY_COLUMNS} FROM entities"
).fetchall()
strategy = "full_scan_fallback"
else:
task_ids, entity_ids = candidate_ids
if task_ids:
ph = ",".join("?" * len(task_ids))
task_rows = conn.execute(
f"SELECT {_LOOKUP_TASK_COLUMNS} FROM tasks WHERE rowid IN ({ph})",
task_ids,
).fetchall()
else:
task_rows = []
if entity_ids:
ph = ",".join("?" * len(entity_ids))
entity_rows = conn.execute(
f"SELECT {_LOOKUP_ENTITY_COLUMNS} FROM entities WHERE id IN ({ph})",
entity_ids,
).fetchall()
else:
entity_rows = []
strategy = "fts_prefilter"
obs_by_entity: dict[int, list[str]] = {}
if entity_rows:
entity_ids = [row["id"] for row in entity_rows]
ph = ",".join("?" * len(entity_ids))
for row in conn.execute(
f"SELECT entity_id, content FROM observations "
f"WHERE entity_id IN ({ph}) ORDER BY entity_id, id",
entity_ids,
):
obs_by_entity.setdefault(row["entity_id"], []).append(row["content"])
return task_rows, entity_rows, obs_by_entity, strategy
def _score_task_lookup(row: sqlite3.Row, query: str) -> dict[str, Any] | None:
title = row["title"] or ""
surface_scores = {
surface: score
for surface, score in (
("title", score_lookup_surface("title", query, title)),
(
"description",
score_lookup_surface("description", query, row["description"]),
),
("notes", score_lookup_surface("notes", query, row["notes"])),
("project", score_lookup_surface("project", query, row["project"])),
)
if score > 0
}
if not surface_scores:
return None
ordered_hits = order_surface_hits(surface_scores)
matched_in = [surface for surface, _ in ordered_hits]
return {
"kind": "note" if row["type"] == "note" else "task",
"id": row["id"],
"title": title,
"type": row["type"],
"status": row["status"],
"section": row["section"],
"priority": row["priority"],
"due_date": row["due_date"],
"project": row["project"],
"updated_at": row["updated_at"],
"created_at": row["created_at"],
"score": float(ordered_hits[0][1]),
"matched_in": matched_in,
"surface_scores": surface_scores,
"primary_surface": matched_in[0],
"confidence": classify_lookup_confidence(surface_scores),
"ranking_contract_version": RETRIEVAL_CONTRACT_VERSION,
}
def _score_entity_lookup(
row: sqlite3.Row,
query: str,
observations: list[str],
) -> dict[str, Any] | None:
name = row["name"] or ""
obs_text = "\n".join(observations)
surface_scores = {
surface: score
for surface, score in (
("name", score_lookup_surface("name", query, name)),
(
"observations",
score_lookup_surface("observations", query, obs_text),
),
("project", score_lookup_surface("project", query, row["project"])),
(
"entity_type",
score_lookup_surface("entity_type", query, row["entity_type"]),
),
)
if score > 0
}
if not surface_scores:
return None
ordered_hits = order_surface_hits(surface_scores)
matched_in = [surface for surface, _ in ordered_hits]
return {
"kind": "entity",
"id": row["id"],
"title": name,
"entityType": row["entity_type"],
"project": row["project"],
"updated_at": row["updated_at"],
"created_at": row["created_at"],
"score": float(ordered_hits[0][1]),
"matched_in": matched_in,
"surface_scores": surface_scores,
"primary_surface": matched_in[0],
"confidence": classify_lookup_confidence(surface_scores),