Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(cloud): keep the typing hint through computer choice, first send and same-named computers

Bugfixer 202af5540aeae
diff --git a/server/index.ts b/server/index.ts
index c23718cc84..2a95bf75b4 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -7590,7 +7590,7 @@ async function beginTurn(
});
}
const personLine = !opts?.coordination && !opts?.cardContinuation && !opts?.automationSource && !opts?.compactOnly
- && !opts?.computerSelectionContinuation && !opts?.editedMessageId && !commsDepth && !opts?.peerAsk && !userMessage.peerAsk; if (personLine) cloudConnections.personTurn(bot, threadId); const typingSession = personLine && !userMessage.relayed ? sendingSession(threadId, userMessage.sendId) : undefined;
+ && !opts?.computerSelectionContinuation && !opts?.editedMessageId && !commsDepth && !opts?.peerAsk && !userMessage.peerAsk; if (personLine) cloudConnections.personTurn(bot, threadId); const typingSession = (personLine || (opts?.computerSelectionContinuation && opts.userMessage && !opts.coordination)) && !userMessage.relayed ? sendingSession(threadId, userMessage.sendId) : undefined;
if (personLine && !userMessage.relayed && (coordinator.answer(threadId, text) || signInSettle.answerInChat(bot.id, threadId, text))) {
askCards.answeredInWords(threadId, text);
store.appendMessage(threadId, { role: "bot", kind: "activity", tool: { name: serverLine(threadLanguage(threadId), "server.teammates.answerPassed"), ok: true } });
diff --git a/server/routes/bot-messages.test.ts b/server/routes/bot-messages.test.ts
index 3c553d45ec..d3af38e638 100644
--- a/server/routes/bot-messages.test.ts
+++ b/server/routes/bot-messages.test.ts
@@ -352,6 +352,14 @@ describe("POST /api/bots/:id/messages and who typed it", () => {
expect(sendingSession(threadId, OTHER_SEND)).toBeUndefined();
});
+ it("keeps the first sender's session when a conflicting send reuses a queued line's sendId", async () => {
+ const { threadId, post } = await fixture();
+ expect((await post({ threadId, sendId: SEND, text: words }, "", { "x-test-session": "pc-session" })).status).toBe(202);
+ const conflict = await post({ threadId, sendId: SEND, text: "Something else" }, "", { "x-test-session": "laptop-session" });
+ expect(conflict.status, JSON.stringify(conflict.body)).toBe(409);
+ expect(sendingSession(threadId, SEND)).toBe("pc-session");
+ });
+
it("does not take a bare loopback send as a person's on a headless server, where it may be a bot's shell", async () => {
const { threadId, post } = await fixture({}, false);
const plain = await post({ threadId, sendId: SEND, text: words });
diff --git a/server/sending-sessions.test.ts b/server/sending-sessions.test.ts
index 94cd245167..243b7d5ea1 100644
--- a/server/sending-sessions.test.ts
+++ b/server/sending-sessions.test.ts
@@ -10,6 +10,12 @@ describe("the session that sent a line", () => {
expect(sendingSession("thread-a", undefined)).toBeUndefined();
});
+ it("keeps the first session for a line: a later send reusing its sendId does not take it over", () => {
+ noteSendingSession("thread-first", "send-1", "pc-session");
+ noteSendingSession("thread-first", "send-1", "laptop-session");
+ expect(sendingSession("thread-first", "send-1")).toBe("pc-session");
+ });
+
it("keeps the newest lines and forgets the oldest past its limit", () => {
noteSendingSession("thread-old", "send-old", "old");
for (let index = 0; index < 2_000; index++) noteSendingSession("thread-many", `send-${index}`, "pc");
diff --git a/server/sending-sessions.ts b/server/sending-sessions.ts
index b38cc5066f..32394a1f44 100644
--- a/server/sending-sessions.ts
+++ b/server/sending-sessions.ts
@@ -6,9 +6,11 @@ const LIMIT = 2_000;
const sessions = new Map<string, string>();
const key = (threadId: string, sendId: string): string => `${threadId}\n${sendId}`;
+/** The first send of a line names its session: a later send reusing that sendId is a conflict the route refuses, and
+ * must not take over the hint of the line already queued. */
export function noteSendingSession(threadId: string, sendId: string, session: string): void {
const at = key(threadId, sendId);
- sessions.delete(at);
+ if (sessions.has(at)) return;
sessions.set(at, session);
if (sessions.size > LIMIT) sessions.delete(sessions.keys().next().value!);
}
diff --git a/server/shared-computers.test.ts b/server/shared-computers.test.ts
index 3acf7b216c..70fd15099d 100644
--- a/server/shared-computers.test.ts
+++ b/server/shared-computers.test.ts
@@ -652,4 +652,21 @@ describe("the computer the person types on", () => {
broker.close();
rmSync(dir, { recursive: true, force: true });
});
+
+ it("is listed live when another computer with the same name holds the one record kept for that name", async () => {
+ vi.useFakeTimers({ now: Date.now() });
+ const dir = mkdtempSync(join(tmpdir(), "omb-typing-same-name-"));
+ const broker = new SharedComputers(() => true, new LentComputers(join(dir, "lent.json")));
+ const first = { ...registration(), name: "PC", platform: "win32" as const };
+ const second = { ...registration(), name: "PC", platform: "win32" as const };
+ broker.register(first, { session: "first", person: alice.person }, secret);
+ await vi.advanceTimersByTimeAsync(1_000);
+ broker.register(second, { session: "second", person: alice.person }, other);
+ expect(broker.known(alice.person).map(computer => computer.id)).toEqual([second.id]);
+ expect(broker.known(alice.person, "first").map(computer => [computer.id, computer.typing === true, computer.online]))
+ .toEqual([[first.id, true, true], [second.id, false, true]]);
+ expect(broker.known(alice.person, "second").map(computer => [computer.id, computer.typing === true])).toEqual([[second.id, true]]);
+ broker.close();
+ rmSync(dir, { recursive: true, force: true });
+ });
});
diff --git a/server/shared-computers.ts b/server/shared-computers.ts
index b1e75e67c7..686bebcb18 100644
--- a/server/shared-computers.ts
+++ b/server/shared-computers.ts
@@ -288,15 +288,23 @@ export class SharedComputers {
* an online computer lent by that session is the one they type on: marked, and put first. */
known(principal: string | null, sending?: string): KnownLentComputer[] {
if (!principal) return [];
- const typingOn = sending === undefined ? undefined
- : [...this.computers.values()].find(entry => entry.owner.person === principal && entry.owner.session === sending && this.online(entry))?.registration.id;
- const known = (this.lent?.list(principal) ?? []).map((record): KnownLentComputer => {
+ const typing = sending === undefined ? undefined
+ : [...this.computers.values()].find(entry => entry.owner.person === principal && entry.owner.session === sending && this.online(entry));
+ const records = this.lent?.list(principal) ?? [];
+ const known = records.map((record): KnownLentComputer => {
const entry = this.chosen(principal, record.id);
- return { ...(record.id === typingOn ? { typing: true as const } : {}), id: record.id, name: record.name, platform: record.platform, online: entry ? this.online(entry) : false, busy: (entry?.jobs.size ?? 0) > 0,
+ return { ...(record.id === typing?.registration.id ? { typing: true as const } : {}), id: record.id, name: record.name, platform: record.platform, online: entry ? this.online(entry) : false, busy: (entry?.jobs.size ?? 0) > 0,
lent: record.stoppedAt === null, lastSeenAt: entry ? Math.max(entry.seen, record.lastSeenAt) : record.lastSeenAt, lastUsedAt: record.lastUsedAt,
scopes: { folders: record.folders, terminal: record.terminal, screen: record.screen, internet: record.internet === true, chrome: record.chrome === true } };
});
- return [...known.filter(computer => computer.typing), ...known.filter(computer => !computer.typing)];
+ // Two computers with one name share one record, kept for the newer, so the one they type on can have none: listed live.
+ const unrecorded = typing && !records.some(record => record.id === typing.registration.id) ? [typing] : [];
+ return [
+ ...unrecorded.map(({ registration, jobs, seen }): KnownLentComputer => ({ typing: true, id: registration.id, name: registration.name, platform: registration.platform ?? null,
+ online: true, busy: jobs.size > 0, lent: true, lastSeenAt: seen, lastUsedAt: null,
+ scopes: { folders: registration.folders.length, terminal: registration.terminal, screen: registration.computer, internet: registration.internet === true, chrome: registration.chrome === true } })),
+ ...known.filter(computer => computer.typing), ...known.filter(computer => !computer.typing),
+ ];
}
/** For a bot: which known computer is offline and since when, and the sentence to say; nothing while one is online. */
away(principal: string | null) {