@fungi.computer/oyster 0.43.0 → 0.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,6 +41,7 @@ import { createAgentComputers, } from "../agent-computers.js";
41
41
  import { createAgentComputerSource, readAgentComputerConnections, } from "../agent-computer-source.js";
42
42
  import { standaloneAgentComputerPorts, standaloneBrowserSave, } from "./agent-computers.js";
43
43
  import { FleetGrantRefused, } from "@fungi.computer/computer/fleet";
44
+ import { vaultExecutionContext, } from "@fungi.computer/computer";
44
45
  import { homeExtension, homeGrant, } from "@fungi.computer/shiitake-extensions/home/host";
45
46
  import { homeContract, homeInstructionsLimit, } from "@fungi.computer/shiitake-extensions/home";
46
47
  import { resolveHome } from "@fungi.computer/shiitake-extensions/home/host";
@@ -50,6 +51,7 @@ import { ShiitakeDO } from "@fungi.computer/shiitake/cloudflare";
50
51
  import { agentStoreKeeping } from "./agent-store.js";
51
52
  import { CredentialError, } from "@fungi.computer/veil/credential-store";
52
53
  import { createInstanceCredentialStore, createModelKeyVault, instanceCredentialKeyring, instanceSecretCustody, ModelKeyRefused, } from "./model-key-vault.js";
54
+ import { createModelKeyUsage } from "./model-key-usage.js";
53
55
  import { agentArchiveOwner } from "./agent-archive-owner.js";
54
56
  import { agentArchiveProjection } from "../agent-archive-projection.js";
55
57
  import { standaloneBrowserLiveView, standaloneBrowserSource, } from "./browser.js";
@@ -97,13 +99,15 @@ const logRedactedFailure = (label, cause) => {
97
99
  console.error(label, text);
98
100
  });
99
101
  };
100
- /** The starter has no Home yet: the owner has not attached a Computer. The seed waits for one. */
101
- class StarterHomePending extends Error {
102
- constructor() {
103
- super("The starter Agent has no Home Computer yet");
104
- this.name = "StarterHomePending";
105
- }
106
- }
102
+ /** The starter's Home attachment: one fixed id, so a replayed create attaches once. */
103
+ const starterHomeAttachment = "starter-home:v1";
104
+ /** The Agent's own storage key for the setup prompt it carries while it has no Home. */
105
+ const firstConversationKey = "first-conversation";
106
+ /** The kept prompt, and the owner whose grant writes it to a later Home. */
107
+ const firstConversationSchema = z.object({
108
+ content: z.string().min(1),
109
+ actor: z.string().min(1),
110
+ });
107
111
  const noHomeInstructions = {
108
112
  home: null,
109
113
  content: null,
@@ -743,11 +747,22 @@ export class StandaloneAgentBase extends ShiitakeDO {
743
747
  const pin = await this.policyDirectory();
744
748
  if (pin?.directoryName !== captured.scopeId)
745
749
  throw new FleetGrantRefused();
746
- return this.env.AGENT_DIRECTORY.getByName(pin.directoryName).admitAgentComputer({
750
+ const grant = await this.env.AGENT_DIRECTORY.getByName(pin.directoryName).admitAgentComputer({
747
751
  scopeId: captured.scopeId,
748
752
  agentId: captured.agentId,
749
753
  attachment: captured.attachment,
750
754
  }, pin.tenantId);
755
+ return {
756
+ computerId: grant.computerId,
757
+ actions: new Set(grant.actions),
758
+ expiresAt: grant.expiresAt,
759
+ provenance: grant.provenance,
760
+ // Whose Vault secrets this Agent's commands may name: its directory's.
761
+ egress: vaultExecutionContext({
762
+ owner: pin.directoryName,
763
+ subject: { kind: "agent", id: captured.agentId },
764
+ }),
765
+ };
751
766
  },
752
767
  };
753
768
  }
@@ -1148,6 +1163,59 @@ export class StandaloneAgentBase extends ShiitakeDO {
1148
1163
  async readModelChoice() {
1149
1164
  return this.choice().read();
1150
1165
  }
1166
+ /**
1167
+ * Keep the starter's setup prompt on this Agent while it has no Home to hold
1168
+ * an AGENTS.md. Written once; Agents that never needed it have no such key.
1169
+ * `actor` is the owner whose grant later writes it into a Home.
1170
+ */
1171
+ async seedFirstConversation(content, actor) {
1172
+ if ((await this.ctx.storage.get(firstConversationKey)) === undefined)
1173
+ await this.ctx.storage.put(firstConversationKey, firstConversationSchema.parse({ content, actor }));
1174
+ }
1175
+ /** The instructions every run of this Agent starts from. */
1176
+ async agentInstructions() {
1177
+ const { appApprovalInstructions } = await import("../mcp.js");
1178
+ const first = await this.firstConversation();
1179
+ return {
1180
+ revision: first === null ? "oyster-7" : "oyster-7+first-conversation",
1181
+ text: instructions +
1182
+ "\n" +
1183
+ appApprovalInstructions +
1184
+ (first === null ? "" : "\n\n" + first),
1185
+ };
1186
+ }
1187
+ /**
1188
+ * The kept setup prompt, until a Home exists. Then it moves into that Home's
1189
+ * AGENTS.md when the Home has none (the starter seed's rule), so an
1190
+ * interview that gains a Home midway keeps its instructions, and the kept
1191
+ * copy is dropped. A Home that already has an AGENTS.md is left as it is.
1192
+ */
1193
+ async firstConversation() {
1194
+ const stored = await this.ctx.storage.get(firstConversationKey);
1195
+ if (stored === undefined)
1196
+ return null;
1197
+ const { content, actor } = firstConversationSchema.parse(stored);
1198
+ const attachments = (await this.preparedComputerSnapshot())?.projection.attachments ?? [];
1199
+ if (attachments.length === 0)
1200
+ return content;
1201
+ // Until the move lands (the Home went away, or its Computer could not
1202
+ // write), the prompt keeps riding in the instructions; a later run retries.
1203
+ let moved;
1204
+ try {
1205
+ moved = await this.replaceInstructions(actor, null, content);
1206
+ }
1207
+ catch (cause) {
1208
+ logRedactedFailure("starter setup prompt move failed", cause);
1209
+ return content;
1210
+ }
1211
+ if ("error" in moved && moved.error !== "conflict") {
1212
+ if (moved.error === "too_large")
1213
+ logRedactedFailure("starter setup prompt move failed", new Error("The setup prompt exceeds the AGENTS.md limit"));
1214
+ return content;
1215
+ }
1216
+ await this.ctx.storage.delete(firstConversationKey);
1217
+ return null;
1218
+ }
1151
1219
  async setModelChoice(input) {
1152
1220
  try {
1153
1221
  return await this.choice().set(input);
@@ -1203,7 +1271,7 @@ export class StandaloneAgentBase extends ShiitakeDO {
1203
1271
  method: request.method,
1204
1272
  headers: [...request.headers],
1205
1273
  body: request.body === null ? null : await request.text(),
1206
- });
1274
+ }, this.ctx.id.name ?? null);
1207
1275
  if (!(sent instanceof Response))
1208
1276
  throw new AgentModelRefused(sent.error === "key_removed"
1209
1277
  ? "key_removed"
@@ -1350,13 +1418,7 @@ export class StandaloneAgentBase extends ShiitakeDO {
1350
1418
  meta: { schema: FungiMessageMetaSchema },
1351
1419
  ...this.mail().host,
1352
1420
  instructions: this.channelInstructions({
1353
- load: async () => {
1354
- const { appApprovalInstructions } = await import("../mcp.js");
1355
- return {
1356
- revision: "oyster-7",
1357
- text: instructions + "\n" + appApprovalInstructions,
1358
- };
1359
- },
1421
+ load: () => this.agentInstructions(),
1360
1422
  }),
1361
1423
  tools: {
1362
1424
  // A run outlasts one ten-minute Computer command; waiting for a
@@ -1555,7 +1617,6 @@ const visitRow = Schema.Struct({
1555
1617
  visited_at: Schema.Number,
1556
1618
  });
1557
1619
  const counterRow = Schema.Struct({ value: Schema.Number });
1558
- /** Names this Oyster's agents and remembers which Sessions the member opened. */
1559
1620
  export class AgentDirectory extends DurableObject {
1560
1621
  archiveProjectionOwner;
1561
1622
  archiveProjection() {
@@ -1620,19 +1681,42 @@ export class AgentDirectory extends DurableObject {
1620
1681
  return this.modelKeys().remove(keyId);
1621
1682
  }
1622
1683
  /** Host-only: the one place a stored key meets a provider request. */
1623
- async sendModelKey(keyId, request) {
1684
+ async sendModelKey(keyId, request,
1685
+ /** The Agent the use is audited to; never its prompt or reply. */
1686
+ agentId = null) {
1687
+ let status = null;
1624
1688
  try {
1625
- return await this.modelKeys().send(keyId, new Request(request.url, {
1689
+ const response = await this.modelKeys().send(keyId, new Request(request.url, {
1626
1690
  method: request.method,
1627
1691
  headers: request.headers.map(([name, value]) => [name, value]),
1628
1692
  body: request.body,
1629
1693
  }));
1694
+ status = response.status;
1695
+ return response;
1630
1696
  }
1631
1697
  catch (error) {
1632
1698
  if (error instanceof ModelKeyRefused)
1633
1699
  return { error: error.reason };
1634
1700
  throw error;
1635
1701
  }
1702
+ finally {
1703
+ this.modelKeyUsage().record({ keyId, agentId, status });
1704
+ }
1705
+ }
1706
+ modelKeyUsageOwner;
1707
+ modelKeyUsage() {
1708
+ return (this.modelKeyUsageOwner ??= createModelKeyUsage({
1709
+ sql: this.ctx.storage.sql,
1710
+ }));
1711
+ }
1712
+ /** The Vault page's one owner door: every stored credential, write-only. */
1713
+ async vault(command) {
1714
+ const { createInstanceVault } = await import("./vault.js");
1715
+ return createInstanceVault({
1716
+ connections: await this.mcpConnections(),
1717
+ modelKeys: () => this.listModelKeys(),
1718
+ modelKeyUsage: (keyId) => this.modelKeyUsage().list(keyId),
1719
+ })(command);
1636
1720
  }
1637
1721
  redactionSet;
1638
1722
  /**
@@ -1723,6 +1807,39 @@ export class AgentDirectory extends DurableObject {
1723
1807
  async removeConnection(connectionId) {
1724
1808
  return (await this.mcpConnections()).remove(connectionId);
1725
1809
  }
1810
+ /** Host-only: save one Vault secret bound to exact destinations. */
1811
+ async addSecret(input) {
1812
+ return (await this.mcpConnections()).addSecret(input);
1813
+ }
1814
+ async grantSecret(connectionId, subject) {
1815
+ return (await this.mcpConnections()).grantSecret(connectionId, subject);
1816
+ }
1817
+ async ungrantSecret(connectionId, subject) {
1818
+ await (await this.mcpConnections()).ungrantSecret(connectionId, subject);
1819
+ }
1820
+ async secretUsage(connectionId) {
1821
+ return (await this.mcpConnections()).secretUsage(connectionId);
1822
+ }
1823
+ /** A Computer opening one Agent command: which secrets it holds now; never a value. */
1824
+ async computerVaultGrants(agentId) {
1825
+ const granted = await (await this.mcpConnections()).projection(agentId);
1826
+ return granted.connections.flatMap((connection) => connection.surface.kind === "http"
1827
+ ? [
1828
+ {
1829
+ name: connection.name,
1830
+ connectionId: connection.connectionId,
1831
+ grantVersion: connection.grantVersion,
1832
+ },
1833
+ ]
1834
+ : []);
1835
+ }
1836
+ /** The Computer gateway's Vault lane: one resolved reference's request, sent here. */
1837
+ async sendComputerVault(admission, data) {
1838
+ return (await this.mcpConnections()).fetchSecret(admission.subject, {
1839
+ connectionId: admission.connectionId,
1840
+ grantVersion: admission.grantVersion,
1841
+ }, data);
1842
+ }
1726
1843
  async connectionTools(connectionId) {
1727
1844
  return (await this.mcpConnections()).tools(connectionId);
1728
1845
  }
@@ -2051,7 +2168,7 @@ export class AgentDirectory extends DurableObject {
2051
2168
  throw new FleetGrantRefused();
2052
2169
  return {
2053
2170
  computerId: held.computerId,
2054
- actions: new Set([
2171
+ actions: [
2055
2172
  "files.read",
2056
2173
  "files.readBytes",
2057
2174
  "files.list",
@@ -2065,7 +2182,7 @@ export class AgentDirectory extends DurableObject {
2065
2182
  "processStart",
2066
2183
  "processList",
2067
2184
  "processKill",
2068
- ]),
2185
+ ],
2069
2186
  expiresAt: Date.now() + 60_000,
2070
2187
  provenance: { actor: oysterPrincipalId(owner), via: captured.agentId },
2071
2188
  };
@@ -2634,6 +2751,11 @@ export function standaloneAgents(env, scope) {
2634
2751
  create: created,
2635
2752
  ensureStarter: (principal) => {
2636
2753
  const owner = directory();
2754
+ const reportStarterFailure = (cause) => {
2755
+ void redactedFailure(cause).then((text) => {
2756
+ console.warn(JSON.stringify({ event: "starter_agent_failed", cause: text }));
2757
+ });
2758
+ };
2637
2759
  return ensureStarterAgent({
2638
2760
  ledger: {
2639
2761
  read: () => owner.readStarter(),
@@ -2641,14 +2763,32 @@ export function standaloneAgents(env, scope) {
2641
2763
  },
2642
2764
  isOwner: () => oysterPrincipalIsOwner(principal),
2643
2765
  agentCount: () => owner.agentTotal(),
2644
- // The ordinary create: the same Computer-less Agent, policy and approval any owner create gets.
2766
+ // The ordinary create, then the ordinary owner attach of the host's own
2767
+ // configured Computer as its Home. A host with no such Computer, or one
2768
+ // the attach refuses, leaves the starter without a Home.
2645
2769
  create: async (request) => {
2646
2770
  const agent = await created(request.name, request.requestId, principal, "owner-direct");
2771
+ const computerId = env.OYSTER_COMPUTER_ID;
2772
+ // The Agent exists now; a failed Home attach must not fail the
2773
+ // create, or the starter ledger is never written. It is reported,
2774
+ // and the seed falls back to the Agent's own record.
2775
+ if (computerId !== undefined && computerId !== "")
2776
+ await directory()
2777
+ .attachComputer({
2778
+ agentId: agent.agentId,
2779
+ attachmentId: starterHomeAttachment,
2780
+ computerId,
2781
+ requestId: starterHomeAttachment,
2782
+ }, principal, scope?.tenantId ?? defaultTenant)
2783
+ .catch((cause) => {
2784
+ reportStarterFailure(cause);
2785
+ });
2647
2786
  return { status: "created", agentId: agent.agentId };
2648
2787
  },
2649
2788
  // No money step on Oyster: the owner brings their own model, so the
2650
2789
  // seed is only the first-run section, written to a Home that has no
2651
- // AGENTS.md. A Home the owner already wrote to is left as it is.
2790
+ // AGENTS.md. A Home the owner already wrote to is left as it is. With
2791
+ // no Home, the same section stays on the Agent until it has one.
2652
2792
  seedHome: async (agentId) => {
2653
2793
  await seedStarterHome(async (path, content) => {
2654
2794
  if (path !== "AGENTS.md")
@@ -2659,18 +2799,11 @@ export function standaloneAgents(env, scope) {
2659
2799
  if (result.error === "conflict")
2660
2800
  return;
2661
2801
  if (result.error === "no_home")
2662
- throw new StarterHomePending();
2802
+ return env.AGENTS.getByName(agentId).seedFirstConversation(content, JSON.stringify(principal));
2663
2803
  throw new Error(`Starter Home seed refused: ${result.error}`);
2664
2804
  });
2665
2805
  },
2666
- report: (cause) => {
2667
- // Waiting for the owner to attach a Computer is not a failure.
2668
- if (cause instanceof StarterHomePending)
2669
- return;
2670
- void redactedFailure(cause).then((text) => {
2671
- console.warn(JSON.stringify({ event: "starter_agent_failed", cause: text }));
2672
- });
2673
- },
2806
+ report: reportStarterFailure,
2674
2807
  });
2675
2808
  },
2676
2809
  rename: async (agentId, name, principal) => {
@@ -2729,6 +2862,7 @@ export function standaloneAgents(env, scope) {
2729
2862
  modelKeys: () => directory().listModelKeys(),
2730
2863
  sealModelKey: (input) => directory().sealModelKey(input),
2731
2864
  removeModelKey: (keyId) => directory().removeModelKey(keyId),
2865
+ vault: (command) => directory().vault(command),
2732
2866
  policyStatus: async (agentId) => directory().policyStatus(agentId),
2733
2867
  appToolPolicy: (actor) => directory().appToolPolicy(actor, scope?.tenantId ?? defaultTenant),
2734
2868
  policyRevision: (agentId) => directory().policyRevision(agentId),
@@ -1,3 +1,25 @@
1
+ /**
2
+ * Standalone Oyster's Computer egress: the same host policy Hub's
3
+ * ComputerEgress decides with. It serves four lanes:
4
+ *
5
+ * - the Vault lane: a request presenting one of this Computer's live `fvr1.`
6
+ * references, which the Agent directory that owns the secret completes
7
+ * (its grant, exact binding, value placement, echo check and audit);
8
+ * - Oyster's own origin Git, carrying only the guest's own credential;
9
+ * - the anonymous public download lane (package registries and release hosts
10
+ * the policy classifies), so a build can install its dependencies, behind
11
+ * the same `COMPUTER_PUBLIC_EGRESS=disabled` kill switch as Hub;
12
+ * - the internal build hosts: a live App build job's read-only clone on
13
+ * `git.team.internal` (an installation repository through the Git door,
14
+ * presented with a host-held job pass the guest never sees, or the App's
15
+ * template through the registry's read-only template exchange), and its
16
+ * `/jobs/:id/*` reports and object uploads on `build.team.internal`, each
17
+ * object streamed into the Release store without passing the registry.
18
+ *
19
+ * Team Git is refused, because this host serves no such lane.
20
+ */
21
+ import { type VaultRequestData } from "@fungi.computer/computer/egress";
22
+ import type { VaultReferenceAdmission } from "@fungi.computer/computer";
1
23
  import { type BuildGitEvent } from "../build-egress.js";
2
24
  import type { ContentObjectOwner } from "../build/content-objects.js";
3
25
  import type { CandidateObject, CandidateObjectMediaType, TeamAppCandidateManifest } from "../build-contract.js";
@@ -6,6 +28,13 @@ import { type GitDoorTarget } from "../git.js";
6
28
  export type OysterComputerEgressProps = Readonly<{
7
29
  computerObjectId: string;
8
30
  }>;
31
+ /** The Vault lane: this Computer resolves a reference, its owner sends. */
32
+ export type OysterComputerVaultPort = Readonly<{
33
+ admit(reference: string): Promise<VaultReferenceAdmission | null>;
34
+ send(admission: VaultReferenceAdmission, data: VaultRequestData): Promise<Response | Readonly<{
35
+ error: string;
36
+ }>>;
37
+ }>;
9
38
  /** The registry's build admission, for the one Computer this egress serves. */
10
39
  export type OysterComputerBuildPort = Readonly<{
11
40
  gitPassFor(target: GitDoorTarget): Promise<string | null>;
@@ -37,5 +66,7 @@ export declare function serveOysterComputerEgress(request: Request, options: Rea
37
66
  /** Live setting from the Computer identified by the host's loopback props. */
38
67
  internetAccess(): Promise<boolean>;
39
68
  build: OysterComputerBuildPort;
69
+ /** Absent: this host offers no Vault lane, and every reference is refused. */
70
+ vault?: OysterComputerVaultPort;
40
71
  forward(request: Request): Promise<Response>;
41
72
  }>): Promise<Response>;
@@ -1,7 +1,10 @@
1
1
  /**
2
2
  * Standalone Oyster's Computer egress: the same host policy Hub's
3
- * ComputerEgress decides with. It serves three lanes:
3
+ * ComputerEgress decides with. It serves four lanes:
4
4
  *
5
+ * - the Vault lane: a request presenting one of this Computer's live `fvr1.`
6
+ * references, which the Agent directory that owns the secret completes
7
+ * (its grant, exact binding, value placement, echo check and audit);
5
8
  * - Oyster's own origin Git, carrying only the guest's own credential;
6
9
  * - the anonymous public download lane (package registries and release hosts
7
10
  * the policy classifies), so a build can install its dependencies, behind
@@ -15,7 +18,8 @@
15
18
  *
16
19
  * Team Git is refused, because this host serves no such lane.
17
20
  */
18
- import { anonymousRequest, anonymousResponse, computerEgressDenied, decideComputerEgress, decidePublicLane, originGitRequest, publicEgressEnabled, recordComputerEgress, } from "@fungi.computer/computer/egress";
21
+ import { anonymousRequest, anonymousResponse, computerEgressDenied, decideComputerEgress, decidePublicLane, originGitRequest, publicEgressEnabled, recordComputerEgress, vaultRefusalStatus, vaultRequestData, } from "@fungi.computer/computer/egress";
22
+ import { connectionBodyLimit } from "@fungi.computer/veil/connections";
19
23
  import { createBuildEgress } from "../build-egress.js";
20
24
  import { oysterBuildGitHost, oysterBuildReportHost } from "../app-builds.js";
21
25
  import { gitDoorRoute, smartHttpExchange } from "../git.js";
@@ -48,7 +52,21 @@ export async function serveOysterComputerEgress(request, options) {
48
52
  if (decision.kind === "denied")
49
53
  return deny(url.hostname, decision.reason);
50
54
  let response;
51
- if (decision.kind === "origin-git")
55
+ if (decision.kind === "vault") {
56
+ if (options.vault === undefined)
57
+ return deny(url.hostname, "lane-not-offered:vault");
58
+ const admission = await options.vault.admit(decision.reference);
59
+ if (admission === null)
60
+ return deny(url.hostname, "vault-reference");
61
+ const prepared = await vaultRequestData(request, url, decision.reference, connectionBodyLimit);
62
+ if (prepared.kind === "refused")
63
+ return deny(url.hostname, `vault-${prepared.reason}`, prepared.reason === "too_large" ? 413 : 403);
64
+ const sent = await options.vault.send(admission, prepared.data);
65
+ if (!(sent instanceof Response))
66
+ return deny(url.hostname, `vault-${sent.error}`, vaultRefusalStatus(sent.error));
67
+ response = sent;
68
+ }
69
+ else if (decision.kind === "origin-git")
52
70
  response = await options.forward(originGitRequest(request, url));
53
71
  else if (decision.kind === "public") {
54
72
  let enabled;
@@ -6,7 +6,7 @@ import { type PreviewSelector } from "./preview.js";
6
6
  import { type StandaloneTenancyEnv } from "./tenancy.js";
7
7
  import * as z from "zod";
8
8
  import { computerProcessSchema, computerToolResultSchema, readOutputSchema } from "@fungi.computer/computer-contract";
9
- import { ComputerDO as NativeComputerDO, type AttachedBackend, type ComputerEnv, type ComputerFilesRpc } from "@fungi.computer/computer";
9
+ import { ComputerDO as NativeComputerDO, type AttachedBackend, type ComputerEnv, type ComputerFilesRpc, type VaultSecretGrant } from "@fungi.computer/computer";
10
10
  import { patchInputSchema, patchOutputSchema } from "@fungi.computer/knapsack/files-patch";
11
11
  import { listOutputSchema, writeOutputSchema, mkdirOutputSchema, moveOutputSchema, removeOutputSchema, computerPortsOutputSchema, computerPortOutputSchema, computerPortRemovalOutputSchema, processListOutputSchema, processKillOutputSchema, processLogsOutputSchema, processWaitOutputSchema, computerOperationResultSchema, type ComputerExecutionHostHooks, type ComputerValue } from "@fungi.computer/computer-contract";
12
12
  import { type WorkflowGitCredential, type WorkflowGitPassGrant } from "@fungi.computer/braid/runtime";
@@ -61,6 +61,14 @@ export type StandaloneComputerOwnerEnv = StandaloneComputerEnv & StandaloneTenan
61
61
  WORKFLOW_RUNS: Readonly<{
62
62
  getByName(libraryId: string): StandaloneWorkflowGitIssuer;
63
63
  }>;
64
+ /** Each Agent directory owns its Agents' Vault secrets. */
65
+ AGENT_DIRECTORY: Readonly<{
66
+ getByName(directoryName: string): StandaloneVaultGrantOwner;
67
+ }>;
68
+ }>;
69
+ /** Which secrets one Agent holds now, for its command's references. */
70
+ export type StandaloneVaultGrantOwner = Readonly<{
71
+ computerVaultGrants(agentId: string): Promise<readonly VaultSecretGrant[]>;
64
72
  }>;
65
73
  /** The library run owner's per-invocation Workflow Git credential ports. */
66
74
  export type StandaloneWorkflowGitIssuer = Readonly<{
@@ -11,7 +11,7 @@ import { standaloneTenancy } from "./tenancy.js";
11
11
  import { Base64 } from "effect/encoding";
12
12
  import * as z from "zod";
13
13
  import { ComputerPortConflictError, computerByteReadReplySchema, computerProcessSchema, installationProcessScope, processStartInputSchema, processStartToolInputSchema, computerCapabilities, computerToolResultSchema, readOutputSchema, } from "@fungi.computer/computer-contract";
14
- import { ComputerDO as NativeComputerDO, ComputerOperationConflictError, ComputerBackendUnavailableError, } from "@fungi.computer/computer";
14
+ import { ComputerDO as NativeComputerDO, ComputerOperationConflictError, ComputerBackendUnavailableError, isVaultExecutionContext, withVaultExecution, } from "@fungi.computer/computer";
15
15
  import { patchInputSchema, patchOutputSchema, } from "@fungi.computer/knapsack/files-patch";
16
16
  import { ComputerFileConflict, ComputerFileMoveError, ComputerFileRemoveError, computerExecutionRequestSchema, computerValueSchema, computerPortInputSchema, listInputSchema, listOutputSchema, readBytesInputSchema, writeInputSchema, replaceInputSchema, writeOutputSchema, mkdirOutputSchema, moveInputSchema, moveOutputSchema, removeOutputSchema, computerPortsOutputSchema, computerPortOutputSchema, computerPortRemovalOutputSchema, processListOutputSchema, processKillOutputSchema, processLogsInputSchema, processWaitInputSchema, processLogsOutputSchema, processWaitOutputSchema, computerOperationResultSchema, mintComputerRequestId, processIdInputSchema, } from "@fungi.computer/computer-contract";
17
17
  import { workflowGitExecutionEnvironment, workflowGitPassGrantSchema, } from "@fungi.computer/braid/runtime";
@@ -460,6 +460,10 @@ export class ComputerDO extends NativeComputerDO {
460
460
  * Computer hands it only to that command's Git environment.
461
461
  */
462
462
  executionHooks() {
463
+ // An Agent command's references come from the directory its grant names.
464
+ const vault = this.vaultExecutionHooks({
465
+ grants: ({ owner, subject }) => this.previewBindings.AGENT_DIRECTORY.getByName(owner).computerVaultGrants(subject.id),
466
+ });
463
467
  const issuer = (context) => {
464
468
  const grant = workflowGitExecutionContextSchema.parse(context).workflowGit;
465
469
  return {
@@ -467,7 +471,7 @@ export class ComputerDO extends NativeComputerDO {
467
471
  runs: this.previewBindings.WORKFLOW_RUNS.getByName(grant.teamId),
468
472
  };
469
473
  };
470
- return {
474
+ return withVaultExecution(vault, {
471
475
  async open(invocation, context) {
472
476
  const { grant, runs } = issuer(context);
473
477
  return {
@@ -482,7 +486,7 @@ export class ComputerDO extends NativeComputerDO {
482
486
  const { grant, runs } = issuer(context);
483
487
  await runs.closeWorkflowGit(invocation.id, grant);
484
488
  },
485
- };
489
+ });
486
490
  }
487
491
  attachedBackends() {
488
492
  if (this.#backends !== undefined)
@@ -524,7 +528,13 @@ export class ComputerDO extends NativeComputerDO {
524
528
  requestId: mintComputerRequestId(),
525
529
  requestScope: `tool:${authority.provenance.actor}`,
526
530
  }), accept);
527
- return output.parse(await this.callAfterAcceptance(tool, input, accept));
531
+ // The host-issued grant names whose Vault references an Agent command holds.
532
+ const vault = (tool === "bash" || tool === "exec") &&
533
+ authority.egress !== undefined &&
534
+ isVaultExecutionContext(authority.egress)
535
+ ? authority.egress
536
+ : undefined;
537
+ return output.parse(await this.callAfterAcceptance(tool, input, accept, vault));
528
538
  }
529
539
  /** JSON operations keep the Computer's maintained parsers and execution engine. */
530
540
  async value(authority, action, input, executionContext) {
@@ -0,0 +1,28 @@
1
+ type ModelKeyUse = Readonly<{
2
+ keyId: string;
3
+ agentId: string | null;
4
+ /** The provider's status, or null when nothing came back. */
5
+ status: number | null;
6
+ }>;
7
+ /** One recorded use, as the Vault lists it. */
8
+ export type ModelKeyUsage = Readonly<{
9
+ keyId: string;
10
+ subject: Readonly<{
11
+ kind: "agent";
12
+ id: string;
13
+ }> | null;
14
+ outcome: "upstream_status" | "failed";
15
+ status: number | null;
16
+ firstAt: number;
17
+ lastAt: number;
18
+ count: number;
19
+ }>;
20
+ export declare function createModelKeyUsage(options: {
21
+ sql: SqlStorage;
22
+ now?: () => number;
23
+ }): {
24
+ record(use: ModelKeyUse): void;
25
+ /** The newest uses of one key, oldest first. */
26
+ list(keyId: string): readonly ModelKeyUsage[];
27
+ };
28
+ export {};
@@ -0,0 +1,70 @@
1
+ import * as Schema from "effect/Schema";
2
+ /**
3
+ * Each use of a stored model key, for the Vault page: which Agent, when, and
4
+ * how the provider answered. Never the value, the prompt or the response.
5
+ * Identical uses within a minute share a row, as Veil's connection audit does;
6
+ * rows older than 90 days, and the oldest beyond the cap, are dropped.
7
+ */
8
+ const retentionMs = 90 * 24 * 60 * 60 * 1_000;
9
+ const coalesceMs = 60_000;
10
+ const rowLimit = 50_000;
11
+ const listLimit = 1_000;
12
+ const usageRow = Schema.Struct({
13
+ key_id: Schema.String,
14
+ agent_id: Schema.NullOr(Schema.String),
15
+ status: Schema.NullOr(Schema.Number),
16
+ first_at: Schema.Number,
17
+ last_at: Schema.Number,
18
+ uses: Schema.Number,
19
+ });
20
+ const decodeRows = Schema.decodeUnknownSync(Schema.Array(usageRow));
21
+ export function createModelKeyUsage(options) {
22
+ const { sql } = options;
23
+ const now = options.now ?? Date.now;
24
+ sql.exec(`CREATE TABLE IF NOT EXISTS oyster_model_key_usage (
25
+ sequence INTEGER PRIMARY KEY AUTOINCREMENT,
26
+ key_id TEXT NOT NULL,
27
+ agent_id TEXT,
28
+ status INTEGER,
29
+ first_at INTEGER NOT NULL,
30
+ last_at INTEGER NOT NULL,
31
+ uses INTEGER NOT NULL
32
+ )`);
33
+ sql.exec("CREATE INDEX IF NOT EXISTS oyster_model_key_usage_key ON oyster_model_key_usage (key_id, sequence)");
34
+ const expire = (at) => {
35
+ sql.exec("DELETE FROM oyster_model_key_usage WHERE last_at <= ?", at - retentionMs);
36
+ };
37
+ return {
38
+ record(use) {
39
+ const at = now();
40
+ expire(at);
41
+ const coalesced = sql
42
+ .exec(`UPDATE oyster_model_key_usage SET last_at = ?, uses = uses + 1
43
+ WHERE sequence = (
44
+ SELECT sequence FROM oyster_model_key_usage
45
+ WHERE key_id = ? AND agent_id IS ? AND status IS ? AND first_at > ?
46
+ ORDER BY sequence DESC LIMIT 1
47
+ ) RETURNING sequence`, at, use.keyId, use.agentId, use.status, at - coalesceMs)
48
+ .toArray();
49
+ if (coalesced.length > 0)
50
+ return;
51
+ sql.exec("DELETE FROM oyster_model_key_usage WHERE sequence IN (SELECT sequence FROM oyster_model_key_usage ORDER BY sequence LIMIT MAX(0, (SELECT COUNT(*) FROM oyster_model_key_usage) - ? + 1))", rowLimit);
52
+ sql.exec("INSERT INTO oyster_model_key_usage (key_id, agent_id, status, first_at, last_at, uses) VALUES (?, ?, ?, ?, ?, 1)", use.keyId, use.agentId, use.status, at, at);
53
+ },
54
+ /** The newest uses of one key, oldest first. */
55
+ list(keyId) {
56
+ expire(now());
57
+ return decodeRows(sql
58
+ .exec("SELECT key_id, agent_id, status, first_at, last_at, uses FROM (SELECT * FROM oyster_model_key_usage WHERE key_id = ? ORDER BY sequence DESC LIMIT ?) ORDER BY sequence", keyId, listLimit)
59
+ .toArray()).map((row) => ({
60
+ keyId: row.key_id,
61
+ subject: row.agent_id === null ? null : { kind: "agent", id: row.agent_id },
62
+ outcome: row.status === null ? "failed" : "upstream_status",
63
+ status: row.status,
64
+ firstAt: row.first_at,
65
+ lastAt: row.last_at,
66
+ count: row.uses,
67
+ }));
68
+ },
69
+ };
70
+ }