@abloatai/ablo 0.27.0 → 0.28.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.
- package/CHANGELOG.md +29 -3
- package/README.md +1 -1
- package/dist/BaseSyncedStore.js +8 -9
- package/dist/Database.d.ts +14 -1
- package/dist/Database.js +225 -28
- package/dist/Model.d.ts +17 -0
- package/dist/Model.js +32 -1
- package/dist/SyncClient.d.ts +10 -6
- package/dist/SyncClient.js +379 -76
- package/dist/adapters/inMemoryStorage.d.ts +1 -0
- package/dist/adapters/inMemoryStorage.js +12 -0
- package/dist/cli.cjs +6 -2
- package/dist/client/Ablo.d.ts +24 -1
- package/dist/client/Ablo.js +4 -3
- package/dist/client/ApiClient.js +309 -43
- package/dist/client/createInternalComponents.d.ts +2 -0
- package/dist/client/createInternalComponents.js +1 -1
- package/dist/client/httpClient.d.ts +2 -0
- package/dist/client/modelRegistration.js +11 -0
- package/dist/client/options.d.ts +23 -0
- package/dist/client/wsMutationExecutor.d.ts +3 -2
- package/dist/client/wsMutationExecutor.js +3 -2
- package/dist/commit/contract.d.ts +493 -0
- package/dist/commit/contract.js +187 -0
- package/dist/commit/index.d.ts +6 -0
- package/dist/commit/index.js +5 -0
- package/dist/core/StoreManager.d.ts +2 -0
- package/dist/core/StoreManager.js +12 -0
- package/dist/errorCodes.js +6 -2
- package/dist/index.d.ts +6 -0
- package/dist/index.js +2 -0
- package/dist/interfaces/index.d.ts +2 -2
- package/dist/mutators/UndoManager.d.ts +2 -0
- package/dist/mutators/UndoManager.js +32 -0
- package/dist/react/useAblo.d.ts +6 -4
- package/dist/react/useAblo.js +25 -3
- package/dist/schema/index.d.ts +1 -1
- package/dist/schema/schema.d.ts +31 -1
- package/dist/stores/ObjectStore.d.ts +14 -1
- package/dist/stores/ObjectStore.js +27 -4
- package/dist/stores/ObjectStoreContract.d.ts +2 -0
- package/dist/surface.d.ts +1 -1
- package/dist/surface.js +2 -0
- package/dist/sync/SyncWebSocket.d.ts +2 -1
- package/dist/sync/persistedPrefix.d.ts +12 -0
- package/dist/sync/persistedPrefix.js +22 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/testing/index.js +1 -0
- package/dist/testing/mocks/FakeDatabase.d.ts +18 -0
- package/dist/testing/mocks/FakeDatabase.js +10 -0
- package/dist/testing/mocks/MockWebSocket.d.ts +2 -1
- package/dist/transactions/TransactionQueue.d.ts +66 -8
- package/dist/transactions/TransactionQueue.js +607 -89
- package/dist/transactions/commitEnvelope.d.ts +132 -0
- package/dist/transactions/commitEnvelope.js +139 -0
- package/dist/transactions/commitOutboxStore.d.ts +32 -0
- package/dist/transactions/commitOutboxStore.js +26 -0
- package/dist/transactions/commitPayload.d.ts +15 -0
- package/dist/transactions/commitPayload.js +6 -0
- package/dist/transactions/httpCommitEnvelope.d.ts +43 -0
- package/dist/transactions/httpCommitEnvelope.js +179 -0
- package/dist/transactions/replayValidation.d.ts +83 -0
- package/dist/transactions/replayValidation.js +46 -1
- package/dist/wire/bootstrapReason.d.ts +9 -0
- package/dist/wire/bootstrapReason.js +8 -0
- package/dist/wire/frames.d.ts +236 -0
- package/dist/wire/frames.js +21 -0
- package/dist/wire/index.d.ts +4 -2
- package/dist/wire/index.js +2 -1
- package/docs/api.md +10 -10
- package/docs/coordination.md +2 -2
- package/docs/mcp.md +1 -1
- package/package.json +7 -2
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime contracts for retry-safe commits.
|
|
3
|
+
*
|
|
4
|
+
* `commitEnvelopeMemberSchema` is the compact pointer stored on an in-memory
|
|
5
|
+
* transaction. `durableCommitEnvelopeSchema` is the actual outbox record: one
|
|
6
|
+
* atomic IndexedDB value containing the stable request key, exact ordered wire
|
|
7
|
+
* operations, and the source mutations it supersedes.
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
export declare const COMMIT_ENVELOPE_VERSION: 1;
|
|
11
|
+
export declare const COMMIT_ENVELOPE_RECORD_PREFIX = "commit-envelope:";
|
|
12
|
+
/** One transaction's position in a commit; this is not the envelope itself. */
|
|
13
|
+
export declare const commitEnvelopeMemberSchema: z.ZodObject<{
|
|
14
|
+
idempotencyKey: z.core.$ZodBranded<z.ZodString, "IdempotencyKey", "out">;
|
|
15
|
+
operationIndex: z.ZodNumber;
|
|
16
|
+
operationCount: z.ZodNumber;
|
|
17
|
+
sealedAt: z.ZodOptional<z.ZodNumber>;
|
|
18
|
+
sequence: z.ZodOptional<z.ZodNumber>;
|
|
19
|
+
}, z.core.$strict>;
|
|
20
|
+
export type CommitEnvelopeMember = z.infer<typeof commitEnvelopeMemberSchema>;
|
|
21
|
+
/** The legacy mutation operation shape sent by the current commit transport. */
|
|
22
|
+
export declare const durableCommitOperationSchema: z.ZodObject<{
|
|
23
|
+
type: z.ZodEnum<{
|
|
24
|
+
CREATE: "CREATE";
|
|
25
|
+
UPDATE: "UPDATE";
|
|
26
|
+
DELETE: "DELETE";
|
|
27
|
+
ARCHIVE: "ARCHIVE";
|
|
28
|
+
UNARCHIVE: "UNARCHIVE";
|
|
29
|
+
}>;
|
|
30
|
+
readAt: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
31
|
+
onStale: z.ZodOptional<z.ZodNullable<z.ZodEnum<{
|
|
32
|
+
reject: "reject";
|
|
33
|
+
overwrite: "overwrite";
|
|
34
|
+
notify: "notify";
|
|
35
|
+
}>>>;
|
|
36
|
+
model: z.ZodString;
|
|
37
|
+
id: z.ZodString;
|
|
38
|
+
input: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
39
|
+
transactionId: z.ZodOptional<z.ZodString>;
|
|
40
|
+
options: z.ZodOptional<z.ZodObject<{
|
|
41
|
+
idempotencyKey: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
42
|
+
label: z.ZodOptional<z.ZodString>;
|
|
43
|
+
}, z.core.$strict>>;
|
|
44
|
+
}, z.core.$strip>;
|
|
45
|
+
export type DurableCommitOperation = z.infer<typeof durableCommitOperationSchema>;
|
|
46
|
+
export type DurableCommitOperationInput = z.input<typeof durableCommitOperationSchema>;
|
|
47
|
+
export declare const commitOutboxScopeSchema: z.ZodObject<{
|
|
48
|
+
organizationId: z.ZodString;
|
|
49
|
+
participantId: z.ZodString;
|
|
50
|
+
namespace: z.ZodString;
|
|
51
|
+
}, z.core.$strict>;
|
|
52
|
+
export type CommitOutboxScope = z.infer<typeof commitOutboxScopeSchema>;
|
|
53
|
+
/**
|
|
54
|
+
* One crash-durable logical commit. Keeping every operation in one record makes
|
|
55
|
+
* membership and order atomic: recovery can observe the old record or the new
|
|
56
|
+
* record, never half an envelope.
|
|
57
|
+
*/
|
|
58
|
+
export declare const durableCommitEnvelopeSchema: z.ZodObject<{
|
|
59
|
+
id: z.ZodString;
|
|
60
|
+
type: z.ZodLiteral<"commit_envelope">;
|
|
61
|
+
storageVersion: z.ZodLiteral<1>;
|
|
62
|
+
origin: z.ZodEnum<{
|
|
63
|
+
model_batch: "model_batch";
|
|
64
|
+
atomic_commit: "atomic_commit";
|
|
65
|
+
}>;
|
|
66
|
+
idempotencyKey: z.core.$ZodBranded<z.ZodString, "IdempotencyKey", "out">;
|
|
67
|
+
operations: z.ZodArray<z.ZodObject<{
|
|
68
|
+
type: z.ZodEnum<{
|
|
69
|
+
CREATE: "CREATE";
|
|
70
|
+
UPDATE: "UPDATE";
|
|
71
|
+
DELETE: "DELETE";
|
|
72
|
+
ARCHIVE: "ARCHIVE";
|
|
73
|
+
UNARCHIVE: "UNARCHIVE";
|
|
74
|
+
}>;
|
|
75
|
+
readAt: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
76
|
+
onStale: z.ZodOptional<z.ZodNullable<z.ZodEnum<{
|
|
77
|
+
reject: "reject";
|
|
78
|
+
overwrite: "overwrite";
|
|
79
|
+
notify: "notify";
|
|
80
|
+
}>>>;
|
|
81
|
+
model: z.ZodString;
|
|
82
|
+
id: z.ZodString;
|
|
83
|
+
input: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
84
|
+
transactionId: z.ZodOptional<z.ZodString>;
|
|
85
|
+
options: z.ZodOptional<z.ZodObject<{
|
|
86
|
+
idempotencyKey: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
87
|
+
label: z.ZodOptional<z.ZodString>;
|
|
88
|
+
}, z.core.$strict>>;
|
|
89
|
+
}, z.core.$strip>>;
|
|
90
|
+
sourceMutationIds: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
91
|
+
commitOptions: z.ZodDefault<z.ZodObject<{
|
|
92
|
+
causedByTaskId: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
93
|
+
reads: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
94
|
+
model: z.ZodString;
|
|
95
|
+
id: z.ZodString;
|
|
96
|
+
readAt: z.ZodNumber;
|
|
97
|
+
fields: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
98
|
+
onStale: z.ZodOptional<z.ZodEnum<{
|
|
99
|
+
reject: "reject";
|
|
100
|
+
overwrite: "overwrite";
|
|
101
|
+
notify: "notify";
|
|
102
|
+
}>>;
|
|
103
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
104
|
+
group: z.ZodString;
|
|
105
|
+
readAt: z.ZodNumber;
|
|
106
|
+
onStale: z.ZodOptional<z.ZodEnum<{
|
|
107
|
+
reject: "reject";
|
|
108
|
+
overwrite: "overwrite";
|
|
109
|
+
notify: "notify";
|
|
110
|
+
}>>;
|
|
111
|
+
}, z.core.$strip>]>>>>;
|
|
112
|
+
}, z.core.$strict>>;
|
|
113
|
+
scope: z.ZodOptional<z.ZodObject<{
|
|
114
|
+
organizationId: z.ZodString;
|
|
115
|
+
participantId: z.ZodString;
|
|
116
|
+
namespace: z.ZodString;
|
|
117
|
+
}, z.core.$strict>>;
|
|
118
|
+
createdAt: z.ZodNumber;
|
|
119
|
+
sealedAt: z.ZodNumber;
|
|
120
|
+
sequence: z.ZodOptional<z.ZodNumber>;
|
|
121
|
+
timestamp: z.ZodNumber;
|
|
122
|
+
}, z.core.$strict>;
|
|
123
|
+
export type DurableCommitEnvelope = z.infer<typeof durableCommitEnvelopeSchema>;
|
|
124
|
+
export declare function commitEnvelopeRecordId(idempotencyKey: string): string;
|
|
125
|
+
/** Constructs validated member metadata when an in-memory batch is formed. */
|
|
126
|
+
export declare function createCommitEnvelopeMember(value: z.input<typeof commitEnvelopeMemberSchema>): CommitEnvelopeMember;
|
|
127
|
+
/**
|
|
128
|
+
* Freezes the exact JSON request that will be persisted and sent. The JSON
|
|
129
|
+
* round-trip deliberately applies the same Date/undefined semantics as the
|
|
130
|
+
* WebSocket transport before the request fingerprint becomes durable.
|
|
131
|
+
*/
|
|
132
|
+
export declare function createDurableCommitEnvelope(value: Omit<z.input<typeof durableCommitEnvelopeSchema>, 'id' | 'type' | 'storageVersion' | 'timestamp'>): DurableCommitEnvelope;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime contracts for retry-safe commits.
|
|
3
|
+
*
|
|
4
|
+
* `commitEnvelopeMemberSchema` is the compact pointer stored on an in-memory
|
|
5
|
+
* transaction. `durableCommitEnvelopeSchema` is the actual outbox record: one
|
|
6
|
+
* atomic IndexedDB value containing the stable request key, exact ordered wire
|
|
7
|
+
* operations, and the source mutations it supersedes.
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
import { idempotencyKeySchema } from '../commit/contract.js';
|
|
11
|
+
import { readDependencySchema } from '../coordination/schema.js';
|
|
12
|
+
import { commitOperationSchema } from '../wire/frames.js';
|
|
13
|
+
export const COMMIT_ENVELOPE_VERSION = 1;
|
|
14
|
+
export const COMMIT_ENVELOPE_RECORD_PREFIX = 'commit-envelope:';
|
|
15
|
+
/** One transaction's position in a commit; this is not the envelope itself. */
|
|
16
|
+
export const commitEnvelopeMemberSchema = z
|
|
17
|
+
.strictObject({
|
|
18
|
+
idempotencyKey: idempotencyKeySchema,
|
|
19
|
+
operationIndex: z.number().int().nonnegative(),
|
|
20
|
+
operationCount: z.number().int().positive(),
|
|
21
|
+
sealedAt: z.number().int().nonnegative().optional(),
|
|
22
|
+
sequence: z.number().int().nonnegative().optional(),
|
|
23
|
+
})
|
|
24
|
+
.refine(({ operationIndex, operationCount }) => operationIndex < operationCount, { message: 'operationIndex must be smaller than operationCount' });
|
|
25
|
+
/** The legacy mutation operation shape sent by the current commit transport. */
|
|
26
|
+
export const durableCommitOperationSchema = commitOperationSchema
|
|
27
|
+
.pick({
|
|
28
|
+
type: true,
|
|
29
|
+
model: true,
|
|
30
|
+
id: true,
|
|
31
|
+
input: true,
|
|
32
|
+
transactionId: true,
|
|
33
|
+
readAt: true,
|
|
34
|
+
onStale: true,
|
|
35
|
+
})
|
|
36
|
+
.extend({
|
|
37
|
+
model: z.string().min(1),
|
|
38
|
+
id: z.string().min(1),
|
|
39
|
+
input: z.record(z.string(), z.unknown()).optional(),
|
|
40
|
+
transactionId: z.string().min(1).optional(),
|
|
41
|
+
options: z
|
|
42
|
+
.strictObject({
|
|
43
|
+
idempotencyKey: z.string().min(1).max(255).nullable().optional(),
|
|
44
|
+
label: z.string().min(1).max(255).optional(),
|
|
45
|
+
})
|
|
46
|
+
.optional(),
|
|
47
|
+
});
|
|
48
|
+
const durableCommitOptionsSchema = z.strictObject({
|
|
49
|
+
causedByTaskId: z.string().min(1).nullable().optional(),
|
|
50
|
+
reads: z.array(readDependencySchema).nullable().optional(),
|
|
51
|
+
});
|
|
52
|
+
export const commitOutboxScopeSchema = z.strictObject({
|
|
53
|
+
organizationId: z.string().min(1),
|
|
54
|
+
participantId: z.string().min(1),
|
|
55
|
+
namespace: z.string().min(1),
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* One crash-durable logical commit. Keeping every operation in one record makes
|
|
59
|
+
* membership and order atomic: recovery can observe the old record or the new
|
|
60
|
+
* record, never half an envelope.
|
|
61
|
+
*/
|
|
62
|
+
export const durableCommitEnvelopeSchema = z
|
|
63
|
+
.strictObject({
|
|
64
|
+
id: z.string().startsWith(COMMIT_ENVELOPE_RECORD_PREFIX),
|
|
65
|
+
type: z.literal('commit_envelope'),
|
|
66
|
+
storageVersion: z.literal(COMMIT_ENVELOPE_VERSION),
|
|
67
|
+
origin: z.enum(['model_batch', 'atomic_commit']),
|
|
68
|
+
idempotencyKey: idempotencyKeySchema,
|
|
69
|
+
operations: z.array(durableCommitOperationSchema).min(1).max(500),
|
|
70
|
+
// Bookkeeping cardinality is independent of the 500 wire-operation cap:
|
|
71
|
+
// hundreds of same-row offline patches may coalesce into one operation.
|
|
72
|
+
sourceMutationIds: z.array(z.string().min(1)).default([]),
|
|
73
|
+
commitOptions: durableCommitOptionsSchema.default({}),
|
|
74
|
+
scope: commitOutboxScopeSchema.optional(),
|
|
75
|
+
createdAt: z.number().int().nonnegative(),
|
|
76
|
+
sealedAt: z.number().int().nonnegative(),
|
|
77
|
+
/** Monotonic within one client; disambiguates writes sealed in the same ms. */
|
|
78
|
+
sequence: z.number().int().nonnegative().optional(),
|
|
79
|
+
timestamp: z.number().int().nonnegative(),
|
|
80
|
+
})
|
|
81
|
+
.superRefine((envelope, context) => {
|
|
82
|
+
if (envelope.id !== commitEnvelopeRecordId(envelope.idempotencyKey)) {
|
|
83
|
+
context.addIssue({
|
|
84
|
+
code: 'custom',
|
|
85
|
+
path: ['id'],
|
|
86
|
+
message: 'Envelope record id must be derived from its idempotency key',
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (new Set(envelope.sourceMutationIds).size !== envelope.sourceMutationIds.length) {
|
|
90
|
+
context.addIssue({
|
|
91
|
+
code: 'custom',
|
|
92
|
+
path: ['sourceMutationIds'],
|
|
93
|
+
message: 'Source mutation ids must be unique',
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (envelope.origin === 'model_batch') {
|
|
97
|
+
const transactionIds = envelope.operations.map((operation) => operation.transactionId);
|
|
98
|
+
if (transactionIds.some((id) => typeof id !== 'string' || id.length === 0)) {
|
|
99
|
+
context.addIssue({
|
|
100
|
+
code: 'custom',
|
|
101
|
+
path: ['operations'],
|
|
102
|
+
message: 'Every model-batch operation must carry a transactionId',
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
else if (new Set(transactionIds).size !== transactionIds.length) {
|
|
106
|
+
context.addIssue({
|
|
107
|
+
code: 'custom',
|
|
108
|
+
path: ['operations'],
|
|
109
|
+
message: 'Model-batch transactionIds must be unique',
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
export function commitEnvelopeRecordId(idempotencyKey) {
|
|
115
|
+
return `${COMMIT_ENVELOPE_RECORD_PREFIX}${idempotencyKey}`;
|
|
116
|
+
}
|
|
117
|
+
/** Constructs validated member metadata when an in-memory batch is formed. */
|
|
118
|
+
export function createCommitEnvelopeMember(value) {
|
|
119
|
+
return commitEnvelopeMemberSchema.parse(value);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Freezes the exact JSON request that will be persisted and sent. The JSON
|
|
123
|
+
* round-trip deliberately applies the same Date/undefined semantics as the
|
|
124
|
+
* WebSocket transport before the request fingerprint becomes durable.
|
|
125
|
+
*/
|
|
126
|
+
export function createDurableCommitEnvelope(value) {
|
|
127
|
+
const candidate = {
|
|
128
|
+
...value,
|
|
129
|
+
id: commitEnvelopeRecordId(value.idempotencyKey),
|
|
130
|
+
type: 'commit_envelope',
|
|
131
|
+
storageVersion: COMMIT_ENVELOPE_VERSION,
|
|
132
|
+
timestamp: value.sealedAt,
|
|
133
|
+
};
|
|
134
|
+
const serialized = JSON.stringify(candidate);
|
|
135
|
+
if (serialized === undefined) {
|
|
136
|
+
throw new TypeError('Commit envelope is not JSON serializable');
|
|
137
|
+
}
|
|
138
|
+
return durableCommitEnvelopeSchema.parse(JSON.parse(serialized));
|
|
139
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Narrow persistence port for crash-durable commit envelopes.
|
|
3
|
+
*
|
|
4
|
+
* Browser clients use {@link DatabaseCommitOutboxStore}. Agent/Node hosts can
|
|
5
|
+
* inject a workflow-, SQLite-, or filesystem-backed implementation without
|
|
6
|
+
* coupling TransactionQueue to the browser database/cache implementation.
|
|
7
|
+
*/
|
|
8
|
+
import type { Database } from '../Database.js';
|
|
9
|
+
import type { DurableCommitEnvelope } from './commitEnvelope.js';
|
|
10
|
+
import type { DurableHttpCommitEnvelope } from './httpCommitEnvelope.js';
|
|
11
|
+
export type CommitOutboxRecord = DurableCommitEnvelope | DurableHttpCommitEnvelope;
|
|
12
|
+
export interface CommitOutboxStore {
|
|
13
|
+
/**
|
|
14
|
+
* Atomically reserve an envelope and consume the staged records it owns.
|
|
15
|
+
* Implementations must be scoped to one logical participant + server plane,
|
|
16
|
+
* reject same-id/different-request seals, and let only one envelope claim a
|
|
17
|
+
* staged record.
|
|
18
|
+
*/
|
|
19
|
+
seal(envelope: CommitOutboxRecord, consumedRecordIds: readonly string[]): Promise<void>;
|
|
20
|
+
/** Load unacknowledged records. Implementations may return untrusted data. */
|
|
21
|
+
list(): Promise<readonly unknown[]>;
|
|
22
|
+
/** Remove one definitively settled envelope. */
|
|
23
|
+
remove(envelopeRecordId: string): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
/** Strict IndexedDB adapter backed by Database's `__transactions` store. */
|
|
26
|
+
export declare class DatabaseCommitOutboxStore implements CommitOutboxStore {
|
|
27
|
+
private readonly database;
|
|
28
|
+
constructor(database: Database);
|
|
29
|
+
seal(envelope: CommitOutboxRecord, consumedRecordIds: readonly string[]): Promise<void>;
|
|
30
|
+
list(): Promise<readonly unknown[]>;
|
|
31
|
+
remove(envelopeRecordId: string): Promise<void>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Narrow persistence port for crash-durable commit envelopes.
|
|
3
|
+
*
|
|
4
|
+
* Browser clients use {@link DatabaseCommitOutboxStore}. Agent/Node hosts can
|
|
5
|
+
* inject a workflow-, SQLite-, or filesystem-backed implementation without
|
|
6
|
+
* coupling TransactionQueue to the browser database/cache implementation.
|
|
7
|
+
*/
|
|
8
|
+
/** Strict IndexedDB adapter backed by Database's `__transactions` store. */
|
|
9
|
+
export class DatabaseCommitOutboxStore {
|
|
10
|
+
database;
|
|
11
|
+
constructor(database) {
|
|
12
|
+
this.database = database;
|
|
13
|
+
}
|
|
14
|
+
async seal(envelope, consumedRecordIds) {
|
|
15
|
+
// This adapter deliberately has no duck-typed fallback. Reporting a
|
|
16
|
+
// successful seal after a no-op or a non-atomic save/delete handoff would
|
|
17
|
+
// authorize network dispatch without the durability this port promises.
|
|
18
|
+
await this.database.sealTransactionRecord(envelope, consumedRecordIds);
|
|
19
|
+
}
|
|
20
|
+
async list() {
|
|
21
|
+
return this.database.getPersistedTransactions();
|
|
22
|
+
}
|
|
23
|
+
async remove(envelopeRecordId) {
|
|
24
|
+
await this.database.removeTransaction(envelopeRecordId);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
*/
|
|
11
11
|
import { MutationOperationType } from '../types/index.js';
|
|
12
12
|
import type { MutationOptions, WriteOptions } from '../interfaces/index.js';
|
|
13
|
+
import type { CommitEnvelopeMember } from './commitEnvelope.js';
|
|
13
14
|
export interface UserContext {
|
|
14
15
|
userId: string;
|
|
15
16
|
organizationId: string;
|
|
@@ -55,6 +56,18 @@ export interface Transaction {
|
|
|
55
56
|
priorityScore: number;
|
|
56
57
|
writeOptions?: WriteOptions;
|
|
57
58
|
batchId?: string;
|
|
59
|
+
/**
|
|
60
|
+
* Stable identity of the wire commit that currently owns this operation.
|
|
61
|
+
*
|
|
62
|
+
* A transport failure is ambiguous: the server may have committed the
|
|
63
|
+
* batch even though the acknowledgement never reached this client. Keeping
|
|
64
|
+
* this envelope on every member lets the queue replay the exact same ordered
|
|
65
|
+
* batch with the exact same idempotency key instead of accidentally
|
|
66
|
+
* re-batching its operations under a fresh key.
|
|
67
|
+
*/
|
|
68
|
+
commitEnvelope?: CommitEnvelopeMember;
|
|
69
|
+
/** Pending-mutation journal entries atomically consumed by this envelope. */
|
|
70
|
+
sourceMutationIds?: string[];
|
|
58
71
|
/** Completed locally without a server operation; no sync echo will arrive. */
|
|
59
72
|
localOnly?: boolean;
|
|
60
73
|
/** Sync-id threshold: the transaction confirms once a delta with an id at least this value arrives. */
|
|
@@ -90,6 +103,8 @@ export declare const stripModelSuffix: (modelName: string) => string;
|
|
|
90
103
|
export declare const computePriorityScore: (type: Transaction["type"], modelName: string) => number;
|
|
91
104
|
export declare const TX_TYPE_TO_MUTATION_OP: Record<Transaction['type'], MutationOperationType>;
|
|
92
105
|
export declare function hasStaleWriteOptions(options?: WriteOptions): boolean;
|
|
106
|
+
/** Options whose identity/audit semantics forbid merging two caller writes. */
|
|
107
|
+
export declare function hasCommitCoalescingBarrier(options?: WriteOptions): boolean;
|
|
93
108
|
export interface WriteOperationFields {
|
|
94
109
|
readAt?: number | null;
|
|
95
110
|
onStale?: 'reject' | 'overwrite' | 'notify' | null;
|
|
@@ -107,6 +107,12 @@ export function hasStaleWriteOptions(options) {
|
|
|
107
107
|
return (options?.readAt !== undefined ||
|
|
108
108
|
options?.onStale !== undefined);
|
|
109
109
|
}
|
|
110
|
+
/** Options whose identity/audit semantics forbid merging two caller writes. */
|
|
111
|
+
export function hasCommitCoalescingBarrier(options) {
|
|
112
|
+
return (hasStaleWriteOptions(options) ||
|
|
113
|
+
typeof options?.idempotencyKey === 'string' ||
|
|
114
|
+
typeof options?.label === 'string');
|
|
115
|
+
}
|
|
110
116
|
/**
|
|
111
117
|
* Copies a transaction's `writeOptions` onto the wire operation. The
|
|
112
118
|
* stale-context guards (`readAt` and `onStale`) sit at the operation's root,
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Crash-durable exact HTTP request used by the stateless agent client. */
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export declare const HTTP_COMMIT_ENVELOPE_VERSION: 1;
|
|
4
|
+
export declare const HTTP_COMMIT_ENVELOPE_PREFIX = "http-commit-envelope:";
|
|
5
|
+
/** Stay one hour inside the server's 24-hour idempotency retention window. */
|
|
6
|
+
export declare const HTTP_COMMIT_REPLAY_WINDOW_MS: number;
|
|
7
|
+
/** Apply normal JSON semantics once, then make object-key order canonical. */
|
|
8
|
+
export declare function canonicalHttpCommitBody(value: unknown): string;
|
|
9
|
+
export declare const durableHttpCommitEnvelopeSchema: z.ZodObject<{
|
|
10
|
+
id: z.ZodString;
|
|
11
|
+
type: z.ZodLiteral<"http_commit_envelope">;
|
|
12
|
+
storageVersion: z.ZodLiteral<1>;
|
|
13
|
+
idempotencyKey: z.core.$ZodBranded<z.ZodString, "IdempotencyKey", "out">;
|
|
14
|
+
request: z.ZodObject<{
|
|
15
|
+
method: z.ZodEnum<{
|
|
16
|
+
DELETE: "DELETE";
|
|
17
|
+
POST: "POST";
|
|
18
|
+
PATCH: "PATCH";
|
|
19
|
+
}>;
|
|
20
|
+
path: z.ZodString;
|
|
21
|
+
body: z.ZodString;
|
|
22
|
+
}, z.core.$strict>;
|
|
23
|
+
scopeNamespace: z.ZodString;
|
|
24
|
+
createdAt: z.ZodNumber;
|
|
25
|
+
sealedAt: z.ZodNumber;
|
|
26
|
+
sequence: z.ZodOptional<z.ZodNumber>;
|
|
27
|
+
timestamp: z.ZodNumber;
|
|
28
|
+
}, z.core.$strict>;
|
|
29
|
+
export type DurableHttpCommitEnvelope = z.infer<typeof durableHttpCommitEnvelopeSchema>;
|
|
30
|
+
export declare function httpCommitEnvelopeRecordId(idempotencyKey: string, scopeNamespace?: string): string;
|
|
31
|
+
export declare function createDurableHttpCommitEnvelope(input: {
|
|
32
|
+
idempotencyKey: string;
|
|
33
|
+
request: {
|
|
34
|
+
method: 'POST' | 'PATCH' | 'DELETE';
|
|
35
|
+
path: string;
|
|
36
|
+
body: unknown;
|
|
37
|
+
};
|
|
38
|
+
scopeNamespace: string;
|
|
39
|
+
createdAt?: number;
|
|
40
|
+
sealedAt?: number;
|
|
41
|
+
sequence?: number;
|
|
42
|
+
}): DurableHttpCommitEnvelope;
|
|
43
|
+
export declare function isHttpCommitReplayExpired(envelope: Pick<DurableHttpCommitEnvelope, 'sealedAt'>, now?: number): boolean;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/** Crash-durable exact HTTP request used by the stateless agent client. */
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { v5 as uuidv5 } from 'uuid';
|
|
4
|
+
import { idempotencyKeySchema } from '../commit/contract.js';
|
|
5
|
+
import { stableStringify } from '../utils/json.js';
|
|
6
|
+
export const HTTP_COMMIT_ENVELOPE_VERSION = 1;
|
|
7
|
+
export const HTTP_COMMIT_ENVELOPE_PREFIX = 'http-commit-envelope:';
|
|
8
|
+
/** Stay one hour inside the server's 24-hour idempotency retention window. */
|
|
9
|
+
export const HTTP_COMMIT_REPLAY_WINDOW_MS = 23 * 60 * 60 * 1000;
|
|
10
|
+
const HTTP_COMMIT_SCOPE_ID_NAMESPACE = '043e8f73-86fc-5f62-af46-935d68fca729';
|
|
11
|
+
const commitPathSchema = z.literal('/v1/commits');
|
|
12
|
+
const modelCollectionPathSchema = z.string().regex(/^\/v1\/models\/[^/]+$/);
|
|
13
|
+
const modelEntityPathSchema = z.string().regex(/^\/v1\/models\/[^/]+\/[^/]+$/);
|
|
14
|
+
function hasSafeModelPathSegments(path) {
|
|
15
|
+
if (!path.startsWith('/v1/models/'))
|
|
16
|
+
return true;
|
|
17
|
+
try {
|
|
18
|
+
return path
|
|
19
|
+
.slice('/v1/models/'.length)
|
|
20
|
+
.split('/')
|
|
21
|
+
.every((segment) => {
|
|
22
|
+
const decoded = decodeURIComponent(segment);
|
|
23
|
+
return (decoded.length > 0 &&
|
|
24
|
+
decoded !== '.' &&
|
|
25
|
+
decoded !== '..' &&
|
|
26
|
+
!decoded.includes('/') &&
|
|
27
|
+
!decoded.includes('\\'));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** Apply normal JSON semantics once, then make object-key order canonical. */
|
|
35
|
+
export function canonicalHttpCommitBody(value) {
|
|
36
|
+
const serialized = JSON.stringify(value);
|
|
37
|
+
if (serialized === undefined) {
|
|
38
|
+
throw new TypeError('HTTP commit body is not JSON serializable');
|
|
39
|
+
}
|
|
40
|
+
return stableStringify(JSON.parse(serialized));
|
|
41
|
+
}
|
|
42
|
+
export const durableHttpCommitEnvelopeSchema = z
|
|
43
|
+
.strictObject({
|
|
44
|
+
id: z.string().startsWith(HTTP_COMMIT_ENVELOPE_PREFIX),
|
|
45
|
+
type: z.literal('http_commit_envelope'),
|
|
46
|
+
storageVersion: z.literal(HTTP_COMMIT_ENVELOPE_VERSION),
|
|
47
|
+
idempotencyKey: idempotencyKeySchema,
|
|
48
|
+
request: z.strictObject({
|
|
49
|
+
method: z.enum(['POST', 'PATCH', 'DELETE']),
|
|
50
|
+
path: z.string().startsWith('/'),
|
|
51
|
+
body: z.string().refine((body) => {
|
|
52
|
+
try {
|
|
53
|
+
JSON.parse(body);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}, 'HTTP commit body must be valid JSON'),
|
|
60
|
+
}),
|
|
61
|
+
scopeNamespace: z.string().min(1),
|
|
62
|
+
createdAt: z.number().int().nonnegative(),
|
|
63
|
+
sealedAt: z.number().int().nonnegative(),
|
|
64
|
+
/** Monotonic within one client; disambiguates writes sealed in the same ms. */
|
|
65
|
+
sequence: z.number().int().nonnegative().optional(),
|
|
66
|
+
timestamp: z.number().int().nonnegative(),
|
|
67
|
+
})
|
|
68
|
+
.superRefine((envelope, context) => {
|
|
69
|
+
const legacyId = httpCommitEnvelopeRecordId(envelope.idempotencyKey);
|
|
70
|
+
const scopedId = httpCommitEnvelopeRecordId(envelope.idempotencyKey, envelope.scopeNamespace);
|
|
71
|
+
if (envelope.id !== legacyId && envelope.id !== scopedId) {
|
|
72
|
+
context.addIssue({
|
|
73
|
+
code: 'custom',
|
|
74
|
+
path: ['id'],
|
|
75
|
+
message: 'HTTP envelope id must be derived from its idempotency key',
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
if (envelope.sealedAt < envelope.createdAt) {
|
|
79
|
+
context.addIssue({
|
|
80
|
+
code: 'custom',
|
|
81
|
+
path: ['sealedAt'],
|
|
82
|
+
message: 'HTTP envelope cannot be sealed before it is created',
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const { method, path } = envelope.request;
|
|
86
|
+
const allowedPath = method === 'POST'
|
|
87
|
+
? commitPathSchema.safeParse(path).success ||
|
|
88
|
+
modelCollectionPathSchema.safeParse(path).success
|
|
89
|
+
: modelEntityPathSchema.safeParse(path).success;
|
|
90
|
+
if (!allowedPath || !hasSafeModelPathSegments(path)) {
|
|
91
|
+
context.addIssue({
|
|
92
|
+
code: 'custom',
|
|
93
|
+
path: ['request', 'path'],
|
|
94
|
+
message: 'HTTP outbox records may target only commit or model-mutation routes',
|
|
95
|
+
});
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
let body;
|
|
99
|
+
try {
|
|
100
|
+
body = JSON.parse(envelope.request.body);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return; // The field-level JSON refinement reports this.
|
|
104
|
+
}
|
|
105
|
+
if (typeof body !== 'object' || body === null || Array.isArray(body)) {
|
|
106
|
+
context.addIssue({
|
|
107
|
+
code: 'custom',
|
|
108
|
+
path: ['request', 'body'],
|
|
109
|
+
message: 'HTTP commit body must be a JSON object',
|
|
110
|
+
});
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const record = body;
|
|
114
|
+
if (record.idempotencyKey !== envelope.idempotencyKey) {
|
|
115
|
+
context.addIssue({
|
|
116
|
+
code: 'custom',
|
|
117
|
+
path: ['request', 'body', 'idempotencyKey'],
|
|
118
|
+
message: 'HTTP body idempotency key must match its envelope',
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (path === '/v1/commits') {
|
|
122
|
+
if (record.clientTxId !== envelope.idempotencyKey ||
|
|
123
|
+
!Array.isArray(record.operations) ||
|
|
124
|
+
record.operations.length === 0) {
|
|
125
|
+
context.addIssue({
|
|
126
|
+
code: 'custom',
|
|
127
|
+
path: ['request', 'body'],
|
|
128
|
+
message: 'Commit-route body must carry the same clientTxId and operations',
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
else if (method === 'POST' && typeof record.id !== 'string') {
|
|
133
|
+
context.addIssue({
|
|
134
|
+
code: 'custom',
|
|
135
|
+
path: ['request', 'body', 'id'],
|
|
136
|
+
message: 'Model-create body must carry its entity id',
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
else if (method === 'PATCH' &&
|
|
140
|
+
(typeof record.data !== 'object' || record.data === null || Array.isArray(record.data))) {
|
|
141
|
+
context.addIssue({
|
|
142
|
+
code: 'custom',
|
|
143
|
+
path: ['request', 'body', 'data'],
|
|
144
|
+
message: 'Model-update body must carry a JSON object patch',
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
export function httpCommitEnvelopeRecordId(idempotencyKey, scopeNamespace) {
|
|
149
|
+
if (!scopeNamespace) {
|
|
150
|
+
return `${HTTP_COMMIT_ENVELOPE_PREFIX}${idempotencyKey}`;
|
|
151
|
+
}
|
|
152
|
+
const scopeId = uuidv5(scopeNamespace, HTTP_COMMIT_SCOPE_ID_NAMESPACE);
|
|
153
|
+
return `${HTTP_COMMIT_ENVELOPE_PREFIX}${scopeId}:${idempotencyKey}`;
|
|
154
|
+
}
|
|
155
|
+
export function createDurableHttpCommitEnvelope(input) {
|
|
156
|
+
const now = Date.now();
|
|
157
|
+
const createdAt = input.createdAt ?? now;
|
|
158
|
+
const sealedAt = input.sealedAt ?? now;
|
|
159
|
+
const body = canonicalHttpCommitBody(input.request.body);
|
|
160
|
+
return durableHttpCommitEnvelopeSchema.parse({
|
|
161
|
+
id: httpCommitEnvelopeRecordId(input.idempotencyKey, input.scopeNamespace),
|
|
162
|
+
type: 'http_commit_envelope',
|
|
163
|
+
storageVersion: HTTP_COMMIT_ENVELOPE_VERSION,
|
|
164
|
+
idempotencyKey: input.idempotencyKey,
|
|
165
|
+
request: {
|
|
166
|
+
method: input.request.method,
|
|
167
|
+
path: input.request.path,
|
|
168
|
+
body,
|
|
169
|
+
},
|
|
170
|
+
scopeNamespace: input.scopeNamespace,
|
|
171
|
+
createdAt,
|
|
172
|
+
sealedAt,
|
|
173
|
+
...(input.sequence !== undefined ? { sequence: input.sequence } : {}),
|
|
174
|
+
timestamp: sealedAt,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
export function isHttpCommitReplayExpired(envelope, now = Date.now()) {
|
|
178
|
+
return now - envelope.sealedAt >= HTTP_COMMIT_REPLAY_WINDOW_MS;
|
|
179
|
+
}
|