|
| 1 | +package boatstack |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | +) |
| 8 | + |
| 9 | +// Attach, detach, and status operations for Detached Supervision. Attaching a |
| 10 | +// repository writes Boatstack's controller state to an external control root and a |
| 11 | +// binding that identifies the repository; it never writes into the target working |
| 12 | +// tree or its Git directory. Detaching removes the attachment (and, unless asked |
| 13 | +// to preserve it, the external state). Status reports the binding and verifies it. |
| 14 | + |
| 15 | +// AttachOptions requests a detached attachment. StateRoot, when set, overrides the |
| 16 | +// external control-state root for this process (the CLI wires --state-root to it). |
| 17 | +type AttachOptions struct { |
| 18 | + Repo string |
| 19 | + Force bool |
| 20 | +} |
| 21 | + |
| 22 | +// AttachResult is the deterministic outcome of an attach request. |
| 23 | +type AttachResult struct { |
| 24 | + SchemaVersion int `json:"schema_version"` |
| 25 | + VerificationStatus string `json:"verification_status"` // VERIFIED | BLOCKED |
| 26 | + Mode string `json:"mode,omitempty"` |
| 27 | + RepoID string `json:"repo_id,omitempty"` |
| 28 | + RepoRoot string `json:"repo_root,omitempty"` |
| 29 | + ControlRoot string `json:"control_root,omitempty"` |
| 30 | + WorktreeID string `json:"worktree_id,omitempty"` |
| 31 | + Reason string `json:"reason"` |
| 32 | +} |
| 33 | + |
| 34 | +func blockedAttach(reason string) AttachResult { |
| 35 | + return AttachResult{SchemaVersion: detachedSchemaVersion, VerificationStatus: "BLOCKED", Reason: reason} |
| 36 | +} |
| 37 | + |
| 38 | +// AttachDetached attaches repo in detached mode. It leaves the repository working |
| 39 | +// tree and Git directory byte-for-byte unchanged; all controller state is written |
| 40 | +// under the external control root. |
| 41 | +func AttachDetached(opts AttachOptions) (AttachResult, error) { |
| 42 | + root, err := ResolveRepository(opts.Repo) |
| 43 | + if err != nil { |
| 44 | + return blockedAttach(err.Error()), nil |
| 45 | + } |
| 46 | + stateRoot, err := detachedStateRoot() |
| 47 | + if err != nil { |
| 48 | + return blockedAttach(err.Error()), nil |
| 49 | + } |
| 50 | + identity, err := repoIdentity(root) |
| 51 | + if err != nil { |
| 52 | + return blockedAttach("Boatstack could not compute a repository identity: " + err.Error()), nil |
| 53 | + } |
| 54 | + |
| 55 | + registry, err := loadRegistry(stateRoot) |
| 56 | + if err != nil { |
| 57 | + return blockedAttach("Boatstack could not read the attachment registry: " + err.Error()), nil |
| 58 | + } |
| 59 | + if existing, ok := registry.Repositories[root]; ok && !opts.Force { |
| 60 | + return blockedAttach(fmt.Sprintf("This repository is already attached (repo_id %s). Detach first, or re-run with --force.", existing)), nil |
| 61 | + } |
| 62 | + |
| 63 | + ctx := detachedContextFromIdentity(stateRoot, identity) |
| 64 | + |
| 65 | + // Synthesize configuration from the repository (test command, default branch, |
| 66 | + // context) exactly as embedded init does. |
| 67 | + config := defaultConfig(root, detectTestCommand(root)) |
| 68 | + rawConfig, err := MarshalJSON(config) |
| 69 | + if err != nil { |
| 70 | + return blockedAttach(err.Error()), nil |
| 71 | + } |
| 72 | + |
| 73 | + // Generate the controller bundle and write it under the external control root. |
| 74 | + // The bundle layout mirrors embedded (.product-loop/** plus host adapter dirs), |
| 75 | + // only relocated outside the repository. |
| 76 | + bundle, err := BuildExportBundle(ctx.SourceConfigPath(), config, rawConfig, "boatstack") |
| 77 | + if err != nil { |
| 78 | + return blockedAttach("Boatstack could not build the controller bundle: " + err.Error()), nil |
| 79 | + } |
| 80 | + if err := os.MkdirAll(ctx.controlRoot, 0o755); err != nil { |
| 81 | + return blockedAttach(err.Error()), nil |
| 82 | + } |
| 83 | + if err := writeExport(ctx.controlRoot, bundle.Files, nil); err != nil { |
| 84 | + return blockedAttach("Boatstack could not write the controller bundle: " + err.Error()), nil |
| 85 | + } |
| 86 | + if err := os.WriteFile(ctx.SourceConfigPath(), rawConfig, 0o644); err != nil { |
| 87 | + return blockedAttach(err.Error()), nil |
| 88 | + } |
| 89 | + |
| 90 | + // Write the binding and index it in the registry. |
| 91 | + binding := DetachedBinding{ |
| 92 | + SchemaVersion: detachedSchemaVersion, |
| 93 | + Mode: string(SupervisionDetached), |
| 94 | + RepoID: identity.RepoID, |
| 95 | + CanonicalRepoPath: identity.CanonicalRepoPath, |
| 96 | + GitCommonIdentity: identity.GitCommonIdentity, |
| 97 | + InitialCommit: identity.InitialCommit, |
| 98 | + NormalizedOrigin: identity.NormalizedOrigin, |
| 99 | + CreatedByVersion: Version, |
| 100 | + CreatedAt: nowRFC3339(), |
| 101 | + } |
| 102 | + bindingRaw, err := MarshalJSON(binding) |
| 103 | + if err != nil { |
| 104 | + return blockedAttach(err.Error()), nil |
| 105 | + } |
| 106 | + if err := os.MkdirAll(filepath.Dir(bindingPath(stateRoot, identity.RepoID)), 0o755); err != nil { |
| 107 | + return blockedAttach(err.Error()), nil |
| 108 | + } |
| 109 | + if err := os.WriteFile(bindingPath(stateRoot, identity.RepoID), bindingRaw, 0o644); err != nil { |
| 110 | + return blockedAttach(err.Error()), nil |
| 111 | + } |
| 112 | + registry.Repositories[root] = identity.RepoID |
| 113 | + if err := saveRegistry(stateRoot, registry); err != nil { |
| 114 | + return blockedAttach(err.Error()), nil |
| 115 | + } |
| 116 | + invalidateWorkspaceCache() |
| 117 | + |
| 118 | + return AttachResult{ |
| 119 | + SchemaVersion: detachedSchemaVersion, |
| 120 | + VerificationStatus: "VERIFIED", |
| 121 | + Mode: string(SupervisionDetached), |
| 122 | + RepoID: identity.RepoID, |
| 123 | + RepoRoot: root, |
| 124 | + ControlRoot: ctx.controlRoot, |
| 125 | + WorktreeID: identity.WorktreeID, |
| 126 | + Reason: "Attached Boatstack in detached mode. The repository was not modified; all controller state lives under the external control root.", |
| 127 | + }, nil |
| 128 | +} |
| 129 | + |
| 130 | +// DetachOptions requests removal of a detached attachment. |
| 131 | +type DetachOptions struct { |
| 132 | + Repo string |
| 133 | + PreserveState bool |
| 134 | +} |
| 135 | + |
| 136 | +// DetachResult is the deterministic outcome of a detach request. |
| 137 | +type DetachResult struct { |
| 138 | + SchemaVersion int `json:"schema_version"` |
| 139 | + VerificationStatus string `json:"verification_status"` // VERIFIED | BLOCKED |
| 140 | + RepoID string `json:"repo_id,omitempty"` |
| 141 | + StateRemoved bool `json:"state_removed"` |
| 142 | + Reason string `json:"reason"` |
| 143 | +} |
| 144 | + |
| 145 | +// DetachDetached removes a repository's detached attachment. It always removes the |
| 146 | +// registry entry; it removes the external controller state only when PreserveState |
| 147 | +// is false. It never touches the repository itself. |
| 148 | +func DetachDetached(opts DetachOptions) (DetachResult, error) { |
| 149 | + root, err := ResolveRepository(opts.Repo) |
| 150 | + if err != nil { |
| 151 | + return DetachResult{SchemaVersion: detachedSchemaVersion, VerificationStatus: "BLOCKED", Reason: err.Error()}, nil |
| 152 | + } |
| 153 | + stateRoot, err := detachedStateRoot() |
| 154 | + if err != nil { |
| 155 | + return DetachResult{SchemaVersion: detachedSchemaVersion, VerificationStatus: "BLOCKED", Reason: err.Error()}, nil |
| 156 | + } |
| 157 | + registry, err := loadRegistry(stateRoot) |
| 158 | + if err != nil { |
| 159 | + return DetachResult{SchemaVersion: detachedSchemaVersion, VerificationStatus: "BLOCKED", Reason: err.Error()}, nil |
| 160 | + } |
| 161 | + repoID, ok := registry.Repositories[root] |
| 162 | + if !ok { |
| 163 | + return DetachResult{ |
| 164 | + SchemaVersion: detachedSchemaVersion, VerificationStatus: "VERIFIED", |
| 165 | + Reason: "This repository is not attached in detached mode; nothing to detach.", |
| 166 | + }, nil |
| 167 | + } |
| 168 | + delete(registry.Repositories, root) |
| 169 | + if err := saveRegistry(stateRoot, registry); err != nil { |
| 170 | + return DetachResult{SchemaVersion: detachedSchemaVersion, VerificationStatus: "BLOCKED", RepoID: repoID, Reason: err.Error()}, nil |
| 171 | + } |
| 172 | + stateRemoved := false |
| 173 | + if !opts.PreserveState { |
| 174 | + if err := os.RemoveAll(repositoryControlRoot(stateRoot, repoID)); err != nil { |
| 175 | + return DetachResult{SchemaVersion: detachedSchemaVersion, VerificationStatus: "BLOCKED", RepoID: repoID, Reason: err.Error()}, nil |
| 176 | + } |
| 177 | + stateRemoved = true |
| 178 | + } |
| 179 | + invalidateWorkspaceCache() |
| 180 | + reason := "Detached Boatstack. The external controller state was removed." |
| 181 | + if opts.PreserveState { |
| 182 | + reason = "Detached Boatstack. The external controller state was preserved." |
| 183 | + } |
| 184 | + return DetachResult{ |
| 185 | + SchemaVersion: detachedSchemaVersion, VerificationStatus: "VERIFIED", |
| 186 | + RepoID: repoID, StateRemoved: stateRemoved, Reason: reason, |
| 187 | + }, nil |
| 188 | +} |
| 189 | + |
| 190 | +// DetachedStatusResult reports whether a repository is attached in detached mode |
| 191 | +// and whether its binding verifies. |
| 192 | +type DetachedStatusResult struct { |
| 193 | + SchemaVersion int `json:"schema_version"` |
| 194 | + Attached bool `json:"attached"` |
| 195 | + Verified bool `json:"verified"` |
| 196 | + Mode string `json:"mode"` |
| 197 | + RepoID string `json:"repo_id,omitempty"` |
| 198 | + RepoRoot string `json:"repo_root,omitempty"` |
| 199 | + ControlRoot string `json:"control_root,omitempty"` |
| 200 | + WorktreeID string `json:"worktree_id,omitempty"` |
| 201 | + Reason string `json:"reason"` |
| 202 | +} |
| 203 | + |
| 204 | +// DetachedStatus reports the detached attachment state for a repository. It is |
| 205 | +// read-only. |
| 206 | +func DetachedStatus(repoPath string) (DetachedStatusResult, error) { |
| 207 | + root, err := ResolveRepository(repoPath) |
| 208 | + if err != nil { |
| 209 | + return DetachedStatusResult{SchemaVersion: detachedSchemaVersion, Reason: err.Error()}, nil |
| 210 | + } |
| 211 | + ctx, ok, verifyErr := detachedContextFor(root) |
| 212 | + if !ok { |
| 213 | + return DetachedStatusResult{ |
| 214 | + SchemaVersion: detachedSchemaVersion, Attached: false, Mode: string(SupervisionEmbedded), |
| 215 | + RepoRoot: root, Reason: "This repository is not attached in detached mode.", |
| 216 | + }, nil |
| 217 | + } |
| 218 | + if verifyErr != nil { |
| 219 | + return DetachedStatusResult{ |
| 220 | + SchemaVersion: detachedSchemaVersion, Attached: true, Verified: false, Mode: string(SupervisionDetached), |
| 221 | + RepoRoot: root, Reason: verifyErr.Error(), |
| 222 | + }, nil |
| 223 | + } |
| 224 | + return DetachedStatusResult{ |
| 225 | + SchemaVersion: detachedSchemaVersion, Attached: true, Verified: true, Mode: string(SupervisionDetached), |
| 226 | + RepoID: ctx.RepoID, RepoRoot: ctx.RepoRoot, ControlRoot: ctx.controlRoot, WorktreeID: ctx.WorktreeID, |
| 227 | + Reason: "This repository is attached in detached mode and its binding verifies.", |
| 228 | + }, nil |
| 229 | +} |
0 commit comments