@furongjun1999/dsh-memory 0.8.1 → 0.9.7

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 (717) hide show
  1. package/README.md +662 -659
  2. package/codebuddy/README.md +1 -1
  3. package/data/policy.json +38 -27
  4. package/docs/README.md +18 -2
  5. package/docs/discipline/harnesses.yaml +1 -1
  6. package/docs/discipline/templates/spec.txt.tmpl +23 -0
  7. package/docs//350/247/204/350/214/203/AI/347/274/226/347/250/213/351/200/232/347/224/250/350/247/204/350/214/203.md +179 -0
  8. package/docs//350/247/204/350/214/203/AI/347/274/226/347/250/213/351/200/232/347/224/250/350/247/204/350/214/203_/347/201/265/346/236/242/351/200/202/351/205/215.md +45 -0
  9. package/docs//350/247/204/350/214/203/AI/347/274/226/347/250/213/351/200/232/347/224/250/350/247/204/350/214/203_/350/207/252/346/243/200/346/270/205/345/215/225.md +69 -0
  10. package/dsh/README.md +1 -1
  11. package/dsh/cordis.yml.example +139 -139
  12. package/hive/README.md +705 -0
  13. package/hive/id_charset_blocks.txt +147 -0
  14. package/lib/bridge.d.ts +3 -0
  15. package/lib/bridge.js +32 -11
  16. package/lib/cli.js +14 -17
  17. package/lib/hooks.d.ts +37 -3
  18. package/lib/hooks.js +176 -24
  19. package/lib/index.d.ts +2 -2
  20. package/lib/index.js +29 -6
  21. package/lib/init.js +21 -21
  22. package/lib/lib/datapath.d.ts +56 -0
  23. package/lib/lib/datapath.js +102 -9
  24. package/lib/lib/hook_audit.d.ts +0 -1
  25. package/lib/lib/hook_audit.js +51 -3
  26. package/lib/lib/mdcg_client.d.ts +4 -1
  27. package/lib/lib/mdcg_client.js +10 -2
  28. package/lib/lib/mutual.js +61 -15
  29. package/lib/lib/prompt_safety.js +28 -2
  30. package/lib/lib/roleplay_web.js +30 -2
  31. package/lib/lib/token_store.d.ts +25 -6
  32. package/lib/lib/token_store.js +224 -33
  33. package/lib/tools.d.ts +15 -2
  34. package/lib/tools.js +44 -4
  35. package/md_cg/_guard_kit.py +121 -0
  36. package/md_cg/admission.py +718 -718
  37. package/md_cg/anchor.py +114 -0
  38. package/md_cg/audit.py +37 -7
  39. package/md_cg/autonomy.py +433 -288
  40. package/md_cg/autonomy_modes.py +781 -642
  41. package/md_cg/bench_beir_bm25.py +154 -0
  42. package/md_cg/bench_beir_scifact.py +170 -0
  43. package/md_cg/bench_bm25_blockmax.py +134 -0
  44. package/md_cg/bench_bm25_incremental.py +246 -0
  45. package/md_cg/bench_ccg_heading.py +118 -0
  46. package/md_cg/bench_csre_correctness.py +145 -0
  47. package/md_cg/bench_excerpt_budget.py +94 -0
  48. package/md_cg/bench_fuzzy_substrings.py +299 -0
  49. package/md_cg/bench_memory_rsi.py +358 -0
  50. package/md_cg/bench_memory_rsi_latency.py +82 -0
  51. package/md_cg/bench_memory_rsi_public.py +116 -0
  52. package/md_cg/bench_memory_rsi_tools.py +229 -0
  53. package/md_cg/bench_rrf_candidates.py +212 -0
  54. package/md_cg/bench_rrf_public.py +100 -0
  55. package/md_cg/bench_text_projections.py +221 -0
  56. package/md_cg/bench_timeanchor.py +357 -0
  57. package/md_cg/bm25.py +601 -0
  58. package/md_cg/branches.py +404 -327
  59. package/md_cg/build_postings.py +74 -73
  60. package/md_cg/ccgc.py +4 -2
  61. package/md_cg/chain.py +573 -379
  62. package/md_cg/coldverify.py +531 -292
  63. package/md_cg/condaccept.py +263 -0
  64. package/md_cg/condgate.py +308 -0
  65. package/md_cg/config_registry.py +329 -0
  66. package/md_cg/config_registry_bulk.py +482 -0
  67. package/md_cg/config_registry_guard.py +487 -0
  68. package/md_cg/config_render.py +354 -0
  69. package/md_cg/config_validate.py +455 -0
  70. package/md_cg/conformance.py +747 -726
  71. package/md_cg/conformance_core.py +753 -0
  72. package/md_cg/consistency.py +1278 -955
  73. package/md_cg/consolidate.py +1793 -1633
  74. package/md_cg/crypto.py +346 -24
  75. package/md_cg/datapath.py +137 -12
  76. package/md_cg/eval_common.py +522 -575
  77. package/md_cg/evidence.py +946 -585
  78. package/md_cg/evidence_delivery.py +101 -0
  79. package/md_cg/experience_consolidation.py +317 -0
  80. package/md_cg/export.py +329 -222
  81. package/md_cg/forgetting.py +1050 -922
  82. package/md_cg/freshness.py +601 -548
  83. package/md_cg/fsutil.py +1120 -824
  84. package/md_cg/goal_gen.py +458 -0
  85. package/md_cg/hotcache.py +107 -9
  86. package/md_cg/hyperedge.py +262 -251
  87. package/md_cg/identity.py +573 -391
  88. package/md_cg/imgskill.py +1712 -0
  89. package/md_cg/insight.py +540 -521
  90. package/md_cg/interface_schema.py +146 -0
  91. package/md_cg/interop.py +20 -0
  92. package/md_cg/judgment_manifest.py +24 -1
  93. package/md_cg/lifecycle.py +426 -345
  94. package/md_cg/links.py +27 -6
  95. package/md_cg/logref.py +332 -327
  96. package/md_cg/mcp_server.py +5227 -4449
  97. package/md_cg/mdcg.py +1301 -203
  98. package/md_cg/mdcos.py +7108 -5440
  99. package/md_cg/memory_rsi.py +308 -0
  100. package/md_cg/metacognition.py +726 -591
  101. package/md_cg/nodefile.py +151 -3
  102. package/md_cg/op_table.py +1128 -0
  103. package/md_cg/postings.py +524 -300
  104. package/md_cg/predict.py +1103 -1100
  105. package/md_cg/probe_memory_rsi.py +95 -0
  106. package/md_cg/probe_memory_rsi_mcp.py +66 -0
  107. package/md_cg/protect.py +654 -494
  108. package/md_cg/protocol.py +492 -411
  109. package/md_cg/provenance.py +606 -600
  110. package/md_cg/qmd.py +477 -0
  111. package/md_cg/qmd_llm.py +334 -0
  112. package/md_cg/query_excerpt.py +171 -0
  113. package/md_cg/queue_health.py +130 -0
  114. package/md_cg/reach.py +470 -453
  115. package/md_cg/readcache.py +115 -3
  116. package/md_cg/reconcile.py +239 -228
  117. package/md_cg/refindex.py +1191 -1042
  118. package/md_cg/refine.py +613 -604
  119. package/md_cg/retrieve.py +128 -0
  120. package/md_cg/review_cli.py +204 -26
  121. package/md_cg/revise.py +140 -0
  122. package/md_cg/rollback.py +657 -420
  123. package/md_cg/rollback_cli.py +124 -104
  124. package/md_cg/rotate.py +347 -0
  125. package/md_cg/routing.py +490 -429
  126. package/md_cg/rrf_candidates.py +204 -0
  127. package/md_cg/run_tests.py +19 -1
  128. package/md_cg/scrub.py +177 -23
  129. package/md_cg/security.py +269 -17
  130. package/md_cg/self_state.py +1053 -1029
  131. package/md_cg/self_surface.py +50 -0
  132. package/md_cg/semantic/canonical.py +144 -144
  133. package/md_cg/semantic/export_en_zh_map.py +2 -2
  134. package/md_cg/semantic/zh_en_atoms.py +141 -139
  135. package/md_cg/session5.py +106 -0
  136. package/md_cg/signer.py +76 -2
  137. package/md_cg/sleep.py +1779 -1583
  138. package/md_cg/sources.py +1091 -833
  139. package/md_cg/state_events.py +75 -10
  140. package/md_cg/state_slots.py +312 -221
  141. package/md_cg/stg.py +85 -18
  142. package/md_cg/stgidx.py +537 -281
  143. package/md_cg/subgraph.py +851 -729
  144. package/md_cg/sustain.py +458 -19
  145. package/md_cg/task_memory.py +367 -0
  146. package/md_cg/test_action_derive.py +210 -203
  147. package/md_cg/test_audit_heartbeat_20261015.py +417 -0
  148. package/md_cg/test_audit_protection_20261015.py +483 -0
  149. package/md_cg/test_audit_rotate_parity.py +280 -0
  150. package/md_cg/test_audit_state_20261015.py +253 -0
  151. package/md_cg/test_audit_write_20261015.py +161 -0
  152. package/md_cg/test_auto_defaults.py +43 -4
  153. package/md_cg/test_autonomy_emotion.py +328 -0
  154. package/md_cg/test_autonomy_modes.py +2013 -1973
  155. package/md_cg/test_b1_foreign_read.py +245 -0
  156. package/md_cg/test_bm25.py +438 -0
  157. package/md_cg/test_bm25_hydration_refill.py +265 -0
  158. package/md_cg/test_bm25_incremental.py +241 -0
  159. package/md_cg/test_bm25_incremental_mutations.py +160 -0
  160. package/md_cg/test_bm25_mutations.py +205 -0
  161. package/md_cg/test_bm25_pool_view.py +196 -0
  162. package/md_cg/test_boot_openblas_guard.py +218 -197
  163. package/md_cg/test_boundary_hit.py +1 -1
  164. package/md_cg/test_branches_sensitivity.py +375 -0
  165. package/md_cg/test_c8_search_rrf_gates.py +1 -1
  166. package/md_cg/test_canonical_timezones.py +211 -0
  167. package/md_cg/test_ccg_heading_parser.py +73 -0
  168. package/md_cg/test_chain_best_first.py +403 -0
  169. package/md_cg/test_chain_candidate_scope.py +227 -0
  170. package/md_cg/test_cn_recall_grams.py +206 -0
  171. package/md_cg/test_code_kind_forbidden.py +101 -0
  172. package/md_cg/test_coldverify_drain.py +127 -0
  173. package/md_cg/test_condaccept.py +156 -0
  174. package/md_cg/test_condgate.py +141 -0
  175. package/md_cg/test_config_registry.py +55 -0
  176. package/md_cg/test_crypto_rotate.py +257 -0
  177. package/md_cg/test_csre_correctness.py +297 -0
  178. package/md_cg/test_csre_index_import_guard.py +161 -0
  179. package/md_cg/test_csre_mutations.py +34 -0
  180. package/md_cg/test_csre_session.py +69 -0
  181. package/md_cg/test_curiosity_budget.py +468 -0
  182. package/md_cg/test_d17_autostart_toolface.py +453 -0
  183. package/md_cg/test_datapath_tilde_home.py +271 -0
  184. package/md_cg/test_derived_cache_generations.py +256 -0
  185. package/md_cg/test_evidence_delivery_compact.py +54 -0
  186. package/md_cg/test_exact_text_cache.py +191 -0
  187. package/md_cg/test_excerpt_budget.py +188 -0
  188. package/md_cg/test_experience_consolidation.py +112 -0
  189. package/md_cg/test_export_default_collision.py +138 -0
  190. package/md_cg/test_export_snapshots.py +322 -0
  191. package/md_cg/test_fuzzy_substring_equivalence.py +127 -0
  192. package/md_cg/test_gain_gate.py +16 -5
  193. package/md_cg/test_goal_gen.py +355 -0
  194. package/md_cg/test_h2_session_view_norm.py +251 -233
  195. package/md_cg/test_h4_sustain_snapshot.py +1490 -1387
  196. package/md_cg/test_heartbeat_ledger.py +557 -0
  197. package/md_cg/test_hierarchy_audit_graph.py +237 -0
  198. package/md_cg/test_i50c_meta_passthrough.py +628 -608
  199. package/md_cg/test_i50e_readside_protection.py +658 -656
  200. package/md_cg/test_identity_attribution.py +96 -2
  201. package/md_cg/test_identity_contract.py +329 -0
  202. package/md_cg/test_imgskill.py +1444 -0
  203. package/md_cg/test_index_snapshot_shape.py +240 -0
  204. package/md_cg/test_interop_judgment.py +258 -228
  205. package/md_cg/test_issue111_gate_order.py +77 -0
  206. package/md_cg/test_issue111_scoring_contract.py +87 -0
  207. package/md_cg/test_issue121_calibration_bounds.py +293 -0
  208. package/md_cg/test_issue128_session_env_authority.py +246 -0
  209. package/md_cg/test_issue130_rrf_rank_credibility.py +217 -0
  210. package/md_cg/test_issue131_postings_content_staleness.py +424 -0
  211. package/md_cg/test_issue145_sleep_no_silent_rollback.py +168 -0
  212. package/md_cg/test_issue146_frontmatter_quarantine.py +428 -0
  213. package/md_cg/test_issue147_pack_id_escape.py +378 -0
  214. package/md_cg/test_issue148_merge_downgrade_leak.py +192 -0
  215. package/md_cg/test_issue149_accel_default_face.py +249 -0
  216. package/md_cg/test_issue150_empty_query_terms.py +328 -0
  217. package/md_cg/test_issue151_153_157_write_cache_index.py +152 -0
  218. package/md_cg/test_issue151b_base_add_importance_guard.py +166 -0
  219. package/md_cg/test_issue152_sealed_postings.py +309 -0
  220. package/md_cg/test_issue152b_decrypt_cache.py +218 -0
  221. package/md_cg/test_issue153b_cache_alias_and_canon.py +236 -0
  222. package/md_cg/test_issue153b_index_generation_invalidation.py +143 -0
  223. package/md_cg/test_issue157b_index_remainder.py +344 -0
  224. package/md_cg/test_issue189_conclusion_bidirectional.py +223 -0
  225. package/md_cg/test_issue190_tidy_idempotent.py +806 -0
  226. package/md_cg/test_issue191_import_trust.py +382 -0
  227. package/md_cg/test_issue192_move_layer_indices_guard.py +258 -0
  228. package/md_cg/test_issue193_consistency_knowledge_guard.py +524 -0
  229. package/md_cg/test_issue193_write_scope.py +378 -0
  230. package/md_cg/test_issue197_rank_credibility_property.py +201 -0
  231. package/md_cg/test_issue211_export_target.py +325 -0
  232. package/md_cg/test_issue212_writeface_gaps.py +349 -0
  233. package/md_cg/test_issue214_reject_sensitivity.py +267 -0
  234. package/md_cg/test_issue215_condition_source_gap.py +272 -0
  235. package/md_cg/test_issue216_clash_routes.py +214 -0
  236. package/md_cg/test_issue217_slot_routes.py +248 -0
  237. package/md_cg/test_issue218_postcondition_value_blind.py +222 -0
  238. package/md_cg/test_issue225_consistency_torn_log.py +433 -0
  239. package/md_cg/test_issue245_swarm_traversal.py +366 -0
  240. package/md_cg/test_issue258_pack_sensitivity.py +468 -0
  241. package/md_cg/test_issue265_coldverify_multiproc.py +438 -0
  242. package/md_cg/test_issue274_read_node_id_none.py +341 -0
  243. package/md_cg/test_issue275_layer_param.py +250 -0
  244. package/md_cg/test_issue277_node_id_traversal.py +384 -0
  245. package/md_cg/test_issue278_neg_separated_tension.py +271 -0
  246. package/md_cg/test_issue278_neg_window.py +294 -0
  247. package/md_cg/test_issue278_negative_layering.py +83 -0
  248. package/md_cg/test_issue279_signers_path_guard.py +652 -0
  249. package/md_cg/test_issue284_skip_not_pass.py +291 -0
  250. package/md_cg/test_issue294_flywheel_sensitivity.py +573 -0
  251. package/md_cg/test_issue295_sleep_root_binding.py +589 -0
  252. package/md_cg/test_issue297_rewrite_sensitivity.py +454 -0
  253. package/md_cg/test_issue298_consistency_ledger_sensitivity.py +421 -0
  254. package/md_cg/test_issue301_baseline_corpus_gate.py +245 -0
  255. package/md_cg/test_issue303_design_boundaries.py +262 -0
  256. package/md_cg/test_issue313_writelimit_sensitivity.py +404 -0
  257. package/md_cg/test_issue315_merge_branch_sensitivity.py +384 -0
  258. package/md_cg/test_issue315_sensitivity_passthrough_guard.py +514 -0
  259. package/md_cg/test_issue319_tenant_unregistered_bypass.py +257 -0
  260. package/md_cg/test_issue320_vstate_requires_verify.py +279 -0
  261. package/md_cg/test_issue321_mcp_deep_json.py +305 -0
  262. package/md_cg/test_issue322_node_write_lock.py +727 -0
  263. package/md_cg/test_issue323_aggregate_segment_boundary.py +365 -0
  264. package/md_cg/test_issue324_write_size_fields.py +215 -0
  265. package/md_cg/test_issue325_audit_poison.py +230 -0
  266. package/md_cg/test_issue326_append_jsonl_oserror.py +464 -0
  267. package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -0
  268. package/md_cg/test_issue326_key_perms.py +355 -0
  269. package/md_cg/test_issue326_twin_parser_parity.py +323 -0
  270. package/md_cg/test_issue326_vm_call_frame.py +569 -0
  271. package/md_cg/test_issue345_writelimit_effective_sens.py +234 -0
  272. package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -0
  273. package/md_cg/test_issue346_review_overwrite_guard.py +509 -0
  274. package/md_cg/test_issue348_insight_verify_op.py +200 -0
  275. package/md_cg/test_issue348_merge_rmw_lock.py +345 -0
  276. package/md_cg/test_issue350_casefold_collision.py +236 -0
  277. package/md_cg/test_issue351_ingest_root_fallback.py +202 -0
  278. package/md_cg/test_issue352_imgaudit.py +418 -0
  279. package/md_cg/test_issue352_pathsjson.py +537 -0
  280. package/md_cg/test_issue352_refindex.py +412 -0
  281. package/md_cg/test_issue352_rollback.py +604 -0
  282. package/md_cg/test_issue352_vstateinherit.py +196 -0
  283. package/md_cg/test_issue354_p1_writelimit_type.py +522 -0
  284. package/md_cg/test_issue354_p2_datapath_clock.py +381 -0
  285. package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -0
  286. package/md_cg/test_issue354_rust_index_containment.py +571 -0
  287. package/md_cg/test_issue354_rust_wal_containment.py +606 -0
  288. package/md_cg/test_issue358_truncation_observable.py +247 -0
  289. package/md_cg/test_issue360_tidy_private_excerpt.py +196 -0
  290. package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -0
  291. package/md_cg/test_issue362_neg_condition.py +468 -0
  292. package/md_cg/test_issue363_lexical_idf.py +275 -0
  293. package/md_cg/test_issue370_readcache_assembly.py +488 -0
  294. package/md_cg/test_issue375_state_slot_retire.py +325 -0
  295. package/md_cg/test_issue376_heartbeat_failed.py +293 -0
  296. package/md_cg/test_issue377_layer_protect_stamp.py +324 -0
  297. package/md_cg/test_issue378_protected_floor.py +310 -0
  298. package/md_cg/test_issue383_state_event_write_gate.py +462 -0
  299. package/md_cg/test_issue397_sens_eff_wiring.py +435 -0
  300. package/md_cg/test_issue398_toolface_op_parity.py +511 -0
  301. package/md_cg/test_issue399_hive_json_shape.py +317 -0
  302. package/md_cg/test_issue400_hive_dirty_args.py +450 -0
  303. package/md_cg/test_issue401_wisdom_codegen.py +295 -0
  304. package/md_cg/test_issue404_heal_receipt.py +390 -0
  305. package/md_cg/test_issue405_git_pathspec.py +333 -0
  306. package/md_cg/test_issue408_eface_registry.py +291 -0
  307. package/md_cg/test_issue409_compact_index_completeness.py +509 -0
  308. package/md_cg/test_issue410_verify_demote_write.py +366 -0
  309. package/md_cg/test_issue411_scrub_idempotence.py +575 -0
  310. package/md_cg/test_issue412_discipline_producer_parity.py +312 -0
  311. package/md_cg/test_issue413_preimage_failclosed.py +239 -0
  312. package/md_cg/test_issue413_preimage_twin.py +340 -0
  313. package/md_cg/test_issue414_write_args_parity.py +613 -0
  314. package/md_cg/test_issue415_deps_gate_probe.py +358 -0
  315. package/md_cg/test_issue416_entry_key_subset.py +525 -0
  316. package/md_cg/test_issue417_twophase_merge_landed.py +423 -0
  317. package/md_cg/test_issue418_identity_role_type_gate.py +188 -0
  318. package/md_cg/test_issue418_role_type_gate.py +265 -0
  319. package/md_cg/test_issue419_pii_redaction.py +642 -0
  320. package/md_cg/test_issue424_merge_confirm_sensitivity.py +381 -0
  321. package/md_cg/test_issue425_ingest_library_path.py +331 -0
  322. package/md_cg/test_issue426a_dedup_receipt.py +242 -0
  323. package/md_cg/test_issue426b_root_clash_invisible.py +270 -0
  324. package/md_cg/test_issue426c_enqueue_not_silent.py +346 -0
  325. package/md_cg/test_issue426e_restore_overwrite_live.py +310 -0
  326. package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -0
  327. package/md_cg/test_issue427_serve_status_shape.py +191 -0
  328. package/md_cg/test_issue431_432_468_protect_single_point.py +322 -0
  329. package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -0
  330. package/md_cg/test_issue434_435_float_gate.py +90 -0
  331. package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -0
  332. package/md_cg/test_issue43_default_policy.py +874 -865
  333. package/md_cg/test_issue443a_state_event_length_gate.py +94 -0
  334. package/md_cg/test_issue443b_audit_record_length_gate.py +83 -0
  335. package/md_cg/test_issue443c_self_state_imgskill_length_gate.py +77 -0
  336. package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -0
  337. package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -0
  338. package/md_cg/test_issue446_453_403_verified_evidence.py +382 -0
  339. package/md_cg/test_issue446_is_encrypted_envelope.py +166 -0
  340. package/md_cg/test_issue447_recent_sens_failclosed.py +270 -0
  341. package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -0
  342. package/md_cg/test_issue450_writegate_unknown_sens.py +203 -0
  343. package/md_cg/test_issue451_audit_actor.py +313 -0
  344. package/md_cg/test_issue452_ingest_root_hint.py +238 -0
  345. package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -0
  346. package/md_cg/test_issue463_ghost_prune_guard.py +259 -0
  347. package/md_cg/test_issue466_basis_registry.py +469 -0
  348. package/md_cg/test_issue467_identity_sensitivity.py +204 -0
  349. package/md_cg/test_issue467_identity_sensitivity_parity.py +361 -0
  350. package/md_cg/test_issue469_scrub_override_passthrough.py +126 -0
  351. package/md_cg/test_issue470_wal_event_durable.py +427 -0
  352. package/md_cg/test_issue471_identity_readable.py +137 -0
  353. package/md_cg/test_issue472_audit_chain.py +91 -0
  354. package/md_cg/test_issue472_prune_ledger.py +106 -0
  355. package/md_cg/test_issue472_rollback_degraded.py +82 -0
  356. package/md_cg/test_issue491_compose_sandbox.py +460 -0
  357. package/md_cg/test_issue492_dup_readout_opt_in.py +292 -0
  358. package/md_cg/test_issue498_merge_trace_stale_index.py +280 -0
  359. package/md_cg/test_issue511_scoreboard_bad_files.py +256 -0
  360. package/md_cg/test_issue521_rotate_lock_suffix.py +110 -0
  361. package/md_cg/test_issue522_review_record_sensitivity.py +267 -0
  362. package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -0
  363. package/md_cg/test_issue525_read_null_semantics.py +52 -0
  364. package/md_cg/test_issue538_hook_condition_space.py +97 -0
  365. package/md_cg/test_issue540_path_guard_whitespace.py +126 -0
  366. package/md_cg/test_issue541_tidy_protect_gate.py +141 -0
  367. package/md_cg/test_issue542_tenant_root_same.py +153 -0
  368. package/md_cg/test_issue543_issue_authorization.py +147 -0
  369. package/md_cg/test_issue544_sandbox_reflection_gate.py +175 -0
  370. package/md_cg/test_issue545_charset_vs_size_diagnostic.py +77 -0
  371. package/md_cg/test_issue546_ledger_concurrency.py +81 -0
  372. package/md_cg/test_issue547_append_jsonl_whitelist.py +115 -0
  373. package/md_cg/test_issue547_audit_durable.py +96 -0
  374. package/md_cg/test_issue549_durable_append.py +143 -0
  375. package/md_cg/test_issue557_r1_credential_value_forms.py +76 -0
  376. package/md_cg/test_issue557_r2_write_entries_size_gate.py +84 -0
  377. package/md_cg/test_issue557_r3_pem_block_redaction.py +75 -0
  378. package/md_cg/test_issue559_refine_prefix.py +70 -0
  379. package/md_cg/test_issue561_pending_review.py +109 -0
  380. package/md_cg/test_issue562_retired_faces.py +150 -0
  381. package/md_cg/test_issue66_scrub_semantics.py +107 -0
  382. package/md_cg/test_issue67_scrub_control_pool.py +233 -0
  383. package/md_cg/test_issue69_audit_boundary.py +129 -0
  384. package/md_cg/test_issue71_queue_backlog.py +377 -0
  385. package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -0
  386. package/md_cg/test_issue73_doc_chapter_refs.py +326 -0
  387. package/md_cg/test_issue75_neural_failure.py +582 -0
  388. package/md_cg/test_issue78_export_concurrency.py +334 -0
  389. package/md_cg/test_issue80_residual_flush.py +169 -0
  390. package/md_cg/test_issue82_credential_rules.py +271 -0
  391. package/md_cg/test_issue82_layer_and_size.py +120 -0
  392. package/md_cg/test_issue84_portability.py +95 -0
  393. package/md_cg/test_issue85_export_overwrite.py +113 -0
  394. package/md_cg/test_issue86_runtime_revoke.py +110 -0
  395. package/md_cg/test_issue87_default_sensitivity_single.py +201 -0
  396. package/md_cg/test_issue88_conclusion_face.py +280 -0
  397. package/md_cg/test_issue88_execution_conflicts.py +87 -0
  398. package/md_cg/test_issue89_zero_score.py +169 -0
  399. package/md_cg/test_issue89a_score_cap_tiebreak.py +279 -0
  400. package/md_cg/test_issue91_verifier_sandbox.py +314 -0
  401. package/md_cg/test_issue91_verifier_timeout.py +528 -0
  402. package/md_cg/test_issue93_remember_redaction.py +282 -0
  403. package/md_cg/test_issue96_key_fallback.py +102 -0
  404. package/md_cg/test_issue98_tenant_root.py +104 -0
  405. package/md_cg/test_jsonl_commit_watermark.py +221 -0
  406. package/md_cg/test_live_validity_cache.py +256 -0
  407. package/md_cg/test_logref.py +1141 -1109
  408. package/md_cg/test_mcp_call_argument_types.py +99 -0
  409. package/md_cg/test_md_access_parity.py +330 -255
  410. package/md_cg/test_md_cg_no_hive_dep.py +140 -0
  411. package/md_cg/test_memory_rsi.py +215 -0
  412. package/md_cg/test_memory_rsi_adversarial.py +91 -0
  413. package/md_cg/test_mode_parity.py +1413 -1393
  414. package/md_cg/test_mutation_residue.py +120 -0
  415. package/md_cg/test_mutation_rollback.py +835 -805
  416. package/md_cg/test_n123_derive_expiry_chain.py +210 -205
  417. package/md_cg/test_n130_verify_falsified_protect.py +185 -185
  418. package/md_cg/test_n209_verify_write_face_gates.py +466 -461
  419. package/md_cg/test_n230_dirty_replay.py +52 -9
  420. package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
  421. package/md_cg/test_neg_coverage_budget.py +500 -0
  422. package/md_cg/test_neg_tail_honesty.py +733 -663
  423. package/md_cg/test_p0.py +16 -2
  424. package/md_cg/test_p14_root_fallback.py +373 -0
  425. package/md_cg/test_p1x_ref_root.py +47 -10
  426. package/md_cg/test_p21_tokens.py +294 -293
  427. package/md_cg/test_p24_evidence.py +232 -227
  428. package/md_cg/test_p26_refindex.py +20 -3
  429. package/md_cg/test_p27_docindex.py +792 -777
  430. package/md_cg/test_p28_refcheck.py +1053 -1032
  431. package/md_cg/test_p29_session_ingest_export.py +6 -0
  432. package/md_cg/test_p2_mcp.py +474 -453
  433. package/md_cg/test_p2_six_elements.py +476 -463
  434. package/md_cg/test_p3.py +370 -261
  435. package/md_cg/test_p33_ccg_wiring.py +300 -293
  436. package/md_cg/test_p3_legacy_closure.py +438 -433
  437. package/md_cg/test_p40_refine_worklist.py +244 -241
  438. package/md_cg/test_p47_session_view.py +25 -9
  439. package/md_cg/test_p4_freshness.py +781 -774
  440. package/md_cg/test_p83_jsonrpc_parse_error.py +124 -0
  441. package/md_cg/test_p9c_dedup_hints.py +288 -283
  442. package/md_cg/test_policy_required_ccg.py +444 -432
  443. package/md_cg/test_postings_digest_memo.py +139 -0
  444. package/md_cg/test_predict_beta.py +138 -135
  445. package/md_cg/test_qmd.py +500 -0
  446. package/md_cg/test_query_excerpt.py +209 -0
  447. package/md_cg/test_reach.py +456 -378
  448. package/md_cg/test_reach_snapshot_admission.py +218 -0
  449. package/md_cg/test_readcache_prodpath.py +4 -0
  450. package/md_cg/test_recall_face_guards.py +854 -812
  451. package/md_cg/test_recall_metadata.py +422 -0
  452. package/md_cg/test_retr_s5.py +312 -213
  453. package/md_cg/test_retr_score_once.py +14 -2
  454. package/md_cg/test_review_cli_attribution.py +11 -7
  455. package/md_cg/test_review_queue_batch.py +123 -0
  456. package/md_cg/test_review_queue_index.py +130 -0
  457. package/md_cg/test_rrf_candidates.py +292 -0
  458. package/md_cg/test_rrf_qualified_topk.py +214 -0
  459. package/md_cg/test_rrf_unique_votes.py +218 -0
  460. package/md_cg/test_s1_ast_judge.py +110 -0
  461. package/md_cg/test_scrub_ledger.py +413 -0
  462. package/md_cg/test_security_audit_b26.py +34 -3
  463. package/md_cg/test_security_audit_v21.py +1 -2
  464. package/md_cg/test_sensitivity_predicate_single_point.py +184 -0
  465. package/md_cg/test_session5_identity.py +93 -0
  466. package/md_cg/test_session_attribution_consistency.py +813 -0
  467. package/md_cg/test_session_identity_tristate.py +486 -0
  468. package/md_cg/test_sleep_p1.py +35 -3
  469. package/md_cg/test_state_event_op.py +521 -514
  470. package/md_cg/test_state_projection_streaming.py +136 -0
  471. package/md_cg/test_stg_interval_candidates.py +280 -0
  472. package/md_cg/test_stg_time_blocks.py +239 -0
  473. package/md_cg/test_stgidx_index_parity.py +1024 -1020
  474. package/md_cg/test_subgraph_bounded_bfs.py +227 -0
  475. package/md_cg/test_sustain_bounded.py +652 -624
  476. package/md_cg/test_task_memory.py +394 -0
  477. package/md_cg/test_text_projection_mutations.py +101 -0
  478. package/md_cg/test_time_anchor.py +421 -0
  479. package/md_cg/test_time_core_lint.py +1024 -975
  480. package/md_cg/test_tool_face.py +200 -189
  481. package/md_cg/test_trust_bounded_frontier.py +248 -0
  482. package/md_cg/test_trust_edge_reachability.py +405 -0
  483. package/md_cg/test_w5_path_fingerprint.py +381 -0
  484. package/md_cg/test_w7_redlines.py +659 -0
  485. package/md_cg/test_write_return_shape.py +919 -0
  486. package/md_cg/test_writepipe.py +228 -226
  487. package/md_cg/timeanchor.py +534 -0
  488. package/md_cg/tokens.py +68 -5
  489. package/md_cg/tool_face.py +273 -260
  490. package/md_cg/trust.py +1114 -1030
  491. package/md_cg/twophase.py +303 -231
  492. package/md_cg/weights.py +642 -638
  493. package/md_cg/whitebox.py +536 -527
  494. package/md_cg/whitebox_kb/aeis_core/core.py +5026 -5026
  495. package/md_cg/whitebox_kb/wisdom/chat_engine.py +1504 -1506
  496. package/md_cg/whitebox_kb/wisdom/code_compose.py +876 -837
  497. package/md_cg/whitebox_kb/wisdom/compiler_code_units.py +3040 -3033
  498. package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1633
  499. package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -298
  500. package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1065
  501. package/md_cg/whitebox_kb/wisdom/csre.py +412 -264
  502. package/md_cg/whitebox_kb/wisdom/csre_session.py +97 -0
  503. package/md_cg/whitebox_kb/wisdom/md_access.py +490 -470
  504. package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -476
  505. package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -958
  506. package/md_cg/whitebox_kb/wisdom/navigate.py +249 -248
  507. package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2895
  508. package/md_cg/whitebox_kb/wisdom/neural_retrieve.py +100 -10
  509. package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4054
  510. package/md_cg/whitebox_kb/wisdom/test_ecosystem_assembly.py +158 -158
  511. package/md_cg/whitebox_kb/wisdom/test_safe_module_proxy.py +147 -0
  512. package/md_cg/whitebox_kb/wisdom/verifier.py +1918 -1297
  513. package/md_cg/writelimit.py +584 -390
  514. package/md_cg/writepipe.py +1337 -826
  515. package/package.json +14 -9
  516. package/scripts/workspace_index.py +328 -0
  517. package/src/bridge.ts +34 -11
  518. package/src/cli.ts +75 -65
  519. package/src/hooks.ts +932 -776
  520. package/src/index.ts +30 -7
  521. package/src/init.ts +362 -361
  522. package/src/lib/datapath.ts +419 -326
  523. package/src/lib/hook_audit.ts +42 -3
  524. package/src/lib/mdcg_client.ts +469 -461
  525. package/src/lib/mutual.ts +481 -432
  526. package/src/lib/prompt_safety.ts +178 -149
  527. package/src/lib/roleplay_web.ts +30 -2
  528. package/src/lib/token_store.ts +274 -52
  529. package/src/tools.ts +271 -223
  530. package/zcode/README.md +1 -1
  531. package/docs/GBrain/345/217/257/345/200/237/351/211/264/347/202/271_/347/201/265/346/236/242/350/220/275/347/202/271/344/272/244/346/216/245_20260919.md +0 -169
  532. package/docs/Pi/345/217/257/345/255/246/344/271/240/344/274/230/347/202/271_/347/201/265/346/236/242/345/244/247/350/204/221/346/224/271/350/277/233/344/272/244/346/216/245_20260915.md +0 -144
  533. package/docs/eval/AGI/344/270/203/347/273/264/350/257/204/345/210/206/346/212/245/345/221/212_md_cg_v1.0.md +0 -299
  534. package/docs/eval/AGI/344/270/203/347/273/264/350/257/204/345/210/206/346/212/245/345/221/212_md_cg_v2.0.md +0 -239
  535. package/docs/eval/AGI/344/270/203/347/273/264/350/257/204/345/210/206/346/212/245/345/221/212_/345/205/255/345/256/266/350/256/260/345/277/206/347/263/273/347/273/237/345/220/214/345/260/272/345/256/236/346/265/213_v3.1.md +0 -340
  536. package/docs/eval/DSH/346/227/245/345/277/227/347/264/242/345/274/225v2_/345/217/202/350/200/203dsh-TUI_v1.0.md +0 -248
  537. package/docs/eval/DSH/346/227/245/345/277/227/347/264/242/345/274/225/346/225/210/346/236/234/351/252/214/350/257/201_v1.0.md +0 -209
  538. package/docs/eval/DSH/347/253/257/347/274/272/351/231/267/344/270/223/351/241/271_v1.0.md +0 -254
  539. package/docs/eval/N225_/347/264/242/345/274/225/346/227/245/345/277/227/351/235/236/345/257/271/350/261/241/350/243/205/350/275/275/351/235/242/347/261/273/345/236/213/351/227/270_v1.0.md +0 -418
  540. package/docs/eval/P1b2_/350/257/273/351/235/242/344/273/243/351/231/205/344/277/256/345/244/215_v1.0.md +0 -124
  541. package/docs/eval/bench_lingshu_self/bench_self.py +0 -140
  542. package/docs/eval/bench_lingshu_self/self_bench_result.json +0 -404
  543. package/docs/eval/bench_lingshu_self//347/201/265/346/236/242/350/207/252/345/272/223/347/253/257/345/210/260/347/253/257/346/243/200/347/264/242/345/256/236/346/265/213_v1.0.md +0 -38
  544. package/docs/eval/cons200_/345/206/262/347/252/201/346/243/200/346/265/213/351/200/211/351/235/242_/345/256/236/346/226/275/350/256/260/345/275/225_v1.0.md +0 -223
  545. package/docs/eval/cons200_/345/206/262/347/252/201/346/243/200/346/265/213/351/200/211/351/235/242_/345/256/236/346/226/275/350/256/260/345/275/225_v1.1.md +0 -340
  546. package/docs/eval/issue43_/351/273/230/350/256/244/347/255/226/347/225/245/344/270/216/351/224/256/347/261/273/345/236/213/351/227/270_v1.1.md +0 -441
  547. package/docs/eval/issue43_/351/273/230/350/256/244/347/255/226/347/225/245/345/212/240/350/275/275/344/270/216/345/207/255/346/215/256/346/230/216/346/226/207/351/230/237/345/210/227_v1.0.md +0 -398
  548. package/docs/eval/issue50_/345/205/203/346/225/260/346/215/256/351/200/217/344/274/240/344/270/216/345/205/234/345/272/225_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -437
  549. package/docs/eval/issue50_/345/215/212/351/207/215/345/244/215/345/276/205/345/256/232/345/244/215/346/240/270_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -488
  550. package/docs/eval/issue50_/345/276/205/345/256/232/345/244/215/346/240/270/345/205/245/351/230/237_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -428
  551. package/docs/eval/issue50_/350/257/273/351/235/242/344/277/235/346/212/244/345/217/252/350/256/244/346/230/276/345/274/217/346/235/245/346/272/220_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -271
  552. package/docs/eval/issue50_/351/207/215/350/246/201/345/272/246/345/220/214/346/272/220/344/270/216/344/277/235/346/212/244/350/257/255/344/271/211_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -306
  553. package/docs/eval/issue51_/344/270/200/351/224/256/345/256/211/350/243/205/345/244/261/350/264/245_/345/275/222/345/261/236/345/210/244/345/256/232_v1.0.md +0 -58
  554. package/docs/eval/issue52_/346/235/241/344/273/266/345/205/210/350/241/214/344/270/216/346/210/252/346/226/255/345/217/257/350/247/202/346/265/213_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -360
  555. package/docs/eval/issue64_replay_check_/346/240/270/351/252/214/344/270/216/344/277/256/345/244/215_v1.0.md +0 -284
  556. package/docs/eval/issue65_session_recall_/344/274/232/350/257/235/351/232/224/347/246/273_/346/240/270/351/252/214/344/270/216/344/277/256/345/244/215_v1.0.md +0 -329
  557. package/docs/eval//344/270/211/346/241/243/350/207/252/346/262/273_/346/255/245/351/252/244/342/221/241/346/241/243/344/275/215/345/215/225/344/270/200/345/205/245/345/217/243_/350/220/275/347/240/201/350/256/260/345/275/225_v1.0.md +0 -651
  558. package/docs/eval//344/270/211/346/241/243/350/207/252/346/262/273_/346/255/245/351/252/244/342/221/242/345/217/230/346/233/264/345/215/225/344/270/216/345/233/236/346/273/232/345/216/237/350/257/255_/350/220/275/347/240/201/350/256/260/345/275/225_v1.0.md +0 -439
  559. package/docs/eval//344/270/211/346/241/243/350/207/252/346/262/273_/346/255/245/351/252/244/342/221/243/345/207/206/345/205/245/350/257/273/346/225/260_/350/220/275/347/240/201/350/256/260/345/275/225_v1.0.md +0 -1337
  560. package/docs/eval//344/270/211/346/241/243/350/207/252/346/262/273_/346/255/245/351/252/244/342/221/244/346/224/266/345/256/230/344/270/216/345/205/250/351/223/276/351/252/214/346/224/266_/350/220/275/347/240/201/350/256/260/345/275/225_v1.0.md +0 -1817
  561. package/docs/eval//344/270/215/345/217/257/351/235/240/346/200/247/350/220/275/345/234/260_P0_v1.0.md +0 -185
  562. package/docs/eval//344/274/230/345/214/226/347/254/254/344/270/200/346/211/271_/346/216/245/347/272/277/344/270/216/347/255/211/344/273/267/345/217/230/346/215/242_v1.0.md +0 -498
  563. package/docs/eval//344/274/230/345/214/226/347/254/254/344/270/211/346/211/271_/351/227/250/347/246/201/350/275/254/346/255/243/344/270/216/350/260/203/345/272/246/346/255/242/350/241/200/344/270/216/351/227/250/346/216/247/346/224/266/345/217/243_v1.0.md +0 -669
  564. package/docs/eval//344/274/230/345/214/226/347/254/254/344/272/214/346/211/271_/344/276/235/350/265/226/351/200/217/344/274/240/344/270/216/345/257/271/346/213/215/345/217/243/345/276/204/344/270/216/350/264/237/347/274/223/345/255/230_v1.0.md +0 -683
  565. package/docs/eval//345/207/272/350/264/247/351/235/242/345/206/222/347/203/237_/350/277/233/350/264/247/351/227/250/347/246/201_v1.0.md +0 -460
  566. package/docs/eval//345/217/221/345/270/20306_/344/270/200/351/224/256/351/205/215/347/275/256/344/270/216DSH0172_v1.0.md +0 -171
  567. package/docs/eval//345/217/221/345/270/20307_/345/244/226/351/203/250/346/212/245/345/221/212/345/233/233/346/211/271/344/277/256/345/244/215/344/270/216/346/217/222/344/273/266/351/235/242/345/212/240/345/233/272_v1.0.md +0 -209
  568. package/docs/eval//345/217/221/345/270/20308_/350/207/252/350/277/255/344/273/243/344/270/216/347/235/241/347/234/240_/345/233/276/346/243/200/347/264/242/350/267/257/344/270/216/346/235/203/351/207/215_v1.0.md +0 -97
  569. package/docs/eval//345/217/221/345/270/20309_/346/243/200/347/264/242/351/235/242/344/270/211/346/211/271/346/224/266/345/217/243_v1.0.md +0 -66
  570. package/docs/eval//345/217/221/345/270/20310_/350/257/273/351/235/242/346/215/237/345/235/217UTF8/345/256/266/346/227/217/346/224/266/345/217/243_v1.0.md +0 -71
  571. package/docs/eval//345/217/221/345/270/20311_/345/244/226/346/212/245/345/205/255/350/277/236/344/277/256/344/270/216/344/270/226/347/225/214/346/250/241/345/236/213/345/272/225/345/272/247_v1.0.md +0 -91
  572. package/docs/eval//345/217/221/345/270/20312_/344/270/226/347/225/214/346/250/241/345/236/213/345/212/237/350/203/275/347/253/257_v1.0.md +0 -108
  573. package/docs/eval//345/217/221/345/270/20313_/344/270/212/344/270/213/346/226/207/350/207/252/347/256/241/347/220/206/346/234/272/345/210/266_v1.0.md +0 -114
  574. package/docs/eval//345/217/221/345/270/20314_/350/272/253/344/275/223/303/227/350/204/221/347/273/204/345/220/210_v1.0.md +0 -160
  575. package/docs/eval//345/217/221/345/270/20315_/344/274/232/350/257/235/351/232/224/347/246/273/344/277/256/345/244/215/344/270/216/350/272/253/344/275/223/345/272/223/345/261/225/347/244/272_v1.0.md +0 -88
  576. package/docs/eval//345/256/236/351/252/214/346/226/271/346/241/210_/345/255/246/344/271/240/351/227/255/347/216/257AB/344/270/216/346/250/252/350/257/204_v1.0.md +0 -174
  577. package/docs/eval//345/275/222/344/270/200/345/261/202/347/274/272/347/234/201/347/277/273/345/205/263_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -458
  578. package/docs/eval//346/200/247/350/203/275/344/270/223/351/241/271_/345/206/267/346/237/245/350/257/242/344/270/216/345/206/205/345/255/230_v1.0.md +0 -218
  579. package/docs/eval//346/225/205/351/232/234/346/263/250/345/205/245/345/256/236/346/265/213_v1.0.md +0 -432
  580. package/docs/eval//346/250/252/350/257/204_/345/205/255/345/256/266100/351/242/230/344/270/255/350/213/261/345/217/214/346/237/245_v1.0.md +0 -223
  581. package/docs/eval//347/224/250/346/210/267/350/243/201/345/206/263/345/244/204/347/275/256_/347/254/254/344/270/200/346/211/271_v1.0.md +0 -195
  582. package/docs/eval//347/224/250/346/210/267/350/243/201/345/206/263/345/244/204/347/275/256_/347/254/254/344/272/214/346/211/271B6_/346/234/254/346/234/272/350/267/257/345/276/204/347/233/270/345/257/271/345/214/226_v1.0.md +0 -78
  583. package/docs/eval//347/253/257/345/210/260/347/253/257LoCoMoQA/345/220/214/345/217/243/345/276/204/345/257/271/347/205/247_v1.0.md +0 -100
  584. package/docs/eval//347/253/257/345/210/260/347/253/257/345/271/262/346/211/260/346/261/240/350/257/204/346/265/213_/347/241/256/345/256/232/346/200/247/350/243/201/345/206/263vsLLM_judge_v1.1.md +0 -197
  585. package/docs/eval//347/254/2543/345/261/202stg/347/273/223/346/236/204/347/264/242/345/274/225_/345/256/236/346/226/275/350/256/260/345/275/225_v1.0.md +0 -250
  586. package/docs/eval//347/254/254/344/270/211/346/226/271/351/252/214/350/257/201/346/212/245/345/221/212_LoCoMo_/347/201/265/346/236/242_.png +0 -0
  587. package/docs/eval//347/254/254/344/270/211/346/226/271/351/252/214/350/257/201/346/212/245/345/221/212_/347/201/265/346/236/242_vs_dejavu_/347/273/237/344/270/200/350/257/204/345/210/206_v7.md +0 -202
  588. package/docs/eval//347/274/226/347/240/201/351/235/242/345/211/215/347/275/256_/345/205/245/345/217/243/350/207/252/344/277/235/350/257/201UTF8/344/270/216/346/226/207/346/234/254open/345/256/210/345/215/253_v1.0.md +0 -646
  589. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v1.md +0 -156
  590. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v10.md +0 -210
  591. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v11.md +0 -227
  592. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v12.md +0 -203
  593. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v13.md +0 -233
  594. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v14.md +0 -191
  595. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v15.md +0 -213
  596. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v16.md +0 -214
  597. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v17.md +0 -199
  598. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v18.md +0 -183
  599. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v19.md +0 -207
  600. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v2.md +0 -156
  601. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v20.md +0 -283
  602. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v21.md +0 -224
  603. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v22.md +0 -223
  604. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v23.md +0 -293
  605. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v24.md +0 -303
  606. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v25.md +0 -230
  607. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v26.md +0 -196
  608. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v27.md +0 -259
  609. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v28.md +0 -217
  610. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v29.md +0 -270
  611. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v3.md +0 -152
  612. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v30.md +0 -231
  613. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v31.md +0 -281
  614. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v4.md +0 -128
  615. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v5.md +0 -114
  616. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v6.md +0 -192
  617. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v7.md +0 -187
  618. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v8.md +0 -207
  619. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v9.md +0 -203
  620. package/docs/eval//350/257/255/344/271/211/346/227/266/347/251/272/345/233/276/350/241/245/345/205/250_P1_/346/212/275/345/217/226/345/231/250v4_/344/270/211/347/216/207/350/257/273/346/225/260_v1.0.md +0 -231
  621. package/docs/eval//351/262/270/345/250/230/344/270/203/347/261/273/347/212/266/346/200/201/350/277/275/350/270/252_v1.0.md +0 -176
  622. package/docs/hive//344/270/273/344/273/243/347/220/206/345/255/220/344/273/243/347/220/206/350/256/260/345/277/206/346/236/266/346/236/204/350/256/276/350/256/241.md +0 -385
  623. package/docs/hive//344/273/244/347/211/214/344/270/216/350/247/222/350/211/262/346/235/203/350/201/214/345/210/206/347/246/273_v0.1.md +0 -165
  624. package/docs/hive//344/273/244/347/211/214/350/257/255/344/271/211/344/277/256/346/255/243_/344/273/262/350/243/201/344/275/215_v0.1.md +0 -76
  625. package/docs/hive//345/244/232/347/253/257harness/351/200/232/344/277/241/345/245/221/347/272/246_v0.1.md +0 -42
  626. package/docs/hive//345/255/220/344/273/243/347/220/206/351/205/215/347/275/256/346/240/207/345/207/206_v0.5.md +0 -236
  627. package/docs/hive//345/256/211/345/205/250/345/256/241/350/256/241/345/256/236/351/224/232_v0.1.md +0 -202
  628. package/docs/hive//346/243/200/347/264/242/346/224/266/346/225/233/345/256/236/346/265/213/344/270/216S1b/350/256/276/350/256/241_v0.1.md +0 -43
  629. package/docs/hive//346/243/200/347/264/242/347/256/227/346/263/225/345/217/243/345/276/204/345/257/271/347/205/247_v0.1.md +0 -210
  630. package/docs/hive//346/243/200/347/264/242/350/267/257/345/276/204/344/270/216/350/256/244/347/237/245/347/273/223/346/236/204/345/245/221/347/272/246_v0.1.md +0 -152
  631. package/docs/hive//350/234/202/345/267/242M6_ingest/345/256/236/346/226/275/350/256/241/345/210/222_v0.1.md +0 -47
  632. package/docs/hive//350/234/202/345/267/242/345/217/214/345/256/236/344/276/213/344/272/222/351/252/214_/350/256/276/350/256/241/345/256/232/347/250/277.md +0 -519
  633. package/docs/hive//350/234/202/345/267/242/345/267/245/344/275/234/350/256/260/345/277/206_/351/241/271/347/233/256/350/256/241/345/210/222.md +0 -85
  634. package/docs/hive//350/234/202/345/267/242/350/256/276/350/256/241_/347/220/206/350/256/272/345/257/271/351/275/220_v0.1.md +0 -191
  635. package/docs/hive//350/234/202/345/267/242/350/277/255/344/273/243_/345/256/217/350/247/202/344/270/216/347/276/244/344/275/223/350/260/203/345/272/246_v0.1.md +0 -90
  636. package/docs/images/lingshu-moonlight-covenant-poster-preview.jpg +0 -0
  637. package/docs/images/lingshu-moonlight-covenant-poster.png +0 -0
  638. package/docs/mdcg/D_meta_/345/267/245/347/250/213/345/214/226/346/226/271/346/241/210_v0.2.md +0 -216
  639. package/docs/mdcg/README/350/257/246/347/273/206/347/211/210_v0.4.10.md +0 -746
  640. package/docs/mdcg/guardrail-charter.md +0 -164
  641. package/docs/mdcg/lingshu_tutorial.html +0 -14956
  642. package/docs/mdcg/memory-assessment.html +0 -923
  643. package/docs/mdcg/memory_score.html +0 -213
  644. package/docs/mdcg/memory_score.png +0 -0
  645. package/docs/mdcg/release_v0.3.0.md +0 -36
  646. package/docs/mdcg/release_v0.4.11.md +0 -49
  647. package/docs/mdcg/release_v0.4.5.md +0 -55
  648. package/docs/mdcg/tool_table_v0.3.0.md +0 -117
  649. package/docs/mdcg//344/270/226/347/225/214/346/250/241/345/236/213/345/212/237/350/203/275/347/253/257_/344/275/277/347/224/250/344/270/216/350/277/220/347/273/264_v1.0.md +0 -384
  650. package/docs/mdcg//344/273/243/347/240/201/350/257/204/345/256/241/344/270/216/346/235/241/344/273/266/345/214/226/346/263/250/351/207/212_/345/245/221/347/272/246_v0.1.md +0 -82
  651. package/docs/mdcg//345/205/250/345/272/223/344/273/243/347/240/201/350/257/204/345/256/241/344/270/216/346/235/241/344/273/266/345/214/226/346/263/250/351/207/212_/350/256/241/345/210/222_v0.1.md +0 -600
  652. package/docs/mdcg//345/212/237/350/203/275/350/260/203/347/224/250/346/230/240/345/260/204/350/241/250_v0.1.md +0 -266
  653. package/docs/mdcg//345/215/225/345/205/203/350/207/252/346/210/221/351/224/232/347/202/271_/347/263/273/347/273/237/346/217/220/347/244/272/350/257/215/346/240/207/345/207/206_v0.3.md +0 -275
  654. package/docs/mdcg//345/217/221/345/270/203/351/227/250/347/246/201/351/223/276_v0.1.md +0 -134
  655. package/docs/mdcg//346/272/220/347/240/201/347/272/247/346/236/266/346/236/204/345/256/241/350/256/241_GPT/346/211/271/350/257/204/345/257/271/347/205/247_v1.0.md +0 -130
  656. package/docs/mdcg//347/201/265/346/236/24282/345/267/245/345/205/267_/345/212/237/350/203/275/346/225/264/347/220/206/344/270/216/350/277/201/347/247/273/346/230/240/345/260/204_v0.1.md +0 -278
  657. package/docs/mdcg//347/201/265/346/236/242MCP/345/267/245/345/205/267/346/200/273/350/241/250_v3.4.md +0 -164
  658. package/docs/mdcg//347/201/265/346/236/242_/350/207/252/346/210/221/345/261/202/345/256/232/344/271/211.md +0 -43
  659. package/docs/mdcg//347/201/265/346/236/242/344/270/211/345/261/202/346/213/206/345/210/206/350/247/204/345/210/222_v0.1.md +0 -181
  660. package/docs/mdcg//347/201/265/346/236/242/350/256/260/345/277/206/345/212/250/350/257/215/345/215/217/350/256/256_v1.0-draft.md +0 -177
  661. package/docs/mdcg//347/216/257/344/272/214_/347/231/275/347/256/261/345/241/253/345/205/205/346/265/201/346/260/264/347/272/277_v0.1.md +0 -31
  662. package/docs/mdcg//347/235/241/347/234/240/345/221/250/346/234/237_/350/277/220/347/273/264/345/211/215/346/217/220/344/270/216/347/273/264/346/212/244/346/214/207/345/215/227_v1.0.md +0 -337
  663. package/docs/mdcg//347/274/272/345/217/243/345/215/225_P0/346/224/266/345/217/243_v0.1.md +0 -270
  664. package/docs/mdcg//350/256/244/347/237/245/345/233/276_G4-G8/347/274/272/345/217/243/350/243/201/345/256/232/345/215/225_v0.1.md +0 -491
  665. package/docs/mdcg//350/256/244/347/237/245/345/233/276_MD/347/233/256/345/275/225/346/226/271/346/241/210_v0.1.md +0 -552
  666. package/docs/mdcg//350/256/244/347/237/245/345/233/276_/346/235/241/344/273/266/347/251/272/351/227/264/345/220/210/346/210/220/344/270/216/347/224/237/346/225/210/346/235/241/344/273/266/345/217/243/345/276/204_v0.1.md +0 -340
  667. package/docs/mdcg//350/256/244/347/237/245/345/233/276_/347/264/242/345/274/225/344/270/216/345/267/245/347/250/213/350/247/204/350/214/203/345/214/226_/350/256/241/345/210/222_v0.1.md +0 -624
  668. package/docs/mdcg//350/256/244/347/237/245/345/233/276/344/275/234/344/270/272/350/256/260/345/277/206/346/223/215/344/275/234/347/263/273/347/273/237_/350/257/204/344/274/260/344/270/216/350/267/257/347/272/277_v0.1.md +0 -94
  669. package/docs/mdcg//350/256/260/345/277/206/346/223/215/344/275/234/347/263/273/347/273/237_MdCGOS/344/270/216MCP/346/216/245/345/205/245_v0.1.md +0 -281
  670. package/docs/mdcg//350/267/250/347/253/257/351/252/214/350/257/201/344/270/216/345/220/214/346/255/245/345/215/217/350/256/256_v0.1.md +0 -93
  671. package/docs/plans/GridWorld/346/234/200/345/260/217/351/227/255/347/216/257/350/247/204/346/240/274_v0.1.md +0 -176
  672. package/docs/plans/stg/346/235/241/344/273/266/345/214/226/344/270/216/347/273/223/346/236/204/347/264/242/345/274/225_/350/256/276/350/256/241_v0.1.md +0 -124
  673. package/docs/plans//345/205/250/344/270/255/346/226/207/347/274/226/347/240/201/344/270/216/350/234/202/345/267/242/344/273/273/345/212/241/346/240/207/350/257/206/345/245/221/347/272/246_v2.0.md +0 -220
  674. package/docs/plans//345/221/275/345/220/215/346/262/273/347/220/206_/351/241/271/347/233/256/350/256/241/345/210/222.md +0 -81
  675. package/docs/plans//345/244/232/344/270/273/344/275/223/344/270/226/347/225/214/346/250/241/345/236/213_/345/257/271/351/275/220/350/257/204/344/274/260/344/270/216/350/220/275/345/234/260/350/256/276/350/256/241_v0.1.md +0 -141
  676. package/docs/plans//347/201/265/346/236/242/350/272/253/344/275/223/303/227/350/204/221_/344/270/226/347/225/214/346/250/241/345/236/213/345/257/271/346/216/245/350/256/276/350/256/241_v0.1.md +0 -119
  677. package/docs/plans//347/234/237/346/272/220/347/264/242/345/274/225_/351/200/232/347/224/250/346/234/272/345/210/266_v0.3.md +0 -227
  678. package/docs/plans//347/235/241/347/234/240/344/270/216/350/207/252/350/277/255/344/273/243_/345/212/237/350/203/275/344/274/230/345/214/226/350/256/276/350/256/241_v0.4.md +0 -547
  679. package/docs/plans//347/237/245/350/257/206/345/233/276/350/260/261/351/241/271/347/233/256_/344/272/244/346/216/245/346/226/207/346/241/243_20260914.md +0 -308
  680. package/docs/plans//350/234/202/345/267/242/346/250/241/345/236/213/345/257/206/351/222/245/351/205/215/347/275/256/351/235/242_v1.0.md +0 -385
  681. package/docs/plans//350/256/260/345/277/206/350/207/252/345/244/204/347/220/206/344/270/211/346/241/243/350/207/252/346/262/273_/350/256/276/350/256/241_v0.2.md +0 -207
  682. package/docs/plans//350/257/255/344/271/211/346/227/266/347/251/272/345/233/276/350/241/245/345/205/250_/344/270/226/347/225/214/346/250/241/345/236/213/345/212/237/350/203/275/347/253/257_/350/256/276/350/256/241_v0.1.md +0 -107
  683. package/docs/promo/bilibili-cover.jpg +0 -0
  684. package/docs/promo/promo-visual-1.png +0 -0
  685. package/docs/promo/promo-visual-2.png +0 -0
  686. package/docs/promo/promo-visual-3.png +0 -0
  687. package/docs/promo/promo-visual-4.png +0 -0
  688. package/docs/promo/promo-visual-5.png +0 -0
  689. package/docs/swarm//350/234/202/347/276/244/344/272/222/350/201/224_v0.1.md +0 -704
  690. package/docs/swarm//350/234/202/347/276/244/345/220/214/351/224/231/346/243/200/346/265/213/345/256/236/351/252/214/345/215/217/350/256/256_v0.1.md +0 -242
  691. package/docs/swarm//350/234/202/347/276/244/345/244/232/346/231/272/350/203/275/344/275/223_/344/275/277/347/224/250/346/212/245/345/221/212_20260913.md +0 -68
  692. package/docs/swarm//350/234/202/347/276/244/345/244/232/346/231/272/350/203/275/344/275/223_/345/212/237/350/203/275/350/257/264/346/230/216_v0.6.md +0 -130
  693. package/docs/swarm//350/234/202/347/276/244/347/233/262/345/214/272/346/240/207/350/256/260_v1.0.md +0 -89
  694. package/docs/theory//344/270/215/345/217/257/351/235/240/345/256/232/347/220/206/344/270/216/345/244/261/346/225/210/344/274/230/345/205/210/346/241/206/346/236/266_v0.3.md +0 -365
  695. package/docs/theory//344/277/241/346/201/257/345/267/256/344/270/272/344/273/200/344/271/210/345/277/205/347/204/266/345/255/230/345/234/250/344/270/224/350/207/252/347/204/266/346/211/251/345/244/247.md +0 -239
  696. package/docs/theory//345/215/225/347/272/277/347/250/213/344/270/216/346/263/250/346/204/217/345/212/233/351/233/206/344/270/255_/346/227/240/344/272/211/350/256/256/347/220/206/350/256/272/346/226/207/346/241/243_v1.0.md +0 -129
  697. package/docs/theory//345/271/266/345/217/221/345/277/205/347/204/266/346/200/247/347/220/206/350/256/272_v0.2.md +0 -134
  698. package/docs/theory//346/231/272/350/203/275/347/232/204/345/205/254/347/220/206/345/214/226/345/237/272/347/237/263.md +0 -789
  699. package/docs/theory//346/231/272/350/203/275/347/232/204/350/256/244/347/237/245/350/277/207/347/250/213.md +0 -379
  700. package/docs/theory//346/231/272/350/203/275/350/256/2723.4.md +0 -5260
  701. package/docs/theory//346/246/202/345/277/265/345/210/206/345/261/202/345/257/271/351/275/220/350/241/250_v0.1.md +0 -145
  702. package/docs/theory//347/220/206/350/256/272_/346/234/272/345/210/266_/344/273/243/347/240/201_/345/256/236/351/252/214_/347/274/272/345/217/243/347/237/251/351/230/265_v0.1.md +0 -144
  703. package/docs/theory//347/220/206/350/256/272/344/273/223/346/213/206/345/210/206/344/270/216/347/231/275/347/256/261/347/237/245/350/257/206/345/272/223/345/206/205/350/277/201_v0.1.md +0 -198
  704. package/docs/theory//347/231/275/347/256/261/346/231/272/350/203/275/346/230/257/344/273/200/344/271/210/357/274/237.md +0 -812
  705. package/docs/theory//347/231/275/347/256/261/346/231/272/350/203/275/347/263/273/345/210/227/302/267/347/254/254/344/272/224/347/257/207/357/274/232/350/256/251AI/347/234/237/346/255/243/350/243/205/344/270/212/350/256/260/345/277/206.md +0 -421
  706. package/docs/theory//350/256/244/347/237/245/344/273/243/347/220/206/344/270/216/346/224/266/346/225/233/347/273/223/346/236/204_/346/235/241/344/273/266/350/256/272/351/207/215/346/236/204_v0.1.md +0 -221
  707. package/docs/world_model//344/270/226/347/225/214/346/250/241/345/236/213_/347/245/236/347/273/217/347/275/221/347/273/234/345/272/225/345/261/202/346/236/266/346/236/204_v1.0.md +0 -237
  708. package/docs//344/270/215/345/217/257/351/235/240/346/200/247/347/220/206/350/256/272_v0.1.md +0 -343
  709. package/docs//345/217/221/345/270/203/344/273/266/345/233/236/346/272/257/350/257/264/346/230/216_v0.1.md +0 -82
  710. package/docs//345/255/230/347/256/227/344/270/200/344/275/223/346/236/266/346/236/204/345/255/246/344/271/240_/345/244/226/351/203/250/347/220/206/350/256/272/345/257/271/347/205/247/344/270/216/350/267/257/347/272/277/344/272/244/346/216/245_20260916.md +0 -98
  711. package/docs//345/255/230/347/256/227/344/270/200/344/275/223/347/245/236/347/273/217/347/275/221/347/273/234/346/236/266/346/236/204_/346/200/273/347/272/262/344/270/216/344/272/244/346/216/245_20260916.md +0 -109
  712. package/docs//347/201/265/346/236/242/350/207/252/346/210/221/346/224/271/350/277/233/345/267/245/344/275/234/350/256/241/345/210/222_/345/244/226/351/203/250/347/240/224/347/251/266/347/263/273/345/210/227/345/220/270/346/224/266_v1_20260919.md +0 -286
  713. package/docs//347/231/275/347/256/261/345/214/226/347/272/262/351/242/206_/344/270/211/351/241/271/347/233/256/345/255/246/344/271/240/346/200/273/346/236/266/346/236/204/344/270/216Ghidra/344/272/244/346/216/245_20260916.md +0 -104
  714. package/docs//347/247/244_/350/256/260/345/277/206/347/263/273/347/273/237/350/257/204/346/265/213/350/247/204/350/214/203_v2.1.md +0 -225
  715. package/docs//350/256/260/345/277/206/347/263/273/347/273/237/345/215/263/347/231/275/347/256/261/345/214/226/346/231/272/350/203/275_/346/236/266/346/236/204/345/257/271/347/205/247_v0.2.md +0 -121
  716. package/docs//350/256/260/345/277/206/347/263/273/347/273/237/345/215/263/347/231/275/347/256/261/345/214/226/346/231/272/350/203/275_/346/236/266/346/236/204/345/257/271/347/205/247_v0.3.md +0 -117
  717. package/docs//350/256/260/345/277/206/350/257/204/345/256/241/347/263/273/347/273/237_/347/253/213/351/241/271/350/256/276/350/256/241/344/270/216/346/226/275/345/267/245/344/272/244/346/216/245_20260915.md +0 -183
@@ -0,0 +1,1444 @@
1
+ # -*- coding: utf-8 -*-
2
+ r"""图像 Skill(`md_cg/imgskill.py`)守卫 · 契约真源 `docs/eval/图像Skill_接口契约_设计_v0.1.md` §五/§六/§九。
3
+
4
+ 断言面(G1–G7 **+ X 面(新增 9 op)**,逐条**能红**并附**定点变异自证**:注入退化 →
5
+ 恰好打中预期红项、不连坐):
6
+
7
+ * **G1 同输入幂等**(§九-#6 像素级门)——同输入同参两次 → **逐像素相等**
8
+ (读回比对;**不比字节**——位级不是门,字节读数只作信息项打印)。
9
+ * **G2 中文路径可用**(§3.5 + 摸底稿 A3)——中文目录名 + 中文文件名跑 resize+convert 全链。
10
+ * **G3 错误态可判别**(§3.3)——造 4 类必需样例(`E_NOINPUT`/`E_BAD_PARAM`/
11
+ `E_PATH_OUT_OF_SCOPE`/`E_TOOL_MISSING`)+ 4 类追加(`E_DECODE`/`E_UNSUPPORTED_OP`/
12
+ `E_LEVEL_DOWNGRADE`/`E_UNSUPPORTED_FORMAT`),断言**指定错误码**(不靠消息文本)。
13
+ (本批起 `crop` 已实现 → 该项的 `E_UNSUPPORTED_OP` 例改用仍在范围外的 `extract`,
14
+ 断言强度不变。)
15
+ * **G4 审计字段齐**(§4.1)——成功/失败各落一行,公共字段逐项在场(缺点名清单)。
16
+ * **G5 结构断言**(§六-G5)——① 模块 import 面零 `md_cg` 依赖、且不含 judge/trust/writepipe;
17
+ ③ 产物落点必须在调用方给的沙箱根之内。
18
+ * **G6 禁裸 convert 硬门**(§六-G6 + §3.5)——AST 扫 `imgskill.py`:不得以裸 `convert`
19
+ 为 argv[0]/不得 `shutil.which("convert")`/不得 `shell=True`;守卫源码自身不被误判。
20
+ * **G7 fail-closed**(§九-#8 + §六-G7)——未给沙箱根/越界路径/等级降级 → 一律拒绝,
21
+ **禁静默放行**(不得返回 `ok=true`)。
22
+ * **X-验收(§五 逐行判据)**——新增的 9 个 op 各按契约 §五 的验收栏判:
23
+ `thumbnail` 最长边 == `max_edge`(缩/放两向);`crop` 尺寸 == 裁窗**且内容对应**;
24
+ `rotate` 90 倍数尺寸对调 + **正角=顺时针**(像素级)+ 非直角外接矩形(±3px 容差,
25
+ 两后端取值本有差异)+ 空白填白;`flip`/`flop` 与源同尺寸 + 镜像像素级断言 +
26
+ 两 op 结果不同;`adjust`/`blur`/`sharpen` 参数入台账 + 同参幂等 + 效果方向
27
+ (亮度单调/模糊变平滑/锐化像素变化);`composite` 尺寸同 base + `gravity` 落位
28
+ 逐像素 + `opacity=0.5` 半透明叠合;`mask` alpha 通道存在**且逐像素对 mask 灰度**。
29
+ (判据一律**以落盘产物为准**,并核对出参 `meta` 与之一致。)
30
+ * **X-台账**——9 op 的台账 `params` 必等于**归一入参**(默认值补齐/别名解析后)。
31
+ * **X-幂等**——同 `idempotency_key` 同参 → 复用已存产物(`reused_from` 在场)+ 像素恒等。
32
+ * **X-中文路径**——9 op 全链(源/over/mask/产物全为中文名)逐个跑通。
33
+ * **X-错误态**——新 op 的 27 例坏参数 → 指定错误码(含 `mask.mode="mul"` → `E_UNSUPPORTED_OP`、
34
+ `mask` 写面收窄 → `E_UNSUPPORTED_FORMAT`)。
35
+ * **X-范围**——`E_UNSUPPORTED_OP` **收窄**:13 个已实现 op 必不返回它;范围外集
36
+ (`extract`/`export`/L1 类/L2 类/未知 op)必返回它;`OPS_IMPL` 与实现表 `_OPS` 一致。
37
+ * **Y-暴露面 / 结构防悬空(契约 §十二)**——`Y1`:以**子进程**跑真 CLI
38
+ (`python -X utf8 -m md_cg.imgskill ...`)——`--help` 列全部已实现 op;`inspect`
39
+ rc=0、出参键面齐、读数与库 `run()` **逐项一致**且**落审计台账**(证明 CLI 与库
40
+ 是**同一条**实现路径,不是第二份逻辑);`resize`/`convert` 产物落盘尺寸/格式对;
41
+ 未给 `--root` → fail-closed(rc=1、`E_BAD_PARAM`);`_CLI_OPS`/`OPS_IMPL`/子命令
42
+ 三处 op 面一致。`Y2`:**结构防悬空**——`__main__` 入口锚点须恰好 1 处;真件判据为
43
+ 绿;**摘掉入口的变异件必须转红**(同形先例:`test_curiosity_budget.py` C6、
44
+ `test_gain_gate.py` 的生产路径断言)。
45
+
46
+ 断言计数:G1–G7 18 条(**未减弱**)+ X 面 21 条 + Y 面 4 条 = **43 条**
47
+ (`--` 见末行 `N passed`)。
48
+
49
+ 边界:测试与实验一律落 `tempfile` 沙箱,**不写任何在役库**;本件不改 `md_cg/` 生产逻辑。
50
+ 后端无关:`imgskill` 走自动路由(`magick` 优先、Pillow 兜底)。若要压 `magick` 面,
51
+ 在环境里显式给 `IMGSKILL_MAGICK=<magick 可执行体全路径>` 再跑本件——本件内**不写本机绝对路径字面量**。
52
+
53
+ 运行:`python -m md_cg.test_imgskill`(退出码 0 全绿 / 1 有红);两条后端路径都要跑绿
54
+ (默认 = PATH 无 magick → Pillow 兜底且降级留痕;`IMGSKILL_MAGICK` 注入 → magick)。
55
+ """
56
+ from __future__ import annotations
57
+
58
+ import ast
59
+ import json
60
+ import math
61
+ import os
62
+ import shutil
63
+ import subprocess
64
+ import sys
65
+ import tempfile
66
+ from contextlib import contextmanager
67
+
68
+ # issue #84 同族(2026-10-09 DSH 端):PIL(Pillow) 是**可选**依赖——本件全程用它
69
+ # 造/读 PNG。无它时原代码在**模块级**直接 ModuleNotFoundError(rc=1,非零退出),
70
+ # 读者会误以为『测试坏了』,实为环境缺依赖。按仓内 #84 口径**显式跳过**:
71
+ # 打印 SKIP 说明并以 rc=2 退出(『有跳过』语义,与本批 #75/#84.1 守卫一致),
72
+ # 不静默放绿、也不伪装成失败。
73
+ try:
74
+ from PIL import Image
75
+ except ImportError:
76
+ print(" SKIP md_cg.test_imgskill:本解释器无 PIL(Pillow)——按 #84 口径显式跳过,"
77
+ "不静默放绿;该件需在有 PIL 的解释器上复跑")
78
+ raise SystemExit(2)
79
+
80
+ from . import imgskill as S
81
+
82
+ PASS = FAIL = 0
83
+ FAILS = []
84
+ READINGS = []
85
+ _ROOTS = []
86
+
87
+ #: §4.1 公共字段(每次操作必落)
88
+ AUDIT_KEYS = ("audit_id", "op", "level", "input_path", "input_sha256",
89
+ "output_path", "output_sha256", "params", "tool_version", "env",
90
+ "ts", "caller", "ok", "error_code")
91
+
92
+
93
+ def ok(cond, label):
94
+ global PASS, FAIL
95
+ if cond:
96
+ PASS += 1
97
+ else:
98
+ FAIL += 1
99
+ FAILS.append(label)
100
+ print(f" FAIL {label}")
101
+
102
+
103
+ def reading(k, v):
104
+ READINGS.append((k, v))
105
+
106
+
107
+ @contextmanager
108
+ def _env(**kw):
109
+ old = {k: os.environ.get(k) for k in kw}
110
+ for k, v in kw.items():
111
+ if v is None:
112
+ os.environ.pop(k, None)
113
+ else:
114
+ os.environ[k] = v
115
+ try:
116
+ yield
117
+ finally:
118
+ for k, v in old.items():
119
+ if v is None:
120
+ os.environ.pop(k, None)
121
+ else:
122
+ os.environ[k] = v
123
+
124
+
125
+ # ---------------------------------------------------------------- 夹具
126
+
127
+ def mk_root(prefix="imgskill_g_"):
128
+ r = tempfile.mkdtemp(prefix=prefix)
129
+ _ROOTS.append(r)
130
+ return r
131
+
132
+
133
+ def mk_src(root, rel, size=(40, 30), color=(12, 200, 40)):
134
+ """造确定性源图(渐变 + 定色),写进沙箱根内。"""
135
+ p = os.path.join(root, rel)
136
+ d = os.path.dirname(p)
137
+ if d:
138
+ os.makedirs(d, exist_ok=True)
139
+ im = Image.new("RGB", size)
140
+ px = im.load()
141
+ for y in range(size[1]):
142
+ for x in range(size[0]):
143
+ px[x, y] = ((color[0] + x * 3) % 256, (color[1] + y * 5) % 256,
144
+ (color[2] + (x + y) * 2) % 256)
145
+ im.save(p)
146
+ return p
147
+
148
+
149
+ def abspath(root, rel):
150
+ return os.path.join(root, (rel or "").replace("/", os.sep))
151
+
152
+
153
+ def pixels(path):
154
+ return Image.open(path).convert("RGB").tobytes()
155
+
156
+
157
+ def under(child, root):
158
+ cn, rn = os.path.normcase(os.path.realpath(child)), os.path.normcase(os.path.realpath(root))
159
+ try:
160
+ return os.path.commonpath([cn, rn]) == rn
161
+ except ValueError:
162
+ return False
163
+
164
+
165
+ def code_of(r):
166
+ return (r.get("error") or {}).get("code")
167
+
168
+
169
+ # ---------------------------------------------------------------- G1 幂等
170
+
171
+ def g1_idempotent(impl):
172
+ """同输入同参 → 像素相等(位级只作读数)。"""
173
+ fails = []
174
+ root = mk_root()
175
+ mk_src(root, "in/p.png", (40, 30))
176
+ req = {"op": "resize", "src": "in/p.png", "dst": "o/a.png",
177
+ "params": {"width": 20, "height": 15, "filter": "lanczos"},
178
+ "sandbox_root": root, "caller": "g1"}
179
+ r1 = impl(req)
180
+ if not r1.get("ok"):
181
+ return ["G1 首次运行失败"]
182
+ px1 = pixels(abspath(root, r1["artifact_path"]))
183
+ b1 = open(abspath(root, r1["artifact_path"]), "rb").read()
184
+ r2 = impl(req) # 同 req 再跑一次(覆盖同名产物)
185
+ if not r2.get("ok"):
186
+ return ["G1 二次运行失败"]
187
+ px2 = pixels(abspath(root, r2["artifact_path"]))
188
+ b2 = open(abspath(root, r2["artifact_path"]), "rb").read()
189
+ if px1 != px2:
190
+ fails.append("G1 同输入同参两次像素不等")
191
+ if impl is S.run: # 读数只记真件(变异件不污染读数)
192
+ reading("G1 resize 像素级幂等", px1 == px2)
193
+ reading("G1 resize 位级(信息项,非门)", b1 == b2)
194
+ # convert 同口径
195
+ reqc = {"op": "convert", "src": "in/p.png", "dst": "o/a.webp",
196
+ "params": {"format": "webp"}, "sandbox_root": root, "caller": "g1"}
197
+ c1 = impl(reqc)
198
+ c2 = impl(reqc)
199
+ if not (c1.get("ok") and c2.get("ok")):
200
+ fails.append("G1 convert 两次运行失败")
201
+ elif pixels(abspath(root, c1["artifact_path"])) != pixels(abspath(root, c2["artifact_path"])):
202
+ fails.append("G1 convert 同输入同参两次像素不等")
203
+ return fails
204
+
205
+
206
+ # ---------------------------------------------------------------- G2 中文路径
207
+
208
+ def g2_cjk_path(impl):
209
+ fails = []
210
+ root = mk_root()
211
+ mk_src(root, os.path.join("中文目录", "看板图.png"), (48, 32))
212
+ r = impl({"op": "resize", "src": "中文目录/看板图.png",
213
+ "dst": "中文输出/结果_缩略.webp",
214
+ "params": {"width": 24, "height": 16, "filter": "lanczos"},
215
+ "sandbox_root": root, "caller": "g2"})
216
+ if not r.get("ok"):
217
+ return [f"G2 中文路径 resize 失败({code_of(r)})"]
218
+ p = abspath(root, r["artifact_path"])
219
+ if not os.path.isfile(p):
220
+ fails.append("G2 中文产物未落盘")
221
+ else:
222
+ try:
223
+ im = Image.open(p)
224
+ im.load()
225
+ if im.size != (24, 16):
226
+ fails.append("G2 中文产物尺寸不符")
227
+ except Exception as e:
228
+ fails.append(f"G2 中文产物不可回读({e!r})")
229
+ r2 = impl({"op": "convert", "src": "中文目录/看板图.png",
230
+ "dst": "中文输出/转格式.png", "params": {"format": "png"},
231
+ "sandbox_root": root, "caller": "g2"})
232
+ if not r2.get("ok"):
233
+ fails.append(f"G2 中文路径 convert 失败({code_of(r2)})")
234
+ elif r2["meta"]["format"] != "PNG":
235
+ fails.append("G2 中文路径 convert 格式不符")
236
+ return fails
237
+
238
+
239
+ # ---------------------------------------------------------------- G3 错误态
240
+
241
+ def g3_error_codes(impl):
242
+ """8 类触发样例 → 指定错误码(必需 4 + 追加 4)。"""
243
+ fails = []
244
+ root = mk_root()
245
+ mk_src(root, "in/a.png", (16, 16))
246
+ with open(os.path.join(root, "in", "bad.png"), "w", encoding="utf-8") as f:
247
+ f.write("not an image at all")
248
+ base = {"op": "resize", "src": "in/a.png", "dst": "o/a.png",
249
+ "params": {"width": 8, "height": 8}, "sandbox_root": root, "caller": "g3"}
250
+ cases = [
251
+ ("E_NOINPUT", dict(base, src="in/missing.png"), None),
252
+ ("E_BAD_PARAM", dict(base, params={"width": 0, "height": 8}), None),
253
+ ("E_PATH_OUT_OF_SCOPE", dict(base, src="../escape.png"), None),
254
+ ("E_TOOL_MISSING", dict(base), {"IMGSKILL_BACKEND": "magick",
255
+ "IMGSKILL_MAGICK": os.path.join(root, "no_such_magick.exe")}),
256
+ ("E_DECODE", dict(base, op="inspect", src="in/bad.png", params={}), None),
257
+ # 本批起 `crop` 已实现 → 本项改用仍在范围外的 `extract`(断言强度不变)
258
+ ("E_UNSUPPORTED_OP", dict(base, op="extract", params={}), None),
259
+ ("E_LEVEL_DOWNGRADE", dict(base, op="generate", params={},
260
+ declared_level="L0"), None),
261
+ ("E_UNSUPPORTED_FORMAT", {"op": "convert", "src": "in/a.png",
262
+ "dst": "o/x.tga", "params": {"format": "tga"},
263
+ "sandbox_root": root, "caller": "g3"}, None),
264
+ ]
265
+ for want, req, env in cases:
266
+ if env:
267
+ with _env(**env):
268
+ r = impl(req)
269
+ else:
270
+ r = impl(req)
271
+ if r.get("ok") is not False:
272
+ fails.append(f"G3 {want} 未拒绝(ok={r.get('ok')!r})")
273
+ elif code_of(r) != want:
274
+ fails.append(f"G3 {want} 码不符(实得 {code_of(r)!r})")
275
+ # 失败也须落审计(§四「每次操作」)
276
+ led = S.read_audit(root)
277
+ if not any(a.get("ok") is False and a.get("error_code") for a in led):
278
+ fails.append("G3 失败操作未落审计(error_code 缺失)")
279
+ return fails
280
+
281
+
282
+ # ---------------------------------------------------------------- G4 审计字段
283
+
284
+ def g4_audit_fields(impl):
285
+ fails = []
286
+ root = mk_root()
287
+ mk_src(root, "in/a.png", (16, 16))
288
+ r = impl({"op": "resize", "src": "in/a.png", "dst": "o/a.png",
289
+ "params": {"width": 8, "height": 8}, "sandbox_root": root, "caller": "g4"})
290
+ if not r.get("ok"):
291
+ return ["G4 前置运行失败"]
292
+ if not os.path.isfile(S.audit_path(root)):
293
+ return ["G4 台账未落盘"]
294
+ if not under(S.audit_path(root), root):
295
+ fails.append("G4 台账越出沙箱根")
296
+ rec = S.read_audit(root)[-1]
297
+ missing = [k for k in AUDIT_KEYS if k not in rec]
298
+ if missing:
299
+ fails.append("G4 缺字段: " + ",".join(missing))
300
+ else:
301
+ if rec.get("ok") is not True:
302
+ fails.append("G4 成功记录 ok 字段不为 True")
303
+ if rec.get("level") != "L0":
304
+ fails.append("G4 level 不等于派生等级 L0")
305
+ if rec.get("op") != "resize":
306
+ fails.append("G4 op 字段不符")
307
+ if len(str(rec.get("input_sha256") or "")) != 64:
308
+ fails.append("G4 input_sha256 非 hex64")
309
+ if rec.get("output_path") != r["artifact_path"]:
310
+ fails.append("G4 output_path 与出参不一致")
311
+ return fails
312
+
313
+
314
+ # ---------------------------------------------------------------- G5 结构断言
315
+
316
+ _SRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), "imgskill.py")
317
+
318
+ #: 禁止出现在 L1 import 面的模块(§六-G5①;本仓资格/信任/写入面)
319
+ _FORBIDDEN = ("judge", "trust", "writepipe")
320
+ #: 规则字面量拼接构造(防守卫自身被误判;沿 check_local_paths.py 做法)
321
+ _BARE_CONVERT = ("con" + "vert", "con" + "vert.exe")
322
+
323
+
324
+ def _src_text():
325
+ with open(_SRC, "r", encoding="utf-8") as f:
326
+ return f.read()
327
+
328
+
329
+ def check_import_face(src_text):
330
+ """返回违规清单:① 任何 md_cg 相对 import(level>0);② 叶子名命中禁用面。"""
331
+ viol = []
332
+ for n in ast.walk(ast.parse(src_text)):
333
+ if isinstance(n, ast.ImportFrom):
334
+ if n.level and n.level > 0:
335
+ viol.append(f"相对 import @L{n.lineno}: .{n.module or ''}")
336
+ for a in n.names:
337
+ leaf = a.name.split(".")[-1].lower()
338
+ if leaf in _FORBIDDEN:
339
+ viol.append(f"禁用模块导入 @L{n.lineno}: {a.name}")
340
+ elif isinstance(n, ast.Import):
341
+ for a in n.names:
342
+ leaf = a.name.split(".")[-1].lower()
343
+ if leaf in _FORBIDDEN:
344
+ viol.append(f"禁用模块导入 @L{n.lineno}: {a.name}")
345
+ return viol
346
+
347
+
348
+ def check_no_bare_convert(src_text):
349
+ """禁裸 convert 硬门(§六-G6):argv[0]/`which` 面/`shell=True` 三条判据。
350
+
351
+ **已知边界**(有意,非缺陷):判据认的是 AST 里的**字面量常量**,故
352
+ `"con" + "vert"` 这类**拼接式规避**不判。这与本仓既有约定一致——规则字面量本身
353
+ 就用拼接构造(见 `_BARE_CONVERT`),若判据也去折叠拼接,反而会把守卫自身判红;
354
+ 本门的对象是「顺手写成裸 `convert`」这一现实错法(`["convert", ...]`),
355
+ 不是有意规避者。
356
+ """
357
+ viol = []
358
+ for n in ast.walk(ast.parse(src_text)):
359
+ if isinstance(n, (ast.List, ast.Tuple)) and n.elts:
360
+ e0 = n.elts[0]
361
+ if isinstance(e0, ast.Constant) and isinstance(e0.value, str):
362
+ if os.path.basename(e0.value).lower() in _BARE_CONVERT:
363
+ viol.append(f"argv[0] 裸 convert @L{e0.lineno}: {e0.value!r}")
364
+ if isinstance(n, ast.Call):
365
+ fn = n.func
366
+ fname = (fn.attr if isinstance(fn, ast.Attribute)
367
+ else (fn.id if isinstance(fn, ast.Name) else ""))
368
+ if fname == "which" and n.args:
369
+ a = n.args[0]
370
+ if isinstance(a, ast.Constant) and isinstance(a.value, str) \
371
+ and a.value.lower() in _BARE_CONVERT:
372
+ viol.append(f"which 裸 convert @L{a.lineno}")
373
+ if fname in ("run", "call", "check_call", "check_output", "Popen"):
374
+ for k in n.keywords:
375
+ if k.arg == "shell" and isinstance(k.value, ast.Constant) \
376
+ and k.value.value is True:
377
+ viol.append(f"subprocess shell=True @L{n.lineno}")
378
+ return viol
379
+
380
+
381
+ def g5_in_root(impl):
382
+ """③ 产物落点必须在调用方给的沙箱根内。"""
383
+ root = mk_root()
384
+ mk_src(root, "in/a.png", (16, 16))
385
+ r = impl({"op": "resize", "src": "in/a.png", "dst": "o/a.png",
386
+ "params": {"width": 8, "height": 8}, "sandbox_root": root, "caller": "g5"})
387
+ if not r.get("ok"):
388
+ return ["G5③ 前置运行失败"]
389
+ ap = r["artifact_path"] or ""
390
+ full = ap if os.path.isabs(ap) else os.path.join(root, ap.replace("/", os.sep))
391
+ return [] if under(full, root) else ["G5③ 产物越出沙箱根"]
392
+
393
+
394
+ # ---------------------------------------------------------------- G7 fail-closed
395
+
396
+ def g7_fail_closed(impl):
397
+ fails = []
398
+ root = mk_root()
399
+ mk_src(root, "in/a.png", (16, 16))
400
+ base = {"op": "resize", "src": "in/a.png", "dst": "o/a.png",
401
+ "params": {"width": 8, "height": 8}, "caller": "g7"}
402
+ # a) 未给沙箱根(§九-#8:不设隐式默认根)
403
+ r = impl(dict(base))
404
+ if r.get("ok"):
405
+ fails.append("G7 未给沙箱根却 ok=true(静默放行)")
406
+ elif code_of(r) not in ("E_BAD_PARAM", "E_PATH_OUT_OF_SCOPE"):
407
+ fails.append(f"G7 未给根错误码不符({code_of(r)!r})")
408
+ # b) 越界路径(`..` 与绝对路径)
409
+ # issue #84.2(2026-10-09 DSH 端):越界样例按平台构造——原写死 Windows
410
+ # 绝对路径,在 POSIX 上 C:/Windows/win.ini **不是绝对路径**,该腿语义随平台
411
+ # 漂移(可能假绿)。../ 相对越界腿保留(跨平台语义一致)。
412
+ _abs_out = "C:/Windows/win.ini" if os.name == "nt" else "/etc/hosts"
413
+ for bad in ("../escape.png", _abs_out):
414
+ r = impl(dict(base, src=bad, sandbox_root=root))
415
+ if r.get("ok"):
416
+ fails.append(f"G7 越界 src={bad} 却 ok=true")
417
+ elif code_of(r) != "E_PATH_OUT_OF_SCOPE":
418
+ fails.append(f"G7 越界 src={bad} 码不符({code_of(r)!r})")
419
+ r = impl(dict(base, src="in/a.png", dst="../../out.png", sandbox_root=root))
420
+ if r.get("ok") or code_of(r) != "E_PATH_OUT_OF_SCOPE":
421
+ fails.append("G7 越界 dst 未拒(E_PATH_OUT_OF_SCOPE)")
422
+ # c) 沙箱根不可用(不存在 → 不隐式创建)
423
+ r = impl(dict(base, sandbox_root=os.path.join(root, "no_such_dir")))
424
+ if r.get("ok"):
425
+ fails.append("G7 沙箱根不存在却 ok=true")
426
+ # d) 等级降级(declared_level 低于派生 → 拒绝)
427
+ r = impl({"op": "generate", "src": "in/a.png", "params": {},
428
+ "sandbox_root": root, "caller": "g7", "declared_level": "L0"})
429
+ if r.get("ok") or code_of(r) != "E_LEVEL_DOWNGRADE":
430
+ fails.append("G7 等级降级未拒(E_LEVEL_DOWNGRADE)")
431
+ # e) dst 覆盖 src(§九-#7 fail-closed)
432
+ r = impl(dict(base, src="in/a.png", dst="in/a.png", sandbox_root=root))
433
+ if r.get("ok"):
434
+ fails.append("G7 dst 覆盖 src 却 ok=true")
435
+ return fails
436
+
437
+
438
+ # ---------------------------------------------------------------- 新增 op:夹具与判据
439
+ #
440
+ # 契约 §五 表内除 inspect/resize/convert 的 9 个 op(thumbnail/crop/rotate/flip/flop/
441
+ # adjust/blur/sharpen/composite/mask)逐行判据在此验收;每件的失败串**定点唯一**,
442
+ # 供下面的「定点变异自证」精确打靶。
443
+
444
+ OV_COLOR = (255, 0, 0) # composite 的 over 恒用纯色 → 合成位置可机械断言
445
+
446
+
447
+ def mk_over(root, rel, size=(10, 10), color=OV_COLOR):
448
+ """造纯色 over(合成位置可断言)。"""
449
+ p = os.path.join(root, rel)
450
+ d = os.path.dirname(p)
451
+ if d:
452
+ os.makedirs(d, exist_ok=True)
453
+ Image.new("RGB", size, color).save(p)
454
+ return p
455
+
456
+
457
+ def mk_mask(root, rel, size=(40, 30), pattern=(255, 200, 128, 0, 32)):
458
+ """造确定性灰度 mask(逐列循环取值,便于逐像素核对 alpha)。"""
459
+ p = os.path.join(root, rel)
460
+ d = os.path.dirname(p)
461
+ if d:
462
+ os.makedirs(d, exist_ok=True)
463
+ im = Image.new("L", size)
464
+ px = im.load()
465
+ for y in range(size[1]):
466
+ for x in range(size[0]):
467
+ px[x, y] = pattern[x % len(pattern)]
468
+ im.save(p)
469
+ return p
470
+
471
+
472
+ def mk_rgba(root, rel, size=(40, 30), alpha=180):
473
+ """造带 alpha 的源(判「alpha 源」下 imgskill 不动 alpha/输出 mode 随 base)。"""
474
+ p = os.path.join(root, rel)
475
+ d = os.path.dirname(p)
476
+ if d:
477
+ os.makedirs(d, exist_ok=True)
478
+ im = Image.new("RGBA", size)
479
+ px = im.load()
480
+ for y in range(size[1]):
481
+ for x in range(size[0]):
482
+ px[x, y] = ((12 + x * 3) % 256, (200 + y * 5) % 256,
483
+ (40 + (x + y) * 2) % 256, alpha)
484
+ im.save(p)
485
+ return p
486
+
487
+
488
+ def mk_checker(root, rel, size=(40, 30), cell=2):
489
+ """造高频棋盘源(模糊/锐化的「效果」判据最灵敏)。"""
490
+ p = os.path.join(root, rel)
491
+ d = os.path.dirname(p)
492
+ if d:
493
+ os.makedirs(d, exist_ok=True)
494
+ im = Image.new("L", size)
495
+ px = im.load()
496
+ for y in range(size[1]):
497
+ for x in range(size[0]):
498
+ px[x, y] = 245 if ((x // cell + y // cell) % 2) else 10
499
+ im.save(p)
500
+ return p
501
+
502
+
503
+ def fixture(root):
504
+ """标准夹具:梯度源 + 纯色 over + 灰度 mask + 越界 over + 尺寸不符 mask。"""
505
+ mk_src(root, "in/p.png", (40, 30))
506
+ mk_over(root, "in/ov.png", (10, 10))
507
+ mk_mask(root, "in/mk.png", (40, 30))
508
+ mk_over(root, "in/big.png", (50, 50))
509
+ mk_mask(root, "in/small_mask.png", (20, 20))
510
+ return root
511
+
512
+
513
+ def rgbof(path):
514
+ return Image.open(path).convert("RGB").tobytes()
515
+
516
+
517
+ def repo(path):
518
+ return Image.open(path).convert("RGBA").tobytes()
519
+
520
+
521
+ def rough(path):
522
+ """相邻像素差绝对值之和(模糊降、锐化升)。"""
523
+ im = Image.open(path).convert("L")
524
+ px = im.load()
525
+ w, h = im.size
526
+ return sum(abs(px[x + 1, y] - px[x, y]) for y in range(h) for x in range(w - 1))
527
+
528
+
529
+ def _run(impl, root, op, params, dst, src="in/p.png", **kw):
530
+ req = {"op": op, "src": src, "dst": dst, "params": params,
531
+ "sandbox_root": root, "caller": "xa"}
532
+ req.update(kw)
533
+ return impl(req)
534
+
535
+
536
+ def xa_thumbnail(impl):
537
+ """判据:最长边 == max_edge(保持纵横比)——缩、放两个方向都判。"""
538
+ root = fixture(mk_root())
539
+ fails = []
540
+ for edge, want in ((20, (20, 15)), (60, (60, 45))):
541
+ r = _run(impl, root, "thumbnail", {"max_edge": edge}, f"o/t{edge}.png")
542
+ if not r.get("ok"):
543
+ fails.append(f"X-thumbnail max_edge={edge} 非 ok({code_of(r)})")
544
+ continue
545
+ got = Image.open(abspath(root, r["artifact_path"])).size # 以**落盘产物**为准
546
+ if got != want:
547
+ fails.append(f"X-thumbnail 落盘产物最长边 != max_edge(max_edge={edge} 实得 {got})")
548
+ if (r["meta"]["width"], r["meta"]["height"]) != want:
549
+ fails.append(f"X-thumbnail 出参 meta 与目标不符(max_edge={edge})")
550
+ if r["meta"]["format"] != "PNG":
551
+ fails.append("X-thumbnail 格式不是同后缀派生(PNG)")
552
+ if impl is S.run:
553
+ reading("X-thumbnail 40x30 max_edge=20", "20x15(判据:最长边==max_edge)")
554
+ return fails
555
+
556
+
557
+ def xa_crop(impl):
558
+ """判据:产物尺寸 == 裁窗(并逐像素核对裁的是哪一块)。"""
559
+ root = fixture(mk_root())
560
+ r = _run(impl, root, "crop", {"x": 3, "y": 5, "width": 10, "height": 8}, "o/c.png")
561
+ if not r.get("ok"):
562
+ return [f"X-crop 非 ok({code_of(r)})"]
563
+ fails = []
564
+ if (r["meta"]["width"], r["meta"]["height"]) != (10, 8):
565
+ fails.append(f"X-crop 尺寸 != 裁窗(实得 {r['meta']['width']}x{r['meta']['height']})")
566
+ want = Image.open(os.path.join(root, "in", "p.png")).convert("RGB").crop((3, 5, 13, 13))
567
+ if rgbof(abspath(root, r["artifact_path"])) != want.tobytes():
568
+ fails.append("X-crop 裁窗内容与源不对应")
569
+ return fails
570
+
571
+
572
+ def xa_rotate(impl):
573
+ """判据:90 倍数时尺寸对调;非直角按外接矩形(两后端取值本有差异 → 容差 3px)。"""
574
+ root = fixture(mk_root())
575
+ sp = os.path.join(root, "in", "p.png")
576
+ base = Image.open(sp).convert("RGB")
577
+ fails = []
578
+ for deg, rot in ((90, -90), (180, 180), (-90, 90)):
579
+ r = _run(impl, root, "rotate", {"degrees": deg}, f"o/r{deg}.png")
580
+ if not r.get("ok"):
581
+ fails.append(f"X-rotate {deg} 非 ok({code_of(r)})")
582
+ continue
583
+ want = base.rotate(rot, expand=True)
584
+ if (r["meta"]["width"], r["meta"]["height"]) != want.size:
585
+ fails.append(f"X-rotate {deg} 尺寸不对调(实得 {r['meta']['width']}x{r['meta']['height']})")
586
+ elif rgbof(abspath(root, r["artifact_path"])) != want.tobytes():
587
+ fails.append(f"X-rotate {deg} 像素与「正角=顺时针」不符")
588
+ # 非直角:外接矩形(容差 3px)
589
+ for deg in (45, 30):
590
+ r = _run(impl, root, "rotate", {"degrees": deg}, f"o/rn{deg}.png")
591
+ if not r.get("ok"):
592
+ fails.append(f"X-rotate {deg} 非 ok({code_of(r)})")
593
+ continue
594
+ c = abs(math.cos(math.radians(deg)))
595
+ sn = abs(math.sin(math.radians(deg)))
596
+ a = (40 * c + 30 * sn, 40 * sn + 30 * c)
597
+ got = (r["meta"]["width"], r["meta"]["height"])
598
+ if abs(got[0] - a[0]) > 3 or abs(got[1] - a[1]) > 3:
599
+ fails.append(f"X-rotate {deg} 偏离外接矩形超容差(解析 {a[0]:.2f} 实得 {got})")
600
+ if impl is S.run and deg == 45:
601
+ reading("X-rotate 45° 尺寸(magick 52x52 / Pillow 50x50;解析 49.50)", got)
602
+ if Image.open(abspath(root, r["artifact_path"])).convert("RGB").getpixel((0, 0)) != (255, 255, 255):
603
+ fails.append("X-rotate 非直角空白处不是白(背景口径漂移)")
604
+ return fails
605
+
606
+
607
+ def xa_mirror(impl):
608
+ """判据:与源同尺寸 + 镜像像素可断言(flip 与 flop 是两个不同 op)。"""
609
+ root = fixture(mk_root())
610
+ sp = os.path.join(root, "in", "p.png")
611
+ base = Image.open(sp).convert("RGB")
612
+ fails = []
613
+ for op, tr, zh in (("flip", Image.Transpose.FLIP_TOP_BOTTOM, "上下"),
614
+ ("flop", Image.Transpose.FLIP_LEFT_RIGHT, "左右")):
615
+ r = _run(impl, root, op, {}, f"o/{op}.png")
616
+ if not r.get("ok"):
617
+ fails.append(f"X-mirror {op} 非 ok({code_of(r)})")
618
+ continue
619
+ if (r["meta"]["width"], r["meta"]["height"]) != base.size:
620
+ fails.append(f"X-mirror {op} 尺寸与源不同")
621
+ elif rgbof(abspath(root, r["artifact_path"])) != base.transpose(tr).tobytes():
622
+ fails.append(f"X-mirror {op} 像素非{zh}镜像")
623
+ # 两个 op 必须给出不同结果(源自身非对称)
624
+ a, b = abspath(root, "o/flip.png"), abspath(root, "o/flop.png")
625
+ if os.path.isfile(a) and os.path.isfile(b) and rgbof(a) == rgbof(b):
626
+ fails.append("X-mirror flip 与 flop 结果相同(两 op 未分离)")
627
+ # axis 选填:同轴确认值可用
628
+ r = _run(impl, root, "flip", {"axis": "vertical"}, "o/flip_ax.png")
629
+ if not r.get("ok"):
630
+ fails.append(f"X-mirror flip axis=vertical 应可用({code_of(r)})")
631
+ return fails
632
+
633
+
634
+ def xa_adjust(impl):
635
+ """判据:参数入台账 + 同参幂等(效果:亮度正负方向单调;尺寸不变)。"""
636
+ root = fixture(mk_root())
637
+ sp = os.path.join(root, "in", "p.png")
638
+ base = Image.open(sp).convert("L")
639
+ m0 = sum(base.tobytes()) / float(len(base.tobytes()))
640
+ fails = []
641
+ means = {}
642
+ for b in (40, -40):
643
+ r = _run(impl, root, "adjust", {"brightness": b, "contrast": 10, "gamma": 1.0},
644
+ f"o/a{b}.png")
645
+ if not r.get("ok"):
646
+ fails.append(f"X-adjust brightness={b} 非 ok({code_of(r)})")
647
+ continue
648
+ if (r["meta"]["width"], r["meta"]["height"]) != base.size:
649
+ fails.append(f"X-adjust brightness={b} 尺寸变了")
650
+ im = Image.open(abspath(root, r["artifact_path"])).convert("L")
651
+ means[b] = sum(im.tobytes()) / float(len(im.tobytes()))
652
+ if len(means) == 2:
653
+ if not (means[40] > m0 > means[-40]):
654
+ fails.append(f"X-adjust 亮度效果非单调(+40={means[40]:.1f} 源={m0:.1f} -40={means[-40]:.1f})")
655
+ if impl is S.run:
656
+ reading("X-adjust 亮度均值 源/+40/-40", f"{m0:.1f}/{means[40]:.1f}/{means[-40]:.1f}")
657
+ # gamma 单参亦可(单独入台账)
658
+ r = _run(impl, root, "adjust", {"gamma": 2.0}, "o/ag.png")
659
+ if not r.get("ok"):
660
+ fails.append(f"X-adjust gamma 单参非 ok({code_of(r)})")
661
+ return fails
662
+
663
+
664
+ def xa_blur(impl):
665
+ """判据:参数入台账 + 同参幂等(效果:尺寸不变、像素变化、变平滑)。"""
666
+ root = fixture(mk_root())
667
+ mk_checker(root, "in/hf.png")
668
+ r = _run(impl, root, "blur", {"sigma": 2}, "o/b.png", src="in/hf.png")
669
+ if not r.get("ok"):
670
+ return [f"X-blur 非 ok({code_of(r)})"]
671
+ fails = []
672
+ if (r["meta"]["width"], r["meta"]["height"]) != (40, 30):
673
+ fails.append("X-blur 尺寸变了")
674
+ if rgbof(abspath(root, r["artifact_path"])) == rgbof(os.path.join(root, "in", "hf.png")):
675
+ fails.append("X-blur 像素与源相同(未生效)")
676
+ if not rough(abspath(root, r["artifact_path"])) < rough(os.path.join(root, "in", "hf.png")):
677
+ fails.append("X-blur 未变平滑(相邻差未降)")
678
+ return fails
679
+
680
+
681
+ def xa_sharpen(impl):
682
+ """判据:参数入台账 + 同参幂等(效果:尺寸不变、像素变化)。"""
683
+ root = fixture(mk_root())
684
+ mk_checker(root, "in/hf.png")
685
+ r = _run(impl, root, "sharpen", {"sigma": 2}, "o/s.png", src="in/hf.png")
686
+ if not r.get("ok"):
687
+ return [f"X-sharpen 非 ok({code_of(r)})"]
688
+ fails = []
689
+ if (r["meta"]["width"], r["meta"]["height"]) != (40, 30):
690
+ fails.append("X-sharpen 尺寸变了")
691
+ if rgbof(abspath(root, r["artifact_path"])) == rgbof(os.path.join(root, "in", "hf.png")):
692
+ fails.append("X-sharpen 像素与源相同(未生效)")
693
+ return fails
694
+
695
+
696
+ def xa_composite(impl):
697
+ """判据:尺寸同 base;合成位置入台账(此处按 gravity 逐像素核对落位)。"""
698
+ root = fixture(mk_root())
699
+ fails = []
700
+ pos = {"northwest": (0, 0), "center": (15, 10), "southeast": (30, 20)}
701
+ for g, (gx, gy) in pos.items():
702
+ r = _run(impl, root, "composite",
703
+ {"over_path": "in/ov.png", "gravity": g, "opacity": 1.0}, f"o/k_{g}.png")
704
+ if not r.get("ok"):
705
+ fails.append(f"X-composite gravity={g} 非 ok({code_of(r)})")
706
+ continue
707
+ if (r["meta"]["width"], r["meta"]["height"]) != (40, 30):
708
+ fails.append(f"X-composite gravity={g} 尺寸不等于 base")
709
+ continue
710
+ im = Image.open(abspath(root, r["artifact_path"])).convert("RGB")
711
+ want_rect = [(x, y) for y in range(gy, gy + 10) for x in range(gx, gx + 10)]
712
+ got = {(x, y) for y in range(im.size[1]) for x in range(im.size[0])
713
+ if im.getpixel((x, y)) == OV_COLOR}
714
+ if got != set(want_rect):
715
+ fails.append(f"X-composite gravity={g} 落位不符(over 应在 {(gx, gy)} 起 10x10)")
716
+ # opacity 0.5:应是 base 与 over 的近似均值(容差 3),且与不透明结果不同
717
+ r = _run(impl, root, "composite",
718
+ {"over_path": "in/ov.png", "gravity": "center", "opacity": 0.5}, "o/k50.png")
719
+ if not r.get("ok"):
720
+ fails.append(f"X-composite opacity=0.5 非 ok({code_of(r)})")
721
+ else:
722
+ base = Image.open(os.path.join(root, "in", "p.png")).convert("RGB")
723
+ im = Image.open(abspath(root, r["artifact_path"])).convert("RGB")
724
+ worst = 0
725
+ for x in range(15, 25):
726
+ for y in range(10, 20):
727
+ bs = base.getpixel((x, y))
728
+ got = im.getpixel((x, y))
729
+ worst = max(worst, max(abs(got[i] - (bs[i] + OV_COLOR[i]) // 2) for i in range(3)))
730
+ if worst > 3:
731
+ fails.append(f"X-composite opacity=0.5 非半透明叠合(最大通道差 {worst})")
732
+ if impl is S.run:
733
+ reading("X-composite opacity=0.5 相对解析均值的最大通道差", worst)
734
+ # alpha 源:输出须随 base 保留 alpha,叠合处为 over 实色、其余保留 base alpha
735
+ root2 = fixture(mk_root())
736
+ mk_rgba(root2, "in/rgba.png", (40, 30), alpha=180)
737
+ r = _run(impl, root2, "composite",
738
+ {"over_path": "in/ov.png", "gravity": "center", "opacity": 1.0},
739
+ "o/ka.png", src="in/rgba.png")
740
+ if not r.get("ok"):
741
+ fails.append(f"X-composite alpha 源非 ok({code_of(r)})")
742
+ else:
743
+ im = Image.open(abspath(root2, r["artifact_path"])).convert("RGBA")
744
+ if "A" not in Image.open(abspath(root2, r["artifact_path"])).getbands():
745
+ fails.append("X-composite alpha 源输出丢了 alpha 通道")
746
+ elif im.getpixel((20, 15))[:3] != OV_COLOR or im.getpixel((0, 0))[3] != 180:
747
+ fails.append("X-composite alpha 源叠合/保留 alpha 不符"
748
+ f"(叠合处 {im.getpixel((20, 15))},域外 alpha {im.getpixel((0, 0))[3]})")
749
+ return fails
750
+
751
+
752
+ def xa_mask(impl):
753
+ """判据:alpha 通道存在且可读回(并逐像素核对 alpha == mask 灰度)。"""
754
+ root = fixture(mk_root())
755
+ r = _run(impl, root, "mask", {"mask_path": "in/mk.png", "mode": "set"}, "o/m.png")
756
+ if not r.get("ok"):
757
+ return [f"X-mask 非 ok({code_of(r)})"]
758
+ ap = abspath(root, r["artifact_path"])
759
+ if "A" not in Image.open(ap).getbands():
760
+ return ["X-mask 产物无 alpha 通道"] # 短路:无 alpha 则值核对无意义
761
+ fails = []
762
+ base = Image.open(os.path.join(root, "in", "p.png")).convert("RGBA")
763
+ base.putalpha(Image.open(os.path.join(root, "in", "mk.png")).convert("L"))
764
+ if repo(ap) != base.tobytes():
765
+ fails.append("X-mask alpha/像素与 mask 灰度不符")
766
+ r2 = _run(impl, root, "mask", {"mask_path": "in/mk.png"}, "o/m2.png")
767
+ if not r2.get("ok") or "A" not in Image.open(abspath(root, r2["artifact_path"])).getbands():
768
+ fails.append("X-mask mode 缺省(set)非 ok 或无 alpha")
769
+ # alpha 源:原有 alpha 被 mask 覆盖(不叠加),像素仍须逐项对得上
770
+ root2 = fixture(mk_root())
771
+ mk_rgba(root2, "in/rgba.png", (40, 30), alpha=180)
772
+ r3 = _run(impl, root2, "mask", {"mask_path": "in/mk.png"}, "o/m3.png", src="in/rgba.png")
773
+ if not r3.get("ok"):
774
+ fails.append(f"X-mask(alpha 源)非 ok({code_of(r3)})")
775
+ else:
776
+ want = Image.open(os.path.join(root2, "in", "rgba.png")).convert("RGBA")
777
+ want.putalpha(Image.open(os.path.join(root2, "in", "mk.png")).convert("L"))
778
+ if repo(abspath(root2, r3["artifact_path"])) != want.tobytes():
779
+ fails.append("X-mask(alpha 源)alpha 与 mask 灰度不符")
780
+ return fails
781
+
782
+
783
+ # ---------------------------------------------------------------- 新增 op:台账/幂等/中文路径
784
+
785
+ #: (op, params, dst, 台账里应记的**归一参数**)——第 4 项即「参数入台账」的判据本身
786
+ LEDGER_CASES = (
787
+ ("thumbnail", {"max_edge": 20}, "o/l_t.png", {"max_edge": 20}),
788
+ ("crop", {"x": 3, "y": 5, "width": 10, "height": 8}, "o/l_c.png",
789
+ {"x": 3, "y": 5, "width": 10, "height": 8}),
790
+ ("rotate", {"degrees": 90}, "o/l_r.png", {"degrees": 90.0}),
791
+ ("flip", {}, "o/l_f.png", {}),
792
+ ("flop", {"axis": "horizontal"}, "o/l_fl.png", {"axis": "horizontal"}),
793
+ ("adjust", {"brightness": 20}, "o/l_a.png",
794
+ {"brightness": 20, "contrast": 0, "gamma": 1.0}),
795
+ ("blur", {"sigma": 2}, "o/l_b.png", {"sigma": 2.0}),
796
+ ("sharpen", {"sigma": 2}, "o/l_s.png", {"sigma": 2.0}),
797
+ ("composite", {"over_path": "in/ov.png", "gravity": "southeast", "opacity": 1.0},
798
+ "o/l_k.png", {"over_path": "in/ov.png", "gravity": "southeast", "opacity": 1.0}),
799
+ ("mask", {"mask_path": "in/mk.png"}, "o/l_m.png",
800
+ {"mask_path": "in/mk.png", "mode": "set"}),
801
+ )
802
+
803
+
804
+ def xb_ledger_params(impl):
805
+ """② 参数入台账:新 op 每次成功的台账 params 必等于归一入参。"""
806
+ root = fixture(mk_root())
807
+ fails = []
808
+ for op, params, dst, want in LEDGER_CASES:
809
+ r = _run(impl, root, op, params, dst)
810
+ if not r.get("ok"):
811
+ fails.append(f"X-台账 {op} 前置运行失败({code_of(r)})")
812
+ continue
813
+ rec = S.read_audit(root)[-1]
814
+ if rec.get("params") != want:
815
+ fails.append(f"X-台账 params 与入参不符({op}:实得 {rec.get('params')!r})")
816
+ elif rec.get("output_path") != r["artifact_path"] \
817
+ or len(str(rec.get("input_sha256") or "")) != 64:
818
+ fails.append(f"X-台账 input/output 字段不符({op})")
819
+ return fails
820
+
821
+
822
+ def xc_idem(impl):
823
+ """② 同参幂等:同 key 同参 → 复用已存产物(且像素恒等)。"""
824
+ root = fixture(mk_root())
825
+ fails = []
826
+ for op, params, dst, _want in LEDGER_CASES:
827
+ kw = {"idempotency_key": f"k_{op}"}
828
+ r1 = _run(impl, root, op, params, dst, **kw)
829
+ r2 = _run(impl, root, op, params, dst, **kw)
830
+ if not (r1.get("ok") and r2.get("ok")):
831
+ fails.append(f"X-幂等 {op} 两次运行失败")
832
+ continue
833
+ if not os.path.isfile(abspath(root, r1["artifact_path"])):
834
+ fails.append(f"X-幂等 {op} 产物未落盘")
835
+ continue
836
+ if rgbof(abspath(root, r1["artifact_path"])) != rgbof(abspath(root, r2["artifact_path"])):
837
+ fails.append(f"X-幂等 {op} 同输入同参两次像素不等")
838
+ if not S.read_audit(root)[-1].get("reused_from"):
839
+ fails.append(f"X-幂等 {op} 同 key 未复用已存产物")
840
+ return fails
841
+
842
+
843
+ def xd_cjk_new_ops(impl):
844
+ """③ 中文路径:新 op 至少各过一遍(源/over/mask/产物全在中文目录内)。"""
845
+ root = mk_root()
846
+ mk_src(root, os.path.join("中文目录", "看板图.png"), (40, 30))
847
+ mk_over(root, os.path.join("中文目录", "水印块.png"), (10, 10))
848
+ mk_mask(root, os.path.join("中文目录", "遮罩.png"), (40, 30))
849
+ cjk_params = {
850
+ "thumbnail": {"max_edge": 20},
851
+ "crop": {"x": 1, "y": 2, "width": 8, "height": 6},
852
+ "rotate": {"degrees": 90},
853
+ "flip": {},
854
+ "flop": {},
855
+ "adjust": {"brightness": 10},
856
+ "blur": {"sigma": 1.5},
857
+ "sharpen": {"sigma": 1.5},
858
+ "composite": {"over_path": "中文目录/水印块.png", "gravity": "northwest"},
859
+ "mask": {"mask_path": "中文目录/遮罩.png"},
860
+ }
861
+ fails = []
862
+ for op, params in cjk_params.items():
863
+ dst = f"中文输出/结果_{op}.png"
864
+ r = _run(impl, root, op, params, dst, src="中文目录/看板图.png")
865
+ if not r.get("ok"):
866
+ fails.append(f"X-中文路径 {op} 失败({code_of(r)})")
867
+ continue
868
+ if not os.path.isfile(abspath(root, r["artifact_path"])):
869
+ fails.append(f"X-中文路径 {op} 产物未落盘")
870
+ return fails
871
+
872
+
873
+ def xe_errors(impl):
874
+ """④ 错误态:新 op 的坏参数 → 指定错误码(不靠消息文本)。"""
875
+ root = fixture(mk_root())
876
+ base = {"src": "in/p.png", "params": {}, "sandbox_root": root, "caller": "xe"}
877
+ cases = (
878
+ ("E_BAD_PARAM", dict(base, op="thumbnail", params={"max_edge": 0}, dst="o/e1.png")),
879
+ ("E_BAD_PARAM", dict(base, op="thumbnail", params={"max_edge": "20"}, dst="o/e2.png")),
880
+ ("E_BAD_PARAM", dict(base, op="crop", params={"x": 0, "y": 0, "width": 0, "height": 8}, dst="o/e3.png")),
881
+ ("E_BAD_PARAM", dict(base, op="crop", params={"x": 35, "y": 0, "width": 10, "height": 8}, dst="o/e4.png")),
882
+ ("E_BAD_PARAM", dict(base, op="crop", params={"x": -1, "y": 0, "width": 8, "height": 8}, dst="o/e5.png")),
883
+ ("E_BAD_PARAM", dict(base, op="rotate", params={"degrees": "90"}, dst="o/e6.png")),
884
+ ("E_BAD_PARAM", dict(base, op="rotate", params={}, dst="o/e7.png")),
885
+ ("E_BAD_PARAM", dict(base, op="flip", params={"axis": "horizontal"}, dst="o/e8.png")),
886
+ ("E_BAD_PARAM", dict(base, op="flop", params={"axis": "vertical"}, dst="o/e9.png")),
887
+ ("E_BAD_PARAM", dict(base, op="adjust", params={}, dst="o/e10.png")),
888
+ ("E_BAD_PARAM", dict(base, op="adjust", params={"brightness": 200}, dst="o/e11.png")),
889
+ ("E_BAD_PARAM", dict(base, op="adjust", params={"gamma": 0}, dst="o/e12.png")),
890
+ ("E_BAD_PARAM", dict(base, op="blur", params={"sigma": 0}, dst="o/e13.png")),
891
+ ("E_BAD_PARAM", dict(base, op="blur", params={}, dst="o/e14.png")),
892
+ ("E_BAD_PARAM", dict(base, op="sharpen", params={"sigma": -1}, dst="o/e15.png")),
893
+ ("E_BAD_PARAM", dict(base, op="composite", params={}, dst="o/e16.png")),
894
+ ("E_BAD_PARAM", dict(base, op="composite", params={"over_path": "in/big.png"}, dst="o/e17.png")),
895
+ ("E_BAD_PARAM", dict(base, op="composite", params={"over_path": "in/ov.png", "gravity": "middle"}, dst="o/e18.png")),
896
+ ("E_BAD_PARAM", dict(base, op="composite", params={"over_path": "in/ov.png", "opacity": 2}, dst="o/e19.png")),
897
+ ("E_NOINPUT", dict(base, op="composite", params={"over_path": "in/none.png"}, dst="o/e20.png")),
898
+ ("E_PATH_OUT_OF_SCOPE", dict(base, op="composite", params={"over_path": "../esc.png"}, dst="o/e21.png")),
899
+ ("E_BAD_PARAM", dict(base, op="mask", params={"mask_path": "in/small_mask.png"}, dst="o/e22.png")),
900
+ ("E_BAD_PARAM", dict(base, op="mask", params={"mask_path": "in/mk.png", "mode": "bogus"}, dst="o/e23.png")),
901
+ ("E_UNSUPPORTED_OP", dict(base, op="mask", params={"mask_path": "in/mk.png", "mode": "mul"}, dst="o/e24.png")),
902
+ ("E_UNSUPPORTED_FORMAT", dict(base, op="mask", params={"mask_path": "in/mk.png"}, dst="o/e25.jpg")),
903
+ ("E_NOINPUT", dict(base, op="mask", params={"mask_path": "in/none.png"}, dst="o/e26.png")),
904
+ ("E_PATH_OUT_OF_SCOPE", dict(base, op="mask", params={"mask_path": "../esc.png"}, dst="o/e27.png")),
905
+ )
906
+ fails = []
907
+ for want, req in cases:
908
+ r = impl(req)
909
+ if r.get("ok") is not False:
910
+ fails.append(f"X-错误态 {req['op']} {want} 未拒绝(ok={r.get('ok')!r})")
911
+ elif code_of(r) != want:
912
+ fails.append(f"X-错误态 {req['op']} {want} 码不符(实得 {code_of(r)!r})")
913
+ return fails
914
+
915
+
916
+ def xf_scope(impl):
917
+ """`E_UNSUPPORTED_OP` 收窄:只对真正未实现者返回(已实现者一律不返回它)。"""
918
+ root = fixture(mk_root())
919
+ fails = []
920
+ impl_ops = {
921
+ "inspect": ({}, "o/z_i.png"), "resize": ({"width": 8}, "o/z_z.png"),
922
+ "convert": ({"format": "png"}, "o/z_c.png"), "thumbnail": ({"max_edge": 20}, "o/z_t.png"),
923
+ "crop": ({"x": 0, "y": 0, "width": 8, "height": 8}, "o/z_p.png"),
924
+ "rotate": ({"degrees": 90}, "o/z_r.png"), "flip": ({}, "o/z_f.png"),
925
+ "flop": ({}, "o/z_l.png"), "adjust": ({"gamma": 2.0}, "o/z_a.png"),
926
+ "blur": ({"sigma": 1}, "o/z_b.png"), "sharpen": ({"sigma": 1}, "o/z_s.png"),
927
+ "composite": ({"over_path": "in/ov.png"}, "o/z_k.png"),
928
+ "mask": ({"mask_path": "in/mk.png"}, "o/z_m.png"),
929
+ }
930
+ if set(impl_ops) != set(S.OPS_IMPL):
931
+ fails.append(f"X-范围 已实现集与守卫覆盖不一致({sorted(set(impl_ops) ^ set(S.OPS_IMPL))})")
932
+ try:
933
+ from . import imgskill as _S
934
+ if set(_S._OPS) != set(_S.OPS_IMPL):
935
+ fails.append("X-范围 实现表 _OPS 与 OPS_IMPL 不一致")
936
+ except Exception as e: # pragma: no cover
937
+ fails.append(f"X-范围 实现表自检失败({e!r})")
938
+ if set(S.OPS_OUT_OF_SCOPE) & set(S.OPS_IMPL):
939
+ fails.append("X-范围 范围外集与已实现集相交")
940
+ for op, (params, dst) in impl_ops.items():
941
+ r = _run(impl, root, op, params, dst)
942
+ if r.get("ok") is False and code_of(r) == "E_UNSUPPORTED_OP":
943
+ fails.append(f"X-范围 已实现 op {op} 仍返回 E_UNSUPPORTED_OP")
944
+ for op in ("extract", "export", "ocr", "bg_remove", "detect",
945
+ "generate", "i2i", "不存在的op"):
946
+ r = _run(impl, root, op, {}, "o/z_out.png")
947
+ if r.get("ok") is not False or code_of(r) != "E_UNSUPPORTED_OP":
948
+ fails.append(f"X-范围 范围外 op {op} 未返回 E_UNSUPPORTED_OP({code_of(r)!r})")
949
+ return fails
950
+
951
+
952
+ # ---------------------------------------------------------------- 定点变异件
953
+
954
+ def d_nonidem(req):
955
+ """G1 退化:偶数次调用后往产物里塞噪声(非幂等)。"""
956
+ r = S.run(req)
957
+ d_nonidem.n = getattr(d_nonidem, "n", 0) + 1
958
+ if r.get("ok") and r.get("artifact_path") and d_nonidem.n % 2 == 0:
959
+ p = abspath(req["sandbox_root"], r["artifact_path"])
960
+ im = Image.open(p).convert("RGB")
961
+ px = im.load()
962
+ px[0, 0] = tuple((c + 9) % 256 for c in px[0, 0])
963
+ im.save(p)
964
+ return r
965
+
966
+
967
+ def d_ascii_only(req):
968
+ """G2 退化:路径含非 ASCII 即拒(中文路径不可用)。"""
969
+ for k in ("src", "dst"):
970
+ v = req.get(k)
971
+ if isinstance(v, str) and any(ord(c) > 127 for c in v):
972
+ return {"ok": False, "artifact_path": None, "asset": None, "meta": None,
973
+ "audit_id": None, "backend": None,
974
+ "error": {"code": "E_BAD_PARAM", "message": "ascii only",
975
+ "detail": None}}
976
+ return S.run(req)
977
+
978
+
979
+ def d_blank_noinput(req):
980
+ """G3 退化:E_NOINPUT 时把错误码抹成 None(不可判别)。"""
981
+ r = S.run(req)
982
+ if r.get("error") and r["error"].get("code") == "E_NOINPUT":
983
+ r["error"]["code"] = None
984
+ return r
985
+
986
+
987
+ def d_drop_env(req):
988
+ """G4 退化:台账末行抹掉公共字段 env。"""
989
+ r = S.run(req)
990
+ p = S.audit_path(req["sandbox_root"])
991
+ with open(p, "r", encoding="utf-8") as f:
992
+ lines = [ln for ln in f.read().splitlines() if ln.strip()]
993
+ rec = json.loads(lines[-1])
994
+ rec.pop("env", None)
995
+ lines[-1] = json.dumps(rec, ensure_ascii=False, sort_keys=True)
996
+ with open(p, "w", encoding="utf-8") as f:
997
+ f.write("\n".join(lines) + "\n")
998
+ return r
999
+
1000
+
1001
+ def d_outside(req):
1002
+ """G5③ 退化:产物路径报成沙箱根之外。"""
1003
+ r = S.run(req)
1004
+ if r.get("ok"):
1005
+ r["artifact_path"] = os.path.abspath(
1006
+ os.path.join(os.path.dirname(req["sandbox_root"]), "escaped.png"))
1007
+ return r
1008
+
1009
+
1010
+ def d_implicit_root(req):
1011
+ """G7 退化:未给沙箱根时静默顶一个隐式根(静默放行)。"""
1012
+ if not req.get("sandbox_root"):
1013
+ req = dict(req, sandbox_root=tempfile.gettempdir())
1014
+ return S.run(req)
1015
+
1016
+
1017
+ def prove(gname, fn, degraded, expected):
1018
+ """定点变异自证:退化件跑出**恰好**预期红项(不连坐)。"""
1019
+ got = set(fn(degraded))
1020
+ ok(got == set(expected),
1021
+ f"{gname} 定点变异自证:注入退化 → 恰好打中 {sorted(expected)}(实得 {sorted(got)})")
1022
+
1023
+
1024
+ # ---------------------------------------------------------------- 新增 op 的定点变异件
1025
+ #
1026
+ # 每件只对**一个** op/参数组合退化,使新增判据的失败串**定点唯一**——证明新断言是
1027
+ # 「能红」的判据而非空转(与 G1–G7 的 prove 同款做法)。
1028
+
1029
+ def d_thumb_noop(req):
1030
+ """X-thumbnail 退化:max_edge=20 的产物写成源图整幅(最长边不再是 max_edge)。"""
1031
+ r = S.run(req)
1032
+ if r.get("ok") and req.get("op") == "thumbnail" \
1033
+ and (req.get("params") or {}).get("max_edge") == 20:
1034
+ shutil.copyfile(abspath(req["sandbox_root"], req["src"]),
1035
+ abspath(req["sandbox_root"], r["artifact_path"]))
1036
+ return r
1037
+
1038
+
1039
+ def d_crop_offset(req):
1040
+ """X-crop 退化:裁错窗口(尺寸对、内容错)→ 只打中内容条。"""
1041
+ r = S.run(req)
1042
+ if r.get("ok") and req.get("op") == "crop":
1043
+ p = req["params"]
1044
+ x, y, w, h = p["x"], p["y"], p["width"], p["height"]
1045
+ Image.open(abspath(req["sandbox_root"], req["src"])).convert("RGB") \
1046
+ .crop((x + 1, y + 1, x + 1 + w, y + 1 + h)) \
1047
+ .save(abspath(req["sandbox_root"], r["artifact_path"]))
1048
+ return r
1049
+
1050
+
1051
+ def d_rot_ccw(req):
1052
+ """X-rotate 退化:把 +90 做成**逆时针**(方向漂移)→ 只打中方向条。"""
1053
+ r = S.run(req)
1054
+ if r.get("ok") and req.get("op") == "rotate" \
1055
+ and (req.get("params") or {}).get("degrees") == 90:
1056
+ Image.open(abspath(req["sandbox_root"], req["src"])).convert("RGB") \
1057
+ .rotate(90, expand=True, fillcolor=(255, 255, 255)) \
1058
+ .save(abspath(req["sandbox_root"], r["artifact_path"]))
1059
+ return r
1060
+
1061
+
1062
+ def d_flip_as_flop(req):
1063
+ """X-mirror 退化:flip 写成左右镜像(两 op 未分离)→ 只打中 flip 的像素条。"""
1064
+ r = S.run(req)
1065
+ if r.get("ok") and req.get("op") == "flip":
1066
+ p = abspath(req["sandbox_root"], r["artifact_path"])
1067
+ Image.open(p).transpose(Image.Transpose.FLIP_LEFT_RIGHT).save(p)
1068
+ return r
1069
+
1070
+
1071
+ def d_ledger_param_thumb(req):
1072
+ """X-台账 退化:thumbnail 的台账 params 抹成空(同参幂等的比对键随之失效)。"""
1073
+ r = S.run(req)
1074
+ if r.get("ok") and req.get("op") == "thumbnail":
1075
+ p = S.audit_path(req["sandbox_root"])
1076
+ with open(p, "r", encoding="utf-8") as f:
1077
+ lines = [ln for ln in f.read().splitlines() if ln.strip()]
1078
+ if lines:
1079
+ rec = json.loads(lines[-1])
1080
+ rec["params"] = {}
1081
+ lines[-1] = json.dumps(rec, ensure_ascii=False, sort_keys=True)
1082
+ with open(p, "w", encoding="utf-8") as f:
1083
+ f.write("\n".join(lines) + "\n")
1084
+ return r
1085
+
1086
+
1087
+ def d_mask_rgb(req):
1088
+ """X-mask 退化:产物抹掉 alpha(存成 RGB)→ 只打中「无 alpha 通道」条。"""
1089
+ r = S.run(req)
1090
+ if r.get("ok") and req.get("op") == "mask":
1091
+ p = abspath(req["sandbox_root"], r["artifact_path"])
1092
+ Image.open(p).convert("RGB").save(p)
1093
+ return r
1094
+
1095
+
1096
+ def d_no_reuse(req):
1097
+ """X-幂等 退化:抹掉 rotate 的 idempotency_key(复用永不命中)→ 只打中复用条。"""
1098
+ if req.get("op") == "rotate":
1099
+ req = dict(req, idempotency_key=None)
1100
+ return S.run(req)
1101
+
1102
+
1103
+ # ---------------------------------------------------------------- Y CLI 暴露面
1104
+ #
1105
+ # **结构防悬空**:CLI 入口必须**真能走到 impl**(`run()`),且**摘掉入口必须转红**。
1106
+ # 同形先例:`test_curiosity_budget.py` C6(生产入口真走到预算门)、`test_gain_gate.py`
1107
+ # 的生产路径断言。判据一律以**子进程跑真 CLI**(`-m md_cg.imgskill`)为准——不靠同进程
1108
+ # 内 `cli_main()`,否则「入口命令真的可跑」这条根本没被打到。
1109
+
1110
+ _CLI_MAIN = ("-m", "md_cg.imgskill")
1111
+ #: `__main__` 入口钩子锚点(须在源码里**恰好** 1 处;缺/重 → 判红)
1112
+ _CLI_ENTRY_ANCHOR = 'if __name__ == "__main__":'
1113
+ #: `run()` 出参的键面(CLI 出参须逐项在场)
1114
+ _CLI_FIELDS = ("ok", "artifact_path", "asset", "meta", "audit_id", "backend", "error")
1115
+ #: 仓根(`_SRC` == `<仓根>/md_cg/imgskill.py`)——Y1i 用它显式指定子进程 cwd
1116
+ _CLI_REPO = os.path.dirname(os.path.dirname(_SRC))
1117
+ #: 仓根相对路径、且在仓内**确实存在**——Y1i① 用它判「路径根锚定而非 cwd 锚定」
1118
+ _CLI_REPO_REL = "md_cg/imgskill.py"
1119
+
1120
+
1121
+ def _cli_src_text():
1122
+ with open(_SRC, "r", encoding="utf-8") as f:
1123
+ return f.read()
1124
+
1125
+
1126
+ def _cli_probe(inv, *args, env=None, cwd=None):
1127
+ """子进程跑 CLI,返回 `(rc, 出参 dict|None, stdout, stderr)`。
1128
+
1129
+ `inv` = `("-m", "md_cg.imgskill")`(真件)或 `("<变异件路径>",)`(变异件以脚本跑)。
1130
+ 子进程统一 argv 列表 + 显式 UTF-8(纪律 15;不拼 shell 串)。
1131
+ `cwd` 缺省 `None` = 继承;**显式给**时用于「路径是否按 cwd 解析」类判据(Y1i)。
1132
+ """
1133
+ e = dict(os.environ)
1134
+ e["PYTHONUTF8"] = "1"
1135
+ if env:
1136
+ e.update(env)
1137
+ cp = subprocess.run([sys.executable, "-X", "utf8"] + list(inv) + list(args),
1138
+ shell=False, capture_output=True, encoding="utf-8",
1139
+ errors="replace", timeout=180, env=e, cwd=cwd)
1140
+ obj = None
1141
+ lines = [ln for ln in (cp.stdout or "").strip().splitlines() if ln.strip()]
1142
+ if lines:
1143
+ try:
1144
+ obj = json.loads(lines[-1])
1145
+ except ValueError:
1146
+ obj = None
1147
+ return cp.returncode, obj, (cp.stdout or ""), (cp.stderr or "")
1148
+
1149
+
1150
+ def _cli_ok(rc, obj):
1151
+ """CLI 判据谓词(真件与变异件**共用**,故可作定点变异的打靶对象)。
1152
+
1153
+ 判据:rc==0 且 stdout 是可解析 JSON 且出参键面齐且 `ok=true`。
1154
+ """
1155
+ return (rc == 0 and isinstance(obj, dict)
1156
+ and set(_CLI_FIELDS) <= set(obj) and obj.get("ok") is True)
1157
+
1158
+
1159
+ def _cli_subcommands():
1160
+ """CLI 已注册的子命令名集(从解析器结构读,不硬编码)。"""
1161
+ for a in S.build_parser()._actions:
1162
+ if a.__class__.__name__ == "_SubParsersAction":
1163
+ return set(a.choices)
1164
+ return set()
1165
+
1166
+
1167
+ def ya_cli_reaches_impl(_impl):
1168
+ """Y1 暴露面落地:`--help` 列 op;inspect/resize/convert 子进程真跑到 `run()`。"""
1169
+ fails = []
1170
+ root = mk_root()
1171
+ mk_src(root, "in/p.png", (40, 30))
1172
+
1173
+ # Y1a `--help` 列全部已实现 op 且 rc=0
1174
+ rc, _obj, out, err = _cli_probe(_CLI_MAIN, "--help")
1175
+ if rc != 0 or not out.strip():
1176
+ fails.append(f"Y1 CLI --help 非 rc=0 或无输出(rc={rc};{err.strip()[:120]})")
1177
+ else:
1178
+ miss = [op for op in S.OPS_IMPL if op not in out]
1179
+ if miss:
1180
+ fails.append(f"Y1 CLI --help 未列 op:{miss}")
1181
+
1182
+ # Y1b/Y1c inspect:rc=0、字段齐、且读数与库调用**逐项一致**(同一实现路径的证据)
1183
+ lib = S.run({"op": "inspect", "src": "in/p.png", "sandbox_root": root,
1184
+ "caller": "y-lib"})
1185
+ n_before = len(S.read_audit(root))
1186
+ rc, obj, out, err = _cli_probe(_CLI_MAIN, "inspect", "in/p.png", "--root", root)
1187
+ if not _cli_ok(rc, obj):
1188
+ fails.append(f"Y1 CLI inspect 子进程未过判据(rc={rc};{err.strip()[:120]})")
1189
+ else:
1190
+ if not isinstance(obj.get("meta"), dict) \
1191
+ or {"format", "width", "height", "sha256"} - set(obj["meta"]):
1192
+ fails.append(f"Y1 CLI inspect meta 字段不齐({obj.get('meta')!r})")
1193
+ elif obj["meta"] != lib.get("meta"):
1194
+ fails.append("Y1 CLI inspect 读数与库 run() 不一致(疑两条实现路径)")
1195
+ if len(S.read_audit(root)) != n_before + 1:
1196
+ fails.append("Y1 CLI inspect 未落审计台账(子进程未走 run())")
1197
+
1198
+ # Y1d resize:产物落盘且尺寸 == 参数要求(width=20 → 20x15 保持纵横比)
1199
+ rc, obj, out, err = _cli_probe(_CLI_MAIN, "resize", "in/p.png",
1200
+ "--width", "20", "--dst", "o/cli_r.png",
1201
+ "--root", root)
1202
+ if not _cli_ok(rc, obj):
1203
+ fails.append(f"Y1 CLI resize 子进程未过判据(rc={rc};{err.strip()[:120]})")
1204
+ elif Image.open(abspath(root, obj["artifact_path"])).size != (20, 15):
1205
+ fails.append(f"Y1 CLI resize 产物尺寸不符({obj['artifact_path']})")
1206
+
1207
+ # Y1e convert:产物格式 == WEBP
1208
+ rc, obj, out, err = _cli_probe(_CLI_MAIN, "convert", "in/p.png",
1209
+ "--format", "webp", "--dst", "o/cli_c.webp",
1210
+ "--root", root)
1211
+ if not _cli_ok(rc, obj):
1212
+ fails.append(f"Y1 CLI convert 子进程未过判据(rc={rc};{err.strip()[:120]})")
1213
+ elif (obj.get("meta") or {}).get("format") != "WEBP":
1214
+ fails.append(f"Y1 CLI convert 格式不符({(obj.get('meta') or {}).get('format')!r})")
1215
+
1216
+ # Y1f 未给 --root:fail-closed(rc=1、E_BAD_PARAM)——CLI 没绕开 run() 的判据
1217
+ rc, obj, out, err = _cli_probe(_CLI_MAIN, "inspect", "in/p.png")
1218
+ code = (obj or {}).get("error", {}).get("code") if isinstance(obj, dict) else None
1219
+ if rc != 1 or not isinstance(obj, dict) or obj.get("ok") is not False \
1220
+ or code != S.E_BAD_PARAM:
1221
+ fails.append(f"Y1 CLI 未给 --root 未 fail-closed(rc={rc};code={code!r})")
1222
+
1223
+ # Y1g op 面三处一致:_CLI_OPS 键集 == OPS_IMPL == 解析器子命令集
1224
+ if set(S._CLI_OPS) != set(S.OPS_IMPL) or _cli_subcommands() != set(S.OPS_IMPL):
1225
+ fails.append("Y1 CLI op 面与 OPS_IMPL 不一致"
1226
+ f"(_CLI_OPS={sorted(S._CLI_OPS)} 子命令={sorted(_cli_subcommands())})")
1227
+
1228
+ # Y1h 范围外 op **在 CLI 上**走用法错 rc=2(库面 run() 才是 E_UNSUPPORTED_OP)。
1229
+ # 该差异契约 §12.2 显式登记——此处用测试钉住,防「登记了却无人测」的漂移。
1230
+ oos = S.OPS_OUT_OF_SCOPE[0]
1231
+ rc, obj, out, err = _cli_probe(_CLI_MAIN, oos, "in/p.png", "--root", root)
1232
+ if rc != 2:
1233
+ fails.append(f"Y1 CLI 范围外 op({oos})未走 argparse 用法错 rc=2(rc={rc})"
1234
+ "——§12.2 登记的差异未钉住")
1235
+
1236
+ # Y1i 路径口径(编排侧亲跑暴露,已写进 --help 与契约 §12.6):路径一律相对沙箱根解析。
1237
+ # ① 仓内**确实存在**的仓根相对路径(子进程 cwd = 仓根,故它按 cwd 真能找到)→ 仍须
1238
+ # `E_NOINPUT`——这正是「根锚定、非 cwd 锚定」的可判形态;若日后有人给解析加 cwd
1239
+ # 兜底(破坏 §九-#8),此例会变成能找到文件而转红。
1240
+ # ② 根外绝对路径 → `E_PATH_OUT_OF_SCOPE`(越界闸在先)。
1241
+ rc, obj, out, err = _cli_probe(_CLI_MAIN, "inspect", _CLI_REPO_REL,
1242
+ "--root", root, cwd=_CLI_REPO)
1243
+ c1 = ((obj or {}).get("error") or {}).get("code")
1244
+ if rc != 1 or c1 != S.E_NOINPUT:
1245
+ fails.append(f"Y1 CLI 仓根相对路径未按「根」解析(rc={rc};code={c1!r},"
1246
+ "期望 E_NOINPUT——路径须根锚定、不得 cwd 兜底)")
1247
+ rc, obj, out, err = _cli_probe(_CLI_MAIN, "inspect", _SRC, "--root", root)
1248
+ c2 = ((obj or {}).get("error") or {}).get("code")
1249
+ if rc != 1 or c2 != S.E_PATH_OUT_OF_SCOPE:
1250
+ fails.append(f"Y1 CLI 根外绝对路径未越界拒绝(rc={rc};code={c2!r},"
1251
+ "期望 E_PATH_OUT_OF_SCOPE)")
1252
+ return fails
1253
+
1254
+
1255
+ def _write_mutant_no_entry():
1256
+ """把 `imgskill.py` 的 `__main__` 入口块**摘掉**,写成一个自足临时模块。
1257
+
1258
+ 该模块零 `md_cg` 相对 import(G5① 已断言),故可直接以脚本跑——「入口被摘」= 跑
1259
+ 起来不再产生任何 CLI 行为(无出参),正是「悬空」的形态。
1260
+ """
1261
+ lines = _cli_src_text().splitlines(keepends=True)
1262
+ idx = [i for i, ln in enumerate(lines) if ln.strip().startswith(_CLI_ENTRY_ANCHOR)]
1263
+ d = tempfile.mkdtemp(prefix="imgskill_mut_")
1264
+ _ROOTS.append(d)
1265
+ p = os.path.join(d, "imgskill_noentry.py")
1266
+ with open(p, "w", encoding="utf-8") as f:
1267
+ f.write("".join(lines[:idx[0]] if idx else lines))
1268
+ return p, len(idx)
1269
+
1270
+
1271
+ def yb_entry_off_red():
1272
+ """Y2 结构防悬空:锚点在位 + 真件为绿 + **摘掉入口恰好转红**(定点变异自证)。"""
1273
+ root = mk_root()
1274
+ mk_src(root, "in/p.png", (40, 30))
1275
+ mutant, n_anchor = _write_mutant_no_entry()
1276
+ ok(n_anchor == 1, f"Y2 结构锚点:`{_CLI_ENTRY_ANCHOR}` 在 imgskill.py 恰好 1 处"
1277
+ f"(实得 {n_anchor})")
1278
+ real = _cli_ok(*_cli_probe(_CLI_MAIN, "inspect", "in/p.png",
1279
+ "--root", root)[:2])
1280
+ ok(real, "Y2 真件 CLI(入口在位)判据为绿")
1281
+ got = _cli_ok(*_cli_probe((mutant,), "inspect", "in/p.png",
1282
+ "--root", root)[:2])
1283
+ ok((not got) and real,
1284
+ f"Y2 结构防悬空变异:摘掉 `__main__` 入口 → CLI 判据转红(真件绿={real} "
1285
+ f"变异件绿={got})——同一判据,入口摘掉即失判")
1286
+
1287
+
1288
+ # ---------------------------------------------------------------- 主流程
1289
+
1290
+ def main():
1291
+ b = S.resolve_backend()
1292
+ reading("后端路由", b[0])
1293
+ reading("后端版本", b[2])
1294
+ reading("降级留痕", b[3])
1295
+ real = S.run
1296
+
1297
+ # ---------- G1 ----------
1298
+ f = g1_idempotent(real)
1299
+ ok(not f, f"G1 幂等(像素级门):同输入同参 → 逐像素相等 [{f}]")
1300
+ prove("G1", g1_idempotent, d_nonidem, {"G1 同输入同参两次像素不等"})
1301
+
1302
+ # ---------- G2 ----------
1303
+ f = g2_cjk_path(real)
1304
+ ok(not f, f"G2 中文路径(目录+文件名)resize/convert 全链可用 [{f}]")
1305
+ prove("G2", g2_cjk_path, d_ascii_only,
1306
+ {"G2 中文路径 resize 失败(E_BAD_PARAM)"})
1307
+
1308
+ # ---------- G3 ----------
1309
+ f = g3_error_codes(real)
1310
+ ok(not f, f"G3 错误态可判别:8 类 → 指定错误码(必需 4 类已含)[{f}]")
1311
+ prove("G3", g3_error_codes, d_blank_noinput,
1312
+ {"G3 E_NOINPUT 码不符(实得 None)"})
1313
+
1314
+ # ---------- G4 ----------
1315
+ f = g4_audit_fields(real)
1316
+ ok(not f, f"G4 审计字段齐(§4.1 全在场)[{f}]")
1317
+ prove("G4", g4_audit_fields, d_drop_env, {"G4 缺字段: env"})
1318
+
1319
+ # ---------- G5 ----------
1320
+ src = _src_text()
1321
+ vi = check_import_face(src)
1322
+ ok(not vi, f"G5① imgskill 源码 import 面零 md_cg 依赖、不含 judge/trust/writepipe [{vi}]")
1323
+ # 附加读数(**非门**,据实报):运行期模块图的禁用面来自**包 `__init__.py`**
1324
+ # 的连带(`mdcg→…→trust`),不是 imgskill 自身的 import——本仓任一同族模块皆然。
1325
+ forb = sorted(m for m in sys.modules
1326
+ if m.startswith("md_cg") and m.rsplit(".", 1)[-1].lower() in _FORBIDDEN)
1327
+ reading("G5① 运行期连带载入的禁用面(来自包 __init__,非 imgskill import)", forb)
1328
+ vb = check_no_bare_convert(src)
1329
+ ok(not vb, f"G5①/G6 imgskill 源码无裸 convert/无 shell=True [{vb}]")
1330
+ # G6 守卫源码自身不被误判(负样例零命中)
1331
+ guard_src = open(os.path.abspath(__file__), "r", encoding="utf-8").read()
1332
+ ok(not check_no_bare_convert(guard_src),
1333
+ "G6 自检:守卫源码自身不被误判(负样例零命中)")
1334
+ f = g5_in_root(real)
1335
+ ok(not f, f"G5③ 产物落点必在调用方给的沙箱根内 [{f}]")
1336
+ prove("G5③", g5_in_root, d_outside, {"G5③ 产物越出沙箱根"})
1337
+ # 定点变异(源码面):注入 `from . import trust` → 恰好打中预期红项
1338
+ mutated_import = src + "\nfrom . import trust\n"
1339
+ hits = check_import_face(mutated_import)
1340
+ ok(len(hits) == 2 and any("相对 import" in h for h in hits)
1341
+ and any("trust" in h for h in hits),
1342
+ f"G5① 定点变异自证:注入 `from . import trust` → 恰好打中 2 项(相对 import + 禁用模块);"
1343
+ f"实得 {hits}")
1344
+
1345
+ # ---------- G6 ----------
1346
+ # 定点变异(源码面):注入裸 convert argv[0] → 恰好打中
1347
+ bad_line = 'subprocess.run(["' + _BARE_CONVERT[0] + '", a, b])'
1348
+ mut = src + "\n" + bad_line + "\n"
1349
+ hits = check_no_bare_convert(mut)
1350
+ ok(len(hits) == 1 and hits[0].startswith("argv[0] 裸 convert"),
1351
+ f"G6 定点变异自证:注入裸 convert argv[0] → 恰好打中 1 项(实得 {hits})")
1352
+ # 正样例:合法 magick 调用不得被误判
1353
+ good_line = 'subprocess.run([exe, "-strip", dst])'
1354
+ ok(not check_no_bare_convert(good_line + "\n"),
1355
+ "G6 正样例:`[exe, \"-strip\", dst]` 不被误判")
1356
+
1357
+ # ---------- G7 ----------
1358
+ f = g7_fail_closed(real)
1359
+ ok(not f, f"G7 fail-closed:未给根/越界/等级降级/覆 src → 一律拒绝 [{f}]")
1360
+ prove("G7", g7_fail_closed, d_implicit_root,
1361
+ {"G7 未给根错误码不符('E_NOINPUT')"})
1362
+
1363
+ # ---------- X 新增 9 op:逐行验收判据(§五) ----------
1364
+ f = xa_thumbnail(real)
1365
+ ok(not f, f"X-thumbnail 验收:最长边 == max_edge(缩/放两向、纵横比保持)[{f}]")
1366
+ prove("X-thumbnail", xa_thumbnail, d_thumb_noop,
1367
+ {"X-thumbnail 落盘产物最长边 != max_edge(max_edge=20 实得 (40, 30))"})
1368
+
1369
+ f = xa_crop(real)
1370
+ ok(not f, f"X-crop 验收:产物尺寸 == 裁窗 且内容对应 [{f}]")
1371
+ prove("X-crop", xa_crop, d_crop_offset, {"X-crop 裁窗内容与源不对应"})
1372
+
1373
+ f = xa_rotate(real)
1374
+ ok(not f, f"X-rotate 验收:90 倍数尺寸对调 + 正角顺时针 + 非直角外接矩形 [{f}]")
1375
+ prove("X-rotate", xa_rotate, d_rot_ccw, {"X-rotate 90 像素与「正角=顺时针」不符"})
1376
+
1377
+ f = xa_mirror(real)
1378
+ ok(not f, f"X-mirror 验收:flip/flop 与源同尺寸且镜像像素可断言 [{f}]")
1379
+ prove("X-mirror", xa_mirror, d_flip_as_flop, {"X-mirror flip 像素非上下镜像"})
1380
+
1381
+ f = xa_adjust(real)
1382
+ ok(not f, f"X-adjust 验收:参数入台账 + 同参幂等(亮度方向单调)[{f}]")
1383
+
1384
+ f = xa_blur(real)
1385
+ ok(not f, f"X-blur 验收:参数入台账 + 同参幂等(变平滑)[{f}]")
1386
+
1387
+ f = xa_sharpen(real)
1388
+ ok(not f, f"X-sharpen 验收:参数入台账 + 同参幂等(像素变化)[{f}]")
1389
+
1390
+ f = xa_composite(real)
1391
+ ok(not f, f"X-composite 验收:尺寸同 base + 合成位置(gravity 落位/半透明)[{f}]")
1392
+
1393
+ f = xa_mask(real)
1394
+ ok(not f, f"X-mask 验收:alpha 通道存在且可读回(逐像素对 mask)[{f}]")
1395
+ prove("X-mask", xa_mask, d_mask_rgb, {"X-mask 产物无 alpha 通道"})
1396
+
1397
+ # ---------- X 台账参数 / 同参幂等 ----------
1398
+ f = xb_ledger_params(real)
1399
+ ok(not f, f"X-台账 参数入台账:9 op 的 params 归一值逐项在场 [{f}]")
1400
+ prove("X-台账", xb_ledger_params, d_ledger_param_thumb,
1401
+ {"X-台账 params 与入参不符(thumbnail:实得 {})"})
1402
+
1403
+ f = xc_idem(real)
1404
+ ok(not f, f"X-幂等 同参幂等:同 key 复用已存产物 + 像素恒等 [{f}]")
1405
+ prove("X-幂等", xc_idem, d_no_reuse, {"X-幂等 rotate 同 key 未复用已存产物"})
1406
+
1407
+ # ---------- X 中文路径 ----------
1408
+ f = xd_cjk_new_ops(real)
1409
+ ok(not f, f"X-中文路径:9 op 全链(源/over/mask/产物均中文名)可用 [{f}]")
1410
+
1411
+ # ---------- X 错误态 ----------
1412
+ f = xe_errors(real)
1413
+ ok(not f, f"X-错误态:新 op 坏参数 → 指定错误码(27 例)[{f}]")
1414
+
1415
+ # ---------- X 范围收窄 ----------
1416
+ f = xf_scope(real)
1417
+ ok(not f, f"X-范围 E_UNSUPPORTED_OP 收窄:只对真正未实现者返回 [{f}]")
1418
+
1419
+ # ---------- Y CLI 暴露面(结构防悬空) ----------
1420
+ f = ya_cli_reaches_impl(real)
1421
+ ok(not f, f"Y1 CLI 暴露面:子进程跑 -m md_cg.imgskill 真走到 run()"
1422
+ f"(inspect/resize/convert 落盘读数齐)[{f}]")
1423
+ yb_entry_off_red()
1424
+
1425
+ print(f"\nimgskill: {PASS} passed, {FAIL} failed")
1426
+ for k, v in READINGS:
1427
+ print(f" [读数] {k}: {v}")
1428
+ if FAILS:
1429
+ for x in FAILS:
1430
+ print(f" - {x}")
1431
+ return 1 if FAIL else 0
1432
+
1433
+
1434
+ def _cleanup():
1435
+ for r in _ROOTS:
1436
+ shutil.rmtree(r, ignore_errors=True)
1437
+
1438
+
1439
+ if __name__ == "__main__":
1440
+ try:
1441
+ rc = main()
1442
+ finally:
1443
+ _cleanup()
1444
+ raise SystemExit(rc)