JuliusBrussee/caveman · error

invalid run-state contract

Error message

invalid run-state contract

What it means

runstate.read parses the on-disk run-state JSON file and enforces a strict contract: schema must match the current Schema constant, the port must equal the expected port, PID must be positive, an instance token must be present, and Owner must be either 'wrap' or 'start'. Any deviation means the state file was written by an incompatible version, is corrupted, or belongs to a different invocation, so read refuses to trust it.

Solutions

  1. Delete the stale/corrupt run-state file and restart the proxy so a fresh state is written
  2. Verify the file was written by the same binary version (Schema constant) — rebuild/restart with a matching version
  3. Check that the port passed to ReadValidated matches the port recorded in the state file
  4. Confirm the launching code sets InstanceToken and Owner ('wrap' or 'start') before writing state

Example fix

// before (hand-edited state file)
{"schema":"v1","port":0,"pid":0,"owner":"manual"}
// after: remove the file and let the proxy rewrite it
rm $(state-file-path) && cave proxy start
Defensive patterns

Strategy: validation

Validate before calling

var s map[string]any
if err := json.Unmarshal(raw, &s); err != nil { return err }
if s["schema"] != runstate.Schema || s["owner"] == nil || (s["owner"] != "wrap" && s["owner"] != "start") || s["instanceToken"] == "" {
    // regenerate the state file instead of reading it
}

Try / catch

state, err := runstate.ReadValidated(path, port)
if err != nil {
    // treat as absent state: remove file and relaunch
    _ = runstate.RemoveMatching(...)
}

Prevention

When it happens

Trigger: Reading a run-state file whose schema version differs from Schema, whose stored port differs from the expected port parameter, whose PID is <= 0, whose InstanceToken field is empty, or whose Owner is anything other than 'wrap' or 'start' — via ReadValidated or RemoveMatching.

Common situations: Hand-edited or truncated state files after a crash, stale state files left by an upgraded binary with a new Schema, a partially written file surviving an interrupted launch, or state from a different proxy instance being picked up.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/afdc83935d024c59. Report an issue: GitHub.

Appendix: source

Thrown at proxy/internal/runstate/runstate.go:180

	}
	if err := tmp.Close(); err != nil {
		return err
	}
	return os.Rename(tmpName, Path(home, state.Port))
}

func read(home string, port int) (State, error) {
	raw, err := os.ReadFile(Path(home, port))
	if err != nil {
		return State{}, err
	}
	var state State
	if err := json.Unmarshal(raw, &state); err != nil {
		return State{}, err
	}
	if state.Schema != Schema || state.Port != port || state.PID < 1 ||
		state.InstanceToken == "" || (state.Owner != "wrap" && state.Owner != "start") {
		return State{}, errors.New("invalid run-state contract")
	}
	listenPort, err := PortFromListen(state.Listen)
	if err != nil || listenPort != state.Port {
		return State{}, errors.New("run-state listen address does not match its port")
	}
	return state, nil
}

type validators struct {
	alive      func(int) bool
	executable func(int) (string, error)
	instance   func(string, string) bool
}

func validate(state State, checks validators) bool {
	if !checks.alive(state.PID) {
		return false
	}

View on GitHub (pinned to 3ee70a1026)