-
Notifications
You must be signed in to change notification settings - Fork 87
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
Open
sahrizvi
wants to merge
1
commit into
main
Choose a base branch
from
fix/question-non-interactive-936
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
|
|
||
| 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)) | ||
| }) | ||
| 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() | ||
|
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. P3: Mode is recomputed after awaiting, so output can claim non-interactive/interactive status inconsistent with how answers were produced. Prompt for AI agents |
||
| ? `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, | ||
| }, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
P2:
"no"is matched as an arbitrary substring, causing false safe-keyword hits and incorrect auto-answers.Prompt for AI agents