@@ -5,11 +5,17 @@ from __future__ import annotations |
| 5 | 5 | from collections.abc import Awaitable, Callable |
| 6 | 6 | from dataclasses import dataclass |
| 7 | 7 | from enum import StrEnum |
| 8 | +from pathlib import Path |
| 8 | 9 | |
| 9 | 10 | from ..llm.base import Message, Role |
| 10 | 11 | from .completion_policy import CompletionPolicy |
| 11 | 12 | from .context import RuntimeContext |
| 12 | | -from .dod import DefinitionOfDone |
| 13 | +from .dod import ( |
| 14 | + DefinitionOfDone, |
| 15 | + collect_planned_artifact_targets, |
| 16 | + infer_next_output_file, |
| 17 | + planned_artifact_target_satisfied, |
| 18 | +) |
| 13 | 19 | from .events import AgentEvent, TurnSummary |
| 14 | 20 | from .evidence_provenance import EvidenceProvenance |
| 15 | 21 | from .executor import ToolExecutor |
@@ -22,9 +28,39 @@ from .policy_timeline import ( |
| 22 | 28 | from .repair import ResponseRepairer |
| 23 | 29 | from .rollback import RollbackPlan |
| 24 | 30 | from .verification_observations import VerificationObservation |
| 31 | +from .workflow import ( |
| 32 | + effective_pending_todo_items, |
| 33 | + infer_pending_todo_output_target, |
| 34 | + preferred_pending_todo_item, |
| 35 | +) |
| 25 | 36 | |
| 26 | 37 | EventSink = Callable[[AgentEvent], Awaitable[None]] |
| 27 | 38 | |
| 39 | +_SPECIAL_DOD_ITEMS = { |
| 40 | + "Complete the requested work", |
| 41 | + "Collect verification evidence", |
| 42 | +} |
| 43 | +_PROGRESS_INTENT_HINTS = ( |
| 44 | + "i'll ", |
| 45 | + "i will ", |
| 46 | + "i am going to ", |
| 47 | + "i'm going to ", |
| 48 | + "let me ", |
| 49 | + "now i'll ", |
| 50 | + "next i'll ", |
| 51 | + "continue by ", |
| 52 | + "continue with ", |
| 53 | +) |
| 54 | +_COMPLETION_HINTS = ( |
| 55 | + "done", |
| 56 | + "completed", |
| 57 | + "finished", |
| 58 | + "all set", |
| 59 | + "verified", |
| 60 | + "successfully completed", |
| 61 | + "everything is done", |
| 62 | +) |
| 63 | + |
| 28 | 64 | |
| 29 | 65 | class TurnCompletionAction(StrEnum): |
| 30 | 66 | """What the runtime should do after evaluating one no-tool text response.""" |
@@ -225,6 +261,42 @@ class TurnCompletionController: |
| 225 | 261 | finalize_reason_summary=continuation_decision.decision_summary, |
| 226 | 262 | ) |
| 227 | 263 | |
| 264 | + progress_intent_prompt = _build_in_progress_continuation_prompt( |
| 265 | + content=content, |
| 266 | + dod=dod, |
| 267 | + project_root=self.context.project_root, |
| 268 | + messages=list(getattr(self.context.session, "messages", []) or []), |
| 269 | + ) |
| 270 | + if progress_intent_prompt: |
| 271 | + assistant_message = Message(role=Role.ASSISTANT, content=response_content) |
| 272 | + self.context.session.append(assistant_message) |
| 273 | + summary.assistant_messages.append(assistant_message) |
| 274 | + self.context.session.append( |
| 275 | + Message(role=Role.USER, content=progress_intent_prompt) |
| 276 | + ) |
| 277 | + self._append_completion_trace_entry( |
| 278 | + summary=summary, |
| 279 | + stage="continuation_check", |
| 280 | + outcome="continue", |
| 281 | + decision_code="in_progress_transition_continue", |
| 282 | + decision_summary=( |
| 283 | + "continued because the assistant described the next planned step " |
| 284 | + "without executing it yet" |
| 285 | + ), |
| 286 | + ) |
| 287 | + self._record_completion_decision( |
| 288 | + summary=summary, |
| 289 | + decision_code="in_progress_transition_continue", |
| 290 | + decision_summary=( |
| 291 | + "continued because the assistant described the next planned step " |
| 292 | + "without executing it yet" |
| 293 | + ), |
| 294 | + ) |
| 295 | + return TurnCompletionDecision( |
| 296 | + action=TurnCompletionAction.CONTINUE, |
| 297 | + continuation_count=continuation_count + 1, |
| 298 | + ) |
| 299 | + |
| 228 | 300 | final_response = self.completion_policy.finalize_response_text( |
| 229 | 301 | content=content, |
| 230 | 302 | actions_taken=actions_taken, |
@@ -281,3 +353,137 @@ class TurnCompletionController: |
| 281 | 353 | action=TurnCompletionAction.COMPLETE, |
| 282 | 354 | continuation_count=continuation_count, |
| 283 | 355 | ) |
| 356 | + |
| 357 | + |
| 358 | +def _build_in_progress_continuation_prompt( |
| 359 | + *, |
| 360 | + content: str, |
| 361 | + dod: DefinitionOfDone, |
| 362 | + project_root: Path, |
| 363 | + messages: list[object], |
| 364 | +) -> str | None: |
| 365 | + if not _looks_like_progress_intent(content): |
| 366 | + return None |
| 367 | + |
| 368 | + missing_artifact = _next_missing_planned_artifact( |
| 369 | + dod, |
| 370 | + project_root=project_root, |
| 371 | + messages=messages, |
| 372 | + ) |
| 373 | + next_pending = preferred_pending_todo_item( |
| 374 | + dod, |
| 375 | + project_root=project_root, |
| 376 | + missing_artifact=missing_artifact, |
| 377 | + ) |
| 378 | + if not next_pending and missing_artifact is None: |
| 379 | + return None |
| 380 | + |
| 381 | + target = _preferred_progress_target( |
| 382 | + dod, |
| 383 | + next_pending=next_pending, |
| 384 | + missing_artifact=missing_artifact, |
| 385 | + project_root=project_root, |
| 386 | + messages=messages, |
| 387 | + ) |
| 388 | + if target is not None: |
| 389 | + return ( |
| 390 | + "[CONTINUE CURRENT STEP]\n" |
| 391 | + "You just described the next planned step, but the concrete output is not on disk yet. " |
| 392 | + f"Respond with one concrete `write` or `edit`-style tool call that creates or updates `{target}` now. " |
| 393 | + "Do not summarize, verify, or restart discovery first." |
| 394 | + ) |
| 395 | + |
| 396 | + if next_pending: |
| 397 | + return ( |
| 398 | + "[CONTINUE CURRENT STEP]\n" |
| 399 | + "You just described the next planned step, but it has not been executed yet. " |
| 400 | + f"Continue with `{next_pending}` now by emitting one concrete tool call instead of another narration, summary, or verification claim." |
| 401 | + ) |
| 402 | + return None |
| 403 | + |
| 404 | + |
| 405 | +def _looks_like_progress_intent(content: str) -> bool: |
| 406 | + text = content.lower().strip() |
| 407 | + if not text or "?" in text: |
| 408 | + return False |
| 409 | + if any(marker in text for marker in _COMPLETION_HINTS): |
| 410 | + return False |
| 411 | + return any(marker in text for marker in _PROGRESS_INTENT_HINTS) |
| 412 | + |
| 413 | + |
| 414 | +def _next_missing_planned_artifact( |
| 415 | + dod: DefinitionOfDone, |
| 416 | + *, |
| 417 | + project_root: Path, |
| 418 | + messages: list[object], |
| 419 | +) -> tuple[Path, bool] | None: |
| 420 | + for target, expect_directory in collect_planned_artifact_targets( |
| 421 | + dod, |
| 422 | + project_root=project_root, |
| 423 | + max_paths=12, |
| 424 | + ): |
| 425 | + if not planned_artifact_target_satisfied( |
| 426 | + dod, |
| 427 | + target=target, |
| 428 | + expect_directory=expect_directory, |
| 429 | + project_root=project_root, |
| 430 | + ): |
| 431 | + return target, expect_directory |
| 432 | + |
| 433 | + for target, expect_directory in collect_planned_artifact_targets( |
| 434 | + dod, |
| 435 | + project_root=project_root, |
| 436 | + max_paths=12, |
| 437 | + ): |
| 438 | + if not expect_directory or not target.is_dir(): |
| 439 | + continue |
| 440 | + next_output_file, _ = infer_next_output_file( |
| 441 | + target=target, |
| 442 | + project_root=project_root, |
| 443 | + messages=list(messages or []), |
| 444 | + ) |
| 445 | + if next_output_file is not None and not next_output_file.exists(): |
| 446 | + return next_output_file, False |
| 447 | + return None |
| 448 | + |
| 449 | + |
| 450 | +def _preferred_progress_target( |
| 451 | + dod: DefinitionOfDone, |
| 452 | + *, |
| 453 | + next_pending: str | None, |
| 454 | + missing_artifact: tuple[Path, bool] | None, |
| 455 | + project_root: Path, |
| 456 | + messages: list[object], |
| 457 | +) -> Path | None: |
| 458 | + pending_items = [ |
| 459 | + item |
| 460 | + for item in effective_pending_todo_items( |
| 461 | + dod, |
| 462 | + project_root=project_root, |
| 463 | + ) |
| 464 | + if item not in _SPECIAL_DOD_ITEMS |
| 465 | + ] |
| 466 | + if next_pending and next_pending in pending_items: |
| 467 | + pending_target = infer_pending_todo_output_target( |
| 468 | + dod, |
| 469 | + next_pending, |
| 470 | + project_root=project_root, |
| 471 | + ) |
| 472 | + if pending_target is not None and not pending_target.exists(): |
| 473 | + return pending_target |
| 474 | + |
| 475 | + if missing_artifact is None: |
| 476 | + return None |
| 477 | + |
| 478 | + target, expect_directory = missing_artifact |
| 479 | + if not expect_directory: |
| 480 | + return target |
| 481 | + |
| 482 | + next_output_file, _ = infer_next_output_file( |
| 483 | + target=target, |
| 484 | + project_root=project_root, |
| 485 | + messages=list(messages or []), |
| 486 | + ) |
| 487 | + if next_output_file is not None: |
| 488 | + return next_output_file |
| 489 | + return None |