Environment
dotagents._env
Chained env-file assembly + env.py execution (plan 07).
Ported from the precursor agentic under frozen contract B -- the
observable behavior (what files are found, in what order, and how they are
evaluated) is preserved verbatim; only the code shape is duho/dotagents-native.
Contract B, the exact sequence :func:get_environment performs:
- Bins onto PATH FIRST, before any env eval. Each level's
bindir (contract-A precedence order, except project-root) is prepended toPATHso env scripts can call overlay helpers by name. - Two tiers, in order: ALL
pre.env.py/pre.env/pre.local.envfirst, THEN ALLenv.py/env/local.env-- the concatenation of two contract-A resolutions (:func:resolve_env_files). - Within each tier, files are in the contract-A precedence order (overlays -> system -> user -> project -> project-root).
- Chained, later-overrides-earlier: each file is evaluated against the ACCUMULATED environment of every file before it; the result also accumulates. Later files win on conflicting keys.
.pyfiles are EXECUTED (:func:get_env_from_pyruns the script and reads back a JSON object of env changes); plain files are sourced (:func:get_env_from_file, viabash ... env -0).- :func:
get_diffreturns only the vars that differ from the caller's base environment; :func:get_environmentreturns the full change set.
Plan-08 identity/proxy model is wired into the output around the file chain:
- Identity (:func:
dotagents._agents.stamp_identity) is seeded BEFORE the file chain, so env files can branch onAGENTS_HARNESSand override the stampedAGENTS_MODELetc. (chained: a later file wins). Never clobbers a value already present in the base env. - Proxy is normalized AFTER the file chain:
AGENTS_PROXYis seeded if unset (fromAGENTS_WEBFETCH_PROXY_URLelse the global HTTPS/HTTP/ALL_PROXY, either case); any proxy var that already exists is mirrored into BOTH cases (never creating one that was not set;http_proxystays lowercase-populated per httpoxy).AGENTS_PROXYis NOT fanned out into the globalHTTP_PROXY.
Security (Leakage rule): env.py runs arbitrary code, but only from files
resolved under the store/overlay/project locations by contract A. Never log the
resulting DOTAGENTS_*/AGENTS_* secret VALUES -- callers that print the
diff must treat it as sensitive; this module logs var NAMES only.
FORMAT_ALIASES = {'export': 'export', 'posix': 'export', 'sh': 'export', 'bash': 'export', 'dotenv': 'dotenv', 'env': 'dotenv', 'powershell': 'powershell', 'pwsh': 'powershell', 'ps': 'powershell', 'cmd': 'cmd', 'bat': 'cmd', 'batch': 'cmd', 'fish': 'fish', 'json': 'json', 'ini': 'ini', 'yaml': 'yaml'}
module-attribute
KNOWN_FORMATS = tuple(sorted(set(FORMAT_ALIASES) | {'auto'}))
module-attribute
apply_proxy_model(osenv)
Return the proxy changes for osenv per the plan-08 proxy model.
- Seed
AGENTS_PROXYif unset, from the first populated :data:_PROXY_SEED_ORDERvar (webfetch var wins, then the global proxy in either case). An existingAGENTS_PROXYis respected. - Mirror every proxy var that ALREADY EXISTS into both cases -- fills only
the missing case, never introduces a proxy that was not set.
http_proxythus stays lowercase-populated whenHTTP_PROXYis set (httpoxy). AGENTS_PROXYis NOT fanned into the globalHTTP_PROXY.
Source code in src/dotagents/_env.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | |
detect_shell_format()
Best-effort detection of the calling shell's output format.
Walks the parent-process chain (Windows: a stdlib-ctypes Toolhelp snapshot;
POSIX: /proc/<ppid>/comm else $SHELL) and returns the canonical
format for the first shell found. Any failure degrades to the OS default
("powershell" on win32, "export" elsewhere) and NEVER raises. Reads
process names only -- no environment values.
Source code in src/dotagents/_env.py
269 270 271 272 273 274 275 276 277 278 279 280 | |
get_bin_paths(*, agents_dir, project_root, global_scope=False)
Each level's bin dir in contract-A precedence order, EXCEPT project-root.
Uses include_missing=True (precursor semantics) so a bin dir is offered
for every level even if absent -- the caller prepends only real dirs where it
matters. project-root's bin is explicitly excluded ({"project-root":
None}): a project's own top-level bin is not an agent bin.
Source code in src/dotagents/_env.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
get_diff(*, agents_dir, project_root, base_env=None, global_scope=False, explicit=None, logger=None)
Only the assembled vars that differ from base_env (current env).
get_environment already returns changes vs base_env, so the diff is
the subset whose value actually differs from the base -- identical to the
precursor's get_diff over os.environ.
Source code in src/dotagents/_env.py
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 | |
get_env_from_file(env_file, base_env, logger=None)
Source a plain env file in bash and return the vars it changed.
Runs set -a; source <file>; env -0 so exported assignments are captured.
If bash is unavailable (or the source errors) the file contributes
nothing -- logged by name, never fatal.
Source code in src/dotagents/_env.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
get_env_from_py(env_py, base_env, project_root, level, global_scope, logger=None)
Execute an env.py and read back its JSON object of env changes.
The script runs as a child with base_env (the accumulated environment)
and must print a JSON dict to stdout. A non-zero exit or unparseable output
contributes nothing and is logged by NAME only -- never abort assembly, and
never echo the child's stdout (it may carry secret values).
Source code in src/dotagents/_env.py
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 | |
get_environment(*, agents_dir, project_root, base_env=None, global_scope=False, explicit=None, logger=None)
Assemble the env CHANGES (vars this adds/overrides vs base_env).
Follows frozen contract B: identity seeded, PATH bins first, the two tiers
chained (later overrides earlier), then proxy normalization. Returns only
what changed -- mirrors the precursor's env={} accumulator.
Source code in src/dotagents/_env.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | |
resolve_env_files(*, agents_dir, project_root, global_scope=False)
The ordered, existing env files: ALL pre-tier then ALL main-tier.
Each tier is one contract-A resolution (:func:get_file_paths). Per-level
filename resolution (contract A point 2): pre.env.py/pre.env and
env.py/env resolve everywhere; the project + project-root levels use
pre.local.env / local.env instead. Only existing files are returned.
Source code in src/dotagents/_env.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |