| 1 | package workspacestate |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // SettleImportedWorkspaceConflict retires only the workspace-conflict report |
| 10 | // for the exact legacy source version that was successfully published. Metadata |
| 11 | // repairs can change workspace ownership without changing durable source bytes; |
| 12 | // other heads, source versions and recovery reasons still require their own proof. |
| 13 | func (s *Store) SettleImportedWorkspaceConflict(ctx context.Context, sourceKey, fingerprint, sessionID string) error { |
| 14 | return s.mutate(ctx, func(state *State) error { |
| 15 | mapping, exists, err := state.ResolveSource(sourceKey) |
| 16 | if err != nil { |
| 17 | return err |
| 18 | } |
| 19 | if !exists || fingerprint == "" || sessionID == "" || mapping.Fingerprint != fingerprint || mapping.SessionID != sessionID { |
| 20 | return ErrMutationConflict |
| 21 | } |
| 22 | if mapping.Format != "legacy" || state.SessionStates[sessionID].Lifecycle == Deleted { |
| 23 | return nil |
| 24 | } |
| 25 | if owner, exists := sessionOwner(*state, sessionID); !exists || owner != mapping.WorkspaceID { |
| 26 | return ErrMutationConflict |
| 27 | } |
| 28 | keys := state.SourceKeys(mapping.SourceKey) |
| 29 | for id, entry := range state.RecoveryEntries { |
| 30 | if entry.Status != "pending" || entry.Reason != "workspace_conflict" || entry.Format != mapping.Format || |
| 31 | entry.HeadID != mapping.HeadID || entry.Fingerprint != fingerprint { |
| 32 | continue |
| 33 | } |
| 34 | key := entry.SourceKey |
| 35 | if !slices.Contains(keys, key) { |
| 36 | // Reviewed imports retain a separate mapping, but failures are |
| 37 | // recorded against the base source. Resolve only this exact |
| 38 | // reviewed version, never the base's older adoption. |
| 39 | _, version, reviewed := strings.Cut(mapping.SourceKey, ":review:") |
| 40 | if !reviewed || version != fingerprint { |
| 41 | continue |
| 42 | } |
| 43 | key += ":review:" + fingerprint |
| 44 | if !slices.Contains(keys, key) { |
| 45 | continue |
| 46 | } |
| 47 | } |
| 48 | owner, exists, err := state.ResolveSource(key) |
| 49 | if err != nil || !exists || owner.SessionID != sessionID || owner.WorkspaceID != mapping.WorkspaceID || owner.Fingerprint != fingerprint { |
| 50 | continue |
| 51 | } |
| 52 | entry.Status, entry.SessionID = "restored", sessionID |
| 53 | state.RecoveryEntries[id] = entry |
| 54 | } |
| 55 | return nil |
| 56 | }) |
| 57 | } |
| 58 |