backlog-tiny-mcp 0.34.2__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.
- backlog_mcp/__init__.py +29 -0
- backlog_mcp/__main__.py +10 -0
- backlog_mcp/auth.py +199 -0
- backlog_mcp/backlog.py +696 -0
- backlog_mcp/cli.py +2046 -0
- backlog_mcp/client.py +320 -0
- backlog_mcp/computer.py +33 -0
- backlog_mcp/database.py +80 -0
- backlog_mcp/embeddings.py +900 -0
- backlog_mcp/global_rules.md +126 -0
- backlog_mcp/guidance.py +11 -0
- backlog_mcp/guide/index.md +11 -0
- backlog_mcp/guide/memory.md +27 -0
- backlog_mcp/guide/projects.md +16 -0
- backlog_mcp/guide/search.md +72 -0
- backlog_mcp/guide/tasks.md +40 -0
- backlog_mcp/guide/workflow.md +22 -0
- backlog_mcp/httpd.py +270 -0
- backlog_mcp/migrate.py +113 -0
- backlog_mcp/migrations/0001-initial.sql +194 -0
- backlog_mcp/migrations/0002-task-assignment.sql +2 -0
- backlog_mcp/migrations/0003-task-edit-claims.sql +5 -0
- backlog_mcp/migrations/0004-memory-visibility.sql +42 -0
- backlog_mcp/model/CITATION.bib +9 -0
- backlog_mcp/model/LICENSE +202 -0
- backlog_mcp/model/README.md +12 -0
- backlog_mcp/model/UPSTREAM.md +2053 -0
- backlog_mcp/model/config.json +12 -0
- backlog_mcp/model/model.safetensors +4 -0
- backlog_mcp/model/modules.json +14 -0
- backlog_mcp/model/provenance.json +16 -0
- backlog_mcp/model/tokenizer.json +1 -0
- backlog_mcp/models.py +581 -0
- backlog_mcp/names.py +35 -0
- backlog_mcp/paths.py +53 -0
- backlog_mcp/protocol_storage.py +104 -0
- backlog_mcp/queries.py +7 -0
- backlog_mcp/queries.sql +531 -0
- backlog_mcp/records.py +215 -0
- backlog_mcp/render.py +479 -0
- backlog_mcp/server.py +2864 -0
- backlog_mcp/skills/autopilot/SKILL.md +96 -0
- backlog_mcp/skills/planning/SKILL.md +106 -0
- backlog_mcp/skills/planning/references/splitting.md +45 -0
- backlog_mcp/skills/triage/SKILL.md +104 -0
- backlog_mcp/skills/triage/references/conventions.md +43 -0
- backlog_mcp/skills.py +56 -0
- backlog_mcp/static/agent-config.js +20 -0
- backlog_mcp/static/app.css +1950 -0
- backlog_mcp/static/app.js +1301 -0
- backlog_mcp/static/assignments.js +96 -0
- backlog_mcp/static/breadcrumbs.js +54 -0
- backlog_mcp/static/comment-composer.js +95 -0
- backlog_mcp/static/connect.css +98 -0
- backlog_mcp/static/connect.html +74 -0
- backlog_mcp/static/connect.js +160 -0
- backlog_mcp/static/credentials.js +72 -0
- backlog_mcp/static/diagrams.js +128 -0
- backlog_mcp/static/dom.js +162 -0
- backlog_mcp/static/editor-claim.js +104 -0
- backlog_mcp/static/editor.js +355 -0
- backlog_mcp/static/index.html +137 -0
- backlog_mcp/static/markdown.css +33 -0
- backlog_mcp/static/markdown.js +72 -0
- backlog_mcp/static/mcp.js +177 -0
- backlog_mcp/static/memory-append.js +34 -0
- backlog_mcp/static/memory-editor.js +173 -0
- backlog_mcp/static/memory.js +279 -0
- backlog_mcp/static/package.json +1 -0
- backlog_mcp/static/project-form.js +71 -0
- backlog_mcp/static/project-settings.js +225 -0
- backlog_mcp/static/scope.js +90 -0
- backlog_mcp/static/statistics.js +87 -0
- backlog_mcp/static/task-browser.js +100 -0
- backlog_mcp/static/task-pagination.js +94 -0
- backlog_mcp/static/task-picker.js +164 -0
- backlog_mcp/static/task-table.js +74 -0
- backlog_mcp/static/vendor/README.md +12 -0
- backlog_mcp/static/vendor/dompurify-LICENSE +202 -0
- backlog_mcp/static/vendor/highlight-LICENSE +29 -0
- backlog_mcp/static/vendor/highlight-dark.css +118 -0
- backlog_mcp/static/vendor/highlight-light.css +118 -0
- backlog_mcp/static/vendor/highlight.min.js +1262 -0
- backlog_mcp/static/vendor/marked-LICENSE +44 -0
- backlog_mcp/static/vendor/marked.esm.js +80 -0
- backlog_mcp/static/vendor/marked.esm.js.map +7 -0
- backlog_mcp/static/vendor/mermaid-LICENSE +21 -0
- backlog_mcp/static/vendor/mermaid.min.js +4376 -0
- backlog_mcp/static/vendor/purify.es.mjs +2722 -0
- backlog_mcp/static/vendor/purify.es.mjs.map +1 -0
- backlog_mcp/transaction.py +47 -0
- backlog_mcp/version.py +79 -0
- backlog_mcp/webui.py +63 -0
- backlog_tiny_mcp-0.34.2.dist-info/METADATA +84 -0
- backlog_tiny_mcp-0.34.2.dist-info/RECORD +97 -0
- backlog_tiny_mcp-0.34.2.dist-info/WHEEL +4 -0
- backlog_tiny_mcp-0.34.2.dist-info/entry_points.txt +4 -0
backlog_mcp/server.py
ADDED
|
@@ -0,0 +1,2864 @@
|
|
|
1
|
+
"""The MCP surface: tools, resources, prompts and completion over one backlog."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import contextlib
|
|
6
|
+
import json
|
|
7
|
+
import logging
|
|
8
|
+
import time
|
|
9
|
+
from collections.abc import Iterable, Sequence
|
|
10
|
+
from contextvars import ContextVar
|
|
11
|
+
from importlib.resources import files
|
|
12
|
+
from typing import Any, Literal
|
|
13
|
+
|
|
14
|
+
import aiosqlite
|
|
15
|
+
from aiohttp_tiny_mcp import (
|
|
16
|
+
Authentication,
|
|
17
|
+
Exchange,
|
|
18
|
+
Extension,
|
|
19
|
+
Hint,
|
|
20
|
+
Icons,
|
|
21
|
+
NeedInput,
|
|
22
|
+
Registry,
|
|
23
|
+
elicit,
|
|
24
|
+
)
|
|
25
|
+
from aiohttp_tiny_mcp.protocol.models import CompleteParams, TextResourceContents
|
|
26
|
+
from aiohttp_tiny_mcp.storage.hub import NOTIFICATIONS, topic
|
|
27
|
+
from pydantic import BaseModel, Field, field_serializer
|
|
28
|
+
|
|
29
|
+
from . import embeddings, guidance, skills
|
|
30
|
+
from .backlog import (
|
|
31
|
+
Backlog,
|
|
32
|
+
Reach,
|
|
33
|
+
UnknownProject,
|
|
34
|
+
attach_roots,
|
|
35
|
+
computers,
|
|
36
|
+
members,
|
|
37
|
+
project_contents,
|
|
38
|
+
read_project,
|
|
39
|
+
read_projects,
|
|
40
|
+
require,
|
|
41
|
+
share,
|
|
42
|
+
slugs,
|
|
43
|
+
)
|
|
44
|
+
from .computer import hostname
|
|
45
|
+
from .models import (
|
|
46
|
+
CLOSED_STATUSES,
|
|
47
|
+
EVENT_KEY,
|
|
48
|
+
OPEN_STATUSES,
|
|
49
|
+
PRIORITIES,
|
|
50
|
+
RANKS,
|
|
51
|
+
Action,
|
|
52
|
+
Actor,
|
|
53
|
+
BoardCounts,
|
|
54
|
+
BoardProject,
|
|
55
|
+
BoardSection,
|
|
56
|
+
BoardTask,
|
|
57
|
+
CommentOut,
|
|
58
|
+
Comments,
|
|
59
|
+
Deleted,
|
|
60
|
+
Event,
|
|
61
|
+
FullNotes,
|
|
62
|
+
FullTasks,
|
|
63
|
+
Identity,
|
|
64
|
+
Kind,
|
|
65
|
+
LinkChange,
|
|
66
|
+
NoteListItem,
|
|
67
|
+
NoteOut,
|
|
68
|
+
Notes,
|
|
69
|
+
NullableAssignment,
|
|
70
|
+
NullableString,
|
|
71
|
+
Priority,
|
|
72
|
+
ProjectBoard,
|
|
73
|
+
ProjectDeleted,
|
|
74
|
+
ProjectOut,
|
|
75
|
+
ProjectRoot,
|
|
76
|
+
ProjectRootOut,
|
|
77
|
+
Projects,
|
|
78
|
+
Relation,
|
|
79
|
+
Stats,
|
|
80
|
+
Status,
|
|
81
|
+
TaskDetails,
|
|
82
|
+
TaskLinkOut,
|
|
83
|
+
TaskListItem,
|
|
84
|
+
TaskOut,
|
|
85
|
+
Tasks,
|
|
86
|
+
check_assignment,
|
|
87
|
+
relation,
|
|
88
|
+
)
|
|
89
|
+
from .names import owner_name, principal_label, principal_user
|
|
90
|
+
from .protocol_storage import ProtocolHub, ProtocolSessions, ProtocolStorage
|
|
91
|
+
from .queries import QUERIES
|
|
92
|
+
from .records import (
|
|
93
|
+
NOTE_LIMIT,
|
|
94
|
+
AssignmentConflict,
|
|
95
|
+
Comment,
|
|
96
|
+
Note,
|
|
97
|
+
Project,
|
|
98
|
+
Task,
|
|
99
|
+
Unset,
|
|
100
|
+
assignment_pattern,
|
|
101
|
+
check_note,
|
|
102
|
+
join_tags,
|
|
103
|
+
match_query,
|
|
104
|
+
split_tags,
|
|
105
|
+
to_note,
|
|
106
|
+
to_project,
|
|
107
|
+
to_task,
|
|
108
|
+
)
|
|
109
|
+
from .transaction import Transaction
|
|
110
|
+
from .version import VERSION
|
|
111
|
+
|
|
112
|
+
log = logging.getLogger(__name__)
|
|
113
|
+
|
|
114
|
+
INSTRUCTIONS = """Backlog is a task tracker and memory for agents.
|
|
115
|
+
Start with project_board(project), then read backlog://guide/workflow.
|
|
116
|
+
Read all global_memory returned by project_board before work; it is required context.
|
|
117
|
+
First list memory_list(project=<returned full_name>) with compact results.
|
|
118
|
+
Read relevant entries with memory_get, including entries shared with this project.
|
|
119
|
+
Search other projects by topic only when needed; do not load unrelated memory.
|
|
120
|
+
Read backlog://guide for the topic index. Use resources/read, or mcp_resources_read(uri=...)
|
|
121
|
+
when connected with resources=tool. Discover resource addresses with resources/list and
|
|
122
|
+
resources/templates/list, or mcp_resources_list in tool mode.
|
|
123
|
+
""".strip()
|
|
124
|
+
|
|
125
|
+
#: The backlog the handlers work on. A context variable, so tests can run several at once.
|
|
126
|
+
ACTIVE: ContextVar[Backlog] = ContextVar("backlog")
|
|
127
|
+
|
|
128
|
+
#: The server identity that clients show beside the server name.
|
|
129
|
+
ICONS = (Icons.INBOX,)
|
|
130
|
+
WEBSITE = "https://github.com/mosquito/backlog-mcp"
|
|
131
|
+
|
|
132
|
+
# Return the whole catalogue for clients that do not follow nextCursor.
|
|
133
|
+
registry = Registry("backlog", VERSION, instructions=INSTRUCTIONS, icons=ICONS, website_url=WEBSITE)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
async def active(ex: Exchange) -> Backlog:
|
|
137
|
+
return ACTIVE.get()
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
async def active_connection(ex: Exchange) -> aiosqlite.Connection:
|
|
141
|
+
return await ACTIVE.get().open()
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
async def active_transaction(ex: Exchange) -> Transaction:
|
|
145
|
+
backlog = ACTIVE.get()
|
|
146
|
+
await backlog.open()
|
|
147
|
+
return backlog.transaction
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
registry.provide(Backlog, active)
|
|
151
|
+
registry.provide(aiosqlite.Connection, active_connection)
|
|
152
|
+
registry.provide(Transaction, active_transaction)
|
|
153
|
+
registry.extension(embeddings.extension)
|
|
154
|
+
|
|
155
|
+
#: The extension that carries the change notifications; see `changed`.
|
|
156
|
+
EVENTS = "backlog-mcp/events"
|
|
157
|
+
TASK_CHANGED = "notifications/backlog/task"
|
|
158
|
+
NOTE_CHANGED = "notifications/backlog/memory"
|
|
159
|
+
PROJECT_CHANGED = "notifications/backlog/project"
|
|
160
|
+
#: Task and note events use owner-qualified project topics.
|
|
161
|
+
#: Project events go to every authorized listener.
|
|
162
|
+
events = Extension(
|
|
163
|
+
EVENTS,
|
|
164
|
+
notifications={TASK_CHANGED: "project", NOTE_CHANGED: "project", PROJECT_CHANGED: None},
|
|
165
|
+
)
|
|
166
|
+
registry.extension(events)
|
|
167
|
+
METHODS = {Kind.TASK: TASK_CHANGED, Kind.NOTE: NOTE_CHANGED, Kind.PROJECT: PROJECT_CHANGED}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
ACTIVE_REGISTRY: ContextVar[Registry] = ContextVar("backlog_registry", default=registry)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def persistent_registry(
|
|
174
|
+
backlog: Backlog, policies: Sequence[Authentication] = ()
|
|
175
|
+
) -> tuple[Registry, ProtocolStorage]:
|
|
176
|
+
"""Use the application database for protocol sessions and notification events."""
|
|
177
|
+
storage = ProtocolStorage(backlog)
|
|
178
|
+
configured = Registry(
|
|
179
|
+
"backlog",
|
|
180
|
+
VERSION,
|
|
181
|
+
instructions=registry.instructions,
|
|
182
|
+
hub=ProtocolHub(storage),
|
|
183
|
+
session_store=ProtocolSessions(storage),
|
|
184
|
+
auth=policies or None,
|
|
185
|
+
page_size=registry.page_size,
|
|
186
|
+
icons=ICONS,
|
|
187
|
+
website_url=WEBSITE,
|
|
188
|
+
)
|
|
189
|
+
configured.tools = registry.tools
|
|
190
|
+
configured.resources_fixed = registry.resources_fixed
|
|
191
|
+
configured.resources_templated = registry.resources_templated
|
|
192
|
+
configured.prompts = registry.prompts
|
|
193
|
+
configured.completer = registry.completer
|
|
194
|
+
configured.providers = registry.providers
|
|
195
|
+
configured.extensions = registry.extensions
|
|
196
|
+
return configured, storage
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def use(backlog: Backlog) -> None:
|
|
200
|
+
"""Bind the backlog every handler resolves. Call it before serving."""
|
|
201
|
+
ACTIVE.set(backlog)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def bound() -> Backlog:
|
|
205
|
+
"""The backlog `use` bound. Raises `LookupError` when nothing is bound yet."""
|
|
206
|
+
return ACTIVE.get()
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def project_out(project: Project, *, computer: str, principal: str) -> ProjectOut:
|
|
210
|
+
"""The project as the tools return it; `computer` is the one this session works from."""
|
|
211
|
+
return ProjectOut(
|
|
212
|
+
id=project.id,
|
|
213
|
+
slug=project.slug,
|
|
214
|
+
full_name=project.full_name,
|
|
215
|
+
owner=project.owner,
|
|
216
|
+
role=project.role,
|
|
217
|
+
name=project.name,
|
|
218
|
+
roots=[
|
|
219
|
+
ProjectRootOut(principal=principal, computer=root.computer, root=root.root)
|
|
220
|
+
for root in project.roots
|
|
221
|
+
if root.root
|
|
222
|
+
],
|
|
223
|
+
here=any(root.computer == computer and root.root for root in project.roots),
|
|
224
|
+
description=project.description,
|
|
225
|
+
preferred_language=project.preferred_language,
|
|
226
|
+
archived=project.archived,
|
|
227
|
+
open_tasks=project.open_tasks,
|
|
228
|
+
total_tasks=project.total_tasks,
|
|
229
|
+
created_by=project.created_by,
|
|
230
|
+
updated_by=project.updated_by,
|
|
231
|
+
created_at=project.created_at,
|
|
232
|
+
updated_at=project.updated_at,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def task_out(task: Task) -> TaskOut:
|
|
237
|
+
return TaskOut(
|
|
238
|
+
id=task.id,
|
|
239
|
+
project=task.project,
|
|
240
|
+
title=task.title,
|
|
241
|
+
body=task.body,
|
|
242
|
+
status=task.status,
|
|
243
|
+
assigned=task.assigned,
|
|
244
|
+
priority=task.priority,
|
|
245
|
+
tags=task.tags,
|
|
246
|
+
created_by=task.created_by,
|
|
247
|
+
updated_by=task.updated_by,
|
|
248
|
+
created_at=task.created_at,
|
|
249
|
+
updated_at=task.updated_at,
|
|
250
|
+
closed_at=task.closed_at,
|
|
251
|
+
snippet=task.snippet,
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def compact_snippet(body: str, limit: int = 150) -> str:
|
|
256
|
+
"""Return the first meaningful markdown paragraph, shortened at a word boundary."""
|
|
257
|
+
paragraph: list[str] = []
|
|
258
|
+
for line in body.splitlines():
|
|
259
|
+
text = line.strip()
|
|
260
|
+
if not text:
|
|
261
|
+
if paragraph:
|
|
262
|
+
break
|
|
263
|
+
continue
|
|
264
|
+
if not paragraph and text.startswith("#"):
|
|
265
|
+
continue
|
|
266
|
+
paragraph.append(text)
|
|
267
|
+
text = " ".join(paragraph)
|
|
268
|
+
if len(text) <= limit:
|
|
269
|
+
return text
|
|
270
|
+
shortened = text[: limit - 1].rsplit(maxsplit=1)[0]
|
|
271
|
+
return f"{shortened or text[: limit - 1]}…"
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def comment_out(comment: Comment) -> CommentOut:
|
|
275
|
+
return CommentOut(
|
|
276
|
+
id=comment.id,
|
|
277
|
+
task_id=comment.task_id,
|
|
278
|
+
task_assigned=comment.task_assigned,
|
|
279
|
+
project=comment.project,
|
|
280
|
+
body=comment.body,
|
|
281
|
+
kind=comment.kind,
|
|
282
|
+
principal=comment.principal,
|
|
283
|
+
computer=comment.computer,
|
|
284
|
+
created_at=comment.created_at,
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
async def task_details(task: Task, backlog: Backlog) -> TaskDetails:
|
|
289
|
+
connection = await backlog.open()
|
|
290
|
+
comments = await read_comments(connection, backlog.actor, task_id=task.id)
|
|
291
|
+
return TaskDetails(
|
|
292
|
+
**task_out(task).model_dump(),
|
|
293
|
+
comments=[comment_out(item) for item in comments],
|
|
294
|
+
links=await task_links(connection, backlog.actor, task.id),
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def task_list_item(task: Task) -> TaskListItem:
|
|
299
|
+
"""The compact task shape used by listings."""
|
|
300
|
+
return TaskListItem(
|
|
301
|
+
id=task.id,
|
|
302
|
+
project=task.project,
|
|
303
|
+
title=task.title,
|
|
304
|
+
status=task.status,
|
|
305
|
+
assigned=task.assigned,
|
|
306
|
+
priority=task.priority,
|
|
307
|
+
tags=task.tags,
|
|
308
|
+
created_by=task.created_by,
|
|
309
|
+
updated_by=task.updated_by,
|
|
310
|
+
created_at=task.created_at,
|
|
311
|
+
updated_at=task.updated_at,
|
|
312
|
+
closed_at=task.closed_at,
|
|
313
|
+
snippet=compact_snippet(task.body),
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def note_out(note: Note) -> NoteOut:
|
|
318
|
+
return NoteOut(
|
|
319
|
+
id=note.id,
|
|
320
|
+
project=note.project,
|
|
321
|
+
title=note.title,
|
|
322
|
+
body=note.body,
|
|
323
|
+
tags=note.tags,
|
|
324
|
+
shared=note.shared,
|
|
325
|
+
global_scope=note.global_scope,
|
|
326
|
+
created_by=note.created_by,
|
|
327
|
+
updated_by=note.updated_by,
|
|
328
|
+
created_at=note.created_at,
|
|
329
|
+
updated_at=note.updated_at,
|
|
330
|
+
snippet=note.snippet,
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def note_list_item(note: Note) -> NoteListItem:
|
|
335
|
+
"""The compact memory entry shape used by listings."""
|
|
336
|
+
return NoteListItem(
|
|
337
|
+
id=note.id,
|
|
338
|
+
project=note.project,
|
|
339
|
+
title=note.title,
|
|
340
|
+
tags=note.tags,
|
|
341
|
+
shared=note.shared,
|
|
342
|
+
global_scope=note.global_scope,
|
|
343
|
+
created_by=note.created_by,
|
|
344
|
+
updated_by=note.updated_by,
|
|
345
|
+
created_at=note.created_at,
|
|
346
|
+
updated_at=note.updated_at,
|
|
347
|
+
snippet=compact_snippet(note.body),
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
async def stats_of(backlog: Backlog, reach: Reach) -> Stats:
|
|
352
|
+
"""Counts for one project, for one computer, or for everything."""
|
|
353
|
+
connection = await backlog.open()
|
|
354
|
+
project_id, computer = reach.project_id, reach.computer
|
|
355
|
+
by_priority = await group(
|
|
356
|
+
connection, backlog.actor, "priority", project_id=project_id, computer=computer
|
|
357
|
+
)
|
|
358
|
+
by_status = await group(
|
|
359
|
+
connection, backlog.actor, "status", project_id=project_id, computer=computer
|
|
360
|
+
)
|
|
361
|
+
return Stats(
|
|
362
|
+
project=reach.slug,
|
|
363
|
+
computer=backlog.computer,
|
|
364
|
+
principal=backlog.principal,
|
|
365
|
+
this_computer=backlog.computer,
|
|
366
|
+
total=sum(by_status.values()),
|
|
367
|
+
open=sum(by_status.get(status.value, 0) for status in OPEN_STATUSES),
|
|
368
|
+
closed=sum(by_status.get(status.value, 0) for status in CLOSED_STATUSES),
|
|
369
|
+
by_status=by_status,
|
|
370
|
+
by_priority={
|
|
371
|
+
PRIORITIES[int(rank)].value: count
|
|
372
|
+
for rank, count in by_priority.items()
|
|
373
|
+
if int(rank) in PRIORITIES
|
|
374
|
+
},
|
|
375
|
+
tags=await read_tags(connection, backlog.actor, project_id=project_id, computer=computer),
|
|
376
|
+
memory=await count_notes(
|
|
377
|
+
connection, backlog.actor, project_id=project_id, computer=computer
|
|
378
|
+
),
|
|
379
|
+
)
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def statuses(wanted: list[Status] | None, *, default_open: bool = True) -> list[Status] | None:
|
|
383
|
+
"""The statuses to filter by. Listings show open tasks unless asked otherwise."""
|
|
384
|
+
if wanted:
|
|
385
|
+
return wanted
|
|
386
|
+
return list(OPEN_STATUSES) if default_open else None
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
async def stale(uris: list[str], event: Event) -> None:
|
|
390
|
+
"""Tell subscribed clients which resources a write invalidated, and what the write did.
|
|
391
|
+
|
|
392
|
+
The event travels in `_meta` of each notification, so a subscriber does not have to read
|
|
393
|
+
the resource again to learn what happened.
|
|
394
|
+
"""
|
|
395
|
+
meta = {EVENT_KEY: event.model_dump(mode="json", exclude_none=True)}
|
|
396
|
+
for uri in uris:
|
|
397
|
+
await ACTIVE_REGISTRY.get().hub.publish(
|
|
398
|
+
topic(NOTIFICATIONS),
|
|
399
|
+
{
|
|
400
|
+
"jsonrpc": "2.0",
|
|
401
|
+
"method": "notifications/resources/updated",
|
|
402
|
+
"params": {"uri": uri, "_meta": meta},
|
|
403
|
+
},
|
|
404
|
+
)
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
async def publish_change(event: Event) -> None:
|
|
408
|
+
"""One write: the resources it touched went stale, and every listener hears the event.
|
|
409
|
+
|
|
410
|
+
Subscribers by URI get `resources/updated` with the event in `_meta`; listeners of the
|
|
411
|
+
`backlog-mcp/events` extension get the event as `notifications/backlog/<kind>`, task and
|
|
412
|
+
memory entry events only for the projects they named.
|
|
413
|
+
"""
|
|
414
|
+
match event.kind:
|
|
415
|
+
case Kind.TASK:
|
|
416
|
+
uris = ["backlog://tasks", "backlog://projects", "backlog://project"]
|
|
417
|
+
uris.append(f"backlog://tasks/{event.id}")
|
|
418
|
+
case Kind.NOTE:
|
|
419
|
+
uris = ["backlog://memory", f"backlog://memory/{event.id}"]
|
|
420
|
+
case Kind.PROJECT:
|
|
421
|
+
uris = ["backlog://tasks", "backlog://projects", "backlog://project"]
|
|
422
|
+
if event.action is Action.DELETED:
|
|
423
|
+
#: The notes of the project went with it.
|
|
424
|
+
uris.append("backlog://memory")
|
|
425
|
+
await stale(uris, event)
|
|
426
|
+
await ACTIVE_REGISTRY.get().broadcast(
|
|
427
|
+
METHODS[event.kind], event.model_dump(mode="json", exclude_none=True)
|
|
428
|
+
)
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
async def changed(event: Event, *, recipients: list[str] | None = None) -> None:
|
|
432
|
+
"""Publish only to the current members of the affected project."""
|
|
433
|
+
from aiohttp_tiny_mcp.storage.namespaces import namespace
|
|
434
|
+
|
|
435
|
+
backlog = bound()
|
|
436
|
+
connection = await backlog.open()
|
|
437
|
+
event.principal = backlog.principal
|
|
438
|
+
if recipients is None:
|
|
439
|
+
recipients = [backlog.principal]
|
|
440
|
+
if event.project_id is not None:
|
|
441
|
+
with contextlib.suppress(LookupError):
|
|
442
|
+
recipients = [
|
|
443
|
+
member["principal"]
|
|
444
|
+
for member in await members(connection, backlog.actor, event.project_id)
|
|
445
|
+
]
|
|
446
|
+
for identity in recipients:
|
|
447
|
+
anonymous_default = ACTIVE_REGISTRY.get().auth is None and identity == backlog.principal
|
|
448
|
+
token = namespace.set(None if anonymous_default else identity)
|
|
449
|
+
try:
|
|
450
|
+
await publish_change(event)
|
|
451
|
+
finally:
|
|
452
|
+
namespace.reset(token)
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
async def linked_tasks(
|
|
456
|
+
backlog: Backlog, *, task_id: int | None = None, project_id: int | None = None
|
|
457
|
+
) -> list[Task]:
|
|
458
|
+
"""Find resource dependents for notification routing, never for a public response."""
|
|
459
|
+
connection = await backlog.open()
|
|
460
|
+
return await read_linked_tasks(
|
|
461
|
+
connection, backlog.actor, task_id=task_id, project_id=project_id
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
async def invalidate_linked_tasks(tasks: list[Task], backlog: Backlog) -> None:
|
|
466
|
+
"""Invalidate neighbor resources without claiming the neighbor was edited.
|
|
467
|
+
|
|
468
|
+
Each recipient receives only metadata from their own accessible task. A link may cross
|
|
469
|
+
project boundaries, so the source event must not be copied into the neighbor's namespace.
|
|
470
|
+
"""
|
|
471
|
+
connection = await backlog.open()
|
|
472
|
+
from aiohttp_tiny_mcp.storage.namespaces import namespace
|
|
473
|
+
|
|
474
|
+
for task in tasks:
|
|
475
|
+
recipients = await notification_recipients(connection, backlog.actor, task.project_id)
|
|
476
|
+
event = task_event(task, Action.UPDATED, computer=backlog.computer)
|
|
477
|
+
for identity in recipients:
|
|
478
|
+
anonymous = ACTIVE_REGISTRY.get().auth is None and identity == backlog.principal
|
|
479
|
+
token = namespace.set(None if anonymous else identity)
|
|
480
|
+
try:
|
|
481
|
+
await stale([f"backlog://tasks/{task.id}"], event)
|
|
482
|
+
finally:
|
|
483
|
+
namespace.reset(token)
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
def task_event(task: Task, action: Action, *, computer: str) -> Event:
|
|
487
|
+
return Event(
|
|
488
|
+
kind=Kind.TASK,
|
|
489
|
+
action=action,
|
|
490
|
+
id=task.id,
|
|
491
|
+
project=task.project,
|
|
492
|
+
project_id=task.project_id,
|
|
493
|
+
title=task.title,
|
|
494
|
+
status=task.status,
|
|
495
|
+
priority=task.priority,
|
|
496
|
+
computer=computer,
|
|
497
|
+
principal=bound().principal,
|
|
498
|
+
)
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
def note_event(note: Note, action: Action, *, computer: str) -> Event:
|
|
502
|
+
return Event(
|
|
503
|
+
kind=Kind.NOTE,
|
|
504
|
+
action=action,
|
|
505
|
+
id=note.id,
|
|
506
|
+
project=note.project,
|
|
507
|
+
project_id=note.project_id,
|
|
508
|
+
title=note.title,
|
|
509
|
+
computer=computer,
|
|
510
|
+
principal=bound().principal,
|
|
511
|
+
)
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
def project_event(project: Project, action: Action, *, computer: str) -> Event:
|
|
515
|
+
return Event(
|
|
516
|
+
kind=Kind.PROJECT,
|
|
517
|
+
action=action,
|
|
518
|
+
id=project.id,
|
|
519
|
+
project=project.full_name,
|
|
520
|
+
project_id=project.id,
|
|
521
|
+
title=project.name,
|
|
522
|
+
computer=computer,
|
|
523
|
+
principal=bound().principal,
|
|
524
|
+
)
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
async def note_of(
|
|
528
|
+
backlog: Backlog, note_id: int | None, title: str | None, project: str | None
|
|
529
|
+
) -> Note:
|
|
530
|
+
"""The memory entry an id or a title names, or `LookupError`.
|
|
531
|
+
|
|
532
|
+
A title resolves in the selected project first, then in visible global entries.
|
|
533
|
+
"""
|
|
534
|
+
connection = await backlog.open()
|
|
535
|
+
if note_id is not None:
|
|
536
|
+
found = await read_note(connection, backlog.actor, note_id)
|
|
537
|
+
if found is None:
|
|
538
|
+
raise LookupError(f"no note #{note_id}")
|
|
539
|
+
return found
|
|
540
|
+
if not title:
|
|
541
|
+
raise ValueError("name the note by id or by title")
|
|
542
|
+
owner = await backlog.resolve(project)
|
|
543
|
+
found = await note_by_title(
|
|
544
|
+
connection, backlog.actor, title, project_id=owner.id, include_global=True
|
|
545
|
+
)
|
|
546
|
+
if found is None:
|
|
547
|
+
raise LookupError(f"no note {title!r} in {owner.full_name}")
|
|
548
|
+
return found
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
class Nothing(BaseModel):
|
|
552
|
+
pass
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
class ProjectRef(BaseModel):
|
|
556
|
+
project: str | None = Field(
|
|
557
|
+
default=None,
|
|
558
|
+
description=(
|
|
559
|
+
"Project owner/slug, unique short slug, #id, or path. Defaults to connection "
|
|
560
|
+
"project scope; required if unscoped."
|
|
561
|
+
),
|
|
562
|
+
)
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
class ShareProject(ProjectRef):
|
|
566
|
+
principal: str = Field(
|
|
567
|
+
description="user@computer or a username without @; computer does not affect access."
|
|
568
|
+
)
|
|
569
|
+
role: Literal["reader", "editor"] = "reader"
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
class Members(Actor):
|
|
573
|
+
items: list[dict[str, str]]
|
|
574
|
+
roots: list[ProjectRoot] = Field(default_factory=list)
|
|
575
|
+
|
|
576
|
+
@field_serializer("items", when_used="json")
|
|
577
|
+
def principal_labels(self, values: list[dict[str, str]]) -> list[dict[str, str]]:
|
|
578
|
+
return [{**item, "principal": principal_label(item["principal"])} for item in values]
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
@registry.tool(icons=[Icons.USERS])
|
|
582
|
+
async def project_members(
|
|
583
|
+
args: ProjectRef, backlog: Backlog, connection: aiosqlite.Connection
|
|
584
|
+
) -> Members:
|
|
585
|
+
"""List project members, roles and their computer/path bindings. Members only.
|
|
586
|
+
Read backlog://guide/projects for rules."""
|
|
587
|
+
project = await backlog.resolve(args.project)
|
|
588
|
+
return await member_details(backlog, connection, project.id)
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
async def member_details(
|
|
592
|
+
backlog: Backlog, connection: aiosqlite.Connection, project_id: int
|
|
593
|
+
) -> Members:
|
|
594
|
+
return Members(
|
|
595
|
+
items=await members(connection, backlog.actor, project_id),
|
|
596
|
+
roots=[
|
|
597
|
+
ProjectRootOut(**dict(row))
|
|
598
|
+
async for row in QUERIES.member_roots(connection, project_id=project_id)
|
|
599
|
+
],
|
|
600
|
+
principal=backlog.principal,
|
|
601
|
+
computer=backlog.computer,
|
|
602
|
+
)
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
@registry.tool(icons=[Icons.USERS])
|
|
606
|
+
async def project_share(
|
|
607
|
+
args: ShareProject, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
608
|
+
) -> Members:
|
|
609
|
+
"""Grant read or edit access to a registered principal. Only the owner can share.
|
|
610
|
+
Read backlog://guide/projects for rules."""
|
|
611
|
+
from .auth import user_identity
|
|
612
|
+
|
|
613
|
+
project = await backlog.resolve(args.project)
|
|
614
|
+
principal = user_identity(principal_user(args.principal))
|
|
615
|
+
async with transaction:
|
|
616
|
+
await share(connection, backlog.actor, project.id, principal, args.role)
|
|
617
|
+
embeddings.invalidate(backlog)
|
|
618
|
+
await changed(project_event(project, Action.UPDATED, computer=backlog.computer))
|
|
619
|
+
return await member_details(backlog, connection, project.id)
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
class ListProjects(BaseModel):
|
|
623
|
+
archived: bool = Field(default=False, description="Include archived projects.")
|
|
624
|
+
computer: str | None = Field(
|
|
625
|
+
default=None, description="Projects of this other computer instead of this one."
|
|
626
|
+
)
|
|
627
|
+
all_computers: bool = Field(default=False, description="Projects of every computer.")
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
class SaveProject(BaseModel):
|
|
631
|
+
project: str | None = Field(
|
|
632
|
+
default=None,
|
|
633
|
+
description=(
|
|
634
|
+
"Project to update. Defaults to the one for `root`, else connection project scope."
|
|
635
|
+
),
|
|
636
|
+
)
|
|
637
|
+
root: str | None = Field(
|
|
638
|
+
default=None, description="Directory to register. Defaults to the working directory."
|
|
639
|
+
)
|
|
640
|
+
name: str | None = None
|
|
641
|
+
description: str | None = None
|
|
642
|
+
preferred_language: str | None = Field(
|
|
643
|
+
default=None,
|
|
644
|
+
description=(
|
|
645
|
+
"Language to use for project work and task text. "
|
|
646
|
+
"Omit to keep the current value; null clears it. New projects default to null."
|
|
647
|
+
),
|
|
648
|
+
)
|
|
649
|
+
archived: bool | None = Field(
|
|
650
|
+
default=None, description="Archive the project, or bring it back with false."
|
|
651
|
+
)
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
class DeleteProject(BaseModel):
|
|
655
|
+
project: str = Field(
|
|
656
|
+
description="Project owner/slug, unique short slug, #id, or explicit path."
|
|
657
|
+
)
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
class AddTask(BaseModel):
|
|
661
|
+
title: str = Field(description="Clear title stating what outcome is to be achieved.")
|
|
662
|
+
body: str = Field(
|
|
663
|
+
default="",
|
|
664
|
+
description=(
|
|
665
|
+
"Reason, constraints and acceptance criteria. Express dependencies, parents "
|
|
666
|
+
"and causes with task_link."
|
|
667
|
+
),
|
|
668
|
+
)
|
|
669
|
+
status: Status = Field(
|
|
670
|
+
default=Status.TODO,
|
|
671
|
+
description="Initial status (todo, doing, blocked, done, cancelled).",
|
|
672
|
+
)
|
|
673
|
+
priority: Priority = Field(
|
|
674
|
+
default=Priority.NORMAL,
|
|
675
|
+
description="Priority ranking (urgent, high, normal, low).",
|
|
676
|
+
)
|
|
677
|
+
tags: list[str] = Field(default_factory=list, description="Tags for grouping and filtering.")
|
|
678
|
+
project: str | None = Field(
|
|
679
|
+
default=None,
|
|
680
|
+
description=(
|
|
681
|
+
"Defaults to connection project scope (?project= in HTTP, --project on "
|
|
682
|
+
"stdio); required if unscoped."
|
|
683
|
+
),
|
|
684
|
+
)
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
class ListTasks(BaseModel):
|
|
688
|
+
project: str | None = Field(
|
|
689
|
+
default=None,
|
|
690
|
+
description=(
|
|
691
|
+
"Defaults to connection project scope (?project= in HTTP, --project on "
|
|
692
|
+
"stdio); required if unscoped."
|
|
693
|
+
),
|
|
694
|
+
)
|
|
695
|
+
all_projects: bool = Field(
|
|
696
|
+
default=False, description="List tasks from every project of this computer."
|
|
697
|
+
)
|
|
698
|
+
computer: str | None = Field(
|
|
699
|
+
default=None, description="Every project of the named computer instead."
|
|
700
|
+
)
|
|
701
|
+
all_computers: bool = Field(default=False, description="Every project of every computer.")
|
|
702
|
+
status: list[Status] | None = Field(
|
|
703
|
+
default=None, description="Statuses to include. Defaults to the open ones."
|
|
704
|
+
)
|
|
705
|
+
tag: str | None = None
|
|
706
|
+
priority: Priority | None = None
|
|
707
|
+
order: Literal["backlog", "recent"] = Field(
|
|
708
|
+
default="backlog", description="`backlog` is by priority, `recent` is by last change."
|
|
709
|
+
)
|
|
710
|
+
limit: int = Field(default=50, ge=1, le=500)
|
|
711
|
+
offset: int = Field(default=0, ge=0)
|
|
712
|
+
full: bool = Field(
|
|
713
|
+
default=False,
|
|
714
|
+
description="Include complete task bodies. Defaults to compact task metadata and snippets.",
|
|
715
|
+
)
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
class TaskId(BaseModel):
|
|
719
|
+
id: int
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
class ClaimTask(BaseModel):
|
|
723
|
+
model_config = {"extra": "forbid"}
|
|
724
|
+
|
|
725
|
+
task: int = Field(description="Task id to claim, release or reassign.")
|
|
726
|
+
expected: NullableAssignment = Field(
|
|
727
|
+
description="Expected current assignment; null requires an unassigned task."
|
|
728
|
+
)
|
|
729
|
+
assigned: NullableAssignment = Field(
|
|
730
|
+
description="New username@computer:marker of the current principal; null releases."
|
|
731
|
+
)
|
|
732
|
+
temporary: bool = Field(
|
|
733
|
+
default=False,
|
|
734
|
+
description="Begin an exclusive edit claim; remember the previous assignment.",
|
|
735
|
+
)
|
|
736
|
+
restore: bool = Field(
|
|
737
|
+
default=False,
|
|
738
|
+
description="Restore the assignment saved by an edit claim. assigned must be null.",
|
|
739
|
+
)
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
class AssignedTasks(BaseModel):
|
|
743
|
+
prefix: NullableString = Field(
|
|
744
|
+
default=None,
|
|
745
|
+
description="Literal prefix; null or omitted selects the current username@computer:.",
|
|
746
|
+
)
|
|
747
|
+
limit: int = Field(default=50, ge=1, le=500)
|
|
748
|
+
offset: int = Field(default=0, ge=0)
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
class UnlinkTask(BaseModel):
|
|
752
|
+
task: int = Field(description="Source task id.")
|
|
753
|
+
relation: Relation = Field(description="Relation from task to target.")
|
|
754
|
+
target: int = Field(description="Target task id.")
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
class LinkTask(UnlinkTask):
|
|
758
|
+
reason: str | None = Field(
|
|
759
|
+
default=None, description="Explanation. Omit to preserve it; an empty string clears it."
|
|
760
|
+
)
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
class AddComment(BaseModel):
|
|
764
|
+
task_id: int
|
|
765
|
+
body: str = Field(
|
|
766
|
+
min_length=1, description="New Markdown comment. Does not change the task description."
|
|
767
|
+
)
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
class ListComments(BaseModel):
|
|
771
|
+
task_id: int
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
class UpdateTask(BaseModel):
|
|
775
|
+
id: int = Field(description="Task id to update.")
|
|
776
|
+
title: str | None = Field(default=None, description="Updated task title.")
|
|
777
|
+
body: str | None = Field(
|
|
778
|
+
default=None,
|
|
779
|
+
description=(
|
|
780
|
+
"Replaces the agreed task definition; an empty string clears it. "
|
|
781
|
+
"Edit only on an explicit user request or to reflect explicitly agreed requirements "
|
|
782
|
+
"that make the current body incorrect or outdated. Add a separate comment explaining "
|
|
783
|
+
"the reason. Use task_comment_add for review feedback, suggestions, progress, "
|
|
784
|
+
"findings, and unapproved requirement changes; do not put them in body edits."
|
|
785
|
+
),
|
|
786
|
+
)
|
|
787
|
+
status: Status | None = Field(
|
|
788
|
+
default=None,
|
|
789
|
+
description="Transition: `doing` when active, `done` when finished, `blocked` if stuck.",
|
|
790
|
+
)
|
|
791
|
+
priority: Priority | None = Field(default=None, description="Updated priority ranking.")
|
|
792
|
+
tags: list[str] | None = Field(default=None, description="Replaces the whole tag list.")
|
|
793
|
+
expected_assigned: NullableAssignment = Field(
|
|
794
|
+
default=None,
|
|
795
|
+
description="Compare assignment atomically before writing. Required for an edit claim.",
|
|
796
|
+
)
|
|
797
|
+
release_edit: bool = Field(
|
|
798
|
+
default=False,
|
|
799
|
+
description="Restore the previous assignment atomically after saving an edit claim.",
|
|
800
|
+
)
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
class MoveTask(BaseModel):
|
|
804
|
+
id: int
|
|
805
|
+
project: str = Field(
|
|
806
|
+
description="Destination project: owner/slug, unique short slug, #id, or explicit path."
|
|
807
|
+
)
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
class SaveNote(BaseModel):
|
|
811
|
+
global_scope: bool | None = Field(
|
|
812
|
+
default=None,
|
|
813
|
+
description="Make the entry visible in all projects without changing its audience.",
|
|
814
|
+
)
|
|
815
|
+
model_config = {"title": "SaveMemory"}
|
|
816
|
+
|
|
817
|
+
create_only: bool = Field(
|
|
818
|
+
default=False,
|
|
819
|
+
description="Create a new memory entry; fail if its title already exists. No id allowed.",
|
|
820
|
+
)
|
|
821
|
+
title: str | None = Field(
|
|
822
|
+
default=None,
|
|
823
|
+
description="Title for one lasting fact, preference, or convention. Optional with `id`.",
|
|
824
|
+
)
|
|
825
|
+
body: str | None = Field(
|
|
826
|
+
default=None,
|
|
827
|
+
max_length=NOTE_LIMIT,
|
|
828
|
+
description=(
|
|
829
|
+
"Lasting user context or constraint unavailable elsewhere; no code snapshots. "
|
|
830
|
+
"Replaces the whole body. "
|
|
831
|
+
f"At most {NOTE_LIMIT} characters."
|
|
832
|
+
),
|
|
833
|
+
)
|
|
834
|
+
tags: list[str] | None = Field(default=None, description="Replaces the whole tag list.")
|
|
835
|
+
shared: bool | None = Field(
|
|
836
|
+
default=None,
|
|
837
|
+
description=(
|
|
838
|
+
"True shares with project members. False restricts access to the entry author. "
|
|
839
|
+
"Title saves default to personal. ID updates keep visibility when omitted."
|
|
840
|
+
),
|
|
841
|
+
)
|
|
842
|
+
id: int | None = Field(
|
|
843
|
+
default=None, description="Change this memory entry, whatever its title."
|
|
844
|
+
)
|
|
845
|
+
project: str | None = Field(
|
|
846
|
+
default=None,
|
|
847
|
+
description=(
|
|
848
|
+
"Defaults to connection project scope (?project= in HTTP, --project on "
|
|
849
|
+
"stdio); required if unscoped."
|
|
850
|
+
),
|
|
851
|
+
)
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
class AppendNote(BaseModel):
|
|
855
|
+
model_config = {"title": "AppendMemory"}
|
|
856
|
+
|
|
857
|
+
text: str = Field(
|
|
858
|
+
max_length=NOTE_LIMIT,
|
|
859
|
+
description=(
|
|
860
|
+
"One related fact, preference, or constraint from the user. "
|
|
861
|
+
f"The whole memory entry must stay within {NOTE_LIMIT} characters."
|
|
862
|
+
),
|
|
863
|
+
)
|
|
864
|
+
id: int | None = Field(default=None, description="The memory entry to extend.")
|
|
865
|
+
title: str | None = Field(default=None, description="Its title instead, in `project`.")
|
|
866
|
+
project: str | None = Field(
|
|
867
|
+
default=None,
|
|
868
|
+
description=(
|
|
869
|
+
"Defaults to connection project scope (?project= in HTTP, --project on "
|
|
870
|
+
"stdio); required if unscoped."
|
|
871
|
+
),
|
|
872
|
+
)
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
class NoteId(BaseModel):
|
|
876
|
+
model_config = {"title": "MemoryId"}
|
|
877
|
+
|
|
878
|
+
id: int
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
class ReadNote(BaseModel):
|
|
882
|
+
model_config = {"title": "ReadMemory"}
|
|
883
|
+
|
|
884
|
+
id: int | None = Field(
|
|
885
|
+
default=None,
|
|
886
|
+
description="Memory entry id.",
|
|
887
|
+
)
|
|
888
|
+
title: str | None = Field(default=None, description="Its title instead, in `project`.")
|
|
889
|
+
project: str | None = Field(
|
|
890
|
+
default=None,
|
|
891
|
+
description=(
|
|
892
|
+
"Defaults to connection project scope (?project= in HTTP, --project on "
|
|
893
|
+
"stdio); required if unscoped."
|
|
894
|
+
),
|
|
895
|
+
)
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
class ListNotes(BaseModel):
|
|
899
|
+
model_config = {"title": "ListMemory"}
|
|
900
|
+
|
|
901
|
+
project: str | None = Field(
|
|
902
|
+
default=None,
|
|
903
|
+
description=(
|
|
904
|
+
"Defaults to connection project scope (?project= in HTTP, --project on "
|
|
905
|
+
"stdio); required if unscoped."
|
|
906
|
+
),
|
|
907
|
+
)
|
|
908
|
+
all_projects: bool = Field(
|
|
909
|
+
default=False, description="List memory entries from every project of this computer."
|
|
910
|
+
)
|
|
911
|
+
computer: str | None = Field(
|
|
912
|
+
default=None, description="Every project of the named computer instead."
|
|
913
|
+
)
|
|
914
|
+
all_computers: bool = Field(default=False, description="Every project of every computer.")
|
|
915
|
+
tag: str | None = None
|
|
916
|
+
shared: bool | None = Field(
|
|
917
|
+
default=None,
|
|
918
|
+
description=(
|
|
919
|
+
"False lists your personal entries. True lists entries shared with the project. "
|
|
920
|
+
"Null lists both within the selected scope, including visible global entries."
|
|
921
|
+
),
|
|
922
|
+
)
|
|
923
|
+
limit: int = Field(default=50, ge=1, le=500)
|
|
924
|
+
offset: int = Field(default=0, ge=0)
|
|
925
|
+
full: bool = Field(
|
|
926
|
+
default=False,
|
|
927
|
+
description=(
|
|
928
|
+
"Include complete memory entry bodies. Defaults to compact memory entry "
|
|
929
|
+
"metadata and snippets."
|
|
930
|
+
),
|
|
931
|
+
)
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
class StatsOf(BaseModel):
|
|
935
|
+
project: str | None = Field(
|
|
936
|
+
default=None,
|
|
937
|
+
description=(
|
|
938
|
+
"Defaults to connection project scope (?project= in HTTP, --project on "
|
|
939
|
+
"stdio); required if unscoped."
|
|
940
|
+
),
|
|
941
|
+
)
|
|
942
|
+
all_projects: bool = Field(
|
|
943
|
+
default=False, description="Count every project of this computer together."
|
|
944
|
+
)
|
|
945
|
+
computer: str | None = Field(
|
|
946
|
+
default=None, description="Every project of the named computer instead."
|
|
947
|
+
)
|
|
948
|
+
all_computers: bool = Field(default=False, description="Every project of every computer.")
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
@registry.tool(
|
|
952
|
+
title="Current identity", annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD, icons=[Icons.USER]
|
|
953
|
+
)
|
|
954
|
+
async def whoami(args: Nothing, backlog: Backlog, connection: aiosqlite.Connection) -> Identity:
|
|
955
|
+
"""Report principal as user@computer, authentication type and owner handle without selecting a
|
|
956
|
+
project.
|
|
957
|
+
Read backlog://guide/projects for rules."""
|
|
958
|
+
return Identity(
|
|
959
|
+
principal=backlog.principal,
|
|
960
|
+
handle=owner_name(backlog.principal),
|
|
961
|
+
auth_type=backlog.auth_type,
|
|
962
|
+
computer=backlog.computer,
|
|
963
|
+
assigned_tasks=await count_assigned(
|
|
964
|
+
connection,
|
|
965
|
+
backlog.actor,
|
|
966
|
+
principal_label(backlog.principal, backlog.computer) + ":",
|
|
967
|
+
),
|
|
968
|
+
)
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
class ReadBoard(BaseModel):
|
|
972
|
+
project: str = Field(
|
|
973
|
+
min_length=1,
|
|
974
|
+
description="Existing project: owner/slug, unique slug, #id or registered path.",
|
|
975
|
+
)
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
@registry.tool(
|
|
979
|
+
title="Project board", annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD, icons=[Icons.CHART_LINE]
|
|
980
|
+
)
|
|
981
|
+
async def project_board(
|
|
982
|
+
args: ReadBoard, backlog: Backlog, connection: aiosqlite.Connection
|
|
983
|
+
) -> ProjectBoard:
|
|
984
|
+
"""Start here for project context, counts and ready/claimed tasks.
|
|
985
|
+
Read every entry in global_memory before work: all visible global entries are included in full.
|
|
986
|
+
Verify principal and roots, then call memory_list(project=<returned full_name>).
|
|
987
|
+
Read relevant entries with memory_get and follow preferred_language.
|
|
988
|
+
Read task_get before claiming; the board is not a reservation.
|
|
989
|
+
This read never creates a project or changes connection scope. See backlog://guide/workflow."""
|
|
990
|
+
project = await backlog.lookup(args.project)
|
|
991
|
+
shown = project_out(project, computer=backlog.computer, principal=backlog.principal)
|
|
992
|
+
counts, ready, claimed = await board(connection, backlog.actor, project.id)
|
|
993
|
+
return ProjectBoard(
|
|
994
|
+
principal=backlog.principal,
|
|
995
|
+
computer=backlog.computer,
|
|
996
|
+
project=BoardProject(**shown.model_dump()),
|
|
997
|
+
tasks=BoardCounts(**counts),
|
|
998
|
+
global_memory=[
|
|
999
|
+
note_out(to_note(row))
|
|
1000
|
+
async for row in QUERIES.global_notes(connection, principal=backlog.principal)
|
|
1001
|
+
],
|
|
1002
|
+
memory=await count_notes(connection, backlog.actor, project_id=project.id),
|
|
1003
|
+
ready=ready,
|
|
1004
|
+
claimed=claimed,
|
|
1005
|
+
)
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
@registry.tool(
|
|
1009
|
+
title="List projects", annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD, icons=[Icons.FOLDER]
|
|
1010
|
+
)
|
|
1011
|
+
async def project_list(
|
|
1012
|
+
args: ListProjects, backlog: Backlog, connection: aiosqlite.Connection
|
|
1013
|
+
) -> Projects:
|
|
1014
|
+
"""List registered projects with open and total task counts.
|
|
1015
|
+
|
|
1016
|
+
`computer` lists another computer's projects, `all_computers` every project there is.
|
|
1017
|
+
Read backlog://guide/projects for rules."""
|
|
1018
|
+
reach = await backlog.reach(
|
|
1019
|
+
all_projects=True, computer=args.computer, all_computers=args.all_computers
|
|
1020
|
+
)
|
|
1021
|
+
found = await read_projects(
|
|
1022
|
+
connection, backlog.actor, archived=args.archived, computer=reach.computer
|
|
1023
|
+
)
|
|
1024
|
+
return Projects(
|
|
1025
|
+
principal=backlog.principal,
|
|
1026
|
+
computer=backlog.computer,
|
|
1027
|
+
items=[
|
|
1028
|
+
project_out(item, computer=backlog.computer, principal=backlog.principal)
|
|
1029
|
+
for item in found
|
|
1030
|
+
],
|
|
1031
|
+
count=len(found),
|
|
1032
|
+
)
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
@registry.tool(
|
|
1036
|
+
title="Register or update a project", annotations=Hint.IDEMPOTENT, icons=[Icons.FLOPPY_DISK]
|
|
1037
|
+
)
|
|
1038
|
+
async def project_save(
|
|
1039
|
+
args: SaveProject, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1040
|
+
) -> ProjectOut:
|
|
1041
|
+
"""Register a project or update its name, description, language, or archive status.
|
|
1042
|
+
|
|
1043
|
+
`preferred_language` sets the language for project work and task text. Omit it to keep
|
|
1044
|
+
the current value, or send null to clear it. New projects default to null.
|
|
1045
|
+
Read backlog://guide/projects for rules."""
|
|
1046
|
+
project = await backlog.save(
|
|
1047
|
+
connection,
|
|
1048
|
+
transaction,
|
|
1049
|
+
reference=args.project,
|
|
1050
|
+
root=args.root,
|
|
1051
|
+
name=args.name,
|
|
1052
|
+
description=args.description,
|
|
1053
|
+
archived=args.archived,
|
|
1054
|
+
preferred_language=(
|
|
1055
|
+
args.preferred_language
|
|
1056
|
+
if "preferred_language" in args.model_fields_set
|
|
1057
|
+
else Unset.VALUE
|
|
1058
|
+
),
|
|
1059
|
+
)
|
|
1060
|
+
await changed(project_event(project, Action.UPDATED, computer=backlog.computer))
|
|
1061
|
+
return project_out(project, computer=backlog.computer, principal=backlog.principal)
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
@registry.tool(
|
|
1065
|
+
title="Delete a project",
|
|
1066
|
+
annotations=Hint.DESTRUCTIVE | ~Hint.OPEN_WORLD,
|
|
1067
|
+
icons=[Icons.TRASH_CAN],
|
|
1068
|
+
)
|
|
1069
|
+
async def project_delete(
|
|
1070
|
+
args: DeleteProject,
|
|
1071
|
+
ex: Exchange,
|
|
1072
|
+
backlog: Backlog,
|
|
1073
|
+
connection: aiosqlite.Connection,
|
|
1074
|
+
transaction: Transaction,
|
|
1075
|
+
) -> ProjectDeleted:
|
|
1076
|
+
"""Delete a project with every task and memory entry on its board, and free its directories.
|
|
1077
|
+
|
|
1078
|
+
DESTRUCTIVE and irreversible. Call it only when the user asked to delete this project in so
|
|
1079
|
+
many words. Never call it on your own initiative, and never to tidy up: archive with
|
|
1080
|
+
`project_save(archived=true)`, which keeps the history. The tool always asks the user to
|
|
1081
|
+
confirm. A client that cannot ask is refused; the terminal command `backlog projects delete`
|
|
1082
|
+
asks there instead.
|
|
1083
|
+
Read backlog://guide/projects for rules."""
|
|
1084
|
+
project = await backlog.lookup(args.project)
|
|
1085
|
+
await require(connection, backlog.actor, project.id, role="owner")
|
|
1086
|
+
contents = await project_contents(connection, backlog.actor, project.id)
|
|
1087
|
+
found = ProjectDeleted(
|
|
1088
|
+
principal=backlog.principal,
|
|
1089
|
+
computer=backlog.computer,
|
|
1090
|
+
id=project.id,
|
|
1091
|
+
slug=project.slug,
|
|
1092
|
+
full_name=project.full_name,
|
|
1093
|
+
name=project.name,
|
|
1094
|
+
tasks=contents.tasks,
|
|
1095
|
+
memory=contents.notes,
|
|
1096
|
+
shared_notes=contents.shared_notes,
|
|
1097
|
+
deleted=False,
|
|
1098
|
+
)
|
|
1099
|
+
held = (
|
|
1100
|
+
f"{project.full_name!r} with {contents.tasks} task(s) and {contents.notes} note(s), "
|
|
1101
|
+
f"{contents.shared_notes} of them shared"
|
|
1102
|
+
)
|
|
1103
|
+
key = f"delete-project:{project.id}"
|
|
1104
|
+
answer = ex.answers.get(key)
|
|
1105
|
+
if answer is None:
|
|
1106
|
+
if not ex.can_ask:
|
|
1107
|
+
raise PermissionError(
|
|
1108
|
+
f"this client cannot ask the user, so it cannot delete project {held}; "
|
|
1109
|
+
"use a client with elicitation, or `backlog projects delete`"
|
|
1110
|
+
)
|
|
1111
|
+
raise NeedInput(
|
|
1112
|
+
{
|
|
1113
|
+
key: elicit(
|
|
1114
|
+
f"Delete project {held}? This cannot be undone.",
|
|
1115
|
+
{"type": "object", "properties": {"ok": {"type": "boolean"}}},
|
|
1116
|
+
)
|
|
1117
|
+
}
|
|
1118
|
+
)
|
|
1119
|
+
if not answer.get("ok"):
|
|
1120
|
+
found.detail = "cancelled"
|
|
1121
|
+
return found
|
|
1122
|
+
recipients = [
|
|
1123
|
+
member["principal"] for member in await members(connection, backlog.actor, project.id)
|
|
1124
|
+
]
|
|
1125
|
+
neighbors = await linked_tasks(backlog, project_id=project.id)
|
|
1126
|
+
await backlog.delete(project, connection, transaction)
|
|
1127
|
+
await invalidate_linked_tasks(
|
|
1128
|
+
[task for task in neighbors if task.project_id != project.id], backlog
|
|
1129
|
+
)
|
|
1130
|
+
await changed(
|
|
1131
|
+
project_event(project, Action.DELETED, computer=backlog.computer), recipients=recipients
|
|
1132
|
+
)
|
|
1133
|
+
found.deleted = True
|
|
1134
|
+
return found
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
@registry.tool(title="Add a task", icons=[Icons.PLUS])
|
|
1138
|
+
async def task_add(
|
|
1139
|
+
args: AddTask, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1140
|
+
) -> TaskOut:
|
|
1141
|
+
"""Create a task for work that spans more than one step or session. Search for an existing task
|
|
1142
|
+
first. See backlog://guide/tasks."""
|
|
1143
|
+
project = await backlog.resolve(args.project)
|
|
1144
|
+
async with transaction:
|
|
1145
|
+
task = await add_task(
|
|
1146
|
+
connection,
|
|
1147
|
+
backlog.actor,
|
|
1148
|
+
project_id=project.id,
|
|
1149
|
+
title=args.title,
|
|
1150
|
+
body=args.body,
|
|
1151
|
+
status=args.status,
|
|
1152
|
+
priority=args.priority,
|
|
1153
|
+
tags=args.tags,
|
|
1154
|
+
)
|
|
1155
|
+
embeddings.enqueue(backlog, task)
|
|
1156
|
+
await changed(task_event(task, Action.ADDED, computer=backlog.computer))
|
|
1157
|
+
return task_out(task)
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
@registry.tool(
|
|
1161
|
+
title="List tasks", annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD, icons=[Icons.LAYER_GROUP]
|
|
1162
|
+
)
|
|
1163
|
+
async def task_list(
|
|
1164
|
+
args: ListTasks, backlog: Backlog, connection: aiosqlite.Connection
|
|
1165
|
+
) -> Tasks | FullTasks:
|
|
1166
|
+
"""List tasks with filters. To pick the next task, use project_board. Defaults to open tasks
|
|
1167
|
+
ordered by priority. See backlog://guide/tasks."""
|
|
1168
|
+
reach = await backlog.reach(
|
|
1169
|
+
args.project,
|
|
1170
|
+
all_projects=args.all_projects,
|
|
1171
|
+
computer=args.computer,
|
|
1172
|
+
all_computers=args.all_computers,
|
|
1173
|
+
)
|
|
1174
|
+
found = await read_tasks(
|
|
1175
|
+
connection,
|
|
1176
|
+
backlog.actor,
|
|
1177
|
+
project_id=reach.project_id,
|
|
1178
|
+
computer=reach.computer,
|
|
1179
|
+
status=statuses(args.status),
|
|
1180
|
+
tag=args.tag,
|
|
1181
|
+
priority=args.priority,
|
|
1182
|
+
order=args.order,
|
|
1183
|
+
limit=args.limit,
|
|
1184
|
+
offset=args.offset,
|
|
1185
|
+
)
|
|
1186
|
+
total = await count_tasks(
|
|
1187
|
+
connection,
|
|
1188
|
+
backlog.actor,
|
|
1189
|
+
project_id=reach.project_id,
|
|
1190
|
+
computer=reach.computer,
|
|
1191
|
+
status=statuses(args.status),
|
|
1192
|
+
tag=args.tag,
|
|
1193
|
+
priority=args.priority,
|
|
1194
|
+
)
|
|
1195
|
+
if args.full:
|
|
1196
|
+
return FullTasks(
|
|
1197
|
+
items=[task_out(item) for item in found],
|
|
1198
|
+
count=total,
|
|
1199
|
+
project=reach.slug,
|
|
1200
|
+
computer=backlog.computer,
|
|
1201
|
+
principal=backlog.principal,
|
|
1202
|
+
)
|
|
1203
|
+
return Tasks(
|
|
1204
|
+
items=[task_list_item(item) for item in found],
|
|
1205
|
+
count=total,
|
|
1206
|
+
project=reach.slug,
|
|
1207
|
+
computer=backlog.computer,
|
|
1208
|
+
principal=backlog.principal,
|
|
1209
|
+
)
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
@registry.tool(
|
|
1213
|
+
title="Read a task", annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD, icons=[Icons.FILE_LINES]
|
|
1214
|
+
)
|
|
1215
|
+
async def task_get(args: TaskId, backlog: Backlog, connection: aiosqlite.Connection) -> TaskDetails:
|
|
1216
|
+
"""Read a task with its direct links, comments and history.
|
|
1217
|
+
|
|
1218
|
+
The body is only the description. Comments are chronological. Links use relation types
|
|
1219
|
+
relative to this task and include current target titles and statuses, without nested links.
|
|
1220
|
+
Links to tasks you cannot access are omitted.
|
|
1221
|
+
Read backlog://guide/tasks for rules."""
|
|
1222
|
+
task = await read_task(connection, backlog.actor, args.id)
|
|
1223
|
+
if task is None:
|
|
1224
|
+
raise LookupError(f"no task #{args.id}")
|
|
1225
|
+
return await task_details(task, backlog)
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
@registry.tool(
|
|
1229
|
+
title="List assigned tasks", annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD, icons=[Icons.INBOX]
|
|
1230
|
+
)
|
|
1231
|
+
async def task_assigned(
|
|
1232
|
+
args: AssignedTasks, backlog: Backlog, connection: aiosqlite.Connection
|
|
1233
|
+
) -> Tasks:
|
|
1234
|
+
"""Find assignments by literal prefix across all accessible projects and statuses.
|
|
1235
|
+
|
|
1236
|
+
Null or omitted prefix selects the current principal. Use username@computer: for a
|
|
1237
|
+
principal or username@computer:marker for an agent marker prefix. Percent and underscore
|
|
1238
|
+
are literal characters, not wildcards. Matching is case-sensitive. Closed tasks remain
|
|
1239
|
+
assigned until explicitly released. Count includes all matches before pagination.
|
|
1240
|
+
Read backlog://guide/tasks for rules."""
|
|
1241
|
+
prefix = args.prefix
|
|
1242
|
+
if prefix is None:
|
|
1243
|
+
prefix = principal_label(backlog.principal, backlog.computer) + ":"
|
|
1244
|
+
tasks = await assigned_tasks(
|
|
1245
|
+
connection, backlog.actor, prefix, limit=args.limit, offset=args.offset
|
|
1246
|
+
)
|
|
1247
|
+
return Tasks(
|
|
1248
|
+
principal=backlog.principal,
|
|
1249
|
+
computer=backlog.computer,
|
|
1250
|
+
items=[task_list_item(task) for task in tasks],
|
|
1251
|
+
count=await count_assigned(connection, backlog.actor, prefix),
|
|
1252
|
+
)
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
@registry.tool(title="Claim a task", icons=[Icons.LOCK])
|
|
1256
|
+
async def task_claim(
|
|
1257
|
+
args: ClaimTask, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1258
|
+
) -> TaskOut:
|
|
1259
|
+
"""Atomically replace assigned only when it equals expected (CAS).
|
|
1260
|
+
Both nullable arguments are required; null expected requires an unassigned task.
|
|
1261
|
+
A mismatch is an error even if assigned already has the requested value.
|
|
1262
|
+
|
|
1263
|
+
Assignment is advisory: it keeps two agents from starting the same task.
|
|
1264
|
+
Any editor may take a task over through CAS. Non-null assigned must use this principal
|
|
1265
|
+
and a URL-safe marker. Keep the marker across reconnects. On conflict, reread before
|
|
1266
|
+
retrying.
|
|
1267
|
+
Closing does not release assignment; release with assigned=null. See backlog://guide/tasks.
|
|
1268
|
+
|
|
1269
|
+
temporary=true saves the previous assignment for exclusive UI editing. restore=true
|
|
1270
|
+
with assigned=null restores it; only that claimant may restore. Writes require the edit
|
|
1271
|
+
marker as expected_assigned. Ordinary CAS reassignment invalidates a temporary claim."""
|
|
1272
|
+
async with transaction:
|
|
1273
|
+
task, updated = await claim_task(
|
|
1274
|
+
connection,
|
|
1275
|
+
backlog.actor,
|
|
1276
|
+
args.task,
|
|
1277
|
+
expected=args.expected,
|
|
1278
|
+
assigned=args.assigned,
|
|
1279
|
+
temporary=args.temporary,
|
|
1280
|
+
restore=args.restore,
|
|
1281
|
+
)
|
|
1282
|
+
comments = (
|
|
1283
|
+
await read_comments(
|
|
1284
|
+
connection, backlog.actor, task_id=args.task, status=list(Status), limit=-1
|
|
1285
|
+
)
|
|
1286
|
+
if updated
|
|
1287
|
+
else []
|
|
1288
|
+
)
|
|
1289
|
+
if updated:
|
|
1290
|
+
embeddings.enqueue(backlog, *comments)
|
|
1291
|
+
await changed(task_event(task, Action.UPDATED, computer=backlog.computer))
|
|
1292
|
+
return task_out(task)
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
@registry.tool(title="Link tasks", annotations=Hint.IDEMPOTENT, icons=[Icons.LINK])
|
|
1296
|
+
async def task_link(
|
|
1297
|
+
args: LinkTask, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1298
|
+
) -> LinkChange:
|
|
1299
|
+
"""Add a typed relation and its inverse atomically, or update their reason.
|
|
1300
|
+
|
|
1301
|
+
Read links through task_get. Requires editor access to both tasks. Self-links and cycles
|
|
1302
|
+
in part_of or depends_on are rejected. Repeated calls are safe, including from the inverse
|
|
1303
|
+
direction. Links do not change task statuses. Closing or soft-deleting tasks keeps links.
|
|
1304
|
+
Read backlog://guide/tasks for rules."""
|
|
1305
|
+
async with transaction:
|
|
1306
|
+
updated = await link_task(
|
|
1307
|
+
connection, backlog.actor, args.task, args.relation, args.target, reason=args.reason
|
|
1308
|
+
)
|
|
1309
|
+
if updated:
|
|
1310
|
+
for task_id in (args.task, args.target):
|
|
1311
|
+
task = await read_task(connection, backlog.actor, task_id)
|
|
1312
|
+
assert task is not None
|
|
1313
|
+
await changed(task_event(task, Action.UPDATED, computer=backlog.computer))
|
|
1314
|
+
return LinkChange(task=args.task, relation=args.relation, target=args.target, changed=updated)
|
|
1315
|
+
|
|
1316
|
+
|
|
1317
|
+
@registry.tool(title="Unlink tasks", annotations=Hint.IDEMPOTENT, icons=[Icons.XMARK])
|
|
1318
|
+
async def task_unlink(
|
|
1319
|
+
args: UnlinkTask, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1320
|
+
) -> LinkChange:
|
|
1321
|
+
"""Remove a relation and its inverse atomically, from either direction.
|
|
1322
|
+
|
|
1323
|
+
Requires editor access to both tasks. A missing link is a safe no-op. Other relation types
|
|
1324
|
+
between the same tasks are preserved.
|
|
1325
|
+
Read backlog://guide/tasks for rules."""
|
|
1326
|
+
async with transaction:
|
|
1327
|
+
updated = await unlink_task(
|
|
1328
|
+
connection, backlog.actor, args.task, args.relation, args.target
|
|
1329
|
+
)
|
|
1330
|
+
if updated:
|
|
1331
|
+
for task_id in (args.task, args.target):
|
|
1332
|
+
task = await read_task(connection, backlog.actor, task_id)
|
|
1333
|
+
assert task is not None
|
|
1334
|
+
await changed(task_event(task, Action.UPDATED, computer=backlog.computer))
|
|
1335
|
+
return LinkChange(task=args.task, relation=args.relation, target=args.target, changed=updated)
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
@registry.tool(title="Add a task comment", annotations=Hint.OPEN_WORLD, icons=[Icons.COMMENTS])
|
|
1339
|
+
async def task_comment_add(
|
|
1340
|
+
args: AddComment, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1341
|
+
) -> CommentOut:
|
|
1342
|
+
"""Record progress, findings, blockers, or verification as a separate Markdown comment.
|
|
1343
|
+
|
|
1344
|
+
Use this for progress, review feedback, research results, hypotheses, questions,
|
|
1345
|
+
suggestions, and proposed requirement changes. New findings do not authorize body edits.
|
|
1346
|
+
Keep proposals here until the user explicitly agrees. After an authorized body edit,
|
|
1347
|
+
add a separate comment explaining its reason. See backlog://guide/tasks.
|
|
1348
|
+
Follow the project's preferred_language. Repeated calls create separate comments.
|
|
1349
|
+
"""
|
|
1350
|
+
async with transaction:
|
|
1351
|
+
comment = await add_comment(connection, backlog.actor, args.task_id, args.body)
|
|
1352
|
+
embeddings.enqueue(backlog, comment)
|
|
1353
|
+
task = await read_task(connection, backlog.actor, args.task_id)
|
|
1354
|
+
assert task is not None
|
|
1355
|
+
await changed(task_event(task, Action.UPDATED, computer=backlog.computer))
|
|
1356
|
+
return comment_out(comment)
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
@registry.tool(
|
|
1360
|
+
title="List task comments",
|
|
1361
|
+
annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD,
|
|
1362
|
+
icons=[Icons.COMMENTS],
|
|
1363
|
+
)
|
|
1364
|
+
async def task_comment_list(
|
|
1365
|
+
args: ListComments, backlog: Backlog, connection: aiosqlite.Connection
|
|
1366
|
+
) -> Comments:
|
|
1367
|
+
"""Read comments and history in creation-time order, with id as a stable tie-breaker.
|
|
1368
|
+
Read backlog://guide/tasks for rules."""
|
|
1369
|
+
task = await read_task(connection, backlog.actor, args.task_id)
|
|
1370
|
+
if task is None:
|
|
1371
|
+
raise LookupError(f"no task #{args.task_id}")
|
|
1372
|
+
details = await task_details(task, backlog)
|
|
1373
|
+
return Comments(
|
|
1374
|
+
principal=backlog.principal,
|
|
1375
|
+
computer=backlog.computer,
|
|
1376
|
+
items=details.comments,
|
|
1377
|
+
task_id=task.id,
|
|
1378
|
+
count=len(details.comments),
|
|
1379
|
+
)
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
@registry.tool(title="Update a task", annotations=Hint.IDEMPOTENT, icons=[Icons.PEN])
|
|
1383
|
+
async def task_update(
|
|
1384
|
+
args: UpdateTask, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1385
|
+
) -> TaskOut:
|
|
1386
|
+
"""Edit task fields. A supplied body replaces the description; an empty string clears it.
|
|
1387
|
+
|
|
1388
|
+
The body is the agreed task definition. Edit it only on an explicit user request or
|
|
1389
|
+
to reflect explicitly agreed requirements that make it incorrect or outdated.
|
|
1390
|
+
Add a separate comment explaining every body edit and its authorization.
|
|
1391
|
+
Use task_comment_add for review feedback, suggestions, progress, findings, verification,
|
|
1392
|
+
and unapproved requirement changes. Do not put these in body edits.
|
|
1393
|
+
Status transitions create separate history entries.
|
|
1394
|
+
Mark active work doing and completed work done or cancelled.
|
|
1395
|
+
Closing stamps the closing time; reopening clears it. expected_assigned, when supplied,
|
|
1396
|
+
is compared under the writer lock. An active temporary edit claim requires that marker.
|
|
1397
|
+
release_edit=true restores its previous assignment in the same transaction.
|
|
1398
|
+
Waiting for another task is a depends_on link, not blocked status. Use blocked for external
|
|
1399
|
+
blockers and explain in a comment. Before closing, update project memory this task made
|
|
1400
|
+
obsolete. After closing, release through task_claim(assigned=null). See backlog://guide/tasks."""
|
|
1401
|
+
async with transaction:
|
|
1402
|
+
task = await update_task(
|
|
1403
|
+
connection,
|
|
1404
|
+
backlog.actor,
|
|
1405
|
+
args.id,
|
|
1406
|
+
title=args.title,
|
|
1407
|
+
body=args.body,
|
|
1408
|
+
status=args.status,
|
|
1409
|
+
priority=args.priority,
|
|
1410
|
+
tags=args.tags,
|
|
1411
|
+
expected_assigned=args.expected_assigned
|
|
1412
|
+
if "expected_assigned" in args.model_fields_set
|
|
1413
|
+
else Unset.VALUE,
|
|
1414
|
+
)
|
|
1415
|
+
if args.release_edit:
|
|
1416
|
+
if "expected_assigned" not in args.model_fields_set:
|
|
1417
|
+
raise ValueError("release_edit requires expected_assigned")
|
|
1418
|
+
task, _ = await claim_task(
|
|
1419
|
+
connection,
|
|
1420
|
+
backlog.actor,
|
|
1421
|
+
args.id,
|
|
1422
|
+
expected=args.expected_assigned,
|
|
1423
|
+
assigned=None,
|
|
1424
|
+
restore=True,
|
|
1425
|
+
)
|
|
1426
|
+
comments = await read_comments(
|
|
1427
|
+
connection, backlog.actor, task_id=args.id, status=list(Status), limit=-1
|
|
1428
|
+
)
|
|
1429
|
+
if task is None:
|
|
1430
|
+
raise LookupError(f"no task #{args.id}")
|
|
1431
|
+
embeddings.enqueue(backlog, task, *comments)
|
|
1432
|
+
await changed(task_event(task, Action.UPDATED, computer=backlog.computer))
|
|
1433
|
+
if args.title is not None or args.status is not None:
|
|
1434
|
+
await invalidate_linked_tasks(await linked_tasks(backlog, task_id=task.id), backlog)
|
|
1435
|
+
return task_out(task)
|
|
1436
|
+
|
|
1437
|
+
|
|
1438
|
+
@registry.tool(title="Move a task", annotations=Hint.IDEMPOTENT, icons=[Icons.ARROWS_ROTATE])
|
|
1439
|
+
async def task_move(
|
|
1440
|
+
args: MoveTask, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1441
|
+
) -> TaskOut:
|
|
1442
|
+
"""Move a task to another project board and record the move in its history.
|
|
1443
|
+
Read backlog://guide/tasks for rules."""
|
|
1444
|
+
project = await backlog.lookup(args.project)
|
|
1445
|
+
existing = await read_task(connection, backlog.actor, args.id)
|
|
1446
|
+
if existing is None:
|
|
1447
|
+
raise LookupError(f"no task #{args.id}")
|
|
1448
|
+
if existing.project_id == project.id:
|
|
1449
|
+
return task_out(existing)
|
|
1450
|
+
async with transaction:
|
|
1451
|
+
task = await update_task(connection, backlog.actor, args.id, project_id=project.id)
|
|
1452
|
+
comments = await read_comments(
|
|
1453
|
+
connection, backlog.actor, task_id=args.id, status=list(Status), limit=-1
|
|
1454
|
+
)
|
|
1455
|
+
assert task is not None
|
|
1456
|
+
embeddings.enqueue(backlog, task, *comments)
|
|
1457
|
+
await changed(task_event(task, Action.MOVED, computer=backlog.computer))
|
|
1458
|
+
await invalidate_linked_tasks(await linked_tasks(backlog, task_id=task.id), backlog)
|
|
1459
|
+
return task_out(task)
|
|
1460
|
+
|
|
1461
|
+
|
|
1462
|
+
async def confirm_cross_project_delete(
|
|
1463
|
+
record: Note, ex: Exchange, backlog: Backlog
|
|
1464
|
+
) -> Deleted | None:
|
|
1465
|
+
"""Ask when the memory entry belongs to another project, or when no project is chosen.
|
|
1466
|
+
|
|
1467
|
+
A client that cannot ask is refused with `PermissionError`: nothing stands in for the
|
|
1468
|
+
user's answer.
|
|
1469
|
+
"""
|
|
1470
|
+
current, _ = await backlog.scope()
|
|
1471
|
+
if current is not None and record.project_id == current.id:
|
|
1472
|
+
return None
|
|
1473
|
+
key = f"delete:{record.project_id}:{record.id}"
|
|
1474
|
+
if (answer := ex.answers.get(key)) is not None:
|
|
1475
|
+
if answer.get("ok"):
|
|
1476
|
+
return None
|
|
1477
|
+
return Deleted(
|
|
1478
|
+
principal=backlog.principal,
|
|
1479
|
+
computer=backlog.computer,
|
|
1480
|
+
id=record.id,
|
|
1481
|
+
title=record.title,
|
|
1482
|
+
deleted=False,
|
|
1483
|
+
detail="cancelled",
|
|
1484
|
+
)
|
|
1485
|
+
where = record.project
|
|
1486
|
+
if not ex.can_ask:
|
|
1487
|
+
raise PermissionError(
|
|
1488
|
+
f"this client cannot ask the user, so it cannot delete note #{record.id} from "
|
|
1489
|
+
f"{where!r}; use a client with elicitation, or `backlog memory delete`"
|
|
1490
|
+
)
|
|
1491
|
+
chosen = (
|
|
1492
|
+
f"The current project is {current.full_name!r}." if current else "No project is chosen."
|
|
1493
|
+
)
|
|
1494
|
+
raise NeedInput(
|
|
1495
|
+
{
|
|
1496
|
+
key: elicit(
|
|
1497
|
+
f"Delete note #{record.id} ({record.title!r}) from {where!r}? "
|
|
1498
|
+
f"{chosen} This cannot be undone.",
|
|
1499
|
+
{"type": "object", "properties": {"ok": {"type": "boolean"}}},
|
|
1500
|
+
)
|
|
1501
|
+
}
|
|
1502
|
+
)
|
|
1503
|
+
|
|
1504
|
+
|
|
1505
|
+
@registry.tool(title="Delete a task", annotations=Hint.IDEMPOTENT, icons=[Icons.TRASH_CAN])
|
|
1506
|
+
async def task_delete(
|
|
1507
|
+
args: TaskId, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1508
|
+
) -> Deleted:
|
|
1509
|
+
"""Delete a task. Marks its status as `deleted` without removing the record.
|
|
1510
|
+
|
|
1511
|
+
Keeps history intact and allows recovery. Works across projects without confirmation.
|
|
1512
|
+
Read backlog://guide/tasks for rules."""
|
|
1513
|
+
task = await read_task(connection, backlog.actor, args.id)
|
|
1514
|
+
if task is None:
|
|
1515
|
+
raise LookupError(f"no task #{args.id}")
|
|
1516
|
+
if task.status != Status.DELETED:
|
|
1517
|
+
async with transaction:
|
|
1518
|
+
gone = await update_task(connection, backlog.actor, task.id, status=Status.DELETED)
|
|
1519
|
+
comments = await read_comments(
|
|
1520
|
+
connection, backlog.actor, task_id=task.id, status=list(Status), limit=-1
|
|
1521
|
+
)
|
|
1522
|
+
assert gone is not None
|
|
1523
|
+
embeddings.enqueue(backlog, gone, *comments)
|
|
1524
|
+
await changed(task_event(gone, Action.DELETED, computer=backlog.computer))
|
|
1525
|
+
await invalidate_linked_tasks(await linked_tasks(backlog, task_id=task.id), backlog)
|
|
1526
|
+
return Deleted(
|
|
1527
|
+
principal=backlog.principal,
|
|
1528
|
+
computer=backlog.computer,
|
|
1529
|
+
id=args.id,
|
|
1530
|
+
title=task.title,
|
|
1531
|
+
deleted=True,
|
|
1532
|
+
)
|
|
1533
|
+
|
|
1534
|
+
|
|
1535
|
+
@registry.tool(title="Save a memory entry", annotations=Hint.IDEMPOTENT, icons=[Icons.FLOPPY_DISK])
|
|
1536
|
+
async def memory_save(
|
|
1537
|
+
args: SaveNote, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1538
|
+
) -> NoteOut:
|
|
1539
|
+
"""Record a lasting agreement, preference or invariant the user stated.
|
|
1540
|
+
If the project, audience or global scope is unclear, ask the user before saving.
|
|
1541
|
+
Do not guess or use defaults to resolve uncertainty.
|
|
1542
|
+
Choose the audience by who needs the fact: "my dev server" is personal (shared=false),
|
|
1543
|
+
while "team dev servers catalog" is shared with project members (shared=true).
|
|
1544
|
+
Use global_scope=true only when the same audience needs it in every project.
|
|
1545
|
+
Check memory_list(project=...) and read relevant entries with memory_get.
|
|
1546
|
+
Update an existing entry by id instead of creating a near-duplicate.
|
|
1547
|
+
A correction rewrites the body with the current truth; do not append
|
|
1548
|
+
history.
|
|
1549
|
+
Do not store task status, progress or code snapshots; use tasks and comments.
|
|
1550
|
+
Body replaces the whole body, at most 1024 characters. See backlog://guide/memory."""
|
|
1551
|
+
if args.create_only and args.id is not None:
|
|
1552
|
+
raise ValueError("create_only cannot be used with id")
|
|
1553
|
+
if args.id is None:
|
|
1554
|
+
if not args.title:
|
|
1555
|
+
raise ValueError("a new note needs a title")
|
|
1556
|
+
project = await backlog.resolve(args.project)
|
|
1557
|
+
existing = await note_by_title(
|
|
1558
|
+
connection, backlog.actor, args.title, project_id=project.id, shared=bool(args.shared)
|
|
1559
|
+
)
|
|
1560
|
+
if existing is None:
|
|
1561
|
+
async with transaction:
|
|
1562
|
+
saved = await add_note(
|
|
1563
|
+
connection,
|
|
1564
|
+
backlog.actor,
|
|
1565
|
+
project_id=project.id,
|
|
1566
|
+
title=args.title,
|
|
1567
|
+
body=args.body or "",
|
|
1568
|
+
tags=args.tags or (),
|
|
1569
|
+
shared=bool(args.shared),
|
|
1570
|
+
global_scope=bool(args.global_scope),
|
|
1571
|
+
)
|
|
1572
|
+
embeddings.enqueue(backlog, saved)
|
|
1573
|
+
await changed(
|
|
1574
|
+
note_event(saved, Action.ADDED, computer=backlog.computer),
|
|
1575
|
+
recipients=None if saved.shared else [backlog.principal],
|
|
1576
|
+
)
|
|
1577
|
+
return note_out(saved)
|
|
1578
|
+
if args.create_only:
|
|
1579
|
+
raise ValueError("a note with this title already exists; choose another title")
|
|
1580
|
+
args.id = existing.id
|
|
1581
|
+
previous = await read_note(connection, backlog.actor, args.id)
|
|
1582
|
+
async with transaction:
|
|
1583
|
+
updated = await update_note(
|
|
1584
|
+
connection,
|
|
1585
|
+
backlog.actor,
|
|
1586
|
+
args.id,
|
|
1587
|
+
title=args.title,
|
|
1588
|
+
body=args.body,
|
|
1589
|
+
tags=args.tags,
|
|
1590
|
+
shared=args.shared,
|
|
1591
|
+
global_scope=args.global_scope,
|
|
1592
|
+
)
|
|
1593
|
+
if updated is None:
|
|
1594
|
+
raise LookupError(f"no note #{args.id}")
|
|
1595
|
+
embeddings.enqueue(backlog, updated)
|
|
1596
|
+
if previous and previous.shared and not updated.shared:
|
|
1597
|
+
await changed(note_event(previous, Action.DELETED, computer=backlog.computer))
|
|
1598
|
+
await changed(
|
|
1599
|
+
note_event(updated, Action.UPDATED, computer=backlog.computer),
|
|
1600
|
+
recipients=None if updated.shared else [backlog.principal],
|
|
1601
|
+
)
|
|
1602
|
+
return note_out(updated)
|
|
1603
|
+
|
|
1604
|
+
|
|
1605
|
+
@registry.tool(title="Append to a memory entry", icons=[Icons.PLUS])
|
|
1606
|
+
async def memory_append(
|
|
1607
|
+
args: AppendNote, backlog: Backlog, connection: aiosqlite.Connection, transaction: Transaction
|
|
1608
|
+
) -> NoteOut:
|
|
1609
|
+
"""Add a new related fact to an entry. To correct or update existing content, use memory_save.
|
|
1610
|
+
See backlog://guide/memory."""
|
|
1611
|
+
found = await note_of(backlog, args.id, args.title, args.project)
|
|
1612
|
+
async with transaction:
|
|
1613
|
+
updated = await append_note(connection, backlog.actor, found.id, args.text)
|
|
1614
|
+
if updated is None:
|
|
1615
|
+
raise LookupError(f"no note #{found.id}")
|
|
1616
|
+
embeddings.enqueue(backlog, updated)
|
|
1617
|
+
await changed(
|
|
1618
|
+
note_event(updated, Action.UPDATED, computer=backlog.computer),
|
|
1619
|
+
recipients=None if updated.shared else [backlog.principal],
|
|
1620
|
+
)
|
|
1621
|
+
return note_out(updated)
|
|
1622
|
+
|
|
1623
|
+
|
|
1624
|
+
class MemorySharing(BaseModel):
|
|
1625
|
+
id: int = Field(description="Memory entry id.")
|
|
1626
|
+
shared: bool = Field(
|
|
1627
|
+
description="Project members can read it; false limits it to the entry author."
|
|
1628
|
+
)
|
|
1629
|
+
global_scope: bool = Field(description="Use in all projects without changing who can read it.")
|
|
1630
|
+
|
|
1631
|
+
|
|
1632
|
+
@registry.tool(title="Set memory sharing", annotations=Hint.IDEMPOTENT, icons=[Icons.USERS])
|
|
1633
|
+
async def memory_sharing(
|
|
1634
|
+
args: MemorySharing,
|
|
1635
|
+
backlog: Backlog,
|
|
1636
|
+
connection: aiosqlite.Connection,
|
|
1637
|
+
transaction: Transaction,
|
|
1638
|
+
) -> NoteOut:
|
|
1639
|
+
"""Set who can read an entry and whether it appears in all projects.
|
|
1640
|
+
If the project, audience or global scope is unclear, ask the user before changing it.
|
|
1641
|
+
Do not guess or use defaults to resolve uncertainty.
|
|
1642
|
+
shared=false means only the entry author; shared=true means source-project members.
|
|
1643
|
+
global_scope=true keeps that audience and makes the entry available in every project.
|
|
1644
|
+
Only the entry author can make a shared entry personal.
|
|
1645
|
+
See backlog://guide/memory."""
|
|
1646
|
+
saved: NoteOut = await memory_save(
|
|
1647
|
+
SaveNote(id=args.id, shared=args.shared, global_scope=args.global_scope),
|
|
1648
|
+
backlog,
|
|
1649
|
+
connection,
|
|
1650
|
+
transaction,
|
|
1651
|
+
)
|
|
1652
|
+
return saved
|
|
1653
|
+
|
|
1654
|
+
|
|
1655
|
+
@registry.tool(
|
|
1656
|
+
title="Read a memory entry",
|
|
1657
|
+
annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD,
|
|
1658
|
+
icons=[Icons.FILE_LINES],
|
|
1659
|
+
)
|
|
1660
|
+
async def memory_get(args: ReadNote, backlog: Backlog) -> NoteOut:
|
|
1661
|
+
"""Read one memory entry by id or title. IDs work across accessible projects; titles resolve
|
|
1662
|
+
within the selected project first, then visible global entries. Personal entries take
|
|
1663
|
+
precedence within each scope. Read entries relevant to the current work.
|
|
1664
|
+
See backlog://guide/memory."""
|
|
1665
|
+
return note_out(await note_of(backlog, args.id, args.title, args.project))
|
|
1666
|
+
|
|
1667
|
+
|
|
1668
|
+
@registry.tool(
|
|
1669
|
+
title="List memory entries",
|
|
1670
|
+
annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD,
|
|
1671
|
+
icons=[Icons.LAYER_GROUP],
|
|
1672
|
+
)
|
|
1673
|
+
async def memory_list(
|
|
1674
|
+
args: ListNotes, backlog: Backlog, connection: aiosqlite.Connection
|
|
1675
|
+
) -> Notes | FullNotes:
|
|
1676
|
+
"""List memory entries. For initial context, use project=<full_name>.
|
|
1677
|
+
Keep the default compact result, then read relevant entries with memory_get.
|
|
1678
|
+
Includes your personal entries and entries shared with the selected project.
|
|
1679
|
+
Explicit global entries are also included and come first.
|
|
1680
|
+
Use shared only to filter visibility, not to select a project.
|
|
1681
|
+
See backlog://guide/memory."""
|
|
1682
|
+
reach = await backlog.reach(
|
|
1683
|
+
args.project,
|
|
1684
|
+
all_projects=args.all_projects,
|
|
1685
|
+
computer=args.computer,
|
|
1686
|
+
all_computers=args.all_computers,
|
|
1687
|
+
)
|
|
1688
|
+
found = await read_notes(
|
|
1689
|
+
connection,
|
|
1690
|
+
backlog.actor,
|
|
1691
|
+
project_id=reach.project_id,
|
|
1692
|
+
computer=reach.computer,
|
|
1693
|
+
tag=args.tag,
|
|
1694
|
+
shared=args.shared,
|
|
1695
|
+
limit=args.limit,
|
|
1696
|
+
offset=args.offset,
|
|
1697
|
+
)
|
|
1698
|
+
total = await count_notes(
|
|
1699
|
+
connection,
|
|
1700
|
+
backlog.actor,
|
|
1701
|
+
project_id=reach.project_id,
|
|
1702
|
+
computer=reach.computer,
|
|
1703
|
+
tag=args.tag,
|
|
1704
|
+
shared=args.shared,
|
|
1705
|
+
)
|
|
1706
|
+
if args.full:
|
|
1707
|
+
return FullNotes(
|
|
1708
|
+
items=[note_out(item) for item in found],
|
|
1709
|
+
count=total,
|
|
1710
|
+
project=reach.slug,
|
|
1711
|
+
computer=backlog.computer,
|
|
1712
|
+
principal=backlog.principal,
|
|
1713
|
+
)
|
|
1714
|
+
return Notes(
|
|
1715
|
+
items=[note_list_item(item) for item in found],
|
|
1716
|
+
count=total,
|
|
1717
|
+
project=reach.slug,
|
|
1718
|
+
computer=backlog.computer,
|
|
1719
|
+
principal=backlog.principal,
|
|
1720
|
+
)
|
|
1721
|
+
|
|
1722
|
+
|
|
1723
|
+
@registry.tool(
|
|
1724
|
+
title="Delete a memory entry",
|
|
1725
|
+
annotations=Hint.DESTRUCTIVE | ~Hint.OPEN_WORLD,
|
|
1726
|
+
icons=[Icons.TRASH_CAN],
|
|
1727
|
+
)
|
|
1728
|
+
async def memory_delete(
|
|
1729
|
+
args: NoteId,
|
|
1730
|
+
ex: Exchange,
|
|
1731
|
+
backlog: Backlog,
|
|
1732
|
+
connection: aiosqlite.Connection,
|
|
1733
|
+
transaction: Transaction,
|
|
1734
|
+
) -> Deleted:
|
|
1735
|
+
"""Delete a memory entry permanently, only at the user's request. Entries within the
|
|
1736
|
+
connection's project scope are deleted immediately. Other entries require user confirmation;
|
|
1737
|
+
clients without elicitation are refused.
|
|
1738
|
+
Read backlog://guide/memory for rules."""
|
|
1739
|
+
found = await read_note(connection, backlog.actor, args.id)
|
|
1740
|
+
if found is None:
|
|
1741
|
+
raise LookupError(f"no note #{args.id}")
|
|
1742
|
+
if refusal := await confirm_cross_project_delete(found, ex, backlog):
|
|
1743
|
+
return refusal
|
|
1744
|
+
async with transaction:
|
|
1745
|
+
deleted = await delete_note(connection, backlog.actor, args.id)
|
|
1746
|
+
embeddings.enqueue(backlog, found, deleted=True)
|
|
1747
|
+
await changed(
|
|
1748
|
+
note_event(found, Action.DELETED, computer=backlog.computer),
|
|
1749
|
+
recipients=None if found.shared else [backlog.principal],
|
|
1750
|
+
)
|
|
1751
|
+
return Deleted(
|
|
1752
|
+
principal=backlog.principal,
|
|
1753
|
+
computer=backlog.computer,
|
|
1754
|
+
id=args.id,
|
|
1755
|
+
title=found.title,
|
|
1756
|
+
deleted=deleted,
|
|
1757
|
+
)
|
|
1758
|
+
|
|
1759
|
+
|
|
1760
|
+
@registry.tool(
|
|
1761
|
+
title="Search the backlog",
|
|
1762
|
+
annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD,
|
|
1763
|
+
icons=[Icons.MAGNIFYING_GLASS],
|
|
1764
|
+
)
|
|
1765
|
+
async def search(
|
|
1766
|
+
args: embeddings.Search,
|
|
1767
|
+
backlog: Backlog,
|
|
1768
|
+
connection: aiosqlite.Connection,
|
|
1769
|
+
transaction: Transaction,
|
|
1770
|
+
) -> embeddings.Matches:
|
|
1771
|
+
"""Find tasks, comments, memory and projects by text or meaning.
|
|
1772
|
+
Use it to look up past work or agreements on a topic; for project context, use
|
|
1773
|
+
project_board.
|
|
1774
|
+
Quoted expressions use full-text search, unquoted text uses vectors, mixed queries use both.
|
|
1775
|
+
Examples: `"refresh token"` matches a phrase; `"token"*` matches a prefix;
|
|
1776
|
+
`token reset` searches meaning. Pass a list in query for alternative formulations.
|
|
1777
|
+
See backlog://guide/search for full syntax and filters."""
|
|
1778
|
+
return await embeddings.search(args, backlog, connection, transaction)
|
|
1779
|
+
|
|
1780
|
+
|
|
1781
|
+
@registry.tool(
|
|
1782
|
+
title="Backlog statistics",
|
|
1783
|
+
annotations=Hint.READ_ONLY | ~Hint.OPEN_WORLD,
|
|
1784
|
+
icons=[Icons.CHART_LINE],
|
|
1785
|
+
)
|
|
1786
|
+
async def stats(args: StatsOf, backlog: Backlog) -> Stats:
|
|
1787
|
+
"""Aggregate counts across projects or computers. For one project, use project_board.
|
|
1788
|
+
Read backlog://guide/workflow for rules."""
|
|
1789
|
+
reach = await backlog.reach(
|
|
1790
|
+
args.project,
|
|
1791
|
+
all_projects=args.all_projects,
|
|
1792
|
+
computer=args.computer,
|
|
1793
|
+
all_computers=args.all_computers,
|
|
1794
|
+
)
|
|
1795
|
+
return await stats_of(backlog, reach)
|
|
1796
|
+
|
|
1797
|
+
|
|
1798
|
+
def register_guide(topic: str | None) -> None:
|
|
1799
|
+
uri = "backlog://guide" + (f"/{topic}" if topic else "")
|
|
1800
|
+
|
|
1801
|
+
@registry.resource(
|
|
1802
|
+
uri, name=f"guide_{topic or 'index'}", mime_type="text/markdown", icons=[Icons.CIRCLE_INFO]
|
|
1803
|
+
)
|
|
1804
|
+
async def content(args: Nothing) -> TextResourceContents:
|
|
1805
|
+
return TextResourceContents(uri=uri, mime_type="text/markdown", text=guidance.read(topic))
|
|
1806
|
+
|
|
1807
|
+
|
|
1808
|
+
for guide_topic in (None, *guidance.TOPICS):
|
|
1809
|
+
register_guide(guide_topic)
|
|
1810
|
+
|
|
1811
|
+
|
|
1812
|
+
@registry.resource("backlog://project", mime_type="application/json", icons=[Icons.FOLDER])
|
|
1813
|
+
async def current_project(args: Nothing, backlog: Backlog) -> dict[str, Any]:
|
|
1814
|
+
"""The project the tools work on, or `null` with origin `none` when there is none."""
|
|
1815
|
+
project, origin = await backlog.scope()
|
|
1816
|
+
shown = (
|
|
1817
|
+
project_out(project, computer=backlog.computer, principal=backlog.principal).model_dump(
|
|
1818
|
+
mode="json"
|
|
1819
|
+
)
|
|
1820
|
+
if project
|
|
1821
|
+
else None
|
|
1822
|
+
)
|
|
1823
|
+
return {"project": shown, "origin": origin.value, "computer": backlog.computer}
|
|
1824
|
+
|
|
1825
|
+
|
|
1826
|
+
@registry.resource("backlog://projects", mime_type="application/json", icons=[Icons.FOLDER])
|
|
1827
|
+
async def all_projects(
|
|
1828
|
+
args: Nothing, backlog: Backlog, connection: aiosqlite.Connection
|
|
1829
|
+
) -> list[dict[str, Any]]:
|
|
1830
|
+
"""Every registered project of this computer."""
|
|
1831
|
+
found = await read_projects(connection, backlog.actor, computer=backlog.computer)
|
|
1832
|
+
return [
|
|
1833
|
+
project_out(item, computer=backlog.computer, principal=backlog.principal).model_dump(
|
|
1834
|
+
mode="json"
|
|
1835
|
+
)
|
|
1836
|
+
for item in found
|
|
1837
|
+
]
|
|
1838
|
+
|
|
1839
|
+
|
|
1840
|
+
@registry.resource("backlog://tasks", mime_type="application/json", icons=[Icons.LAYER_GROUP])
|
|
1841
|
+
async def open_tasks(
|
|
1842
|
+
args: Nothing, backlog: Backlog, connection: aiosqlite.Connection
|
|
1843
|
+
) -> list[dict[str, Any]]:
|
|
1844
|
+
"""Open tasks of the current project, most important first."""
|
|
1845
|
+
project = await backlog.resolve(None)
|
|
1846
|
+
found = await read_tasks(
|
|
1847
|
+
connection, backlog.actor, project_id=project.id, status=list(OPEN_STATUSES), limit=200
|
|
1848
|
+
)
|
|
1849
|
+
return [task_out(item).model_dump(mode="json") for item in found]
|
|
1850
|
+
|
|
1851
|
+
|
|
1852
|
+
class TaskUri(BaseModel):
|
|
1853
|
+
id: str
|
|
1854
|
+
|
|
1855
|
+
|
|
1856
|
+
@registry.resource(
|
|
1857
|
+
"backlog://tasks/{id}", name="task", mime_type="application/json", icons=[Icons.FILE_LINES]
|
|
1858
|
+
)
|
|
1859
|
+
async def one_task(
|
|
1860
|
+
args: TaskUri, backlog: Backlog, connection: aiosqlite.Connection
|
|
1861
|
+
) -> dict[str, Any]:
|
|
1862
|
+
"""One task by id."""
|
|
1863
|
+
if not args.id.isdigit():
|
|
1864
|
+
return {"error": f"invalid task id {args.id!r}"}
|
|
1865
|
+
task = await read_task(connection, backlog.actor, int(args.id))
|
|
1866
|
+
if task is None:
|
|
1867
|
+
return {"error": f"no task #{args.id}"}
|
|
1868
|
+
return (await task_details(task, backlog)).model_dump(mode="json")
|
|
1869
|
+
|
|
1870
|
+
|
|
1871
|
+
@registry.resource("backlog://memory", mime_type="application/json", icons=[Icons.DATABASE])
|
|
1872
|
+
async def known_notes(
|
|
1873
|
+
args: Nothing, backlog: Backlog, connection: aiosqlite.Connection
|
|
1874
|
+
) -> list[dict[str, Any]]:
|
|
1875
|
+
"""Full memory entries in connection project scope: your personal and project-shared entries.
|
|
1876
|
+
For initial context, use memory_list(project=<full_name>) instead.
|
|
1877
|
+
See backlog://guide/memory."""
|
|
1878
|
+
project = await backlog.resolve(None)
|
|
1879
|
+
found = await read_notes(connection, backlog.actor, project_id=project.id, limit=200)
|
|
1880
|
+
return [note_out(item).model_dump(mode="json") for item in found]
|
|
1881
|
+
|
|
1882
|
+
|
|
1883
|
+
@registry.resource(
|
|
1884
|
+
"backlog://memory/{id}", name="note", mime_type="application/json", icons=[Icons.FILE_LINES]
|
|
1885
|
+
)
|
|
1886
|
+
async def one_note(
|
|
1887
|
+
args: TaskUri, backlog: Backlog, connection: aiosqlite.Connection
|
|
1888
|
+
) -> dict[str, Any]:
|
|
1889
|
+
"""One memory entry by id."""
|
|
1890
|
+
if not args.id.isdigit():
|
|
1891
|
+
return {"error": f"invalid note id {args.id!r}"}
|
|
1892
|
+
found = await read_note(connection, backlog.actor, int(args.id))
|
|
1893
|
+
if found is None:
|
|
1894
|
+
return {"error": f"no note #{args.id}"}
|
|
1895
|
+
return note_out(found).model_dump(mode="json")
|
|
1896
|
+
|
|
1897
|
+
|
|
1898
|
+
registry.extension(skills.extension)
|
|
1899
|
+
|
|
1900
|
+
|
|
1901
|
+
@registry.prompt(title="Install Backlog rules into the global Agents config", icons=[Icons.GEAR])
|
|
1902
|
+
async def install_backlog_rules(args: Nothing) -> str:
|
|
1903
|
+
"""Install the Backlog workflow in global AGENTS.md and CLAUDE.md files."""
|
|
1904
|
+
rules = files("backlog_mcp").joinpath("global_rules.md").read_text(encoding="utf-8")
|
|
1905
|
+
return (
|
|
1906
|
+
"Install the following Backlog rules in the user's global agent configuration.\n\n"
|
|
1907
|
+
"Work on the user's local computer, not on the MCP server. For Codex, use "
|
|
1908
|
+
"AGENTS.md under CODEX_HOME when configured, otherwise ~/.codex/AGENTS.md. "
|
|
1909
|
+
"For Claude Code, use ~/.claude/CLAUDE.md. Respect an explicitly configured "
|
|
1910
|
+
"alternative global location. Do not put these rules in repository-local files.\n\n"
|
|
1911
|
+
"Read both existing files first. Create missing parent directories and files. "
|
|
1912
|
+
"Replace the complete section between <!-- BACKLOG_START --> and "
|
|
1913
|
+
"<!-- BACKLOG_END -->, including the markers, with the block below. "
|
|
1914
|
+
"If no marked section exists, append the block with a blank line before it. "
|
|
1915
|
+
"Preserve unrelated instructions. If markers are incomplete or duplicated, "
|
|
1916
|
+
"resolve the ambiguity before replacing any content. Do not duplicate the rules "
|
|
1917
|
+
"when this prompt is run again.\n\n"
|
|
1918
|
+
"Verify that both files contain exactly one identical Backlog block and that "
|
|
1919
|
+
"unrelated content is unchanged. Report the actual paths and any conflicting "
|
|
1920
|
+
"existing instructions. If local file access is unavailable, return the block "
|
|
1921
|
+
"and installation instructions without claiming that files were changed.\n\n"
|
|
1922
|
+
"Install this exact block:\n\n" + rules
|
|
1923
|
+
)
|
|
1924
|
+
|
|
1925
|
+
|
|
1926
|
+
class Standup(BaseModel):
|
|
1927
|
+
project: str | None = None
|
|
1928
|
+
all_projects: bool = False
|
|
1929
|
+
computer: str | None = None
|
|
1930
|
+
all_computers: bool = False
|
|
1931
|
+
|
|
1932
|
+
|
|
1933
|
+
@registry.prompt(icons=[Icons.CLOCK_ROTATE_LEFT])
|
|
1934
|
+
async def standup(args: Standup, backlog: Backlog, connection: aiosqlite.Connection) -> str:
|
|
1935
|
+
"""Draft a standup update from what closed recently and what is still open."""
|
|
1936
|
+
reach = await backlog.reach(
|
|
1937
|
+
args.project,
|
|
1938
|
+
all_projects=args.all_projects,
|
|
1939
|
+
computer=args.computer,
|
|
1940
|
+
all_computers=args.all_computers,
|
|
1941
|
+
)
|
|
1942
|
+
done = await read_tasks(
|
|
1943
|
+
connection,
|
|
1944
|
+
backlog.actor,
|
|
1945
|
+
project_id=reach.project_id,
|
|
1946
|
+
computer=reach.computer,
|
|
1947
|
+
status=[Status.DONE],
|
|
1948
|
+
order="recent",
|
|
1949
|
+
limit=10,
|
|
1950
|
+
)
|
|
1951
|
+
open_ = await read_tasks(
|
|
1952
|
+
connection,
|
|
1953
|
+
backlog.actor,
|
|
1954
|
+
project_id=reach.project_id,
|
|
1955
|
+
computer=reach.computer,
|
|
1956
|
+
status=list(OPEN_STATUSES),
|
|
1957
|
+
limit=15,
|
|
1958
|
+
)
|
|
1959
|
+
lines = [f"- done: {item.title}" for item in done]
|
|
1960
|
+
lines += [f"- {item.status.value} ({item.priority.value}): {item.title}" for item in open_]
|
|
1961
|
+
return (
|
|
1962
|
+
f"Backlog for {reach.label}:\n\n"
|
|
1963
|
+
+ ("\n".join(lines) or "- nothing recorded yet")
|
|
1964
|
+
+ "\n\nWrite a short standup update: done, next, blockers."
|
|
1965
|
+
)
|
|
1966
|
+
|
|
1967
|
+
|
|
1968
|
+
class Groom(BaseModel):
|
|
1969
|
+
project: str | None = None
|
|
1970
|
+
limit: int = Field(default=30, ge=1, le=200)
|
|
1971
|
+
|
|
1972
|
+
|
|
1973
|
+
@registry.prompt(icons=[Icons.BROOM])
|
|
1974
|
+
async def groom(args: Groom, backlog: Backlog, connection: aiosqlite.Connection) -> str:
|
|
1975
|
+
"""Ask for a review of the open backlog: order, duplicates and missing detail."""
|
|
1976
|
+
project = await backlog.resolve(args.project)
|
|
1977
|
+
found = await read_tasks(
|
|
1978
|
+
connection,
|
|
1979
|
+
backlog.actor,
|
|
1980
|
+
project_id=project.id,
|
|
1981
|
+
status=list(OPEN_STATUSES),
|
|
1982
|
+
limit=args.limit,
|
|
1983
|
+
)
|
|
1984
|
+
lines = [
|
|
1985
|
+
f"- #{item.id} [{item.priority.value}/{item.status.value}] {item.title}"
|
|
1986
|
+
+ (f" (tags: {', '.join(item.tags)})" if item.tags else "")
|
|
1987
|
+
for item in found
|
|
1988
|
+
]
|
|
1989
|
+
return (
|
|
1990
|
+
f"Open backlog of {project.full_name}:\n\n"
|
|
1991
|
+
+ ("\n".join(lines) or "- nothing open")
|
|
1992
|
+
+ "\n\nReview it: what should be done first, what duplicates what, "
|
|
1993
|
+
"and which items are too vague to start. Suggest concrete edits."
|
|
1994
|
+
)
|
|
1995
|
+
|
|
1996
|
+
|
|
1997
|
+
@registry.completions
|
|
1998
|
+
async def complete(
|
|
1999
|
+
args: CompleteParams, backlog: Backlog, connection: aiosqlite.Connection
|
|
2000
|
+
) -> list[str]:
|
|
2001
|
+
"""Complete project names, tags, statuses, priorities, task ids and memory entry ids."""
|
|
2002
|
+
value = args.argument.value
|
|
2003
|
+
#: The task and the note templates both call their argument `id`; the URI tells them apart.
|
|
2004
|
+
about_notes = "memory" in (args.ref.uri or "")
|
|
2005
|
+
match args.argument.name, args.ref.type:
|
|
2006
|
+
case "project", _:
|
|
2007
|
+
return [
|
|
2008
|
+
name
|
|
2009
|
+
for name in await slugs(connection, backlog.actor)
|
|
2010
|
+
if name.startswith(value) or name.split("/", 1)[1].startswith(value)
|
|
2011
|
+
]
|
|
2012
|
+
case "computer", _:
|
|
2013
|
+
return [
|
|
2014
|
+
name
|
|
2015
|
+
for name in await computers(connection, backlog.actor)
|
|
2016
|
+
if name.startswith(value)
|
|
2017
|
+
]
|
|
2018
|
+
case "tag", _:
|
|
2019
|
+
project, _origin = await backlog.current()
|
|
2020
|
+
tags = await read_tags(connection, backlog.actor, project_id=project.id)
|
|
2021
|
+
return [tag for tag in tags if tag.startswith(value)]
|
|
2022
|
+
case "status", _:
|
|
2023
|
+
return [item.value for item in Status if item.value.startswith(value)]
|
|
2024
|
+
case "priority", _:
|
|
2025
|
+
return [item.value for item in Priority if item.value.startswith(value)]
|
|
2026
|
+
case "id", "ref/resource" if about_notes:
|
|
2027
|
+
project, _origin = await backlog.current()
|
|
2028
|
+
notes = await read_notes(connection, backlog.actor, project_id=project.id, limit=200)
|
|
2029
|
+
return [str(item.id) for item in notes if str(item.id).startswith(value)]
|
|
2030
|
+
case "id", "ref/resource":
|
|
2031
|
+
project, _origin = await backlog.current()
|
|
2032
|
+
found = await read_tasks(connection, backlog.actor, project_id=project.id, limit=200)
|
|
2033
|
+
return [str(item.id) for item in found if str(item.id).startswith(value)]
|
|
2034
|
+
case _:
|
|
2035
|
+
return []
|
|
2036
|
+
|
|
2037
|
+
|
|
2038
|
+
__all__ = ["INSTRUCTIONS", "VERSION", "UnknownProject", "bound", "registry", "use"]
|
|
2039
|
+
|
|
2040
|
+
|
|
2041
|
+
async def search_projects(
|
|
2042
|
+
connection: aiosqlite.Connection,
|
|
2043
|
+
actor: Actor,
|
|
2044
|
+
query: str,
|
|
2045
|
+
*,
|
|
2046
|
+
project_id: int | None = None,
|
|
2047
|
+
computer: str | None = None,
|
|
2048
|
+
raw: bool = False,
|
|
2049
|
+
limit: int = 20,
|
|
2050
|
+
) -> list[Project]:
|
|
2051
|
+
match = query if raw else match_query(query)
|
|
2052
|
+
if not match:
|
|
2053
|
+
return []
|
|
2054
|
+
rows = QUERIES.search_projects(
|
|
2055
|
+
connection,
|
|
2056
|
+
match=match,
|
|
2057
|
+
project_id=project_id,
|
|
2058
|
+
computer=computer,
|
|
2059
|
+
principal=actor.principal,
|
|
2060
|
+
limit=limit,
|
|
2061
|
+
)
|
|
2062
|
+
return await attach_roots(connection, actor, [to_project(row) async for row in rows])
|
|
2063
|
+
|
|
2064
|
+
|
|
2065
|
+
async def board(
|
|
2066
|
+
connection: aiosqlite.Connection, actor: Actor, project_id: int
|
|
2067
|
+
) -> tuple[dict[str, int], BoardSection, BoardSection]:
|
|
2068
|
+
await require(connection, actor, project_id)
|
|
2069
|
+
row = await QUERIES.board_counts(connection, project_id=project_id)
|
|
2070
|
+
assert row is not None
|
|
2071
|
+
counts = dict(row)
|
|
2072
|
+
blocked = await QUERIES.board_blocked_count(
|
|
2073
|
+
connection, project_id=project_id, principal=actor.principal
|
|
2074
|
+
)
|
|
2075
|
+
assert blocked is not None
|
|
2076
|
+
counts["blocked"] = blocked[0]
|
|
2077
|
+
|
|
2078
|
+
ready_count = await QUERIES.board_ready_count(
|
|
2079
|
+
connection, project_id=project_id, principal=actor.principal
|
|
2080
|
+
)
|
|
2081
|
+
assert ready_count is not None
|
|
2082
|
+
ready = BoardSection(
|
|
2083
|
+
count=ready_count[0],
|
|
2084
|
+
items=[
|
|
2085
|
+
BoardTask.model_validate(dict(row))
|
|
2086
|
+
async for row in QUERIES.board_ready_tasks(
|
|
2087
|
+
connection, project_id=project_id, principal=actor.principal
|
|
2088
|
+
)
|
|
2089
|
+
],
|
|
2090
|
+
)
|
|
2091
|
+
|
|
2092
|
+
prefix = principal_label(actor.principal, actor.computer) + ":"
|
|
2093
|
+
claimed_count = await QUERIES.board_claimed_count(
|
|
2094
|
+
connection, project_id=project_id, prefix=prefix
|
|
2095
|
+
)
|
|
2096
|
+
assert claimed_count is not None
|
|
2097
|
+
claimed = BoardSection(
|
|
2098
|
+
count=claimed_count[0],
|
|
2099
|
+
items=[
|
|
2100
|
+
BoardTask.model_validate(dict(row))
|
|
2101
|
+
async for row in QUERIES.board_claimed_tasks(
|
|
2102
|
+
connection, project_id=project_id, prefix=prefix
|
|
2103
|
+
)
|
|
2104
|
+
],
|
|
2105
|
+
)
|
|
2106
|
+
return counts, ready, claimed
|
|
2107
|
+
|
|
2108
|
+
|
|
2109
|
+
async def assigned_tasks(
|
|
2110
|
+
connection: aiosqlite.Connection, actor: Actor, prefix: str, *, limit: int = 50, offset: int = 0
|
|
2111
|
+
) -> list[Task]:
|
|
2112
|
+
rows = QUERIES.assigned_tasks(
|
|
2113
|
+
connection,
|
|
2114
|
+
prefix=prefix,
|
|
2115
|
+
pattern=assignment_pattern(prefix),
|
|
2116
|
+
principal=actor.principal,
|
|
2117
|
+
limit=limit,
|
|
2118
|
+
offset=offset,
|
|
2119
|
+
)
|
|
2120
|
+
return [to_task(row) async for row in rows]
|
|
2121
|
+
|
|
2122
|
+
|
|
2123
|
+
async def count_assigned(connection: aiosqlite.Connection, actor: Actor, prefix: str) -> int:
|
|
2124
|
+
row = await QUERIES.count_assigned(
|
|
2125
|
+
connection,
|
|
2126
|
+
prefix=prefix,
|
|
2127
|
+
pattern=assignment_pattern(prefix),
|
|
2128
|
+
principal=actor.principal,
|
|
2129
|
+
)
|
|
2130
|
+
assert row is not None
|
|
2131
|
+
return int(row[0])
|
|
2132
|
+
|
|
2133
|
+
|
|
2134
|
+
async def claim_task(
|
|
2135
|
+
connection: aiosqlite.Connection,
|
|
2136
|
+
actor: Actor,
|
|
2137
|
+
task_id: int,
|
|
2138
|
+
*,
|
|
2139
|
+
expected: str | None,
|
|
2140
|
+
assigned: str | None,
|
|
2141
|
+
temporary: bool = False,
|
|
2142
|
+
restore: bool = False,
|
|
2143
|
+
) -> tuple[Task, bool]:
|
|
2144
|
+
"""Compare and replace an assignment under a database writer lock."""
|
|
2145
|
+
if temporary and restore:
|
|
2146
|
+
raise ValueError("temporary and restore are mutually exclusive")
|
|
2147
|
+
if temporary and (assigned is None or assigned == expected):
|
|
2148
|
+
raise ValueError("An edit claim requires a new assignment marker")
|
|
2149
|
+
if restore and (assigned is not None or expected is None):
|
|
2150
|
+
raise ValueError("Restore requires expected and assigned=null")
|
|
2151
|
+
if expected is not None:
|
|
2152
|
+
check_assignment(expected)
|
|
2153
|
+
if assigned is not None:
|
|
2154
|
+
check_assignment(assigned)
|
|
2155
|
+
principal = principal_label(actor.principal, actor.computer or hostname())
|
|
2156
|
+
if assigned.rsplit(":", 1)[0] != principal:
|
|
2157
|
+
raise PermissionError(f"assigned must belong to the current principal {principal!r}")
|
|
2158
|
+
|
|
2159
|
+
task = await read_task(connection, actor, task_id)
|
|
2160
|
+
if task is None:
|
|
2161
|
+
raise LookupError(f"no task #{task_id}")
|
|
2162
|
+
await require(connection, actor, task.project_id, role="editor")
|
|
2163
|
+
if task.assigned != expected:
|
|
2164
|
+
raise AssignmentConflict(task_id, expected, task.assigned)
|
|
2165
|
+
lease = await QUERIES.edit_claim(connection, task_id=task_id)
|
|
2166
|
+
if restore:
|
|
2167
|
+
principal = principal_label(actor.principal, actor.computer or hostname())
|
|
2168
|
+
if expected is None or expected.rsplit(":", 1)[0] != principal:
|
|
2169
|
+
raise PermissionError("Only the edit claimant can restore its previous assignment")
|
|
2170
|
+
if lease is None or lease["assigned"] != expected:
|
|
2171
|
+
raise ValueError("No matching edit claim to restore")
|
|
2172
|
+
assigned = lease["previous"]
|
|
2173
|
+
elif temporary and lease is not None:
|
|
2174
|
+
raise ValueError(f"Task is being edited by {task.assigned}")
|
|
2175
|
+
if expected == assigned:
|
|
2176
|
+
return task, False
|
|
2177
|
+
if temporary:
|
|
2178
|
+
await QUERIES.save_edit_claim(
|
|
2179
|
+
connection, task_id=task_id, assigned=assigned, previous=expected
|
|
2180
|
+
)
|
|
2181
|
+
else:
|
|
2182
|
+
await QUERIES.remove_edit_claim(connection, task_id=task_id)
|
|
2183
|
+
now = time.time()
|
|
2184
|
+
await QUERIES.claim_task(
|
|
2185
|
+
connection,
|
|
2186
|
+
task_id=task_id,
|
|
2187
|
+
expected=expected,
|
|
2188
|
+
assigned=assigned,
|
|
2189
|
+
now=now,
|
|
2190
|
+
principal=actor.principal,
|
|
2191
|
+
computer=actor.computer or hostname(),
|
|
2192
|
+
)
|
|
2193
|
+
await QUERIES.add_comment(
|
|
2194
|
+
connection,
|
|
2195
|
+
task_id=task_id,
|
|
2196
|
+
body=f"Assignment changed: {expected or 'null'} → {assigned or 'null'}",
|
|
2197
|
+
created_at=now,
|
|
2198
|
+
principal=actor.principal,
|
|
2199
|
+
computer=actor.computer or hostname(),
|
|
2200
|
+
)
|
|
2201
|
+
saved = await read_task(connection, actor, task_id)
|
|
2202
|
+
assert saved is not None
|
|
2203
|
+
return saved, True
|
|
2204
|
+
|
|
2205
|
+
|
|
2206
|
+
async def read_linked_tasks(
|
|
2207
|
+
connection: aiosqlite.Connection,
|
|
2208
|
+
actor: Actor,
|
|
2209
|
+
*,
|
|
2210
|
+
task_id: int | None = None,
|
|
2211
|
+
project_id: int | None = None,
|
|
2212
|
+
) -> list[Task]:
|
|
2213
|
+
"""Resource dependents for internal routing, never for a public response."""
|
|
2214
|
+
rows = QUERIES.linked_tasks(connection, task_id=task_id, project_id=project_id)
|
|
2215
|
+
return [to_task(row) async for row in rows]
|
|
2216
|
+
|
|
2217
|
+
|
|
2218
|
+
async def notification_recipients(
|
|
2219
|
+
connection: aiosqlite.Connection, actor: Actor, project_id: int
|
|
2220
|
+
) -> list[str]:
|
|
2221
|
+
return [
|
|
2222
|
+
row["principal"]
|
|
2223
|
+
async for row in QUERIES.notification_recipients(connection, project_id=project_id)
|
|
2224
|
+
]
|
|
2225
|
+
|
|
2226
|
+
|
|
2227
|
+
async def task_links(
|
|
2228
|
+
connection: aiosqlite.Connection, actor: Actor, task_id: int
|
|
2229
|
+
) -> list[TaskLinkOut]:
|
|
2230
|
+
if await read_task(connection, actor, task_id) is None:
|
|
2231
|
+
raise LookupError(f"no task #{task_id}")
|
|
2232
|
+
rows = QUERIES.task_links(connection, task_id=task_id, principal=actor.principal)
|
|
2233
|
+
return [TaskLinkOut(**dict(row)) async for row in rows]
|
|
2234
|
+
|
|
2235
|
+
|
|
2236
|
+
async def link_task(
|
|
2237
|
+
connection: aiosqlite.Connection,
|
|
2238
|
+
actor: Actor,
|
|
2239
|
+
task_id: int,
|
|
2240
|
+
kind: Relation,
|
|
2241
|
+
target_id: int,
|
|
2242
|
+
*,
|
|
2243
|
+
reason: str | None = None,
|
|
2244
|
+
) -> bool:
|
|
2245
|
+
"""Write both directions under one SQLite writer lock, including cycle checks."""
|
|
2246
|
+
return await _task_link(
|
|
2247
|
+
connection, actor, task_id, kind, target_id, reason=reason, remove=False
|
|
2248
|
+
)
|
|
2249
|
+
|
|
2250
|
+
|
|
2251
|
+
async def unlink_task(
|
|
2252
|
+
connection: aiosqlite.Connection, actor: Actor, task_id: int, kind: Relation, target_id: int
|
|
2253
|
+
) -> bool:
|
|
2254
|
+
return await _task_link(connection, actor, task_id, kind, target_id, reason=None, remove=True)
|
|
2255
|
+
|
|
2256
|
+
|
|
2257
|
+
async def _task_link(
|
|
2258
|
+
connection: aiosqlite.Connection,
|
|
2259
|
+
actor: Actor,
|
|
2260
|
+
task_id: int,
|
|
2261
|
+
kind: Relation,
|
|
2262
|
+
target_id: int,
|
|
2263
|
+
*,
|
|
2264
|
+
reason: str | None,
|
|
2265
|
+
remove: bool,
|
|
2266
|
+
) -> bool:
|
|
2267
|
+
if task_id == target_id:
|
|
2268
|
+
raise ValueError("a task cannot link to itself")
|
|
2269
|
+
forward, inverse = relation(kind)
|
|
2270
|
+
|
|
2271
|
+
for record_id in (task_id, target_id):
|
|
2272
|
+
task = await read_task(connection, actor, record_id)
|
|
2273
|
+
if task is None:
|
|
2274
|
+
raise LookupError(f"no task #{record_id}")
|
|
2275
|
+
await require(connection, actor, task.project_id, role="editor")
|
|
2276
|
+
pair = {
|
|
2277
|
+
"task_id": task_id,
|
|
2278
|
+
"target_id": target_id,
|
|
2279
|
+
"relation": forward.value,
|
|
2280
|
+
"inverse": inverse.value,
|
|
2281
|
+
}
|
|
2282
|
+
existing = [row async for row in QUERIES.link_pair(connection, **pair)]
|
|
2283
|
+
if remove:
|
|
2284
|
+
if not existing:
|
|
2285
|
+
return False
|
|
2286
|
+
await QUERIES.unlink_tasks(connection, **pair)
|
|
2287
|
+
else:
|
|
2288
|
+
reasons = {row["reason"] for row in existing}
|
|
2289
|
+
if len(reasons) > 1:
|
|
2290
|
+
raise ValueError("inverse links have conflicting reasons; unlink them first")
|
|
2291
|
+
saved_reason = reason if reason is not None else next(iter(reasons), "")
|
|
2292
|
+
if len(existing) == 2 and reasons == {saved_reason}:
|
|
2293
|
+
return False
|
|
2294
|
+
# Inspect only one orientation; reverse rows must not create false cycles.
|
|
2295
|
+
source, target, directed = task_id, target_id, forward
|
|
2296
|
+
if forward in (Relation.CONTAINS, Relation.BLOCKS):
|
|
2297
|
+
source, target, directed = target_id, task_id, inverse
|
|
2298
|
+
if directed in (Relation.PART_OF, Relation.DEPENDS_ON):
|
|
2299
|
+
cycle = await QUERIES.link_cycle(
|
|
2300
|
+
connection, source=source, target=target, relation=directed.value
|
|
2301
|
+
)
|
|
2302
|
+
if cycle:
|
|
2303
|
+
raise ValueError(f"{directed.value} would create a cycle")
|
|
2304
|
+
now = time.time()
|
|
2305
|
+
await QUERIES.link_tasks(
|
|
2306
|
+
connection,
|
|
2307
|
+
**pair,
|
|
2308
|
+
reason=saved_reason,
|
|
2309
|
+
now=now,
|
|
2310
|
+
principal=actor.principal,
|
|
2311
|
+
computer=actor.computer or hostname(),
|
|
2312
|
+
)
|
|
2313
|
+
await QUERIES.touch_link_tasks(
|
|
2314
|
+
connection,
|
|
2315
|
+
task_id=task_id,
|
|
2316
|
+
target_id=target_id,
|
|
2317
|
+
now=time.time(),
|
|
2318
|
+
principal=actor.principal,
|
|
2319
|
+
computer=actor.computer or hostname(),
|
|
2320
|
+
)
|
|
2321
|
+
return True
|
|
2322
|
+
|
|
2323
|
+
|
|
2324
|
+
async def read_task(connection: aiosqlite.Connection, actor: Actor, task_id: int) -> Task | None:
|
|
2325
|
+
row = await QUERIES.task(connection, task_id=task_id, principal=actor.principal)
|
|
2326
|
+
return to_task(row) if row else None
|
|
2327
|
+
|
|
2328
|
+
|
|
2329
|
+
async def add_task(
|
|
2330
|
+
connection: aiosqlite.Connection,
|
|
2331
|
+
actor: Actor,
|
|
2332
|
+
*,
|
|
2333
|
+
project_id: int,
|
|
2334
|
+
title: str,
|
|
2335
|
+
body: str = "",
|
|
2336
|
+
status: Status = Status.TODO,
|
|
2337
|
+
priority: Priority = Priority.NORMAL,
|
|
2338
|
+
tags: Iterable[str] = (),
|
|
2339
|
+
) -> Task:
|
|
2340
|
+
await require(connection, actor, project_id, role="editor")
|
|
2341
|
+
|
|
2342
|
+
now = time.time()
|
|
2343
|
+
closed = now if status in CLOSED_STATUSES else None
|
|
2344
|
+
inserted_id = await QUERIES.add_task(
|
|
2345
|
+
connection,
|
|
2346
|
+
project_id=project_id,
|
|
2347
|
+
title=title,
|
|
2348
|
+
body=body,
|
|
2349
|
+
status=status.value,
|
|
2350
|
+
priority=RANKS[priority],
|
|
2351
|
+
tags=join_tags(tags),
|
|
2352
|
+
created_at=now,
|
|
2353
|
+
updated_at=now,
|
|
2354
|
+
closed_at=closed,
|
|
2355
|
+
created_principal=actor.principal,
|
|
2356
|
+
created_computer=actor.computer or hostname(),
|
|
2357
|
+
updated_principal=actor.principal,
|
|
2358
|
+
updated_computer=actor.computer or hostname(),
|
|
2359
|
+
)
|
|
2360
|
+
created = await read_task(connection, actor, inserted_id or 0)
|
|
2361
|
+
assert created is not None, "the task just written is not there"
|
|
2362
|
+
return created
|
|
2363
|
+
|
|
2364
|
+
|
|
2365
|
+
async def update_task(
|
|
2366
|
+
connection: aiosqlite.Connection,
|
|
2367
|
+
actor: Actor,
|
|
2368
|
+
task_id: int,
|
|
2369
|
+
*,
|
|
2370
|
+
title: str | None = None,
|
|
2371
|
+
body: str | None = None,
|
|
2372
|
+
status: Status | None = None,
|
|
2373
|
+
priority: Priority | None = None,
|
|
2374
|
+
tags: Iterable[str] | None = None,
|
|
2375
|
+
project_id: int | None = None,
|
|
2376
|
+
expected_assigned: str | None | Unset = Unset.VALUE,
|
|
2377
|
+
) -> Task | None:
|
|
2378
|
+
|
|
2379
|
+
existing = await read_task(connection, actor, task_id)
|
|
2380
|
+
if existing is None:
|
|
2381
|
+
return None
|
|
2382
|
+
await require(connection, actor, existing.project_id, role="editor")
|
|
2383
|
+
if expected_assigned is not Unset.VALUE and existing.assigned != expected_assigned:
|
|
2384
|
+
raise AssignmentConflict(task_id, expected_assigned, existing.assigned)
|
|
2385
|
+
lease = await QUERIES.edit_claim(connection, task_id=task_id)
|
|
2386
|
+
if lease is not None:
|
|
2387
|
+
principal = principal_label(actor.principal, actor.computer or hostname())
|
|
2388
|
+
if expected_assigned != lease["assigned"]:
|
|
2389
|
+
raise ValueError(
|
|
2390
|
+
f"Task is being edited by {lease['assigned']}; expected_assigned is required"
|
|
2391
|
+
)
|
|
2392
|
+
if lease["assigned"].rsplit(":", 1)[0] != principal:
|
|
2393
|
+
raise PermissionError("Only the edit claimant can save this task")
|
|
2394
|
+
changes: dict[str, Any] = {}
|
|
2395
|
+
now = time.time()
|
|
2396
|
+
if title is not None:
|
|
2397
|
+
changes["title"] = title
|
|
2398
|
+
if body is not None:
|
|
2399
|
+
changes["body"] = body
|
|
2400
|
+
if status is not None and status != existing.status:
|
|
2401
|
+
changes["status"] = status.value
|
|
2402
|
+
changes["closed_at"] = now if status in CLOSED_STATUSES else None
|
|
2403
|
+
if priority is not None:
|
|
2404
|
+
changes["priority"] = RANKS[priority]
|
|
2405
|
+
if tags is not None:
|
|
2406
|
+
changes["tags"] = join_tags(tags)
|
|
2407
|
+
if project_id is not None and project_id != existing.project_id:
|
|
2408
|
+
await require(connection, actor, project_id, role="editor")
|
|
2409
|
+
destination = await read_project(connection, actor, project_id)
|
|
2410
|
+
if destination is None:
|
|
2411
|
+
raise ValueError(f"no project #{project_id}")
|
|
2412
|
+
changes["project_id"] = project_id
|
|
2413
|
+
if changes:
|
|
2414
|
+
changes["updated_principal"] = actor.principal
|
|
2415
|
+
changes["updated_computer"] = actor.computer or hostname()
|
|
2416
|
+
changes["updated_at"] = now
|
|
2417
|
+
await QUERIES.update_task(connection, task_id=task_id, changes=json.dumps(changes))
|
|
2418
|
+
return await read_task(connection, actor, task_id)
|
|
2419
|
+
|
|
2420
|
+
|
|
2421
|
+
async def delete_task(connection: aiosqlite.Connection, actor: Actor, task_id: int) -> bool:
|
|
2422
|
+
task = await read_task(connection, actor, task_id)
|
|
2423
|
+
if task is None or task.status == Status.DELETED:
|
|
2424
|
+
return False
|
|
2425
|
+
return await update_task(connection, actor, task_id, status=Status.DELETED) is not None
|
|
2426
|
+
|
|
2427
|
+
|
|
2428
|
+
async def read_comment(
|
|
2429
|
+
connection: aiosqlite.Connection, actor: Actor, comment_id: int
|
|
2430
|
+
) -> Comment | None:
|
|
2431
|
+
row = await QUERIES.comment(connection, comment_id=comment_id, principal=actor.principal)
|
|
2432
|
+
return Comment(**dict(row)) if row else None
|
|
2433
|
+
|
|
2434
|
+
|
|
2435
|
+
async def add_comment(
|
|
2436
|
+
connection: aiosqlite.Connection, actor: Actor, task_id: int, body: str
|
|
2437
|
+
) -> Comment:
|
|
2438
|
+
if not body.strip():
|
|
2439
|
+
raise ValueError("a comment must not be empty")
|
|
2440
|
+
|
|
2441
|
+
task = await read_task(connection, actor, task_id)
|
|
2442
|
+
if task is None:
|
|
2443
|
+
raise LookupError(f"no task #{task_id}")
|
|
2444
|
+
await require(connection, actor, task.project_id, role="editor")
|
|
2445
|
+
now = time.time()
|
|
2446
|
+
inserted_id = await QUERIES.add_comment(
|
|
2447
|
+
connection,
|
|
2448
|
+
task_id=task_id,
|
|
2449
|
+
body=body,
|
|
2450
|
+
created_at=now,
|
|
2451
|
+
principal=actor.principal,
|
|
2452
|
+
computer=actor.computer or hostname(),
|
|
2453
|
+
)
|
|
2454
|
+
saved = await read_comment(connection, actor, inserted_id or 0)
|
|
2455
|
+
assert saved is not None
|
|
2456
|
+
return saved
|
|
2457
|
+
|
|
2458
|
+
|
|
2459
|
+
async def count_comments(connection: aiosqlite.Connection, actor: Actor, task_id: int) -> int:
|
|
2460
|
+
if await read_task(connection, actor, task_id) is None:
|
|
2461
|
+
return 0
|
|
2462
|
+
row = await QUERIES.count_comments(connection, task_id=task_id)
|
|
2463
|
+
return int(row[0]) if row else 0
|
|
2464
|
+
|
|
2465
|
+
|
|
2466
|
+
async def read_comments(
|
|
2467
|
+
connection: aiosqlite.Connection,
|
|
2468
|
+
actor: Actor,
|
|
2469
|
+
*,
|
|
2470
|
+
task_id: int | None = None,
|
|
2471
|
+
project_id: int | None = None,
|
|
2472
|
+
computer: str | None = None,
|
|
2473
|
+
status: Sequence[Status] | None = None,
|
|
2474
|
+
tag: str | None = None,
|
|
2475
|
+
query: str | None = None,
|
|
2476
|
+
raw: bool = False,
|
|
2477
|
+
limit: int = -1,
|
|
2478
|
+
) -> list[Comment]:
|
|
2479
|
+
parameters = filters(
|
|
2480
|
+
connection,
|
|
2481
|
+
actor,
|
|
2482
|
+
project_id=project_id,
|
|
2483
|
+
computer=computer,
|
|
2484
|
+
status=status,
|
|
2485
|
+
tag=tag,
|
|
2486
|
+
priority=None,
|
|
2487
|
+
)
|
|
2488
|
+
parameters.update(task_id=task_id, exclude_deleted=task_id is None and not status, limit=limit)
|
|
2489
|
+
if query is not None:
|
|
2490
|
+
match = query if raw else match_query(query)
|
|
2491
|
+
if not match:
|
|
2492
|
+
return []
|
|
2493
|
+
rows = QUERIES.search_comments(connection, match=match, **parameters)
|
|
2494
|
+
else:
|
|
2495
|
+
rows = QUERIES.comments(connection, **parameters)
|
|
2496
|
+
return [Comment(**dict(row)) async for row in rows]
|
|
2497
|
+
|
|
2498
|
+
|
|
2499
|
+
def filters(
|
|
2500
|
+
connection: aiosqlite.Connection,
|
|
2501
|
+
actor: Actor,
|
|
2502
|
+
*,
|
|
2503
|
+
project_id: int | None,
|
|
2504
|
+
status: Sequence[Status] | None,
|
|
2505
|
+
tag: str | None,
|
|
2506
|
+
priority: Priority | None,
|
|
2507
|
+
computer: str | None = None,
|
|
2508
|
+
) -> dict[str, Any]:
|
|
2509
|
+
return {
|
|
2510
|
+
"principal": actor.principal,
|
|
2511
|
+
"project_id": project_id,
|
|
2512
|
+
"computer": computer,
|
|
2513
|
+
"statuses": json.dumps([item.value for item in status]) if status else None,
|
|
2514
|
+
"tag": f"%,{tag.strip().lower()},%" if tag else None,
|
|
2515
|
+
"priority": RANKS[priority] if priority is not None else None,
|
|
2516
|
+
}
|
|
2517
|
+
|
|
2518
|
+
|
|
2519
|
+
async def read_tasks(
|
|
2520
|
+
connection: aiosqlite.Connection,
|
|
2521
|
+
actor: Actor,
|
|
2522
|
+
*,
|
|
2523
|
+
project_id: int | None = None,
|
|
2524
|
+
status: Sequence[Status] | None = None,
|
|
2525
|
+
tag: str | None = None,
|
|
2526
|
+
priority: Priority | None = None,
|
|
2527
|
+
computer: str | None = None,
|
|
2528
|
+
order: str = "backlog",
|
|
2529
|
+
limit: int = 50,
|
|
2530
|
+
offset: int = 0,
|
|
2531
|
+
) -> list[Task]:
|
|
2532
|
+
if order not in {"backlog", "recent"}:
|
|
2533
|
+
raise KeyError(order)
|
|
2534
|
+
parameters = filters(
|
|
2535
|
+
connection,
|
|
2536
|
+
actor,
|
|
2537
|
+
project_id=project_id,
|
|
2538
|
+
status=status,
|
|
2539
|
+
tag=tag,
|
|
2540
|
+
priority=priority,
|
|
2541
|
+
computer=computer,
|
|
2542
|
+
)
|
|
2543
|
+
rows = QUERIES.tasks(connection, **parameters, ordering=order, limit=limit, offset=offset)
|
|
2544
|
+
return [to_task(row) async for row in rows]
|
|
2545
|
+
|
|
2546
|
+
|
|
2547
|
+
async def count_tasks(
|
|
2548
|
+
connection: aiosqlite.Connection,
|
|
2549
|
+
actor: Actor,
|
|
2550
|
+
*,
|
|
2551
|
+
project_id: int | None = None,
|
|
2552
|
+
status: Sequence[Status] | None = None,
|
|
2553
|
+
tag: str | None = None,
|
|
2554
|
+
priority: Priority | None = None,
|
|
2555
|
+
computer: str | None = None,
|
|
2556
|
+
) -> int:
|
|
2557
|
+
parameters = filters(
|
|
2558
|
+
connection,
|
|
2559
|
+
actor,
|
|
2560
|
+
project_id=project_id,
|
|
2561
|
+
status=status,
|
|
2562
|
+
tag=tag,
|
|
2563
|
+
priority=priority,
|
|
2564
|
+
computer=computer,
|
|
2565
|
+
)
|
|
2566
|
+
row = await QUERIES.count_tasks(connection, **parameters)
|
|
2567
|
+
return int(row["found"]) if row else 0
|
|
2568
|
+
|
|
2569
|
+
|
|
2570
|
+
async def search_tasks(
|
|
2571
|
+
connection: aiosqlite.Connection,
|
|
2572
|
+
actor: Actor,
|
|
2573
|
+
query: str,
|
|
2574
|
+
*,
|
|
2575
|
+
project_id: int | None = None,
|
|
2576
|
+
status: Sequence[Status] | None = None,
|
|
2577
|
+
tag: str | None = None,
|
|
2578
|
+
computer: str | None = None,
|
|
2579
|
+
raw: bool = False,
|
|
2580
|
+
limit: int = 20,
|
|
2581
|
+
) -> list[Task]:
|
|
2582
|
+
match = query if raw else match_query(query)
|
|
2583
|
+
if not match:
|
|
2584
|
+
return []
|
|
2585
|
+
parameters = filters(
|
|
2586
|
+
connection,
|
|
2587
|
+
actor,
|
|
2588
|
+
project_id=project_id,
|
|
2589
|
+
status=status,
|
|
2590
|
+
tag=tag,
|
|
2591
|
+
priority=None,
|
|
2592
|
+
computer=computer,
|
|
2593
|
+
)
|
|
2594
|
+
rows = QUERIES.search_tasks(
|
|
2595
|
+
connection,
|
|
2596
|
+
**parameters,
|
|
2597
|
+
match=match,
|
|
2598
|
+
exclude_deleted=status is None,
|
|
2599
|
+
limit=limit,
|
|
2600
|
+
)
|
|
2601
|
+
return [to_task(row) async for row in rows]
|
|
2602
|
+
|
|
2603
|
+
|
|
2604
|
+
async def read_tags(
|
|
2605
|
+
connection: aiosqlite.Connection,
|
|
2606
|
+
actor: Actor,
|
|
2607
|
+
*,
|
|
2608
|
+
project_id: int | None = None,
|
|
2609
|
+
computer: str | None = None,
|
|
2610
|
+
) -> list[str]:
|
|
2611
|
+
parameters = filters(
|
|
2612
|
+
connection,
|
|
2613
|
+
actor,
|
|
2614
|
+
project_id=project_id,
|
|
2615
|
+
status=None,
|
|
2616
|
+
tag=None,
|
|
2617
|
+
priority=None,
|
|
2618
|
+
computer=computer,
|
|
2619
|
+
)
|
|
2620
|
+
rows = QUERIES.tags(connection, **parameters)
|
|
2621
|
+
found: set[str] = set()
|
|
2622
|
+
async for row in rows:
|
|
2623
|
+
found.update(split_tags(row["tags"]))
|
|
2624
|
+
return sorted(found)
|
|
2625
|
+
|
|
2626
|
+
|
|
2627
|
+
async def group(
|
|
2628
|
+
connection: aiosqlite.Connection,
|
|
2629
|
+
actor: Actor,
|
|
2630
|
+
column: str,
|
|
2631
|
+
*,
|
|
2632
|
+
project_id: int | None = None,
|
|
2633
|
+
computer: str | None = None,
|
|
2634
|
+
) -> dict[str, int]:
|
|
2635
|
+
if column not in {"status", "priority"}:
|
|
2636
|
+
raise ValueError("unsupported grouping column")
|
|
2637
|
+
parameters = filters(
|
|
2638
|
+
connection,
|
|
2639
|
+
actor,
|
|
2640
|
+
project_id=project_id,
|
|
2641
|
+
status=None,
|
|
2642
|
+
tag=None,
|
|
2643
|
+
priority=None,
|
|
2644
|
+
computer=computer,
|
|
2645
|
+
)
|
|
2646
|
+
rows = QUERIES.group_tasks(connection, column=column, **parameters)
|
|
2647
|
+
return {str(row["key"]): int(row["found"]) async for row in rows}
|
|
2648
|
+
|
|
2649
|
+
|
|
2650
|
+
async def read_note(connection: aiosqlite.Connection, actor: Actor, note_id: int) -> Note | None:
|
|
2651
|
+
row = await QUERIES.note(connection, note_id=note_id, principal=actor.principal)
|
|
2652
|
+
return to_note(row) if row else None
|
|
2653
|
+
|
|
2654
|
+
|
|
2655
|
+
async def note_by_title(
|
|
2656
|
+
connection: aiosqlite.Connection,
|
|
2657
|
+
actor: Actor,
|
|
2658
|
+
title: str,
|
|
2659
|
+
*,
|
|
2660
|
+
project_id: int,
|
|
2661
|
+
shared: bool | None = None,
|
|
2662
|
+
include_global: bool = False,
|
|
2663
|
+
) -> Note | None:
|
|
2664
|
+
row = await QUERIES.note_by_title(
|
|
2665
|
+
connection,
|
|
2666
|
+
project_id=project_id,
|
|
2667
|
+
title=title,
|
|
2668
|
+
principal=actor.principal,
|
|
2669
|
+
shared=shared,
|
|
2670
|
+
include_global=include_global,
|
|
2671
|
+
)
|
|
2672
|
+
return to_note(row) if row else None
|
|
2673
|
+
|
|
2674
|
+
|
|
2675
|
+
async def add_note(
|
|
2676
|
+
connection: aiosqlite.Connection,
|
|
2677
|
+
actor: Actor,
|
|
2678
|
+
*,
|
|
2679
|
+
project_id: int,
|
|
2680
|
+
title: str,
|
|
2681
|
+
body: str = "",
|
|
2682
|
+
tags: Iterable[str] = (),
|
|
2683
|
+
shared: bool = False,
|
|
2684
|
+
global_scope: bool = False,
|
|
2685
|
+
) -> Note:
|
|
2686
|
+
check_note(body)
|
|
2687
|
+
await require(connection, actor, project_id, role="editor")
|
|
2688
|
+
|
|
2689
|
+
now = time.time()
|
|
2690
|
+
inserted_id = await QUERIES.add_note(
|
|
2691
|
+
connection,
|
|
2692
|
+
project_id=project_id,
|
|
2693
|
+
title=title,
|
|
2694
|
+
body=body,
|
|
2695
|
+
tags=join_tags(tags),
|
|
2696
|
+
shared=int(shared),
|
|
2697
|
+
global_scope=int(global_scope),
|
|
2698
|
+
created_at=now,
|
|
2699
|
+
updated_at=now,
|
|
2700
|
+
created_principal=actor.principal,
|
|
2701
|
+
created_computer=actor.computer or hostname(),
|
|
2702
|
+
updated_principal=actor.principal,
|
|
2703
|
+
updated_computer=actor.computer or hostname(),
|
|
2704
|
+
)
|
|
2705
|
+
created = await read_note(connection, actor, inserted_id or 0)
|
|
2706
|
+
assert created is not None, "the note just written is not there"
|
|
2707
|
+
return created
|
|
2708
|
+
|
|
2709
|
+
|
|
2710
|
+
async def update_note(
|
|
2711
|
+
connection: aiosqlite.Connection,
|
|
2712
|
+
actor: Actor,
|
|
2713
|
+
note_id: int,
|
|
2714
|
+
*,
|
|
2715
|
+
title: str | None = None,
|
|
2716
|
+
body: str | None = None,
|
|
2717
|
+
tags: Iterable[str] | None = None,
|
|
2718
|
+
shared: bool | None = None,
|
|
2719
|
+
global_scope: bool | None = None,
|
|
2720
|
+
project_id: int | None = None,
|
|
2721
|
+
) -> Note | None:
|
|
2722
|
+
existing = await read_note(connection, actor, note_id)
|
|
2723
|
+
if existing is None:
|
|
2724
|
+
return None
|
|
2725
|
+
await require(connection, actor, existing.project_id, role="editor")
|
|
2726
|
+
if (
|
|
2727
|
+
shared is False
|
|
2728
|
+
and existing.shared
|
|
2729
|
+
and (existing.created_by is None or existing.created_by.principal != actor.principal)
|
|
2730
|
+
):
|
|
2731
|
+
raise PermissionError("only the author can make a shared memory entry personal")
|
|
2732
|
+
if project_id is not None:
|
|
2733
|
+
await require(connection, actor, project_id, role="editor")
|
|
2734
|
+
changes: dict[str, Any] = {}
|
|
2735
|
+
if title is not None:
|
|
2736
|
+
changes["title"] = title
|
|
2737
|
+
if body is not None:
|
|
2738
|
+
check_note(body)
|
|
2739
|
+
changes["body"] = body
|
|
2740
|
+
if tags is not None:
|
|
2741
|
+
changes["tags"] = join_tags(tags)
|
|
2742
|
+
if shared is not None:
|
|
2743
|
+
changes["shared"] = int(shared)
|
|
2744
|
+
if global_scope is not None:
|
|
2745
|
+
changes["global_scope"] = int(global_scope)
|
|
2746
|
+
if project_id is not None:
|
|
2747
|
+
changes["project_id"] = project_id
|
|
2748
|
+
if changes:
|
|
2749
|
+
changes["updated_principal"] = actor.principal
|
|
2750
|
+
changes["updated_computer"] = actor.computer or hostname()
|
|
2751
|
+
changes["updated_at"] = time.time()
|
|
2752
|
+
|
|
2753
|
+
await QUERIES.update_note(connection, note_id=note_id, changes=json.dumps(changes))
|
|
2754
|
+
return await read_note(connection, actor, note_id)
|
|
2755
|
+
|
|
2756
|
+
|
|
2757
|
+
async def append_note(
|
|
2758
|
+
connection: aiosqlite.Connection, actor: Actor, note_id: int, text: str
|
|
2759
|
+
) -> Note | None:
|
|
2760
|
+
"""Add a paragraph at the end of a note.
|
|
2761
|
+
|
|
2762
|
+
One statement writes the whole body, so the FTS triggers see the final text once. The
|
|
2763
|
+
result must fit `NOTE_LIMIT` like any other body; a note that would overflow is untouched.
|
|
2764
|
+
"""
|
|
2765
|
+
existing = await read_note(connection, actor, note_id)
|
|
2766
|
+
if existing is None:
|
|
2767
|
+
return None
|
|
2768
|
+
await require(connection, actor, existing.project_id, role="editor")
|
|
2769
|
+
check_note(f"{existing.body}\n\n{text}" if existing.body else text)
|
|
2770
|
+
|
|
2771
|
+
await QUERIES.append_note(
|
|
2772
|
+
connection,
|
|
2773
|
+
text=text,
|
|
2774
|
+
suffix=f"\n\n{text}",
|
|
2775
|
+
now=time.time(),
|
|
2776
|
+
principal=actor.principal,
|
|
2777
|
+
computer=actor.computer or hostname(),
|
|
2778
|
+
note_id=note_id,
|
|
2779
|
+
)
|
|
2780
|
+
return await read_note(connection, actor, note_id)
|
|
2781
|
+
|
|
2782
|
+
|
|
2783
|
+
async def delete_note(connection: aiosqlite.Connection, actor: Actor, note_id: int) -> bool:
|
|
2784
|
+
existing = await read_note(connection, actor, note_id)
|
|
2785
|
+
if existing is None:
|
|
2786
|
+
return False
|
|
2787
|
+
await require(connection, actor, existing.project_id, role="editor")
|
|
2788
|
+
|
|
2789
|
+
removed = await QUERIES.delete_note(connection, note_id=note_id)
|
|
2790
|
+
return bool(removed)
|
|
2791
|
+
|
|
2792
|
+
|
|
2793
|
+
def note_filters(
|
|
2794
|
+
connection: aiosqlite.Connection,
|
|
2795
|
+
actor: Actor,
|
|
2796
|
+
*,
|
|
2797
|
+
project_id: int | None,
|
|
2798
|
+
tag: str | None,
|
|
2799
|
+
shared: bool | None,
|
|
2800
|
+
computer: str | None = None,
|
|
2801
|
+
) -> dict[str, Any]:
|
|
2802
|
+
return {
|
|
2803
|
+
"principal": actor.principal,
|
|
2804
|
+
"project_id": project_id,
|
|
2805
|
+
"computer": computer,
|
|
2806
|
+
"shared": shared,
|
|
2807
|
+
"tag": f"%,{tag.strip().lower()},%" if tag else None,
|
|
2808
|
+
}
|
|
2809
|
+
|
|
2810
|
+
|
|
2811
|
+
async def read_notes(
|
|
2812
|
+
connection: aiosqlite.Connection,
|
|
2813
|
+
actor: Actor,
|
|
2814
|
+
*,
|
|
2815
|
+
project_id: int | None = None,
|
|
2816
|
+
tag: str | None = None,
|
|
2817
|
+
shared: bool | None = None,
|
|
2818
|
+
computer: str | None = None,
|
|
2819
|
+
limit: int = 50,
|
|
2820
|
+
offset: int = 0,
|
|
2821
|
+
) -> list[Note]:
|
|
2822
|
+
parameters = note_filters(
|
|
2823
|
+
connection, actor, project_id=project_id, tag=tag, shared=shared, computer=computer
|
|
2824
|
+
)
|
|
2825
|
+
rows = QUERIES.notes(connection, **parameters, limit=limit, offset=offset)
|
|
2826
|
+
return [to_note(row) async for row in rows]
|
|
2827
|
+
|
|
2828
|
+
|
|
2829
|
+
async def count_notes(
|
|
2830
|
+
connection: aiosqlite.Connection,
|
|
2831
|
+
actor: Actor,
|
|
2832
|
+
*,
|
|
2833
|
+
project_id: int | None = None,
|
|
2834
|
+
tag: str | None = None,
|
|
2835
|
+
shared: bool | None = None,
|
|
2836
|
+
computer: str | None = None,
|
|
2837
|
+
) -> int:
|
|
2838
|
+
parameters = note_filters(
|
|
2839
|
+
connection, actor, project_id=project_id, tag=tag, shared=shared, computer=computer
|
|
2840
|
+
)
|
|
2841
|
+
row = await QUERIES.count_notes(connection, **parameters)
|
|
2842
|
+
return int(row["found"]) if row else 0
|
|
2843
|
+
|
|
2844
|
+
|
|
2845
|
+
async def search_notes(
|
|
2846
|
+
connection: aiosqlite.Connection,
|
|
2847
|
+
actor: Actor,
|
|
2848
|
+
query: str,
|
|
2849
|
+
*,
|
|
2850
|
+
project_id: int | None = None,
|
|
2851
|
+
tag: str | None = None,
|
|
2852
|
+
shared: bool | None = None,
|
|
2853
|
+
computer: str | None = None,
|
|
2854
|
+
raw: bool = False,
|
|
2855
|
+
limit: int = 20,
|
|
2856
|
+
) -> list[Note]:
|
|
2857
|
+
match = query if raw else match_query(query)
|
|
2858
|
+
if not match:
|
|
2859
|
+
return []
|
|
2860
|
+
parameters = note_filters(
|
|
2861
|
+
connection, actor, project_id=project_id, tag=tag, shared=shared, computer=computer
|
|
2862
|
+
)
|
|
2863
|
+
rows = QUERIES.search_notes(connection, **parameters, match=match, limit=limit)
|
|
2864
|
+
return [to_note(row) async for row in rows]
|