mirror of
https://github.com/block/buzz.git
synced 2026-11-26 03:01:38 +00:00
Reused managed identities now apply their intended inbound-author policy before attachment or startup. An explicit policy wins, then the linked persona policy, then `owner-only`. This covers both channel-agent provisioning and the direct composer autocomplete path. If the policy update fails, attachment, startup, and message send stop. The production Playwright regression starts with a persona-linked agent set to `anyone` with a stale allowlist, then verifies the update happens before membership and startup and that the stored policy is `owner-only`. A small resolver test covers explicit, persona, and default precedence. This closes the stale-policy entry described by [Project Loupe issue #497](https://github.com/project-loupe/audit-buzz/issues/497). The separate Welcome Team reuse path is outside this PR. Checked with the 5,502-test desktop suite, `pnpm check`, `pnpm typecheck`, `pnpm check:file-sizes`, and the focused Playwright composer regression. --------- Signed-off-by: Jordan Mecom <jm@squareup.com>
137 lines
3.7 KiB
TypeScript
137 lines
3.7 KiB
TypeScript
import type {
|
|
AgentPersona,
|
|
CreateManagedAgentInput,
|
|
ManagedAgent,
|
|
} from "@/shared/api/types";
|
|
|
|
/** Inline normalization — avoids runtime dependency on @/shared/lib/pubkey. */
|
|
function normalizePubkey(pubkey: string): string {
|
|
return pubkey.trim().toLowerCase();
|
|
}
|
|
|
|
function commandBasename(command: string) {
|
|
const normalized = command.trim().replace(/\\/g, "/");
|
|
const parts = normalized.split("/");
|
|
return parts[parts.length - 1] ?? normalized;
|
|
}
|
|
|
|
function normalizeCommandIdentity(command: string) {
|
|
const lower = commandBasename(command).toLowerCase();
|
|
if (lower === "claude-code-acp" || lower === "claude-agent-acp") {
|
|
return "claude-acp";
|
|
}
|
|
return lower;
|
|
}
|
|
|
|
export function commandsMatch(left: string, right: string) {
|
|
return normalizeCommandIdentity(left) === normalizeCommandIdentity(right);
|
|
}
|
|
|
|
export function parseTimestamp(value: string | null | undefined) {
|
|
if (!value) {
|
|
return 0;
|
|
}
|
|
|
|
const timestamp = Date.parse(value);
|
|
return Number.isNaN(timestamp) ? 0 : timestamp;
|
|
}
|
|
|
|
export function pickPreferredManagedAgent(agents: ManagedAgent[]) {
|
|
return [...agents].sort((left, right) => {
|
|
const leftRunningScore =
|
|
left.status === "running" || left.status === "deployed" ? 1 : 0;
|
|
const rightRunningScore =
|
|
right.status === "running" || right.status === "deployed" ? 1 : 0;
|
|
if (leftRunningScore !== rightRunningScore) {
|
|
return rightRunningScore - leftRunningScore;
|
|
}
|
|
|
|
return parseTimestamp(right.updatedAt) - parseTimestamp(left.updatedAt);
|
|
})[0];
|
|
}
|
|
|
|
export function findReusablePersonaAgent(
|
|
agents: ManagedAgent[],
|
|
personaId: string,
|
|
channelMemberPubkeys: ReadonlySet<string>,
|
|
): ManagedAgent | undefined {
|
|
const candidates = agents.filter(
|
|
(agent) =>
|
|
agent.personaId === personaId &&
|
|
!channelMemberPubkeys.has(normalizePubkey(agent.pubkey)),
|
|
);
|
|
return pickPreferredManagedAgent(candidates);
|
|
}
|
|
|
|
export function findReusableGenericAgent(
|
|
agents: ManagedAgent[],
|
|
command: string,
|
|
channelMemberPubkeys: ReadonlySet<string>,
|
|
): ManagedAgent | undefined {
|
|
const candidates = agents.filter(
|
|
(agent) =>
|
|
!agent.personaId &&
|
|
!agent.systemPrompt?.trim() &&
|
|
commandsMatch(agent.agentCommand, command) &&
|
|
!channelMemberPubkeys.has(normalizePubkey(agent.pubkey)),
|
|
);
|
|
return pickPreferredManagedAgent(candidates);
|
|
}
|
|
|
|
/**
|
|
* Check if a reusable agent exists for the given input. Used by the UI to
|
|
* surface the "reuse vs create new" guardrail before submission.
|
|
*/
|
|
export function findReusableAgent(
|
|
agents: ManagedAgent[],
|
|
channelMemberPubkeys: ReadonlySet<string>,
|
|
input: {
|
|
personaId?: string | null;
|
|
systemPrompt?: string;
|
|
command: string;
|
|
},
|
|
): ManagedAgent | undefined {
|
|
if (input.personaId) {
|
|
return findReusablePersonaAgent(
|
|
agents,
|
|
input.personaId,
|
|
channelMemberPubkeys,
|
|
);
|
|
}
|
|
if (!input.systemPrompt?.trim()) {
|
|
return findReusableGenericAgent(
|
|
agents,
|
|
input.command,
|
|
channelMemberPubkeys,
|
|
);
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export function resolveReusableAgentAccessPolicy(
|
|
request: Pick<CreateManagedAgentInput, "respondTo" | "respondToAllowlist">,
|
|
persona?: Pick<AgentPersona, "respondTo" | "respondToAllowlist">,
|
|
) {
|
|
const requestedAllowlist = request.respondToAllowlist ?? [];
|
|
if (request.respondTo !== undefined) {
|
|
return {
|
|
respondTo: request.respondTo,
|
|
respondToAllowlist: [...requestedAllowlist],
|
|
};
|
|
}
|
|
if (persona?.respondTo != null) {
|
|
return {
|
|
respondTo: persona.respondTo,
|
|
respondToAllowlist: [
|
|
...(requestedAllowlist.length > 0
|
|
? requestedAllowlist
|
|
: persona.respondToAllowlist),
|
|
],
|
|
};
|
|
}
|
|
return {
|
|
respondTo: "owner-only" as const,
|
|
respondToAllowlist: [...requestedAllowlist],
|
|
};
|
|
}
|