Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Trust a verified live turn as proof of the speaker's room membership (#1507)

Joshua France44612e5856fa
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.ts
index 0d6c824d1..d5bbd866b 100644
--- a/src/core/orchestrator.ts
+++ b/src/core/orchestrator.ts
@@ -8,7 +8,7 @@ import {
loadDocumentInputs,
} from "./document-inputs.ts";
import { recoveredRuntime } from "../harness/runtime-recovery.ts";
-import { createCanWriteScope } from "../resolution/scope-membership.ts";
+import { createCanWriteScope, withLiveTurnMembership } from "../resolution/scope-membership.ts";
import { evaluateCommandWithLayer } from "../policy/command-policy.ts";
import { createSecretValueMasker } from "../security/secret-masking.ts";
import { shq } from "../util/shell.ts";
@@ -684,6 +684,11 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
const resolution = await deps.resolution.resolve(conversation, actor);
const scopeId = deps.resolution.scopeFor(conversation, actor);
+ const isCurrentSharedScopeMember = withLiveTurnMembership(deps.isCurrentSharedScopeMember, {
+ actorId: actor.id,
+ scopeId,
+ verified: liveAuthorTurn && conversation.kind !== "dm",
+ });
let participantHistorySeqs: Set<number> | undefined;
let participantHistoryMaxSeq = -1;
const filterHistory = (entries: SessionEntry[]): SessionEntry[] =>
@@ -1049,7 +1054,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
targetScope: scopeId,
config: deps.config,
sessions: deps.sessions,
- isCurrentSharedScopeMember: deps.isCurrentSharedScopeMember,
+ isCurrentSharedScopeMember,
resolution,
memoryPolicy,
useMemory,
@@ -1716,7 +1721,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
provisionPending,
invalidateProvision,
} = createTurnSandboxes({
- deps,
+ deps: { ...deps, isCurrentSharedScopeMember },
input,
actor,
session,
diff --git a/src/resolution/scope-membership.ts b/src/resolution/scope-membership.ts
index a282e476d..cb5c7e6ac 100644
--- a/src/resolution/scope-membership.ts
+++ b/src/resolution/scope-membership.ts
@@ -97,6 +97,15 @@ export function createIsCurrentSharedScopeMember(deps: ScopeMembershipDeps): IsC
};
}
+export function withLiveTurnMembership(
+ stored: IsCurrentSharedScopeMember | undefined,
+ turn: { actorId: string; scopeId: ScopeId; verified: boolean },
+): IsCurrentSharedScopeMember {
+ return async (principalId, scope) =>
+ (turn.verified && scope === turn.scopeId && samePerson(principalId, turn.actorId)) ||
+ (await stored?.(principalId, scope)) === true;
+}
+
export type CurrentScopeMembers = (scope: ScopeId) => Promise<Principal[] | undefined>;
export function createCurrentScopeMembers(deps: ScopeMembershipDeps): CurrentScopeMembers {
diff --git a/test/scope-membership-live-roster.test.ts b/test/scope-membership-live-roster.test.ts
new file mode 100644
index 000000000..dbe3688f1
--- /dev/null
+++ b/test/scope-membership-live-roster.test.ts
@@ -0,0 +1,26 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { withLiveTurnMembership } from "../src/resolution/scope-membership.ts";
+import type { ScopeId } from "../src/types.ts";
+
+const room = "group:C0NEW" as ScopeId;
+const otherRoom = "group:C0OTHER" as ScopeId;
+const stored = async (principalId: string, scope: ScopeId) =>
+ scope === otherRoom && principalId === "[email protected]";
+
+test("a verified live turn proves the speaker's membership in its own scope while the store lags", async () => {
+ const check = withLiveTurnMembership(stored, { actorId: "[email protected]", scopeId: room, verified: true });
+ assert.equal(await check("[email protected]", room), true);
+ assert.equal(await check("[email protected]", room), true);
+ assert.equal(await check("[email protected]", room), false);
+});
+
+test("the live turn says nothing about other scopes or unverified turns", async () => {
+ const verified = withLiveTurnMembership(stored, { actorId: "[email protected]", scopeId: room, verified: true });
+ assert.equal(await verified("[email protected]", otherRoom), false);
+ assert.equal(await verified("[email protected]", otherRoom), true);
+ const unverified = withLiveTurnMembership(stored, { actorId: "[email protected]", scopeId: room, verified: false });
+ assert.equal(await unverified("[email protected]", room), false);
+ const noStore = withLiveTurnMembership(undefined, { actorId: "[email protected]", scopeId: room, verified: true });
+ assert.equal(await noStore("[email protected]", otherRoom), false);
+});
diff --git a/test/sharing-context-e2e.test.ts b/test/sharing-context-e2e.test.ts
index 3aa8605d0..e3787e2c3 100644
--- a/test/sharing-context-e2e.test.ts
+++ b/test/sharing-context-e2e.test.ts
@@ -514,7 +514,33 @@ test("Open speaker keychain uses a disposable computer, follows the speaker, and
await assert.rejects(denied({ kind: "automation" }), /owner-auth box is not available/);
await assert.rejects(denied({ kind: "ambient" }), /owner-auth box is not available/);
await assert.rejects(denied({ kind: "ambient", live: false }), /owner-auth box is not available/);
+ const firstTurn = (channelRef: string, origin: TurnRequest["origin"] = { kind: "human" }) =>
+ b.turn(`!owner ${probe}`, true, "U1", {
+ origin,
+ conversation: {
+ kind: "channel",
+ channelRef,
+ threadRef: `${channelRef}:first-turn-${++deniedTurn}`,
+ audience: [{ externalId: "U1" }, { externalId: "U2" }],
+ publishMembers: [{ externalId: "U1" }, { externalId: "U2" }],
+ },
+ });
+ assert.equal(await firstTurn("C-unsynced"), "npm_U1|gmail_U1|file_U1|unset|isolated");
+ assert.equal(
+ await firstTurn("C-unsynced", { kind: "ambient", live: true }),
+ "npm_U1|gmail_U1|file_U1|unset|isolated",
+ );
await b.remove("U1");
- await assert.rejects(denied(), /owner-auth box is not available/);
- await assert.rejects(denied({ kind: "ambient", live: true }), /owner-auth box is not available/);
+ const removed = (origin: TurnRequest["origin"] = { kind: "human" }) =>
+ b.turn("!owner true", true, "U1", {
+ origin,
+ conversation: {
+ kind: "channel",
+ channelRef: "C1",
+ threadRef: `C1:keychain-denied-${++deniedTurn}`,
+ audience: [{ externalId: "U1" }, { externalId: "slack-external", isExternalGuest: true }],
+ },
+ });
+ await assert.rejects(removed(), /non-internal participant/);
+ await assert.rejects(removed({ kind: "ambient", live: true }), /non-internal participant/);
});