From f3db564b2e624184425bc0215199e9f18f2e3fc4 Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Thu, 1 Jan 2026 20:23:23 +0900 Subject: [PATCH] fix: reduce context duplication from ~22k to ~11k tokens (#383) * fix: reduce context duplication from ~22k to ~11k tokens Remove redundant env info and root AGENTS.md injection that OpenCode already provides, addressing significant token waste on startup. Changes: - src/agents/utils.ts: Remove duplicated env fields (working dir, platform, date) from createEnvContext(), keep only OmO-specific fields (time, timezone, locale) - src/hooks/directory-agents-injector/index.ts: Skip root AGENTS.md injection since OpenCode's system.ts already loads it via custom() Fixes #379 * refactor: remove unused _directory parameter from createEnvContext() --------- Co-authored-by: sisyphus-dev-ai --- src/agents/utils.ts | 27 ++++++++------------ src/hooks/directory-agents-injector/index.ts | 13 +++++++--- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/agents/utils.ts b/src/agents/utils.ts index 78213a8..1f0d0ac 100644 --- a/src/agents/utils.ts +++ b/src/agents/utils.ts @@ -29,18 +29,17 @@ function buildAgent(source: AgentSource, model?: string): AgentConfig { return isFactory(source) ? source(model) : source } -export function createEnvContext(directory: string): string { +/** + * Creates OmO-specific environment context (time, timezone, locale). + * Note: Working directory, platform, and date are already provided by OpenCode's system.ts, + * so we only include fields that OpenCode doesn't provide to avoid duplication. + * See: https://github.com/code-yeongyu/oh-my-opencode/issues/379 + */ +export function createEnvContext(): string { const now = new Date() const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone const locale = Intl.DateTimeFormat().resolvedOptions().locale - const dateStr = now.toLocaleDateString("en-US", { - weekday: "short", - year: "numeric", - month: "short", - day: "numeric", - }) - const timeStr = now.toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit", @@ -48,18 +47,12 @@ export function createEnvContext(directory: string): string { hour12: true, }) - const platform = process.platform as "darwin" | "linux" | "win32" | string - return ` -Here is some useful information about the environment you are running in: - - Working directory: ${directory} - Platform: ${platform} - Today's date: ${dateStr} (NOT 2024, NEVEREVER 2024) + Current time: ${timeStr} Timezone: ${timezone} Locale: ${locale} -` +` } function mergeAgentConfig( @@ -97,7 +90,7 @@ export function createBuiltinAgents( let config = buildAgent(source, model) if ((agentName === "Sisyphus" || agentName === "librarian") && directory && config.prompt) { - const envContext = createEnvContext(directory) + const envContext = createEnvContext() config = { ...config, prompt: config.prompt + envContext } } diff --git a/src/hooks/directory-agents-injector/index.ts b/src/hooks/directory-agents-injector/index.ts index 2b08877..e25d114 100644 --- a/src/hooks/directory-agents-injector/index.ts +++ b/src/hooks/directory-agents-injector/index.ts @@ -60,12 +60,17 @@ export function createDirectoryAgentsInjectorHook(ctx: PluginInput) { let current = startDir; while (true) { - const agentsPath = join(current, AGENTS_FILENAME); - if (existsSync(agentsPath)) { - found.push(agentsPath); + // Skip root AGENTS.md - OpenCode's system.ts already loads it via custom() + // See: https://github.com/code-yeongyu/oh-my-opencode/issues/379 + const isRootDir = current === ctx.directory; + if (!isRootDir) { + const agentsPath = join(current, AGENTS_FILENAME); + if (existsSync(agentsPath)) { + found.push(agentsPath); + } } - if (current === ctx.directory) break; + if (isRootDir) break; const parent = dirname(current); if (parent === current) break; if (!parent.startsWith(ctx.directory)) break;