Skip to content

Commit 5650694

Browse files
committed
fix(core): render the session ID block after all shared instructions
Review follow-up to anomalyco#51055: placing core/session right after core/date still left the Code Mode catalog, AGENTS.md, skills, references, and MCP instructions after the first per-session byte, so cross-session prefix reuse barely improved (~2.8K → ~2.9K tokens measured by the reporter). InstructionBuiltIns now exposes the session ID as a separate source, and SessionContext.combine renders it last — after discovered instructions, skills, references, MCP, and session entries — so nearly the whole system text is shared across sessions. Built-in env and date keep their early position, and the context tests carry mocks for the new method. Fixes anomalyco#51007
1 parent 317ea31 commit 5650694

6 files changed

Lines changed: 36 additions & 15 deletions

File tree

‎packages/core/src/instructions/builtins.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { Location } from "../location.js"
88
import { Instructions } from "./index.js"
99

1010
export interface Interface {
11-
readonly load: (sessionID: Session.ID) => Effect.Effect<Instructions.List>
11+
readonly load: () => Effect.Effect<Instructions.List>
12+
readonly session: (sessionID: Session.ID) => Effect.Effect<Instructions.List>
1213
}
1314

1415
export class Service extends Context.Service<Service, Interface>()("@opencode/InstructionBuiltIns") {}
@@ -19,7 +20,7 @@ const layer = Layer.effect(
1920
const global = yield* Global.Service
2021
const location = yield* Location.Service
2122
return Service.of({
22-
load: (sessionID) =>
23+
load: () =>
2324
Effect.succeed(
2425
Instructions.combine([
2526
Instructions.make({
@@ -52,8 +53,14 @@ const layer = Layer.effect(
5253
changed: (_previous, date) => `Today's date is now: ${date}`,
5354
},
5455
}),
55-
// The session ID is unique per session; keeping it out of the shared
56-
// prefix lets provider prompt caches match across sessions.
56+
]),
57+
),
58+
// The session ID is unique per session. Consumers render this block last in the combined
59+
// instruction list so the shared prefix (env, date, Code Mode catalog, AGENTS.md, skills,
60+
// MCP) stays cacheable across sessions; anything after the first unique byte is uncachable.
61+
session: (sessionID) =>
62+
Effect.succeed(
63+
Instructions.combine([
5764
Instructions.make({
5865
key: Instructions.Key.make("core/session"),
5966
codec: Schema.toCodecJson(Schema.String),

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ const layer = Layer.effect(
132132
const loaded = yield* Effect.all(
133133
{
134134
tools: registry.snapshot(permissions),
135-
builtins: builtins.load(sessionID),
135+
builtins: builtins.load(),
136+
sessionBlock: builtins.session(sessionID),
136137
discovery: discovery.load(),
137138
skills: skillInstructions.load(permissions),
138139
references: referenceInstructions.load(),
@@ -152,6 +153,9 @@ const layer = Layer.effect(
152153
loaded.references,
153154
loaded.mcp,
154155
loaded.entries,
156+
// Last so the per-session ID does not break the cross-session cacheable
157+
// prefix shared by everything above it.
158+
loaded.sessionBlock,
155159
]),
156160
tools: loaded.tools,
157161
}

‎packages/core/test/instructions/builtins.test.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ const it = testEffect(
3636
)
3737

3838
describe("InstructionBuiltIns", () => {
39-
it.effect("loads location-scoped environment and host-local date instructions", () =>
39+
it.effect("loads location-scoped environment and host-local date instructions without the session ID", () =>
4040
Effect.gen(function* () {
4141
yield* TestClock.setTime(timestamp)
4242
const context = yield* InstructionBuiltIns.Service
43-
const initialized = yield* readInitial(yield* context.load(sessionID))
43+
const initialized = yield* readInitial(yield* context.load())
4444

4545
expect(initialized.text).toBe(
4646
[
@@ -54,21 +54,29 @@ describe("InstructionBuiltIns", () => {
5454
"</env>",
5555
"",
5656
`Today's date: ${localDate(timestamp)}`,
57-
"",
58-
`Current conversation session ID: ${sessionID}`,
5957
].join("\n"),
6058
)
6159
}),
6260
)
6361

62+
it.effect("renders the session ID as a separate trailing block", () =>
63+
Effect.gen(function* () {
64+
yield* TestClock.setTime(timestamp)
65+
const context = yield* InstructionBuiltIns.Service
66+
const initialized = yield* readInitial(yield* context.session(sessionID))
67+
68+
expect(initialized.text).toBe(`Current conversation session ID: ${sessionID}`)
69+
}),
70+
)
71+
6472
it.effect("updates the date without repeating unchanged environment instructions", () =>
6573
Effect.gen(function* () {
6674
yield* TestClock.setTime(timestamp)
6775
const context = yield* InstructionBuiltIns.Service
68-
const initialized = yield* readInitial(yield* context.load(sessionID))
76+
const initialized = yield* readInitial(yield* context.load())
6977

7078
yield* TestClock.setTime(timestamp + 24 * 60 * 60 * 1000)
71-
const refreshed = yield* readUpdate(yield* context.load(sessionID), initialized)
79+
const refreshed = yield* readUpdate(yield* context.load(), initialized)
7280

7381
expect(refreshed.text).toBe(`Today's date is now: ${localDate(timestamp + 24 * 60 * 60 * 1000)}`)
7482
}),
@@ -78,10 +86,10 @@ describe("InstructionBuiltIns", () => {
7886
Effect.gen(function* () {
7987
yield* TestClock.setTime(timestamp)
8088
const context = yield* InstructionBuiltIns.Service
81-
const initialized = yield* readInitial(yield* context.load(sessionID))
89+
const initialized = yield* readInitial(yield* context.load())
8290

8391
yield* TestClock.setTime(timestamp + 60 * 60 * 1000)
84-
expect((yield* readUpdate(yield* context.load(sessionID), initialized)).changed).toBe(false)
92+
expect((yield* readUpdate(yield* context.load(), initialized)).changed).toBe(false)
8593
}),
8694
)
8795
})

‎packages/core/test/session-generate.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const builtins = Layer.mock(InstructionBuiltIns.Service, {
105105
},
106106
}),
107107
),
108+
session: () => Effect.succeed(Instructions.empty),
108109
})
109110
const discovery = Layer.mock(InstructionDiscovery.Service, {
110111
project: true,
@@ -236,7 +237,7 @@ const setup = Effect.gen(function* () {
236237
db,
237238
bus,
238239
session,
239-
instructions: yield* instructionBuiltIns.load(sessionID),
240+
instructions: yield* Effect.all([instructionBuiltIns.load(), instructionBuiltIns.session(sessionID)]).pipe(Effect.map(([early, late]) => Instructions.combine([early, late]))),
240241
instances: Instance.Service.of({
241242
// Generation only exercises the Location's model context.
242243
provide: () =>

‎packages/core/test/session-runner-recorded.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const models = Layer.mock(SessionRunnerModel.Service)({
7474
}),
7575
),
7676
})
77-
const systemContext = Layer.mock(InstructionBuiltIns.Service, { load: () => Effect.succeed(Instructions.empty) })
77+
const systemContext = Layer.mock(InstructionBuiltIns.Service, { load: () => Effect.succeed(Instructions.empty), session: () => Effect.succeed(Instructions.empty) })
7878
const instructionContext = Layer.mock(InstructionDiscovery.Service, {
7979
project: true,
8080
global: true,

‎packages/core/test/session-runner.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ const layer = Layer.unwrap(
355355
},
356356
}),
357357
),
358+
session: () => Effect.succeed(Instructions.empty),
358359
})
359360
const instructionContext = Layer.mock(InstructionDiscovery.Service, {
360361
project: true,

0 commit comments

Comments
 (0)