Skip to content

ADFA-7287: Stop rewriting unchanged plugin templates at every start - #2310

Open
davidschachterADFA wants to merge 5 commits into
stagefrom
fix/ADFA-7287-stable-plugin-templates
Open

davidschachterADFA wants to merge 5 commits into
stagefrom
fix/ADFA-7287-stable-plugin-templates

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

ADFA-7287

Plugins build and register their .cgt templates in activate(), which runs at every start. Each start wrote every template again, and copied it into the templates folder again, with the same contents and a new checksum: CgtTemplateBuilder left each zip entry's time unset, so ZipOutputStream stamped the current time, and both writes were unconditional. On a Note 20, the JavaScript add-on's three templates (1.7 MB) changed checksum at every start.

Change

  • CgtTemplateBuilder (plugin-api):
    • every entry gets a fixed time, 1980-02-01 00:00 local, the time Gradle gives reproducible archives, so the same template gives the same bytes;
    • build() builds the zip in memory and writes the file only when its bytes differ from what is there.
  • IdeTemplateServiceImpl.registerTemplate() (plugin-manager) copies only when the registered copy is missing or differs, comparing the two in 8 KB chunks. When nothing changed it reports no change, and still returns true. That skipped report has no effect today: nothing calls PluginManager.setTemplateReloadListener(), so the callback was already a no-op, and the template list rescans the folder by itself.
  • In both comparisons a failed read is logged at debug and counts as a difference, so the write is tried, as before.
  • KDoc on CgtTemplateBuilder.build() and IdeTemplateService.registerTemplate() says an identical file is left untouched.

Review by commit. Two commits only reformat with Spotless: CgtTemplateBuilder.kt (spaces to tabs, one parameter per line) and IdeTemplateService.kt. The first also re-indents the JSON inside the builder's raw strings; JSON ignores that whitespace, but a built template.json changes once.

No public API changes: plugin-api.api is unchanged, and the plugin API changelog lists capabilities, not this.

Sites checked

  • PluginProjectManager.extractBundledCgtTemplates() also writes into the templates folder unconditionally, but nothing calls it. Left alone.
  • core.cgt (BundledAssetsInstaller, SplitAssetsInstaller) is written when assets install, not at every start. Left alone.
  • Registered copies are deleted only when a plugin is disabled or uninstalled (cleanupAllTemplates, cleanupPluginTemplates); nothing deletes them at start, so the skip in registerTemplate() applies on a normal start.
  • The Shell Hooks add-on also copies cogo-hook-run.sh at every activation, on purpose. It lives in addons, not here; the ticket leaves it to its owner.

Testing

At the head, 3f153ea:

  • Unit tests, local: CgtTemplateBuilderTest (5), IdeTemplateServiceImplRegisterTest (5), and the existing PluginTemplateCleanupTest (3) and PluginManagerTemplateWiringTest (3) pass, none skipped. :plugin-api:apiCheck passes.
  • The tests fail without the fix:
    • On d2ae433, with the fixed time and both content checks taken out, the four tests aimed at them failed for the reasons they name: entry time, time zone, and an identical file or copy rewritten.
    • On 07c5418, with both comparisons cut to length-only, the three changed-template tests failed.
    • On 3f153ea, with both IOException catches removed, the two unreadable-file tests fail. They skip when run as root, which can read any file.
  • Independent AI reviews:
    • d2ae433: pass with minor findings (F01-F07). It built templates with the old, reformatted and fixed builders for 288 option combinations: all 576 template.json files parse, and all three versions mean the same. The fixed time gave one SHA-256 across all 632 JDK time zones, with no extra zip fields.
    • 07c5418, which fixes them (the no-op reload is disclosed under Change): pass with three nits (F08-F10). F08: the chunked comparison did not check that the plugin's file had also ended, which mattered only if it grew during the comparison.
    • 3f153ea, which fixes those: pass, no new findings.

Not done

  • A device check of two starts with the JavaScript add-on installed. The unit tests cover both writes; the add-on's source isn't in our repos, so its call sequence is taken from its compiled code.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw

davidschachterADFA and others added 5 commits November 24, 2026 14:08
ktlint's output for the whole file, before the ADFA-7287 change to it:
tabs, and one parameter per line. It also re-indents the JSON inside the
raw strings with tabs. JSON ignores that whitespace, so the templates
read the same, but a built .cgt's template.json bytes differ.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
Plugins build and register their .cgt templates in activate(), which
runs at every start. CgtTemplateBuilder left each zip entry's time
unset, so ZipOutputStream stamped the current time, and both the build
and the registration wrote unconditionally. Each start therefore wrote
every template twice, with the same contents and a new checksum.

- CgtTemplateBuilder gives every entry a fixed time (1980-02-01 00:00
  local, Gradle's choice for reproducible archives), builds the zip in
  memory, and writes the file only when its bytes differ.
- IdeTemplateServiceImpl.registerTemplate() copies only when the
  registered copy is missing or differs, and then skips the template
  list reload too. It still returns true.

Tests: CgtTemplateBuilderTest (4) and IdeTemplateServiceImplRegisterTest
(3) pass, as do PluginTemplateCleanupTest and
PluginManagerTemplateWiringTest. With the fixed time and the two
content checks taken out, the four tests aimed at them fail for the
reasons they name, and the three guard tests still pass.
:plugin-api:apiCheck passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
Tabs for spaces, and no blank line after the interface's opening brace,
before the next commit adds KDoc to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
- registerTemplate() compares the two files in 8 KB chunks instead of
  reading both whole, since a plugin may register a template of any
  size. In both comparisons a failed read counts as a difference, so
  the write is tried, as it was before.
- CgtTemplateBuilderTest changes one byte of a template without
  changing its length, so it pins the byte comparison: with length-only
  comparisons the changed-template tests fail.
  IdeTemplateServiceImplRegisterTest adds a 20,000-byte template that
  differs only in its last byte.
- The time-zone test uses Tokyo and Los Angeles, 17 hours apart in
  1980, and its failure message no longer prints both byte arrays.
- KDoc on CgtTemplateBuilder.build() and
  IdeTemplateService.registerTemplate() says an identical file is left
  untouched and no change is reported.
- The new log line uses {} placeholders.

The template-list reload that registerTemplate() now skips for an
identical copy was a no-op already: nothing calls
PluginManager.setTemplateReloadListener(). The template list rescans
the folder by itself.

Tests: CgtTemplateBuilderTest 4, IdeTemplateServiceImplRegisterTest 4,
PluginTemplateCleanupTest 3, PluginManagerTemplateWiringTest 3, all
pass. :plugin-api:apiCheck passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
- sameBytes() also checks that the plugin's .cgt has ended, in case it
  grew after its length was read. Before, a longer .cgt compared equal
  to a copy that matched its first part.
- A comparison that cannot read a file now logs that at debug level
  before it falls back to writing. Two tests cover that path: a built
  .cgt and a registered copy that are unreadable, the same length and
  different in content are written again. With either catch no longer
  catching IOException, those two tests fail. They skip when the test
  runs as root, which can read any file.
- KDoc: build() promises the same bytes on one device, not everywhere
  (deflate output depends on the runtime's zlib), and names its
  IllegalArgumentException. registerTemplate() costs reading both
  files, not one.

Tests: CgtTemplateBuilderTest 5, IdeTemplateServiceImplRegisterTest 5,
PluginTemplateCleanupTest 3 and PluginManagerTemplateWiringTest 3 pass,
none skipped. :plugin-api:apiCheck passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
@davidschachterADFA
davidschachterADFA marked this pull request as ready for review November 24, 2026 23:09

@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 Nov 24, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

Release notes:

  • Plugin templates (.cgt) are no longer rewritten at every start when their content is unchanged.
  • CgtTemplateBuilder.build writes an output file only when its bytes differ. ZIP entries use a fixed timestamp.
  • IdeTemplateService.registerTemplate copies a template only when the registered copy is missing or differs. The comparison reads in 8 KB chunks.
  • An unchanged registration returns true and does not notify template changes.
  • A failed comparison read is logged at debug level and treated as a difference, so the copy proceeds.
  • Added tests cover fixed timestamps, time-zone independence, unchanged and changed templates, unreadable files, and cleanup restore.

Risks and best-practice concerns:

  • The reload callback is a no-op. Nothing calls PluginManager.setTemplateReloadListener(). The template list rescans the folder itself.
  • Registration returns true for an unchanged copy. Callers that expect a change signal may misread the result.
  • Reproducible bytes hold on one device only. Deflate output can differ across runtimes because of zlib differences.
  • Reformatting the builder's raw-string JSON changes template.json bytes once. The JSON content is equivalent.
  • Unreadable-file tests skip when run as root. The unreadable-file path is not verified in that case.
  • A two-start device check with the JavaScript add-on was not performed. Its call sequence comes from compiled code, not source.
  • PluginProjectManager.extractBundledCgtTemplates(), asset-install writes, and the Shell Hooks activation copy are unchanged. They still write on every start.

The template builder now generates archives with fixed ZIP entry timestamps and leaves matching output files unchanged. Template registration now skips copying and change notifications when the registered copy matches the source. Tests cover both behaviors.

Changes: Template consistency

Layer / File(s) Summary
Deterministic archive builds
plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/templates/CgtTemplateBuilder.kt, plugin-api/src/test/kotlin/com/itsaky/androidide/plugins/templates/CgtTemplateBuilderTest.kt
The builder uses fixed timestamps for ZIP entries and skips writing when existing output bytes match. Tests check timestamp consistency, cross-time-zone output, and repeat-build behavior.
Idempotent template registration
plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/services/IdeTemplateService.kt, plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt, plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImplRegisterTest.kt
The API documentation describes registration behavior. The implementation compares source and destination contents before copying. Tests cover unchanged, changed, unreadable, and restored copies.

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

Suggested reviewers: daniel-adfa, hal-eisen-adfa

Merge Risk: Minimal · up to 3f153

This change stops plugin templates from being rewritten on every start when their contents have not changed. No concrete defect was found, and the skipped change notification currently has no listener in production. It looks safe to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the ticket and states the main change: plugin templates are no longer rewritten at every start when unchanged. The changeset adds fixed ZIP entry timestamps, in-memory archive building…
Description check Passed The description explains the problem (templates rewritten at every activation with new checksums), the fix in CgtTemplateBuilder and IdeTemplateServiceImpl.registerTemplate, the KDoc updates, the site…
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 ticket and states the main change: plugin templates are no longer rewritten at every start when unchanged. The changeset adds fixed ZIP entry timestamps, in-memory archive building with conditional writes in CgtTemplateBuilder, and a content comparison before copying in IdeTemplateServiceImpl.registerTemplate. The title matches these changes.

Full details: Description check

Explanation

The description explains the problem (templates rewritten at every activation with new checksums), the fix in CgtTemplateBuilder and IdeTemplateServiceImpl.registerTemplate, the KDoc updates, the sites checked, and the testing performed. It is related to the changeset.

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 checks each archive byte
Fixed timestamps keep the entries right
Matching copies rest in place
Changed ones take their proper place
The burrow builds with care tonight

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: 0e9bbe03-be2a-4b08-8bda-3e9af77d2b59
Commits

Reviewing files that changed from the base of the PR and between b5931b2 and 3f153ea.

Files selected for processing: 5

  • plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/services/IdeTemplateService.kt
  • plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/templates/CgtTemplateBuilder.kt
  • plugin-api/src/test/kotlin/com/itsaky/androidide/plugins/templates/CgtTemplateBuilderTest.kt
  • plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt
  • plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImplRegisterTest.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.

2 participants