| … | … | @@ -76,6 +76,14 @@ export function createApplyFunction (opts = {}) { |
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | // The op's own clock, stamped by encodeOp on the authoring peer. Every timestamp and |
| 80 | // every time COMPARISON below uses it, so this apply is a pure function of the log. |
| 81 | // |
| 82 | // ⚠ The `at` fallback is for ops written BEFORE this field existed, already |
| 83 | // durable in deployed logs. Those replay non-deterministically exactly as they do today — |
| 84 | // no worse, and unfixable without rewriting history. Every NEW op converges. |
| 85 | const at = op.timestamp || Date.now() |
| 86 | |
| 79 | 87 | switch (op.type) { |
| 80 | 88 | case OpTypes.CREATE_USER: { |
| 81 | 89 | const key = `${prefix}user:${op.userId}` |
| … | … | @@ -104,8 +112,8 @@ export function createApplyFunction (opts = {}) { |
| 104 | 112 | globalPermissions: permissions, |
| 105 | 113 | resources, |
| 106 | 114 | identities: op.identities || [], |
| 107 | | createdAt: op.createdAt || Date.now(), |
| 108 | | updatedAt: op.updatedAt || Date.now() |
| 115 | createdAt: op.createdAt || at, |
| 116 | updatedAt: op.updatedAt || at |
| 109 | 117 | } |
| 110 | 118 | await b.put(key, user) |
| 111 | 119 | |
| … | … | @@ -128,7 +136,7 @@ export function createApplyFunction (opts = {}) { |
| 128 | 136 | if (op.profilePicture !== undefined) user.profilePicture = op.profilePicture |
| 129 | 137 | if (op.theme !== undefined) user.theme = op.theme |
| 130 | 138 | if (op.globalPermissions !== undefined) user.globalPermissions = op.globalPermissions |
| 131 | | user.updatedAt = Date.now() |
| 139 | user.updatedAt = at |
| 132 | 140 | await b.put(key, user) |
| 133 | 141 | break |
| 134 | 142 | } |
| … | … | @@ -177,7 +185,7 @@ export function createApplyFunction (opts = {}) { |
| 177 | 185 | const user = existing.value |
| 178 | 186 | if (!user.identities.includes(op.identityId)) { |
| 179 | 187 | user.identities.push(op.identityId) |
| 180 | | user.updatedAt = Date.now() |
| 188 | user.updatedAt = at |
| 181 | 189 | await b.put(key, user) |
| 182 | 190 | await b.put(`${prefix}user-identity:${op.identityId}`, { userId: op.userId }) |
| 183 | 191 | } |
| … | … | @@ -191,7 +199,7 @@ export function createApplyFunction (opts = {}) { |
| 191 | 199 | |
| 192 | 200 | const user = existing.value |
| 193 | 201 | user.identities = user.identities.filter(id => id !== op.identityId) |
| 194 | | user.updatedAt = Date.now() |
| 202 | user.updatedAt = at |
| 195 | 203 | await b.put(key, user) |
| 196 | 204 | await b.del(`${prefix}user-identity:${op.identityId}`) |
| 197 | 205 | break |
| … | … | @@ -205,7 +213,7 @@ export function createApplyFunction (opts = {}) { |
| 205 | 213 | const user = existing.value |
| 206 | 214 | if (!user.identities.includes(op.identityId)) { |
| 207 | 215 | user.identities.push(op.identityId) |
| 208 | | user.updatedAt = Date.now() |
| 216 | user.updatedAt = at |
| 209 | 217 | await b.put(key, user) |
| 210 | 218 | } |
| 211 | 219 | } |
| … | … | @@ -219,7 +227,7 @@ export function createApplyFunction (opts = {}) { |
| 219 | 227 | if (idEntry && idEntry.value.userId !== op.userId) { |
| 220 | 228 | const idRec = idEntry.value |
| 221 | 229 | idRec.userId = op.userId |
| 222 | | idRec.updatedAt = Date.now() |
| 230 | idRec.updatedAt = at |
| 223 | 231 | await b.put(idKey, idRec) |
| 224 | 232 | } |
| 225 | 233 | break |
| … | … | @@ -246,7 +254,7 @@ export function createApplyFunction (opts = {}) { |
| 246 | 254 | user.resources.push({ name: resourceName, permissions: op.permissions }) |
| 247 | 255 | } |
| 248 | 256 | } |
| 249 | | user.updatedAt = Date.now() |
| 257 | user.updatedAt = at |
| 250 | 258 | await b.put(key, user) |
| 251 | 259 | break |
| 252 | 260 | } |
| … | … | @@ -284,12 +292,12 @@ export function createApplyFunction (opts = {}) { |
| 284 | 292 | const allPerms = new Set([...pUser.globalPermissions, ...sUser.globalPermissions]) |
| 285 | 293 | pUser.globalPermissions = [...allPerms] |
| 286 | 294 | |
| 287 | | pUser.updatedAt = Date.now() |
| 295 | pUser.updatedAt = at |
| 288 | 296 | await b.put(primaryKey, pUser) |
| 289 | 297 | |
| 290 | 298 | // Mark secondary as merged |
| 291 | 299 | sUser.mergedInto = op.primaryUserId |
| 292 | | sUser.updatedAt = Date.now() |
| 300 | sUser.updatedAt = at |
| 293 | 301 | await b.put(secondaryKey, sUser) |
| 294 | 302 | break |
| 295 | 303 | } |
| … | … | @@ -299,7 +307,7 @@ export function createApplyFunction (opts = {}) { |
| 299 | 307 | // Direct-write db is updated separately in registry method |
| 300 | 308 | await b.put(`${prefix}revoked:${op.identityId}`, { |
| 301 | 309 | identityId: op.identityId, |
| 302 | | revokedAt: op.revokedAt || Date.now(), |
| 310 | revokedAt: op.revokedAt || at, |
| 303 | 311 | reason: op.reason || null |
| 304 | 312 | }) |
| 305 | 313 | break |
| … | … | @@ -326,9 +334,9 @@ export function createApplyFunction (opts = {}) { |
| 326 | 334 | if (adminCount <= 1) continue // Silently skip — can't deactivate last admin |
| 327 | 335 | } |
| 328 | 336 | |
| 329 | | user.deactivatedAt = Date.now() |
| 337 | user.deactivatedAt = at |
| 330 | 338 | user.deactivatedReason = op.reason || null |
| 331 | | user.updatedAt = Date.now() |
| 339 | user.updatedAt = at |
| 332 | 340 | await b.put(key, user) |
| 333 | 341 | break |
| 334 | 342 | } |
| … | … | @@ -341,7 +349,7 @@ export function createApplyFunction (opts = {}) { |
| 341 | 349 | const user = existing.value |
| 342 | 350 | delete user.deactivatedAt |
| 343 | 351 | delete user.deactivatedReason |
| 344 | | user.updatedAt = Date.now() |
| 352 | user.updatedAt = at |
| 345 | 353 | await b.put(key, user) |
| 346 | 354 | break |
| 347 | 355 | } |
| … | … | @@ -356,7 +364,7 @@ export function createApplyFunction (opts = {}) { |
| 356 | 364 | tokenHash: op.tokenHash, |
| 357 | 365 | userId: op.userId, |
| 358 | 366 | expiresAt: op.expiresAt, |
| 359 | | createdAt: op.createdAt || Date.now(), |
| 367 | createdAt: op.createdAt || at, |
| 360 | 368 | consumed: false |
| 361 | 369 | }) |
| 362 | 370 | break |
| … | … | @@ -368,13 +376,13 @@ export function createApplyFunction (opts = {}) { |
| 368 | 376 | if (!tokenEntry) break // unknown token |
| 369 | 377 | const token = tokenEntry.value |
| 370 | 378 | if (token.consumed) break // double-consume |
| 371 | | if (token.expiresAt && token.expiresAt < Date.now()) break // expired |
| 379 | if (token.expiresAt && token.expiresAt < at) break // expired |
| 372 | 380 | |
| 373 | 381 | // 1. Mark token consumed (kept for audit, never deleted) |
| 374 | 382 | await b.put(tokenKey, { |
| 375 | 383 | ...token, |
| 376 | 384 | consumed: true, |
| 377 | | consumedAt: Date.now(), |
| 385 | consumedAt: at, |
| 378 | 386 | consumedBy: op.newIdentityId |
| 379 | 387 | }) |
| 380 | 388 | |
| … | … | @@ -385,7 +393,7 @@ export function createApplyFunction (opts = {}) { |
| 385 | 393 | const target = targetEntry.value |
| 386 | 394 | if (!target.identities.includes(op.newIdentityId)) { |
| 387 | 395 | target.identities.push(op.newIdentityId) |
| 388 | | target.updatedAt = Date.now() |
| 396 | target.updatedAt = at |
| 389 | 397 | await b.put(targetKey, target) |
| 390 | 398 | } |
| 391 | 399 | // Always rewrite the user-identity index — if the identity |
| … | … | @@ -402,9 +410,9 @@ export function createApplyFunction (opts = {}) { |
| 402 | 410 | if (dEntry) { |
| 403 | 411 | const u = dEntry.value |
| 404 | 412 | u.identities = (u.identities || []).filter(id => id !== op.newIdentityId) |
| 405 | | u.deactivatedAt = Date.now() |
| 413 | u.deactivatedAt = at |
| 406 | 414 | u.deactivatedReason = 'merged-via-link-token' |
| 407 | | u.updatedAt = Date.now() |
| 415 | u.updatedAt = at |
| 408 | 416 | await b.put(dKey, u) |
| 409 | 417 | } |
| 410 | 418 | } |
| … | … | @@ -425,7 +433,7 @@ export function createApplyFunction (opts = {}) { |
| 425 | 433 | |
| 426 | 434 | // Set new username |
| 427 | 435 | user.username = op.username |
| 428 | | user.updatedAt = Date.now() |
| 436 | user.updatedAt = at |
| 429 | 437 | await b.put(key, user) |
| 430 | 438 | await b.put(`${prefix}username:${op.username}`, { userId: op.userId }) |
| 431 | 439 | break |
| … | … | @@ -434,7 +442,7 @@ export function createApplyFunction (opts = {}) { |
| 434 | 442 | case OpTypes.ADD_WRITER: { |
| 435 | 443 | // Audit trail in the view |
| 436 | 444 | await b.put(`${prefix}writer:${op.writerKey}`, { |
| 437 | | addedAt: Date.now(), |
| 445 | addedAt: at, |
| 438 | 446 | addedBy: op.addedBy || null, |
| 439 | 447 | role: op.role || 'writer' |
| 440 | 448 | }) |
| … | … | @@ -471,7 +479,7 @@ export function createApplyFunction (opts = {}) { |
| 471 | 479 | // no side effects on other autobases from here. |
| 472 | 480 | await b.put(`${prefix}mirror-writers:${op.devicePubkey}`, { |
| 473 | 481 | keys: op.keys, |
| 474 | | recordedAt: Date.now() |
| 482 | recordedAt: at |
| 475 | 483 | }) |
| 476 | 484 | break |
| 477 | 485 | } |