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.
- package/docs/customizing.md +1 -1
- package/docs/features.md +3 -3
- package/docs/map.md +1 -1
- package/npm-shrinkwrap.json +69 -65
- package/package.json +8 -8
- package/src/server/alerts.ts +21 -8
- package/src/server/anthropic-shape.ts +4 -3
- package/src/server/app.ts +18 -3
- package/src/server/artifact-state.ts +1 -1
- package/src/server/background.ts +1 -1
- package/src/server/cache-keeper.ts +1 -0
- package/src/server/commands.ts +386 -77
- package/src/server/compaction.ts +56 -2
- package/src/server/design/bundle.ts +1 -1
- package/src/server/docs.ts +43 -1
- package/src/server/extension-points.ts +1 -1
- package/src/server/extension-request.ts +1 -1
- package/src/server/extensions/codemode.ts +1 -1
- package/src/server/extensions/schedules.ts +1 -1
- package/src/server/extensions/subagents.ts +1 -1
- package/src/server/goals.ts +1 -1
- package/src/server/hidden.ts +1 -1
- package/src/server/host.ts +1 -1
- package/src/server/http/conversation-routes.ts +22 -0
- package/src/server/linked-sessions.ts +1 -1
- package/src/server/memory.ts +1 -1
- package/src/server/pip-compact.ts +1 -1
- package/src/server/pip.ts +9 -1
- package/src/server/public-links.ts +1 -1
- package/src/server/repo-watches.ts +1 -1
- package/src/server/resend.ts +26 -9
- package/src/server/room.ts +17 -1
- package/src/server/running.ts +3 -0
- package/src/server/schedules.ts +112 -12
- package/src/server/shell.ts +1 -1
- package/src/server/spend.ts +1 -1
- package/src/server/watches.ts +1 -1
- package/types/pocket.d.ts +1 -1
- 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/docs.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Kinds are part of the stored data: rename one and old sessions lose it.
|
|
6
6
|
*/
|
|
7
|
-
import type { JsonValue } from "@earendil-works/chord";
|
|
7
|
+
import type { JsonValue } from "@earendil-works/chord/json";
|
|
8
8
|
import {
|
|
9
9
|
type CheckpointInfo,
|
|
10
10
|
type ConversationId,
|
|
@@ -59,6 +59,44 @@ export const SessionsDoc = defineDoc<{ items: Record<string, SessionMeta> }>({
|
|
|
59
59
|
initial: () => ({ items: {} }),
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
+
/** Interrupt handoffs not yet admitted as messages: retries find the same durable task, even after a restart. */
|
|
63
|
+
export const InterruptsDoc = defineDoc<{ pending: Record<string, number> }>({
|
|
64
|
+
kind: "pocket.interrupts",
|
|
65
|
+
version: 1,
|
|
66
|
+
...rebased,
|
|
67
|
+
scope: "conversation",
|
|
68
|
+
history: "latest",
|
|
69
|
+
fork: "initial",
|
|
70
|
+
initial: () => ({ pending: {} }),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
/** A message that Stop took out of the queue: it waits, and goes to Pi only when someone sends it. */
|
|
74
|
+
export type HeldMessage = {
|
|
75
|
+
/** The id of the queued message it was. */
|
|
76
|
+
key: string;
|
|
77
|
+
/** Pi Durable's `UserInput`, as it was queued. */
|
|
78
|
+
content: JsonValue;
|
|
79
|
+
mode: "steer" | "followUp";
|
|
80
|
+
/** The request id it goes out with: its sender's, so it stays theirs, and a retry never sends it twice. */
|
|
81
|
+
requestId: string;
|
|
82
|
+
/** Pi Durable's `InputEntry`, when it had one. */
|
|
83
|
+
entry?: JsonValue;
|
|
84
|
+
/** Who wrote it, when a person did. */
|
|
85
|
+
by?: string;
|
|
86
|
+
heldAt: number;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** The messages a Stop kept from the queue, in their order (`Commands.abort`, `Commands.sendHeld`). */
|
|
90
|
+
export const HeldDoc = defineDoc<{ items: HeldMessage[] }>({
|
|
91
|
+
kind: "pocket.held",
|
|
92
|
+
version: 1,
|
|
93
|
+
...rebased,
|
|
94
|
+
scope: "conversation",
|
|
95
|
+
history: "latest",
|
|
96
|
+
fork: "initial",
|
|
97
|
+
initial: () => ({ items: [] }),
|
|
98
|
+
});
|
|
99
|
+
|
|
62
100
|
/** Favorite working folders in shortcut order, private to each person and shared by their devices. */
|
|
63
101
|
export const FolderFavoritesDoc = defineDoc<{ users: Record<string, string[]> }>({
|
|
64
102
|
kind: "pocket.folder-favorites",
|
|
@@ -498,6 +536,10 @@ export type Schedule = {
|
|
|
498
536
|
createdAt: number;
|
|
499
537
|
/** How many times it went out: each time sends with its own request id, so a restart never sends one twice. */
|
|
500
538
|
runs: number;
|
|
539
|
+
/** How many of a repeat's times sent nothing because its last message still waited in the queue (`schedules.ts`). */
|
|
540
|
+
skipped?: number;
|
|
541
|
+
/** When the newest of those was. */
|
|
542
|
+
skippedAt?: number;
|
|
501
543
|
/** The durable task that sends it. */
|
|
502
544
|
taskId: TaskId;
|
|
503
545
|
/** Set by the same call already, when a tool call is replayed: its `taskId:callId`. */
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* - `browser`: a session's browser page (`browser.ts`), with the actions an extension needs and nothing of the page's
|
|
8
8
|
* internals: no script to run in it, and a `fill` for a person's value that core never shows.
|
|
9
9
|
*/
|
|
10
|
-
import type { Context } from "@earendil-works/chord";
|
|
10
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
11
11
|
import type { Browsers } from "./browser.ts";
|
|
12
12
|
import type { BrowserPage, Target } from "./browser/page.ts";
|
|
13
13
|
import { normalizeUrl } from "./browser/urls.ts";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Request-lifetime document access and conversation authorization for extension routes. */
|
|
2
|
-
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
3
3
|
import { BACKGROUND_CONTEXT, withAbortSignal } from "@earendil-works/chord/context";
|
|
4
4
|
import type { AgentState, ConversationId, DocumentReader, Tx } from "@earendil-works/pi-durable";
|
|
5
5
|
import type { PocketApp } from "./app.ts";
|
|
@@ -9,7 +9,7 @@ import { randomBytes } from "node:crypto";
|
|
|
9
9
|
import { writeFile } from "node:fs/promises";
|
|
10
10
|
import { tmpdir } from "node:os";
|
|
11
11
|
import { join } from "node:path";
|
|
12
|
-
import type { JsonValue } from "@earendil-works/chord";
|
|
12
|
+
import type { JsonValue } from "@earendil-works/chord/json";
|
|
13
13
|
import { withAbortSignal } from "@earendil-works/chord/context";
|
|
14
14
|
import { type ToolCall } from "@earendil-works/pi-ai";
|
|
15
15
|
import { obj, str } from "@earendil-works/pi-schema";
|
|
@@ -110,7 +110,7 @@ export default function createSchedules(host: PocketHost, web: ExtensionWeb) {
|
|
|
110
110
|
execute: async (args, api) => {
|
|
111
111
|
const reply = (text: string) => ({ output: [{ type: "text" as const, text }] });
|
|
112
112
|
const line = (item: Schedule) =>
|
|
113
|
-
`${item.id}: ${item.every === undefined ? describeMoment(item.next, item.zone) : `${describeRepeat(item.every)}, next ${describeMoment(item.next, item.zone)}`} · ${item.text}`;
|
|
113
|
+
`${item.id}: ${item.every === undefined ? describeMoment(item.next, item.zone) : `${describeRepeat(item.every)}, next ${describeMoment(item.next, item.zone)}`}${(item.skipped ?? 0) > 0 ? `, skipped ${String(item.skipped)}× (last ${describeMoment(item.skippedAt ?? item.next, item.zone)}): its last message still waits in the queue` : ""} · ${item.text}`;
|
|
114
114
|
|
|
115
115
|
if (args.action === "list") {
|
|
116
116
|
const items = await host.schedules.list(api.conversationId);
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
import type { AssistantMessage, ModelThinkingLevel } from "@earendil-works/pi-ai";
|
|
18
18
|
import { StringEnum } from "@earendil-works/pi-ai";
|
|
19
19
|
import { arr, bool, obj, opt, str } from "@earendil-works/pi-schema";
|
|
20
|
-
import type { Context } from "@earendil-works/chord";
|
|
20
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
21
21
|
import {
|
|
22
22
|
AgentDoc,
|
|
23
23
|
AssistantEntry,
|
package/src/server/goals.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* extension (`extensions/goals.ts`) runs the check in the session's folder; while it fails, Pi is told so, with the
|
|
4
4
|
* end of the output, and keeps going, up to a number of checks. This module keeps the goal and runs the check.
|
|
5
5
|
*/
|
|
6
|
-
import type { Context } from "@earendil-works/chord";
|
|
6
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
7
7
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
8
8
|
import type { ConversationId, TaskId } from "@earendil-works/pi-durable";
|
|
9
9
|
import type { PocketApp } from "./app.ts";
|
package/src/server/hidden.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* keeps values out of transcripts by accident and by default: it is no boundary against a command that transforms a
|
|
14
14
|
* value on purpose.
|
|
15
15
|
*/
|
|
16
|
-
import type { Context } from "@earendil-works/chord";
|
|
16
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
17
17
|
import type { ConversationId, ToolExecutionResult } from "@earendil-works/pi-durable";
|
|
18
18
|
import type {
|
|
19
19
|
ExecutionEnv,
|
package/src/server/host.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Context } from "@earendil-works/chord";
|
|
1
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
2
2
|
import { awaitWithContext } from "@earendil-works/chord/context";
|
|
3
3
|
import type { ProviderConfig, Skill } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import type { Api, Model, ModelThinkingLevel } from "@earendil-works/pi-ai";
|
|
@@ -196,6 +196,28 @@ export async function conversationRoutes(
|
|
|
196
196
|
);
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
+
// What a Stop held: `send` them all, or `drop` or `edit` one, by its `key`.
|
|
200
|
+
if (third === "held" && method === "POST") {
|
|
201
|
+
const body = await readJson<{ action?: unknown; key?: unknown }>(request);
|
|
202
|
+
const key = String(body.key ?? "");
|
|
203
|
+
|
|
204
|
+
if (body.action === "send") {
|
|
205
|
+
return json(response, 200, { sent: await app.commands.sendHeld(id, user) });
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (body.action === "drop") {
|
|
209
|
+
await app.commands.dropHeld(id, user, key);
|
|
210
|
+
|
|
211
|
+
return json(response, 200, { ok: true });
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (body.action === "edit") {
|
|
215
|
+
return json(response, 200, await app.commands.editHeld(id, user, key));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
throw new HttpError(400, "Say send, drop, or edit.");
|
|
219
|
+
}
|
|
220
|
+
|
|
199
221
|
if (third === "move" && method === "POST") {
|
|
200
222
|
const body = await readJson<{ submissionId?: number; mode?: string }>(request);
|
|
201
223
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* at the target's next pause, and each one's final answer carried back to the session that started it.
|
|
4
4
|
*/
|
|
5
5
|
import { createHash, randomUUID } from "node:crypto";
|
|
6
|
-
import type { Context } from "@earendil-works/chord";
|
|
6
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
7
7
|
import type { AssistantMessage, ModelThinkingLevel } from "@earendil-works/pi-ai";
|
|
8
8
|
import { BACKGROUND_CONTEXT, withAbortSignal } from "@earendil-works/chord/context";
|
|
9
9
|
import {
|
package/src/server/memory.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
import { randomUUID } from "node:crypto";
|
|
11
11
|
import { realpath } from "node:fs/promises";
|
|
12
12
|
import { basename, dirname, resolve } from "node:path";
|
|
13
|
-
import type { Context } from "@earendil-works/chord";
|
|
13
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
14
14
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
15
15
|
import type { ConversationId, Tx } from "@earendil-works/pi-durable";
|
|
16
16
|
import type { PocketApp } from "./app.ts";
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* its own notes. It keeps what PiP's person asked for, what is still open, and what PiP follows and watches (which
|
|
11
11
|
* live on the server, and work on as they did). What it costs is added to `pi.usage`, once, as Pi Durable's are.
|
|
12
12
|
*/
|
|
13
|
-
import type { Context } from "@earendil-works/chord";
|
|
13
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
14
14
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
15
15
|
import type { AssistantMessage, Message, Usage } from "@earendil-works/pi-ai";
|
|
16
16
|
import {
|
package/src/server/pip.ts
CHANGED
|
@@ -17,7 +17,7 @@ import { createHash, randomUUID } from "node:crypto";
|
|
|
17
17
|
import { existsSync, mkdirSync, readdirSync, renameSync, rmSync, statSync } from "node:fs";
|
|
18
18
|
import { basename, dirname, join } from "node:path";
|
|
19
19
|
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
20
|
-
import type { Context } from "@earendil-works/chord";
|
|
20
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
21
21
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
22
22
|
import {
|
|
23
23
|
AgentDoc,
|
|
@@ -1581,6 +1581,14 @@ export class Pip {
|
|
|
1581
1581
|
/** A session's last words, for PiP: how its run ended, and Pi's last answer, fenced. */
|
|
1582
1582
|
async #lastWords(id: ConversationId, title: string, state: string): Promise<string> {
|
|
1583
1583
|
const name = `${quoted(title, 80)} (/s/${String(id)})`;
|
|
1584
|
+
// A run that failed with no answer of its own (its compaction failed) left the answer before it as the last:
|
|
1585
|
+
// the failure is what came of the run, not that answer.
|
|
1586
|
+
const failure = this.#app.runFailure(id);
|
|
1587
|
+
|
|
1588
|
+
if (failure !== undefined) {
|
|
1589
|
+
return `${name} stopped with an error: ${snippet(failure, 300)}`;
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1584
1592
|
const page = await (await this.#app.conversation(id)).entries({}, 16, undefined, context);
|
|
1585
1593
|
const last = page.items
|
|
1586
1594
|
.map((entry) => projectEntry(entry))
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* the address answers nothing.
|
|
7
7
|
*/
|
|
8
8
|
import { randomBytes } from "node:crypto";
|
|
9
|
-
import type { JsonValue } from "@earendil-works/chord";
|
|
9
|
+
import type { JsonValue } from "@earendil-works/chord/json";
|
|
10
10
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
11
11
|
import type { PocketApp } from "./app.ts";
|
|
12
12
|
import { type PublicLink, PublicLinksDoc } from "./docs.ts";
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* (`extensions/pip.ts`).
|
|
15
15
|
*/
|
|
16
16
|
import { randomUUID } from "node:crypto";
|
|
17
|
-
import type { Context } from "@earendil-works/chord";
|
|
17
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
18
18
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
19
19
|
import {
|
|
20
20
|
type ConversationId,
|
package/src/server/resend.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* the message went out cannot leave the fork without it. Whoever sends it again sends it right away as well; with one
|
|
4
4
|
* request id per fork, it goes out once either way.
|
|
5
5
|
*/
|
|
6
|
-
import type { Context } from "@earendil-works/chord";
|
|
6
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
7
7
|
import type { ImageContent, TextContent } from "@earendil-works/pi-ai";
|
|
8
8
|
import {
|
|
9
9
|
defineTask,
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
type InputEntry,
|
|
12
12
|
type UserInput,
|
|
13
13
|
} from "@earendil-works/pi-durable";
|
|
14
|
+
import { InterruptsDoc } from "./docs.ts";
|
|
14
15
|
import { CONTEXT_BLOCK, FILE_BLOCK } from "./entry-format.ts";
|
|
15
16
|
import { describe } from "./errors.ts";
|
|
16
17
|
import { ownRequest } from "./requests.ts";
|
|
@@ -100,13 +101,14 @@ export type Requeue = {
|
|
|
100
101
|
};
|
|
101
102
|
|
|
102
103
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
104
|
+
* An Interrupt: what it says, the request id it goes out with, the run it stops, and the other queued messages, in
|
|
105
|
+
* their order, which the stop would withdraw and which are queued again after it.
|
|
105
106
|
*/
|
|
106
107
|
export type Interrupt = {
|
|
107
108
|
content: UserInput;
|
|
108
109
|
requestId: string;
|
|
109
|
-
|
|
110
|
+
entry?: InputEntry;
|
|
111
|
+
/** The run going at admission: only that run is stopped, never one that came after. */
|
|
110
112
|
run?: number;
|
|
111
113
|
others?: Requeue[];
|
|
112
114
|
};
|
|
@@ -115,8 +117,8 @@ export type Interrupt = {
|
|
|
115
117
|
export type StopRun = (run: number, context: Context) => Promise<void>;
|
|
116
118
|
|
|
117
119
|
/**
|
|
118
|
-
* Stops the run, then sends
|
|
119
|
-
*
|
|
120
|
+
* Stops the run, then sends the message stored by the commit making this task, so a restart in between cannot lose
|
|
121
|
+
* it, and puts the other messages that were queued back. Every message has a request id, so a
|
|
120
122
|
* restart at any point sends each once. Background, so the stop does not stop this task too. It stops the run it was
|
|
121
123
|
* made for by its id (`stopRun`, from the app's harness), never whichever run is going: one that started after it ended
|
|
122
124
|
* goes on.
|
|
@@ -128,7 +130,7 @@ export const interruptTask = (stopRun: StopRun) =>
|
|
|
128
130
|
initial: () => ({ phase: "send" }),
|
|
129
131
|
phases: {
|
|
130
132
|
send: async (task, runtime, context) => {
|
|
131
|
-
const { content, requestId, run, others } = task.input;
|
|
133
|
+
const { content, requestId, entry, run, others } = task.input;
|
|
132
134
|
const id = runtime.conversationId;
|
|
133
135
|
let sent = false;
|
|
134
136
|
|
|
@@ -148,7 +150,17 @@ export const interruptTask = (stopRun: StopRun) =>
|
|
|
148
150
|
await stopRun(run, context);
|
|
149
151
|
}
|
|
150
152
|
|
|
151
|
-
await
|
|
153
|
+
await runtime.commit(async (tx) => {
|
|
154
|
+
await conversation?.submit(tx, {
|
|
155
|
+
type: "input",
|
|
156
|
+
content,
|
|
157
|
+
requestId,
|
|
158
|
+
...(entry === undefined ? {} : { entry }),
|
|
159
|
+
});
|
|
160
|
+
const interrupts = await tx.doc(InterruptsDoc, id);
|
|
161
|
+
|
|
162
|
+
delete interrupts.pending[requestId];
|
|
163
|
+
}, context);
|
|
152
164
|
} catch (error) {
|
|
153
165
|
if (context.abortSignal?.aborted === true) {
|
|
154
166
|
throw error;
|
|
@@ -186,7 +198,12 @@ export const interruptTask = (stopRun: StopRun) =>
|
|
|
186
198
|
}
|
|
187
199
|
}
|
|
188
200
|
|
|
189
|
-
|
|
201
|
+
// A message that was not sent leaves no receipt: a retry of its request makes a new handoff.
|
|
202
|
+
await runtime.commit(async (tx) => {
|
|
203
|
+
delete (await tx.doc(InterruptsDoc, id)).pending[requestId];
|
|
204
|
+
|
|
205
|
+
return finished;
|
|
206
|
+
}, context);
|
|
190
207
|
},
|
|
191
208
|
},
|
|
192
209
|
abort: (_task, runtime, context) =>
|
package/src/server/room.ts
CHANGED
|
@@ -28,6 +28,8 @@ import {
|
|
|
28
28
|
DecisionsDoc,
|
|
29
29
|
type Goal,
|
|
30
30
|
GoalDoc,
|
|
31
|
+
HeldDoc,
|
|
32
|
+
type HeldMessage,
|
|
31
33
|
LinkedSessionPeersDoc,
|
|
32
34
|
type Notes,
|
|
33
35
|
NotesDoc,
|
|
@@ -88,6 +90,7 @@ export const ROOM_DOCS = new Set(
|
|
|
88
90
|
GoalDoc,
|
|
89
91
|
WatchDoc,
|
|
90
92
|
LinkedSessionPeersDoc,
|
|
93
|
+
HeldDoc,
|
|
91
94
|
].map((doc) => doc.definition.kind),
|
|
92
95
|
);
|
|
93
96
|
/** A typing indicator lasts this long unless the browser renews it. */
|
|
@@ -380,6 +383,8 @@ export class Room {
|
|
|
380
383
|
decisions: Record<string, Decision> = {};
|
|
381
384
|
plan: { on: boolean; by?: string; at?: number } = { on: false };
|
|
382
385
|
schedules: Record<string, Schedule> = {};
|
|
386
|
+
/** The messages a Stop held out of the queue, in their order. */
|
|
387
|
+
held: HeldMessage[] = [];
|
|
383
388
|
goal: Goal | undefined;
|
|
384
389
|
watch: PrWatch | undefined;
|
|
385
390
|
/** The sessions this one knows by an address (`linked-sessions.ts`): whom each linked session's message is from. */
|
|
@@ -460,6 +465,7 @@ export class Room {
|
|
|
460
465
|
this.schedules = {
|
|
461
466
|
...((await harness.snapshot(ScheduleDoc, this.id, context))?.items ?? {}),
|
|
462
467
|
};
|
|
468
|
+
this.held = [...((await harness.snapshot(HeldDoc, this.id, context))?.items ?? [])];
|
|
463
469
|
this.goal = (await harness.snapshot(GoalDoc, this.id, context))?.goal;
|
|
464
470
|
this.watch = (await harness.snapshot(WatchDoc, this.id, context))?.watch;
|
|
465
471
|
this.peers = {
|
|
@@ -532,6 +538,8 @@ export class Room {
|
|
|
532
538
|
this.plan = { on: false, ...((value as Room["plan"] | null) ?? {}) };
|
|
533
539
|
} else if (kind === ScheduleDoc.definition.kind) {
|
|
534
540
|
this.schedules = { ...((value?.items as Record<string, Schedule>) ?? {}) };
|
|
541
|
+
} else if (kind === HeldDoc.definition.kind) {
|
|
542
|
+
this.held = [...((value?.items as HeldMessage[] | undefined) ?? [])];
|
|
535
543
|
} else if (kind === GoalDoc.definition.kind) {
|
|
536
544
|
this.goal = (value?.goal as Goal | undefined) ?? undefined;
|
|
537
545
|
} else if (kind === WatchDoc.definition.kind) {
|
|
@@ -846,14 +854,22 @@ export class Room {
|
|
|
846
854
|
plan: this.plan,
|
|
847
855
|
schedules: Object.values(this.schedules)
|
|
848
856
|
.sort((a, b) => a.next - b.next)
|
|
849
|
-
.map(({ id, text, next, every, by, runs }) => ({
|
|
857
|
+
.map(({ id, text, next, every, by, runs, skipped, skippedAt }) => ({
|
|
850
858
|
id,
|
|
851
859
|
text,
|
|
852
860
|
next,
|
|
853
861
|
...(every === undefined ? {} : { repeat: describeRepeat(every) }),
|
|
854
862
|
...(by === undefined ? {} : { by }),
|
|
855
863
|
runs,
|
|
864
|
+
...(skipped === undefined ? {} : { skipped, skippedAt }),
|
|
856
865
|
})),
|
|
866
|
+
// What a Stop held: each message's start and who wrote it, as the queue shows them.
|
|
867
|
+
held: this.held.map((item) => ({
|
|
868
|
+
key: item.key,
|
|
869
|
+
mode: item.mode,
|
|
870
|
+
...(item.by === undefined ? {} : { by: item.by }),
|
|
871
|
+
text: cutQueued(queuedText(item.content)),
|
|
872
|
+
})),
|
|
857
873
|
// The branch is read from the repository each time, so one Pi or a person switched to shows on the next update.
|
|
858
874
|
branch: this.#readHead(),
|
|
859
875
|
goal:
|
package/src/server/running.ts
CHANGED
|
@@ -78,6 +78,8 @@ export type BoardSchedule = {
|
|
|
78
78
|
requestedBy?: string;
|
|
79
79
|
createdAt: number;
|
|
80
80
|
runs: number;
|
|
81
|
+
/** How many times it sent nothing because its last message still waited in the queue (`schedules.ts`). */
|
|
82
|
+
skipped?: number;
|
|
81
83
|
/** The next times it goes out, the first being `next`: up to `UPCOMING`, within `UPCOMING_MS`. */
|
|
82
84
|
upcoming: number[];
|
|
83
85
|
/** Why it would not go out now (a spend limit, or nobody left to steer), when it would not. */
|
|
@@ -525,6 +527,7 @@ export async function runningBoard(app: PocketApp, user: User): Promise<RunningB
|
|
|
525
527
|
: { requestedBy: schedule.requestedBy }),
|
|
526
528
|
createdAt: schedule.createdAt,
|
|
527
529
|
runs: schedule.runs,
|
|
530
|
+
...(schedule.skipped === undefined ? {} : { skipped: schedule.skipped }),
|
|
528
531
|
upcoming: upcomingOf(schedule.id, schedule.next, schedule.every, schedule.zone),
|
|
529
532
|
...(held === undefined ? {} : { held }),
|
|
530
533
|
});
|
package/src/server/schedules.ts
CHANGED
|
@@ -3,13 +3,15 @@
|
|
|
3
3
|
* its message, and for a repeat goes back to sleep until the next time. A restart while it sleeps continues the
|
|
4
4
|
* sleep, and one that comes after the time sends at once. Each sending has its own request id, so a restart in the
|
|
5
5
|
* middle of one never sends twice. A schedule goes out only while its person may still steer the session and no
|
|
6
|
-
* spend limit is reached; otherwise it is cancelled, with a line in the chat saying why.
|
|
6
|
+
* spend limit is reached; otherwise it is cancelled, with a line in the chat saying why. A repeat whose last message
|
|
7
|
+
* still waits in the session's queue sends no other: that time is skipped, counted on the schedule, and said in the
|
|
8
|
+
* chat, so messages do not pile up behind a session that cannot answer. One in PiP's conversation
|
|
7
9
|
* waits while PiP is turned off, and goes out once it is back. The schedules extension
|
|
8
10
|
* (`extensions/schedules.ts`) installs the task and gives Pi a tool to schedule its own follow-ups; while it is off,
|
|
9
11
|
* nothing goes out.
|
|
10
12
|
*/
|
|
11
13
|
import { randomUUID } from "node:crypto";
|
|
12
|
-
import type { Context } from "@earendil-works/chord";
|
|
14
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
13
15
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
14
16
|
import {
|
|
15
17
|
type ConversationId,
|
|
@@ -19,7 +21,7 @@ import {
|
|
|
19
21
|
} from "@earendil-works/pi-durable";
|
|
20
22
|
import type { PocketApp } from "./app.ts";
|
|
21
23
|
import { addActivity } from "./collab.ts";
|
|
22
|
-
import { ChatDoc, type Schedule, ScheduleDoc } from "./docs.ts";
|
|
24
|
+
import { ChatDoc, type ChatMessage, type Schedule, ScheduleDoc } from "./docs.ts";
|
|
23
25
|
import { describe, HttpError } from "./errors.ts";
|
|
24
26
|
import { FROM_PREFIX } from "./entry-format.ts";
|
|
25
27
|
import { snippet } from "./projection.ts";
|
|
@@ -69,6 +71,51 @@ async function stored(
|
|
|
69
71
|
|
|
70
72
|
const finished = { status: "terminal", outcome: { status: "completed", result: null } } as const;
|
|
71
73
|
|
|
74
|
+
/** Who the chat names for a skipped time: no person did it. */
|
|
75
|
+
const SCHEDULER = { id: "pi", name: "Schedules" };
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Say in the chat that a schedule skipped a time. Times are 10 minutes apart at least, so a session that cannot take
|
|
79
|
+
* its messages for hours would get a line for each: while nothing else is said, the last line counts them instead.
|
|
80
|
+
*/
|
|
81
|
+
function sayWhatWasSkipped(chat: { messages: ChatMessage[] }, text: string, now: number): void {
|
|
82
|
+
const what = `skipped “${snippet(text, 60)}”`;
|
|
83
|
+
const why = "its last message still waits in the queue.";
|
|
84
|
+
const last = chat.messages.at(-1);
|
|
85
|
+
const times = /^skipped “.*” (\d+) times:/s.exec(last?.text ?? "");
|
|
86
|
+
|
|
87
|
+
if (
|
|
88
|
+
last?.kind === "event" &&
|
|
89
|
+
last.userId === SCHEDULER.id &&
|
|
90
|
+
last.name === SCHEDULER.name &&
|
|
91
|
+
last.text.startsWith(`${what}`) &&
|
|
92
|
+
last.text.endsWith(why)
|
|
93
|
+
) {
|
|
94
|
+
const count = (times === null ? 1 : Number(times[1])) + 1;
|
|
95
|
+
|
|
96
|
+
last.text = `${what} ${count} times: ${why}`;
|
|
97
|
+
last.at = now;
|
|
98
|
+
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
addActivity(chat, SCHEDULER, `${what}: ${why}`, now);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The request id of a schedule's sending number `run`. Sent as the person who set it up, so it shows as theirs. Pi's own
|
|
107
|
+
* are sent as Pi's, but as the work of whom Pi worked for when it set them up.
|
|
108
|
+
*/
|
|
109
|
+
function sendingRequest(schedule: Schedule, run: number): string {
|
|
110
|
+
const sending = `${String(schedule.taskId)}-${run}`;
|
|
111
|
+
|
|
112
|
+
return schedule.by !== undefined
|
|
113
|
+
? ownRequest(schedule.by, `schedule-${sending}`)
|
|
114
|
+
: schedule.requestedBy !== undefined
|
|
115
|
+
? requestFor(schedule.requestedBy, `schedule-${sending}`)
|
|
116
|
+
: `schedule:${sending}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
72
119
|
export type ScheduleRequest = {
|
|
73
120
|
/** When it goes out (`in 30m`, `every weekday 8:00`, …), followed by the message unless `text` is given. */
|
|
74
121
|
when: string;
|
|
@@ -148,16 +195,19 @@ export class Schedules {
|
|
|
148
195
|
}, context);
|
|
149
196
|
}
|
|
150
197
|
|
|
151
|
-
|
|
152
|
-
// worked for when it set them up.
|
|
153
|
-
const sending = `${String(schedule.taskId)}-${schedule.runs}`;
|
|
154
|
-
const requestId =
|
|
155
|
-
schedule.by !== undefined
|
|
156
|
-
? ownRequest(schedule.by, `schedule-${sending}`)
|
|
157
|
-
: schedule.requestedBy !== undefined
|
|
158
|
-
? requestFor(schedule.requestedBy, `schedule-${sending}`)
|
|
159
|
-
: `schedule:${sending}`;
|
|
198
|
+
const requestId = sendingRequest(schedule, schedule.runs);
|
|
160
199
|
const stopped = this.#stopped(conversationId, schedule);
|
|
200
|
+
// Its last message, not yet in the transcript: the session has not taken it, so another would only wait too.
|
|
201
|
+
const waiting =
|
|
202
|
+
stopped === undefined &&
|
|
203
|
+
schedule.runs > 0 &&
|
|
204
|
+
(await this.#queued(runtime, sendingRequest(schedule, schedule.runs - 1), context)) &&
|
|
205
|
+
// Run again after a crash, this phase may find its own message sent already: that one counts as sent.
|
|
206
|
+
!(await this.#queued(runtime, requestId, context, true));
|
|
207
|
+
|
|
208
|
+
if (waiting) {
|
|
209
|
+
return this.#skip(id, runtime, context);
|
|
210
|
+
}
|
|
161
211
|
|
|
162
212
|
if (stopped === undefined) {
|
|
163
213
|
await conversation.submit(
|
|
@@ -204,6 +254,56 @@ export class Schedules {
|
|
|
204
254
|
}, context);
|
|
205
255
|
}
|
|
206
256
|
|
|
257
|
+
/**
|
|
258
|
+
* Whether the message sent with `requestId` waits in the conversation's queue, not yet taken into a run. `any`: whether
|
|
259
|
+
* it was sent at all, in any state.
|
|
260
|
+
*/
|
|
261
|
+
async #queued(
|
|
262
|
+
runtime: Runtime,
|
|
263
|
+
requestId: string,
|
|
264
|
+
context: Context,
|
|
265
|
+
any = false,
|
|
266
|
+
): Promise<boolean> {
|
|
267
|
+
const conversationId = runtime.conversationId;
|
|
268
|
+
const record = await this.#app.harness.commit(
|
|
269
|
+
(tx) => tx.submissionByRequest(conversationId, requestId),
|
|
270
|
+
context,
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
return record !== undefined && (any || record.status === "queued");
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Skip this time of a repeat whose last message still waits in the queue: count it on the schedule, say so in the
|
|
278
|
+
* chat (one line, updated while the skips go on), and wait for the next time. `runs` stays, so the message that
|
|
279
|
+
* waits keeps its request id and the next check finds it.
|
|
280
|
+
*/
|
|
281
|
+
async #skip(id: string, runtime: Runtime, context: Context): Promise<void> {
|
|
282
|
+
const conversationId = runtime.conversationId;
|
|
283
|
+
|
|
284
|
+
await runtime.commit(async (tx) => {
|
|
285
|
+
const doc = await tx.doc(ScheduleDoc, conversationId);
|
|
286
|
+
const item = doc.items[id];
|
|
287
|
+
|
|
288
|
+
if (item === undefined) {
|
|
289
|
+
return finished;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const now = runtime.now();
|
|
293
|
+
|
|
294
|
+
item.skipped = (item.skipped ?? 0) + 1;
|
|
295
|
+
item.skippedAt = now;
|
|
296
|
+
// A one-time message has no next time; it never has a message of its own waiting either.
|
|
297
|
+
item.next = item.every === undefined ? now : nextRepeat(item.every, now, item.zone);
|
|
298
|
+
sayWhatWasSkipped(await tx.doc(ChatDoc, conversationId), item.text, now);
|
|
299
|
+
|
|
300
|
+
return { status: "running", checkpoint: { phase: "wait" } };
|
|
301
|
+
}, context);
|
|
302
|
+
this.#app.log(
|
|
303
|
+
`Schedule ${id} in session ${String(conversationId)} skipped a time: its last message still waits in the queue.`,
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
|
|
207
307
|
/** Whether a conversation is a PiP whose person cannot have it now (its extension is off). */
|
|
208
308
|
#waitsForPip(conversationId: ConversationId): boolean {
|
|
209
309
|
const app = this.#app;
|
package/src/server/shell.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* done part of its work, so its entry says it was cut off. The entry goes in as a write submission, which Pi Durable
|
|
6
6
|
* queues while Pi works and places where a message can go.
|
|
7
7
|
*/
|
|
8
|
-
import type { Context } from "@earendil-works/chord";
|
|
8
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
9
9
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
10
10
|
import type { Message } from "@earendil-works/pi-ai";
|
|
11
11
|
import {
|
package/src/server/spend.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* the work's usage is committed. The owner can limit a session or a person. Past a limit, new messages are refused,
|
|
5
5
|
* and a run that crosses it is stopped; the model request that crossed it still counts.
|
|
6
6
|
*/
|
|
7
|
-
import { copyJson } from "@earendil-works/chord";
|
|
7
|
+
import { copyJson } from "@earendil-works/chord/json";
|
|
8
8
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
9
9
|
import type { AssistantMessage, Usage } from "@earendil-works/pi-ai";
|
|
10
10
|
import { type ConversationId, UsageDoc, type UsageState } from "@earendil-works/pi-durable";
|
package/src/server/watches.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* or closed, when there is more to tell than it may send, after two weeks with nothing new, after an hour of failed
|
|
11
11
|
* looks, or when someone stops it. The triggers extension (`extensions/triggers.ts`) installs the task and Pi's tool.
|
|
12
12
|
*/
|
|
13
|
-
import type { Context } from "@earendil-works/chord";
|
|
13
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
14
14
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
15
15
|
import {
|
|
16
16
|
type ConversationId,
|
package/types/pocket.d.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* release. A member without it is stable. The reference is in `docs/extensions/reference/`. `test/public-types.ts`
|
|
14
14
|
* fails when this file and Pi Pocket's code do not agree.
|
|
15
15
|
*/
|
|
16
|
-
import type { Context } from "@earendil-works/chord";
|
|
16
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
17
17
|
import type { Api, Model, ModelThinkingLevel } from "@earendil-works/pi-ai";
|
|
18
18
|
import type { ProviderConfig, Skill } from "@earendil-works/pi-coding-agent";
|
|
19
19
|
import type {
|