feat(substrate): converge egress policy on every resume #291
If another writer installs an allow policy after actor creation and the callback then returns nil, this early return lets Create/Fork resume with that policy instead of deny-all. For a nil result, use the existing read/compare/update path rather than assuming the fresh actor is still policy-free. Add a regression for this interleaving and update the callback-count documentation and call-sequence tests for the extra read-then-evaluate step.
A no-op Sync does not fence an older convergence: a resume can read no policy and evaluate an allow policy, pause, then successfully create it after configuration changes to nil and Sync returns success. The equal-policy branches at lines 436–441 have the same problem because they leave the version unchanged, allowing a delayed update to restore revoked permissions. Fence stale evaluations across backend instances, or require callers to quiesce convergence during configuration changes and narrow the documented guarantee; add regressions for both no-write cases.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
Critical races allow stale convergence to restore revoked permissions and let Create/Fork retain an allow policy when the callback returns nil instead of enforcing deny-all.
<details open>
<summary><strong>2 open issues</strong></summary>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
This PR extends the Substrate backend’s egress-policy management so configuration changes reach existing actors before backend-driven resumes, supporting the session isolation work in #251.
Changes:
SyncEgressPolicy for running actors and classifies unreadable stored policies as configuration errors.| File | Description |
| ---- | ----------- |
| spec.md | Records policy convergence before later resumes. |
| session/egress_policy_error_test.go | Tests API error classification and safe error text. |
| runtime/substrate/substrate.go | Integrates convergence into create, restore, attach, and fork. |
| runtime/substrate/egress.go | Implements comparison, convergence, retries, and explicit sync. |
| runtime/substrate/egress_test.go | Tests policy changes, clearing, conflicts, and sync behavior. |
| integrations/substrate/egress.go | Classifies stored-policy conversion failures. |
| integrations/substrate/egress_test.go | Tests conversion-error classification and privacy. |
| integrations/substrate/e2e/egress_test.go | Adds stored-policy checks across resume and running-actor sync. |
| integrations/substrate/cmd/agentsessionsd/substrate.go | Updates policy-file flag help for convergence behavior. |
| integrations/substrate/cmd/agentsessionsd/egress_test.go | Tests changed policy files reaching existing actors. |
| docs/substrate-conformance.md | Documents convergence, sync, cache limits, and coverage. |
| docs/security.md | Explains policy authority, revocation, and enforcement limits. |
| changelog/unreleased/egress-policy-sync.md | Announces the explicit sync API. |
| changelog/unreleased/egress-policy-stored-invalid.md | Records stored-policy error classification changes. |
| changelog/unreleased/egress-policy-converge-compat.md | Documents changed behavior and client requirements. |
</details>
<sub>Review effort: Balanced</sub>
If GetActorEgressPolicy returns a wrapped ErrActorBusy, this includes the client's diagnostic in the error that Exec/Resume expose as ABORTED, even with authorization enabled. A diagnostic containing a private hostname or credential reference therefore reaches the caller. Use busyRefusal for busy read failures, retain the cause in the error chain, and add a regression test with a wrapped ErrActorBusy.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
A wrapped ErrActorBusy can leak private client diagnostics through Exec/Resume ABORTED errors even with authorization enabled, requiring sanitization via busyRefusal and a regression test.
<details open>
<summary><strong>1 open issue</strong></summary>
</details>
<details>
<summary><strong>2 resolved since last review</strong></summary>
</details>
<sub>Review effort: Balanced</sub>
This refusal requires WithEgressPolicy to be set: when the option is unset, converge returns without an egress call and an existing actor can resume with its stored policy. Please distinguish an unset option from a configured function returning nil, so removing configuration is not described as fail-closed.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
The security documentation should distinguish an unset WithEgressPolicy option, which allows an existing actor to resume with its stored policy, from a configured function returning nil, rather than describing removed configuration as fail-closed.
<details open>
<summary><strong>2 open issues</strong></summary>
</details>
<sub>Review effort: Balanced</sub>
A quick suspend, policy tightening and resume can still allow traffic under the cached old policy, despite the stored policy being updated. Qualify this as a stored-policy guarantee and apply the Sync cache caveat to resumes too; upstream's default-branch cache documents a 10-second default (the integration pins v1.0.0). Apply the same qualification in runtime/substrate/egress.go:165–168.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
The documentation needs to qualify its stored-policy guarantee because a quick suspend, policy tightening, and resume can still permit traffic under the cached old policy.
<details open>
<summary><strong>1 open issue</strong></summary>
</details>
<details>
<summary><strong>2 resolved since last review</strong></summary>
</details>
<sub>Review effort: Balanced</sub>
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
Maintainer validation is needed because security-sensitive policy convergence depends on Substrate versioning and gateway-cache behavior, while tests verify stored policies rather than live traffic enforcement or credential injection.
No open issues 🎉
<details>
<summary><strong>1 resolved since last review</strong></summary>
</details>
<sub>Review effort: Balanced</sub>
Thank you for consistently retaining causes while removing control-client diagnostics from caller-visible busy/configuration refusals. One adjacent observation: callback errors retain their full text here. A temporary authorized Exec with the callback returning fmt.Errorf("%w: policy lookup for ate-secret://provider/private-key failed", api.ErrComputeBusy) produced ABORTED and returned that credential reference to the caller; authorization sanitizes only INTERNAL. The Fork path already hides a busy callback error at runtime/substrate/substrate.go:1434-1437, but Create/Resume do not.
Would it be worth applying the same fixed-text/cause-in-chain treatment when a callback error carries a caller-visible runtime sentinel such as ErrComputeBusy or ErrComputeConfigInvalid, with an authorized Exec/Resume regression? I am treating this as an observation about the public error boundary rather than assuming callbacks normally include sensitive text.
Thank you for the careful UID check after the clone resumes. One observation specific to changing this step from install-only to convergence: the policy operation is still by actor name before that UID check. In a temporary test, I replaced the clone after CreateActorFromTag and gave the replacement its own policy. The fresh create saw AlreadyExists, retried through read/update, and replaced that actor's policy with the child's credential-bearing policy. ResumeActor then returned the replacement UID, so Fork correctly failed ErrActorBusy, but the replacement was intentionally left running with its policy changed.
Before this PR, the create-only install treated AlreadyExists as already installed and did not update it, so the overwrite appears to be introduced by convergence. Would it make sense for fresh convergence to treat ErrEgressPolicyExists as busy unless it can bind the policy operation to created.UID? I could not find an actor-UID precondition in the v1 policy API/store (the policy CAS protects policy UID/version, not actor UID), but please correct me if an upstream lease or transaction closes this window.
Thank you for preserving compatibility with custom ControlClient implementations. One observation from exercising this branch: when WithEgressPolicy returns nil and the client does not implement EgressPolicyClient, the fresh-actor path assumes the actor remains policy-free until ResumeActor. I added a temporary interleaving that installed a credential-bearing allow policy immediately after CreateActor; the call still completed with get -> create -> resume, because this branch could neither read nor clear that policy.
My reading is that this leaves a narrow gap in the documented nil = deny-all behavior for custom embedders (the shipped adapter/daemon are not affected). Would it be safer to require EgressPolicyClient whenever WithEgressPolicy is configured, including a fresh actor with a nil result, unless the resume can atomically assert policy absence? I may be overlooking a Substrate guarantee around who can write during this interval, so please do correct me if the actor lifecycle already excludes it.
Thank you for the thoughtful and thorough work here. The convergence protocol, bounded CAS retries, Sync fencing, fixed-text client refusals, cache caveats, and extensive tests show a great deal of care, and I appreciate how each earlier review observation was worked through.
While exercising a few less common interleavings locally, I noticed three edge cases and left the reproductions inline. I am sharing them as observations rather than assumptions about the intended Substrate behavior, especially since you know this control plane and integration much better than I do. Please do correct me if an upstream lease, authorization rule, or actor-policy invariant already closes any of these windows.
For context, the root runtime/substrate and session race suites, the full integrations/substrate race suite, vet, doccheck, diff checks, all GitHub checks, and a synthetic merge with current main passed on my side. Thank you again for the careful work and for making the security boundary much clearer.
Approved these changes.
Approved these changes.
Fixed, a bit more broadly than suggested: any error from the policy function now has fixed text naming the session, with the original error kept in the chain, whatever sentinel it wraps. Hiding only busy and config-invalid would still leak through the other non-INTERNAL mappings, like SESSION_BUSY.
Good catch. The convergence change turned an Exists on a fork's fresh clone into read+update, so a replacement that already had a policy got it overwritten and resumed. A fork now refuses (busy) when its new clone already has a policy, writes nothing, and goes through discardClone, so a replacement is left untouched and not resumed. One window remains, and it predates this PR: if the clone is replaced by an actor with no policy, the create succeeds on the replacement before the UID check after ResumeActor. Closing that needs a UID precondition on the policy or resume call, which Substrate v1 doesn't have. I've documented the gap.
I checked this one. With the client, an outside write before the backend's create is caught: the create returns Exists and the converge clears it. It doesn't close the window between that create and ResumeActor, or on a running actor, so the guarantee assumes the backend is the only policy writer in the atespace either way. I've made that explicit in docs/security.md and the WithEgressPolicy doc rather than refusing a config this wouldn't make safe.
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
The security-sensitive policy replacement depends on Substrate v1.0.0 precondition/version semantics and gateway cache behavior, but current tests establish storage ordering rather than traffic enforcement, leaving live gateway validation outstanding.
No open issues 🎉
<sub>Review effort: Balanced</sub>
Tatsinnit
aramase
Closes #251.
A changed
WithEgressPolicy(or-substrate-egress-policy-file) used to reach only actors created after it changed: an actor that already had a policy kept it. The backend now converges each actor's stored policy on the configured one before every resume of a suspended actor, with no opt-out.UpdateActorEgressPolicyunder the UID and version it read. A conflicting writer starts a new read, up to three attempts, then the call fails asErrActorBusy.Backend.SyncEgressPolicy(ctx, sessionUID)applies a changed policy to a running actor without suspending it. Substrate picks it up within its policy caches (about 10 seconds for requests and new connections, 5 seconds for DNS on v1.0.0); an open TLS passthrough connection is not cut. The daemon does not call it.ErrEgressPolicyInvalid, which reaches callers asFAILED_PRECONDITION/COMPUTE_CONFIG_INVALIDwith fixed text.WithEgressPolicyfunction keeps its code but not its text on every path, since a non-INTERNAL code publishes the text and the function's message can name a credential reference.Compatibility (changelog fragment in the Compatibility category):
EgressPolicyClientused withWithEgressPolicymust implementGetActorEgressPolicyandUpdateActorEgressPolicyas well asCreateActorEgressPolicy. AControlClientwithoutEgressPolicyClientis refused (ErrEgressPolicyUnsupported) on every resume of an existing actor and every Sync, whatever the function returns, instead of resuming with the old policy. The daemon already refused that setup at startup.ErrActorBusyand writes nothing: the actor under the child's name may no longer be the clone, and policy calls cannot check its UID. The configured policy is authoritative only while the backend is the only policy writer in its atespace.Docs updated: the
EgressPolicyClientandSyncEgressPolicydocs inruntime/substrate/egress.go,docs/security.md,docs/substrate-conformance.md(with spec and conformance IDs) andspec.md.Tested:
ControlClientcover unchanged, changed, nil-clears, create-exists and update-not-found races, precondition conflicts and retry exhaustion, error classification through Exec, Resume and Fork, fork children, and Sync. The fail-closed refusal for a client withoutEgressPolicyClientis pinned for Restore, Create attach and Sync.go test -race ./runtime/substrate/... ./session/...andgo test -race ./...inintegrations/substrate.gofmt,go vet,hack/changelog check,hack/check-doc-links.pyandhack/doccheckpass.