Skip to content
Merged
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
28 changes: 27 additions & 1 deletion docs/UPSTREAM_AUTO.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ separate publication mandate, or a task's own `STOP` and read-only restrictions.
3. Normalize source identity, intervention ID and PR target before comparing
work. Existing five-source CAS claims cover their **batch** domain; they
do not prove exclusive ownership of PR-maintenance or outcome-review tasks.
**Resource scope follows the operation, not just the repository:**
- `READ_ONLY_REVIEW` of one PR reserves its exact `pr:owner/repo#number`
identity, **not** `source:owner/repo`. Other independent PR reviews in
the same repository may be selected in parallel. This is a deliberate
manifest contract: stale broad source locks in new PR review cards fail
validation rather than silently serializing every review.
- `INTERNAL_PREPARE` needs explicit `source:owner/repo` locks for each PR
target, in addition to the PR key. Mutable work therefore remains
conservatively serialized at repository scope until a separately
reviewed, complete path/branch/dependency lock scheme exists.
- A source-level claim is still mandatory for each frozen batch member,
and intervention-specific review without a PR retains its exact
intervention identity. No PR-level lock substitutes for batch source CAS.
4. Prefer genuine maintainer review/CI events and safely resumable work over
speculative discovery; balance lane WIP, starvation and risk. Explain each
suggested priority with time-of-check source refs, without promoting a
Expand All @@ -72,7 +85,9 @@ separate publication mandate, or a task's own `STOP` and read-only restrictions.
The selector sorts by explicit priority, age (bounded anti-starvation boost),
and work-class weight: urgent maintainer/CI feedback and existing outcomes
generally precede speculative new screening. WIP caps are `maintenance=2`,
`outcomes=2`, `portfolio=1`, `batch=1`. These are provisional workload
`outcomes=2`, `batch=1`. Portfolio uses **two independent read-only reviews**
and **one mutable preparation** as separate per-operation caps, with a total
ceiling of three. These are provisional workload
heuristics, **not** VSM S2 decisions by themselves. `status` makes **no write**,
takes no reservation and creates no new Organization `NOW`/`NEXT` entry.

Expand Down Expand Up @@ -103,6 +118,17 @@ only reserves the named local task. The agent must **revalidate current
source, owner permissions and task-specific tests before any work**, execute
at most one admitted action, record the actual owner result, and release with
an attributable reason. A release does not itself prove successful execution.
New claim/release events additionally record `operation` to make WIP
attribution reproducible after the original worklist expires. Historic ledger
events without that field remain readable; until release, a legacy portfolio
claim consumes **both** operation caps conservatively and cannot be silently
reclassified by a new task proposal.

At **any** subsequent mutable write or integration boundary the native owner
must re-read the exact PR head and base, changed files including rename/delete
identities, and relevant dependencies, then prove the writes do not overlap
or serialize them. A read-only task's disjoint changed-file preview is **not**
proof of merge compatibility, nor authority to modify a branch or merge.
`resume` never silently reassigns a claim. Claims older than 24 hours are
reported as `STALE_CLAIM_UNRESOLVED` until auditable recovery; they are not
automatically stolen, expired or treated as completed.
Expand Down
60 changes: 45 additions & 15 deletions scripts/upstream_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class AutoError(ValueError):
LEDGER_REF = "refs/heads/upstream-auto-task-reservations"
LANE_OWNER = {"maintenance": 125, "outcomes": 159, "portfolio": 150,
"batch": 56}
LANE_WIP_CAP = {"maintenance": 2, "outcomes": 2, "portfolio": 1,
LANE_WIP_CAP = {"maintenance": 2, "outcomes": 2, "portfolio": 3,
"batch": 1}
PORTFOLIO_OPERATION_WIP_CAP = {"READ_ONLY_REVIEW": 2, "INTERNAL_PREPARE": 1}
LANE_BONUS = {"maintenance": 8, "outcomes": 7, "batch": 5,
"portfolio": 4}
PRIORITY_BASE = {"P0": 100, "P1": 70, "P2": 40, "P3": 10}
Expand All @@ -48,6 +49,7 @@ class AutoError(ValueError):
"priority", "operation", "head_sha"}
EVENT_FIELDS = {"action", "claim_id", "task_id", "actor", "resources",
"owner_issue", "evidence_url", "timestamp"}
EVENT_OPERATION = {"operation"}


def _timestamp(value):
Expand Down Expand Up @@ -111,7 +113,7 @@ def validate_manifest(data, *, now=None):
raise AutoError("Unknown task admission status")
if raw["priority"] not in PRIORITY_BASE:
raise AutoError("Unknown task priority")
if raw["operation"] not in {"READ_ONLY_REVIEW", "INTERNAL_PREPARE"}:
if not isinstance(raw["operation"], str) or raw["operation"] not in {"READ_ONLY_REVIEW", "INTERNAL_PREPARE"}:
raise AutoError("External writes are not an Upstream Auto task operation")
queue_time, observed = _timestamp(raw["queued_at"]), _timestamp(raw["observed_at"])
if queue_time > observed or observed > snapshot or now - observed > timedelta(minutes=60):
Expand All @@ -131,8 +133,13 @@ def validate_manifest(data, *, now=None):
evidence_url = _issue_url(raw["evidence_url"])
pr_refs = [k[3:] for k in keys if k.startswith("pr:")]
evidence = urlparse(evidence_url).path.casefold()
if any("source:" + name.split("#", 1)[0] not in keys for name in pr_refs):
raise AutoError("PR task needs its normalized source resource lock")
if pr_refs and raw["operation"] == "READ_ONLY_REVIEW" and any(
key.startswith("source:") for key in keys):
raise AutoError("READ_ONLY_REVIEW PR must lock the exact PR, not a repository-wide source")
if raw["operation"] == "INTERNAL_PREPARE" and (
not any(key.startswith("source:") for key in keys)
or any("source:" + name.split("#", 1)[0] not in keys for name in pr_refs)):
raise AutoError("INTERNAL_PREPARE requires source locks for mutable PR resources")
if pr_refs and not any(evidence == "/" + name.replace("#", "/pull/") for name in pr_refs):
raise AutoError("PR work evidence must link to its exact target PR")
task = dict(raw, resources=keys)
Expand Down Expand Up @@ -161,8 +168,14 @@ def replay(ledger):
_issue_url(event["evidence_url"])
_timestamp(event["timestamp"])
keys = _resources(event["resources"])
if ("operation" in event and
(not isinstance(event["operation"], str)
or event["operation"] not in PORTFOLIO_OPERATION_WIP_CAP)):
raise AutoError("Unknown task claim operation")
if action == "CLAIM":
if set(event) != EVENT_FIELDS or claim_id in seen or event["task_id"] in {x["task_id"] for x in active.values()}:
if (set(event) not in (EVENT_FIELDS, EVENT_FIELDS | EVENT_OPERATION)
or claim_id in seen
or event["task_id"] in {x["task_id"] for x in active.values()}):
raise AutoError("Duplicate task claim")
collision = [k for k in keys if k in resources]
if collision:
Expand All @@ -172,10 +185,12 @@ def replay(ledger):
seen.add(claim_id)
else:
prior = active.get(claim_id)
if (set(event) != EVENT_FIELDS | {"reason"}
if (set(event) not in (EVENT_FIELDS | {"reason"},
EVENT_FIELDS | EVENT_OPERATION | {"reason"})
or prior is None or not isinstance(event["reason"], str)
or len(event["reason"].strip()) < 15
or _timestamp(event["timestamp"]) < _timestamp(prior["timestamp"])
or event.get("operation") != prior.get("operation")
or any(event[k] != prior[k] for k in ("task_id", "actor", "resources", "owner_issue"))):
raise AutoError("Release requires active original claim, actor and auditable reason")
for key in keys:
Expand All @@ -191,17 +206,19 @@ def plan(manifest, ledger, *, now=None):
active = replay(ledger)
busy = {key for event in active.values() for key in event["resources"]}
lane_active = {lane: 0 for lane in LANE_OWNER}
portfolio_operations = {op: 0 for op in PORTFOLIO_OPERATION_WIP_CAP}
for event in active.values():
lane = next((t["lane"] for t in tasks if t["task_id"] == event["task_id"]), None)
# Claims may outlive a worklist; never trust the new proposal to
# reclassify an already held reservation's operation or owner lane.
lane = next((k for k, v in LANE_OWNER.items() if v == event["owner_issue"]), None)
if lane is not None:
lane_active[lane] += 1
# Existing claims not present in the input must still count against WIP.
# Their lane is derivable from stable owner issue, not current manifest.
for event in active.values():
if not any(t["task_id"] == event["task_id"] for t in tasks):
lane = next((k for k, v in LANE_OWNER.items() if v == event["owner_issue"]), None)
if lane is not None:
lane_active[lane] += 1
if lane == "portfolio":
# Historical v1 claims have no operation field. Conservatively
# count each toward both operation caps until released.
modes = (event["operation"],) if "operation" in event else PORTFOLIO_OPERATION_WIP_CAP
for mode in modes:
portfolio_operations[mode] += 1
eligible, blocked = [], []
for task in tasks:
if task["status"] != "ACTIONABLE":
Expand All @@ -213,6 +230,10 @@ def plan(manifest, ledger, *, now=None):
if lane_active[task["lane"]] >= LANE_WIP_CAP[task["lane"]]:
blocked.append({"task_id": task["task_id"], "reason": "LANE_WIP_CAP"})
continue
if (task["lane"] == "portfolio" and
portfolio_operations[task["operation"]] >= PORTFOLIO_OPERATION_WIP_CAP[task["operation"]]):
blocked.append({"task_id": task["task_id"], "reason": "PORTFOLIO_OPERATION_WIP_CAP"})
continue
age_hours = max(0, (now - _timestamp(task["queued_at"])).total_seconds() // 3600)
score = PRIORITY_BASE[task["priority"]] + min(60, age_hours * 2) + LANE_BONUS[task["lane"]]
eligible.append({"task_id": task["task_id"], "lane": task["lane"],
Expand Down Expand Up @@ -258,6 +279,7 @@ def live_preflight(manifest, task, reader):
"""Live source identity/PR state gate; semantic actionability remains agent-owned."""
if reader is None:
raise AutoError("BLOCKED_GITHUB_VISIBILITY: live reader required before claim")
base_shas = {}
try:
branch = reader.get("/repos/opensiro/upstream/branches/main")
if branch.get("commit", {}).get("sha") != manifest["upstream_main_sha"]:
Expand All @@ -273,14 +295,20 @@ def live_preflight(manifest, task, reader):
pr = reader.get(f"/repos/{repo}/pulls/{number}")
if pr.get("head", {}).get("sha") != task["head_sha"]:
raise AutoError("BLOCKED_PR_HEAD_CHANGED: re-read before dispatch")
if task["operation"] == "INTERNAL_PREPARE":
base_sha = pr.get("base", {}).get("sha")
if not isinstance(base_sha, str) or not SHA.fullmatch(base_sha):
raise AutoError("BLOCKED_PR_BASE_VISIBILITY: mutable PR base not pinned")
base_shas[identifier] = base_sha
if task["lane"] in {"maintenance", "portfolio"} and pr.get("state") != "open":
raise AutoError("BLOCKED_PR_LIFECYCLE: review target no longer open")
except AutoError:
raise
except (BatchError, KeyError, TypeError, ValueError, OSError) as exc:
raise AutoError("BLOCKED_GITHUB_VISIBILITY: incomplete first-party readback") from exc
return {"owner_issue": task["owner_issue"], "main_sha": manifest["upstream_main_sha"],
"head_sha": task["head_sha"], "status": "LIVE_PREFLIGHT_PASS"}
"head_sha": task["head_sha"], "pr_base_shas": base_shas,
"status": "LIVE_PREFLIGHT_PASS"}


def _append(root, remote, parent, ledger, event, *, write):
Expand Down Expand Up @@ -331,6 +359,7 @@ def claim(root, remote, manifest, actor, *, task_id=None, write=False, reader=No
event = {"action": "CLAIM", "claim_id": uuid.uuid4().hex,
"task_id": task_ref, "actor": actor, "resources": selected["resources"],
"owner_issue": selected["owner_issue"],
"operation": task["operation"],
"evidence_url": selected["evidence_url"],
"timestamp": now.strftime("%Y-%m-%dT%H:%M:%SZ")}
receipt = _append(root, remote, parent, ledger, event, write=write)
Expand All @@ -354,6 +383,7 @@ def resume(root, remote, actor, *, now=None):
age = now - _timestamp(event["timestamp"])
result.append({"claim_id": event["claim_id"], "task_id": event["task_id"],
"owner_issue": event["owner_issue"], "resources": event["resources"],
"operation": event.get("operation", "UNKNOWN_LEGACY"),
"status": "STALE_CLAIM_UNRESOLVED" if age > timedelta(hours=24) else "CLAIMED_NEEDS_REVALIDATION"})
return {"status": "RESUME_INSPECTION_ONLY", "claims": result}

Expand Down
Loading
Loading