diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index dca8085c5b2e..1ee108c0c3d0 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -655,9 +655,10 @@ export namespace SessionPrompt { // Build system prompt, adding structured output instruction if needed const skills = await SystemPrompt.skills(agent) const system = [ - ...(await SystemPrompt.environment(model)), - ...(skills ? [skills] : []), ...(await InstructionPrompt.system()), + // Put the most stable instructions first to maximize prompt-cache reuse. + ...(await SystemPrompt.environment(model, session.time.created)), + ...(skills ? [skills] : []), ] const format = lastUser.format ?? { type: "text" } if (format.type === "json_schema") { diff --git a/packages/opencode/src/session/system.ts b/packages/opencode/src/session/system.ts index ca324652d9dc..902b3e48b48c 100644 --- a/packages/opencode/src/session/system.ts +++ b/packages/opencode/src/session/system.ts @@ -25,7 +25,7 @@ export namespace SystemPrompt { return [PROMPT_DEFAULT] } - export async function environment(model: Provider.Model) { + export async function environment(model: Provider.Model, sessionCreatedAt: number) { const project = Instance.project return [ [ @@ -36,7 +36,8 @@ export namespace SystemPrompt { ` Workspace root folder: ${Instance.worktree}`, ` Is directory a git repo: ${project.vcs === "git" ? "yes" : "no"}`, ` Platform: ${process.platform}`, - ` Today's date: ${new Date().toDateString()}`, + // Keep the session day stable so long-lived sessions do not churn the prompt at midnight. + ` Today's date: ${new Date(sessionCreatedAt).toDateString()}`, ``, ``, ` ${ diff --git a/packages/opencode/test/session/system.test.ts b/packages/opencode/test/session/system.test.ts index 47f5f6fc25dd..7767b0d83933 100644 --- a/packages/opencode/test/session/system.test.ts +++ b/packages/opencode/test/session/system.test.ts @@ -6,6 +6,30 @@ import { SystemPrompt } from "../../src/session/system" import { tmpdir } from "../fixture/fixture" describe("session.system", () => { + test("environment uses the session creation day", async () => { + await using tmp = await tmpdir({ + git: true, + }) + + await Instance.provide({ + directory: tmp.path, + fn: async () => { + const environment = await SystemPrompt.environment( + { + providerID: "openai", + api: { + id: "gpt-5.2", + }, + } as any, + Date.UTC(2026, 2, 22, 23, 59, 59), + ) + + expect(environment).toHaveLength(1) + expect(environment[0]).toContain("Today's date: Sun Mar 22 2026") + }, + }) + }) + test("skills output is sorted by name and stable across calls", async () => { await using tmp = await tmpdir({ git: true,