@hasna/todos 0.11.74 → 0.11.81
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 +8 -0
- package/dist/cli/cloud-router.d.ts +71 -0
- package/dist/cli/cloud-router.d.ts.map +1 -0
- package/dist/cli/commands/project-commands.d.ts.map +1 -1
- package/dist/cli/commands/query-commands.d.ts.map +1 -1
- package/dist/cli/commands/storage-commands.d.ts.map +1 -1
- package/dist/cli/commands/task-commands.d.ts.map +1 -1
- package/dist/cli/helpers.d.ts +22 -0
- package/dist/cli/helpers.d.ts.map +1 -1
- package/dist/cli/index.js +72388 -61727
- package/dist/contracts.js +314 -14
- package/dist/db/database.d.ts.map +1 -1
- package/dist/index.js +1158 -236
- package/dist/lib/routing-doctor.d.ts +144 -0
- package/dist/lib/routing-doctor.d.ts.map +1 -0
- package/dist/lib/shadow-status.d.ts +38 -0
- package/dist/lib/shadow-status.d.ts.map +1 -0
- package/dist/mcp/index.js +10249 -340
- package/dist/mcp/tools/task-adv-tools.d.ts.map +1 -1
- package/dist/mcp/tools/task-crud.d.ts.map +1 -1
- package/dist/mcp/tools/task-project-tools.d.ts.map +1 -1
- package/dist/mcp/tools/task-workflow-tools.d.ts.map +1 -1
- package/dist/registry.js +314 -14
- package/dist/release-provenance.json +3 -3
- package/dist/sdk/index.d.ts +2 -0
- package/dist/sdk/index.d.ts.map +1 -1
- package/dist/sdk/index.js +135 -0
- package/dist/sdk/v1.generated.d.ts +124 -0
- package/dist/sdk/v1.generated.d.ts.map +1 -0
- package/dist/server/cloud.d.ts +50 -0
- package/dist/server/cloud.d.ts.map +1 -0
- package/dist/server/index.js +11944 -2012
- package/dist/server/openapi.d.ts +628 -0
- package/dist/server/openapi.d.ts.map +1 -0
- package/dist/server/serve.d.ts.map +1 -1
- package/dist/server/v1.d.ts +19 -0
- package/dist/server/v1.d.ts.map +1 -0
- package/dist/storage/cloud-client.d.ts +30 -0
- package/dist/storage/cloud-client.d.ts.map +1 -0
- package/dist/storage/config.d.ts +12 -0
- package/dist/storage/config.d.ts.map +1 -1
- package/dist/storage/factory.d.ts +2 -0
- package/dist/storage/factory.d.ts.map +1 -1
- package/dist/storage/index.d.ts +8 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/postgres-adapter.d.ts.map +1 -1
- package/dist/storage/postgres-sync.d.ts.map +1 -1
- package/dist/storage/shadow-outbox-schema.d.ts +21 -0
- package/dist/storage/shadow-outbox-schema.d.ts.map +1 -0
- package/dist/storage/shadow-outbox.d.ts +125 -0
- package/dist/storage/shadow-outbox.d.ts.map +1 -0
- package/dist/storage/shadow-runtime.d.ts +29 -0
- package/dist/storage/shadow-runtime.d.ts.map +1 -0
- package/dist/storage/shadow.d.ts +118 -0
- package/dist/storage/shadow.d.ts.map +1 -0
- package/dist/storage.d.ts +2 -1
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +1157 -220
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +6 -2
- package/vendor/hasna-contracts-0.5.1.tgz +0 -0
package/dist/storage.js
CHANGED
|
@@ -39,6 +39,213 @@ var __export = (target, all) => {
|
|
|
39
39
|
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
40
40
|
var __require = import.meta.require;
|
|
41
41
|
|
|
42
|
+
// src/storage/config.ts
|
|
43
|
+
var exports_config = {};
|
|
44
|
+
__export(exports_config, {
|
|
45
|
+
parseStorageMode: () => parseStorageMode,
|
|
46
|
+
loadTodosStorageConfig: () => loadTodosStorageConfig,
|
|
47
|
+
loadStorageConfig: () => loadStorageConfig,
|
|
48
|
+
isTodosShadowEnabled: () => isTodosShadowEnabled,
|
|
49
|
+
isTodosRemoteStorageEnabled: () => isTodosRemoteStorageEnabled,
|
|
50
|
+
getTodosStorageShadowEnvName: () => getTodosStorageShadowEnvName,
|
|
51
|
+
getTodosStorageMode: () => getTodosStorageMode,
|
|
52
|
+
getTodosStorageEnvName: () => getTodosStorageEnvName,
|
|
53
|
+
getTodosStorageDatabaseUrl: () => getTodosStorageDatabaseUrl,
|
|
54
|
+
getTodosStorageDatabaseEnv: () => getTodosStorageDatabaseEnv,
|
|
55
|
+
getStorageMode: () => getStorageMode,
|
|
56
|
+
getStorageDatabaseUrl: () => getStorageDatabaseUrl,
|
|
57
|
+
getStorageDatabaseEnv: () => getStorageDatabaseEnv,
|
|
58
|
+
getCanonicalTodosRdsConfig: () => getCanonicalTodosRdsConfig,
|
|
59
|
+
assertTodosShadowConfig: () => assertTodosShadowConfig,
|
|
60
|
+
assertTodosRemoteStorageConfig: () => assertTodosRemoteStorageConfig,
|
|
61
|
+
TODOS_STORAGE_TABLES: () => TODOS_STORAGE_TABLES,
|
|
62
|
+
TODOS_STORAGE_FALLBACK_ENV: () => TODOS_STORAGE_FALLBACK_ENV,
|
|
63
|
+
TODOS_STORAGE_ENV: () => TODOS_STORAGE_ENV,
|
|
64
|
+
STORAGE_TABLES: () => STORAGE_TABLES,
|
|
65
|
+
CANONICAL_TODOS_RDS_RUNTIME_PATH: () => CANONICAL_TODOS_RDS_RUNTIME_PATH,
|
|
66
|
+
CANONICAL_TODOS_RDS_DATABASE: () => CANONICAL_TODOS_RDS_DATABASE,
|
|
67
|
+
CANONICAL_TODOS_RDS_CLUSTER: () => CANONICAL_TODOS_RDS_CLUSTER
|
|
68
|
+
});
|
|
69
|
+
function getCanonicalTodosRdsConfig() {
|
|
70
|
+
return {
|
|
71
|
+
cluster: CANONICAL_TODOS_RDS_CLUSTER,
|
|
72
|
+
database: CANONICAL_TODOS_RDS_DATABASE,
|
|
73
|
+
runtimeSecretPath: CANONICAL_TODOS_RDS_RUNTIME_PATH,
|
|
74
|
+
primaryEnv: TODOS_STORAGE_ENV.databaseUrl,
|
|
75
|
+
fallbackEnv: TODOS_STORAGE_FALLBACK_ENV.databaseUrl
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function loadTodosStorageConfig(env = process.env) {
|
|
79
|
+
const mode = getTodosStorageMode(env);
|
|
80
|
+
const databaseUrl = getTodosStorageDatabaseUrl(env);
|
|
81
|
+
const bucket = readStorageEnv(env, "s3Bucket").value;
|
|
82
|
+
const prefix = readStorageEnv(env, "s3Prefix").value ?? "todos/";
|
|
83
|
+
const region = readStorageEnv(env, "awsRegion").value;
|
|
84
|
+
const endpoint = readStorageEnv(env, "s3Endpoint").value;
|
|
85
|
+
const schema = readStorageEnv(env, "databaseSchema").value;
|
|
86
|
+
return {
|
|
87
|
+
service: "todos",
|
|
88
|
+
mode,
|
|
89
|
+
...databaseUrl ? {
|
|
90
|
+
database: {
|
|
91
|
+
provider: "postgres",
|
|
92
|
+
url: databaseUrl,
|
|
93
|
+
ssl: parseBoolean(readStorageEnv(env, "databaseSsl").value, true),
|
|
94
|
+
...schema ? { schema } : {}
|
|
95
|
+
}
|
|
96
|
+
} : {},
|
|
97
|
+
...bucket ? {
|
|
98
|
+
objectStorage: {
|
|
99
|
+
provider: "s3",
|
|
100
|
+
bucket,
|
|
101
|
+
prefix,
|
|
102
|
+
...region ? { region } : {},
|
|
103
|
+
...endpoint ? { endpoint } : {},
|
|
104
|
+
forcePathStyle: parseBoolean(readStorageEnv(env, "s3ForcePathStyle").value, false)
|
|
105
|
+
}
|
|
106
|
+
} : {},
|
|
107
|
+
sync: {
|
|
108
|
+
batchSize: parsePositiveInteger(readStorageEnv(env, "syncBatchSize").value, 500),
|
|
109
|
+
dryRun: parseBoolean(readStorageEnv(env, "syncDryRun").value, false)
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function loadStorageConfig(env = process.env) {
|
|
114
|
+
return loadTodosStorageConfig(env);
|
|
115
|
+
}
|
|
116
|
+
function isTodosRemoteStorageEnabled(config) {
|
|
117
|
+
return config.mode === "remote" || config.mode === "hybrid";
|
|
118
|
+
}
|
|
119
|
+
function assertTodosRemoteStorageConfig(config) {
|
|
120
|
+
if (!isTodosRemoteStorageEnabled(config))
|
|
121
|
+
return;
|
|
122
|
+
if (!config.database?.url) {
|
|
123
|
+
throw new Error(`${TODOS_STORAGE_ENV.databaseUrl} is required when ${TODOS_STORAGE_ENV.mode}=${config.mode}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function parseStorageMode(value) {
|
|
127
|
+
const normalized = clean(value)?.toLowerCase();
|
|
128
|
+
if (!normalized)
|
|
129
|
+
return "local";
|
|
130
|
+
if (normalized === "local" || normalized === "remote" || normalized === "hybrid")
|
|
131
|
+
return normalized;
|
|
132
|
+
throw new Error(`${TODOS_STORAGE_ENV.mode} must be local, remote, or hybrid`);
|
|
133
|
+
}
|
|
134
|
+
function getTodosStorageMode(env = process.env) {
|
|
135
|
+
return parseStorageMode(readStorageEnv(env, "mode").value);
|
|
136
|
+
}
|
|
137
|
+
function getStorageMode(env = process.env) {
|
|
138
|
+
return getTodosStorageMode(env);
|
|
139
|
+
}
|
|
140
|
+
function isTodosShadowEnabled(env = process.env) {
|
|
141
|
+
return parseBoolean(readStorageEnv(env, "shadow").value, false);
|
|
142
|
+
}
|
|
143
|
+
function getTodosStorageShadowEnvName(env = process.env) {
|
|
144
|
+
return readStorageEnv(env, "shadow").name;
|
|
145
|
+
}
|
|
146
|
+
function assertTodosShadowConfig(config, env = process.env) {
|
|
147
|
+
if (!isTodosShadowEnabled(env))
|
|
148
|
+
return;
|
|
149
|
+
if (config.mode !== "local") {
|
|
150
|
+
throw new Error(`${readStorageEnv(env, "shadow").name} shadow mirror requires ${TODOS_STORAGE_ENV.mode}=local (got ${config.mode})`);
|
|
151
|
+
}
|
|
152
|
+
if (!config.database?.url) {
|
|
153
|
+
throw new Error(`${TODOS_STORAGE_ENV.databaseUrl} is required when ${readStorageEnv(env, "shadow").name} is enabled`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function getTodosStorageDatabaseEnv(env = process.env) {
|
|
157
|
+
return readStorageEnv(env, "databaseUrl").name;
|
|
158
|
+
}
|
|
159
|
+
function getStorageDatabaseEnv(env = process.env) {
|
|
160
|
+
return getTodosStorageDatabaseEnv(env);
|
|
161
|
+
}
|
|
162
|
+
function getTodosStorageDatabaseUrl(env = process.env) {
|
|
163
|
+
return readStorageEnv(env, "databaseUrl").value;
|
|
164
|
+
}
|
|
165
|
+
function getStorageDatabaseUrl(env = process.env) {
|
|
166
|
+
return getTodosStorageDatabaseUrl(env);
|
|
167
|
+
}
|
|
168
|
+
function getTodosStorageEnvName(env, key) {
|
|
169
|
+
return readStorageEnv(env, key).name;
|
|
170
|
+
}
|
|
171
|
+
function readStorageEnv(env, key) {
|
|
172
|
+
const primaryName = TODOS_STORAGE_ENV[key];
|
|
173
|
+
const primaryValue = clean(env[primaryName]);
|
|
174
|
+
if (primaryValue !== undefined)
|
|
175
|
+
return { name: primaryName, value: primaryValue };
|
|
176
|
+
const fallbackName = TODOS_STORAGE_FALLBACK_ENV[key];
|
|
177
|
+
const fallbackValue = clean(env[fallbackName]);
|
|
178
|
+
if (fallbackValue !== undefined)
|
|
179
|
+
return { name: fallbackName, value: fallbackValue };
|
|
180
|
+
return { name: primaryName };
|
|
181
|
+
}
|
|
182
|
+
function clean(value) {
|
|
183
|
+
const trimmed = value?.trim();
|
|
184
|
+
return trimmed ? trimmed : undefined;
|
|
185
|
+
}
|
|
186
|
+
function parseBoolean(value, fallback) {
|
|
187
|
+
const normalized = clean(value)?.toLowerCase();
|
|
188
|
+
if (!normalized)
|
|
189
|
+
return fallback;
|
|
190
|
+
if (["1", "true", "yes", "on"].includes(normalized))
|
|
191
|
+
return true;
|
|
192
|
+
if (["0", "false", "no", "off"].includes(normalized))
|
|
193
|
+
return false;
|
|
194
|
+
throw new Error(`Expected boolean env value, got ${value}`);
|
|
195
|
+
}
|
|
196
|
+
function parsePositiveInteger(value, fallback) {
|
|
197
|
+
const normalized = clean(value);
|
|
198
|
+
if (!normalized)
|
|
199
|
+
return fallback;
|
|
200
|
+
const parsed = Number.parseInt(normalized, 10);
|
|
201
|
+
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
202
|
+
throw new Error(`${TODOS_STORAGE_ENV.syncBatchSize} must be a positive integer`);
|
|
203
|
+
}
|
|
204
|
+
return parsed;
|
|
205
|
+
}
|
|
206
|
+
var TODOS_STORAGE_TABLES, STORAGE_TABLES, TODOS_STORAGE_ENV, TODOS_STORAGE_FALLBACK_ENV, CANONICAL_TODOS_RDS_CLUSTER = "hasna-xyz-infra-apps-prod-postgres", CANONICAL_TODOS_RDS_DATABASE = "todos", CANONICAL_TODOS_RDS_RUNTIME_PATH = "hasna/xyz/opensource/todos/prod/rds";
|
|
207
|
+
var init_config = __esm(() => {
|
|
208
|
+
TODOS_STORAGE_TABLES = [
|
|
209
|
+
"todos_sync_records",
|
|
210
|
+
"todos_sync_cursors"
|
|
211
|
+
];
|
|
212
|
+
STORAGE_TABLES = TODOS_STORAGE_TABLES;
|
|
213
|
+
TODOS_STORAGE_ENV = {
|
|
214
|
+
mode: "HASNA_TODOS_STORAGE_MODE",
|
|
215
|
+
shadow: "HASNA_TODOS_SHADOW",
|
|
216
|
+
databaseUrl: "HASNA_TODOS_DATABASE_URL",
|
|
217
|
+
databaseSsl: "HASNA_TODOS_DATABASE_SSL",
|
|
218
|
+
databaseSchema: "HASNA_TODOS_DATABASE_SCHEMA",
|
|
219
|
+
s3Bucket: "HASNA_TODOS_S3_BUCKET",
|
|
220
|
+
s3Prefix: "HASNA_TODOS_S3_PREFIX",
|
|
221
|
+
awsRegion: "HASNA_TODOS_AWS_REGION",
|
|
222
|
+
s3Endpoint: "HASNA_TODOS_S3_ENDPOINT",
|
|
223
|
+
s3ForcePathStyle: "HASNA_TODOS_S3_FORCE_PATH_STYLE",
|
|
224
|
+
s3AccessKeyId: "HASNA_TODOS_S3_ACCESS_KEY_ID",
|
|
225
|
+
s3SecretAccessKey: "HASNA_TODOS_S3_SECRET_ACCESS_KEY",
|
|
226
|
+
s3SessionToken: "HASNA_TODOS_S3_SESSION_TOKEN",
|
|
227
|
+
syncBatchSize: "HASNA_TODOS_SYNC_BATCH_SIZE",
|
|
228
|
+
syncDryRun: "HASNA_TODOS_SYNC_DRY_RUN"
|
|
229
|
+
};
|
|
230
|
+
TODOS_STORAGE_FALLBACK_ENV = {
|
|
231
|
+
mode: "TODOS_STORAGE_MODE",
|
|
232
|
+
shadow: "TODOS_SHADOW",
|
|
233
|
+
databaseUrl: "TODOS_DATABASE_URL",
|
|
234
|
+
databaseSsl: "TODOS_DATABASE_SSL",
|
|
235
|
+
databaseSchema: "TODOS_DATABASE_SCHEMA",
|
|
236
|
+
s3Bucket: "TODOS_S3_BUCKET",
|
|
237
|
+
s3Prefix: "TODOS_S3_PREFIX",
|
|
238
|
+
awsRegion: "TODOS_AWS_REGION",
|
|
239
|
+
s3Endpoint: "TODOS_S3_ENDPOINT",
|
|
240
|
+
s3ForcePathStyle: "TODOS_S3_FORCE_PATH_STYLE",
|
|
241
|
+
s3AccessKeyId: "TODOS_S3_ACCESS_KEY_ID",
|
|
242
|
+
s3SecretAccessKey: "TODOS_S3_SECRET_ACCESS_KEY",
|
|
243
|
+
s3SessionToken: "TODOS_S3_SESSION_TOKEN",
|
|
244
|
+
syncBatchSize: "TODOS_SYNC_BATCH_SIZE",
|
|
245
|
+
syncDryRun: "TODOS_SYNC_DRY_RUN"
|
|
246
|
+
};
|
|
247
|
+
});
|
|
248
|
+
|
|
42
249
|
// src/types/index.ts
|
|
43
250
|
var TASK_STATUSES, TASK_PRIORITIES, PLAN_STATUSES, VersionConflictError, TaskNotFoundError, ProjectNotFoundError, PlanNotFoundError, LockError, AgentNotFoundError, TaskListNotFoundError, DependencyCycleError, CompletionGuardError, DISPATCH_STATUSES, DispatchNotFoundError;
|
|
44
251
|
var init_types = __esm(() => {
|
|
@@ -2773,6 +2980,87 @@ var init_machines = __esm(() => {
|
|
|
2773
2980
|
];
|
|
2774
2981
|
});
|
|
2775
2982
|
|
|
2983
|
+
// src/storage/shadow-outbox-schema.ts
|
|
2984
|
+
var exports_shadow_outbox_schema = {};
|
|
2985
|
+
__export(exports_shadow_outbox_schema, {
|
|
2986
|
+
installShadowOutboxSchema: () => installShadowOutboxSchema,
|
|
2987
|
+
SHADOW_TRIGGER_TABLES: () => SHADOW_TRIGGER_TABLES
|
|
2988
|
+
});
|
|
2989
|
+
function installShadowOutboxSchema(db) {
|
|
2990
|
+
db.run(`CREATE TABLE IF NOT EXISTS shadow_outbox (
|
|
2991
|
+
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
2992
|
+
object_type TEXT NOT NULL,
|
|
2993
|
+
object_id TEXT NOT NULL,
|
|
2994
|
+
op TEXT NOT NULL,
|
|
2995
|
+
enqueued_at INTEGER NOT NULL,
|
|
2996
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
2997
|
+
next_attempt_at INTEGER NOT NULL DEFAULT 0,
|
|
2998
|
+
last_error TEXT,
|
|
2999
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
3000
|
+
revision INTEGER NOT NULL DEFAULT 0
|
|
3001
|
+
)`);
|
|
3002
|
+
ensureColumn(db, "shadow_outbox", "revision", "INTEGER NOT NULL DEFAULT 0");
|
|
3003
|
+
db.run(`CREATE UNIQUE INDEX IF NOT EXISTS shadow_outbox_pending_uq
|
|
3004
|
+
ON shadow_outbox(object_type, object_id) WHERE status='pending'`);
|
|
3005
|
+
db.run(`CREATE INDEX IF NOT EXISTS shadow_outbox_ready_idx
|
|
3006
|
+
ON shadow_outbox(status, next_attempt_at)`);
|
|
3007
|
+
for (const { table, objectType, deletes } of SHADOW_TRIGGER_TABLES) {
|
|
3008
|
+
const exists = db.query(`SELECT name FROM sqlite_master WHERE type='table' AND name=?`).get(table);
|
|
3009
|
+
if (!exists)
|
|
3010
|
+
continue;
|
|
3011
|
+
const insertTrigger = `shadow_ob_${table}_ai`;
|
|
3012
|
+
const updateTrigger = `shadow_ob_${table}_au`;
|
|
3013
|
+
db.run(`DROP TRIGGER IF EXISTS ${insertTrigger}`);
|
|
3014
|
+
db.run(`DROP TRIGGER IF EXISTS ${updateTrigger}`);
|
|
3015
|
+
db.run(upsertTriggerSql(insertTrigger, table, objectType, "INSERT", "NEW"));
|
|
3016
|
+
db.run(upsertTriggerSql(updateTrigger, table, objectType, "UPDATE", "NEW"));
|
|
3017
|
+
if (deletes) {
|
|
3018
|
+
const deleteTrigger = `shadow_ob_${table}_ad`;
|
|
3019
|
+
db.run(`DROP TRIGGER IF EXISTS ${deleteTrigger}`);
|
|
3020
|
+
db.run(deleteTriggerSql(deleteTrigger, table, objectType));
|
|
3021
|
+
}
|
|
3022
|
+
}
|
|
3023
|
+
}
|
|
3024
|
+
function ensureColumn(db, table, column, definition) {
|
|
3025
|
+
const rows = db.query(`PRAGMA table_info(${table})`).all();
|
|
3026
|
+
if (rows.some((row) => row.name === column))
|
|
3027
|
+
return;
|
|
3028
|
+
db.run(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`);
|
|
3029
|
+
}
|
|
3030
|
+
function upsertTriggerSql(name, table, objectType, event, row) {
|
|
3031
|
+
return `CREATE TRIGGER IF NOT EXISTS ${name}
|
|
3032
|
+
AFTER ${event} ON ${table}
|
|
3033
|
+
BEGIN
|
|
3034
|
+
INSERT INTO shadow_outbox(object_type, object_id, op, enqueued_at, attempts, next_attempt_at, last_error, status, revision)
|
|
3035
|
+
VALUES('${objectType}', ${row}.id, 'upsert', CAST(strftime('%s','now') AS INTEGER) * 1000, 0, 0, NULL, 'pending', 0)
|
|
3036
|
+
ON CONFLICT(object_type, object_id) WHERE status='pending'
|
|
3037
|
+
DO UPDATE SET op='upsert', enqueued_at=excluded.enqueued_at, attempts=0, next_attempt_at=0, last_error=NULL, revision=shadow_outbox.revision + 1;
|
|
3038
|
+
END`;
|
|
3039
|
+
}
|
|
3040
|
+
function deleteTriggerSql(name, table, objectType) {
|
|
3041
|
+
return `CREATE TRIGGER IF NOT EXISTS ${name}
|
|
3042
|
+
AFTER DELETE ON ${table}
|
|
3043
|
+
BEGIN
|
|
3044
|
+
INSERT INTO shadow_outbox(object_type, object_id, op, enqueued_at, attempts, next_attempt_at, last_error, status, revision)
|
|
3045
|
+
VALUES('${objectType}', OLD.id, 'delete', CAST(strftime('%s','now') AS INTEGER) * 1000, 0, 0, NULL, 'pending', 0)
|
|
3046
|
+
ON CONFLICT(object_type, object_id) WHERE status='pending'
|
|
3047
|
+
DO UPDATE SET op='delete', enqueued_at=excluded.enqueued_at, attempts=0, next_attempt_at=0, last_error=NULL, revision=shadow_outbox.revision + 1;
|
|
3048
|
+
END`;
|
|
3049
|
+
}
|
|
3050
|
+
var SHADOW_TRIGGER_TABLES;
|
|
3051
|
+
var init_shadow_outbox_schema = __esm(() => {
|
|
3052
|
+
SHADOW_TRIGGER_TABLES = [
|
|
3053
|
+
{ table: "tasks", objectType: "tasks", deletes: true },
|
|
3054
|
+
{ table: "projects", objectType: "projects", deletes: true },
|
|
3055
|
+
{ table: "project_machine_paths", objectType: "project_machine_paths", deletes: true },
|
|
3056
|
+
{ table: "plans", objectType: "plans", deletes: true },
|
|
3057
|
+
{ table: "agents", objectType: "agents", deletes: false },
|
|
3058
|
+
{ table: "task_lists", objectType: "task_lists", deletes: true },
|
|
3059
|
+
{ table: "task_templates", objectType: "templates", deletes: true },
|
|
3060
|
+
{ table: "task_history", objectType: "audit_history", deletes: false }
|
|
3061
|
+
];
|
|
3062
|
+
});
|
|
3063
|
+
|
|
2776
3064
|
// src/db/database.ts
|
|
2777
3065
|
var exports_database = {};
|
|
2778
3066
|
__export(exports_database, {
|
|
@@ -2863,8 +3151,20 @@ function openDatabase(path) {
|
|
|
2863
3151
|
runMigrations(db);
|
|
2864
3152
|
backfillTaskTags(db);
|
|
2865
3153
|
backfillMachineId(db);
|
|
3154
|
+
maybeInstallShadowCapture(db);
|
|
2866
3155
|
return db;
|
|
2867
3156
|
}
|
|
3157
|
+
function maybeInstallShadowCapture(db) {
|
|
3158
|
+
try {
|
|
3159
|
+
const { isTodosShadowEnabled: isTodosShadowEnabled2 } = (init_config(), __toCommonJS(exports_config));
|
|
3160
|
+
if (!isTodosShadowEnabled2())
|
|
3161
|
+
return;
|
|
3162
|
+
const { installShadowOutboxSchema: installShadowOutboxSchema2 } = (init_shadow_outbox_schema(), __toCommonJS(exports_shadow_outbox_schema));
|
|
3163
|
+
installShadowOutboxSchema2(db);
|
|
3164
|
+
} catch (error) {
|
|
3165
|
+
console.error(`[todos] shadow capture install failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
3166
|
+
}
|
|
3167
|
+
}
|
|
2868
3168
|
function getDatabase(dbPath) {
|
|
2869
3169
|
const path = dbPath || getDbPath();
|
|
2870
3170
|
if (_db && _dbPath === path)
|
|
@@ -3111,7 +3411,7 @@ function getCompletionGuardConfig(projectPath) {
|
|
|
3111
3411
|
return global;
|
|
3112
3412
|
}
|
|
3113
3413
|
var cached = null, GUARD_DEFAULTS;
|
|
3114
|
-
var
|
|
3414
|
+
var init_config2 = __esm(() => {
|
|
3115
3415
|
init_sync_utils();
|
|
3116
3416
|
GUARD_DEFAULTS = {
|
|
3117
3417
|
enabled: false,
|
|
@@ -3482,7 +3782,7 @@ function checkCompletionGuard(task, agentId, db, configOverride) {
|
|
|
3482
3782
|
}
|
|
3483
3783
|
var init_completion_guard = __esm(() => {
|
|
3484
3784
|
init_types();
|
|
3485
|
-
|
|
3785
|
+
init_config2();
|
|
3486
3786
|
init_projects();
|
|
3487
3787
|
});
|
|
3488
3788
|
|
|
@@ -3490,8 +3790,8 @@ var init_completion_guard = __esm(() => {
|
|
|
3490
3790
|
import { tmpdir } from "os";
|
|
3491
3791
|
import { resolve as resolve3, sep } from "path";
|
|
3492
3792
|
function envFlag(name) {
|
|
3493
|
-
const value = process.env[name];
|
|
3494
|
-
return value === "1" || value === "true" || value === "yes";
|
|
3793
|
+
const value = process.env[name]?.trim().toLowerCase();
|
|
3794
|
+
return value === "1" || value === "true" || value === "yes" || value === "on";
|
|
3495
3795
|
}
|
|
3496
3796
|
function isUnder(parent, child) {
|
|
3497
3797
|
const normalizedParent = resolve3(parent);
|
|
@@ -3517,7 +3817,7 @@ function usesIsolatedTodosHome() {
|
|
|
3517
3817
|
function shouldEmitSharedTaskEvents(dbPath) {
|
|
3518
3818
|
if (envFlag("TODOS_DISABLE_SHARED_EVENTS"))
|
|
3519
3819
|
return false;
|
|
3520
|
-
if (envFlag("TODOS_ALLOW_EPHEMERAL_SHARED_EVENTS"))
|
|
3820
|
+
if (envFlag("TODOS_ALLOW_EPHEMERAL_SHARED_EVENTS") || envFlag("HASNA_TODOS_ALLOW_GLOBAL_EVENTS_FROM_TEMP_DB"))
|
|
3521
3821
|
return true;
|
|
3522
3822
|
if (!isEphemeralTodosDatabase(dbPath))
|
|
3523
3823
|
return true;
|
|
@@ -3619,7 +3919,7 @@ function upsertSecretSafetyConfig(input) {
|
|
|
3619
3919
|
}
|
|
3620
3920
|
var DEFAULT_SECRET_PATTERNS, DEFAULT_SECRET_KEY_PATTERN, NON_SECRET_USAGE_KEYS;
|
|
3621
3921
|
var init_redaction = __esm(() => {
|
|
3622
|
-
|
|
3922
|
+
init_config2();
|
|
3623
3923
|
DEFAULT_SECRET_PATTERNS = [
|
|
3624
3924
|
{ name: "aws-access-key", regex: /\b(AKIA|ASIA)[0-9A-Z]{16}\b/g, replacement: "[REDACTED_AWS_KEY]" },
|
|
3625
3925
|
{ name: "private-key", regex: /-----BEGIN (?:RSA |EC |OPENSSH |)PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |OPENSSH |)PRIVATE KEY-----/g, replacement: "[REDACTED_PRIVATE_KEY]" },
|
|
@@ -3776,7 +4076,7 @@ function checkWorkspacePermission(input = {}) {
|
|
|
3776
4076
|
}
|
|
3777
4077
|
var DEFAULT_DENYLIST, DEFAULT_ENV_REDACTIONS, PRESET_DEFAULTS;
|
|
3778
4078
|
var init_workspace_trust = __esm(() => {
|
|
3779
|
-
|
|
4079
|
+
init_config2();
|
|
3780
4080
|
DEFAULT_DENYLIST = ["rm -rf", "mkfs", "dd if=", "curl | sh", "wget | sh"];
|
|
3781
4081
|
DEFAULT_ENV_REDACTIONS = ["API_KEY", "TOKEN", "SECRET", "PASSWORD", "AUTH"];
|
|
3782
4082
|
PRESET_DEFAULTS = {
|
|
@@ -4010,7 +4310,7 @@ function explainRunnerSandbox(input = {}) {
|
|
|
4010
4310
|
}
|
|
4011
4311
|
var DEFAULT_COMMAND_DENYLIST, DEFAULT_ENV_REDACTIONS2;
|
|
4012
4312
|
var init_runner_sandbox = __esm(() => {
|
|
4013
|
-
|
|
4313
|
+
init_config2();
|
|
4014
4314
|
init_workspace_trust();
|
|
4015
4315
|
DEFAULT_COMMAND_DENYLIST = ["rm -rf", "mkfs", "dd if=", "curl | sh", "wget | sh"];
|
|
4016
4316
|
DEFAULT_ENV_REDACTIONS2 = ["API_KEY", "TOKEN", "SECRET", "PASSWORD", "AUTH"];
|
|
@@ -4246,7 +4546,7 @@ var LOCAL_EVENT_TYPES, VALID_TARGETS;
|
|
|
4246
4546
|
var init_event_hooks = __esm(() => {
|
|
4247
4547
|
init_redaction();
|
|
4248
4548
|
init_runner_sandbox();
|
|
4249
|
-
|
|
4549
|
+
init_config2();
|
|
4250
4550
|
init_event_emission_safety();
|
|
4251
4551
|
LOCAL_EVENT_TYPES = [
|
|
4252
4552
|
"task.created",
|
|
@@ -10224,168 +10524,9 @@ var init_tasks = __esm(() => {
|
|
|
10224
10524
|
init_calendar();
|
|
10225
10525
|
});
|
|
10226
10526
|
|
|
10227
|
-
// src/storage/
|
|
10228
|
-
|
|
10229
|
-
|
|
10230
|
-
"todos_sync_cursors"
|
|
10231
|
-
];
|
|
10232
|
-
var STORAGE_TABLES = TODOS_STORAGE_TABLES;
|
|
10233
|
-
var TODOS_STORAGE_ENV = {
|
|
10234
|
-
mode: "HASNA_TODOS_STORAGE_MODE",
|
|
10235
|
-
databaseUrl: "HASNA_TODOS_DATABASE_URL",
|
|
10236
|
-
databaseSsl: "HASNA_TODOS_DATABASE_SSL",
|
|
10237
|
-
databaseSchema: "HASNA_TODOS_DATABASE_SCHEMA",
|
|
10238
|
-
s3Bucket: "HASNA_TODOS_S3_BUCKET",
|
|
10239
|
-
s3Prefix: "HASNA_TODOS_S3_PREFIX",
|
|
10240
|
-
awsRegion: "HASNA_TODOS_AWS_REGION",
|
|
10241
|
-
s3Endpoint: "HASNA_TODOS_S3_ENDPOINT",
|
|
10242
|
-
s3ForcePathStyle: "HASNA_TODOS_S3_FORCE_PATH_STYLE",
|
|
10243
|
-
s3AccessKeyId: "HASNA_TODOS_S3_ACCESS_KEY_ID",
|
|
10244
|
-
s3SecretAccessKey: "HASNA_TODOS_S3_SECRET_ACCESS_KEY",
|
|
10245
|
-
s3SessionToken: "HASNA_TODOS_S3_SESSION_TOKEN",
|
|
10246
|
-
syncBatchSize: "HASNA_TODOS_SYNC_BATCH_SIZE",
|
|
10247
|
-
syncDryRun: "HASNA_TODOS_SYNC_DRY_RUN"
|
|
10248
|
-
};
|
|
10249
|
-
var TODOS_STORAGE_FALLBACK_ENV = {
|
|
10250
|
-
mode: "TODOS_STORAGE_MODE",
|
|
10251
|
-
databaseUrl: "TODOS_DATABASE_URL",
|
|
10252
|
-
databaseSsl: "TODOS_DATABASE_SSL",
|
|
10253
|
-
databaseSchema: "TODOS_DATABASE_SCHEMA",
|
|
10254
|
-
s3Bucket: "TODOS_S3_BUCKET",
|
|
10255
|
-
s3Prefix: "TODOS_S3_PREFIX",
|
|
10256
|
-
awsRegion: "TODOS_AWS_REGION",
|
|
10257
|
-
s3Endpoint: "TODOS_S3_ENDPOINT",
|
|
10258
|
-
s3ForcePathStyle: "TODOS_S3_FORCE_PATH_STYLE",
|
|
10259
|
-
s3AccessKeyId: "TODOS_S3_ACCESS_KEY_ID",
|
|
10260
|
-
s3SecretAccessKey: "TODOS_S3_SECRET_ACCESS_KEY",
|
|
10261
|
-
s3SessionToken: "TODOS_S3_SESSION_TOKEN",
|
|
10262
|
-
syncBatchSize: "TODOS_SYNC_BATCH_SIZE",
|
|
10263
|
-
syncDryRun: "TODOS_SYNC_DRY_RUN"
|
|
10264
|
-
};
|
|
10265
|
-
var CANONICAL_TODOS_RDS_CLUSTER = "hasna-xyz-infra-apps-prod-postgres";
|
|
10266
|
-
var CANONICAL_TODOS_RDS_DATABASE = "todos";
|
|
10267
|
-
var CANONICAL_TODOS_RDS_RUNTIME_PATH = "hasna/xyz/opensource/todos/prod/rds";
|
|
10268
|
-
function getCanonicalTodosRdsConfig() {
|
|
10269
|
-
return {
|
|
10270
|
-
cluster: CANONICAL_TODOS_RDS_CLUSTER,
|
|
10271
|
-
database: CANONICAL_TODOS_RDS_DATABASE,
|
|
10272
|
-
runtimeSecretPath: CANONICAL_TODOS_RDS_RUNTIME_PATH,
|
|
10273
|
-
primaryEnv: TODOS_STORAGE_ENV.databaseUrl,
|
|
10274
|
-
fallbackEnv: TODOS_STORAGE_FALLBACK_ENV.databaseUrl
|
|
10275
|
-
};
|
|
10276
|
-
}
|
|
10277
|
-
function loadTodosStorageConfig(env = process.env) {
|
|
10278
|
-
const mode = getTodosStorageMode(env);
|
|
10279
|
-
const databaseUrl = getTodosStorageDatabaseUrl(env);
|
|
10280
|
-
const bucket = readStorageEnv(env, "s3Bucket").value;
|
|
10281
|
-
const prefix = readStorageEnv(env, "s3Prefix").value ?? "todos/";
|
|
10282
|
-
const region = readStorageEnv(env, "awsRegion").value;
|
|
10283
|
-
const endpoint = readStorageEnv(env, "s3Endpoint").value;
|
|
10284
|
-
const schema = readStorageEnv(env, "databaseSchema").value;
|
|
10285
|
-
return {
|
|
10286
|
-
service: "todos",
|
|
10287
|
-
mode,
|
|
10288
|
-
...databaseUrl ? {
|
|
10289
|
-
database: {
|
|
10290
|
-
provider: "postgres",
|
|
10291
|
-
url: databaseUrl,
|
|
10292
|
-
ssl: parseBoolean(readStorageEnv(env, "databaseSsl").value, true),
|
|
10293
|
-
...schema ? { schema } : {}
|
|
10294
|
-
}
|
|
10295
|
-
} : {},
|
|
10296
|
-
...bucket ? {
|
|
10297
|
-
objectStorage: {
|
|
10298
|
-
provider: "s3",
|
|
10299
|
-
bucket,
|
|
10300
|
-
prefix,
|
|
10301
|
-
...region ? { region } : {},
|
|
10302
|
-
...endpoint ? { endpoint } : {},
|
|
10303
|
-
forcePathStyle: parseBoolean(readStorageEnv(env, "s3ForcePathStyle").value, false)
|
|
10304
|
-
}
|
|
10305
|
-
} : {},
|
|
10306
|
-
sync: {
|
|
10307
|
-
batchSize: parsePositiveInteger(readStorageEnv(env, "syncBatchSize").value, 500),
|
|
10308
|
-
dryRun: parseBoolean(readStorageEnv(env, "syncDryRun").value, false)
|
|
10309
|
-
}
|
|
10310
|
-
};
|
|
10311
|
-
}
|
|
10312
|
-
function loadStorageConfig(env = process.env) {
|
|
10313
|
-
return loadTodosStorageConfig(env);
|
|
10314
|
-
}
|
|
10315
|
-
function isTodosRemoteStorageEnabled(config) {
|
|
10316
|
-
return config.mode === "remote" || config.mode === "hybrid";
|
|
10317
|
-
}
|
|
10318
|
-
function assertTodosRemoteStorageConfig(config) {
|
|
10319
|
-
if (!isTodosRemoteStorageEnabled(config))
|
|
10320
|
-
return;
|
|
10321
|
-
if (!config.database?.url) {
|
|
10322
|
-
throw new Error(`${TODOS_STORAGE_ENV.databaseUrl} is required when ${TODOS_STORAGE_ENV.mode}=${config.mode}`);
|
|
10323
|
-
}
|
|
10324
|
-
}
|
|
10325
|
-
function parseStorageMode(value) {
|
|
10326
|
-
const normalized = clean(value)?.toLowerCase();
|
|
10327
|
-
if (!normalized)
|
|
10328
|
-
return "local";
|
|
10329
|
-
if (normalized === "local" || normalized === "remote" || normalized === "hybrid")
|
|
10330
|
-
return normalized;
|
|
10331
|
-
throw new Error(`${TODOS_STORAGE_ENV.mode} must be local, remote, or hybrid`);
|
|
10332
|
-
}
|
|
10333
|
-
function getTodosStorageMode(env = process.env) {
|
|
10334
|
-
return parseStorageMode(readStorageEnv(env, "mode").value);
|
|
10335
|
-
}
|
|
10336
|
-
function getStorageMode(env = process.env) {
|
|
10337
|
-
return getTodosStorageMode(env);
|
|
10338
|
-
}
|
|
10339
|
-
function getTodosStorageDatabaseEnv(env = process.env) {
|
|
10340
|
-
return readStorageEnv(env, "databaseUrl").name;
|
|
10341
|
-
}
|
|
10342
|
-
function getStorageDatabaseEnv(env = process.env) {
|
|
10343
|
-
return getTodosStorageDatabaseEnv(env);
|
|
10344
|
-
}
|
|
10345
|
-
function getTodosStorageDatabaseUrl(env = process.env) {
|
|
10346
|
-
return readStorageEnv(env, "databaseUrl").value;
|
|
10347
|
-
}
|
|
10348
|
-
function getStorageDatabaseUrl(env = process.env) {
|
|
10349
|
-
return getTodosStorageDatabaseUrl(env);
|
|
10350
|
-
}
|
|
10351
|
-
function getTodosStorageEnvName(env, key) {
|
|
10352
|
-
return readStorageEnv(env, key).name;
|
|
10353
|
-
}
|
|
10354
|
-
function readStorageEnv(env, key) {
|
|
10355
|
-
const primaryName = TODOS_STORAGE_ENV[key];
|
|
10356
|
-
const primaryValue = clean(env[primaryName]);
|
|
10357
|
-
if (primaryValue !== undefined)
|
|
10358
|
-
return { name: primaryName, value: primaryValue };
|
|
10359
|
-
const fallbackName = TODOS_STORAGE_FALLBACK_ENV[key];
|
|
10360
|
-
const fallbackValue = clean(env[fallbackName]);
|
|
10361
|
-
if (fallbackValue !== undefined)
|
|
10362
|
-
return { name: fallbackName, value: fallbackValue };
|
|
10363
|
-
return { name: primaryName };
|
|
10364
|
-
}
|
|
10365
|
-
function clean(value) {
|
|
10366
|
-
const trimmed = value?.trim();
|
|
10367
|
-
return trimmed ? trimmed : undefined;
|
|
10368
|
-
}
|
|
10369
|
-
function parseBoolean(value, fallback) {
|
|
10370
|
-
const normalized = clean(value)?.toLowerCase();
|
|
10371
|
-
if (!normalized)
|
|
10372
|
-
return fallback;
|
|
10373
|
-
if (["1", "true", "yes", "on"].includes(normalized))
|
|
10374
|
-
return true;
|
|
10375
|
-
if (["0", "false", "no", "off"].includes(normalized))
|
|
10376
|
-
return false;
|
|
10377
|
-
throw new Error(`Expected boolean env value, got ${value}`);
|
|
10378
|
-
}
|
|
10379
|
-
function parsePositiveInteger(value, fallback) {
|
|
10380
|
-
const normalized = clean(value);
|
|
10381
|
-
if (!normalized)
|
|
10382
|
-
return fallback;
|
|
10383
|
-
const parsed = Number.parseInt(normalized, 10);
|
|
10384
|
-
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
10385
|
-
throw new Error(`${TODOS_STORAGE_ENV.syncBatchSize} must be a positive integer`);
|
|
10386
|
-
}
|
|
10387
|
-
return parsed;
|
|
10388
|
-
}
|
|
10527
|
+
// src/storage/index.ts
|
|
10528
|
+
init_config();
|
|
10529
|
+
|
|
10389
10530
|
// src/storage/local-sqlite.ts
|
|
10390
10531
|
init_tasks();
|
|
10391
10532
|
init_projects();
|
|
@@ -11496,6 +11637,9 @@ function postgresTodosSyncSchemaSql(tableName = DEFAULT_TODOS_POSTGRES_SYNC_TABL
|
|
|
11496
11637
|
PRIMARY KEY (service, object_type, object_id)
|
|
11497
11638
|
)`,
|
|
11498
11639
|
`CREATE INDEX IF NOT EXISTS ${tableName}_updated_idx ON ${tableName} (service, updated_at)`,
|
|
11640
|
+
`CREATE INDEX IF NOT EXISTS ${tableName}_task_status_idx ON ${tableName} ((payload->>'status')) WHERE object_type = 'tasks' AND deleted_at IS NULL`,
|
|
11641
|
+
`CREATE INDEX IF NOT EXISTS ${tableName}_task_project_idx ON ${tableName} ((payload->>'project_id')) WHERE object_type = 'tasks' AND deleted_at IS NULL`,
|
|
11642
|
+
`CREATE INDEX IF NOT EXISTS ${tableName}_payload_gin ON ${tableName} USING gin (payload jsonb_path_ops)`,
|
|
11499
11643
|
`CREATE TABLE IF NOT EXISTS ${cursorTableName} (
|
|
11500
11644
|
service text NOT NULL,
|
|
11501
11645
|
cursor_name text NOT NULL,
|
|
@@ -11546,7 +11690,7 @@ class PostgresTodosSyncStore {
|
|
|
11546
11690
|
this.service,
|
|
11547
11691
|
entry.type,
|
|
11548
11692
|
entry.id,
|
|
11549
|
-
|
|
11693
|
+
entry.payload,
|
|
11550
11694
|
entry.updatedAt,
|
|
11551
11695
|
entry.deletedAt,
|
|
11552
11696
|
sourceMachineId,
|
|
@@ -11752,8 +11896,8 @@ function createPostgresTodosStorageAdapter(options) {
|
|
|
11752
11896
|
tasks: {
|
|
11753
11897
|
create: (input, context) => createTask2(input, store, context),
|
|
11754
11898
|
get: (id) => store.get("tasks", id),
|
|
11755
|
-
list: (filter = {}) =>
|
|
11756
|
-
count:
|
|
11899
|
+
list: (filter = {}) => store.listTasks(filter),
|
|
11900
|
+
count: (filter = {}) => store.countTasks(filter),
|
|
11757
11901
|
update: (id, input) => updateTask2(id, input, store),
|
|
11758
11902
|
delete: (id, context) => store.delete("tasks", id, context),
|
|
11759
11903
|
start: (id, agentId) => startTask2(id, agentId, store),
|
|
@@ -11869,6 +12013,72 @@ class PostgresJsonRecordStore {
|
|
|
11869
12013
|
updatedAt: stringValue2(row.updated_at) ?? new Date().toISOString()
|
|
11870
12014
|
}));
|
|
11871
12015
|
}
|
|
12016
|
+
buildTaskFilterSql(filter) {
|
|
12017
|
+
const params = [this.service, "tasks"];
|
|
12018
|
+
const conds = ["service = $1", "object_type = $2", "deleted_at IS NULL"];
|
|
12019
|
+
const p = (value) => {
|
|
12020
|
+
params.push(value);
|
|
12021
|
+
return `$${params.length}`;
|
|
12022
|
+
};
|
|
12023
|
+
const inClause = (column, values) => {
|
|
12024
|
+
if (values.length === 0)
|
|
12025
|
+
return "1=0";
|
|
12026
|
+
return `${column} IN (${values.map((v) => p(v)).join(", ")})`;
|
|
12027
|
+
};
|
|
12028
|
+
if (filter.ids)
|
|
12029
|
+
conds.push(inClause("payload->>'id'", filter.ids));
|
|
12030
|
+
if (filter.project_id !== undefined)
|
|
12031
|
+
conds.push(`payload->>'project_id' = ${p(filter.project_id)}`);
|
|
12032
|
+
if (filter.parent_id !== undefined)
|
|
12033
|
+
conds.push(`payload->>'parent_id' IS NOT DISTINCT FROM ${p(filter.parent_id)}`);
|
|
12034
|
+
if (filter.plan_id !== undefined)
|
|
12035
|
+
conds.push(`payload->>'plan_id' = ${p(filter.plan_id)}`);
|
|
12036
|
+
if (filter.task_list_id !== undefined)
|
|
12037
|
+
conds.push(`payload->>'task_list_id' = ${p(filter.task_list_id)}`);
|
|
12038
|
+
if (filter.status !== undefined)
|
|
12039
|
+
conds.push(inClause("payload->>'status'", toFilterArray(filter.status)));
|
|
12040
|
+
if (filter.priority !== undefined)
|
|
12041
|
+
conds.push(inClause("payload->>'priority'", toFilterArray(filter.priority)));
|
|
12042
|
+
if (filter.assigned_to !== undefined)
|
|
12043
|
+
conds.push(`payload->>'assigned_to' = ${p(filter.assigned_to)}`);
|
|
12044
|
+
if (filter.agent_id !== undefined)
|
|
12045
|
+
conds.push(`payload->>'agent_id' = ${p(filter.agent_id)}`);
|
|
12046
|
+
if (filter.session_id !== undefined)
|
|
12047
|
+
conds.push(`payload->>'session_id' = ${p(filter.session_id)}`);
|
|
12048
|
+
if (filter.tags?.length)
|
|
12049
|
+
conds.push(`payload->'tags' @> ${p(filter.tags)}::jsonb`);
|
|
12050
|
+
if (filter.has_recurrence !== undefined) {
|
|
12051
|
+
conds.push(`(COALESCE(payload->>'recurrence_rule', '') <> '') = ${p(filter.has_recurrence)}`);
|
|
12052
|
+
}
|
|
12053
|
+
if (filter.task_type !== undefined) {
|
|
12054
|
+
conds.push(inClause("COALESCE(payload->>'task_type', '')", toFilterArray(filter.task_type)));
|
|
12055
|
+
}
|
|
12056
|
+
if (filter.include_subtasks !== true)
|
|
12057
|
+
conds.push(`(payload->>'parent_id' IS NULL OR payload->>'parent_id' = '')`);
|
|
12058
|
+
return { where: conds.join(" AND "), params };
|
|
12059
|
+
}
|
|
12060
|
+
async listTasks(filter) {
|
|
12061
|
+
await this.ensureSchema();
|
|
12062
|
+
const { where, params } = this.buildTaskFilterSql(filter);
|
|
12063
|
+
let sql = `/* todos:list-tasks */ SELECT payload FROM ${this.tableName} WHERE ${where} ${TASK_ORDER_BY}`;
|
|
12064
|
+
if (filter.limit !== undefined) {
|
|
12065
|
+
params.push(filter.limit);
|
|
12066
|
+
sql += ` LIMIT $${params.length}`;
|
|
12067
|
+
}
|
|
12068
|
+
if (filter.offset) {
|
|
12069
|
+
params.push(filter.offset);
|
|
12070
|
+
sql += ` OFFSET $${params.length}`;
|
|
12071
|
+
}
|
|
12072
|
+
const result = await this.options.client.query(sql, params);
|
|
12073
|
+
return result.rows.map((row) => payloadRecord2(row.payload));
|
|
12074
|
+
}
|
|
12075
|
+
async countTasks(filter) {
|
|
12076
|
+
await this.ensureSchema();
|
|
12077
|
+
const { where, params } = this.buildTaskFilterSql(filter);
|
|
12078
|
+
const sql = `/* todos:count-tasks */ SELECT COUNT(*)::int AS count FROM ${this.tableName} WHERE ${where}`;
|
|
12079
|
+
const result = await this.options.client.query(sql, params);
|
|
12080
|
+
return Number(result.rows[0]?.count ?? 0);
|
|
12081
|
+
}
|
|
11872
12082
|
async listTombstones() {
|
|
11873
12083
|
await this.ensureSchema();
|
|
11874
12084
|
const result = await this.options.client.query(`SELECT object_type, object_id, payload, updated_at, deleted_at, source_machine_id, version
|
|
@@ -11909,7 +12119,7 @@ class PostgresJsonRecordStore {
|
|
|
11909
12119
|
this.service,
|
|
11910
12120
|
type,
|
|
11911
12121
|
value.id,
|
|
11912
|
-
|
|
12122
|
+
jsonbParam(value),
|
|
11913
12123
|
updatedAt,
|
|
11914
12124
|
context.requestId ?? this.sourceMachineId ?? null,
|
|
11915
12125
|
numberValue2(value.version)
|
|
@@ -11971,7 +12181,7 @@ class PostgresJsonRecordStore {
|
|
|
11971
12181
|
this.service,
|
|
11972
12182
|
tombstone.object_type,
|
|
11973
12183
|
tombstone.object_id,
|
|
11974
|
-
|
|
12184
|
+
jsonbParam(tombstone.payload ?? { id: tombstone.object_id, deleted_at: deletedAt }),
|
|
11975
12185
|
updatedAt,
|
|
11976
12186
|
deletedAt,
|
|
11977
12187
|
tombstone.source_machine_id ?? context.requestId ?? this.sourceMachineId ?? null,
|
|
@@ -12150,13 +12360,15 @@ async function patchTask(task, patch, store) {
|
|
|
12150
12360
|
await store.upsert("tasks", updated);
|
|
12151
12361
|
return updated;
|
|
12152
12362
|
}
|
|
12363
|
+
var TASK_ORDER_BY = "ORDER BY CASE payload->>'priority' WHEN 'critical' THEN 0 WHEN 'high' THEN 1 WHEN 'medium' THEN 2 WHEN 'low' THEN 3 ELSE 4 END ASC, payload->>'created_at' ASC, payload->>'id' ASC";
|
|
12364
|
+
function toFilterArray(value) {
|
|
12365
|
+
return Array.isArray(value) ? value : [value];
|
|
12366
|
+
}
|
|
12367
|
+
function jsonbParam(value) {
|
|
12368
|
+
return value;
|
|
12369
|
+
}
|
|
12153
12370
|
async function listTasks2(filter, store) {
|
|
12154
|
-
|
|
12155
|
-
tasks = tasks.filter((task) => taskMatchesFilter(task, filter));
|
|
12156
|
-
tasks.sort((a, b) => priorityRank(a.priority) - priorityRank(b.priority) || a.created_at.localeCompare(b.created_at));
|
|
12157
|
-
const offset = filter.offset ?? 0;
|
|
12158
|
-
const limit = filter.limit ?? tasks.length;
|
|
12159
|
-
return tasks.slice(offset, offset + limit);
|
|
12371
|
+
return store.listTasks(filter);
|
|
12160
12372
|
}
|
|
12161
12373
|
async function getNextTask2(filters, store) {
|
|
12162
12374
|
return (await listTasks2({ ...filters, status: "pending", limit: 1 }, store))[0] ?? null;
|
|
@@ -12464,43 +12676,6 @@ async function generateProjectPrefix(name, store) {
|
|
|
12464
12676
|
}
|
|
12465
12677
|
return candidate;
|
|
12466
12678
|
}
|
|
12467
|
-
function taskMatchesFilter(task, filter) {
|
|
12468
|
-
if (filter.ids && !filter.ids.includes(task.id))
|
|
12469
|
-
return false;
|
|
12470
|
-
if (filter.project_id !== undefined && task.project_id !== filter.project_id)
|
|
12471
|
-
return false;
|
|
12472
|
-
if (filter.parent_id !== undefined && task.parent_id !== filter.parent_id)
|
|
12473
|
-
return false;
|
|
12474
|
-
if (filter.plan_id !== undefined && task.plan_id !== filter.plan_id)
|
|
12475
|
-
return false;
|
|
12476
|
-
if (filter.task_list_id !== undefined && task.task_list_id !== filter.task_list_id)
|
|
12477
|
-
return false;
|
|
12478
|
-
if (filter.status !== undefined && !matchesOne(task.status, filter.status))
|
|
12479
|
-
return false;
|
|
12480
|
-
if (filter.priority !== undefined && !matchesOne(task.priority, filter.priority))
|
|
12481
|
-
return false;
|
|
12482
|
-
if (filter.assigned_to !== undefined && task.assigned_to !== filter.assigned_to)
|
|
12483
|
-
return false;
|
|
12484
|
-
if (filter.agent_id !== undefined && task.agent_id !== filter.agent_id)
|
|
12485
|
-
return false;
|
|
12486
|
-
if (filter.session_id !== undefined && task.session_id !== filter.session_id)
|
|
12487
|
-
return false;
|
|
12488
|
-
if (filter.tags?.length && !filter.tags.every((tag) => task.tags.includes(tag)))
|
|
12489
|
-
return false;
|
|
12490
|
-
if (filter.has_recurrence !== undefined && Boolean(task.recurrence_rule) !== filter.has_recurrence)
|
|
12491
|
-
return false;
|
|
12492
|
-
if (filter.include_subtasks !== true && task.parent_id)
|
|
12493
|
-
return false;
|
|
12494
|
-
if (filter.task_type !== undefined && !matchesOne(task.task_type ?? "", filter.task_type))
|
|
12495
|
-
return false;
|
|
12496
|
-
return true;
|
|
12497
|
-
}
|
|
12498
|
-
function matchesOne(value, expected) {
|
|
12499
|
-
return Array.isArray(expected) ? expected.includes(value) : value === expected;
|
|
12500
|
-
}
|
|
12501
|
-
function priorityRank(priority) {
|
|
12502
|
-
return { critical: 0, high: 1, medium: 2, low: 3 }[priority];
|
|
12503
|
-
}
|
|
12504
12679
|
function slugifyRaw(value) {
|
|
12505
12680
|
return value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
12506
12681
|
}
|
|
@@ -12560,11 +12735,424 @@ function numberValue2(value) {
|
|
|
12560
12735
|
return typeof value === "number" && Number.isSafeInteger(value) ? value : null;
|
|
12561
12736
|
}
|
|
12562
12737
|
|
|
12738
|
+
// src/storage/shadow.ts
|
|
12739
|
+
var SNAPSHOT_TO_OBJECT_TYPE = {
|
|
12740
|
+
tasks: "tasks",
|
|
12741
|
+
projects: "projects",
|
|
12742
|
+
plans: "plans",
|
|
12743
|
+
agents: "agents",
|
|
12744
|
+
taskLists: "task_lists",
|
|
12745
|
+
templates: "templates",
|
|
12746
|
+
auditHistory: "audit_history"
|
|
12747
|
+
};
|
|
12748
|
+
|
|
12749
|
+
class TodosShadowMirror {
|
|
12750
|
+
queue = [];
|
|
12751
|
+
syncStore;
|
|
12752
|
+
maxRetries;
|
|
12753
|
+
retryBaseMs;
|
|
12754
|
+
onEvent;
|
|
12755
|
+
schemaReady;
|
|
12756
|
+
pumping = false;
|
|
12757
|
+
scheduledRetries = 0;
|
|
12758
|
+
idleResolvers = [];
|
|
12759
|
+
metrics = {
|
|
12760
|
+
enqueued: 0,
|
|
12761
|
+
mirrored: 0,
|
|
12762
|
+
retries: 0,
|
|
12763
|
+
failed: 0,
|
|
12764
|
+
lastMirrorAt: null,
|
|
12765
|
+
lastLagMs: null,
|
|
12766
|
+
lastError: null
|
|
12767
|
+
};
|
|
12768
|
+
constructor(options) {
|
|
12769
|
+
const syncStore = options.syncStore ?? (options.postgresClient ? createPostgresTodosSyncStore(options.postgresClient, {
|
|
12770
|
+
...options.sourceMachineId ? { sourceMachineId: options.sourceMachineId } : {}
|
|
12771
|
+
}) : null);
|
|
12772
|
+
if (!syncStore) {
|
|
12773
|
+
throw new Error("shadow mirror requires a Postgres sync store or query client");
|
|
12774
|
+
}
|
|
12775
|
+
this.syncStore = syncStore;
|
|
12776
|
+
this.maxRetries = Math.max(0, options.maxRetries ?? 5);
|
|
12777
|
+
this.retryBaseMs = Math.max(1, options.retryBaseMs ?? 250);
|
|
12778
|
+
if (options.onEvent)
|
|
12779
|
+
this.onEvent = options.onEvent;
|
|
12780
|
+
this.schemaReady = options.ensureSchema === false ? Promise.resolve() : null;
|
|
12781
|
+
}
|
|
12782
|
+
getMetrics() {
|
|
12783
|
+
return {
|
|
12784
|
+
enabled: true,
|
|
12785
|
+
enqueued: this.metrics.enqueued,
|
|
12786
|
+
mirrored: this.metrics.mirrored,
|
|
12787
|
+
retries: this.metrics.retries,
|
|
12788
|
+
failed: this.metrics.failed,
|
|
12789
|
+
divergence: this.metrics.failed + this.queue.length + this.scheduledRetries,
|
|
12790
|
+
pending: this.queue.length + this.scheduledRetries,
|
|
12791
|
+
inFlight: this.pumping || this.scheduledRetries > 0,
|
|
12792
|
+
lastMirrorAt: this.metrics.lastMirrorAt,
|
|
12793
|
+
lastLagMs: this.metrics.lastLagMs,
|
|
12794
|
+
lastError: this.metrics.lastError
|
|
12795
|
+
};
|
|
12796
|
+
}
|
|
12797
|
+
enqueueUpsert(key, record, context) {
|
|
12798
|
+
if (!record || typeof record !== "object")
|
|
12799
|
+
return;
|
|
12800
|
+
const payload = record;
|
|
12801
|
+
if (typeof payload["id"] !== "string" || !payload["id"])
|
|
12802
|
+
return;
|
|
12803
|
+
this.metrics.enqueued += 1;
|
|
12804
|
+
this.queue.push({
|
|
12805
|
+
kind: "upsert",
|
|
12806
|
+
key,
|
|
12807
|
+
record: payload,
|
|
12808
|
+
...context ? { context } : {},
|
|
12809
|
+
enqueuedAt: Date.now(),
|
|
12810
|
+
attempts: 0
|
|
12811
|
+
});
|
|
12812
|
+
this.pump();
|
|
12813
|
+
}
|
|
12814
|
+
enqueueDelete(objectType, id, context) {
|
|
12815
|
+
if (!id)
|
|
12816
|
+
return;
|
|
12817
|
+
this.metrics.enqueued += 1;
|
|
12818
|
+
this.queue.push({
|
|
12819
|
+
kind: "delete",
|
|
12820
|
+
objectType,
|
|
12821
|
+
id,
|
|
12822
|
+
...context ? { context } : {},
|
|
12823
|
+
enqueuedAt: Date.now(),
|
|
12824
|
+
attempts: 0
|
|
12825
|
+
});
|
|
12826
|
+
this.pump();
|
|
12827
|
+
}
|
|
12828
|
+
async flush() {
|
|
12829
|
+
if (this.idle())
|
|
12830
|
+
return;
|
|
12831
|
+
await new Promise((resolve8) => {
|
|
12832
|
+
this.idleResolvers.push(resolve8);
|
|
12833
|
+
this.pump();
|
|
12834
|
+
});
|
|
12835
|
+
}
|
|
12836
|
+
idle() {
|
|
12837
|
+
return this.queue.length === 0 && !this.pumping && this.scheduledRetries === 0;
|
|
12838
|
+
}
|
|
12839
|
+
notifyIdle() {
|
|
12840
|
+
if (!this.idle())
|
|
12841
|
+
return;
|
|
12842
|
+
const resolvers = this.idleResolvers;
|
|
12843
|
+
this.idleResolvers = [];
|
|
12844
|
+
for (const resolve8 of resolvers)
|
|
12845
|
+
resolve8();
|
|
12846
|
+
}
|
|
12847
|
+
pump() {
|
|
12848
|
+
if (this.pumping)
|
|
12849
|
+
return;
|
|
12850
|
+
this.pumping = true;
|
|
12851
|
+
this.drain();
|
|
12852
|
+
}
|
|
12853
|
+
async drain() {
|
|
12854
|
+
try {
|
|
12855
|
+
while (this.queue.length > 0) {
|
|
12856
|
+
const op = this.queue.shift();
|
|
12857
|
+
await this.process(op);
|
|
12858
|
+
}
|
|
12859
|
+
} finally {
|
|
12860
|
+
this.pumping = false;
|
|
12861
|
+
this.notifyIdle();
|
|
12862
|
+
}
|
|
12863
|
+
}
|
|
12864
|
+
async process(op) {
|
|
12865
|
+
try {
|
|
12866
|
+
await this.ensureSchema();
|
|
12867
|
+
await this.push(op);
|
|
12868
|
+
const objectType = op.kind === "upsert" ? SNAPSHOT_TO_OBJECT_TYPE[op.key] : op.objectType;
|
|
12869
|
+
const id = op.kind === "upsert" ? String(op.record["id"]) : op.id;
|
|
12870
|
+
const lagMs = Date.now() - op.enqueuedAt;
|
|
12871
|
+
this.metrics.mirrored += 1;
|
|
12872
|
+
this.metrics.lastMirrorAt = new Date().toISOString();
|
|
12873
|
+
this.metrics.lastLagMs = lagMs;
|
|
12874
|
+
this.onEvent?.({ type: "mirrored", objectType, id, lagMs });
|
|
12875
|
+
} catch (error) {
|
|
12876
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
12877
|
+
const objectType = op.kind === "upsert" ? SNAPSHOT_TO_OBJECT_TYPE[op.key] : op.objectType;
|
|
12878
|
+
const id = op.kind === "upsert" ? String(op.record["id"]) : op.id;
|
|
12879
|
+
this.metrics.retries += 1;
|
|
12880
|
+
this.metrics.lastError = message;
|
|
12881
|
+
op.attempts += 1;
|
|
12882
|
+
if (op.attempts <= this.maxRetries) {
|
|
12883
|
+
this.onEvent?.({ type: "retry", objectType, id, attempt: op.attempts, error: message });
|
|
12884
|
+
const delay = this.retryBaseMs * 2 ** (op.attempts - 1);
|
|
12885
|
+
this.scheduledRetries += 1;
|
|
12886
|
+
setTimeout(() => {
|
|
12887
|
+
this.scheduledRetries -= 1;
|
|
12888
|
+
this.queue.push(op);
|
|
12889
|
+
this.pump();
|
|
12890
|
+
}, delay);
|
|
12891
|
+
return;
|
|
12892
|
+
}
|
|
12893
|
+
this.metrics.failed += 1;
|
|
12894
|
+
this.onEvent?.({ type: "dropped", objectType, id, error: message });
|
|
12895
|
+
}
|
|
12896
|
+
}
|
|
12897
|
+
ensureSchema() {
|
|
12898
|
+
if (!this.schemaReady) {
|
|
12899
|
+
this.schemaReady = this.syncStore.ensureSchema().catch((error) => {
|
|
12900
|
+
this.schemaReady = null;
|
|
12901
|
+
throw error;
|
|
12902
|
+
});
|
|
12903
|
+
}
|
|
12904
|
+
return this.schemaReady;
|
|
12905
|
+
}
|
|
12906
|
+
async push(op) {
|
|
12907
|
+
const snapshot = emptySnapshot();
|
|
12908
|
+
const context = op.context ?? {};
|
|
12909
|
+
if (op.kind === "upsert") {
|
|
12910
|
+
snapshot[op.key].push(op.record);
|
|
12911
|
+
} else {
|
|
12912
|
+
const now3 = new Date().toISOString();
|
|
12913
|
+
const tombstone = {
|
|
12914
|
+
object_type: op.objectType,
|
|
12915
|
+
object_id: op.id,
|
|
12916
|
+
deleted_at: now3,
|
|
12917
|
+
updated_at: now3,
|
|
12918
|
+
payload: { id: op.id, deleted_at: now3 },
|
|
12919
|
+
version: null
|
|
12920
|
+
};
|
|
12921
|
+
snapshot.tombstones = [tombstone];
|
|
12922
|
+
}
|
|
12923
|
+
await this.syncStore.pushSnapshot(snapshot, context);
|
|
12924
|
+
}
|
|
12925
|
+
}
|
|
12926
|
+
function emptySnapshot() {
|
|
12927
|
+
return {
|
|
12928
|
+
exportedAt: new Date().toISOString(),
|
|
12929
|
+
source: "sqlite",
|
|
12930
|
+
tasks: [],
|
|
12931
|
+
projects: [],
|
|
12932
|
+
projectMachinePaths: [],
|
|
12933
|
+
plans: [],
|
|
12934
|
+
agents: [],
|
|
12935
|
+
taskLists: [],
|
|
12936
|
+
templates: [],
|
|
12937
|
+
auditHistory: [],
|
|
12938
|
+
tombstones: []
|
|
12939
|
+
};
|
|
12940
|
+
}
|
|
12941
|
+
function createShadowTodosStorageAdapter(options) {
|
|
12942
|
+
const local = options.localAdapter ?? createLocalSqliteTodosStorageAdapter(options.local);
|
|
12943
|
+
const mirror = new TodosShadowMirror(options);
|
|
12944
|
+
const adapter = {
|
|
12945
|
+
...local,
|
|
12946
|
+
kind: "sqlite",
|
|
12947
|
+
capabilities: { ...local.capabilities, remotePersistence: false, sync: true },
|
|
12948
|
+
tasks: {
|
|
12949
|
+
...local.tasks,
|
|
12950
|
+
async create(input, context) {
|
|
12951
|
+
const task = await local.tasks.create(input, context);
|
|
12952
|
+
mirror.enqueueUpsert("tasks", task, context);
|
|
12953
|
+
return task;
|
|
12954
|
+
},
|
|
12955
|
+
async update(id, input, context) {
|
|
12956
|
+
const task = await local.tasks.update(id, input, context);
|
|
12957
|
+
mirror.enqueueUpsert("tasks", task, context);
|
|
12958
|
+
return task;
|
|
12959
|
+
},
|
|
12960
|
+
async start(id, agentId, context) {
|
|
12961
|
+
const task = await local.tasks.start(id, agentId, context);
|
|
12962
|
+
mirror.enqueueUpsert("tasks", task, context);
|
|
12963
|
+
return task;
|
|
12964
|
+
},
|
|
12965
|
+
async complete(id, agentId, opts, context) {
|
|
12966
|
+
const task = await local.tasks.complete(id, agentId, opts, context);
|
|
12967
|
+
mirror.enqueueUpsert("tasks", task, context);
|
|
12968
|
+
return task;
|
|
12969
|
+
},
|
|
12970
|
+
async fail(id, agentId, reason, opts, context) {
|
|
12971
|
+
const result = await local.tasks.fail(id, agentId, reason, opts, context);
|
|
12972
|
+
mirror.enqueueUpsert("tasks", result.task, context);
|
|
12973
|
+
if (result.retryTask)
|
|
12974
|
+
mirror.enqueueUpsert("tasks", result.retryTask, context);
|
|
12975
|
+
return result;
|
|
12976
|
+
},
|
|
12977
|
+
async claimNext(agentId, filters, context) {
|
|
12978
|
+
const task = await local.tasks.claimNext(agentId, filters, context);
|
|
12979
|
+
if (task)
|
|
12980
|
+
mirror.enqueueUpsert("tasks", task, context);
|
|
12981
|
+
return task;
|
|
12982
|
+
},
|
|
12983
|
+
async delete(id, context) {
|
|
12984
|
+
const deleted = await local.tasks.delete(id, context);
|
|
12985
|
+
if (deleted)
|
|
12986
|
+
mirror.enqueueDelete("tasks", id, context);
|
|
12987
|
+
return deleted;
|
|
12988
|
+
}
|
|
12989
|
+
},
|
|
12990
|
+
projects: {
|
|
12991
|
+
...local.projects,
|
|
12992
|
+
async create(input, context) {
|
|
12993
|
+
const project = await local.projects.create(input, context);
|
|
12994
|
+
mirror.enqueueUpsert("projects", project, context);
|
|
12995
|
+
return project;
|
|
12996
|
+
},
|
|
12997
|
+
async update(id, input, context) {
|
|
12998
|
+
const project = await local.projects.update(id, input, context);
|
|
12999
|
+
mirror.enqueueUpsert("projects", project, context);
|
|
13000
|
+
return project;
|
|
13001
|
+
},
|
|
13002
|
+
async delete(id, context) {
|
|
13003
|
+
const deleted = await local.projects.delete(id, context);
|
|
13004
|
+
if (deleted)
|
|
13005
|
+
mirror.enqueueDelete("projects", id, context);
|
|
13006
|
+
return deleted;
|
|
13007
|
+
}
|
|
13008
|
+
},
|
|
13009
|
+
plans: {
|
|
13010
|
+
...local.plans,
|
|
13011
|
+
async create(input, context) {
|
|
13012
|
+
const plan = await local.plans.create(input, context);
|
|
13013
|
+
mirror.enqueueUpsert("plans", plan, context);
|
|
13014
|
+
return plan;
|
|
13015
|
+
},
|
|
13016
|
+
async update(id, input, context) {
|
|
13017
|
+
const plan = await local.plans.update(id, input, context);
|
|
13018
|
+
mirror.enqueueUpsert("plans", plan, context);
|
|
13019
|
+
return plan;
|
|
13020
|
+
},
|
|
13021
|
+
async delete(id, context) {
|
|
13022
|
+
const deleted = await local.plans.delete(id, context);
|
|
13023
|
+
if (deleted)
|
|
13024
|
+
mirror.enqueueDelete("plans", id, context);
|
|
13025
|
+
return deleted;
|
|
13026
|
+
}
|
|
13027
|
+
},
|
|
13028
|
+
agents: {
|
|
13029
|
+
...local.agents,
|
|
13030
|
+
async register(input, context) {
|
|
13031
|
+
const result = await local.agents.register(input, context);
|
|
13032
|
+
if (isAgent(result))
|
|
13033
|
+
mirror.enqueueUpsert("agents", result, context);
|
|
13034
|
+
return result;
|
|
13035
|
+
},
|
|
13036
|
+
async update(id, input, context) {
|
|
13037
|
+
const agent = await local.agents.update(id, input, context);
|
|
13038
|
+
if (agent)
|
|
13039
|
+
mirror.enqueueUpsert("agents", agent, context);
|
|
13040
|
+
return agent;
|
|
13041
|
+
}
|
|
13042
|
+
},
|
|
13043
|
+
taskLists: {
|
|
13044
|
+
...local.taskLists,
|
|
13045
|
+
async create(input, context) {
|
|
13046
|
+
const list = await local.taskLists.create(input, context);
|
|
13047
|
+
mirror.enqueueUpsert("taskLists", list, context);
|
|
13048
|
+
return list;
|
|
13049
|
+
},
|
|
13050
|
+
async update(id, input, context) {
|
|
13051
|
+
const list = await local.taskLists.update(id, input, context);
|
|
13052
|
+
mirror.enqueueUpsert("taskLists", list, context);
|
|
13053
|
+
return list;
|
|
13054
|
+
},
|
|
13055
|
+
async delete(id, context) {
|
|
13056
|
+
const deleted = await local.taskLists.delete(id, context);
|
|
13057
|
+
if (deleted)
|
|
13058
|
+
mirror.enqueueDelete("task_lists", id, context);
|
|
13059
|
+
return deleted;
|
|
13060
|
+
}
|
|
13061
|
+
},
|
|
13062
|
+
templates: {
|
|
13063
|
+
...local.templates,
|
|
13064
|
+
async create(input, context) {
|
|
13065
|
+
const template = await local.templates.create(input, context);
|
|
13066
|
+
mirror.enqueueUpsert("templates", template, context);
|
|
13067
|
+
return template;
|
|
13068
|
+
},
|
|
13069
|
+
async update(id, input, context) {
|
|
13070
|
+
const template = await local.templates.update(id, input, context);
|
|
13071
|
+
if (template)
|
|
13072
|
+
mirror.enqueueUpsert("templates", template, context);
|
|
13073
|
+
return template;
|
|
13074
|
+
},
|
|
13075
|
+
async delete(id, context) {
|
|
13076
|
+
const deleted = await local.templates.delete(id, context);
|
|
13077
|
+
if (deleted)
|
|
13078
|
+
mirror.enqueueDelete("templates", id, context);
|
|
13079
|
+
return deleted;
|
|
13080
|
+
}
|
|
13081
|
+
},
|
|
13082
|
+
audit: {
|
|
13083
|
+
...local.audit,
|
|
13084
|
+
async logTaskChange(taskId, action, field, oldValue, newValue, agentId, context) {
|
|
13085
|
+
const history = await local.audit.logTaskChange(taskId, action, field, oldValue, newValue, agentId, context);
|
|
13086
|
+
mirror.enqueueUpsert("auditHistory", history, context);
|
|
13087
|
+
return history;
|
|
13088
|
+
}
|
|
13089
|
+
},
|
|
13090
|
+
shadow: mirror
|
|
13091
|
+
};
|
|
13092
|
+
if (adapter.transaction) {
|
|
13093
|
+
const localTransaction = local.transaction;
|
|
13094
|
+
adapter.transaction = (fn, context) => localTransaction(() => fn(adapter), context);
|
|
13095
|
+
}
|
|
13096
|
+
return adapter;
|
|
13097
|
+
}
|
|
13098
|
+
function isAgent(value) {
|
|
13099
|
+
return !value.conflict;
|
|
13100
|
+
}
|
|
13101
|
+
|
|
13102
|
+
// src/storage/cloud-client.ts
|
|
13103
|
+
init_config();
|
|
13104
|
+
function resolveBunSql() {
|
|
13105
|
+
const runtime = globalThis.Bun;
|
|
13106
|
+
const ctor = runtime?.SQL;
|
|
13107
|
+
if (typeof ctor !== "function") {
|
|
13108
|
+
throw new Error("Live Postgres access requires the Bun runtime (Bun.SQL). Run todos under bun, or inject a TodosPostgresQueryClient.");
|
|
13109
|
+
}
|
|
13110
|
+
return ctor;
|
|
13111
|
+
}
|
|
13112
|
+
function toRows(result) {
|
|
13113
|
+
if (Array.isArray(result))
|
|
13114
|
+
return result;
|
|
13115
|
+
if (result && typeof result === "object" && Array.isArray(result.rows)) {
|
|
13116
|
+
return result.rows;
|
|
13117
|
+
}
|
|
13118
|
+
return [];
|
|
13119
|
+
}
|
|
13120
|
+
function createTodosCloudQueryClient(url, options = {}) {
|
|
13121
|
+
const SQL = resolveBunSql();
|
|
13122
|
+
const sql = new SQL(url, {
|
|
13123
|
+
max: options.max ?? 2,
|
|
13124
|
+
idleTimeout: options.idleTimeout ?? 20,
|
|
13125
|
+
connectionTimeout: options.connectionTimeout ?? 15
|
|
13126
|
+
});
|
|
13127
|
+
return {
|
|
13128
|
+
async query(text, values = []) {
|
|
13129
|
+
const result = await sql.unsafe(text, values.length ? [...values] : []);
|
|
13130
|
+
return { rows: toRows(result) };
|
|
13131
|
+
},
|
|
13132
|
+
async close() {
|
|
13133
|
+
if (typeof sql.end === "function")
|
|
13134
|
+
await sql.end();
|
|
13135
|
+
else if (typeof sql.close === "function")
|
|
13136
|
+
await sql.close();
|
|
13137
|
+
}
|
|
13138
|
+
};
|
|
13139
|
+
}
|
|
13140
|
+
function createTodosCloudQueryClientFromEnv(env = process.env, options = {}) {
|
|
13141
|
+
const url = getTodosStorageDatabaseUrl(env);
|
|
13142
|
+
if (!url)
|
|
13143
|
+
return null;
|
|
13144
|
+
return createTodosCloudQueryClient(url, options);
|
|
13145
|
+
}
|
|
13146
|
+
|
|
12563
13147
|
// src/storage/factory.ts
|
|
13148
|
+
init_config();
|
|
12564
13149
|
function createTodosStorageAdapter(options = {}) {
|
|
12565
13150
|
const config = options.config ?? loadTodosStorageConfig(options.env);
|
|
12566
|
-
if (config.mode === "local")
|
|
13151
|
+
if (config.mode === "local") {
|
|
13152
|
+
if (isTodosShadowEnabled(options.env))
|
|
13153
|
+
return createShadowAdapter(options, config);
|
|
12567
13154
|
return createLocalSqliteTodosStorageAdapter(options.local);
|
|
13155
|
+
}
|
|
12568
13156
|
assertTodosRemoteStorageConfig(config);
|
|
12569
13157
|
const adapter = config.mode === "hybrid" ? options.hybridAdapter ?? createImplicitHybridAdapter(options) ?? options.remoteAdapter : options.remoteAdapter ?? createImplicitPostgresAdapter(options);
|
|
12570
13158
|
if (!adapter) {
|
|
@@ -12573,6 +13161,20 @@ function createTodosStorageAdapter(options = {}) {
|
|
|
12573
13161
|
assertRemoteAdapterCapabilities(adapter, config.mode);
|
|
12574
13162
|
return adapter;
|
|
12575
13163
|
}
|
|
13164
|
+
function createShadowAdapter(options, config) {
|
|
13165
|
+
assertTodosShadowConfig(config, options.env);
|
|
13166
|
+
const postgresClient = options.postgresClient ?? createTodosCloudQueryClientFromEnv(options.env);
|
|
13167
|
+
if (!options.postgresSyncStore && !postgresClient) {
|
|
13168
|
+
throw new Error("shadow mirror requires a remote database URL or an injected Postgres client");
|
|
13169
|
+
}
|
|
13170
|
+
return createShadowTodosStorageAdapter({
|
|
13171
|
+
...options.shadow ?? {},
|
|
13172
|
+
...options.local ? { local: options.local } : {},
|
|
13173
|
+
...options.postgresSyncStore ? { syncStore: options.postgresSyncStore } : {},
|
|
13174
|
+
...postgresClient ? { postgresClient } : {},
|
|
13175
|
+
...options.hybrid?.sourceMachineId ? { sourceMachineId: options.hybrid.sourceMachineId } : {}
|
|
13176
|
+
});
|
|
13177
|
+
}
|
|
12576
13178
|
function createImplicitPostgresAdapter(options) {
|
|
12577
13179
|
if (!options.postgresClient)
|
|
12578
13180
|
return null;
|
|
@@ -12599,6 +13201,326 @@ function assertRemoteAdapterCapabilities(adapter, mode) {
|
|
|
12599
13201
|
throw new Error("hybrid storage adapter must also set capabilities.localPersistence=true");
|
|
12600
13202
|
}
|
|
12601
13203
|
}
|
|
13204
|
+
// src/storage/shadow-outbox.ts
|
|
13205
|
+
init_shadow_outbox_schema();
|
|
13206
|
+
init_shadow_outbox_schema();
|
|
13207
|
+
var MAX_BACKOFF_MS = 5 * 60000;
|
|
13208
|
+
|
|
13209
|
+
class TodosShadowOutbox {
|
|
13210
|
+
db;
|
|
13211
|
+
syncStore;
|
|
13212
|
+
local;
|
|
13213
|
+
maxRetries;
|
|
13214
|
+
retryBaseMs;
|
|
13215
|
+
batchSize;
|
|
13216
|
+
onEvent;
|
|
13217
|
+
schemaReady;
|
|
13218
|
+
loopTimer = null;
|
|
13219
|
+
draining = false;
|
|
13220
|
+
metrics = {
|
|
13221
|
+
mirrored: 0,
|
|
13222
|
+
retries: 0,
|
|
13223
|
+
lastMirrorAt: null,
|
|
13224
|
+
lastLagMs: null,
|
|
13225
|
+
lastError: null
|
|
13226
|
+
};
|
|
13227
|
+
constructor(options) {
|
|
13228
|
+
this.db = options.db;
|
|
13229
|
+
const syncStore = options.syncStore ?? (options.postgresClient ? createPostgresTodosSyncStore(options.postgresClient, {
|
|
13230
|
+
...options.sourceMachineId ? { sourceMachineId: options.sourceMachineId } : {}
|
|
13231
|
+
}) : null);
|
|
13232
|
+
if (!syncStore) {
|
|
13233
|
+
throw new Error("shadow outbox requires a Postgres sync store or query client");
|
|
13234
|
+
}
|
|
13235
|
+
this.syncStore = syncStore;
|
|
13236
|
+
this.local = options.localAdapter ?? createLocalSqliteTodosStorageAdapter({ db: options.db });
|
|
13237
|
+
this.maxRetries = Math.max(0, options.maxRetries ?? 8);
|
|
13238
|
+
this.retryBaseMs = Math.max(1, options.retryBaseMs ?? 500);
|
|
13239
|
+
this.batchSize = Math.max(1, options.batchSize ?? 50);
|
|
13240
|
+
if (options.onEvent)
|
|
13241
|
+
this.onEvent = options.onEvent;
|
|
13242
|
+
this.schemaReady = options.ensureSchema === false ? Promise.resolve() : null;
|
|
13243
|
+
}
|
|
13244
|
+
install() {
|
|
13245
|
+
installShadowOutboxSchema(this.db);
|
|
13246
|
+
}
|
|
13247
|
+
getStats() {
|
|
13248
|
+
const pending = this.countByStatus("pending");
|
|
13249
|
+
const failed = this.countByStatus("failed");
|
|
13250
|
+
return {
|
|
13251
|
+
enabled: true,
|
|
13252
|
+
durable: true,
|
|
13253
|
+
pending,
|
|
13254
|
+
failed,
|
|
13255
|
+
depth: pending + failed,
|
|
13256
|
+
mirrored: this.metrics.mirrored,
|
|
13257
|
+
retries: this.metrics.retries,
|
|
13258
|
+
lastMirrorAt: this.metrics.lastMirrorAt,
|
|
13259
|
+
lastLagMs: this.metrics.lastLagMs,
|
|
13260
|
+
lastError: this.metrics.lastError,
|
|
13261
|
+
draining: this.loopTimer !== null
|
|
13262
|
+
};
|
|
13263
|
+
}
|
|
13264
|
+
countByStatus(status) {
|
|
13265
|
+
const row = this.db.query(`SELECT count(*) AS n FROM shadow_outbox WHERE status=?`).get(status);
|
|
13266
|
+
return row?.n ?? 0;
|
|
13267
|
+
}
|
|
13268
|
+
startLoop(intervalMs = 2000) {
|
|
13269
|
+
if (this.loopTimer)
|
|
13270
|
+
return;
|
|
13271
|
+
this.loopTimer = setInterval(() => {
|
|
13272
|
+
this.drainOnce().catch(() => {});
|
|
13273
|
+
}, Math.max(250, intervalMs));
|
|
13274
|
+
if (typeof this.loopTimer.unref === "function")
|
|
13275
|
+
this.loopTimer.unref();
|
|
13276
|
+
}
|
|
13277
|
+
stopLoop() {
|
|
13278
|
+
if (this.loopTimer) {
|
|
13279
|
+
clearInterval(this.loopTimer);
|
|
13280
|
+
this.loopTimer = null;
|
|
13281
|
+
}
|
|
13282
|
+
}
|
|
13283
|
+
async flush(timeoutMs = 15000) {
|
|
13284
|
+
const deadline = Date.now() + Math.max(0, timeoutMs);
|
|
13285
|
+
while (Date.now() < deadline) {
|
|
13286
|
+
const processed = await this.drainOnce();
|
|
13287
|
+
if (this.countByStatus("pending") === 0)
|
|
13288
|
+
break;
|
|
13289
|
+
if (processed === 0) {
|
|
13290
|
+
const remaining = deadline - Date.now();
|
|
13291
|
+
if (remaining <= 0)
|
|
13292
|
+
break;
|
|
13293
|
+
await new Promise((resolve8) => setTimeout(resolve8, Math.min(200, remaining)));
|
|
13294
|
+
}
|
|
13295
|
+
}
|
|
13296
|
+
return this.getStats();
|
|
13297
|
+
}
|
|
13298
|
+
async drainOnce() {
|
|
13299
|
+
if (this.draining)
|
|
13300
|
+
return 0;
|
|
13301
|
+
this.draining = true;
|
|
13302
|
+
try {
|
|
13303
|
+
await this.ensureSchema();
|
|
13304
|
+
const now3 = Date.now();
|
|
13305
|
+
const rows = this.db.query(`SELECT seq, object_type, object_id, op, enqueued_at, attempts, revision
|
|
13306
|
+
FROM shadow_outbox
|
|
13307
|
+
WHERE status='pending' AND next_attempt_at <= ?
|
|
13308
|
+
ORDER BY seq ASC
|
|
13309
|
+
LIMIT ?`).all(now3, this.batchSize);
|
|
13310
|
+
if (rows.length === 0)
|
|
13311
|
+
return 0;
|
|
13312
|
+
for (const row of rows) {
|
|
13313
|
+
await this.processRow(row);
|
|
13314
|
+
}
|
|
13315
|
+
return rows.length;
|
|
13316
|
+
} catch {
|
|
13317
|
+
return 0;
|
|
13318
|
+
} finally {
|
|
13319
|
+
this.draining = false;
|
|
13320
|
+
}
|
|
13321
|
+
}
|
|
13322
|
+
async processRow(row) {
|
|
13323
|
+
try {
|
|
13324
|
+
const snapshot = await this.buildSnapshot(row);
|
|
13325
|
+
await this.syncStore.pushSnapshot(snapshot, {});
|
|
13326
|
+
const lagMs = Date.now() - row.enqueued_at;
|
|
13327
|
+
this.db.run(`DELETE FROM shadow_outbox WHERE seq=? AND revision=?`, [row.seq, row.revision]);
|
|
13328
|
+
this.metrics.mirrored += 1;
|
|
13329
|
+
this.metrics.lastMirrorAt = new Date().toISOString();
|
|
13330
|
+
this.metrics.lastLagMs = lagMs;
|
|
13331
|
+
this.onEvent?.({ type: "mirrored", objectType: row.object_type, id: row.object_id, lagMs });
|
|
13332
|
+
} catch (error) {
|
|
13333
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
13334
|
+
this.metrics.retries += 1;
|
|
13335
|
+
this.metrics.lastError = message;
|
|
13336
|
+
const attempts = row.attempts + 1;
|
|
13337
|
+
if (attempts > this.maxRetries) {
|
|
13338
|
+
this.db.run(`UPDATE shadow_outbox SET attempts=?, last_error=?, status='failed' WHERE seq=? AND revision=?`, [attempts, message, row.seq, row.revision]);
|
|
13339
|
+
this.onEvent?.({ type: "parked", objectType: row.object_type, id: row.object_id, error: message });
|
|
13340
|
+
return;
|
|
13341
|
+
}
|
|
13342
|
+
const delay = Math.min(MAX_BACKOFF_MS, this.retryBaseMs * 2 ** (attempts - 1));
|
|
13343
|
+
this.db.run(`UPDATE shadow_outbox SET attempts=?, next_attempt_at=?, last_error=? WHERE seq=? AND revision=?`, [attempts, Date.now() + delay, message, row.seq, row.revision]);
|
|
13344
|
+
this.onEvent?.({ type: "retry", objectType: row.object_type, id: row.object_id, attempt: attempts, error: message });
|
|
13345
|
+
}
|
|
13346
|
+
}
|
|
13347
|
+
async buildSnapshot(row) {
|
|
13348
|
+
const snapshot = emptySnapshot2();
|
|
13349
|
+
if (row.op === "delete") {
|
|
13350
|
+
snapshot.tombstones = [this.tombstone(row.object_type, row.object_id)];
|
|
13351
|
+
return snapshot;
|
|
13352
|
+
}
|
|
13353
|
+
const record = await this.readCurrent(row.object_type, row.object_id);
|
|
13354
|
+
if (!record) {
|
|
13355
|
+
snapshot.tombstones = [this.tombstone(row.object_type, row.object_id)];
|
|
13356
|
+
return snapshot;
|
|
13357
|
+
}
|
|
13358
|
+
switch (row.object_type) {
|
|
13359
|
+
case "tasks":
|
|
13360
|
+
snapshot.tasks.push(record);
|
|
13361
|
+
break;
|
|
13362
|
+
case "projects":
|
|
13363
|
+
snapshot.projects.push(record);
|
|
13364
|
+
break;
|
|
13365
|
+
case "project_machine_paths":
|
|
13366
|
+
snapshot.projectMachinePaths?.push(record);
|
|
13367
|
+
break;
|
|
13368
|
+
case "plans":
|
|
13369
|
+
snapshot.plans.push(record);
|
|
13370
|
+
break;
|
|
13371
|
+
case "agents":
|
|
13372
|
+
snapshot.agents.push(record);
|
|
13373
|
+
break;
|
|
13374
|
+
case "task_lists":
|
|
13375
|
+
snapshot.taskLists.push(record);
|
|
13376
|
+
break;
|
|
13377
|
+
case "templates":
|
|
13378
|
+
snapshot.templates.push(record);
|
|
13379
|
+
break;
|
|
13380
|
+
case "audit_history":
|
|
13381
|
+
snapshot.auditHistory.push(record);
|
|
13382
|
+
break;
|
|
13383
|
+
default:
|
|
13384
|
+
break;
|
|
13385
|
+
}
|
|
13386
|
+
return snapshot;
|
|
13387
|
+
}
|
|
13388
|
+
async readCurrent(objectType, id) {
|
|
13389
|
+
switch (objectType) {
|
|
13390
|
+
case "tasks":
|
|
13391
|
+
return await this.local.tasks.get(id);
|
|
13392
|
+
case "projects":
|
|
13393
|
+
return await this.local.projects.get(id);
|
|
13394
|
+
case "project_machine_paths": {
|
|
13395
|
+
const raw = this.db.query(`SELECT * FROM project_machine_paths WHERE id=?`).get(id);
|
|
13396
|
+
return raw ?? null;
|
|
13397
|
+
}
|
|
13398
|
+
case "plans":
|
|
13399
|
+
return await this.local.plans.get(id);
|
|
13400
|
+
case "agents":
|
|
13401
|
+
return await this.local.agents.get(id);
|
|
13402
|
+
case "task_lists":
|
|
13403
|
+
return await this.local.taskLists.get(id);
|
|
13404
|
+
case "templates":
|
|
13405
|
+
return await this.local.templates.get(id);
|
|
13406
|
+
case "audit_history": {
|
|
13407
|
+
const raw = this.db.query(`SELECT * FROM task_history WHERE id=?`).get(id);
|
|
13408
|
+
return raw ?? null;
|
|
13409
|
+
}
|
|
13410
|
+
default:
|
|
13411
|
+
return null;
|
|
13412
|
+
}
|
|
13413
|
+
}
|
|
13414
|
+
tombstone(objectType, id) {
|
|
13415
|
+
const now3 = new Date().toISOString();
|
|
13416
|
+
return {
|
|
13417
|
+
object_type: objectType,
|
|
13418
|
+
object_id: id,
|
|
13419
|
+
deleted_at: now3,
|
|
13420
|
+
updated_at: now3,
|
|
13421
|
+
payload: { id, deleted_at: now3 },
|
|
13422
|
+
version: null
|
|
13423
|
+
};
|
|
13424
|
+
}
|
|
13425
|
+
ensureSchema() {
|
|
13426
|
+
if (!this.schemaReady) {
|
|
13427
|
+
this.schemaReady = this.syncStore.ensureSchema().catch((error) => {
|
|
13428
|
+
this.schemaReady = null;
|
|
13429
|
+
throw error;
|
|
13430
|
+
});
|
|
13431
|
+
}
|
|
13432
|
+
return this.schemaReady;
|
|
13433
|
+
}
|
|
13434
|
+
}
|
|
13435
|
+
function emptySnapshot2() {
|
|
13436
|
+
return {
|
|
13437
|
+
exportedAt: new Date().toISOString(),
|
|
13438
|
+
source: "sqlite",
|
|
13439
|
+
tasks: [],
|
|
13440
|
+
projects: [],
|
|
13441
|
+
projectMachinePaths: [],
|
|
13442
|
+
plans: [],
|
|
13443
|
+
agents: [],
|
|
13444
|
+
taskLists: [],
|
|
13445
|
+
templates: [],
|
|
13446
|
+
auditHistory: [],
|
|
13447
|
+
tombstones: []
|
|
13448
|
+
};
|
|
13449
|
+
}
|
|
13450
|
+
function createTodosShadowOutbox(options) {
|
|
13451
|
+
const outbox = new TodosShadowOutbox(options);
|
|
13452
|
+
outbox.install();
|
|
13453
|
+
return outbox;
|
|
13454
|
+
}
|
|
13455
|
+
// src/storage/shadow-runtime.ts
|
|
13456
|
+
init_config();
|
|
13457
|
+
var _capturedDb = null;
|
|
13458
|
+
var _outbox = null;
|
|
13459
|
+
var _cloud = null;
|
|
13460
|
+
var _exitRegistered = false;
|
|
13461
|
+
function maybeInstallShadowCapture2(db, env = process.env) {
|
|
13462
|
+
if (!isTodosShadowEnabled(env))
|
|
13463
|
+
return false;
|
|
13464
|
+
if (_capturedDb === db)
|
|
13465
|
+
return true;
|
|
13466
|
+
installShadowOutboxSchema(db);
|
|
13467
|
+
_capturedDb = db;
|
|
13468
|
+
return true;
|
|
13469
|
+
}
|
|
13470
|
+
function getRuntimeShadowOutbox(db, env = process.env) {
|
|
13471
|
+
if (!isTodosShadowEnabled(env)) {
|
|
13472
|
+
throw new Error(`dual-write shadow is disabled: set ${getTodosStorageShadowEnvName(env)}=1 to enable it`);
|
|
13473
|
+
}
|
|
13474
|
+
if (_outbox && _capturedDb === db)
|
|
13475
|
+
return _outbox;
|
|
13476
|
+
const cloud = createTodosCloudQueryClientFromEnv(env);
|
|
13477
|
+
if (!cloud) {
|
|
13478
|
+
throw new Error(`dual-write shadow drain requires a remote Postgres DSN in ${getTodosStorageDatabaseEnv(env)}`);
|
|
13479
|
+
}
|
|
13480
|
+
_cloud = cloud;
|
|
13481
|
+
installShadowOutboxSchema(db);
|
|
13482
|
+
_capturedDb = db;
|
|
13483
|
+
_outbox = new TodosShadowOutbox({ db, postgresClient: cloud });
|
|
13484
|
+
return _outbox;
|
|
13485
|
+
}
|
|
13486
|
+
function startRuntimeShadowDrain(db, env = process.env) {
|
|
13487
|
+
if (!isTodosShadowEnabled(env))
|
|
13488
|
+
return;
|
|
13489
|
+
maybeInstallShadowCapture2(db, env);
|
|
13490
|
+
try {
|
|
13491
|
+
const outbox = getRuntimeShadowOutbox(db, env);
|
|
13492
|
+
outbox.startLoop();
|
|
13493
|
+
registerShadowExitFlush(db, env);
|
|
13494
|
+
} catch (error) {
|
|
13495
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
13496
|
+
console.error(`[todos] shadow drain not started: ${message}`);
|
|
13497
|
+
}
|
|
13498
|
+
}
|
|
13499
|
+
function registerShadowExitFlush(db, env = process.env) {
|
|
13500
|
+
if (_exitRegistered)
|
|
13501
|
+
return;
|
|
13502
|
+
if (!isTodosShadowEnabled(env))
|
|
13503
|
+
return;
|
|
13504
|
+
_exitRegistered = true;
|
|
13505
|
+
const flush = async () => {
|
|
13506
|
+
try {
|
|
13507
|
+
const outbox = getRuntimeShadowOutbox(db, env);
|
|
13508
|
+
await outbox.flush(5000);
|
|
13509
|
+
} catch {} finally {
|
|
13510
|
+
await closeRuntimeShadowCloud();
|
|
13511
|
+
}
|
|
13512
|
+
};
|
|
13513
|
+
process.once("beforeExit", () => {
|
|
13514
|
+
flush();
|
|
13515
|
+
});
|
|
13516
|
+
}
|
|
13517
|
+
async function closeRuntimeShadowCloud() {
|
|
13518
|
+
if (_cloud) {
|
|
13519
|
+
const cloud = _cloud;
|
|
13520
|
+
_cloud = null;
|
|
13521
|
+
await cloud.close().catch(() => {});
|
|
13522
|
+
}
|
|
13523
|
+
}
|
|
12602
13524
|
// src/storage/s3-artifacts.ts
|
|
12603
13525
|
import { createHash as createHash3, createHmac as createHmac2 } from "crypto";
|
|
12604
13526
|
function createTodosS3ArtifactStore(options) {
|
|
@@ -13027,14 +13949,20 @@ function mediaType(metadata) {
|
|
|
13027
13949
|
}
|
|
13028
13950
|
export {
|
|
13029
13951
|
uploadRunArtifactsToS3,
|
|
13952
|
+
startRuntimeShadowDrain,
|
|
13030
13953
|
signAwsV4Request,
|
|
13954
|
+
registerShadowExitFlush,
|
|
13031
13955
|
postgresTodosSyncSchemaSql,
|
|
13032
13956
|
planRunArtifactsS3Sync,
|
|
13033
13957
|
parseStorageMode,
|
|
13958
|
+
maybeInstallShadowCapture2 as maybeInstallShadowCapture,
|
|
13034
13959
|
loadTodosStorageConfig,
|
|
13035
13960
|
loadStorageConfig,
|
|
13961
|
+
isTodosShadowEnabled,
|
|
13036
13962
|
isTodosRemoteStorageEnabled,
|
|
13963
|
+
installShadowOutboxSchema,
|
|
13037
13964
|
importSqliteTodosStorageSnapshot,
|
|
13965
|
+
getTodosStorageShadowEnvName,
|
|
13038
13966
|
getTodosStorageMode,
|
|
13039
13967
|
getTodosStorageEnvName,
|
|
13040
13968
|
getTodosStorageDatabaseUrl,
|
|
@@ -13042,18 +13970,27 @@ export {
|
|
|
13042
13970
|
getStorageMode,
|
|
13043
13971
|
getStorageDatabaseUrl,
|
|
13044
13972
|
getStorageDatabaseEnv,
|
|
13973
|
+
getRuntimeShadowOutbox,
|
|
13045
13974
|
getCanonicalTodosRdsConfig,
|
|
13046
13975
|
exportSqliteTodosStorageSnapshot,
|
|
13047
13976
|
downloadRunArtifactsFromS3,
|
|
13048
13977
|
createTodosStorageAdapter,
|
|
13978
|
+
createTodosShadowOutbox,
|
|
13049
13979
|
createTodosS3ArtifactStore,
|
|
13980
|
+
createTodosCloudQueryClientFromEnv,
|
|
13981
|
+
createTodosCloudQueryClient,
|
|
13982
|
+
createShadowTodosStorageAdapter,
|
|
13050
13983
|
createPostgresTodosSyncStore,
|
|
13051
13984
|
createPostgresTodosStorageAdapter,
|
|
13052
13985
|
createLocalSqliteTodosStorageAdapter,
|
|
13053
13986
|
createHybridTodosStorageAdapter,
|
|
13987
|
+
closeRuntimeShadowCloud,
|
|
13054
13988
|
buildS3ObjectUrl,
|
|
13055
13989
|
buildS3ObjectKey,
|
|
13990
|
+
assertTodosShadowConfig,
|
|
13056
13991
|
assertTodosRemoteStorageConfig,
|
|
13992
|
+
TodosShadowOutbox,
|
|
13993
|
+
TodosShadowMirror,
|
|
13057
13994
|
TODOS_STORAGE_TABLES,
|
|
13058
13995
|
TODOS_STORAGE_FALLBACK_ENV,
|
|
13059
13996
|
TODOS_STORAGE_ENV,
|