API Reference¶
Auto-generated reference for the public akms_learn surface. Every symbol below
is exported from the package root (from akms_learn import ...). For narrative
guidance see the Usage guide; for the model behind these symbols see
Core Concepts.
Compiler¶
The nine-stage pipeline entry point, its result bundle, and the stage tuple.
compile_learning_source¶
akms_learn.compile_learning_source
¶
compile_learning_source(
request: LearningRequest | dict[str, Any],
graph_path: str | Path | None = None,
graph_slice: GraphSlice | dict[str, Any] | None = None,
output_dir: str | Path | None = None,
domain_pack_paths: list[str | Path] | None = None,
source_pack_paths: list[str | Path] | None = None,
) -> CompileResult
Run the 9-stage LSP compiler pipeline.
Parameters¶
request:
A :class:LearningRequest instance OR a raw dict that
:func:normalize_request will canonicalise.
graph_path:
Filesystem path to a graph JSON. Mutually exclusive with
graph_slice.
graph_slice:
Either a :class:GraphSlice instance or a raw dict payload.
Mutually exclusive with graph_path.
output_dir:
If provided, the canonical packet JSON is written to
<output_dir>/<request_hash>.json (Stage 9). Directory is created
on demand.
domain_pack_paths:
Optional list of paths to domain_pack.yaml files (or directories
containing one). Loaded into a :class:DomainPackRegistry whose
descriptors are attached to the packet body.
source_pack_paths:
Optional list of paths to source-pack YAMLs whose descriptors are
attached to the packet body.
Returns¶
CompileResult Wraps the validated packet, any export paths, warnings, and the stage execution log.
Raises¶
LearningCapabilityError
If a required capability is unavailable, or a domain-pack /
source-pack path is missing or invalid.
PacketValidationError
If the assembled packet violates a hard cross-field invariant.
ValueError
If neither / both of graph_path and graph_slice are given.
Source code in packages/akms_learn/src/akms_learn/compiler.py
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 | |
CompileResult¶
akms_learn.CompileResult
dataclass
¶
CompileResult(
packet: LearningSourcePacket,
packet_path: Path | None = None,
export_paths: list[Path] = list(),
warnings: list[LearningWarning] = list(),
unavailable_capabilities: list[str] = list(),
stage_log: list[str] = list(),
)
Result bundle returned by :func:compile_learning_source.
Fields¶
packet:
The fully-assembled, validated :class:LearningSourcePacket.
packet_path:
Filesystem path the canonical JSON packet was written to (Stage 9),
or None when no output_dir was supplied.
export_paths:
Paths of every artifact produced by Stage 9 exporters. Always
includes packet_path when present.
warnings:
Accumulated :class:LearningWarning instances from every stage.
unavailable_capabilities:
Capability strings that were requested but not satisfied by either
the static plugin set or the domain-pack registry.
stage_log:
List of stage names appended after each stage completes. Always
ends equal to STAGES on success.
STAGES¶
akms_learn.STAGES
module-attribute
¶
STAGES: tuple[str, ...] = (
"plugin_compat_check",
"request_normalization",
"graph_source_resolution",
"seed_tag_handling",
"slice_conversion",
"learning_ordering",
"section_extraction",
"packet_assembly_and_validation",
"export",
)
Requests¶
The learning-request model, its canonical normalization, and the request hash.
LearningRequest¶
akms_learn.LearningRequest
¶
Bases: BaseModel
Input model for a learning request (plan §10).
Only these 11 fields contribute to request_hash. UI-only state from
Logic-Loom (preview_mode, ui_theme, session_id, ...) is rejected at
normalize_request time and never reaches the hash.
audience, depth, generation_option are kept as free-form
str rather than Literal to avoid coupling Phase 2 to a frozen
enum set; normalize_request lowercases them so case variations
collapse to a single canonical form.
normalize_request¶
akms_learn.normalize_request
¶
Return the canonical dict representation of a learning request.
The output is a plain dict containing exactly the 11 normalized
fields (see :data:NORMALIZED_FIELDS), with extras dropped and lists
sorted. Calling :func:request_hash on the result yields a byte-stable
SHA-256 digest (plan §10, L203).
Behavior:
- Accepts either a raw
dict(e.g. from JSON / Logic-Loom UI) or a validated :class:LearningRequestinstance. - Drops keys not in the 11-field schema — including Logic-Loom UI state
such as
preview_mode,ui_theme,session_id. topic/goalare.strip()only (case preserved).audience/depth/generation_optionare trimmed + lowercased.seed_tags/exporterselements are trimmed + lowercased + sorted.- Missing optional fields receive documented defaults.
Source code in packages/akms_learn/src/akms_learn/requests.py
to_canonical_dict¶
akms_learn.to_canonical_dict
¶
Convenience alias for :func:normalize_request.
Provided so call-sites that read more naturally as "give me the canonical
dict for this request" don't have to import normalize_request
directly. Returns the same canonical form (same 11 keys, same rules).
Source code in packages/akms_learn/src/akms_learn/requests.py
request_hash¶
akms_learn.request_hash
¶
Return the 64-char hex SHA-256 digest of the canonical JSON form.
The input is expected to be the output of :func:normalize_request
(or an equivalently shaped dict). The function is idempotent for
pre-normalized input and byte-identical across Python sessions because:
sort_keys=Truemakes the JSON form key-order-invariant.separators=(",", ":")removes incidental whitespace.ensure_ascii=Falsekeeps non-ASCII characters in their native UTF-8 form so the digest does not depend on locale or escape choice.
See :func:normalize_request for the canonical key set and rules.
Source code in packages/akms_learn/src/akms_learn/requests.py
Graph import¶
The in-memory graph slice and helpers, including the demo fixture.
GraphSlice¶
akms_learn.GraphSlice
¶
Bases: BaseModel
In-memory representation of an AKMS graph slice.
Fields mirror the top-level structure of a graph.json produced by the
AKMS compiler:
nodes– list of node dicts (raw AKMS schema dicts, not typed).edges– list of edge dicts.metadata– free-form graph-level metadata (version, build_time, …).
The model is frozen so that equal slices hash identically and can be
used as dict keys or set members. GraphSlice instances are immutable
after construction; mutate by constructing a new instance.
load_graph¶
akms_learn.load_graph
¶
load_graph(
graph_path: str | Path | None = None,
graph_slice: dict[str, Any] | None = None,
) -> GraphSlice
Dispatcher: load a GraphSlice from exactly one of the two sources.
Parameters¶
graph_path:
If provided, read the graph from this filesystem path.
graph_slice:
If provided, validate this in-memory dict into a GraphSlice.
Returns¶
GraphSlice
Raises¶
ValueError
If both graph_path and graph_slice are provided (mutual
exclusion), or if neither is provided.
Source code in packages/akms_learn/src/akms_learn/graph_import.py
compute_graph_hash¶
akms_learn.compute_graph_hash
¶
Return a deterministic SHA-256 hex digest for graph_slice.
The recipe is identical to request_hash in requests.py:
- Build a canonical
dictfrom the slice (nodes as list, edges as list, metadata as dict). - Serialise with
json.dumps(..., sort_keys=True, separators=(',',':'), ensure_ascii=False). - Encode as UTF-8.
- Return
hashlib.sha256(...).hexdigest().
The sort_keys=True flag guarantees that dict key ordering inside
individual node/edge dicts does NOT affect the digest.
Parameters¶
graph_slice:
A GraphSlice instance (frozen Pydantic model).
Returns¶
str 64-character lowercase hex digest.
Source code in packages/akms_learn/src/akms_learn/graph_import.py
fixture_graph¶
akms_learn.fixture_graph
¶
Return a hand-built GraphSlice for use in tests and pipeline demos.
Graph topology (j² return-mapping theme, 6 nodes):
.. code-block:: text
prereq_linear_algebra ──requires──► core_j2_return_mapping
prereq_complex_numbers ──requires──► core_j2_return_mapping
core_j2_return_mapping ──derives──► deriv_state_space
deriv_state_space ──implements──► impl_pole_placement
core_j2_return_mapping ──pitfall_of──► pitfall_sign_convention
impl_pole_placement ──exercise_for──► exercise_verify_poles
Edge types used: requires, derives, implements,
pitfall_of, exercise_for.
The fixture satisfies the Phase 3 ordering vocabulary (§12) and is large enough to be reused as the Phase 4 mode fixture.
Source code in packages/akms_learn/src/akms_learn/graph_import.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
Models¶
The Learning Source Packet and its view models.
LearningSourcePacket¶
akms_learn.LearningSourcePacket
¶
Bases: BaseModel
Root of the LSP (spec §3, L33-L69).
Holds top-level header fields, the request snapshot, the packet body, and a list of soft warnings accumulated during compilation.
PacketBody¶
akms_learn.PacketBody
¶
Bases: BaseModel
The body of the LSP — all the rendered view collections (spec §3).
Optional forward-compat fields domain_pack_provenance and
source_pack_provenance exist so the domain-pack layer can populate
metadata without forcing a v2 schema bump (spec §12).
CompilerInfo¶
akms_learn.CompilerInfo
¶
Bases: BaseModel
Identifies the compiler that produced the packet (spec §3).
SourceInfo¶
akms_learn.SourceInfo
¶
Bases: BaseModel
Identifies the AKMS graph + query that produced this packet (spec §3).
graph_hash and graph_path are the stable provenance anchors that
let the review bundle reproduce the packet from the same graph.
LearningRequestInfo¶
akms_learn.LearningRequestInfo
¶
Bases: BaseModel
Normalized request snapshot + hash (spec §3, plan §10).
Only the normalized 11 request fields contribute to request_hash; UI
state from Logic-Loom must NOT contribute (plan §10, L203). Hash stability
is enforced upstream in request.normalize.
LearningNodeView¶
akms_learn.LearningNodeView
¶
Bases: BaseModel
A node included in the packet (spec §4).
Required provenance: node_id, source_path, line_range.
line_range is a (start, end) tuple of 1-indexed inclusive line
numbers into source_path.
LearningEdgeView¶
akms_learn.LearningEdgeView
¶
Bases: BaseModel
An edge included in the packet (spec §5).
Required provenance: edge_id, source_path, line_range. The
from/to semantic endpoints from the spec YAML are mapped via
aliases so the dumped form matches the spec key names.
PitfallView¶
akms_learn.PitfallView
¶
Bases: BaseModel
A pitfall surfaced into the packet (spec §4, extracted.pitfalls + §6).
CodeLinkView¶
akms_learn.CodeLinkView
¶
Bases: BaseModel
A code link (spec §7).
This view-type carries optional fields that the
implementation-first mode populates when walking implements
edges:
source_node_id— the learning/spec node from which the edge starts.target— the code-mirror node id (preferred) or the source-file path of the implementation referenced by the edge.relation— the edge type that produced this view. Defaults to"implements"since CodeLinkViews are only emitted fromimplementsedges.file_path— optional file path of the implementation.line_range— optional(start, end)line range.
The original fields (node_id, source_file, symbols,
concept, mirror_node_id, explanation_mode) are preserved.
CodeLinkView is a view-type and may be extended; the v2 graph schema
(node/edge models) remains frozen.
ReferenceView¶
akms_learn.ReferenceView
¶
Bases: BaseModel
A reference / further-reading entry (spec §9, references array).
AssessmentView¶
akms_learn.AssessmentView
¶
Bases: BaseModel
Forward-compatibility stub for assessment items (spec §8).
Schema is NOT enforced; arbitrary keys are accepted. An empty
assessments=[] is the typical value.
LearningWarning¶
akms_learn.LearningWarning
¶
Bases: BaseModel
Soft validation issue emitted during LSP compilation.
Severity follows the spec's tri-state: info / warning / error.
source_ref is an optional free-form reference to the offending input
(e.g. node id, request field, descriptor path).
Validation¶
Packet validation and its hard-error type.
validate_packet¶
akms_learn.validate_packet
¶
Validate cross-field invariants on a compiled LSP.
Returns the list of accumulated soft warnings (possibly empty) on success.
Raises :class:PacketValidationError if any hard invariant is violated.
Hard checks run first and short-circuit on failure (a packet that lacks a
request_hash is not meaningful to soft-check).
Source code in packages/akms_learn/src/akms_learn/validation.py
PacketValidationError¶
akms_learn.PacketValidationError
¶
Bases: Exception
Raised when a :class:LearningSourcePacket violates a hard invariant.
Carries one or more textual issues describing the missing field(s) or
dangling reference(s). The string form joins issues with "; " so error
messages stay informative when surfaced through a single str(exc).
Source code in packages/akms_learn/src/akms_learn/validation.py
Exporters¶
Exporter entry points. The compiler dispatches these in Stage 9 — callers list
exporter names in request.exporters rather than calling these directly.
markdown_export¶
akms_learn.markdown_export
¶
Write a lesson.md file to output_dir and return its path.
This function is the Exporter Protocol entry point; the compiler's Stage 9
dispatches it automatically when "markdown" appears in
request.exporters.
Parameters¶
packet:
The fully-validated :class:~akms_learn.models.LearningSourcePacket
produced by :func:~akms_learn.compiler.compile_learning_source.
output_dir:
Target directory. Created on demand if it does not exist.
Returns¶
list[Path]
A one-element list containing the absolute path to lesson.md.
Source code in packages/akms_learn/src/akms_learn/exporters/markdown.py
bundle_export¶
akms_learn.bundle_export
¶
Write the 7-artifact Mode 12 bundle to output_dir.
Steps:
lesson.mdis rendered by delegating to :func:akms_learn.exporters.markdown.export(keeps a single source of truth for Markdown rendering).- All remaining artifact payloads are built in memory before any second write hits disk. Manifest is built last because it lists the other artifacts.
- Artifacts are written in alphabetical order for visual consistency
with
manifest.json'sartifactsfield. - Empty
exports/andassets/directories are created and pinned with.gitkeepsentinels so git tracks them.
Returns¶
list[Path]
Sorted (by path string) list of every file written, including the
two .gitkeep sentinels.
Source code in packages/akms_learn/src/akms_learn/exporters/bundle.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
MANIFEST_VERSION¶
Domain packs¶
Metadata-only descriptors, the registry, and the capability error type.
DomainPackDescriptor¶
akms_learn.DomainPackDescriptor
¶
Bases: BaseModel
Top-level domain pack descriptor (spec §3).
A domain pack declares a curated learning domain. The core compiler MUST be able to load and reason about this descriptor without importing any companion package.
SourcePackDescriptor¶
akms_learn.SourcePackDescriptor
¶
Bases: BaseModel
A source pack — a companion source repo / package (spec §4).
Source packs are pure metadata declarations. They describe a companion's repository roots, capability surface, and adapter id, but they never trigger any import of the companion code.
DomainPackRegistry¶
akms_learn.DomainPackRegistry
¶
In-memory map of :class:DomainPackDescriptor keyed by descriptor.id.
Deterministic: :meth:ordered_descriptors always returns descriptors
sorted alphabetically by id, regardless of insertion order.
Source code in packages/akms_learn/src/akms_learn/domain_packs/registry.py
register
¶
Add descriptor to the registry.
Raises:
| Type | Description |
|---|---|
ValueError
|
If a descriptor with the same id is already
registered. Duplicate ids would make
:meth: |
Source code in packages/akms_learn/src/akms_learn/domain_packs/registry.py
get
¶
Return the descriptor with id descriptor_id or None.
ordered_descriptors
¶
Return descriptors sorted alphabetically by id.
Deterministic across runs and Python sessions.
Source code in packages/akms_learn/src/akms_learn/domain_packs/registry.py
load_from_yaml
¶
Load a :class:DomainPackDescriptor from yaml_path.
The descriptor is parsed and returned but not automatically registered. Callers may decide whether to register it (this keeps the loader free of registration side effects).
Source code in packages/akms_learn/src/akms_learn/domain_packs/registry.py
build_registry_from_paths¶
akms_learn.build_registry_from_paths
¶
Load multiple domain-pack YAMLs into a fresh :class:DomainPackRegistry.
Each path is parsed via :func:load_descriptor_from_yaml and registered
in input order, but :meth:DomainPackRegistry.ordered_descriptors
output remains deterministic (alphabetic by id).
Source code in packages/akms_learn/src/akms_learn/domain_packs/registry.py
LearningCapabilityError¶
akms_learn.LearningCapabilityError
¶
Bases: Exception
Raised when a request needs an unavailable required capability.
Per plan §21 rule 5 / spec §4 rule 2: missing source packs degrade to warnings unless the requested mode explicitly requires that pack — in which case the compiler MUST raise this error.