GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #2905 from developing-today-forks/xaltha/2904-inbo… · developing-today-forks/opencode@95cccca

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/core
    • src/session
      • inbox.ts
    • test
      • session-inbox.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/core/src/session/inbox.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/session/inbox.ts
+137-86Lines changed: 137 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * as SessionInbox from "./inbox.js"
22

33
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"
55
import { makeGlobalNode } from "@opencode/util/effect/app-node"
66
import {
77
Command,
@@ -64,19 +64,19 @@ export {
6464
*/
6565
export type Promotable = "input" | "steer"
6666

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)
7070
const encodeStoredUser = Schema.encodeSync(SessionStored.UserPayload)
71-
const decodeSynthetic = Schema.decodeUnknownSync(SyntheticPayload)
71+
const decodeSynthetic = Schema.decodeUnknownOption(SyntheticPayload)
7272
const encodeSynthetic = Schema.encodeSync(SyntheticPayload)
73-
const decodeCompaction = Schema.decodeUnknownSync(CompactionPayload)
73+
const decodeCompaction = Schema.decodeUnknownOption(CompactionPayload)
7474
const encodeCompaction = Schema.encodeSync(CompactionPayload)
75-
const decodeMove = Schema.decodeUnknownSync(MovePayload)
75+
const decodeMove = Schema.decodeUnknownOption(MovePayload)
7676
const encodeMove = Schema.encodeSync(MovePayload)
77-
const decodeShell = Schema.decodeUnknownSync(ShellPayload)
77+
const decodeShell = Schema.decodeUnknownOption(ShellPayload)
7878
const encodeShell = Schema.encodeSync(ShellPayload)
79-
const decodeCommand = Schema.decodeUnknownSync(CommandPayload)
79+
const decodeCommand = Schema.decodeUnknownOption(CommandPayload)
8080
const encodeCommand = Schema.encodeSync(CommandPayload)
8181
const inboxLocks = KeyedMutex.makeUnsafe<SessionSchema.ID>()
8282
type PendingRef = { readonly id: SessionMessage.ID; readonly sessionID: SessionSchema.ID }
@@ -95,57 +95,60 @@ function matches<Type extends Item["type"]>(
9595
return stored.sessionID === request.sessionID && stored.type === request.type
9696
}
9797

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,
101105
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+
})
143146

144147
export const find = Effect.fn("SessionInbox.find")(function* (db: DatabaseService, id: SessionMessage.ID) {
145148
const row = yield* db.select().from(SessionInboxTable).where(eq(SessionInboxTable.id, id)).get().pipe(Effect.orDie)
146149
if (row === undefined) return undefined
147150
const [hydrated] = yield* SessionBlob.hydrateInbox(db, [row])
148-
return hydrated && fromRow(hydrated)
151+
return hydrated ? yield* fromRow(hydrated) : undefined
149152
})
150153

151154
const promotedFromMessage = Effect.fn("SessionInbox.promotedFromMessage")(function* (
@@ -394,7 +397,9 @@ export const projectDelivered = Effect.fn("SessionInbox.projectDelivered")(funct
394397
.get()
395398
.pipe(Effect.orDie)
396399
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)
398403
})
399404

400405
/** 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 =
415420
}
416421
| { readonly id: SessionMessage.ID; readonly type: "compaction" | "move" | "shell" | "command" }
417422

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+
})
425440

426441
export const projectCancelled = Effect.fn("SessionInbox.projectCancelled")(function* (
427442
db: DatabaseService,
@@ -482,7 +497,8 @@ const listRows = (db: DatabaseService, sessionID: SessionSchema.ID) =>
482497
.pipe(Effect.orDie)
483498

484499
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)
486502
})
487503

488504
/** {@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* (
491507
sessionID: SessionSchema.ID,
492508
) {
493509
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)
506529
})
507530

508531
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*
521544
promotable: Promotable,
522545
) {
523546
const steer = (yield* pendingSteers(db, sessionID))[0]
524-
if (steer) return promotion(steer)
547+
if (steer) return yield* promotion(steer).pipe(Effect.orDie)
525548
if (promotable !== "input") return undefined
526549
const queued = yield* firstQueued(db, sessionID)
527-
return queued ? promotion(queued) : undefined
550+
return queued ? yield* promotion(queued).pipe(Effect.orDie) : undefined
528551
})
529552

530553
/** 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 =
552575
/** An admitted shell or command whose execution has not started yet. */
553576
export type Operation = Extract<Promotion, { readonly type: "shell" | "command" }>
554577

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+
}
563607

564608
/**
565609
* Admitted shell/command operations still pending execution, in admission
@@ -583,7 +627,14 @@ export const pendingOperations = Effect.fn("SessionInbox.pendingOperations")(fun
583627
.orderBy(asc(SessionInboxTable.enqueued_seq))
584628
.all()
585629
.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")
587638
})
588639

589640
/** Which pending rows count: "input" means any item in either delivery mode. */

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.