@adea-ai/runtime-gateway-protocol 1.15.0 → 1.16.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 CHANGED
@@ -1,18 +1,10 @@
1
1
  # @control-plane/runtime-gateway-protocol
2
2
 
3
- Versioned wire protocol between the Control Plane and external runtimes
4
- (gateways, adapters, emulators). Pure Zod schemas plus a protocol emulator and
5
- deterministic fixtures — no transport, no credentials, no secrets.
6
-
7
- ## What's inside
8
-
9
- - **Protocol schemas** — manifests, capabilities, deprecation records, and
10
- envelope schemas for runtime discovery, health, sessions, and routing.
11
- - **Protocol version manifest** — the current and supported
12
- (`major.minor`) protocol versions with deprecation metadata.
13
- - **Emulator** — an in-memory protocol peer for testing gateways without a
14
- live runtime.
15
- - **Authentication helpers** — gateway credential schemas and validation.
3
+ End-to-end device relay envelopes and workspace content keys. A browser, phone
4
+ or device seals a payload to the target device with HPKE and signs it with
5
+ Ed25519; the relay stores and forwards envelopes it cannot read. Pure WebCrypto,
6
+ `@hpke` and Zod, so the same modules run in pi-host, the browser, the phone and
7
+ the relay worker — no transport, no credentials, no secrets.
16
8
 
17
9
  ## Install
18
10
 
@@ -22,22 +14,19 @@ bun add @adea-ai/runtime-gateway-protocol
22
14
 
23
15
  ## Subpath exports
24
16
 
25
- | Path | Contents |
26
- | ----------------------------------- | ---------------------------------------------------------- |
27
- | `.` | Protocol schemas, emulator, fixtures, authentication |
28
- | `./fixtures` | Golden and malformed fixture files for conformance testing |
29
- | `./schema/gateway-envelope.v1.json` | JSON Schema for gateway envelopes |
17
+ | Path | Contents |
18
+ | ---------------- | ----------------------------------------------------------------------- |
19
+ | `./device-relay` | Authenticated relay envelopes, signed sender keyrings and replay checks |
20
+ | `./content-key` | Workspace content keys: derivation, sealing and signed key wraps |
21
+ | `.` | Both of the above |
30
22
 
31
23
  ## Usage
32
24
 
33
25
  ```ts
34
- import { GatewayProtocolManifest, GatewayEnvelopeSchemas } from '@adea-ai/runtime-gateway-protocol'
26
+ import { sealAuthenticatedRelayCommand } from '@adea-ai/runtime-gateway-protocol/device-relay'
27
+ import { sealContent } from '@adea-ai/runtime-gateway-protocol/content-key'
35
28
  ```
36
29
 
37
- Protocol compatibility: the package currently speaks protocol `1.x`; see the
38
- `GatewayProtocolManifest` export for the supported version window and
39
- deprecation records.
40
-
41
30
  ## License
42
31
 
43
32
  Apache-2.0
@@ -28,6 +28,7 @@ export declare const RelayRejectionCodeSchema: z.ZodEnum<{
28
28
  RELAY_ENVELOPE_REPLAYED: "RELAY_ENVELOPE_REPLAYED";
29
29
  RELAY_ENVELOPE_TOO_LARGE: "RELAY_ENVELOPE_TOO_LARGE";
30
30
  RELAY_KEYRING_INVALID: "RELAY_KEYRING_INVALID";
31
+ RELAY_SENDER_CONTENT_ONLY: "RELAY_SENDER_CONTENT_ONLY";
31
32
  RELAY_SENDER_REVOKED: "RELAY_SENDER_REVOKED";
32
33
  RELAY_SENDER_UNAUTHENTICATED: "RELAY_SENDER_UNAUTHENTICATED";
33
34
  RELAY_SIGNATURE_INVALID: "RELAY_SIGNATURE_INVALID";
@@ -155,6 +156,7 @@ export declare const AuthorizedSenderKeySchema: z.ZodObject<{
155
156
  encryptionPublicKey: z.ZodOptional<z.ZodString>;
156
157
  approvedAt: z.ZodISODateTime;
157
158
  revokedAt: z.ZodOptional<z.ZodISODateTime>;
159
+ scope: z.ZodOptional<z.ZodLiteral<"content">>;
158
160
  }, z.core.$strict>;
159
161
  export declare const AuthorizedSenderKeyringBodySchema: z.ZodObject<{
160
162
  keyringVersion: z.ZodLiteral<1>;
@@ -176,6 +178,7 @@ export declare const AuthorizedSenderKeyringBodySchema: z.ZodObject<{
176
178
  encryptionPublicKey: z.ZodOptional<z.ZodString>;
177
179
  approvedAt: z.ZodISODateTime;
178
180
  revokedAt: z.ZodOptional<z.ZodISODateTime>;
181
+ scope: z.ZodOptional<z.ZodLiteral<"content">>;
179
182
  }, z.core.$strict>>;
180
183
  }, z.core.$strict>;
181
184
  export declare const AuthorizedSenderKeyringSchema: z.ZodObject<{
@@ -198,6 +201,7 @@ export declare const AuthorizedSenderKeyringSchema: z.ZodObject<{
198
201
  encryptionPublicKey: z.ZodOptional<z.ZodString>;
199
202
  approvedAt: z.ZodISODateTime;
200
203
  revokedAt: z.ZodOptional<z.ZodISODateTime>;
204
+ scope: z.ZodOptional<z.ZodLiteral<"content">>;
201
205
  }, z.core.$strict>>;
202
206
  signature: z.ZodString;
203
207
  }, z.core.$strict>;
@@ -264,6 +268,7 @@ export interface SealAuthenticatedRelayCommandInput {
264
268
  readonly payloadSchemaVersion: number;
265
269
  readonly issuedAt: string;
266
270
  readonly expiresAt: string;
271
+ readonly ownerExpiresAt?: string;
267
272
  readonly plaintext: Uint8Array;
268
273
  readonly returnKey: {
269
274
  readonly keyId: string;
@@ -275,6 +280,7 @@ export interface SealAuthenticatedRelayCommandInput {
275
280
  readonly privateKey: CryptoKey;
276
281
  };
277
282
  }
283
+ export declare function relayCommandExpiresAt(issuedAt: string, ownerExpiresAt?: string): string;
278
284
  export declare function sealAuthenticatedRelayCommand(input: SealAuthenticatedRelayCommandInput): Promise<AuthenticatedRelayCommandEnvelope>;
279
285
  export interface RelayReplayReservation {
280
286
  readonly senderKeyId: string;
@@ -62,6 +62,7 @@ export const RelayRejectionCodeSchema = z.enum([
62
62
  'RELAY_ENVELOPE_REPLAYED',
63
63
  'RELAY_SENDER_UNAUTHENTICATED',
64
64
  'RELAY_SENDER_REVOKED',
65
+ 'RELAY_SENDER_CONTENT_ONLY',
65
66
  'RELAY_SIGNATURE_INVALID',
66
67
  'RELAY_TARGET_MISMATCH',
67
68
  'RELAY_KEYRING_INVALID',
@@ -125,12 +126,16 @@ export const AuthorizedSenderKeySchema = z
125
126
  encryptionPublicKey: PublicKeySchema.optional(),
126
127
  approvedAt: z.iso.datetime(),
127
128
  revokedAt: z.iso.datetime().optional(),
129
+ scope: z.literal('content').optional(),
128
130
  })
129
131
  .strict()
130
132
  .superRefine((value, context) => {
131
133
  if ((value.kind === 'device') !== (value.deviceId !== undefined)) {
132
134
  context.addIssue({ code: 'custom', message: 'a device key names its device' });
133
135
  }
136
+ if (value.scope === 'content' && value.kind !== 'session') {
137
+ context.addIssue({ code: 'custom', message: 'only a session key is limited to content' });
138
+ }
134
139
  if ((value.encryptionKeyId === undefined) !== (value.encryptionPublicKey === undefined)) {
135
140
  context.addIssue({ code: 'custom', message: 'encryption key id and key go together' });
136
141
  }
@@ -226,8 +231,22 @@ export async function verifyAuthorizedSenderKeyring(input) {
226
231
  }
227
232
  return keyring;
228
233
  }
234
+ export function relayCommandExpiresAt(issuedAt, ownerExpiresAt) {
235
+ const issued = Date.parse(issuedAt);
236
+ const bound = issued + MAX_AUTHENTICATED_RELAY_COMMAND_LIFETIME_MS;
237
+ const owner = ownerExpiresAt === undefined ? bound : Date.parse(ownerExpiresAt);
238
+ if (!Number.isFinite(issued) || !Number.isFinite(owner) || owner <= issued) {
239
+ throw new AuthenticatedRelayError('RELAY_ENVELOPE_LIFETIME_INVALID');
240
+ }
241
+ return new Date(Math.min(bound, owner)).toISOString();
242
+ }
229
243
  export async function sealAuthenticatedRelayCommand(input) {
230
244
  validateDuration(input.issuedAt, input.expiresAt, MAX_AUTHENTICATED_RELAY_COMMAND_LIFETIME_MS);
245
+ if (input.ownerExpiresAt !== undefined &&
246
+ Date.parse(input.expiresAt) >
247
+ Date.parse(relayCommandExpiresAt(input.issuedAt, input.ownerExpiresAt))) {
248
+ throw new AuthenticatedRelayError('RELAY_ENVELOPE_LIFETIME_INVALID');
249
+ }
231
250
  const header = AuthenticatedRelayCommandHeaderSchema.parse({
232
251
  envelopeVersion: AUTHENTICATED_RELAY_ENVELOPE_VERSION,
233
252
  suite: RELAY_HPKE_SUITE,
@@ -288,6 +307,8 @@ export async function openAuthenticatedRelayCommand(input) {
288
307
  if (!(await verifyRelayBytes(sender.signingPublicKey, signature, await envelopeSigningInput(header, encapsulatedKey, ciphertext)))) {
289
308
  throw new AuthenticatedRelayError('RELAY_SIGNATURE_INVALID');
290
309
  }
310
+ if (sender.scope === 'content')
311
+ throw new AuthenticatedRelayError('RELAY_SENDER_CONTENT_ONLY');
291
312
  const delivery = await input.replay.reserve({
292
313
  senderKeyId: header.senderKeyId,
293
314
  nonce: header.nonce,
@@ -0,0 +1,239 @@
1
+ import { z } from 'zod';
2
+ import { type DeviceSyncErrorCode, type SignedSyncEntry } from '@adea-ai/contracts/device-sync';
3
+ import { ContentSealSchema, type ContentKey } from './content-key.js';
4
+ import { type AuthorizedSenderKey, type AuthorizedSenderKeyring, type RelaySigningKeyPair } from './device-relay.js';
5
+ export { DeviceSyncErrorCodeSchema, MAX_SYNC_BATCH, MAX_SYNC_CIPHERTEXT_BYTES, MAX_SYNC_ENTRY_BYTES, MAX_SYNC_LOG_PAGE, MIN_SYNC_CIPHERTEXT_BYTES, SignedSyncEntrySchema, StoredSyncEntrySchema, SyncAppendRequestSchema, SyncAppendResponseSchema, SyncCommitResponseSchema, SyncDownloadRequestSchema, SyncDownloadResponseSchema, SyncEntryIdSchema, SyncLogPageSchema, SyncLogRequestSchema, SyncObjectIdSchema, SyncObjectRequestSchema, SyncReleaseResponseSchema, SyncUploadRequestSchema, SyncUploadResponseSchema, SyncUsageSchema, type DeviceSyncErrorCode, type SignedSyncEntry, type StoredSyncEntry, } from '@adea-ai/contracts/device-sync';
6
+ export declare const SYNC_BLOB_VERSION: 1;
7
+ export declare const SYNC_ENTRY_VERSION: 1;
8
+ export declare const SYNC_CHUNK_BYTES: number;
9
+ export declare const MAX_SYNC_OBJECT_BYTES: number;
10
+ export declare const SYNC_REQUEST_HEADER = "adea-sync-signature";
11
+ export declare class DeviceSyncError extends Error {
12
+ readonly code: DeviceSyncErrorCode;
13
+ constructor(code: DeviceSyncErrorCode);
14
+ }
15
+ export declare const SyncItemIdSchema: z.ZodString;
16
+ export declare const newSyncObjectId: () => string;
17
+ export declare const newSyncEntryId: () => string;
18
+ export declare const newSyncItemId: () => string;
19
+ export declare function padmeLength(length: number): number;
20
+ export declare function sealedBlobLength(plaintextBytes: number): number;
21
+ export interface SyncBlobBinding {
22
+ readonly workspaceId: string;
23
+ readonly objectId: string;
24
+ }
25
+ export declare function generateSyncDataKey(): Uint8Array;
26
+ export declare function sealSyncBlob(dataKey: Uint8Array, binding: SyncBlobBinding, plaintext: Uint8Array): Promise<Uint8Array>;
27
+ export declare function openSyncBlob(dataKey: Uint8Array, binding: SyncBlobBinding, sealed: Uint8Array): Promise<Uint8Array>;
28
+ export declare function sealSyncDataKey(contentKey: ContentKey, binding: SyncBlobBinding, dataKey: Uint8Array): Promise<{
29
+ ckv: number;
30
+ nonce: string;
31
+ ciphertext: string;
32
+ }>;
33
+ export declare function openSyncDataKey(contentKey: ContentKey, binding: SyncBlobBinding, sealed: unknown): Promise<Uint8Array>;
34
+ export declare const SyncNamespaceSchema: z.ZodEnum<{
35
+ artifacts: "artifacts";
36
+ files: "files";
37
+ sessions: "sessions";
38
+ }>;
39
+ export type SyncNamespace = z.infer<typeof SyncNamespaceSchema>;
40
+ export declare const SyncHlcSchema: z.ZodObject<{
41
+ wallMs: z.ZodNumber;
42
+ counter: z.ZodNumber;
43
+ node: z.ZodString;
44
+ }, z.core.$strict>;
45
+ export type SyncHlc = z.infer<typeof SyncHlcSchema>;
46
+ export declare function nextSyncHlc(previous: SyncHlc | undefined, nowMs: number, node: string): SyncHlc;
47
+ export declare function observeSyncHlc(local: SyncHlc | undefined, seen: SyncHlc): SyncHlc | undefined;
48
+ export declare function compareSyncHlc(left: SyncHlc, right: SyncHlc): number;
49
+ export declare const SyncPutEntrySchema: z.ZodObject<{
50
+ entryVersion: z.ZodLiteral<1>;
51
+ entryId: z.ZodString;
52
+ namespace: z.ZodEnum<{
53
+ artifacts: "artifacts";
54
+ files: "files";
55
+ sessions: "sessions";
56
+ }>;
57
+ itemId: z.ZodString;
58
+ hlc: z.ZodObject<{
59
+ wallMs: z.ZodNumber;
60
+ counter: z.ZodNumber;
61
+ node: z.ZodString;
62
+ }, z.core.$strict>;
63
+ kind: z.ZodLiteral<"put">;
64
+ path: z.ZodString;
65
+ objectId: z.ZodString;
66
+ dataKey: z.ZodObject<{
67
+ ckv: z.ZodNumber;
68
+ nonce: z.ZodString;
69
+ ciphertext: z.ZodString;
70
+ }, z.core.$strict>;
71
+ plaintextSha256: z.ZodString;
72
+ size: z.ZodNumber;
73
+ contentType: z.ZodOptional<z.ZodString>;
74
+ mtimeMs: z.ZodOptional<z.ZodNumber>;
75
+ parents: z.ZodArray<z.ZodString>;
76
+ }, z.core.$strict>;
77
+ export declare const SyncDeleteEntrySchema: z.ZodObject<{
78
+ entryVersion: z.ZodLiteral<1>;
79
+ entryId: z.ZodString;
80
+ namespace: z.ZodEnum<{
81
+ artifacts: "artifacts";
82
+ files: "files";
83
+ sessions: "sessions";
84
+ }>;
85
+ itemId: z.ZodString;
86
+ hlc: z.ZodObject<{
87
+ wallMs: z.ZodNumber;
88
+ counter: z.ZodNumber;
89
+ node: z.ZodString;
90
+ }, z.core.$strict>;
91
+ kind: z.ZodLiteral<"delete">;
92
+ parents: z.ZodArray<z.ZodString>;
93
+ }, z.core.$strict>;
94
+ export declare const SyncRewrapEntrySchema: z.ZodObject<{
95
+ entryVersion: z.ZodLiteral<1>;
96
+ entryId: z.ZodString;
97
+ namespace: z.ZodEnum<{
98
+ artifacts: "artifacts";
99
+ files: "files";
100
+ sessions: "sessions";
101
+ }>;
102
+ itemId: z.ZodString;
103
+ hlc: z.ZodObject<{
104
+ wallMs: z.ZodNumber;
105
+ counter: z.ZodNumber;
106
+ node: z.ZodString;
107
+ }, z.core.$strict>;
108
+ kind: z.ZodLiteral<"rewrap">;
109
+ objectId: z.ZodString;
110
+ dataKey: z.ZodObject<{
111
+ ckv: z.ZodNumber;
112
+ nonce: z.ZodString;
113
+ ciphertext: z.ZodString;
114
+ }, z.core.$strict>;
115
+ }, z.core.$strict>;
116
+ export declare const SyncEntrySchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
117
+ entryVersion: z.ZodLiteral<1>;
118
+ entryId: z.ZodString;
119
+ namespace: z.ZodEnum<{
120
+ artifacts: "artifacts";
121
+ files: "files";
122
+ sessions: "sessions";
123
+ }>;
124
+ itemId: z.ZodString;
125
+ hlc: z.ZodObject<{
126
+ wallMs: z.ZodNumber;
127
+ counter: z.ZodNumber;
128
+ node: z.ZodString;
129
+ }, z.core.$strict>;
130
+ kind: z.ZodLiteral<"put">;
131
+ path: z.ZodString;
132
+ objectId: z.ZodString;
133
+ dataKey: z.ZodObject<{
134
+ ckv: z.ZodNumber;
135
+ nonce: z.ZodString;
136
+ ciphertext: z.ZodString;
137
+ }, z.core.$strict>;
138
+ plaintextSha256: z.ZodString;
139
+ size: z.ZodNumber;
140
+ contentType: z.ZodOptional<z.ZodString>;
141
+ mtimeMs: z.ZodOptional<z.ZodNumber>;
142
+ parents: z.ZodArray<z.ZodString>;
143
+ }, z.core.$strict>, z.ZodObject<{
144
+ entryVersion: z.ZodLiteral<1>;
145
+ entryId: z.ZodString;
146
+ namespace: z.ZodEnum<{
147
+ artifacts: "artifacts";
148
+ files: "files";
149
+ sessions: "sessions";
150
+ }>;
151
+ itemId: z.ZodString;
152
+ hlc: z.ZodObject<{
153
+ wallMs: z.ZodNumber;
154
+ counter: z.ZodNumber;
155
+ node: z.ZodString;
156
+ }, z.core.$strict>;
157
+ kind: z.ZodLiteral<"delete">;
158
+ parents: z.ZodArray<z.ZodString>;
159
+ }, z.core.$strict>, z.ZodObject<{
160
+ entryVersion: z.ZodLiteral<1>;
161
+ entryId: z.ZodString;
162
+ namespace: z.ZodEnum<{
163
+ artifacts: "artifacts";
164
+ files: "files";
165
+ sessions: "sessions";
166
+ }>;
167
+ itemId: z.ZodString;
168
+ hlc: z.ZodObject<{
169
+ wallMs: z.ZodNumber;
170
+ counter: z.ZodNumber;
171
+ node: z.ZodString;
172
+ }, z.core.$strict>;
173
+ kind: z.ZodLiteral<"rewrap">;
174
+ objectId: z.ZodString;
175
+ dataKey: z.ZodObject<{
176
+ ckv: z.ZodNumber;
177
+ nonce: z.ZodString;
178
+ ciphertext: z.ZodString;
179
+ }, z.core.$strict>;
180
+ }, z.core.$strict>], "kind">;
181
+ export type SyncEntry = z.infer<typeof SyncEntrySchema>;
182
+ export type SyncPutEntry = z.infer<typeof SyncPutEntrySchema>;
183
+ export declare function sealSyncEntry(input: {
184
+ readonly contentKey: ContentKey;
185
+ readonly workspaceId: string;
186
+ readonly entry: SyncEntry;
187
+ readonly author: RelaySigningKeyPair;
188
+ }): Promise<SignedSyncEntry>;
189
+ export declare function syncAuthor(keyring: AuthorizedSenderKeyring, workspaceId: string, keyId: string, at: number): AuthorizedSenderKey;
190
+ export declare function openSyncEntry(input: {
191
+ readonly workspaceId: string;
192
+ readonly keyring: AuthorizedSenderKeyring;
193
+ readonly entry: unknown;
194
+ readonly contentKey: (ckv: number) => Promise<ContentKey | undefined> | ContentKey | undefined;
195
+ }): Promise<SyncEntry & {
196
+ readonly authorKeyId: string;
197
+ }>;
198
+ export declare function assertSyncLogAdvances(lastSeenSeq: number, page: readonly {
199
+ seq: number;
200
+ }[]): void;
201
+ export interface SyncItemVersion {
202
+ readonly entryId: string;
203
+ readonly path: string;
204
+ readonly objectId: string;
205
+ readonly dataKey: z.infer<typeof ContentSealSchema>;
206
+ readonly plaintextSha256: string;
207
+ readonly size: number;
208
+ readonly contentType?: string;
209
+ readonly mtimeMs?: number;
210
+ readonly hlc: SyncHlc;
211
+ readonly authorKeyId: string;
212
+ }
213
+ export interface SyncItemState {
214
+ readonly itemId: string;
215
+ readonly namespace: SyncNamespace;
216
+ readonly current?: SyncItemVersion;
217
+ readonly conflicts: readonly SyncItemVersion[];
218
+ readonly missingParents: readonly string[];
219
+ }
220
+ type OpenedEntry = SyncEntry & {
221
+ readonly authorKeyId: string;
222
+ };
223
+ export declare function foldSyncItems(entries: Iterable<OpenedEntry>): Map<string, SyncItemState>;
224
+ export declare function signSyncRequest(input: {
225
+ readonly key: RelaySigningKeyPair;
226
+ readonly method: string;
227
+ readonly path: string;
228
+ readonly body: Uint8Array;
229
+ readonly now: Date;
230
+ }): Promise<string>;
231
+ export declare function verifySyncRequest(input: {
232
+ readonly header: string | null | undefined;
233
+ readonly method: string;
234
+ readonly path: string;
235
+ readonly body: Uint8Array;
236
+ readonly keyring: AuthorizedSenderKeyring;
237
+ readonly workspaceId: string;
238
+ readonly now: Date;
239
+ }): Promise<AuthorizedSenderKey>;