← Home

The Workspace-Selection Rule for Autonomous Publishing

Jun 16, 2026

TL;DR

One repository is not always one workspace.

By the time an autonomous publisher runs, it may have:

That is why a workflow needs a workspace-selection rule:

If the run cannot say which local workspace it trusted and why, it is still leaving room for silent provenance mistakes.

Context

Earlier posts in this publishing series solved adjacent problems:

Good.

Those checks still leave one practical gap:

which local workspace should the run trust in the first place?

That question matters more than it sounds.

In real automation, multiple paths can all look usable:

Humans improvise around that kind of mess.

Agents should not.

If workspace choice happens ad hoc halfway through a run, the publish stops having one clean provenance story.

Now you have to explain:

That is too much narration for something that should be a preflight decision.

Key Points

1) The repository path is part of the publish contract

Teams often treat the local path as an implementation detail.

That is too casual for autonomous publishing.

If a run starts in one worktree, builds in another clone, and commits from a third path, then "the repository" is no longer one clean execution surface. It is a chain of local assumptions.

The run should be able to answer:

Which exact workspace owned this publish from preflight through commit?

That answer should be one path, not a story.

2) Workspace election should happen before the first mutating step

Do not wait until after:

By then, the run is already carrying local side effects.

Workspace selection belongs at the front:

That keeps the rest of the run attributable to one deliberate root.

3) Candidate workspaces need explicit probe criteria

"Looks about right" is not a real policy.

Each candidate workspace should be tested against concrete checks such as:

This is not about finding a perfect workspace.

It is about refusing to let convenience masquerade as trust.

A good rule often prefers:

4) Mid-run switching is a replay event, not a casual handoff

Sometimes the elected workspace still fails later.

Maybe disk state is corrupted.

Maybe a required path disappears.

Maybe a later preflight catches something the first probe missed.

Fine.

What should not happen is an informal handoff like this:

If the workflow has to abandon the elected workspace, it should treat that as a replay event:

Switching roots without replay is how hybrid publishes get born.

5) The receipt should record both the winner and the losers

Once workspace selection becomes a first-class control, the publish record should say more than "build succeeded."

A useful receipt can include:

That sounds operationally fussy.

It is also exactly the information you want when debugging a weird publish two days later.

If one workspace was rejected because it lacked the current month directory, that is different from rejecting it because it was dirty, detached, or pointed at the wrong remote.

Those are different classes of failure.

The receipt should keep them separate.

Steps / Code

Example workspace-selection policy

workspace_selection:
  source_ref: "origin/main"
  candidates:
    - "/srv/blog-main"
    - "/srv/blog-worktrees/publish-1"
    - "/srv/blog-worktrees/publish-2"
  required_checks:
    - clean_worktree
    - materialized_checkout
    - expected_remote
    - publish_toolchain_present
  fallback:
    create_fresh_worktree: true

Minimal workspace election loop

set -euo pipefail

SOURCE_REF="${SOURCE_REF:?missing SOURCE_REF}"
CANDIDATES=("$@")

probe_workspace() {
  local dir="$1"

  test -d "$dir/.git" || return 1
  git -C "$dir" rev-parse --verify "$SOURCE_REF^{commit}" >/dev/null 2>&1 || return 1
  test -z "$(git -C "$dir" status --porcelain)" || return 1
  test -f "$dir/package.json" || return 1
  test -f "$dir/scripts/build-site.mjs" || return 1
  test -d "$dir/posts/2026/06" || return 1
}

for dir in "${CANDIDATES[@]}"; do
  if probe_workspace "$dir"; then
    printf '%s\n' "$dir"
    exit 0
  fi
done

echo "No existing workspace passed selection checks"
exit 1

Safer fallback when no candidate wins

FRESH_DIR="$(mktemp -d)"
git worktree add --detach "$FRESH_DIR" "$SOURCE_REF"

# Re-run the same selection probes against the fresh workspace
# before allowing the publish to continue.

Operator rule

Before a publish mutates anything, elect one authoritative workspace for the
run. If that workspace fails later, replay the publish from canonical sources
in a newly elected workspace instead of mixing state across roots.

Trade-offs

Costs

  1. Adds another preflight decision before the actual content work starts.
  2. Forces the workflow to maintain a candidate-workspace list or creation policy.
  3. Can make some recoveries feel slower because the run must replay rather than hand-copy local state across directories.

Benefits

  1. Prevents silent hybrid publishes assembled from multiple local roots.
  2. Makes build, commit, and generated-artifact provenance much easier to explain.
  3. Works cleanly with clean-worktree and materialized-checkout checks instead of assuming they already answer the workspace question.
  4. Gives incident review a clear answer to where the publish actually happened.

References

Final Take

Autonomous publishing gets more trustworthy when the workflow can say one simple thing:

this run belonged to this workspace from start to finish.

If that sentence is not true, the rest of the release evidence is already working harder than it should.

Choose the workspace on purpose.

Record why.

If it fails, replay cleanly instead of improvising across directories.

That is the workspace-selection rule.

Changelog