An open-source, faster Devin alternative
TypeScript
managed-agents: a Slack link reaches back to the sessions before it (#1061)
opencomputer-app[bot]d4cdd8335eb4
diff --git a/services/managed-agents/src/computer/sessions-api.ts b/services/managed-agents/src/computer/sessions-api.tsindex aaa480ab..b00a58c2 100644--- a/services/managed-agents/src/computer/sessions-api.ts+++ b/services/managed-agents/src/computer/sessions-api.ts@@ -399,6 +399,7 @@ export class ComputerSessionsApi {case "create":return this.create(caller, op.body, op.idempotencyKey);case "list":+ await this.attachLinked();return this.list(caller, op.query);case "get":return this.get(caller, op.sessionId, op.minSeq);@@ -1418,15 +1419,14 @@ export class ComputerSessionsApi {const linked = !row.ownerUserId ? this.deps.linkOfSlack(ownerSlackId) : undefined;if (minted || linked) {const member = linked ? await memberId(linked) : row.ownerUserId;- const person = this.deps.slackPerson(ownerSlackId);- const name = person.name ?? (person.gitName.endsWith(" via Computer") ? person.gitName.slice(0, -" via Computer".length) : null);+ const { name, email } = this.slackOwner(ownerSlackId);this.deps.sql.exec(`UPDATE sessions SET session_id = COALESCE(session_id, ?), origin_type = 'slack', owner_user_id = COALESCE(owner_user_id, ?),owner_name = COALESCE(owner_name, ?), owner_email = COALESCE(owner_email, ?) WHERE runner_key = ?`,mintId("cs"),member ?? null,name,- person.gitEmail.endsWith("@users.noreply.opencomputer.dev") ? null : person.gitEmail,+ email,key,);row = this.rowByKey(key)!;@@ -1435,6 +1435,43 @@ export class ComputerSessionsApi {return { sessionId: row.sessionId, ...(row.ownerUserId ? { userId: row.ownerUserId } : {}), epoch: row.logEpoch, lastSeq: row.lastSeq };}+ /** A Slack person as a session's owner names them: no " via Computer", no placeholder email. */+ private slackOwner(slackUserId: string): { name: string | null; email: string | null } {+ const person = this.deps.slackPerson(slackUserId);+ const name = person.name ?? (person.gitName.endsWith(" via Computer") ? person.gitName.slice(0, -" via Computer".length) : null);+ return { name, email: person.gitEmail.endsWith("@users.noreply.opencomputer.dev") ? null : person.gitEmail };+ }++ /**+ * Slack sessions whose owner linked a dashboard user after their last message become that user's,+ * as their next delivery would make them (slackDelivery): a link reaches back to the threads before it.+ * Runs before each list; it reads one row per Slack person with unowned sessions, so it stays cheap.+ */+ private async attachLinked(): Promise<void> {+ const unowned = [+ ...this.deps.sql.exec<{ ownerId: string }>(+ `SELECT DISTINCT owner_id AS ownerId FROM sessions+ WHERE session_id IS NOT NULL AND origin_type = 'slack' AND owner_user_id IS NULL AND owner_id IS NOT NULL`,+ ),+ ];+ for (const { ownerId } of unowned) {+ const linked = this.deps.linkOfSlack(ownerId);+ if (!linked) continue;+ const member = await memberId(linked);+ const { name, email } = this.slackOwner(ownerId);+ const unownedOf = `owner_id = ? AND owner_user_id IS NULL AND session_id IS NOT NULL AND origin_type = 'slack'`;+ const rows = [...this.deps.sql.exec<{ sessionId: string }>(`SELECT session_id AS sessionId FROM sessions WHERE ${unownedOf}`, ownerId)];+ this.deps.sql.exec(+ `UPDATE sessions SET owner_user_id = ?, owner_name = COALESCE(owner_name, ?), owner_email = COALESCE(owner_email, ?) WHERE ${unownedOf}`,+ member,+ name,+ email,+ ownerId,+ );+ if (this.v4Host()) for (const { sessionId } of rows) this.sendAccess({ sessionId, originType: "slack", ownerUserId: member });+ }+ }+// ---------------------------------------------------------------- idempotencyprivate idempotency(member: string, op: string, key: string): { hash: string; body: string } | undefined {diff --git a/services/managed-agents/test/computer-sessions-api.test.ts b/services/managed-agents/test/computer-sessions-api.test.tsindex b622a17b..fd0cb97a 100644--- a/services/managed-agents/test/computer-sessions-api.test.ts+++ b/services/managed-agents/test/computer-sessions-api.test.ts@@ -533,6 +533,23 @@ describe("visibility and lists", () => {expect((idle.body as { sessions: Array<{ id: string }> }).sessions.map((s) => s.id)).toEqual([slackId]);});+ it("a link reaches back: Slack sessions from before it become the owner's on their next list", async () => {+ const f = fixture();+ f.slackRow("T1:C1:1.2");+ const slackId = (await f.api.slackDelivery("T1:C1:1.2", "U1"))!.sessionId;+ const ids = async (who?: string) =>+ ((await f.api.handle(caller(who), { op: "list", query: [] })).body as { sessions: Array<{ id: string }> }).sessions.map((s) => s.id);+ expect(await ids()).toEqual([]);+ f.state.links.set("U1", "user_01JABCDEFGHJKMNPQRSTVWXYZ0");+ expect(await ids(MO)).toEqual([]);+ expect(f.frames("access").at(-1)).toEqual({ t: "access", access: { sessionId: slackId, owner: OWNER, members: [OWNER] } });+ expect(await ids()).toEqual([slackId]);+ // Once theirs, it is not attached again.+ const accesses = f.frames("access").length;+ await ids();+ expect(f.frames("access")).toHaveLength(accesses);+ });+it("a v3 host's Slack deliveries are unchanged (no id, no frames)", async () => {const f = fixture({ protocol: 3 });f.slackRow("T1:C1:1.2");