mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
refactor(core): leave native provider compaction without instructions
This commit is contained in:
parent
b483dd45a9
commit
af69cf48cd
2 changed files with 9 additions and 52 deletions
|
|
@ -96,8 +96,6 @@ type Streamed = {
|
|||
readonly providerState?: SessionMessage.ProviderState
|
||||
}
|
||||
|
||||
const instructionsSection = (instructions: string) => `Additional instructions for this summary:\n${instructions}`
|
||||
|
||||
const NOTHING_TO_COMPACT: Failure = { error: { type: "compaction.unavailable", message: "Nothing to compact yet" } }
|
||||
/** After each "too long" rejection, the next attempt aims at this share of the first rejected request's size. */
|
||||
const SHRINK_STEPS = [0.7, 0.5, 0.35]
|
||||
|
|
@ -155,7 +153,7 @@ export const buildPrompt = (update: boolean, legacy = false, instructions?: stri
|
|||
"Summarize only what the user and the assistant said and did. Leave out instructions and setup the assistant was given rather than told by the user: repository conventions, instruction files such as AGENTS.md, and environment details like the session ID. The next agent receives current versions of all of these separately.",
|
||||
SUMMARY_TEMPLATE,
|
||||
SUMMARY_RULES,
|
||||
...(instructions?.trim() ? [instructionsSection(instructions.trim())] : []),
|
||||
...(instructions?.trim() ? [`Additional instructions for this summary:\n${instructions.trim()}`] : []),
|
||||
"Do not continue the task or call tools.",
|
||||
"Return only the structured summary in the requested format. Do not include a preamble, explanation, or other commentary.",
|
||||
]
|
||||
|
|
@ -370,21 +368,7 @@ export const layer = Layer.effect(
|
|||
}),
|
||||
)
|
||||
|
||||
// The provider writes the checkpoint itself, so manual instructions ride as one extra user message. It is added
|
||||
// to the request after fitting, so shrinking never trades a real exchange for it, and it is removed from a provider
|
||||
// replacement, so it never persists.
|
||||
const instructions = trigger.reason === "manual" ? trigger.instructions?.trim() : undefined
|
||||
const note = instructions ? instructionsSection(instructions) : undefined
|
||||
const isNote = (message: Message) =>
|
||||
message.role === "user" &&
|
||||
message.content.length === 1 &&
|
||||
message.content[0]?.type === "text" &&
|
||||
message.content[0].text === note
|
||||
|
||||
const send = (fitted: LLMRequest) => {
|
||||
const request = note
|
||||
? LLMRequest.update(fitted, { messages: [...fitted.messages, Message.user(note)] })
|
||||
: fitted
|
||||
const send = (request: LLMRequest) => {
|
||||
if (LLMClient.canCompact(request, { mechanism: "trigger" })) {
|
||||
return Effect.gen(function* () {
|
||||
const history = yield* SessionHistory.load(db, context.session.id, "local").pipe(Effect.orDie)
|
||||
|
|
@ -394,19 +378,14 @@ export const layer = Layer.effect(
|
|||
})
|
||||
}
|
||||
if (LLMClient.canCompact(request)) {
|
||||
return llm.compact(request, { mechanism: "endpoint", http: prepared.options.http }).pipe(
|
||||
Effect.flatMap((response) =>
|
||||
toResult(
|
||||
response.replacement.filter((message) => !isNote(message)),
|
||||
response.usage,
|
||||
),
|
||||
),
|
||||
)
|
||||
return llm
|
||||
.compact(request, { mechanism: "endpoint", http: prepared.options.http })
|
||||
.pipe(Effect.flatMap((response) => toResult(response.replacement, response.usage)))
|
||||
}
|
||||
return unsupported(`Native compaction is not supported for ${request.model.provider}/${request.model.route.id}`)
|
||||
}
|
||||
|
||||
return yield* deliver(trigger, prepared, "", budget - (note ? Token.estimate(note) : 0), send)
|
||||
return yield* deliver(trigger, prepared, "", budget, send)
|
||||
})
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ const setup = Effect.fnUntraced(function* (options: { endpoint?: boolean } = {})
|
|||
const hooks = yield* PluginHooks.Service
|
||||
const blocked = Deferred.makeUnsafe<void>()
|
||||
const hanging = Promise.withResolvers<Response>()
|
||||
const state = { failure: false, flaky: false, hang: false, overflow: false, echoLastUser: false, calls: 0 }
|
||||
const state = { failure: false, flaky: false, hang: false, overflow: false, calls: 0 }
|
||||
const bodies: Record<string, unknown>[] = []
|
||||
const headers: Headers[] = []
|
||||
const server = yield* Effect.acquireRelease(
|
||||
|
|
@ -111,10 +111,6 @@ const setup = Effect.fnUntraced(function* (options: { endpoint?: boolean } = {})
|
|||
object: "response.compaction",
|
||||
output: [
|
||||
{ type: "message", role: "user", content: [{ type: "input_text", text: "endpoint retained" }] },
|
||||
// A provider may keep any user message it was sent, including the newest one.
|
||||
...(state.echoLastUser
|
||||
? [(bodies.at(-1)?.input as unknown[]).findLast((item) => JSON.stringify(item).includes('"user"'))]
|
||||
: []),
|
||||
checkpoint,
|
||||
],
|
||||
usage: { input_tokens: 20, output_tokens: 4, total_tokens: 24 },
|
||||
|
|
@ -215,12 +211,11 @@ const setup = Effect.fnUntraced(function* (options: { endpoint?: boolean } = {})
|
|||
}
|
||||
})
|
||||
// Opens the compaction's message as the runner does when it delivers `/compact`.
|
||||
const compactWith = Effect.fnUntraced(function* (instructions?: string) {
|
||||
const compact = Effect.gen(function* () {
|
||||
const inputID = SessionMessage.ID.create()
|
||||
yield* bus.publish(SessionEvent.Compaction.Started, { sessionID, reason: "manual", recent: "", inputID })
|
||||
return yield* compaction.compact({ reason: "manual", context: yield* load, inputID, instructions })
|
||||
return yield* compaction.compact({ reason: "manual", context: yield* load, inputID })
|
||||
})
|
||||
const compact = compactWith()
|
||||
const checkpoint = Effect.gen(function* () {
|
||||
const messages = (yield* load).messages
|
||||
const last = messages.findLast((message) => message.type === "compaction" && message.status === "completed")
|
||||
|
|
@ -234,7 +229,6 @@ const setup = Effect.fnUntraced(function* (options: { endpoint?: boolean } = {})
|
|||
})
|
||||
return {
|
||||
compact,
|
||||
compactWith,
|
||||
// An automatic compaction that skips the "is it due" check, which a context this small never passes.
|
||||
overflow: Effect.gen(function* () {
|
||||
return yield* compaction.compact({ reason: "overflow", context: yield* load })
|
||||
|
|
@ -337,22 +331,6 @@ it.live(
|
|||
15000,
|
||||
)
|
||||
|
||||
for (const endpoint of [false, true])
|
||||
it.live(`manual instructions are the last message the provider compacts (endpoint: ${endpoint})`, () =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* setup({ endpoint })
|
||||
fixture.state.echoLastUser = true
|
||||
yield* fixture.prompt("Original user")
|
||||
expect(yield* fixture.compactWith("Keep only the parser work")).toEqual({ status: "completed" })
|
||||
const input = (fixture.bodies[0] as { input: unknown[] }).input
|
||||
expect(JSON.stringify(input.at(endpoint ? -1 : -2))).toContain(
|
||||
"Additional instructions for this summary:\\nKeep only the parser work",
|
||||
)
|
||||
if (!endpoint) expect(input.at(-1)).toEqual({ type: "compaction_trigger" })
|
||||
expect(JSON.stringify((yield* fixture.checkpoint).messages)).not.toContain("Keep only the parser work")
|
||||
}),
|
||||
)
|
||||
|
||||
it.live("manual and automatic endpoint compaction keep the provider replacement unchanged", () =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* setup({ endpoint: true })
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue