@fungi.computer/oyster 0.42.1 → 0.43.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.
@@ -24,7 +24,7 @@ import * as Schema from "effect/Schema";
24
24
  import type { WatchdogSqliteOwner } from "@fungi.computer/watchdog";
25
25
  import { type AppStorageKey } from "@fungi.computer/puffball";
26
26
  import * as Stream from "effect/Stream";
27
- import { AppStorageError, type AppStorageResult, type AppStorageWrite, type AppStorageReset, type ResolvedAppStorage } from "@fungi.computer/agar/storage";
27
+ import { AppStorageError, type AppStorageResult, type AppStorageWrite, type AppStorageReset, type AppStorageClaim, type ResolvedAppStorage } from "@fungi.computer/agar/storage";
28
28
  /** The App storage handle in `env.APP_STORAGE`. */
29
29
  export type AppStorageBinding = Readonly<{
30
30
  /** Read one declared key, resolved across the team and member layers. */
@@ -105,6 +105,12 @@ export declare function createAppStorage(options: {
105
105
  reset: (input: AppStorageReset) => Effect.Effect<{
106
106
  revision: number;
107
107
  }, AppStorageError, never>;
108
+ /** Claim an absent declared team layer, or return the layer already there. */
109
+ claim: (input: AppStorageClaim) => Effect.Effect<{
110
+ readonly value: Schema.Json;
111
+ readonly revision: number;
112
+ readonly claimed: boolean;
113
+ }, AppStorageError, never>;
108
114
  };
109
115
  /** Bind a member view to live admission, declared keys and the current membership version. */
110
116
  open(authorize: () => Effect.Effect<AppStorageAdmission, Error>): {
@@ -130,6 +136,12 @@ export declare function createAppStorage(options: {
130
136
  reset: (input: AppStorageReset) => Effect.Effect<{
131
137
  revision: number;
132
138
  }, AppStorageError, never>;
139
+ /** Claim an admitted absent layer, or return the layer already there. */
140
+ claim: (input: AppStorageClaim) => Effect.Effect<{
141
+ readonly value: Schema.Json;
142
+ readonly revision: number;
143
+ readonly claimed: boolean;
144
+ }, AppStorageError, never>;
133
145
  /** One bounded invalidation stream. Null marks an armed subscription. */
134
146
  events: (key: string) => Stream.Stream<{
135
147
  readonly appId: string;
@@ -132,7 +132,7 @@ export function createAppStorage(options) {
132
132
  throw new AppStorageError("quota_exceeded");
133
133
  sql.exec("INSERT INTO hub_app_storage_writes(address,member_id,membership_version,started_at,count) VALUES(?,?,?,?,?) ON CONFLICT(address,member_id,membership_version) DO UPDATE SET started_at=excluded.started_at,count=excluded.count", admission.address, writerIdentity(admission).memberId, writerIdentity(admission).membershipVersion, current ? saved.started_at : now, current ? saved.count + 1 : 1);
134
134
  };
135
- const mutate = (admission, input, value) => {
135
+ const requireWritable = (admission, scope) => {
136
136
  const writer = "memberId" in admission
137
137
  ? {
138
138
  kind: "member",
@@ -140,8 +140,11 @@ export function createAppStorage(options) {
140
140
  isOwner: admission.isOwner,
141
141
  }
142
142
  : { kind: "lifecycle", handler: "installation" };
143
- if (!appStorageWritable(writer, input.scope))
143
+ if (!appStorageWritable(writer, scope))
144
144
  throw new AppStorageError("owner_required");
145
+ };
146
+ const mutate = (admission, input, value) => {
147
+ requireWritable(admission, input.scope);
145
148
  const declared = declare(admission, input.key);
146
149
  if (!declared.scopes.includes(input.scope))
147
150
  throw new AppStorageError("scope_not_declared");
@@ -199,6 +202,32 @@ export function createAppStorage(options) {
199
202
  canWriteTeam: "isOwner" in admission ? admission.isOwner : true,
200
203
  };
201
204
  };
205
+ /**
206
+ * Keep an existing layer, or store `input.value` in the absent layer with
207
+ * every write check. One transaction, so concurrent first writers settle
208
+ * on one value and none of them sees a `conflict`.
209
+ */
210
+ const claimLayer = (admission, input) => {
211
+ requireWritable(admission, input.scope);
212
+ const declared = declare(admission, input.key);
213
+ if (!declared.scopes.includes(input.scope))
214
+ throw new AppStorageError("scope_not_declared");
215
+ const current = layers(admission, input.key).find((row) => row.scope === input.scope);
216
+ if (current !== undefined)
217
+ return {
218
+ value: current.value,
219
+ revision: current.revision,
220
+ claimed: false,
221
+ };
222
+ const { revision } = mutate(admission, { key: input.key, scope: input.scope, basedOn: null }, input.value);
223
+ return { value: input.value, revision, claimed: true };
224
+ };
225
+ const commitClaim = (admission, input) => Effect.uninterruptible(options
226
+ .commit(() => claimLayer(admission, input))
227
+ .pipe(Effect.tap((result) => Effect.sync(() => {
228
+ if (result.claimed)
229
+ publish(admission, { key: input.key, scope: input.scope, basedOn: null }, result);
230
+ }))));
202
231
  const commitMutation = (admission, input, value) => Effect.uninterruptible(options
203
232
  .commit(() => mutate(admission, input, value))
204
233
  .pipe(Effect.tap((result) => Effect.sync(() => {
@@ -226,6 +255,8 @@ export function createAppStorage(options) {
226
255
  write: (input) => mutation(input, input.value),
227
256
  /** Reset a declared team layer; member scope is rejected with owner_required. */
228
257
  reset: (input) => mutation(input, undefined),
258
+ /** Claim an absent declared team layer, or return the layer already there. */
259
+ claim: (input) => authorize().pipe(Effect.flatMap((admission) => commitClaim(admission, input)), Effect.mapError(unavailable)),
229
260
  };
230
261
  },
231
262
  /** Bind a member view to live admission, declared keys and the current membership version. */
@@ -243,6 +274,8 @@ export function createAppStorage(options) {
243
274
  write: (input) => admitted().pipe(Effect.flatMap((admission) => commitMutation(admission, input, input.value)), Effect.mapError(unavailable)),
244
275
  /** Remove an admitted layer at its expected revision and notify watchers. */
245
276
  reset: (input) => admitted().pipe(Effect.flatMap((admission) => commitMutation(admission, input, undefined)), Effect.mapError(unavailable)),
277
+ /** Claim an admitted absent layer, or return the layer already there. */
278
+ claim: (input) => admitted().pipe(Effect.flatMap((admission) => commitClaim(admission, input)), Effect.mapError(unavailable)),
246
279
  /** One bounded invalidation stream. Null marks an armed subscription. */
247
280
  events: (key) => Stream.unwrap(Effect.gen(function* () {
248
281
  const admission = yield* admitted();
package/dist/host.d.ts CHANGED
@@ -256,7 +256,7 @@ export type OysterHost = Readonly<{
256
256
  /** Read the owner's live installation directory. */
257
257
  directory(): Promise<OysterRootDirectory>;
258
258
  /** Open storage after fresh installation and owner admission. */
259
- storage(installationId: string, ownerId: string): Pick<AppStorageWire, "read" | "write" | "reset"> & Readonly<{
259
+ storage(installationId: string, ownerId: string): Pick<AppStorageWire, "read" | "write" | "reset" | "claim"> & Readonly<{
260
260
  /** Stream changes for an admitted declared storage key. */
261
261
  events(key: string): Promise<Response>;
262
262
  }>;
package/dist/host.js CHANGED
@@ -12,7 +12,7 @@ import { shellModuleAsset, shellModuleAssetPath, } from "@fungi.computer/puffbal
12
12
  import { gitRepositoryNameSchema } from "@fungi.computer/puffball/git-representation";
13
13
  import { gillRequestSchema, decodeGillReply, decodeDeclaredGillResponse, } from "@fungi.computer/gills";
14
14
  import { causeClass } from "./cause-class.js";
15
- import { appStorageKeyNameSchema, appStorageWriteSchema, appStorageResetSchema, } from "@fungi.computer/agar/storage";
15
+ import { appStorageKeyNameSchema, appStorageWriteSchema, appStorageResetSchema, appStorageClaimSchema, } from "@fungi.computer/agar/storage";
16
16
  import { guestLimits } from "./guest-policy.js";
17
17
  import { APP_INVOCATION_CEILING_MS, admittedCallerFacts, admittedRegionConnectFacts, admittedMethodCallerFacts, invocationAuthorityOf, invocationCallerContext, invocationFacts, } from "./invocation.js";
18
18
  import { appFacetResponse } from "./backend-request.js";
@@ -589,6 +589,14 @@ export function oysterRouteTable(host) {
589
589
  ? Response.json(await view.read(key.value))
590
590
  : view.events(key.value);
591
591
  }
592
+ if (operation === "claim" && context.req.method === "POST") {
593
+ const input = Schema.decodeUnknownOption(appStorageClaimSchema, {
594
+ onExcessProperty: "error",
595
+ })(await context.req.json());
596
+ return input._tag === "None"
597
+ ? refusal(400, "Invalid storage claim")
598
+ : Response.json(await view.claim(input.value));
599
+ }
592
600
  if ((operation === "write" || operation === "reset") &&
593
601
  context.req.method === "POST") {
594
602
  if (operation === "write") {
@@ -32,6 +32,7 @@ export declare function oysterShellHandles(config: OysterAppConfig): {
32
32
  reset(input: import("@fungi.computer/agar").AppStorageReset): Promise<{
33
33
  revision: number;
34
34
  }>;
35
+ claim(input: import("@fungi.computer/agar/storage-wire").AppStorageClaim): Promise<import("@fungi.computer/agar/storage-wire").AppStorageClaimed>;
35
36
  watch(key: string): Promise<import("@fungi.computer/agar").AppStorageWatch>;
36
37
  }> | null;
37
38
  methods: {
@@ -1,7 +1,7 @@
1
1
  import { RpcTarget } from "@fungi.computer/capnweb";
2
2
  import * as Schema from "effect/Schema";
3
3
  import { AppError } from "@fungi.computer/agar";
4
- import { appStorageKeyNameSchema, appStorageWriteSchema, appStorageResetSchema, appStorageReadResultSchema, appStorageMutationResultSchema, } from "@fungi.computer/agar/storage";
4
+ import { appStorageKeyNameSchema, appStorageWriteSchema, appStorageResetSchema, appStorageClaimSchema, appStorageReadResultSchema, appStorageMutationResultSchema, appStorageClaimResultSchema, } from "@fungi.computer/agar/storage";
5
5
  const NativeTarget = RpcTarget;
6
6
  function path(installationId, operation) {
7
7
  return `/api/workbench/storage/${encodeURIComponent(installationId)}/${operation}`;
@@ -61,6 +61,12 @@ class Storage extends NativeTarget {
61
61
  })(raw);
62
62
  return Schema.decodeUnknownSync(appStorageMutationResultSchema)(await this.mutate("reset", input));
63
63
  }
64
+ async claim(raw) {
65
+ const input = Schema.decodeUnknownSync(appStorageClaimSchema, {
66
+ onExcessProperty: "error",
67
+ })(raw);
68
+ return Schema.decodeUnknownSync(appStorageClaimResultSchema)(await this.mutate("claim", input));
69
+ }
64
70
  async mutate(operation, input) {
65
71
  return reply(await fetch(path(this.installationId, operation), {
66
72
  method: "POST",