| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "reasonix/internal/session" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // DeleteSession is the legacy archive RPC. Canonical content and legacy |
| 10 | // originals remain in place; the lifecycle owner rejects active runtimes. |
| 11 | func (a *App) DeleteSession(path string) error { |
| 12 | _, err := a.archiveSessionPathWithOperation(path, "archive-"+strings.TrimPrefix(newTabID(), "tab_")) |
| 13 | return friendlySessionFileError(err) |
| 14 | } |
| 15 | |
| 16 | func (a *App) archiveSessionPathWithOperation(path, operationID string) (SessionTarget, error) { |
| 17 | target, err := a.resolveSessionMutationTarget(sessionTargetSelector{SessionPath: path}) |
| 18 | if err != nil { |
| 19 | return SessionTarget{}, err |
| 20 | } |
| 21 | if target.SessionRef.SessionID != "" { |
| 22 | return a.archiveCanonicalSessionWithOperation(target.SessionRef, operationID) |
| 23 | } |
| 24 | a.cancelAISessionTitle(target.key()) |
| 25 | _, valid, err := a.sessionDirForPath(path) |
| 26 | if err != nil { |
| 27 | return SessionTarget{}, err |
| 28 | } |
| 29 | ref, adopted, err := a.legacyCanonicalRef(a.bootContext(), valid) |
| 30 | if err != nil { |
| 31 | return SessionTarget{}, err |
| 32 | } |
| 33 | if adopted { |
| 34 | return a.archiveCanonicalSessionWithOperation(ref, operationID) |
| 35 | } |
| 36 | release, ok := a.tryLockRuntimeMutation("archive historical session") |
| 37 | if !ok { |
| 38 | return SessionTarget{}, errTopicArchiveBusy |
| 39 | } |
| 40 | ref, dependency, err := a.stageArchiveSource(a.bootContext(), valid) |
| 41 | if err == nil { |
| 42 | dependencies := []string{} |
| 43 | if dependency != "" { |
| 44 | dependencies = append(dependencies, dependency) |
| 45 | } |
| 46 | err = a.archiveSessionRefsWithOperation([]session.SessionRef{ref}, operationID, dependencies...) |
| 47 | } |
| 48 | release() |
| 49 | if err == nil { |
| 50 | a.emitProjectTreeChanged() |
| 51 | archived, resolveErr := a.resolveCanonicalSessionTargetState(ref, "", true) |
| 52 | if resolveErr != nil { |
| 53 | archived = SessionTarget{SessionRef: ref} |
| 54 | } |
| 55 | a.emitSessionTargetChange("session_archived", SessionTargetChangeEvent{ |
| 56 | TargetKey: target.key(), OperationID: operationID, |
| 57 | LifecycleGeneration: archived.LifecycleGeneration, WorkspaceID: archived.WorkspaceID, |
| 58 | }) |
| 59 | return archived, nil |
| 60 | } |
| 61 | return SessionTarget{}, err |
| 62 | } |
| 63 | |
| 64 | // PurgeTrashedSession resolves a legacy trash identity and removes only its |
| 65 | // canonical application session, retaining upgrade originals. |
| 66 | func (a *App) PurgeTrashedSession(path string) error { |
| 67 | if _, err := a.trashedSessionDir(path); err != nil { |
| 68 | return err |
| 69 | } |
| 70 | if !explicitlyDeletedLegacyEntry(path) { |
| 71 | return errors.New("historical recovery entries cannot be permanently cleared") |
| 72 | } |
| 73 | // Legacy public RPCs retain upgrade originals too. Resolve/import the |
| 74 | // archived application identity before delegating to canonical purge. |
| 75 | if err := a.discoverHistoricalTrash(a.bootContext()); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | state, err := a.workspaceRegistry().Load(a.bootContext()) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | if mapping, adopted := state.SourceMappings[desktopSourceKey(path, "")]; adopted { |
| 83 | return a.PurgeCanonicalSession(session.SessionRef{HostID: localDesktopHostID, SessionID: mapping.SessionID}) |
| 84 | } |
| 85 | return errors.New("historical session has no verified canonical identity") |
| 86 | } |
| 87 |