Skip to content

fix(offline): honor OFFLINE_WORK in git-ref2info and memoize TTL#9797

Open
iav wants to merge 2 commits into
mainfrom
fix/offline-work-respect-cache
Open

fix(offline): honor OFFLINE_WORK in git-ref2info and memoize TTL#9797
iav wants to merge 2 commits into
mainfrom
fix/offline-work-respect-cache

Conversation

@iav

@iav iav commented May 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #6439.

memoized_git_ref_to_info() unconditionally ran git ls-remote against the
upstream source even with OFFLINE_WORK=yes; when the remote was unreachable
(e.g. git.kernel.org 502) the build aborted with a misleading network error
instead of honoring the offline flag.

Two coordinated guards:

  • memoize-cached.sh: when OFFLINE_WORK=yes, serve the cache file
    regardless of TTL (the alternative is a network call that will fail). The
    "stale cache served" event is logged via display_alert.

  • git-ref2info.sh: early offline guard before the ls-remote loop. For
    ref_type=commit the SHA1 is taken from the ref itself (no network); for
    branch/tag we look up a pinned SHA1 in config/sources/git_sources.json. If
    neither is available, exit_with_error with a clear message instead of letting
    curl/ls-remote surface as a 502.

    The same guard is applied to the include_makefile_body block (curl to git
    host) since there is no local-bare fallback in scope here.

Behaviour matrix unchanged for OFFLINE_WORK=no (default).

OFFLINE_WORK memoize cache pinned in git_sources.json result
no hit (fresh) * use cache (unchanged)
no hit (stale) * regen via ls-remote (unchanged)
no miss yes ls-remote then pinned override (unchanged)
no miss no ls-remote (unchanged)
yes hit (any age) * use cache (NEW: ignore TTL)
yes miss yes use pinned, no network (NEW)
yes miss no fail with clear message (NEW: previously misleading 502)
yes Makefile body needed + miss * fail with clear message (NEW)

Refactor: shared pinned-SHA helper

The pinned-SHA lookup from config/sources/git_sources.json was duplicated across
three call sites, two of which built the jq filter by concatenating source/
branch directly into the program text (breaks or mis-parses on values
containing quotes). Extracted into a single _git_sources_pinned_sha1 helper that
passes values through jq --arg, now shared by:

  • the offline guard,
  • the existing branch-resolution override in git-ref2info.sh,
  • fetch_from_repo() in git.sh.

Each call site validates the pinned SHA (40-hex) before use, failing loudly on a
malformed pin instead of silently overriding an already-resolved revision.

Test plan

Verified via standalone scaffold against an isolated ${SRC}/cache/memoize/
sandbox:

  • OFFLINE_WORK=yes build with prior populated cache -> uses cache, no network calls
  • OFFLINE_WORK=yes build with empty cache + no pinned SHA1 -> fails with clear "OFFLINE_WORK=yes but no SHA1 available ... run online once or pin sha1" message
  • OFFLINE_WORK=yes build with empty cache + pinned in config/sources/git_sources.json -> uses pinned SHA1, no network calls
  • OFFLINE_WORK=no (default) build -> behaviour unchanged (online ls-remote returned real SHA1)
  • OFFLINE_WORK=yes kernel-config (needs Makefile body) without cache -> fails with clear "Makefile body ... not in cache" message

Summary by CodeRabbit

  • New Features
    • Enhanced offline mode to resolve pinned git references using SHA1s from the sources configuration, reducing the need for online lookups.
  • Improvements
    • Added stricter validation for pinned git SHA1 values, with clearer errors when pinned entries are invalid.
    • Improved memoized behavior in offline mode by serving stale cached results instead of failing.
  • Bug Fixes
    • Prevented offline runs from proceeding when required cached data is missing, avoiding downstream failures.

@iav iav requested a review from a team as a code owner May 10, 2026 17:45
@iav iav requested review from lanefu and swissiety and removed request for a team May 10, 2026 17:45
@coderabbitai

coderabbitai Bot commented May 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 80d357b6-e2b7-4161-8fa0-eaf8e8c2334c

📥 Commits

Reviewing files that changed from the base of the PR and between adca367 and cd2a802.

📒 Files selected for processing (3)
  • lib/functions/general/git-ref2info.sh
  • lib/functions/general/git.sh
  • lib/functions/general/memoize-cached.sh
🚧 Files skipped from review as they are similar to previous changes (3)
  • lib/functions/general/git.sh
  • lib/functions/general/memoize-cached.sh
  • lib/functions/general/git-ref2info.sh

📝 Walkthrough

Walkthrough

Adds offline handling for memoized cache reuse and git ref resolution, including pinned SHA lookups from ${SRC}/config/sources/git_sources.json, offline early-fails for missing cached Makefile bodies, and validation of pinned branch SHAs.

Changes

Offline Mode Support

Layer / File(s) Summary
Cache staleness handling in run_memoized
lib/functions/general/memoize-cached.sh
Rewrites stale-cache detection with cache_is_stale; when OFFLINE_WORK=yes, stale cached entries are served instead of being deleted. The lock wait declarations keep the same defaults and infinite-wait behavior.
Git reference resolution with offline support
lib/functions/general/git-ref2info.sh
Adds _git_sources_pinned_sha1() for ${SRC}/config/sources/git_sources.json, uses it to skip git ls-remote for non-commit refs in offline mode, and fails fast when the cached Makefile body is unavailable offline.
Pinned branch validation in fetch_from_repo
lib/functions/general/git.sh
fetch_from_repo now resolves pinned branch revisions through _git_sources_pinned_sha1(), validates non-empty values as 40-hex SHA1s, and errors on invalid pinned values. Submodule name derivation is reformatted without changing logic.

Sequence Diagram(s)

sequenceDiagram
  participant BuildScript
  participant memoized_git_ref_to_info
  participant git_sources_json
  participant git_remote
  BuildScript->>memoized_git_ref_to_info: request ref info
  memoized_git_ref_to_info->>git_sources_json: lookup pinned SHA1
  alt pinned SHA1 found
    git_sources_json-->>memoized_git_ref_to_info: sha1
    memoized_git_ref_to_info-->>BuildScript: return pinned SHA1
  else OFFLINE_WORK=yes and no pinned SHA1
    memoized_git_ref_to_info-->>BuildScript: exit_with_error
  else online
    memoized_git_ref_to_info->>git_remote: git ls-remote
    git_remote-->>memoized_git_ref_to_info: sha1
    memoized_git_ref_to_info-->>BuildScript: return SHA1
  end
Loading

Estimated code review effort: 3 (Moderate) | ~22 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the offline cache and SHA1 resolution changes.
Linked Issues check ✅ Passed The changes match #6439 by preventing online SHA1 fetches offline, using pinned SHA1s, and serving stale cache entries.
Out of Scope Changes check ✅ Passed No unrelated code changes are evident; the refactor and alignment changes support the offline behavior fix.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/offline-work-respect-cache

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added size/small PR with less then 50 lines 05 Milestone: Second quarter release Needs review Seeking for review Framework Framework components labels May 10, 2026
@iav

iav commented May 10, 2026

Copy link
Copy Markdown
Contributor Author

Test results

Standalone scaffold (.tmp/test-offline-pr9797.sh) drives run_memoized + memoized_git_ref_to_info against an isolated ${SRC}/cache/memoize/ sandbox.

# Scenario Result
1 online, cache miss Producing new & cachinggit ls-remote → real SHA1 of linux-7.0.y HEAD (3fd2ca34…)
2 offline, cache hit Using cached → same SHA1, no network
3 offline, cache miss, no pin fatal: OFFLINE_WORK=yes but no SHA1 available for '<source>' 'branch' 'linux-7.0.y' - run online once to populate cache, or pin sha1 in config/sources/git_sources.json
4 offline, cache miss, pinned in git_sources.json OFFLINE_WORK: using pinned SHA1 from git_sources.json → pinned SHA1 returned, no network
5 offline, cache miss, include_makefile_body fatal: OFFLINE_WORK=yes but Makefile body for '<source>' 'linux-7.0.y' (sha1 …) not in cache - run online once to populate <SRC>/cache/memoize/

All three guards exercised; OFFLINE_WORK=no path verified unchanged via scenario 1.

@igorpecovnik igorpecovnik added 08 Milestone: Third quarter release and removed 05 Milestone: Second quarter release labels May 16, 2026
@iav iav force-pushed the fix/offline-work-respect-cache branch from 88aabfd to accc536 Compare May 20, 2026 00:55
@github-actions github-actions Bot added the 05 Milestone: Second quarter release label May 20, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/functions/general/git-ref2info.sh`:
- Around line 41-43: The offline_pinned assignment can produce multiple SHA
lines when git_sources.json has duplicate source+branch entries; change the jq
invocation in the offline_pinned assignment so it returns only a single match
(the first) for the given MEMO_DICT[GIT_SOURCE] and ref_name instead of multiple
lines (e.g., use jq’s first(...) or index [0] on the matching array) so the
subsequent 40-hex validation sees a single deterministic SHA for offline
resolution.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9fa27bd7-cd6a-4c46-ade3-b86004f8cfba

📥 Commits

Reviewing files that changed from the base of the PR and between 88aabfd and accc536.

📒 Files selected for processing (2)
  • lib/functions/general/git-ref2info.sh
  • lib/functions/general/memoize-cached.sh

Comment thread lib/functions/general/git-ref2info.sh Outdated
@iav iav force-pushed the fix/offline-work-respect-cache branch 2 times, most recently from c4cabdc to a7acb6f Compare May 21, 2026 19:27
@iav

iav commented May 27, 2026

Copy link
Copy Markdown
Contributor Author

@ColorfulRhino ping

@iav iav force-pushed the fix/offline-work-respect-cache branch 4 times, most recently from 44e689f to 4c8d016 Compare June 14, 2026 14:18
@github-actions github-actions Bot added size/medium PR with more then 50 and less then 250 lines and removed size/small PR with less then 50 lines labels Jun 14, 2026
@iav

iav commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

Refactored the pinned-SHA lookup into a small shared helper (_git_sources_pinned_sha1), now used by both the offline guard and the existing branch-resolution block.

Beyond removing the duplicated jq call, this addresses a latent issue in the pre-existing block: it interpolated ${MEMO_DICT[GIT_SOURCE]} and ${ref_name} straight into the jq program text via shell concatenation, which breaks or mis-parses on source/branch values containing quotes. The helper passes them through jq --arg, so they are treated as data rather than program text.

Also rebased onto current main.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/functions/general/git-ref2info.sh`:
- Around line 121-127: The condition checking `cached_revision` from the
`_git_sources_pinned_sha1` function call only validates that it is non-empty,
but does not verify it is a valid SHA1 format. This allows malformed pinned
values to overwrite the already-validated `sha1` from line 99. Add proper
validation of the `cached_revision` value to ensure it matches valid SHA1 format
before assigning it to `sha1`. Only override the `sha1` variable if
`cached_revision` passes the same validation checks that were applied to the
original `sha1` to prevent invalid MEMO_DICT[SHA1] values from being propagated.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e7212a4d-2ee7-413f-ad6e-94852bb1930c

📥 Commits

Reviewing files that changed from the base of the PR and between 44e689f and 4c8d016.

📒 Files selected for processing (2)
  • lib/functions/general/git-ref2info.sh
  • lib/functions/general/memoize-cached.sh
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/functions/general/memoize-cached.sh

Comment thread lib/functions/general/git-ref2info.sh
@iav iav force-pushed the fix/offline-work-respect-cache branch from 4c8d016 to 6ea7313 Compare June 14, 2026 14:31
@iav iav marked this pull request as draft June 14, 2026 14:48
@iav iav marked this pull request as ready for review June 14, 2026 14:59

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
lib/functions/general/git.sh (1)

255-263: Note inconsistent URL handling between git.sh and git-ref2info.sh when looking up pinned SHA1s.

While config/sources/git_sources.json doesn't currently exist in the repository, the code is designed to support it as user-configurable (as indicated by comments like "populate cache, or pin sha1 in config/sources/git_sources.json").

There is an inconsistency in how the two callers pass URLs to _git_sources_pinned_sha1:

  • git.sh (line 257) passes "${url}", which has already been transformed by the GITHUB_SOURCE substitution on line 89
  • git-ref2info.sh passes MEMO_DICT[GIT_SOURCE], which preserves the original URL

If a user configures GITHUB_MIRROR (e.g., to ghproxy or gitclone), the transformed URL in git.sh won't match canonical URLs that would be stored in the JSON file, causing pinned SHA1 lookups to silently fail in git.sh while succeeding in git-ref2info.sh.

Consider passing the original (untransformed) URL to the helper, or transform it before writing to JSON, to ensure consistent matching across both call sites.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/functions/general/git.sh` around lines 255 - 263, The
_git_sources_pinned_sha1 function call in the branch handling block is passing
the already-transformed URL (which has been modified by GITHUB_SOURCE
substitution), but this won't match the canonical URLs that would be stored in
the configuration JSON file, causing pinned SHA1 lookups to fail when
GITHUB_MIRROR is configured. Pass the original untransformed URL to
_git_sources_pinned_sha1 instead of the transformed "${url}" variable, ensuring
the lookup key matches what users would configure in the JSON file.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@lib/functions/general/git.sh`:
- Around line 255-263: The _git_sources_pinned_sha1 function call in the branch
handling block is passing the already-transformed URL (which has been modified
by GITHUB_SOURCE substitution), but this won't match the canonical URLs that
would be stored in the configuration JSON file, causing pinned SHA1 lookups to
fail when GITHUB_MIRROR is configured. Pass the original untransformed URL to
_git_sources_pinned_sha1 instead of the transformed "${url}" variable, ensuring
the lookup key matches what users would configure in the JSON file.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 3fb63c03-93cc-409f-ae1a-afa7f8e414b6

📥 Commits

Reviewing files that changed from the base of the PR and between 6ea7313 and adca367.

📒 Files selected for processing (1)
  • lib/functions/general/git.sh

@iav

iav commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

Re the git.sh nitpick (pinned-SHA lookup keying on the mirror-transformed URL): this is pre-existing behavior — the block this commit refactored already used the same post-GITHUB_SOURCE $url as the lookup key, so the refactor doesn't change it. Filed separately as #9972 to decide whether to align fetch_from_repo with the canonical key used by git-ref2info.sh; keeping it out of scope for this cleanup.

iav added 2 commits June 14, 2026 22:12
Fixes #6439.

memoized_git_ref_to_info() unconditionally ran 'git ls-remote' against the
upstream source even with OFFLINE_WORK=yes; when the remote was unreachable
the build aborted with a misleading network error.

Two coordinated guards:

* memoize-cached.sh: when OFFLINE_WORK=yes, serve the cache file regardless
  of TTL (alternative is a network call that will fail). The "stale cache
  served" event is logged.

* git-ref2info.sh: early offline guard before the ls-remote loop. For
  ref_type=commit the SHA1 is taken from the ref itself (no network); for
  branch/tag we look up a pinned SHA1 in config/sources/git_sources.json.
  If neither is available, exit_with_error with a clear message instead
  of letting curl/ls-remote surface as 502.

  Same guard applied to the include_makefile_body block (curl to git host)
  since there is no local-bare fallback in scope here.

Behaviour matrix unchanged for OFFLINE_WORK=no (default).

Assisted-by: Claude:claude-opus-4.7
fetch_from_repo() carried its own copy of the pinned-SHA lookup from
memoized_git_ref_to_info(), with the same shell-interpolated jq program
(source/branch concatenated into the filter text). Replace it with the
shared _git_sources_pinned_sha1 helper added earlier in this branch,
which passes values through jq --arg, and validate the pinned SHA before
overriding fetched_revision so a malformed pin fails loudly instead of
silently — matching the branch path in git-ref2info.sh.

Pure code cleanup: removes the last duplicated jq-injection of this lookup.

Assisted-by: Claude:claude-opus-4.8
@iav iav force-pushed the fix/offline-work-respect-cache branch from adca367 to cd2a802 Compare June 14, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

05 Milestone: Second quarter release 08 Milestone: Third quarter release Framework Framework components Needs review Seeking for review size/medium PR with more then 50 and less then 250 lines

Development

Successfully merging this pull request may close these issues.

Scripts are still trying to fetch SHA1 online even if OFFLINE_WORK=yes is set

2 participants