@@ -110,11 +110,11 @@ func (a ociSource) ParentDir() string {
110110 return ""
111111}
112112
113- // Read loads an agent configuration from an OCI artifact
113+ // Read loads an agent configuration from an OCI artifact.
114114//
115- // The OCI registry remains the source of truth
116- // The local content store is used as a cache and fallback only
117- // A forced re-pull is triggered exclusively when store corruption is detected
115+ // The OCI registry remains the source of truth.
116+ // The local content store is used as a cache and fallback only.
117+ // A forced re-pull is triggered exclusively when store corruption is detected.
118118func (a ociSource ) Read (ctx context.Context ) ([]byte , error ) {
119119 store , err := content .NewStore ()
120120 if err != nil {
@@ -129,59 +129,63 @@ func (a ociSource) Read(ctx context.Context) ([]byte, error) {
129129 return nil , fmt .Errorf ("normalizing OCI reference %s: %w" , a .reference , err )
130130 }
131131
132- tryLoad := func () ([]byte , error ) {
133- af , err := store .GetArtifact (storeKey )
134- if err != nil {
135- return nil , err
132+ // For digest references, the content is immutable. If we already have
133+ // the artifact locally, serve it directly without any network call.
134+ if remote .IsDigestReference (a .reference ) {
135+ if data , loadErr := loadArtifact (store , storeKey ); loadErr == nil {
136+ slog .Debug ("Serving digest-pinned OCI artifact from cache" , "ref" , a .reference )
137+ return data , nil
136138 }
137- return []byte (af ), nil
138139 }
139140
140- // Check if we have any local metadata
141- _ , metaErr := store .GetArtifactMetadata (storeKey )
142- hasLocal := metaErr == nil
141+ // Check whether we have a local copy to fall back on.
142+ hasLocal := hasLocalArtifact (store , storeKey )
143143
144- // Always try normal pull first (preserves pull-interval behavior)
144+ // Pull from registry (checks remote digest, skips download if unchanged).
145145 if _ , pullErr := remote .Pull (ctx , a .reference , false ); pullErr != nil {
146146 if ! hasLocal {
147147 return nil , fmt .Errorf ("failed to pull OCI image %s: %w" , a .reference , pullErr )
148148 }
149-
150- slog .Debug (
151- "Failed to check for OCI reference updates, using cached version" ,
152- "ref" , a .reference ,
153- "error" , pullErr ,
154- )
149+ slog .Debug ("Failed to check for OCI reference updates, using cached version" ,
150+ "ref" , a .reference , "error" , pullErr )
155151 }
156152
157- // Try loading from store
158- data , err := tryLoad ( )
153+ // Try loading from store.
154+ data , err := loadArtifact ( store , storeKey )
159155 if err == nil {
160156 return data , nil
161157 }
162158
163- // If loading failed due to corruption, force re-pull
164- if errors .Is (err , content .ErrStoreCorrupted ) {
165- slog .Warn (
166- "Local OCI store corrupted, forcing re-pull" ,
167- "ref" , a .reference ,
168- )
159+ // If corrupted, force re-pull and try once more.
160+ if ! errors .Is (err , content .ErrStoreCorrupted ) {
161+ return nil , fmt .Errorf ("failed to load agent from OCI source %s: %w" , a .reference , err )
162+ }
169163
170- if _ , pullErr := remote .Pull (ctx , a .reference , true ); pullErr != nil {
171- return nil , fmt .Errorf ("failed to force re-pull OCI image %s: %w" , a .reference , pullErr )
172- }
164+ slog .Warn ("Local OCI store corrupted, forcing re-pull" , "ref" , a .reference )
165+ if _ , pullErr := remote .Pull (ctx , a .reference , true ); pullErr != nil {
166+ return nil , fmt .Errorf ("failed to force re-pull OCI image %s: %w" , a .reference , pullErr )
167+ }
173168
174- data , err = tryLoad ()
175- if err == nil {
176- return data , nil
177- }
169+ data , err = loadArtifact (store , storeKey )
170+ if err != nil {
171+ return nil , fmt .Errorf ("failed to load agent from OCI source %s: %w" , a .reference , err )
178172 }
173+ return data , nil
174+ }
175+
176+ // loadArtifact reads the agent YAML from the content store.
177+ func loadArtifact (store * content.Store , storeKey string ) ([]byte , error ) {
178+ af , err := store .GetArtifact (storeKey )
179+ if err != nil {
180+ return nil , err
181+ }
182+ return []byte (af ), nil
183+ }
179184
180- return nil , fmt .Errorf (
181- "failed to load agent from OCI source %s: %w" ,
182- a .reference ,
183- err ,
184- )
185+ // hasLocalArtifact reports whether the content store has metadata for the given key.
186+ func hasLocalArtifact (store * content.Store , storeKey string ) bool {
187+ _ , err := store .GetArtifactMetadata (storeKey )
188+ return err == nil
185189}
186190
187191// urlSource is used to load an agent configuration from an HTTP/HTTPS URL.
0 commit comments