toolroll 0.9.128 → 0.9.130
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/dist/accent-colors.js +3 -1
- package/dist/agent-fence.js +0 -2
- package/dist/agents-board.js +0 -3
- package/dist/approval-authority.js +2 -1
- package/dist/assignment.js +56 -2
- package/dist/backup-ui.js +6 -3
- package/dist/backup.js +6 -0
- package/dist/bounded-spawn.js +0 -1
- package/dist/browser/manifest.json +57 -0
- package/dist/browser/terminal.2a682d0b91a35288271c73b7ee253e524cca0917d17fbaf6733e80b7a767a37c.js.br +0 -0
- package/dist/browser/terminal.2a682d0b91a35288271c73b7ee253e524cca0917d17fbaf6733e80b7a767a37c.js.gz +0 -0
- package/dist/browser/{terminal.css → terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css} +1 -1
- package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.br +0 -0
- package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.gz +0 -0
- package/dist/browser/{workspace.js → workspace.9bd1db81d3c897045a5d82d4804e374e41e78b1b2cfd05d37b982a4ea0d02626.js} +25 -25
- package/dist/browser/workspace.9bd1db81d3c897045a5d82d4804e374e41e78b1b2cfd05d37b982a4ea0d02626.js.br +0 -0
- package/dist/browser/workspace.9bd1db81d3c897045a5d82d4804e374e41e78b1b2cfd05d37b982a4ea0d02626.js.gz +0 -0
- package/dist/browser/{workspace.css → workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css} +1 -1
- package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.br +0 -0
- package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.gz +0 -0
- package/dist/browser-shell.js +271 -25
- package/dist/canvas-freshness.js +122 -0
- package/dist/canvas-fs.js +273 -0
- package/dist/canvas-reads.js +747 -0
- package/dist/canvas-state-key.js +45 -0
- package/dist/canvas-state-worker.js +12 -0
- package/dist/central-recovery-stage.js +3 -3
- package/dist/chat-actions.js +26 -0
- package/dist/chat-channel.js +4 -3
- package/dist/chat-task-actions.js +1 -1
- package/dist/chat-warmth.js +0 -1
- package/dist/check-report.js +0 -6
- package/dist/commit-extract.js +79 -40
- package/dist/companion/access.js +162 -0
- package/dist/companion/actions.js +622 -0
- package/dist/companion/bonjour.js +45 -0
- package/dist/companion/canvas.js +141 -0
- package/dist/companion/chat-origin.js +92 -0
- package/dist/companion/chat.js +571 -0
- package/dist/companion/daemon.js +1387 -0
- package/dist/companion/direct-server.js +177 -0
- package/dist/companion/finish.js +51 -0
- package/dist/companion/framing.js +498 -0
- package/dist/companion/noise.js +461 -0
- package/dist/companion/passkey-gate.js +287 -0
- package/dist/companion/paths.js +90 -0
- package/dist/companion/reads.js +283 -0
- package/dist/companion/relay-auth.js +265 -0
- package/dist/companion/relay-server.js +870 -0
- package/dist/companion/schema.js +289 -0
- package/dist/companion/service.js +581 -0
- package/dist/companion/store.js +325 -0
- package/dist/companion/webauthn.js +407 -0
- package/dist/console-instance.js +0 -4
- package/dist/contracts/console-api.js +18 -0
- package/dist/converse.js +0 -1
- package/dist/coordinator.js +1 -1
- package/dist/design/kit.css +30 -0
- package/dist/desktop-config.js +156 -0
- package/dist/desktop-host.js +6 -152
- package/dist/desktop-update.js +1 -1
- package/dist/finished-task-sql.js +14 -0
- package/dist/flexible-answers.js +0 -1
- package/dist/github-read.js +41 -0
- package/dist/guides.js +4 -3
- package/dist/lead-doors.js +50 -3
- package/dist/lead-queue-runtime.js +11 -2
- package/dist/lead-queue.js +25 -0
- package/dist/live-backup-change.js +93 -0
- package/dist/live-backup.js +75 -31
- package/dist/live-bus.js +3 -2
- package/dist/live-records.js +0 -1
- package/dist/local-landing.js +47 -0
- package/dist/local-owner.js +0 -1
- package/dist/model-choices.js +0 -11
- package/dist/nightly-check.js +0 -3
- package/dist/nightly-github.js +1 -2
- package/dist/notification-retention.js +119 -0
- package/dist/observations.js +0 -1
- package/dist/operate.js +17 -4
- package/dist/person-proof.js +5 -1
- package/dist/pull-request-flow.js +116 -2
- package/dist/qr.js +360 -0
- package/dist/release-adapters.js +14 -43
- package/dist/release-auto.js +17 -13
- package/dist/release-card.js +0 -4
- package/dist/release-cli.js +37 -2
- package/dist/release.js +13 -2
- package/dist/restore-retirement.js +6 -1
- package/dist/result-review.js +0 -3
- package/dist/retention-ui.js +1 -1
- package/dist/retention.js +14 -21
- package/dist/review-context.js +0 -5
- package/dist/route-rules.js +1 -5
- package/dist/sandbox-runtime.js +0 -1
- package/dist/scope.js +68 -17
- package/dist/scout.js +0 -6
- package/dist/serve.js +60 -9
- package/dist/server/kit.js +1 -0
- package/dist/server/live.js +9 -2
- package/dist/server/pages.js +12 -4
- package/dist/server/phone-engine-api.js +79 -0
- package/dist/server/render-pages.js +1 -11
- package/dist/server/render-results.js +0 -1
- package/dist/server/render-tasks.js +0 -68
- package/dist/server/route-table.js +12 -1
- package/dist/server/settings-phone.js +515 -0
- package/dist/server/settings-scripts.js +2 -0
- package/dist/server/settings-section-table.js +0 -3
- package/dist/server/settings-sections.js +10 -4
- package/dist/server/settings.js +20 -5
- package/dist/server/terminals.js +2 -1
- package/dist/settings-groups-ui.js +2 -1
- package/dist/settings-proofs.js +6 -0
- package/dist/store.js +52 -4
- package/dist/structured-output.js +0 -1
- package/dist/style-asset.js +20 -7
- package/dist/tailnet.js +39 -0
- package/dist/task-control.js +5 -0
- package/dist/task-sizing.js +0 -1
- package/dist/toolroll-update-cli.js +19 -3
- package/dist/toolroll-update-ui.js +2 -2
- package/dist/toolroll-update.js +98 -16
- package/dist/youiyoux-intake.js +8 -0
- package/package.json +7 -2
- package/dist/contracts/contract-test.js +0 -55
- package/dist/unfenced-test-support.js +0 -6
- /package/dist/browser/{terminal.js → terminal.2a682d0b91a35288271c73b7ee253e524cca0917d17fbaf6733e80b7a767a37c.js} +0 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { deriveMachineId, generateX25519KeyPair } from "./noise.js";
|
|
2
|
+
import { generateEd25519KeyPair } from "./relay-auth.js";
|
|
3
|
+
import { COMPANION_RETIRED_REASONS } from "./schema.js";
|
|
4
|
+
export const PENDING_ACTIVATION_MS = 10 * 60 * 1000;
|
|
5
|
+
export const TOMBSTONE_MS = 30 * 24 * 60 * 60 * 1000;
|
|
6
|
+
export const MAX_PHONES_PER_MAC = 32;
|
|
7
|
+
const UNSAFE_DEVICE_NAME = /[\u0000-\u001f\u007f-\u009f\u2028\u2029\u061c\u200b-\u200f\u202a-\u202e\u2066-\u2069\ufeff]/g;
|
|
8
|
+
export const MAX_DEVICE_NAME = 48;
|
|
9
|
+
export function sanitizeDeviceName(raw) {
|
|
10
|
+
if (typeof raw !== "string")
|
|
11
|
+
return "iPhone";
|
|
12
|
+
const cleaned = [...raw.replace(UNSAFE_DEVICE_NAME, "").trim()].slice(0, MAX_DEVICE_NAME).join("").trim();
|
|
13
|
+
return cleaned.length > 0 ? cleaned : "iPhone";
|
|
14
|
+
}
|
|
15
|
+
export function loadOrCreateCompanionHostKeys(db, now = new Date()) {
|
|
16
|
+
const existing = db.prepare("SELECT * FROM companion_host_keys WHERE singleton = 1").get();
|
|
17
|
+
if (existing !== undefined) {
|
|
18
|
+
return {
|
|
19
|
+
noiseStaticPriv: Buffer.from(String(existing["noise_static_priv"]), "base64url"),
|
|
20
|
+
noiseStaticPub: Buffer.from(String(existing["noise_static_pub"]), "base64url"),
|
|
21
|
+
relayPriv: Buffer.from(String(existing["relay_priv"]), "base64url"),
|
|
22
|
+
relayPub: Buffer.from(String(existing["relay_pub"]), "base64url"),
|
|
23
|
+
machineId: String(existing["machine_id"]),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const noise = generateX25519KeyPair();
|
|
27
|
+
const relay = generateEd25519KeyPair();
|
|
28
|
+
db.prepare(`
|
|
29
|
+
INSERT OR IGNORE INTO companion_host_keys (singleton, noise_static_priv, noise_static_pub, relay_priv, relay_pub, machine_id, created_at)
|
|
30
|
+
VALUES (1, ?, ?, ?, ?, ?, ?)
|
|
31
|
+
`).run(noise.privateKey.toString("base64url"), noise.publicKey.toString("base64url"), relay.privateKey.toString("base64url"), relay.publicKey.toString("base64url"), deriveMachineId(relay.publicKey), now.toISOString());
|
|
32
|
+
return loadOrCreateCompanionHostKeys(db, now);
|
|
33
|
+
}
|
|
34
|
+
export function readCompanionHostKeys(db) {
|
|
35
|
+
const existing = db.prepare("SELECT 1 AS hit FROM companion_host_keys WHERE singleton = 1").get();
|
|
36
|
+
return existing === undefined ? null : loadOrCreateCompanionHostKeys(db);
|
|
37
|
+
}
|
|
38
|
+
function rowOf(row) {
|
|
39
|
+
const reason = row["revoked_reason"];
|
|
40
|
+
return {
|
|
41
|
+
deviceId: String(row["device_id"]),
|
|
42
|
+
accountName: String(row["account_name"]),
|
|
43
|
+
deviceName: String(row["device_name"]),
|
|
44
|
+
phoneStaticPub: Buffer.from(String(row["phone_static_pub"]), "base64url"),
|
|
45
|
+
deviceRelayPub: Buffer.from(String(row["device_relay_pub"]), "base64url"),
|
|
46
|
+
devicePsk: Buffer.from(String(row["device_psk"]), "base64url"),
|
|
47
|
+
recoveryGeneration: String(row["recovery_generation"]),
|
|
48
|
+
accountGeneration: Number(row["account_generation"]),
|
|
49
|
+
createdAt: String(row["created_at"]),
|
|
50
|
+
activatedAt: row["activated_at"] == null ? null : String(row["activated_at"]),
|
|
51
|
+
lastSeenAt: row["last_seen_at"] == null ? null : String(row["last_seen_at"]),
|
|
52
|
+
revokedAt: row["revoked_at"] == null ? null : String(row["revoked_at"]),
|
|
53
|
+
revokedReason: typeof reason === "string" && COMPANION_RETIRED_REASONS.includes(reason) ? reason : null,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function savePendingDevice(db, row) {
|
|
57
|
+
db.prepare(`
|
|
58
|
+
INSERT INTO companion_paired_device
|
|
59
|
+
(device_id, account_name, device_name, phone_static_pub, device_relay_pub, device_psk, recovery_generation, account_generation, created_at)
|
|
60
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
61
|
+
`).run(row.deviceId, row.accountName, sanitizeDeviceName(row.deviceName), row.phoneStaticPub.toString("base64url"), row.deviceRelayPub.toString("base64url"), row.devicePsk.toString("base64url"), row.recoveryGeneration, row.accountGeneration, row.createdAt);
|
|
62
|
+
}
|
|
63
|
+
export function deletePendingDevice(db, deviceId) {
|
|
64
|
+
return Number(db.prepare("DELETE FROM companion_paired_device WHERE device_id = ? AND activated_at IS NULL AND revoked_at IS NULL").run(deviceId).changes) > 0;
|
|
65
|
+
}
|
|
66
|
+
export function getPairedDevice(db, deviceId) {
|
|
67
|
+
const row = db.prepare("SELECT * FROM companion_paired_device WHERE device_id = ?").get(deviceId);
|
|
68
|
+
return row === undefined ? null : rowOf(row);
|
|
69
|
+
}
|
|
70
|
+
export function listPairedDevices(db) {
|
|
71
|
+
return db.prepare("SELECT * FROM companion_paired_device ORDER BY created_at DESC, device_id").all().map(rowOf);
|
|
72
|
+
}
|
|
73
|
+
export function touchPairedDeviceLastSeen(db, deviceId, now) {
|
|
74
|
+
db.prepare("UPDATE companion_paired_device SET last_seen_at = ? WHERE device_id = ? AND revoked_at IS NULL").run(now.toISOString(), deviceId);
|
|
75
|
+
}
|
|
76
|
+
export function deviceAccountStands(device, account) {
|
|
77
|
+
return account !== null && account.revokedAt === null && device.accountGeneration === account.generation;
|
|
78
|
+
}
|
|
79
|
+
const pendingLive = (device, nowMs) => device.activatedAt === null && device.revokedAt === null && Date.parse(device.createdAt) > nowMs - PENDING_ACTIVATION_MS;
|
|
80
|
+
export function livePairedDevices(store, opts = {}) {
|
|
81
|
+
const generation = store.workspaceIdentity().recoveryGeneration;
|
|
82
|
+
const nowMs = (opts.now ?? new Date()).getTime();
|
|
83
|
+
return listPairedDevices(store.handle).filter(d => d.revokedAt === null && d.recoveryGeneration === generation
|
|
84
|
+
&& deviceAccountStands(d, store.accountOf(d.accountName))
|
|
85
|
+
&& (d.activatedAt !== null || (opts.includePending === true && pendingLive(d, nowMs))));
|
|
86
|
+
}
|
|
87
|
+
export function phoneCount(db, now) {
|
|
88
|
+
const cutoff = new Date(now.getTime() - PENDING_ACTIVATION_MS).toISOString();
|
|
89
|
+
return Number(db.prepare("SELECT count(*) AS n FROM companion_paired_device WHERE revoked_at IS NULL AND (activated_at IS NOT NULL OR created_at > ?)")
|
|
90
|
+
.get(cutoff)?.["n"] ?? 0);
|
|
91
|
+
}
|
|
92
|
+
export function activatePendingDevice(store, deviceId, now) {
|
|
93
|
+
return store.transact(() => {
|
|
94
|
+
const device = getPairedDevice(store.handle, deviceId);
|
|
95
|
+
if (device === null || device.revokedAt !== null || device.recoveryGeneration !== store.workspaceIdentity().recoveryGeneration)
|
|
96
|
+
return "refused";
|
|
97
|
+
if (!deviceAccountStands(device, store.accountOf(device.accountName)))
|
|
98
|
+
return "refused";
|
|
99
|
+
if (device.activatedAt !== null)
|
|
100
|
+
return "already_active";
|
|
101
|
+
const cutoff = new Date(now.getTime() - PENDING_ACTIVATION_MS).toISOString();
|
|
102
|
+
const done = store.handle.prepare(`UPDATE companion_paired_device SET activated_at = ?
|
|
103
|
+
WHERE device_id = ? AND activated_at IS NULL AND revoked_at IS NULL AND created_at > ? AND account_generation = ? AND recovery_generation = ?`)
|
|
104
|
+
.run(now.toISOString(), deviceId, cutoff, device.accountGeneration, device.recoveryGeneration);
|
|
105
|
+
return Number(done.changes) === 1 ? "activated" : "refused";
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
export function revokePairedDeviceRecord(db, deviceId, now, reason = "removed") {
|
|
109
|
+
const res = db.prepare("UPDATE companion_paired_device SET revoked_at = ?, revoked_reason = ? WHERE device_id = ? AND revoked_at IS NULL")
|
|
110
|
+
.run(now.toISOString(), reason, deviceId);
|
|
111
|
+
return Number(res.changes) > 0;
|
|
112
|
+
}
|
|
113
|
+
export function sweepCompanionRecords(db, now) {
|
|
114
|
+
const tombstoneCutoff = new Date(now.getTime() - TOMBSTONE_MS).toISOString();
|
|
115
|
+
const pendingCutoff = new Date(now.getTime() - PENDING_ACTIVATION_MS).toISOString();
|
|
116
|
+
const tombstones = Number(db.prepare("DELETE FROM companion_paired_device WHERE revoked_at IS NOT NULL AND revoked_at < ?").run(tombstoneCutoff).changes);
|
|
117
|
+
const pending = db.prepare("SELECT device_id FROM companion_paired_device WHERE activated_at IS NULL AND revoked_at IS NULL AND created_at <= ?")
|
|
118
|
+
.all(pendingCutoff).map(row => String(row["device_id"]));
|
|
119
|
+
for (const deviceId of pending)
|
|
120
|
+
deletePendingDevice(db, deviceId);
|
|
121
|
+
return { tombstones, pending };
|
|
122
|
+
}
|
|
123
|
+
export function listPhones(store, opts = {}) {
|
|
124
|
+
const nowMs = (opts.now ?? new Date()).getTime();
|
|
125
|
+
const generation = store.workspaceIdentity().recoveryGeneration;
|
|
126
|
+
const out = [];
|
|
127
|
+
for (const d of listPairedDevices(store.handle)) {
|
|
128
|
+
if (opts.accountName !== undefined && d.accountName !== opts.accountName)
|
|
129
|
+
continue;
|
|
130
|
+
if (d.recoveryGeneration !== generation)
|
|
131
|
+
continue;
|
|
132
|
+
let state;
|
|
133
|
+
let reason = null;
|
|
134
|
+
if (d.revokedAt !== null) {
|
|
135
|
+
if (Date.parse(d.revokedAt) <= nowMs - TOMBSTONE_MS)
|
|
136
|
+
continue;
|
|
137
|
+
if (d.activatedAt === null)
|
|
138
|
+
continue;
|
|
139
|
+
state = "retired";
|
|
140
|
+
reason = d.revokedReason ?? "removed";
|
|
141
|
+
}
|
|
142
|
+
else if (!deviceAccountStands(d, store.accountOf(d.accountName))) {
|
|
143
|
+
state = "retired";
|
|
144
|
+
reason = "access_changed";
|
|
145
|
+
}
|
|
146
|
+
else if (d.activatedAt === null) {
|
|
147
|
+
if (!pendingLive(d, nowMs))
|
|
148
|
+
continue;
|
|
149
|
+
state = "finishing";
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
state = "active";
|
|
153
|
+
}
|
|
154
|
+
out.push({ deviceId: d.deviceId, name: d.deviceName, accountName: d.accountName, pairedAt: d.createdAt, lastSeenAt: d.lastSeenAt, state, retiredReason: reason });
|
|
155
|
+
}
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
export function renamePhone(store, deviceId, name, accountName) {
|
|
159
|
+
const res = store.handle.prepare("UPDATE companion_paired_device SET device_name = ? WHERE device_id = ? AND account_name = ? AND revoked_at IS NULL")
|
|
160
|
+
.run(sanitizeDeviceName(name), deviceId, accountName);
|
|
161
|
+
return Number(res.changes) > 0;
|
|
162
|
+
}
|
|
163
|
+
const COMPANION_RESTORE_TABLES = [
|
|
164
|
+
"companion_host_keys",
|
|
165
|
+
"companion_paired_device",
|
|
166
|
+
"account_passkeys",
|
|
167
|
+
"passkey_challenges",
|
|
168
|
+
"device_idempotency",
|
|
169
|
+
"companion_chat_origin",
|
|
170
|
+
];
|
|
171
|
+
export function retireCompanionOnRestore(db) {
|
|
172
|
+
let deleted = 0;
|
|
173
|
+
for (const table of COMPANION_RESTORE_TABLES) {
|
|
174
|
+
deleted += Number(db.prepare(`DELETE FROM ${table}`).run().changes);
|
|
175
|
+
const remaining = Number(db.prepare(`SELECT count(*) AS n FROM ${table}`).get()?.["n"] ?? 0);
|
|
176
|
+
if (remaining !== 0)
|
|
177
|
+
throw new Error(`ADR 0001 postcondition failed: ${table} still has ${remaining} rows after restore`);
|
|
178
|
+
}
|
|
179
|
+
return deleted;
|
|
180
|
+
}
|
|
181
|
+
export const IDEMPOTENCY_RETENTION_MS = 24 * 60 * 60 * 1000;
|
|
182
|
+
export const IDEMPOTENCY_MAX_PER_DEVICE = 1_000;
|
|
183
|
+
function passkeyRowOf(row) {
|
|
184
|
+
return {
|
|
185
|
+
credentialId: String(row["credential_id"]),
|
|
186
|
+
workspaceId: String(row["workspace_id"]),
|
|
187
|
+
accountName: String(row["account_name"]),
|
|
188
|
+
userHandle: String(row["user_handle"]),
|
|
189
|
+
publicKeySpki: String(row["public_key_spki"]),
|
|
190
|
+
signCount: Number(row["sign_count"]),
|
|
191
|
+
label: String(row["label"]),
|
|
192
|
+
createdAt: String(row["created_at"]),
|
|
193
|
+
lastUsedAt: row["last_used_at"] == null ? null : String(row["last_used_at"]),
|
|
194
|
+
recoveryGeneration: String(row["recovery_generation"]),
|
|
195
|
+
revokedAt: row["revoked_at"] == null ? null : String(row["revoked_at"]),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
export function getAccountPasskey(db, credentialId) {
|
|
199
|
+
const row = db.prepare("SELECT * FROM account_passkeys WHERE credential_id = ?").get(credentialId);
|
|
200
|
+
return row === undefined ? null : passkeyRowOf(row);
|
|
201
|
+
}
|
|
202
|
+
export function insertAccountPasskey(db, row) {
|
|
203
|
+
const res = db.prepare(`
|
|
204
|
+
INSERT OR IGNORE INTO account_passkeys
|
|
205
|
+
(credential_id, workspace_id, account_name, user_handle, public_key_spki, sign_count, label, created_at, recovery_generation)
|
|
206
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
207
|
+
`).run(row.credentialId, row.workspaceId, row.accountName, row.userHandle, row.publicKeySpki, row.signCount, sanitizeDeviceName(row.label), row.createdAt, row.recoveryGeneration);
|
|
208
|
+
return Number(res.changes) === 1;
|
|
209
|
+
}
|
|
210
|
+
export function listActiveAccountPasskeys(db, workspaceId, accountName, recoveryGeneration) {
|
|
211
|
+
return db.prepare(`
|
|
212
|
+
SELECT * FROM account_passkeys
|
|
213
|
+
WHERE workspace_id = ? AND account_name = ? AND recovery_generation = ? AND revoked_at IS NULL
|
|
214
|
+
ORDER BY created_at DESC, credential_id
|
|
215
|
+
`).all(workspaceId, accountName, recoveryGeneration).map(passkeyRowOf);
|
|
216
|
+
}
|
|
217
|
+
export function listAllAccountPasskeys(db, workspaceId, accountName, recoveryGeneration) {
|
|
218
|
+
return db.prepare(`
|
|
219
|
+
SELECT * FROM account_passkeys
|
|
220
|
+
WHERE workspace_id = ? AND account_name = ? AND recovery_generation = ?
|
|
221
|
+
ORDER BY created_at DESC, credential_id
|
|
222
|
+
`).all(workspaceId, accountName, recoveryGeneration).map(passkeyRowOf);
|
|
223
|
+
}
|
|
224
|
+
export function updateAccountPasskeyUsage(db, credentialId, from, signCount, now) {
|
|
225
|
+
return Number(db.prepare("UPDATE account_passkeys SET sign_count = ?, last_used_at = ? WHERE credential_id = ? AND sign_count = ? AND revoked_at IS NULL")
|
|
226
|
+
.run(signCount, now.toISOString(), credentialId, from).changes) === 1;
|
|
227
|
+
}
|
|
228
|
+
export function revokeAccountPasskey(db, credentialId, accountName, now) {
|
|
229
|
+
const res = db.prepare("UPDATE account_passkeys SET revoked_at = ? WHERE credential_id = ? AND account_name = ? AND revoked_at IS NULL")
|
|
230
|
+
.run(now.toISOString(), credentialId, accountName);
|
|
231
|
+
return Number(res.changes) > 0;
|
|
232
|
+
}
|
|
233
|
+
export function savePasskeyChallenge(db, row) {
|
|
234
|
+
db.prepare(`
|
|
235
|
+
INSERT INTO passkey_challenges
|
|
236
|
+
(challenge, kind, workspace_id, account_name, device_id, recovery_generation, binding_json, expires_at)
|
|
237
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
238
|
+
`).run(row.challenge, row.kind, row.workspaceId, row.accountName, row.deviceId, row.recoveryGeneration, row.bindingJson, row.expiresAt);
|
|
239
|
+
}
|
|
240
|
+
export function getPasskeyChallenge(db, challenge) {
|
|
241
|
+
const row = db.prepare("SELECT * FROM passkey_challenges WHERE challenge = ?").get(challenge);
|
|
242
|
+
if (row === undefined)
|
|
243
|
+
return null;
|
|
244
|
+
return {
|
|
245
|
+
challenge: String(row["challenge"]),
|
|
246
|
+
kind: String(row["kind"]),
|
|
247
|
+
workspaceId: String(row["workspace_id"]),
|
|
248
|
+
accountName: String(row["account_name"]),
|
|
249
|
+
deviceId: String(row["device_id"]),
|
|
250
|
+
recoveryGeneration: String(row["recovery_generation"]),
|
|
251
|
+
bindingJson: String(row["binding_json"]),
|
|
252
|
+
expiresAt: String(row["expires_at"]),
|
|
253
|
+
consumedAt: row["consumed_at"] == null ? null : String(row["consumed_at"]),
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
export function consumePasskeyChallenge(db, challenge, now) {
|
|
257
|
+
const res = db.prepare("UPDATE passkey_challenges SET consumed_at = ? WHERE challenge = ? AND consumed_at IS NULL AND expires_at > ?")
|
|
258
|
+
.run(now.toISOString(), challenge, now.toISOString());
|
|
259
|
+
return Number(res.changes) === 1;
|
|
260
|
+
}
|
|
261
|
+
export function prunePasskeyChallenges(db, now) {
|
|
262
|
+
return Number(db.prepare("DELETE FROM passkey_challenges WHERE expires_at <= ? OR consumed_at IS NOT NULL").run(now.toISOString()).changes);
|
|
263
|
+
}
|
|
264
|
+
function idempotencyRowOf(row) {
|
|
265
|
+
return {
|
|
266
|
+
deviceId: String(row["device_id"]),
|
|
267
|
+
idempotencyKey: String(row["idempotency_key"]),
|
|
268
|
+
method: String(row["method"]),
|
|
269
|
+
requestHash: String(row["request_hash"]),
|
|
270
|
+
state: String(row["state"]),
|
|
271
|
+
outcomeJson: row["outcome_json"] == null ? null : String(row["outcome_json"]),
|
|
272
|
+
updatedAt: String(row["updated_at"]),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
export function getDeviceIdempotency(db, deviceId, idempotencyKey) {
|
|
276
|
+
const row = db.prepare("SELECT * FROM device_idempotency WHERE device_id = ? AND idempotency_key = ?").get(deviceId, idempotencyKey);
|
|
277
|
+
return row === undefined ? null : idempotencyRowOf(row);
|
|
278
|
+
}
|
|
279
|
+
export function claimDeviceIdempotency(db, input) {
|
|
280
|
+
const inserted = db.prepare(`
|
|
281
|
+
INSERT OR IGNORE INTO device_idempotency (device_id, idempotency_key, method, request_hash, state, outcome_json, updated_at)
|
|
282
|
+
VALUES (?, ?, ?, ?, 'in_flight', NULL, ?)
|
|
283
|
+
`).run(input.deviceId, input.idempotencyKey, input.method, input.requestHash, input.now.toISOString());
|
|
284
|
+
if (Number(inserted.changes) === 1)
|
|
285
|
+
return { status: "claimed" };
|
|
286
|
+
const existing = getDeviceIdempotency(db, input.deviceId, input.idempotencyKey);
|
|
287
|
+
if (existing === null)
|
|
288
|
+
return { status: "conflict" };
|
|
289
|
+
if (existing.method !== input.method || existing.requestHash !== input.requestHash)
|
|
290
|
+
return { status: "conflict" };
|
|
291
|
+
if (existing.state === "in_flight")
|
|
292
|
+
return { status: "in_flight" };
|
|
293
|
+
if (existing.state === "unknown")
|
|
294
|
+
return { status: "unknown" };
|
|
295
|
+
return { status: "completed", outcomeJson: existing.outcomeJson ?? "{}" };
|
|
296
|
+
}
|
|
297
|
+
export function settleDeviceIdempotency(db, deviceId, idempotencyKey, state, outcomeJson, now) {
|
|
298
|
+
db.prepare("UPDATE device_idempotency SET state = ?, outcome_json = ?, updated_at = ? WHERE device_id = ? AND idempotency_key = ?")
|
|
299
|
+
.run(state, outcomeJson, now.toISOString(), deviceId, idempotencyKey);
|
|
300
|
+
}
|
|
301
|
+
export function reconcileIdempotencyOnStartup(db, now) {
|
|
302
|
+
return Number(db.prepare("UPDATE device_idempotency SET state = 'unknown', updated_at = ? WHERE state = 'in_flight'").run(now.toISOString()).changes);
|
|
303
|
+
}
|
|
304
|
+
export function pruneDeviceIdempotency(db, now) {
|
|
305
|
+
const cutoff = new Date(now.getTime() - IDEMPOTENCY_RETENTION_MS).toISOString();
|
|
306
|
+
let deleted = Number(db.prepare("DELETE FROM device_idempotency WHERE state IN ('completed', 'unknown') AND updated_at < ?").run(cutoff).changes);
|
|
307
|
+
const devices = db.prepare("SELECT device_id FROM device_idempotency GROUP BY device_id HAVING count(*) > ?").all(IDEMPOTENCY_MAX_PER_DEVICE);
|
|
308
|
+
for (const row of devices) {
|
|
309
|
+
const deviceId = String(row["device_id"]);
|
|
310
|
+
const total = Number(db.prepare("SELECT count(*) AS n FROM device_idempotency WHERE device_id = ?").get(deviceId)?.["n"] ?? 0);
|
|
311
|
+
const excess = total - IDEMPOTENCY_MAX_PER_DEVICE;
|
|
312
|
+
if (excess <= 0)
|
|
313
|
+
continue;
|
|
314
|
+
deleted += Number(db.prepare(`
|
|
315
|
+
DELETE FROM device_idempotency
|
|
316
|
+
WHERE rowid IN (
|
|
317
|
+
SELECT rowid FROM device_idempotency
|
|
318
|
+
WHERE device_id = ? AND state IN ('completed', 'unknown')
|
|
319
|
+
ORDER BY updated_at ASC, rowid ASC
|
|
320
|
+
LIMIT ?
|
|
321
|
+
)
|
|
322
|
+
`).run(deviceId, excess).changes);
|
|
323
|
+
}
|
|
324
|
+
return deleted;
|
|
325
|
+
}
|