Skip to content

ADFA-7438 Write cogo-plugin.jar and init.gradle only when they differ from the asset - #2360

Open
davidschachterADFA wants to merge 5 commits into
stagefrom
common/ADFA-7438-sync-extracted-files
Open

davidschachterADFA wants to merge 5 commits into
stagefrom
common/ADFA-7438-sync-extracted-files

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

ADFA-7438

Problem. At every start, ToolsManager.init deleted cogo-plugin.jar and init.gradle and wrote them again. A Gradle build that opened either file in that window found it missing, and every start wrote about 265 KB to flash for nothing.

Fix. A new package-private class, ExtractedFile, keeps a file equal to its APK asset:

  • syncIfChanged streams the file against the asset and stops at the first difference. It writes only when the file is missing or differs.
  • A write goes to <name>.part, is fsynced, then renamed over the target, so a reader sees the old complete file or the new one, never a missing or partial file. The temp file is removed on every failure path.
  • cogo-plugin.jar and init.gradle now go through syncIfChanged. init.gradle is copied as bytes; the old String round trip (readInitScript) is gone.
  • The tooling jar keeps its stamp check, since a full comparison would be too slow on the tooling server's launch path. It is now written through ExtractedFile.replaceAtomically, which adds the fsync. Its stamp file also records the jar's length, so a jar truncated after extraction re-extracts.

The two SQLite debugger AARs move onto syncIfChanged in a follow-up PR once #2262 merges.

Behaviour notes.

  • If a sync fails (a full disk, for example), the previous file stays in place and the failure is logged. Before, the file was deleted first, so it went missing.
  • A stamp written by the current release has no length, so the tooling jar re-extracts once after this update. An app update changes the stamp's lastUpdateTime anyway, so this coincides with the upgrade.

Tests (:common:testV8DebugUnitTest, managers package): ExtractedFileTest 16 and ToolsManagerToolingJarTest 16, all passing.

  • ExtractedFileTest covers:
    • first start;
    • a restart with no write (checked by mtime and by the asset's open count);
    • an app update;
    • truncated, longer and flipped-byte files;
    • a reader during the write;
    • failing sources and a failed rename;
    • an asset stream that returns a few bytes per read;
    • files of exactly one and two buffers, and an empty file;
    • a stale .part.
  • Revert proofs:
    • With the comparison disabled, the restart test fails, because the file is rewritten.
    • With the stamp check ignoring the length, the four new stamp tests fail.

Checked on a Galaxy J7 (Android 9; the J7's baseline debug build with only the changed classes2.dex swapped in, so the bundled assets stayed identical):

Step Result
First start after the update cogo-plugin.jar and init.gradle not rewritten (same mtime and md5); the tooling jar re-extracted once and its stamp ends in ;48496870, its size
Second start No file rewritten; no .part
:app:assembleDebug of an Empty Activity project Succeeded
cogo-plugin.jar truncated to 1,000 bytes and one byte of init.gradle changed, with the app stopped, then a start Both restored byte for byte (md5 equal to the assets) before the tooling server started; no .part

Not in this PR. The same delete-then-copy window exists in extractLogSender (the LogSender AAR, which Gradle also reads), extractColorScheme and ResourceUtils.copyFileFromAssets.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn


Code Reviewer: Code Reviewer couldn't review this pull request
The pull request author does not have access to AI for the associated Atlassian organization.

davidschachterADFA and others added 5 commits December 5, 2026 10:36
One write path for extracted files: temp sibling, fsync, atomic rename. The tooling jar keeps its stamp logic; it only gains the fsync, and a failed rename now removes its .part. AssetSource replaces ToolingJarSource. No call site of cogo-plugin.jar or init.gradle changes yet.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
Covers first start, restart with no write (mtime and open count), app
update, truncated, longer and flipped-byte files, a reader during the
write, and failing sources.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
… from the asset

Each start deleted both files and wrote them again, leaving a window where a build could find them missing, and wrote about 265 KB to flash. They now go through ExtractedFile.syncIfChanged: compared with the APK asset byte for byte, replaced through a temp file and atomic rename only when different. readInitScript is gone; the init script is copied as bytes.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
A stamp alone cannot see a jar truncated after extraction. The stamp file now holds versionName:lastUpdateTime;length and isToolingJarCurrent requires the jar's current length to match, so a truncated jar re-extracts. A stamp from the current release has no length and is treated as not current: the jar re-extracts once (an app update already changes lastUpdateTime, so this coincides with the upgrade).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
From the independent review: an inflating asset stream may return a few bytes per read, so the comparison and the copy are now tested against such a stream. Also tested: files of exactly one and two buffers, an empty file, files one byte off a buffer boundary, and a stale .part left by a killed process. The lock comment now names both callers and the lock order. No behaviour change.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai

coderabbitai Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

Release notes:

  • cogo-plugin.jar and init.gradle now rewrite only when missing or different from the APK asset.
  • Writes go to a .part sibling file, are synced, then renamed over the target. The previous file stays in place on failure.
  • init.gradle is copied as bytes. The String round trip is removed.
  • The tooling jar stamp now includes the jar length. A truncated jar triggers re-extraction.
  • Tests added for ExtractedFile and ToolingJar behavior.

Risks:

  • The tooling jar re-extracts once after upgrade. The current release stamp has no length.
  • The tooling jar still uses a stamp check, not a content comparison. Same-length byte corruption goes undetected.
  • syncIfChanged returns false on failure and logs it. Callers must check the result.
  • The delete-then-copy window remains in extractLogSender, extractColorScheme, and ResourceUtils.copyFileFromAssets.
  • The SQLite debugger AARs still use the old path. Migration is planned after #2262.
  • Test results are author-reported. They were not verified here.

The change adds ExtractedFile to compare asset contents and replace files through a temporary file. ToolsManager uses it for tooling-jar, Cogo-plugin, and init-script extraction. Tooling-jar stamps now include the extracted jar’s length.

Changes: Asset File Synchronization

Layer / File(s) Summary
File comparison and atomic replacement
common/src/main/java/com/itsaky/androidide/managers/ExtractedFile.java, common/src/test/java/com/itsaky/androidide/managers/ExtractedFileTest.kt
ExtractedFile compares asset and target contents in chunks. When the contents differ, it writes to a sibling .part file, syncs it, and renames it over the target. Tests cover matching and changed files, short reads, replacement behavior, and failures.
ToolsManager asset extraction and jar stamps
common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java, common/src/test/java/com/itsaky/androidide/managers/ToolsManagerToolingJarTest.kt
ToolsManager uses ExtractedFile for tooling-jar, Cogo-plugin, and init-script extraction. The tooling-jar stamp includes the extracted jar’s length. Tests cover length mismatches and legacy stamps.

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

Suggested reviewers: fryanpan, itsaky-adfa

Merge Risk: Minimal · up to 030a5

This change avoids rewriting the Cogo plugin jar and init script when they already match the app's assets, and closes the delete-then-copy window. It keeps the previous file if an update fails. The tooling jar re-extracts once after this update because its stamp format changed. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: cogo-plugin.jar and init.gradle are written only when they differ from the APK asset. The changeset adds ExtractedFile.syncIfChanged, which matches this, and the ticke…
Description check Passed The description states the problem (ToolsManager.init deleted and rewrote cogo-plugin.jar and init.gradle at every start), the fix (ExtractedFile with syncIfChanged and atomic replacement), the toolin…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: cogo-plugin.jar and init.gradle are written only when they differ from the APK asset. The changeset adds ExtractedFile.syncIfChanged, which matches this, and the ticket ID gives traceability. The title is specific and concise.

Full details: Description check

Explanation

The description states the problem (ToolsManager.init deleted and rewrote cogo-plugin.jar and init.gradle at every start), the fix (ExtractedFile with syncIfChanged and atomic replacement), the tooling-jar stamp change, behaviour notes, test coverage, and device checks. It closely matches the changeset and objectives.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit watched the file compare,
Then saw fresh bytes arrive with care.
The old file stayed until the new,
Was synced and ready to come through.
“The stamp now notes the jar’s full size!”
The rabbit twitched its nose and sighed.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: c70a0ddf-f154-47dd-be7c-afb639b8e84a
Commits

Reviewing files that changed from the base of the PR and between e453096 and 030a539.

Files selected for processing: 4

  • common/src/main/java/com/itsaky/androidide/managers/ExtractedFile.java
  • common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java
  • common/src/test/java/com/itsaky/androidide/managers/ExtractedFileTest.kt
  • common/src/test/java/com/itsaky/androidide/managers/ToolsManagerToolingJarTest.kt

Included review availability: This review used your included allowance. Your plan provides up to 5 included reviews per hour; 3 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant