-
Notifications
You must be signed in to change notification settings - Fork 134
fix: auto-resolve question tool in non-interactive contexts #937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a49442b
f981199
d2a8516
764291c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [🟠 MEDIUM] There's a potential null pointer exception here. Depending on the runtime environment (e.g., embedded environments or child processes without standard streams), Suggested change:
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| 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)) | ||||||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
|
||||||
| }) | ||||||
| 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" | ||||||
|
|
@@ -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() | ||||||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
|
||||||
| ? `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, | ||||||
| }, | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likely regression for server / IDE (headless-but-interactive) mode —
!process.stdin.isTTYis the wrong signal for "no human is listening."Question.ask()does not only resolve on a TUI keypress. Answers also arrive over HTTP:Question.reply({ requestID, answers })is exposed atPOST /question/:requestID/reply(packages/opencode/src/server/routes/question.ts), withGET /questionto list pending ones. So when altimate-code runs as a server with a frontend/IDE client (VS Code / JetBrains / web),process.stdin.isTTYis false, yet a real human can answer via the route.With this guard, that existing interactive flow is misclassified as non-interactive:
execute()short-circuits toautoAnswer()→ returns Unanswered immediately and never publishes the question for the client to reply to. The UI user loses the ability to answer, out of the box —ALTIMATE_FORCE_INTERACTIVE=1exists but server deployments won't have it set.Suggestion: gate on whether an answer channel actually exists, not on TTY. E.g. treat it as interactive when the server/question clients are connected (a pending-question listener is registered), and reserve the auto-answer path for true headless runs (
runsubprocess / CI). At minimum, default server mode to interactive and require explicitALTIMATE_NON_INTERACTIVE=1to opt into auto-answer, so thealtimate-code run-as-subprocess fix doesn't also silently disable the HTTP reply path.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this — your point was the core insight that drove the entire redesign of this PR. Addressed across
f981199,d2a85165, and764291c0.Specifically:
tool/question.tsno longer touchesprocess.stdin.isTTYat all. Detection is now opt-in viaALTIMATE_NON_INTERACTIVE, andrunis the only entrypoint that sets it.serve,web,acp, andworkspace-servedeliberately leave it unset, so theirPOST /question/:requestID/replypath stays live for connected IDE/web clients exactly as you described.cli/cmd/run.tssets the env var only whenargs.attachis not set — attach-mode runs the agent on the remote server, so the local env var would just be noise and could mislead other host-local tools.tool/bash.tsstripsALTIMATE_NON_INTERACTIVEfrommergedEnvbefore spawning children, so a nestedaltimate-code serve(or any server entrypoint) spawned from arunsession doesn't inherit the flag and silently disable its own HTTP reply path. This was a latent regression of the same class you flagged — it would have hit the same IDE/web users.The block comment at
tool/question.ts:6-38captures the rationale and explicitly mentions PR #937 review for future maintainers. Tests attest/tool/question.test.tslock in the default-interactive contract (the suryaiyer95-named regression class) plusFORCE_INTERACTIVEprecedence andNON_INTERACTIVE=0opt-out.There's one remaining architectural item your review surfaced indirectly:
session/llm.ts:299only consultsagent.permission, not the mergedsession.permission, so thequestion: denyrule inrun.tsis intent-marker only. Tracked as a follow-up — out of scope here because the env-var path now handles the actual behavior correctly.