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
81 changes: 75 additions & 6 deletions packages/opencode/src/tool/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,78 @@ import { Tool } from "./tool"
import { Question } from "../question"
import DESCRIPTION from "./question.txt"

// altimate_change start — non-interactive auto-answer support.
// When running under `claude --print`, CI, or any other context without a TTY,
// there is nobody to click an option in the TUI. The default Question.ask()
// behaviour is to await a Deferred indefinitely, which causes the parent
// process to TaskStop the subprocess after a long wait — looking exactly like
// a hang. See deliverable 02 (Run F first sub-session) for the trace.
//
// Resolution policy: in non-interactive mode, pick the option whose label
// contains a "safe" keyword (skip / cancel / profile only / no / abort).
// If no such option exists, pick the LAST option (UX convention: safer/cancel
// usually sits at the end). The agent then sees a concrete answer in the
// tool result and can continue without blocking. Override via env var:
// ALTIMATE_AUTO_ANSWER=first — always pick first option
// ALTIMATE_AUTO_ANSWER=last — always pick last option (default)
// ALTIMATE_AUTO_ANSWER=skip — return Unanswered for all questions
const SAFE_KEYWORDS = [
"skip",
"cancel",
"no",
"abort",
"profile only",
"profile-only",
"decline",
"deny",
"stop",
]

function isNonInteractive(): boolean {
if (process.env["ALTIMATE_FORCE_INTERACTIVE"] === "1") return false
if (process.env["ALTIMATE_NON_INTERACTIVE"] === "1") return true
return !process.stdin.isTTY
}

function autoAnswer(questions: Question.Info[]): Question.Answer[] {
const mode = (process.env["ALTIMATE_AUTO_ANSWER"] ?? "last").toLowerCase()
return questions.map((q) => {
if (mode === "skip") return []
if (mode === "first") return q.options[0] ? [q.options[0].label] : []
if (mode === "last") {
const safe = q.options.find((o) => {
const text = `${o.label} ${o.description}`.toLowerCase()
return SAFE_KEYWORDS.some((k) => text.includes(k))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: "no" is matched as an arbitrary substring, causing false safe-keyword hits and incorrect auto-answers.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/opencode/src/tool/question.ts, line 47:

<comment>`"no"` is matched as an arbitrary substring, causing false safe-keyword hits and incorrect auto-answers.</comment>

<file context>
@@ -3,17 +3,78 @@ import { Tool } from "./tool"
+    if (mode === "last") {
+      const safe = q.options.find((o) => {
+        const text = `${o.label} ${o.description}`.toLowerCase()
+        return SAFE_KEYWORDS.some((k) => text.includes(k))
+      })
+      if (safe) return [safe.label]
</file context>
Suggested change
return SAFE_KEYWORDS.some((k) => text.includes(k))
return SAFE_KEYWORDS.some((k) => (k === "no" ? /\bno\b/.test(text) : text.includes(k)))

})
if (safe) return [safe.label]
const last = q.options[q.options.length - 1]
return last ? [last.label] : []
}
// exact label match for explicit answers, e.g. ALTIMATE_AUTO_ANSWER="Profile only"
const match = q.options.find((o) => o.label.toLowerCase() === mode)
return match ? [match.label] : []
})
}
// altimate_change end

export const QuestionTool = Tool.define("question", {
description: DESCRIPTION,
parameters: z.object({
questions: z.array(Question.Info.omit({ custom: true })).describe("Questions to ask"),
}),
async execute(params, ctx) {
const answers = await Question.ask({
sessionID: ctx.sessionID,
questions: params.questions,
tool: ctx.callID ? { messageID: ctx.messageID, callID: ctx.callID } : undefined,
})
// altimate_change start — short-circuit when no human is listening.
let answers: Question.Answer[]
if (isNonInteractive()) {
answers = autoAnswer(params.questions)
} else {
answers = await Question.ask({
sessionID: ctx.sessionID,
questions: params.questions,
tool: ctx.callID ? { messageID: ctx.messageID, callID: ctx.callID } : undefined,
})
}
// altimate_change end

function format(answer: Question.Answer | undefined) {
if (!answer?.length) return "Unanswered"
Expand All @@ -22,9 +83,17 @@ export const QuestionTool = Tool.define("question", {

const formatted = params.questions.map((q, i) => `"${q.question}"="${format(answers[i])}"`).join(", ")

// altimate_change start — flag auto-answers explicitly so the agent
// knows the user didn't actually answer and can decide whether to
// proceed with that choice or fail back gracefully.
const prefix = isNonInteractive()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: Mode is recomputed after awaiting, so output can claim non-interactive/interactive status inconsistent with how answers were produced.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/opencode/src/tool/question.ts, line 89:

<comment>Mode is recomputed after awaiting, so output can claim non-interactive/interactive status inconsistent with how answers were produced.</comment>

<file context>
@@ -22,9 +83,17 @@ export const QuestionTool = Tool.define("question", {
+    // altimate_change start — flag auto-answers explicitly so the agent
+    // knows the user didn't actually answer and can decide whether to
+    // proceed with that choice or fail back gracefully.
+    const prefix = isNonInteractive()
+      ? `Running in non-interactive mode (no TTY). Auto-answered with safe defaults: `
+      : `User has answered your questions: `
</file context>

? `Running in non-interactive mode (no TTY). Auto-answered with safe defaults: `
: `User has answered your questions: `
// altimate_change end

return {
title: `Asked ${params.questions.length} question${params.questions.length > 1 ? "s" : ""}`,
output: `User has answered your questions: ${formatted}. You can now continue with the user's answers in mind.`,
output: `${prefix}${formatted}. You can now continue with the user's answers in mind.`,
metadata: {
answers,
},
Expand Down
123 changes: 123 additions & 0 deletions packages/opencode/test/tool/question.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ describe("tool.question", () => {
let askSpy: any

beforeEach(() => {
// Force the original interactive path for the legacy tests below — the
// test environment is non-TTY (bun:test runs without a terminal), so
// without this override the non-interactive auto-answer branch would
// short-circuit `Question.ask` and the existing spies would never fire.
process.env["ALTIMATE_FORCE_INTERACTIVE"] = "1"
askSpy = spyOn(QuestionModule.Question, "ask").mockImplementation(async () => {
return []
})
})

afterEach(() => {
delete process.env["ALTIMATE_FORCE_INTERACTIVE"]
askSpy.mockRestore()
})

Expand Down Expand Up @@ -106,3 +112,120 @@ describe("tool.question", () => {
// }
// })
})

describe("tool.question non-interactive auto-answer", () => {
let askSpy: any

beforeEach(() => {
process.env["ALTIMATE_NON_INTERACTIVE"] = "1"
askSpy = spyOn(QuestionModule.Question, "ask").mockImplementation(async () => [])
})

afterEach(() => {
delete process.env["ALTIMATE_NON_INTERACTIVE"]
delete process.env["ALTIMATE_AUTO_ANSWER"]
askSpy.mockRestore()
})

test("picks safe-keyword option when present and does not invoke Question.ask", async () => {
const tool = await QuestionTool.init()
const questions = [
{
question: "May I run row-level hashdiff comparisons?",
header: "PII consent",
options: [
{ label: "Approve row diff", description: "Sample rows may appear" },
{ label: "Profile only", description: "Safer; no row content surfaced" },
],
},
]

const result = await tool.execute({ questions }, ctx)
expect(askSpy).not.toHaveBeenCalled()
expect(result.output).toContain("Profile only")
expect(result.output).toContain("non-interactive mode")
})

test("falls back to last option when no safe keyword matches", async () => {
const tool = await QuestionTool.init()
const questions = [
{
question: "Pick a color",
header: "Color",
options: [
{ label: "Red", description: "The color of passion" },
{ label: "Blue", description: "The color of sky" },
],
},
]

const result = await tool.execute({ questions }, ctx)
expect(askSpy).not.toHaveBeenCalled()
expect(result.output).toContain("Blue")
})

test("ALTIMATE_AUTO_ANSWER=first picks first option", async () => {
process.env["ALTIMATE_AUTO_ANSWER"] = "first"
const tool = await QuestionTool.init()
const questions = [
{
question: "Pick a color",
header: "Color",
options: [
{ label: "Red", description: "" },
{ label: "Blue", description: "" },
],
},
]

const result = await tool.execute({ questions }, ctx)
expect(result.output).toContain("Red")
})

test("ALTIMATE_AUTO_ANSWER=skip returns Unanswered for each question", async () => {
process.env["ALTIMATE_AUTO_ANSWER"] = "skip"
const tool = await QuestionTool.init()
const questions = [
{
question: "Pick a color",
header: "Color",
options: [{ label: "Red", description: "" }],
},
]

const result = await tool.execute({ questions }, ctx)
expect(result.output).toContain("Unanswered")
})

test("ALTIMATE_AUTO_ANSWER=<exact label> picks matching option", async () => {
process.env["ALTIMATE_AUTO_ANSWER"] = "blue"
const tool = await QuestionTool.init()
const questions = [
{
question: "Pick a color",
header: "Color",
options: [
{ label: "Red", description: "" },
{ label: "Blue", description: "" },
],
},
]

const result = await tool.execute({ questions }, ctx)
expect(result.output).toContain("Blue")
})

test("non-interactive prefix is set when Question.ask is bypassed", async () => {
const tool = await QuestionTool.init()
const questions = [
{
question: "OK to proceed?",
header: "Proceed",
options: [{ label: "Cancel", description: "Stop" }],
},
]

const result = await tool.execute({ questions }, ctx)
expect(result.output.startsWith("Running in non-interactive mode")).toBe(true)
})
})
Loading