Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions design/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,22 @@ after their registries and agent outputs have been rolled back.
Failed setup or durable package commit restores those
changes, not entire agent directories or all of `.specify`. Independent workflow
progress and unowned user files are left untouched.
A built-in `upgrade`, or a built-in `init` into an existing directory, with no
external transaction journals the same way until its manifest and integration
state are saved, because `setup()` writes generated files, in place where they
exist, before the new manifest does. The upgrade journal starts before the first
shared-infrastructure refresh; the init journal starts at `setup()` and ends at
the `integration.json` write, so init's later shared-infrastructure install and
optional steps run after that commit, as before. Both journals' scopes are
`.specify` and the integration's own output. They take no project lock and read
no other integration's state; when the journal cannot cover the operation (a
symlinked or unsafe scope, an unreadable directory), it runs unjournalled as
before rather than failing. Extension and preset re-registration runs after that
commit, as best effort. A failed `init` into a new directory still removes the
directory instead.
Rollback also removes directories under its scopes that the census did not
record and that are still empty, such as an output directory `setup()` created
before failing; it never removes a file that way.
While the lifecycle journal is active, install/switch failure handlers defer
cleanup to it rather than forcing teardown or rewriting fallback state first.
This preserves pending-file conflicts and the original directory inventory.
Expand Down
10 changes: 9 additions & 1 deletion src/specify_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ def ensure_executable_scripts(project_path: Path, tracker: StepTracker | None =
"""Ensure POSIX .sh scripts under .specify/scripts and .specify/extensions (recursively) have execute bits (no-op on Windows)."""
if os.name == "nt":
return # Windows: skip silently
from .integrations._file_changes import changing_file
from .integrations.installer import IntegrationInstallError

scan_roots = [
project_path / ".specify" / "scripts",
project_path / ".specify" / "extensions",
Expand Down Expand Up @@ -249,8 +252,13 @@ def ensure_executable_scripts(project_path: Path, tracker: StepTracker | None =
new_mode |= 0o001
if not (new_mode & 0o100):
new_mode |= 0o100
os.chmod(script, new_mode)
# Journalled, so a rolled-back upgrade restores the old mode.
with changing_file(script):
os.chmod(script, new_mode)
updated += 1
except IntegrationInstallError:
# The upgrade journal refused the change; fail the operation.
raise
except Exception as e:
failures.append(f"{_display_project_path(project_path, script)}: {e}")
if tracker:
Expand Down
76 changes: 50 additions & 26 deletions src/specify_cli/command_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import shutil
import subprocess
import sys
from contextlib import nullcontext
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -262,7 +263,7 @@ def ensure_constitution_from_template(


def register(app: typer.Typer) -> None:
from .integrations._lifecycle import external_lifecycle, initial_directory_state, lifecycle_owns_rollback, lifecycle_success
from .integrations._lifecycle import builtin_journal, external_lifecycle, initial_directory_state, lifecycle_owns_rollback, lifecycle_success

@app.command()
@external_lifecycle("init")
Expand Down Expand Up @@ -729,16 +730,55 @@ def init(
project_path,
integration_parsed_options or None,
)
resolved_integration.setup(
project_path,
manifest,
parsed_options=integration_parsed_options or None,
script_type=selected_script,
raw_options=integration_options,
events=events_map,
)
manifest.save()
# Into an existing directory, journal every write up to the
# manifest and state commit, so a failure restores it instead
# of leaving files no manifest describes. A new directory is
# removed on failure instead.
journal = nullcontext()
if dir_existed_before and not lifecycle_owns_rollback():
from .integrations._helpers import _MANIFEST_READ_ERRORS

try:
recorded = IntegrationManifest.load(
resolved_integration.key, project_path
).files
except _MANIFEST_READ_ERRORS:
# Unreadable or absent: the journal still covers the
# integration's folder and observed writes.
recorded = {}
journal = builtin_journal(
project_path, resolved_integration.key, recorded,
lambda: resolved_integration.upgrade_output_folders(
project_path, integration_parsed_options or None, integration_options
),
)
with journal:
resolved_integration.setup(
project_path,
manifest,
parsed_options=integration_parsed_options or None,
script_type=selected_script,
raw_options=integration_options,
events=events_map,
)
manifest.save()
integration_settings = _with_integration_setting(
{},
resolved_integration.key,
resolved_integration,
script_type=selected_script,
raw_options=integration_options,
parsed_options=integration_parsed_options or None,
project_root=project_path,
)
_write_integration_json(
Comment thread
echarrod marked this conversation as resolved.
Resolved
project_path,
resolved_integration.key,
[resolved_integration.key],
integration_settings,
)

# Re-registration is best effort, so it runs after the commit.
if force:
from .integrations._helpers import (
_register_extensions_for_agent,
Expand Down Expand Up @@ -774,22 +814,6 @@ def init(
continuing="The project was re-initialized, but its manifest hashes may be stale.",
)

integration_settings = _with_integration_setting(
{},
resolved_integration.key,
resolved_integration,
script_type=selected_script,
raw_options=integration_options,
parsed_options=integration_parsed_options or None,
project_root=project_path,
)
_write_integration_json(
project_path,
resolved_integration.key,
[resolved_integration.key],
integration_settings,
)

tracker.complete(
"integration",
resolved_integration.config.get("name", resolved_integration.key),
Expand Down
Loading
Loading