@feltdb/feltkit 0.3.29 → 0.3.31

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.
Files changed (185) hide show
  1. package/dist/app.d.ts +101 -11
  2. package/dist/app.js +131 -3
  3. package/dist/app.js.map +1 -1
  4. package/dist/appboundry/build.js +2 -0
  5. package/dist/appboundry/build.js.map +1 -1
  6. package/dist/appboundry/guest-tenancy.d.ts +2 -2
  7. package/dist/appboundry/guest-tenancy.js.map +1 -1
  8. package/dist/appboundry/guest.js +27 -2
  9. package/dist/appboundry/guest.js.map +1 -1
  10. package/dist/appboundry/host.js +3 -0
  11. package/dist/appboundry/host.js.map +1 -1
  12. package/dist/authorize.d.ts +47 -4
  13. package/dist/authorize.js +37 -0
  14. package/dist/authorize.js.map +1 -1
  15. package/dist/cli/deploy-appboundry.d.ts +1 -1
  16. package/dist/cli/deploy-appboundry.js.map +1 -1
  17. package/dist/cli/deploy.js.map +1 -1
  18. package/dist/cli/main.js +27 -1
  19. package/dist/cli/main.js.map +1 -1
  20. package/dist/cli/skills.js.map +1 -1
  21. package/dist/client/index.js.map +1 -1
  22. package/dist/dashboard/assets/index-9rMS3yBm.js +15 -0
  23. package/dist/dashboard/assets/index-DkiYTUMR.css +1 -0
  24. package/dist/dashboard/index.html +2 -2
  25. package/dist/docs.d.ts +1 -1
  26. package/dist/docs.js +4 -0
  27. package/dist/docs.js.map +1 -1
  28. package/dist/errors.js.map +1 -1
  29. package/dist/event-stage.js.map +1 -1
  30. package/dist/execution.d.ts +186 -0
  31. package/dist/execution.js +105 -0
  32. package/dist/execution.js.map +1 -0
  33. package/dist/index.d.ts +10 -2
  34. package/dist/index.js +5 -0
  35. package/dist/index.js.map +1 -1
  36. package/dist/measure/index.js.map +1 -1
  37. package/dist/platform/approvals.js.map +1 -1
  38. package/dist/platform/artifacts.js.map +1 -1
  39. package/dist/platform/changes.d.ts +54 -54
  40. package/dist/platform/changes.js.map +1 -1
  41. package/dist/platform/deployments.d.ts +32 -32
  42. package/dist/platform/exec.js.map +1 -1
  43. package/dist/platform/platform.js.map +1 -1
  44. package/dist/platform/releases.js.map +1 -1
  45. package/dist/platform/server.js.map +1 -1
  46. package/dist/platform/workspaces.js.map +1 -1
  47. package/dist/policy.d.ts +172 -0
  48. package/dist/policy.js +311 -0
  49. package/dist/policy.js.map +1 -0
  50. package/dist/react/auth.d.ts +10 -10
  51. package/dist/react/auth.js +1 -1
  52. package/dist/react/auth.js.map +1 -1
  53. package/dist/react/context.d.ts +1 -1
  54. package/dist/react/index.js.map +1 -1
  55. package/dist/runtime/accounts.js.map +1 -1
  56. package/dist/runtime/activity.d.ts +17 -1
  57. package/dist/runtime/activity.js.map +1 -1
  58. package/dist/runtime/agent.d.ts +3 -3
  59. package/dist/runtime/agent.js +9 -3
  60. package/dist/runtime/agent.js.map +1 -1
  61. package/dist/runtime/application-authority.d.ts +33 -3
  62. package/dist/runtime/application-authority.js +74 -14
  63. package/dist/runtime/application-authority.js.map +1 -1
  64. package/dist/runtime/assemble.d.ts +13 -0
  65. package/dist/runtime/assemble.js +29 -1
  66. package/dist/runtime/assemble.js.map +1 -1
  67. package/dist/runtime/audit.js.map +1 -1
  68. package/dist/runtime/authboundry.d.ts +17 -0
  69. package/dist/runtime/authboundry.js +8 -1
  70. package/dist/runtime/authboundry.js.map +1 -1
  71. package/dist/runtime/authority.d.ts +10 -0
  72. package/dist/runtime/authority.js +17 -0
  73. package/dist/runtime/authority.js.map +1 -1
  74. package/dist/runtime/backup-environment.js.map +1 -1
  75. package/dist/runtime/backup.js.map +1 -1
  76. package/dist/runtime/changes.js.map +1 -1
  77. package/dist/runtime/claims.js.map +1 -1
  78. package/dist/runtime/collections.js.map +1 -1
  79. package/dist/runtime/content.d.ts +77 -2
  80. package/dist/runtime/content.js +153 -6
  81. package/dist/runtime/content.js.map +1 -1
  82. package/dist/runtime/context.d.ts +207 -3
  83. package/dist/runtime/contract.js +16 -3
  84. package/dist/runtime/contract.js.map +1 -1
  85. package/dist/runtime/credentials.d.ts +31 -2
  86. package/dist/runtime/credentials.js +55 -3
  87. package/dist/runtime/credentials.js.map +1 -1
  88. package/dist/runtime/crypto-token.js.map +1 -1
  89. package/dist/runtime/data.d.ts +25 -0
  90. package/dist/runtime/data.js +92 -8
  91. package/dist/runtime/data.js.map +1 -1
  92. package/dist/runtime/decisions.js.map +1 -1
  93. package/dist/runtime/deployment.js.map +1 -1
  94. package/dist/runtime/execution-host.d.ts +54 -0
  95. package/dist/runtime/execution-host.js +54 -0
  96. package/dist/runtime/execution-host.js.map +1 -0
  97. package/dist/runtime/generation.js.map +1 -1
  98. package/dist/runtime/image-variants.d.ts +48 -0
  99. package/dist/runtime/image-variants.js +295 -0
  100. package/dist/runtime/image-variants.js.map +1 -0
  101. package/dist/runtime/instance.js.map +1 -1
  102. package/dist/runtime/integrations.d.ts +8 -6
  103. package/dist/runtime/integrations.js +46 -5
  104. package/dist/runtime/integrations.js.map +1 -1
  105. package/dist/runtime/invocations.d.ts +313 -0
  106. package/dist/runtime/invocations.js +493 -0
  107. package/dist/runtime/invocations.js.map +1 -0
  108. package/dist/runtime/live.d.ts +10 -7
  109. package/dist/runtime/live.js +24 -12
  110. package/dist/runtime/live.js.map +1 -1
  111. package/dist/runtime/loader.js.map +1 -1
  112. package/dist/runtime/local-authority.d.ts +92 -0
  113. package/dist/runtime/local-authority.js +396 -0
  114. package/dist/runtime/local-authority.js.map +1 -0
  115. package/dist/runtime/oauth-credentials.d.ts +40 -3
  116. package/dist/runtime/oauth-credentials.js +133 -25
  117. package/dist/runtime/oauth-credentials.js.map +1 -1
  118. package/dist/runtime/outbox.js +3 -0
  119. package/dist/runtime/outbox.js.map +1 -1
  120. package/dist/runtime/policies.d.ts +89 -0
  121. package/dist/runtime/policies.js +152 -0
  122. package/dist/runtime/policies.js.map +1 -0
  123. package/dist/runtime/projects.d.ts +1 -1
  124. package/dist/runtime/readiness.js +11 -0
  125. package/dist/runtime/readiness.js.map +1 -1
  126. package/dist/runtime/runtime.d.ts +66 -5
  127. package/dist/runtime/runtime.js +328 -18
  128. package/dist/runtime/runtime.js.map +1 -1
  129. package/dist/runtime/services-rules.d.ts +9 -0
  130. package/dist/runtime/services-rules.js +47 -1
  131. package/dist/runtime/services-rules.js.map +1 -1
  132. package/dist/runtime/services.d.ts +154 -9
  133. package/dist/runtime/services.js +343 -14
  134. package/dist/runtime/services.js.map +1 -1
  135. package/dist/runtime/sessions.js.map +1 -1
  136. package/dist/runtime/state-contract.js.map +1 -1
  137. package/dist/runtime/storage-check.js.map +1 -1
  138. package/dist/runtime/targets.d.ts +1 -1
  139. package/dist/runtime/tenancy.js.map +1 -1
  140. package/dist/runtime/transactions.d.ts +15 -0
  141. package/dist/runtime/transactions.js +5 -0
  142. package/dist/runtime/transactions.js.map +1 -1
  143. package/dist/schema.js.map +1 -1
  144. package/dist/server/admin.d.ts +72 -51
  145. package/dist/server/admin.js +125 -0
  146. package/dist/server/admin.js.map +1 -1
  147. package/dist/server/components.js.map +1 -1
  148. package/dist/server/index.js +72 -6
  149. package/dist/server/index.js.map +1 -1
  150. package/dist/server/management.js.map +1 -1
  151. package/dist/server/mcp.d.ts +59 -37
  152. package/dist/server/mcp.js +24 -1
  153. package/dist/server/mcp.js.map +1 -1
  154. package/dist/server/operators.d.ts +1 -0
  155. package/dist/server/operators.js +1 -1
  156. package/dist/server/operators.js.map +1 -1
  157. package/dist/server/ports.d.ts +7 -0
  158. package/dist/server/ports.js +17 -0
  159. package/dist/server/ports.js.map +1 -1
  160. package/dist/server/production.js.map +1 -1
  161. package/dist/server/project.js.map +1 -1
  162. package/dist/server/session-http.js.map +1 -1
  163. package/dist/server/tenancy.js.map +1 -1
  164. package/dist/surfaces.d.ts +153 -0
  165. package/dist/surfaces.js +228 -0
  166. package/dist/surfaces.js.map +1 -0
  167. package/dist/testing/index.d.ts +11 -0
  168. package/dist/testing/index.js +7 -0
  169. package/dist/testing/index.js.map +1 -1
  170. package/docs/AGENT-OPERATIONS.md +1 -1
  171. package/docs/ARCHITECTURE.md +2 -1
  172. package/docs/CONNECTIONS.md +151 -0
  173. package/docs/EXECUTION.md +271 -0
  174. package/docs/GUARANTEES.md +134 -8
  175. package/docs/INVOCATIONS.md +170 -0
  176. package/docs/POLICIES.md +121 -0
  177. package/docs/README.md +6 -2
  178. package/docs/SECURITY.md +30 -0
  179. package/docs/TRANSACTIONS.md +4 -0
  180. package/package.json +27 -19
  181. package/skills/.claude-plugin/marketplace.json +1 -1
  182. package/skills/.claude-plugin/plugin.json +1 -1
  183. package/skills/feltkit-services/SKILL.md +1 -1
  184. package/dist/dashboard/assets/index-BehzsQOa.css +0 -1
  185. package/dist/dashboard/assets/index-UuZ63NA6.js +0 -15
@@ -21,6 +21,12 @@ export type FileLinkGrant = {
21
21
  fileId: string;
22
22
  expiresAt: number;
23
23
  disposition: "attachment" | "inline";
24
+ /** The checksum of the content the link was issued for: other content behind the file is not served by it. */
25
+ checksum?: string;
26
+ /** A declared image variant (`services.files.variants`) to serve instead of the original. */
27
+ variant?: string;
28
+ /** A cacheable link (`files.link(id, { cache: true })`): served `private, max-age=<remaining>, immutable`. */
29
+ cache?: true;
24
30
  } | {
25
31
  kind: "upload";
26
32
  expiresAt: number;
@@ -36,8 +42,34 @@ export type FileLinkGrant = {
36
42
  maxBytes: number;
37
43
  metadata?: Record<string, string | number | boolean>;
38
44
  };
45
+ /** A cacheable link's token: the same scope (file, content, variant, caller, window…) gives the same token. */
46
+ export declare function derivedLinkToken(scope: readonly unknown[]): string;
39
47
  /** Where file bytes are kept, one row per file. */
40
48
  export declare const FILE_CONTENT_COLLECTION = "_feltkit_file_content";
49
+ /**
50
+ * Derived renditions of stored bytes (image variants), one row per source storage key and variant
51
+ * name. A row is derived content: produced from its source, released with it (`releaseVariants`),
52
+ * and never served except through a link to a file whose bytes it was derived from.
53
+ */
54
+ export declare const FILE_VARIANT_COLLECTION = "_feltkit_file_variants";
55
+ export interface StoredVariant {
56
+ id: string;
57
+ /** The storage key of the bytes it was derived from. */
58
+ source: string;
59
+ variant: string;
60
+ /** Digest of the variant's spec and codec: a changed declaration derives it again. */
61
+ spec: string;
62
+ /** derived: `data` holds it. original: it could not be derived (reason), and the original is served. */
63
+ status: "derived" | "original";
64
+ reason?: string;
65
+ data?: string;
66
+ size?: number;
67
+ checksum?: string;
68
+ width?: number;
69
+ height?: number;
70
+ contentType?: string;
71
+ storedAt: number;
72
+ }
41
73
  export declare class FileContent {
42
74
  #private;
43
75
  constructor(db: StateFirstDB);
@@ -46,18 +78,61 @@ export declare class FileContent {
46
78
  storageKey: string;
47
79
  checksum: string;
48
80
  }>;
81
+ /**
82
+ * Remove a file's bytes, keeping what was removed so the caller can put it back (a byte release
83
+ * that finds, after the removal, a reference that appeared during it). Version-checked: bytes
84
+ * changed meanwhile are refused. `removed: false` when they were already gone.
85
+ */
86
+ take(storageKey: string): Promise<{
87
+ removed: false;
88
+ } | {
89
+ removed: true;
90
+ restore: () => Promise<void>;
91
+ }>;
92
+ /** Whether bytes are stored under a storage key. */
93
+ has(storageKey: string): Promise<boolean>;
94
+ /**
95
+ * Every stored row's storage key, and when it was stored (rows written before `storedAt` existed
96
+ * have none). Reads every row, bytes included: FeltDB has no projection.
97
+ */
98
+ stored(): Promise<Array<{
99
+ storageKey: string;
100
+ storedAt?: number;
101
+ size: number;
102
+ }>>;
49
103
  /** The bytes behind a storage key, verified against their digest. */
50
104
  read(fileId: string, storageKey: string, checksum?: string): Promise<{
51
105
  bytes: Uint8Array;
52
106
  collection: string;
53
107
  }>;
54
- /** Remove a file's bytes: after the file is deleted, or when its metadata could not be committed. */
55
- release(storageKey: string): Promise<void>;
108
+ /**
109
+ * Remove a file's bytes: after the file is deleted, or when its metadata could not be committed.
110
+ * Idempotent: `true` when this call removed them, `false` when they were already gone.
111
+ */
112
+ release(storageKey: string): Promise<boolean>;
113
+ /** The stored variant of `source` named `variant`, if any (whatever spec it was derived under). */
114
+ variant(source: string, variant: string): Promise<StoredVariant | null>;
115
+ /**
116
+ * Keep a derived variant (or the reason it could not be derived). Replaces a row derived under an
117
+ * older spec; if another request stored the same one first, that one is kept and returned.
118
+ */
119
+ storeVariant(input: Omit<StoredVariant, "id" | "storedAt" | "data" | "size" | "checksum"> & {
120
+ bytes?: Uint8Array;
121
+ }): Promise<StoredVariant>;
122
+ /** A derived variant's bytes, verified against their digest. */
123
+ readVariant(row: StoredVariant): Uint8Array;
124
+ /** Remove every variant derived from `source` (its bytes are being released). Returns how many were removed. */
125
+ releaseVariants(source: string): Promise<number>;
56
126
  /**
57
127
  * Issue an opaque link token for `grant`. Only its SHA-256 is stored, so the stored row cannot
58
128
  * be replayed as a link. The function that issues it has already authorized the caller.
59
129
  */
60
130
  issueLink(grant: FileLinkGrant): Promise<string>;
131
+ /**
132
+ * Store the grant for a token the caller derived (a cacheable link: the same token for the same
133
+ * file, content, caller and window). Issuing it again is idempotent; only its digest is stored.
134
+ */
135
+ issueDerivedLink(token: string, grant: FileLinkGrant): Promise<string>;
61
136
  /**
62
137
  * Resolve a link token. Unknown, expired and (for upload links) already-used tokens are all
63
138
  * NOT_FOUND. An upload link is consumed here, before its bytes are read: it works once.
@@ -1,12 +1,30 @@
1
- import { createHash, randomBytes, randomUUID } from "node:crypto";
1
+ import { createHash, createHmac, randomBytes, randomUUID } from "node:crypto";
2
2
  import { FeltKitError } from "../errors.js";
3
+ import { readAll } from "./paged-read.js";
3
4
  /** Outstanding file links (download and upload), keyed by the SHA-256 of their token. */
4
5
  export const FILE_LINK_COLLECTION = "_feltkit_file_links";
5
6
  const LINK_PREFIX = "fkf_";
6
7
  const digestOf = (token) => createHash("sha256").update(token).digest("hex");
8
+ /**
9
+ * The key cacheable link tokens are derived under: random per process, never stored. A token is
10
+ * HMAC(key, its scope), so it cannot be computed from anything in the database, and a restart
11
+ * issues new URLs (one more download per file, not a weaker link).
12
+ */
13
+ const LINK_KEY = randomBytes(32);
14
+ /** A cacheable link's token: the same scope (file, content, variant, caller, window…) gives the same token. */
15
+ export function derivedLinkToken(scope) {
16
+ return LINK_PREFIX + createHmac("sha256", LINK_KEY).update(JSON.stringify(scope)).digest("base64url");
17
+ }
7
18
  /** Where file bytes are kept, one row per file. */
8
19
  export const FILE_CONTENT_COLLECTION = "_feltkit_file_content";
9
20
  const PREFIX = "feltkit-content:";
21
+ /**
22
+ * Derived renditions of stored bytes (image variants), one row per source storage key and variant
23
+ * name. A row is derived content: produced from its source, released with it (`releaseVariants`),
24
+ * and never served except through a link to a file whose bytes it was derived from.
25
+ */
26
+ export const FILE_VARIANT_COLLECTION = "_feltkit_file_variants";
27
+ const variantId = (source, variant) => createHash("sha256").update(`${source}\0${variant}`).digest("hex");
10
28
  export class FileContent {
11
29
  #db;
12
30
  constructor(db) {
@@ -18,11 +36,46 @@ export class FileContent {
18
36
  const id = randomUUID();
19
37
  await this.#db.transaction({
20
38
  operations: [
21
- { collection: FILE_CONTENT_COLLECTION, id, requireAbsent: true, value: { id, data: Buffer.from(bytes).toString("base64"), size: bytes.byteLength, checksum } },
39
+ { collection: FILE_CONTENT_COLLECTION, id, requireAbsent: true, value: { id, data: Buffer.from(bytes).toString("base64"), size: bytes.byteLength, checksum, storedAt: Date.now() } },
22
40
  ],
23
41
  });
24
42
  return { storageKey: `${PREFIX}${id}`, checksum };
25
43
  }
44
+ /**
45
+ * Remove a file's bytes, keeping what was removed so the caller can put it back (a byte release
46
+ * that finds, after the removal, a reference that appeared during it). Version-checked: bytes
47
+ * changed meanwhile are refused. `removed: false` when they were already gone.
48
+ */
49
+ async take(storageKey) {
50
+ if (!storageKey.startsWith(PREFIX))
51
+ return { removed: false };
52
+ const id = storageKey.slice(PREFIX.length);
53
+ const row = (await this.#db.collection(FILE_CONTENT_COLLECTION).get(id)) ?? null;
54
+ if (!row)
55
+ return { removed: false };
56
+ await this.#db.transaction({ operations: [{ collection: FILE_CONTENT_COLLECTION, id, ifVersion: row.__version }] });
57
+ const { __version: _version, ...value } = row;
58
+ return {
59
+ removed: true,
60
+ restore: async () => {
61
+ await this.#db.transaction({ operations: [{ collection: FILE_CONTENT_COLLECTION, id, requireAbsent: true, value: value }] });
62
+ },
63
+ };
64
+ }
65
+ /** Whether bytes are stored under a storage key. */
66
+ async has(storageKey) {
67
+ if (!storageKey.startsWith(PREFIX))
68
+ return false;
69
+ return Boolean(await this.#db.collection(FILE_CONTENT_COLLECTION).get(storageKey.slice(PREFIX.length)));
70
+ }
71
+ /**
72
+ * Every stored row's storage key, and when it was stored (rows written before `storedAt` existed
73
+ * have none). Reads every row, bytes included: FeltDB has no projection.
74
+ */
75
+ async stored() {
76
+ const rows = (await readAll(this.#db, { collection: FILE_CONTENT_COLLECTION }));
77
+ return rows.map((row) => ({ storageKey: `${PREFIX}${row.id}`, ...(typeof row.storedAt === "number" ? { storedAt: row.storedAt } : {}), size: Number(row.size ?? 0) }));
78
+ }
26
79
  /** The bytes behind a storage key, verified against their digest. */
27
80
  async read(fileId, storageKey, checksum) {
28
81
  if (!storageKey.startsWith(PREFIX))
@@ -37,14 +90,90 @@ export class FileContent {
37
90
  }
38
91
  return { bytes, collection: FILE_CONTENT_COLLECTION };
39
92
  }
40
- /** Remove a file's bytes: after the file is deleted, or when its metadata could not be committed. */
93
+ /**
94
+ * Remove a file's bytes: after the file is deleted, or when its metadata could not be committed.
95
+ * Idempotent: `true` when this call removed them, `false` when they were already gone.
96
+ */
41
97
  async release(storageKey) {
42
98
  if (!storageKey.startsWith(PREFIX))
43
- return;
99
+ return false;
44
100
  const id = storageKey.slice(PREFIX.length);
45
- const row = await this.#db.collection(FILE_CONTENT_COLLECTION).get(id);
46
- if (row)
101
+ const rows = this.#db.collection(FILE_CONTENT_COLLECTION);
102
+ const row = await rows.get(id);
103
+ if (!row)
104
+ return false;
105
+ try {
47
106
  await this.#db.transaction({ operations: [{ collection: FILE_CONTENT_COLLECTION, id, ifVersion: row.__version }] });
107
+ return true;
108
+ }
109
+ catch (error) {
110
+ // Removed concurrently (another release of the same bytes) is the same outcome: gone.
111
+ if (!(await rows.get(id)))
112
+ return false;
113
+ throw error;
114
+ }
115
+ }
116
+ /** The stored variant of `source` named `variant`, if any (whatever spec it was derived under). */
117
+ async variant(source, variant) {
118
+ const row = await this.#db.collection(FILE_VARIANT_COLLECTION).get(variantId(source, variant));
119
+ if (!row)
120
+ return null;
121
+ const { __version: _version, ...value } = row;
122
+ return value;
123
+ }
124
+ /**
125
+ * Keep a derived variant (or the reason it could not be derived). Replaces a row derived under an
126
+ * older spec; if another request stored the same one first, that one is kept and returned.
127
+ */
128
+ async storeVariant(input) {
129
+ const id = variantId(input.source, input.variant);
130
+ const { bytes, ...rest } = input;
131
+ const value = {
132
+ ...rest,
133
+ id,
134
+ ...(bytes ? { data: Buffer.from(bytes).toString("base64"), size: bytes.byteLength, checksum: createHash("sha256").update(bytes).digest("hex") } : {}),
135
+ storedAt: Date.now(),
136
+ };
137
+ const rows = this.#db.collection(FILE_VARIANT_COLLECTION);
138
+ const existing = await rows.get(id);
139
+ try {
140
+ await this.#db.transaction({ operations: [existing ? { collection: FILE_VARIANT_COLLECTION, id, ifVersion: existing.__version, value: value } : { collection: FILE_VARIANT_COLLECTION, id, requireAbsent: true, value: value }] });
141
+ return value;
142
+ }
143
+ catch (error) {
144
+ const now = await this.variant(input.source, input.variant);
145
+ if (now && now.spec === input.spec)
146
+ return now; // produced concurrently: the same derivation
147
+ throw error;
148
+ }
149
+ }
150
+ /** A derived variant's bytes, verified against their digest. */
151
+ readVariant(row) {
152
+ const bytes = new Uint8Array(Buffer.from(row.data ?? "", "base64"));
153
+ if (!row.checksum || createHash("sha256").update(bytes).digest("hex") !== row.checksum) {
154
+ throw new FeltKitError("INTERNAL", `Variant ${row.variant} failed its checksum; the stored bytes do not match the recorded digest.`);
155
+ }
156
+ return bytes;
157
+ }
158
+ /** Remove every variant derived from `source` (its bytes are being released). Returns how many were removed. */
159
+ async releaseVariants(source) {
160
+ const rows = (await readAll(this.#db, { collection: FILE_VARIANT_COLLECTION, where: [{ field: "source", eq: source }] }));
161
+ let removed = 0;
162
+ for (const row of rows) {
163
+ const remove = (version) => this.#db.transaction({ operations: [{ collection: FILE_VARIANT_COLLECTION, id: row.id, ifVersion: version }] });
164
+ try {
165
+ await remove(row.__version);
166
+ }
167
+ catch (error) {
168
+ // Derived again meanwhile: remove what is there now; gone already is the same outcome.
169
+ const now = await this.#db.collection(FILE_VARIANT_COLLECTION).get(row.id);
170
+ if (!now)
171
+ continue;
172
+ await remove(now.__version);
173
+ }
174
+ removed += 1;
175
+ }
176
+ return removed;
48
177
  }
49
178
  /**
50
179
  * Issue an opaque link token for `grant`. Only its SHA-256 is stored, so the stored row cannot
@@ -56,6 +185,24 @@ export class FileContent {
56
185
  await this.#db.transaction({ operations: [{ collection: FILE_LINK_COLLECTION, id, requireAbsent: true, value: { id, ...grant } }] });
57
186
  return token;
58
187
  }
188
+ /**
189
+ * Store the grant for a token the caller derived (a cacheable link: the same token for the same
190
+ * file, content, caller and window). Issuing it again is idempotent; only its digest is stored.
191
+ */
192
+ async issueDerivedLink(token, grant) {
193
+ const id = digestOf(token);
194
+ const rows = this.#db.collection(FILE_LINK_COLLECTION);
195
+ if (await rows.get(id))
196
+ return token;
197
+ try {
198
+ await this.#db.transaction({ operations: [{ collection: FILE_LINK_COLLECTION, id, requireAbsent: true, value: { id, ...grant } }] });
199
+ }
200
+ catch (error) {
201
+ if (!(await rows.get(id)))
202
+ throw error; // not a concurrent issue of the same link
203
+ }
204
+ return token;
205
+ }
59
206
  /**
60
207
  * Resolve a link token. Unknown, expired and (for upload links) already-used tokens are all
61
208
  * NOT_FOUND. An upload link is consumed here, before its bytes are read: it works once.
@@ -1 +1 @@
1
- {"version":3,"file":"content.js","sourceRoot":"","sources":["../../src/runtime/content.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,yFAAyF;AACzF,MAAM,CAAC,MAAM,oBAAoB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E,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC3B,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,UAAU,CAAwC,oBAAoB,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC3G,IAAI,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,KAAK,IAAI;YAAE,MAAM,OAAO,EAAE,CAAC;QAC/C,IAAI,GAAG,CAAC,SAAS,IAAI,GAAG,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC9C,uFAAuF;YACvF,IAAI,CAAC;gBACH,MAAM,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,EAAE,oBAAoB,EAAE,EAAE,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,CAAC,EAAE,CAAC,CAAC;YACnH,CAAC;YAAC,MAAM,CAAC;gBACP,MAAM,OAAO,EAAE,CAAC,CAAC,wCAAwC;YAC3D,CAAC;YACD,IAAI,GAAG,CAAC,SAAS,IAAI,GAAG;gBAAE,MAAM,OAAO,EAAE,CAAC;QAC5C,CAAC;QACD,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,GAAG,KAAK,EAAE,GAAG,GAAyD,CAAC;QACpG,OAAQ,KAAyB,CAAC,EAAE,CAAC;QACrC,OAAO,KAAsB,CAAC;IAChC,CAAC;CACF"}
@@ -42,7 +42,109 @@ export interface TenancyReader {
42
42
  email?: string;
43
43
  }>>;
44
44
  }
45
+ /**
46
+ * The platform invocation store (runtime/invocations.ts), as the caller: invocations from registered
47
+ * surfaces (app.surface), their lifecycle, dispatch and recovery. An invocation is visible to the
48
+ * person it is for and to the principal that delivered it; to anyone else it does not exist.
49
+ */
50
+ export interface InvocationsReader {
51
+ get(id: string): Promise<import("./invocations.js").InvocationView>;
52
+ list(filter?: {
53
+ state?: import("../surfaces.js").InvocationState;
54
+ surface?: string;
55
+ limit?: number;
56
+ }): Promise<import("./invocations.js").InvocationView[]>;
57
+ }
58
+ export interface InvocationsWorker extends InvocationsReader {
59
+ /**
60
+ * Claim execution (the dispatched work's first step) as a new attempt, recorded with its environment
61
+ * and worker. proceed: false when there is nothing to run. `previous` lists earlier attempts; one whose
62
+ * outcome is `unknown` may have partly happened, so reconcile before repeating an external effect.
63
+ */
64
+ start(id: string, options?: {
65
+ leaseMs?: number;
66
+ environment?: string;
67
+ worker?: string;
68
+ }): Promise<{
69
+ proceed: boolean;
70
+ claim?: string;
71
+ attempt?: number;
72
+ attemptId?: string;
73
+ execution?: string;
74
+ environment?: string;
75
+ previous?: Array<{
76
+ attempt: string;
77
+ outcome: string;
78
+ }>;
79
+ state: import("../surfaces.js").InvocationState;
80
+ reason?: string;
81
+ }>;
82
+ /** Report progress on the attempt holding `claim` (evidence on the attempt; renews its lease). */
83
+ progress(id: string, options: {
84
+ claim: string;
85
+ note?: string;
86
+ data?: unknown;
87
+ }): Promise<{
88
+ leaseUntil: number;
89
+ }>;
90
+ /** After approval: awaiting_approval → running, with a new claim (a new attempt). */
91
+ resume(id: string, options?: {
92
+ leaseMs?: number;
93
+ environment?: string;
94
+ worker?: string;
95
+ }): Promise<{
96
+ proceed: boolean;
97
+ claim?: string;
98
+ attemptId?: string;
99
+ state: import("../surfaces.js").InvocationState;
100
+ reason?: string;
101
+ }>;
102
+ /**
103
+ * running → awaiting_approval | completed | failed (needs the current claim from running). `result`: a
104
+ * small reference (≤ 4 KB), never the application's data. An external effect whose outcome can't be
105
+ * known: fail with error code OUTCOME_UNKNOWN (the attempt reads `unknown`); never complete it.
106
+ */
107
+ transition(id: string, to: "awaiting_approval" | "completed" | "failed", options?: {
108
+ claim?: string;
109
+ note?: string;
110
+ result?: unknown;
111
+ error?: {
112
+ code: string;
113
+ message: string;
114
+ };
115
+ }): Promise<import("./invocations.js").InvocationView>;
116
+ /** The person cancels their own; terminal states are final. */
117
+ cancel(id: string, options?: {
118
+ reason?: string;
119
+ }): Promise<import("./invocations.js").InvocationView>;
120
+ }
121
+ export interface InvocationsWriter extends InvocationsWorker {
122
+ /**
123
+ * Accept an invocation from a registered surface. The record and its dispatch (`dispatch.job`, run
124
+ * as the caller after the commit, with `{ ...payload, invocation: <id> }`) are staged in this
125
+ * mutation's transaction: both commit or neither. A resubmission of the same request answers the
126
+ * stored receipt (`replayed: true`) and dispatches nothing. `person`: a bridge surface's resolved person.
127
+ *
128
+ * Before dispatch: the caller must hold what the job's function requires (FORBIDDEN otherwise), and the
129
+ * work is placed (src/execution.ts) by its `requirements` and `placement` constraints among the
130
+ * registered execution environments under the operator's policy; unplaceable work is refused
131
+ * (FAILED_PRECONDITION, details.reason UNPLACEABLE, with the decision). Placement never adds authority.
132
+ */
133
+ accept(input: {
134
+ surface: string;
135
+ envelope: unknown;
136
+ person?: string;
137
+ dispatch: {
138
+ job: string;
139
+ payload?: Record<string, unknown>;
140
+ requirements?: Partial<import("../execution.js").ExecutionRequirements>;
141
+ placement?: import("../execution.js").PlacementConstraints;
142
+ };
143
+ }): Promise<import("./invocations.js").Receipt>;
144
+ }
45
145
  export interface QueryCtx {
146
+ /** Invocations from registered surfaces, read-only (runtime/invocations.ts). */
147
+ readonly invocations: InvocationsReader;
46
148
  /** Read-only. Queries cannot write. */
47
149
  readonly db: DatabaseReader;
48
150
  readonly auth: AuthContext;
@@ -60,6 +162,8 @@ export interface QueryCtx {
60
162
  readonly crypto: CryptoContext;
61
163
  }
62
164
  export interface MutationCtx {
165
+ /** Invocations from registered surfaces: accept (with its dispatch, in this transaction), read, move, cancel. */
166
+ readonly invocations: InvocationsWriter;
63
167
  /** Writes are buffered and commit atomically when the handler returns. */
64
168
  readonly db: DatabaseWriter;
65
169
  readonly auth: AuthContext;
@@ -154,6 +258,8 @@ export interface AgentWriter {
154
258
  * environment's model and stream to its caller.
155
259
  */
156
260
  export interface ActionCtx {
261
+ /** Invocations from registered surfaces: read, claim, move, cancel. */
262
+ readonly invocations: InvocationsWorker;
157
263
  readonly auth: AuthContext;
158
264
  readonly environment: EnvironmentInfo;
159
265
  readonly requestId: string;
@@ -184,9 +290,66 @@ export interface ActionCtx {
184
290
  * custody references, resolved for the request and never returned, logged or recorded.
185
291
  */
186
292
  export interface ApplicationAuthority {
293
+ /**
294
+ * The caller's grants to the application's executor (authority.local): what this person allows the
295
+ * application to do for them. Always the caller's own; a person, signed in.
296
+ */
297
+ grants: {
298
+ /** The caller's active grants, to the executor (`agent: null`) and to each agent (`agent`: its AuthBoundry principal). */
299
+ list(): Promise<AuthorityGrant[]>;
300
+ /** `agent` (an agent's principal id): the grant is for that agent only; without it, for the application's executor. */
301
+ grant(input: {
302
+ capabilities: string[];
303
+ expiresAt?: number;
304
+ agent?: string;
305
+ }): Promise<AuthorityGrant>;
306
+ /** One of the caller's own grants, whichever agent it is for. */
307
+ revoke(id: string): Promise<void>;
308
+ };
309
+ /**
310
+ * The application's agents at its local authority (authority.local, per-agent delegation). Every
311
+ * `app.agent` is registered automatically at each start; these are for agents that arrive at run time
312
+ * (an external MCP agent's principal, say). Needs `feltkit.authority.agents` and a caller that is not an
313
+ * agent: registration is an operator's act, and an agent never registers itself. Registering grants
314
+ * nothing: a person must still grant the agent capabilities.
315
+ */
316
+ agents: {
317
+ register(input: {
318
+ id: string;
319
+ name?: string;
320
+ }): Promise<{
321
+ id: string;
322
+ status: string;
323
+ }>;
324
+ list(): Promise<Array<{
325
+ id: string;
326
+ name: string | null;
327
+ status: string;
328
+ }>>;
329
+ /** Final: none of the agent's credentials work again, so none of its grants allow anything. */
330
+ revoke(id: string): Promise<void>;
331
+ };
332
+ /**
333
+ * `onBehalfOf` names the requester, in one of these forms (FRAME-GAP-11):
334
+ * "ada" an application principal id (no ":"): mapped through
335
+ * `authority.principal`, default `user:ada`, as `grants` maps the delegator
336
+ * "user:ada" an AuthBoundry principal (`<type>:<id>`, contains ":"): sent unchanged
337
+ * { person: "ada" } always an application principal id, mapped (for ids that contain ":")
338
+ * { principal: "user:ada" } always an AuthBoundry principal, unchanged (for a custom
339
+ * `authority.principal` whose principals have no ":")
340
+ * The decision's `onBehalfOf` is the AuthBoundry principal that was asked about.
341
+ */
342
+ /**
343
+ * `agent` (local authority): decide as that registered agent, with its own credential, so only a
344
+ * grant to that agent allows it. A caller that is itself an agent (an `app.agent` handler, an agent
345
+ * session) always decides as itself: `agent` may only name itself, and the executor's grants never
346
+ * apply to it. Anyone else decides as the executor unless it names an agent. An agent that is not
347
+ * registered is a deny, never an error.
348
+ */
187
349
  decide(request: {
188
350
  capability: string;
189
- onBehalfOf?: string;
351
+ onBehalfOf?: OnBehalfOf;
352
+ agent?: string;
190
353
  resource?: {
191
354
  type: string;
192
355
  id: string;
@@ -194,18 +357,39 @@ export interface ApplicationAuthority {
194
357
  context?: Record<string, string>;
195
358
  }): Promise<ApplicationAuthorityDecision>;
196
359
  }
360
+ export interface AuthorityGrant {
361
+ id: string;
362
+ /** The agent it is for (AuthBoundry principal), or null: the application's executor. */
363
+ agent: string | null;
364
+ capabilities: string[];
365
+ expiresAt: number | null;
366
+ status: string;
367
+ }
368
+ /** Whom an authority decision is on behalf of (see `ApplicationAuthority.decide`). */
369
+ export type OnBehalfOf = string | {
370
+ person: string;
371
+ } | {
372
+ principal: string;
373
+ };
197
374
  export interface ApplicationAuthorityDecision {
198
375
  allowed: boolean;
199
376
  outcome: "allow" | "deny" | "unavailable" | "invalid";
200
377
  decisionId: string | null;
201
378
  reason: string | null;
202
379
  capability: string;
203
- /** Who acts: the application's executor principal at AuthBoundry. */
380
+ /** Who acts: the application's executor principal at AuthBoundry, or the agent that decided (`agent`). */
204
381
  executor: string;
205
- /** For whom (the requester), when asked on behalf of someone. */
382
+ /** The agent the decision was made as (its AuthBoundry principal), or null: the executor. */
383
+ agent: string | null;
384
+ /** For whom (the requester, as the AuthBoundry principal asked about), when asked on behalf of someone. */
206
385
  onBehalfOf: string | null;
207
386
  /** The delegation that decided an on-behalf allow. */
208
387
  delegationId: string | null;
388
+ /**
389
+ * How the executor was established: `proven` only when AuthBoundry authenticated the executor's
390
+ * agent credential for this decision; otherwise `asserted` (the application named it). Never a secret.
391
+ */
392
+ executorProof: import("./authboundry.js").ExecutorProof;
209
393
  }
210
394
  /**
211
395
  * `ctx.integrations.invoke("<integration>.<operation>", input)`: the operation's own `access`
@@ -214,9 +398,29 @@ export interface ApplicationAuthorityDecision {
214
398
  * and errors never carry the credential.
215
399
  */
216
400
  export interface IntegrationInvoker {
401
+ /**
402
+ * `onBehalfOf` (a person's id): for an integration each person connects themselves
403
+ * (`credential.owner: "person"`), the person whose connection the application's own principal (a
404
+ * service, an agent, a system job) uses. A signed-in person always uses their own and can name no
405
+ * one else. AuthBoundry decides every such call on that person's behalf.
406
+ */
217
407
  invoke(capability: string, input: Record<string, unknown>, options?: {
218
408
  idempotencyKey?: string;
409
+ onBehalfOf?: string;
219
410
  }): Promise<unknown>;
411
+ /** The signed-in person's own connections (credential.owner: "person"). Never anyone else's; never a token. */
412
+ readonly connections: {
413
+ list(): Promise<import("./oauth-credentials.js").ConnectionStatus[]>;
414
+ /** Start connecting: the provider's authorization URL to open. It returns to FeltKit's callback. */
415
+ connect(integration: string): Promise<{
416
+ url: string;
417
+ expiresAt: string;
418
+ }>;
419
+ disconnect(integration: string): Promise<{
420
+ disconnected: boolean;
421
+ providerRevocation: "yes" | "no-endpoint" | "failed" | "none";
422
+ }>;
423
+ };
220
424
  }
221
425
  export interface ActionModel {
222
426
  /** The environment's model: where it comes from and whether it is ready. Never a secret. */