pi-pocket 0.28.0 → 0.28.2-beta.1

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.
Files changed (44) hide show
  1. package/docs/customizing.md +1 -1
  2. package/docs/features.md +3 -3
  3. package/docs/map.md +1 -1
  4. package/npm-shrinkwrap.json +69 -65
  5. package/package.json +8 -8
  6. package/src/server/alerts.ts +21 -8
  7. package/src/server/anthropic-shape.ts +4 -3
  8. package/src/server/app.ts +18 -3
  9. package/src/server/artifact-state.ts +1 -1
  10. package/src/server/background.ts +1 -1
  11. package/src/server/cache-keeper.ts +1 -0
  12. package/src/server/commands.ts +386 -77
  13. package/src/server/compaction.ts +56 -2
  14. package/src/server/design/bundle.ts +1 -1
  15. package/src/server/docs.ts +43 -1
  16. package/src/server/extension-points.ts +1 -1
  17. package/src/server/extension-request.ts +1 -1
  18. package/src/server/extensions/codemode.ts +1 -1
  19. package/src/server/extensions/schedules.ts +1 -1
  20. package/src/server/extensions/subagents.ts +1 -1
  21. package/src/server/goals.ts +1 -1
  22. package/src/server/hidden.ts +1 -1
  23. package/src/server/host.ts +1 -1
  24. package/src/server/http/conversation-routes.ts +22 -0
  25. package/src/server/linked-sessions.ts +1 -1
  26. package/src/server/memory.ts +1 -1
  27. package/src/server/pip-compact.ts +1 -1
  28. package/src/server/pip.ts +9 -1
  29. package/src/server/public-links.ts +1 -1
  30. package/src/server/repo-watches.ts +1 -1
  31. package/src/server/resend.ts +26 -9
  32. package/src/server/room.ts +17 -1
  33. package/src/server/running.ts +3 -0
  34. package/src/server/schedules.ts +112 -12
  35. package/src/server/shell.ts +1 -1
  36. package/src/server/spend.ts +1 -1
  37. package/src/server/watches.ts +1 -1
  38. package/types/pocket.d.ts +1 -1
  39. package/web/composer.js +101 -3
  40. package/web/running-board.js +1 -0
  41. package/web/sheets/schedules.js +6 -0
  42. package/web/store.js +7 -0
  43. package/web/style.css +37 -0
  44. package/web/transcript.js +15 -0
@@ -7,7 +7,8 @@
7
7
  import { randomUUID } from "node:crypto";
8
8
  import { statSync } from "node:fs";
9
9
  import { basename, extname, join, sep } from "node:path";
10
- import { type Context, copyJson } from "@earendil-works/chord";
10
+ import type { Context } from "@earendil-works/chord/context";
11
+ import { copyJson, type JsonValue } from "@earendil-works/chord/json";
11
12
  import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
12
13
  import {
13
14
  clampThinkingLevel,
@@ -40,6 +41,9 @@ import type { User } from "./config.ts";
40
41
  import {
41
42
  AuthorsDoc,
42
43
  ChatDoc,
44
+ HeldDoc,
45
+ type HeldMessage,
46
+ InterruptsDoc,
43
47
  NotesDoc,
44
48
  PinsDoc,
45
49
  PlanDoc,
@@ -67,7 +71,7 @@ import { writePiSession } from "./pi-sessions.ts";
67
71
  import { snippet } from "./projection.ts";
68
72
  import { globalModelSettings, projectModelDefault } from "./project-model.ts";
69
73
  import { expandPromptTemplate, expandSkillCommand, unwrapSkill } from "./prompts.ts";
70
- import { clientKey, ownRequest, requestFor } from "./requests.ts";
74
+ import { clientKey, ownRequest, requestFor, requestPerson } from "./requests.ts";
71
75
  import {
72
76
  type Interrupt,
73
77
  interruptTask,
@@ -335,7 +339,7 @@ export class Commands {
335
339
  * says who dealt with the failure. A message left in the queue by a run that failed ends it, settling nothing.
336
340
  */
337
341
  readonly followTask: Task<Followed, { phase: "wait" }, null, object>;
338
- /** A queued message moved to Interrupt (`moveQueued`): it stops the run it was made for, by that run's id. */
342
+ /** An Interrupt (`submit`, `moveQueued`): it stops the run it was made for, by that run's id. */
339
343
  readonly interruptTask: Task<Interrupt, { phase: "send" }, null, object>;
340
344
 
341
345
  constructor(app: PocketApp) {
@@ -900,38 +904,52 @@ export class Commands {
900
904
  submitContext.abortSignal?.throwIfAborted();
901
905
  }
902
906
 
903
- // Interrupt: stop the run now, rather than waiting for its tool round or turn to end. Like Stop, this withdraws
904
- // what waits in the queue. Only a person's own message interrupts, never one sent for them (`via`).
905
- // A retried request finds its first submission: it must not stop the run that the first one started. (Two POSTs
906
- // of one request at the same moment can both find none and both stop the run: a rare race, left as it is.)
907
- if (
908
- request.mode === "interrupt" &&
909
- via === undefined &&
910
- app.isBusy(id) &&
911
- (await app.harness.commit((tx) => tx.submissionByRequest(id, requestId), context)) ===
912
- undefined
913
- ) {
914
- await this.#discardReports(id);
915
- await conversation.abort(context);
916
- await app.collab.activity(id, user, "interrupted the run");
917
- }
918
-
919
907
  // A retry of a request finds its first submission: it is no new message, and no event goes out for it again.
920
908
  const known =
921
909
  app.pip.ownerOf(id) === undefined &&
922
910
  (await app.harness.commit((tx) => tx.submissionByRequest(id, requestId), context)) !==
923
911
  undefined;
924
- const submission = await conversation
925
- .submit(
926
- {
927
- type: "input",
928
- content,
929
- whenBusy: request.mode === "steer" ? "steer" : "followUp",
930
- requestId,
931
- },
932
- submitContext,
933
- )
934
- .finally(() => admission?.afterSubmit());
912
+ // Only a person's own message interrupts, never one sent for them (`via`). The same durable handoff as a
913
+ // queued message's Interrupt keeps everything waiting, before it stops the run. Admission releases its lease
914
+ // once that handoff is stored, not while the task stops the run and puts the messages back.
915
+ const admitted = await (
916
+ request.mode === "interrupt" && via === undefined
917
+ ? this.#interrupt(id, { content, requestId }, submitContext)
918
+ : conversation
919
+ .submit(
920
+ {
921
+ type: "input",
922
+ content,
923
+ whenBusy: request.mode === "steer" ? "steer" : "followUp",
924
+ requestId,
925
+ },
926
+ submitContext,
927
+ )
928
+ .then((submission) => ({ submissionId: submission.id }))
929
+ ).finally(() => admission?.afterSubmit());
930
+ let submissionId: SubmissionId;
931
+
932
+ if ("task" in admitted) {
933
+ if (admitted.started) {
934
+ await app.collab.activity(id, user, "interrupted the run");
935
+ }
936
+
937
+ await app.harness.waitForTask(admitted.task, context);
938
+ const sent = await app.harness.commit(
939
+ (tx) => tx.submissionByRequest(id, requestId),
940
+ context,
941
+ );
942
+
943
+ if (sent === undefined) {
944
+ throw new HttpError(409, "The Interrupt message could not be sent. Try again.");
945
+ }
946
+
947
+ submissionId = sent.id;
948
+ } else {
949
+ submissionId = admitted.submissionId;
950
+ }
951
+
952
+ const submission = (await app.harness.submission(submissionId, context))!;
935
953
 
936
954
  app.collab.setTyping(id, user, null);
937
955
  // The title comes from the message as it was stored: without hidden values.
@@ -1344,15 +1362,251 @@ export class Commands {
1344
1362
  app.requireSteer(user);
1345
1363
  const conversation = await app.conversation(id);
1346
1364
  const busy = app.isBusy(id);
1365
+ // People's waiting messages are held, before the stop can withdraw them: they wait until someone sends them. PiP's
1366
+ // panel has no place to show them, so a Stop there withdraws them, as before.
1367
+ const held = app.pip.ownerOf(id) === undefined ? await this.#hold(id) : 0;
1347
1368
 
1348
- // Stop withdraws what waits in the queue, subagents' reports too: a person meant them gone.
1369
+ // Subagents' reports go: a person meant the work stopped.
1349
1370
  await this.#discardReports(id);
1350
1371
  void conversation
1351
1372
  .abort(context)
1352
1373
  .catch((error: unknown) => app.notice("error", `Abort failed: ${describe(error)}`, id));
1353
1374
 
1354
- if (busy) {
1355
- await app.collab.activity(id, user, "stopped the run");
1375
+ if (busy || held > 0) {
1376
+ await app.collab.activity(
1377
+ id,
1378
+ user,
1379
+ held === 0
1380
+ ? "stopped the run"
1381
+ : `stopped the run; ${held} queued ${held === 1 ? "message waits" : "messages wait"} to be sent`,
1382
+ );
1383
+ }
1384
+ }
1385
+
1386
+ /**
1387
+ * Take the queue's messages out of it into `HeldDoc`, in one commit, so the stop that follows cannot withdraw them.
1388
+ * Writes stay queued, and a batch of subagents' reports goes back to its courier. How many were held.
1389
+ */
1390
+ async #hold(id: ConversationId): Promise<number> {
1391
+ const app = this.#app;
1392
+ // Request ids of the queued messages, from Pi Durable's records: a commit cannot read a submission by its id.
1393
+ const requests = new Map<number, string | undefined>();
1394
+
1395
+ for (let attempt = 0; ; attempt++) {
1396
+ const queued = (await app.harness.snapshot(InboxDoc, id, context))?.items ?? [];
1397
+
1398
+ for (const item of queued) {
1399
+ if (item.mode !== "write" && !requests.has(Number(item.id))) {
1400
+ const record = await (
1401
+ await app.harness.submission(item.id, context)
1402
+ )?.status(context);
1403
+
1404
+ requests.set(Number(item.id), record?.requestId);
1405
+ }
1406
+ }
1407
+
1408
+ const held = await app.harness.commit(async (tx) => {
1409
+ const items = (await tx.doc(InboxDoc, id)).items;
1410
+ // Every table read comes before the commit's first table write.
1411
+ const subagents = await tx.doc(SubagentsDoc, id);
1412
+ const batch =
1413
+ subagents.sending === undefined
1414
+ ? undefined
1415
+ : await tx.submissionByRequest(id, subagents.sending.request);
1416
+ const now = app.now();
1417
+ const taken = new Set<SubmissionId>();
1418
+ const adding: HeldMessage[] = [];
1419
+
1420
+ for (const item of items) {
1421
+ if (item.mode === "write" || item.id === batch?.id) {
1422
+ continue;
1423
+ }
1424
+
1425
+ if (!requests.has(Number(item.id))) {
1426
+ // Queued since the requests were read. Stop must not fail: after a few tries, it goes.
1427
+ if (attempt < 3) {
1428
+ return "again";
1429
+ }
1430
+
1431
+ continue;
1432
+ }
1433
+
1434
+ const first = requests.get(Number(item.id));
1435
+ const person = requestPerson(first);
1436
+
1437
+ taken.add(item.id);
1438
+ adding.push({
1439
+ key: String(item.id),
1440
+ content: copyJson(item.content) as JsonValue,
1441
+ mode: item.mode,
1442
+ // The person is in the request id: the new one keeps it, for who wrote it and whose work it is.
1443
+ requestId:
1444
+ first === undefined
1445
+ ? `held-${String(item.id)}`
1446
+ : `${first.replace(/(~\d+)+$/, "")}~${String(item.id)}`,
1447
+ ...(item.entry === undefined
1448
+ ? {}
1449
+ : { entry: copyJson(item.entry) as JsonValue }),
1450
+ ...(person?.wrote === true ? { by: person.userId } : {}),
1451
+ heldAt: now,
1452
+ });
1453
+ }
1454
+
1455
+ if (taken.size === 0) {
1456
+ return 0;
1457
+ }
1458
+
1459
+ for (let at = items.length - 1; at >= 0; at--) {
1460
+ if (taken.has(items[at]!.id)) {
1461
+ items.splice(at, 1);
1462
+ }
1463
+ }
1464
+
1465
+ for (const item of taken) {
1466
+ tx.settleSubmission(item, { status: "unanswered", reason: "aborted" });
1467
+ }
1468
+
1469
+ (await tx.doc(HeldDoc, id)).items.push(...adding);
1470
+
1471
+ return taken.size;
1472
+ }, context);
1473
+
1474
+ if (held !== "again") {
1475
+ return held;
1476
+ }
1477
+ }
1478
+ }
1479
+
1480
+ /**
1481
+ * Send the messages a Stop held, in their order, each as it was queued: they go to Pi as the people who wrote them.
1482
+ * Sending them is sending to Pi: it takes the wheel and the spend limit, as a message does. Each keeps its request
1483
+ * id, so a retry after a failure sends none twice.
1484
+ */
1485
+ async sendHeld(id: ConversationId, user: User): Promise<number> {
1486
+ const app = this.#app;
1487
+
1488
+ app.requireSee(user, id);
1489
+ app.requireSteer(user);
1490
+ await app.requireDriver(id, user);
1491
+
1492
+ if (app.pip.ownerOf(id) !== undefined && !app.pip.available(user)) {
1493
+ throw new HttpError(409, "PiP is turned off in Extensions.");
1494
+ }
1495
+
1496
+ app.spend.check(user, id);
1497
+ const conversation = await app.conversation(id);
1498
+ const held = (await app.harness.snapshot(HeldDoc, id, context))?.items ?? [];
1499
+
1500
+ for (const item of held) {
1501
+ await conversation.submit(
1502
+ {
1503
+ type: "input",
1504
+ content: item.content as unknown as UserInput,
1505
+ whenBusy: item.mode,
1506
+ requestId: item.requestId,
1507
+ ...(item.entry === undefined
1508
+ ? {}
1509
+ : { entry: item.entry as unknown as InputEntry }),
1510
+ },
1511
+ context,
1512
+ );
1513
+ }
1514
+
1515
+ const sent = new Set(held.map((item) => item.key));
1516
+
1517
+ await app.harness.commit(async (tx) => {
1518
+ const doc = await tx.doc(HeldDoc, id);
1519
+
1520
+ doc.items = doc.items.filter((item) => !sent.has(item.key));
1521
+ }, context);
1522
+
1523
+ if (held.length > 0) {
1524
+ await app.collab.activity(
1525
+ id,
1526
+ user,
1527
+ `sent the ${held.length === 1 ? "held message" : `${held.length} held messages`}`,
1528
+ );
1529
+ }
1530
+
1531
+ return held.length;
1532
+ }
1533
+
1534
+ /** Take a held message away for good. Anyone may their own; someone else's needs the right to drive, as in the queue. */
1535
+ async dropHeld(id: ConversationId, user: User, key: string): Promise<void> {
1536
+ const app = this.#app;
1537
+
1538
+ app.requireSee(user, id);
1539
+ app.requireSteer(user);
1540
+ const item = await this.#heldItem(id, key);
1541
+
1542
+ if (item.by !== user.id) {
1543
+ await app.requireDriver(id, user);
1544
+ }
1545
+
1546
+ await this.#unhold(id, key);
1547
+
1548
+ if (item.by !== undefined && item.by !== user.id) {
1549
+ const author = app.config.userById(item.by)?.name ?? "someone";
1550
+
1551
+ await app.collab.activity(id, user, `withdrew ${author}’s held message`);
1552
+ }
1553
+ }
1554
+
1555
+ /** Take one's own held message back into the message box, as Edit does for a queued one. */
1556
+ async editHeld(
1557
+ id: ConversationId,
1558
+ user: User,
1559
+ key: string,
1560
+ ): Promise<{ text: string; attachments: Attachment[] }> {
1561
+ const app = this.#app;
1562
+
1563
+ app.requireSee(user, id);
1564
+ app.requireSteer(user);
1565
+ const item = await this.#heldItem(id, key);
1566
+
1567
+ if (item.by !== user.id) {
1568
+ throw new HttpError(403, "Only its sender can edit a held message.");
1569
+ }
1570
+
1571
+ await this.#unhold(id, key);
1572
+ const [written, files] = splitAttachments(
1573
+ userContent({ content: item.content as unknown as UserInput }).text.replace(
1574
+ FROM_PREFIX,
1575
+ "",
1576
+ ),
1577
+ );
1578
+ const uploads = app.workspace.uploadDirectory(id);
1579
+
1580
+ return {
1581
+ text: written,
1582
+ attachments: (files ?? "").split("\n").flatMap((line) => attachedFile(line, uploads)),
1583
+ };
1584
+ }
1585
+
1586
+ async #heldItem(id: ConversationId, key: string): Promise<HeldMessage> {
1587
+ const item = (await this.#app.harness.snapshot(HeldDoc, id, context))?.items.find(
1588
+ (each) => each.key === key,
1589
+ );
1590
+
1591
+ if (item === undefined) {
1592
+ throw new HttpError(409, "That message is no longer held.");
1593
+ }
1594
+
1595
+ return item;
1596
+ }
1597
+
1598
+ async #unhold(id: ConversationId, key: string): Promise<void> {
1599
+ const gone = await this.#app.harness.commit(async (tx) => {
1600
+ const doc = await tx.doc(HeldDoc, id);
1601
+ const before = doc.items.length;
1602
+
1603
+ doc.items = doc.items.filter((item) => item.key !== key);
1604
+
1605
+ return doc.items.length < before;
1606
+ }, context);
1607
+
1608
+ if (!gone) {
1609
+ throw new HttpError(409, "That message is no longer held.");
1356
1610
  }
1357
1611
  }
1358
1612
 
@@ -1494,38 +1748,82 @@ export class Commands {
1494
1748
  await app.requireDriver(id, user);
1495
1749
  }
1496
1750
 
1751
+ if (mode !== "interrupt") {
1752
+ const moved = await app.harness.commit(async (tx) => {
1753
+ const item = (await tx.doc(InboxDoc, id)).items.find(
1754
+ (entry) => Number(entry.id) === submissionId,
1755
+ );
1756
+
1757
+ if (item === undefined || item.mode === "write") {
1758
+ return false;
1759
+ }
1760
+
1761
+ item.mode = mode;
1762
+
1763
+ return true;
1764
+ }, context);
1765
+
1766
+ if (!moved) {
1767
+ throw new HttpError(409, "That message has already gone to Pi.");
1768
+ }
1769
+
1770
+ return;
1771
+ }
1772
+
1773
+ await this.#interrupt(id, {
1774
+ submissionId,
1775
+ requestId: ownRequest(user.id, `interrupt-${submissionId}`),
1776
+ });
1777
+ await app.collab.activity(id, user, "interrupted the run");
1778
+ }
1779
+
1780
+ /** Store an Interrupt and every waiting input together, before stopping only the run seen in this commit. */
1781
+ async #interrupt(
1782
+ id: ConversationId,
1783
+ input: { requestId: string } & ({ content: UserInput } | { submissionId: number }),
1784
+ admissionContext: Context = context,
1785
+ ): Promise<{ submissionId: SubmissionId } | { task: TaskId; started: boolean }> {
1786
+ const app = this.#app;
1787
+ const conversation = await app.conversation(id);
1497
1788
  // Request ids of the queued messages, from Pi Durable's records: a commit cannot read a submission by its id.
1498
1789
  const requests = new Map<number, string | undefined>();
1499
- let moved: boolean | "again" = "again";
1500
1790
 
1501
- for (let attempt = 0; moved === "again"; attempt++) {
1502
- if (mode === "interrupt") {
1503
- const queued = (await app.harness.snapshot(InboxDoc, id, context))?.items ?? [];
1791
+ for (let attempt = 0; ; attempt++) {
1792
+ const queued = (await app.harness.snapshot(InboxDoc, id, context))?.items ?? [];
1504
1793
 
1505
- for (const item of queued) {
1506
- if (item.mode !== "write" && !requests.has(Number(item.id))) {
1507
- const record = await (
1508
- await app.harness.submission(item.id, context)
1509
- )?.status(context);
1794
+ for (const item of queued) {
1795
+ if (item.mode !== "write" && !requests.has(Number(item.id))) {
1796
+ const record = await (
1797
+ await app.harness.submission(item.id, context)
1798
+ )?.status(context);
1510
1799
 
1511
- requests.set(Number(item.id), record?.requestId);
1512
- }
1800
+ requests.set(Number(item.id), record?.requestId);
1513
1801
  }
1514
1802
  }
1515
1803
 
1516
- moved = await app.harness.commit(async (tx) => {
1517
- const items = (await tx.doc(InboxDoc, id)).items;
1518
- const index = items.findIndex((entry) => Number(entry.id) === submissionId);
1519
- const item = items[index];
1804
+ const admitted = await app.harness.commit(async (tx) => {
1805
+ const known = await tx.submissionByRequest(id, input.requestId);
1806
+ const interrupts = await tx.doc(InterruptsDoc, id);
1807
+ const pending = interrupts.pending[input.requestId];
1520
1808
 
1521
- if (item === undefined || item.mode === "write") {
1522
- return false;
1809
+ // The task is the receipt until the actual submission exists. Two concurrent POSTs, and a retry after
1810
+ // a restart between stop and send, find that same handoff rather than stopping a newer run.
1811
+ if (known !== undefined) {
1812
+ return { submissionId: known.id };
1813
+ }
1814
+
1815
+ if (pending !== undefined) {
1816
+ return { task: pending as unknown as TaskId, started: false };
1523
1817
  }
1524
1818
 
1525
- if (mode !== "interrupt") {
1526
- item.mode = mode;
1819
+ const items = (await tx.doc(InboxDoc, id)).items;
1820
+ const item =
1821
+ "submissionId" in input
1822
+ ? items.find((entry) => Number(entry.id) === input.submissionId)
1823
+ : undefined;
1527
1824
 
1528
- return true;
1825
+ if (item?.mode === "write" || ("submissionId" in input && item === undefined)) {
1826
+ throw new HttpError(409, "That message has already gone to Pi.");
1529
1827
  }
1530
1828
 
1531
1829
  // Every table read comes before the commit's first table write.
@@ -1535,16 +1833,23 @@ export class Commands {
1535
1833
  ? undefined
1536
1834
  : await tx.submissionByRequest(id, subagents.sending.request);
1537
1835
  const live = await tx.doc(LiveDoc, id);
1836
+
1837
+ if (live.run === undefined && "content" in input) {
1838
+ return {
1839
+ submissionId: await conversation.submit(tx, {
1840
+ type: "input",
1841
+ content: input.content,
1842
+ requestId: input.requestId,
1843
+ }),
1844
+ };
1845
+ }
1846
+
1538
1847
  const others: Requeue[] = [];
1539
- const stay: number[] = [];
1848
+ const stay = new Set<SubmissionId>();
1540
1849
 
1541
1850
  for (const other of items) {
1542
1851
  // Writes stay in the queue by themselves; the reports go back to their courier when withdrawn.
1543
- if (
1544
- Number(other.id) === submissionId ||
1545
- other.mode === "write" ||
1546
- other.id === batch?.id
1547
- ) {
1852
+ if (other.id === item?.id || other.mode === "write" || other.id === batch?.id) {
1548
1853
  continue;
1549
1854
  }
1550
1855
 
@@ -1559,63 +1864,67 @@ export class Commands {
1559
1864
 
1560
1865
  const first = requests.get(Number(other.id));
1561
1866
 
1562
- stay.push(Number(other.id));
1867
+ stay.add(other.id);
1563
1868
  others.push({
1564
1869
  content: copyJson(other.content) as UserInput,
1565
1870
  mode: other.mode,
1566
1871
  // The person is in the request id: the new one keeps it, for who wrote it and whose work it is.
1567
1872
  requestId:
1568
1873
  first === undefined
1569
- ? `interrupt-${submissionId}-${others.length}`
1570
- : `${first.replace(/(~\d+)+$/, "")}~${submissionId}`,
1874
+ ? `interrupt-${input.requestId}-${others.length}`
1875
+ : `${first.replace(/(~\d+)+$/, "")}~${other.id}`,
1571
1876
  ...(other.entry === undefined
1572
1877
  ? {}
1573
1878
  : { entry: copyJson(other.entry) as InputEntry }),
1574
1879
  });
1575
1880
  }
1576
1881
 
1577
- const content = copyJson(item.content) as UserInput;
1882
+ const content =
1883
+ "content" in input ? input.content : (copyJson(item!.content) as UserInput);
1578
1884
  const run = live.run === undefined ? undefined : Number(live.run.taskId);
1579
1885
 
1580
1886
  // Taken out of the queue here, so the stop's withdrawal cannot reach them: the task puts them back.
1581
1887
  for (let at = items.length - 1; at >= 0; at--) {
1582
- const queued = Number(items[at]!.id);
1888
+ const queued = items[at]!.id;
1583
1889
 
1584
- if (queued === submissionId || stay.includes(queued)) {
1890
+ if (queued === item?.id || stay.has(queued)) {
1585
1891
  items.splice(at, 1);
1586
1892
  }
1587
1893
  }
1588
1894
 
1589
- tx.settleSubmission(item.id, { status: "unanswered", reason: "aborted" });
1895
+ if (item !== undefined) {
1896
+ tx.settleSubmission(item.id, { status: "unanswered", reason: "aborted" });
1897
+ }
1590
1898
 
1591
1899
  for (const taken of stay) {
1592
- tx.settleSubmission(taken as unknown as SubmissionId, {
1900
+ tx.settleSubmission(taken, {
1593
1901
  status: "unanswered",
1594
1902
  reason: "aborted",
1595
1903
  });
1596
1904
  }
1597
1905
 
1598
- await tx.createTask(
1906
+ const task = await tx.createTask(
1599
1907
  this.interruptTask,
1600
1908
  {
1601
1909
  content,
1602
- requestId: ownRequest(user.id, `interrupt-${submissionId}`),
1910
+ requestId: input.requestId,
1911
+ ...(item?.entry === undefined
1912
+ ? {}
1913
+ : { entry: copyJson(item.entry) as InputEntry }),
1603
1914
  ...(run === undefined ? {} : { run }),
1604
1915
  others,
1605
1916
  },
1606
1917
  { ownership: { kind: "conversation" }, background: true, conversationId: id },
1607
1918
  );
1608
1919
 
1609
- return true;
1610
- }, context);
1611
- }
1920
+ interrupts.pending[input.requestId] = Number(task);
1612
1921
 
1613
- if (!moved) {
1614
- throw new HttpError(409, "That message has already gone to Pi.");
1615
- }
1922
+ return { task, started: true };
1923
+ }, admissionContext);
1616
1924
 
1617
- if (mode === "interrupt") {
1618
- await app.collab.activity(id, user, "interrupted the run");
1925
+ if (admitted !== "again") {
1926
+ return admitted;
1927
+ }
1619
1928
  }
1620
1929
  }
1621
1930
 
@@ -1,11 +1,47 @@
1
1
  /**
2
2
  * Pi's compaction settings for Pi Durable, with each model's own thresholds: Pi's `compaction.modelOverrides`, keyed
3
- * by `provider/modelId`, resolved by Pi itself (its checks, project over user settings).
3
+ * by `provider/modelId`, resolved by Pi itself (its checks, project over user settings). Where Pi's settings give no
4
+ * `reserveTokens`, each model gets room for its summary (`summaryReserve`).
4
5
  */
5
6
  import type { SettingsManager } from "@earendil-works/pi-coding-agent";
6
- import type { CompactionPolicy, CompactionSettings } from "@earendil-works/pi-durable";
7
+ import {
8
+ type CompactionPolicy,
9
+ type CompactionSettings,
10
+ DEFAULT_COMPACTION_POLICY,
11
+ } from "@earendil-works/pi-durable";
7
12
  import { describe } from "./errors.ts";
8
13
 
14
+ /** What `summaryReserve` reads of a model. */
15
+ export type ReserveModel = {
16
+ readonly provider: string;
17
+ readonly id: string;
18
+ readonly contextWindow: number;
19
+ readonly maxTokens: number;
20
+ };
21
+
22
+ /**
23
+ * The most free context kept by default. Pi Durable's summary budget is then 26,214 tokens: room for a long session's
24
+ * summary (about 16,000 tokens of output, thinking included, in the session that showed the fault), without compacting
25
+ * much earlier than before.
26
+ */
27
+ export const RESERVE_LIMIT = 32_768;
28
+
29
+ /**
30
+ * The free context kept for a model when Pi's settings give none. Pi Durable writes a summary in at most 80% of it
31
+ * (`maxTokens = 0.8 × reserveTokens`), and that budget holds the model's thinking as well as the summary. Its default,
32
+ * 16,384, gives 13,107 tokens, and a long session's summary grows past that: each one folds in the one before, so a
33
+ * session whose summary hits the limit can no longer compact, and every run after fails. This keeps room for all the
34
+ * output the model can write, up to `RESERVE_LIMIT` and a quarter of its context window, and never less than Pi
35
+ * Durable's default.
36
+ */
37
+ export function summaryReserve(model: ReserveModel): number {
38
+ const fallback = DEFAULT_COMPACTION_POLICY.reserveTokens;
39
+ const output = model.maxTokens > 0 ? Math.ceil(model.maxTokens / 0.8) : RESERVE_LIMIT;
40
+ const quarter = model.contextWindow > 0 ? Math.floor(model.contextWindow / 4) : fallback;
41
+
42
+ return Math.max(fallback, Math.min(output, RESERVE_LIMIT, quarter));
43
+ }
44
+
9
45
  /** The model names that Pi's settings (user and project, merged) give compaction values of their own. */
10
46
  function overridden(settings: SettingsManager): string[] {
11
47
  return Object.keys(settings.getSettings().compaction?.modelOverrides ?? {});
@@ -50,6 +86,7 @@ function readGeneral(
50
86
  export function compactionReader(
51
87
  settings: SettingsManager,
52
88
  warn: (message: string) => void,
89
+ models: () => Iterable<ReserveModel> = () => [],
53
90
  ): () => CompactionSettings {
54
91
  const warned = new Set<string>();
55
92
 
@@ -103,6 +140,23 @@ export function compactionReader(
103
140
  }
104
141
  }
105
142
 
143
+ // No reserve set in Pi's settings: each model gets room for its summary. One the owner gave its own keeps it.
144
+ if (settings.getSettings().compaction?.reserveTokens === undefined) {
145
+ for (const model of models()) {
146
+ const name = `${model.provider}/${model.id}`;
147
+ const own = modelOverrides[name];
148
+
149
+ if (own === undefined) {
150
+ modelOverrides[name] = { reserveTokens: summaryReserve(model) };
151
+ } else if (
152
+ settings.getSettings().compaction?.modelOverrides?.[name]?.reserveTokens ===
153
+ undefined
154
+ ) {
155
+ own.reserveTokens = summaryReserve(model);
156
+ }
157
+ }
158
+ }
159
+
106
160
  return Object.keys(modelOverrides).length === 0 ? general : { ...general, modelOverrides };
107
161
  };
108
162
  }
@@ -1,5 +1,5 @@
1
1
  /** A design's bundle, read from the artifact documents: the page, and every asset of the version. */
2
- import type { Context } from "@earendil-works/chord";
2
+ import type { Context } from "@earendil-works/chord/context";
3
3
  import type { ConversationId, DocumentReader } from "@earendil-works/pi-durable";
4
4
  import { ArtifactAssetDoc, ArtifactBodyDoc, ArtifactsDoc, type ArtifactMeta } from "../docs.ts";
5
5
  import type { PocketHost } from "../host.ts";