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