Land guarded model delivery stabilization

This commit is contained in:
2026-10-18 14:50:52 +01:00
23 changed files with 1728 additions and 5 deletions
+1
View File
@@ -58,6 +58,7 @@ dependencies {
implementation(libs.androidx.compose.material.icons.extended)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.lifecycle.runtime.compose)
implementation(libs.androidx.lifecycle.process)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.lifecycle.viewmodel.ktx)
implementation(libs.kotlinx.coroutines.android)
@@ -1,12 +1,15 @@
package net.awan.pipelines
import android.app.Application
import androidx.lifecycle.ProcessLifecycleOwner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import net.awan.pipelines.data.ForegroundReconnector
import net.awan.pipelines.data.PipelinesRepository
import net.awan.pipelines.data.SettingsRepository
import net.awan.pipelines.data.transport.PipelinesClient
import net.awan.pipelines.data.transport.ServerUrl
import net.awan.pipelines.notify.RunNotifier
/**
@@ -30,8 +33,39 @@ class PipelinesApplication : Application() {
*/
val runNotifier: RunNotifier by lazy { RunNotifier(this) }
/**
* The one owner of return-to-foreground recovery: when the process comes back
* to the front it restores a connection the background took away, with no
* interaction. It is created once with the Application and observes the
* process lifecycle, so every Activity and ViewModel recreation leaves it
* untouched. The liveness check is the existing `ping`; the reconnect is the
* same shared-client connect the app's own Connect/Reconnect uses, so the
* policy lives only in `foregroundAction` and the client's backoff stays the
* only reconnect mechanism.
*/
private val foregroundReconnector: ForegroundReconnector by lazy {
ForegroundReconnector(
state = { repository.connectionState.value },
confirmAlive = {
// The existing ping is the positive check: on a dead socket it
// throws (or never answers) instead of returning, so reaching the
// next line is the proof of life.
repository.ping()
true
},
reconnect = {
// A saved URL that no longer validates is left for Settings to
// report; there is nothing to reconnect to.
val validated = ServerUrl.validate(settings.serverUrl.value)
if (validated.valid) repository.recover(validated)
},
scope = applicationScope,
)
}
override fun onCreate() {
super.onCreate()
runNotifier.ensureChannel()
ProcessLifecycleOwner.get().lifecycle.addObserver(foregroundReconnector)
}
}
@@ -0,0 +1,81 @@
package net.awan.pipelines.data
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
import net.awan.pipelines.data.transport.ConnectionState
import net.awan.pipelines.data.transport.ForegroundAction
import net.awan.pipelines.data.transport.foregroundAction
/**
* The one lifecycle-aware owner of the app's return-to-foreground recovery.
*
* While another app is in front Android freezes — and often kills — this process,
* so the socket it holds dies or goes quiet and the client's own bounded backoff
* either never starts or starts too late for the operator, who comes back and
* taps Connect. The client's backoff (see `PipelinesClient.scheduleReconnect`) is
* still the only reconnect mechanism; this owner only decides *when* to drive it,
* and does so the moment the app becomes visible again.
*
* It observes the whole process lifecycle (a `ProcessLifecycleOwner`), never the
* Activity or the ViewModel, both of which Android recreates: the owner is
* created once with the Application and survives every recreation. The decision
* itself is the one pure [foregroundAction] policy; this class supplies only the
* current state, a positive liveness check and the reconnect effect, so the
* policy is unit-testable with no device, socket or framework.
*/
class ForegroundReconnector(
private val state: () -> ConnectionState,
private val confirmAlive: suspend () -> Boolean,
private val reconnect: () -> Unit,
private val scope: CoroutineScope,
private val confirmTimeoutMillis: Long = CONFIRM_TIMEOUT_MILLIS,
) : DefaultLifecycleObserver {
/**
* The app became visible again. A state the policy calls reconnecting is
* reconnected at once — the pending backoff delay is cancelled by the
* reconnect effect, so the operator never waits it out. A socket the state
* still calls Connected is first confirmed to answer, so a silent
* "Connected" is never trusted; the state is re-read afterwards, because the
* socket's own failure callback may have run while the check was in flight.
*/
override fun onStart(owner: LifecycleOwner) {
val before = state()
if (before !is ConnectionState.Connected) {
if (foregroundAction(before, confirmedAlive = false) == ForegroundAction.Reconnect) reconnect()
return
}
scope.launch {
val alive = confirm()
if (foregroundAction(state(), alive) == ForegroundAction.Reconnect) reconnect()
}
}
/**
* Confirms a supposedly-connected socket still answers, bounded so a dead one
* cannot hold recovery: the check runs off the main thread on the app scope
* and a probe that fails, throws or outlives its budget counts as not alive.
*/
private suspend fun confirm(): Boolean =
withTimeoutOrNull(confirmTimeoutMillis) {
try {
confirmAlive()
} catch (t: Throwable) {
if (t is CancellationException) throw t
false
}
} ?: false
companion object {
/**
* Bounds the liveness check. A healthy round trip on the trusted LAN/VPN
* is far shorter, so a socket that cannot answer is noticed promptly
* instead of holding the "Connected" the operator is looking at.
*/
const val CONFIRM_TIMEOUT_MILLIS = 3_000L
}
}
@@ -66,6 +66,9 @@ class PipelinesRepository(
/** Opens the single shared connection to a validated server URL. */
fun connect(validated: ServerUrl.Validated) = client.connect(validated)
/** Recovers a lost connection without reversing an explicit Disconnect. */
fun recover(validated: ServerUrl.Validated) = client.recover(validated)
/** Explicit disconnect; the client then stays Idle. */
fun disconnect() = client.disconnect()
@@ -0,0 +1,40 @@
package net.awan.pipelines.data.transport
/** What the app does with the connection the moment it becomes visible again. */
enum class ForegroundAction {
/** Leave the connection exactly as it is. */
LeaveAlone,
/** Re-establish the connection now, cancelling any pending reconnect delay. */
Reconnect,
}
/**
* The app's return-to-foreground recovery policy, in one pure place.
*
* Android freezes and often kills a backgrounded app's process, so the socket
* the app holds dies or goes quiet while another app is in front and nothing in
* the process notices in time. Coming back must restore that connection without
* the operator tapping Connect — but an explicit Disconnect is the operator's
* own decision and stays authoritative. A deliberate Disconnect is the only way
* the client reaches [ConnectionState.Idle], so Idle is left alone; every other
* state reconnects.
*
* A still [ConnectionState.Connected] socket is not trusted on return: a frozen
* process can leave it claiming Connected while it answers nothing, which is
* exactly the stale state this replaces. The caller therefore checks it with a
* positive probe (the existing ping) and passes the verdict as [confirmedAlive];
* only a connection just confirmed to answer is left alone.
*
* @param state the connection state at the moment the app became visible again.
* @param confirmedAlive whether a [ConnectionState.Connected] socket has just
* been confirmed to answer. It is consulted only for Connected; every other
* state carries its own answer.
*/
fun foregroundAction(state: ConnectionState, confirmedAlive: Boolean): ForegroundAction = when (state) {
ConnectionState.Idle -> ForegroundAction.LeaveAlone
is ConnectionState.Connected ->
if (confirmedAlive) ForegroundAction.LeaveAlone else ForegroundAction.Reconnect
is ConnectionState.Connecting -> ForegroundAction.Reconnect
is ConnectionState.Disconnected -> ForegroundAction.Reconnect
}
@@ -0,0 +1,138 @@
package net.awan.pipelines.data
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import net.awan.pipelines.data.transport.ConnectionState
import org.junit.Assert.assertEquals
import org.junit.Test
/**
* The one lifecycle-aware owner of return-to-foreground recovery: it applies the
* pure `foregroundAction` policy to the state it reads, confirms a supposedly
* connected socket before trusting it, and reconnects when — and only when — the
* policy says so. The state, the liveness check and the reconnect are injected,
* so the owner is exercised with no device, socket or Android framework. The
* owner never touches the LifecycleOwner it is handed.
*/
class ForegroundReconnectorTest {
private val reconnects = AtomicInteger(0)
private val probes = AtomicInteger(0)
private val owner = object : LifecycleOwner {
override val lifecycle: Lifecycle get() = throw UnsupportedOperationException("not used")
}
private val connected = ConnectionState.Connected(build = null, serverTime = null)
/**
* A reconnector whose probe answers [alive] without suspending, so
* `Dispatchers.Unconfined` runs the whole `onStart` decision inline and the
* assertions below hold the moment it returns.
*/
private fun reconnector(
state: () -> ConnectionState,
alive: suspend () -> Boolean = { true },
) = ForegroundReconnector(
state = state,
confirmAlive = { probes.incrementAndGet(); alive() },
reconnect = { reconnects.incrementAndGet() },
scope = CoroutineScope(Dispatchers.Unconfined),
)
@Test
fun aDeliberateDisconnectIsLeftAloneAndNotEvenProbed() {
reconnector(state = { ConnectionState.Idle }).onStart(owner)
assertEquals(0, probes.get())
assertEquals(0, reconnects.get())
}
@Test
fun aDroppedConnectionReconnectsImmediatelyWithoutProbing() {
reconnector(state = { ConnectionState.Disconnected("socket closed", retrying = true) }).onStart(owner)
assertEquals("a dropped socket has nothing to confirm", 0, probes.get())
assertEquals(1, reconnects.get())
}
@Test
fun aConfirmedConnectedSocketIsLeftAlone() {
reconnector(state = { connected }).onStart(owner)
assertEquals(1, probes.get())
assertEquals(0, reconnects.get())
}
@Test
fun aConnectedSocketThatDoesNotAnswerReconnects() {
reconnector(state = { connected }, alive = { false }).onStart(owner)
assertEquals(1, probes.get())
assertEquals(1, reconnects.get())
}
@Test
fun aProbeThatThrowsIsNotAlive() {
reconnector(
state = { connected },
alive = { throw IllegalStateException("socket closed") },
).onStart(owner)
assertEquals(1, reconnects.get())
}
@Test
fun aSocketThatDiesWhileTheProbeRunsStillReconnects() {
// The socket's own failure callback lands mid-check; the owner re-reads
// the state and the policy then decides on what is actually there.
var state: ConnectionState = connected
reconnector(
state = { state },
alive = {
state = ConnectionState.Disconnected("socket closed", retrying = true)
false
},
).onStart(owner)
assertEquals(1, reconnects.get())
}
@Test
fun aDisconnectWhileTheProbeRunsStaysDisconnected() {
// The user can tap Disconnect while the bounded foreground probe is in
// flight. Re-reading state after that probe keeps the explicit choice
// authoritative rather than reconnecting over it.
var state: ConnectionState = connected
reconnector(
state = { state },
alive = {
state = ConnectionState.Idle
true
},
).onStart(owner)
assertEquals(0, reconnects.get())
}
@Test
fun aProbeThatNeverAnswersIsNotAliveWhenItsBudgetExpires() {
val done = CountDownLatch(1)
val reconnector = ForegroundReconnector(
state = { connected },
confirmAlive = { delay(5_000); true },
reconnect = {
reconnects.incrementAndGet()
done.countDown()
},
scope = CoroutineScope(Dispatchers.Unconfined),
confirmTimeoutMillis = 20L,
)
reconnector.onStart(owner)
assertEquals(
"the bounded check must give up on its own rather than hold the UI's Connected",
true,
done.await(2, TimeUnit.SECONDS),
)
assertEquals(1, reconnects.get())
}
}
+1
View File
@@ -23,6 +23,7 @@ androidx-compose-material3 = { group = "androidx.compose.material3", name = "mat
androidx-compose-material-icons-extended = { group = "androidx.compose.material", name = "material-icons-extended" }
androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigationCompose" }
androidx-lifecycle-runtime-compose = { group = "androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "lifecycle" }
androidx-lifecycle-process = { group = "androidx.lifecycle", name = "lifecycle-process", version.ref = "lifecycle" }
androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycle" }
androidx-lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycle" }
kotlinx-coroutines-android = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "coroutines" }
+1 -1
View File
@@ -47,7 +47,7 @@ func main() {
// Each new run gets a bounded one-line title from the configured default
// harness; the summariser falls back to the prompt's first line, so title
// generation never blocks or fails run creation.
options := server.Options{ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason}
options := server.Options{ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason, VerificationFixtures: server.VerificationFixturesFromEnvironment(os.Getenv)}
recorded := ""
if value, err := store.OpenCodeEndpoint(ctx); err != nil {
log.Printf("OpenCode endpoint setting unreadable: %v", err)
+47
View File
@@ -0,0 +1,47 @@
# Delivery stabilization
Date: 2026-10-18.
The server now has draft-only `delivery=true` support for OpenCode model stages.
At run time it derives the canonical `main` worktree and linked-worktree Git
metadata from Git rather than accepting paths from a stage prompt. It verifies
the committed item tree in a disposable candidate worktree before fast-forwarding
canonical `main`; declared checks are bounded configuration, not proof that any
deployed definition has used them. It is denied for Pi (until equivalent
capability enforcement exists), foreign repositories, missing main worktrees,
and malformed capabilities. This item does not publish or deploy a definition.
The source also supports an optional controlled-fixture verification declaration
(`backend`, `revision`, `capability`, and `fixture`). Ordinary server setup wires
an explicit, bounded `reconnect-foreground` HTTP fixture registry when
`PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_URL` and its expected `BACKEND`,
`REVISION`, `CAPABILITY`, and `STATE` values are configured. There is no default
remote URL. The server, not the model, probes that registered endpoint and
collects its returned backend identity, build revision, capability, fixture ID,
and state before comparing provenance. Unknown fixtures, incomplete setup,
wrong backend/revision/capability/state, non-200 responses, cancellation, and
oversized or malformed responses fail closed. A model result's labels are not
evidence. This is controlled local/backend fixture infrastructure, not evidence
of a rendered layout or a live Android device.
Definitions must be scratch-validated against the deployed server only after
the server support is deployed; no future delivery or verification definition
is activated by this source change.
`internal/author/skill/references/format.md` and
`internal/author/skill/references/queue.md` are stale: a later docs fold should
describe `delivery=true`, OpenCode-only availability, candidate verification,
and the required activation order (deploy server support before scratch-validating
and then publishing a definition).
OpenCode V2 resolves external resources at a containing directory, so delivery
uses the verified repository metadata directory rather than an impossible
single-lock-file scope. It does not grant a parent, home, foreign repository,
or global policy. Unrestricted shell permissions are not an OS sandbox.
Activation remains draft-only: deploy compatible server support, configure the
controlled fixture endpoint and matching provenance, scratch-validate the
fixture, then publish a definition. No definition is activated by this source
change.
Recovery checklist: retain `connection-reconnect` and unfinished feature
branches; merge only complete committed trees that pass mapped checks; do not
infer delivery from a dirty worktree or an old deployed-backend capture.
+95
View File
@@ -0,0 +1,95 @@
# Android: coming back to the app restores the connection
## What this item changed
Android app only.
- `data/transport/ConnectionRecovery.kt` (new): `ForegroundAction` and the one
pure `foregroundAction(state, confirmedAlive)` policy — `Idle` → leave alone (a
deliberate Disconnect is the only way the client reaches Idle), `Connected` with
a confirmed-alive socket → leave alone, `Connected` unconfirmed → reconnect,
`Connecting`/`Disconnected` → reconnect.
- `data/ForegroundReconnector.kt` (new): the one lifecycle-aware owner. It is
created once with the Application and observes `ProcessLifecycleOwner`, never
the Activity or the ViewModel (both recreated). On `ON_START` it applies the
policy: a non-Connected state reconnects at once, and the reconnect cancels the
client's pending backoff delay, so the operator never waits it out; a state
that still claims `Connected` is first confirmed with the existing `ping`,
bounded to 3 s, and the state is re-read afterwards (the socket's own failure
callback can land while the check is in flight).
- `PipelinesApplication.kt`: wires the owner to the process lifecycle;
`confirmAlive` is `repository.ping()`, `reconnect` is
`ServerUrl.validate(...) + repository.recover(...)`. Recovery shares the
client's state lock with Disconnect, so a Disconnect after the probe has made
its decision but before its reconnect effect runs stays authoritative. Manual
Connect/Reconnect continue to use `repository.connect(...)`; the client's
bounded backoff stays the only reconnect mechanism and the policy lives in the
one pure place.
- `data/transport/PipelinesClient.kt`: `open()` now closes the socket it
supersedes, and every `WebSocketListener` callback ignores a socket that is no
longer the current one (`isCurrent`). A failure the replaced socket reports late
— the common case when the process was frozen while it died — can then neither
clear the new connection nor schedule a reconnect that would undo the recovery.
- `app/build.gradle.kts` and `gradle/libs.versions.toml`:
`androidx.lifecycle:lifecycle-process` 2.8.7 (already in the offline cache,
with its `androidx.startup`/`androidx.tracing` transitives).
- Tests: `data/transport/ConnectionRecoveryTest.kt` (5 cases) and
`data/ForegroundReconnectorTest.kt` (8 cases).
## Shared documents this change makes stale
`android/README.md`.
- The Configuration section (`android/README.md:99-101`) — "Tap **Connect** to
open the single shared WebSocket (probed with `ping`, showing the server
build). Reconnect uses bounded exponential backoff; **Disconnect** stops it." —
should record that coming back to the app restores the connection on its own:
the process coming to the front reconnects immediately, cancelling the pending
backoff delay, and when the last state still claims Connected it first confirms
the socket with `ping` before trusting it. An explicit Disconnect stays
authoritative and is never reconnected over; the manual Reconnect control
stays, but normal recovery never needs it.
- The Architecture block (`android/README.md:310-323`) lists `data/transport` and
`data`. It should name the two new files — `data/transport/ConnectionRecovery.kt`
(the pure return-to-foreground policy) and `data/ForegroundReconnector.kt` (the
process-lifecycle owner) — and the new `androidx.lifecycle:lifecycle-process`
dependency behind the owner.
## Not stale
- `android/AGENTS.md` names neither the presence nor the absence of a lifecycle
observer; its "Where things are" summary stays correct (it could optionally add
the new owner beside `data/PipelinesRepository.kt`).
- `docs/app-integration-verification.md` — its connection mentions are about
restoring the stub URL, which this item does not change.
- No packaged OpenCode skill reference (`internal/author/skill/references/`)
documents the app's connection lifecycle, so nothing there is stale.
## Verified on the device, and what was not
Built and captured on the operator's Pixel 9 Pro XL (`47241FDAS00DLK`) from
`51a2ba4` + this change; after merging main the same tree (now `b93eee2`) was
rebuilt and the full unit suite re-run (691 tests, 0 failures). The operator's
`ws://10.0.0.75:8080/connect` and `/Users/alan/repos/pipelines` were used
throughout and were not changed. The phone was left on main's build.
Captured, all with `android/scripts/capture_png.sh` (every image validated):
- a **live** socket on return: the server log shows a `ping`/`pong` on the same
connection at the second of the return and no reconnect — the positive check
runs and a healthy connection is left alone;
- a **dead** socket on return: the reconnect and re-subscribe land in the same
second as the return, with no interaction (the process was frozen, so the
client's own backoff had not run);
- **no network** on return: the banner reads "Disconnected: Failed to connect to
…:8080 · retrying with backoff" — never a false Connected — and recovery
follows on its own once the network returns;
- an explicit **Disconnect**: still "Not connected. Enter the server URL in
Settings, then connect." after returning, with no reconnect in the server log.
**Not verified live:** the stale-Connected probe branch. In every capture the
frozen socket's own failure was delivered to the client before the lifecycle
owner ran, so the client already read `Disconnected` and no probe was attempted
(confirmed by the client's request-id sequence never skipping a value in the
server log). That branch — `Connected` still claimed, probe fails, reconnect — is
covered by unit tests only.
+185
View File
@@ -0,0 +1,185 @@
package server
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"git.a-wan.net/alan/pipelines/internal/stage"
)
// verifyDelivery proves that the stage landed a complete, committed tree before
// its success can settle the run. It re-derives the capability after the stage:
// a stage result and its input cannot select either the landing path or checks.
const maxDeliveryCheckOutput = 64 << 10
const deliveryCleanupTimeout = 10 * time.Second
// verifyDelivery verifies the committed item tree in a disposable worktree
// before fast-forwarding canonical main. Settlement deliberately performs only
// non-mutating repository checks after that mutation: a declared check must
// never be the first signal that the landed tree is known-red.
func verifyDelivery(ctx context.Context, workspace string, access stage.LandingAccess, checks []string) error {
lock, err := acquireDeliveryLock(ctx, access.LockPath)
if err != nil {
return err
}
defer lock.Close()
current, err := landingAccess(workspace)
if err != nil {
return fmt.Errorf("delivery access changed: %w", err)
}
if current != access {
return fmt.Errorf("delivery access changed during stage")
}
for _, directory := range []string{workspace, access.MainWorktree} {
status, err := gitOutput(directory, "status", "--porcelain=v1", "--untracked-files=all")
if err != nil {
return fmt.Errorf("delivery checkout status: %w", err)
}
if status != "" {
return fmt.Errorf("delivery checkout is not clean: %s", strings.Split(status, "\n")[0])
}
}
itemHead, err := gitOutput(workspace, "rev-parse", "HEAD")
if err != nil {
return fmt.Errorf("delivery item commit: %w", err)
}
if err := runDeliveryCandidate(ctx, workspace, itemHead, checks); err != nil {
return err
}
mainHead, err := gitOutput(access.MainWorktree, "rev-parse", "HEAD")
if err != nil {
return fmt.Errorf("delivery main commit: %w", err)
}
if err := exec.CommandContext(ctx, "git", "-C", workspace, "merge-base", "--is-ancestor", mainHead, itemHead).Run(); err != nil {
return fmt.Errorf("delivery item commit cannot fast-forward canonical main")
}
if output, err := exec.CommandContext(ctx, "git", "-C", access.MainWorktree, "merge", "--ff-only", itemHead).CombinedOutput(); err != nil {
return fmt.Errorf("delivery fast-forward canonical main: %w: %s", err, strings.TrimSpace(string(output)))
}
for _, directory := range []string{workspace, access.MainWorktree} {
status, err := gitOutput(directory, "status", "--porcelain=v1", "--untracked-files=all")
if err != nil {
return fmt.Errorf("delivery settlement status: %w", err)
}
if status != "" {
return fmt.Errorf("delivery checkout is not clean after checks: %s", strings.Split(status, "\n")[0])
}
}
// Re-derive at the settlement boundary. A success means exactly the checked
// item commit is now reachable from the same canonical main worktree.
current, err = landingAccess(workspace)
if err != nil || current != access {
return fmt.Errorf("delivery access changed during settlement")
}
mainHead, err = gitOutput(access.MainWorktree, "rev-parse", "HEAD")
if err != nil {
return fmt.Errorf("delivery main commit: %w", err)
}
if err := exec.CommandContext(ctx, "git", "-C", workspace, "merge-base", "--is-ancestor", itemHead, mainHead).Run(); err != nil {
return fmt.Errorf("delivery item commit is not an ancestor of canonical main")
}
return nil
}
func acquireDeliveryLock(ctx context.Context, path string) (*os.File, error) {
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, fmt.Errorf("delivery lock: %w", err)
}
for {
err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err == nil {
return file, nil
}
if !errors.Is(err, syscall.EWOULDBLOCK) && !errors.Is(err, syscall.EAGAIN) {
_ = file.Close()
return nil, fmt.Errorf("delivery lock: %w", err)
}
select {
case <-ctx.Done():
_ = file.Close()
return nil, fmt.Errorf("delivery lock: %w", ctx.Err())
case <-time.After(20 * time.Millisecond):
}
}
}
func runDeliveryCandidate(ctx context.Context, workspace, itemHead string, checks []string) error {
candidate, err := os.MkdirTemp(filepath.Dir(workspace), ".pipelines-delivery-")
if err != nil {
return fmt.Errorf("create delivery candidate: %w", err)
}
if err := os.Remove(candidate); err != nil {
return fmt.Errorf("prepare delivery candidate: %w", err)
}
if output, err := boundedCommandOutput(ctx, workspace, "worktree", "add", "--detach", candidate, itemHead); err != nil {
cleanupDeliveryCandidate(workspace, candidate)
return fmt.Errorf("create delivery candidate: %w: %s", err, strings.TrimSpace(string(output)))
}
defer cleanupDeliveryCandidate(workspace, candidate)
for _, check := range checks {
if err := runDeliveryCheck(ctx, candidate, check); err != nil {
return err
}
}
return nil
}
// cleanupDeliveryCandidate always uses its own bounded context: the stage context
// may already be canceled when a check fails or times out.
func cleanupDeliveryCandidate(workspace, candidate string) {
ctx, cancel := context.WithTimeout(context.Background(), deliveryCleanupTimeout)
defer cancel()
_ = exec.CommandContext(ctx, "git", "-C", workspace, "worktree", "remove", "--force", candidate).Run()
_ = os.RemoveAll(candidate)
}
func boundedCommandOutput(ctx context.Context, directory string, args ...string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "git", append([]string{"-C", directory}, args...)...)
var output boundedDeliveryOutput
cmd.Stdout = &output
cmd.Stderr = &output
err := cmd.Run()
return []byte(output.String()), err
}
func runDeliveryCheck(ctx context.Context, directory, check string) error {
cmd := exec.CommandContext(ctx, "sh", "-c", check)
cmd.Dir = directory
var output boundedDeliveryOutput
cmd.Stdout = &output
cmd.Stderr = &output
if err := cmd.Run(); err != nil {
return fmt.Errorf("delivery check %q failed: %w: %s", check, err, output.String())
}
return nil
}
type boundedDeliveryOutput struct{ bytes.Buffer }
func (b *boundedDeliveryOutput) Write(p []byte) (int, error) {
remaining := maxDeliveryCheckOutput - b.Len()
if remaining > 0 {
_, _ = b.Buffer.Write(p[:min(remaining, len(p))])
}
return len(p), nil
}
func (b *boundedDeliveryOutput) String() string {
text := strings.TrimSpace(b.Buffer.String())
if b.Len() == maxDeliveryCheckOutput {
return text + " (output truncated)"
}
return text
}
var _ io.Writer = (*boundedDeliveryOutput)(nil)
+130
View File
@@ -0,0 +1,130 @@
package server
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"git.a-wan.net/alan/pipelines/internal/stage"
)
// landingAccess derives delivery paths from Git's verified worktree metadata,
// never from stage input. A missing canonical main worktree fails closed.
func landingAccess(workspace string) (stage.LandingAccess, error) {
workspace, err := canonicalDirectory(workspace)
if err != nil {
return stage.LandingAccess{}, fmt.Errorf("delivery workspace: %w", err)
}
gitDir, err := gitOutput(workspace, "rev-parse", "--git-dir")
if err != nil {
return stage.LandingAccess{}, fmt.Errorf("delivery workspace is not a Git worktree: %w", err)
}
commonDir, err := gitOutput(workspace, "rev-parse", "--git-common-dir")
if err != nil {
return stage.LandingAccess{}, fmt.Errorf("delivery repository metadata: %w", err)
}
gitDir, err = canonicalDirectory(resolveGitPath(workspace, gitDir))
if err != nil {
return stage.LandingAccess{}, fmt.Errorf("delivery Git metadata: %w", err)
}
commonDir, err = canonicalDirectory(resolveGitPath(workspace, commonDir))
if err != nil {
return stage.LandingAccess{}, fmt.Errorf("delivery common Git metadata: %w", err)
}
main, err := mainWorktree(workspace, commonDir)
if err != nil {
return stage.LandingAccess{}, err
}
// Delivery originates in a linked item worktree. Its metadata must be under
// the repository's canonical common directory, never a standalone or foreign
// checkout. Git's common directory is the bounded capability needed for refs,
// worktree state, and the landing lock.
if !pathWithin(filepath.Join(commonDir, "worktrees"), gitDir) {
return stage.LandingAccess{}, fmt.Errorf("delivery workspace is not a linked Git worktree")
}
return stage.LandingAccess{MainWorktree: main, GitMetadata: commonDir, LockPath: filepath.Join(commonDir, "pipelines-landing.lock")}, nil
}
func mainWorktree(workspace, commonDir string) (string, error) {
list, err := gitOutput(workspace, "worktree", "list", "--porcelain")
if err != nil {
return "", fmt.Errorf("list repository worktrees: %w", err)
}
for _, record := range strings.Split(strings.TrimSpace(list), "\n\n") {
lines := strings.Split(record, "\n")
if len(lines) == 0 || !strings.HasPrefix(lines[0], "worktree ") || !worktreeHasBranch(lines, "refs/heads/main") {
continue
}
candidate, err := canonicalDirectory(strings.TrimPrefix(lines[0], "worktree "))
if err != nil || candidate == workspace {
continue
}
candidateGitDir, err := gitOutput(candidate, "rev-parse", "--git-dir")
if err != nil {
continue
}
candidateGitDir, err = canonicalDirectory(resolveGitPath(candidate, candidateGitDir))
if err != nil || candidateGitDir != commonDir {
continue
}
candidateCommon, err := gitOutput(candidate, "rev-parse", "--git-common-dir")
if err != nil {
continue
}
candidateCommon, err = canonicalDirectory(resolveGitPath(candidate, candidateCommon))
if err == nil && candidateCommon == commonDir {
return candidate, nil
}
}
return "", fmt.Errorf("no canonical main worktree in the delivery repository")
}
func worktreeHasBranch(lines []string, branch string) bool {
for _, line := range lines[1:] {
if line == "branch "+branch {
return true
}
}
return false
}
func canonicalDirectory(path string) (string, error) {
resolved, err := filepath.EvalSymlinks(path)
if err != nil {
return "", err
}
info, err := os.Stat(resolved)
if err != nil {
return "", err
}
if !info.IsDir() {
return "", fmt.Errorf("not a directory")
}
return filepath.Clean(resolved), nil
}
func resolveGitPath(workspace, value string) string {
if filepath.IsAbs(value) {
return value
}
return filepath.Join(workspace, value)
}
func pathWithin(parent, child string) bool {
rel, err := filepath.Rel(parent, child)
return err == nil && rel != "." && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
}
func gitOutput(directory string, args ...string) (string, error) {
cmd := exec.Command("git", append([]string{"-C", directory}, args...)...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
data, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("git %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(stderr.String()))
}
return strings.TrimSpace(string(data)), nil
}
+304
View File
@@ -0,0 +1,304 @@
package server
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
func TestLandingAccessAcceptsOnlyVerifiedSameRepositoryMainWorktree(t *testing.T) {
root := t.TempDir()
main := filepath.Join(root, "main")
runGit(t, root, "init", "-b", "main", main)
runGit(t, main, "config", "user.email", "test@example.invalid")
runGit(t, main, "config", "user.name", "test")
if err := os.WriteFile(filepath.Join(main, "README"), []byte("base\n"), 0600); err != nil {
t.Fatal(err)
}
runGit(t, main, "add", "README")
runGit(t, main, "commit", "-m", "base")
item := filepath.Join(root, "item")
runGit(t, main, "worktree", "add", "-b", "item", item)
access, err := landingAccess(item)
if err != nil {
t.Fatal(err)
}
canonicalMain, err := canonicalDirectory(main)
if err != nil {
t.Fatal(err)
}
if access.MainWorktree != canonicalMain || access.GitMetadata != filepath.Join(canonicalMain, ".git") || access.LockPath != filepath.Join(canonicalMain, ".git", "pipelines-landing.lock") {
t.Fatalf("unexpected access: %+v", access)
}
foreign := filepath.Join(root, "foreign")
runGit(t, root, "init", "-b", "main", foreign)
if _, err := landingAccess(foreign); err == nil {
t.Fatal("foreign repository without an item worktree gained landing access")
}
if _, err := landingAccess(main); err == nil {
t.Fatal("main worktree gained linked-worktree delivery access")
}
if err := os.RemoveAll(item); err != nil {
t.Fatal(err)
}
runGit(t, main, "worktree", "prune")
if _, err := landingAccess(main); err == nil {
t.Fatal("repository without a linked delivery worktree gained landing access")
}
secondItem := filepath.Join(root, "second-item")
runGit(t, main, "worktree", "add", "-b", "second-item", secondItem)
runGit(t, main, "branch", "-m", "main", "trunk")
if _, err := landingAccess(secondItem); err == nil {
t.Fatal("repository without a canonical main worktree gained landing access")
}
}
func TestVerifyDeliveryRequiresCleanLandedMainAndPassingChecks(t *testing.T) {
root := t.TempDir()
main := filepath.Join(root, "main")
runGit(t, root, "init", "-b", "main", main)
runGit(t, main, "config", "user.email", "test@example.invalid")
runGit(t, main, "config", "user.name", "test")
if err := os.WriteFile(filepath.Join(main, "README"), []byte("base\n"), 0600); err != nil {
t.Fatal(err)
}
runGit(t, main, "add", "README")
runGit(t, main, "commit", "-m", "base")
item := filepath.Join(root, "item")
runGit(t, main, "worktree", "add", "-b", "item", item)
if err := os.WriteFile(filepath.Join(item, "delivered"), []byte("complete\n"), 0600); err != nil {
t.Fatal(err)
}
runGit(t, item, "add", "delivered")
runGit(t, item, "commit", "-m", "delivery")
runGit(t, main, "merge", "--ff-only", "item")
access, err := landingAccess(item)
if err != nil {
t.Fatal(err)
}
if err := verifyDelivery(context.Background(), item, access, []string{"test -f delivered"}); err != nil {
t.Fatalf("complete landed delivery rejected: %v", err)
}
if err := os.WriteFile(filepath.Join(item, "missing-helper"), []byte("dirty\n"), 0600); err != nil {
t.Fatal(err)
}
if err := verifyDelivery(context.Background(), item, access, []string{"true"}); err == nil {
t.Fatal("dirty task-owned helper was accepted")
}
if err := os.Remove(filepath.Join(item, "missing-helper")); err != nil {
t.Fatal(err)
}
if err := verifyDelivery(context.Background(), item, access, []string{"false"}); err == nil {
t.Fatal("failed required check was accepted")
}
if err := os.WriteFile(filepath.Join(item, "unlanded"), []byte("no\n"), 0600); err != nil {
t.Fatal(err)
}
runGit(t, item, "add", "unlanded")
runGit(t, item, "commit", "-m", "unlanded")
if err := verifyDelivery(context.Background(), item, access, []string{"test -f unlanded"}); err != nil {
t.Fatalf("complete unlanded item was not landed after candidate verification: %v", err)
}
mainHead := runGitOutput(t, main, "rev-parse", "HEAD")
itemHead := runGitOutput(t, item, "rev-parse", "HEAD")
if mainHead != itemHead {
t.Fatal("verified item was not fast-forwarded into canonical main")
}
}
func TestVerifyDeliveryFailingCandidateLeavesMainUnchanged(t *testing.T) {
root := t.TempDir()
main := filepath.Join(root, "main")
runGit(t, root, "init", "-b", "main", main)
runGit(t, main, "config", "user.email", "test@example.invalid")
runGit(t, main, "config", "user.name", "test")
if err := os.WriteFile(filepath.Join(main, "README"), []byte("base\n"), 0600); err != nil {
t.Fatal(err)
}
runGit(t, main, "add", "README")
runGit(t, main, "commit", "-m", "base")
item := filepath.Join(root, "item")
runGit(t, main, "worktree", "add", "-b", "item", item)
if err := os.WriteFile(filepath.Join(item, "candidate"), []byte("candidate\n"), 0600); err != nil {
t.Fatal(err)
}
runGit(t, item, "add", "candidate")
runGit(t, item, "commit", "-m", "candidate")
access, err := landingAccess(item)
if err != nil {
t.Fatal(err)
}
before := runGitOutput(t, main, "rev-parse", "HEAD")
if err := verifyDelivery(context.Background(), item, access, []string{"test -f candidate", "false"}); err == nil {
t.Fatal("failing candidate check landed main")
}
if after := runGitOutput(t, main, "rev-parse", "HEAD"); after != before {
t.Fatal("failing candidate check changed canonical main")
}
}
func TestRunnerSettlesSuccessfulDeliveryAfterCandidateLanding(t *testing.T) {
ctx := context.Background()
root := t.TempDir()
main := filepath.Join(root, "main")
runGit(t, root, "init", "-b", "main", main)
runGit(t, main, "config", "user.email", "test@example.invalid")
runGit(t, main, "config", "user.name", "test")
if err := os.WriteFile(filepath.Join(main, "README"), []byte("base\n"), 0600); err != nil {
t.Fatal(err)
}
runGit(t, main, "add", "README")
runGit(t, main, "commit", "-m", "base")
item := filepath.Join(root, "item")
runGit(t, main, "worktree", "add", "-b", "item", item)
if err := os.WriteFile(filepath.Join(item, "unlanded"), []byte("no\n"), 0600); err != nil {
t.Fatal(err)
}
runGit(t, item, "add", "unlanded")
runGit(t, item, "commit", "-m", "unlanded")
store, err := pipeline.Open(filepath.Join(root, "runner.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
source := pipeline.Template([]string{"committer"}, true)
source = strings.Replace(source, "```configs", "```checks\ntrue\n```\n\n```configs", 1)
source = strings.Replace(source, "timeout=1h", "timeout=1h\ndelivery=true", 1)
if _, err := store.Submit(ctx, "delivery", source); err != nil {
t.Fatal(err)
}
items, err := store.List(ctx)
if err != nil {
t.Fatal(err)
}
r, err := newRunner(ctx, store)
if err != nil {
t.Fatal(err)
}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
}
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "unlanded", Workspace: item, Input: map[string]any{"prompt": "task"}})
if err != nil {
t.Fatal(err)
}
r.wg.Wait()
detail, err := store.GetRun(ctx, run.ID)
if err != nil {
t.Fatal(err)
}
if detail.Status != "succeeded" || len(detail.Steps) != 1 {
t.Fatalf("complete delivery did not settle successfully: %+v", detail)
}
if runGitOutput(t, main, "rev-parse", "HEAD") != runGitOutput(t, item, "rev-parse", "HEAD") {
t.Fatal("runner did not land the verified item commit")
}
}
func TestRunnerRejectsWrongBackendVerificationEvidence(t *testing.T) {
ctx := context.Background()
source := pipeline.Template([]string{"worker"}, true)
source = strings.Replace(source, "```configs", "```verification\nbackend=branch-api\nrevision=abc123\ncapability=controlled-state\nfixture=reconnect-foreground\n```\n\n```configs", 1)
r, store, id := submitRunner(t, ctx, source)
defer store.Close()
r.verifyTarget = func(context.Context, stage.VerificationTarget) (stage.VerificationEvidence, error) {
return stage.VerificationEvidence{Backend: "old-api", Revision: "abc123", Capability: "controlled-state", Fixture: "reconnect-foreground"}, nil
}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
}
run := startRun(t, r, id, "wrong-backend-evidence")
detail, err := store.GetRun(ctx, run.ID)
if err != nil {
t.Fatal(err)
}
if detail.Status != "failed" || len(detail.Steps) != 1 || !strings.Contains(detail.Steps[0].Diagnostic, "backend") {
t.Fatalf("wrong-backend evidence settled as success: %+v", detail)
}
}
func TestRunnerIgnoresModelSuppliedVerificationEvidence(t *testing.T) {
ctx := context.Background()
source := pipeline.Template([]string{"worker"}, true)
source = strings.Replace(source, "```configs", "```verification\nbackend=branch-api\nrevision=abc123\ncapability=controlled-state\nfixture=reconnect-foreground\n```\n\n```configs", 1)
r, store, id := submitRunner(t, ctx, source)
defer store.Close()
r.verifyTarget = func(context.Context, stage.VerificationTarget) (stage.VerificationEvidence, error) {
return stage.VerificationEvidence{Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", Fixture: "reconnect-foreground"}, nil
}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
return stage.Result{Outcome: "success", Output: stage.Values{"verification": map[string]any{"backend": "old-api", "revision": "stale", "capability": "invented", "fixture": "other"}}}, nil
}
run := startRun(t, r, id, "model-supplied-evidence")
detail, err := store.GetRun(ctx, run.ID)
if err != nil {
t.Fatal(err)
}
if detail.Status != "succeeded" || len(detail.Steps) != 1 {
t.Fatalf("controlled verification did not settle: %+v", detail)
}
}
func TestMainWorktreeRejectsNearMatchAndLinkedMain(t *testing.T) {
for _, test := range []struct {
name string
branch string
}{
{name: "mainline is not main", branch: "mainline"},
{name: "linked main is not canonical", branch: "main"},
} {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
primary := filepath.Join(root, "primary")
runGit(t, root, "init", "-b", "trunk", primary)
runGit(t, primary, "config", "user.email", "test@example.invalid")
runGit(t, primary, "config", "user.name", "test")
if err := os.WriteFile(filepath.Join(primary, "README"), []byte("base\n"), 0600); err != nil {
t.Fatal(err)
}
runGit(t, primary, "add", "README")
runGit(t, primary, "commit", "-m", "base")
item := filepath.Join(root, "item")
runGit(t, primary, "worktree", "add", "-b", "item", item)
candidate := filepath.Join(root, test.branch)
runGit(t, primary, "worktree", "add", "-b", test.branch, candidate)
commonDir, err := gitOutput(item, "rev-parse", "--git-common-dir")
if err != nil {
t.Fatal(err)
}
commonDir, err = canonicalDirectory(resolveGitPath(item, commonDir))
if err != nil {
t.Fatal(err)
}
if _, err := mainWorktree(item, commonDir); err == nil {
t.Fatalf("%s gained canonical main access", test.name)
}
})
}
}
func runGit(t *testing.T, directory string, args ...string) {
t.Helper()
cmd := exec.Command("git", append([]string{"-C", directory}, args...)...)
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v: %s", args, err, output)
}
}
func runGitOutput(t *testing.T, directory string, args ...string) string {
t.Helper()
output, err := exec.Command("git", append([]string{"-C", directory}, args...)...).Output()
if err != nil {
t.Fatalf("git %v: %v", args, err)
}
return strings.TrimSpace(string(output))
}
+59 -2
View File
@@ -58,8 +58,11 @@ type runner struct {
// directory. It defaults to stage.ExportSession; a failure is best-effort and
// never changes the stage's status, outcome, target or result validation.
exportSession func(context.Context, stage.SessionExportRequest) (stage.SessionExportResult, error)
wg sync.WaitGroup
schedulerWG sync.WaitGroup
// verifyTarget is installed only by server wiring for a controlled fixture
// workflow. Leaving it nil fails closed for target-aware definitions.
verifyTarget stage.VerificationRunner
wg sync.WaitGroup
schedulerWG sync.WaitGroup
// attentionMu guards attentionSent, the per-run set of run-attention events
// already delivered — by a stage's own notify policy or by the run-level
// default — so neither republishes the same genuine event.
@@ -1079,7 +1082,22 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
}
}
operations := &stage.OperationLog{}
var landing stage.LandingAccess
delivery, deliveryErr := stage.DeliveryEnabled(def)
if deliveryErr != nil {
finish("failed", "error", deliveryErr.Error())
return
}
if delivery {
access, accessErr := landingAccess(workspace)
if accessErr != nil {
finish("failed", "error", "delivery access denied: "+accessErr.Error())
return
}
landing = access
}
client := r.agentClient(id, sequence, state.artifactDirs, workspace, artifactsDir, operations, request.WritePaths, tools)
client.LandingAccess = landing
client.OnSession = func(session string, active bool) error {
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
@@ -1222,6 +1240,45 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
step.Output["salvage"] = result.Output
}
}
if err == nil && result.Outcome == "success" {
target, targetErr := stage.VerificationTargetFor(def)
if targetErr != nil {
err = targetErr
} else if target != nil {
if r.verifyTarget == nil {
err = fmt.Errorf("verification target %q requires a server-controlled fixture runner", target.Fixture)
} else {
evidence, verificationErr := r.verifyTarget(stageCtx, *target)
if verificationErr != nil {
err = verificationErr
} else {
err = stage.VerifyTargetEvidence(*target, evidence)
}
}
}
if err != nil {
step.Status = "failed"
step.Diagnostic = err.Error()
step.Outcome = "error"
step.Output = stage.Values{"error": err.Error(), "stage": key, "input": input}
}
}
if err == nil && delivery && result.Outcome == "success" {
checks, checksErr := stage.DeliveryChecks(def)
if checksErr != nil {
err = checksErr
} else {
gateCtx, gateCancel := context.WithTimeout(stageCtx, modelConfig.Timeout)
err = verifyDelivery(gateCtx, workspace, landing, checks)
gateCancel()
}
if err != nil {
step.Status = "failed"
step.Diagnostic = err.Error()
step.Outcome = "error"
step.Output = stage.Values{"error": err.Error(), "stage": key, "input": input}
}
}
// A failed or timed-out step carries the session pointer even when the
// bounded transcript would otherwise be only its stage_limits envelope.
if err != nil && stageSession != "" {
+135
View File
@@ -0,0 +1,135 @@
package server
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"git.a-wan.net/alan/pipelines/internal/stage"
)
const (
verificationFixtureID = "reconnect-foreground"
verificationFixtureResponseLimit int64 = 64 << 10
verificationFixtureTimeout = 10 * time.Second
)
// VerificationFixture is a server-owned, explicitly registered probe. Its
// endpoint and expected provenance come from server configuration, never a
// stage definition or result.
type VerificationFixture struct {
ID, Endpoint, Backend, Revision, Capability, State string
}
// VerificationFixturesFromEnvironment returns the single supported fixture
// registration when its endpoint is explicitly configured. There is no default
// endpoint: an unconfigured server fails target-aware verification closed.
func VerificationFixturesFromEnvironment(getenv func(string) string) []VerificationFixture {
if getenv == nil {
getenv = os.Getenv
}
endpoint := strings.TrimSpace(getenv("PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_URL"))
if endpoint == "" {
return nil
}
return []VerificationFixture{{
ID: verificationFixtureID,
Endpoint: endpoint,
Backend: strings.TrimSpace(getenv("PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_BACKEND")),
Revision: strings.TrimSpace(getenv("PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_REVISION")),
Capability: strings.TrimSpace(getenv("PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_CAPABILITY")),
State: strings.TrimSpace(getenv("PIPELINES_VERIFICATION_RECONNECT_FOREGROUND_STATE")),
}}
}
type verificationFixtureRegistry struct {
fixtures map[string]VerificationFixture
err error
}
func newVerificationFixtureRegistry(fixtures []VerificationFixture) *verificationFixtureRegistry {
r := &verificationFixtureRegistry{fixtures: make(map[string]VerificationFixture, len(fixtures))}
for _, fixture := range fixtures {
if err := validVerificationFixture(fixture); err != nil {
r.err = err
return r
}
if _, exists := r.fixtures[fixture.ID]; exists {
r.err = fmt.Errorf("duplicate verification fixture %q", fixture.ID)
return r
}
r.fixtures[fixture.ID] = fixture
}
return r
}
func validVerificationFixture(fixture VerificationFixture) error {
if fixture.ID != verificationFixtureID {
return fmt.Errorf("unsupported verification fixture %q", fixture.ID)
}
for _, value := range []string{fixture.Endpoint, fixture.Backend, fixture.Revision, fixture.Capability, fixture.State} {
if value == "" || len(value) > 256 {
return fmt.Errorf("verification fixture %q has incomplete configuration", fixture.ID)
}
}
u, err := url.Parse(fixture.Endpoint)
if err != nil || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") {
return fmt.Errorf("verification fixture %q has invalid endpoint", fixture.ID)
}
return nil
}
func (r *verificationFixtureRegistry) verify(ctx context.Context, target stage.VerificationTarget) (stage.VerificationEvidence, error) {
if r.err != nil {
return stage.VerificationEvidence{}, r.err
}
fixture, exists := r.fixtures[target.Fixture]
if !exists {
return stage.VerificationEvidence{}, fmt.Errorf("unknown verification fixture %q", target.Fixture)
}
if fixture.Backend != target.Backend || fixture.Revision != target.Revision || fixture.Capability != target.Capability {
return stage.VerificationEvidence{}, fmt.Errorf("verification target does not match registered fixture %q", target.Fixture)
}
probeCtx, cancel := context.WithTimeout(ctx, verificationFixtureTimeout)
defer cancel()
req, err := http.NewRequestWithContext(probeCtx, http.MethodGet, fixture.Endpoint, nil)
if err != nil {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q request: %w", fixture.ID, err)
}
client := &http.Client{Timeout: verificationFixtureTimeout, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
response, err := client.Do(req)
if err != nil {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q probe: %w", fixture.ID, err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q probe returned %s", fixture.ID, response.Status)
}
var observed struct {
Backend string `json:"backend"`
Revision string `json:"revision"`
Capability string `json:"capability"`
Fixture string `json:"fixture"`
State string `json:"state"`
}
body, err := io.ReadAll(io.LimitReader(response.Body, verificationFixtureResponseLimit+1))
if err != nil {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q response: %w", fixture.ID, err)
}
if int64(len(body)) > verificationFixtureResponseLimit {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q response exceeds %d bytes", fixture.ID, verificationFixtureResponseLimit)
}
if err := json.Unmarshal(body, &observed); err != nil {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q response: %w", fixture.ID, err)
}
if observed.Backend != fixture.Backend || observed.Revision != fixture.Revision || observed.Capability != fixture.Capability || observed.Fixture != fixture.ID || observed.State != fixture.State {
return stage.VerificationEvidence{}, fmt.Errorf("verification fixture %q returned incompatible backend, revision, capability, fixture, or state", fixture.ID)
}
return stage.VerificationEvidence{Backend: observed.Backend, Revision: observed.Revision, Capability: observed.Capability, Fixture: observed.Fixture}, nil
}
@@ -0,0 +1,148 @@
package server
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
func TestHandlerRunsRegisteredVerificationFixture(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
var mu sync.Mutex
response := map[string]string{"backend": "branch-api", "revision": "abc123", "capability": "controlled-state", "fixture": verificationFixtureID, "state": "ready"}
calls := 0
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
calls++
_ = json.NewEncoder(w).Encode(response)
}))
defer fixture.Close()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "fixture.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
source := pipeline.Template([]string{"worker"}, true)
source = strings.Replace(source, "```configs", "```verification\nbackend=branch-api\nrevision=abc123\ncapability=controlled-state\nfixture="+verificationFixtureID+"\n```\n\n```configs", 1)
if _, err := store.Submit(ctx, "controlled-fixture", source); err != nil {
t.Fatal(err)
}
items, err := store.List(ctx)
if err != nil {
t.Fatal(err)
}
handler := HandlerWithOptions(ctx, Options{VerificationFixtures: []VerificationFixture{{ID: verificationFixtureID, Endpoint: fixture.URL, Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", State: "ready"}}}, store).(*runHandler)
defer func() {
cancel()
WaitRuns(handler)
}()
r := handler.runner
if r.verifyTarget == nil {
t.Fatal("normal server construction did not wire controlled fixture verification")
}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
// These forged model labels must not influence fixture provenance.
return stage.Result{Outcome: "success", Output: stage.Values{"verification": map[string]any{"backend": "old-api", "revision": "stale", "capability": "forged", "fixture": "other"}}}, nil
}
start := func(key string) *protocol.RunDetail {
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: key, Workspace: t.TempDir(), Input: map[string]any{"prompt": "verify"}})
if err != nil {
t.Fatal(err)
}
r.wg.Wait()
detail, err := store.GetRun(ctx, run.ID)
if err != nil {
t.Fatal(err)
}
return detail
}
if detail := start("compatible"); detail.Status != "succeeded" {
t.Fatalf("compatible controlled fixture did not settle: %+v", detail)
}
mu.Lock()
response["revision"] = "old-build"
mu.Unlock()
if detail := start("stale"); detail.Status != "failed" || !strings.Contains(detail.Steps[0].Diagnostic, "incompatible") {
t.Fatalf("stale fixture provenance settled as success: %+v", detail)
}
mu.Lock()
if calls != 2 {
t.Fatalf("fixture probes = %d, want one per target-aware stage", calls)
}
mu.Unlock()
}
func TestVerificationFixtureCancellationClosesProbe(t *testing.T) {
started := make(chan struct{})
canceled := make(chan struct{})
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
close(started)
<-r.Context().Done()
close(canceled)
}))
defer fixture.Close()
registry := newVerificationFixtureRegistry([]VerificationFixture{{ID: verificationFixtureID, Endpoint: fixture.URL, Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", State: "ready"}})
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() {
_, err := registry.verify(ctx, stage.VerificationTarget{Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", Fixture: verificationFixtureID})
done <- err
}()
select {
case <-started:
case <-time.After(time.Second):
t.Fatal("fixture probe did not start")
}
cancel()
select {
case err := <-done:
if err == nil {
t.Fatal("canceled fixture probe succeeded")
}
case <-time.After(time.Second):
t.Fatal("canceled fixture probe did not return")
}
select {
case <-canceled:
case <-time.After(time.Second):
t.Fatal("canceled fixture request was not cleaned up")
}
}
func TestVerificationFixtureRejectsUnknownAndMismatchedProvenance(t *testing.T) {
base := map[string]string{"backend": "branch-api", "revision": "abc123", "capability": "controlled-state", "fixture": verificationFixtureID, "state": "ready"}
for field, wrong := range map[string]string{"backend": "old-api", "revision": "old-build", "capability": "other-state", "state": "stale"} {
t.Run(field, func(t *testing.T) {
response := make(map[string]string, len(base))
for key, value := range base {
response[key] = value
}
response[field] = wrong
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(response)
}))
defer fixture.Close()
registry := newVerificationFixtureRegistry([]VerificationFixture{{ID: verificationFixtureID, Endpoint: fixture.URL, Backend: base["backend"], Revision: base["revision"], Capability: base["capability"], State: base["state"]}})
_, err := registry.verify(context.Background(), stage.VerificationTarget{Backend: base["backend"], Revision: base["revision"], Capability: base["capability"], Fixture: verificationFixtureID})
if err == nil || !strings.Contains(err.Error(), "incompatible") {
t.Fatalf("mismatched %s provenance was accepted: %v", field, err)
}
})
}
registry := newVerificationFixtureRegistry(nil)
if _, err := registry.verify(context.Background(), stage.VerificationTarget{Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", Fixture: "unknown"}); err == nil || !strings.Contains(err.Error(), "unknown") {
t.Fatalf("unknown fixture was accepted: %v", err)
}
}
+8
View File
@@ -49,6 +49,10 @@ type Options struct {
// read time with no model call, which keeps tests and unconfigured servers
// deterministic.
StepVerdicts func(context.Context, map[string]any) string
// VerificationFixtures are explicit server-owned HTTP fixtures for optional
// target-aware verification. Definitions can name only a registered ID; they
// cannot select an endpoint or supply provenance.
VerificationFixtures []VerificationFixture
}
// Handler serves the persistent connection with default options. Canceling ctx
@@ -81,6 +85,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
if runErr == nil && options.StepVerdicts != nil {
runs.verdict = options.StepVerdicts
}
if runErr == nil {
fixtures := newVerificationFixtureRegistry(options.VerificationFixtures)
runs.verifyTarget = fixtures.verify
}
if runErr == nil {
runs.schedulerWG.Add(1)
go runs.scheduleLoop()
+1
View File
@@ -19,6 +19,7 @@ type AgentClient struct {
// ArtifactAccess is supplied only by the runner. Write is this visit's
// directory; Read contains this and completed earlier visits in the same run.
ArtifactAccess ArtifactAccess
LandingAccess LandingAccess
OnSession func(string, bool) error
SessionID string
Transcript string
+14 -2
View File
@@ -59,7 +59,7 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
}
for block := range def.Blocks {
switch block {
case "output", "prompt", "edges", "configs", "tools", "permissions":
case "output", "prompt", "edges", "configs", "tools", "permissions", "checks", "verification":
default:
return ModelSpec{}, fmt.Errorf("unexpected block %q for model stage", block)
}
@@ -70,6 +70,9 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
if _, err := StagePermissions(def); err != nil {
return ModelSpec{}, err
}
if _, err := VerificationTargetFor(def); err != nil {
return ModelSpec{}, err
}
for _, key := range []string{"provider", "model"} {
if def.Configs[key] == "" {
return ModelSpec{}, fmt.Errorf("missing config %s", key)
@@ -77,7 +80,7 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
}
for key := range def.Configs {
switch key {
case "provider", "model", "retries", "timeout", "budget_usd", "budget_tokens", "harness":
case "provider", "model", "retries", "timeout", "budget_usd", "budget_tokens", "harness", "delivery":
default:
return ModelSpec{}, fmt.Errorf("unknown config %q", key)
}
@@ -92,6 +95,15 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
if backend != "opencode" && backend != "pi" {
return ModelSpec{}, fmt.Errorf("harness must be opencode or pi")
}
if delivery := def.Configs["delivery"]; delivery != "" && delivery != "true" {
return ModelSpec{}, fmt.Errorf("delivery must be true when set")
}
if def.Configs["delivery"] == "true" && backend != "opencode" {
return ModelSpec{}, fmt.Errorf("delivery is supported only by the opencode harness")
}
if _, present := def.Blocks["checks"]; present && def.Configs["delivery"] != "true" {
return ModelSpec{}, fmt.Errorf("checks block is supported only by delivery stages")
}
if backend == "pi" {
tools, _, _ := ToolNames(def)
if _, err := piTools(tools); err != nil {
+75
View File
@@ -14,6 +14,77 @@ type ArtifactAccess struct {
Read []string
}
// LandingAccess is the server-derived external capability for a verified
// delivery operation. Empty or malformed values grant nothing.
type LandingAccess struct {
MainWorktree string
GitMetadata string
LockPath string
}
func (a LandingAccess) valid() bool {
for _, path := range []string{a.MainWorktree, a.GitMetadata, a.LockPath} {
if path == "" || !filepath.IsAbs(path) || filepath.Clean(path) != path || strings.ContainsAny(path, "*?\\") {
return false
}
}
// The server grants the canonical repository metadata directory as OpenCode
// V2's smallest external-directory unit. Keep the lock inside that directory
// so a caller cannot turn a valid metadata capability into access elsewhere.
return a.GitMetadata == filepath.Join(a.MainWorktree, ".git") &&
a.LockPath == filepath.Join(a.GitMetadata, "pipelines-landing.lock")
}
// DeliveryEnabled is the narrow, declarative capability check used before the
// runner supplies any landing paths. Only a validated model definition can ask
// for it; runtime paths are still derived by the server.
func DeliveryEnabled(def Definition) (bool, error) {
delivery := def.Configs["delivery"]
if delivery == "" {
return false, nil
}
if delivery != "true" {
return false, fmt.Errorf("delivery must be true when set")
}
if EffectiveType(def.Type) != ModelType {
return false, fmt.Errorf("delivery is supported only by model stages")
}
if _, err := (ModelStage{}).Spec(def); err != nil {
return false, fmt.Errorf("invalid delivery model stage: %w", err)
}
if _, err := DeliveryChecks(def); err != nil {
return false, err
}
return true, nil
}
// DeliveryChecks returns the bounded, definition-declared checks that the
// runner executes against a committed delivery candidate before canonical main
// is changed. They
// are intentionally a block rather than a generic runner setting: each
// workflow owns its repository-specific verification commands.
func DeliveryChecks(def Definition) ([]string, error) {
text := strings.TrimSpace(def.Blocks["checks"])
if text == "" {
return nil, fmt.Errorf("delivery stages require a nonempty checks block")
}
var checks []string
for _, line := range strings.Split(text, "\n") {
check := strings.TrimSpace(line)
if check == "" {
continue
}
if len(check) > 4096 {
return nil, fmt.Errorf("delivery check exceeds 4096 bytes")
}
checks = append(checks, check)
}
if len(checks) == 0 || len(checks) > 20 {
return nil, fmt.Errorf("delivery stages require 1 to 20 checks")
}
return checks, nil
}
// ToolNames validates model-stage permissions, not general graph semantics.
// A present empty block explicitly denies all tools.
func ToolNames(def Definition) ([]string, bool, error) {
@@ -155,6 +226,10 @@ func (c *AgentClient) permissionRules() []map[string]string {
allow("external_directory", filepath.ToSlash(filepath.Join(c.ArtifactAccess.Write, "*")))
allow("edit", filepath.ToSlash(filepath.Join(c.ArtifactAccess.Write, "*")))
}
if c.LandingAccess.valid() {
allow("external_directory", filepath.ToSlash(filepath.Join(c.LandingAccess.MainWorktree, "*")))
allow("external_directory", filepath.ToSlash(filepath.Join(c.LandingAccess.GitMetadata, "*")))
}
for _, pattern := range []string{"../*", "*/../*"} {
rules = append(rules, map[string]string{"action": "external_directory", "resource": pattern, "effect": "deny"})
}
+104
View File
@@ -174,3 +174,107 @@ func TestArtifactPermissionsAreExactAndToolScoped(t *testing.T) {
t.Fatal("missing artifact scope enabled an external directory")
}
}
func TestLandingPermissionsAreExactAndServerDerived(t *testing.T) {
access := LandingAccess{MainWorktree: "/repos/pipelines", GitMetadata: "/repos/pipelines/.git", LockPath: "/repos/pipelines/.git/pipelines-landing.lock"}
client := &AgentClient{Tools: []string{"read", "shell"}, LandingAccess: access}
rules := client.permissionRules()
for _, path := range []string{"/repos/pipelines/go.mod", "/repos/pipelines/.git/worktrees/item/HEAD", "/repos/pipelines/.git/pipelines-landing.lock"} {
if permissionEffect(rules, "external_directory", path) != "allow" {
t.Fatalf("verified landing path denied: %q", path)
}
}
for _, path := range []string{"/repos/other/go.mod", "/repos/pipelines/../other/go.mod", "/Users/alan/.git/config", "/.git/config"} {
if permissionEffect(rules, "external_directory", path) != "deny" {
t.Fatalf("unverified landing path allowed: %q", path)
}
}
client.LandingAccess.MainWorktree = "/repos/pipelines/../other"
if permissionEffect(client.permissionRules(), "external_directory", "/repos/other/go.mod") != "deny" {
t.Fatal("non-canonical landing capability allowed")
}
client.LandingAccess = LandingAccess{MainWorktree: "/repos/pipelines", GitMetadata: "/repos/pipelines/.git", LockPath: "/tmp/pipelines-landing.lock"}
if permissionEffect(client.permissionRules(), "external_directory", "/tmp/pipelines-landing.lock") != "deny" {
t.Fatal("landing lock outside verified metadata allowed")
}
client.LandingAccess = LandingAccess{MainWorktree: "/repos/pipelines", GitMetadata: "/repos/other/.git", LockPath: "/repos/other/.git/pipelines-landing.lock"}
if permissionEffect(client.permissionRules(), "external_directory", "/repos/other/.git/config") != "deny" {
t.Fatal("unrelated Git metadata allowed")
}
}
func TestDeliveryEnabledRequiresValidatedModelStage(t *testing.T) {
def := modelDefinition()
def.Configs["delivery"] = "true"
def.Blocks["checks"] = "go test ./..."
if enabled, err := DeliveryEnabled(def); err != nil || !enabled {
t.Fatalf("valid delivery stage = %v, %v", enabled, err)
}
for name, change := range map[string]func(*Definition){
"malformed value": func(d *Definition) { d.Configs["delivery"] = "yes" },
"non-model": func(d *Definition) { d.Type = HumanType },
"invalid model": func(d *Definition) { d.Configs["unexpected"] = "value" },
} {
t.Run(name, func(t *testing.T) {
copy := def
copy.Configs = map[string]string{}
for key, value := range def.Configs {
copy.Configs[key] = value
}
change(&copy)
if enabled, err := DeliveryEnabled(copy); err == nil || enabled {
t.Fatalf("invalid delivery stage = %v, %v", enabled, err)
}
})
}
}
func TestDeliveryRejectsPiBeforeAgentExecution(t *testing.T) {
def := modelDefinition()
def.Configs["delivery"] = "true"
def.Configs["harness"] = "pi"
def.Blocks["checks"] = "true"
if err := (ModelStage{}).Validate(def); err == nil || !strings.Contains(err.Error(), "opencode harness") {
t.Fatal("Pi delivery was accepted", err)
}
delete(def.Configs, "delivery")
delete(def.Blocks, "checks")
if err := (ModelStage{}).Validate(def); err != nil {
t.Fatal("Pi non-delivery was changed", err)
}
}
func TestDeliveryChecksRequireBoundedDeliveryOnlyDeclaration(t *testing.T) {
def := modelDefinition()
def.Configs["delivery"] = "true"
for name, mutate := range map[string]func(*Definition){
"missing": func(d *Definition) {},
"empty": func(d *Definition) { d.Blocks["checks"] = "\n" },
"non delivery": func(d *Definition) { d.Blocks["checks"] = "go test ./..." },
"too many checks": func(d *Definition) { d.Blocks["checks"] = strings.Repeat("true\n", 21) },
"oversized check": func(d *Definition) { d.Blocks["checks"] = strings.Repeat("x", 4097) },
} {
t.Run(name, func(t *testing.T) {
copy := def
copy.Blocks = map[string]string{}
copy.Configs = map[string]string{}
for key, value := range def.Blocks {
copy.Blocks[key] = value
}
for key, value := range def.Configs {
copy.Configs[key] = value
}
mutate(&copy)
if name == "non delivery" {
delete(copy.Configs, "delivery")
if _, err := (ModelStage{}).Spec(copy); err == nil {
t.Fatal("non-delivery checks block was accepted")
}
return
}
if _, err := DeliveryEnabled(copy); err == nil {
t.Fatal("invalid delivery checks were accepted")
}
})
}
}
+97
View File
@@ -0,0 +1,97 @@
package stage
import (
"context"
"fmt"
"strings"
)
// VerificationTarget declares the controlled fixture that can substantiate a
// target-specific behavior claim. It is intentionally data-only: the runner
// does not execute fixture input supplied by a stage result.
type VerificationTarget struct {
Backend string
Revision string
Capability string
Fixture string
}
// VerificationEvidence is returned by a server-controlled fixture workflow,
// never parsed from a model's result.
type VerificationEvidence struct {
Backend string
Revision string
Capability string
Fixture string
}
// VerificationRunner is the narrow server seam for a controlled fixture or
// workflow. The runner selects it during server setup; model output cannot
// select a backend, revision, fixture, or executable.
type VerificationRunner func(context.Context, VerificationTarget) (VerificationEvidence, error)
// VerificationTargetFor parses an optional, bounded verification block. A
// declared target requires all provenance fields so an old or other-backend
// fixture cannot be presented as evidence for the current target.
func VerificationTargetFor(def Definition) (*VerificationTarget, error) {
text, declared := def.Blocks["verification"]
if !declared {
return nil, nil
}
if len(text) > 4096 {
return nil, fmt.Errorf("verification block exceeds 4096 bytes")
}
values := map[string]string{}
for _, line := range strings.Split(text, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
key, value, ok := strings.Cut(line, "=")
key, value = strings.TrimSpace(key), strings.TrimSpace(value)
if !ok || value == "" || len(value) > 256 {
return nil, fmt.Errorf("invalid verification entry %q", line)
}
switch key {
case "backend", "revision", "capability", "fixture":
default:
return nil, fmt.Errorf("unknown verification entry %q", key)
}
if _, exists := values[key]; exists {
return nil, fmt.Errorf("duplicate verification entry %q", key)
}
values[key] = value
}
target := &VerificationTarget{Backend: values["backend"], Revision: values["revision"], Capability: values["capability"], Fixture: values["fixture"]}
if target.Backend == "" || target.Revision == "" || target.Capability == "" || target.Fixture == "" {
return nil, fmt.Errorf("verification requires backend, revision, capability, and fixture")
}
return target, nil
}
// VerifyTargetEvidence compares server-collected controlled-fixture provenance
// with the workflow declaration. It deliberately accepts no model result: a
// stage cannot substantiate its own target-specific behavior claim.
func VerifyTargetEvidence(target VerificationTarget, evidence VerificationEvidence) error {
for key, want := range map[string]string{
"backend": target.Backend, "revision": target.Revision, "capability": target.Capability, "fixture": target.Fixture,
} {
got := map[string]string{
"backend": evidence.Backend, "revision": evidence.Revision, "capability": evidence.Capability, "fixture": evidence.Fixture,
}[key]
if got != want {
return fmt.Errorf("verification evidence %s does not match declared target", key)
}
}
return nil
}
// VerifyTargetResult is retained for callers that only need declaration
// validation. Result-provided provenance is intentionally not evidence.
func VerifyTargetResult(def Definition, _ Values) error {
target, err := VerificationTargetFor(def)
if err != nil || target == nil {
return err
}
return fmt.Errorf("verification target %q requires a server-controlled fixture runner", target.Fixture)
}
+27
View File
@@ -0,0 +1,27 @@
package stage
import "testing"
func TestTargetVerificationRejectsWrongBackendEvidence(t *testing.T) {
def := modelDefinition()
def.Blocks["verification"] = "backend=branch-api\nrevision=abc123\ncapability=controlled-state\nfixture=reconnect-foreground"
if err := (ModelStage{}).Validate(def); err != nil {
t.Fatal(err)
}
target, err := VerificationTargetFor(def)
if err != nil {
t.Fatal(err)
}
good := VerificationEvidence{Backend: "branch-api", Revision: "abc123", Capability: "controlled-state", Fixture: "reconnect-foreground"}
if err := VerifyTargetEvidence(*target, good); err != nil {
t.Fatalf("matching fixture evidence rejected: %v", err)
}
wrongBackend := VerificationEvidence{Backend: "old-api", Revision: "abc123", Capability: "controlled-state", Fixture: "reconnect-foreground"}
if err := VerifyTargetEvidence(*target, wrongBackend); err == nil {
t.Fatal("wrong-backend fixture evidence accepted")
}
def.Blocks["verification"] = "backend=branch-api\nrevision=abc123"
if err := (ModelStage{}).Validate(def); err == nil {
t.Fatal("incomplete target verification declaration accepted")
}
}