| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "strings" |
| 7 | "unicode" |
| 8 | ) |
| 9 | |
| 10 | // LegacySessionTopicID preserves the Desktop topic identity used before |
| 11 | // metadata-only discovery. Computing it never writes a branch sidecar. |
| 12 | func LegacySessionTopicID(path string) string { |
| 13 | id := strings.TrimSpace(BranchID(path)) |
| 14 | if id == "" { |
| 15 | return "" |
| 16 | } |
| 17 | sum := sha256.Sum256([]byte(id)) |
| 18 | var b strings.Builder |
| 19 | b.WriteString("legacy_") |
| 20 | for _, r := range id { |
| 21 | switch { |
| 22 | case unicode.IsLetter(r), unicode.IsDigit(r), r == '-', r == '_': |
| 23 | b.WriteRune(r) |
| 24 | default: |
| 25 | b.WriteByte('_') |
| 26 | } |
| 27 | } |
| 28 | prefix := strings.TrimRight(b.String(), "_") |
| 29 | if prefix == "legacy" { |
| 30 | prefix = "legacy_session" |
| 31 | } |
| 32 | return prefix + "_" + hex.EncodeToString(sum[:])[:12] |
| 33 | } |
| 34 |