fix(bootstrap): allow author worktree bootstrap from clean control checkout (Closes #892)
Align assess_author_issue_bootstrap with bootstrap_permits_control_checkout so gitea_bootstrap_author_issue_worktree can create the first branches/ worktree without the lock↔worktree deadlock. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
+157
-46
@@ -386,6 +386,68 @@ def run_compensating_recovery(
|
||||
return recovery_info
|
||||
|
||||
|
||||
def _normalize_sha(value: str | None) -> str | None:
|
||||
"""Normalize a Git object id for comparison, or ``None`` when unknown."""
|
||||
normalized = (value or "").strip().lower()
|
||||
return normalized or None
|
||||
|
||||
|
||||
def _author_bootstrap_assessment(
|
||||
*,
|
||||
not_applicable: bool,
|
||||
allowed: bool,
|
||||
block: bool,
|
||||
reasons: list[str],
|
||||
workspace: str,
|
||||
root: str,
|
||||
branch: str | None,
|
||||
dirty: list[str],
|
||||
under_branches: bool,
|
||||
bootstrap_path: str | None = None,
|
||||
local_head_sha: str | None = None,
|
||||
remote_master_sha: str | None = None,
|
||||
exact_next_action: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Structured author-bootstrap assessment consumable by bootstrap_permits (#892).
|
||||
|
||||
Field shape mirrors :func:`create_issue_bootstrap._result` so the shared
|
||||
``bootstrap_permits_control_checkout`` predicate can prove control-checkout
|
||||
eligibility for ``gitea_bootstrap_author_issue_worktree`` the same way it
|
||||
does for ``create_issue``. Allowed control assessments must use empty
|
||||
``reasons`` — narrative belongs in other fields, not the refusal list.
|
||||
"""
|
||||
local_tip = _normalize_sha(local_head_sha)
|
||||
remote_tip = _normalize_sha(remote_master_sha)
|
||||
base_tips_verified = bool(local_tip and remote_tip and local_tip == remote_tip)
|
||||
return {
|
||||
"not_applicable": not_applicable,
|
||||
"allowed": allowed,
|
||||
"block": block,
|
||||
"proven": bool(allowed and not block and not not_applicable),
|
||||
"reasons": list(reasons),
|
||||
"workspace_path": workspace,
|
||||
"canonical_repo_root": root,
|
||||
"current_branch": branch,
|
||||
"dirty_files": list(dirty),
|
||||
"under_branches": under_branches,
|
||||
"exact_next_action": exact_next_action,
|
||||
"bootstrap_path": bootstrap_path,
|
||||
"task_scope": "author_issue_bootstrap",
|
||||
"local_head_sha": local_tip,
|
||||
"remote_master_sha": remote_tip,
|
||||
"base_tips_verified": base_tips_verified,
|
||||
}
|
||||
|
||||
|
||||
EXACT_NEXT_ACTION_AUTHOR_BOOTSTRAP = (
|
||||
"Restore the canonical control checkout to a clean accepted base branch "
|
||||
"(master/main/dev) that matches live master, with no tracked local edits. "
|
||||
"Re-resolve bootstrap_author_issue_worktree, then re-run "
|
||||
"gitea_bootstrap_author_issue_worktree from that clean control checkout. "
|
||||
"Do not use shell git worktree add as the primary path once bootstrap is healthy."
|
||||
)
|
||||
|
||||
|
||||
def assess_author_issue_bootstrap(
|
||||
*,
|
||||
workspace_path: str,
|
||||
@@ -397,7 +459,13 @@ def assess_author_issue_bootstrap(
|
||||
remote_master_sha_error: str | None = None,
|
||||
task: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Assess whether author issue worktree bootstrap may proceed from control or worktree root."""
|
||||
"""Assess whether author issue worktree bootstrap may proceed from control or worktree root.
|
||||
|
||||
#892: control-checkout successes emit the full field set required by
|
||||
``create_issue_bootstrap.bootstrap_permits_control_checkout`` (empty reasons,
|
||||
task_scope, base tip proof, binding paths) so the #274/#604 guards can
|
||||
waive control-checkout for this one sanctioned bootstrap task.
|
||||
"""
|
||||
root = os.path.realpath(canonical_repo_root or "")
|
||||
workspace = os.path.realpath(workspace_path or root or ".")
|
||||
branch = (current_branch or "").strip()
|
||||
@@ -407,34 +475,50 @@ def assess_author_issue_bootstrap(
|
||||
if root
|
||||
else False
|
||||
)
|
||||
local_tip = _normalize_sha(head_sha)
|
||||
remote_tip = _normalize_sha(remote_master_sha)
|
||||
|
||||
if not is_author_issue_bootstrap_task(task):
|
||||
return {
|
||||
"not_applicable": True,
|
||||
"allowed": False,
|
||||
"block": False,
|
||||
"proven": False,
|
||||
"reasons": ["task is not author_issue_bootstrap"],
|
||||
}
|
||||
return _author_bootstrap_assessment(
|
||||
not_applicable=True,
|
||||
allowed=False,
|
||||
block=False,
|
||||
reasons=["task is not author_issue_bootstrap"],
|
||||
workspace=workspace,
|
||||
root=root,
|
||||
branch=branch or None,
|
||||
dirty=dirty,
|
||||
under_branches=under_branches,
|
||||
)
|
||||
|
||||
# Already under branches/: ordinary #274 path applies; not a control waiver.
|
||||
if under_branches:
|
||||
return {
|
||||
"not_applicable": False,
|
||||
"allowed": True,
|
||||
"block": False,
|
||||
"proven": True,
|
||||
"bootstrap_path": "existing_branches_worktree",
|
||||
"reasons": [
|
||||
"workspace is already a registered worktree under branches/"
|
||||
],
|
||||
}
|
||||
return _author_bootstrap_assessment(
|
||||
not_applicable=True,
|
||||
allowed=False,
|
||||
block=False,
|
||||
reasons=["workspace is under branches/; ordinary #274 path applies"],
|
||||
workspace=workspace,
|
||||
root=root,
|
||||
branch=branch or None,
|
||||
dirty=dirty,
|
||||
under_branches=True,
|
||||
bootstrap_path="existing_branches_worktree",
|
||||
local_head_sha=local_tip,
|
||||
remote_master_sha=remote_tip,
|
||||
)
|
||||
|
||||
reasons: list[str] = []
|
||||
if workspace != root:
|
||||
if not root or workspace != root:
|
||||
reasons.append(
|
||||
"bootstrap requires workspace to be canonical control checkout or branches/ worktree"
|
||||
)
|
||||
if branch not in author_mutation_worktree.BASE_BRANCHES:
|
||||
if not branch:
|
||||
reasons.append(
|
||||
"control checkout is detached HEAD; expected an accepted base branch "
|
||||
f"({', '.join(sorted(author_mutation_worktree.BASE_BRANCHES))})"
|
||||
)
|
||||
elif branch not in author_mutation_worktree.BASE_BRANCHES:
|
||||
reasons.append(
|
||||
f"control checkout branch '{branch}' is not an accepted base branch "
|
||||
f"({', '.join(sorted(author_mutation_worktree.BASE_BRANCHES))})"
|
||||
@@ -444,37 +528,64 @@ def assess_author_issue_bootstrap(
|
||||
f"control checkout has tracked local edits: {', '.join(dirty[:5])}"
|
||||
)
|
||||
|
||||
if remote_master_sha_error:
|
||||
# Fail closed on missing tip proof (same bar as create_issue bootstrap #757).
|
||||
if not local_tip:
|
||||
reasons.append(
|
||||
f"could not verify live master tip: {remote_master_sha_error}"
|
||||
"control checkout HEAD SHA is unknown; base equivalence to live "
|
||||
"master cannot be proven (fail closed)"
|
||||
)
|
||||
resolver_error = (remote_master_sha_error or "").strip() or None
|
||||
if resolver_error:
|
||||
reasons.append(
|
||||
f"live master tip could not be resolved ({resolver_error}); "
|
||||
"base equivalence cannot be proven (fail closed)"
|
||||
)
|
||||
elif not remote_tip:
|
||||
reasons.append(
|
||||
"live master tip is unknown; base equivalence cannot be proven "
|
||||
"(fail closed)"
|
||||
)
|
||||
elif local_tip and remote_tip and local_tip != remote_tip:
|
||||
reasons.append(
|
||||
f"control checkout HEAD ({local_tip[:12]}) != live master tip "
|
||||
f"({remote_tip[:12]})"
|
||||
)
|
||||
elif remote_master_sha and head_sha:
|
||||
h = head_sha.strip().lower()
|
||||
rm = remote_master_sha.strip().lower()
|
||||
if h != rm:
|
||||
reasons.append(
|
||||
f"control checkout HEAD ({h[:12]}) != live master tip ({rm[:12]})"
|
||||
)
|
||||
|
||||
if reasons:
|
||||
return {
|
||||
"not_applicable": False,
|
||||
"allowed": False,
|
||||
"block": True,
|
||||
"proven": False,
|
||||
"reasons": reasons,
|
||||
}
|
||||
return _author_bootstrap_assessment(
|
||||
not_applicable=False,
|
||||
allowed=False,
|
||||
block=True,
|
||||
reasons=reasons,
|
||||
workspace=workspace,
|
||||
root=root,
|
||||
branch=branch or None,
|
||||
dirty=dirty,
|
||||
under_branches=False,
|
||||
local_head_sha=local_tip,
|
||||
remote_master_sha=remote_tip,
|
||||
exact_next_action=EXACT_NEXT_ACTION_AUTHOR_BOOTSTRAP,
|
||||
)
|
||||
|
||||
return {
|
||||
"not_applicable": False,
|
||||
"allowed": True,
|
||||
"block": False,
|
||||
"proven": True,
|
||||
"bootstrap_path": "clean_canonical_control_checkout",
|
||||
"reasons": [
|
||||
"control checkout is clean on accepted base branch matching live master"
|
||||
],
|
||||
}
|
||||
# Allowed: empty reasons so bootstrap_permits_control_checkout can pass.
|
||||
return _author_bootstrap_assessment(
|
||||
not_applicable=False,
|
||||
allowed=True,
|
||||
block=False,
|
||||
reasons=[],
|
||||
workspace=workspace,
|
||||
root=root,
|
||||
branch=branch or None,
|
||||
dirty=dirty,
|
||||
under_branches=False,
|
||||
bootstrap_path="clean_canonical_control_checkout",
|
||||
local_head_sha=local_tip,
|
||||
remote_master_sha=remote_tip,
|
||||
exact_next_action=(
|
||||
"Call gitea_bootstrap_author_issue_worktree with the allocated "
|
||||
"issue/lease pins; it will create the branches/ worktree and lock."
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
import fcntl
|
||||
|
||||
Reference in New Issue
Block a user