commands.py: Defines a closed slash-command grammar and parser for the workflow.runtime.py: Manages the interactive stdin loop and event-driven user input. Providesslug_from_description(text)(URL-safe fallback slug from text) andderive_workflow_id_from_llm(description, provider)(async — calls the LLM to generate a short, meaningful English slug; falls back toslug_from_descriptionon error or invalid output).scratchbook_adapter.py: ImplementsPlanTaskScratchbookAdapterfor durable persistence of all workflow data viaScratchbookService(structured I/O) andArtifactRegistry(canonical artifact records). Also exposesbuild_scratchbook_prompt_config(workflow_id)which returns aScratchbookPromptConfigcomponent wired on the agent entity soSystemPromptRenderSysteminjects scratchbook context into system prompts.state_models.py: Dataclass definitions forRuntimeState,TaskRecord,ReviewVerdict, andSubagentRecord.plan_schema.py: Handles parsing and validation of the Markdown-based workflow plans with YAML frontmatter.controller.py:PlanControllermanages the high-level workflow logic, including plan initialization and review gating.task_exec.py:TaskExechandles plan loading, dependency-aware task queueing, and subagent execution context assembly.state_machine.py:WorkflowStateMachinedefines valid phase transitions and handles process restart recovery.prompts.py: Contains system prompt templates.PLAN_INTERVIEW_SYSTEM_PROMPTenforces a progressive interview protocol: the LLM asks one question at a time, callsread_fileto get the LINE#HASH annotated content, then uses hash-anchorededit_file(op=..., pos=..., end=..., content=...)calls to fill in the matching section ofdraft.mdincrementally. Full rewrites viawrite_fileare explicitly discouraged.main.py: Entrypoint that bootstraps the ECS world. Exposesbuild_plan_task_world(model, base_dir=None, *, compaction_threshold_tokens=..., compaction_method=...)as a public factory returning(world, agent_id, adapter_ref: list[ArtifactAdapter | None], runtime_state).adapter_refstarts as[None]— no workflow ID is resolved and no scratchbook folder is created at startup. The/plan:starthandler callsderive_workflow_id_from_llm(description, provider)to get a meaningful slug, creates theArtifactAdapter, addsScratchbookPromptConfigto the ECS world, and setsadapter_ref[0]in-place. InstallsSubagentRegistryComponent(with "advisor" and "qa" subagents),SubagentSessionTableComponent,SubagentSystem(priority=-1), and framework-native auto compaction via_install_auto_compaction(...)(CompactionConfigComponent,ConversationArchiveComponent,CompactionSystem(priority=-30)). Also exposes_reset_compaction_state(...)and_reset_workflow_boundary_state(...)so workflow reload paths discard stale summaries and raw conversation history before prompt rendering.
-
No TaskSystem/TaskComponent usage: This example intentionally uses a custom
TaskExecandRuntimeStateto demonstrate manual orchestration and artifact-based persistence instead of the built-in ECS task components. -
Prompt systems run before reasoning:
WorkflowStateSystembinds the active prompt profile at priority-25,UserPromptNormalizationSystemnormalizes outbound user prompts at priority-10, andSystemPromptRenderSystemrenders the selectedSystemPromptConfigSpecat priority-5before the LLM turn begins. -
Compaction runs before prompt render:
CompactionSystemis registered at priority-30, ahead ofWorkflowStateSystem/SystemPromptRenderSystem, so any summary generated during compaction is already available for${_chat_history_summary_xml}injection on the same tick. -
ScratchbookService for I/O:
write_state/read_state/append_event/append_memory/write_review_verdictall go throughScratchbookService, which provides atomic index writes (write_index) and append-only logs (append_log). Plan file writes (write_plan,write_draft) use a local_write_text_atomicfor Markdown content not suited to JSON serialization. -
Tool result records temporarily disabled for the main agent:
ToolExecutionSystem(priority=5)is currently registered without anArtifactRegistry, so main-agent tool calls keep their raw result inline inToolResultsComponentand conversation tool messages. The genericToolResultsSinkremains available in the framework, but this example does not wire it in for now. -
ScratchbookPromptConfig wired as ECS component:
build_scratchbook_prompt_config(workflow_id)is registered as a component on the agent entity.SystemPromptRenderSystemdetects it and automatically creates aScratchbookPromptPlaceholderProviderto inject scratchbook artifact context into system prompts — no manual provider registration required. -
Progressive Draft Editing:
PLAN_INTERVIEW_SYSTEM_PROMPTestablishes a strictread_file→edit_file(op=..., pos=..., end=..., content=...)workflow. The initialdraft.mdtemplate contains seven structured sections each with unique placeholder text. The LLM reads the file first (getting LINE#HASH annotated output), then fills each section one at a time per conversation turn using hash-anchored edits. This prevents full-file rewrites and makes each edit traceable. -
Subagent compaction inheritance:
SubagentSystemcopies the parent'sCompactionConfigComponentinto each child world, seeds a freshConversationArchiveComponent, and registers its ownCompactionSystem(priority=-30). This keeps compaction behavior consistent across the main agent and subagents while preserving isolated archives. -
Workflow reload clears stale summaries and raw conversation:
/plan:startand/plan:resumeinvoke_reset_workflow_boundary_state(...), while/task:start <workflow_id>reaches the same reset path through_load_workflow(...)before task activation. That avoids carryingCurrentCompactionSummaryComponent, archived summaries, a staleRenderedSystemPromptComponent, or raw conversation messages across workflow boundaries. -
workflow_id Auto-Derivation: When
/plan:start <description>is called,derive_workflow_id_from_llm(description, provider)asks the LLM to generate a short English slug (e.g.,"writing-assistant-multi-agent"). Falls back toslug_from_description(description)on provider error or invalid output. Theadapter_ref[0]is swapped in-place to the derived id so all downstream handlers target the correct scratchbook directory. -
edit_file hash-anchored interface:
edit_fileacceptsfile_path,op,pos, optionalend,content, andworkspace_root. Thepos/endvalues use theN#HASHline references obtained from a priorread_filecall. The system prompt instructs the LLM to always read first to capture LINE#HASH values, then apply targeted replacements. -
Subagent-Driven Reviews: Advisor and QA reviews are implemented as ECS subagents registered in
SubagentRegistryComponent. The planner LLM callssubagent(category="advisor", ...)with the draft content. -
Verdict Recording via DelegationCompletedEvent: An event bus subscription automatically extracts verdicts (
approved,revise, orblocked) from subagent result text using the regex\b(approved|revise|blocked)\b(case-insensitive). It defaults toreviseif no match is found. -
Advisor Retry Loop: When the advisor returns
reviseorblocked,PLAN_INTERVIEW_SYSTEM_PROMPTinstructs the planner LLM to read the feedback, apply edits todraft.mdviaedit_file, and re-call the advisor subagent. The QA subagent is only called once the advisor returnsapproved. All advisor verdicts are appended toreview_verdicts;_missing_approved_reviewsuses the last verdict per phase to determine gating. -
Trigger Dispatch: Eight
TriggerSpec(action='script')entries handle all slash commands inside the ECS pipeline, transforming them into workflow actions. -
Circuit-Breaker for Delegation:
TaskExectracks retry counts for each task and blocks execution if a task fails repeatedly.
The system uses a canonical directory structure under scratchbook/<workflow_id>/:
plan/: Drafts and finalized plans.state/: Runtime state, event logs, and task queues.memory/: Shared knowledge across tasks.review/: Structured review verdicts.
-
Integration Tests:
tests/integration/test_plan_and_task_flow.pycovers the command surface, state machine, artifact persistence, credential-gated CLI checks, and plan-and-task compaction behavior without depending onFakeModel. -
Compaction coverage includes
test_plan_task_world_installs_auto_compaction,test_plan_task_compaction_summarizes_before_prompt_render,test_plan_start_resets_stale_compaction_state,test_plan_resume_resets_stale_compaction_state, andtest_task_start_auto_load_resets_stale_compaction_state. -
Existing setup/behavior coverage includes
test_main_world_setup_installs_subagent_infrastructure,test_delegation_event_subscription_updates_runtime_state,test_prompt_builders_return_non_empty_strings,test_build_scratchbook_prompt_config_returns_valid_config,test_main_world_does_not_add_scratchbook_prompt_config_at_init,test_plan_interview_system_prompt_instructs_edit_file_usage,test_plan_interview_system_prompt_defines_interview_flow,test_draft_template_has_structured_sections,test_draft_template_has_placeholder_content,test_edit_file_schema_exposes_edits_json,test_edit_file_old_str_replaces_content,test_edit_file_raises_when_old_str_not_found,test_edit_file_raises_on_invalid_edits_json,test_derive_workflow_id_uses_llm_slug,test_derive_workflow_id_normalizes_llm_output,test_derive_workflow_id_falls_back_on_empty_response,test_derive_workflow_id_falls_back_on_provider_error. -
Live Tests:
tests/live/test_plan_and_task_flow_live.pyprovides credential-gated acceptance tests using a real LLM model backend. It includestest_live_derive_workflow_id_from_llm_returns_valid_slug, which verifies the LLM returns a valid^[a-z][a-z0-9-]*$slug (3–50 chars) for a real Chinese task description, andtest_anthropic_plan_task_auto_compaction_summarizes_context, which exercises the auto-compaction path against an Anthropic-compatible Messages API.