Skip to main content
coreydaley.dev

Hierarchical tags from a flat list of strings

Update, November 20, 2026: hierarchical tags shipped in Secret Store 1.2, on Mac, iPhone and iPad.

Secret Store’s tags were a flat list. That’s fine for a dozen tags, but once you have aws-prod, aws-dev, aws-staging and github-work, what you want is to pick “everything AWS” in one tap. So tags can now nest with a slash: aws/prod, aws/dev. The part I like is that nothing about how tags are stored changed.

Nesting is optional. A vault with only flat tags, or none, works as before: up to 20 tags per secret, All Secrets and Untagged groups in the sidebar, and picking several tags narrows the list to secrets that have all of them.

Nothing new is stored

The most useful decision I made was not to store the hierarchy at all. A secret’s tags are still [String], and aws/prod is just a string with a slash in it. The tree is built from the set of tags each time the sidebar needs it.

That means:

  • Most existing flat tags are already valid. api-key is a path with one segment.
  • The keychain items and the backup format don’t change.
  • A parent like aws doesn’t have to exist as a tag. If only aws/prod and aws/dev are stored, aws appears in the tree as a group.
  • A flat tag that already had a slash in it, like ci/cd, simply becomes a group with one child, which is probably what it meant anyway.

A slug policy for tag names

The old normalizer trimmed whitespace and merged case-insensitive duplicates, keeping the first casing it saw. Once a slash means something, tags need stricter rules, so they now follow a slug policy:

private func sanitizeTag(_ raw: String) -> String? {
    let s = raw.lowercased().replacingOccurrences(of: " ", with: "-")
    let kept = s.filter { c in
        c.isASCII && (c.isLetter || c.isNumber || c == "-" || c == "_" || c == "/")
    }
    let segments = kept.split(separator: "/", omittingEmptySubsequences: true).map(String.init)
    guard !segments.isEmpty else { return nil }
    return segments.joined(separator: "/")
}

Lowercase everything, turn spaces into hyphens, drop anything outside a-z 0-9 - _ /, then tidy the slashes: no leading or trailing slash, no //, no empty segments. The 50-character limit applies to the whole path. AWS Prod becomes aws-prod. /aws//prod/ becomes aws/prod. If nothing is left, there’s no tag.

The allowlist is ASCII, so accented letters are dropped rather than kept. That keeps tag names predictable, but it is a real limitation.

The policy runs in more than one place. The tag field cleans what you type when you add the chip, so you see aws-prod straight away. Renaming or adding a tag from the sidebar goes through it too. And the stores run it again whenever a secret is created or updated, so the store stays the authority and the UI checks are a convenience.

That last point is also how older tags catch up. There’s no migration pass. A tag with capitals displays in lowercase right away. A tag with characters the policy drops, like a dot or an @, is rewritten the next time its secret is saved. It’s a lazy migration, and it only ever touches a secret you’re already editing.

The tree, in the core package

TagTree is a small pure type in SecretStoreCore. It takes the tags and their counts and returns sorted TagNodes:

public struct TagNode: Sendable {
    public let segment: String     // "prod"
    public let fullPath: String    // "aws/prod"
    public let children: [TagNode]
    public let exactCount: Int     // secrets tagged exactly this path
    public let isLeafTag: Bool     // a real stored tag, not just a prefix
}

The builder splits each path on / and inserts it one segment at a time. A node gets isLeafTag = true only if that exact path is a stored tag. Intermediate nodes that exist only because something is nested under them have isLeafTag = false and a count of zero. Children are sorted alphabetically at every level.

The tricky case is a node that is both. If aws and aws/prod are both stored tags, the aws node is a tag you can select and a group you can expand. It has children and isLeafTag = true, which shows the name is a little off: read it as “this path is a stored tag.”

Because the tree is pure and lives in the core package, the view-model exposes it as a computed property, and the Mac, iPhone and iPad sidebars all use the same tree.

Filtering by a parent

Selecting aws should show secrets tagged aws/prod and aws/dev, and aws itself if it’s a stored tag. The filter does that with a prefix match:

result = result.filter { meta in
    let metaKeys = (meta.tags ?? []).map { $0.lowercased() }
    return activeTagFilter.allSatisfy { key in
        let prefix = key + "/"
        return metaKeys.contains { $0 == key || $0.hasPrefix(prefix) }
    }
}

Two details matter. The prefix includes the slash, so aws matches aws/prod but not aws-legacy. And within one key it’s an OR (exact match or any descendant), while across keys it’s still an AND, as it was with flat tags. Pick aws and work and you get the AWS secrets that are also tagged work.

Tapping a parent adds one key to the filter, not one per descendant. The search field shows a single aws token instead of a row of aws/prod, aws/dev and aws/staging tokens, and tapping again removes it.

Counts on parent nodes

Each row in the sidebar shows a count. A leaf shows how many secrets carry that exact tag. A group shows the sum over its subtree:

var aggregateCount: Int {
    exactCount + children.reduce(0) { $0 + $1.aggregateCount }
}

That sum can overcount. A secret tagged both aws/prod and aws/dev is counted twice under aws. It follows from the design: the tree is built from tags and their counts, not from secrets, so it has nothing to deduplicate against. The comment on aggregateCount says so, and calls it acceptable for a personal vault.

Renaming and deleting a group

The context menu on a leaf has Rename Tag and Delete Tag, which work on that exact path. On a group it has Rename Group and Delete Group, which work on the path and everything under it.

renameSubtree(from:to:) rewrites every tag that equals the old path or starts with it plus a slash, keeping the rest of the path. With prefix set to oldKey + "/", the core of it is:

if lower == oldKey { return newSlug }
if lower.hasPrefix(prefix) {
    let suffix = String(lower.dropFirst(prefix.count))
    return "\(newSlug)/\(suffix)"
}

Renaming aws to cloud/aws moves the whole branch under a new parent. A rename is also how you nest tags you already have: rename aws-prod to aws/prod. Each secret’s rewritten tags go back through the normalizer, so if a rename makes two tags identical, say aws/prod renamed into an existing gcp/prod, the secret ends up with one copy, not two.

deleteSubtree(_:) removes the path and its descendants from every secret that has them. It deletes tags, not secrets.

Both check the same over-cap edit lock as every other edit, from the free-tier grace rule, and the menu items are disabled when it applies.

A recursive sidebar

The sidebar row is a recursive SwiftUI view. A node with no children is a plain button that toggles its filter. A node with children is a DisclosureGroup whose label is a button that filters the subtree and whose content is more TagNodeRows:

DisclosureGroup(isExpanded: $isExpanded) {
    ForEach(node.children, id: \.fullPath) { child in
        TagNodeRow(node: child, vm: vm, /* rename/delete callbacks */)
    }
} label: {
    parentLabelButton
}

Each row keeps its own isExpanded state. Everything starts expanded, and collapsing a group doesn’t change the filter. A group that is only a prefix gets a folder icon; one that is also a real tag keeps the tag icon. There’s no depth limit.

Group rows have the header trait, so VoiceOver users can move between them with the headings rotor, and DisclosureGroup announces expanded and collapsed by itself.

What’s not tested yet

The core logic has unit tests: ten for the tree builder (flat tags, siblings, deep paths like a/b/c, the bare-parent case, full paths and counts), plus tests for subtree filtering, the single-token rule, aggregate counts, and cascading rename and delete. The UI tests for the tree on macOS and iOS, and a proper VoiceOver walkthrough, are still to do.

What to take away

  • Before adding structure to stored data, check whether you can derive it instead. A slash in a string gave a hierarchy with no schema change.
  • Once a character has meaning, sanitize input everywhere it enters, and again at the store. Decide what happens to old values that break the new rules.
  • Match on key + "/", not on key, or aws will match aws-legacy.
  • Say where an approximation is good enough, in the code, so the next person doesn’t “fix” it by accident or trust it too far.

Where else have you gotten structure for free by putting meaning into a string instead of a schema?

Related posts