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.
Files changed (430) hide show
  1. package/.opencode/agents/med-chat-triager.md +204 -0
  2. package/.opencode/agents/med-flashcard-maker.md +63 -0
  3. package/.opencode/agents/med-knowledge-architect.md +230 -0
  4. package/.opencode/agents/med-link-graph-curator.md +177 -0
  5. package/.opencode/agents/med-publish-guard.md +62 -0
  6. package/.opencode/commands/flashcards.md +25 -0
  7. package/.opencode/commands/mednotes/create.md +25 -0
  8. package/.opencode/commands/mednotes/enrich.md +27 -0
  9. package/.opencode/commands/mednotes/fix-wiki.md +27 -0
  10. package/.opencode/commands/mednotes/history.md +22 -0
  11. package/.opencode/commands/mednotes/link-body.md +25 -0
  12. package/.opencode/commands/mednotes/link-related.md +27 -0
  13. package/.opencode/commands/mednotes/link.md +27 -0
  14. package/.opencode/commands/mednotes/pdf-library.md +27 -0
  15. package/.opencode/commands/mednotes/process-chats.md +23 -0
  16. package/.opencode/commands/mednotes/setup.md +21 -0
  17. package/.opencode/commands/mednotes/status.md +27 -0
  18. package/.opencode/commands/mednotes/telemetry.md +27 -0
  19. package/.opencode/commands/report.md +26 -0
  20. package/.opencode/mednotes/AGENTS.md +57 -0
  21. package/.opencode/mednotes/agents/med-chat-triager.md +197 -0
  22. package/.opencode/mednotes/agents/med-flashcard-maker.md +56 -0
  23. package/.opencode/mednotes/agents/med-knowledge-architect.md +224 -0
  24. package/.opencode/mednotes/agents/med-link-graph-curator.md +171 -0
  25. package/.opencode/mednotes/agents/med-publish-guard.md +55 -0
  26. package/.opencode/mednotes/contracts/.gitkeep +1 -0
  27. package/.opencode/mednotes/contracts/agents.json +116 -0
  28. package/.opencode/mednotes/contracts/opencode-plugin.json +70 -0
  29. package/.opencode/mednotes/docs/agent-prompt-hardening.md +567 -0
  30. package/.opencode/mednotes/docs/agent-role-contracts.md +94 -0
  31. package/.opencode/mednotes/docs/anki-mcp-twenty-rules.md +214 -0
  32. package/.opencode/mednotes/docs/anki-templates/README.md +39 -0
  33. package/.opencode/mednotes/docs/anki-templates/cloze.back.html +23 -0
  34. package/.opencode/mednotes/docs/anki-templates/cloze.front.html +14 -0
  35. package/.opencode/mednotes/docs/anki-templates/qa.back.html +24 -0
  36. package/.opencode/mednotes/docs/anki-templates/qa.front.html +14 -0
  37. package/.opencode/mednotes/docs/anki-templates/style.css +182 -0
  38. package/.opencode/mednotes/docs/atomicity-splitting-policy.md +113 -0
  39. package/.opencode/mednotes/docs/extension-docs.md +40 -0
  40. package/.opencode/mednotes/docs/flashcard-ingestion.md +278 -0
  41. package/.opencode/mednotes/docs/knowledge-architect.md +208 -0
  42. package/.opencode/mednotes/docs/merge-policy.md +110 -0
  43. package/.opencode/mednotes/docs/public-vocabulary.md +104 -0
  44. package/.opencode/mednotes/docs/semantic-linker.md +141 -0
  45. package/.opencode/mednotes/docs/taxonomy-policy.md +90 -0
  46. package/.opencode/mednotes/docs/triage-policy.md +187 -0
  47. package/.opencode/mednotes/docs/vault-version-control.md +758 -0
  48. package/.opencode/mednotes/docs/vocabulary-db-recovery.md +58 -0
  49. package/.opencode/mednotes/docs/workflow-output-contract.md +779 -0
  50. package/.opencode/mednotes/hooks/hooks.json +79 -0
  51. package/.opencode/mednotes/package-lock.json +6361 -0
  52. package/.opencode/mednotes/package.json +15 -0
  53. package/.opencode/mednotes/pyproject.toml +48 -0
  54. package/.opencode/mednotes/scripts/bootstrap_windows_python_uv.cmd +13 -0
  55. package/.opencode/mednotes/scripts/bootstrap_windows_python_uv.ps1 +172 -0
  56. package/.opencode/mednotes/scripts/enrich_notes.py +23 -0
  57. package/.opencode/mednotes/scripts/full_reset_windows_python_uv.cmd +13 -0
  58. package/.opencode/mednotes/scripts/hooks/antigravity_hook_status.mjs +212 -0
  59. package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/antigravity.mjs +169 -0
  60. package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/harness_payload.mjs +103 -0
  61. package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/opencode_plugin.mjs +341 -0
  62. package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/opencode_user_config_sync.mjs +177 -0
  63. package/.opencode/mednotes/scripts/hooks/mednotes_hook/anki_preflight.mjs +214 -0
  64. package/.opencode/mednotes/scripts/hooks/mednotes_hook/cli.mjs +143 -0
  65. package/.opencode/mednotes/scripts/hooks/mednotes_hook/diagnostics.mjs +11 -0
  66. package/.opencode/mednotes/scripts/hooks/mednotes_hook/domain/agent_directive_core.mjs +160 -0
  67. package/.opencode/mednotes/scripts/hooks/mednotes_hook/fsm_directive.mjs +1470 -0
  68. package/.opencode/mednotes/scripts/hooks/mednotes_hook/hook_errors.mjs +120 -0
  69. package/.opencode/mednotes/scripts/hooks/mednotes_hook/retention.mjs +114 -0
  70. package/.opencode/mednotes/scripts/hooks/mednotes_hook/runtime.mjs +174 -0
  71. package/.opencode/mednotes/scripts/hooks/mednotes_hook/telemetry_capture.mjs +511 -0
  72. package/.opencode/mednotes/scripts/hooks/mednotes_hook/vault_guard.mjs +624 -0
  73. package/.opencode/mednotes/scripts/hooks/mednotes_hook.mjs +5 -0
  74. package/.opencode/mednotes/scripts/mednotes/_runtime_paths.py +24 -0
  75. package/.opencode/mednotes/scripts/mednotes/anki_model_validator.py +18 -0
  76. package/.opencode/mednotes/scripts/mednotes/capture_extension_diff.py +1562 -0
  77. package/.opencode/mednotes/scripts/mednotes/feedback_report.py +16 -0
  78. package/.opencode/mednotes/scripts/mednotes/flashcard_index.py +18 -0
  79. package/.opencode/mednotes/scripts/mednotes/flashcard_pipeline.py +18 -0
  80. package/.opencode/mednotes/scripts/mednotes/flashcard_report.py +18 -0
  81. package/.opencode/mednotes/scripts/mednotes/flashcard_sources.py +18 -0
  82. package/.opencode/mednotes/scripts/mednotes/obsidian/README.md +6 -0
  83. package/.opencode/mednotes/scripts/mednotes/obsidian_note_utils.py +20 -0
  84. package/.opencode/mednotes/scripts/mednotes/pdf_library/cli.py +16 -0
  85. package/.opencode/mednotes/scripts/mednotes/project_fsm.py +229 -0
  86. package/.opencode/mednotes/scripts/mednotes/setup_telemetry_email.py +404 -0
  87. package/.opencode/mednotes/scripts/mednotes/sync_anki_twenty_rules.py +18 -0
  88. package/.opencode/mednotes/scripts/mednotes/sync_opencode_user_config.py +36 -0
  89. package/.opencode/mednotes/scripts/mednotes/wiki/cli.py +20 -0
  90. package/.opencode/mednotes/scripts/mednotes/wiki_graph.py +18 -0
  91. package/.opencode/mednotes/scripts/mednotes/wiki_tree.py +134 -0
  92. package/.opencode/mednotes/scripts/reset_windows_python_uv.ps1 +625 -0
  93. package/.opencode/mednotes/scripts/run_python.mjs +109 -0
  94. package/.opencode/mednotes/scripts/vault/vault_commit.ps1 +19 -0
  95. package/.opencode/mednotes/scripts/vault/vault_commit.sh +18 -0
  96. package/.opencode/mednotes/scripts/vault/vault_git.ps1 +19 -0
  97. package/.opencode/mednotes/scripts/vault/vault_git.py +3107 -0
  98. package/.opencode/mednotes/scripts/vault/vault_git.sh +18 -0
  99. package/.opencode/mednotes/scripts/vault/vault_precommit.ps1 +19 -0
  100. package/.opencode/mednotes/scripts/vault/vault_precommit.sh +18 -0
  101. package/.opencode/mednotes/skills/THIRD_PARTY_NOTICES.md +45 -0
  102. package/.opencode/mednotes/skills/create-medical-flashcards/SKILL.md +113 -0
  103. package/.opencode/mednotes/skills/create-medical-note/SKILL.md +90 -0
  104. package/.opencode/mednotes/skills/enrich-medical-note/SKILL.md +120 -0
  105. package/.opencode/mednotes/skills/fix-medical-wiki/SKILL.md +559 -0
  106. package/.opencode/mednotes/skills/link-medical-wiki/SKILL.md +224 -0
  107. package/.opencode/mednotes/skills/obsidian-cli/SKILL.md +118 -0
  108. package/.opencode/mednotes/skills/obsidian-markdown/SKILL.md +207 -0
  109. package/.opencode/mednotes/skills/obsidian-markdown/references/CALLOUTS.md +58 -0
  110. package/.opencode/mednotes/skills/obsidian-markdown/references/EMBEDS.md +63 -0
  111. package/.opencode/mednotes/skills/obsidian-markdown/references/PROPERTIES.md +61 -0
  112. package/.opencode/mednotes/skills/obsidian-ops/SKILL.md +136 -0
  113. package/.opencode/mednotes/skills/pdf-library/SKILL.md +45 -0
  114. package/.opencode/mednotes/skills/process-medical-chats/SKILL.md +246 -0
  115. package/.opencode/mednotes/skills/workflow-report/SKILL.md +100 -0
  116. package/.opencode/mednotes/src/mednotes/__init__.py +5 -0
  117. package/.opencode/mednotes/src/mednotes/domains/__init__.py +5 -0
  118. package/.opencode/mednotes/src/mednotes/domains/flashcards/README.md +26 -0
  119. package/.opencode/mednotes/src/mednotes/domains/flashcards/__init__.py +2 -0
  120. package/.opencode/mednotes/src/mednotes/domains/flashcards/build_demo_apkg.py +177 -0
  121. package/.opencode/mednotes/src/mednotes/domains/flashcards/contracts.py +385 -0
  122. package/.opencode/mednotes/src/mednotes/domains/flashcards/flashcards_machine.py +522 -0
  123. package/.opencode/mednotes/src/mednotes/domains/flashcards/fsm.py +817 -0
  124. package/.opencode/mednotes/src/mednotes/domains/flashcards/index.py +630 -0
  125. package/.opencode/mednotes/src/mednotes/domains/flashcards/install_models.py +445 -0
  126. package/.opencode/mednotes/src/mednotes/domains/flashcards/model.py +359 -0
  127. package/.opencode/mednotes/src/mednotes/domains/flashcards/obsidian_links.py +135 -0
  128. package/.opencode/mednotes/src/mednotes/domains/flashcards/obsidian_note_utils.py +546 -0
  129. package/.opencode/mednotes/src/mednotes/domains/flashcards/pipeline.py +580 -0
  130. package/.opencode/mednotes/src/mednotes/domains/flashcards/report.py +510 -0
  131. package/.opencode/mednotes/src/mednotes/domains/flashcards/sources.py +682 -0
  132. package/.opencode/mednotes/src/mednotes/domains/flashcards/sync_rules.py +184 -0
  133. package/.opencode/mednotes/src/mednotes/domains/history/__init__.py +1 -0
  134. package/.opencode/mednotes/src/mednotes/domains/history/history_fsm.py +852 -0
  135. package/.opencode/mednotes/src/mednotes/domains/history/history_machine.py +453 -0
  136. package/.opencode/mednotes/src/mednotes/domains/setup/__init__.py +7 -0
  137. package/.opencode/mednotes/src/mednotes/domains/setup/setup_fsm.py +808 -0
  138. package/.opencode/mednotes/src/mednotes/domains/setup/setup_machine.py +973 -0
  139. package/.opencode/mednotes/src/mednotes/domains/wiki/README.md +64 -0
  140. package/.opencode/mednotes/src/mednotes/domains/wiki/__init__.py +1 -0
  141. package/.opencode/mednotes/src/mednotes/domains/wiki/api.py +668 -0
  142. package/.opencode/mednotes/src/mednotes/domains/wiki/batch_state.py +102 -0
  143. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/__init__.py +1 -0
  144. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/atomicity/__init__.py +1 -0
  145. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/atomicity/atomicity.py +877 -0
  146. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/body_link/__init__.py +1 -0
  147. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/body_link/body_linker.py +1562 -0
  148. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/effects/__init__.py +1 -0
  149. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/effects/effect_adapters.py +949 -0
  150. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/effects/fix_wiki_runtime_adapters.py +433 -0
  151. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/__init__.py +1 -0
  152. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/coverage.py +413 -0
  153. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/graph.py +396 -0
  154. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/graph_fixes.py +161 -0
  155. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/hygiene/__init__.py +1 -0
  156. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/hygiene/hygiene.py +483 -0
  157. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/__init__.py +2 -0
  158. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/anchors.py +185 -0
  159. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/__init__.py +0 -0
  160. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/cache.py +223 -0
  161. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/config.py +131 -0
  162. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/download.py +224 -0
  163. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/frontmatter.py +59 -0
  164. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/insert.py +227 -0
  165. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/local_import.py +54 -0
  166. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/__init__.py +42 -0
  167. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/web_profiles.py +99 -0
  168. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/web_search.py +203 -0
  169. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/wikimedia.py +102 -0
  170. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/__init__.py +1 -0
  171. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/markdown_db_adapter.mjs +434 -0
  172. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/markdown_node_runtime.py +274 -0
  173. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/markdown_query.py +227 -0
  174. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/__init__.py +1 -0
  175. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/artifacts.py +605 -0
  176. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/canonical_merge.py +277 -0
  177. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/markdown_zones.py +85 -0
  178. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/meaning_planner.py +307 -0
  179. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_iter.py +67 -0
  180. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_merge.py +278 -0
  181. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_plan.py +409 -0
  182. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_policy.py +22 -0
  183. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/__init__.py +79 -0
  184. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/fixes.py +264 -0
  185. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/frontmatter.py +435 -0
  186. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/models.py +208 -0
  187. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/prompts.py +37 -0
  188. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/tables.py +236 -0
  189. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/validate.py +404 -0
  190. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/provenance.py +478 -0
  191. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/raw_chats.py +273 -0
  192. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/sources_backfill.py +235 -0
  193. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/__init__.py +10 -0
  194. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/anchors.py +16 -0
  195. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/captions.py +47 -0
  196. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/cli.py +179 -0
  197. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/cloud.py +52 -0
  198. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/config.py +196 -0
  199. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/context_packets.py +76 -0
  200. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/db.py +81 -0
  201. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/doctor.py +102 -0
  202. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/figure_ids.py +42 -0
  203. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/ingest.py +326 -0
  204. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/insert.py +316 -0
  205. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/mentions.py +57 -0
  206. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/ocr.py +71 -0
  207. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/paths.py +35 -0
  208. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/pdf_engine.py +77 -0
  209. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/schema.py +155 -0
  210. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/search.py +188 -0
  211. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/__init__.py +1 -0
  212. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/app.py +89 -0
  213. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/image_backend.py +29 -0
  214. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/state.py +65 -0
  215. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/__init__.py +1 -0
  216. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/publish.py +1139 -0
  217. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/publish_receipts.py +365 -0
  218. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/publish_recovery.py +240 -0
  219. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/__init__.py +1 -0
  220. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/agent_behavior_corpus.py +2069 -0
  221. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/agent_report_validation.py +4448 -0
  222. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/agent_run_audit.py +852 -0
  223. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/architect_prompt_eval.py +341 -0
  224. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/body_linker_eval.py +240 -0
  225. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/curator_output_validation.py +175 -0
  226. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/curator_prompt_eval.py +865 -0
  227. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/triager_prompt_eval.py +1295 -0
  228. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/related_notes/__init__.py +1 -0
  229. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/related_notes/related_notes.py +1920 -0
  230. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/related_notes/related_notes_headless.py +1186 -0
  231. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/__init__.py +1 -0
  232. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/plan_attestation.py +148 -0
  233. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/specialist_receipts.py +360 -0
  234. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/specialist_runtime.py +52 -0
  235. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/specialist_task_runner.py +2470 -0
  236. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/style/__init__.py +1 -0
  237. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/style/style.py +1952 -0
  238. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/subagents/__init__.py +1 -0
  239. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/subagents/agents.py +1767 -0
  240. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/__init__.py +1 -0
  241. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/alias_projection.py +331 -0
  242. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/link_terms.py +151 -0
  243. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/llm_disambiguation.py +182 -0
  244. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/__init__.py +116 -0
  245. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/audit.py +201 -0
  246. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/migration.py +314 -0
  247. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/normalize.py +72 -0
  248. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/policy.py +135 -0
  249. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/resolve.py +413 -0
  250. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/schema.py +157 -0
  251. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/status.py +137 -0
  252. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_bootstrap.py +509 -0
  253. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_curator_batch.py +1115 -0
  254. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_ingestion.py +632 -0
  255. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_map.py +930 -0
  256. package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_recovery.py +1388 -0
  257. package/.opencode/mednotes/src/mednotes/domains/wiki/cli.py +6665 -0
  258. package/.opencode/mednotes/src/mednotes/domains/wiki/common.py +69 -0
  259. package/.opencode/mednotes/src/mednotes/domains/wiki/config.py +210 -0
  260. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/__init__.py +74 -0
  261. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/agent_report.py +242 -0
  262. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/agent_run_audit.py +196 -0
  263. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/agents.py +601 -0
  264. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/curator.py +256 -0
  265. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/effect_payloads.py +519 -0
  266. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/happy_path.py +190 -0
  267. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/link_git.py +110 -0
  268. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/link_runtime_artifact.py +52 -0
  269. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/note_plan.py +75 -0
  270. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/paths.py +114 -0
  271. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/public_report.py +53 -0
  272. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/publish.py +111 -0
  273. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/raw_coverage.py +217 -0
  274. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/related_notes.py +136 -0
  275. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/related_notes_headless.py +153 -0
  276. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/related_notes_runtime.py +395 -0
  277. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/schema_registry.py +637 -0
  278. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/specialist.py +432 -0
  279. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/status.py +62 -0
  280. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/style_rewrite.py +568 -0
  281. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/vocabulary_ingestion.py +223 -0
  282. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_blockers.py +510 -0
  283. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_guardrails.py +637 -0
  284. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_outcomes.py +121 -0
  285. package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_receipts.py +100 -0
  286. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/__init__.py +1 -0
  287. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/__init__.py +1 -0
  288. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/__main__.py +4 -0
  289. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/cli.py +275 -0
  290. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/__init__.py +2 -0
  291. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/candidates.py +193 -0
  292. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/cli.py +189 -0
  293. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/gemini.py +220 -0
  294. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/inputs.py +120 -0
  295. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/models.py +34 -0
  296. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/parsing.py +48 -0
  297. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/prompts.py +216 -0
  298. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/quality.py +54 -0
  299. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/reporting.py +24 -0
  300. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/runner.py +433 -0
  301. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/utils.py +39 -0
  302. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/vault_guard_bridge.py +17 -0
  303. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/__init__.py +1 -0
  304. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_context_packets.py +454 -0
  305. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_decision_projection.py +133 -0
  306. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_effects.py +1260 -0
  307. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_fsm.py +2768 -0
  308. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_machine.py +1588 -0
  309. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_plan.py +306 -0
  310. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_primary_objective.py +316 -0
  311. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_problem.py +153 -0
  312. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_receipt_evidence.py +306 -0
  313. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_states.py +290 -0
  314. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_user_report.py +342 -0
  315. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/health.py +6332 -0
  316. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/__init__.py +1 -0
  317. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_fsm.py +1119 -0
  318. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_git.py +638 -0
  319. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_machine.py +1106 -0
  320. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_retry_governance.py +374 -0
  321. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_runtime_result.py +485 -0
  322. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_triggers.py +183 -0
  323. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/linking.py +2758 -0
  324. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/reference_repair.py +718 -0
  325. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/related_notes_fsm.py +1855 -0
  326. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link_related/__init__.py +1 -0
  327. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link_related/link_related_machine.py +834 -0
  328. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/__init__.py +1 -0
  329. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_fsm.py +1592 -0
  330. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_machine.py +3097 -0
  331. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_primary_objective.py +28 -0
  332. package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_runtime_result.py +185 -0
  333. package/.opencode/mednotes/src/mednotes/domains/wiki/performance.py +97 -0
  334. package/.opencode/mednotes/src/mednotes/kernel/__init__.py +6 -0
  335. package/.opencode/mednotes/src/mednotes/kernel/agent_directive.py +336 -0
  336. package/.opencode/mednotes/src/mednotes/kernel/base.py +51 -0
  337. package/.opencode/mednotes/src/mednotes/kernel/blockers.py +39 -0
  338. package/.opencode/mednotes/src/mednotes/kernel/effect_executor.py +55 -0
  339. package/.opencode/mednotes/src/mednotes/kernel/effect_intent.py +69 -0
  340. package/.opencode/mednotes/src/mednotes/kernel/effects.py +160 -0
  341. package/.opencode/mednotes/src/mednotes/kernel/errors.py +38 -0
  342. package/.opencode/mednotes/src/mednotes/kernel/fsm_event.py +35 -0
  343. package/.opencode/mednotes/src/mednotes/kernel/fsm_model.py +55 -0
  344. package/.opencode/mednotes/src/mednotes/kernel/fsm_transition_result.py +75 -0
  345. package/.opencode/mednotes/src/mednotes/kernel/guardrails.py +188 -0
  346. package/.opencode/mednotes/src/mednotes/kernel/progress.py +319 -0
  347. package/.opencode/mednotes/src/mednotes/kernel/public_report.py +346 -0
  348. package/.opencode/mednotes/src/mednotes/kernel/state_machine.py +164 -0
  349. package/.opencode/mednotes/src/mednotes/kernel/workflow.py +619 -0
  350. package/.opencode/mednotes/src/mednotes/platform/__init__.py +5 -0
  351. package/.opencode/mednotes/src/mednotes/platform/backup_policy.py +382 -0
  352. package/.opencode/mednotes/src/mednotes/platform/feedback/__init__.py +62 -0
  353. package/.opencode/mednotes/src/mednotes/platform/feedback/cli.py +275 -0
  354. package/.opencode/mednotes/src/mednotes/platform/feedback/contracts.py +83 -0
  355. package/.opencode/mednotes/src/mednotes/platform/feedback/core.py +4168 -0
  356. package/.opencode/mednotes/src/mednotes/platform/feedback/integrity.py +989 -0
  357. package/.opencode/mednotes/src/mednotes/platform/feedback/operational_contract.py +2293 -0
  358. package/.opencode/mednotes/src/mednotes/platform/feedback/telemetry.py +875 -0
  359. package/.opencode/mednotes/src/mednotes/platform/feedback/telemetry_config.py +65 -0
  360. package/.opencode/mednotes/src/mednotes/platform/opencode_runtime_config.py +182 -0
  361. package/.opencode/mednotes/src/mednotes/platform/paths/__init__.py +1560 -0
  362. package/.opencode/mednotes/src/mednotes/platform/secrets.py +89 -0
  363. package/.opencode/mednotes/src/mednotes/platform/user_config.py +103 -0
  364. package/.opencode/mednotes/src/mednotes/platform/vault_guard.py +214 -0
  365. package/.opencode/mednotes/uv.lock +932 -0
  366. package/.opencode/mednotes.generated.json +395 -0
  367. package/.opencode/opencode.json +31 -0
  368. package/.opencode/plugins/mednotes-fsm.mjs +7 -0
  369. package/.opencode/plugins/mednotes_hook/adapters/antigravity.mjs +169 -0
  370. package/.opencode/plugins/mednotes_hook/adapters/harness_payload.mjs +103 -0
  371. package/.opencode/plugins/mednotes_hook/adapters/opencode_plugin.mjs +341 -0
  372. package/.opencode/plugins/mednotes_hook/adapters/opencode_user_config_sync.mjs +177 -0
  373. package/.opencode/plugins/mednotes_hook/anki_preflight.mjs +214 -0
  374. package/.opencode/plugins/mednotes_hook/cli.mjs +143 -0
  375. package/.opencode/plugins/mednotes_hook/diagnostics.mjs +11 -0
  376. package/.opencode/plugins/mednotes_hook/domain/agent_directive_core.mjs +160 -0
  377. package/.opencode/plugins/mednotes_hook/fsm_directive.mjs +1470 -0
  378. package/.opencode/plugins/mednotes_hook/hook_errors.mjs +120 -0
  379. package/.opencode/plugins/mednotes_hook/retention.mjs +114 -0
  380. package/.opencode/plugins/mednotes_hook/runtime.mjs +174 -0
  381. package/.opencode/plugins/mednotes_hook/telemetry_capture.mjs +511 -0
  382. package/.opencode/plugins/mednotes_hook/vault_guard.mjs +624 -0
  383. package/AGENTS.md +57 -0
  384. package/README.md +194 -0
  385. package/adapters/antigravity/agents.json +80 -0
  386. package/adapters/antigravity/templates/med-chat-triager.md +214 -0
  387. package/adapters/antigravity/templates/med-flashcard-maker.md +72 -0
  388. package/adapters/antigravity/templates/med-knowledge-architect.md +241 -0
  389. package/adapters/antigravity/templates/med-link-graph-curator.md +187 -0
  390. package/adapters/antigravity/templates/med-publish-guard.md +71 -0
  391. package/adapters/gemini-cli/gemini-extension.json +14 -0
  392. package/adapters/gemini-cli/package.json +15 -0
  393. package/adapters/gemini-cli/pyproject.toml +48 -0
  394. package/bin/mednotes-opencode.mjs +155 -0
  395. package/contracts/agents.json +116 -0
  396. package/core/agents/med-chat-triager.md +197 -0
  397. package/core/agents/med-flashcard-maker.md +56 -0
  398. package/core/agents/med-knowledge-architect.md +224 -0
  399. package/core/agents/med-link-graph-curator.md +171 -0
  400. package/core/agents/med-publish-guard.md +55 -0
  401. package/core/commands/flashcards.toml +22 -0
  402. package/core/commands/mednotes/create.toml +22 -0
  403. package/core/commands/mednotes/enrich.toml +24 -0
  404. package/core/commands/mednotes/fix-wiki.toml +24 -0
  405. package/core/commands/mednotes/history.toml +19 -0
  406. package/core/commands/mednotes/link-body.toml +22 -0
  407. package/core/commands/mednotes/link-related.toml +24 -0
  408. package/core/commands/mednotes/link.toml +24 -0
  409. package/core/commands/mednotes/pdf-library.toml +24 -0
  410. package/core/commands/mednotes/process-chats.toml +20 -0
  411. package/core/commands/mednotes/setup.toml +18 -0
  412. package/core/commands/mednotes/status.toml +24 -0
  413. package/core/commands/mednotes/telemetry.toml +24 -0
  414. package/core/commands/report.toml +23 -0
  415. package/core/skills/THIRD_PARTY_NOTICES.md +45 -0
  416. package/core/skills/create-medical-flashcards/SKILL.md +113 -0
  417. package/core/skills/create-medical-note/SKILL.md +90 -0
  418. package/core/skills/enrich-medical-note/SKILL.md +120 -0
  419. package/core/skills/fix-medical-wiki/SKILL.md +559 -0
  420. package/core/skills/link-medical-wiki/SKILL.md +224 -0
  421. package/core/skills/obsidian-cli/SKILL.md +118 -0
  422. package/core/skills/obsidian-markdown/SKILL.md +207 -0
  423. package/core/skills/obsidian-markdown/references/CALLOUTS.md +58 -0
  424. package/core/skills/obsidian-markdown/references/EMBEDS.md +63 -0
  425. package/core/skills/obsidian-markdown/references/PROPERTIES.md +61 -0
  426. package/core/skills/obsidian-ops/SKILL.md +136 -0
  427. package/core/skills/pdf-library/SKILL.md +45 -0
  428. package/core/skills/process-medical-chats/SKILL.md +246 -0
  429. package/core/skills/workflow-report/SKILL.md +100 -0
  430. package/package.json +45 -0
@@ -0,0 +1,630 @@
1
+ #!/usr/bin/env python3
2
+ """Local idempotency index for /flashcards candidate cards.
3
+
4
+ This script does not talk to Anki. It owns a deterministic local index used by
5
+ the Gemini command before/after Anki MCP writes:
6
+
7
+ - `check` filters candidate cards into new vs duplicate.
8
+ - `record` stores cards that Anki accepted.
9
+ - `summary` reports the current index.
10
+ """
11
+ from __future__ import annotations
12
+
13
+ import argparse
14
+ import json
15
+ import os
16
+ import re
17
+ import sys
18
+ import tempfile
19
+ from datetime import UTC, datetime
20
+ from hashlib import sha256
21
+ from pathlib import Path
22
+ from typing import Literal
23
+
24
+ from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, field_validator, model_validator
25
+ from pydantic import ValidationError as PydanticValidationError
26
+ from typing_extensions import TypeAliasType
27
+
28
+ SCHEMA = "medical-notes-workbench.flashcards-index.v1"
29
+ CHECK_SCHEMA = "medical-notes-workbench.flashcards-index-check.v1"
30
+ SOURCE_STATUS_SCHEMA = "medical-notes-workbench.flashcards-source-status.v1"
31
+ DEFAULT_INDEX = "~/.mednotes/FLASHCARDS_INDEX.json"
32
+
33
+ EXIT_OK = 0
34
+ EXIT_USAGE = 2
35
+ EXIT_IO = 5
36
+
37
+
38
+ class IndexErrorWithCode(Exception):
39
+ exit_code = EXIT_IO
40
+
41
+
42
+ class UsageError(IndexErrorWithCode):
43
+ exit_code = EXIT_USAGE
44
+
45
+
46
+ JsonValue = TypeAliasType(
47
+ "JsonValue",
48
+ str | int | float | bool | None | list["JsonValue"] | dict[str, "JsonValue"],
49
+ )
50
+ JsonObject = dict[str, JsonValue]
51
+ JsonValueAdapter = TypeAdapter(JsonValue)
52
+ JsonObjectAdapter = TypeAdapter(JsonObject)
53
+ JsonArrayAdapter = TypeAdapter(list[JsonValue])
54
+
55
+
56
+ class _FieldModel(BaseModel):
57
+ # Flashcards index is a CLI boundary: parse permissively at the edge, then
58
+ # use only declared fields for decisions and hashes.
59
+ model_config = ConfigDict(extra="ignore", populate_by_name=True, validate_assignment=True)
60
+
61
+ def to_payload(self) -> JsonObject:
62
+ return _json_object(self.model_dump(mode="json", by_alias=True), prefix=type(self).__name__)
63
+
64
+
65
+ class _SourceIndexRecordFields(_FieldModel):
66
+ content_sha256: str = ""
67
+ card_hashes: list[str] = Field(default_factory=list)
68
+
69
+
70
+ class _FlashcardIndexCardRecord(_FieldModel):
71
+ card_hash: str = ""
72
+ recorded_at: str = ""
73
+ source_path: str = ""
74
+ source_relative_path: str = ""
75
+ source_content_sha256: str = ""
76
+ source_excerpt: str = ""
77
+ deck: str = ""
78
+ note_model: str = ""
79
+ front: str = ""
80
+ back: str = ""
81
+ extra: str = ""
82
+ obsidian: str = ""
83
+
84
+
85
+ class _FlashcardIndexSourceRecord(_FieldModel):
86
+ path: str = ""
87
+ vault_relative_path: str = ""
88
+ content_sha256: str = ""
89
+ card_hashes: list[str] = Field(default_factory=list)
90
+ updated_at: str = ""
91
+
92
+
93
+ class _FlashcardIndex(_FieldModel):
94
+ schema_id: Literal["medical-notes-workbench.flashcards-index.v1"] = Field(
95
+ default=SCHEMA,
96
+ alias="schema",
97
+ )
98
+ version: int = 1
99
+ updated_at: str | None = None
100
+ cards: dict[str, _FlashcardIndexCardRecord] = Field(default_factory=dict)
101
+ sources: dict[str, _FlashcardIndexSourceRecord] = Field(default_factory=dict)
102
+
103
+
104
+ class _SourceNote(_FieldModel):
105
+ path: str = ""
106
+ vault_relative_path: str = ""
107
+ content_sha256: str = ""
108
+ deck: str = ""
109
+ deeplink: str = ""
110
+
111
+ @field_validator("path", "vault_relative_path", "content_sha256", "deck", "deeplink", mode="before")
112
+ @classmethod
113
+ def _text_or_empty(cls, value: object) -> str:
114
+ return "" if value is None else str(value)
115
+
116
+
117
+ class _SourceManifest(_FieldModel):
118
+ notes: list[_SourceNote] = Field(default_factory=list)
119
+
120
+ def notes_by_path(self) -> dict[str, _SourceNote]:
121
+ return {note.path: note for note in self.notes if note.path}
122
+
123
+
124
+ class _CardFields(_FieldModel):
125
+ fields: JsonObject = Field(default_factory=dict)
126
+
127
+
128
+ class _CandidateCard(_FieldModel):
129
+ source_path: str = ""
130
+ source: str = ""
131
+ path: str = ""
132
+ source_relative_path: str = ""
133
+ vault_relative_path: str = ""
134
+ source_content_sha256: str = ""
135
+ content_sha256: str = ""
136
+ note_sha256: str = ""
137
+ source_excerpt: str = ""
138
+ trecho: str = ""
139
+ deck: str = ""
140
+ note_model: str = ""
141
+ model: str = ""
142
+ fields: JsonObject = Field(default_factory=dict)
143
+ card_hash: str = ""
144
+
145
+ @field_validator(
146
+ "source_path",
147
+ "source",
148
+ "path",
149
+ "source_relative_path",
150
+ "vault_relative_path",
151
+ "source_content_sha256",
152
+ "content_sha256",
153
+ "note_sha256",
154
+ "source_excerpt",
155
+ "trecho",
156
+ "deck",
157
+ "note_model",
158
+ "model",
159
+ "card_hash",
160
+ mode="before",
161
+ )
162
+ @classmethod
163
+ def _text_or_empty(cls, value: object) -> str:
164
+ return "" if value is None else str(value)
165
+
166
+ @property
167
+ def resolved_source_path(self) -> str:
168
+ return self.source_path or self.source or self.path
169
+
170
+ @property
171
+ def resolved_relative_path(self) -> str:
172
+ return self.source_relative_path or self.vault_relative_path
173
+
174
+ @property
175
+ def resolved_content_sha(self) -> str:
176
+ return self.source_content_sha256 or self.content_sha256 or self.note_sha256
177
+
178
+ @property
179
+ def resolved_note_model(self) -> str:
180
+ return self.note_model or self.model
181
+
182
+ def with_source_note(self, source_note: _SourceNote | None) -> _CandidateCard:
183
+ if source_note is None:
184
+ return self
185
+ fields = dict(self.fields)
186
+ if "Obsidian" not in fields and source_note.deeplink:
187
+ fields["Obsidian"] = source_note.deeplink
188
+ return self.model_copy(
189
+ update={
190
+ "source_content_sha256": self.resolved_content_sha or source_note.content_sha256,
191
+ "source_relative_path": self.resolved_relative_path or source_note.vault_relative_path,
192
+ "deck": self.deck or source_note.deck,
193
+ "fields": fields,
194
+ }
195
+ )
196
+
197
+ def output_payload(self, digest: str) -> JsonObject:
198
+ payload: JsonObject = {
199
+ "source_path": self.resolved_source_path,
200
+ "source_relative_path": self.resolved_relative_path,
201
+ "source_content_sha256": self.resolved_content_sha,
202
+ "deck": self.deck,
203
+ "note_model": self.resolved_note_model,
204
+ "fields": self.fields,
205
+ "card_hash": digest,
206
+ }
207
+ if self.source_excerpt:
208
+ payload["source_excerpt"] = self.source_excerpt
209
+ return payload
210
+
211
+
212
+ class _CandidatePayload(_FieldModel):
213
+ source_manifest: _SourceManifest | None = None
214
+ candidate_cards: list[_CandidateCard] = Field(default_factory=list)
215
+ accepted_cards: list[_CandidateCard] = Field(default_factory=list)
216
+ new_cards: list[_CandidateCard] = Field(default_factory=list)
217
+ cards: list[_CandidateCard] = Field(default_factory=list)
218
+
219
+ @model_validator(mode="before")
220
+ @classmethod
221
+ def _normalize_source_manifest(cls, value: object) -> object:
222
+ if not isinstance(value, dict):
223
+ return value
224
+ if "source_manifest" in value:
225
+ return value
226
+ if "notes" in value:
227
+ normalized = dict(value)
228
+ normalized["source_manifest"] = {"notes": value["notes"]}
229
+ return normalized
230
+ return value
231
+
232
+ def selected_cards(self) -> list[_CandidateCard]:
233
+ for cards in (self.candidate_cards, self.accepted_cards, self.new_cards, self.cards):
234
+ if cards:
235
+ return cards
236
+ return []
237
+
238
+ def notes_by_path(self) -> dict[str, _SourceNote]:
239
+ if self.source_manifest is None:
240
+ return {}
241
+ return self.source_manifest.notes_by_path()
242
+
243
+
244
+ class _NormalizedCard(_FieldModel):
245
+ source_path: str = ""
246
+ source_relative_path: str = ""
247
+ source_content_sha256: str = ""
248
+ source_excerpt: str = ""
249
+ deck: str = ""
250
+ note_model: str = ""
251
+ front: str = ""
252
+ back: str = ""
253
+ extra: str = ""
254
+ obsidian: str = ""
255
+
256
+
257
+ class _RecordSummary(_FieldModel):
258
+ accepted_count: int = Field(ge=0)
259
+ added_count: int = Field(ge=0)
260
+ already_present_count: int = Field(ge=0)
261
+
262
+
263
+ def _now_iso() -> str:
264
+ return datetime.now(UTC).replace(microsecond=0).isoformat()
265
+
266
+
267
+ def _validation_error(exc: PydanticValidationError, *, prefix: str) -> UsageError:
268
+ first = exc.errors()[0] if exc.errors() else {}
269
+ loc_parts = first["loc"] if "loc" in first else ()
270
+ loc = ".".join(str(part) for part in loc_parts) or "$"
271
+ msg = str(first["msg"] if "msg" in first else str(exc))
272
+ return UsageError(f"{prefix}: {loc}: {msg}")
273
+
274
+
275
+ def _json_object(value: object, *, prefix: str) -> JsonObject:
276
+ try:
277
+ return JsonObjectAdapter.validate_python(value)
278
+ except PydanticValidationError as exc:
279
+ raise _validation_error(exc, prefix=prefix) from exc
280
+
281
+
282
+ def _json_array(value: object, *, prefix: str) -> list[JsonValue]:
283
+ try:
284
+ return JsonArrayAdapter.validate_python(value)
285
+ except PydanticValidationError as exc:
286
+ raise _validation_error(exc, prefix=prefix) from exc
287
+
288
+
289
+ def _source_index_record(value: JsonValue, *, label: str) -> _SourceIndexRecordFields | None:
290
+ if value is None:
291
+ return None
292
+ if not isinstance(value, dict):
293
+ raise UsageError(f"{label}: source record must be an object")
294
+ payload = _json_object(value, prefix=label)
295
+ fields: JsonObject = {}
296
+ if "content_sha256" in payload:
297
+ fields["content_sha256"] = payload["content_sha256"]
298
+ if "card_hashes" in payload:
299
+ fields["card_hashes"] = payload["card_hashes"]
300
+ try:
301
+ return _SourceIndexRecordFields.model_validate(fields)
302
+ except PydanticValidationError as exc:
303
+ raise _validation_error(exc, prefix=label) from exc
304
+
305
+
306
+ def _path(value: str | os.PathLike[str]) -> Path:
307
+ return Path(os.path.expandvars(str(value))).expanduser()
308
+
309
+
310
+ def _index_path(value: str | None = None) -> Path:
311
+ return _path(value or os.getenv("MED_FLASHCARDS_INDEX") or DEFAULT_INDEX)
312
+
313
+
314
+ def _read_json(path: str | Path) -> JsonValue:
315
+ if str(path) == "-":
316
+ raw = json.loads(sys.stdin.read())
317
+ else:
318
+ with Path(path).open("r", encoding="utf-8") as fh:
319
+ raw = json.load(fh)
320
+ try:
321
+ return JsonValueAdapter.validate_python(raw)
322
+ except PydanticValidationError as exc:
323
+ raise _validation_error(exc, prefix=f"JSON payload {path}") from exc
324
+
325
+
326
+ def _write_json_atomic(path: Path, data: object) -> None:
327
+ path.parent.mkdir(parents=True, exist_ok=True)
328
+ fd, tmp_name = tempfile.mkstemp(prefix=f".{path.name}.", suffix=".tmp", dir=str(path.parent))
329
+ tmp = Path(tmp_name)
330
+ try:
331
+ with os.fdopen(fd, "w", encoding="utf-8", newline="") as fh:
332
+ json.dump(data, fh, ensure_ascii=False, indent=2, sort_keys=True)
333
+ fh.write("\n")
334
+ tmp.replace(path)
335
+ except Exception:
336
+ tmp.unlink(missing_ok=True)
337
+ raise
338
+
339
+
340
+ def _index_model(value: object) -> _FlashcardIndex:
341
+ try:
342
+ return _FlashcardIndex.model_validate(value)
343
+ except PydanticValidationError as exc:
344
+ raise _validation_error(exc, prefix="flashcards index") from exc
345
+
346
+
347
+ def _load_index(path: Path) -> JsonObject:
348
+ if not path.exists():
349
+ return _FlashcardIndex().to_payload()
350
+ data = _read_json(path)
351
+ if not isinstance(data, dict) or "schema" not in data or data["schema"] != SCHEMA:
352
+ raise UsageError(f"Unsupported flashcards index schema in {path}")
353
+ return _index_model(data).to_payload()
354
+
355
+
356
+ def _json(data: object) -> None:
357
+ print(json.dumps(data, ensure_ascii=False, indent=2, sort_keys=True))
358
+
359
+
360
+ def _clean_text(value: object) -> str:
361
+ text = "" if value is None else str(value)
362
+ return re.sub(r"\s+", " ", text.strip())
363
+
364
+
365
+ def _value_for_name(values: JsonObject, names: list[str]) -> JsonValue:
366
+ casefold_values = {str(key).casefold(): value for key, value in values.items()}
367
+ for name in names:
368
+ if name in values:
369
+ return values[name]
370
+ lowered = name.casefold()
371
+ if lowered in casefold_values:
372
+ return casefold_values[lowered]
373
+ return ""
374
+
375
+
376
+ def _field_from(card: _CandidateCard, names: list[str]) -> str:
377
+ return _clean_text(_value_for_name(card.fields, names))
378
+
379
+
380
+ def normalize_card(card: object) -> dict[str, str]:
381
+ candidate = _candidate_card(card, label="flashcard candidate")
382
+ normalized = _NormalizedCard(
383
+ source_path=candidate.resolved_source_path,
384
+ source_relative_path=candidate.resolved_relative_path,
385
+ source_content_sha256=candidate.resolved_content_sha,
386
+ source_excerpt=_clean_text(candidate.source_excerpt or candidate.trecho),
387
+ deck=_clean_text(candidate.deck),
388
+ note_model=_clean_text(candidate.resolved_note_model),
389
+ front=_field_from(candidate, ["Frente", "Front", "front", "pergunta"]),
390
+ back=_field_from(candidate, ["Verso", "Back", "back", "resposta"]),
391
+ extra=_field_from(candidate, ["Verso Extra", "Extra", "extra", "verso_extra"]),
392
+ obsidian=_field_from(candidate, ["Obsidian", "obsidian"]),
393
+ )
394
+ return {key: str(value) for key, value in normalized.to_payload().items()}
395
+
396
+
397
+ def card_hash(card: object) -> str:
398
+ normalized = normalize_card(card)
399
+ payload = json.dumps(normalized, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
400
+ return sha256(payload.encode("utf-8")).hexdigest()
401
+
402
+
403
+ def _payload_model(payload: object) -> _CandidatePayload:
404
+ try:
405
+ return _CandidatePayload.model_validate(payload)
406
+ except PydanticValidationError as exc:
407
+ raise _validation_error(exc, prefix="flashcards candidate payload") from exc
408
+
409
+
410
+ def _source_notes(payload: object) -> dict[str, _SourceNote]:
411
+ return _payload_model(payload).notes_by_path()
412
+
413
+
414
+ def _candidate_card(value: object, *, label: str) -> _CandidateCard:
415
+ try:
416
+ return _CandidateCard.model_validate(value)
417
+ except PydanticValidationError as exc:
418
+ raise _validation_error(exc, prefix=label) from exc
419
+
420
+
421
+ def _candidate_cards(payload: object) -> list[_CandidateCard]:
422
+ if isinstance(payload, list):
423
+ return [_candidate_card(card, label="flashcards candidate") for card in _json_array(payload, prefix="candidate cards")]
424
+
425
+ parsed = _payload_model(payload)
426
+ source_notes = parsed.notes_by_path()
427
+ normalized_cards: list[_CandidateCard] = []
428
+ for card in parsed.selected_cards():
429
+ source_path = card.resolved_source_path
430
+ source_note = source_notes[source_path] if source_path in source_notes else None
431
+ normalized_cards.append(card.with_source_note(source_note))
432
+ return normalized_cards
433
+
434
+
435
+ def check_candidates(payload: object, index: object) -> JsonObject:
436
+ known = _index_model(index).cards
437
+ new_cards: list[JsonObject] = []
438
+ duplicate_cards: list[JsonObject] = []
439
+ for card in _candidate_cards(payload):
440
+ digest = card.card_hash or card_hash(card.to_payload())
441
+ record = card.output_payload(digest)
442
+ if digest in known:
443
+ duplicate_cards.append({**record, "duplicate_of": digest})
444
+ else:
445
+ new_cards.append(record)
446
+ return _json_object({
447
+ "schema": CHECK_SCHEMA,
448
+ "summary": {
449
+ "candidate_count": len(new_cards) + len(duplicate_cards),
450
+ "new_count": len(new_cards),
451
+ "duplicate_count": len(duplicate_cards),
452
+ },
453
+ "new_cards": new_cards,
454
+ "duplicate_cards": duplicate_cards,
455
+ }, prefix="flashcards index check")
456
+
457
+
458
+ def record_cards(payload: object, index: object) -> tuple[JsonObject, JsonObject]:
459
+ cards = _candidate_cards(payload)
460
+ index_model = _index_model(index)
461
+ now = _now_iso()
462
+ added = 0
463
+ already_present = 0
464
+ for card in cards:
465
+ digest = card.card_hash or card_hash(card.to_payload())
466
+ normalized = normalize_card(card)
467
+ if digest in index_model.cards:
468
+ already_present += 1
469
+ else:
470
+ added += 1
471
+ index_model.cards[digest] = _FlashcardIndexCardRecord(
472
+ card_hash=digest,
473
+ recorded_at=now,
474
+ **normalized,
475
+ )
476
+ source_key = normalized["source_path"] or normalized["source_relative_path"] or "__unknown__"
477
+ if source_key not in index_model.sources:
478
+ index_model.sources[source_key] = _FlashcardIndexSourceRecord(
479
+ path=normalized["source_path"],
480
+ vault_relative_path=normalized["source_relative_path"],
481
+ content_sha256=normalized["source_content_sha256"],
482
+ updated_at=now,
483
+ )
484
+ source = index_model.sources[source_key]
485
+ source.content_sha256 = normalized["source_content_sha256"]
486
+ source.updated_at = now
487
+ if digest not in source.card_hashes:
488
+ source.card_hashes.append(digest)
489
+ index_model.updated_at = now
490
+ summary = _RecordSummary(
491
+ accepted_count=len(cards),
492
+ added_count=added,
493
+ already_present_count=already_present,
494
+ )
495
+ return index_model.to_payload(), summary.to_payload()
496
+
497
+
498
+ def source_status(payload: object, index: object) -> JsonObject:
499
+ notes = _source_notes(payload)
500
+ records: list[JsonObject] = []
501
+ new_count = 0
502
+ unchanged_count = 0
503
+ changed_count = 0
504
+ sources = _index_model(index).sources
505
+
506
+ for path, note in sorted(notes.items()):
507
+ relative = note.vault_relative_path
508
+ current_sha = note.content_sha256
509
+ source_record = sources[path] if path in sources else sources[relative] if relative in sources else None
510
+ existing = _source_index_record(
511
+ source_record.to_payload() if source_record is not None else None,
512
+ label=f"flashcards source {path}",
513
+ )
514
+ if not existing:
515
+ status = "new"
516
+ new_count += 1
517
+ elif existing.content_sha256 == current_sha:
518
+ status = "unchanged"
519
+ unchanged_count += 1
520
+ else:
521
+ status = "changed"
522
+ changed_count += 1
523
+ records.append(
524
+ {
525
+ "path": path,
526
+ "vault_relative_path": relative,
527
+ "status": status,
528
+ "current_content_sha256": current_sha,
529
+ "indexed_content_sha256": existing.content_sha256 if existing else "",
530
+ "indexed_card_count": len(existing.card_hashes) if existing else 0,
531
+ }
532
+ )
533
+
534
+ summary: JsonObject = {
535
+ "new_count": new_count,
536
+ "unchanged_count": unchanged_count,
537
+ "changed_count": changed_count,
538
+ }
539
+ return _json_object({"schema": SOURCE_STATUS_SCHEMA, "summary": summary, "sources": records}, prefix="source status")
540
+
541
+
542
+ def _cmd_check(args: argparse.Namespace) -> int:
543
+ path = _index_path(args.index)
544
+ result = check_candidates(_read_json(args.candidates), _load_index(path))
545
+ result["index_path"] = str(path)
546
+ _json(result)
547
+ return EXIT_OK
548
+
549
+
550
+ def _cmd_source_status(args: argparse.Namespace) -> int:
551
+ path = _index_path(args.index)
552
+ result = source_status(_read_json(args.manifest), _load_index(path))
553
+ result["index_path"] = str(path)
554
+ _json(result)
555
+ return EXIT_OK
556
+
557
+
558
+ def _cmd_record(args: argparse.Namespace) -> int:
559
+ path = _index_path(args.index)
560
+ index = _load_index(path)
561
+ updated, summary = record_cards(_read_json(args.accepted), index)
562
+ result = {
563
+ "schema": SCHEMA,
564
+ "index_path": str(path),
565
+ "dry_run": args.dry_run,
566
+ "summary": summary,
567
+ }
568
+ if not args.dry_run:
569
+ _write_json_atomic(path, updated)
570
+ _json(result)
571
+ return EXIT_OK
572
+
573
+
574
+ def _cmd_summary(args: argparse.Namespace) -> int:
575
+ path = _index_path(args.index)
576
+ index = _index_model(_load_index(path))
577
+ _json(
578
+ {
579
+ "schema": SCHEMA,
580
+ "index_path": str(path),
581
+ "updated_at": index.updated_at,
582
+ "card_count": len(index.cards),
583
+ "source_count": len(index.sources),
584
+ }
585
+ )
586
+ return EXIT_OK
587
+
588
+
589
+ def build_parser() -> argparse.ArgumentParser:
590
+ parser = argparse.ArgumentParser(description=__doc__)
591
+ sub = parser.add_subparsers(dest="command", required=True)
592
+
593
+ check = sub.add_parser("check", help="split candidate cards into new and duplicate")
594
+ check.add_argument("--candidates", required=True, help="candidate-card JSON file, or '-' for stdin")
595
+ check.add_argument("--index", help=f"index path; default {DEFAULT_INDEX}")
596
+ check.set_defaults(func=_cmd_check)
597
+
598
+ status = sub.add_parser("source-status", help="compare source note hashes against the index")
599
+ status.add_argument("--manifest", required=True, help="source manifest JSON file, or '-' for stdin")
600
+ status.add_argument("--index", help=f"index path; default {DEFAULT_INDEX}")
601
+ status.set_defaults(func=_cmd_source_status)
602
+
603
+ record = sub.add_parser("record", help="record cards accepted by Anki")
604
+ record.add_argument("--accepted", required=True, help="accepted-card JSON file, or '-' for stdin")
605
+ record.add_argument("--index", help=f"index path; default {DEFAULT_INDEX}")
606
+ record.add_argument("--dry-run", action="store_true", help="report without writing the index")
607
+ record.set_defaults(func=_cmd_record)
608
+
609
+ summary = sub.add_parser("summary", help="summarize the local flashcards index")
610
+ summary.add_argument("--index", help=f"index path; default {DEFAULT_INDEX}")
611
+ summary.set_defaults(func=_cmd_summary)
612
+
613
+ return parser
614
+
615
+
616
+ def main(argv: list[str] | None = None) -> int:
617
+ parser = build_parser()
618
+ args = parser.parse_args(argv)
619
+ try:
620
+ return args.func(args)
621
+ except IndexErrorWithCode as exc:
622
+ print(str(exc), file=sys.stderr)
623
+ return exc.exit_code
624
+ except (OSError, json.JSONDecodeError) as exc:
625
+ print(str(exc), file=sys.stderr)
626
+ return EXIT_IO
627
+
628
+
629
+ if __name__ == "__main__":
630
+ raise SystemExit(main())