Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Carry the directory roster into scheduled channel and group turns (#1342)

Sina Matian9add29b13345
diff --git a/src/triggers/run-trigger.ts b/src/triggers/run-trigger.ts
index afa5bf488..59d7a09e8 100644
--- a/src/triggers/run-trigger.ts
+++ b/src/triggers/run-trigger.ts
@@ -16,6 +16,7 @@ import { userFacingFailureClause } from "../core/failure-copy.ts";
import { principalDestination, reachEnqueue } from "../reach/reach.ts";
import { consentRequiredRecipient, recipientConsentSatisfied } from "./trigger-store.ts";
import { isVisible, type VisibilityDirectory } from "../directory/visibility.ts";
+import type { DirectoryStore } from "../directory/directory-store.ts";
import { samePerson } from "../directory/person.ts";
import type { CurrentScopeMembers } from "../resolution/scope-membership.ts";
@@ -33,6 +34,7 @@ export interface TriggerDeps {
get(principalId: string): Promise<{ displayName: string } | null>;
channelPrivacy?(channelId: string): Promise<boolean | undefined>;
groupMembership?(groupId: string, principalId: string): Promise<boolean | undefined>;
+ conversationMembers?: DirectoryStore["conversationMembers"];
};
sessions?: { listByParticipant(principalId: string): Promise<readonly { scopeId: ScopeId }[]> };
}
@@ -205,10 +207,15 @@ export async function runTrigger(deps: TriggerDeps, spec: TriggerSpec): Promise<
const { kind: ownerKind, ref: ownerRef } = parseScopeId(spec.ownerScopeId);
const threadRef = spec.threadRef ?? spec.fireKey;
let conversation: TurnRequest["conversation"] = { kind: "dm", threadRef };
- if (ownerKind === "channel") {
- conversation = { kind: "channel", channelRef: ownerRef, threadRef, ...(audience ? { audience } : {}) };
- } else if (ownerKind === "group") {
- conversation = { kind: "group", channelRef: ownerRef, threadRef, ...(audience ? { audience } : {}) };
+ if (ownerKind === "channel" || ownerKind === "group") {
+ const roster = await deps.directory?.conversationMembers?.(ownerKind, ownerRef).catch(() => undefined);
+ conversation = {
+ kind: ownerKind,
+ channelRef: ownerRef,
+ threadRef,
+ ...(audience ? { audience } : {}),
+ ...(roster ? { publishMembers: roster.map((m) => ({ externalId: m.principalId })) } : {}),
+ };
}
let homeAccess: { ok: boolean; note?: string };
diff --git a/test/orchestrator.test.ts b/test/orchestrator.test.ts
index 1eedd1ccb..371a40d5e 100644
--- a/test/orchestrator.test.ts
+++ b/test/orchestrator.test.ts
@@ -21,6 +21,10 @@ import { encodeRef, serviceCredRef } from "../src/acl/resource-ref.ts";
import type { AclStore } from "../src/acl/acl-store.ts";
import type { ScopeId } from "../src/types.ts";
import type { SecurityScreener } from "../src/security/security-screener.ts";
+import { runTrigger } from "../src/triggers/run-trigger.ts";
+import { createDirectoryStore } from "../src/directory/directory-store.ts";
+import { createIdempotencyStore } from "../src/idempotency/idempotency-store.ts";
+import { createMemoryMap } from "../src/persistence/durable-map.ts";
function freshApp(overrides: Partial<Config> = {}, securityScreener?: SecurityScreener) {
const config = testConfig({
@@ -734,6 +738,78 @@ test("env-delivery injection is all-internal only, and an existing env key (keyc
assert.equal(captures.at(-1)?.env?.STEEL_API_KEY, undefined, "a room with externals gets no org env credentials");
});
+test("a channel cron receives env credentials only when the directory proves an all-internal roster", async () => {
+ const config = testConfig({
+ dataDir: mkdtempSync(join(tmpdir(), "ap-")),
+ signingSecret: "test-secret",
+ apiBaseUrl: "https://core.example.com",
+ });
+ const { app, sandbox, serviceCreds, acl, deliveries, identity } = buildApp(config);
+ const org = scopeId("org", "default-org");
+ await serviceCreds.setServiceCredential(org, {
+ slug: "browse-steel",
+ name: "Steel",
+ delivery: "env",
+ envKey: "STEEL_API_KEY",
+ secret: "steel-org-key",
+ host: "",
+ });
+ await grantCred(acl, org, "browse-steel");
+ const captures: ProvisionOptions[] = [];
+ const realProvision = sandbox.provision.bind(sandbox);
+ sandbox.provision = (layers, opts) => {
+ if (opts) captures.push(opts);
+ return realProvision(layers, opts);
+ };
+ const directory = createDirectoryStore();
+ await directory.replace([
+ { principalId: "U1", displayName: "One", type: "internal" },
+ { principalId: "U2", displayName: "Two", type: "internal" },
+ ]);
+ await directory.replaceChannels(
+ [
+ { channelId: "C-internal", name: "internal" },
+ { channelId: "C-unsynced", name: "unsynced" },
+ { channelId: "C-shared", name: "shared", isExternal: true },
+ { channelId: "C-guest", name: "guest" },
+ ],
+ [
+ { channelId: "C-internal", principalId: "U1" },
+ { channelId: "C-internal", principalId: "U2" },
+ { channelId: "C-shared", principalId: "U1" },
+ { channelId: "C-shared", principalId: "U2" },
+ { channelId: "C-guest", principalId: "U1" },
+ { channelId: "C-guest", principalId: "visitor" },
+ ],
+ undefined,
+ ["C-internal", "C-shared", "C-guest"],
+ );
+ const fire = async (channelId: string) => {
+ const out = await runTrigger(
+ {
+ deliveries,
+ idempotency: createIdempotencyStore(createMemoryMap()),
+ identity,
+ run: (req) => app.turn(req),
+ directory,
+ },
+ {
+ owner: "U1",
+ ownerScopeId: scopeId("channel", channelId),
+ input: "!run echo keys",
+ fireKey: `cron:${channelId}:1`,
+ surface: "cron",
+ },
+ );
+ assert.equal(out.status, "ok", `the ${channelId} cron turn runs`);
+ return captures.at(-1)?.env?.STEEL_API_KEY;
+ };
+ assert.equal(await fire("C-internal"), "steel-org-key", "an all-internal synced roster admits the env credential");
+ assert.equal(await fire("C-unsynced"), undefined, "a channel with no synced roster stays fail-closed");
+ assert.equal(await fire("C-shared"), undefined, "an externally shared channel stays fail-closed");
+ assert.equal(await fire("C-guest"), undefined, "a roster with a non-internal principal stays fail-closed");
+});
+
test("env-delivery credentials are gated by service-cred grants — no grant, no env var; a person grant admits only that person", async () => {
const config = testConfig({
dataDir: mkdtempSync(join(tmpdir(), "ap-")),