|
1 | 1 | export * as SessionInbox from "./inbox.js" |
2 | 2 |
|
3 | 3 | import { and, asc, eq, notInArray, or } from "drizzle-orm" |
4 | | -import { Context, DateTime, Effect, Layer, Schema } from "effect" |
| 4 | +import { Context, DateTime, Effect, Layer, Option, Schema } from "effect" |
5 | 5 | import { makeGlobalNode } from "@opencode/util/effect/app-node" |
6 | 6 | import { |
7 | 7 | Command, |
@@ -64,19 +64,19 @@ export { |
64 | 64 | */ |
65 | 65 | export type Promotable = "input" | "steer" |
66 | 66 |
|
67 | | -const decodeUser = Schema.decodeUnknownSync(UserPayload) |
68 | | -const decodeMediaUser = Schema.decodeUnknownSync(MediaUserPayload) |
69 | | -const decodeStoredUser = Schema.decodeUnknownSync(SessionStored.UserPayload) |
| 67 | +const decodeUser = Schema.decodeUnknownOption(UserPayload) |
| 68 | +const decodeMediaUser = Schema.decodeUnknownOption(MediaUserPayload) |
| 69 | +const decodeStoredUser = Schema.decodeUnknownOption(SessionStored.UserPayload) |
70 | 70 | const encodeStoredUser = Schema.encodeSync(SessionStored.UserPayload) |
71 | | -const decodeSynthetic = Schema.decodeUnknownSync(SyntheticPayload) |
| 71 | +const decodeSynthetic = Schema.decodeUnknownOption(SyntheticPayload) |
72 | 72 | const encodeSynthetic = Schema.encodeSync(SyntheticPayload) |
73 | | -const decodeCompaction = Schema.decodeUnknownSync(CompactionPayload) |
| 73 | +const decodeCompaction = Schema.decodeUnknownOption(CompactionPayload) |
74 | 74 | const encodeCompaction = Schema.encodeSync(CompactionPayload) |
75 | | -const decodeMove = Schema.decodeUnknownSync(MovePayload) |
| 75 | +const decodeMove = Schema.decodeUnknownOption(MovePayload) |
76 | 76 | const encodeMove = Schema.encodeSync(MovePayload) |
77 | | -const decodeShell = Schema.decodeUnknownSync(ShellPayload) |
| 77 | +const decodeShell = Schema.decodeUnknownOption(ShellPayload) |
78 | 78 | const encodeShell = Schema.encodeSync(ShellPayload) |
79 | | -const decodeCommand = Schema.decodeUnknownSync(CommandPayload) |
| 79 | +const decodeCommand = Schema.decodeUnknownOption(CommandPayload) |
80 | 80 | const encodeCommand = Schema.encodeSync(CommandPayload) |
81 | 81 | const inboxLocks = KeyedMutex.makeUnsafe<SessionSchema.ID>() |
82 | 82 | type PendingRef = { readonly id: SessionMessage.ID; readonly sessionID: SessionSchema.ID } |
@@ -95,57 +95,60 @@ function matches<Type extends Item["type"]>( |
95 | 95 | return stored.sessionID === request.sessionID && stored.type === request.type |
96 | 96 | } |
97 | 97 |
|
98 | | -const fromRow = (row: typeof SessionInboxTable.$inferSelect): Info => { |
99 | | - const base = { |
100 | | - id: row.id, |
| 98 | +// A persisted row whose payload or timestamp no longer decodes (schema skew after an |
| 99 | +// upgrade, a hand edit) must not die every read of the session's inbox as an untyped |
| 100 | +// defect: list-shaped reads skip it with a warning naming the row, point reads treat |
| 101 | +// it as absent, and the healthy rows around it stay readable. |
| 102 | +const skipCorruptRow = (row: typeof SessionInboxTable.$inferSelect) => |
| 103 | + Effect.logWarning("session inbox row skipped: stored payload does not decode", { |
| 104 | + inboxID: row.id, |
101 | 105 | sessionID: row.session_id, |
102 | | - time: { created: DateTime.makeUnsafe(row.time_created) }, |
103 | | - } |
104 | | - if (row.type === "compaction") |
105 | | - return Compaction.make({ |
106 | | - ...base, |
107 | | - type: "compaction", |
108 | | - payload: decodeCompaction(row.payload), |
109 | | - delivery: row.delivery, |
110 | | - }) |
111 | | - if (row.type === "move") |
112 | | - return Move.make({ ...base, type: "move", payload: decodeMove(row.payload), delivery: row.delivery }) |
113 | | - if (row.type === "user") |
114 | | - return User.make({ |
115 | | - ...base, |
116 | | - type: "user", |
117 | | - payload: decodeUser(row.payload), |
118 | | - delivery: row.delivery, |
119 | | - }) |
120 | | - if (row.type === "synthetic") |
121 | | - return Synthetic.make({ |
122 | | - ...base, |
123 | | - type: "synthetic", |
124 | | - payload: decodeSynthetic(row.payload), |
125 | | - delivery: row.delivery, |
126 | | - }) |
127 | | - if (row.type === "shell") |
128 | | - return Shell.make({ |
129 | | - ...base, |
130 | | - type: "shell", |
131 | | - payload: decodeShell(row.payload), |
132 | | - delivery: row.delivery, |
133 | | - }) |
134 | | - if (row.type === "command") |
135 | | - return Command.make({ |
136 | | - ...base, |
137 | | - type: "command", |
138 | | - payload: decodeCommand(row.payload), |
139 | | - delivery: row.delivery, |
140 | | - }) |
141 | | - throw new LifecycleConflict({ id: base.id }) |
142 | | -} |
| 106 | + type: row.type, |
| 107 | + }).pipe(Effect.as(undefined)) |
| 108 | + |
| 109 | +const fromRow = (row: typeof SessionInboxTable.$inferSelect): Effect.Effect<Info | undefined> => |
| 110 | + Effect.gen(function* () { |
| 111 | + const created = DateTime.make(row.time_created) |
| 112 | + if (Option.isNone(created)) return yield* skipCorruptRow(row) |
| 113 | + const base = { id: row.id, sessionID: row.session_id, time: { created: created.value } } |
| 114 | + if (row.type === "compaction") { |
| 115 | + const payload = decodeCompaction(row.payload) |
| 116 | + if (Option.isNone(payload)) return yield* skipCorruptRow(row) |
| 117 | + return Compaction.make({ ...base, type: "compaction", payload: payload.value, delivery: row.delivery }) |
| 118 | + } |
| 119 | + if (row.type === "move") { |
| 120 | + const payload = decodeMove(row.payload) |
| 121 | + if (Option.isNone(payload)) return yield* skipCorruptRow(row) |
| 122 | + return Move.make({ ...base, type: "move", payload: payload.value, delivery: row.delivery }) |
| 123 | + } |
| 124 | + if (row.type === "user") { |
| 125 | + const payload = decodeUser(row.payload) |
| 126 | + if (Option.isNone(payload)) return yield* skipCorruptRow(row) |
| 127 | + return User.make({ ...base, type: "user", payload: payload.value, delivery: row.delivery }) |
| 128 | + } |
| 129 | + if (row.type === "synthetic") { |
| 130 | + const payload = decodeSynthetic(row.payload) |
| 131 | + if (Option.isNone(payload)) return yield* skipCorruptRow(row) |
| 132 | + return Synthetic.make({ ...base, type: "synthetic", payload: payload.value, delivery: row.delivery }) |
| 133 | + } |
| 134 | + if (row.type === "shell") { |
| 135 | + const payload = decodeShell(row.payload) |
| 136 | + if (Option.isNone(payload)) return yield* skipCorruptRow(row) |
| 137 | + return Shell.make({ ...base, type: "shell", payload: payload.value, delivery: row.delivery }) |
| 138 | + } |
| 139 | + if (row.type === "command") { |
| 140 | + const payload = decodeCommand(row.payload) |
| 141 | + if (Option.isNone(payload)) return yield* skipCorruptRow(row) |
| 142 | + return Command.make({ ...base, type: "command", payload: payload.value, delivery: row.delivery }) |
| 143 | + } |
| 144 | + throw new LifecycleConflict({ id: base.id }) |
| 145 | + }) |
143 | 146 |
|
144 | 147 | export const find = Effect.fn("SessionInbox.find")(function* (db: DatabaseService, id: SessionMessage.ID) { |
145 | 148 | const row = yield* db.select().from(SessionInboxTable).where(eq(SessionInboxTable.id, id)).get().pipe(Effect.orDie) |
146 | 149 | if (row === undefined) return undefined |
147 | 150 | const [hydrated] = yield* SessionBlob.hydrateInbox(db, [row]) |
148 | | - return hydrated && fromRow(hydrated) |
| 151 | + return hydrated ? yield* fromRow(hydrated) : undefined |
149 | 152 | }) |
150 | 153 |
|
151 | 154 | const promotedFromMessage = Effect.fn("SessionInbox.promotedFromMessage")(function* ( |
@@ -394,7 +397,9 @@ export const projectDelivered = Effect.fn("SessionInbox.projectDelivered")(funct |
394 | 397 | .get() |
395 | 398 | .pipe(Effect.orDie) |
396 | 399 | if (!deleted) return yield* Effect.die(new LifecycleConflict({ id: input.id })) |
397 | | - return delivered(deleted) |
| 400 | + // Died, like the missing-row conflict above: the projector's error channel is |
| 401 | + // closed, and bus boundaries turn a LifecycleConflict defect into a typed failure. |
| 402 | + return yield* delivered(deleted).pipe(Effect.orDie) |
398 | 403 | }) |
399 | 404 |
|
400 | 405 | /** A consumed row with its payload as stored, so user file refs reach the message without their blobs. */ |
@@ -415,13 +420,23 @@ export type Delivered = |
415 | 420 | } |
416 | 421 | | { readonly id: SessionMessage.ID; readonly type: "compaction" | "move" | "shell" | "command" } |
417 | 422 |
|
418 | | -const delivered = (row: typeof SessionInboxTable.$inferSelect): Delivered => { |
419 | | - if (row.type === "user") |
420 | | - return { id: row.id, type: "user", delivery: row.delivery, payload: decodeStoredUser(row.payload) } |
421 | | - if (row.type === "synthetic") |
422 | | - return { id: row.id, type: "synthetic", delivery: row.delivery, payload: decodeSynthetic(row.payload) } |
423 | | - return { id: row.id, type: row.type } |
424 | | -} |
| 423 | +// The row is already consumed (deleted) when this runs, so a payload that no longer |
| 424 | +// decodes cannot be skipped: fail the delivery as a LifecycleConflict, the typed |
| 425 | +// conflict shape this module dies with, instead of an untyped SchemaError defect. |
| 426 | +const delivered = (row: typeof SessionInboxTable.$inferSelect): Effect.Effect<Delivered, LifecycleConflict> => |
| 427 | + Effect.gen(function* () { |
| 428 | + if (row.type === "user") { |
| 429 | + const payload = decodeStoredUser(row.payload) |
| 430 | + if (Option.isNone(payload)) return yield* new LifecycleConflict({ id: row.id }) |
| 431 | + return { id: row.id, type: "user", delivery: row.delivery, payload: payload.value } satisfies Delivered |
| 432 | + } |
| 433 | + if (row.type === "synthetic") { |
| 434 | + const payload = decodeSynthetic(row.payload) |
| 435 | + if (Option.isNone(payload)) return yield* new LifecycleConflict({ id: row.id }) |
| 436 | + return { id: row.id, type: "synthetic", delivery: row.delivery, payload: payload.value } satisfies Delivered |
| 437 | + } |
| 438 | + return { id: row.id, type: row.type } satisfies Delivered |
| 439 | + }) |
425 | 440 |
|
426 | 441 | export const projectCancelled = Effect.fn("SessionInbox.projectCancelled")(function* ( |
427 | 442 | db: DatabaseService, |
@@ -482,7 +497,8 @@ const listRows = (db: DatabaseService, sessionID: SessionSchema.ID) => |
482 | 497 | .pipe(Effect.orDie) |
483 | 498 |
|
484 | 499 | export const list = Effect.fn("SessionInbox.list")(function* (db: DatabaseService, sessionID: SessionSchema.ID) { |
485 | | - return (yield* SessionBlob.hydrateInbox(db, yield* listRows(db, sessionID))).map(fromRow) |
| 500 | + const decoded = yield* Effect.forEach(yield* SessionBlob.hydrateInbox(db, yield* listRows(db, sessionID)), fromRow) |
| 501 | + return decoded.filter((item) => item !== undefined) |
486 | 502 | }) |
487 | 503 |
|
488 | 504 | /** {@link list} for clients that fetch media themselves: files stored in `blob` stay `media:` refs. */ |
@@ -491,18 +507,25 @@ export const listWithRefs = Effect.fn("SessionInbox.listWithRefs")(function* ( |
491 | 507 | sessionID: SessionSchema.ID, |
492 | 508 | ) { |
493 | 509 | const kept = yield* SessionBlob.keepInboxRefs(db, yield* listRows(db, sessionID)) |
494 | | - return kept.rows.map((row): MediaInfo => { |
495 | | - if (row.type !== "user") return fromRow(row) |
496 | | - const payload = decodeMediaUser(row.payload) |
497 | | - return MediaUser.make({ |
498 | | - id: row.id, |
499 | | - sessionID: row.session_id, |
500 | | - time: { created: DateTime.makeUnsafe(row.time_created) }, |
501 | | - type: "user", |
502 | | - payload: payload.files ? { ...payload, files: SessionBlob.describe(payload.files, kept.meta) } : payload, |
503 | | - delivery: row.delivery, |
504 | | - }) |
505 | | - }) |
| 510 | + const decoded = yield* Effect.forEach(kept.rows, (row) => |
| 511 | + Effect.gen(function* () { |
| 512 | + if (row.type !== "user") return yield* fromRow(row) |
| 513 | + const payload = decodeMediaUser(row.payload) |
| 514 | + const created = DateTime.make(row.time_created) |
| 515 | + if (Option.isNone(payload) || Option.isNone(created)) return yield* skipCorruptRow(row) |
| 516 | + return MediaUser.make({ |
| 517 | + id: row.id, |
| 518 | + sessionID: row.session_id, |
| 519 | + time: { created: created.value }, |
| 520 | + type: "user", |
| 521 | + payload: payload.value.files |
| 522 | + ? { ...payload.value, files: SessionBlob.describe(payload.value.files, kept.meta) } |
| 523 | + : payload.value, |
| 524 | + delivery: row.delivery, |
| 525 | + }) |
| 526 | + }), |
| 527 | + ) |
| 528 | + return decoded.filter((item) => item !== undefined) |
506 | 529 | }) |
507 | 530 |
|
508 | 531 | export const moveIDs = Effect.fn("SessionInbox.moveIDs")(function* (db: DatabaseService, sessionID: SessionSchema.ID) { |
@@ -521,10 +544,10 @@ export const nextPromotable = Effect.fn("SessionInbox.nextPromotable")(function* |
521 | 544 | promotable: Promotable, |
522 | 545 | ) { |
523 | 546 | const steer = (yield* pendingSteers(db, sessionID))[0] |
524 | | - if (steer) return promotion(steer) |
| 547 | + if (steer) return yield* promotion(steer).pipe(Effect.orDie) |
525 | 548 | if (promotable !== "input") return undefined |
526 | 549 | const queued = yield* firstQueued(db, sessionID) |
527 | | - return queued ? promotion(queued) : undefined |
| 550 | + return queued ? yield* promotion(queued).pipe(Effect.orDie) : undefined |
528 | 551 | }) |
529 | 552 |
|
530 | 553 | /** What a boundary decides on: prompts are promoted by id, so only a move's payload is read. */ |
@@ -552,14 +575,35 @@ export type Promotion = |
552 | 575 | /** An admitted shell or command whose execution has not started yet. */ |
553 | 576 | export type Operation = Extract<Promotion, { readonly type: "shell" | "command" }> |
554 | 577 |
|
555 | | -const promotion = (row: typeof SessionInboxTable.$inferSelect): Promotion => |
556 | | - row.type === "move" |
557 | | - ? { id: row.id, type: "move", delivery: row.delivery, payload: decodeMove(row.payload) } |
558 | | - : row.type === "shell" |
559 | | - ? { id: row.id, type: "shell", delivery: row.delivery, payload: decodeShell(row.payload) } |
560 | | - : row.type === "command" |
561 | | - ? { id: row.id, type: "command", delivery: row.delivery, payload: decodeCommand(row.payload) } |
562 | | - : { id: row.id, type: row.type, delivery: row.delivery } |
| 578 | +const tryPromotion = (row: typeof SessionInboxTable.$inferSelect): Promotion | undefined => { |
| 579 | + if (row.type === "move") { |
| 580 | + const payload = decodeMove(row.payload) |
| 581 | + return Option.isNone(payload) |
| 582 | + ? undefined |
| 583 | + : { id: row.id, type: "move", delivery: row.delivery, payload: payload.value } |
| 584 | + } |
| 585 | + if (row.type === "shell") { |
| 586 | + const payload = decodeShell(row.payload) |
| 587 | + return Option.isNone(payload) |
| 588 | + ? undefined |
| 589 | + : { id: row.id, type: "shell", delivery: row.delivery, payload: payload.value } |
| 590 | + } |
| 591 | + if (row.type === "command") { |
| 592 | + const payload = decodeCommand(row.payload) |
| 593 | + return Option.isNone(payload) |
| 594 | + ? undefined |
| 595 | + : { id: row.id, type: "command", delivery: row.delivery, payload: payload.value } |
| 596 | + } |
| 597 | + return { id: row.id, type: row.type, delivery: row.delivery } |
| 598 | +} |
| 599 | + |
| 600 | +// A boundary decision reads one specific row, so a payload that no longer decodes |
| 601 | +// fails as a LifecycleConflict rather than an untyped SchemaError defect; the |
| 602 | +// exported boundary above dies with it, matching this module's conflict handling. |
| 603 | +const promotion = (row: typeof SessionInboxTable.$inferSelect): Effect.Effect<Promotion, LifecycleConflict> => { |
| 604 | + const decoded = tryPromotion(row) |
| 605 | + return decoded ? Effect.succeed(decoded) : Effect.fail(new LifecycleConflict({ id: row.id })) |
| 606 | +} |
563 | 607 |
|
564 | 608 | /** |
565 | 609 | * Admitted shell/command operations still pending execution, in admission |
@@ -583,7 +627,14 @@ export const pendingOperations = Effect.fn("SessionInbox.pendingOperations")(fun |
583 | 627 | .orderBy(asc(SessionInboxTable.enqueued_seq)) |
584 | 628 | .all() |
585 | 629 | .pipe(Effect.orDie) |
586 | | - return rows.map(promotion).filter((item): item is Operation => item.type === "shell" || item.type === "command") |
| 630 | + const decoded = yield* Effect.forEach(rows, (row) => |
| 631 | + Effect.gen(function* () { |
| 632 | + const item = tryPromotion(row) |
| 633 | + if (item === undefined) return yield* skipCorruptRow(row) |
| 634 | + return item |
| 635 | + }), |
| 636 | + ) |
| 637 | + return decoded.filter((item): item is Operation => item?.type === "shell" || item?.type === "command") |
587 | 638 | }) |
588 | 639 |
|
589 | 640 | /** Which pending rows count: "input" means any item in either delivery mode. */ |
|