@fungi.computer/oyster 0.37.2 → 0.38.1
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.
- package/README.md +5 -4
- package/dist/agents.js +9 -2
- package/dist/app-builds.d.ts +1 -1
- package/dist/app-region.d.ts +94 -0
- package/dist/app-region.js +316 -0
- package/dist/authorization.d.ts +58 -9
- package/dist/authorization.js +56 -11
- package/dist/backend-request.d.ts +4 -0
- package/dist/backend-request.js +3 -10
- package/dist/boot-host-script.js +1 -1
- package/dist/build/candidate-builder.d.ts +1 -1
- package/dist/build/candidate-cli.mjs +284 -17
- package/dist/build/effect.d.ts +3 -3
- package/dist/build/submission-contract.d.ts +1 -1
- package/dist/build/workspace-build.d.ts +2 -2
- package/dist/channels/discord-replies.d.ts +8 -1
- package/dist/channels/discord-replies.js +80 -29
- package/dist/cloudflare.d.ts +1 -0
- package/dist/cloudflare.js +12 -2
- package/dist/credential-migration-001.d.ts +14 -0
- package/dist/credential-migration-001.js +13 -0
- package/dist/credential-migration-002.d.ts +38 -0
- package/dist/credential-migration-002.js +74 -0
- package/dist/embed-metafiles.js +2 -2
- package/dist/embed-scripts.js +1 -1
- package/dist/host.d.ts +2 -0
- package/dist/host.js +23 -13
- package/dist/index.d.ts +15 -1
- package/dist/index.js +19 -0
- package/dist/invocation.d.ts +165 -0
- package/dist/invocation.js +67 -0
- package/dist/keys.d.ts +4 -3
- package/dist/keys.js +5 -4
- package/dist/mcp-owner.d.ts +4 -3
- package/dist/mcp-owner.js +3 -2
- package/dist/page/mount.js +5 -1
- package/dist/page/region.d.ts +6 -0
- package/dist/page/region.js +12 -0
- package/dist/page-config.d.ts +2 -0
- package/dist/page-config.js +2 -0
- package/dist/page-script.js +2 -2
- package/dist/source/seed-receipt.d.ts +1 -1
- package/dist/source/template-seed.d.ts +1 -1
- package/dist/standalone/agent-connections.d.ts +49 -1
- package/dist/standalone/agent-connections.js +101 -12
- package/dist/standalone/agent-self.js +2 -1
- package/dist/standalone/agents.d.ts +7 -3
- package/dist/standalone/agents.js +42 -19
- package/dist/standalone/computer-fleet.d.ts +12 -6
- package/dist/standalone/computer-fleet.js +70 -29
- package/dist/standalone/computer.js +39 -14
- package/dist/standalone/instance.d.ts +3 -1
- package/dist/standalone/instance.js +8 -0
- package/dist/standalone/model-key-vault.d.ts +33 -4
- package/dist/standalone/model-key-vault.js +74 -17
- package/dist/standalone/preview.js +11 -10
- package/dist/standalone/try-cli.d.ts +12 -2
- package/dist/standalone/try-cli.js +170 -32
- package/dist/standalone/try-worker.d.ts +2 -0
- package/dist/standalone/try-worker.js +15 -0
- package/dist/standalone/worker.js +3 -3
- package/dist/standalone/workflow-host.d.ts +1 -1
- package/dist/standalone/workflows.d.ts +3 -3
- package/dist/standalone/workflows.js +7 -0
- package/dist/testing/capabilities.d.ts +2 -0
- package/dist/testing/capabilities.js +2 -0
- package/dist/webhook-custody.d.ts +14 -3
- package/dist/webhook-custody.js +165 -43
- package/dist/webhooks.d.ts +1 -1
- package/dist/webhooks.js +15 -3
- package/dist/workers-git.d.ts +24 -8
- package/dist/workers-git.js +105 -15
- package/dist/workers.d.ts +16 -0
- package/dist/workers.js +95 -5
- package/docs/api/apps.md +31 -31
- package/docs/api/authorization.md +119 -17
- package/docs/api/build-contract.md +8 -8
- package/docs/api/build.md +163 -174
- package/docs/api/cloudflare.md +171 -0
- package/docs/api/host.md +1 -0
- package/docs/api/index.md +23 -0
- package/docs/api/invocation.md +623 -23
- package/docs/api/keys.md +6 -6
- package/docs/api/pack.md +3 -3
- package/docs/api/page/mount.md +22 -20
- package/docs/api/workers-git.md +273 -157
- package/docs/api/workers.md +257 -175
- package/package.json +19 -18
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { CredentialError } from "@fungi.computer/veil/credential-store";
|
|
2
3
|
/**
|
|
3
4
|
* The Discord replies an Agent still owes. An interaction token is valid for
|
|
4
5
|
* about 15 minutes: until then the answer fills the original message. A run
|
|
@@ -7,7 +8,8 @@ import { z } from "zod";
|
|
|
7
8
|
* keeps the debt in the Agent's own store, so an evicted run still answers
|
|
8
9
|
* after the Agent wakes. A row names no secret once it is closed, and a closed
|
|
9
10
|
* row says honestly why: `expired` (no answer within two hours) or `failed`
|
|
10
|
-
* (delivery kept failing).
|
|
11
|
+
* (delivery kept failing). An open row's interaction token rests only in the
|
|
12
|
+
* Agent's S1 envelope.
|
|
11
13
|
*/
|
|
12
14
|
/** The interaction token window we honour, a little under Discord's 15 minutes. */
|
|
13
15
|
const DISCORD_REPLY_WINDOW_MS = 14 * 60_000;
|
|
@@ -29,7 +31,7 @@ const pendingRow = z.strictObject({
|
|
|
29
31
|
interaction_id: z.string(),
|
|
30
32
|
binding_id: z.string(),
|
|
31
33
|
application_id: z.string(),
|
|
32
|
-
|
|
34
|
+
sealed_token: z.string(),
|
|
33
35
|
guild_id: z.string(),
|
|
34
36
|
channel_id: z.string(),
|
|
35
37
|
user_id: z.string(),
|
|
@@ -51,46 +53,82 @@ const closedRow = z.strictObject({
|
|
|
51
53
|
closed_at: z.number().int(),
|
|
52
54
|
});
|
|
53
55
|
const dueRow = z.strictObject({ due: z.number().int().nullable() });
|
|
56
|
+
const tokenContext = (interactionId) => [
|
|
57
|
+
"discord-reply",
|
|
58
|
+
interactionId,
|
|
59
|
+
];
|
|
60
|
+
function ensureReplies(sql) {
|
|
61
|
+
sql.exec(`CREATE TABLE IF NOT EXISTS oyster_discord_replies (
|
|
62
|
+
interaction_id TEXT PRIMARY KEY,
|
|
63
|
+
binding_id TEXT NOT NULL,
|
|
64
|
+
application_id TEXT NOT NULL,
|
|
65
|
+
sealed_token TEXT,
|
|
66
|
+
guild_id TEXT NOT NULL,
|
|
67
|
+
channel_id TEXT NOT NULL,
|
|
68
|
+
user_id TEXT NOT NULL,
|
|
69
|
+
text TEXT NOT NULL,
|
|
70
|
+
echo TEXT,
|
|
71
|
+
display_name TEXT,
|
|
72
|
+
delivered_parts INTEGER NOT NULL DEFAULT 0,
|
|
73
|
+
still_working INTEGER NOT NULL DEFAULT 0,
|
|
74
|
+
session_id TEXT,
|
|
75
|
+
command_id TEXT,
|
|
76
|
+
state TEXT NOT NULL CHECK (state IN ('waiting','expired','failed')),
|
|
77
|
+
attempts INTEGER NOT NULL,
|
|
78
|
+
created_at INTEGER NOT NULL,
|
|
79
|
+
expires_at INTEGER NOT NULL,
|
|
80
|
+
next_at INTEGER NOT NULL,
|
|
81
|
+
closed_at INTEGER
|
|
82
|
+
) STRICT`);
|
|
83
|
+
}
|
|
84
|
+
const legacyReply = z.looseObject({
|
|
85
|
+
interaction_id: z.string(),
|
|
86
|
+
token: z.string(),
|
|
87
|
+
state: z.enum(["waiting", "expired", "failed"]),
|
|
88
|
+
});
|
|
89
|
+
/** Migration 002's step: an open row's token is sealed; a closed row holds none. */
|
|
90
|
+
export const discordReplies002 = {
|
|
91
|
+
table: "oyster_discord_replies",
|
|
92
|
+
plaintext: (columns) => columns.includes("token"),
|
|
93
|
+
secretColumns: ["token"],
|
|
94
|
+
ensure: ensureReplies,
|
|
95
|
+
async seal(sealer, raw) {
|
|
96
|
+
const { token, ...row } = legacyReply.parse(raw);
|
|
97
|
+
return z
|
|
98
|
+
.record(z.string(), z.union([z.string(), z.number(), z.null()]))
|
|
99
|
+
.parse({
|
|
100
|
+
...row,
|
|
101
|
+
sealed_token: row.state === "waiting" && token !== ""
|
|
102
|
+
? await sealer.seal(tokenContext(row.interaction_id), token)
|
|
103
|
+
: null,
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
};
|
|
54
107
|
/** The pending-reply store and the host work that pays it off. */
|
|
55
108
|
export function createDiscordReplies(options) {
|
|
56
109
|
const { sql } = options;
|
|
57
110
|
const ensure = () => {
|
|
58
|
-
sql
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
channel_id TEXT NOT NULL,
|
|
65
|
-
user_id TEXT NOT NULL,
|
|
66
|
-
text TEXT NOT NULL,
|
|
67
|
-
echo TEXT,
|
|
68
|
-
display_name TEXT,
|
|
69
|
-
delivered_parts INTEGER NOT NULL DEFAULT 0,
|
|
70
|
-
still_working INTEGER NOT NULL DEFAULT 0,
|
|
71
|
-
session_id TEXT,
|
|
72
|
-
command_id TEXT,
|
|
73
|
-
state TEXT NOT NULL CHECK (state IN ('waiting','expired','failed')),
|
|
74
|
-
attempts INTEGER NOT NULL,
|
|
75
|
-
created_at INTEGER NOT NULL,
|
|
76
|
-
expires_at INTEGER NOT NULL,
|
|
77
|
-
next_at INTEGER NOT NULL,
|
|
78
|
-
closed_at INTEGER
|
|
79
|
-
) STRICT`);
|
|
111
|
+
ensureReplies(sql);
|
|
112
|
+
};
|
|
113
|
+
const sealer = () => {
|
|
114
|
+
if (options.sealer === undefined)
|
|
115
|
+
throw new CredentialError("unavailable");
|
|
116
|
+
return options.sealer;
|
|
80
117
|
};
|
|
81
118
|
const close = (interactionId, state) => {
|
|
82
|
-
sql.exec("UPDATE oyster_discord_replies SET state=?,
|
|
119
|
+
sql.exec("UPDATE oyster_discord_replies SET state=?, sealed_token=NULL, text='', echo=NULL, closed_at=? WHERE interaction_id=?", state, options.now(), interactionId);
|
|
83
120
|
};
|
|
84
121
|
const reschedule = (interactionId, at, attempts) => {
|
|
85
122
|
sql.exec("UPDATE oyster_discord_replies SET next_at=?, attempts=? WHERE interaction_id=?", at, attempts, interactionId);
|
|
86
123
|
};
|
|
87
124
|
let active;
|
|
88
125
|
const pass = async () => {
|
|
126
|
+
await options.ready();
|
|
89
127
|
ensure();
|
|
90
128
|
const now = options.now();
|
|
91
129
|
sql.exec("DELETE FROM oyster_discord_replies WHERE state!='waiting' AND closed_at<?", now - CLOSED_RETENTION_MS);
|
|
92
130
|
const rows = sql
|
|
93
|
-
.exec("SELECT interaction_id,binding_id,application_id,
|
|
131
|
+
.exec("SELECT interaction_id,binding_id,application_id,sealed_token,guild_id,channel_id,user_id,text,echo,display_name,session_id,command_id,attempts,delivered_parts,still_working,created_at,expires_at FROM oyster_discord_replies WHERE state='waiting' AND next_at<=? ORDER BY next_at LIMIT ?", now, PASS_LIMIT)
|
|
94
132
|
.toArray()
|
|
95
133
|
.map((raw) => pendingRow.parse(raw));
|
|
96
134
|
for (const row of rows) {
|
|
@@ -100,6 +138,17 @@ export function createDiscordReplies(options) {
|
|
|
100
138
|
options.onExpired(row.interaction_id);
|
|
101
139
|
continue;
|
|
102
140
|
}
|
|
141
|
+
let interactionToken;
|
|
142
|
+
try {
|
|
143
|
+
interactionToken = await sealer().open(tokenContext(row.interaction_id), row.sealed_token);
|
|
144
|
+
}
|
|
145
|
+
catch (cause) {
|
|
146
|
+
// A missing keyring waits for one; a token that never opens is lost.
|
|
147
|
+
if (cause instanceof CredentialError && cause.code === "unavailable")
|
|
148
|
+
throw cause;
|
|
149
|
+
close(row.interaction_id, "failed");
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
103
152
|
const tokenLive = at < row.expires_at;
|
|
104
153
|
const delivery = await options.deliver({
|
|
105
154
|
interactionId: row.interaction_id,
|
|
@@ -107,7 +156,7 @@ export function createDiscordReplies(options) {
|
|
|
107
156
|
ask: {
|
|
108
157
|
interactionId: row.interaction_id,
|
|
109
158
|
applicationId: row.application_id,
|
|
110
|
-
interactionToken
|
|
159
|
+
interactionToken,
|
|
111
160
|
guildId: row.guild_id,
|
|
112
161
|
channelId: row.channel_id,
|
|
113
162
|
userId: row.user_id,
|
|
@@ -147,10 +196,12 @@ export function createDiscordReplies(options) {
|
|
|
147
196
|
};
|
|
148
197
|
return {
|
|
149
198
|
/** Owe one reply. A second record of the same interaction is the first. */
|
|
150
|
-
record: (bindingId, ask) => {
|
|
199
|
+
record: async (bindingId, ask) => {
|
|
200
|
+
await options.ready();
|
|
151
201
|
ensure();
|
|
202
|
+
const sealedToken = await sealer().seal(tokenContext(ask.interactionId), ask.interactionToken);
|
|
152
203
|
const now = options.now();
|
|
153
|
-
sql.exec("INSERT OR IGNORE INTO oyster_discord_replies (interaction_id,binding_id,application_id,
|
|
204
|
+
sql.exec("INSERT OR IGNORE INTO oyster_discord_replies (interaction_id,binding_id,application_id,sealed_token,guild_id,channel_id,user_id,text,echo,display_name,state,attempts,created_at,expires_at,next_at) VALUES (?,?,?,?,?,?,?,?,?,?,'waiting',0,?,?,?)", ask.interactionId, bindingId, ask.applicationId, sealedToken, ask.guildId, ask.channelId, ask.userId, ask.text, ask.echo ?? null, ask.displayName ?? null, now, now + DISCORD_REPLY_WINDOW_MS, now);
|
|
154
205
|
},
|
|
155
206
|
/** The member has been told the run is still going. */
|
|
156
207
|
markStillWorking: (interactionId) => {
|
package/dist/cloudflare.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ import type { Release, AppApi } from "@fungi.computer/puffball";
|
|
|
15
15
|
import { type UiBootCheckOutcome } from "./boot-gate.js";
|
|
16
16
|
import { type AppTestCopy, type AppTestCopyAuthority, type AppTestCopyPorts } from "./app-test-copies.js";
|
|
17
17
|
export { checkBackendConstruction } from "./boot-construction.js";
|
|
18
|
+
export { createAppRegion, durableAppRegionNative, isAppRegionSocket, type AppRegion, type AppRegionDeliver, type AppRegionDurableState, type AppRegionNative, type AppRegionSocket, } from "./app-region.js";
|
|
18
19
|
import type { WatchdogSqliteOwner } from "@fungi.computer/watchdog";
|
|
19
20
|
import { AppInstanceError, type AppInstance, type Backend, type InstancePorts } from "./index.js";
|
|
20
21
|
/**
|
package/dist/cloudflare.js
CHANGED
|
@@ -50,7 +50,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
|
|
|
50
50
|
var e = new Error(message);
|
|
51
51
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
52
|
});
|
|
53
|
-
import {
|
|
53
|
+
import { appMethodReplySchema } from "@fungi.computer/agar/backend";
|
|
54
54
|
import { nativeGitFrameMethods } from "@fungi.computer/agar/git-rpc";
|
|
55
55
|
import { rpcHandleSchema, rpcMethod } from "@fungi.computer/agar/rpc";
|
|
56
56
|
import { facetInvocationModule, facetFrameBindingModule, } from "./facet-invocation.js";
|
|
@@ -59,6 +59,7 @@ import { backgroundSyncRefusalSchema, claimedJobAdmissionSchema, } from "@fungi.
|
|
|
59
59
|
import { causeClass, failureDiagnostic, guestFailure } from "./cause-class.js";
|
|
60
60
|
import { appGitPushFactSchema, gitPushVerdictSchema, } from "@fungi.computer/agar/git";
|
|
61
61
|
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
62
|
+
import { appRegionReplySchema } from "@fungi.computer/puffball/backend";
|
|
62
63
|
import { guestLimits } from "./guest-policy.js";
|
|
63
64
|
import { headContentObject } from "./build/content-objects.js";
|
|
64
65
|
import { appBackendBindingKinds, } from "@fungi.computer/puffball/app";
|
|
@@ -74,6 +75,7 @@ import { BOOT_DEADLINE_MS, BootGateError, } from "./boot-gate.js";
|
|
|
74
75
|
import { appFacetRequest, appFacetResponse } from "./backend-request.js";
|
|
75
76
|
import { createAppTestCopies, } from "./app-test-copies.js";
|
|
76
77
|
export { checkBackendConstruction } from "./boot-construction.js";
|
|
78
|
+
export { createAppRegion, durableAppRegionNative, isAppRegionSocket, } from "./app-region.js";
|
|
77
79
|
import { createAppInstance, AppInstanceError, instanceIdSchema, instanceSchema, appInstanceErrorCodeSchema, } from "./index.js";
|
|
78
80
|
const bindingWire = createFrameBindingWire(appInstanceErrorCodeSchema.literals);
|
|
79
81
|
/**
|
|
@@ -438,7 +440,6 @@ export function createCloudflareAppTestCopies(ctx, loader, ports, limits) {
|
|
|
438
440
|
/** Run ordinary backend HTTP with bounded bodies and no live bindings. */
|
|
439
441
|
fetch: (authority, copyId, request) => execute(authority, copyId, async (stub) => {
|
|
440
442
|
const safe = appFacetRequest(request);
|
|
441
|
-
safe.headers.delete(appBackendComputerHeader);
|
|
442
443
|
const reply = facetHttpReplySchema.parse(await stub.http({
|
|
443
444
|
url: safe.url,
|
|
444
445
|
method: safe.method,
|
|
@@ -719,6 +720,15 @@ export function createCloudflareAppInstance(ctx, loader, resolve, guestEnv, boot
|
|
|
719
720
|
},
|
|
720
721
|
catch: (cause) => facetFailure(frame, "git_push", cause, () => new Error("App Git push handler failed", { cause })),
|
|
721
722
|
}),
|
|
723
|
+
region: (event) => Effect.tryPromise({
|
|
724
|
+
try: async () => {
|
|
725
|
+
const reply = appRegionReplySchema.parse(await stub.invoke("onRegion", [event], bindings));
|
|
726
|
+
if (frame.signal.aborted)
|
|
727
|
+
throw new AppInstanceError("revoked");
|
|
728
|
+
return reply;
|
|
729
|
+
},
|
|
730
|
+
catch: (cause) => facetFailure(frame, "region_event", cause, () => new Error("App Region handler failed", { cause })),
|
|
731
|
+
}),
|
|
722
732
|
call: (name, input) => invokeEntry(name, input).pipe(Effect.flatMap((raw) => Effect.try({
|
|
723
733
|
try: () => appMethodReplySchema.parse(raw),
|
|
724
734
|
catch: (cause) => new Error("App method reply invalid", { cause }),
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type CredentialKeyring, type CredentialStorage } from "@fungi.computer/veil/credential-store";
|
|
2
|
+
import type { SqlStore } from "@fungi.computer/veil/connections";
|
|
3
|
+
/**
|
|
4
|
+
* Oyster credential migration 001: the old `instance`-sealed model, Discord
|
|
5
|
+
* and MCP rows move to owner envelopes without changing ids or references.
|
|
6
|
+
* Operators retain `instance` in the configured keyring during this conversion.
|
|
7
|
+
* Veil records completion; subsequent boots only rewrap the owner's data key.
|
|
8
|
+
*/
|
|
9
|
+
export declare function migrateCredentialRows001(options: {
|
|
10
|
+
owner: SqlStore;
|
|
11
|
+
storage: CredentialStorage;
|
|
12
|
+
ownerId: string;
|
|
13
|
+
keyring: CredentialKeyring | undefined;
|
|
14
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { migrateCredentialEnvelopes, } from "@fungi.computer/veil/credential-store";
|
|
2
|
+
/**
|
|
3
|
+
* Oyster credential migration 001: the old `instance`-sealed model, Discord
|
|
4
|
+
* and MCP rows move to owner envelopes without changing ids or references.
|
|
5
|
+
* Operators retain `instance` in the configured keyring during this conversion.
|
|
6
|
+
* Veil records completion; subsequent boots only rewrap the owner's data key.
|
|
7
|
+
*/
|
|
8
|
+
export async function migrateCredentialRows001(options) {
|
|
9
|
+
await migrateCredentialEnvelopes({
|
|
10
|
+
...options,
|
|
11
|
+
domain: "fungi.oyster.credentials.v1",
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type OwnerCredentialSealer } from "@fungi.computer/veil/credential-store";
|
|
2
|
+
import type { SqlStore } from "@fungi.computer/veil/connections";
|
|
3
|
+
import * as Schema from "effect/Schema";
|
|
4
|
+
/** S1's per-owner envelope sealer; `undefined` is a host with no keyring. */
|
|
5
|
+
export type SecretSealer = Pick<OwnerCredentialSealer, "seal" | "open">;
|
|
6
|
+
declare const rowSchema: Schema.$Record<Schema.String, Schema.Union<readonly [Schema.String, Schema.Number, Schema.Null]>>;
|
|
7
|
+
/** One legacy row as SQLite returned it; its owner decodes its own columns. */
|
|
8
|
+
export type SqlRow002 = typeof rowSchema.Type;
|
|
9
|
+
/**
|
|
10
|
+
* One owner's table whose secret column moves from plaintext to an S1
|
|
11
|
+
* envelope. The owner keeps its current DDL and sealing context; this
|
|
12
|
+
* migration only moves rows.
|
|
13
|
+
*/
|
|
14
|
+
export type SealedTable002 = Readonly<{
|
|
15
|
+
table: string;
|
|
16
|
+
/** True when the table still has its plaintext shape. */
|
|
17
|
+
plaintext(columns: readonly string[]): boolean;
|
|
18
|
+
/** The legacy columns that hold secret bytes. */
|
|
19
|
+
secretColumns: readonly string[];
|
|
20
|
+
/** The owner's current `CREATE TABLE IF NOT EXISTS` for `table`. */
|
|
21
|
+
ensure(sql: SqlStore["sql"]): void;
|
|
22
|
+
/** One legacy row to its current column values, with the secret sealed. */
|
|
23
|
+
seal(sealer: SecretSealer, row: SqlRow002): Promise<SqlRow002>;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* Oyster credential migration 002: webhook signing secrets, subscription
|
|
27
|
+
* secrets and open Discord interaction tokens move from plaintext columns to
|
|
28
|
+
* the owner's S1 envelope, and the plaintext columns are dropped with them.
|
|
29
|
+
* Runs before an owner reads or writes its table. A failed attempt is
|
|
30
|
+
* retried by the next use instead of being cached. Without a keyring a
|
|
31
|
+
* plaintext row refuses as `unavailable`; it is never read in the clear.
|
|
32
|
+
*/
|
|
33
|
+
export declare function credentialMigration002(options: {
|
|
34
|
+
store: SqlStore;
|
|
35
|
+
sealer: SecretSealer | undefined;
|
|
36
|
+
tables: readonly SealedTable002[];
|
|
37
|
+
}): () => Promise<void>;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { CredentialError, } from "@fungi.computer/veil/credential-store";
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
const rowSchema = Schema.Record(Schema.String, Schema.Union([Schema.String, Schema.Number, Schema.Null]));
|
|
4
|
+
const columnsSchema = Schema.Array(Schema.Struct({ name: Schema.String }));
|
|
5
|
+
const identifier = /^[a-z_]+$/u;
|
|
6
|
+
function columnsOf(store, table) {
|
|
7
|
+
return Schema.decodeUnknownSync(columnsSchema)(store.sql.exec(`PRAGMA table_info(${table})`).toArray()).map(({ name }) => name);
|
|
8
|
+
}
|
|
9
|
+
async function migrateTable(store, sealer, step) {
|
|
10
|
+
if (!identifier.test(step.table))
|
|
11
|
+
throw new Error("Invalid table name");
|
|
12
|
+
if (!step.plaintext(columnsOf(store, step.table)))
|
|
13
|
+
return;
|
|
14
|
+
const rows = Schema.decodeUnknownSync(Schema.Array(rowSchema))(store.sql.exec(`SELECT * FROM ${step.table}`).toArray());
|
|
15
|
+
// No keyring: plaintext rows stay closed to every reader until one exists.
|
|
16
|
+
if (rows.length !== 0 && sealer === undefined)
|
|
17
|
+
throw new CredentialError("unavailable");
|
|
18
|
+
const sealed = [];
|
|
19
|
+
for (const row of rows) {
|
|
20
|
+
if (sealer === undefined)
|
|
21
|
+
throw new CredentialError("unavailable");
|
|
22
|
+
sealed.push(await step.seal(sealer, row));
|
|
23
|
+
}
|
|
24
|
+
const legacy = `${step.table}_plaintext_002`;
|
|
25
|
+
store.transactionSync(() => {
|
|
26
|
+
// A concurrent attempt already finished; its rows are the current ones.
|
|
27
|
+
if (!step.plaintext(columnsOf(store, step.table)))
|
|
28
|
+
return;
|
|
29
|
+
store.sql.exec(`ALTER TABLE ${step.table} RENAME TO ${legacy}`);
|
|
30
|
+
step.ensure(store.sql);
|
|
31
|
+
for (const values of sealed) {
|
|
32
|
+
const names = Object.keys(values);
|
|
33
|
+
if (!names.every((name) => identifier.test(name)))
|
|
34
|
+
throw new Error("Invalid column name");
|
|
35
|
+
store.sql.exec(`INSERT INTO ${step.table} (${names.join(",")}) VALUES (${names.map(() => "?").join(",")})`, ...names.map((name) => values[name] ?? null));
|
|
36
|
+
}
|
|
37
|
+
// Overwrite the secret bytes in place before the pages are freed.
|
|
38
|
+
for (const column of step.secretColumns) {
|
|
39
|
+
if (!identifier.test(column))
|
|
40
|
+
throw new Error("Invalid column name");
|
|
41
|
+
store.sql.exec(`UPDATE ${legacy} SET ${column} = substr(hex(zeroblob(length(CAST(${column} AS BLOB)))), 1, length(CAST(${column} AS BLOB))) WHERE ${column} IS NOT NULL`);
|
|
42
|
+
}
|
|
43
|
+
store.sql.exec(`DROP TABLE ${legacy}`);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Oyster credential migration 002: webhook signing secrets, subscription
|
|
48
|
+
* secrets and open Discord interaction tokens move from plaintext columns to
|
|
49
|
+
* the owner's S1 envelope, and the plaintext columns are dropped with them.
|
|
50
|
+
* Runs before an owner reads or writes its table. A failed attempt is
|
|
51
|
+
* retried by the next use instead of being cached. Without a keyring a
|
|
52
|
+
* plaintext row refuses as `unavailable`; it is never read in the clear.
|
|
53
|
+
*/
|
|
54
|
+
export function credentialMigration002(options) {
|
|
55
|
+
let migration;
|
|
56
|
+
let done = false;
|
|
57
|
+
return async () => {
|
|
58
|
+
if (done)
|
|
59
|
+
return;
|
|
60
|
+
const running = (migration ??= (async () => {
|
|
61
|
+
for (const table of options.tables)
|
|
62
|
+
await migrateTable(options.store, options.sealer, table);
|
|
63
|
+
})());
|
|
64
|
+
try {
|
|
65
|
+
await running;
|
|
66
|
+
done = true;
|
|
67
|
+
}
|
|
68
|
+
catch (cause) {
|
|
69
|
+
if (migration === running)
|
|
70
|
+
migration = undefined;
|
|
71
|
+
throw cause;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|