qwen-sci 0.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qwen_sci-0.2.2.dist-info/METADATA +492 -0
- qwen_sci-0.2.2.dist-info/RECORD +549 -0
- qwen_sci-0.2.2.dist-info/WHEEL +4 -0
- qwen_sci-0.2.2.dist-info/entry_points.txt +6 -0
- qwen_sci-0.2.2.dist-info/licenses/LICENSE +21 -0
- src/1.txt +1 -0
- src/__init__.py +1 -0
- src/__main__.py +5 -0
- src/agents/__init__.py +1 -0
- src/agents/experiment_design_agent/__init__.py +268 -0
- src/agents/experiment_design_agent/artifacts.py +747 -0
- src/agents/experiment_design_agent/cache.py +319 -0
- src/agents/experiment_design_agent/completeness.py +130 -0
- src/agents/experiment_design_agent/contracts.py +771 -0
- src/agents/experiment_design_agent/counterexample_analyzer.py +187 -0
- src/agents/experiment_design_agent/design_evidence_paper_screener.py +470 -0
- src/agents/experiment_design_agent/discipline_catalog.py +178 -0
- src/agents/experiment_design_agent/evidence_cards.py +338 -0
- src/agents/experiment_design_agent/evidence_planner.py +452 -0
- src/agents/experiment_design_agent/formal_reasoning_planner.py +884 -0
- src/agents/experiment_design_agent/fulltext_acquisition.py +863 -0
- src/agents/experiment_design_agent/idea_adapter.py +100 -0
- src/agents/experiment_design_agent/idea_intake.py +87 -0
- src/agents/experiment_design_agent/llm_json.py +328 -0
- src/agents/experiment_design_agent/orchestrator.py +1220 -0
- src/agents/experiment_design_agent/reasoning_context.py +183 -0
- src/agents/experiment_design_agent/reasoning_validation.py +546 -0
- src/agents/experiment_design_agent/run.py +481 -0
- src/agents/experiment_design_agent/run_logging.py +292 -0
- src/agents/experiment_design_agent/scope_gate.py +198 -0
- src/agents/experiment_design_agent/study_type_composer.py +1360 -0
- src/agents/experiment_design_agent/survey_evidence.py +1381 -0
- src/agents/experiment_design_agent/template_router.py +195 -0
- src/agents/experiment_design_agent/variable_claim_extractor.py +93 -0
- src/agents/idea_agent/__init__.py +1 -0
- src/agents/idea_agent/agent/__init__.py +6 -0
- src/agents/idea_agent/agent/artifacts.py +308 -0
- src/agents/idea_agent/agent/base.py +122 -0
- src/agents/idea_agent/agent/ligagent.py +488 -0
- src/agents/idea_agent/agent/mcts.py +2060 -0
- src/agents/idea_agent/agent/prompts/__init__.py +48 -0
- src/agents/idea_agent/agent/prompts/advanced_analysis.py +235 -0
- src/agents/idea_agent/agent/prompts/algorithm_alignment.py +55 -0
- src/agents/idea_agent/agent/prompts/algorithm_structuring.py +48 -0
- src/agents/idea_agent/agent/prompts/component_extraction.py +62 -0
- src/agents/idea_agent/agent/prompts/component_novelty_evaluation.py +54 -0
- src/agents/idea_agent/agent/prompts/experiment_findings_extraction.py +39 -0
- src/agents/idea_agent/agent/prompts/idea_fusion.py +295 -0
- src/agents/idea_agent/agent/prompts/idea_introduction.py +30 -0
- src/agents/idea_agent/agent/prompts/idea_result_alignment.py +39 -0
- src/agents/idea_agent/agent/prompts/input_interpreter.py +52 -0
- src/agents/idea_agent/agent/prompts/keynote_ops.py +78 -0
- src/agents/idea_agent/agent/prompts/mcts_evaluation.py +235 -0
- src/agents/idea_agent/agent/prompts/mcts_generation.py +140 -0
- src/agents/idea_agent/agent/prompts/mechanism_commit_query.py +99 -0
- src/agents/idea_agent/agent/prompts/memory_selection.py +0 -0
- src/agents/idea_agent/agent/prompts/prompt_modes.py +16 -0
- src/agents/idea_agent/agent/prompts/rag_query.py +34 -0
- src/agents/idea_agent/agent/prompts/re_analysis_replan.py +48 -0
- src/agents/idea_agent/agent/prompts/reference_grounding.py +34 -0
- src/agents/idea_agent/agent/prompts/root_domain_classification.py +32 -0
- src/agents/idea_agent/agent/prompts/scientific_debate.py +88 -0
- src/agents/idea_agent/agent/prompts/scientific_materialization.py +45 -0
- src/agents/idea_agent/agent/prompts/skill_instantiation.py +193 -0
- src/agents/idea_agent/agent/prompts/theory_transfer_query.py +93 -0
- src/agents/idea_agent/agent/prompts/topic_background.py +15 -0
- src/agents/idea_agent/agent/skills/DEFAULT_SKILL_TEMPLATES.json +263 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/alternative-path-contrast/SKILL.md +40 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/alternative-path-contrast/references/contrast_patterns.md +22 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/feedback-closed-loop/SKILL.md +40 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/feedback-closed-loop/references/mechanism_cards.md +24 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/hierarchical-decomposition/SKILL.md +39 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/hierarchical-decomposition/references/hierarchy_patterns.md +22 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/mechanism-commit-innovation/SKILL.md +39 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/mechanism-commit-innovation/references/mechanism_commit_patterns.md +24 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/multi-scale-coordinator/SKILL.md +39 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/multi-scale-coordinator/references/coordination_patterns.md +22 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/speculative-execution-with-repair/SKILL.md +41 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/speculative-execution-with-repair/references/speculation_patterns.md +22 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/surgical-modularity/SKILL.md +39 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/surgical-modularity/references/modularity_patterns.md +22 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/theory-transfer-injection/SKILL.md +40 -0
- src/agents/idea_agent/agent/skills/edit_operator_skills/theory-transfer-injection/references/transfer_patterns.md +24 -0
- src/agents/idea_agent/example/idea_result.json +900 -0
- src/agents/idea_agent/run.py +276 -0
- src/agents/idea_agent/tests/test_advanced_analysis_contract.py +90 -0
- src/agents/idea_agent/tests/test_gap_hypothesis_bridge.py +166 -0
- src/agents/idea_agent/tests/test_idea_debate.py +480 -0
- src/agents/idea_agent/tests/test_idea_result_v5.py +99 -0
- src/agents/idea_agent/tests/test_idea_synthesis.py +92 -0
- src/agents/idea_agent/tests/test_mature_idea_sources.py +27 -0
- src/agents/idea_agent/tests/test_multimodal_idea_integration.py +386 -0
- src/agents/idea_agent/tests/test_profile_materialization_and_scoring.py +219 -0
- src/agents/idea_agent/tests/test_profile_native_skill_rendering.py +122 -0
- src/agents/idea_agent/tests/test_result_persistence.py +183 -0
- src/agents/idea_agent/tests/test_scientific_intervention_profiles.py +241 -0
- src/agents/idea_agent/tests/test_scientific_rubric_and_prompts.py +559 -0
- src/agents/idea_agent/tests/test_strict_json_object_responses.py +48 -0
- src/agents/idea_agent/utils/core/__init__.py +1 -0
- src/agents/idea_agent/utils/core/ablation_inputs.py +60 -0
- src/agents/idea_agent/utils/core/chat_errors.py +95 -0
- src/agents/idea_agent/utils/core/chat_router.py +130 -0
- src/agents/idea_agent/utils/core/chat_transport.py +188 -0
- src/agents/idea_agent/utils/core/config_loader.py +166 -0
- src/agents/idea_agent/utils/core/json_utils.py +28 -0
- src/agents/idea_agent/utils/core/logger.py +287 -0
- src/agents/idea_agent/utils/core/progress.py +58 -0
- src/agents/idea_agent/utils/core/response_parsing.py +57 -0
- src/agents/idea_agent/utils/core/run_inputs.py +198 -0
- src/agents/idea_agent/utils/mcts/__init__.py +1 -0
- src/agents/idea_agent/utils/mcts/component_novelty.py +342 -0
- src/agents/idea_agent/utils/mcts/defect_registry.py +302 -0
- src/agents/idea_agent/utils/mcts/idea_routes.py +61 -0
- src/agents/idea_agent/utils/mcts/idea_taste_presets.py +312 -0
- src/agents/idea_agent/utils/mcts/mcts_helpers.py +738 -0
- src/agents/idea_agent/utils/mcts/mcts_runtime.py +3000 -0
- src/agents/idea_agent/utils/mcts/scientific_intervention_ontology.py +1561 -0
- src/agents/idea_agent/utils/mcts/scientific_rubric.py +211 -0
- src/agents/idea_agent/utils/mcts/skill_parsing.py +92 -0
- src/agents/idea_agent/utils/papers/__init__.py +1 -0
- src/agents/idea_agent/utils/papers/paper_graph_vector_store.py +499 -0
- src/agents/idea_agent/utils/papers/paper_repository.py +372 -0
- src/agents/idea_agent/utils/prompting/__init__.py +1 -0
- src/agents/idea_agent/utils/prompting/prompt_views.py +637 -0
- src/agents/idea_agent/utils/workflow/__init__.py +1 -0
- src/agents/idea_agent/utils/workflow/advanced_analysis_contract.py +244 -0
- src/agents/idea_agent/utils/workflow/gap_hypothesis_bridge.py +461 -0
- src/agents/idea_agent/utils/workflow/idea_contract.py +426 -0
- src/agents/idea_agent/utils/workflow/idea_debate.py +1238 -0
- src/agents/idea_agent/utils/workflow/idea_diversity.py +163 -0
- src/agents/idea_agent/utils/workflow/idea_fusion.py +889 -0
- src/agents/idea_agent/utils/workflow/idea_helpers.py +478 -0
- src/agents/idea_agent/utils/workflow/idea_portfolio.py +422 -0
- src/agents/idea_agent/utils/workflow/idea_synthesis.py +482 -0
- src/agents/idea_agent/utils/workflow/ligagent_flow.py +581 -0
- src/agents/idea_agent/utils/workflow/ligagent_handlers.py +1836 -0
- src/agents/idea_agent/utils/workflow/ligagent_helpers.py +1331 -0
- src/agents/idea_agent/utils/workflow/ligagent_utils.py +322 -0
- src/agents/idea_agent/utils/workflow/mature_idea_sources.py +648 -0
- src/agents/idea_agent/utils/workflow/multimodal_data_anchoring.py +578 -0
- src/agents/idea_agent/utils/workflow/result_persistence.py +774 -0
- src/agents/idea_agent/utils/workflow/stage_contract.py +76 -0
- src/agents/idea_agent/utils/workflow/workflow_runtime.py +288 -0
- src/agents/research_plan_author/__init__.py +50 -0
- src/agents/research_plan_author/artifacts.py +270 -0
- src/agents/research_plan_author/authoring_blueprint.py +1246 -0
- src/agents/research_plan_author/bibtex_renderer.py +252 -0
- src/agents/research_plan_author/contract_repair.py +131 -0
- src/agents/research_plan_author/contracts.py +745 -0
- src/agents/research_plan_author/cross_section_editor.py +232 -0
- src/agents/research_plan_author/document_quality.py +987 -0
- src/agents/research_plan_author/idea_evolution.py +223 -0
- src/agents/research_plan_author/input_loader.py +76 -0
- src/agents/research_plan_author/latex_compiler.py +260 -0
- src/agents/research_plan_author/latex_safety.py +159 -0
- src/agents/research_plan_author/llm_json.py +74 -0
- src/agents/research_plan_author/markdown_renderer.py +275 -0
- src/agents/research_plan_author/pdf_validator.py +149 -0
- src/agents/research_plan_author/render.py +166 -0
- src/agents/research_plan_author/render_artifacts.py +187 -0
- src/agents/research_plan_author/run.py +765 -0
- src/agents/research_plan_author/run_logging.py +209 -0
- src/agents/research_plan_author/section_cache.py +184 -0
- src/agents/research_plan_author/section_composer.py +1764 -0
- src/agents/research_plan_author/section_router.py +209 -0
- src/agents/research_plan_author/semantic_validator.py +254 -0
- src/agents/research_plan_author/source_bundle.py +46 -0
- src/agents/research_plan_author/source_registry.py +208 -0
- src/agents/research_plan_author/survey_source_loader.py +137 -0
- src/agents/research_plan_author/template_adapter.py +173 -0
- src/agents/research_plan_author/template_profile.py +204 -0
- src/agents/research_plan_author/tex_renderer.py +496 -0
- src/agents/research_plan_author/theory_presentation.py +134 -0
- src/agents/research_plan_author/theory_spine.py +703 -0
- src/agents/survey_agent/Dockerfile +54 -0
- src/agents/survey_agent/README.md +350 -0
- src/agents/survey_agent/config/deep_survey.yaml +217 -0
- src/agents/survey_agent/config/deep_survey_batch.yaml +176 -0
- src/agents/survey_agent/config/deep_survey_fast.yaml +183 -0
- src/agents/survey_agent/config/evaluate_baseline.yaml +197 -0
- src/agents/survey_agent/config/outcomeRAG.yaml +159 -0
- src/agents/survey_agent/env_report.md +348 -0
- src/agents/survey_agent/modules/__init__.py +0 -0
- src/agents/survey_agent/modules/code_collector.py +2194 -0
- src/agents/survey_agent/modules/code_report_generator.py +1282 -0
- src/agents/survey_agent/modules/data_manager.py +1713 -0
- src/agents/survey_agent/modules/database.py +167 -0
- src/agents/survey_agent/modules/fulltext_download_cache.py +462 -0
- src/agents/survey_agent/modules/fulltext_http.py +134 -0
- src/agents/survey_agent/modules/fulltext_resolution.py +749 -0
- src/agents/survey_agent/modules/judge.py +434 -0
- src/agents/survey_agent/modules/outcome_RAG.py +236 -0
- src/agents/survey_agent/modules/paper_graph_retriever.py +1075 -0
- src/agents/survey_agent/modules/pe.py +1729 -0
- src/agents/survey_agent/modules/pseudo_creator.py +782 -0
- src/agents/survey_agent/modules/pseudo_reviser.py +537 -0
- src/agents/survey_agent/modules/refine_agent.py +1952 -0
- src/agents/survey_agent/modules/survey_generator.py +5691 -0
- src/agents/survey_agent/modules/survey_visualizer.py +1817 -0
- src/agents/survey_agent/modules/visual_prompts.py +327 -0
- src/agents/survey_agent/modules/work_analyzer.py +2750 -0
- src/agents/survey_agent/modules/work_collector.py +5649 -0
- src/agents/survey_agent/scripts/baseline_evaluation.py +411 -0
- src/agents/survey_agent/scripts/run_deep_survey.py +224 -0
- src/agents/survey_agent/scripts/run_deep_survey_adapter.py +407 -0
- src/agents/survey_agent/scripts/run_deep_survey_adapter_with_input.py +409 -0
- src/agents/survey_agent/scripts/run_deep_survey_batch.py +228 -0
- src/agents/survey_agent/scripts/run_deep_survey_batch_arg.py +243 -0
- src/agents/survey_agent/scripts/run_deep_survey_batch_settings.py +218 -0
- src/agents/survey_agent/scripts/run_keynotes_gen.py +128 -0
- src/agents/survey_agent/scripts/run_work_clusters.py +140 -0
- src/agents/survey_agent/utils/__init__.py +0 -0
- src/agents/survey_agent/utils/ablation_utils.py +317 -0
- src/agents/survey_agent/utils/api_call.py +2518 -0
- src/agents/survey_agent/utils/chat_utils.py +82 -0
- src/agents/survey_agent/utils/config_utils.py +70 -0
- src/agents/survey_agent/utils/convert_to_md.py +29 -0
- src/agents/survey_agent/utils/err_info.py +24 -0
- src/agents/survey_agent/utils/file_utils.py +176 -0
- src/agents/survey_agent/utils/gpu_utils.py +145 -0
- src/agents/survey_agent/utils/graph_visualizer.py +279 -0
- src/agents/survey_agent/utils/html_utils.py +181 -0
- src/agents/survey_agent/utils/latex_generator_intra_cluster.py +529 -0
- src/agents/survey_agent/utils/latex_generator_relation_table.py +193 -0
- src/agents/survey_agent/utils/mineru_section_packer.py +334 -0
- src/agents/survey_agent/utils/mineru_utils.py +396 -0
- src/agents/survey_agent/utils/paper.py +144 -0
- src/agents/survey_agent/utils/read_yaml.py +15 -0
- src/agents/survey_agent/utils/repo_utils.py +167 -0
- src/agents/survey_agent/utils/rich_logger.py +77 -0
- src/agents/survey_agent/utils/step2v2.py +874 -0
- src/agents/survey_agent/utils/step2v2_extractor.py +856 -0
- src/agents/survey_agent/utils/topic_survey_storage.py +309 -0
- src/agents/survey_agent/utils/utils.py +118 -0
- src/agents/survey_agent/utils/work.py +2 -0
- src/cli.py +1611 -0
- src/config/__init__.py +213 -0
- src/config/default.yaml +1122 -0
- src/harness/LICENSE +21 -0
- src/harness/VENDORED.md +14 -0
- src/harness/src/__init__.py +0 -0
- src/harness/src/openharness/__init__.py +0 -0
- src/harness/src/openharness/__main__.py +6 -0
- src/harness/src/openharness/api/__init__.py +21 -0
- src/harness/src/openharness/api/client.py +281 -0
- src/harness/src/openharness/api/codex_client.py +407 -0
- src/harness/src/openharness/api/copilot_auth.py +240 -0
- src/harness/src/openharness/api/copilot_client.py +134 -0
- src/harness/src/openharness/api/errors.py +19 -0
- src/harness/src/openharness/api/openai_client.py +577 -0
- src/harness/src/openharness/api/provider.py +186 -0
- src/harness/src/openharness/api/registry.py +437 -0
- src/harness/src/openharness/api/usage.py +17 -0
- src/harness/src/openharness/auth/__init__.py +29 -0
- src/harness/src/openharness/auth/external.py +610 -0
- src/harness/src/openharness/auth/flows.py +189 -0
- src/harness/src/openharness/auth/manager.py +482 -0
- src/harness/src/openharness/auth/storage.py +269 -0
- src/harness/src/openharness/autopilot/__init__.py +23 -0
- src/harness/src/openharness/autopilot/service.py +2239 -0
- src/harness/src/openharness/autopilot/types.py +91 -0
- src/harness/src/openharness/bridge/__init__.py +20 -0
- src/harness/src/openharness/bridge/manager.py +106 -0
- src/harness/src/openharness/bridge/session_runner.py +46 -0
- src/harness/src/openharness/bridge/types.py +37 -0
- src/harness/src/openharness/bridge/work_secret.py +41 -0
- src/harness/src/openharness/channels/UPSTREAM +6 -0
- src/harness/src/openharness/channels/__init__.py +22 -0
- src/harness/src/openharness/channels/adapter.py +131 -0
- src/harness/src/openharness/channels/bus/__init__.py +6 -0
- src/harness/src/openharness/channels/bus/events.py +38 -0
- src/harness/src/openharness/channels/bus/queue.py +44 -0
- src/harness/src/openharness/channels/impl/__init__.py +6 -0
- src/harness/src/openharness/channels/impl/base.py +141 -0
- src/harness/src/openharness/channels/impl/dingtalk.py +445 -0
- src/harness/src/openharness/channels/impl/discord.py +311 -0
- src/harness/src/openharness/channels/impl/email.py +410 -0
- src/harness/src/openharness/channels/impl/feishu.py +1342 -0
- src/harness/src/openharness/channels/impl/manager.py +257 -0
- src/harness/src/openharness/channels/impl/matrix.py +700 -0
- src/harness/src/openharness/channels/impl/mochat.py +897 -0
- src/harness/src/openharness/channels/impl/qq.py +141 -0
- src/harness/src/openharness/channels/impl/slack.py +284 -0
- src/harness/src/openharness/channels/impl/telegram.py +525 -0
- src/harness/src/openharness/channels/impl/whatsapp.py +159 -0
- src/harness/src/openharness/cli.py +2551 -0
- src/harness/src/openharness/commands/__init__.py +21 -0
- src/harness/src/openharness/commands/registry.py +2783 -0
- src/harness/src/openharness/config/__init__.py +34 -0
- src/harness/src/openharness/config/paths.py +160 -0
- src/harness/src/openharness/config/schema.py +119 -0
- src/harness/src/openharness/config/settings.py +1104 -0
- src/harness/src/openharness/coordinator/__init__.py +12 -0
- src/harness/src/openharness/coordinator/agent_definitions.py +975 -0
- src/harness/src/openharness/coordinator/coordinator_mode.py +520 -0
- src/harness/src/openharness/engine/__init__.py +80 -0
- src/harness/src/openharness/engine/cost_tracker.py +24 -0
- src/harness/src/openharness/engine/messages.py +222 -0
- src/harness/src/openharness/engine/query.py +1188 -0
- src/harness/src/openharness/engine/query_engine.py +313 -0
- src/harness/src/openharness/engine/stream_events.py +90 -0
- src/harness/src/openharness/hooks/__init__.py +50 -0
- src/harness/src/openharness/hooks/events.py +20 -0
- src/harness/src/openharness/hooks/executor.py +242 -0
- src/harness/src/openharness/hooks/hot_reload.py +31 -0
- src/harness/src/openharness/hooks/loader.py +68 -0
- src/harness/src/openharness/hooks/schemas.py +66 -0
- src/harness/src/openharness/hooks/types.py +38 -0
- src/harness/src/openharness/keybindings/__init__.py +14 -0
- src/harness/src/openharness/keybindings/default_bindings.py +11 -0
- src/harness/src/openharness/keybindings/loader.py +22 -0
- src/harness/src/openharness/keybindings/parser.py +18 -0
- src/harness/src/openharness/keybindings/resolver.py +13 -0
- src/harness/src/openharness/mcp/__init__.py +79 -0
- src/harness/src/openharness/mcp/client.py +298 -0
- src/harness/src/openharness/mcp/config.py +16 -0
- src/harness/src/openharness/mcp/types.py +76 -0
- src/harness/src/openharness/memory/__init__.py +25 -0
- src/harness/src/openharness/memory/agent.py +91 -0
- src/harness/src/openharness/memory/manager.py +183 -0
- src/harness/src/openharness/memory/memdir.py +52 -0
- src/harness/src/openharness/memory/migrate.py +137 -0
- src/harness/src/openharness/memory/paths.py +22 -0
- src/harness/src/openharness/memory/relevance.py +145 -0
- src/harness/src/openharness/memory/scan.py +116 -0
- src/harness/src/openharness/memory/schema.py +443 -0
- src/harness/src/openharness/memory/search.py +71 -0
- src/harness/src/openharness/memory/team.py +90 -0
- src/harness/src/openharness/memory/types.py +33 -0
- src/harness/src/openharness/memory/usage.py +153 -0
- src/harness/src/openharness/output_styles/__init__.py +5 -0
- src/harness/src/openharness/output_styles/loader.py +42 -0
- src/harness/src/openharness/permissions/__init__.py +26 -0
- src/harness/src/openharness/permissions/checker.py +200 -0
- src/harness/src/openharness/permissions/modes.py +13 -0
- src/harness/src/openharness/personalization/__init__.py +6 -0
- src/harness/src/openharness/personalization/extractor.py +138 -0
- src/harness/src/openharness/personalization/rules.py +64 -0
- src/harness/src/openharness/personalization/session_hook.py +65 -0
- src/harness/src/openharness/platforms.py +86 -0
- src/harness/src/openharness/plugins/__init__.py +53 -0
- src/harness/src/openharness/plugins/bundled/__init__.py +0 -0
- src/harness/src/openharness/plugins/installer.py +39 -0
- src/harness/src/openharness/plugins/loader.py +730 -0
- src/harness/src/openharness/plugins/schemas.py +24 -0
- src/harness/src/openharness/plugins/types.py +59 -0
- src/harness/src/openharness/prompts/__init__.py +14 -0
- src/harness/src/openharness/prompts/claudemd.py +48 -0
- src/harness/src/openharness/prompts/context.py +187 -0
- src/harness/src/openharness/prompts/environment.py +135 -0
- src/harness/src/openharness/prompts/system_prompt.py +109 -0
- src/harness/src/openharness/sandbox/Dockerfile +6 -0
- src/harness/src/openharness/sandbox/__init__.py +33 -0
- src/harness/src/openharness/sandbox/adapter.py +148 -0
- src/harness/src/openharness/sandbox/docker_backend.py +232 -0
- src/harness/src/openharness/sandbox/docker_image.py +103 -0
- src/harness/src/openharness/sandbox/path_validator.py +37 -0
- src/harness/src/openharness/sandbox/session.py +63 -0
- src/harness/src/openharness/services/__init__.py +30 -0
- src/harness/src/openharness/services/autodream/__init__.py +34 -0
- src/harness/src/openharness/services/autodream/backup.py +104 -0
- src/harness/src/openharness/services/autodream/lock.py +138 -0
- src/harness/src/openharness/services/autodream/prompt.py +128 -0
- src/harness/src/openharness/services/autodream/service.py +313 -0
- src/harness/src/openharness/services/compact/__init__.py +1725 -0
- src/harness/src/openharness/services/cron.py +137 -0
- src/harness/src/openharness/services/cron_scheduler.py +595 -0
- src/harness/src/openharness/services/lsp/__init__.py +216 -0
- src/harness/src/openharness/services/memory_extract/__init__.py +261 -0
- src/harness/src/openharness/services/oauth/__init__.py +0 -0
- src/harness/src/openharness/services/session_backend.py +97 -0
- src/harness/src/openharness/services/session_memory/__init__.py +139 -0
- src/harness/src/openharness/services/session_storage.py +230 -0
- src/harness/src/openharness/services/token_estimation.py +15 -0
- src/harness/src/openharness/services/tool_outputs.py +55 -0
- src/harness/src/openharness/skills/__init__.py +44 -0
- src/harness/src/openharness/skills/_frontmatter.py +97 -0
- src/harness/src/openharness/skills/bundled/__init__.py +75 -0
- src/harness/src/openharness/skills/bundled/content/commit.md +25 -0
- src/harness/src/openharness/skills/bundled/content/debug.md +25 -0
- src/harness/src/openharness/skills/bundled/content/diagnose.md +35 -0
- src/harness/src/openharness/skills/bundled/content/plan.md +39 -0
- src/harness/src/openharness/skills/bundled/content/review.md +33 -0
- src/harness/src/openharness/skills/bundled/content/simplify.md +26 -0
- src/harness/src/openharness/skills/bundled/content/skill-creator.md +175 -0
- src/harness/src/openharness/skills/bundled/content/test.md +38 -0
- src/harness/src/openharness/skills/loader.py +229 -0
- src/harness/src/openharness/skills/registry.py +29 -0
- src/harness/src/openharness/skills/types.py +24 -0
- src/harness/src/openharness/state/__init__.py +6 -0
- src/harness/src/openharness/state/app_state.py +30 -0
- src/harness/src/openharness/state/store.py +40 -0
- src/harness/src/openharness/swarm/__init__.py +70 -0
- src/harness/src/openharness/swarm/in_process.py +693 -0
- src/harness/src/openharness/swarm/lockfile.py +24 -0
- src/harness/src/openharness/swarm/mailbox.py +522 -0
- src/harness/src/openharness/swarm/permission_sync.py +1168 -0
- src/harness/src/openharness/swarm/registry.py +410 -0
- src/harness/src/openharness/swarm/spawn_utils.py +229 -0
- src/harness/src/openharness/swarm/subprocess_backend.py +171 -0
- src/harness/src/openharness/swarm/team_lifecycle.py +910 -0
- src/harness/src/openharness/swarm/types.py +398 -0
- src/harness/src/openharness/swarm/worktree.py +315 -0
- src/harness/src/openharness/tasks/__init__.py +18 -0
- src/harness/src/openharness/tasks/local_agent_task.py +28 -0
- src/harness/src/openharness/tasks/local_shell_task.py +17 -0
- src/harness/src/openharness/tasks/manager.py +475 -0
- src/harness/src/openharness/tasks/stop_task.py +11 -0
- src/harness/src/openharness/tasks/types.py +32 -0
- src/harness/src/openharness/themes/__init__.py +21 -0
- src/harness/src/openharness/themes/builtin.py +89 -0
- src/harness/src/openharness/themes/loader.py +55 -0
- src/harness/src/openharness/themes/schema.py +54 -0
- src/harness/src/openharness/tools/__init__.py +107 -0
- src/harness/src/openharness/tools/agent_tool.py +141 -0
- src/harness/src/openharness/tools/ask_user_question_tool.py +46 -0
- src/harness/src/openharness/tools/base.py +80 -0
- src/harness/src/openharness/tools/bash_tool.py +259 -0
- src/harness/src/openharness/tools/brief_tool.py +33 -0
- src/harness/src/openharness/tools/config_tool.py +54 -0
- src/harness/src/openharness/tools/cron_create_tool.py +105 -0
- src/harness/src/openharness/tools/cron_delete_tool.py +32 -0
- src/harness/src/openharness/tools/cron_list_tool.py +69 -0
- src/harness/src/openharness/tools/cron_toggle_tool.py +37 -0
- src/harness/src/openharness/tools/enter_plan_mode_tool.py +28 -0
- src/harness/src/openharness/tools/enter_worktree_tool.py +80 -0
- src/harness/src/openharness/tools/exit_plan_mode_tool.py +28 -0
- src/harness/src/openharness/tools/exit_worktree_tool.py +42 -0
- src/harness/src/openharness/tools/file_edit_tool.py +95 -0
- src/harness/src/openharness/tools/file_read_tool.py +72 -0
- src/harness/src/openharness/tools/file_write_tool.py +87 -0
- src/harness/src/openharness/tools/glob_tool.py +175 -0
- src/harness/src/openharness/tools/grep_tool.py +374 -0
- src/harness/src/openharness/tools/image_generation_tool.py +447 -0
- src/harness/src/openharness/tools/image_to_text_tool.py +237 -0
- src/harness/src/openharness/tools/list_mcp_resources_tool.py +36 -0
- src/harness/src/openharness/tools/lsp_tool.py +154 -0
- src/harness/src/openharness/tools/mcp_auth_tool.py +71 -0
- src/harness/src/openharness/tools/mcp_tool.py +72 -0
- src/harness/src/openharness/tools/notebook_edit_tool.py +97 -0
- src/harness/src/openharness/tools/read_mcp_resource_tool.py +38 -0
- src/harness/src/openharness/tools/remote_trigger_tool.py +74 -0
- src/harness/src/openharness/tools/send_message_tool.py +58 -0
- src/harness/src/openharness/tools/skill_tool.py +43 -0
- src/harness/src/openharness/tools/sleep_tool.py +32 -0
- src/harness/src/openharness/tools/task_create_tool.py +56 -0
- src/harness/src/openharness/tools/task_get_tool.py +33 -0
- src/harness/src/openharness/tools/task_list_tool.py +35 -0
- src/harness/src/openharness/tools/task_output_tool.py +35 -0
- src/harness/src/openharness/tools/task_stop_tool.py +30 -0
- src/harness/src/openharness/tools/task_update_tool.py +50 -0
- src/harness/src/openharness/tools/team_create_tool.py +31 -0
- src/harness/src/openharness/tools/team_delete_tool.py +30 -0
- src/harness/src/openharness/tools/todo_write_tool.py +46 -0
- src/harness/src/openharness/tools/tool_search_tool.py +38 -0
- src/harness/src/openharness/tools/web_fetch_tool.py +117 -0
- src/harness/src/openharness/tools/web_search_tool.py +119 -0
- src/harness/src/openharness/ui/__init__.py +5 -0
- src/harness/src/openharness/ui/app.py +320 -0
- src/harness/src/openharness/ui/backend_host.py +941 -0
- src/harness/src/openharness/ui/coordinator_drain.py +198 -0
- src/harness/src/openharness/ui/input.py +32 -0
- src/harness/src/openharness/ui/output.py +265 -0
- src/harness/src/openharness/ui/permission_dialog.py +14 -0
- src/harness/src/openharness/ui/protocol.py +247 -0
- src/harness/src/openharness/ui/react_launcher.py +179 -0
- src/harness/src/openharness/ui/runtime.py +799 -0
- src/harness/src/openharness/ui/textual_app.py +495 -0
- src/harness/src/openharness/utils/__init__.py +0 -0
- src/harness/src/openharness/utils/file_lock.py +86 -0
- src/harness/src/openharness/utils/fs.py +98 -0
- src/harness/src/openharness/utils/helpers.py +77 -0
- src/harness/src/openharness/utils/network_guard.py +340 -0
- src/harness/src/openharness/utils/shell.py +147 -0
- src/harness/src/openharness/vim/__init__.py +5 -0
- src/harness/src/openharness/vim/transitions.py +8 -0
- src/harness/src/openharness/voice/__init__.py +7 -0
- src/harness/src/openharness/voice/keyterms.py +10 -0
- src/harness/src/openharness/voice/stream_stt.py +8 -0
- src/harness/src/openharness/voice/voice_mode.py +44 -0
- src/llm/__init__.py +29 -0
- src/llm/image_generation.py +402 -0
- src/llm/provider_registry.py +419 -0
- src/llm/runtime_env.py +64 -0
- src/llm/structured_output.py +131 -0
- src/llm/vision.py +198 -0
- src/memory/__init__.py +2 -0
- src/memory/api/__init__.py +1 -0
- src/memory/api/base_memory_system_api.py +98 -0
- src/memory/api/base_symbolic_memory_system_api.py +190 -0
- src/memory/api/base_vector_memory_system_api.py +116 -0
- src/memory/api/faiss_memory_system_api.py +228 -0
- src/memory/api/slot_process_api.py +827 -0
- src/memory/api/symbolic_memory_system_api.py +572 -0
- src/memory/memory_system/__init__.py +48 -0
- src/memory/memory_system/component_taxonomy.py +430 -0
- src/memory/memory_system/llm.py +437 -0
- src/memory/memory_system/models.py +168 -0
- src/memory/memory_system/user_prompt.py +418 -0
- src/memory/memory_system/utils.py +282 -0
- src/memory/memory_system/vectorstore.py +311 -0
- src/memory/memory_system/working_slot.py +99 -0
- src/pipeline/__init__.py +129 -0
- src/pipeline/contracts.py +531 -0
- src/pipeline/discipline_taxonomy.py +916 -0
- src/pipeline/evidence_coverage_ledger.py +1150 -0
- src/pipeline/evidence_refinement.py +620 -0
- src/pipeline/experiment_to_symbolic.py +137 -0
- src/pipeline/multimodal_evidence/__init__.py +28 -0
- src/pipeline/multimodal_evidence/capabilities.py +107 -0
- src/pipeline/multimodal_evidence/claims.py +135 -0
- src/pipeline/multimodal_evidence/contract.py +291 -0
- src/pipeline/multimodal_evidence/data_sh_compiler.py +505 -0
- src/pipeline/multimodal_evidence/handoff_binding.py +135 -0
- src/pipeline/multimodal_evidence/manifest.py +254 -0
- src/pipeline/multimodal_evidence/native_analysis.py +479 -0
- src/pipeline/multimodal_evidence/perception.py +277 -0
- src/pipeline/multimodal_evidence/query_binding.py +65 -0
- src/pipeline/multimodal_evidence/rendering.py +120 -0
- src/pipeline/multimodal_evidence/safety.py +42 -0
- src/pipeline/multimodal_evidence/sampling.py +69 -0
- src/pipeline/multimodal_evidence/service.py +148 -0
- src/pipeline/multimodal_evidence/survey_integration.py +460 -0
- src/pipeline/paper_identity.py +52 -0
- src/pipeline/research_design_inventory.py +338 -0
- src/pipeline/research_domain_resolution.py +669 -0
- src/pipeline/research_identity.py +548 -0
- src/pipeline/research_question_contract.py +646 -0
- src/pipeline/retrieval_lanes.py +889 -0
- src/pipeline/run_loop.py +862 -0
- src/pipeline/science_events.py +30 -0
- src/pipeline/science_manifests.py +602 -0
- src/pipeline/science_run.py +918 -0
- src/pipeline/science_stages.py +709 -0
- src/pipeline/science_workflow.py +807 -0
- src/pipeline/sh_cluster_projection.py +306 -0
- src/pipeline/sh_graph_provenance.py +648 -0
- src/pipeline/subhypothesis_decomposition.py +392 -0
- src/pipeline/subhypothesis_evidence.py +856 -0
- src/pipeline/survey_evidence_plan.py +858 -0
- src/pipeline/survey_gap_adjudication.py +647 -0
- src/pipeline/survey_gap_candidates.py +840 -0
- src/pipeline/survey_gap_ledger.py +683 -0
- src/pipeline/survey_gap_triage.py +401 -0
- src/pipeline/survey_handoff_persistence.py +481 -0
- src/pipeline/survey_handoff_projection.py +457 -0
- src/pipeline/survey_idea_handoff.py +1112 -0
- src/pipeline/survey_idea_loader.py +289 -0
- src/research_run_ids.py +11 -0
|
@@ -0,0 +1,747 @@
|
|
|
1
|
+
"""Deterministic ExperimentDesign, Markdown, and Author handoff artifacts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from copy import deepcopy
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
import json
|
|
9
|
+
import hashlib
|
|
10
|
+
import os
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
import re
|
|
13
|
+
import tempfile
|
|
14
|
+
from collections.abc import Mapping
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from .contracts import validate_experiment_design
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
ARTIFACT_SCHEMA_VERSION = "experiment_design_artifacts_v1"
|
|
21
|
+
AUTHOR_HANDOFF_SCHEMA_VERSION = "research_plan_author_input_v3"
|
|
22
|
+
TIMESTAMP_FORMAT = "%Y%m%d-%H%M%S-%f"
|
|
23
|
+
_TIMESTAMP_PATTERN = re.compile(r"^\d{8}-\d{6}-\d{6}$")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ArtifactError(RuntimeError):
|
|
27
|
+
"""Base error for ExperimentDesign artifact generation."""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ArtifactValidationError(ArtifactError):
|
|
31
|
+
"""Raised when an unvalidated design is submitted for artifact writing."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ArtifactWriteError(ArtifactError):
|
|
35
|
+
"""Raised when artifact output cannot be written atomically."""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True)
|
|
39
|
+
class ExperimentDesignArtifactPaths:
|
|
40
|
+
"""Paths for the three artifacts produced from one validated design."""
|
|
41
|
+
|
|
42
|
+
timestamp: str
|
|
43
|
+
collision_index: int
|
|
44
|
+
experiment_design_json: Path
|
|
45
|
+
experiment_design_markdown: Path
|
|
46
|
+
author_json: Path
|
|
47
|
+
|
|
48
|
+
def as_dict(self) -> dict[str, Any]:
|
|
49
|
+
return {
|
|
50
|
+
"schema_version": ARTIFACT_SCHEMA_VERSION,
|
|
51
|
+
"timestamp": self.timestamp,
|
|
52
|
+
"collision_index": self.collision_index,
|
|
53
|
+
"experiment_design_json": str(self.experiment_design_json),
|
|
54
|
+
"experiment_design_markdown": str(self.experiment_design_markdown),
|
|
55
|
+
"author_json": str(self.author_json),
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _mapping(value: object) -> dict[str, Any]:
|
|
60
|
+
return dict(value) if isinstance(value, Mapping) else {}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _text(value: object) -> str:
|
|
64
|
+
return str(value or "").strip()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _iso_datetime(value: datetime | None = None) -> str:
|
|
68
|
+
current = value or datetime.now().astimezone()
|
|
69
|
+
if current.tzinfo is None:
|
|
70
|
+
current = current.astimezone()
|
|
71
|
+
return current.isoformat(timespec="microseconds")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def generate_timestamp(generated_at: datetime | None = None) -> str:
|
|
75
|
+
"""Return the filename timestamp in local time without timezone punctuation."""
|
|
76
|
+
|
|
77
|
+
current = generated_at or datetime.now().astimezone()
|
|
78
|
+
if current.tzinfo is None:
|
|
79
|
+
current = current.astimezone()
|
|
80
|
+
timestamp = current.strftime(TIMESTAMP_FORMAT)
|
|
81
|
+
if not _TIMESTAMP_PATTERN.fullmatch(timestamp):
|
|
82
|
+
raise ValueError(f"Generated timestamp does not match {TIMESTAMP_FORMAT}: {timestamp}")
|
|
83
|
+
return timestamp
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _extract_design(payload: object) -> tuple[dict[str, Any], dict[str, Any]]:
|
|
87
|
+
if not isinstance(payload, dict):
|
|
88
|
+
raise ArtifactValidationError("Artifact input must be a JSON object")
|
|
89
|
+
run_payload = dict(payload)
|
|
90
|
+
nested_design = payload.get("experiment_design")
|
|
91
|
+
design = dict(nested_design) if isinstance(nested_design, dict) else dict(payload)
|
|
92
|
+
if "experiment_design" in design and not isinstance(nested_design, dict):
|
|
93
|
+
design.pop("experiment_design", None)
|
|
94
|
+
return design, run_payload
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _validated_design(payload: object) -> tuple[dict[str, Any], dict[str, Any]]:
|
|
98
|
+
design, run_payload = _extract_design(payload)
|
|
99
|
+
errors = validate_experiment_design(design)
|
|
100
|
+
if errors:
|
|
101
|
+
raise ArtifactValidationError("ExperimentDesign validation failed: " + "; ".join(errors))
|
|
102
|
+
return deepcopy(design), deepcopy(run_payload)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _canonical_item(field_path: object, status: str, reason: object) -> dict[str, str]:
|
|
106
|
+
return {
|
|
107
|
+
"field_path": _text(field_path),
|
|
108
|
+
"status": status,
|
|
109
|
+
"reason": _text(reason),
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _stable_items(items: list[dict[str, str]]) -> list[dict[str, str]]:
|
|
114
|
+
reasons_by_item: dict[tuple[str, str], set[str]] = {}
|
|
115
|
+
for item in items:
|
|
116
|
+
field_path = item["field_path"]
|
|
117
|
+
status = item["status"]
|
|
118
|
+
reason = item["reason"]
|
|
119
|
+
if field_path and status and reason:
|
|
120
|
+
reasons_by_item.setdefault((field_path, status), set()).add(reason)
|
|
121
|
+
return [
|
|
122
|
+
_canonical_item(field_path, status, "; ".join(sorted(reasons_by_item[(field_path, status)])))
|
|
123
|
+
for field_path, status in sorted(reasons_by_item)
|
|
124
|
+
]
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _explicit_unknown_items(container: Mapping[str, Any], *, field_prefix: str) -> list[dict[str, str]]:
|
|
128
|
+
collected: list[dict[str, str]] = []
|
|
129
|
+
for index, raw_item in enumerate(container.get("unknown_items") or [], start=1):
|
|
130
|
+
item = _mapping(raw_item)
|
|
131
|
+
if item:
|
|
132
|
+
field_path = _text(item.get("field_path")) or f"{field_prefix}.unknown_items[{index}]"
|
|
133
|
+
reason = _text(item.get("reason")) or "The final canonical item remains unresolved."
|
|
134
|
+
else:
|
|
135
|
+
field_path = f"{field_prefix}.unknown_items[{index}]"
|
|
136
|
+
reason = _text(raw_item) or "The final canonical item remains unresolved."
|
|
137
|
+
collected.append(_canonical_item(field_path, "needs_human_input", reason))
|
|
138
|
+
return collected
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _canonical_unknown_items(design: Mapping[str, Any]) -> list[dict[str, str]]:
|
|
142
|
+
"""Derive Author open items exclusively from the final canonical design."""
|
|
143
|
+
|
|
144
|
+
collected: list[dict[str, str]] = []
|
|
145
|
+
for field_path, status in sorted(_mapping(design.get("field_statuses")).items()):
|
|
146
|
+
if _text(status) == "needs_human_input":
|
|
147
|
+
collected.append(
|
|
148
|
+
_canonical_item(
|
|
149
|
+
field_path,
|
|
150
|
+
"needs_human_input",
|
|
151
|
+
"The final canonical field status requires human input.",
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
for index, question in enumerate(design.get("open_design_questions") or [], start=1):
|
|
155
|
+
item = _mapping(question)
|
|
156
|
+
collected.append(
|
|
157
|
+
_canonical_item(
|
|
158
|
+
_text(item.get("field_path")) or f"open_design_questions[{index}]",
|
|
159
|
+
"needs_human_input",
|
|
160
|
+
_text(item.get("reason")) or _text(question) or "The final canonical design question remains open.",
|
|
161
|
+
)
|
|
162
|
+
)
|
|
163
|
+
brief = _mapping(design.get("research_brief"))
|
|
164
|
+
for index, known_unknown in enumerate(brief.get("known_unknowns") or [], start=1):
|
|
165
|
+
if _text(known_unknown):
|
|
166
|
+
collected.append(
|
|
167
|
+
_canonical_item(
|
|
168
|
+
f"research_brief.known_unknowns[{index}]",
|
|
169
|
+
"needs_human_input",
|
|
170
|
+
known_unknown,
|
|
171
|
+
)
|
|
172
|
+
)
|
|
173
|
+
collected.extend(_explicit_unknown_items(_mapping(design.get("variable_claim_model")), field_prefix="variable_claim_model"))
|
|
174
|
+
collected.extend(_explicit_unknown_items(_mapping(design.get("formal_reasoning_plan")), field_prefix="formal_reasoning_plan"))
|
|
175
|
+
collected.extend(_explicit_unknown_items(_mapping(design.get("counterexample_analysis")), field_prefix="counterexample_analysis"))
|
|
176
|
+
return _stable_items(collected)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _canonical_review_items(design: Mapping[str, Any]) -> list[dict[str, str]]:
|
|
180
|
+
"""Derive stable review requirements from final canonical review states."""
|
|
181
|
+
|
|
182
|
+
collected: list[dict[str, str]] = []
|
|
183
|
+
risk = _mapping(design.get("risk_and_human_review"))
|
|
184
|
+
if risk.get("human_review_required") is True:
|
|
185
|
+
collected.append(
|
|
186
|
+
_canonical_item(
|
|
187
|
+
"risk_and_human_review.human_review_required",
|
|
188
|
+
"review_required",
|
|
189
|
+
"The final canonical risk gate requires qualified human review.",
|
|
190
|
+
)
|
|
191
|
+
)
|
|
192
|
+
for index, trigger in enumerate(risk.get("review_triggers") or [], start=1):
|
|
193
|
+
if _text(trigger):
|
|
194
|
+
collected.append(
|
|
195
|
+
_canonical_item(
|
|
196
|
+
f"risk_and_human_review.review_triggers[{index}]",
|
|
197
|
+
"review_required",
|
|
198
|
+
trigger,
|
|
199
|
+
)
|
|
200
|
+
)
|
|
201
|
+
for field_path in ("formal_reasoning_plan", "counterexample_analysis"):
|
|
202
|
+
component = _mapping(design.get(field_path))
|
|
203
|
+
if _text(component.get("status")) == "requires_human_review":
|
|
204
|
+
collected.append(
|
|
205
|
+
_canonical_item(
|
|
206
|
+
field_path,
|
|
207
|
+
"review_required",
|
|
208
|
+
f"The final canonical {field_path} status requires qualified human review.",
|
|
209
|
+
)
|
|
210
|
+
)
|
|
211
|
+
return _stable_items(collected)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
_BIBLIOGRAPHIC_METADATA_FIELDS = ("authors", "title", "year", "venue", "doi", "url")
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _compact_bibliographic_metadata(record: Mapping[str, Any]) -> dict[str, Any]:
|
|
218
|
+
return {
|
|
219
|
+
field: deepcopy(record[field])
|
|
220
|
+
for field in _BIBLIOGRAPHIC_METADATA_FIELDS
|
|
221
|
+
if record.get(field) not in (None, "", [])
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _compact_source_registry(evidence_bundle: Mapping[str, Any]) -> dict[str, Any]:
|
|
226
|
+
cards: dict[str, dict[str, Any]] = {}
|
|
227
|
+
allowed_source_ids: list[str] = []
|
|
228
|
+
citations: list[dict[str, Any]] = []
|
|
229
|
+
papers_by_source_id: dict[str, Mapping[str, Any]] = {}
|
|
230
|
+
for raw_paper in evidence_bundle.get("paper_registry") or []:
|
|
231
|
+
if not isinstance(raw_paper, Mapping):
|
|
232
|
+
continue
|
|
233
|
+
source_id = _text(raw_paper.get("canonical_paper_id"))
|
|
234
|
+
if source_id:
|
|
235
|
+
papers_by_source_id[source_id] = raw_paper
|
|
236
|
+
for index, raw_card in enumerate(evidence_bundle.get("evidence_cards") or [], start=1):
|
|
237
|
+
if not isinstance(raw_card, Mapping):
|
|
238
|
+
continue
|
|
239
|
+
card_id = _text(raw_card.get("card_id") or raw_card.get("evidence_card_id")) or f"evidence-card-{index}"
|
|
240
|
+
source_id = _text(raw_card.get("source_id") or raw_card.get("canonical_paper_id"))
|
|
241
|
+
record = {
|
|
242
|
+
"card_id": card_id,
|
|
243
|
+
"source_id": source_id,
|
|
244
|
+
"citation_key": (
|
|
245
|
+
f"cite_{hashlib.sha256(source_id.encode('utf-8')).hexdigest()[:16]}"
|
|
246
|
+
if source_id
|
|
247
|
+
else f"cite_{card_id}"
|
|
248
|
+
),
|
|
249
|
+
"evidence_level": _text(raw_card.get("evidence_level")) or "metadata",
|
|
250
|
+
"claim_slot": _text(raw_card.get("claim_slot")),
|
|
251
|
+
"source_location": _text(raw_card.get("source_location")),
|
|
252
|
+
"support_statement": _text(raw_card.get("statement")),
|
|
253
|
+
}
|
|
254
|
+
cards[card_id] = record
|
|
255
|
+
if source_id and source_id not in allowed_source_ids:
|
|
256
|
+
allowed_source_ids.append(source_id)
|
|
257
|
+
if source_id:
|
|
258
|
+
existing = next((item for item in citations if item.get("source_id") == source_id), None)
|
|
259
|
+
if existing is None:
|
|
260
|
+
paper = papers_by_source_id.get(source_id, {})
|
|
261
|
+
citation = {
|
|
262
|
+
"citation_key": record["citation_key"],
|
|
263
|
+
"source_id": source_id,
|
|
264
|
+
"evidence_level": record["evidence_level"],
|
|
265
|
+
"evidence_card_ids": [card_id],
|
|
266
|
+
}
|
|
267
|
+
bibliographic_metadata = _compact_bibliographic_metadata(paper)
|
|
268
|
+
if bibliographic_metadata:
|
|
269
|
+
citation["bibliographic_metadata"] = bibliographic_metadata
|
|
270
|
+
citation_rendering_status = _text(paper.get("citation_rendering_status"))
|
|
271
|
+
if citation_rendering_status:
|
|
272
|
+
citation["citation_rendering_status"] = citation_rendering_status
|
|
273
|
+
citation_missing_fields = [
|
|
274
|
+
_text(field)
|
|
275
|
+
for field in paper.get("citation_missing_fields") or []
|
|
276
|
+
if _text(field)
|
|
277
|
+
]
|
|
278
|
+
if citation_missing_fields:
|
|
279
|
+
citation["citation_missing_fields"] = citation_missing_fields
|
|
280
|
+
citations.append(citation)
|
|
281
|
+
elif card_id not in existing["evidence_card_ids"]:
|
|
282
|
+
existing["evidence_card_ids"].append(card_id)
|
|
283
|
+
return {
|
|
284
|
+
"allowed_source_ids": allowed_source_ids,
|
|
285
|
+
"allowed_survey_anchor_ids": [],
|
|
286
|
+
"evidence_cards_by_id": cards,
|
|
287
|
+
"citation_registry": citations,
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def _formal_reasoning_summary(plan: Mapping[str, Any]) -> dict[str, Any]:
|
|
292
|
+
def records(collection: str, identifier: str, fields: tuple[str, ...]) -> list[dict[str, Any]]:
|
|
293
|
+
output: list[dict[str, Any]] = []
|
|
294
|
+
for raw in plan.get(collection) or []:
|
|
295
|
+
if not isinstance(raw, Mapping):
|
|
296
|
+
continue
|
|
297
|
+
item = {key: deepcopy(raw[key]) for key in fields if key in raw}
|
|
298
|
+
if _text(item.get(identifier)):
|
|
299
|
+
output.append(item)
|
|
300
|
+
return output
|
|
301
|
+
|
|
302
|
+
forward = _mapping(plan.get("forward_derivation"))
|
|
303
|
+
return {
|
|
304
|
+
"schema_version": _text(plan.get("schema_version")),
|
|
305
|
+
"applicability": _text(plan.get("applicability")),
|
|
306
|
+
"status": _text(plan.get("status")),
|
|
307
|
+
"target_proposition_id": _text(plan.get("target_proposition_id") or forward.get("target_proposition_id")),
|
|
308
|
+
"final_conclusion": _text(plan.get("final_conclusion") or forward.get("final_conclusion")),
|
|
309
|
+
"assumptions": records("assumptions", "assumption_id", ("assumption_id", "statement", "status", "variable_references", "symbol_references")),
|
|
310
|
+
"definitions": records("definitions", "definition_id", ("definition_id", "symbol", "statement", "status", "variable_references", "symbol_references")),
|
|
311
|
+
"propositions": records("propositions", "proposition_id", ("proposition_id", "statement", "status", "variable_references", "symbol_references")),
|
|
312
|
+
"proof_obligations": records("proof_obligations", "obligation_id", ("obligation_id", "statement", "status", "proposition_id", "variable_references", "symbol_references")),
|
|
313
|
+
"forward_derivation": {
|
|
314
|
+
"status": _text(forward.get("status")),
|
|
315
|
+
"steps": [
|
|
316
|
+
{key: deepcopy(step[key]) for key in ("step_id", "premises", "status", "symbol_references", "variable_references", "rule_or_lemma", "derived_statement") if key in step}
|
|
317
|
+
for step in forward.get("steps") or []
|
|
318
|
+
if isinstance(step, Mapping) and _text(step.get("step_id"))
|
|
319
|
+
],
|
|
320
|
+
},
|
|
321
|
+
"unknown_items": deepcopy(plan.get("unknown_items") or []),
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def _counterexample_summary(analysis: Mapping[str, Any]) -> dict[str, Any]:
|
|
326
|
+
return {
|
|
327
|
+
"schema_version": _text(analysis.get("schema_version")),
|
|
328
|
+
"applicability": _text(analysis.get("applicability")),
|
|
329
|
+
"status": _text(analysis.get("status")),
|
|
330
|
+
"target_claim_id": _text(analysis.get("target_claim_id")),
|
|
331
|
+
"negated_conclusion": _text(analysis.get("negated_conclusion")),
|
|
332
|
+
"candidate_counterexamples": [
|
|
333
|
+
{
|
|
334
|
+
key: deepcopy(candidate[key])
|
|
335
|
+
for key in ("counterexample_id", "witness", "validity", "search_method", "assumption_checks", "conclusion_check")
|
|
336
|
+
if key in candidate
|
|
337
|
+
}
|
|
338
|
+
for candidate in analysis.get("candidate_counterexamples") or []
|
|
339
|
+
if isinstance(candidate, Mapping) and _text(candidate.get("counterexample_id"))
|
|
340
|
+
],
|
|
341
|
+
"limitations": deepcopy(analysis.get("limitations") or []),
|
|
342
|
+
"unknown_items": deepcopy(analysis.get("unknown_items") or []),
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def build_author_handoff(
|
|
347
|
+
payload: object,
|
|
348
|
+
*,
|
|
349
|
+
generated_at: datetime | None = None,
|
|
350
|
+
idea_result_path: str = "",
|
|
351
|
+
) -> dict[str, Any]:
|
|
352
|
+
"""Build a deterministic Research Plan Author input from a design package."""
|
|
353
|
+
|
|
354
|
+
design, run_payload = _validated_design(payload)
|
|
355
|
+
brief = _mapping(design.get("research_brief"))
|
|
356
|
+
source = _mapping(brief.get("source"))
|
|
357
|
+
intake = _mapping(run_payload.get("intake"))
|
|
358
|
+
template = _mapping(design.get("template_composition"))
|
|
359
|
+
validation_report = _mapping(design.get("validation_report"))
|
|
360
|
+
design_id = _text(design.get("design_id"))
|
|
361
|
+
observed_results = design.get("observed_results") or []
|
|
362
|
+
input_path = (
|
|
363
|
+
idea_result_path
|
|
364
|
+
or _text(intake.get("canonical_input_path"))
|
|
365
|
+
or _text(_mapping(source.get("upstream_source_paths")).get("idea_result"))
|
|
366
|
+
)
|
|
367
|
+
risk = _mapping(design.get("risk_and_human_review"))
|
|
368
|
+
return {
|
|
369
|
+
"schema_version": AUTHOR_HANDOFF_SCHEMA_VERSION,
|
|
370
|
+
"generated_at": _iso_datetime(generated_at),
|
|
371
|
+
"source_design_id": design_id,
|
|
372
|
+
"selected_direction": deepcopy(brief.get("selected_direction") or {}),
|
|
373
|
+
"research_design": deepcopy(design.get("research_design") or {}),
|
|
374
|
+
"hypothesis_mapping": deepcopy(design.get("hypothesis_mapping") or []),
|
|
375
|
+
"variables_and_operationalization": deepcopy(design.get("variables_and_operationalization") or {}),
|
|
376
|
+
"field_statuses": deepcopy(design.get("field_statuses") or {}),
|
|
377
|
+
"reasoning_context": deepcopy(_mapping(brief.get("reasoning_context"))),
|
|
378
|
+
"formal_reasoning": _formal_reasoning_summary(_mapping(design.get("formal_reasoning_plan"))),
|
|
379
|
+
"counterexample_analysis": _counterexample_summary(_mapping(design.get("counterexample_analysis"))),
|
|
380
|
+
"outcome_branches": deepcopy(design.get("outcome_branches") or []),
|
|
381
|
+
"unknown_items": _canonical_unknown_items(design),
|
|
382
|
+
"review_items": _canonical_review_items(design),
|
|
383
|
+
"source_registry": _compact_source_registry(_mapping(design.get("evidence_bundle"))),
|
|
384
|
+
"authoring_constraints": {
|
|
385
|
+
"proposal_without_observed_results": True,
|
|
386
|
+
"unverified_reasoning_must_be_labeled": True,
|
|
387
|
+
"unsupported_claims_forbidden": True,
|
|
388
|
+
"counterexample_must_satisfy_all_assumptions": True,
|
|
389
|
+
"formal_and_empirical_claims_must_remain_separate": True,
|
|
390
|
+
"observed_results_are_absent": len(observed_results) == 0,
|
|
391
|
+
},
|
|
392
|
+
"provenance": {
|
|
393
|
+
"idea_result_path": input_path,
|
|
394
|
+
"audit_source_paths": deepcopy(
|
|
395
|
+
intake.get("audit_source_paths")
|
|
396
|
+
or _mapping(source.get("upstream_source_paths"))
|
|
397
|
+
),
|
|
398
|
+
"selected_direction_id": _text(
|
|
399
|
+
intake.get("selected_direction_id")
|
|
400
|
+
or source.get("direction_id")
|
|
401
|
+
or _mapping(brief.get("selected_direction")).get("id")
|
|
402
|
+
),
|
|
403
|
+
"template_id": _text(template.get("template_id")),
|
|
404
|
+
"discipline_ids": deepcopy(brief.get("discipline_ids") or []),
|
|
405
|
+
"survey_binding": deepcopy(source.get("survey_binding") or {}),
|
|
406
|
+
"risk_level": _text(risk.get("risk_level")),
|
|
407
|
+
"validation_status": _text(
|
|
408
|
+
validation_report.get("status")
|
|
409
|
+
or _mapping(run_payload.get("validation")).get("status")
|
|
410
|
+
),
|
|
411
|
+
},
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
def _human_label(value: object) -> str:
|
|
416
|
+
return re.sub(r"\s+", " ", str(value).replace("_", " ").strip()).title()
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def _markdown_scalar(value: object) -> str:
|
|
420
|
+
if value is None or value == "":
|
|
421
|
+
return "not provided"
|
|
422
|
+
if isinstance(value, bool):
|
|
423
|
+
return "true" if value else "false"
|
|
424
|
+
if isinstance(value, (int, float)):
|
|
425
|
+
return str(value)
|
|
426
|
+
return str(value).replace("\r\n", "\n").replace("\r", "\n")
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
def _render_markdown_block(value: object, *, indent: int = 0) -> list[str]:
|
|
430
|
+
prefix = " " * indent
|
|
431
|
+
if isinstance(value, dict):
|
|
432
|
+
if not value:
|
|
433
|
+
return [f"{prefix}- not provided"]
|
|
434
|
+
lines: list[str] = []
|
|
435
|
+
for key in sorted(value):
|
|
436
|
+
item = value[key]
|
|
437
|
+
label = _human_label(key)
|
|
438
|
+
if isinstance(item, (dict, list, tuple)):
|
|
439
|
+
lines.append(f"{prefix}- **{label}:**")
|
|
440
|
+
lines.extend(_render_markdown_block(item, indent=indent + 1))
|
|
441
|
+
else:
|
|
442
|
+
lines.append(f"{prefix}- **{label}:** {_markdown_scalar(item)}")
|
|
443
|
+
return lines
|
|
444
|
+
if isinstance(value, (list, tuple)):
|
|
445
|
+
if not value:
|
|
446
|
+
return [f"{prefix}- not provided"]
|
|
447
|
+
lines = []
|
|
448
|
+
for item in value:
|
|
449
|
+
if isinstance(item, (dict, list, tuple)):
|
|
450
|
+
lines.append(f"{prefix}-")
|
|
451
|
+
lines.extend(_render_markdown_block(item, indent=indent + 1))
|
|
452
|
+
else:
|
|
453
|
+
lines.append(f"{prefix}- {_markdown_scalar(item)}")
|
|
454
|
+
return lines
|
|
455
|
+
return [f"{prefix}- {_markdown_scalar(value)}"]
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def render_markdown(
|
|
459
|
+
payload: object,
|
|
460
|
+
*,
|
|
461
|
+
generated_at: datetime | None = None,
|
|
462
|
+
timestamp: str | None = None,
|
|
463
|
+
) -> str:
|
|
464
|
+
"""Render a complete design package deterministically without an LLM."""
|
|
465
|
+
|
|
466
|
+
design, run_payload = _extract_design(payload)
|
|
467
|
+
brief = _mapping(design.get("research_brief"))
|
|
468
|
+
execution_policy = _mapping(design.get("execution_policy"))
|
|
469
|
+
observed_results = design.get("observed_results") or []
|
|
470
|
+
effective_timestamp = timestamp or generate_timestamp(generated_at)
|
|
471
|
+
effective_generated_at = _iso_datetime(generated_at)
|
|
472
|
+
title = _text(_mapping(brief.get("selected_direction")).get("title")) or _text(design.get("design_id"))
|
|
473
|
+
sections: list[tuple[str, object]] = [
|
|
474
|
+
(
|
|
475
|
+
"Run Metadata",
|
|
476
|
+
{
|
|
477
|
+
"artifact_timestamp": effective_timestamp,
|
|
478
|
+
"generated_at": effective_generated_at,
|
|
479
|
+
"run_status": _text(run_payload.get("status")) or "COMPLETED",
|
|
480
|
+
"design_id": design.get("design_id", ""),
|
|
481
|
+
"schema_version": design.get("schema_version", ""),
|
|
482
|
+
"execution_mode": execution_policy.get("mode", "DESIGN_ONLY"),
|
|
483
|
+
"observed_results": "none" if not observed_results else observed_results,
|
|
484
|
+
},
|
|
485
|
+
),
|
|
486
|
+
("Selected Idea Direction", brief.get("selected_direction", {})),
|
|
487
|
+
(
|
|
488
|
+
"Research Scope",
|
|
489
|
+
{
|
|
490
|
+
"discipline_ids": brief.get("discipline_ids", []),
|
|
491
|
+
"research_object": brief.get("research_object", {}),
|
|
492
|
+
"intervention_or_transformation": brief.get("intervention_or_transformation", ""),
|
|
493
|
+
"boundary_conditions": brief.get("boundary_conditions", []),
|
|
494
|
+
"alternative_explanations": brief.get("alternative_explanations", []),
|
|
495
|
+
"execution_policy": execution_policy,
|
|
496
|
+
},
|
|
497
|
+
),
|
|
498
|
+
("Research Design", design.get("research_design", {})),
|
|
499
|
+
("Hypothesis-to-Observable Mapping", design.get("hypothesis_mapping", [])),
|
|
500
|
+
("Variables and Operationalization", design.get("variables_and_operationalization", {})),
|
|
501
|
+
("Sampling and Eligibility", design.get("sampling_and_eligibility", {})),
|
|
502
|
+
("Measurement and Calibration", design.get("measurement_and_calibration", {})),
|
|
503
|
+
(
|
|
504
|
+
"Groups, Controls, Baselines, and Comparisons",
|
|
505
|
+
{
|
|
506
|
+
"groups": _mapping(design.get("comparison_and_robustness")).get("groups", []),
|
|
507
|
+
"controls": _mapping(design.get("comparison_and_robustness")).get("controls", []),
|
|
508
|
+
"baselines": _mapping(design.get("comparison_and_robustness")).get("baselines", []),
|
|
509
|
+
"comparisons": _mapping(design.get("comparison_and_robustness")).get("comparisons", []),
|
|
510
|
+
},
|
|
511
|
+
),
|
|
512
|
+
(
|
|
513
|
+
"Ablation, Sensitivity, and Robustness",
|
|
514
|
+
_mapping(design.get("comparison_and_robustness")).get("ablation_sensitivity_robustness", []),
|
|
515
|
+
),
|
|
516
|
+
(
|
|
517
|
+
"Randomization, Blinding, Repetition, and Batch Effects",
|
|
518
|
+
{
|
|
519
|
+
key: _mapping(design.get("analysis_plan")).get(key, {})
|
|
520
|
+
for key in ("randomization", "blinding", "repetitions", "batch_effects")
|
|
521
|
+
},
|
|
522
|
+
),
|
|
523
|
+
(
|
|
524
|
+
"Missing Data and Statistical Analysis",
|
|
525
|
+
{
|
|
526
|
+
"missing_data": _mapping(design.get("analysis_plan")).get("missing_data", {}),
|
|
527
|
+
"statistical_analysis": _mapping(design.get("analysis_plan")).get("statistical_analysis", {}),
|
|
528
|
+
},
|
|
529
|
+
),
|
|
530
|
+
("Data Governance and Reproducibility", design.get("data_governance_and_reproducibility", {})),
|
|
531
|
+
("Evidence Bundle and Coverage Ledger", design.get("evidence_bundle", {})),
|
|
532
|
+
("Formal Reasoning Plan", design.get("formal_reasoning_plan", {})),
|
|
533
|
+
("Counterexample Analysis", design.get("counterexample_analysis", {})),
|
|
534
|
+
("Expected Outcome Branches", design.get("outcome_branches", [])),
|
|
535
|
+
("Unknown Items", _canonical_unknown_items(design)),
|
|
536
|
+
("Human Review Requirements", design.get("risk_and_human_review", {})),
|
|
537
|
+
(
|
|
538
|
+
"Validation Report",
|
|
539
|
+
{
|
|
540
|
+
"design_validation": design.get("validation_report", {}),
|
|
541
|
+
"run_validation": run_payload.get("validation", {}),
|
|
542
|
+
},
|
|
543
|
+
),
|
|
544
|
+
]
|
|
545
|
+
lines = [
|
|
546
|
+
f"# Experiment Design: {title}",
|
|
547
|
+
"",
|
|
548
|
+
f"- Execution Mode: `{execution_policy.get('mode', 'DESIGN_ONLY')}`",
|
|
549
|
+
f"- Observed Results: {'none' if not observed_results else 'present in input'}",
|
|
550
|
+
f"- Evidence Status: `{_text(design.get('evidence_status')) or 'DESIGNED_NOT_EXECUTED'}`",
|
|
551
|
+
"",
|
|
552
|
+
]
|
|
553
|
+
for heading, content in sections:
|
|
554
|
+
lines.append(f"## {heading}")
|
|
555
|
+
lines.extend(_render_markdown_block(content))
|
|
556
|
+
lines.append("")
|
|
557
|
+
lines.extend(
|
|
558
|
+
[
|
|
559
|
+
"## Complete ExperimentDesign JSON",
|
|
560
|
+
"",
|
|
561
|
+
"~~~json",
|
|
562
|
+
json.dumps(design, ensure_ascii=False, indent=2, sort_keys=True, allow_nan=False),
|
|
563
|
+
"~~~",
|
|
564
|
+
"",
|
|
565
|
+
]
|
|
566
|
+
)
|
|
567
|
+
return "\n".join(lines)
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def _json_text(payload: object) -> str:
|
|
571
|
+
return json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True, allow_nan=False) + "\n"
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
def _write_temp_text(directory: Path, filename: str, content: str) -> Path:
|
|
575
|
+
try:
|
|
576
|
+
descriptor, raw_path = tempfile.mkstemp(
|
|
577
|
+
prefix=f".{filename}.",
|
|
578
|
+
suffix=".tmp",
|
|
579
|
+
dir=str(directory),
|
|
580
|
+
text=True,
|
|
581
|
+
)
|
|
582
|
+
temp_path = Path(raw_path)
|
|
583
|
+
with os.fdopen(descriptor, "w", encoding="utf-8", newline="\n") as handle:
|
|
584
|
+
handle.write(content)
|
|
585
|
+
handle.flush()
|
|
586
|
+
os.fsync(handle.fileno())
|
|
587
|
+
return temp_path
|
|
588
|
+
except (OSError, UnicodeError, ValueError) as exc:
|
|
589
|
+
try:
|
|
590
|
+
if "temp_path" in locals():
|
|
591
|
+
temp_path.unlink(missing_ok=True)
|
|
592
|
+
except OSError:
|
|
593
|
+
pass
|
|
594
|
+
raise ArtifactWriteError(f"Cannot create temporary artifact for '{filename}': {exc}") from exc
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
def _publish_without_overwrite(temp_path: Path, target_path: Path) -> None:
|
|
598
|
+
try:
|
|
599
|
+
os.link(temp_path, target_path)
|
|
600
|
+
temp_path.unlink(missing_ok=True)
|
|
601
|
+
return
|
|
602
|
+
except FileExistsError:
|
|
603
|
+
raise
|
|
604
|
+
except OSError as link_error:
|
|
605
|
+
try:
|
|
606
|
+
descriptor = os.open(
|
|
607
|
+
target_path,
|
|
608
|
+
os.O_WRONLY | os.O_CREAT | os.O_EXCL,
|
|
609
|
+
0o600,
|
|
610
|
+
)
|
|
611
|
+
except FileExistsError:
|
|
612
|
+
raise
|
|
613
|
+
except OSError:
|
|
614
|
+
raise link_error
|
|
615
|
+
else:
|
|
616
|
+
os.close(descriptor)
|
|
617
|
+
try:
|
|
618
|
+
os.replace(temp_path, target_path)
|
|
619
|
+
except OSError:
|
|
620
|
+
try:
|
|
621
|
+
target_path.unlink(missing_ok=True)
|
|
622
|
+
except OSError:
|
|
623
|
+
pass
|
|
624
|
+
raise
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
def _candidate_paths(output_dir: Path, timestamp: str, collision_index: int) -> ExperimentDesignArtifactPaths:
|
|
628
|
+
suffix = "" if collision_index == 0 else f"_{collision_index}"
|
|
629
|
+
stem = f"experiment_design_{timestamp}{suffix}"
|
|
630
|
+
return ExperimentDesignArtifactPaths(
|
|
631
|
+
timestamp=timestamp,
|
|
632
|
+
collision_index=collision_index,
|
|
633
|
+
experiment_design_json=output_dir / f"{stem}.json",
|
|
634
|
+
experiment_design_markdown=output_dir / f"{stem}.md",
|
|
635
|
+
author_json=output_dir / f"experiment_design_author_{timestamp}{suffix}.json",
|
|
636
|
+
)
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
class ExperimentDesignArtifactWriter:
|
|
640
|
+
"""Validate and atomically write the three design artifacts."""
|
|
641
|
+
|
|
642
|
+
def __init__(self, output_dir: str | Path) -> None:
|
|
643
|
+
self.output_dir = Path(output_dir).expanduser().resolve()
|
|
644
|
+
|
|
645
|
+
def write(
|
|
646
|
+
self,
|
|
647
|
+
payload: object,
|
|
648
|
+
*,
|
|
649
|
+
generated_at: datetime | None = None,
|
|
650
|
+
timestamp: str | None = None,
|
|
651
|
+
idea_result_path: str = "",
|
|
652
|
+
) -> ExperimentDesignArtifactPaths:
|
|
653
|
+
design, run_payload = _validated_design(payload)
|
|
654
|
+
effective_generated_at = generated_at or datetime.now().astimezone()
|
|
655
|
+
effective_timestamp = timestamp or generate_timestamp(effective_generated_at)
|
|
656
|
+
if not _TIMESTAMP_PATTERN.fullmatch(effective_timestamp):
|
|
657
|
+
raise ValueError(f"timestamp must match YYYYMMDD-HHMMSS-ffffff: {effective_timestamp}")
|
|
658
|
+
try:
|
|
659
|
+
self.output_dir.mkdir(parents=True, exist_ok=True)
|
|
660
|
+
except OSError as exc:
|
|
661
|
+
raise ArtifactWriteError(f"Cannot create artifact directory '{self.output_dir}': {exc}") from exc
|
|
662
|
+
|
|
663
|
+
author_handoff = build_author_handoff(
|
|
664
|
+
{"experiment_design": design, **run_payload},
|
|
665
|
+
generated_at=effective_generated_at,
|
|
666
|
+
idea_result_path=idea_result_path,
|
|
667
|
+
)
|
|
668
|
+
markdown = render_markdown(
|
|
669
|
+
{"experiment_design": design, **run_payload},
|
|
670
|
+
generated_at=effective_generated_at,
|
|
671
|
+
timestamp=effective_timestamp,
|
|
672
|
+
)
|
|
673
|
+
contents = {
|
|
674
|
+
"experiment_design_json": _json_text(design),
|
|
675
|
+
"experiment_design_markdown": markdown,
|
|
676
|
+
"author_json": _json_text(author_handoff),
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
for collision_index in range(1000):
|
|
680
|
+
paths = _candidate_paths(self.output_dir, effective_timestamp, collision_index)
|
|
681
|
+
targets = [
|
|
682
|
+
paths.experiment_design_json,
|
|
683
|
+
paths.experiment_design_markdown,
|
|
684
|
+
paths.author_json,
|
|
685
|
+
]
|
|
686
|
+
if any(target.exists() for target in targets):
|
|
687
|
+
continue
|
|
688
|
+
temporary_paths: dict[str, Path] = {}
|
|
689
|
+
published: list[Path] = []
|
|
690
|
+
try:
|
|
691
|
+
for key, target in zip(contents, targets):
|
|
692
|
+
temporary_paths[key] = _write_temp_text(self.output_dir, target.name, contents[key])
|
|
693
|
+
for key, target in zip(contents, targets):
|
|
694
|
+
_publish_without_overwrite(temporary_paths[key], target)
|
|
695
|
+
published.append(target)
|
|
696
|
+
return paths
|
|
697
|
+
except FileExistsError:
|
|
698
|
+
for target in published:
|
|
699
|
+
target.unlink(missing_ok=True)
|
|
700
|
+
continue
|
|
701
|
+
except (OSError, ArtifactWriteError) as exc:
|
|
702
|
+
for target in published:
|
|
703
|
+
target.unlink(missing_ok=True)
|
|
704
|
+
raise ArtifactWriteError(f"Cannot publish ExperimentDesign artifacts: {exc}") from exc
|
|
705
|
+
finally:
|
|
706
|
+
for temporary_path in temporary_paths.values():
|
|
707
|
+
temporary_path.unlink(missing_ok=True)
|
|
708
|
+
raise ArtifactWriteError(
|
|
709
|
+
f"Could not find an unused artifact filename for timestamp '{effective_timestamp}'"
|
|
710
|
+
)
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
def write_experiment_design_artifacts(
|
|
714
|
+
payload: object,
|
|
715
|
+
output_dir: str | Path,
|
|
716
|
+
*,
|
|
717
|
+
generated_at: datetime | None = None,
|
|
718
|
+
timestamp: str | None = None,
|
|
719
|
+
idea_result_path: str = "",
|
|
720
|
+
) -> ExperimentDesignArtifactPaths:
|
|
721
|
+
"""Convenience wrapper for writing validated ExperimentDesign artifacts."""
|
|
722
|
+
|
|
723
|
+
return ExperimentDesignArtifactWriter(output_dir).write(
|
|
724
|
+
payload,
|
|
725
|
+
generated_at=generated_at,
|
|
726
|
+
timestamp=timestamp,
|
|
727
|
+
idea_result_path=idea_result_path,
|
|
728
|
+
)
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
write_artifacts = write_experiment_design_artifacts
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
__all__ = [
|
|
735
|
+
"ARTIFACT_SCHEMA_VERSION",
|
|
736
|
+
"AUTHOR_HANDOFF_SCHEMA_VERSION",
|
|
737
|
+
"ArtifactError",
|
|
738
|
+
"ArtifactValidationError",
|
|
739
|
+
"ArtifactWriteError",
|
|
740
|
+
"ExperimentDesignArtifactPaths",
|
|
741
|
+
"ExperimentDesignArtifactWriter",
|
|
742
|
+
"build_author_handoff",
|
|
743
|
+
"generate_timestamp",
|
|
744
|
+
"render_markdown",
|
|
745
|
+
"write_artifacts",
|
|
746
|
+
"write_experiment_design_artifacts",
|
|
747
|
+
]
|