refactor(schema): apply session review decisions (#35793)
This commit is contained in:
parent
d27746e5b3
commit
ed6ad272ec
142 changed files with 4239 additions and 3174 deletions
|
|
@ -64,7 +64,7 @@ export function fromRow(row: SessionRow): Info {
|
|||
messageID: MessageID.make(row.revert.messageID),
|
||||
partID: row.revert.partID ? PartID.make(row.revert.partID) : undefined,
|
||||
snapshot: row.revert.snapshot,
|
||||
diff: row.revert.diff,
|
||||
diff: "diff" in row.revert ? row.revert.diff : undefined,
|
||||
}
|
||||
: undefined
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ type State = {
|
|||
type Data =
|
||||
| {
|
||||
type: "session"
|
||||
data: SDK.Session
|
||||
data: SDK.SessionV1Info
|
||||
}
|
||||
| {
|
||||
type: "message"
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import { FSUtil } from "@opencode-ai/core/fs-util"
|
|||
import { Hash } from "@opencode-ai/core/util/hash"
|
||||
import { Config } from "@/config/config"
|
||||
import { Global } from "@opencode-ai/core/global"
|
||||
import { Info } from "@opencode-ai/schema/file-diff"
|
||||
import { LegacyInfo } from "@opencode-ai/schema/file-diff"
|
||||
|
||||
export const Patch = Schema.Struct({
|
||||
hash: Schema.String,
|
||||
|
|
@ -17,7 +17,7 @@ export const Patch = Schema.Struct({
|
|||
})
|
||||
export type Patch = typeof Patch.Type
|
||||
|
||||
export const FileDiff = Info
|
||||
export const FileDiff = LegacyInfo
|
||||
export type FileDiff = typeof FileDiff.Type
|
||||
|
||||
const prune = "7.days"
|
||||
|
|
|
|||
|
|
@ -53,7 +53,13 @@ function connected(id = "evt_connected") {
|
|||
return { id, type: "server.connected", data: {} } satisfies RunV2Event
|
||||
}
|
||||
|
||||
function durable(sessionID: string, seq = 0, version = 1) {
|
||||
function durable(sessionID: string, seq?: number): { aggregateID: string; seq: number; version: 1 }
|
||||
function durable<const Version extends 1 | 2>(
|
||||
sessionID: string,
|
||||
seq: number,
|
||||
version: Version,
|
||||
): { aggregateID: string; seq: number; version: Version }
|
||||
function durable(sessionID: string, seq = 0, version: 1 | 2 = 1) {
|
||||
return { aggregateID: sessionID, seq, version }
|
||||
}
|
||||
|
||||
|
|
@ -1311,17 +1317,10 @@ describe("V2 mini transport", () => {
|
|||
{
|
||||
id: "msg_shell",
|
||||
type: "shell" as const,
|
||||
shell: {
|
||||
id: "sh_1",
|
||||
status: "exited",
|
||||
command: "ls",
|
||||
cwd: "/tmp",
|
||||
shell: "/bin/sh",
|
||||
file: "/tmp/opencode-shell",
|
||||
exit: 0,
|
||||
metadata: {},
|
||||
time: { started: 0, completed: 1 },
|
||||
},
|
||||
shellID: "sh_1",
|
||||
status: "exited",
|
||||
command: "ls",
|
||||
exit: 0,
|
||||
output: { output: "file.txt", cursor: 8, size: 8, truncated: false },
|
||||
time: { created: 1, completed: 2 },
|
||||
},
|
||||
|
|
@ -1378,17 +1377,10 @@ describe("V2 mini transport", () => {
|
|||
{
|
||||
id: "msg_failed_shell",
|
||||
type: "shell" as const,
|
||||
shell: {
|
||||
id: "sh_failed",
|
||||
status: "exited",
|
||||
command: "false",
|
||||
cwd: "/tmp",
|
||||
shell: "/bin/sh",
|
||||
file: "/tmp/failed",
|
||||
exit: 7,
|
||||
metadata: {},
|
||||
time: { started: 0, completed: 1 },
|
||||
},
|
||||
shellID: "sh_failed",
|
||||
status: "exited",
|
||||
command: "false",
|
||||
exit: 7,
|
||||
output: { output: "failure output", cursor: 14, size: 14, truncated: false },
|
||||
time: { created: 1, completed: 2 },
|
||||
},
|
||||
|
|
@ -1551,6 +1543,7 @@ describe("V2 mini transport", () => {
|
|||
durable: durable("ses_1"),
|
||||
data: {
|
||||
sessionID: "ses_1",
|
||||
id: input.skill ?? "tigerstyle",
|
||||
name: input.skill ?? "tigerstyle",
|
||||
text: "skill instructions",
|
||||
},
|
||||
|
|
@ -1633,6 +1626,7 @@ describe("V2 mini transport", () => {
|
|||
durable: durable("ses_1"),
|
||||
data: {
|
||||
sessionID: "ses_1",
|
||||
id: "other",
|
||||
name: "other",
|
||||
text: "other instructions",
|
||||
},
|
||||
|
|
@ -1655,6 +1649,7 @@ describe("V2 mini transport", () => {
|
|||
durable: durable("ses_1"),
|
||||
data: {
|
||||
sessionID: "ses_1",
|
||||
id: "tigerstyle",
|
||||
name: "tigerstyle",
|
||||
text: "skill instructions",
|
||||
},
|
||||
|
|
@ -1722,6 +1717,7 @@ describe("V2 mini transport", () => {
|
|||
{
|
||||
id: "msg_skill",
|
||||
type: "skill" as const,
|
||||
skill: "tigerstyle",
|
||||
name: "tigerstyle",
|
||||
text: "skill instructions",
|
||||
time: { created: 2 },
|
||||
|
|
@ -1745,6 +1741,7 @@ describe("V2 mini transport", () => {
|
|||
durable: durable("ses_1"),
|
||||
data: {
|
||||
sessionID: "ses_1",
|
||||
id: "tigerstyle",
|
||||
name: "tigerstyle",
|
||||
text: "skill instructions",
|
||||
},
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import { MessageID, PartID, SessionID, type SessionID as SessionIDType } from ".
|
|||
import { Database } from "@opencode-ai/core/database/database"
|
||||
import { SessionInputTable, SessionMessageTable, SessionTable } from "@opencode-ai/core/session/sql"
|
||||
import { SessionMessage } from "@opencode-ai/core/session/message"
|
||||
import { Agent } from "@opencode-ai/schema/agent"
|
||||
import { ModelV2 } from "@opencode-ai/core/model"
|
||||
import { ProviderV2 } from "@opencode-ai/core/provider"
|
||||
import * as DateTime from "effect/DateTime"
|
||||
|
|
@ -76,7 +77,7 @@ function createTextMessage(sessionID: SessionIDType, text: string) {
|
|||
id: MessageID.ascending(),
|
||||
role: "user",
|
||||
sessionID,
|
||||
agent: "build",
|
||||
agent: Agent.ID.make("build"),
|
||||
model: { providerID: ProviderV2.ID.make("test"), modelID: ModelV2.ID.make("test") },
|
||||
time: { created: Date.now() },
|
||||
})
|
||||
|
|
@ -123,7 +124,7 @@ const insertLegacyAssistantMessage = (sessionID: SessionIDType, seq = 1, time =
|
|||
const message = SessionMessage.Assistant.make({
|
||||
id: SessionMessage.ID.create(),
|
||||
type: "assistant",
|
||||
agent: "build",
|
||||
agent: Agent.ID.make("build"),
|
||||
model: {
|
||||
id: ModelV2.ID.make("model"),
|
||||
providerID: ProviderV2.ID.make("provider"),
|
||||
|
|
@ -380,7 +381,7 @@ describe("session HttpApi", () => {
|
|||
yield* insertLegacyAssistantMessage(parent.id)
|
||||
|
||||
expect(
|
||||
(yield* requestJson<{ data: SessionMessage.Message[] }>(`/api/session/${parent.id}/message`, {
|
||||
(yield* requestJson<{ data: SessionMessage.Info[] }>(`/api/session/${parent.id}/message`, {
|
||||
headers,
|
||||
})).data,
|
||||
).toMatchObject([{ type: "assistant" }])
|
||||
|
|
@ -474,7 +475,7 @@ describe("session HttpApi", () => {
|
|||
})
|
||||
|
||||
const messagePage = yield* request(`/api/session/${session.id}/message?limit=1`, { headers })
|
||||
const messageBody = yield* json<{ data: SessionMessage.Message[]; cursor: { next?: string } }>(messagePage)
|
||||
const messageBody = yield* json<{ data: SessionMessage.Info[]; cursor: { next?: string } }>(messagePage)
|
||||
const messageCursor = messageBody.cursor.next
|
||||
expect(messageCursor).toBeTruthy()
|
||||
expect(messageBody.data.map((message) => message.id)).toEqual([secondMessage.id])
|
||||
|
|
@ -488,7 +489,7 @@ describe("session HttpApi", () => {
|
|||
headers,
|
||||
})
|
||||
expect(
|
||||
(yield* json<{ data: SessionMessage.Message[] }>(nextMessagePage)).data.map((message) => message.id),
|
||||
(yield* json<{ data: SessionMessage.Info[] }>(nextMessagePage)).data.map((message) => message.id),
|
||||
).toEqual([firstMessage.id])
|
||||
|
||||
const legacyMessageCursor = Buffer.from(
|
||||
|
|
@ -498,7 +499,7 @@ describe("session HttpApi", () => {
|
|||
headers,
|
||||
})
|
||||
expect(
|
||||
(yield* json<{ data: SessionMessage.Message[] }>(legacyMessagePage)).data.map((message) => message.id),
|
||||
(yield* json<{ data: SessionMessage.Info[] }>(legacyMessagePage)).data.map((message) => message.id),
|
||||
).toEqual([firstMessage.id])
|
||||
|
||||
const messageCursorWithOrder = yield* request(
|
||||
|
|
@ -625,7 +626,7 @@ describe("session HttpApi", () => {
|
|||
})
|
||||
expect(wake.status).toBe(200)
|
||||
const message = yield* pollWithTimeout(
|
||||
requestJson<{ data: SessionMessage.Message[] }>(`/api/session/${session.id}/message`, { headers }).pipe(
|
||||
requestJson<{ data: SessionMessage.Info[] }>(`/api/session/${session.id}/message`, { headers }).pipe(
|
||||
Effect.map(({ data }) => data.find((message) => message.id === wakeID)),
|
||||
),
|
||||
"V2 prompt was not promoted after wake",
|
||||
|
|
@ -637,28 +638,25 @@ describe("session HttpApi", () => {
|
|||
)
|
||||
|
||||
it.instance(
|
||||
"returns v2 public unavailable errors for unfinished session mutations",
|
||||
"supports current session compact and wait endpoints",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const test = yield* TestInstance
|
||||
const headers = { "x-opencode-directory": test.directory }
|
||||
const session = yield* createSession({ title: "v2 unavailable" })
|
||||
|
||||
const compact = yield* request(`/api/session/${session.id}/compact`, { method: "POST", headers })
|
||||
expect(compact.status).toBe(503)
|
||||
expect(yield* responseJson(compact)).toEqual({
|
||||
_tag: "ServiceUnavailableError",
|
||||
message: "Session compact is not available yet",
|
||||
service: "session.compact",
|
||||
const compact = yield* request(`/api/session/${session.id}/compact`, {
|
||||
method: "POST",
|
||||
headers: { ...headers, "content-type": "application/json" },
|
||||
body: JSON.stringify({}),
|
||||
})
|
||||
expect(compact.status).toBe(200)
|
||||
expect(yield* responseJson(compact)).toMatchObject({
|
||||
data: { type: "compaction", sessionID: session.id },
|
||||
})
|
||||
|
||||
const wait = yield* request(`/api/session/${session.id}/wait`, { method: "POST", headers })
|
||||
expect(wait.status).toBe(503)
|
||||
expect(yield* responseJson(wait)).toEqual({
|
||||
_tag: "ServiceUnavailableError",
|
||||
message: "Session wait is not available yet",
|
||||
service: "session.wait",
|
||||
})
|
||||
expect(wait.status).toBe(204)
|
||||
}),
|
||||
{ git: true, config: { formatter: false, lsp: false } },
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import * as DateTime from "effect/DateTime"
|
||||
import { DateTime, Effect } from "effect"
|
||||
import { SessionID } from "../../src/session/schema"
|
||||
import { EventV2 } from "@opencode-ai/core/event"
|
||||
import { ModelV2 } from "@opencode-ai/core/model"
|
||||
|
|
@ -8,6 +7,9 @@ import { ProviderV2 } from "@opencode-ai/core/provider"
|
|||
import { SessionEvent } from "@opencode-ai/core/session/event"
|
||||
import { SessionMessageUpdater } from "@opencode-ai/core/session/message-updater"
|
||||
import { SessionMessage } from "@opencode-ai/core/session/message"
|
||||
import { Agent } from "@opencode-ai/schema/agent"
|
||||
import { Money } from "@opencode-ai/schema/money"
|
||||
import { Snapshot } from "@opencode-ai/schema/snapshot"
|
||||
|
||||
function durable(sessionID: SessionID, seq = 0, version = 1) {
|
||||
return { aggregateID: sessionID, seq: EventV2.Seq.make(seq), version: EventV2.Version.make(version) }
|
||||
|
|
@ -27,13 +29,13 @@ test.skip("step snapshots carry over to assistant messages", () => {
|
|||
data: {
|
||||
sessionID,
|
||||
assistantMessageID,
|
||||
agent: "build",
|
||||
agent: Agent.ID.make("build"),
|
||||
model: {
|
||||
id: ModelV2.ID.make("model"),
|
||||
providerID: ProviderV2.ID.make("provider"),
|
||||
variant: ModelV2.VariantID.make("default"),
|
||||
},
|
||||
snapshot: "before",
|
||||
snapshot: Snapshot.ID.make("before"),
|
||||
},
|
||||
} satisfies SessionEvent.Event),
|
||||
)
|
||||
|
|
@ -50,21 +52,24 @@ test.skip("step snapshots carry over to assistant messages", () => {
|
|||
sessionID,
|
||||
assistantMessageID,
|
||||
finish: "stop",
|
||||
cost: 0,
|
||||
cost: Money.USD.zero,
|
||||
tokens: {
|
||||
input: 1,
|
||||
output: 2,
|
||||
reasoning: 0,
|
||||
cache: { read: 0, write: 0 },
|
||||
},
|
||||
snapshot: "after",
|
||||
snapshot: Snapshot.ID.make("after"),
|
||||
},
|
||||
} satisfies SessionEvent.Event),
|
||||
)
|
||||
|
||||
expect(state.messages[0]?.type).toBe("assistant")
|
||||
if (state.messages[0]?.type !== "assistant") return
|
||||
expect(state.messages[0].snapshot).toEqual({ start: "before", end: "after" })
|
||||
expect(state.messages[0].snapshot).toEqual({
|
||||
start: Snapshot.ID.make("before"),
|
||||
end: Snapshot.ID.make("after"),
|
||||
})
|
||||
expect(state.messages[0].finish).toBe("stop")
|
||||
})
|
||||
|
||||
|
|
@ -82,7 +87,7 @@ test.skip("text ended populates assistant text content", () => {
|
|||
data: {
|
||||
sessionID,
|
||||
assistantMessageID,
|
||||
agent: "build",
|
||||
agent: Agent.ID.make("build"),
|
||||
model: {
|
||||
id: ModelV2.ID.make("model"),
|
||||
providerID: ProviderV2.ID.make("provider"),
|
||||
|
|
@ -141,7 +146,7 @@ test.skip("tool completion stores completed timestamp", () => {
|
|||
data: {
|
||||
sessionID,
|
||||
assistantMessageID,
|
||||
agent: "build",
|
||||
agent: Agent.ID.make("build"),
|
||||
model: {
|
||||
id: ModelV2.ID.make("model"),
|
||||
providerID: ProviderV2.ID.make("provider"),
|
||||
|
|
@ -213,7 +218,7 @@ test.skip("tool completion stores completed timestamp", () => {
|
|||
})
|
||||
})
|
||||
|
||||
test("compaction events reduce to compaction message only when completed", () => {
|
||||
test("compaction events reduce to a compaction message through completion", () => {
|
||||
const state: SessionMessageUpdater.MemoryState = { messages: [] }
|
||||
const sessionID = SessionID.make("session")
|
||||
const id = EventV2.ID.create()
|
||||
|
|
@ -224,15 +229,25 @@ test("compaction events reduce to compaction message only when completed", () =>
|
|||
id,
|
||||
created: DateTime.makeUnsafe(0),
|
||||
type: "session.compaction.started",
|
||||
durable: durable(sessionID),
|
||||
durable: durable(sessionID, 0, 2),
|
||||
data: {
|
||||
sessionID,
|
||||
reason: "auto",
|
||||
recent: "recent context",
|
||||
},
|
||||
} satisfies SessionEvent.Event),
|
||||
)
|
||||
|
||||
expect(state.messages).toEqual([])
|
||||
expect(state.messages).toMatchObject([
|
||||
{
|
||||
id: SessionMessage.ID.fromEvent(id),
|
||||
type: "compaction",
|
||||
reason: "auto",
|
||||
recent: "recent context",
|
||||
status: "running",
|
||||
summary: "",
|
||||
},
|
||||
])
|
||||
|
||||
Effect.runSync(
|
||||
SessionMessageUpdater.update(SessionMessageUpdater.memory(state), {
|
||||
|
|
@ -263,7 +278,7 @@ test("compaction events reduce to compaction message only when completed", () =>
|
|||
id: endedID,
|
||||
created: DateTime.makeUnsafe(0),
|
||||
type: "session.compaction.ended",
|
||||
durable: durable(sessionID, 1),
|
||||
durable: durable(sessionID, 3),
|
||||
data: {
|
||||
sessionID,
|
||||
reason: "auto",
|
||||
|
|
@ -275,9 +290,10 @@ test("compaction events reduce to compaction message only when completed", () =>
|
|||
|
||||
expect(state.messages).toHaveLength(1)
|
||||
expect(state.messages[0]).toMatchObject({
|
||||
id: SessionMessage.ID.fromEvent(endedID),
|
||||
id: SessionMessage.ID.fromEvent(id),
|
||||
type: "compaction",
|
||||
reason: "auto",
|
||||
status: "completed",
|
||||
summary: "final summary",
|
||||
recent: "recent context",
|
||||
time: { created: DateTime.makeUnsafe(0) },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue