Instruction to Coding Agent: Use this checklist to create concrete tasks and implement an LLM-first Point Extractor and Query Builder that plug cleanly into the existing architecture. Favor small, testable increments. Each public module/class/function must include professional docstrings. Keep files ≤500 LOC. No shims; wire via interfaces.
- Define clean contracts in the application layer (no framework deps):
-
PointExtractorGateway(port): extract concise, structured points from rawPipelineInput. -
QueryBuilderGateway(port): produce follow-up questions/queries from points and context. -
ExtractionServiceandQueryBuildingServiceorchestration services (thin, pure, testable).
-
- DTOs (domain/application):
-
ExtractedPoint(id, title, summary, evidence_refs, confidence [0-1], tags[]). -
ExtractionResult(points[], source_stats, truncated: bool). -
BuiltQuery(id, text, purpose, priority, depends_on_ids[], target_audience, suggested_tooling?). -
QueryPlan(queries[], rationale, assumptions, risks).
-
- Ensure dependency direction: presentation → application → providers/infrastructure (inward only).
- Author robust LLM prompts (system + user) for:
- Point extraction: require strict JSON output matching a JSON Schema.
- Query building: produce questions/queries with purpose and dependency fields.
- Define JSON Schemas (in
src/contexts/schemas/):-
extraction.schema.jsonforExtractionResult. -
query_plan.schema.jsonforQueryPlan.
-
- Implement parser/validator:
- Strict JSON parsing with schema validation; reject if invalid and attempt 1 retry with error message reflection.
- Safe fallbacks: if still invalid, store raw text with
validation_errorsmetadata and continue.
- Implement provider adapters behind gateways (e.g., OpenAI first):
- Route GPT‑5/4.1/o‑series through Responses API with
max_output_tokens. - Set deterministic defaults (e.g.,
temperature=0.2) unless model requires override. - Enforce timeouts via shared timeout config; no hard-coded numeric literals.
- Route GPT‑5/4.1/o‑series through Responses API with
- Add thin mappers: DTO ↔ provider payloads; keep providers isolated from application types.
- Add services:
-
ExtractionService.run(PipelineInput) -> ExtractionResult. -
QueryBuildingService.run(ExtractionResult, PipelineInput?) -> QueryPlan.
-
- Update orchestrator(s) (optional toggle):
- New preflight stage: run extraction → queries before critique, or write artifacts for later stages.
- Record outputs to artifacts directory (JSON files) and attach paths to run metadata.
- CLI flags in
run_critique.py(and any relevant entrypoints):-
--preflight-extractto enable extraction. -
--preflight-build-queriesto enable query building. -
--points-out <path>and--queries-out <path>to control JSON artifact locations. -
--max-points <n>and--max-queries <n>(caps enforced in prompts and post-filtering).
-
- Help text and examples updated; defaults sourced from
config.json.
- Extend
config.jsonwith:-
preflight.extract.enabled,preflight.extract.max_points. -
preflight.queries.enabled,preflight.queries.max_queries. - Model + provider settings for preflight stages (model name, temperature, tokens).
-
- Loader changes: keep YAML loader optional; ensure CLI path reads JSON config.
- Structured logs (no content):
- Extraction summary: points_count, truncated, time_ms.
- Query plan summary: queries_count, dependencies_present, time_ms.
- Provider context on errors: provider, operation, stage, failure_class, fallback_used.
- Emit internal metrics:
time_to_first_token_ms,total_duration_ms,emitted_countwhen streaming is applicable.
- Use shared timeout config and
operation_timeoutwrappers for blocking segments. - Retry policy: single retry on schema-parse failure with corrective system instruction.
- Never fail silently; return artifacts with
validation_errorswhen strict validation fails.
- Do not log content; only counts/ids.
- Mask API keys in logs; validate executables via
shutil.whichif any subprocesses are introduced (avoid shell). - Respect max tokens and caps to avoid data overexposure to providers.
- Unit tests:
- Prompt builder emits constraints and exemplars.
- Parser validates schema and surfaces errors; retry path covered.
- Services handle caps and truncated inputs.
- Integration tests:
- CLI with
--preflight-extractproducespoints.jsonwith valid schema. - CLI with
--preflight-build-queriesproducesqueries.jsonwith valid schema.
- CLI with
- Edge cases:
- Empty/very small input → zero points, no errors.
- Large input → capped points,
truncated=truein metadata. - Provider error → artifact with
fallback_used=trueand error logged once.
- Decomposition Output Robustness (array vs object):
- Normalization layer accepts both shapes:
- If result is a list of strings, use directly.
- If result is an object with a list-of-strings under common keys (prefer
topics, fallbackitems/subtopics), extract that list. - If neither, log a single structured warning per run (provider, model, keys seen, expected) and skip recursion for that branch.
- Prompt alignment:
- Update decomposition prompt to request an object shape:
{ "topics": ["...", "..."] }to match providers that enforcejson_objectresponses whenis_structured=true. - Alternatively, for o-series Responses API, prefer
json_schemawith an array-of-strings schema when supported; otherwise keep object-with-topics.
- Update decomposition prompt to request an object shape:
- Tests:
- Unit: parser accepts
list[str]and{topics: list[str]}; rejects other shapes with a single warning. - Integration: run with decomposition using
gpt-5and confirm no repeated warnings; recursion proceeds with extracted topics.
- Unit: parser accepts
- Normalization layer accepts both shapes:
- README: quickstart for preflight extraction and query building with example commands.
- Add short JSON schema docs under
docs/with sample outputs. - CHANGELOG: note the new preflight stages and artifacts.
- For large inputs, use a bounded chunk → summarize (map) → merge (reduce) pipeline:
- Split content into chunks by semantic boundaries with hard size caps; attach path/offset metadata.
- Run per‑chunk LLM passes (summaries/points) with item caps (e.g.,
max_points_per_chunk). - Merge and deduplicate across chunks; select top‑K globally by salience/coverage.
- Optional final pass to normalize and fill gaps; keep total tokens within configured budget.
- Keep memory bounded (no single giant prompt or whole‑corpus in memory at once); prefer streaming/iterative processing.
- Passing unit + integration tests for extraction and query building paths.
- Artifacts (
points.json,queries.json) validate against schemas. - Clean architecture preserved; no layer violations; files ≤500 LOC; full docstrings present.
- CLI help and README updated; logs show summaries without leaking content.