loopctl-mcp-server 3.37.0 → 3.39.0
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.
- package/README.md +5 -3
- package/index.js +159 -17
- package/lib/checkpoints.js +38 -15
- package/lib/holder-journal.js +1015 -0
- package/lib/holder-replay.js +62 -0
- package/lib/work-items.js +43 -4
- package/package.json +1 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* US-52.61: `holder_replay`, POST /api/v1/holders/:id/replay.
|
|
3
|
+
*
|
|
4
|
+
* A session that lost loopctl kept its notes and checkpoints in a local journal; this sends
|
|
5
|
+
* them, in the order it wrote them, on the session's own holder credential. loopctl answers
|
|
6
|
+
* one result per entry: applied, replayed (already applied), fenced (recorded after the claim
|
|
7
|
+
* ended, kept as history) or refused.
|
|
8
|
+
*
|
|
9
|
+
* The holder id is the session's own and is never taken from the caller: loopctl refuses
|
|
10
|
+
* 403 holder_mismatch for a key whose holder is not the path's, so a holder id passed in could
|
|
11
|
+
* only ever fail. The bound on a body is loopctl's (422 replay_too_large); this module checks
|
|
12
|
+
* only what it can say without that number: a non-empty list in strictly ascending seq.
|
|
13
|
+
* Everything an entry carries is judged per entry by loopctl, so one bad entry is refused
|
|
14
|
+
* there without holding back the others.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { HOLDERS_PATH } from "./holder.js";
|
|
18
|
+
|
|
19
|
+
function refuse(body) {
|
|
20
|
+
return { error: true, status: 0, body };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @param {string} id */
|
|
24
|
+
export function replayPath(id) {
|
|
25
|
+
return `${HOLDERS_PATH}/${encodeURIComponent(id)}/replay`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {{entries?: unknown}} args
|
|
30
|
+
* @param {{apiCall: Function, holderId: string | null | undefined}} deps
|
|
31
|
+
*/
|
|
32
|
+
export async function replayHolder({ entries } = {}, { apiCall, holderId } = {}) {
|
|
33
|
+
if (typeof holderId !== "string" || holderId === "") {
|
|
34
|
+
return refuse(
|
|
35
|
+
"This session holds no live holder credential, so it has nothing to replay under. " +
|
|
36
|
+
"Call holder_register first; entries of an earlier holder of this session come back fenced.",
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!Array.isArray(entries) || entries.length === 0) {
|
|
41
|
+
return refuse("`entries` is a non-empty list of journal entries.");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let previous = null;
|
|
45
|
+
for (const [i, entry] of entries.entries()) {
|
|
46
|
+
if (entry === null || typeof entry !== "object" || Array.isArray(entry)) {
|
|
47
|
+
return refuse(`entries[${i}] is not an object.`);
|
|
48
|
+
}
|
|
49
|
+
if (!Number.isInteger(entry.seq)) {
|
|
50
|
+
return refuse(`entries[${i}].seq is not an integer.`);
|
|
51
|
+
}
|
|
52
|
+
if (previous !== null && entry.seq <= previous) {
|
|
53
|
+
return refuse(
|
|
54
|
+
`entries[${i}].seq (${entry.seq}) is not greater than the one before it (${previous}); ` +
|
|
55
|
+
"send the journal in the order it was written.",
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
previous = entry.seq;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return apiCall("POST", replayPath(holderId), { entries });
|
|
62
|
+
}
|
package/lib/work-items.js
CHANGED
|
@@ -27,10 +27,18 @@ export function notesPath(storyId) {
|
|
|
27
27
|
return `/api/v1/stories/${encodeURIComponent(storyId)}/notes`;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* `POST /api/v1/stories/:id/notes`: append one note.
|
|
32
|
+
*
|
|
33
|
+
* With `journal` (lib/holder-journal.js, US-52.62) the note carries a client UUIDv7
|
|
34
|
+
* `event_id` from its first attempt, and a note that gets no answer from loopctl is journaled
|
|
35
|
+
* and answered `{queued: true, event_id}`. Its journal entry needs the claim epoch it was
|
|
36
|
+
* written under: `claim_epoch` when given, else the one this session last saw for the story,
|
|
37
|
+
* else read from the story before the write (once per story; its answer is remembered).
|
|
38
|
+
*/
|
|
31
39
|
export async function writeWorkItemNote(
|
|
32
|
-
{ story_id, section, body, idempotency_key } = {},
|
|
33
|
-
{ apiCall } = {},
|
|
40
|
+
{ story_id, section, body, idempotency_key, claim_epoch } = {},
|
|
41
|
+
{ apiCall, journal = null } = {},
|
|
34
42
|
) {
|
|
35
43
|
const bad = uuid(story_id, "story_id");
|
|
36
44
|
if (bad) return bad;
|
|
@@ -47,12 +55,43 @@ export async function writeWorkItemNote(
|
|
|
47
55
|
return refuse("`idempotency_key` must be a string.");
|
|
48
56
|
}
|
|
49
57
|
|
|
58
|
+
if (claim_epoch !== undefined && claim_epoch !== null && !Number.isInteger(claim_epoch)) {
|
|
59
|
+
return refuse("`claim_epoch` must be an integer: the epoch the claim returned.");
|
|
60
|
+
}
|
|
61
|
+
|
|
50
62
|
const payload = { section, body };
|
|
51
63
|
if (typeof idempotency_key === "string" && idempotency_key !== "") {
|
|
52
64
|
payload.idempotency_key = idempotency_key;
|
|
53
65
|
}
|
|
54
66
|
|
|
55
|
-
return apiCall("POST", notesPath(story_id), payload);
|
|
67
|
+
if (!journal) return apiCall("POST", notesPath(story_id), payload);
|
|
68
|
+
|
|
69
|
+
const event_id = journal.newEventId();
|
|
70
|
+
payload.event_id = event_id;
|
|
71
|
+
const send = () => apiCall("POST", notesPath(story_id), payload);
|
|
72
|
+
|
|
73
|
+
if (!journal.active()) return send();
|
|
74
|
+
|
|
75
|
+
let epoch = Number.isInteger(claim_epoch) ? claim_epoch : journal.epochOf(story_id);
|
|
76
|
+
if (!Number.isInteger(epoch)) {
|
|
77
|
+
// One read per story: its answer is what the journal remembers. A read that gets no
|
|
78
|
+
// answer leaves the epoch unknown, and the write says so if it must be journaled.
|
|
79
|
+
const read = await apiCall("GET", `/api/v1/stories/${encodeURIComponent(story_id)}`, null);
|
|
80
|
+
const seen = read && !read.error && read.story && read.story.claim_epoch;
|
|
81
|
+
if (Number.isInteger(seen)) {
|
|
82
|
+
journal.rememberEpoch(story_id, seen);
|
|
83
|
+
epoch = seen;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return journal.write({
|
|
88
|
+
kind: "note",
|
|
89
|
+
story_id,
|
|
90
|
+
claim_epoch: Number.isInteger(epoch) ? epoch : null,
|
|
91
|
+
event_id,
|
|
92
|
+
payload: { section, body },
|
|
93
|
+
send,
|
|
94
|
+
});
|
|
56
95
|
}
|
|
57
96
|
|
|
58
97
|
/** `GET /api/v1/stories/:id/notes`: one page, oldest first, `current` on the first page. */
|