@ftrdotdev/pi-tracker 0.2.1 → 0.3.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 CHANGED
@@ -32,7 +32,7 @@ supports these actions:
32
32
  | `delete_list` | Delete a list | `list_id` |
33
33
  | `set_active` | Set or clear the active list | `list_id` (optional) |
34
34
  | `add_item` | Add one or more items | `list_id`, `text` (string or array of strings) |
35
- | `update_item` | Update one or more items | `item_id`/`text?`/`done?` or `items` |
35
+ | `update_item` | Update one or more items | `item_id`/`text?`/`done?` or `list_id` + `items` |
36
36
  | `remove_item` | Remove an item | `item_id` |
37
37
 
38
38
  `create_list` accepts `initial_items` (an array of item texts) to create the
@@ -46,19 +46,21 @@ items: an id is just the list name plus the array position, so ids are
46
46
  unique across lists because list names are unique, and ids shift when items
47
47
  are removed (`Work:3` becomes `Work:2`) — re-list before referencing items
48
48
  after a removal. `update_item` accepts the scalar form (`item_id` with
49
- optional `text`/`done`) or a batched `items` array
50
- (`[{item_id, text?, done?}, ...]`); batches may span lists, since each
51
- `item_id` names its own list. Creating a list makes it the active list (the
52
- widget switches to it); pass `activate: false` to keep the current active
53
- list.
49
+ optional `text`/`done`) or a per-list batched form: `list_id` plus an
50
+ `items` array (`[{index, text?, done?}, ...]`) where `index` is the item's
51
+ 1-based position in that list — mirroring `add_item`'s `list_id + text[]`
52
+ shape, so one batch stays within a single list. Creating a list makes it the
53
+ active list (the widget switches to it); pass `activate: false` to keep the
54
+ current active list.
54
55
 
55
56
  The tool validates every call and returns an error that names exactly what to
56
57
  fix: each action accepts only its own parameters, required fields are
57
58
  enforced, and the two `update_item` forms never mix. Read the error and retry
58
59
  with corrected parameters — not-found errors also list the available ids.
59
60
  `update_item` also appends a reminder when one call marks two or more items
60
- done at once: the working rhythm is to mark each item done in the same turn
61
- it completes, never batch the marking at the end.
61
+ done and leaves no open items behind (the terminal batch): the working
62
+ rhythm is to mark each item done in the same turn it completes, never batch
63
+ the marking at the end.
62
64
 
63
65
  Example prompt:
64
66
 
package/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default } from "./src";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ftrdotdev/pi-tracker",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Session-persistent todolist extension for Pi",
5
5
  "keywords": [
6
6
  "effect",
@@ -25,6 +25,7 @@
25
25
  "directory": "packages/tracker"
26
26
  },
27
27
  "files": [
28
+ "index.ts",
28
29
  "src",
29
30
  "README.md",
30
31
  "LICENSE"
@@ -44,7 +45,7 @@
44
45
  },
45
46
  "dependencies": {
46
47
  "@effect/platform-node": "4.0.0-rc.108",
47
- "@ftrdotdev/pi-tui": "^0.2.1",
48
+ "@ftrdotdev/pi-tui": "^0.3.0",
48
49
  "effect": "4.0.0-rc.108"
49
50
  },
50
51
  "devDependencies": {
@@ -65,7 +66,7 @@
65
66
  },
66
67
  "pi": {
67
68
  "extensions": [
68
- "./src/index.ts"
69
+ "./index.ts"
69
70
  ]
70
71
  }
71
72
  }
package/src/index.ts CHANGED
@@ -80,12 +80,12 @@ export default function (pi: ExtensionAPI): void {
80
80
  }
81
81
  case "update_item": {
82
82
  if (params.items !== undefined) {
83
- // Wire format is snake_case (item_id); the store uses itemId.
84
- const batch = params.items.map(({ item_id, ...patch }) => ({
85
- itemId: item_id,
86
- ...patch,
87
- }));
88
- return withStore((store) => store.updateItems(batch));
83
+ // Batch form: list_id + index-based patches within one list (mirrors
84
+ // add_item's list_id + text[]). The store resolves each index against
85
+ // that single list.
86
+ const listId = requireParam(params.list_id, "list_id");
87
+ const batch = params.items;
88
+ return withStore((store) => store.updateItems(listId, batch));
89
89
  }
90
90
  const patch: UpdateItemPatch = {
91
91
  ...(params.text !== undefined && !Array.isArray(params.text)
@@ -94,7 +94,7 @@ export default function (pi: ExtensionAPI): void {
94
94
  ...(params.done !== undefined ? { done: params.done } : {}),
95
95
  };
96
96
  return withStore((store) =>
97
- store.updateItems([{ itemId: requireParam(params.item_id, "item_id"), ...patch }]),
97
+ store.updateItem(requireParam(params.item_id, "item_id"), patch),
98
98
  );
99
99
  }
100
100
  case "remove_item":
@@ -279,22 +279,43 @@ export default function (pi: ExtensionAPI): void {
279
279
  case "update_item": {
280
280
  const items = value as TodoItem[];
281
281
  details.items = items;
282
- const patches =
283
- params.items !== undefined
284
- ? params.items
285
- : [{ item_id: params.item_id, text: params.text, done: params.done }];
282
+ // Normalize both forms into patch records the renderer and reminder
283
+ // share: each has a display id plus optional text/done.
284
+ const batch = params.items;
285
+ const list =
286
+ batch !== undefined ? current.lists.find((l) => l.id === params.list_id) : undefined;
287
+ const listName = list ? list.name : `#${params.list_id}`;
288
+ const patches: Array<{ id: string; text?: string; done?: boolean }> =
289
+ batch !== undefined
290
+ ? batch.map((p) => ({ id: `${listName}:${p.index}`, text: p.text, done: p.done }))
291
+ : [
292
+ {
293
+ id: params.item_id ?? "?",
294
+ text:
295
+ params.text !== undefined && !Array.isArray(params.text)
296
+ ? params.text
297
+ : undefined,
298
+ done: params.done,
299
+ },
300
+ ];
286
301
  const parts = items.map((item, i) => {
287
302
  const patch = patches[i]!;
288
303
  const changes: string[] = [];
289
304
  if (patch.done !== undefined) changes.push(item.done ? "completed" : "uncompleted");
290
305
  if (patch.text !== undefined) changes.push(`text: ${item.text}`);
291
- return `${patch.item_id}${changes.length === 0 ? " (no change)" : ` (${changes.join(", ")})`}`;
306
+ return `${patch.id}${changes.length === 0 ? " (no change)" : ` (${changes.join(", ")})`}`;
292
307
  });
293
308
  text = `Updated ${items.length} item${items.length === 1 ? "" : "s"}: ${parts.join(", ")}`;
294
- // Anti-pattern guard: batch-marking done items at once is the
295
- // terminal-batch signature; the reminder lands in the result so the
296
- // caller sees it exactly where the behavior happens.
297
- const reminder = doneMarkReminder(patches);
309
+ // Anti-pattern guard: batch-marking the *last* open items at once is
310
+ // the terminal-batch signature; the reminder lands in the result so
311
+ // the caller sees it exactly where the behavior happens. `current` is
312
+ // the post-call state, so remaining open items tell us whether this
313
+ // batch cleared the tracker (terminal) or just caught up mid-work.
314
+ const openRemaining = current.lists.reduce(
315
+ (n, l) => n + l.items.filter((i) => !i.done).length,
316
+ 0,
317
+ );
318
+ const reminder = doneMarkReminder(patches, openRemaining);
298
319
  if (reminder !== null) text += `\n${reminder}`;
299
320
  break;
300
321
  }
package/src/store.test.ts CHANGED
@@ -473,7 +473,7 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
473
473
  }),
474
474
  );
475
475
 
476
- it.effect("updateItems applies several patches in one call", () =>
476
+ it.effect("updateItems applies several patches within one list", () =>
477
477
  Effect.gen(function* () {
478
478
  const store = yield* TrackerStore;
479
479
  yield* store.reset(emptyState());
@@ -481,10 +481,10 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
481
481
  const list = yield* store.createList("Work");
482
482
  yield* store.addItems(list.id, ["a", "b", "c"]);
483
483
 
484
- const updated = yield* store.updateItems([
485
- { itemId: "Work:1", done: true },
486
- { itemId: "Work:2", text: "bee" },
487
- { itemId: "Work:3", text: "sea", done: true },
484
+ const updated = yield* store.updateItems(list.id, [
485
+ { index: 1, done: true },
486
+ { index: 2, text: "bee" },
487
+ { index: 3, text: "sea", done: true },
488
488
  ]);
489
489
  // Returned in patch order, with the final state of each item.
490
490
  assert.deepStrictEqual(
@@ -495,10 +495,8 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
495
495
  ["sea", true],
496
496
  ],
497
497
  );
498
-
499
- const state = yield* store.state;
500
498
  assert.deepStrictEqual(
501
- state.lists[0]?.items.map((i) => [i.text, i.done]),
499
+ (yield* store.state).lists[0]?.items.map((i) => [i.text, i.done]),
502
500
  [
503
501
  ["a", true],
504
502
  ["bee", false],
@@ -508,7 +506,7 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
508
506
  }),
509
507
  );
510
508
 
511
- it.effect("updateItems with all-empty patches is a no-op", () =>
509
+ it.effect("updateItems fails atomically for an out-of-range index", () =>
512
510
  Effect.gen(function* () {
513
511
  const store = yield* TrackerStore;
514
512
  yield* store.reset(emptyState());
@@ -517,37 +515,33 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
517
515
  yield* store.addItem(list.id, "a");
518
516
  const before = yield* store.state;
519
517
 
520
- const result = yield* store.updateItems([{ itemId: "Work:1" }, { itemId: "Work:1" }]);
521
- assert.deepStrictEqual(
522
- result.map((i) => i.text),
523
- ["a", "a"],
518
+ const result = yield* Effect.result(
519
+ store.updateItems(list.id, [
520
+ { index: 1, done: true },
521
+ { index: 99, done: true },
522
+ ]),
524
523
  );
524
+ assert(Result.isFailure(result));
525
+ assert.strictEqual(Option.getOrThrow(Result.getFailure(result)).reason, "ItemNotFound");
526
+ // The valid patch in the batch was not applied.
525
527
  assert.deepStrictEqual(yield* store.state, before);
526
528
  }),
527
529
  );
528
530
 
529
- it.effect("updateItems rejects empty replacement text atomically", () =>
531
+ it.effect("updateItems fails for a missing list", () =>
530
532
  Effect.gen(function* () {
531
533
  const store = yield* TrackerStore;
532
534
  yield* store.reset(emptyState());
533
535
 
534
- const list = yield* store.createList("Work");
535
- yield* store.addItem(list.id, "a");
536
- const before = yield* store.state;
537
-
538
536
  const result = yield* Effect.result(
539
- store.updateItems([
540
- { itemId: "Work:1", done: true },
541
- { itemId: "Work:1", text: " " },
542
- ]),
537
+ store.updateItems(999, [{ index: 1, done: true }]),
543
538
  );
544
539
  assert(Result.isFailure(result));
545
- assert.strictEqual(Option.getOrThrow(Result.getFailure(result)).reason, "EmptyText");
546
- assert.deepStrictEqual(yield* store.state, before);
540
+ assert.strictEqual(Option.getOrThrow(Result.getFailure(result)).reason, "ListNotFound");
547
541
  }),
548
542
  );
549
543
 
550
- it.effect("updateItems fails atomically when any item is missing", () =>
544
+ it.effect("updateItems rejects empty replacement text atomically", () =>
551
545
  Effect.gen(function* () {
552
546
  const store = yield* TrackerStore;
553
547
  yield* store.reset(emptyState());
@@ -556,28 +550,19 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
556
550
  yield* store.addItem(list.id, "a");
557
551
  const before = yield* store.state;
558
552
 
559
- const noList = yield* Effect.result(store.updateItems([{ itemId: "Nope:1", done: true }]));
560
- assert(Result.isFailure(noList));
561
- const noListFailure = Option.getOrThrow(Result.getFailure(noList));
562
- assert.strictEqual(noListFailure.reason, "ItemNotFound");
563
- assert.strictEqual(noListFailure.itemId, "Nope:1");
564
-
565
- const noItem = yield* Effect.result(
566
- store.updateItems([
567
- { itemId: "Work:1", done: true },
568
- { itemId: "Work:99", done: true },
553
+ const result = yield* Effect.result(
554
+ store.updateItems(list.id, [
555
+ { index: 1, done: true },
556
+ { index: 1, text: " " },
569
557
  ]),
570
558
  );
571
- assert(Result.isFailure(noItem));
572
- const failure = Option.getOrThrow(Result.getFailure(noItem));
573
- assert.strictEqual(failure.reason, "ItemNotFound");
574
- assert.strictEqual(failure.itemId, "Work:99");
575
- // The valid patch in the batch was not applied.
559
+ assert(Result.isFailure(result));
560
+ assert.strictEqual(Option.getOrThrow(Result.getFailure(result)).reason, "EmptyText");
576
561
  assert.deepStrictEqual(yield* store.state, before);
577
562
  }),
578
563
  );
579
564
 
580
- it.effect("updateItems with duplicate item ids: the last patch wins", () =>
565
+ it.effect("updateItems with duplicate index: the last patch wins", () =>
581
566
  Effect.gen(function* () {
582
567
  const store = yield* TrackerStore;
583
568
  yield* store.reset(emptyState());
@@ -585,9 +570,9 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
585
570
  const list = yield* store.createList("Work");
586
571
  yield* store.addItem(list.id, "a");
587
572
 
588
- const updated = yield* store.updateItems([
589
- { itemId: "Work:1", text: "first" },
590
- { itemId: "Work:1", text: "second", done: true },
573
+ const updated = yield* store.updateItems(list.id, [
574
+ { index: 1, text: "first" },
575
+ { index: 1, text: "second", done: true },
591
576
  ]);
592
577
  assert.deepStrictEqual(
593
578
  updated.map((i) => [i.text, i.done]),
@@ -603,44 +588,6 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
603
588
  }),
604
589
  );
605
590
 
606
- it.effect("updateItems spans several lists in one atomic batch", () =>
607
- Effect.gen(function* () {
608
- const store = yield* TrackerStore;
609
- yield* store.reset(emptyState());
610
-
611
- const work = yield* store.createList("Work");
612
- yield* store.addItem(work.id, "a");
613
- yield* store.addItem(work.id, "b");
614
- const home = yield* store.createList("Home", { activate: false });
615
- yield* store.addItem(home.id, "c");
616
-
617
- const updated = yield* store.updateItems([
618
- { itemId: "Work:1", done: true },
619
- { itemId: "Home:1", text: "water plants" },
620
- ]);
621
- assert.deepStrictEqual(
622
- updated.map((i) => [i.text, i.done]),
623
- [
624
- ["a", true],
625
- ["water plants", false],
626
- ],
627
- );
628
-
629
- const state = yield* store.state;
630
- assert.deepStrictEqual(
631
- state.lists[0]?.items.map((i) => [i.text, i.done]),
632
- [
633
- ["a", true],
634
- ["b", false],
635
- ],
636
- );
637
- assert.deepStrictEqual(
638
- state.lists[1]?.items.map((i) => [i.text, i.done]),
639
- [["water plants", false]],
640
- );
641
- }),
642
- );
643
-
644
591
  it.effect("updateItem rejects malformed ids with a format hint", () =>
645
592
  Effect.gen(function* () {
646
593
  const store = yield* TrackerStore;
package/src/store.ts CHANGED
@@ -138,10 +138,14 @@ export interface UpdateItemPatch {
138
138
  readonly done?: boolean;
139
139
  }
140
140
 
141
- /** An `UpdateItemPatch` that also names the item it targets (batch form). */
142
- export interface UpdateItemPatchWithId extends UpdateItemPatch {
143
- /** `listName:index`, as shown by the list action (e.g. `Work:2`). */
144
- readonly itemId: string;
141
+ /**
142
+ * An `UpdateItemPatch` that targets an item by its 1-based position within a
143
+ * single list (mirrors `add_item`'s `list_id + text[]` shape: the list is
144
+ * factored out, the patches are positional).
145
+ */
146
+ export interface UpdateItemInListPatch extends UpdateItemPatch {
147
+ /** 1-based position of the item within its list (e.g. 2 for the second item). */
148
+ readonly index: number;
145
149
  }
146
150
 
147
151
  /** Options for `createList`: activation behavior and initial items. */
@@ -173,7 +177,8 @@ export class TrackerStore extends Context.Service<
173
177
  patch: UpdateItemPatch,
174
178
  ) => Effect.Effect<TodoItem, TrackerError>;
175
179
  readonly updateItems: (
176
- patches: readonly UpdateItemPatchWithId[],
180
+ listId: number,
181
+ patches: readonly UpdateItemInListPatch[],
177
182
  ) => Effect.Effect<TodoItem[], TrackerError>;
178
183
  readonly removeItem: (itemId: string) => Effect.Effect<void, TrackerError>;
179
184
  }
@@ -404,12 +409,14 @@ export class TrackerStore extends Context.Service<
404
409
  });
405
410
 
406
411
  /**
407
- * Batched updates. Each patch's `itemId` names its own list
408
- * (`listName:index`), so one batch may span several lists. The whole
409
- * batch fails atomically if any target is missing.
412
+ * Per-list batched updates, mirroring `addItems(listId, texts)`: the
413
+ * list is named once and each patch targets an item by its 1-based
414
+ * position (`index`) within that list. The whole batch fails atomically
415
+ * if the list is missing or any index is out of range.
410
416
  */
411
417
  const updateItems = Effect.fn("TrackerStore.updateItems")(function* (
412
- patches: readonly UpdateItemPatchWithId[],
418
+ listId: number,
419
+ patches: readonly UpdateItemInListPatch[],
413
420
  ) {
414
421
  // Reject empty replacement texts up front so the whole batch fails
415
422
  // before touching the state (atomicity).
@@ -423,52 +430,54 @@ export class TrackerStore extends Context.Service<
423
430
  }
424
431
  return yield* mutate(
425
432
  (s): readonly [Result.Result<TodoItem[], TrackerError>, TrackerState] => {
426
- // Every target item must exist; the batch fails atomically otherwise.
427
- const resolved: Array<{
428
- readonly index: number;
429
- readonly list: TodoList;
430
- readonly item: TodoItem;
431
- }> = [];
432
- for (const patch of patches) {
433
- const r = resolveItemOrError(s, patch.itemId);
434
- if (!r.ok) {
435
- return [Result.fail(r.error), s];
436
- }
437
- resolved.push({ index: r.index, list: r.list, item: r.item });
438
- }
439
- // All-empty batch: return the current items, leave state untouched.
440
- if (patches.every((patch) => patch.text === undefined && patch.done === undefined)) {
441
- return [Result.succeed(resolved.map((r) => r.item)), s];
433
+ const list = s.lists.find((l) => l.id === listId);
434
+ if (!list) {
435
+ return [
436
+ Result.fail(
437
+ new TrackerError({
438
+ reason: "ListNotFound",
439
+ message: listNotFoundMessage(s, listId),
440
+ listId,
441
+ }),
442
+ ),
443
+ s,
444
+ ];
442
445
  }
443
- // Later patches win for duplicate positions. Group by list id so
444
- // affected lists are rebuilt exactly once each.
445
- const byList = new Map<number, Map<number, UpdateItemPatch>>();
446
- for (const [i, patch] of patches.entries()) {
447
- const listId = resolved[i]!.list.id;
448
- let patchesByIndex = byList.get(listId);
449
- if (!patchesByIndex) {
450
- patchesByIndex = new Map();
451
- byList.set(listId, patchesByIndex);
446
+ // Resolve every target position; the batch fails atomically if any
447
+ // index is out of range.
448
+ const positions: number[] = [];
449
+ for (const patch of patches) {
450
+ if (patch.index < 1 || patch.index > list.items.length) {
451
+ return [
452
+ Result.fail(
453
+ new TrackerError({
454
+ reason: "ItemNotFound",
455
+ message:
456
+ `Item ${list.name}:${patch.index} not found — list "${list.name}" has ` +
457
+ `${list.items.length} item${list.items.length === 1 ? "" : "s"}`,
458
+ }),
459
+ ),
460
+ s,
461
+ ];
452
462
  }
453
- patchesByIndex.set(resolved[i]!.index, patch);
463
+ positions.push(patch.index);
454
464
  }
455
- const lists = s.lists.map((l) => {
456
- const patchesByIndex = byList.get(l.id);
457
- if (!patchesByIndex) return l;
458
- const nextItems = l.items.map((item, idx) => {
459
- const patch = patchesByIndex.get(idx + 1);
460
- if (!patch) return item;
461
- const text = patch.text === undefined ? item.text : patch.text.trim();
462
- const done = patch.done ?? item.done;
463
- return new TodoItem({ text, done });
464
- });
465
- return new TodoList({ id: l.id, name: l.name, items: nextItems });
465
+ // Later patches win for duplicate positions.
466
+ const byIndex = new Map<number, UpdateItemPatch>();
467
+ patches.forEach((patch, i) => byIndex.set(positions[i]!, patch));
468
+ const nextItems = list.items.map((item, idx) => {
469
+ const patch = byIndex.get(idx + 1);
470
+ if (!patch) return item;
471
+ const text = patch.text === undefined ? item.text : patch.text.trim();
472
+ const done = patch.done ?? item.done;
473
+ return new TodoItem({ text, done });
466
474
  });
475
+ const lists = s.lists.map((l) =>
476
+ l.id === listId ? new TodoList({ id: l.id, name: l.name, items: nextItems }) : l,
477
+ );
467
478
  return [
468
479
  // Results in patch order so the caller can map them 1:1.
469
- Result.succeed(
470
- resolved.map((r) => lists.find((l) => l.id === r.list.id)!.items[r.index - 1]!),
471
- ),
480
+ Result.succeed(positions.map((index) => nextItems[index - 1]!)),
472
481
  new TrackerState({ ...s, lists }),
473
482
  ];
474
483
  },
@@ -27,9 +27,10 @@ describe("tracker tool parameter schema", () => {
27
27
  { action: "update_item", item_id: "Work:2" },
28
28
  {
29
29
  action: "update_item",
30
+ list_id: 1,
30
31
  items: [
31
- { item_id: "Work:1", done: true },
32
- { item_id: "Work:2", text: "New text" },
32
+ { index: 1, done: true },
33
+ { index: 2, text: "New text" },
33
34
  ],
34
35
  },
35
36
  { action: "remove_item", item_id: "Work:2" },
@@ -57,7 +58,16 @@ describe("tracker tool parameter schema", () => {
57
58
  expect(
58
59
  Value.Check(TrackerToolParams, {
59
60
  action: "update_item",
60
- items: [{ item_id: "Work:1", done: "yes" }],
61
+ list_id: 1,
62
+ items: [{ index: "one", done: "yes" }],
63
+ }),
64
+ ).toBe(false);
65
+ expect(
66
+ Value.Check(TrackerToolParams, {
67
+ // item_id inside the batch is a typo now; patch objects are strict.
68
+ action: "update_item",
69
+ list_id: 1,
70
+ items: [{ item_id: "Work:1", done: true }],
61
71
  }),
62
72
  ).toBe(false);
63
73
  expect(Value.Check(TrackerToolParams, { action: "update_item", item_id: 2 })).toBe(false); // ids are strings
@@ -91,7 +101,7 @@ describe("validateTrackerCall (error-nudging layer)", () => {
91
101
  { action: "add_item", list_id: 1, text: ["a", "b"] },
92
102
  { action: "update_item", item_id: "Work:2" },
93
103
  { action: "update_item", item_id: "Work:2", text: "x", done: true },
94
- { action: "update_item", items: [{ item_id: "Work:2", done: true }] },
104
+ { action: "update_item", list_id: 1, items: [{ index: 2, done: true }] },
95
105
  { action: "remove_item", item_id: "Work:2" },
96
106
  ];
97
107
  for (const call of valid) {
@@ -114,14 +124,15 @@ describe("validateTrackerCall (error-nudging layer)", () => {
114
124
  expect(bare.ok).toBe(false);
115
125
  if (!bare.ok) expect(bare.message).toContain("accepts no parameters");
116
126
 
117
- // The old numeric contract (list_id + numeric item_id) is rejected with
118
- // the new parameter list, so a stale agent call self-corrects.
119
- const stale = validateTrackerCall({ action: "update_item", list_id: 1, item_id: 2 });
120
- expect(stale.ok).toBe(false);
121
- if (!stale.ok) {
122
- expect(stale.message).toContain("list_id");
123
- expect(stale.message).toContain("item_id");
124
- }
127
+ // The old numeric contract (list_id + numeric item_id) is enforced by the
128
+ // schema (item_id must be a string), not here; the mixed-form and
129
+ // missing-list_id nudges below cover the batch contract.
130
+ const batch = validateTrackerCall({
131
+ action: "update_item",
132
+ list_id: 1,
133
+ items: [{ index: 2, done: true }],
134
+ });
135
+ expect(batch.ok).toBe(true);
125
136
  });
126
137
 
127
138
  it("nudges on missing required fields", () => {
@@ -144,11 +155,18 @@ describe("validateTrackerCall (error-nudging layer)", () => {
144
155
  const mixed = validateTrackerCall({
145
156
  action: "update_item",
146
157
  item_id: "Work:2",
147
- items: [{ item_id: "Work:3" }],
158
+ items: [{ index: 3 }],
148
159
  });
149
160
  expect(mixed.ok).toBe(false);
150
161
  if (!mixed.ok) expect(mixed.message).toContain("not both");
151
162
 
163
+ const missingList = validateTrackerCall({
164
+ action: "update_item",
165
+ items: [{ index: 1, done: true }],
166
+ });
167
+ expect(missingList.ok).toBe(false);
168
+ if (!missingList.ok) expect(missingList.message).toContain("list_id");
169
+
152
170
  const arrayText = validateTrackerCall({
153
171
  action: "update_item",
154
172
  item_id: "Work:2",
@@ -171,22 +189,30 @@ describe("validateTrackerCall (error-nudging layer)", () => {
171
189
  });
172
190
  });
173
191
 
174
- describe("doneMarkReminder (batch done-mark guard)", () => {
175
- it("reminds when one call marks two or more items done", () => {
176
- const reminder = doneMarkReminder([{ done: true }, { done: true }]);
192
+ describe("doneMarkReminder (terminal batch done-mark guard)", () => {
193
+ it("reminds when one call marks two or more items done and clears the tracker", () => {
194
+ const reminder = doneMarkReminder([{ done: true }, { done: true }], 0);
177
195
  expect(reminder).not.toBeNull();
178
196
  expect(reminder).toContain("same turn");
197
+ expect(reminder).toContain("no open items");
179
198
  expect(reminder).toContain("2 items");
180
199
  });
181
200
 
182
- it("is silent for single done marks and text-only batches", () => {
183
- expect(doneMarkReminder([{ done: true }])).toBeNull();
184
- expect(doneMarkReminder([{}, {}])).toBeNull(); // text-only/no-op patches
185
- expect(doneMarkReminder([])).toBeNull();
201
+ it("is silent when a multi-done batch leaves open items (mid-turn progress)", () => {
202
+ // Two done marks but other work still open: not a terminal batch.
203
+ expect(doneMarkReminder([{ done: true }, { done: true }], 1)).toBeNull();
204
+ // Multi-done batch, post-call state unknown: stay quiet rather than guess.
205
+ expect(doneMarkReminder([{ done: true }, { done: true }])).toBeNull();
206
+ });
207
+
208
+ it("is silent for single done marks, text-only batches, and empty batches", () => {
209
+ expect(doneMarkReminder([{ done: true }], 0)).toBeNull();
210
+ expect(doneMarkReminder([{}, {}], 0)).toBeNull(); // text-only/no-op patches
211
+ expect(doneMarkReminder([], 0)).toBeNull();
186
212
  });
187
213
 
188
214
  it("does not count reopening (done: false) as marking done", () => {
189
- expect(doneMarkReminder([{ done: true }, { done: false }])).toBeNull();
215
+ expect(doneMarkReminder([{ done: true }, { done: false }], 0)).toBeNull();
190
216
  });
191
217
  });
192
218
 
@@ -39,7 +39,7 @@ export const TrackerToolParams = Type.Object({
39
39
  list_id: Type.Optional(
40
40
  Type.Number({
41
41
  description:
42
- "List id, as shown by the list action. Required for delete_list, add_item; optional for set_active (omit to deselect).",
42
+ "List id, as shown by the list action. Required for delete_list, add_item, and for update_item's items={...} batch; optional for set_active (omit to deselect).",
43
43
  }),
44
44
  ),
45
45
  item_id: Type.Optional(
@@ -85,8 +85,9 @@ export const TrackerToolParams = Type.Object({
85
85
  Type.Array(
86
86
  Type.Object(
87
87
  {
88
- item_id: Type.String({
89
- description: 'Item id, as shown by the list action (listName:index, e.g. "Work:2").',
88
+ index: Type.Number({
89
+ description:
90
+ '1-based position of the item within list_id (e.g. 2 for the second item).',
90
91
  }),
91
92
  text: Type.Optional(Type.String({ description: "Replacement text for the item." })),
92
93
  done: Type.Optional(
@@ -98,7 +99,7 @@ export const TrackerToolParams = Type.Object({
98
99
  ),
99
100
  {
100
101
  description:
101
- "For update_item: one or more item updates in a single call (all in the same list). Alternative to item_id/text/done.",
102
+ "For update_item: one or more item updates within a single list_id (mirrors add_item's list_id + text[]). Alternative to item_id/text/done.",
102
103
  minItems: 1,
103
104
  },
104
105
  ),
@@ -114,7 +115,7 @@ const ACTION_PARAMS: Readonly<Record<TrackerToolAction, readonly string[]>> = {
114
115
  delete_list: ["list_id"],
115
116
  set_active: ["list_id"],
116
117
  add_item: ["list_id", "text"],
117
- update_item: ["item_id", "text", "done", "items"],
118
+ update_item: ["list_id", "item_id", "text", "done", "items"],
118
119
  remove_item: ["item_id"],
119
120
  };
120
121
 
@@ -186,6 +187,7 @@ export function validateTrackerCall(args: unknown): TrackerCallValidation {
186
187
  break;
187
188
  case "update_item":
188
189
  if (record.items !== undefined) {
190
+ // Batch form: list_id + items (index-based, one list).
189
191
  if (
190
192
  record.item_id !== undefined ||
191
193
  record.text !== undefined ||
@@ -194,13 +196,21 @@ export function validateTrackerCall(args: unknown): TrackerCallValidation {
194
196
  return {
195
197
  ok: false,
196
198
  message:
197
- "update_item: pass either item_id/text/done (one item) or items (several items in one call), not both.",
199
+ "update_item: pass either item_id/text/done (one item) or list_id + items (several items in one list), not both.",
200
+ };
201
+ }
202
+ if (record.list_id === undefined) {
203
+ return {
204
+ ok: false,
205
+ message:
206
+ "update_item with 'items' requires the 'list_id' parameter (the list whose items are being updated).",
198
207
  };
199
208
  }
200
209
  } else if (record.item_id === undefined) {
201
210
  return {
202
211
  ok: false,
203
- message: "update_item requires 'item_id' (or 'items' for batched updates).",
212
+ message:
213
+ "update_item requires 'item_id' (one item) or 'list_id' + 'items' (several items in one list).",
204
214
  };
205
215
  } else if (Array.isArray(record.text)) {
206
216
  return {
@@ -240,18 +250,31 @@ export interface TrackerToolDetails {
240
250
  /**
241
251
  * Anti-pattern guard: the tracker guideline says to mark each item done in
242
252
  * the same turn it completes and never batch the marking at the end. A call
243
- * that marks two or more items done at once is the terminal-batch signature.
244
- * Returns a reminder (or null when the call is fine); it is advisory text,
245
- * never a rejection, so legitimate same-turn multi-completions still work.
253
+ * that marks two or more items done at once AND leaves no open items behind is
254
+ * the terminal-batch signature — the agent did all its work, then finished the
255
+ * list in one sweep. Returns a reminder (or null when the call is fine); it is
256
+ * advisory text, never a rejection, so legitimate same-turn multi-completions
257
+ * (when work is still open) still pass silently.
258
+ *
259
+ * @param patches the update_item patches in call order.
260
+ * @param openRemaining number of not-done items across all lists *after* the
261
+ * call (0 means the batch cleared the whole tracker). Pass undefined when
262
+ * the post-call state isn't known.
246
263
  */
247
264
  export const doneMarkReminder = (
248
265
  patches: readonly { readonly done?: boolean }[],
266
+ openRemaining?: number,
249
267
  ): string | null => {
250
268
  const doneCount = patches.filter((patch) => patch.done === true).length;
269
+ // Only the terminal batch — two or more done marks with nothing left open —
270
+ // is flagged. Batches that finish several items while other work remains are
271
+ // normal mid-turn progress and stay quiet. When the post-call open count is
272
+ // unknown (not 0) we stay quiet rather than guess at a terminal batch.
251
273
  if (doneCount < 2) return null;
274
+ if (openRemaining !== 0) return null;
252
275
  return (
253
- `Note: this call completed ${doneCount} items at once — the tracker guideline is to mark ` +
254
- "each item done in the same turn it completes, not batch the marking at the end."
276
+ `Note: this call completed ${doneCount} items at once and left no open items — the tracker ` +
277
+ "guideline is to mark each item done in the same turn it completes, not batch the marking at the end."
255
278
  );
256
279
  };
257
280
 
@@ -272,18 +295,20 @@ const TRACKER_TOOL_DESCRIPTION =
272
295
  "- set_active: list_id (omit the field to deselect)\n" +
273
296
  "- add_item: list_id (required), text (required: a string, or an array of strings to add several " +
274
297
  "items in one call)\n" +
275
- "- update_item: item_id (required, listName:index) with optional text/done, or items=" +
276
- "[{item_id, text?, done?}, ...] for several updates in one call (items may span lists; never " +
277
- "mix the two forms)\n" +
298
+ "- update_item: update several items in one list with list_id (required) + items=[{index, " +
299
+ "text?, done?}, ...] — just like add_item's list_id + text[] (index is the item's 1-based " +
300
+ "position in that list), or one item with item_id + optional text/done. Never mix the two " +
301
+ "forms. Example: update_item list_id=2 items=[{index: 2, done: true}, {index: 3, text: \"Ship " +
302
+ "the fix\"}]\n" +
278
303
  "- remove_item: item_id (required, listName:index)";
279
304
 
280
305
  const TRACKER_TOOL_PROMPT_SNIPPET =
281
- "Manage todolists and track progress: create lists with initial items, add/update/remove items, " +
282
- "batch adds and updates in one call";
306
+ "Manage todolists and track progress: create lists with initial items, add/update/remove items. " +
307
+ "Batch adds with text arrays and updates with update_item list_id + items=[...] in one call";
283
308
 
284
309
  const TRACKER_TOOL_PROMPT_GUIDELINES = [
285
310
  "Use tracker for todo lists, checklists, and multi-step work. Put progress in tracker, not in prose.",
286
- "Break work into tracker items up front. One item per deliverable. Mark an item done in the same turn it completes. Never batch the marking at the end. Batch related adds and updates in one call.",
311
+ "Break work into tracker items up front. One item per deliverable. Mark an item done in the same turn it completes. Never batch the marking at the end. Batch related adds and updates in one call — use update_item's list_id + items=[...] form to update several entries in one list at once.",
287
312
  "Before starting work, call tracker with action list. Work from the list, not from memory. Re-check it when the task drifts.",
288
313
  "Copy tracker item ids (listName:index, e.g. Work:2) from the list action output; removing an item renumbers the items after it.",
289
314
  "When a tracker call fails, read the error. It tells you what to fix. Not-found errors name the available ids. Retry with corrected parameters in the same turn. Never repeat the same failing call.",