mednotes-opencode 0.1.0
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.
- package/.opencode/agents/med-chat-triager.md +204 -0
- package/.opencode/agents/med-flashcard-maker.md +63 -0
- package/.opencode/agents/med-knowledge-architect.md +230 -0
- package/.opencode/agents/med-link-graph-curator.md +177 -0
- package/.opencode/agents/med-publish-guard.md +62 -0
- package/.opencode/commands/flashcards.md +25 -0
- package/.opencode/commands/mednotes/create.md +25 -0
- package/.opencode/commands/mednotes/enrich.md +27 -0
- package/.opencode/commands/mednotes/fix-wiki.md +27 -0
- package/.opencode/commands/mednotes/history.md +22 -0
- package/.opencode/commands/mednotes/link-body.md +25 -0
- package/.opencode/commands/mednotes/link-related.md +27 -0
- package/.opencode/commands/mednotes/link.md +27 -0
- package/.opencode/commands/mednotes/pdf-library.md +27 -0
- package/.opencode/commands/mednotes/process-chats.md +23 -0
- package/.opencode/commands/mednotes/setup.md +21 -0
- package/.opencode/commands/mednotes/status.md +27 -0
- package/.opencode/commands/mednotes/telemetry.md +27 -0
- package/.opencode/commands/report.md +26 -0
- package/.opencode/mednotes/AGENTS.md +57 -0
- package/.opencode/mednotes/agents/med-chat-triager.md +197 -0
- package/.opencode/mednotes/agents/med-flashcard-maker.md +56 -0
- package/.opencode/mednotes/agents/med-knowledge-architect.md +224 -0
- package/.opencode/mednotes/agents/med-link-graph-curator.md +171 -0
- package/.opencode/mednotes/agents/med-publish-guard.md +55 -0
- package/.opencode/mednotes/contracts/.gitkeep +1 -0
- package/.opencode/mednotes/contracts/agents.json +116 -0
- package/.opencode/mednotes/contracts/opencode-plugin.json +70 -0
- package/.opencode/mednotes/docs/agent-prompt-hardening.md +567 -0
- package/.opencode/mednotes/docs/agent-role-contracts.md +94 -0
- package/.opencode/mednotes/docs/anki-mcp-twenty-rules.md +214 -0
- package/.opencode/mednotes/docs/anki-templates/README.md +39 -0
- package/.opencode/mednotes/docs/anki-templates/cloze.back.html +23 -0
- package/.opencode/mednotes/docs/anki-templates/cloze.front.html +14 -0
- package/.opencode/mednotes/docs/anki-templates/qa.back.html +24 -0
- package/.opencode/mednotes/docs/anki-templates/qa.front.html +14 -0
- package/.opencode/mednotes/docs/anki-templates/style.css +182 -0
- package/.opencode/mednotes/docs/atomicity-splitting-policy.md +113 -0
- package/.opencode/mednotes/docs/extension-docs.md +40 -0
- package/.opencode/mednotes/docs/flashcard-ingestion.md +278 -0
- package/.opencode/mednotes/docs/knowledge-architect.md +208 -0
- package/.opencode/mednotes/docs/merge-policy.md +110 -0
- package/.opencode/mednotes/docs/public-vocabulary.md +104 -0
- package/.opencode/mednotes/docs/semantic-linker.md +141 -0
- package/.opencode/mednotes/docs/taxonomy-policy.md +90 -0
- package/.opencode/mednotes/docs/triage-policy.md +187 -0
- package/.opencode/mednotes/docs/vault-version-control.md +758 -0
- package/.opencode/mednotes/docs/vocabulary-db-recovery.md +58 -0
- package/.opencode/mednotes/docs/workflow-output-contract.md +779 -0
- package/.opencode/mednotes/hooks/hooks.json +79 -0
- package/.opencode/mednotes/package-lock.json +6361 -0
- package/.opencode/mednotes/package.json +15 -0
- package/.opencode/mednotes/pyproject.toml +48 -0
- package/.opencode/mednotes/scripts/bootstrap_windows_python_uv.cmd +13 -0
- package/.opencode/mednotes/scripts/bootstrap_windows_python_uv.ps1 +172 -0
- package/.opencode/mednotes/scripts/enrich_notes.py +23 -0
- package/.opencode/mednotes/scripts/full_reset_windows_python_uv.cmd +13 -0
- package/.opencode/mednotes/scripts/hooks/antigravity_hook_status.mjs +212 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/antigravity.mjs +169 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/harness_payload.mjs +103 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/opencode_plugin.mjs +341 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/opencode_user_config_sync.mjs +177 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/anki_preflight.mjs +214 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/cli.mjs +143 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/diagnostics.mjs +11 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/domain/agent_directive_core.mjs +160 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/fsm_directive.mjs +1470 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/hook_errors.mjs +120 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/retention.mjs +114 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/runtime.mjs +174 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/telemetry_capture.mjs +511 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/vault_guard.mjs +624 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook.mjs +5 -0
- package/.opencode/mednotes/scripts/mednotes/_runtime_paths.py +24 -0
- package/.opencode/mednotes/scripts/mednotes/anki_model_validator.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/capture_extension_diff.py +1562 -0
- package/.opencode/mednotes/scripts/mednotes/feedback_report.py +16 -0
- package/.opencode/mednotes/scripts/mednotes/flashcard_index.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/flashcard_pipeline.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/flashcard_report.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/flashcard_sources.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/obsidian/README.md +6 -0
- package/.opencode/mednotes/scripts/mednotes/obsidian_note_utils.py +20 -0
- package/.opencode/mednotes/scripts/mednotes/pdf_library/cli.py +16 -0
- package/.opencode/mednotes/scripts/mednotes/project_fsm.py +229 -0
- package/.opencode/mednotes/scripts/mednotes/setup_telemetry_email.py +404 -0
- package/.opencode/mednotes/scripts/mednotes/sync_anki_twenty_rules.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/sync_opencode_user_config.py +36 -0
- package/.opencode/mednotes/scripts/mednotes/wiki/cli.py +20 -0
- package/.opencode/mednotes/scripts/mednotes/wiki_graph.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/wiki_tree.py +134 -0
- package/.opencode/mednotes/scripts/reset_windows_python_uv.ps1 +625 -0
- package/.opencode/mednotes/scripts/run_python.mjs +109 -0
- package/.opencode/mednotes/scripts/vault/vault_commit.ps1 +19 -0
- package/.opencode/mednotes/scripts/vault/vault_commit.sh +18 -0
- package/.opencode/mednotes/scripts/vault/vault_git.ps1 +19 -0
- package/.opencode/mednotes/scripts/vault/vault_git.py +3107 -0
- package/.opencode/mednotes/scripts/vault/vault_git.sh +18 -0
- package/.opencode/mednotes/scripts/vault/vault_precommit.ps1 +19 -0
- package/.opencode/mednotes/scripts/vault/vault_precommit.sh +18 -0
- package/.opencode/mednotes/skills/THIRD_PARTY_NOTICES.md +45 -0
- package/.opencode/mednotes/skills/create-medical-flashcards/SKILL.md +113 -0
- package/.opencode/mednotes/skills/create-medical-note/SKILL.md +90 -0
- package/.opencode/mednotes/skills/enrich-medical-note/SKILL.md +120 -0
- package/.opencode/mednotes/skills/fix-medical-wiki/SKILL.md +559 -0
- package/.opencode/mednotes/skills/link-medical-wiki/SKILL.md +224 -0
- package/.opencode/mednotes/skills/obsidian-cli/SKILL.md +118 -0
- package/.opencode/mednotes/skills/obsidian-markdown/SKILL.md +207 -0
- package/.opencode/mednotes/skills/obsidian-markdown/references/CALLOUTS.md +58 -0
- package/.opencode/mednotes/skills/obsidian-markdown/references/EMBEDS.md +63 -0
- package/.opencode/mednotes/skills/obsidian-markdown/references/PROPERTIES.md +61 -0
- package/.opencode/mednotes/skills/obsidian-ops/SKILL.md +136 -0
- package/.opencode/mednotes/skills/pdf-library/SKILL.md +45 -0
- package/.opencode/mednotes/skills/process-medical-chats/SKILL.md +246 -0
- package/.opencode/mednotes/skills/workflow-report/SKILL.md +100 -0
- package/.opencode/mednotes/src/mednotes/__init__.py +5 -0
- package/.opencode/mednotes/src/mednotes/domains/__init__.py +5 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/README.md +26 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/__init__.py +2 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/build_demo_apkg.py +177 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/contracts.py +385 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/flashcards_machine.py +522 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/fsm.py +817 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/index.py +630 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/install_models.py +445 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/model.py +359 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/obsidian_links.py +135 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/obsidian_note_utils.py +546 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/pipeline.py +580 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/report.py +510 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/sources.py +682 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/sync_rules.py +184 -0
- package/.opencode/mednotes/src/mednotes/domains/history/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/history/history_fsm.py +852 -0
- package/.opencode/mednotes/src/mednotes/domains/history/history_machine.py +453 -0
- package/.opencode/mednotes/src/mednotes/domains/setup/__init__.py +7 -0
- package/.opencode/mednotes/src/mednotes/domains/setup/setup_fsm.py +808 -0
- package/.opencode/mednotes/src/mednotes/domains/setup/setup_machine.py +973 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/README.md +64 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/api.py +668 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/batch_state.py +102 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/atomicity/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/atomicity/atomicity.py +877 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/body_link/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/body_link/body_linker.py +1562 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/effects/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/effects/effect_adapters.py +949 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/effects/fix_wiki_runtime_adapters.py +433 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/coverage.py +413 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/graph.py +396 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/graph_fixes.py +161 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/hygiene/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/hygiene/hygiene.py +483 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/__init__.py +2 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/anchors.py +185 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/__init__.py +0 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/cache.py +223 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/config.py +131 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/download.py +224 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/frontmatter.py +59 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/insert.py +227 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/local_import.py +54 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/__init__.py +42 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/web_profiles.py +99 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/web_search.py +203 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/wikimedia.py +102 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/markdown_db_adapter.mjs +434 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/markdown_node_runtime.py +274 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/markdown_query.py +227 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/artifacts.py +605 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/canonical_merge.py +277 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/markdown_zones.py +85 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/meaning_planner.py +307 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_iter.py +67 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_merge.py +278 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_plan.py +409 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_policy.py +22 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/__init__.py +79 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/fixes.py +264 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/frontmatter.py +435 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/models.py +208 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/prompts.py +37 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/tables.py +236 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/validate.py +404 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/provenance.py +478 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/raw_chats.py +273 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/sources_backfill.py +235 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/__init__.py +10 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/anchors.py +16 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/captions.py +47 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/cli.py +179 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/cloud.py +52 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/config.py +196 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/context_packets.py +76 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/db.py +81 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/doctor.py +102 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/figure_ids.py +42 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/ingest.py +326 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/insert.py +316 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/mentions.py +57 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/ocr.py +71 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/paths.py +35 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/pdf_engine.py +77 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/schema.py +155 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/search.py +188 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/app.py +89 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/image_backend.py +29 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/state.py +65 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/publish.py +1139 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/publish_receipts.py +365 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/publish_recovery.py +240 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/agent_behavior_corpus.py +2069 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/agent_report_validation.py +4448 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/agent_run_audit.py +852 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/architect_prompt_eval.py +341 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/body_linker_eval.py +240 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/curator_output_validation.py +175 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/curator_prompt_eval.py +865 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/triager_prompt_eval.py +1295 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/related_notes/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/related_notes/related_notes.py +1920 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/related_notes/related_notes_headless.py +1186 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/plan_attestation.py +148 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/specialist_receipts.py +360 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/specialist_runtime.py +52 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/specialist_task_runner.py +2470 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/style/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/style/style.py +1952 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/subagents/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/subagents/agents.py +1767 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/alias_projection.py +331 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/link_terms.py +151 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/llm_disambiguation.py +182 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/__init__.py +116 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/audit.py +201 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/migration.py +314 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/normalize.py +72 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/policy.py +135 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/resolve.py +413 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/schema.py +157 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/status.py +137 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_bootstrap.py +509 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_curator_batch.py +1115 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_ingestion.py +632 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_map.py +930 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_recovery.py +1388 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/cli.py +6665 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/common.py +69 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/config.py +210 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/__init__.py +74 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/agent_report.py +242 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/agent_run_audit.py +196 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/agents.py +601 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/curator.py +256 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/effect_payloads.py +519 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/happy_path.py +190 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/link_git.py +110 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/link_runtime_artifact.py +52 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/note_plan.py +75 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/paths.py +114 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/public_report.py +53 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/publish.py +111 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/raw_coverage.py +217 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/related_notes.py +136 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/related_notes_headless.py +153 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/related_notes_runtime.py +395 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/schema_registry.py +637 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/specialist.py +432 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/status.py +62 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/style_rewrite.py +568 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/vocabulary_ingestion.py +223 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_blockers.py +510 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_guardrails.py +637 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_outcomes.py +121 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_receipts.py +100 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/__main__.py +4 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/cli.py +275 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/__init__.py +2 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/candidates.py +193 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/cli.py +189 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/gemini.py +220 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/inputs.py +120 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/models.py +34 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/parsing.py +48 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/prompts.py +216 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/quality.py +54 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/reporting.py +24 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/runner.py +433 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/utils.py +39 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/vault_guard_bridge.py +17 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_context_packets.py +454 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_decision_projection.py +133 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_effects.py +1260 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_fsm.py +2768 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_machine.py +1588 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_plan.py +306 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_primary_objective.py +316 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_problem.py +153 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_receipt_evidence.py +306 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_states.py +290 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_user_report.py +342 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/health.py +6332 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_fsm.py +1119 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_git.py +638 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_machine.py +1106 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_retry_governance.py +374 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_runtime_result.py +485 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_triggers.py +183 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/linking.py +2758 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/reference_repair.py +718 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/related_notes_fsm.py +1855 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link_related/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link_related/link_related_machine.py +834 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_fsm.py +1592 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_machine.py +3097 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_primary_objective.py +28 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_runtime_result.py +185 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/performance.py +97 -0
- package/.opencode/mednotes/src/mednotes/kernel/__init__.py +6 -0
- package/.opencode/mednotes/src/mednotes/kernel/agent_directive.py +336 -0
- package/.opencode/mednotes/src/mednotes/kernel/base.py +51 -0
- package/.opencode/mednotes/src/mednotes/kernel/blockers.py +39 -0
- package/.opencode/mednotes/src/mednotes/kernel/effect_executor.py +55 -0
- package/.opencode/mednotes/src/mednotes/kernel/effect_intent.py +69 -0
- package/.opencode/mednotes/src/mednotes/kernel/effects.py +160 -0
- package/.opencode/mednotes/src/mednotes/kernel/errors.py +38 -0
- package/.opencode/mednotes/src/mednotes/kernel/fsm_event.py +35 -0
- package/.opencode/mednotes/src/mednotes/kernel/fsm_model.py +55 -0
- package/.opencode/mednotes/src/mednotes/kernel/fsm_transition_result.py +75 -0
- package/.opencode/mednotes/src/mednotes/kernel/guardrails.py +188 -0
- package/.opencode/mednotes/src/mednotes/kernel/progress.py +319 -0
- package/.opencode/mednotes/src/mednotes/kernel/public_report.py +346 -0
- package/.opencode/mednotes/src/mednotes/kernel/state_machine.py +164 -0
- package/.opencode/mednotes/src/mednotes/kernel/workflow.py +619 -0
- package/.opencode/mednotes/src/mednotes/platform/__init__.py +5 -0
- package/.opencode/mednotes/src/mednotes/platform/backup_policy.py +382 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/__init__.py +62 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/cli.py +275 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/contracts.py +83 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/core.py +4168 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/integrity.py +989 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/operational_contract.py +2293 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/telemetry.py +875 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/telemetry_config.py +65 -0
- package/.opencode/mednotes/src/mednotes/platform/opencode_runtime_config.py +182 -0
- package/.opencode/mednotes/src/mednotes/platform/paths/__init__.py +1560 -0
- package/.opencode/mednotes/src/mednotes/platform/secrets.py +89 -0
- package/.opencode/mednotes/src/mednotes/platform/user_config.py +103 -0
- package/.opencode/mednotes/src/mednotes/platform/vault_guard.py +214 -0
- package/.opencode/mednotes/uv.lock +932 -0
- package/.opencode/mednotes.generated.json +395 -0
- package/.opencode/opencode.json +31 -0
- package/.opencode/plugins/mednotes-fsm.mjs +7 -0
- package/.opencode/plugins/mednotes_hook/adapters/antigravity.mjs +169 -0
- package/.opencode/plugins/mednotes_hook/adapters/harness_payload.mjs +103 -0
- package/.opencode/plugins/mednotes_hook/adapters/opencode_plugin.mjs +341 -0
- package/.opencode/plugins/mednotes_hook/adapters/opencode_user_config_sync.mjs +177 -0
- package/.opencode/plugins/mednotes_hook/anki_preflight.mjs +214 -0
- package/.opencode/plugins/mednotes_hook/cli.mjs +143 -0
- package/.opencode/plugins/mednotes_hook/diagnostics.mjs +11 -0
- package/.opencode/plugins/mednotes_hook/domain/agent_directive_core.mjs +160 -0
- package/.opencode/plugins/mednotes_hook/fsm_directive.mjs +1470 -0
- package/.opencode/plugins/mednotes_hook/hook_errors.mjs +120 -0
- package/.opencode/plugins/mednotes_hook/retention.mjs +114 -0
- package/.opencode/plugins/mednotes_hook/runtime.mjs +174 -0
- package/.opencode/plugins/mednotes_hook/telemetry_capture.mjs +511 -0
- package/.opencode/plugins/mednotes_hook/vault_guard.mjs +624 -0
- package/AGENTS.md +57 -0
- package/README.md +194 -0
- package/adapters/antigravity/agents.json +80 -0
- package/adapters/antigravity/templates/med-chat-triager.md +214 -0
- package/adapters/antigravity/templates/med-flashcard-maker.md +72 -0
- package/adapters/antigravity/templates/med-knowledge-architect.md +241 -0
- package/adapters/antigravity/templates/med-link-graph-curator.md +187 -0
- package/adapters/antigravity/templates/med-publish-guard.md +71 -0
- package/adapters/gemini-cli/gemini-extension.json +14 -0
- package/adapters/gemini-cli/package.json +15 -0
- package/adapters/gemini-cli/pyproject.toml +48 -0
- package/bin/mednotes-opencode.mjs +155 -0
- package/contracts/agents.json +116 -0
- package/core/agents/med-chat-triager.md +197 -0
- package/core/agents/med-flashcard-maker.md +56 -0
- package/core/agents/med-knowledge-architect.md +224 -0
- package/core/agents/med-link-graph-curator.md +171 -0
- package/core/agents/med-publish-guard.md +55 -0
- package/core/commands/flashcards.toml +22 -0
- package/core/commands/mednotes/create.toml +22 -0
- package/core/commands/mednotes/enrich.toml +24 -0
- package/core/commands/mednotes/fix-wiki.toml +24 -0
- package/core/commands/mednotes/history.toml +19 -0
- package/core/commands/mednotes/link-body.toml +22 -0
- package/core/commands/mednotes/link-related.toml +24 -0
- package/core/commands/mednotes/link.toml +24 -0
- package/core/commands/mednotes/pdf-library.toml +24 -0
- package/core/commands/mednotes/process-chats.toml +20 -0
- package/core/commands/mednotes/setup.toml +18 -0
- package/core/commands/mednotes/status.toml +24 -0
- package/core/commands/mednotes/telemetry.toml +24 -0
- package/core/commands/report.toml +23 -0
- package/core/skills/THIRD_PARTY_NOTICES.md +45 -0
- package/core/skills/create-medical-flashcards/SKILL.md +113 -0
- package/core/skills/create-medical-note/SKILL.md +90 -0
- package/core/skills/enrich-medical-note/SKILL.md +120 -0
- package/core/skills/fix-medical-wiki/SKILL.md +559 -0
- package/core/skills/link-medical-wiki/SKILL.md +224 -0
- package/core/skills/obsidian-cli/SKILL.md +118 -0
- package/core/skills/obsidian-markdown/SKILL.md +207 -0
- package/core/skills/obsidian-markdown/references/CALLOUTS.md +58 -0
- package/core/skills/obsidian-markdown/references/EMBEDS.md +63 -0
- package/core/skills/obsidian-markdown/references/PROPERTIES.md +61 -0
- package/core/skills/obsidian-ops/SKILL.md +136 -0
- package/core/skills/pdf-library/SKILL.md +45 -0
- package/core/skills/process-medical-chats/SKILL.md +246 -0
- package/core/skills/workflow-report/SKILL.md +100 -0
- package/package.json +45 -0
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
"""Vault hygiene checks and cleanup for ``fix-wiki``."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import hashlib
|
|
5
|
+
import os
|
|
6
|
+
import shutil
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from pydantic import ConfigDict, Field
|
|
12
|
+
|
|
13
|
+
from mednotes.domains.wiki.capabilities.notes.note_iter import is_note_markdown, iter_notes
|
|
14
|
+
from mednotes.domains.wiki.capabilities.vocabulary.link_terms import is_index_note as _is_index_note
|
|
15
|
+
from mednotes.domains.wiki.capabilities.vocabulary.link_terms import is_index_target, normalize_key
|
|
16
|
+
from mednotes.kernel.base import ContractModel, JsonObject
|
|
17
|
+
from mednotes.platform.backup_policy import (
|
|
18
|
+
LEGACY_BACKUP_CLEANUP_SCHEMA,
|
|
19
|
+
archive_legacy_backups,
|
|
20
|
+
collect_legacy_backup_candidates,
|
|
21
|
+
is_canonical_backup_path,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
WIKI_HYGIENE_SCHEMA = "medical-notes-workbench.wiki-hygiene.v1"
|
|
25
|
+
WIKI_HYGIENE_CLEANUP_SCHEMA = "medical-notes-workbench.wiki-hygiene-cleanup.v1"
|
|
26
|
+
ROOT_HYGIENE_AUDIT_SCHEMA = "medical-notes-workbench.root-hygiene-audit.v1"
|
|
27
|
+
IGNORED_EMPTY_DIR_NAMES = {"attachments", "_Mock_Embeds"}
|
|
28
|
+
ROOT_HYGIENE_SUFFIXES = {".py", ".json", ".md", ".bak", ".log", ".ps1", ".cmd", ".sh"}
|
|
29
|
+
ROOT_HYGIENE_AGENT_MARKERS = (
|
|
30
|
+
"medical-notes-workbench",
|
|
31
|
+
"mednotes",
|
|
32
|
+
"Wiki_Medicina",
|
|
33
|
+
"related-notes",
|
|
34
|
+
"super_linker",
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True)
|
|
39
|
+
class _HygieneNoteSnapshot:
|
|
40
|
+
path: Path
|
|
41
|
+
content: str
|
|
42
|
+
sha256: str
|
|
43
|
+
is_index_note: bool
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class _BackupCleanupSummaryFields(ContractModel):
|
|
47
|
+
"""Typed summary consumed from retired-backup hygiene reports."""
|
|
48
|
+
|
|
49
|
+
model_config = ConfigDict(extra="ignore", populate_by_name=True, validate_assignment=True)
|
|
50
|
+
|
|
51
|
+
candidate_count: int = Field(default=0, ge=0, strict=True)
|
|
52
|
+
ambiguous_count: int = Field(default=0, ge=0, strict=True)
|
|
53
|
+
archived_count: int = Field(default=0, ge=0, strict=True)
|
|
54
|
+
archived: list[JsonObject] = Field(default_factory=list)
|
|
55
|
+
errors: list[JsonObject] = Field(default_factory=list)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def collect_wiki_hygiene(wiki_dir: Path, *, sample_limit: int = 20) -> dict[str, Any]:
|
|
59
|
+
note_snapshots = _note_snapshots(wiki_dir)
|
|
60
|
+
backup_files = _backup_or_temp_files(wiki_dir, kind="backup")
|
|
61
|
+
rewrite_files = _backup_or_temp_files(wiki_dir, kind="rewrite")
|
|
62
|
+
legacy_backups: JsonObject = (
|
|
63
|
+
collect_legacy_backup_candidates(wiki_dir, sample_limit=sample_limit)
|
|
64
|
+
if wiki_dir.exists()
|
|
65
|
+
else {
|
|
66
|
+
"schema": LEGACY_BACKUP_CLEANUP_SCHEMA,
|
|
67
|
+
"root": str(wiki_dir),
|
|
68
|
+
"candidate_count": 0,
|
|
69
|
+
"ambiguous_count": 0,
|
|
70
|
+
"high_confidence_count": 0,
|
|
71
|
+
"high_confidence": [],
|
|
72
|
+
"ambiguous": [],
|
|
73
|
+
}
|
|
74
|
+
)
|
|
75
|
+
legacy_fields = _BackupCleanupSummaryFields.model_validate(legacy_backups)
|
|
76
|
+
empty_dirs = _empty_dirs(wiki_dir)
|
|
77
|
+
duplicate_hash_groups = _duplicate_hash_groups_from_snapshots(wiki_dir, note_snapshots)
|
|
78
|
+
duplicate_filename_groups = _duplicate_filename_groups_from_snapshots(wiki_dir, note_snapshots)
|
|
79
|
+
depth_issues = _note_depth_issues_from_snapshots(wiki_dir, note_snapshots)
|
|
80
|
+
loose_root_notes = _loose_root_note_snapshots(wiki_dir, note_snapshots)
|
|
81
|
+
empty_root_notes = [snapshot.path for snapshot in loose_root_notes if snapshot.content.strip() == ""]
|
|
82
|
+
legacy_count = legacy_fields.candidate_count
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
"schema": WIKI_HYGIENE_SCHEMA,
|
|
86
|
+
"wiki_dir": str(wiki_dir),
|
|
87
|
+
"bak_or_rewrite": len(backup_files) + len(rewrite_files) + legacy_count,
|
|
88
|
+
"backup_files": _rel_sample(wiki_dir, backup_files, sample_limit),
|
|
89
|
+
"rewrite_files": _rel_sample(wiki_dir, rewrite_files, sample_limit),
|
|
90
|
+
"backup_file_count": len(backup_files),
|
|
91
|
+
"rewrite_file_count": len(rewrite_files),
|
|
92
|
+
"legacy_backup_cleanup": legacy_backups,
|
|
93
|
+
"legacy_backup_candidate_count": legacy_count,
|
|
94
|
+
"legacy_backup_ambiguous_count": legacy_fields.ambiguous_count,
|
|
95
|
+
"empty_dirs": len(empty_dirs),
|
|
96
|
+
"empty_dir_paths": _rel_sample(wiki_dir, empty_dirs, sample_limit),
|
|
97
|
+
"duplicate_hash_groups": len(duplicate_hash_groups),
|
|
98
|
+
"duplicate_hash_samples": duplicate_hash_groups[:sample_limit],
|
|
99
|
+
"duplicate_filename_groups": len(duplicate_filename_groups),
|
|
100
|
+
"duplicate_filename_samples": duplicate_filename_groups[:sample_limit],
|
|
101
|
+
"note_depth_issues": len(depth_issues),
|
|
102
|
+
"note_depth_samples": depth_issues[:sample_limit],
|
|
103
|
+
"loose_root_note_count": len(loose_root_notes),
|
|
104
|
+
"loose_root_note_samples": _rel_sample(wiki_dir, [snapshot.path for snapshot in loose_root_notes], sample_limit),
|
|
105
|
+
"empty_root_note_count": len(empty_root_notes),
|
|
106
|
+
"empty_root_note_samples": _rel_sample(wiki_dir, empty_root_notes, sample_limit),
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def cleanup_wiki_hygiene(
|
|
111
|
+
wiki_dir: Path,
|
|
112
|
+
*,
|
|
113
|
+
archive_root: Path,
|
|
114
|
+
archive_backups: bool = True,
|
|
115
|
+
remove_rewrites: bool = True,
|
|
116
|
+
remove_empty_dirs: bool = True,
|
|
117
|
+
remove_empty_root_notes: bool = True,
|
|
118
|
+
sample_limit: int = 20,
|
|
119
|
+
) -> dict[str, Any]:
|
|
120
|
+
archive_root.mkdir(parents=True, exist_ok=True)
|
|
121
|
+
archived: list[dict[str, str]] = []
|
|
122
|
+
archived_total_count = 0
|
|
123
|
+
removed_rewrites: list[str] = []
|
|
124
|
+
removed_empty_dirs: list[str] = []
|
|
125
|
+
removed_empty_dir_entries: list[dict[str, str]] = []
|
|
126
|
+
removed_empty_root_notes: list[str] = []
|
|
127
|
+
removed_empty_root_note_entries: list[dict[str, str]] = []
|
|
128
|
+
errors: list[dict[str, str]] = []
|
|
129
|
+
legacy_cleanup: JsonObject | None = None
|
|
130
|
+
|
|
131
|
+
if remove_empty_root_notes:
|
|
132
|
+
for path in _empty_root_note_files(wiki_dir):
|
|
133
|
+
try:
|
|
134
|
+
archived.append(_archive_file(wiki_dir, path, archive_root))
|
|
135
|
+
archived_total_count += 1
|
|
136
|
+
rel = path.relative_to(wiki_dir).as_posix()
|
|
137
|
+
removed_empty_root_notes.append(rel)
|
|
138
|
+
removed_empty_root_note_entries.append(
|
|
139
|
+
{
|
|
140
|
+
"path": rel,
|
|
141
|
+
"action": "archive_empty_root_note",
|
|
142
|
+
"problem_code": "structure.empty_root_note.present",
|
|
143
|
+
"phase": "structure_empty_root_note_cleanup",
|
|
144
|
+
}
|
|
145
|
+
)
|
|
146
|
+
except OSError as exc:
|
|
147
|
+
errors.append({"path": str(path), "operation": "archive_empty_root_note", "error": str(exc)})
|
|
148
|
+
|
|
149
|
+
if archive_backups:
|
|
150
|
+
for path in _backup_or_temp_files(wiki_dir, kind="backup"):
|
|
151
|
+
try:
|
|
152
|
+
archived.append(_archive_file(wiki_dir, path, archive_root))
|
|
153
|
+
archived_total_count += 1
|
|
154
|
+
except OSError as exc:
|
|
155
|
+
errors.append({"path": str(path), "operation": "archive_backup", "error": str(exc)})
|
|
156
|
+
try:
|
|
157
|
+
legacy_cleanup = archive_legacy_backups(
|
|
158
|
+
wiki_dir,
|
|
159
|
+
archive_root=archive_root,
|
|
160
|
+
apply=True,
|
|
161
|
+
sample_limit=sample_limit,
|
|
162
|
+
)
|
|
163
|
+
legacy_cleanup_fields = _BackupCleanupSummaryFields.model_validate(legacy_cleanup)
|
|
164
|
+
archived.extend(legacy_cleanup_fields.archived)
|
|
165
|
+
archived_total_count += legacy_cleanup_fields.archived_count
|
|
166
|
+
errors.extend(legacy_cleanup_fields.errors)
|
|
167
|
+
except OSError as exc:
|
|
168
|
+
errors.append({"path": str(wiki_dir), "operation": "archive_legacy_backups", "error": str(exc)})
|
|
169
|
+
|
|
170
|
+
if remove_rewrites:
|
|
171
|
+
for path in _backup_or_temp_files(wiki_dir, kind="rewrite"):
|
|
172
|
+
try:
|
|
173
|
+
archived.append(_archive_file(wiki_dir, path, archive_root))
|
|
174
|
+
archived_total_count += 1
|
|
175
|
+
removed_rewrites.append(path.relative_to(wiki_dir).as_posix())
|
|
176
|
+
except OSError as exc:
|
|
177
|
+
errors.append({"path": str(path), "operation": "archive_rewrite", "error": str(exc)})
|
|
178
|
+
|
|
179
|
+
if remove_empty_dirs:
|
|
180
|
+
for path in sorted(_empty_dirs(wiki_dir), key=lambda item: len(item.relative_to(wiki_dir).parts), reverse=True):
|
|
181
|
+
try:
|
|
182
|
+
path.rmdir()
|
|
183
|
+
rel = path.relative_to(wiki_dir).as_posix()
|
|
184
|
+
removed_empty_dirs.append(rel)
|
|
185
|
+
removed_empty_dir_entries.append(
|
|
186
|
+
{
|
|
187
|
+
"path": rel,
|
|
188
|
+
"action": "remove_empty_dir",
|
|
189
|
+
"problem_code": "structure.empty_dir.present",
|
|
190
|
+
"phase": "structure_empty_dir_cleanup",
|
|
191
|
+
}
|
|
192
|
+
)
|
|
193
|
+
except OSError as exc:
|
|
194
|
+
errors.append({"path": str(path), "operation": "remove_empty_dir", "error": str(exc)})
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
"schema": WIKI_HYGIENE_CLEANUP_SCHEMA,
|
|
198
|
+
"wiki_dir": str(wiki_dir),
|
|
199
|
+
"archive_root": str(archive_root),
|
|
200
|
+
"archived_count": archived_total_count,
|
|
201
|
+
"archived": archived[:sample_limit],
|
|
202
|
+
"legacy_backup_cleanup": legacy_cleanup,
|
|
203
|
+
"legacy_backup_ambiguous_count": (
|
|
204
|
+
_BackupCleanupSummaryFields.model_validate(legacy_cleanup).ambiguous_count if legacy_cleanup else 0
|
|
205
|
+
),
|
|
206
|
+
"removed_rewrite_count": len(removed_rewrites),
|
|
207
|
+
"removed_rewrites": removed_rewrites[:sample_limit],
|
|
208
|
+
"removed_empty_dir_count": len(removed_empty_dirs),
|
|
209
|
+
"removed_empty_dirs": removed_empty_dirs[:sample_limit],
|
|
210
|
+
"removed_empty_dir_entries": removed_empty_dir_entries[:sample_limit],
|
|
211
|
+
"removed_empty_root_note_count": len(removed_empty_root_notes),
|
|
212
|
+
"removed_empty_root_notes": removed_empty_root_notes[:sample_limit],
|
|
213
|
+
"removed_empty_root_note_entries": removed_empty_root_note_entries[:sample_limit],
|
|
214
|
+
"error_count": len(errors),
|
|
215
|
+
"errors": errors[:sample_limit],
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def audit_user_root_hygiene(user_root: Path | None = None, *, sample_limit: int = 50) -> dict[str, Any]:
|
|
220
|
+
"""Audit suspicious loose files in a user root without deleting anything."""
|
|
221
|
+
root = (user_root or Path.home()).expanduser()
|
|
222
|
+
candidates: list[dict[str, Any]] = []
|
|
223
|
+
if root.exists() and root.is_dir():
|
|
224
|
+
for path in sorted(root.iterdir(), key=lambda item: item.name.lower()):
|
|
225
|
+
if not path.is_file() or path.suffix.lower() not in ROOT_HYGIENE_SUFFIXES:
|
|
226
|
+
continue
|
|
227
|
+
candidates.append(_root_hygiene_entry(root, path))
|
|
228
|
+
unexpected = [item for item in candidates if item.get("signal") == "agent.unexpected_mutation"]
|
|
229
|
+
return {
|
|
230
|
+
"schema": ROOT_HYGIENE_AUDIT_SCHEMA,
|
|
231
|
+
"phase": "environment/root_hygiene_audit",
|
|
232
|
+
"status": "completed_with_warnings" if candidates else "completed",
|
|
233
|
+
"blocked_reason": "",
|
|
234
|
+
"next_action": (
|
|
235
|
+
"Revisar candidates[] e remover manualmente apenas arquivos confirmados como artefatos soltos."
|
|
236
|
+
if candidates
|
|
237
|
+
else ""
|
|
238
|
+
),
|
|
239
|
+
"required_inputs": ["user_root"],
|
|
240
|
+
"human_decision_required": False,
|
|
241
|
+
"user_root": str(root),
|
|
242
|
+
"candidate_count": len(candidates),
|
|
243
|
+
"unexpected_mutation_count": len(unexpected),
|
|
244
|
+
"candidates": candidates[:sample_limit],
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _root_hygiene_entry(root: Path, path: Path) -> dict[str, Any]:
|
|
249
|
+
stat = path.stat()
|
|
250
|
+
digest = hashlib.sha256(path.read_bytes()).hexdigest()
|
|
251
|
+
rel = path.relative_to(root).as_posix()
|
|
252
|
+
signal = "agent.unexpected_mutation" if _looks_agent_generated_root_file(path) else "loose_root_file"
|
|
253
|
+
return {
|
|
254
|
+
"path": rel,
|
|
255
|
+
"absolute_path": str(path),
|
|
256
|
+
"size_bytes": stat.st_size,
|
|
257
|
+
"sha256": f"sha256:{digest}",
|
|
258
|
+
"suffix": path.suffix.lower(),
|
|
259
|
+
"signal": signal,
|
|
260
|
+
"action": "manual_review",
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _looks_agent_generated_root_file(path: Path) -> bool:
|
|
265
|
+
name = path.name.casefold()
|
|
266
|
+
if any(marker.casefold() in name for marker in ROOT_HYGIENE_AGENT_MARKERS):
|
|
267
|
+
return True
|
|
268
|
+
try:
|
|
269
|
+
sample = path.read_text(encoding="utf-8", errors="replace")[:2048].casefold()
|
|
270
|
+
except OSError:
|
|
271
|
+
return False
|
|
272
|
+
return any(marker.casefold() in sample for marker in ROOT_HYGIENE_AGENT_MARKERS)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _backup_or_temp_files(wiki_dir: Path, *, kind: str) -> list[Path]:
|
|
276
|
+
if not wiki_dir.exists():
|
|
277
|
+
return []
|
|
278
|
+
files: list[Path] = []
|
|
279
|
+
for root, dirnames, filenames in os.walk(wiki_dir):
|
|
280
|
+
dirnames[:] = [dirname for dirname in dirnames if not dirname.startswith(".")]
|
|
281
|
+
root_path = Path(root)
|
|
282
|
+
for filename in filenames:
|
|
283
|
+
if filename.startswith("."):
|
|
284
|
+
continue
|
|
285
|
+
path = root_path / filename
|
|
286
|
+
if kind == "backup" and is_canonical_backup_path(path):
|
|
287
|
+
files.append(path)
|
|
288
|
+
elif kind == "rewrite" and ".rewrite" in filename:
|
|
289
|
+
files.append(path)
|
|
290
|
+
return sorted(files, key=lambda item: item.as_posix())
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _empty_dirs(wiki_dir: Path) -> list[Path]:
|
|
294
|
+
if not wiki_dir.exists():
|
|
295
|
+
return []
|
|
296
|
+
empty: list[Path] = []
|
|
297
|
+
for path in sorted((item for item in wiki_dir.rglob("*") if item.is_dir()), key=lambda item: item.as_posix()):
|
|
298
|
+
rel = path.relative_to(wiki_dir)
|
|
299
|
+
if not rel.parts or any(part.startswith(".") for part in rel.parts):
|
|
300
|
+
continue
|
|
301
|
+
if path.name in IGNORED_EMPTY_DIR_NAMES:
|
|
302
|
+
continue
|
|
303
|
+
try:
|
|
304
|
+
if not any(path.iterdir()):
|
|
305
|
+
empty.append(path)
|
|
306
|
+
except OSError:
|
|
307
|
+
continue
|
|
308
|
+
return empty
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def _note_snapshots(wiki_dir: Path) -> list[_HygieneNoteSnapshot]:
|
|
312
|
+
snapshots: list[_HygieneNoteSnapshot] = []
|
|
313
|
+
for path in _note_files(wiki_dir):
|
|
314
|
+
try:
|
|
315
|
+
raw = path.read_bytes()
|
|
316
|
+
except OSError:
|
|
317
|
+
continue
|
|
318
|
+
content = raw.decode("utf-8", errors="replace")
|
|
319
|
+
snapshots.append(
|
|
320
|
+
_HygieneNoteSnapshot(
|
|
321
|
+
path=path,
|
|
322
|
+
content=content,
|
|
323
|
+
sha256=hashlib.sha256(raw).hexdigest(),
|
|
324
|
+
is_index_note=_is_index_note_snapshot(path, content),
|
|
325
|
+
)
|
|
326
|
+
)
|
|
327
|
+
return snapshots
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def _is_index_note_snapshot(path: Path, content: str) -> bool:
|
|
331
|
+
return is_index_target(path.stem) or _is_index_note(path, content)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def _duplicate_hash_groups(wiki_dir: Path) -> list[dict[str, Any]]:
|
|
335
|
+
by_hash: dict[str, list[str]] = {}
|
|
336
|
+
for path in _note_files(wiki_dir):
|
|
337
|
+
digest = hashlib.sha256(path.read_bytes()).hexdigest()
|
|
338
|
+
by_hash.setdefault(digest, []).append(path.relative_to(wiki_dir).as_posix())
|
|
339
|
+
return [
|
|
340
|
+
{"sha256": digest, "files": sorted(files), "count": len(files)}
|
|
341
|
+
for digest, files in sorted(by_hash.items())
|
|
342
|
+
if len(files) > 1
|
|
343
|
+
]
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def _duplicate_hash_groups_from_snapshots(
|
|
347
|
+
wiki_dir: Path,
|
|
348
|
+
snapshots: list[_HygieneNoteSnapshot],
|
|
349
|
+
) -> list[dict[str, Any]]:
|
|
350
|
+
by_hash: dict[str, list[str]] = {}
|
|
351
|
+
for snapshot in snapshots:
|
|
352
|
+
by_hash.setdefault(snapshot.sha256, []).append(snapshot.path.relative_to(wiki_dir).as_posix())
|
|
353
|
+
return [
|
|
354
|
+
{"sha256": digest, "files": sorted(files), "count": len(files)}
|
|
355
|
+
for digest, files in sorted(by_hash.items())
|
|
356
|
+
if len(files) > 1
|
|
357
|
+
]
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def _duplicate_filename_groups(wiki_dir: Path) -> list[dict[str, Any]]:
|
|
361
|
+
by_name: dict[str, list[str]] = {}
|
|
362
|
+
for path in _note_files(wiki_dir):
|
|
363
|
+
if _is_index_note_file(path):
|
|
364
|
+
continue
|
|
365
|
+
by_name.setdefault(normalize_key(path.stem), []).append(path.relative_to(wiki_dir).as_posix())
|
|
366
|
+
return [
|
|
367
|
+
{"key": key, "files": sorted(files), "count": len(files)}
|
|
368
|
+
for key, files in sorted(by_name.items())
|
|
369
|
+
if len(files) > 1
|
|
370
|
+
]
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
def _duplicate_filename_groups_from_snapshots(
|
|
374
|
+
wiki_dir: Path,
|
|
375
|
+
snapshots: list[_HygieneNoteSnapshot],
|
|
376
|
+
) -> list[dict[str, Any]]:
|
|
377
|
+
by_name: dict[str, list[str]] = {}
|
|
378
|
+
for snapshot in snapshots:
|
|
379
|
+
if snapshot.is_index_note:
|
|
380
|
+
continue
|
|
381
|
+
by_name.setdefault(normalize_key(snapshot.path.stem), []).append(snapshot.path.relative_to(wiki_dir).as_posix())
|
|
382
|
+
return [
|
|
383
|
+
{"key": key, "files": sorted(files), "count": len(files)}
|
|
384
|
+
for key, files in sorted(by_name.items())
|
|
385
|
+
if len(files) > 1
|
|
386
|
+
]
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def _note_depth_issues(wiki_dir: Path) -> list[dict[str, Any]]:
|
|
390
|
+
issues: list[dict[str, Any]] = []
|
|
391
|
+
for path in _note_files(wiki_dir):
|
|
392
|
+
rel = path.relative_to(wiki_dir)
|
|
393
|
+
if _is_index_note_file(path):
|
|
394
|
+
continue
|
|
395
|
+
if len(rel.parts) != 4:
|
|
396
|
+
issues.append({"file": rel.as_posix(), "depth": len(rel.parts)})
|
|
397
|
+
return issues
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def _note_depth_issues_from_snapshots(
|
|
401
|
+
wiki_dir: Path,
|
|
402
|
+
snapshots: list[_HygieneNoteSnapshot],
|
|
403
|
+
) -> list[dict[str, Any]]:
|
|
404
|
+
issues: list[dict[str, Any]] = []
|
|
405
|
+
for snapshot in snapshots:
|
|
406
|
+
rel = snapshot.path.relative_to(wiki_dir)
|
|
407
|
+
if snapshot.is_index_note:
|
|
408
|
+
continue
|
|
409
|
+
if len(rel.parts) != 4:
|
|
410
|
+
issues.append({"file": rel.as_posix(), "depth": len(rel.parts)})
|
|
411
|
+
return issues
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def _loose_root_note_files(wiki_dir: Path) -> list[Path]:
|
|
415
|
+
return sorted(
|
|
416
|
+
path
|
|
417
|
+
for path in _note_files(wiki_dir)
|
|
418
|
+
if len(path.relative_to(wiki_dir).parts) == 1 and not _is_index_note_file(path)
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def _loose_root_note_snapshots(
|
|
423
|
+
wiki_dir: Path,
|
|
424
|
+
snapshots: list[_HygieneNoteSnapshot],
|
|
425
|
+
) -> list[_HygieneNoteSnapshot]:
|
|
426
|
+
return sorted(
|
|
427
|
+
(
|
|
428
|
+
snapshot
|
|
429
|
+
for snapshot in snapshots
|
|
430
|
+
if len(snapshot.path.relative_to(wiki_dir).parts) == 1 and not snapshot.is_index_note
|
|
431
|
+
),
|
|
432
|
+
key=lambda snapshot: snapshot.path.relative_to(wiki_dir).as_posix(),
|
|
433
|
+
)
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def _empty_root_note_files(wiki_dir: Path) -> list[Path]:
|
|
437
|
+
return [path for path in _loose_root_note_files(wiki_dir) if _is_strictly_empty(path)]
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
def _is_strictly_empty(path: Path) -> bool:
|
|
441
|
+
try:
|
|
442
|
+
return path.read_text(encoding="utf-8").strip() == ""
|
|
443
|
+
except OSError:
|
|
444
|
+
return False
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def _note_files(wiki_dir: Path) -> list[Path]:
|
|
448
|
+
return iter_notes(wiki_dir)
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def _is_index_note_file(path: Path) -> bool:
|
|
452
|
+
if is_index_target(path.stem):
|
|
453
|
+
return True
|
|
454
|
+
try:
|
|
455
|
+
return _is_index_note(path, path.read_text(encoding="utf-8"))
|
|
456
|
+
except OSError:
|
|
457
|
+
return False
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
def _is_note_candidate(wiki_dir: Path, path: Path) -> bool:
|
|
461
|
+
return is_note_markdown(wiki_dir, path)
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
def _archive_file(wiki_dir: Path, path: Path, archive_root: Path) -> dict[str, str]:
|
|
465
|
+
rel = path.relative_to(wiki_dir)
|
|
466
|
+
destination = _unique_destination(archive_root / rel)
|
|
467
|
+
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
468
|
+
shutil.move(str(path), str(destination))
|
|
469
|
+
return {"source": rel.as_posix(), "destination": str(destination)}
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def _unique_destination(path: Path) -> Path:
|
|
473
|
+
if not path.exists():
|
|
474
|
+
return path
|
|
475
|
+
for idx in range(1, 1000):
|
|
476
|
+
candidate = path.with_name(f"{path.name}.{idx}")
|
|
477
|
+
if not candidate.exists():
|
|
478
|
+
return candidate
|
|
479
|
+
raise OSError(f"Too many archived files with same name: {path}")
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
def _rel_sample(wiki_dir: Path, paths: list[Path], limit: int) -> list[str]:
|
|
483
|
+
return [path.relative_to(wiki_dir).as_posix() for path in paths[:limit]]
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"""Shared anchor generation/cache helper."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import hashlib
|
|
5
|
+
import json
|
|
6
|
+
import sqlite3
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from datetime import UTC, datetime
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Literal
|
|
11
|
+
|
|
12
|
+
from mednotes.domains.wiki.capabilities.illustrate.core.insert import parse_sections
|
|
13
|
+
from mednotes.domains.wiki.flows.enrich.workflow import gemini, parsing, prompts
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class AnchorProviderConfig:
|
|
18
|
+
provider: Literal["gemini_cli"] = "gemini_cli"
|
|
19
|
+
binary: str = "gemini"
|
|
20
|
+
model: str | None = None
|
|
21
|
+
timeout_seconds: int = 120
|
|
22
|
+
skip_trust: bool = True
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True)
|
|
26
|
+
class AnchorBuildResult:
|
|
27
|
+
anchors: list[dict[str, object]]
|
|
28
|
+
provider_receipt: dict[str, object]
|
|
29
|
+
cache_key: str
|
|
30
|
+
cache_hit: bool
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def cache_key(
|
|
34
|
+
note_body_sha256: str,
|
|
35
|
+
provider_config: AnchorProviderConfig,
|
|
36
|
+
preferred_language: str,
|
|
37
|
+
max_anchors: int,
|
|
38
|
+
prompt_version: str,
|
|
39
|
+
) -> str:
|
|
40
|
+
material = {
|
|
41
|
+
"note_body_sha256": note_body_sha256,
|
|
42
|
+
"provider": provider_config.provider,
|
|
43
|
+
"model": provider_config.model or "",
|
|
44
|
+
"preferred_language": preferred_language,
|
|
45
|
+
"max_anchors": max_anchors,
|
|
46
|
+
"prompt_version": prompt_version,
|
|
47
|
+
}
|
|
48
|
+
encoded = json.dumps(material, sort_keys=True, separators=(",", ":")).encode("utf-8")
|
|
49
|
+
return f"sha256:{hashlib.sha256(encoded).hexdigest()}"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def build_or_load_anchors(
|
|
53
|
+
note_path: Path,
|
|
54
|
+
*,
|
|
55
|
+
cache_db: Path,
|
|
56
|
+
max_anchors: int,
|
|
57
|
+
preferred_language: str,
|
|
58
|
+
provider_config: AnchorProviderConfig,
|
|
59
|
+
prompt_version: str = "pdf-library-anchors-v1",
|
|
60
|
+
) -> AnchorBuildResult:
|
|
61
|
+
note_text = note_path.read_text(encoding="utf-8")
|
|
62
|
+
note_sha = hashlib.sha256(note_text.encode("utf-8")).hexdigest()
|
|
63
|
+
key = cache_key(note_sha, provider_config, preferred_language, max_anchors, prompt_version)
|
|
64
|
+
conn = _connect(cache_db)
|
|
65
|
+
cached = _load(conn, key)
|
|
66
|
+
if cached is not None:
|
|
67
|
+
return AnchorBuildResult(cached, _receipt(provider_config, "anchors", "completed", len(cached)), key, True)
|
|
68
|
+
if provider_config.provider != "gemini_cli":
|
|
69
|
+
return AnchorBuildResult([], _receipt(provider_config, "anchors", "blocked", 0, "provider_not_implemented"), key, False)
|
|
70
|
+
prompt = prompts.build_anchors_prompt(
|
|
71
|
+
note_text,
|
|
72
|
+
parse_sections(note_text),
|
|
73
|
+
max_anchors=max_anchors,
|
|
74
|
+
preferred_language=preferred_language,
|
|
75
|
+
)
|
|
76
|
+
anchors, _raw = gemini.call_gemini_json_with_retry(
|
|
77
|
+
prompt,
|
|
78
|
+
parsing.parse_anchors_json,
|
|
79
|
+
binary=provider_config.binary,
|
|
80
|
+
model=provider_config.model,
|
|
81
|
+
timeout_seconds=provider_config.timeout_seconds,
|
|
82
|
+
skip_trust=provider_config.skip_trust,
|
|
83
|
+
label="pdf-library anchors",
|
|
84
|
+
)
|
|
85
|
+
normalized = [dict(anchor) for anchor in anchors]
|
|
86
|
+
_store(conn, key, note_sha, note_path, normalized, provider_config, preferred_language, max_anchors, prompt_version)
|
|
87
|
+
return AnchorBuildResult(normalized, _receipt(provider_config, "anchors", "completed", len(normalized)), key, False)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _connect(path: Path) -> sqlite3.Connection:
|
|
91
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
92
|
+
conn = sqlite3.connect(path)
|
|
93
|
+
conn.execute(
|
|
94
|
+
"""
|
|
95
|
+
CREATE TABLE IF NOT EXISTS anchor_cache (
|
|
96
|
+
cache_key TEXT NOT NULL,
|
|
97
|
+
note_sha256 TEXT NOT NULL,
|
|
98
|
+
anchor_id TEXT NOT NULL,
|
|
99
|
+
note_path TEXT NOT NULL DEFAULT '',
|
|
100
|
+
section_path_json TEXT NOT NULL DEFAULT '[]',
|
|
101
|
+
concept TEXT NOT NULL,
|
|
102
|
+
visual_type TEXT NOT NULL,
|
|
103
|
+
search_queries_json TEXT NOT NULL DEFAULT '[]',
|
|
104
|
+
provider TEXT NOT NULL DEFAULT '',
|
|
105
|
+
model_id TEXT NOT NULL DEFAULT '',
|
|
106
|
+
preferred_language TEXT NOT NULL DEFAULT '',
|
|
107
|
+
max_anchors INTEGER NOT NULL DEFAULT 0,
|
|
108
|
+
prompt_version TEXT NOT NULL DEFAULT 'pdf-library-anchors-v1',
|
|
109
|
+
created_at TEXT NOT NULL,
|
|
110
|
+
payload_json TEXT NOT NULL,
|
|
111
|
+
PRIMARY KEY (cache_key, anchor_id)
|
|
112
|
+
)
|
|
113
|
+
"""
|
|
114
|
+
)
|
|
115
|
+
return conn
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _load(conn: sqlite3.Connection, key: str) -> list[dict[str, object]] | None:
|
|
119
|
+
rows = conn.execute("SELECT payload_json FROM anchor_cache WHERE cache_key = ? ORDER BY anchor_id", (key,)).fetchall()
|
|
120
|
+
if not rows:
|
|
121
|
+
return None
|
|
122
|
+
return [json.loads(str(row[0])) for row in rows]
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _store(
|
|
126
|
+
conn: sqlite3.Connection,
|
|
127
|
+
key: str,
|
|
128
|
+
note_sha: str,
|
|
129
|
+
note_path: Path,
|
|
130
|
+
anchors: list[dict[str, object]],
|
|
131
|
+
provider_config: AnchorProviderConfig,
|
|
132
|
+
preferred_language: str,
|
|
133
|
+
max_anchors: int,
|
|
134
|
+
prompt_version: str,
|
|
135
|
+
) -> None:
|
|
136
|
+
now = datetime.now(UTC).isoformat()
|
|
137
|
+
with conn:
|
|
138
|
+
for index, anchor in enumerate(anchors):
|
|
139
|
+
anchor_id = str(anchor.get("anchor_id") or f"a{index + 1}")
|
|
140
|
+
conn.execute(
|
|
141
|
+
"""
|
|
142
|
+
INSERT OR REPLACE INTO anchor_cache(
|
|
143
|
+
cache_key, note_sha256, anchor_id, note_path, section_path_json,
|
|
144
|
+
concept, visual_type, search_queries_json, provider, model_id,
|
|
145
|
+
preferred_language, max_anchors, prompt_version, created_at, payload_json
|
|
146
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
147
|
+
""",
|
|
148
|
+
(
|
|
149
|
+
key,
|
|
150
|
+
note_sha,
|
|
151
|
+
anchor_id,
|
|
152
|
+
str(note_path),
|
|
153
|
+
json.dumps(anchor.get("section_path") or [], ensure_ascii=False),
|
|
154
|
+
str(anchor.get("concept") or ""),
|
|
155
|
+
str(anchor.get("visual_type") or ""),
|
|
156
|
+
json.dumps(anchor.get("search_queries") or [], ensure_ascii=False),
|
|
157
|
+
provider_config.provider,
|
|
158
|
+
provider_config.model or "",
|
|
159
|
+
preferred_language,
|
|
160
|
+
max_anchors,
|
|
161
|
+
prompt_version,
|
|
162
|
+
now,
|
|
163
|
+
json.dumps(anchor, ensure_ascii=False),
|
|
164
|
+
),
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _receipt(
|
|
169
|
+
provider_config: AnchorProviderConfig,
|
|
170
|
+
purpose: str,
|
|
171
|
+
status: str,
|
|
172
|
+
item_count: int,
|
|
173
|
+
blocked_reason: str = "",
|
|
174
|
+
) -> dict[str, object]:
|
|
175
|
+
return {
|
|
176
|
+
"schema": "medical-notes-workbench.pdf-library-provider-receipt.v1",
|
|
177
|
+
"provider": provider_config.provider,
|
|
178
|
+
"model": provider_config.model or "gemini-configured-default",
|
|
179
|
+
"purpose": purpose,
|
|
180
|
+
"status": status,
|
|
181
|
+
"blocked_reason": blocked_reason,
|
|
182
|
+
"quota_limited": False,
|
|
183
|
+
"item_count": item_count,
|
|
184
|
+
"created_at": datetime.now(UTC).isoformat(),
|
|
185
|
+
}
|
|
File without changes
|