or3-provider-sqlite 0.0.23 → 0.0.24
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
|
@@ -400,3 +400,4 @@ cancels preparation and restores admission rather than treating failure as idle.
|
|
|
400
400
|
An additive covering index migration keeps counts off stored response bodies.
|
|
401
401
|
|
|
402
402
|
Admin workspace summaries, workspace details, and deployment-admin lists expose `createdAt` in Unix milliseconds for the host UI. SQLite continues to store the underlying creation timestamps in Unix seconds; no data migration is needed.
|
|
403
|
+
Background history admission and finalization failures expose structured codes for validation, authorization, conflicts, and supersession. Transient SQLite failures retain their original errors for retry.
|
package/dist/module.json
CHANGED
|
@@ -63,6 +63,13 @@ const QUOTA_RESERVATIONS_SQL = `SELECT intent.hash, MAX(intent.size_bytes) AS re
|
|
|
63
63
|
function uid() {
|
|
64
64
|
return globalThis.crypto.randomUUID();
|
|
65
65
|
}
|
|
66
|
+
function parseRequiredHistoryAdmission(input) {
|
|
67
|
+
try {
|
|
68
|
+
return parseChatGenerationAdmissionEnvelope(input);
|
|
69
|
+
} catch (cause) {
|
|
70
|
+
throw Object.assign(new Error("Invalid background history admission", { cause }), { code: "VALIDATION_ERROR" });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
66
73
|
function nowEpoch() {
|
|
67
74
|
return Math.floor(Date.now() / 1e3);
|
|
68
75
|
}
|
|
@@ -659,20 +666,20 @@ export class SqliteSyncGatewayAdapter {
|
|
|
659
666
|
}
|
|
660
667
|
assertCanonicalActor(actor) {
|
|
661
668
|
if (isD1Driver()) {
|
|
662
|
-
throw new Error("SQLite D1 does not support background generation history v1");
|
|
669
|
+
throw Object.assign(new Error("SQLite D1 does not support background generation history v1"), { code: "UNSUPPORTED" });
|
|
663
670
|
}
|
|
664
671
|
const membership = getRawDb().prepare(
|
|
665
672
|
`SELECT role FROM workspace_members
|
|
666
673
|
WHERE workspace_id = ? AND user_id = ?`
|
|
667
674
|
).get(actor.workspaceId, actor.userId);
|
|
668
675
|
if (!membership || membership.role !== "owner" && membership.role !== "editor") {
|
|
669
|
-
throw new Error("Forbidden background history actor");
|
|
676
|
+
throw Object.assign(new Error("Forbidden background history actor"), { code: "FORBIDDEN" });
|
|
670
677
|
}
|
|
671
678
|
}
|
|
672
679
|
async admitChatGeneration(actor, input) {
|
|
673
|
-
const admission =
|
|
680
|
+
const admission = parseRequiredHistoryAdmission(input);
|
|
674
681
|
if (actor.workspaceId !== admission.workspaceId) {
|
|
675
|
-
throw new Error("Invalid background history workspace");
|
|
682
|
+
throw Object.assign(new Error("Invalid background history workspace"), { code: "VALIDATION_ERROR" });
|
|
676
683
|
}
|
|
677
684
|
const raw = getRawDb();
|
|
678
685
|
const fingerprint = createHash("sha256").update(stableJson(admission)).digest("hex");
|
|
@@ -686,7 +693,7 @@ export class SqliteSyncGatewayAdapter {
|
|
|
686
693
|
).get(admission.workspaceId, admission.generationId);
|
|
687
694
|
if (receipt) {
|
|
688
695
|
if (receipt.fingerprint !== fingerprint) {
|
|
689
|
-
throw new Error("Conflicting background admission replay");
|
|
696
|
+
throw Object.assign(new Error("Conflicting background admission replay"), { code: "CONFLICT" });
|
|
690
697
|
}
|
|
691
698
|
return {
|
|
692
699
|
status: "admitted",
|
|
@@ -703,7 +710,7 @@ export class SqliteSyncGatewayAdapter {
|
|
|
703
710
|
const expectedClock = admission.expectedAssistant?.clock ?? -1;
|
|
704
711
|
const admissionClock = admission.assistantMessage.clock;
|
|
705
712
|
if (row && row.deleted === 1 || row && row.clock > admissionClock || row && row.clock === admissionClock && generationId !== admission.generationId || row && row.clock > expectedClock && row.clock < admissionClock || row && row.clock === expectedClock && admission.expectedAssistant?.generationId !== void 0 && generationId !== admission.expectedAssistant.generationId) {
|
|
706
|
-
throw new Error("Conflicting continuation admission");
|
|
713
|
+
throw Object.assign(new Error("Conflicting continuation admission"), { code: "CONFLICT" });
|
|
707
714
|
}
|
|
708
715
|
}
|
|
709
716
|
const records = [
|
|
@@ -737,7 +744,7 @@ export class SqliteSyncGatewayAdapter {
|
|
|
737
744
|
{ clock: entry.record.clock, hlc, opId },
|
|
738
745
|
tombstone
|
|
739
746
|
)) {
|
|
740
|
-
throw new Error("Background history record was superseded");
|
|
747
|
+
throw Object.assign(new Error("Background history record was superseded"), { code: "SUPERSEDED" });
|
|
741
748
|
}
|
|
742
749
|
const sanitized = sanitizePayloadForSync(
|
|
743
750
|
entry.table,
|
|
@@ -745,12 +752,12 @@ export class SqliteSyncGatewayAdapter {
|
|
|
745
752
|
"put"
|
|
746
753
|
);
|
|
747
754
|
if (!sanitized) {
|
|
748
|
-
throw new Error("Invalid background history payload");
|
|
755
|
+
throw Object.assign(new Error("Invalid background history payload"), { code: "VALIDATION_ERROR" });
|
|
749
756
|
}
|
|
750
757
|
const payload = { ...sanitized, hlc, op_id: opId };
|
|
751
758
|
const payloadJson = JSON.stringify(payload);
|
|
752
759
|
if (new TextEncoder().encode(payloadJson).byteLength > MAX_SYNC_PAYLOAD_BYTES) {
|
|
753
|
-
throw new Error("Invalid background history payload size");
|
|
760
|
+
throw Object.assign(new Error("Invalid background history payload size"), { code: "VALIDATION_ERROR" });
|
|
754
761
|
}
|
|
755
762
|
const version = baseVersion + index + 1;
|
|
756
763
|
raw.prepare(
|
|
@@ -806,7 +813,7 @@ export class SqliteSyncGatewayAdapter {
|
|
|
806
813
|
const admittedPayload = admittedAssistant ? JSON.parse(admittedAssistant.data_json) : null;
|
|
807
814
|
const admittedData = admittedPayload?.data && typeof admittedPayload.data === "object" ? admittedPayload.data : {};
|
|
808
815
|
if (!admittedAssistant || admittedAssistant.deleted === 1 || admittedAssistant.clock !== admission.assistantMessage.clock || admittedData.generation_id !== admission.generationId) {
|
|
809
|
-
throw new Error("Background assistant admission was superseded");
|
|
816
|
+
throw Object.assign(new Error("Background assistant admission was superseded"), { code: "SUPERSEDED" });
|
|
810
817
|
}
|
|
811
818
|
raw.prepare(
|
|
812
819
|
`INSERT INTO background_generation_receipts
|
|
@@ -826,9 +833,9 @@ export class SqliteSyncGatewayAdapter {
|
|
|
826
833
|
return result;
|
|
827
834
|
}
|
|
828
835
|
async finalizeChatGeneration(actor, input) {
|
|
829
|
-
const admission =
|
|
836
|
+
const admission = parseRequiredHistoryAdmission(input.admission);
|
|
830
837
|
if (actor.workspaceId !== admission.workspaceId) {
|
|
831
|
-
throw new Error("Invalid background history workspace");
|
|
838
|
+
throw Object.assign(new Error("Invalid background history workspace"), { code: "VALIDATION_ERROR" });
|
|
832
839
|
}
|
|
833
840
|
const raw = getRawDb();
|
|
834
841
|
const fingerprint = createHash("sha256").update(stableJson(input)).digest("hex");
|
|
@@ -841,7 +848,7 @@ export class SqliteSyncGatewayAdapter {
|
|
|
841
848
|
).get(admission.workspaceId, admission.generationId);
|
|
842
849
|
if (receipt) {
|
|
843
850
|
if (receipt.fingerprint !== fingerprint) {
|
|
844
|
-
throw new Error("Conflicting background finalization replay");
|
|
851
|
+
throw Object.assign(new Error("Conflicting background finalization replay"), { code: "CONFLICT" });
|
|
845
852
|
}
|
|
846
853
|
return receipt.outcome === "committed" ? {
|
|
847
854
|
status: "committed",
|
|
@@ -900,7 +907,7 @@ export class SqliteSyncGatewayAdapter {
|
|
|
900
907
|
};
|
|
901
908
|
const payloadJson = JSON.stringify(payload);
|
|
902
909
|
if (new TextEncoder().encode(payloadJson).byteLength > MAX_SYNC_PAYLOAD_BYTES) {
|
|
903
|
-
throw new Error("Invalid background history payload size");
|
|
910
|
+
throw Object.assign(new Error("Invalid background history payload size"), { code: "VALIDATION_ERROR" });
|
|
904
911
|
}
|
|
905
912
|
const counter = raw.prepare("SELECT value FROM server_version_counter WHERE workspace_id = ?").get(admission.workspaceId);
|
|
906
913
|
const serverVersion = (counter?.value ?? 0) + 1;
|