Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ConfigProvider } from "./config/provider"
import { ConfigReference } from "./config/reference"
import { ConfigToolOutput } from "./config/tool-output"
import { ConfigWatcher } from "./config/watcher"
import { Flag } from "./flag/flag"
import { ConfigV1 } from "./v1/config/config"
import { ConfigMigrateV1 } from "./v1/config/migrate"

Expand Down Expand Up @@ -199,7 +200,15 @@ export const layer = Layer.effect(
const supplementary = yield* Effect.forEach(directories, loadDirectory).pipe(Effect.orDie)
// Apply general settings first and more specific settings last:
// global config, project files, then `.opencode` files.
const configs = [...(supplementary[0] ?? []), ...direct, ...supplementary.slice(1).flat()]
const envCompaction = new ConfigCompaction.Info({
...(Flag.OPENCODE_DISABLE_AUTOCOMPACT ? { auto: false } : {}),
...(Flag.OPENCODE_DISABLE_PRUNE ? { prune: false } : {}),
})
const envConfig =
envCompaction.auto === undefined && envCompaction.prune === undefined
? []
: [new Document({ type: "document", info: new Info({ compaction: envCompaction }) })]
const configs = [...(supplementary[0] ?? []), ...direct, ...supplementary.slice(1).flat(), ...envConfig]
// Rules use the opposite order so a user-global rule can override a
// repository rule. Statement order inside each file stays unchanged.
yield* policy.load(
Expand All @@ -210,9 +219,7 @@ export const layer = Layer.effect(
)

return Service.of({
entries: Effect.fn("Config.entries")(function* () {
return configs
}),
entries: Effect.fn("Config.entries")(() => Effect.succeed(configs)),
})
}),
)
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/flag/flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ export const Flag = {
OPENCODE_CONFIG_CONTENT: process.env["OPENCODE_CONFIG_CONTENT"],
OPENCODE_DISABLE_AUTOUPDATE: truthy("OPENCODE_DISABLE_AUTOUPDATE"),
OPENCODE_ALWAYS_NOTIFY_UPDATE: truthy("OPENCODE_ALWAYS_NOTIFY_UPDATE"),
OPENCODE_DISABLE_PRUNE: truthy("OPENCODE_DISABLE_PRUNE"),
OPENCODE_DISABLE_TERMINAL_TITLE: truthy("OPENCODE_DISABLE_TERMINAL_TITLE"),
OPENCODE_SHOW_TTFD: truthy("OPENCODE_SHOW_TTFD"),
OPENCODE_DISABLE_AUTOCOMPACT: truthy("OPENCODE_DISABLE_AUTOCOMPACT"),
OPENCODE_DISABLE_MODELS_FETCH: truthy("OPENCODE_DISABLE_MODELS_FETCH"),
OPENCODE_DISABLE_MOUSE: truthy("OPENCODE_DISABLE_MOUSE"),
OPENCODE_FAKE_VCS: process.env["OPENCODE_FAKE_VCS"],
Expand All @@ -51,6 +49,12 @@ export const Flag = {

// Evaluated at access time (not module load) because tests, the CLI, and
// external tooling set these env vars at runtime.
get OPENCODE_DISABLE_PRUNE() {
return truthy("OPENCODE_DISABLE_PRUNE")
},
get OPENCODE_DISABLE_AUTOCOMPACT() {
return truthy("OPENCODE_DISABLE_AUTOCOMPACT")
},
get OPENCODE_DISABLE_PROJECT_CONFIG() {
return truthy("OPENCODE_DISABLE_PROJECT_CONFIG")
},
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/session/compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export const buildPrompt = (input: { readonly previousSummary?: string; readonly
export const make = (dependencies: Dependencies) => {
const config = settings(dependencies.config)
const compactAfterOverflow = Effect.fn("SessionCompaction.compactAfterOverflow")(function* (input: Input) {
if (!config.auto) return false
const context = input.model.route.defaults.limits?.context
if (context === undefined || context <= 0) return false
const output = input.request.generation?.maxTokens ?? input.model.route.defaults.limits?.output ?? 0
Expand Down
60 changes: 60 additions & 0 deletions packages/core/test/config-env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import path from "path"
import fs from "fs/promises"
import { expect, test } from "bun:test"
import { Effect, Layer } from "effect"
import { Config } from "@opencode-ai/core/config"
import { FSUtil } from "@opencode-ai/core/fs-util"
import { Global } from "@opencode-ai/core/global"
import { Location } from "@opencode-ai/core/location"
import { Policy } from "@opencode-ai/core/policy"
import { AbsolutePath } from "@opencode-ai/core/schema"
import { location } from "./fixture/location"
import { tmpdir } from "./fixture/tmpdir"

function testLayer(directory: string) {
return Config.locationLayer.pipe(
Layer.provide(FSUtil.defaultLayer),
Layer.provide(Global.layerWith({ config: path.join(directory, "global") })),
Layer.provide(
Layer.succeed(
Location.Service,
Location.Service.of(
location(
{ directory: AbsolutePath.make(directory) },
{ projectDirectory: AbsolutePath.make(directory) },
),
),
),
),
)
}

test("applies compaction disable env flags as highest-priority overrides", async () => {
const tmp = await tmpdir()
const previous = {
OPENCODE_DISABLE_AUTOCOMPACT: process.env.OPENCODE_DISABLE_AUTOCOMPACT,
OPENCODE_DISABLE_PRUNE: process.env.OPENCODE_DISABLE_PRUNE,
}
process.env.OPENCODE_DISABLE_AUTOCOMPACT = "true"
process.env.OPENCODE_DISABLE_PRUNE = "true"

try {
await fs.writeFile(path.join(tmp.path, "opencode.json"), JSON.stringify({ compaction: { auto: true, prune: true } }))

const documents = await Effect.runPromise(
Config.Service.pipe(
Effect.flatMap((config) => config.entries()),
Effect.map((entries) => entries.filter((entry) => entry.type === "document")),
Effect.provide(testLayer(tmp.path)),
),
)

expect(documents.at(-1)?.info.compaction).toMatchObject({ auto: false, prune: false })
} finally {
if (previous.OPENCODE_DISABLE_AUTOCOMPACT === undefined) delete process.env.OPENCODE_DISABLE_AUTOCOMPACT
else process.env.OPENCODE_DISABLE_AUTOCOMPACT = previous.OPENCODE_DISABLE_AUTOCOMPACT
if (previous.OPENCODE_DISABLE_PRUNE === undefined) delete process.env.OPENCODE_DISABLE_PRUNE
else process.env.OPENCODE_DISABLE_PRUNE = previous.OPENCODE_DISABLE_PRUNE
await tmp[Symbol.asyncDispose]()
}
})
84 changes: 84 additions & 0 deletions packages/core/test/session-compaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
import { expect, test } from "bun:test"
import { LLM, LLMEvent, Message, Model, type LLMRequest } from "@opencode-ai/llm"
import * as OpenAIChat from "@opencode-ai/llm/protocols/openai-chat"
import { Config } from "@opencode-ai/core/config"
import { ConfigCompaction } from "@opencode-ai/core/config/compaction"
import { EventV2 } from "@opencode-ai/core/event"
import { SessionCompaction } from "@opencode-ai/core/session/compaction"
import { SessionMessage } from "@opencode-ai/core/session/message"
import { SessionSchema } from "@opencode-ai/core/session/schema"
import { DateTime, Effect, Stream } from "effect"

const compactModel = Model.make({
id: "compact",
provider: "fake",
route: OpenAIChat.route.with({ limits: { context: 20_000, output: 1_000 } }),
})

function eventRecorder(published: string[]): EventV2.Interface {
return EventV2.Service.of({
publish: (definition, data) =>
Effect.sync(() => {
published.push(definition.type)
return { id: EventV2.ID.create(), type: definition.type, data }
}),
subscribe: () => Stream.empty,
all: () => Stream.empty,
aggregateEvents: () => Stream.empty,
sync: () => Effect.succeed(Effect.void),
listen: () => Effect.succeed(Effect.void),
beforeCommit: () => Effect.void,
project: () => Effect.void,
replay: () => Effect.void,
replayAll: () => Effect.succeed(undefined),
remove: () => Effect.void,
claim: () => Effect.void,
})
}

function overflowInput(request: LLMRequest) {
return {
sessionID: SessionSchema.ID.create(),
model: compactModel,
request,
entries: [
{
seq: 1,
message: new SessionMessage.User({
id: SessionMessage.ID.create(),
type: "user",
text: "Earlier question ".repeat(700),
time: { created: DateTime.makeUnsafe(1) },
}),
},
],
}
}

test("compaction describes tool media without embedding base64", () => {
const base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB"
Expand All @@ -16,3 +70,33 @@ test("compaction describes tool media without embedding base64", () => {
expect(serialized).toBe("Image read successfully\n[Attached image/png: pixel.png]")
expect(serialized).not.toContain(base64)
})

test("provider overflow recovery does not compact when automatic compaction is disabled", async () => {
const published: string[] = []
const compaction = SessionCompaction.make({
events: eventRecorder(published),
llm: {
stream: () => Stream.fromIterable([LLMEvent.textDelta({ id: "summary", text: "## Goal\n- Should not compact" })]),
},
config: [
new Config.Document({
type: "document",
info: new Config.Info({ compaction: new ConfigCompaction.Info({ auto: false }) }),
}),
],
})

const compacted = await Effect.runPromise(
compaction.compactAfterOverflow(
overflowInput(
LLM.request({
model: compactModel,
messages: [Message.user("Continue")],
}),
),
),
)

expect(compacted).toBe(false)
expect(published).toEqual([])
})
Loading