Skip to content

Commit cef9718

Browse files
authored
refactor(core): drop redundant branding and re-validation across session modules (#57952)
1 parent eeb9124 commit cef9718

7 files changed

Lines changed: 22 additions & 32 deletions

File tree

‎packages/core/src/session.ts‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * as Session from "./session.js"
22
export * from "./session/schema.js"
33

4-
import { DateTime, Effect, Layer, Schema, Context, Stream } from "effect"
4+
import { DateTime, Effect, Layer, Context, Stream } from "effect"
55
import { LLMClient } from "@opencode/ai"
66
import { ListAnchor, ListCursor, ListFilter } from "@opencode/schema/session"
77
import { and, desc, eq } from "drizzle-orm"
@@ -105,6 +105,7 @@ export {
105105
AttachmentError,
106106
BusyError,
107107
CompactionConflictError,
108+
ForkEmptyError,
108109
InboxConflictError,
109110
InvalidListCursorError,
110111
MessageDecodeError,
@@ -267,7 +268,8 @@ const layer = Layer.effect(
267268
const environments = yield* SessionEnvironment.Service
268269
const locations = yield* LocationServiceMap.Service
269270
const sessions = yield* Session.make()
270-
const isDurableSessionEvent = Schema.is(SessionEvent.Durable)
271+
const isDurableSessionEvent = (item: Bus.LogItem): item is SessionEvent.DurableEvent =>
272+
Object.hasOwn(SessionEvent.Durable.cases, item.type)
271273

272274
const result = Service.of({
273275
create: Effect.fn("Session.create")(function* (input) {
@@ -297,13 +299,7 @@ const layer = Layer.effect(
297299
// location, so host policies that read them treat the family uniformly.
298300
metadata: input.metadata ?? parent?.metadata,
299301
permissions: input.permissions ?? parent?.permissions,
300-
model: input.model
301-
? {
302-
id: Model.ID.make(input.model.id),
303-
providerID: input.model.providerID,
304-
variant: input.model.variant,
305-
}
306-
: undefined,
302+
model: input.model,
307303
},
308304
{ location },
309305
)

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { DateTime, Schema } from "effect"
2-
import { Location } from "@opencode/schema/location"
32
import { Model } from "@opencode/schema/model"
4-
import { Project } from "@opencode/schema/project"
53
import { Provider } from "@opencode/schema/provider"
64
import { AbsolutePath, RelativePath } from "../schema.js"
7-
import { Workspace } from "@opencode/schema/workspace"
85
import { SessionSchema } from "./schema.js"
96
import type { SessionTable } from "./sql.js"
107
import { PersistedRevert } from "@opencode/schema/session-revert"
@@ -14,14 +11,14 @@ const decodeRevert = Schema.decodeUnknownSync(PersistedRevert)
1411

1512
export function fromRow(row: typeof SessionTable.$inferSelect): SessionSchema.Info {
1613
return SessionSchema.Info.make({
17-
id: SessionSchema.ID.make(row.id),
18-
projectID: Project.ID.make(row.project_id),
14+
id: row.id,
15+
projectID: row.project_id,
1916
title: row.title ?? undefined,
20-
parentID: row.parent_id ? SessionSchema.ID.make(row.parent_id) : undefined,
17+
parentID: row.parent_id ?? undefined,
2118
fork:
2219
row.fork_session_id && row.fork_boundary
2320
? {
24-
sessionID: SessionSchema.ID.make(row.fork_session_id),
21+
sessionID: row.fork_session_id,
2522
boundary: row.fork_boundary,
2623
}
2724
: undefined,
@@ -43,10 +40,10 @@ export function fromRow(row: typeof SessionTable.$inferSelect): SessionSchema.In
4340
write: row.tokens_cache_write,
4441
},
4542
},
46-
location: Location.Ref.make({
43+
location: {
4744
directory: AbsolutePath.make(row.directory),
48-
workspaceID: row.workspace_id ? Workspace.ID.make(row.workspace_id) : undefined,
49-
}),
45+
workspaceID: row.workspace_id ?? undefined,
46+
},
5047
subpath: row.path ? RelativePath.make(row.path) : undefined,
5148
metadata: row.metadata ?? undefined,
5249
permissions: row.permission ?? undefined,

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { SessionEvent } from "./event.js"
1111
import { SessionMessage } from "./message.js"
1212
import { SessionMessageUpdater } from "./message-updater.js"
1313
import { SessionInbox } from "./inbox.js"
14-
import { Workspace } from "@opencode/schema/workspace"
1514
import { InstructionState } from "./instruction-state.js"
1615
import { SessionInboxTable, SessionMessageTable, SessionTable } from "./sql.js"
1716
import { InstructionEntry } from "./instruction-entry.js"
@@ -306,7 +305,7 @@ function run(db: DatabaseService, event: MessageEvent) {
306305
? {
307306
location: {
308307
directory: AbsolutePath.make(row.directory),
309-
workspaceID: row.workspaceID ? Workspace.ID.make(row.workspaceID) : undefined,
308+
workspaceID: row.workspaceID ?? undefined,
310309
},
311310
projectID: row.projectID,
312311
subpath: row.subpath === null ? undefined : RelativePath.make(row.subpath),
@@ -462,7 +461,7 @@ const layer = Layer.effectDiscard(
462461
.values({
463462
id: event.data.sessionID,
464463
project_id: event.data.projectID,
465-
workspace_id: event.data.location.workspaceID ? Workspace.ID.make(event.data.location.workspaceID) : null,
464+
workspace_id: event.data.location.workspaceID ?? null,
466465
parent_id: event.data.parentID,
467466
slug: event.data.slug,
468467
directory: event.data.location.directory,
@@ -492,7 +491,7 @@ const layer = Layer.effectDiscard(
492491
directory: event.data.location.directory,
493492
path: event.data.subpath,
494493
...(event.data.projectID ? { project_id: event.data.projectID } : {}),
495-
workspace_id: event.data.location.workspaceID ? Workspace.ID.make(event.data.location.workspaceID) : null,
494+
workspace_id: event.data.location.workspaceID ?? null,
496495
time_updated: event.created,
497496
})
498497
.where(eq(SessionTable.id, event.data.sessionID))
@@ -767,7 +766,7 @@ const layer = Layer.effectDiscard(
767766
yield* db
768767
.update(SessionTable)
769768
.set({
770-
revert: { ...revert, files: revert.files ? [...revert.files] : undefined },
769+
revert,
771770
time_updated: event.created,
772771
})
773772
.where(eq(SessionTable.id, event.data.sessionID))

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ export const stage = Effect.fn("SessionRevert.stage")(function* (input: {
3030

3131
return yield* Effect.gen(function* () {
3232
const snapshot = yield* Snapshot.Service
33-
const original = input.session.revert?.snapshot
34-
? Snapshot.ID.make(input.session.revert.snapshot)
35-
: yield* snapshot.capture()
33+
const original = input.session.revert?.snapshot ?? (yield* snapshot.capture())
3634
const next = yield* plan(database.db, { sessionID: input.session.id, messageID: input.messageID })
3735
const restore = new Map<RelativePath, Snapshot.ID>()
3836
if (original) {
@@ -63,7 +61,7 @@ export const clear = Effect.fn("SessionRevert.clear")(function* (session: Sessio
6361
yield* Effect.gen(function* () {
6462
const snapshot = yield* Snapshot.Service
6563
if (!session.revert) return
66-
const original = session.revert.snapshot ? Snapshot.ID.make(session.revert.snapshot) : undefined
64+
const original = session.revert.snapshot
6765
if (original)
6866
yield* snapshot.restore({
6967
files: new Map((session.revert.files ?? []).map((file) => [RelativePath.make(file.file), original])),
@@ -108,7 +106,7 @@ const plan = Effect.fn("SessionRevert.plan")(function* (db: Database.Interface["
108106
const message = SessionHistory.decodeMessageRowSync(row)
109107
if (message.type !== "assistant" || !message.snapshot?.start) continue
110108
for (const file of message.snapshot.files ?? [])
111-
if (!files.has(file)) files.set(file, Snapshot.ID.make(message.snapshot.start))
109+
if (!files.has(file)) files.set(file, message.snapshot.start)
112110
}
113111
return files
114112
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export const make = Effect.fn("Session.make")(function* () {
303303
description: input.description,
304304
metadata: input.metadata,
305305
}),
306-
delivery: SessionInbox.Delivery.make(input.delivery ?? "steer"),
306+
delivery: input.delivery ?? "steer",
307307
} satisfies SessionInbox.Item
308308
const admitted = yield* admission
309309
.admit({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ const layer = Layer.effect(
189189
.pipe(Effect.orDie)
190190
return row
191191
? {
192-
sessionID: Session.ID.make(row.session_id),
192+
sessionID: row.session_id,
193193
message: yield* SessionHistory.decodeMessageRow(row).pipe(Effect.orDie),
194194
}
195195
: undefined

‎packages/schema/src/session-event.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,8 @@ export const EphemeralDefinitions = Event.inventory(
725725
)
726726

727727
export const Durable = Schema.Union(DurableDefinitions, { mode: "oneOf" })
728-
.pipe(Schema.toTaggedUnion("type"))
729728
.annotate({ identifier: "Session.Event.Durable" })
729+
.pipe(Schema.toTaggedUnion("type"))
730730
export type DurableEvent = typeof Durable.Type
731731

732732
export const All = Schema.Union([Durable, ...EphemeralDefinitions], { mode: "oneOf" }).pipe(

0 commit comments

Comments
 (0)