Multiplayer agent harness for work.
Surface named environments in admin (#360)
Joshua France76b27d2f125c
diff --git a/plugins/admin/public/index.html b/plugins/admin/public/index.htmlindex e7c0367b0..d06d4996a 100644--- a/plugins/admin/public/index.html+++ b/plugins/admin/public/index.html@@ -2905,6 +2905,30 @@text-overflow: ellipsis;white-space: nowrap;}+ .environment-notice {+ display: flex;+ align-items: center;+ justify-content: space-between;+ gap: 18px;+ margin: 0 0 18px;+ padding: 14px 16px;+ border: 1px solid color-mix(in srgb, var(--warn) 42%, var(--border));+ border-radius: 10px;+ background: color-mix(in srgb, var(--warn) 8%, var(--surface));+ }+ .environment-notice strong,+ .environment-notice p {+ display: block;+ margin: 0;+ }+ .environment-notice p {+ margin-top: 3px;+ color: var(--muted);+ font-size: 12px;+ }+ .environment-notice button {+ flex: none;+ }.governance-overview {margin: 0 0 22px;padding: 18px 20px;@@ -3987,6 +4011,13 @@<span>Scope</span><strong id="governance-scope-label">Organization</strong></div></div>+ <aside class="environment-notice hidden" id="environment-notice" role="status">+ <div>+ <strong id="environment-notice-title"></strong>+ <p id="environment-notice-detail"></p>+ </div>+ <button type="button" id="environment-notice-open">Open environment</button>+ </aside><section class="governance-overview" id="governance-overview" aria-labelledby="governance-overview-title"><div class="governance-overview-head"><h2 id="governance-overview-title">Effective state</h2>@@ -5717,6 +5748,7 @@});let scopeDir = null;+ let environmentDir = [];let scopeDirNote = "Loading scopes…";async function loadScopeDirectory() {const r = await api("GET", "/api/scopes");@@ -5728,6 +5760,7 @@}viewLoadedAt.history = Date.now();scopeDir = r.data.scopes || [];+ environmentDir = r.data.environments || [];if (SCOPED.has(view) && !urlToState().session) {const memoryEditor = view === "memory" && !(orgWideView() && urlToState().mem !== "edit");@@ -6244,6 +6277,19 @@);}window.addEventListener("scroll", syncGovernanceSectionNav, { passive: true });+ function renderEnvironmentNotice(data) {+ const notice = $("environment-notice");+ const attachment = data?.environmentAttachment;+ notice.classList.toggle("hidden", !attachment);+ if (!attachment) return;+ const name = attachment.environmentName || shortName(attachment.environmentId);+ $("environment-notice-title").textContent = "Uses named environment " + name;+ $("environment-notice-detail").textContent =+ "Computer files and working memory resolve to this environment. Governance and conversation history remain scoped here.";+ $("environment-notice-open").textContent = "Open " + name;+ $("environment-notice-open").onclick = () =>+ go({ view: "governance", scope: attachment.environmentId, session: null, page: 1 });+ }let governanceReq = 0;async function loadScope() {const requestedScope = scope;@@ -6259,6 +6305,7 @@);return;}+ renderEnvironmentNotice(r.data);renderGovernanceOverview(r.data);syncGovernanceSectionNav();loadedCommandPolicyPresent = r.data.commandPolicy != null;@@ -11451,6 +11498,21 @@actions: [sortControl],});const activityTime = (s) => (scopeSort === "human" ? s.lastConversationActivity || 0 : s.lastActivity || 0);+ if (environmentDir.length) {+ const environments = denseList(+ environmentDir,+ (environment) => ({+ name: environment.name || shortName(environment.id),+ preview: plural(environment.attachedScopes?.length || 0, "attached scope"),+ href: stateToUrl({ view: "history", scope: environment.id, historyKind }),+ }),+ (environment) => selectScope(environment.id),+ "No named environments.",+ );+ root.appendChild(+ dataCard("Named environments", "Named computers and working memory that scopes can share.", environments),+ );+ }const t = denseList(activeRows,(s) => {diff --git a/plugins/admin/test/environments.test.ts b/plugins/admin/test/environments.test.tsnew file mode 100644index 000000000..3ec8c9fb8--- /dev/null+++ b/plugins/admin/test/environments.test.ts@@ -0,0 +1,13 @@+import test from "node:test";+import assert from "node:assert/strict";+import { readFileSync } from "node:fs";+import { join } from "node:path";++const html = readFileSync(join(import.meta.dirname, "../public/index.html"), "utf8");++test("the admin UI lists named environments and links attachment warnings", () => {+ assert.match(html, /Named environments/);+ assert.match(html, /id="environment-notice"/);+ assert.match(html, /Uses named environment/);+ assert.match(html, /scope: attachment\.environmentId/);+});diff --git a/src/api/routes/admin/scope-config.ts b/src/api/routes/admin/scope-config.tsindex 22acc4f7e..17d206168 100644--- a/src/api/routes/admin/scope-config.ts+++ b/src/api/routes/admin/scope-config.ts@@ -132,10 +132,25 @@ export async function listAdminScopes(ctx: ApiCtx): Promise<void> {const crons = await app.listCrons();const deployments = await app.listDeployments();const skills = await app.listSkills();+ const environmentRows = await app.listEnvironments();+ const environments = environmentRows.map(({ environment, attachments }) => ({+ id: environment.id,+ name: environment.name,+ ownerActorId: environment.ownerActorId,+ attachedScopes: attachments.map((attachment) => attachment.scopeId).sort(),+ }));+ const environmentById = new Map(environments.map((environment) => [environment.id, environment]));+ const attachmentByScope = new Map(+ environments.flatMap((environment) =>+ environment.attachedScopes.map((attachedScope) => [attachedScope, environment] as const),+ ),+ );const owners = [...crons.map((c) => c.ownerScopeId),...deployments.map((d) => d.ownerScopeId),...skills.map((s) => s.scopeId),+ ...environments.map((environment) => environment.id),+ ...environments.flatMap((environment) => environment.attachedScopes),];const labels = await discoverScopes(app, deps, owners);const countBy = (ids: string[]): Map<string, number> => {@@ -171,18 +186,31 @@ export async function listAdminScopes(ctx: ApiCtx): Promise<void> {const cronN = countBy(crons.map((c) => c.ownerScopeId));const deployN = countBy(deployments.map((d) => d.ownerScopeId));const skillN = countBy(skills.map((s) => s.scopeId));- const scopes = [...labels].map(([id, label]) => ({- scopeId: id,- ...(label ? { label } : {}),- sessions: sessionN.get(id) ?? 0,- backgroundSessions: backgroundN.get(id) ?? 0,- lastActivity: lastActivityBy.get(id) ?? 0,- lastConversationActivity: lastConversationBy.get(id) ?? 0,- lastMessage: lastMessageBy.get(id) ?? "",- crons: cronN.get(id) ?? 0,- deployments: deployN.get(id) ?? 0,- skills: skillN.get(id) ?? 0,- }));+ const scopes = [...labels].map(([id, label]) => {+ const environment = environmentById.get(id);+ const attachment = attachmentByScope.get(id);+ return {+ scopeId: id,+ ...(label ? { label } : {}),+ ...(environment?.name ? { environmentName: environment.name } : {}),+ ...(attachment+ ? {+ environmentAttachment: {+ environmentId: attachment.id,+ environmentName: attachment.name,+ },+ }+ : {}),+ sessions: sessionN.get(id) ?? 0,+ backgroundSessions: backgroundN.get(id) ?? 0,+ lastActivity: lastActivityBy.get(id) ?? 0,+ lastConversationActivity: lastConversationBy.get(id) ?? 0,+ lastMessage: lastMessageBy.get(id) ?? "",+ crons: cronN.get(id) ?? 0,+ deployments: deployN.get(id) ?? 0,+ skills: skillN.get(id) ?? 0,+ };+ });scopes.sort((a, b) =>b.lastActivity - a.lastActivity ||@@ -190,7 +218,35 @@ export async function listAdminScopes(ctx: ApiCtx): Promise<void> {b.backgroundSessions - a.backgroundSessions ||a.scopeId.localeCompare(b.scopeId),);- return sendJson(res, 200, { scopeId: scope, scopes });+ return sendJson(res, 200, { scopeId: scope, scopes, environments });+}++interface ScopeEnvironmentMetadata {+ environment?: { id: string; name: string; ownerActorId: string | null };+ environmentAttachment?: { environmentId: string; environmentName: string | null };+}++async function scopeEnvironmentMetadata(deps: ApiCtx["deps"], targetScope: string): Promise<ScopeEnvironmentMetadata> {+ const store = deps.environments;+ if (!store) return {};++ const [environment, attachment] = await Promise.all([store.get(targetScope), store.getAttachment(targetScope)]);+ const metadata: ScopeEnvironmentMetadata = {};+ if (environment?.name) {+ metadata.environment = {+ id: environment.id,+ name: environment.name,+ ownerActorId: environment.ownerActorId,+ };+ }+ if (attachment) {+ const attachedEnvironment = await store.get(attachment.environmentId);+ metadata.environmentAttachment = {+ environmentId: attachment.environmentId,+ environmentName: attachedEnvironment?.name ?? null,+ };+ }+ return metadata;}export async function getScopeConfig(ctx: ApiCtx): Promise<void> {@@ -202,6 +258,7 @@ export async function getScopeConfig(ctx: ApiCtx): Promise<void> {if (!actor) return;await deps.config.refreshScope(targetScope);audit(deps, { principalId: actor.id, action: "config.read", resource: "config", scopeLabel: targetScope });+ const environmentMetadata = await scopeEnvironmentMetadata(deps, targetScope);const serviceCredentials = await Promise.all((deps.serviceCreds ? await deps.serviceCreds.listServiceCredentials(targetScope) : []).map(async (c) => {const usage = (await deps.credentialUsage?.list({ slug: c.slug, limit: 5000 })) ?? [];@@ -261,6 +318,7 @@ export async function getScopeConfig(ctx: ApiCtx): Promise<void> {};return sendJson(res, 200, {scopeId: targetScope,+ ...environmentMetadata,...values,soulVersion: deps.config.soulVersion(targetScope),soulHistory: deps.config.soulHistory(targetScope),diff --git a/test/admin-scopes-directory.test.ts b/test/admin-scopes-directory.test.tsindex dcb280e50..b604750af 100644--- a/test/admin-scopes-directory.test.ts+++ b/test/admin-scopes-directory.test.ts@@ -16,6 +16,8 @@ function start() {admin: built.admin,auditLog: built.auditLog,sessions: built.sessions,+ config: built.config,+ environments: built.environments,});server.listen(0);const base = `http://localhost:${(server.address() as AddressInfo).port}`;@@ -118,3 +120,34 @@ test("scopes without a session label fall back to the org directory (people's naawait s.close();}});++test("the admin scope directory exposes named environments and attached scopes", async () => {+ const s = start();+ try {+ await s.built.app.upsertChannels([+ { channelId: "A", name: "source" },+ { channelId: "B", name: "attached" },+ ]);+ await s.built.app.createEnvironment({ scopeId: "channel:A", name: "A-permanent", actorId: "U1" });+ await s.built.app.attachScope({ scopeId: "channel:B", environmentId: "channel:A", actorId: "U1" });++ const directory = await json(await fetch(`${s.base}/v1/admin/scopes`, { headers: ALICE_ADMIN }));+ const byId = new Map(directory.scopes.map((row: any) => [row.scopeId, row]));+ assert.deepEqual(directory.environments, [+ { id: "channel:A", name: "A-permanent", ownerActorId: "U1", attachedScopes: ["channel:B"] },+ ]);+ assert.equal((byId.get("channel:A") as any).environmentName, "A-permanent");+ assert.deepEqual((byId.get("channel:B") as any).environmentAttachment, {+ environmentId: "channel:A",+ environmentName: "A-permanent",+ });++ const attached = await json(await fetch(`${s.base}/v1/admin/scopes/channel:B`, { headers: ALICE_ADMIN }));+ assert.deepEqual(attached.environmentAttachment, {+ environmentId: "channel:A",+ environmentName: "A-permanent",+ });+ } finally {+ await s.close();+ }+});