Repository navigation
feat(spec): per-invocation RunEnv — retire bed os.Setenv isolation - #159
Conversation
Add spec.RunEnv (a per-invocation env map carried on context.Context) and a short-bound file lock, so a concurrent in-process check-bed roster isolates each bed by EXPLICIT DATA rather than process-global os.Setenv. Root cause (plan §4.2 / RCA issue 1): a bed isolated itself by os.Setenv'ing CHARLY_DEPLOY_CONFIG / CHARLY_REPO_OVERRIDE / CHARLY_PREEMPT_LEASE. Under a roster — many beds as goroutines in ONE process — bed A's value is inherited by bed B (and bed B's forked children); two beds then contended on one deploy-config lock and hung for the 30-minute image-build bound (RCA issue 2). - spec/runenv.go: RunEnv + WithRunEnv/RunEnvFrom/RunEnvGet (ctx value wins, else os.Getenv — byte-identical for the legacy single-invocation host CLI). - DefaultDeployConfigPath(ctx?...): a ctx RunEnv override wins over the process env, so each bed reads its own overlay. - lock.AcquireFileLockWithin + flockBoundedWithin: a brief critical section (a config read-modify-write) gets a SHORT explicit bound, so a contended overlay write fails FAST with the holder named instead of waiting the 30-minute image-build bound. Tests: spec/runenv_test.go (ctx wins / process fallback / no-op empty), lock/filelock_within_test.go (short bound fails fast, named + actionable). Assisted-by: opencode deepseek-v4.1-flash (fully tested and validated)
Review — BLOCKHead SHA: Change class: code/config — Go contract-module library change ( Blocks
Verdict: BLOCK ollama-cloud/deepseek-v4.1-flash — action-review. |
…d children Two more RCA'd defects found live while proving the roster cutover, plus the deleted dead flockBounded wrapper (validator finding). - loadmodel.go: ResolveBedForRoot(ref) resolves a bed and rewrites every namespace-LOCAL cross-ref (from:/image:, recursively through members) to the root-qualified `ns.<leaf>` form on a COPY. A namespaced pod bed's run drives `charly box build <image>` / `charly deploy add <name> <image>` from the project ROOT, where a bare namespaced ref does not resolve (measured live: charly.check-sidecar-pod failed `unknown box "check-k8s-deploy-app"`). The stored tree is unmutated, so `charly box validate` still reads the authored refs. - proc/run_subcommand.go: RunCharlySubcommandCtx(ctx, ...) merges the ctx RunEnv (spec.RunEnvFrom) OVER the inherited environment, so a forked `charly` child receives a bed's CHARLY_DEPLOY_CONFIG/CHARLY_REPO_OVERRIDE as explicit data. The legacy RunCharlySubcommand delegates with context.Background() (byte-identical inherit). This is the fix for the most severe live defect: deploykit.BringUpMembers/ TearDownMembers fork member children via RunCharlySubcommand, so once bed isolation retired os.Setenv those children resolved the REAL per-host overlay and wrote the operator's state (observed live). RunCharlySubcommandCtx closes that. - lock/filelock.go: delete the now-unused flockBounded wrapper (AcquireFileLock delegates directly through AcquireFileLockWithin -> flockBoundedWithin). Tests: loadmodel_beds_test.go (TestBedResolveForRoot_QualifiesNamespacedRefs), run_subcommand_test.go (RunEnv merge/override/inherit). Assisted-by: opencode deepseek-v4.1-flash (fully tested and validated)
…v fork test Address the validator's two code findings on PR #159. - loadmodel.go: ResolveBedForRoot / qualifyBedRefs now CLONE the Member slice and every member node before rewriting. ResolveBed returns the stored node by value, but its Member entries carry *Deploy pointers into the stored tree, so the previous in-place recursion mutated the stored members — the exact mutation the doc comment denied, and the "charly box validate reads the authored refs" claim depends on it. Test extended to assert the stored member node is unchanged. - proc/run_subcommand.go: extract the testable runCharlySubcommandCtx(ctx, exe, args) exec body so the cmd.Env = mergedChildEnv(RunEnvFrom(ctx)) wiring is exercised against a REAL child. run_subcommand_test.go's TestRunCharlySubcommandCtx_ThreadsRunEnv now runs /bin/sh printing the variable, proving per-call override + inheritance, and its doc comment matches what it does (the former comment claimed a real fork it did not perform). Assisted-by: opencode deepseek-v4.1-flash (fully tested and validated)
Review — BLOCKHead SHA: Change class: code/config — Go contract-module library change ( Security screen: PASS (no secrets/egress, no dependency or guardrail/rulebook edits, no embedded directives). Blocks
Comment disposition
Verdict: BLOCK ollama-cloud/deepseek-v4.1-flash — action-review. |
Review — BLOCKHead SHA: Change class: code/config — Go contract-module library change ( Security screen: PASS — no secrets/egress, no new dependency or version pin, no guardrail/rulebook/validator edits, no embedded directives. (T1–T4 clean.) Blocks
Comment disposition
Verdict: BLOCK ollama-cloud/deepseek-v4.1-flash — action-review. |
Review — PASSHead SHA: Security screen: PASS — no secrets/egress, no new dependency or version pin, no edits to the validator spec / rulebook / gates, no embedded directives (T1–T4 clean). Checklist: All applicable rules PASS or NA. Verified from the current diff: diff-stats reconcile exactly with the 9 file headers (544 insertions / 12 deletions); the pasted bed summary lists 17 named steps and closes Comment dispositions (all re-derived against head
Verdict: PASS ollama-cloud/deepseek-v4.1-flash — action-review. |
Review — PASSHead SHA: Security screen: PASS — no secrets/egress, no new dependency or version pin, no edits to the validator spec / rulebook / gates, no embedded directives (T1–T4 clean). Checklist: All applicable rules PASS or NA. Re-derived against the current head: the 9 file headers reconcile exactly with the pasted diff-stats (26+39+57+112+13+66+61+63+107 = 544 insertions; 10+2 = 12 deletions); the bed paste lists 17 named steps and closes Comment dispositions (all re-derived against head
Verdict: PASS ollama-cloud/deepseek-v4.1-flash — action-review. |
Summary of changes
Retire the check-bed roster's process-global
os.Setenvisolation in favourof explicit per-invocation data, and fix three further defects exposed by running
the cutover live. This is the
specleg of thecheck-bed-rosterprogram(plan §4.2 / F8, RCA issues 1–2 and 5–7). The sdk + plugin consumers land
sdk-first after this is merged + tagged.
1.
RunEnv— per-invocation environment oncontext.Contextbed_session.goisolated each bed withos.Setenv(CHARLY_DEPLOY_CONFIG / CHARLY_REPO_OVERRIDE / CHARLY_PREEMPT_LEASE).os.Setenvis process-global, sounder a roster — many beds as goroutines in one process — bed A's value is
inherited by bed B and bed B's forked children; two beds then contended on one
deploy-config lock and HUNG for the 30-minute image-build bound (RCA issues 1–2).
spec/runenv.go—RunEnv,WithRunEnv,RunEnvFrom,RunEnvGet(ctx valuewins, else
os.Getenv— byte-identical for the legacy host CLI).spec/deployconfig.go—DefaultDeployConfigPath(ctx ...context.Context)resolves the ctx
RunEnvCHARLY_DEPLOY_CONFIGbefore the process env.lock/filelock.go—AcquireFileLockWithin+flockBoundedWithin: a briefconfig read-modify-write passes a SHORT bound so a contended overlay write fails
fast with the holder named, instead of waiting the 30-minute image-build
bound. The now-unused
flockBoundedwrapper is deleted.2.
ResolveBedForRoot— root-qualify namespaced bed cross-refs (RCA issue 5)A namespaced bed's
image:/from:(and each member's, recursively) live in itsOWN namespace, but the run drives
charly box build <image>/charly deploy add <name> <image>from the project ROOT, where a barenamespace-local ref does not resolve. Measured live:
charly.check-sidecar-podfailed[image-build] unknown box "check-k8s-deploy-app".spec.UnifiedFile.ResolveBedForRoot(ref)resolves the bed and rewrites everynamespace-LOCAL cross-ref to the root-qualified
ns.<leaf>form on a deep copy— the Member slice and each member node are cloned, so the stored tree is
unmutated and
charly box validatestill reads the authored refs.3.
RunCharlySubcommandCtx— carry RunEnv into forked children (RCA issue 7)deploykit.BringUpMembers/TearDownMembersfork member children viaproc.RunCharlySubcommand, which inherited the parent PROCESS env. Once bedisolation retired
os.Setenv, those children resolved the real per-hostoverlay and wrote the operator's state (observed live: bed entries landed in
~/.config/charly/charly.yml).RunCharlySubcommandCtx(ctx, …)merges the ctxRunEnvOVER the inherited environment; the legacyRunCharlySubcommanddelegates with
context.Background()(byte-identical inherit when no RunEnv).Additive / no schema change — new helpers only;
DefaultDeployConfigPathstays call-compatible via its variadic tail. No
#SchemaVersionbump.Closes / relates to: part of the
check-bed-rostercutover (plan §4.2).Change class
charly-or-sdk Go(thespeccontract module).How R10-tested — exact gate, commands, and pasted output
Evidence scope, stated explicitly (per the previous block): the new entry
points (
RunEnv/DefaultDeployConfigPath(ctx)/AcquireFileLockWithin/ResolveBedForRoot/RunCharlySubcommandCtx) have no in-repo callers — theyare reached only from the sdk + plugin consumers, which are not yet landed.
So the live bed was run on a combined dev tree: this branch plus the local
unlanded consumers (
sdk: ctx onBringUpMembers/TearDownMembers/PersistBedDeployOverrides;plugin-check:ResolveBedForRootadoption +bedArtifactRef;plugin-fleet: ctx threading), wired byreplacedirectives.go test ./... -count=1— full outputpasted below.
charly check run check-sidecar-podon thecombined dev tree — all 17 steps PASS, 0 errors / 0 warnings; the operator's real
overlay left structurally identical (the issue-7 proof). Because (b) exercised
unlanded consumer code, the tier is set to what THIS tree alone supports:
analysed on a live system(changed runtime path ran live with retainedoutput; the complete fresh-rebuild R10 gate for the contract module alone is N/A).
1. Module gate —
go test ./... -count=1(fresh, full output):2. Changed-path tests (fresh):
3. Binary provenance (R9) — fresh build from the combined tree:
4. Live bed — the COMPLETE 17-step sequence (verbatim from summary.yml + stderr):
All 17 steps in summary.yml carry
ok: true,errors: 0,warnings: 0,advisories: 0. The only diagnostic per step is the allowlisteddev-worktree-binary-notice(the expected warning for a worktree build).5. Issue-7 proof — the operator's real overlay is untouched:
6. Final head + diff-stats:
Rulebook compliance
no "pre-existing"/"out of scope" labels.
(consumer legs); the unused
flockBoundedwrapper is deleted.RunEnvprimitive; oneResolveBedForRootqualifier; oneRunCharlySubcommandCtxfork path.git statusonspec/was clean before the disposable bedteardown/cleanup; no destructive action touched a working-tree change.
/bin/shchild for theenv wiring); changed paths ran live in a real disposable R10 bed (no mock).
(
calver.sh→2026.268.1428;charly version→2026.268.1428, matching).records the gitlink via
task syncafter merge.Change classification
go test ./... -count=1(module, final tree) + livecharly check run check-sidecar-pod(PASS, 17 steps, 0 errors/warnings) on thecombined dev tree (this branch + the named unlanded consumers).
analysed on a live system— the changed runtime path ranlive with retained output, but the complete fresh-rebuild R10 gate is not
attributable to this contract module alone (the run necessarily exercised the
unlanded consumer code, since the new entry points have no in-repo callers).
Assisted-by: opencode deepseek-v4.1-flash (analysed on a live system)