Skip to content

CodexBar 是一个 macOS 菜单栏应用,用于查看本机 Codex 账号状态、额度、Token 用量、详细统计信息

34.0 KB
Raw Blame
import Foundation
import Testing

struct ActivityTaskTests {
    @Test(arguments: [ActivityDataEventKind.turnCompleted, .turnAborted])
    func realTaskSnapshotsPreserveThreadIDAcrossTurnsAndTerminalRecords(terminal: ActivityDataEventKind) throws {
        let now = Date(timeIntervalSince1970: 1800000000)
        var graph = ActivityTaskGraph()
        let start = TestFixtures.event(at: now)
        _ = graph.consume([start], source: .live)
        let first = try #require(graph.tasks[ActivityTaskKey(thread: "thread-a", turn: "turn-a")])
        let threadID = first.snapshot.threadID
        #expect(threadID == "thread-a")
        _ = graph.consume([TestFixtures.event(terminal, at: now.addingTimeInterval(1))], source: .live)
        if terminal == .turnCompleted {
            #expect(graph.completions.first?.threadID == threadID)
        } else {
            #expect(graph.terminations.first?.threadID == threadID)
        }
        _ = graph.consume([TestFixtures.event(at: now.addingTimeInterval(2), turn: "turn-b")], source: .live)
        let next = try #require(graph.tasks[ActivityTaskKey(thread: "thread-a", turn: "turn-b")])
        #expect(next.snapshot.threadID == threadID)
        #expect(next.displayID != first.displayID)
        _ = graph.consume([TestFixtures.event(at: now.addingTimeInterval(3), thread: "another-thread", turn: "turn-b")], source: .live)
        let other = try #require(graph.tasks[ActivityTaskKey(thread: "another-thread", turn: "turn-b")])
        #expect(other.snapshot.threadID == "another-thread")
        #expect(other.snapshot.threadID != threadID)
    }

    @Test(arguments: [ActivityDataEventKind.turnCompleted, .turnAborted])
    func terminalSnapshotPreservesTaskIdentityAcrossTurns(terminal: ActivityDataEventKind) throws {
        let directory = try TestDirectory()
        defer { try? directory.remove() }
        let preferences = try TestPreferences()
        defer { preferences.remove() }
        let monitor = ActivityMonitor(
            protectionSettings: ProtectionSettings(defaults: preferences.defaults),
            protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url
        )
        defer { monitor.stop() }
        monitor.isActivitySourceHealthy = true
        let now = Date()
        monitor.consume(.live([TestFixtures.event(at: now)]))
        let firstID = try #require(monitor.graph.tasks.values.first?.snapshot.id)
        monitor.consume(.live([TestFixtures.event(terminal, at: now.addingTimeInterval(1))]))
        if terminal == .turnCompleted {
            let completion = try #require(monitor.graph.completions.first)
            #expect(completion.taskID == firstID)
            #expect(completion.id != firstID)
        } else {
            let termination = try #require(monitor.graph.terminations.first)
            #expect(termination.taskID == firstID)
            #expect(termination.id != firstID)
        }
        monitor.consume(.live([TestFixtures.event(at: now.addingTimeInterval(2), turn: "turn-b")]))
        let nextID = try #require(monitor.graph.tasks.values.first?.snapshot.id)
        #expect(nextID != firstID)
    }

    @Test(arguments: ["thread", "turn", "both", "empty-thread", "empty-turn"], [false, true])
    func incompleteIdentityCannotCreateOrChangeTasks(missing: String, snapshot: Bool) throws {
        let directory = try TestDirectory()
        defer { try? directory.remove() }
        let preferences = try TestPreferences()
        defer { preferences.remove() }
        let monitor = ActivityMonitor(
            protectionSettings: ProtectionSettings(defaults: preferences.defaults),
            protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url
        )
        defer { monitor.stop() }
        monitor.isActivitySourceHealthy = true
        let now = Date()
        let thread: String? = switch missing {
        case "thread", "both": nil
        case "empty-thread": ""
        default: "thread-a"
        }
        let turn: String? = switch missing {
        case "turn", "both": nil
        case "empty-turn": ""
        default: "turn-a"
        }
        let events = ActivityDataEventKind.allCases.map {
            TestFixtures.event($0, at: now.addingTimeInterval(1), thread: thread, turn: turn)
        }
        monitor.consume(snapshot ? .snapshotEvents(events) : .live(events))
        #expect(monitor.graph.tasks.isEmpty)
        #expect(monitor.graph.completions.isEmpty)
        #expect(monitor.graph.terminations.isEmpty)
        monitor.consume(.live([TestFixtures.event(at: now)]))
        let original = try #require(monitor.graph.tasks.values.first?.snapshot)
        monitor.consume(snapshot ? .snapshotEvents(events) : .live(events))
        #expect(monitor.graph.tasks.count == 1)
        #expect(monitor.graph.tasks.values.first?.snapshot == original)
        #expect(monitor.graph.completions.isEmpty)
        #expect(monitor.graph.terminations.isEmpty)
        #expect(monitor.graph.pendingTerminalTasks.isEmpty)
        #expect(monitor.graph.terminalTokenUsageRequests.isEmpty)
    }

    @Test func lateTerminalOnlyCompletesItsOwnTurnOnce() throws {
        let directory = try TestDirectory()
        defer { try? directory.remove() }
        let preferences = try TestPreferences()
        defer { preferences.remove() }
        let monitor = ActivityMonitor(
            protectionSettings: ProtectionSettings(defaults: preferences.defaults),
            protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url
        )
        defer { monitor.stop() }
        monitor.isActivitySourceHealthy = true
        let now = Date()
        monitor.consume(.live([TestFixtures.event(at: now)]))
        monitor.consume(.live([TestFixtures.event(at: now.addingTimeInterval(1), turn: "turn-b")]))
        let terminal = TestFixtures.event(.turnCompleted, at: now.addingTimeInterval(2))
        monitor.consume(.live([terminal, terminal]))
        #expect(Set(monitor.graph.tasks.keys) == [.init(thread: "thread-a", turn: "turn-b")])
        #expect(monitor.graph.pendingTerminalTasks.isEmpty)
        #expect(monitor.graph.completions.count == 1)
        #expect(monitor.graph.terminations.isEmpty)
    }

    @Test func dailySnapshotDoesNotEnterConnectionRecovery() throws {
        let directory = try TestDirectory()
        defer { try? directory.remove() }
        let preferences = try TestPreferences()
        defer { preferences.remove() }
        let monitor = ActivityMonitor(
            protectionSettings: ProtectionSettings(defaults: preferences.defaults),
            protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url
        )
        defer { monitor.stop() }
        monitor.isActivitySourceHealthy = true
        let presentation = monitor.sourcePresentation
        monitor.consume(.snapshotEvents([TestFixtures.event(at: Date())]))
        #expect(monitor.graph.tasks.count == 1)
        #expect(monitor.sourcePresentation == presentation)
        #expect(!monitor.isBootstrapping)
    }

    @Test func guardianIsFilteredAtEntryAndAutoReviewedUserTaskRemainsVisible() throws {
        let directory = try TestDirectory()
        defer { try? directory.remove() }
        let preferences = try TestPreferences()
        defer { preferences.remove() }
        let monitor = ActivityMonitor(
            protectionSettings: ProtectionSettings(defaults: preferences.defaults),
            protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url
        )
        defer { monitor.stop() }
        monitor.isActivitySourceHealthy = true
        let now = Date()
        for kind in [ActivityDataEventKind.turnStarted, .toolStarted, .approvalRequested, .turnCompleted, .turnAborted] {
            monitor.consume(.live([TestFixtures.event(kind, at: now, origin: .autoReview)]))
        }
        #expect(monitor.graph.tasks.isEmpty)
        #expect(monitor.graph.pendingTerminalTasks.isEmpty)
        #expect(monitor.graph.completions.isEmpty)
        #expect(monitor.graph.terminations.isEmpty)
        #expect(monitor.graph.recentlyEndedTaskAt.isEmpty)
        monitor.consume(.live([TestFixtures.event(at: now)]))
        #expect(monitor.graph.tasks.count == 1)
    }

    @Test func subagentLinkRetentionDoesNotDependOnIdentityOrRefreshTime() {
        var graph = ActivityTaskGraph()
        let root = ActivityTaskKey(thread: "root", turn: "root-turn")
        let child = ActivityTurnReference(threadID: "child", turnID: "child-turn")
        let expired = Date().addingTimeInterval(-ActivityRetention.window - 1)
        graph.subagentTurnLinks[child] = (root, expired)
        var state = TurnLifecycleState(
            requestedThreadID: "child", turnID: "child-turn", startedAt: nil,
            effort: nil, lastProgressAt: nil, terminal: nil
        )
        state.rootThreadID = "root"
        state.rootTurnID = "root-turn"
        state.parentThreadID = "root"
        graph.applySubagentLifecycle(state)
        #expect(graph.subagentTurnLinks[child]?.retainedAt == expired)
        _ = graph.subagentLifecycleReferences()
        #expect(graph.subagentTurnLinks[child] == nil)
    }

    @Test func incompleteSubagentRootCannotBeGuessedFromQueuedEventsOrPreviousLink() throws {
        let directory = try TestDirectory()
        defer { try? directory.remove() }
        let preferences = try TestPreferences()
        defer { preferences.remove() }
        let monitor = ActivityMonitor(
            protectionSettings: ProtectionSettings(defaults: preferences.defaults),
            protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url
        )
        defer { monitor.stop() }
        let now = Date()
        let event = TestFixtures.event(.toolStarted, at: now, agentThreadID: "child", origin: .auxiliary)
        let observedResult1 = monitor.graph.deferUnassociatedSubagentEvent(event, source: .live)
        #expect(observedResult1)
        var state = TurnLifecycleState(
            requestedThreadID: "child", turnID: "turn-a", startedAt: now,
            effort: nil, lastProgressAt: nil, terminal: nil,
            rootTurnID: "turn-a", parentThreadID: "thread-a"
        )
        let reference = ActivityTurnReference(threadID: "child", turnID: "turn-a")
        monitor.applySubagentLifecycle(state)
        #expect(monitor.graph.subagentTurnLinks[reference] == nil)
        monitor.graph.subagentTurnLinks[reference] = (.init(thread: "thread-a", turn: "turn-a"), now)
        monitor.applySubagentLifecycle(state)
        #expect(monitor.graph.subagentTurnLinks[reference]?.root == .init(thread: "thread-a", turn: "turn-a"))
        state.rootThreadID = "different-thread"
        monitor.applySubagentLifecycle(state)
        #expect(monitor.graph.subagentTurnLinks[reference]?.root == .init(thread: "thread-a", turn: "turn-a"))
    }

    @Test(arguments: [-172800.0, 0.5, 2.0])
    func lifecycleBackfillsMissingStartWithTimestampTolerance(offset: TimeInterval) {
        var task = makeTask()
        task.startedAt = nil
        let start = task.lastActivityAt.addingTimeInterval(offset)
        let state = healthyLifecycle(startedAt: start)
        ActivityTaskGraph.mergeLifecycleBackfill(from: state, into: &task)
        #expect(task.startedAt == (offset <= 1 ? start : nil))
        if task.startedAt != nil {
            let earlier = healthyLifecycle(startedAt: start.addingTimeInterval(-60))
            ActivityTaskGraph.mergeLifecycleBackfill(from: earlier, into: &task)
            #expect(task.startedAt == start)
        }
    }

    @Test func onlyPendingApprovalExposesNotificationAction() {
        var task = makeTask(TestFixtures.event(.toolStarted))
        #expect(task.snapshot.approvalActionText == nil)
        let event = ActivityDataRecord(
            timestamp: TestFixtures.now.addingTimeInterval(1), name: ActivityDataEventKind.toolStarted.rawValue,
            origin: .main, cwd: nil, toolName: nil, model: nil, effort: nil,
            threadID: "thread-a", turnID: "turn-a", agentThreadID: nil
        )
        var nameless = event
        nameless.context = ActivityDataContext(method: "item/started", threadID: "thread-a", itemType: "fileChange")
        task.mergeMetadata(from: nameless)
        #expect(task.snapshot.approvalActionText == nil)

        var approval = ActivityDataRecord(
            timestamp: TestFixtures.now.addingTimeInterval(2), name: ActivityDataEventKind.approvalRequested.rawValue,
            origin: .main, cwd: nil, toolName: nil, model: nil, effort: nil,
            threadID: "thread-a", turnID: "turn-a", agentThreadID: nil
        )
        approval.context = ActivityDataContext(method: "item/commandExecution/requestApproval", threadID: "thread-a", itemType: "commandExecution")
        task.mergeMetadata(from: approval)
        let enteredWaiting = task.recordApprovalRequest(from: approval)
        #expect(enteredWaiting)
        #expect(task.snapshot.approvalActionText == String(localized: "activity.action.command"))
        task.resumeExecution(from: TestFixtures.event(.toolStarted, at: TestFixtures.now.addingTimeInterval(3)))
        #expect(task.snapshot.approvalActionText == String(localized: "activity.action.command"))
        var lifecycle = healthyLifecycle(startedAt: TestFixtures.now)
        lifecycle.isWaitingApproval = false
        lifecycle.approvalChangedAt = TestFixtures.now.addingTimeInterval(4)
        task.mergeExecutionLifecycle(lifecycle, owner: ActivityExecutionKey(agentThreadID: nil, turnID: "turn-a"))
        #expect(task.snapshot.approvalActionText == nil)
    }

    @Test(arguments: [nil, [], ["read"]] as [[String]?])
    func approvalActionsDoNotFallBackToAnotherCommandsActions(_ actions: [String]?) {
        var task = makeTask()
        var earlier = TestFixtures.event(.toolStarted)
        earlier.context = ActivityDataContext(method: "item/started", threadID: "thread-a", itemType: "commandExecution")
        task.mergeMetadata(from: earlier)
        var approval = ActivityDataRecord(
            timestamp: TestFixtures.now.addingTimeInterval(1), name: ActivityDataEventKind.approvalRequested.rawValue,
            origin: .main, cwd: nil, toolName: nil, commandActionTypes: actions, model: nil, effort: nil,
            threadID: "thread-a", turnID: "turn-a", agentThreadID: nil
        )
        approval.context = ActivityDataContext(method: "item/commandExecution/requestApproval", threadID: "thread-a", itemType: "commandExecution")
        let enteredWaiting = task.recordApprovalRequest(from: approval)
        #expect(enteredWaiting)
        #expect(task.displayedApproval?.commandActionTypes == actions)
        let expected = actions?.isEmpty == false
            ? String(localized: "activity.live.actions-read")
            : String(localized: "activity.action.command")
        #expect(task.snapshot.approvalActionText == expected)
    }

    @Test func unknownSourceCannotReuseAnotherEventsOrigin() throws {
        let directory = try TestDirectory()
        defer { try? directory.remove() }
        let preferences = try TestPreferences()
        defer { preferences.remove() }
        let monitor = ActivityMonitor(
            protectionSettings: ProtectionSettings(defaults: preferences.defaults),
            protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url
        )
        defer { monitor.stop() }
        monitor.isActivitySourceHealthy = true
        monitor.consume(.live([TestFixtures.event(at: Date())]))
        monitor.consume(.live([TestFixtures.event(.turnCompleted, at: Date(), origin: .unknown)]))
        #expect(monitor.graph.tasks.count == 1)
        #expect(monitor.graph.completions.isEmpty)
    }

    @Test func taskIdentityRequiresThreadAndTurnAndSeparatesTheirBoundaries() {
        #expect(ActivityTaskKey(event: TestFixtures.event())?.threadID == "thread-a")
        #expect(ActivityTaskKey(event: TestFixtures.event(turn: nil)) == nil)
        #expect(ActivityTaskKey(event: TestFixtures.event(thread: nil)) == nil)
        let first = ActivityTaskKey(thread: "ab", turn: "c").protectionIdentifier
        let second = ActivityTaskKey(thread: "a", turn: "bc").protectionIdentifier
        #expect(first != second)
        #expect(first.count == 64)
    }

    @Test func preciseDurationRequiresKnownStartAndNonnegativeInterval() {
        let task = makeTask()
        #expect(task.preciseDuration(until: TestFixtures.now.addingTimeInterval(42)) == 42)
        #expect(task.preciseDuration(until: TestFixtures.now.addingTimeInterval(-1)) == nil)
        var restored = task
        restored.startedAt = nil
        #expect(restored.preciseDuration(until: TestFixtures.now) == nil)
    }

    @Test func mixedEffortIsStickyAndEmptyMetadataIsIgnored() {
        var task = makeTask()
        task.mergeEffort(" high ")
        #expect(task.effort == "high")
        task.mergeEffort(" \n")
        #expect(task.effort == "high")
        task.mergeEffort("low")
        #expect(task.effort == "mixed")
        task.mergeEffort("high")
        #expect(task.effort == "mixed")
    }

    @Test func executionProgressDoesNotMoveEventOrderingBarrier() {
        var task = makeTask()
        task.recordProgress(at: TestFixtures.now.addingTimeInterval(30))
        task.recordProgress(at: TestFixtures.now.addingTimeInterval(10))
        task.recordProgress(at: TestFixtures.now.addingTimeInterval(30))
        #expect(task.acceptsExecutionEvent(TestFixtures.event(.toolCompleted, at: TestFixtures.now.addingTimeInterval(10))))
        #expect(task.lastProgressAt == TestFixtures.now.addingTimeInterval(30))
        #expect(task.progressGeneration == 2)
    }

    @Test func protectionDeadlineUsesLatestProgressAndCurrentThreshold() {
        var task = makeTask()
        let observed = TestFixtures.now
        let checked = observed.addingTimeInterval(3600)
        task.recordLifecycleRead(healthyLifecycle(), at: checked)
        task.recordProgress(at: observed.addingTimeInterval(1800))
        #expect(task.protectionDeadline(at: checked, inactivityDuration: 3600) == observed.addingTimeInterval(5400))
        #expect(task.protectionDeadline(at: checked, inactivityDuration: 1800) == checked)
        #expect(task.hasFreshLifecycle(at: checked))
        #expect(task.protectionDeadline(at: checked.addingTimeInterval(5), inactivityDuration: 3600) == nil)
    }

    @Test(arguments: [TurnReadStatus.unavailable, .notFound])
    func losingLifecycleCoverageRevokesProtectionEligibility(status: TurnReadStatus) {
        var task = makeTask()
        let now = TestFixtures.now.addingTimeInterval(3600)
        task.recordLifecycleRead(healthyLifecycle(), at: now)
        #expect(task.protectionDeadline(at: now, inactivityDuration: 3600) == now)
        var lost = healthyLifecycle()
        lost.readStatus = status
        task.recordLifecycleRead(lost, at: now)
        #expect(task.protectionDeadline(at: now, inactivityDuration: 3600) == nil)
    }

    @Test func onlyCompleteAuthoritativeStateCanResumeApproval() {
        var task = makeTask()
        _ = task.recordApprovalRequest(from: TestFixtures.event(.approvalRequested))
        let now = TestFixtures.now.addingTimeInterval(3600)
        var state = healthyLifecycle()
        state.readStatus = .unavailable
        state.lastProgressAt = now
        state.isWaitingApproval = false
        state.approvalChangedAt = now
        let owner = ActivityExecutionKey(agentThreadID: nil, turnID: "turn-a")
        task.recordLifecycleRead(state, at: now)
        task.mergeExecutionLifecycle(state, owner: owner)
        #expect(task.state == .waitingApproval)
        #expect(!task.hasFreshLifecycle(at: now))

        state.readStatus = .complete
        task.recordLifecycleRead(state, at: now)
        task.mergeExecutionLifecycle(state, owner: owner)
        #expect(task.hasFreshLifecycle(at: now))
        #expect(task.state == .running)
    }

    private func healthyLifecycle(startedAt: Date? = nil) -> TurnLifecycleState {
        TurnLifecycleState(
            requestedThreadID: "thread-a", turnID: "turn-a", startedAt: startedAt, effort: nil,
            lastProgressAt: nil, terminal: nil, readStatus: .complete
        )
    }

    @Test(arguments: [-10.0, 0.0, 30.0])
    func delayedChildToolEventOnlyInvalidatesProtectionForLaterProgress(offset: TimeInterval) throws {
        let directory = try TestDirectory()
        defer { try? directory.remove() }
        let preferences = try TestPreferences()
        defer { preferences.remove() }
        let monitor = try makeProtectionMonitor(in: directory, preferences: preferences)
        defer { monitor.stop() }
        let now = Date()
        let progress = now.addingTimeInterval(-7200)
        let root = ActivityTurnReference(threadID: "thread-a", turnID: "turn-a")
        let child = ActivityTurnReference(threadID: "child", turnID: "child-turn")
        let rootState = TurnLifecycleState(
            requestedThreadID: root.threadID, turnID: root.turnID, startedAt: progress.addingTimeInterval(-100),
            lastProgressAt: progress, terminal: nil, rootTurnID: root.turnID, rootThreadID: root.threadID
        )
        var childState = TurnLifecycleState(
            requestedThreadID: child.threadID, turnID: child.turnID, startedAt: progress.addingTimeInterval(-90),
            lastProgressAt: progress.addingTimeInterval(-60), terminal: nil,
            rootTurnID: root.turnID, rootThreadID: root.threadID, parentThreadID: root.threadID
        )
        var graph = ActivityTaskGraph()
        _ = graph.update(
            events: [TestFixtures.event(at: progress.addingTimeInterval(-100))], source: .bootstrap,
            lifecycle: TurnLifecycleSnapshot(states: [root: rootState, child: childState], verifiedTurns: [root: now, child: now])
        )
        graph.backfilledTasks.removeAll()
        monitor.graph = graph
        let task = try #require(graph.tasks.values.first)
        let candidate = ProtectionCandidate(
            key: task.key, taskID: task.displayID, projectName: task.projectName,
            lastProgressAt: task.lastProgressAt, progressGeneration: task.progressGeneration, inactivityDuration: .oneHour
        )
        #expect(monitor.isProtectionCandidateRelevant(candidate, now: now))
        let attemptID = UUID()
        monitor.protectionAttempts[task.key] = ProtectionAttempt(
            id: attemptID, candidate: candidate, markedAt: now, timeoutTask: Task {}
        )
        let eventDate = progress.addingTimeInterval(offset)
        childState.lastProgressAt = eventDate
        let transitions = graph.update(
            events: [TestFixtures.event(.toolCompleted, at: eventDate, turn: child.turnID, agentThreadID: child.threadID, origin: .auxiliary)], source: .live,
            lifecycle: TurnLifecycleSnapshot(states: [root: rootState, child: childState], verifiedTurns: [root: now, child: now])
        )
        #expect(transitions.isEmpty)
        #expect(graph.tasks[task.key]?.lastProgressAt == max(progress, eventDate))
        #expect(graph.tasks[task.key]?.progressGeneration == task.progressGeneration + (offset > 0 ? 1 : 0))
        #expect(graph.tasks[task.key]?.executions[.init(agentThreadID: child.threadID, turnID: child.turnID)]?.lastEventAt == eventDate)
        monitor.acceptFixtureGraph(graph, phase: .live, transitions: transitions)
        #expect(monitor.isProtectionCandidateRelevant(candidate, now: now) == (offset <= 0))
        monitor.finishProtectionAttempt(task.key, attemptID: attemptID, taskID: task.displayID, notificationWasSubmitted: false)
        #expect(monitor.suppressedTasks.contains(task.key) == (offset <= 0))
        #expect(monitor.protectionAttempts.isEmpty)
    }

    @Test(arguments: ["unchanged", "growth", "unavailable", "stale"])
    func suppressionRevalidatesCoverageAfterNotification(scenario: String) throws {
        let directory = try TestDirectory()
        defer { try? directory.remove() }
        let preferences = try TestPreferences()
        defer { preferences.remove() }
        let monitor = try makeProtectionMonitor(in: directory, preferences: preferences)
        defer { monitor.stop() }
        let now = Date()
        var task = makeTask(TestFixtures.event(at: now.addingTimeInterval(-7200)))
        task.recordLifecycleRead(healthyLifecycle(), at: now)
        monitor.graph.tasks[task.key] = task
        let candidate = ProtectionCandidate(
            key: task.key, taskID: task.displayID, projectName: task.projectName,
            lastProgressAt: task.lastProgressAt, progressGeneration: task.progressGeneration, inactivityDuration: .oneHour
        )
        #expect(monitor.isProtectionCandidateRelevant(candidate, now: now))

        switch scenario {
        case "growth": task.recordProgress(at: now)
        case "unavailable":
            var state = healthyLifecycle()
            state.readStatus = .unavailable
            task.recordLifecycleRead(state, at: now)
        case "stale": task.lifecycleCoverageCheckedAt = now.addingTimeInterval(-5)
        default: break
        }
        monitor.graph.tasks[task.key] = task
        let attemptID = UUID()
        monitor.protectionAttempts[task.key] = ProtectionAttempt(
            id: attemptID, candidate: candidate, markedAt: now, timeoutTask: Task {}
        )
        monitor.finishProtectionAttempt(task.key, attemptID: attemptID, taskID: task.displayID, notificationWasSubmitted: false)
        #expect(monitor.graph.tasks[task.key]?.state == .running)
        #expect(monitor.suppressedTasks.contains(task.key) == (scenario == "unchanged"))
        #expect(monitor.protectionAttempts.isEmpty)
    }

    @Test(arguments: [false, true], [ProtectionSettings.InactivityDuration.thirtyMinutes, .oneHour])
    func longerThresholdRestoresSuppressionAndUsesNewDeadline(
        expiredObservation: Bool, previousDuration: ProtectionSettings.InactivityDuration
    ) async throws {
        let directory = try TestDirectory()
        defer { try? directory.remove() }
        let preferences = try TestPreferences()
        defer { preferences.remove() }
        let monitor = try makeProtectionMonitor(in: directory, preferences: preferences)
        defer { monitor.stop() }
        let changedAt = Date()
        let hiddenAfter: TimeInterval = previousDuration == .thirtyMinutes ? 3000 : 3600
        let newDuration: ProtectionSettings.InactivityDuration = previousDuration == .thirtyMinutes ? .oneHour : .twoHours
        let progressStoppedAt = changedAt.addingTimeInterval(-hiddenAfter - 300)
        let hiddenAt = progressStoppedAt.addingTimeInterval(hiddenAfter)
        let newDeadline = progressStoppedAt.addingTimeInterval(newDuration.timeInterval)
        var task = makeTask(TestFixtures.event(at: progressStoppedAt))
        let key = task.key
        let identifier = key.protectionIdentifier
        task.recordLifecycleRead(healthyLifecycle(), at: hiddenAt)
        monitor.graph.tasks[key] = task
        monitor.protectionSettings.setInactivityDuration(previousDuration)
        monitor.reconcileProtection(now: hiddenAt, sendsNotification: false)
        #expect(monitor.graph.tasks[key]?.state == .running)
        #expect(monitor.suppressedTasks.contains(key))
        #expect(monitor.protectionRecords[identifier] != nil)

        let checkedAt = expiredObservation ? changedAt.addingTimeInterval(-5) : changedAt
        monitor.graph.tasks[key]?.recordLifecycleRead(healthyLifecycle(), at: checkedAt)
        monitor.protectionSettings.setInactivityDuration(newDuration)
        monitor.handleProtectionTimingChange()
        #expect(monitor.graph.tasks[key]?.state == .running)
        #expect(monitor.protectionRecords[identifier] == nil)
        #expect(!monitor.suppressedTasks.contains(key))
        #expect(monitor.graph.tasks[key]?.lastProgressAt == task.lastProgressAt)
        #expect(monitor.graph.tasks[key]?.hasFreshLifecycle(at: changedAt) == !expiredObservation)

        monitor.reconcileProtection(now: newDeadline, sendsNotification: false)
        #expect(monitor.graph.tasks[key]?.state == .running)
        monitor.graph.tasks[key]?.recordLifecycleRead(healthyLifecycle(), at: newDeadline.addingTimeInterval(-1))
        monitor.reconcileProtection(now: newDeadline.addingTimeInterval(-1), sendsNotification: false)
        #expect(monitor.graph.tasks[key]?.state == .running)
        monitor.reconcileProtection(now: newDeadline, sendsNotification: false)
        #expect(monitor.graph.tasks[key]?.state == .running)
        #expect(monitor.suppressedTasks.contains(key))
        #expect(monitor.protectionRecords[identifier] != nil)
        monitor.cancelInactivityCheck()
        await monitor.protectionPersistenceTask?.value
    }

    private func makeProtectionMonitor(in directory: TestDirectory, preferences: TestPreferences) throws -> ActivityMonitor {
        let monitor = ActivityMonitor(
            protectionSettings: ProtectionSettings(defaults: preferences.defaults),
            protectionStore: ProtectionStore(directoryURL: directory.url)
        )
        monitor.protectionLoadState = .available
        monitor.isStarted = true
        monitor.isProtectionEnabled = true
        monitor.isActivitySourceHealthy = true
        monitor.activityReader = AppServerActivityReader(probe: ActivityReaderProbe(), socketURL: directory.url.appendingPathComponent("test.sock")) { _ in }
        return monitor
    }

    @Test func subagentCountDeduplicatesAndRejectsOlderEvents() {
        var task = makeTask()
        #expect(task.snapshot.activeSubagentCount == nil)
        task.hasCompleteSubagentCoverage = true
        #expect(task.snapshot.activeSubagentCount == 0)
        task.recordSubagentActivity(agentThreadID: "agent", isStarting: true, at: TestFixtures.now)
        task.recordSubagentActivity(agentThreadID: "agent", isStarting: true, at: TestFixtures.now)
        #expect(task.snapshot.activeSubagentCount == 1)
        task.recordSubagentActivity(agentThreadID: "agent", isStarting: false, at: TestFixtures.now.addingTimeInterval(2))
        task.recordSubagentActivity(agentThreadID: "agent", isStarting: true, at: TestFixtures.now.addingTimeInterval(1))
        #expect(task.snapshot.activeSubagentCount == 0)
    }

    @Test func missingSubagentIdentityAndUnmatchedStopMakeCountUnavailable() {
        var missing = makeTask()
        missing.hasCompleteSubagentCoverage = true
        missing.recordSubagentActivity(agentThreadID: nil, isStarting: true, at: TestFixtures.now)
        #expect(missing.snapshot.activeSubagentCount == nil)
        var unmatched = makeTask()
        unmatched.hasCompleteSubagentCoverage = true
        unmatched.recordSubagentActivity(agentThreadID: "unknown", isStarting: false, at: TestFixtures.now)
        #expect(unmatched.snapshot.activeSubagentCount == nil)
    }

    @Test func approvalRequestImmediatelyWaitsWithoutReviewerContext() {
        var task = makeTask(TestFixtures.event())
        let request = TestFixtures.event(.approvalRequested)
        let enteredWaiting = task.recordApprovalRequest(from: request)
        #expect(enteredWaiting)
        #expect(task.state == .waitingApproval)
        let repeated = task.recordApprovalRequest(from: request)
        #expect(!repeated)
        #expect(task.state == .waitingApproval)
    }

    @Test func mainProgressCannotDismissSubagentApproval() {
        var task = makeTask()
        let approval = TestFixtures.event(.approvalRequested, agentThreadID: "agent", origin: .auxiliary)
        let enteredWaiting = task.recordApprovalRequest(from: approval)
        #expect(enteredWaiting)
        task.resumeExecution(from: TestFixtures.event(.toolCompleted, at: TestFixtures.now.addingTimeInterval(1)))
        #expect(task.state == .waitingApproval)
        task.resumeExecution(from: TestFixtures.event(.toolCompleted, at: TestFixtures.now.addingTimeInterval(2), agentThreadID: "agent", origin: .auxiliary))
        #expect(task.state == .waitingApproval)
        task.finishExecution(ActivityExecutionKey(agentThreadID: "agent", turnID: "turn-a"), at: TestFixtures.now.addingTimeInterval(3))
        #expect(task.state == .running)
    }

    @Test func resolvingOneApprovalKeepsOtherExecutionWaiting() {
        var task = makeTask()
        let mainEnteredWaiting = task.recordApprovalRequest(from: TestFixtures.event(.approvalRequested))
        #expect(mainEnteredWaiting)
        let agentRequest = TestFixtures.event(.approvalRequested, at: TestFixtures.now.addingTimeInterval(1), agentThreadID: "agent", origin: .auxiliary)
        let agentEnteredWaiting = task.recordApprovalRequest(from: agentRequest)
        #expect(!agentEnteredWaiting)
        task.finishExecution(ActivityExecutionKey(agentThreadID: nil, turnID: "turn-a"), at: TestFixtures.now.addingTimeInterval(2))
        #expect(task.state == .waitingApproval)
        #expect(task.displayedApproval?.requestedAt == TestFixtures.now.addingTimeInterval(1))
        task.finishExecution(ActivityExecutionKey(agentThreadID: "agent", turnID: "turn-a"), at: TestFixtures.now.addingTimeInterval(3))
        #expect(task.state == .running)
    }

    @Test func anotherToolsProgressDoesNotInvalidateAnApprovalRequest() {
        var task = makeTask()
        let progress = TestFixtures.now.addingTimeInterval(2)
        task.resumeExecution(from: TestFixtures.event(.toolCompleted, at: progress))
        let lateRequestChanged = task.recordApprovalRequest(from: TestFixtures.event(.approvalRequested))
        #expect(lateRequestChanged)
        #expect(task.state == .waitingApproval)
        task.finishExecution(ActivityExecutionKey(agentThreadID: nil, turnID: "turn-a"), at: progress)
        #expect(!task.acceptsExecutionEvent(TestFixtures.event(.toolStarted, at: progress.addingTimeInterval(1))))
    }

    @Test func executionProgressDoesNotDismissApproval() {
        var task = makeTask()
        _ = task.recordApprovalRequest(from: TestFixtures.event(.approvalRequested))
        task.resumeExecution(from: TestFixtures.event(.toolCompleted))
        #expect(task.state == .waitingApproval)
        task.resumeExecution(from: TestFixtures.event(.toolCompleted, at: TestFixtures.now.addingTimeInterval(0.001)))
        #expect(task.state == .waitingApproval)
    }

    private func makeTask(_ event: ActivityDataRecord = TestFixtures.event()) -> ActivityTask {
        ActivityTask(
            displayID: UUID(), key: ActivityTaskKey(event: event)!, event: event,
            state: .running, startedAt: event.timestamp, progressGeneration: 1
        )
    }
}