Rewrite OpenCode plugins an older release left behind (#3554, #3732). Releases up to 0.9.74 wrote a plugin only OpenCode 1 can load, and an opencode.json entry OpenCode 2 fails on. Upgrading OpenCode to 2 turns both into a load error on every start, and upgrading graphify touches neithe
()
| 407 | |
| 408 | |
| 409 | def _refresh_v1_opencode_plugins() -> None: |
| 410 | """Rewrite OpenCode plugins an older release left behind (#3554, #3732). |
| 411 | |
| 412 | Releases up to 0.9.74 wrote a plugin only OpenCode 1 can load, and an |
| 413 | opencode.json entry OpenCode 2 fails on. Upgrading OpenCode to 2 turns both |
| 414 | into a load error on every start, and upgrading graphify touches neither. |
| 415 | So on a CLI run, replace such a plugin with the current one and drop the |
| 416 | entry. |
| 417 | |
| 418 | Two directories are checked: the working directory, where the install |
| 419 | writes the plugin, and the home directory, where it lands when the install |
| 420 | was run from there (OpenCode then loads it for every project below home). |
| 421 | Only a plugin graphify wrote is touched. The entry is dropped beside a |
| 422 | current plugin too: a checkout or a failed write can bring it back alone. |
| 423 | |
| 424 | An old plugin is replaced only when the installed OpenCode can load the new |
| 425 | one (see _opencode_loads_current_plugin), and under the directory's lock, |
| 426 | without waiting: when another graphify process is already at it, this one |
| 427 | leaves it to them. ``GRAPHIFY_NO_AUTO_REFRESH=1`` opts out, as it does for |
| 428 | skills. |
| 429 | """ |
| 430 | if os.environ.get("GRAPHIFY_NO_AUTO_REFRESH", "").strip().lower() in ("1", "true", "yes"): |
| 431 | return |
| 432 | try: |
| 433 | # The cwd and home .opencode dirs are where installs before #3398 wrote, |
| 434 | # ~/.config/opencode is the global install's dir since. |
| 435 | directories = dict.fromkeys( |
| 436 | d.resolve() |
| 437 | for d in ( |
| 438 | _opencode_dir(project=True, project_dir=Path.cwd()), |
| 439 | _opencode_dir(project=True, project_dir=Path.home()), |
| 440 | _opencode_dir(project=False), |
| 441 | ) |
| 442 | ) |
| 443 | except (OSError, RuntimeError): |
| 444 | return |
| 445 | loadable: "bool | None" = None # asked once, and only when an old plugin is found |
| 446 | for directory in directories: |
| 447 | plugin_file = directory / _OPENCODE_PLUGIN_PATH.relative_to(".opencode") |
| 448 | |
| 449 | def pending() -> "bool | None": |
| 450 | """True for an old plugin, False for a stale entry only, None for neither.""" |
| 451 | try: |
| 452 | body = plugin_file.read_text(encoding="utf-8") |
| 453 | except (OSError, UnicodeDecodeError): |
| 454 | return None # no plugin here |
| 455 | if not body.startswith(_OPENCODE_PLUGIN_HEADER): |
| 456 | return None # not graphify's |
| 457 | if _opencode_plugin_is_v1_only(body): |
| 458 | return True |
| 459 | return False if _opencode_config_names_plugin(directory) else None |
| 460 | |
| 461 | stale = pending() |
| 462 | if stale is None: |
| 463 | continue |
| 464 | if stale: |
| 465 | if loadable is None: |
| 466 | loadable = _opencode_loads_current_plugin() |
no test coverage detected