{"record":{"id":"afdc83935d024c59","repo":"JuliusBrussee/caveman","slug":"invalid-run-state-contract","errorCode":null,"errorMessage":"invalid run-state contract","messagePattern":"invalid run-state contract","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/runstate/runstate.go","lineNumber":180,"sourceCode":"\t}\n\tif err := tmp.Close(); err != nil {\n\t\treturn err\n\t}\n\treturn os.Rename(tmpName, Path(home, state.Port))\n}\n\nfunc read(home string, port int) (State, error) {\n\traw, err := os.ReadFile(Path(home, port))\n\tif err != nil {\n\t\treturn State{}, err\n\t}\n\tvar state State\n\tif err := json.Unmarshal(raw, &state); err != nil {\n\t\treturn State{}, err\n\t}\n\tif state.Schema != Schema || state.Port != port || state.PID < 1 ||\n\t\tstate.InstanceToken == \"\" || (state.Owner != \"wrap\" && state.Owner != \"start\") {\n\t\treturn State{}, errors.New(\"invalid run-state contract\")\n\t}\n\tlistenPort, err := PortFromListen(state.Listen)\n\tif err != nil || listenPort != state.Port {\n\t\treturn State{}, errors.New(\"run-state listen address does not match its port\")\n\t}\n\treturn state, nil\n}\n\ntype validators struct {\n\talive      func(int) bool\n\texecutable func(int) (string, error)\n\tinstance   func(string, string) bool\n}\n\nfunc validate(state State, checks validators) bool {\n\tif !checks.alive(state.PID) {\n\t\treturn false\n\t}","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/internal/runstate/runstate.go#L162-L198","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Delete the stale/corrupt run-state file and restart the proxy so a fresh state is written","Verify the file was written by the same binary version (Schema constant) — rebuild/restart with a matching version","Check that the port passed to ReadValidated matches the port recorded in the state file","Confirm the launching code sets InstanceToken and Owner ('wrap' or 'start') before writing state"],"exampleFix":"// before (hand-edited state file)\n{\"schema\":\"v1\",\"port\":0,\"pid\":0,\"owner\":\"manual\"}\n// after: remove the file and let the proxy rewrite it\nrm $(state-file-path) && cave proxy start\n","handlingStrategy":"validation","validationCode":"var s map[string]any\nif err := json.Unmarshal(raw, &s); err != nil { return err }\nif s[\"schema\"] != runstate.Schema || s[\"owner\"] == nil || (s[\"owner\"] != \"wrap\" && s[\"owner\"] != \"start\") || s[\"instanceToken\"] == \"\" {\n    // regenerate the state file instead of reading it\n}\n","typeGuard":null,"tryCatchPattern":"state, err := runstate.ReadValidated(path, port)\nif err != nil {\n    // treat as absent state: remove file and relaunch\n    _ = runstate.RemoveMatching(...)\n}\n","preventionTips":["Never hand-edit run-state files","Delete stale state files after upgrading the binary","Let only the proxy write the state file","Validate Owner and InstanceToken are set at write time"],"tags":["runstate","schema-validation","ipc"],"backgroundTag":"schema-validation-failed","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}