ticket-docket 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- docket/__init__.py +11 -0
- docket/cli/__init__.py +171 -0
- docket/cli/__main__.py +16 -0
- docket/cli/commands.py +428 -0
- docket/cli/grammar.py +376 -0
- docket/cli/output.py +134 -0
- docket/core/__init__.py +5 -0
- docket/core/atomic.py +62 -0
- docket/core/config.py +386 -0
- docket/core/deploy.py +288 -0
- docket/core/errors.py +122 -0
- docket/core/fields.py +166 -0
- docket/core/graph.py +418 -0
- docket/core/ids.py +215 -0
- docket/core/inputs.py +91 -0
- docket/core/lock.py +95 -0
- docket/core/mermaid.py +164 -0
- docket/core/store.py +534 -0
- docket/core/ticket.py +272 -0
- docket/core/validate.py +378 -0
- docket/py.typed +0 -0
- docket/server.py +330 -0
- docket/templates/CLAUDE.md +108 -0
- docket/templates/docket.toml +22 -0
- ticket_docket-1.0.0.dist-info/METADATA +226 -0
- ticket_docket-1.0.0.dist-info/RECORD +29 -0
- ticket_docket-1.0.0.dist-info/WHEEL +4 -0
- ticket_docket-1.0.0.dist-info/entry_points.txt +3 -0
- ticket_docket-1.0.0.dist-info/licenses/LICENSE +693 -0
docket/core/store.py
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Docket Store
|
|
3
|
+
|
|
4
|
+
Discovery, loading, writing, and mutation of tickets on disk.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
# MARK: Imports
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any, Iterator, Optional
|
|
12
|
+
|
|
13
|
+
from docket.core.atomic import writeTextAtomic
|
|
14
|
+
from docket.core.config import Config
|
|
15
|
+
from docket.core.errors import ConflictingArgumentsError, InvalidPriorityError, InvalidStatusError, TicketNotFoundError, TicketParseError
|
|
16
|
+
from docket.core.ids import buildFilename, nextId, parseId, requireValidKey
|
|
17
|
+
from docket.core.inputs import requireText
|
|
18
|
+
from docket.core.ticket import STATUS_DONE, STATUSES, Ticket, buildBody, parseTicket, serializeTicket
|
|
19
|
+
|
|
20
|
+
# MARK: Constants
|
|
21
|
+
|
|
22
|
+
# Only markdown files are considered tickets, so a stray README or image under a status directory is ignored rather than reported.
|
|
23
|
+
TICKET_GLOB: str = "*.md"
|
|
24
|
+
|
|
25
|
+
# MARK: Classes
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class LoadFailure:
|
|
30
|
+
"""
|
|
31
|
+
A file under a status directory that could not be read as a ticket.
|
|
32
|
+
|
|
33
|
+
These are collected rather than raised, so one broken file does not prevent every other ticket from loading and so `validate` can report it as a finding.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
# MARK: Properties
|
|
37
|
+
|
|
38
|
+
path: Path
|
|
39
|
+
message: str
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True)
|
|
43
|
+
class DuplicateId:
|
|
44
|
+
"""
|
|
45
|
+
A file carrying an id another file already claimed.
|
|
46
|
+
|
|
47
|
+
Two branches minting under the same key produce this, which is the accepted cost of deriving numbers by scanning rather than from a counter file.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
# MARK: Properties
|
|
51
|
+
|
|
52
|
+
id: str
|
|
53
|
+
path: Path
|
|
54
|
+
existingPath: Path
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass
|
|
58
|
+
class TicketResult:
|
|
59
|
+
"""
|
|
60
|
+
A written ticket together with any non-fatal warnings raised while writing it.
|
|
61
|
+
|
|
62
|
+
A dangling `requires` entry is a warning rather than an error at write time, so an agent writing a batch out of order is never stranded mid-batch.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
# MARK: Properties
|
|
66
|
+
|
|
67
|
+
ticket: Ticket
|
|
68
|
+
warnings: list[str] = field(default_factory=list)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass
|
|
72
|
+
class TicketSet:
|
|
73
|
+
"""
|
|
74
|
+
Every ticket found under the configured root, plus whatever could not be read.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
# MARK: Properties
|
|
78
|
+
|
|
79
|
+
tickets: dict[str, Ticket] = field(default_factory=dict)
|
|
80
|
+
failures: list[LoadFailure] = field(default_factory=list)
|
|
81
|
+
duplicates: list[DuplicateId] = field(default_factory=list)
|
|
82
|
+
|
|
83
|
+
# MARK: Python Functions
|
|
84
|
+
|
|
85
|
+
def __contains__(self, ticketId: str) -> bool:
|
|
86
|
+
"""
|
|
87
|
+
Report whether an id is present.
|
|
88
|
+
|
|
89
|
+
ticketId: The id to test.
|
|
90
|
+
|
|
91
|
+
Returns `True` when a ticket carries the id.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
return ticketId in self.tickets
|
|
95
|
+
|
|
96
|
+
def __iter__(self) -> Iterator[Ticket]:
|
|
97
|
+
"""
|
|
98
|
+
Iterate every loaded ticket in sorted order.
|
|
99
|
+
|
|
100
|
+
Returns an iterator over tickets.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
return iter(self.sorted())
|
|
104
|
+
|
|
105
|
+
def __len__(self) -> int:
|
|
106
|
+
"""
|
|
107
|
+
Count the loaded tickets.
|
|
108
|
+
|
|
109
|
+
Returns the ticket count.
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
return len(self.tickets)
|
|
113
|
+
|
|
114
|
+
# MARK: Functions
|
|
115
|
+
|
|
116
|
+
def get(self, ticketId: str) -> Ticket:
|
|
117
|
+
"""
|
|
118
|
+
Fetch one ticket by id.
|
|
119
|
+
|
|
120
|
+
ticketId: The id to look up.
|
|
121
|
+
|
|
122
|
+
Returns the ticket.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
if ticketId not in self.tickets:
|
|
126
|
+
raise TicketNotFoundError(f"No ticket with id '{ticketId}'.")
|
|
127
|
+
|
|
128
|
+
return self.tickets[ticketId]
|
|
129
|
+
|
|
130
|
+
def ids(self) -> list[str]:
|
|
131
|
+
"""
|
|
132
|
+
List every loaded id.
|
|
133
|
+
|
|
134
|
+
Returns the ids, unsorted.
|
|
135
|
+
"""
|
|
136
|
+
|
|
137
|
+
return list(self.tickets)
|
|
138
|
+
|
|
139
|
+
def sorted(self) -> list[Ticket]:
|
|
140
|
+
"""
|
|
141
|
+
Order tickets by priority, then by key, then by number.
|
|
142
|
+
|
|
143
|
+
The number is compared numerically rather than as text, so `CORE-2` precedes `CORE-10`.
|
|
144
|
+
|
|
145
|
+
Returns the ordered tickets.
|
|
146
|
+
"""
|
|
147
|
+
|
|
148
|
+
return sorted(self.tickets.values(), key=_sortKey)
|
|
149
|
+
|
|
150
|
+
def filtered(self, status: Optional[str] = None, key: Optional[str] = None, priorityMax: Optional[int] = None) -> list[Ticket]:
|
|
151
|
+
"""
|
|
152
|
+
Select tickets matching every supplied filter, in sorted order.
|
|
153
|
+
|
|
154
|
+
status: Keep only tickets with this status.
|
|
155
|
+
key: Keep only tickets carrying this key.
|
|
156
|
+
priorityMax: Keep only tickets at or below this priority number, meaning at or above this urgency.
|
|
157
|
+
|
|
158
|
+
Returns the matching tickets.
|
|
159
|
+
"""
|
|
160
|
+
|
|
161
|
+
matches: list[Ticket] = self.sorted()
|
|
162
|
+
|
|
163
|
+
# Apply each filter only when it was supplied, so an omitted filter never narrows the result.
|
|
164
|
+
if status is not None:
|
|
165
|
+
matches = [ticket for ticket in matches if ticket.status == status]
|
|
166
|
+
if key is not None:
|
|
167
|
+
matches = [ticket for ticket in matches if ticket.key == key]
|
|
168
|
+
if priorityMax is not None:
|
|
169
|
+
matches = [ticket for ticket in matches if ticket.priority <= priorityMax]
|
|
170
|
+
|
|
171
|
+
return matches
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class Store:
|
|
175
|
+
"""
|
|
176
|
+
Reads and writes tickets under one configuration's ticket root.
|
|
177
|
+
"""
|
|
178
|
+
|
|
179
|
+
# MARK: Initializer
|
|
180
|
+
|
|
181
|
+
def __init__(self, config: Config) -> None:
|
|
182
|
+
"""
|
|
183
|
+
Bind a store to a configuration.
|
|
184
|
+
|
|
185
|
+
config: The configuration naming the ticket root and the status directories.
|
|
186
|
+
"""
|
|
187
|
+
|
|
188
|
+
self.config: Config = config
|
|
189
|
+
|
|
190
|
+
# MARK: Functions
|
|
191
|
+
|
|
192
|
+
def directoryFor(self, status: str) -> Path:
|
|
193
|
+
"""
|
|
194
|
+
Resolve the directory a status belongs in.
|
|
195
|
+
|
|
196
|
+
`done` is the only status that moves a file, so everything else shares the todo directory. That rule is what keeps the vocabulary fixed rather than configurable.
|
|
197
|
+
|
|
198
|
+
status: The status to resolve.
|
|
199
|
+
|
|
200
|
+
Returns the absolute directory path.
|
|
201
|
+
"""
|
|
202
|
+
|
|
203
|
+
return self.config.donePath if status == STATUS_DONE else self.config.todoPath
|
|
204
|
+
|
|
205
|
+
def pathFor(self, ticket: Ticket) -> Path:
|
|
206
|
+
"""
|
|
207
|
+
Resolve where a ticket's file belongs.
|
|
208
|
+
|
|
209
|
+
Filenames are frozen at creation, so an already-written ticket keeps its existing filename even after a retitle. Only the directory follows the status.
|
|
210
|
+
|
|
211
|
+
ticket: The ticket to place.
|
|
212
|
+
|
|
213
|
+
Returns the absolute file path.
|
|
214
|
+
"""
|
|
215
|
+
|
|
216
|
+
# Reuse the existing filename when there is one, since retitling must not rename the file.
|
|
217
|
+
filename: str = ticket.path.name if ticket.path is not None else buildFilename(ticket.id, ticket.title)
|
|
218
|
+
|
|
219
|
+
return self.directoryFor(ticket.status) / filename
|
|
220
|
+
|
|
221
|
+
def discoverPaths(self) -> list[Path]:
|
|
222
|
+
"""
|
|
223
|
+
List every candidate ticket file under both status directories.
|
|
224
|
+
|
|
225
|
+
Returns the paths, sorted so results are deterministic across platforms.
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
paths: list[Path] = []
|
|
229
|
+
|
|
230
|
+
# A directory that does not exist yet is not an error, since a freshly deployed repository has no tickets.
|
|
231
|
+
for directory in (self.config.todoPath, self.config.donePath):
|
|
232
|
+
if directory.is_dir():
|
|
233
|
+
paths.extend(sorted(directory.glob(TICKET_GLOB)))
|
|
234
|
+
|
|
235
|
+
return paths
|
|
236
|
+
|
|
237
|
+
def loadAll(self) -> TicketSet:
|
|
238
|
+
"""
|
|
239
|
+
Load every ticket under the configured root, holding the repository's read lock while doing so.
|
|
240
|
+
|
|
241
|
+
Returns the loaded `TicketSet`.
|
|
242
|
+
"""
|
|
243
|
+
|
|
244
|
+
# A caller already inside a write lock must not come through here, since the lock refuses to downgrade from writing to reading. Those callers use the unlocked form directly.
|
|
245
|
+
with self.config.sharedLock():
|
|
246
|
+
return self.__loadAllUnlocked()
|
|
247
|
+
|
|
248
|
+
def load(self, ticketId: str) -> Ticket:
|
|
249
|
+
"""
|
|
250
|
+
Load one ticket by id.
|
|
251
|
+
|
|
252
|
+
ticketId: The id to look up.
|
|
253
|
+
|
|
254
|
+
Returns the ticket.
|
|
255
|
+
"""
|
|
256
|
+
|
|
257
|
+
return self.loadAll().get(ticketId)
|
|
258
|
+
|
|
259
|
+
def write(self, ticket: Ticket) -> Ticket:
|
|
260
|
+
"""
|
|
261
|
+
Write a ticket to the path its status and filename imply.
|
|
262
|
+
|
|
263
|
+
This does not move an existing file. `setStatus` owns that, so no caller can change a status without the move happening in the same operation.
|
|
264
|
+
|
|
265
|
+
ticket: The ticket to write.
|
|
266
|
+
|
|
267
|
+
Returns the ticket with its path recorded.
|
|
268
|
+
"""
|
|
269
|
+
|
|
270
|
+
path: Path = self.pathFor(ticket)
|
|
271
|
+
|
|
272
|
+
# A freshly deployed repository may not have the status directories yet.
|
|
273
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
274
|
+
|
|
275
|
+
# Replace the file in one step, so a concurrent reader sees either the old ticket or the new one and never a truncated file.
|
|
276
|
+
writeTextAtomic(path, serializeTicket(ticket))
|
|
277
|
+
ticket.path = path
|
|
278
|
+
|
|
279
|
+
return ticket
|
|
280
|
+
|
|
281
|
+
def create(
|
|
282
|
+
self,
|
|
283
|
+
key: str,
|
|
284
|
+
title: str,
|
|
285
|
+
body: Optional[str] = None,
|
|
286
|
+
requires: Optional[list[str]] = None,
|
|
287
|
+
priority: Optional[int] = None,
|
|
288
|
+
) -> TicketResult:
|
|
289
|
+
"""
|
|
290
|
+
Allocate an id and write a new ticket.
|
|
291
|
+
|
|
292
|
+
The id is derived by scanning what already exists, so the scan and the write are held together under one lock. Without that, two processes minting under one key read the same set and allocate the same number.
|
|
293
|
+
|
|
294
|
+
key: The key to mint under, which must be registered.
|
|
295
|
+
title: The ticket title, which the filename slug derives from once, here.
|
|
296
|
+
body: Prose for the body, placed under a heading built from the title.
|
|
297
|
+
requires: Ids this ticket depends on.
|
|
298
|
+
priority: The priority, defaulting to the configuration's `defaultPriority`.
|
|
299
|
+
|
|
300
|
+
Returns the written ticket and any warnings.
|
|
301
|
+
"""
|
|
302
|
+
|
|
303
|
+
# The filename slug derives from the title once, here, so an empty one is frozen into the filename as well as the field.
|
|
304
|
+
requireText(title, "title")
|
|
305
|
+
|
|
306
|
+
# A key must be registered before anything is minted under it, and the error names `add_key` as the way out.
|
|
307
|
+
requireValidKey(key)
|
|
308
|
+
self.config.requireKnownKey(key)
|
|
309
|
+
|
|
310
|
+
with self.config.exclusiveLock():
|
|
311
|
+
existing: TicketSet = self.__loadAllUnlocked()
|
|
312
|
+
|
|
313
|
+
resolvedPriority: int = priority if priority is not None else self.config.defaultPriority
|
|
314
|
+
self.__requireValidPriority(resolvedPriority)
|
|
315
|
+
|
|
316
|
+
ticket: Ticket = Ticket(
|
|
317
|
+
id=nextId(key, existing.ids()),
|
|
318
|
+
title=title,
|
|
319
|
+
status=STATUSES[0],
|
|
320
|
+
priority=resolvedPriority,
|
|
321
|
+
requires=list(requires or []),
|
|
322
|
+
body=buildBody(title, body),
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
return TicketResult(ticket=self.write(ticket), warnings=self.__danglingWarnings(ticket, existing))
|
|
326
|
+
|
|
327
|
+
def update(
|
|
328
|
+
self,
|
|
329
|
+
ticketId: str,
|
|
330
|
+
title: Optional[str] = None,
|
|
331
|
+
priority: Optional[int] = None,
|
|
332
|
+
requires: Optional[list[str]] = None,
|
|
333
|
+
requiresAdd: Optional[list[str]] = None,
|
|
334
|
+
requiresRemove: Optional[list[str]] = None,
|
|
335
|
+
) -> TicketResult:
|
|
336
|
+
"""
|
|
337
|
+
Change a ticket's title, priority, or dependencies in place.
|
|
338
|
+
|
|
339
|
+
Status is deliberately excluded, because changing it moves the file and that belongs to `setStatus`. The filename is excluded because it is frozen at creation, so a retitle leaves every prose cross-reference intact.
|
|
340
|
+
|
|
341
|
+
The dependency list is edited either wholesale or in place, never both in one call. `requires` replaces it. `requiresAdd` and `requiresRemove` edit whatever is already there, which is what spares a caller from retyping a long list to change one entry.
|
|
342
|
+
|
|
343
|
+
The load and the write back are held together under one lock, since a second process changing a different field in the gap would have its change reverted by this write.
|
|
344
|
+
|
|
345
|
+
ticketId: The ticket to change.
|
|
346
|
+
title: A new title, if any.
|
|
347
|
+
priority: A new priority, if any.
|
|
348
|
+
requires: A replacement dependency list, if any.
|
|
349
|
+
requiresAdd: Ids to append to the existing list, if any.
|
|
350
|
+
requiresRemove: Ids to drop from the existing list, if any.
|
|
351
|
+
|
|
352
|
+
Returns the written ticket and any warnings.
|
|
353
|
+
"""
|
|
354
|
+
|
|
355
|
+
# Replacing the list and editing it in place at once names no order the caller actually asked for.
|
|
356
|
+
if requires is not None and (requiresAdd is not None or requiresRemove is not None):
|
|
357
|
+
raise ConflictingArgumentsError("A replacement dependency list cannot be combined with adding to or removing from the existing one. Pass either the replacement or the edits.")
|
|
358
|
+
|
|
359
|
+
with self.config.exclusiveLock():
|
|
360
|
+
existing: TicketSet = self.__loadAllUnlocked()
|
|
361
|
+
ticket: Ticket = existing.get(ticketId)
|
|
362
|
+
|
|
363
|
+
if title is not None:
|
|
364
|
+
ticket.title = requireText(title, "title")
|
|
365
|
+
|
|
366
|
+
if priority is not None:
|
|
367
|
+
self.__requireValidPriority(priority)
|
|
368
|
+
ticket.priority = priority
|
|
369
|
+
|
|
370
|
+
if requires is not None:
|
|
371
|
+
ticket.requires = list(requires)
|
|
372
|
+
|
|
373
|
+
# Removal runs first, so passing the same id to both ends with it present rather than gone.
|
|
374
|
+
if requiresRemove is not None:
|
|
375
|
+
dropped: set[str] = set(requiresRemove)
|
|
376
|
+
ticket.requires = [required for required in ticket.requires if required not in dropped]
|
|
377
|
+
|
|
378
|
+
# New ids append in the order given, and one already present is left where it is rather than duplicated.
|
|
379
|
+
if requiresAdd is not None:
|
|
380
|
+
for required in requiresAdd:
|
|
381
|
+
if required not in ticket.requires:
|
|
382
|
+
ticket.requires.append(required)
|
|
383
|
+
|
|
384
|
+
return TicketResult(ticket=self.write(ticket), warnings=self.__danglingWarnings(ticket, existing))
|
|
385
|
+
|
|
386
|
+
def setMetadata(self, ticketId: str, key: str, value: Optional[Any]) -> TicketResult:
|
|
387
|
+
"""
|
|
388
|
+
Set or clear one entry in a ticket's `metadata` map.
|
|
389
|
+
|
|
390
|
+
Only the named entry is touched, so one consumer's key survives another consumer writing its own. `value` of `None` removes the entry rather than storing a null, keeping the map free of placeholders nothing reads.
|
|
391
|
+
|
|
392
|
+
The whole file is rewritten to change the one entry, so the load and the write back are held together under one lock. Two consumers namespacing their keys correctly would still lose one of the two without it.
|
|
393
|
+
|
|
394
|
+
ticketId: The ticket to change.
|
|
395
|
+
key: The metadata key, which cannot be empty.
|
|
396
|
+
value: The value to store, or `None` to remove the key.
|
|
397
|
+
|
|
398
|
+
Returns the written ticket and any warnings.
|
|
399
|
+
"""
|
|
400
|
+
|
|
401
|
+
requireText(key, "metadata key")
|
|
402
|
+
|
|
403
|
+
with self.config.exclusiveLock():
|
|
404
|
+
existing: TicketSet = self.__loadAllUnlocked()
|
|
405
|
+
ticket: Ticket = existing.get(ticketId)
|
|
406
|
+
|
|
407
|
+
if value is None:
|
|
408
|
+
ticket.metadata.pop(key, None)
|
|
409
|
+
else:
|
|
410
|
+
ticket.metadata[key] = value
|
|
411
|
+
|
|
412
|
+
return TicketResult(ticket=self.write(ticket), warnings=self.__danglingWarnings(ticket, existing))
|
|
413
|
+
|
|
414
|
+
def setStatus(self, ticketId: str, status: str) -> Ticket:
|
|
415
|
+
"""
|
|
416
|
+
Change a ticket's status and move its file in the same operation.
|
|
417
|
+
|
|
418
|
+
The frontmatter is the truth and the directory is a projection of it, so neither is ever written without the other.
|
|
419
|
+
|
|
420
|
+
The move is a write followed by a delete, which is two steps no matter how each one is performed, so the lock is what stops a reader from seeing both files at once and reporting a duplicate id.
|
|
421
|
+
|
|
422
|
+
ticketId: The ticket to change.
|
|
423
|
+
status: The new status.
|
|
424
|
+
|
|
425
|
+
Returns the updated ticket.
|
|
426
|
+
"""
|
|
427
|
+
|
|
428
|
+
# Reject an unrecognized status at the boundary rather than writing it and leaving `validate` to find it later.
|
|
429
|
+
if status not in STATUSES:
|
|
430
|
+
raise InvalidStatusError(f"Status '{status}' is not one of {', '.join(STATUSES)}.")
|
|
431
|
+
|
|
432
|
+
with self.config.exclusiveLock():
|
|
433
|
+
ticket: Ticket = self.__loadAllUnlocked().get(ticketId)
|
|
434
|
+
previousPath: Optional[Path] = ticket.path
|
|
435
|
+
|
|
436
|
+
ticket.status = status
|
|
437
|
+
self.write(ticket)
|
|
438
|
+
|
|
439
|
+
# Remove the old file only once the new one is on disk, and only when the move actually crossed directories.
|
|
440
|
+
if previousPath is not None and previousPath != ticket.path and previousPath.exists():
|
|
441
|
+
previousPath.unlink()
|
|
442
|
+
|
|
443
|
+
return ticket
|
|
444
|
+
|
|
445
|
+
def usedKeys(self) -> dict[str, list[str]]:
|
|
446
|
+
"""
|
|
447
|
+
Map every key currently in use to the ids carrying it.
|
|
448
|
+
|
|
449
|
+
This is what `key reject` needs in order to refuse loudly and name the tickets standing in the way.
|
|
450
|
+
|
|
451
|
+
Returns keys mapped to their ticket ids.
|
|
452
|
+
"""
|
|
453
|
+
|
|
454
|
+
used: dict[str, list[str]] = {}
|
|
455
|
+
for ticket in self.loadAll().tickets.values():
|
|
456
|
+
used.setdefault(ticket.key, []).append(ticket.id)
|
|
457
|
+
|
|
458
|
+
return used
|
|
459
|
+
|
|
460
|
+
# MARK: Private Functions
|
|
461
|
+
|
|
462
|
+
def __loadAllUnlocked(self) -> TicketSet:
|
|
463
|
+
"""
|
|
464
|
+
Load every ticket under the configured root without taking any lock.
|
|
465
|
+
|
|
466
|
+
A file that cannot be read is collected rather than raised, so one broken ticket does not hide the rest and `validate` can report it.
|
|
467
|
+
|
|
468
|
+
This exists because the lock refuses to downgrade from writing to reading, so a mutator already holding the write lock cannot call `loadAll`. Every caller of this is either inside a write lock or is `loadAll` itself.
|
|
469
|
+
|
|
470
|
+
Returns the loaded `TicketSet`.
|
|
471
|
+
"""
|
|
472
|
+
|
|
473
|
+
result: TicketSet = TicketSet()
|
|
474
|
+
|
|
475
|
+
for path in self.discoverPaths():
|
|
476
|
+
try:
|
|
477
|
+
ticket: Ticket = parseTicket(path.read_text(encoding="utf-8"), path=path)
|
|
478
|
+
except (TicketParseError, OSError, UnicodeDecodeError) as error:
|
|
479
|
+
result.failures.append(LoadFailure(path=path, message=str(error)))
|
|
480
|
+
continue
|
|
481
|
+
|
|
482
|
+
# Keep the first file claiming an id and record the rest, so a merge collision is reported instead of silently overwriting.
|
|
483
|
+
existing: Optional[Ticket] = result.tickets.get(ticket.id)
|
|
484
|
+
if existing is not None:
|
|
485
|
+
result.duplicates.append(DuplicateId(id=ticket.id, path=path, existingPath=existing.path or path))
|
|
486
|
+
continue
|
|
487
|
+
|
|
488
|
+
result.tickets[ticket.id] = ticket
|
|
489
|
+
|
|
490
|
+
return result
|
|
491
|
+
|
|
492
|
+
def __requireValidPriority(self, priority: int) -> None:
|
|
493
|
+
"""
|
|
494
|
+
Reject a priority outside the configured band.
|
|
495
|
+
|
|
496
|
+
priority: The priority to check.
|
|
497
|
+
"""
|
|
498
|
+
|
|
499
|
+
if not 0 <= priority <= self.config.maxPriority:
|
|
500
|
+
raise InvalidPriorityError(f"Priority {priority} is outside 0 through {self.config.maxPriority}. 0 is most urgent.")
|
|
501
|
+
|
|
502
|
+
def __danglingWarnings(self, ticket: Ticket, existing: TicketSet) -> list[str]:
|
|
503
|
+
"""
|
|
504
|
+
Report `requires` entries naming ids that do not exist.
|
|
505
|
+
|
|
506
|
+
This is a warning rather than an error, so an agent writing a batch out of order completes the batch. `validate` reports the same condition as an error once the batch is done.
|
|
507
|
+
|
|
508
|
+
ticket: The ticket whose dependencies are being checked.
|
|
509
|
+
existing: The set loaded before the write.
|
|
510
|
+
|
|
511
|
+
Returns one warning per unknown id.
|
|
512
|
+
"""
|
|
513
|
+
|
|
514
|
+
# The ticket may legitimately require something written earlier in this same batch, so check against the set loaded before the write.
|
|
515
|
+
return [f"Ticket '{ticket.id}' requires '{required}', which does not exist yet. Run 'docket validate' once the batch is complete." for required in ticket.requires if required != ticket.id and required not in existing]
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
# MARK: Functions
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
def _sortKey(ticket: Ticket) -> tuple[int, str, int]:
|
|
522
|
+
"""
|
|
523
|
+
Build the ordering key for a ticket.
|
|
524
|
+
|
|
525
|
+
The id's number is compared numerically rather than as text, so `CORE-2` precedes `CORE-10` instead of following it.
|
|
526
|
+
|
|
527
|
+
ticket: The ticket to order.
|
|
528
|
+
|
|
529
|
+
Returns a `(priority, key, number)` tuple.
|
|
530
|
+
"""
|
|
531
|
+
|
|
532
|
+
key, number = parseId(ticket.id)
|
|
533
|
+
|
|
534
|
+
return ticket.priority, key, number
|