11import path from "node:path" ;
22
3- import { summarizeBacklog , selectNextTask } from "./backlog.mjs" ;
3+ import { summarizeBacklog } from "./backlog.mjs" ;
44import { nowIso , writeJson , writeText , readTextIfExists } from "./common.mjs" ;
55import {
6- loadBacklog ,
76 loadPolicy ,
87 loadProjectConfig ,
98 scaffoldIfMissing ,
@@ -17,205 +16,17 @@ import {
1716 rememberEngineSelection ,
1817 resolveEngineSelection ,
1918} from "./engine_selection.mjs" ;
19+ import {
20+ buildAnalysisSummaryText ,
21+ buildCurrentWorkspaceDiscovery ,
22+ normalizeAnalysisPayload ,
23+ persistAnalysisFailure ,
24+ renderAnalysisState ,
25+ summarizeFailedAnalysisResult ,
26+ } from "./analyzer_support.mjs" ;
2027import { runEngineTask } from "./process.mjs" ;
2128import { buildAnalysisPrompt } from "./analyze_prompt.mjs" ;
2229
23- function renderAnalysisState ( context , backlog , analysis ) {
24- const summary = summarizeBacklog ( backlog ) ;
25- const nextTask = selectNextTask ( backlog ) ;
26-
27- return [
28- "## 当前状态" ,
29- `- backlog 文件:${ path . relative ( context . repoRoot , context . backlogFile ) . replaceAll ( "\\" , "/" ) } ` ,
30- `- 总任务数:${ summary . total } ` ,
31- `- 已完成:${ summary . done } ` ,
32- `- 待处理:${ summary . pending } ` ,
33- `- 进行中:${ summary . inProgress } ` ,
34- `- 失败:${ summary . failed } ` ,
35- `- 阻塞:${ summary . blocked } ` ,
36- `- 当前任务:${ nextTask ? nextTask . title : "无" } ` ,
37- `- 最近结果:${ analysis . summary . currentState } ` ,
38- `- 下一建议:${ analysis . summary . nextAction } ` ,
39- ] . join ( "\n" ) ;
40- }
41-
42- function createEmptyBacklogSummary ( ) {
43- return {
44- total : 0 ,
45- pending : 0 ,
46- inProgress : 0 ,
47- done : 0 ,
48- failed : 0 ,
49- blocked : 0 ,
50- } ;
51- }
52-
53- function getExistingBacklogSnapshot ( context ) {
54- try {
55- const backlog = loadBacklog ( context ) ;
56- return {
57- summary : summarizeBacklog ( backlog ) ,
58- nextTask : selectNextTask ( backlog ) ,
59- } ;
60- } catch {
61- return {
62- summary : createEmptyBacklogSummary ( ) ,
63- nextTask : null ,
64- } ;
65- }
66- }
67-
68- function firstMeaningfulLine ( text , fallback ) {
69- return String ( text || "" )
70- . split ( / \r ? \n / )
71- . map ( ( line ) => line . trim ( ) )
72- . find ( Boolean ) || fallback ;
73- }
74-
75- function summarizeFailedAnalysisResult ( result , fallback ) {
76- const combined = [
77- String ( result ?. stdout || "" ) . trim ( ) ,
78- String ( result ?. stderr || "" ) . trim ( ) ,
79- ] . filter ( Boolean ) . join ( "\n\n" ) . trim ( ) ;
80- return combined || fallback ;
81- }
82-
83- function renderAnalysisFailureState ( context , backlogSummary , nextTask , failureSummary , runDir = "" ) {
84- const runDirHint = runDir
85- ? path . relative ( context . repoRoot , runDir ) . replaceAll ( "\\" , "/" )
86- : "" ;
87-
88- return [
89- "## 当前状态" ,
90- `- backlog 文件:${ path . relative ( context . repoRoot , context . backlogFile ) . replaceAll ( "\\" , "/" ) } ` ,
91- `- 总任务数:${ backlogSummary . total } ` ,
92- `- 已完成:${ backlogSummary . done } ` ,
93- `- 待处理:${ backlogSummary . pending } ` ,
94- `- 进行中:${ backlogSummary . inProgress } ` ,
95- `- 失败:${ backlogSummary . failed } ` ,
96- `- 阻塞:${ backlogSummary . blocked } ` ,
97- `- 当前任务:${ nextTask ? nextTask . title : "无" } ` ,
98- `- 最近结果:${ firstMeaningfulLine ( failureSummary , "HelloLoop 分析失败" ) } ` ,
99- `- 下一建议:${ runDirHint ? `先检查 ${ runDirHint } 中的日志后再重新执行 npx helloloop` : "修复错误后重新执行 npx helloloop" } ` ,
100- ] . join ( "\n" ) ;
101- }
102-
103- function persistAnalysisFailure ( context , failureSummary , runDir = "" ) {
104- const snapshot = getExistingBacklogSnapshot ( context ) ;
105- writeStatus ( context , {
106- ok : false ,
107- stage : "analysis_failed" ,
108- taskId : null ,
109- taskTitle : "" ,
110- runDir,
111- summary : snapshot . summary ,
112- message : failureSummary ,
113- } ) ;
114- writeStateMarkdown (
115- context ,
116- renderAnalysisFailureState ( context , snapshot . summary , snapshot . nextTask , failureSummary , runDir ) ,
117- ) ;
118- }
119-
120- function sanitizeTask ( task ) {
121- return {
122- id : String ( task . id || "" ) . trim ( ) ,
123- title : String ( task . title || "" ) . trim ( ) ,
124- status : [ "done" , "blocked" ] . includes ( String ( task . status || "" ) ) ? String ( task . status ) : "pending" ,
125- priority : [ "P0" , "P1" , "P2" , "P3" ] . includes ( String ( task . priority || "" ) ) ? String ( task . priority ) : "P2" ,
126- risk : [ "medium" , "high" , "critical" ] . includes ( String ( task . risk || "" ) ) ? String ( task . risk ) : "low" ,
127- goal : String ( task . goal || "" ) . trim ( ) ,
128- docs : Array . isArray ( task . docs ) ? task . docs . map ( ( item ) => String ( item || "" ) . trim ( ) ) . filter ( Boolean ) : [ ] ,
129- paths : Array . isArray ( task . paths ) ? task . paths . map ( ( item ) => String ( item || "" ) . trim ( ) ) . filter ( Boolean ) : [ ] ,
130- acceptance : Array . isArray ( task . acceptance ) ? task . acceptance . map ( ( item ) => String ( item || "" ) . trim ( ) ) . filter ( Boolean ) : [ ] ,
131- dependsOn : Array . isArray ( task . dependsOn ) ? task . dependsOn . map ( ( item ) => String ( item || "" ) . trim ( ) ) . filter ( Boolean ) : [ ] ,
132- verify : Array . isArray ( task . verify ) ? task . verify . map ( ( item ) => String ( item || "" ) . trim ( ) ) . filter ( Boolean ) : [ ] ,
133- } ;
134- }
135-
136- function normalizeAnalysisPayload ( payload , docsEntries ) {
137- const summary = {
138- currentState : String ( payload ?. summary ?. currentState || "" ) . trim ( ) || "已完成接续分析。" ,
139- implemented : Array . isArray ( payload ?. summary ?. implemented ) ? payload . summary . implemented . map ( ( item ) => String ( item || "" ) . trim ( ) ) . filter ( Boolean ) : [ ] ,
140- remaining : Array . isArray ( payload ?. summary ?. remaining ) ? payload . summary . remaining . map ( ( item ) => String ( item || "" ) . trim ( ) ) . filter ( Boolean ) : [ ] ,
141- nextAction : String ( payload ?. summary ?. nextAction || "" ) . trim ( ) || "查看下一任务。" ,
142- } ;
143- const tasks = Array . isArray ( payload . tasks )
144- ? payload . tasks . map ( ( task ) => sanitizeTask ( task ) ) . filter ( ( task ) => (
145- task . id && task . title && task . goal && task . acceptance . length
146- ) )
147- : [ ] ;
148-
149- if ( ! tasks . length && summary . remaining . length ) {
150- throw new Error ( "分析结果无效:仍存在剩余工作,但未生成可用任务。" ) ;
151- }
152-
153- const requestInterpretation = payload ?. requestInterpretation && typeof payload . requestInterpretation === "object"
154- ? {
155- summary : String ( payload . requestInterpretation . summary || "" ) . trim ( ) ,
156- priorities : Array . isArray ( payload . requestInterpretation . priorities )
157- ? payload . requestInterpretation . priorities . map ( ( item ) => String ( item || "" ) . trim ( ) ) . filter ( Boolean )
158- : [ ] ,
159- cautions : Array . isArray ( payload . requestInterpretation . cautions )
160- ? payload . requestInterpretation . cautions . map ( ( item ) => String ( item || "" ) . trim ( ) ) . filter ( Boolean )
161- : [ ] ,
162- }
163- : null ;
164- const repoDecision = payload ?. repoDecision && typeof payload . repoDecision === "object"
165- ? {
166- compatibility : [ "compatible" , "conflict" , "uncertain" ] . includes ( String ( payload . repoDecision . compatibility || "" ) )
167- ? String ( payload . repoDecision . compatibility )
168- : "compatible" ,
169- action : [ "continue_existing" , "confirm_rebuild" , "start_new" ] . includes ( String ( payload . repoDecision . action || "" ) )
170- ? String ( payload . repoDecision . action )
171- : "continue_existing" ,
172- reason : String ( payload . repoDecision . reason || "" ) . trim ( ) ,
173- }
174- : null ;
175-
176- return {
177- project : String ( payload . project || "" ) . trim ( ) || "helloloop-project" ,
178- summary,
179- constraints : Array . isArray ( payload . constraints )
180- ? payload . constraints . map ( ( item ) => String ( item || "" ) . trim ( ) ) . filter ( Boolean )
181- : [ ] ,
182- requestInterpretation : requestInterpretation && (
183- requestInterpretation . summary
184- || requestInterpretation . priorities . length
185- || requestInterpretation . cautions . length
186- )
187- ? requestInterpretation
188- : null ,
189- repoDecision : repoDecision && repoDecision . reason
190- ? repoDecision
191- : null ,
192- tasks,
193- requiredDocs : docsEntries ,
194- } ;
195- }
196-
197- function buildAnalysisSummaryText ( context , analysis , backlog , engineResolution ) {
198- const summary = summarizeBacklog ( backlog ) ;
199- const nextTask = selectNextTask ( backlog ) ;
200-
201- return [
202- "HelloLoop 已完成接续分析。" ,
203- `项目仓库:${ context . repoRoot } ` ,
204- `开发文档:${ analysis . requiredDocs . join ( ", " ) } ` ,
205- `执行引擎:${ engineResolution ?. displayName || "未记录" } ` ,
206- "" ,
207- "当前进度:" ,
208- analysis . summary . currentState ,
209- "" ,
210- `任务统计:done ${ summary . done } / pending ${ summary . pending } / blocked ${ summary . blocked } ` ,
211- nextTask ? `下一任务:${ nextTask . title } ` : "下一任务:暂无可执行任务" ,
212- "" ,
213- "下一步建议:" ,
214- `- npx helloloop` ,
215- `- npx helloloop --dry-run` ,
216- ] . join ( "\n" ) ;
217- }
218-
21930async function analyzeResolvedWorkspace ( context , discovery , options = { } ) {
22031 scaffoldIfMissing ( context ) ;
22132 const policy = loadPolicy ( context ) ;
@@ -268,9 +79,20 @@ async function analyzeResolvedWorkspace(context, discovery, options = {}) {
26879 outputSchemaFile : schemaFile ,
26980 outputPrefix : `${ engineResolution . engine } -analysis` ,
27081 skipGitRepoCheck : true ,
82+ hostLease : options . hostLease || null ,
27183 } ) ;
27284
27385 if ( ! analysisResult . ok ) {
86+ if ( analysisResult . leaseExpired ) {
87+ return {
88+ ok : false ,
89+ code : "host-lease-stopped" ,
90+ summary : analysisResult . leaseReason || "检测到宿主窗口已关闭,主线终态复核已停止。" ,
91+ stopped : true ,
92+ engineResolution,
93+ discovery,
94+ } ;
95+ }
27496 const failureSummary = summarizeFailedAnalysisResult (
27597 analysisResult ,
27698 `${ engineResolution . displayName } 接续分析失败。` ,
@@ -368,38 +190,6 @@ async function analyzeResolvedWorkspace(context, discovery, options = {}) {
368190 } ;
369191}
370192
371- function buildCurrentWorkspaceDiscovery ( context , docsEntries ) {
372- return {
373- ok : true ,
374- repoRoot : context . repoRoot ,
375- docsEntries,
376- resolvedDocs : docsEntries ,
377- resolution : {
378- repo : {
379- source : "current_repo" ,
380- sourceLabel : "当前项目" ,
381- confidence : "high" ,
382- confidenceLabel : "高" ,
383- path : context . repoRoot ,
384- exists : true ,
385- basis : [
386- "已在当前项目基础上执行主线终态复核。" ,
387- ] ,
388- } ,
389- docs : {
390- source : "existing_state" ,
391- sourceLabel : "已有 .helloloop 配置" ,
392- confidence : "high" ,
393- confidenceLabel : "高" ,
394- entries : docsEntries ,
395- basis : [
396- "已复用 `.helloloop/project.json` 中记录的 requiredDocs。" ,
397- ] ,
398- } ,
399- } ,
400- } ;
401- }
402-
403193export async function reanalyzeCurrentWorkspace ( context , options = { } ) {
404194 const existingProjectConfig = loadProjectConfig ( context ) ;
405195 const docsEntries = Array . isArray ( options . requiredDocs ) && options . requiredDocs . length
0 commit comments