Skip to content

perf(config): memoize successful OCI reads to halve warm startup latency#3397

Merged
dgageot merged 1 commit into
mainfrom
worktree-board-11206af511638a06
Jul 2, 2026
Merged

perf(config): memoize successful OCI reads to halve warm startup latency#3397
dgageot merged 1 commit into
mainfrom
worktree-board-11206af511638a06

Conversation

@dgageot

@dgageot dgageot commented Jul 1, 2026

Copy link
Copy Markdown
Member

A single docker-agent run <oci-ref> invocation reads the same agent-config source twice: once in the sandbox-default probe (resolveSandboxDefault) and once during team load. Each ociSource.Read performed a blocking registry digest round-trip (~600 ms) even when the artifact was already in the local content store and unchanged. Warm startup for OCI refs was ~1.3 s, versus ~80 ms for local or built-in agents.

ociSource.Read now memoizes successful results per process using the existing pkg/memoize helper (singleflight + TTL), with a 1-minute TTL. The cache key is the fully-qualified reference via a new remote.FullyQualifiedReference helper — two refs that differ only by registry host are distinct trust boundaries and never share cached bytes. Failures are never cached, and degraded fallbacks (registry unreachable → stale local copy served) are deliberately excluded from the cache via a sentinel error so that registry recovery is retried immediately on the next read. The corruption force-repull path is preserved unchanged; only a fully validated read populates the cache. Callers receive a clone of the cached bytes so no caller can mutate the shared entry.

Warm run <oci-ref> startup drops from ~1.3 s to ~0.65 s (one registry round-trip instead of two). Digest-pinned refs were already cache-first and are unaffected. New tests cover: memoization of successful reads (including equivalent ref forms sharing one entry), cache scoped to registry host, failed reads being retried rather than cached, and degraded fallbacks not being cached while validated reads are cached again after recovery. All tests are hermetic under go test -count=2.

A single `docker-agent run <oci-ref>` reads the same source twice (sandbox
probe + team load); without caching each read pays a blocking registry digest
round-trip even when the artifact is cached and unchanged. Memoize successful
reads per process (1-minute TTL, singleflight via pkg/memoize) so the second
call is free — warm OCI startup drops ~1.3s → ~0.65s.

The cache key is the fully-qualified reference (registry host included): two
refs differing only by registry are distinct trust boundaries and must never
share cached bytes. Failures are never cached, and neither are degraded
fallbacks (registry unreachable, stale local copy served) so recovery is
retried on the next read. The corruption force-repull path is preserved, and
callers get a clone of the cached bytes.

Assisted-By: Claude
@dgageot dgageot requested a review from a team as a code owner July 1, 2026 23:05
@aheritier aheritier added area/config For configuration parsing, YAML, environment variables area/distribution Agent registry, packaging, distribution, sharing status/needs-triage For issues that need to be triaged kind/refactor PR refactors code without behavior change labels Jul 1, 2026
@dgageot dgageot merged commit 0c6eccc into main Jul 2, 2026
13 checks passed
@dgageot dgageot deleted the worktree-board-11206af511638a06 branch July 2, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config For configuration parsing, YAML, environment variables area/distribution Agent registry, packaging, distribution, sharing kind/refactor PR refactors code without behavior change status/needs-triage For issues that need to be triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants