ocean-crew 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +156 -2
- package/THIRD_PARTY_NOTICES.md +159 -0
- package/client.js +31389 -0
- package/cordis.patch.yml +6 -0
- package/dist/host/chat/btw.js +436 -0
- package/dist/host/chat/channel.js +1241 -0
- package/dist/host/chat/chat-index.js +344 -0
- package/dist/host/chat/delivery.js +231 -0
- package/dist/host/chat/feed.js +165 -0
- package/dist/host/chat/handoffs.js +625 -0
- package/dist/host/chat/parts.js +2033 -0
- package/dist/host/chat/requests.js +155 -0
- package/dist/host/chat/resume.js +364 -0
- package/dist/host/chat/side.js +457 -0
- package/dist/host/chat/steer.js +346 -0
- package/dist/host/chat/turns.js +1457 -0
- package/dist/host/connectors/catalog.js +331 -0
- package/dist/host/connectors/gateway.js +339 -0
- package/dist/host/connectors/index.js +1235 -0
- package/dist/host/connectors/mail/index.js +912 -0
- package/dist/host/connectors/mail/presets.js +53 -0
- package/dist/host/connectors/mail/transport.js +519 -0
- package/dist/host/connectors/oauth.js +337 -0
- package/dist/host/core/constants.js +123 -0
- package/dist/host/core/diagnostics.js +310 -0
- package/dist/host/core/dsh-update.js +212 -0
- package/dist/host/core/events.js +16 -0
- package/dist/host/core/files.js +37 -0
- package/dist/host/core/folders.js +283 -0
- package/dist/host/core/home-lock.js +137 -0
- package/dist/host/core/links.js +52 -0
- package/dist/host/core/runtime.js +5 -0
- package/dist/host/core/store.js +662 -0
- package/dist/host/core/text.js +59 -0
- package/dist/host/core/thinking.js +60 -0
- package/dist/host/core/time-context.js +69 -0
- package/dist/host/core/update.js +466 -0
- package/dist/host/groups/calls.js +684 -0
- package/dist/host/groups/index.js +79 -0
- package/dist/host/groups/log.js +334 -0
- package/dist/host/groups/prompt.js +124 -0
- package/dist/host/groups/relay.js +489 -0
- package/dist/host/groups/round.js +992 -0
- package/dist/host/groups/tasks.js +217 -0
- package/dist/host/groups/tools.js +478 -0
- package/dist/host/groups/types.js +8 -0
- package/dist/host/im/format.js +228 -0
- package/dist/host/im/index.js +2461 -0
- package/dist/host/im/platforms/feishu.js +1134 -0
- package/dist/host/im/platforms/index.js +6 -0
- package/dist/host/im/platforms/media-type.js +70 -0
- package/dist/host/im/platforms/weixin.js +1117 -0
- package/dist/host/im/reaction.js +51 -0
- package/dist/host/im/supervisor.js +56 -0
- package/dist/host/team/agents.js +161 -0
- package/dist/host/team/models.js +427 -0
- package/dist/host/team/permissions.js +263 -0
- package/dist/host/team/privacy.js +108 -0
- package/dist/host/team/prompt.js +86 -0
- package/dist/host/team/providers.js +461 -0
- package/dist/host/team/roster.js +1011 -0
- package/dist/host/team/tools.js +1638 -0
- package/dist/host/work/approvals.js +290 -0
- package/dist/host/work/avatar-library.js +155 -0
- package/dist/host/work/avatar.js +132 -0
- package/dist/host/work/capabilities.js +862 -0
- package/dist/host/work/companion.js +306 -0
- package/dist/host/work/goal-skills.js +91 -0
- package/dist/host/work/goals.js +1337 -0
- package/dist/host/work/images.js +283 -0
- package/dist/host/work/learning.js +1021 -0
- package/dist/host/work/memory-pending.js +108 -0
- package/dist/host/work/memory.js +3237 -0
- package/dist/host/work/notify.js +146 -0
- package/dist/host/work/reset.js +43 -0
- package/dist/host/work/schedules.js +309 -0
- package/dist/host/work/secrets.js +702 -0
- package/dist/host/work/steward.js +166 -0
- package/dist/host/work/tts.js +192 -0
- package/dist/host/work/usage.js +84 -0
- package/dist/index.js +137 -0
- package/dist/shared/api.js +32 -0
- package/dist/shared/companion.js +110 -0
- package/dist/shared/config.js +3 -0
- package/dist/shared/coordination.js +6 -0
- package/dist/shared/group-replies.js +95 -0
- package/dist/shared/im-platforms.js +35 -0
- package/dist/shared/im.js +3 -0
- package/dist/shared/mentions.js +85 -0
- package/dist/shared/state.js +25 -0
- package/package.json +81 -5
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
// The recoverable side of a group's calls: the batch a Bot's "@" opened, the summary it
|
|
2
|
+
// still owes once every callee settled, the replay of a group_relay call by its callId,
|
|
3
|
+
// and the member/summary budgets kept apart from the idle ping-pong guard.
|
|
4
|
+
//
|
|
5
|
+
// The in-memory CallBatch stays the live object a round mutates; every change is also
|
|
6
|
+
// written into the room in state.json, so a restart can go on with a pending summary or
|
|
7
|
+
// fail an interrupted call out loud instead of losing it.
|
|
8
|
+
import { randomUUID } from "node:crypto";
|
|
9
|
+
import { headOf } from "../core/text.js";
|
|
10
|
+
import { asRoom } from "./types.js";
|
|
11
|
+
// Bounded retention: a room keeps the recent calls, never an unresolved one.
|
|
12
|
+
const BATCHES_KEPT = 24;
|
|
13
|
+
const RELAYS_KEPT = 12;
|
|
14
|
+
// What state.json keeps of a result for a restart: the group log keeps it whole.
|
|
15
|
+
const RESULT_TEXT_KEPT = 800;
|
|
16
|
+
const REPLAY_TEXT_KEPT = 1000;
|
|
17
|
+
const LATE_GOALS_KEPT = 20;
|
|
18
|
+
const SUMMARY_ATTEMPTS_MAX = 3;
|
|
19
|
+
const SUMMARY_TEXT_KEPT = 300;
|
|
20
|
+
// A saved summary that names a caller the group lost fails instead of waiting forever.
|
|
21
|
+
const goneCaller = "the member that called them is gone";
|
|
22
|
+
export function installCalls(host) {
|
|
23
|
+
const { rt } = host;
|
|
24
|
+
const now = () => host.now();
|
|
25
|
+
// One per host process: a record with another writer was left by an earlier process.
|
|
26
|
+
const writer = randomUUID();
|
|
27
|
+
const table = (room) => {
|
|
28
|
+
room.calls ??= {};
|
|
29
|
+
return room.calls;
|
|
30
|
+
};
|
|
31
|
+
// The goal as the member was called sees it. A result that comes back later answers
|
|
32
|
+
// this revision; the goal may have moved on since.
|
|
33
|
+
host.goalAt = (roomId) => {
|
|
34
|
+
const saved = rt.roomOf(roomId);
|
|
35
|
+
if (saved === undefined)
|
|
36
|
+
return undefined;
|
|
37
|
+
const goal = asRoom(saved).goal;
|
|
38
|
+
if (goal === undefined)
|
|
39
|
+
return undefined;
|
|
40
|
+
const at = {
|
|
41
|
+
...(typeof goal.id === "string" && goal.id !== "" ? { id: goal.id } : {}),
|
|
42
|
+
...(typeof goal.revision === "number" ? { revision: goal.revision } : {}),
|
|
43
|
+
...(typeof goal.objectiveRevision === "number" ? { objectiveRevision: goal.objectiveRevision } : {}),
|
|
44
|
+
};
|
|
45
|
+
return Object.keys(at).length === 0 ? undefined : at;
|
|
46
|
+
};
|
|
47
|
+
host.batchKeyOf = (roomId, botId) => `${roomId}:${botId}`;
|
|
48
|
+
const notice = (room, text) => {
|
|
49
|
+
host.appendRoom(room, { who: "Ocean Crew", kind: "notice", reason: "calls", text });
|
|
50
|
+
};
|
|
51
|
+
// Save one live batch into the room. The in-memory Set is not saved: state.json keeps
|
|
52
|
+
// the callees still working as a plain list.
|
|
53
|
+
host.saveBatch = (room, batch) => {
|
|
54
|
+
const kept = table(room);
|
|
55
|
+
const previous = kept[batch.key];
|
|
56
|
+
const summary = batch.summary;
|
|
57
|
+
kept[batch.key] = {
|
|
58
|
+
key: batch.key,
|
|
59
|
+
roomId: batch.roomId,
|
|
60
|
+
...(batch.callId !== undefined ? { callId: batch.callId } : {}),
|
|
61
|
+
by: batch.by,
|
|
62
|
+
callees: [...batch.callees],
|
|
63
|
+
pending: [...batch.pending],
|
|
64
|
+
done: [...batch.done],
|
|
65
|
+
worked: batch.worked,
|
|
66
|
+
answered: batch.answered,
|
|
67
|
+
...(batch.goal !== undefined ? { goal: batch.goal } : {}),
|
|
68
|
+
state: batch.state ?? previous?.state ?? "open",
|
|
69
|
+
createdAt: previous?.createdAt ?? now(),
|
|
70
|
+
updatedAt: now(),
|
|
71
|
+
writer,
|
|
72
|
+
...(batch.pending.size === 0 ? { completedAt: previous?.completedAt ?? now() } : {}),
|
|
73
|
+
...(batch.error !== undefined ? { error: batch.error } : {}),
|
|
74
|
+
...(previous?.results !== undefined ? { results: previous.results } : {}),
|
|
75
|
+
...(previous?.resultsNoted !== undefined ? { resultsNoted: previous.resultsNoted } : {}),
|
|
76
|
+
...(summary !== undefined ? { summary: { ...summary } } : {}),
|
|
77
|
+
};
|
|
78
|
+
pruneBatches(room);
|
|
79
|
+
void rt.save().catch(() => { });
|
|
80
|
+
};
|
|
81
|
+
// The result itself, kept with the batch: a restart that finds a pending summary must
|
|
82
|
+
// still know what it is summarizing.
|
|
83
|
+
host.noteBatchResult = (room, batch, botId, name, text) => {
|
|
84
|
+
const record = table(room)[batch.key] ?? {
|
|
85
|
+
key: batch.key,
|
|
86
|
+
roomId: batch.roomId,
|
|
87
|
+
by: batch.by,
|
|
88
|
+
callees: [...batch.callees],
|
|
89
|
+
pending: [...batch.pending],
|
|
90
|
+
done: [...batch.done],
|
|
91
|
+
worked: batch.worked,
|
|
92
|
+
answered: batch.answered,
|
|
93
|
+
state: batch.state ?? "open",
|
|
94
|
+
createdAt: now(),
|
|
95
|
+
updatedAt: now(),
|
|
96
|
+
};
|
|
97
|
+
const results = record.results ?? [];
|
|
98
|
+
record.results = [
|
|
99
|
+
...results.filter((result) => result.botId !== botId),
|
|
100
|
+
{ botId, name, text: headOf(text, RESULT_TEXT_KEPT), at: now() },
|
|
101
|
+
];
|
|
102
|
+
record.updatedAt = now();
|
|
103
|
+
table(room)[record.key] = record;
|
|
104
|
+
void rt.save().catch(() => { });
|
|
105
|
+
};
|
|
106
|
+
const pruneBatches = (room) => {
|
|
107
|
+
const kept = table(room);
|
|
108
|
+
const entries = Object.values(kept).sort((left, right) => left.updatedAt - right.updatedAt);
|
|
109
|
+
for (const entry of entries.slice(0, Math.max(0, entries.length - BATCHES_KEPT))) {
|
|
110
|
+
// An unresolved call is never dropped silently; it is failed first.
|
|
111
|
+
if (entry.state === "open" || entry.state === "awaiting-summary")
|
|
112
|
+
continue;
|
|
113
|
+
delete kept[entry.key];
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
// The batch every callee list is held in, so a late reply finds it after a restart of
|
|
117
|
+
// the in-memory maps (the saved record is then the only copy).
|
|
118
|
+
const batchOf = (room, key) => table(room)[key];
|
|
119
|
+
// Settle the batch once every callee has. A worked batch whose callers were not
|
|
120
|
+
// answered by a callee still owes the caller's final summary: that is the
|
|
121
|
+
// `awaiting-summary` state the user-visible result reaches `delivered` from.
|
|
122
|
+
host.settleBatch = (room, batch) => {
|
|
123
|
+
if (batch.pending.size > 0) {
|
|
124
|
+
batch.state = "open";
|
|
125
|
+
host.saveBatch(room, batch);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
batch.state ??= "open";
|
|
129
|
+
const needsSummary = batch.done.length > 0 && batch.worked && !batch.answered;
|
|
130
|
+
if (needsSummary) {
|
|
131
|
+
batch.state = "awaiting-summary";
|
|
132
|
+
batch.summary = {
|
|
133
|
+
...(batch.summary ?? {}),
|
|
134
|
+
state: batch.summary?.state === "posted" ? "posted" : "pending",
|
|
135
|
+
attempts: batch.summary?.attempts ?? 0,
|
|
136
|
+
...(batch.summary?.text !== undefined ? { text: batch.summary.text } : {}),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
else if (batch.done.length > 0 || batch.answered) {
|
|
140
|
+
// A callee that answered the caller, or a chat that did no tool work, is
|
|
141
|
+
// already in the group: there is nothing left to summarize for the user.
|
|
142
|
+
batch.state = "delivered";
|
|
143
|
+
batch.summary = {
|
|
144
|
+
state: "posted",
|
|
145
|
+
at: now(),
|
|
146
|
+
attempts: batch.summary?.attempts ?? 0,
|
|
147
|
+
...(batch.summary?.text !== undefined ? { text: batch.summary.text } : {}),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
batch.state = "failed";
|
|
152
|
+
batch.error = "no member of the call finished the work";
|
|
153
|
+
batch.summary = { state: "failed", at: now(), attempts: batch.summary?.attempts ?? 0, error: batch.error };
|
|
154
|
+
}
|
|
155
|
+
host.saveBatch(room, batch);
|
|
156
|
+
};
|
|
157
|
+
// Whether the batch still owes a summary to the user.
|
|
158
|
+
const owesSummary = (record) => record.state === "awaiting-summary" && record.summary?.state !== "posted" && record.summary?.state !== "failed";
|
|
159
|
+
// Ask the caller for its final summary. One round asks once for the same batch; a
|
|
160
|
+
// later round, or a restarted host, may ask again up to a hard limit, then the batch
|
|
161
|
+
// fails out loud instead of staying in the ledger forever.
|
|
162
|
+
host.askSummary = (room, round, batch, note) => {
|
|
163
|
+
if (batch.pending.size > 0)
|
|
164
|
+
return false;
|
|
165
|
+
if (!(batch.done.length > 0 && batch.worked && !batch.answered)) {
|
|
166
|
+
host.settleBatch(room, batch);
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
const record = batchOf(room, batch.key) ?? {
|
|
170
|
+
key: batch.key,
|
|
171
|
+
roomId: batch.roomId,
|
|
172
|
+
by: batch.by,
|
|
173
|
+
callees: [...batch.callees],
|
|
174
|
+
pending: [...batch.pending],
|
|
175
|
+
done: [...batch.done],
|
|
176
|
+
worked: batch.worked,
|
|
177
|
+
answered: batch.answered,
|
|
178
|
+
state: "awaiting-summary",
|
|
179
|
+
createdAt: now(),
|
|
180
|
+
updatedAt: now(),
|
|
181
|
+
};
|
|
182
|
+
const summary = record.summary;
|
|
183
|
+
if (summary?.state === "requested" && summary.requestedIn === round.callId)
|
|
184
|
+
return false;
|
|
185
|
+
const attempts = (summary?.attempts ?? 0) + 1;
|
|
186
|
+
const caller = rt.botOf(record.by);
|
|
187
|
+
if (caller === undefined || !room.members.includes(caller.id)) {
|
|
188
|
+
failSummary(room, record, goneCaller);
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
if (!host.allowSummary(room)) {
|
|
192
|
+
failSummary(room, record, "the group's summary budget is used up");
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
if (attempts > SUMMARY_ATTEMPTS_MAX) {
|
|
196
|
+
failSummary(room, record, `the summary was asked ${SUMMARY_ATTEMPTS_MAX} times and never posted`);
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
host.chargeWork(room, "summary");
|
|
200
|
+
record.state = "awaiting-summary";
|
|
201
|
+
record.writer = writer;
|
|
202
|
+
record.summary = {
|
|
203
|
+
state: "requested",
|
|
204
|
+
at: now(),
|
|
205
|
+
by: caller.id,
|
|
206
|
+
attempts,
|
|
207
|
+
...(round.callId !== undefined ? { requestedIn: round.callId } : {}),
|
|
208
|
+
};
|
|
209
|
+
batch.state = "awaiting-summary";
|
|
210
|
+
batch.summary = record.summary;
|
|
211
|
+
table(room)[record.key] = record;
|
|
212
|
+
const goalNote = record.goal?.revision !== undefined
|
|
213
|
+
? ` The results were asked for at goal revision ${record.goal.revision}${goalMoved(room, record.goal)}.`
|
|
214
|
+
: "";
|
|
215
|
+
const slot = round.slots.find((other) => other.id === caller.id && other.status === "waiting");
|
|
216
|
+
if (slot !== undefined) {
|
|
217
|
+
slot.summaryBatch = record.key;
|
|
218
|
+
slot.note = `${note}${goalNote}`;
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
host.callIn(round, [caller], `${note}${goalNote}`, undefined);
|
|
222
|
+
const woken = round.slots.find((other) => other.id === caller.id && other.status === "waiting");
|
|
223
|
+
if (woken !== undefined)
|
|
224
|
+
woken.summaryBatch = record.key;
|
|
225
|
+
}
|
|
226
|
+
void rt.save().catch(() => { });
|
|
227
|
+
return true;
|
|
228
|
+
};
|
|
229
|
+
// The goal moved on since the call: say so next to the revision the result answers.
|
|
230
|
+
const goalMoved = (room, at) => {
|
|
231
|
+
const current = room.goal?.revision;
|
|
232
|
+
if (typeof current !== "number" || current === at.revision)
|
|
233
|
+
return "";
|
|
234
|
+
return `; it is now at revision ${current}`;
|
|
235
|
+
};
|
|
236
|
+
// The caller's line reached the group, which is where the user reads it: the batch's
|
|
237
|
+
// summary is delivered, not merely taken by a Bot. A Bot line in the group also
|
|
238
|
+
// settles any hand-off result the same Bot still owed that room.
|
|
239
|
+
host.deliverSummary = (room, botId, text) => {
|
|
240
|
+
let changed = false;
|
|
241
|
+
for (const record of Object.values(table(room))) {
|
|
242
|
+
const summary = record.summary;
|
|
243
|
+
if (summary?.state !== "requested" || summary.by !== botId)
|
|
244
|
+
continue;
|
|
245
|
+
record.summary = {
|
|
246
|
+
...summary,
|
|
247
|
+
state: "posted",
|
|
248
|
+
at: now(),
|
|
249
|
+
text: headOf(text, SUMMARY_TEXT_KEPT),
|
|
250
|
+
};
|
|
251
|
+
record.state = "delivered";
|
|
252
|
+
record.completedAt ??= now();
|
|
253
|
+
record.updatedAt = now();
|
|
254
|
+
changed = true;
|
|
255
|
+
}
|
|
256
|
+
if (changed)
|
|
257
|
+
void rt.save().catch(() => { });
|
|
258
|
+
if (typeof rt.deliverWorkInRoom === "function")
|
|
259
|
+
rt.deliverWorkInRoom(room.id, botId);
|
|
260
|
+
};
|
|
261
|
+
const failSummary = (room, record, reason) => {
|
|
262
|
+
record.state = "failed";
|
|
263
|
+
record.error = reason;
|
|
264
|
+
record.summary = {
|
|
265
|
+
...(record.summary ?? { attempts: 0 }),
|
|
266
|
+
state: "failed",
|
|
267
|
+
at: now(),
|
|
268
|
+
error: reason,
|
|
269
|
+
};
|
|
270
|
+
record.updatedAt = now();
|
|
271
|
+
record.writer = writer;
|
|
272
|
+
table(room)[record.key] = record;
|
|
273
|
+
notice(room, `The results of ${nameOf(record.by)}'s call will not be summarized: ${reason}.`);
|
|
274
|
+
void rt.save().catch(() => { });
|
|
275
|
+
};
|
|
276
|
+
const nameOf = (botId) => rt.botOf(botId)?.name ?? "a member";
|
|
277
|
+
// A new round or a restarted host finds every batch that still owes a summary and
|
|
278
|
+
// either asks its caller or fails it. Running this on every round is what makes the
|
|
279
|
+
// duty recoverable rather than a memory-only promise.
|
|
280
|
+
host.restoreSummaries = (room, round) => {
|
|
281
|
+
for (const record of Object.values(table(room))) {
|
|
282
|
+
if (!owesSummary(record))
|
|
283
|
+
continue;
|
|
284
|
+
const caller = rt.botOf(record.by);
|
|
285
|
+
if (caller === undefined || !room.members.includes(caller.id)) {
|
|
286
|
+
failSummary(room, record, goneCaller);
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
const existing = round.slots.find((slot) => slot.id === caller.id && slot.status === "waiting");
|
|
290
|
+
if (existing !== undefined) {
|
|
291
|
+
// It is already answering other work in this round; its answer owes the
|
|
292
|
+
// summary too, and a second call would only fork its place.
|
|
293
|
+
if (!host.allowSummary(room)) {
|
|
294
|
+
failSummary(room, record, "the group's summary budget is used up");
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
existing.summaryBatch = record.key;
|
|
298
|
+
if (record.summary?.state === "pending") {
|
|
299
|
+
host.chargeWork(room, "summary");
|
|
300
|
+
record.summary = {
|
|
301
|
+
...record.summary,
|
|
302
|
+
state: "requested",
|
|
303
|
+
by: caller.id,
|
|
304
|
+
at: now(),
|
|
305
|
+
attempts: (record.summary.attempts ?? 0) + 1,
|
|
306
|
+
requestedIn: round.callId,
|
|
307
|
+
};
|
|
308
|
+
record.updatedAt = now();
|
|
309
|
+
void rt.save().catch(() => { });
|
|
310
|
+
}
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (record.summary?.state === "requested" && record.summary.requestedIn === round.callId)
|
|
314
|
+
continue;
|
|
315
|
+
const batch = liveBatch(record);
|
|
316
|
+
if (batch !== undefined)
|
|
317
|
+
host.askSummary(room, round, batch, `${nameOf(record.by)}: the results of your call are back; post your final summary for the user.`);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
// The batches a done post's caller still owes a summary for, as live shapes. A late
|
|
321
|
+
// barrier completion needs them before the new round exists.
|
|
322
|
+
host.summaryBatches = (room, by) => Object.values(table(room))
|
|
323
|
+
.filter((record) => owesSummary(record) && record.by === by)
|
|
324
|
+
.map((record) => liveBatch(record))
|
|
325
|
+
.filter((batch) => batch !== undefined);
|
|
326
|
+
// A saved record as the live shape settle/ask expect.
|
|
327
|
+
const liveBatch = (record) => {
|
|
328
|
+
const batch = {
|
|
329
|
+
key: record.key,
|
|
330
|
+
roomId: record.roomId,
|
|
331
|
+
by: record.by,
|
|
332
|
+
callees: [...record.callees],
|
|
333
|
+
pending: new Set(record.pending),
|
|
334
|
+
done: [...record.done],
|
|
335
|
+
worked: record.worked,
|
|
336
|
+
answered: record.answered,
|
|
337
|
+
...(record.callId !== undefined ? { callId: record.callId } : {}),
|
|
338
|
+
...(record.goal !== undefined ? { goal: record.goal } : {}),
|
|
339
|
+
state: record.state,
|
|
340
|
+
...(record.summary !== undefined ? { summary: { ...record.summary } } : {}),
|
|
341
|
+
...(record.error !== undefined ? { error: record.error } : {}),
|
|
342
|
+
};
|
|
343
|
+
return batch.pending.size === 0 && owesSummary(record) ? batch : undefined;
|
|
344
|
+
};
|
|
345
|
+
// What a restart left behind. An open call cannot continue (its member turns died
|
|
346
|
+
// with the process): its finished results move on to the caller's summary when there
|
|
347
|
+
// are any, and otherwise it fails. A summary the dead process asked for is askable
|
|
348
|
+
// again; its caller is rechecked.
|
|
349
|
+
host.recoverCalls = (room) => {
|
|
350
|
+
let changed = false;
|
|
351
|
+
for (const record of Object.values(table(room))) {
|
|
352
|
+
const lost = record.pending;
|
|
353
|
+
// This process wrote the record last: nothing about it is a restart leftover.
|
|
354
|
+
if (record.writer === writer)
|
|
355
|
+
continue;
|
|
356
|
+
if (record.state === "open") {
|
|
357
|
+
record.pending = [];
|
|
358
|
+
record.completedAt ??= now();
|
|
359
|
+
record.lost = [...new Set([...(record.lost ?? []), ...lost])];
|
|
360
|
+
if (record.done.length > 0 && record.worked && !record.answered) {
|
|
361
|
+
record.state = "awaiting-summary";
|
|
362
|
+
record.summary = {
|
|
363
|
+
state: "pending",
|
|
364
|
+
attempts: record.summary?.attempts ?? 0,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
record.state = "failed";
|
|
369
|
+
record.error = "DeepSeek Harness restarted while this call was still out";
|
|
370
|
+
record.summary = {
|
|
371
|
+
state: "failed",
|
|
372
|
+
at: now(),
|
|
373
|
+
attempts: record.summary?.attempts ?? 0,
|
|
374
|
+
error: record.error,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
record.updatedAt = now();
|
|
378
|
+
record.writer = writer;
|
|
379
|
+
const names = lost.map(nameOf);
|
|
380
|
+
const results = record.results !== undefined && record.results.length > 0
|
|
381
|
+
? ` Recovered results: ${record.results
|
|
382
|
+
.map((result) => `${result.name}: ${headOf(result.text, SUMMARY_TEXT_KEPT)}`)
|
|
383
|
+
.join("; ")}`
|
|
384
|
+
: "";
|
|
385
|
+
if (results !== "")
|
|
386
|
+
record.resultsNoted = true;
|
|
387
|
+
notice(room, names.length > 0
|
|
388
|
+
? `DeepSeek Harness restarted while ${nameOf(record.by)}'s call to ${names.join(" and ")} was out. What those members had not finished is lost; what came back is kept${record.done.length > 0 ? `, and ${nameOf(record.by)} is asked to summarize it` : ""}.${results}`
|
|
389
|
+
: `DeepSeek Harness restarted while ${nameOf(record.by)}'s call was out; it is closed.${results}`);
|
|
390
|
+
changed = true;
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
if (record.state !== "awaiting-summary")
|
|
394
|
+
continue;
|
|
395
|
+
const caller = rt.botOf(record.by);
|
|
396
|
+
if (caller === undefined || !room.members.includes(caller.id)) {
|
|
397
|
+
failSummary(room, record, goneCaller);
|
|
398
|
+
changed = true;
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
if (record.summary?.state === "requested") {
|
|
402
|
+
// The process that asked died before its caller answered: ask again.
|
|
403
|
+
record.summary = { ...record.summary, state: "pending", requestedIn: undefined };
|
|
404
|
+
record.updatedAt = now();
|
|
405
|
+
changed = true;
|
|
406
|
+
}
|
|
407
|
+
// The results may have died with the process before their group lines were
|
|
408
|
+
// written; put them in front of the caller once, as a notice it can summarize.
|
|
409
|
+
if (record.results !== undefined && record.results.length > 0 && record.resultsNoted !== true) {
|
|
410
|
+
notice(room, `Results recovered after a restart (${nameOf(record.by)}'s call): ${record.results
|
|
411
|
+
.map((result) => `${result.name}: ${headOf(result.text, SUMMARY_TEXT_KEPT)}`)
|
|
412
|
+
.join("; ")}`);
|
|
413
|
+
record.resultsNoted = true;
|
|
414
|
+
changed = true;
|
|
415
|
+
}
|
|
416
|
+
record.writer = writer;
|
|
417
|
+
record.updatedAt = now();
|
|
418
|
+
changed = true;
|
|
419
|
+
}
|
|
420
|
+
if (changed)
|
|
421
|
+
void rt.save().catch(() => { });
|
|
422
|
+
};
|
|
423
|
+
// A late result keeps the goal snapshot it was asked under until the group line is
|
|
424
|
+
// written: the post travels through the group Session, which does not carry it.
|
|
425
|
+
host.noteLateGoal = (room, botId, goal) => {
|
|
426
|
+
if (goal === undefined)
|
|
427
|
+
return;
|
|
428
|
+
room.lateGoals ??= {};
|
|
429
|
+
const list = room.lateGoals[botId] ?? [];
|
|
430
|
+
room.lateGoals[botId] = [...list, goal].slice(-LATE_GOALS_KEPT);
|
|
431
|
+
};
|
|
432
|
+
host.takeLateGoal = (room, botId) => {
|
|
433
|
+
const late = room.lateGoals;
|
|
434
|
+
const list = late?.[botId];
|
|
435
|
+
if (late === undefined || list === undefined || list.length === 0)
|
|
436
|
+
return undefined;
|
|
437
|
+
const [first, ...rest] = list;
|
|
438
|
+
if (rest.length === 0)
|
|
439
|
+
delete late[botId];
|
|
440
|
+
else
|
|
441
|
+
late[botId] = rest;
|
|
442
|
+
if (Object.keys(late).length === 0)
|
|
443
|
+
delete room.lateGoals;
|
|
444
|
+
return first;
|
|
445
|
+
};
|
|
446
|
+
// Replay identity: a group_relay callId is the one key. The same words sent as a new
|
|
447
|
+
// call get a new callId and run; a replay of a finished call returns its saved
|
|
448
|
+
// result without calling another member.
|
|
449
|
+
host.beginRelay = (room, callId) => {
|
|
450
|
+
room.callRuns ??= {};
|
|
451
|
+
const saved = room.callRuns[callId];
|
|
452
|
+
if (saved === undefined) {
|
|
453
|
+
room.callRuns[callId] = { callId, at: now(), state: "running" };
|
|
454
|
+
pruneRelays(room);
|
|
455
|
+
void rt.save().catch(() => { });
|
|
456
|
+
return undefined;
|
|
457
|
+
}
|
|
458
|
+
if (saved.state === "done" && saved.result !== undefined)
|
|
459
|
+
return { ...saved.result, replayed: true };
|
|
460
|
+
if (saved.state === "failed")
|
|
461
|
+
return { replies: [], passed: [], replayed: true, error: saved.error };
|
|
462
|
+
// It started in the process that died here: running it again would duplicate
|
|
463
|
+
// every member turn, so it fails out loud instead.
|
|
464
|
+
saved.state = "failed";
|
|
465
|
+
saved.error = "DeepSeek Harness restarted while this relay was running";
|
|
466
|
+
void rt.save().catch(() => { });
|
|
467
|
+
return { replies: [], passed: [], interrupted: true, error: saved.error };
|
|
468
|
+
};
|
|
469
|
+
host.finishRelay = (room, callId, result) => {
|
|
470
|
+
room.callRuns ??= {};
|
|
471
|
+
const saved = {
|
|
472
|
+
callId,
|
|
473
|
+
at: room.callRuns[callId]?.at ?? now(),
|
|
474
|
+
state: "done",
|
|
475
|
+
result: {
|
|
476
|
+
replies: result.replies.map((reply) => ({
|
|
477
|
+
...reply,
|
|
478
|
+
...(typeof reply.text === "string" ? { text: headOf(reply.text, REPLAY_TEXT_KEPT) } : {}),
|
|
479
|
+
})),
|
|
480
|
+
passed: [...result.passed],
|
|
481
|
+
...(result.failed !== undefined ? { failed: [...result.failed] } : {}),
|
|
482
|
+
},
|
|
483
|
+
};
|
|
484
|
+
room.callRuns[callId] = saved;
|
|
485
|
+
pruneRelays(room);
|
|
486
|
+
void rt.save().catch(() => { });
|
|
487
|
+
};
|
|
488
|
+
host.failRelay = (room, callId, error) => {
|
|
489
|
+
room.callRuns ??= {};
|
|
490
|
+
room.callRuns[callId] = {
|
|
491
|
+
callId,
|
|
492
|
+
at: room.callRuns[callId]?.at ?? now(),
|
|
493
|
+
state: "failed",
|
|
494
|
+
error: String(error?.message ?? error).slice(0, 300),
|
|
495
|
+
};
|
|
496
|
+
pruneRelays(room);
|
|
497
|
+
void rt.save().catch(() => { });
|
|
498
|
+
};
|
|
499
|
+
const pruneRelays = (room) => {
|
|
500
|
+
const kept = room.callRuns ?? {};
|
|
501
|
+
const entries = Object.values(kept).sort((left, right) => left.at - right.at);
|
|
502
|
+
for (const entry of entries.slice(0, Math.max(0, entries.length - RELAYS_KEPT))) {
|
|
503
|
+
if (entry.state === "running")
|
|
504
|
+
continue;
|
|
505
|
+
delete kept[entry.callId];
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
// Member work and summary work are counted apart. The idle guard's `idleRelays` is a
|
|
509
|
+
// different mechanism and never lands in either side.
|
|
510
|
+
const side = (room, which) => {
|
|
511
|
+
room.budget ??= {};
|
|
512
|
+
const current = room.budget[which];
|
|
513
|
+
if (current !== undefined)
|
|
514
|
+
return current;
|
|
515
|
+
const created = { spent: 0 };
|
|
516
|
+
room.budget[which] = created;
|
|
517
|
+
return created;
|
|
518
|
+
};
|
|
519
|
+
const configuredLimit = (which) => {
|
|
520
|
+
// Profile patches may set these even though the typed config does not name them.
|
|
521
|
+
const config = rt.config;
|
|
522
|
+
const value = which === "members" ? config.groupMemberTurns : config.groupSummaryTurns;
|
|
523
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
|
|
524
|
+
};
|
|
525
|
+
const limitOf = (room, which) => {
|
|
526
|
+
const local = room.budget?.[which]?.limit;
|
|
527
|
+
return local ?? configuredLimit(which);
|
|
528
|
+
};
|
|
529
|
+
// A token budget is enforced from what DSH already reports; without that projection
|
|
530
|
+
// it cannot be measured, so it is not guessed at.
|
|
531
|
+
const tokensOver = (room, which) => {
|
|
532
|
+
const budget = room.budget?.[which];
|
|
533
|
+
return budget?.tokenLimit !== undefined && budget.tokens !== undefined && budget.tokens >= budget.tokenLimit;
|
|
534
|
+
};
|
|
535
|
+
const remaining = (room, which) => {
|
|
536
|
+
const limit = limitOf(room, which);
|
|
537
|
+
if (limit === undefined)
|
|
538
|
+
return undefined;
|
|
539
|
+
return Math.max(0, limit - side(room, which).spent);
|
|
540
|
+
};
|
|
541
|
+
// A new scope resets what was spent, keeping the limits. A goal runs until its
|
|
542
|
+
// objective revision changes; without one, each ask from the user is its own scope.
|
|
543
|
+
host.beginBudget = (room, round) => {
|
|
544
|
+
const goal = room.goal;
|
|
545
|
+
const scope = goal?.id !== undefined && goal.status === "active"
|
|
546
|
+
? `goal:${goal.id}:${goal.objectiveRevision ?? goal.revision ?? 0}`
|
|
547
|
+
: round.userMessage === true
|
|
548
|
+
? `ask:${round.startSeq}`
|
|
549
|
+
: undefined;
|
|
550
|
+
if (scope === undefined)
|
|
551
|
+
return;
|
|
552
|
+
room.budget ??= {};
|
|
553
|
+
if (room.budget.scope === scope)
|
|
554
|
+
return;
|
|
555
|
+
room.budget.scope = scope;
|
|
556
|
+
delete room.budget.noticed;
|
|
557
|
+
for (const which of ["members", "summary"]) {
|
|
558
|
+
const current = room.budget[which];
|
|
559
|
+
room.budget[which] = {
|
|
560
|
+
...(current?.limit !== undefined ? { limit: current.limit } : {}),
|
|
561
|
+
...(current?.tokenLimit !== undefined ? { tokenLimit: current.tokenLimit } : {}),
|
|
562
|
+
spent: 0,
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
host.allowMemberWork = (room) => {
|
|
567
|
+
const limit = limitOf(room, "members");
|
|
568
|
+
if (limit !== undefined && side(room, "members").spent >= limit)
|
|
569
|
+
return false;
|
|
570
|
+
return !tokensOver(room, "members");
|
|
571
|
+
};
|
|
572
|
+
host.allowSummary = (room) => {
|
|
573
|
+
const limit = limitOf(room, "summary");
|
|
574
|
+
if (limit !== undefined && side(room, "summary").spent >= limit)
|
|
575
|
+
return false;
|
|
576
|
+
return !tokensOver(room, "summary");
|
|
577
|
+
};
|
|
578
|
+
host.chargeWork = (room, which) => {
|
|
579
|
+
const budget = side(room, which);
|
|
580
|
+
budget.spent += 1;
|
|
581
|
+
};
|
|
582
|
+
host.chargeTokens = (room, summary, tokens) => {
|
|
583
|
+
if (tokens === undefined || tokens <= 0)
|
|
584
|
+
return;
|
|
585
|
+
const budget = side(room, summary ? "summary" : "members");
|
|
586
|
+
budget.tokens = (budget.tokens ?? 0) + tokens;
|
|
587
|
+
};
|
|
588
|
+
// The DSH token-meter projection of one Session: the budget consumes what the team
|
|
589
|
+
// already reports instead of keeping a second token ledger.
|
|
590
|
+
host.sessionTokens = async (sessionId) => {
|
|
591
|
+
try {
|
|
592
|
+
const registry = rt.ctx.get?.("sessionProjections");
|
|
593
|
+
if (registry?.snapshot === undefined)
|
|
594
|
+
return undefined;
|
|
595
|
+
const agent = (await rt.resolveAgent?.(sessionId));
|
|
596
|
+
if (agent?.session === undefined)
|
|
597
|
+
return undefined;
|
|
598
|
+
const usage = registry.snapshot(agent.session)?.values?.tokenUsage;
|
|
599
|
+
if (usage === undefined)
|
|
600
|
+
return undefined;
|
|
601
|
+
const count = (key) => Number(usage[key]) || 0;
|
|
602
|
+
return (count("uncachedInputTokens") + count("outputTokens") + count("cacheReadTokens") + count("cacheWriteTokens"));
|
|
603
|
+
}
|
|
604
|
+
catch {
|
|
605
|
+
return undefined;
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
host.budgetOf = async (room) => {
|
|
609
|
+
const registry = (() => {
|
|
610
|
+
try {
|
|
611
|
+
return rt.ctx.get?.("sessionProjections");
|
|
612
|
+
}
|
|
613
|
+
catch {
|
|
614
|
+
return undefined;
|
|
615
|
+
}
|
|
616
|
+
})();
|
|
617
|
+
const view = (which) => {
|
|
618
|
+
const spent = side(room, which);
|
|
619
|
+
const limit = limitOf(room, which);
|
|
620
|
+
const left = remaining(room, which);
|
|
621
|
+
return {
|
|
622
|
+
...(limit !== undefined ? { limit } : {}),
|
|
623
|
+
...(spent.tokenLimit !== undefined ? { tokenLimit: spent.tokenLimit } : {}),
|
|
624
|
+
spent: spent.spent,
|
|
625
|
+
...(spent.tokens !== undefined ? { tokens: spent.tokens } : {}),
|
|
626
|
+
...(left !== undefined ? { remaining: left } : {}),
|
|
627
|
+
};
|
|
628
|
+
};
|
|
629
|
+
return {
|
|
630
|
+
available: registry?.snapshot !== undefined,
|
|
631
|
+
...(room.budget?.scope !== undefined ? { scope: room.budget.scope } : {}),
|
|
632
|
+
members: view("members"),
|
|
633
|
+
summary: view("summary"),
|
|
634
|
+
idle: { streak: room.idleRelays ?? 0, paused: room.idlePaused !== undefined },
|
|
635
|
+
};
|
|
636
|
+
};
|
|
637
|
+
// A view for the group's own settings and for the host's checks.
|
|
638
|
+
rt.groupBudgetView = async (roomId) => {
|
|
639
|
+
const saved = rt.roomOf(roomId);
|
|
640
|
+
return saved === undefined ? undefined : host.budgetOf(asRoom(saved));
|
|
641
|
+
};
|
|
642
|
+
// What the group may spend before its calls are refused. A limit is a whole number of
|
|
643
|
+
// turns or tokens; an explicit null clears it, and `clear` removes both sides at once.
|
|
644
|
+
// The user sets this from the group's settings, so the shape is checked here.
|
|
645
|
+
rt.setGroupBudget = async (roomId, patch) => {
|
|
646
|
+
const saved = rt.roomOf(roomId);
|
|
647
|
+
if (saved === undefined)
|
|
648
|
+
return undefined;
|
|
649
|
+
const room = asRoom(saved);
|
|
650
|
+
room.budget ??= {};
|
|
651
|
+
const budget = room.budget;
|
|
652
|
+
const whole = (value, label) => {
|
|
653
|
+
if (value === null)
|
|
654
|
+
return undefined;
|
|
655
|
+
const number = Number(value);
|
|
656
|
+
if (!Number.isInteger(number) || number <= 0) {
|
|
657
|
+
throw Object.assign(new Error(`${label} must be a whole number above zero`), { code: "bot/invalid" });
|
|
658
|
+
}
|
|
659
|
+
return number;
|
|
660
|
+
};
|
|
661
|
+
const apply = (which, given) => {
|
|
662
|
+
if (given === undefined || given === null || typeof given !== "object")
|
|
663
|
+
return;
|
|
664
|
+
budget[which] ??= { spent: 0 };
|
|
665
|
+
const side = budget[which];
|
|
666
|
+
if ("limit" in given)
|
|
667
|
+
side.limit = whole(given.limit, "The turn limit");
|
|
668
|
+
if ("tokenLimit" in given)
|
|
669
|
+
side.tokenLimit = whole(given.tokenLimit, "The token limit");
|
|
670
|
+
};
|
|
671
|
+
if (patch.clear === true) {
|
|
672
|
+
budget.members = undefined;
|
|
673
|
+
budget.summary = undefined;
|
|
674
|
+
}
|
|
675
|
+
apply("members", patch.members);
|
|
676
|
+
apply("summary", patch.summary);
|
|
677
|
+
// A fresh limit says nothing about the notices already posted: the next overrun is
|
|
678
|
+
// a new event for the user.
|
|
679
|
+
if (budget.noticed !== undefined)
|
|
680
|
+
budget.noticed = {};
|
|
681
|
+
await rt.save();
|
|
682
|
+
return host.budgetOf(room);
|
|
683
|
+
};
|
|
684
|
+
}
|