@@ -5,8 +5,9 @@ import { GithubError, Issue, Comment, type Pull } from "./github.js";
55import { Serial , redact , type Store } from "./state.js" ;
66import { branchText , type BranchInput , type BaseChoice } from "./branch.js" ;
77import { activityOf , type Activity } from "./activity.js" ;
8+ import { AnalysisDecision } from "./analysis.js" ;
89
9- export const PendingQuestion = z . object ( { id : z . string ( ) , text : z . string ( ) , sessionID : z . string ( ) . optional ( ) , purpose : z . literal ( "base" ) . optional ( ) , commentID : z . number ( ) . optional ( ) ,
10+ export const PendingQuestion = z . object ( { id : z . string ( ) , text : z . string ( ) , sessionID : z . string ( ) . optional ( ) , purpose : z . enum ( [ "base" , "analysis" ] ) . optional ( ) , commentID : z . number ( ) . optional ( ) ,
1011 permission : z . object ( { action : z . string ( ) , resources : z . array ( z . string ( ) ) } ) . optional ( ) ,
1112 answer : Comment . optional ( ) , delivered : z . boolean ( ) . optional ( ) , answerSent : z . boolean ( ) . optional ( ) } ) ;
1213const Phase = z . enum ( [ "queued" , "analyzing" , "commented" , "running" , "verifying" , "publishing" , "pr_opened" ] ) ;
@@ -15,6 +16,8 @@ export const Task = z.object({
1516 phase : Phase , status : z . enum ( [ "ready" , "retry_wait" , "blocked" , "failed" , "done" , "waiting" ] ) ,
1617 attempts : z . number ( ) , nextAt : z . number ( ) , createdAt : z . number ( ) ,
1718 analysis : z . string ( ) . optional ( ) , commentID : z . number ( ) . optional ( ) ,
19+ analysisDecision : AnalysisDecision . optional ( ) ,
20+ analysisDialogue : z . array ( z . object ( { question : z . string ( ) , answer : Comment } ) ) . optional ( ) ,
1821 baseBranch : z . string ( ) . optional ( ) , question : PendingQuestion . optional ( ) ,
1922 baseDialogue : z . array ( z . object ( { question : z . string ( ) , answer : Comment } ) ) . optional ( ) ,
2023 permissions : z . array ( z . object ( { sessionID : z . string ( ) , action : z . string ( ) , resources : z . array ( z . string ( ) ) , allow : z . boolean ( ) } ) ) . optional ( ) ,
@@ -48,7 +51,7 @@ export interface GithubPort {
4851export interface Executor {
4952 selectBase ( task : Task , repo : Repository , inputs : BranchInput [ ] ) : Promise < BaseChoice > ;
5053 hasBranch ( repo : Repository , branch : string ) : Promise < boolean > ;
51- analyze ( task : Task ) : Promise < string > ;
54+ analyze ( task : Task ) : Promise < AnalysisDecision > ;
5255 title ( task : Task ) : Promise < string > ;
5356 prepare ( task : Task , repo : Repository ) : Promise < { worktree : string ; baseSha : string } > ;
5457 run ( task : Task , checkpoint : ( patch : Partial < Task > ) => Promise < void > ) : Promise < void > ;
@@ -97,6 +100,8 @@ export class Dispatcher {
97100 const existing = this . queue . tasks . find ( t => t . key === key ) ;
98101 if ( ! existing && issue . state !== "open" ) { ignored ++ ; continue ; }
99102 const comments = await this . github . comments ( repo . repo , issue . number ) ;
103+ // A person may share the posting account with the bot. Exclude marked
104+ // automation messages, never the authenticated account's login itself.
100105 const authorized = comments . filter ( c => this . authorized ( c . user . login , repo . allowedAuthors ) && c . user . type !== "Bot" && c . body . trim ( ) && ! c . body . includes ( "<!-- opencode2:" ) ) ;
101106 const cursor = Math . max ( 0 , ...comments . map ( c => c . id ) ) ;
102107 if ( existing ) {
@@ -165,6 +170,7 @@ export class Dispatcher {
165170 if ( ! finished ) return ;
166171 Object . assign ( finished , { round : ( finished . round ?? 1 ) + 1 , feedback : finished . pendingFeedback , pendingFeedback : [ ] , previousSessionID : finished . sessionID ,
167172 phase : "queued" , status : "ready" , attempts : 0 , nextAt : this . now ( ) , analysis : undefined , commentID : undefined ,
173+ analysisDecision : undefined , analysisDialogue : undefined , question : undefined ,
168174 sessionID : undefined , sessionReady : false , promptAttempted : false , checks : undefined , commit : undefined , error : undefined } ) ;
169175 await this . store . save ( this . queue ) ;
170176 } ) ;
@@ -196,12 +202,14 @@ export class Dispatcher {
196202 await this . update ( task , { issue : latest , route } ) ;
197203 }
198204 if ( task . phase === "queued" || task . phase === "analyzing" ) {
199- await this . update ( task , { phase : "analyzing" } ) ;
200- if ( ! task . analysis ) await this . update ( task , { analysis : await this . executor . analyze ( task ) } ) ;
201- const commentID = await this . github . ensureComment ( task . repo , task . issue . number , `<!-- opencode2:${ task . key } :analysis:v${ task . round ?? 1 } -->` , task . analysis ! ) ;
202- await this . update ( task , { commentID, phase : "commented" , attempts : 0 } ) ;
205+ await this . resolveAnalysis ( task , repo ) ;
203206 }
204207 if ( task . phase === "commented" ) {
208+ // Saved pre-upgrade analyses had no decision. Reassess them before any
209+ // implementation, preserving an already-pending base question first.
210+ if ( task . question ?. purpose === "base" ) await this . resolveBase ( task , repo ) ;
211+ if ( ! task . analysisDecision ) await this . resolveAnalysis ( task , repo ) ;
212+ if ( task . analysisDecision ?. kind !== "proceed" ) throw new WaitingForAnswer ( "Waiting for clarification before implementation" ) ;
205213 if ( ! task . commentID ) throw new Blocked ( "Missing confirmed analysis comment" ) ;
206214 if ( ! task . baseBranch ) await this . resolveBase ( task , repo ) ;
207215 repo = { ...repo , baseBranch : task . baseBranch ! } ;
@@ -237,6 +245,35 @@ export class Dispatcher {
237245 await this . update ( task , { attempts, error : redact ( error , this . secrets ) , status : blocked ? "blocked" : attempts >= this . options . maxAttempts ? "failed" : "retry_wait" , nextAt : Math . max ( this . now ( ) + Math . min ( 3600 , 5 * 2 ** attempts ) * 1000 , error instanceof GithubError ? error . retryAt ?? 0 : 0 ) } ) ;
238246 }
239247 }
248+ private async resolveAnalysis ( task : Task , repo : Repository ) {
249+ await this . update ( task , { phase : "analyzing" } ) ;
250+ if ( task . analysisDialogue ?. some ( d => ! this . authorized ( d . answer . user . login , repo . allowedAuthors ) ) ) throw new Blocked ( "A clarification reply author is no longer authorized" ) ;
251+ const q = task . question ;
252+ if ( q ?. purpose === "analysis" && ! q . delivered ) {
253+ if ( ! q . answer ) { await this . publishQuestion ( task , q ) ; throw new WaitingForAnswer ( "Waiting for the issue reply before implementation" ) ; }
254+ if ( ! this . authorized ( q . answer . user . login , repo . allowedAuthors ) ) throw new Blocked ( "The clarification reply author is no longer authorized" ) ;
255+ // Save the answer and invalidate the previous decision in one checkpoint.
256+ // An unclear reply is assessed again and can produce another question.
257+ await this . update ( task , {
258+ analysisDialogue : [ ...task . analysisDialogue ?? [ ] , { question : q . text , answer : q . answer } ] ,
259+ question : { ...q , delivered : true } , analysisDecision : undefined ,
260+ } ) ;
261+ }
262+ if ( ! task . analysisDecision ) {
263+ const decision = AnalysisDecision . parse ( await this . executor . analyze ( task ) ) ;
264+ await this . update ( task , { analysisDecision : decision , analysis : decision . comment } ) ;
265+ }
266+ const decision = task . analysisDecision ! ;
267+ if ( decision . kind === "question" ) {
268+ const id = `analysis_${ createHash ( "sha256" ) . update ( JSON . stringify ( { key : task . key , round : task . round ?? 1 , dialogue : task . analysisDialogue ?? [ ] , decision } ) ) . digest ( "hex" ) . slice ( 0 , 24 ) } ` ;
269+ await this . askTask ( task , { id, text : `${ decision . comment } \n\n${ decision . question } ` , purpose : "analysis" } ) ;
270+ throw new WaitingForAnswer ( "Waiting for the issue reply before implementation" ) ;
271+ }
272+ const lastAnswer = task . analysisDialogue ?. at ( - 1 ) ?. answer . id ;
273+ const marker = `<!-- opencode2:${ task . key } :analysis:v${ task . round ?? 1 } ${ lastAnswer === undefined ? "" : `:reply:${ lastAnswer } ` } -->` ;
274+ const commentID = await this . github . ensureComment ( task . repo , task . issue . number , marker , decision . comment ) ;
275+ await this . update ( task , { commentID, phase : "commented" , attempts : 0 , ...( task . question ?. purpose === "analysis" ? { question : undefined } : { } ) } ) ;
276+ }
240277 private async resolveBase ( task : Task , repo : Repository ) {
241278 const q = task . question ;
242279 if ( q ?. purpose === "base" && ! q . delivered ) {
@@ -247,6 +284,7 @@ export class Dispatcher {
247284 const inputs : BranchInput [ ] = [
248285 ...( this . authorized ( task . issue . user . login , repo . allowedAuthors ) ? [ task . issue . title , task . issue . body ?? "" ] . map ( text => ( { text } ) ) : [ ] ) ,
249286 ...( task . feedback ?? [ ] ) . filter ( c => this . authorized ( c . user . login , repo . allowedAuthors ) ) . map ( c => ( { text : c . body } ) ) ,
287+ ...( task . analysisDialogue ?? [ ] ) . filter ( d => this . authorized ( d . answer . user . login , repo . allowedAuthors ) ) . map ( d => ( { text : d . answer . body , question : d . question } ) ) ,
250288 ...( task . baseDialogue ?? [ ] ) . filter ( d => this . authorized ( d . answer . user . login , repo . allowedAuthors ) ) . map ( d => ( { text : d . answer . body , question : d . question } ) ) ,
251289 ] . map ( input => ( { ...input , text : branchText ( input . text ) } ) ) . filter ( input => input . text ) ;
252290 let choice = await this . executor . selectBase ( task , repo , inputs ) ;
0 commit comments