@context-use/open-sync 0.2.0 → 0.3.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.
Files changed (59) hide show
  1. package/LICENSE +3 -0
  2. package/package.json +7 -4
  3. package/src/api.ts +18 -14
  4. package/src/build.ts +3 -21
  5. package/src/connector/client.ts +61 -28
  6. package/src/connector/failure.ts +49 -0
  7. package/src/connector/file.ts +104 -0
  8. package/src/connector/response.ts +22 -1
  9. package/src/db/client.ts +1 -11
  10. package/src/db/providers.sql +2 -3
  11. package/src/db/providers.ts +1 -11
  12. package/src/db/schema.sql +41 -36
  13. package/src/execution/abortable.ts +12 -0
  14. package/src/execution/diagnostics.ts +1 -2
  15. package/src/execution/provider.ts +1 -0
  16. package/src/execution/worker.ts +162 -39
  17. package/src/http/controller.ts +63 -42
  18. package/src/http/providers.ts +13 -0
  19. package/src/models/asset.ts +61 -0
  20. package/src/models/definition.ts +24 -15
  21. package/src/models/delivery.ts +25 -31
  22. package/src/models/error.ts +12 -1
  23. package/src/models/limits.ts +8 -3
  24. package/src/models/metadata.ts +42 -0
  25. package/src/models/page.ts +68 -13
  26. package/src/models/provider-catalog.ts +57 -0
  27. package/src/models/provider-path.ts +25 -0
  28. package/src/models/providers.ts +13 -2
  29. package/src/models/record.ts +22 -0
  30. package/src/models/registry.ts +15 -15
  31. package/src/models/source-http-error.ts +37 -0
  32. package/src/models/sync.ts +71 -0
  33. package/src/models/validation.ts +1 -1
  34. package/src/open-sync.ts +31 -7
  35. package/src/repositories/acquisition/contract.ts +18 -10
  36. package/src/repositories/acquisition/lease.ts +80 -94
  37. package/src/repositories/acquisition/outbox.ts +22 -22
  38. package/src/repositories/acquisition/records.ts +44 -13
  39. package/src/repositories/acquisition/sqlite.ts +78 -31
  40. package/src/repositories/assets/capacity.ts +16 -0
  41. package/src/repositories/assets/contract.ts +25 -0
  42. package/src/repositories/assets/filesystem.ts +103 -0
  43. package/src/repositories/assets/sqlite.ts +174 -0
  44. package/src/repositories/catalog/contract.ts +16 -18
  45. package/src/repositories/catalog/sqlite.ts +107 -115
  46. package/src/repositories/delivery/contract.ts +7 -11
  47. package/src/repositories/delivery/lease.ts +16 -0
  48. package/src/repositories/delivery/sqlite.ts +57 -34
  49. package/src/repositories/providers/contract.ts +2 -1
  50. package/src/repositories/providers/sqlite.ts +22 -6
  51. package/src/repositories/queue-usage.ts +7 -8
  52. package/src/repositories/rows.ts +10 -29
  53. package/src/runtime.ts +27 -6
  54. package/src/services/acquisition.ts +178 -52
  55. package/src/services/delivery.ts +39 -19
  56. package/src/services/management.ts +108 -44
  57. package/src/services/providers/service.ts +187 -31
  58. package/src/build/connector.ts +0 -143
  59. package/src/models/installation.ts +0 -37
@@ -0,0 +1,174 @@
1
+ import type { Database } from 'bun:sqlite';
2
+ import type { AssetMetadata, AssetRef, DeliveryAsset } from '../../models/asset';
3
+ import { assetKey } from '../../models/asset';
4
+ import { fail } from '../../models/error';
5
+ import { canonicalJson } from '../../models/json';
6
+ import { normalizeSourceTimestamps } from '../../models/metadata';
7
+ import { identifier } from '../../models/validation';
8
+ import type { AcquisitionLease } from '../acquisition/contract';
9
+ import { assertAcquisition } from '../acquisition/lease';
10
+ import { assertDelivery } from '../delivery/lease';
11
+ import { assetBytes } from './capacity';
12
+ import type { AssetRepository } from './contract';
13
+
14
+ const retained = `EXISTS (SELECT 1 FROM deliveries d WHERE d.owner_id=a.owner_id AND d.id=a.delivery_id)
15
+ OR EXISTS (SELECT 1 FROM syncs s WHERE s.owner_id=a.owner_id AND s.id=a.sync_id
16
+ AND s.generation=a.generation AND s.enabled=1 AND s.expires_at>?)`;
17
+
18
+ export class SqliteAssets implements AssetRepository {
19
+ constructor(private readonly input: { db: Database; maxBytes: number }) {}
20
+ stage(input: Parameters<AssetRepository['stage']>[0]): string {
21
+ const { db } = this.input;
22
+ validateMetadata(input.asset);
23
+ if (input.unavailable !== undefined) {
24
+ identifier(input.unavailable);
25
+ }
26
+ return db
27
+ .transaction(() => {
28
+ assertAcquisition({ db, lease: input.lease });
29
+ const id = crypto.randomUUID();
30
+ const descriptor = canonicalJson({
31
+ ...input.asset,
32
+ ...normalizeSourceTimestamps(input.asset),
33
+ ...(input.unavailable === undefined ? {} : { unavailable: input.unavailable }),
34
+ }).json;
35
+ db.query(`INSERT INTO delivery_assets(id,owner_id,sync_id,generation,asset_id,asset_version,descriptor,ready)
36
+ VALUES (?,?,?,?,?,?,?,?)`).run(
37
+ id,
38
+ input.lease.ownerId,
39
+ input.lease.sync.id,
40
+ input.lease.generation,
41
+ input.asset.id,
42
+ input.asset.version,
43
+ descriptor,
44
+ Number(input.unavailable !== undefined),
45
+ );
46
+ return id;
47
+ })
48
+ .immediate();
49
+ }
50
+ captured(input: Parameters<AssetRepository['captured']>[0]): void {
51
+ const { db } = this.input;
52
+ db.transaction(() => {
53
+ assertAcquisition({ db, lease: input.lease });
54
+ const row = db
55
+ .query<{ descriptor: string }, (string | number)[]>(
56
+ 'SELECT descriptor FROM delivery_assets WHERE id=? AND owner_id=? AND sync_id=? AND generation=? AND bytes=? AND ready=0',
57
+ )
58
+ .get(
59
+ input.id,
60
+ input.lease.ownerId,
61
+ input.lease.sync.id,
62
+ input.lease.generation,
63
+ input.size,
64
+ );
65
+ if (!row) {
66
+ fail('asset_reservation_missing');
67
+ }
68
+ db.query('UPDATE delivery_assets SET descriptor=?,ready=1 WHERE id=?').run(
69
+ canonicalJson({ ...JSON.parse(row!.descriptor), size: input.size, sha256: input.sha256 })
70
+ .json,
71
+ input.id,
72
+ );
73
+ }).immediate();
74
+ }
75
+ reserve(input: Parameters<AssetRepository['reserve']>[0]): void {
76
+ const { db, maxBytes } = this.input;
77
+ db.transaction(() => {
78
+ assertAcquisition({ db, lease: input.lease });
79
+ const row = db
80
+ .query<{ bytes: number }, (string | number)[]>(
81
+ 'SELECT bytes FROM delivery_assets WHERE id=? AND owner_id=? AND sync_id=? AND generation=? AND ready=0',
82
+ )
83
+ .get(input.id, input.lease.ownerId, input.lease.sync.id, input.lease.generation);
84
+ if (!row || input.bytes < row.bytes) {
85
+ fail('asset_reservation_conflict');
86
+ }
87
+ const delta = input.bytes - row!.bytes;
88
+ if (delta + assetBytes({ db, lease: input.lease }) > maxBytes) {
89
+ fail('step_exceeds_asset_capacity');
90
+ }
91
+ if (delta + assetBytes({ db }) > maxBytes) {
92
+ fail('waiting_for_capacity');
93
+ }
94
+ db.query('UPDATE delivery_assets SET bytes=? WHERE id=?').run(input.bytes, input.id);
95
+ }).immediate();
96
+ }
97
+ discarded(id: string): void {
98
+ const deleted = this.input.db
99
+ .query<{ owner_id: string; sync_id: string }, [string, number]>(
100
+ `DELETE FROM delivery_assets AS a WHERE id=? AND NOT (${retained}) RETURNING owner_id,sync_id`,
101
+ )
102
+ .get(id, Date.now());
103
+ if (deleted) {
104
+ this.input.db
105
+ .query(
106
+ "UPDATE syncs SET next_due_at=? WHERE enabled=1 AND status='waiting_for_capacity' AND NOT (owner_id=? AND id=?)",
107
+ )
108
+ .run(Date.now(), deleted.owner_id, deleted.sync_id);
109
+ }
110
+ }
111
+ garbage(): string[] {
112
+ return this.input.db
113
+ .query<{ id: string }, [number]>(`SELECT a.id FROM delivery_assets a WHERE NOT (${retained})`)
114
+ .all(Date.now())
115
+ .map(({ id }) => id);
116
+ }
117
+ retained(id: string): boolean {
118
+ // Any ledger row is retained by the filesystem sweep. Garbage cleanup owns its deletion.
119
+ return !!this.input.db.query('SELECT 1 FROM delivery_assets WHERE id=?').get(id);
120
+ }
121
+ read(input: Parameters<AssetRepository['read']>[0]): string {
122
+ assertDelivery({ db: this.input.db, lease: input.lease });
123
+ const row = this.input.db
124
+ .query<{ id: string; descriptor: string }, string[]>(
125
+ 'SELECT id,descriptor FROM delivery_assets WHERE owner_id=? AND delivery_id=? AND asset_id=? AND asset_version=? AND ready=1',
126
+ )
127
+ .get(input.lease.ownerId, input.lease.delivery.id, input.asset.id, input.asset.version);
128
+ if (!row) {
129
+ return fail('not_found');
130
+ }
131
+ if ('unavailable' in JSON.parse(row.descriptor)) {
132
+ fail('asset_content_missing');
133
+ }
134
+ return row.id;
135
+ }
136
+ }
137
+
138
+ /** Called inside the checkpoint transaction. A previous acquisition generation cannot supply bytes. */
139
+ export function capturedAssets(input: {
140
+ db: Database;
141
+ lease: AcquisitionLease;
142
+ refs: readonly AssetRef[];
143
+ }): DeliveryAsset[] {
144
+ const refs = new Map(input.refs.map((ref) => [assetKey(ref), ref]));
145
+ return [...refs.keys()].sort().map((key) => {
146
+ const ref = refs.get(key)!;
147
+ const row = input.db
148
+ .query<{ descriptor: string }, (string | number)[]>(
149
+ 'SELECT descriptor FROM delivery_assets WHERE owner_id=? AND sync_id=? AND generation=? AND asset_id=? AND asset_version=? AND ready=1',
150
+ )
151
+ .get(input.lease.ownerId, input.lease.sync.id, input.lease.generation, ref.id, ref.version);
152
+ if (!row) {
153
+ return fail('asset_not_captured');
154
+ }
155
+ return JSON.parse(row.descriptor) as DeliveryAsset;
156
+ });
157
+ }
158
+ function validateMetadata(asset: AssetMetadata) {
159
+ identifier(asset.id);
160
+ identifier(asset.version);
161
+ const maxName = 1024;
162
+ const maxType = 256;
163
+ if (
164
+ typeof asset.name !== 'string' ||
165
+ asset.name.length > maxName ||
166
+ !asset.name ||
167
+ typeof asset.mediaType !== 'string' ||
168
+ !asset.mediaType ||
169
+ asset.mediaType.length > maxType ||
170
+ /[\r\n]/.test(asset.mediaType)
171
+ ) {
172
+ fail('invalid_asset_metadata');
173
+ }
174
+ }
@@ -1,23 +1,21 @@
1
- import type { SyncDefinition } from '../../models/definition';
2
- import type { Destination } from '../../models/delivery';
1
+ import type { ConnectionRef } from '../../models/definition';
3
2
  import type { Resource, Scope } from '../../models/identity';
4
- import type { CreateInstallation, Installation, SyncRun } from '../../models/installation';
5
3
  import type { JsonObject, JsonValue } from '../../models/json';
4
+ import type { CreateSync, Sync, SyncPoll } from '../../models/sync';
6
5
 
7
6
  export interface CatalogRepository {
8
- register(definition: SyncDefinition): void;
9
- createDestination(
10
- input: Scope & { type: string; version: string; config: JsonObject },
11
- ): Destination;
12
- destinations(scope: Scope): Destination[];
13
- createInstallation(input: CreateInstallation & { initialCheckpoint: JsonValue }): Installation;
14
- installation(input: Resource): Installation;
15
- installations(scope: Scope): Installation[];
16
- runs(input: Resource & { offset: number }): {
17
- runs: SyncRun[];
18
- hasMore: boolean;
19
- pageSize: number;
20
- };
21
- setEnabled(input: Resource & { enabled: boolean }): Installation;
22
- queue(input: Resource & { checkpoint?: JsonValue }): void;
7
+ createSync(
8
+ input: Omit<CreateSync, 'destination'> & {
9
+ destination: { type: string; config: JsonObject };
10
+ initialCheckpoint: JsonValue;
11
+ },
12
+ ): Sync;
13
+ sync(input: Resource): Sync;
14
+ syncs(scope: Scope): Sync[];
15
+ polls(input: Resource & { before?: number }): { polls: SyncPoll[]; nextCursor: number | null };
16
+ connectSync(input: Resource & { connection: ConnectionRef }): Sync;
17
+ setEnabled(input: Resource & { enabled: boolean }): Sync;
18
+ runNow(input: Resource): void;
19
+ resync(input: Resource & { checkpoint: JsonValue }): void;
20
+ removeSync(input: Resource): void;
23
21
  }
@@ -1,125 +1,94 @@
1
1
  import type { Database } from 'bun:sqlite';
2
- import type { SyncDefinition } from '../../models/definition';
2
+ import type { ConnectionRef } from '../../models/definition';
3
3
  import { fail } from '../../models/error';
4
4
  import type { Resource, Scope } from '../../models/identity';
5
- import type { CreateInstallation } from '../../models/installation';
6
5
  import { canonicalJson, type JsonObject, type JsonValue } from '../../models/json';
7
- import { defaultTiming } from '../../models/limits';
8
- import { readDestination, readInstallation } from '../rows';
6
+ import type { CreateSync, SyncPoll } from '../../models/sync';
7
+ import { readSync } from '../rows';
9
8
  import type { CatalogRepository } from './contract';
10
9
 
10
+ const defaultIntervalMs = 60_000;
11
+
11
12
  export class SqliteCatalog implements CatalogRepository {
12
13
  constructor(private readonly db: Database) {}
13
- register(definition: SyncDefinition): void {
14
- const manifest = canonicalJson(definition).json;
15
- this.db
16
- .transaction(() => {
17
- const previous = this.db
18
- .query<{ manifest: string }, [string, string]>(
19
- 'SELECT manifest FROM definitions WHERE id=? AND version=?',
20
- )
21
- .get(definition.id, definition.version);
22
- if (previous && previous.manifest !== manifest) {
23
- fail('definition_conflict');
24
- }
25
- this.db
26
- .query('INSERT OR IGNORE INTO definitions VALUES (?,?,?,?)')
27
- .run(definition.id, definition.version, definition.artifactId, manifest);
28
- })
29
- .immediate();
30
- }
31
- createDestination(input: Scope & { type: string; version: string; config: JsonObject }) {
32
- const id = `dest_${crypto.randomUUID()}`;
33
- this.db
34
- .query('INSERT INTO destinations VALUES (?,?,?,?,?)')
35
- .run(input.ownerId, id, input.type, input.version, canonicalJson(input.config).json);
36
- return readDestination({ db: this.db, scope: { ...input, id } });
37
- }
38
- destinations(scope: Scope) {
39
- return this.db
40
- .query<{ id: string }, [string]>(
41
- 'SELECT id FROM destinations WHERE owner_id=? ORDER BY rowid',
42
- )
43
- .all(scope.ownerId)
44
- .map(({ id }) => readDestination({ db: this.db, scope: { ...scope, id } }));
45
- }
46
- createInstallation(input: CreateInstallation & { initialCheckpoint: JsonValue }) {
14
+ createSync(
15
+ input: Omit<CreateSync, 'destination'> & {
16
+ destination: { type: string; config: JsonObject };
17
+ initialCheckpoint: JsonValue;
18
+ },
19
+ ) {
47
20
  const id = `sync_${crypto.randomUUID()}`;
48
21
  this.db
49
- .transaction(() => {
50
- readDestination({ db: this.db, scope: { ...input, id: input.destinationId } });
51
- this.db
52
- .query(`INSERT INTO installations (owner_id,id,source_id,definition_id,definition_version,artifact_id,connection,config,destination_id,enabled,checkpoint,interval_ms,next_due_at,status)
53
- VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)`)
54
- .run(
55
- input.ownerId,
56
- id,
57
- `source_${crypto.randomUUID()}`,
58
- input.definition.id,
59
- input.definition.version,
60
- input.definition.artifactId,
61
- input.connection ? canonicalJson(input.connection).json : null,
62
- canonicalJson(input.config).json,
63
- input.destinationId,
64
- Number(input.enabled ?? true),
65
- canonicalJson(input.initialCheckpoint).json,
66
- input.intervalMs ?? defaultTiming.leaseMs,
67
- Date.now(),
68
- input.enabled === false ? 'disabled' : 'ready',
69
- );
70
- })
71
- .immediate();
72
- return this.installation({ ...input, id });
22
+ .query(`INSERT INTO syncs (owner_id,id,definition_id,connection,config,destination_type,destination_config,enabled,checkpoint,interval_ms,next_due_at,status)
23
+ VALUES (?,?,?,?,?,?,?,?,?,?,?,?)`)
24
+ .run(
25
+ input.ownerId,
26
+ id,
27
+ input.definition,
28
+ input.connection ? canonicalJson(input.connection).json : null,
29
+ canonicalJson(input.config).json,
30
+ input.destination.type,
31
+ canonicalJson(input.destination.config).json,
32
+ Number(input.enabled ?? true),
33
+ canonicalJson(input.initialCheckpoint).json,
34
+ input.intervalMs ?? defaultIntervalMs,
35
+ Date.now(),
36
+ input.enabled === false ? 'disabled' : 'ready',
37
+ );
38
+ return this.sync({ ...input, id });
73
39
  }
74
- installation(input: Resource) {
75
- return readInstallation({ db: this.db, scope: input });
40
+ sync(input: Resource) {
41
+ return readSync({ db: this.db, scope: input });
76
42
  }
77
- installations(scope: Scope) {
43
+ syncs(scope: Scope) {
78
44
  return this.db
79
- .query<{ id: string }, [string]>(
80
- 'SELECT id FROM installations WHERE owner_id=? ORDER BY rowid',
81
- )
45
+ .query<{ id: string }, [string]>('SELECT id FROM syncs WHERE owner_id=? ORDER BY rowid')
82
46
  .all(scope.ownerId)
83
- .map(({ id }) => this.installation({ ...scope, id }));
47
+ .map(({ id }) => this.sync({ ...scope, id }));
84
48
  }
85
- runs(input: Resource & { offset: number }) {
86
- this.installation(input);
87
- const limit = 50;
49
+ polls(input: Resource & { before?: number }) {
50
+ this.sync(input);
51
+ const pageSize = 20;
88
52
  const rows = this.db
89
53
  .query<
90
- {
91
- id: string;
92
- state: string;
93
- started_at: number;
94
- completed_at: number | null;
95
- pages: number;
96
- checkpoint_revision: number;
97
- },
54
+ SyncPoll,
98
55
  [string, string, number, number]
99
- >(
100
- 'SELECT id,state,started_at,completed_at,pages,checkpoint_revision FROM runs WHERE owner_id=? AND installation_id=? ORDER BY started_at DESC,rowid DESC LIMIT ? OFFSET ?',
101
- )
102
- .all(input.ownerId, input.id, limit + 1, input.offset);
103
- return {
104
- runs: rows.slice(0, limit).map((row) => ({
105
- id: row.id,
106
- state: row.state,
107
- startedAt: row.started_at,
108
- completedAt: row.completed_at,
109
- pages: row.pages,
110
- checkpointRevision: row.checkpoint_revision,
111
- })),
112
- hasMore: rows.length > limit,
113
- pageSize: limit,
114
- };
56
+ >(`SELECT id,started_at AS startedAt,completed_at AS completedAt,
57
+ state,error_code AS errorCode,records_processed AS recordsProcessed,records_queued AS recordsQueued
58
+ FROM sync_polls WHERE owner_id=? AND sync_id=? AND id<? ORDER BY id DESC LIMIT ?`)
59
+ .all(input.ownerId, input.id, input.before ?? Number.MAX_SAFE_INTEGER, pageSize + 1);
60
+ const polls = rows.slice(0, pageSize);
61
+ return { polls, nextCursor: rows.length > pageSize ? polls.at(-1)!.id : null };
62
+ }
63
+ connectSync(input: Resource & { connection: ConnectionRef }) {
64
+ return this.db
65
+ .transaction(() => {
66
+ const sync = this.sync(input);
67
+ if (sync.connection || sync.enabled) {
68
+ fail('already_connected');
69
+ }
70
+ this.db
71
+ .query(`UPDATE syncs SET connection=?,enabled=1,
72
+ status='ready',error_code=NULL,next_due_at=? WHERE owner_id=? AND id=?`)
73
+ .run(canonicalJson(input.connection).json, Date.now(), input.ownerId, input.id);
74
+ return this.sync(input);
75
+ })
76
+ .immediate();
115
77
  }
116
78
  setEnabled(input: Resource & { enabled: boolean }) {
117
79
  return this.db
118
80
  .transaction(() => {
119
- this.installation(input);
81
+ const sync = this.sync(input);
82
+ if (sync.enabled === input.enabled) {
83
+ return sync;
84
+ }
85
+ this.db
86
+ .query(`UPDATE sync_polls SET state=?,error_code=NULL
87
+ WHERE owner_id=? AND sync_id=? AND completed_at IS NULL`)
88
+ .run(input.enabled ? 'ready' : 'disabled', input.ownerId, input.id);
120
89
  this.db
121
90
  .query(
122
- 'UPDATE installations SET enabled=?,binding_epoch=binding_epoch+1,status=?,next_due_at=? WHERE owner_id=? AND id=?',
91
+ `UPDATE syncs SET enabled=?,status=?,error_code=NULL,next_due_at=?,generation=generation+1,expires_at=NULL WHERE owner_id=? AND id=?`,
123
92
  )
124
93
  .run(
125
94
  Number(input.enabled),
@@ -128,38 +97,61 @@ export class SqliteCatalog implements CatalogRepository {
128
97
  input.ownerId,
129
98
  input.id,
130
99
  );
131
- this.db
132
- .query(
133
- "UPDATE runs SET state='cancelled',completed_at=? WHERE owner_id=? AND installation_id=? AND state='running'",
134
- )
135
- .run(Date.now(), input.ownerId, input.id);
136
- return this.installation(input);
100
+ return this.sync(input);
137
101
  })
138
102
  .immediate();
139
103
  }
140
- queue(input: Resource & { checkpoint?: JsonValue }): void {
104
+ runNow(input: Resource): void {
141
105
  this.db
142
106
  .transaction(() => {
143
- if (!this.installation(input).enabled) {
107
+ if (!this.sync(input).enabled) {
144
108
  fail('disabled');
145
109
  }
146
110
  if (
147
111
  this.db
148
- .query("SELECT 1 FROM runs WHERE owner_id=? AND installation_id=? AND state='running'")
149
- .get(input.ownerId, input.id)
112
+ .query(`SELECT 1 FROM syncs WHERE owner_id=? AND id=? AND expires_at>?`)
113
+ .get(input.ownerId, input.id, Date.now())
150
114
  ) {
151
115
  fail('busy');
152
116
  }
153
- if (input.checkpoint !== undefined) {
154
- this.db
155
- .query(
156
- 'UPDATE installations SET checkpoint=?,checkpoint_revision=checkpoint_revision+1 WHERE owner_id=? AND id=?',
157
- )
158
- .run(canonicalJson(input.checkpoint).json, input.ownerId, input.id);
117
+ this.db
118
+ .query(`UPDATE syncs SET next_due_at=? WHERE owner_id=? AND id=?`)
119
+ .run(Date.now(), input.ownerId, input.id);
120
+ })
121
+ .immediate();
122
+ }
123
+ resync(input: Resource & { checkpoint: JsonValue }): void {
124
+ this.db
125
+ .transaction(() => {
126
+ if (!this.sync(input).enabled) {
127
+ fail('disabled');
159
128
  }
160
129
  this.db
161
- .query("UPDATE installations SET next_due_at=?,status='ready' WHERE owner_id=? AND id=?")
130
+ .query(`UPDATE sync_polls SET state='interrupted',error_code='resync_requested',completed_at=?
131
+ WHERE owner_id=? AND sync_id=? AND completed_at IS NULL`)
162
132
  .run(Date.now(), input.ownerId, input.id);
133
+ this.db
134
+ .query(
135
+ `UPDATE syncs SET checkpoint=?,status='ready',error_code=NULL,next_due_at=?,resync=1,failure_count=0,generation=generation+1,expires_at=NULL WHERE owner_id=? AND id=?`,
136
+ )
137
+ .run(canonicalJson(input.checkpoint).json, Date.now(), input.ownerId, input.id);
138
+ })
139
+ .immediate();
140
+ }
141
+ removeSync(input: Resource): void {
142
+ this.db
143
+ .transaction(() => {
144
+ this.sync(input);
145
+ this.db
146
+ .query('DELETE FROM deliveries WHERE owner_id=? AND sync_id=?')
147
+ .run(input.ownerId, input.id);
148
+ this.db
149
+ .query('DELETE FROM record_state WHERE owner_id=? AND sync_id=?')
150
+ .run(input.ownerId, input.id);
151
+ this.db.query('DELETE FROM syncs WHERE owner_id=? AND id=?').run(input.ownerId, input.id);
152
+ this.db
153
+ .query(`UPDATE syncs SET next_due_at=? WHERE enabled=1 AND status='waiting_for_capacity'`)
154
+ .run(Date.now());
163
155
  })
164
156
  .immediate();
165
157
  }
@@ -1,23 +1,19 @@
1
- import type {
2
- Delivery,
3
- DeliveryPage,
4
- DeliveryResult,
5
- Destination,
6
- QueueStatus,
7
- } from '../../models/delivery';
1
+ import type { Deliverable, DeliveryPage, DeliveryResult, QueueStatus } from '../../models/delivery';
8
2
  import type { Resource, Scope } from '../../models/identity';
3
+ import type { Sync } from '../../models/sync';
9
4
 
10
5
  export interface DeliveryLease extends Scope {
11
- delivery: Delivery;
12
- destination: Destination;
13
- workerId: string;
6
+ delivery: Omit<Deliverable, 'openAsset'>;
7
+ destination: Sync['destination'];
14
8
  generation: number;
15
9
  attempt: number;
16
10
  }
17
11
  export interface DeliveryRepository {
12
+ nextDue(): number | undefined;
18
13
  claim(leaseMs: number): DeliveryLease | undefined;
19
14
  complete(input: { lease: DeliveryLease; result: DeliveryResult; delay: number }): void;
20
15
  status(scope: Scope): QueueStatus;
21
- pending(input: Scope & { offset: number }): DeliveryPage;
16
+ pending(input: Scope & { syncId: string; before?: number }): DeliveryPage;
17
+ deliverable(input: Resource & { syncId: string }): Omit<Deliverable, 'openAsset'>;
22
18
  retry(input: Resource): void;
23
19
  }
@@ -0,0 +1,16 @@
1
+ import type { Database } from 'bun:sqlite';
2
+ import { fail } from '../../models/error';
3
+ import type { DeliveryLease } from './contract';
4
+
5
+ export function assertDelivery(input: { db: Database; lease: DeliveryLease }): void {
6
+ const { db, lease } = input;
7
+ if (
8
+ !db
9
+ .query(
10
+ "SELECT 1 FROM deliveries WHERE owner_id=? AND id=? AND state='leased' AND generation=? AND expires_at>?",
11
+ )
12
+ .get(lease.ownerId, lease.delivery.id, lease.generation, Date.now())
13
+ ) {
14
+ fail('lease_lost');
15
+ }
16
+ }