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
2 changes: 2 additions & 0 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,8 @@ NOTE: At any point in time through this workflow you should feel free to ask the
env: {
...process.env,
TERM: "dumb",
OPENCODE_SESSION_ID: input.sessionID,
OPENCODE_SESSION_TITLE: session.title,
},
})

Expand Down
10 changes: 10 additions & 0 deletions packages/opencode/src/tool/bash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Shell } from "@/shell/shell"

import { BashArity } from "@/permission/arity"
import { Truncate } from "./truncation"
import { Session } from "@/session"

const MAX_METADATA_LENGTH = 30_000
const DEFAULT_TIMEOUT = Flag.OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS || 2 * 60 * 1000
Expand Down Expand Up @@ -154,11 +155,20 @@ export const BashTool = Tool.define("bash", async () => {
})
}

const session = await (async () => {
try {
return await Session.get(ctx.sessionID)
} catch {
return undefined
}
})()
const proc = spawn(params.command, {
shell,
cwd,
env: {
...process.env,
OPENCODE_SESSION_ID: ctx.sessionID,
...(session?.title && { OPENCODE_SESSION_TITLE: session.title }),
},
stdio: ["ignore", "pipe", "pipe"],
detached: process.platform !== "win32",
Expand Down
49 changes: 49 additions & 0 deletions packages/opencode/test/tool/bash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Instance } from "../../src/project/instance"
import { tmpdir } from "../fixture/fixture"
import type { PermissionNext } from "../../src/permission/next"
import { Truncate } from "../../src/tool/truncation"
import { Session } from "../../src/session"

const ctx = {
sessionID: "test",
Expand Down Expand Up @@ -36,6 +37,54 @@ describe("tool.bash", () => {
},
})
})

test("exposes OPENCODE_SESSION_ID environment variable", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const bash = await BashTool.init()
const sessionID = "session_abc123"
const testCtx = {
...ctx,
sessionID,
}
const result = await bash.execute(
{
command: process.platform === "win32" ? "echo %OPENCODE_SESSION_ID%" : "echo $OPENCODE_SESSION_ID",
description: "Print session ID env var",
},
testCtx,
)
expect(result.metadata.exit).toBe(0)
expect(result.metadata.output.trim()).toBe(sessionID)
},
})
})

test("exposes OPENCODE_SESSION_TITLE environment variable", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const title = "My Custom Session Title"
const session = await Session.create({ title })
const bash = await BashTool.init()
const testCtx = {
...ctx,
sessionID: session.id,
}
const result = await bash.execute(
{
command: process.platform === "win32" ? "echo %OPENCODE_SESSION_TITLE%" : "echo $OPENCODE_SESSION_TITLE",
description: "Print session title env var",
},
testCtx,
)
expect(result.metadata.exit).toBe(0)
expect(result.metadata.output.trim()).toBe(title)
await Session.remove(session.id)
},
})
})
})

describe("tool.bash permissions", () => {
Expand Down