Overlays
dotagents._overlays
Overlay support: the :class:Overlay type, plus the multi-overlay recompose.
An overlay is a directory (optionally carrying an overlay.toml manifest) that
installs as a unit into <store>/overlays/<name>/, its files keeping their
relative paths inside that directory. Everything that
depends only on ONE overlay -- its name rules, its manifest, its setup script,
its files, what it contributes to the managed AGENTS.md block -- is a method or
property of :class:Overlay. Manifest keys:
routing— lines appended to the core's "Load on demand" list.rules— overlay-relative paths to markdown files whose- **…bullet blocks are appended to "Always-on rules".requires— overlay names this one needs;overlays addinstalls them first.name,description,priority— see :meth:Overlay.read_manifest.
The operations over a set of overlays stay module functions:
_compose_block folds their rules and routing into a base block, and
:func:recompose_overlay_block rewrites a store's AGENTS.md with it.
DEFAULT_PRIORITY = 500
module-attribute
DEFAULT_SETUP_TIMEOUT = 300
module-attribute
OVERLAY_ROOT_NOTE = 'Overlay paths below use `$<NAME>_OVERLAY_ROOT` -- an environment variable `dotagents env` exports per installed overlay (its install dir); resolve it in a shell (`echo $ENGINEERING_OVERLAY_ROOT`; PowerShell `$env:ENGINEERING_OVERLAY_ROOT`) before opening the file with a file tool.'
module-attribute
SCRIPT_NAME_RE = re.compile('^[A-Za-z0-9][A-Za-z0-9._-]*$')
module-attribute
InstallResult()
Bases: object
What :meth:Overlay.install_to did. Unpacks as the historical
(written, skipped, lines), skipped being the unchanged files.
Source code in src/dotagents/_overlays.py
396 397 398 399 400 401 402 403 404 405 406 | |
backed_up = []
instance-attribute
lines = []
instance-attribute
removed = []
instance-attribute
replaced = []
instance-attribute
skipped
property
unchanged = 0
instance-attribute
written = 0
instance-attribute
__iter__()
Source code in src/dotagents/_overlays.py
412 413 | |
Overlay(path, store=None)
One overlay: a directory whose NAME identifies it and whose files install
as a unit under <scope>/overlays/<name>/.
Construct it from the overlay's directory (a source dir or an installed
dir -- both are overlays). Nothing is read at construction; every method
reads the directory when called, so an Overlay made before its files
land still sees them afterwards. The name-only rules (:meth:is_valid_name,
:meth:normalize_name, :meth:root_var_for) are static so a caller holding
just a name -- overlays add <name> -- uses the same rule as one holding a
directory.
Source code in src/dotagents/_overlays.py
475 476 477 478 479 480 481 482 | |
DEFAULT_PRIORITY = DEFAULT_PRIORITY
class-attribute
instance-attribute
INSTALL_RECORD = '.dotagents-install.json'
class-attribute
instance-attribute
MANIFEST_NAME = 'overlay.toml'
class-attribute
instance-attribute
NAME_RE = re.compile('^[A-Za-z](?:[A-Za-z0-9_.-]*[A-Za-z0-9])?$')
class-attribute
instance-attribute
RESERVED_NAMES = frozenset(['con', 'prn', 'aux', 'nul'] + ['com%d' % i for i in range(1, 10)] + ['lpt%d' % i for i in range(1, 10)])
class-attribute
instance-attribute
SETUP_SCRIPT_NAMES = ('setup.py',)
class-attribute
instance-attribute
SKIP_PARTS = frozenset({'.git', '.hg', '.svn', '__pycache__', '.mypy_cache', '.pytest_cache', '.ruff_cache', '.tox', '.venv', 'node_modules'})
class-attribute
instance-attribute
__slots__ = ('path', 'store')
class-attribute
instance-attribute
install_record_path
property
is_valid
property
:meth:is_valid_name of :attr:name.
manifest_path
property
name
property
The overlay's name: its directory name.
normalized_name
property
:meth:normalize_name of :attr:name.
path = Path(path)
instance-attribute
priority
property
The manifest priority, DEFAULT_PRIORITY when absent or
unparseable (:meth:read_manifest already did the checking). Lower
sorts earlier.
root_var
property
:meth:root_var_for of :attr:name -- the <NAME>_OVERLAY_ROOT var.
sort_key
property
The (priority, name) merge-order key.
Lower priority (default DEFAULT_PRIORITY, 500) sorts earlier; a
numerically higher-priority overlay therefore lands later in the merged
AGENTS.md block and wins on conflict -- the same convention _context.py
uses. The tiebreaker is the manifest name (falling back to the directory
name), then the directory name itself (two dirs can carry the same
manifest name), so output is deterministic regardless of input order.
store = Path(store) if store is not None else None
instance-attribute
__eq__(other)
Source code in src/dotagents/_overlays.py
492 493 | |
__fspath__()
Source code in src/dotagents/_overlays.py
489 490 | |
__hash__()
Source code in src/dotagents/_overlays.py
495 496 | |
__repr__()
Source code in src/dotagents/_overlays.py
486 487 | |
discover(root, store=None)
classmethod
The overlays under ONE overlays/ root, sorted by name.
No registry, no manifest required: a directory under root is an
overlay as long as its name passes :meth:is_valid_name (so .git/
__pycache__/dotfiles are skipped). is_dir() follows symlinks, so a
symlink-to-dir counts. Empty if root is absent. This is the ONE
discovery rule: :meth:installed folds it over several stores,
and the overlays command's add/remove/sync use it directly
for the single scope they install into. store is stamped on each
result (see :attr:store).
Source code in src/dotagents/_overlays.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 | |
files()
Files the overlay installs: everything except its manifest, its
install record, VCS metadata and tool caches (:attr:SKIP_PARTS) and
compiled *.pyc.
Source code in src/dotagents/_overlays.py
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 | |
find_setup_script()
The overlay's setup script, or None if it ships none.
Call this on the installed overlay: setup runs against the copy under
<scope>/overlays/<name>/ with that as its cwd, so a script can
reference its own sibling files by relative path.
Source code in src/dotagents/_overlays.py
741 742 743 744 745 746 747 748 749 750 751 | |
ignore_rules()
The rules of the .gitignore / .ignore at the overlay's root
(:mod:dotagents._ignore); one in a subdirectory is an ordinary file.
Source code in src/dotagents/_overlays.py
948 949 950 951 952 953 | |
install_to(dest_overlay_dir, dry_run, *, prune=False, backup_root=None, source=None)
Make dest_overlay_dir (<scope>/overlays/<name>/) exactly this
overlay: every file it ships (see :meth:files) is installed or
replaced, its overlay.toml refreshed, and every other file in the
install is removed -- except what the .gitignore / .ignore at
the overlay's root match, plus its install record and tool caches
(:attr:SKIP_PARTS): those are the install's own (setup output,
local state) and are never copied, replaced or removed. prune
clears them too, leaving exactly the upstream files (and the record).
A file replaced or removed that is not what dotagents installed there
(edited here, or never installed by it) is first copied to
backup_root when one is given (:attr:InstallResult.backed_up,
:attr:InstallResult.replaced); what prune clears is not backed up.
The install record (:attr:INSTALL_RECORD) keeps the digest of every
file written and source (where the overlay came from; the previous
value when None).
Source code in src/dotagents/_overlays.py
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 | |
installed(*stores)
classmethod
Every overlay a session uses, across stores (.agents-shaped
roots) given in precedence order -- Scope.stores: the system store,
the user store, then the project's <project>/.agents (a user scope
has no project store). A None store is skipped.
The overlays come back store by store, each store's sorted by name,
with every result stamped with the :attr:store it came from.
Shadowing: an overlay whose name (compared normalized, so
my_overlay and my-overlay are one) also appears in a LATER store
is dropped -- the later store's copy REPLACES it, so the project's bin/lib/
env/cmds/CONTEXT.md/root var are the only ones that resolve, not both
stacked. This is the one function behind the contract-A walk
(Scope.paths), env's overlay roots, context's
sources/placeholders/skills and overlays list/show, so they can
never disagree about what is installed.
Source code in src/dotagents/_overlays.py
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | |
is_valid_name(name)
staticmethod
A dir under overlays/ is an overlay iff its name matches
:data:NAME_RE: a leading ASCII letter, then ASCII letters/digits/_/
./-. Dots are allowed mid-name (foo.bar, v1.2) but NOT as
the first char, so .git/.hidden and __pycache__ (leading _)
and 2fast (leading digit) are excluded. The contract-A level names
(user, project, system, project-root, default,
overlay -- :data:dotagents._scope.LEVEL_NAMES) are reserved: an
overlay's dir name is its level label in the walk, and one of these
would collide with the per-level filename keys. So are Windows device
names (:data:RESERVED_NAMES, with or without an extension).
Source code in src/dotagents/_overlays.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
normalize_name(name)
staticmethod
THE canonical overlay name: lowercase, _ and . -> -.
Every place that refers to an overlay by name goes through this one
function, so My_Overlay and my-overlay are one overlay everywhere:
overlays add installs to <scope>/overlays/<normalize_name(name)>/
and resolves the source dir by the same name, shadowing across stores
compares it, and :meth:root_var_for derives the <NAME>_OVERLAY_ROOT
env var from it. A dot is legal in a dir name (:meth:is_valid_name)
but no shell variable can carry it, so v1.2 and v1-2 would share
V1_2_OVERLAY_ROOT; normalizing it to - makes them one overlay
rather than two that alias.
Source code in src/dotagents/_overlays.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 | |
read_install_record()
{"source": {...} | None, "files": {rel: sha256}} of an installed
overlay; empty for a source dir or an install that predates records.
Source code in src/dotagents/_overlays.py
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 | |
read_manifest()
Parse overlay.toml, returning name / description / routing / rules /
requires / priority / setup_timeout (seconds, 0 = none; None
when the manifest does not set it) / scripts ([scripts] as
written: launcher name -> script path relative to the overlay, or
False; :meth:scripts adds the detected ones).
A missing or unreadable manifest yields empty contributions -- an overlay is allowed to be just a directory of files.
Source code in src/dotagents/_overlays.py
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 | |
root_var_for(name)
staticmethod
The env-var name carrying an overlay's installed root: <NAME>_OVERLAY_ROOT.
NAME is :meth:normalize_name of the overlay upper-cased, with -
(and a mid-name ., which no shell variable can carry) turned into
_: my-overlay / My_Overlay -> MY_OVERLAY_OVERLAY_ROOT,
v1.2 -> V1_2_OVERLAY_ROOT. Because it starts from the same
normalized name the install dir uses, the variable for overlays/<n>/
is always root_var_for(n) (:attr:root_var on the instance). Both
consumers go through here: dotagents env EMITS the variable and
dotagents context expands the <NAME_OVERLAY_ROOT> placeholder to
the same path.
Source code in src/dotagents/_overlays.py
535 536 537 538 539 540 541 542 543 544 545 546 547 548 | |
rule_blocks(rel_paths)
Extract - **… bullet blocks from each referenced markdown file.
Returns (blocks, warnings). Only the leading run of bullets is taken: a
rules file may carry explanatory prose under a ## heading
(the engineering overlay's rules/ENGINEERING.md documents why its rules are not in the base), and
that must not be merged into the core. A path that does not exist is
reported, not fatal -- a broken optional overlay must never block the
base config landing.
Source code in src/dotagents/_overlays.py
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 | |
run_setup(*, agents_dir, dry_run, logger, scope_root=None, scope_level=None, base_env=None, timeout=None)
Run the overlay's idempotent setup script if it ships one.
Returns the script's exit code, or None when the overlay has no setup
script (nothing to run -- not an error). Presence of setup.py at the
overlay root is the opt-in; the runner never second-guesses a script the
user chose to install.
Idempotency is the overlay author's contract: the script must be safe
to run on every add/sync (check-then-act). The runner only
invokes it:
- cwd = the installed overlay dir, so the script sees its own files.
- env is
base_env(defaultos.environ;overlayspasses the assembleddotagents env, so the script imports any overlay'sliband calls any overlay'sbinby name), plusAGENTS_HOME=agents_dir, the USER store -- what the variable means everywhere else, so adotagentscall the script makes resolves the same stores -- plusAGENTS_SCOPE_ROOT= the store the overlay is installed into (scope_root; the user store for a-ginstall, the project's.agentsotherwise),AGENTS_SCOPE=user/project(scope_level, when given) andAGENTS_OVERLAY_DIR= its own installed dir. - the script runs under
AGENTS_PYTHONfrom that env (the scope venv, which has every overlay's requirements and libs), else the interpreter running dotagents. - it is stopped after
timeoutseconds -- else the manifest'ssetup_timeout, else :data:DEFAULT_SETUP_TIMEOUT;0means no limit -- and a stopped script is a failure (exit 124).
A non-zero exit is returned so the caller can raise a clear error -- never a silent skip.
Source code in src/dotagents/_overlays.py
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 | |
scripts()
The scripts that get a launcher (:mod:dotagents._launchers):
each top-level bin/<name>.py whose stem is a valid script name,
then [scripts] from the manifest, which adds or replaces a path
by name, or with false drops a detected one.
Source code in src/dotagents/_overlays.py
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 | |
setup_timeout(override=None)
Seconds the setup script may run: override (--setup-timeout),
else the manifest's setup_timeout, else
:data:DEFAULT_SETUP_TIMEOUT. 0 means no limit.
Source code in src/dotagents/_overlays.py
753 754 755 756 757 758 759 760 | |
sort_by_priority(overlays)
classmethod
overlays (instances or dirs) sorted by (priority, name).
Deterministic regardless of the caller's order (add passes invocation
order, sync discovery order): a high-priority overlay's lines must
always land last. See :attr:sort_key.
Source code in src/dotagents/_overlays.py
728 729 730 731 732 733 734 735 736 737 | |
parse_manifest_text(text, origin='overlay.toml')
The manifest document: tomllib/tomli when importable, else the
hand reader. None (and a warning naming origin) when it is not
valid TOML.
Source code in src/dotagents/_overlays.py
318 319 320 321 322 323 324 325 326 327 328 329 | |
recompose_overlay_block(agents_md, base_block, overlays, dry_run, logger)
Rebuild AGENTS.md's managed block from the pristine base over ALL installed
overlays in (priority, name) order, in place.
base_block is the base overlay's managed block (no overlay content),
overlays is every installed overlay in the scope (instances or dirs), and
_compose_block folds them in sorted order -- so the block is a pure
function of which overlays are installed, never when each was added
(an incremental append could only put a newcomer last).
Only the managed block (between the dotagents markers) is rewritten; content outside the markers is untouched. Returns True if the file changed, False on a no-op (nothing to merge, file/markers absent, or block already correct).
Source code in src/dotagents/_overlays.py
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 | |