From f383280ad0c1250db5d809bdb58a75fed36eb522 Mon Sep 17 00:00:00 2001 From: Chris Busillo Date: Thu, 28 May 2026 19:20:39 -0400 Subject: [PATCH] Avoid name-only reset primer reuse --- .../ContextPanelCore/ResetPrimerPlanner.swift | 33 ++----------------- .../ResetPrimerPlannerTests.swift | 10 +++--- 2 files changed, 7 insertions(+), 36 deletions(-) diff --git a/Sources/ContextPanelCore/ResetPrimerPlanner.swift b/Sources/ContextPanelCore/ResetPrimerPlanner.swift index 5620a85..7623193 100644 --- a/Sources/ContextPanelCore/ResetPrimerPlanner.swift +++ b/Sources/ContextPanelCore/ResetPrimerPlanner.swift @@ -97,30 +97,18 @@ public struct ResetPrimerRunState: Codable, Equatable, Sendable { } public func record(for candidate: ResetPrimerCandidate) -> ResetPrimerRunRecord? { - record( - for: candidate.key, - legacyAccountIDs: candidate.legacyAccountIDs, - accountName: candidate.accountName, - allowsLegacyConfiguredKeyMatch: candidate.usesResolvedAccountKey - ) + record(for: candidate.key, legacyAccountIDs: candidate.legacyAccountIDs) } private func record( for key: ResetPrimerRunKey, - legacyAccountIDs: [String], - accountName: String? = nil, - allowsLegacyConfiguredKeyMatch: Bool = false + legacyAccountIDs: [String] ) -> ResetPrimerRunRecord? { records .filter { record in record.key == key || record.key.matchesLegacyIdentity(of: key, legacyAccountIDs: legacyAccountIDs) || key.matchesHistoricalIdentity(of: record) - || record.matchesLegacyConfiguredIdentity( - of: key, - accountName: accountName, - isAllowed: allowsLegacyConfiguredKeyMatch - ) } .max { lhs, rhs in lhs.updatedAt < rhs.updatedAt } } @@ -686,10 +674,6 @@ private extension ResetPrimerCandidate { var legacyAccountIDs: [String] { Array(Set(resolvedAccountIDs).subtracting([configuredAccountID])).sorted() } - - var usesResolvedAccountKey: Bool { - key.accountID == resolvedAccountID && configuredAccountID == resolvedAccountID - } } private extension Array where Element == String { @@ -725,19 +709,6 @@ private extension ResetPrimerRunKey { } } -private extension ResetPrimerRunRecord { - func matchesLegacyConfiguredIdentity( - of other: ResetPrimerRunKey, - accountName otherAccountName: String?, - isAllowed: Bool - ) -> Bool { - guard isAllowed, let otherAccountName else { return false } - guard key.provider == other.provider, key.resetAt == other.resetAt, key.accountID != other.accountID else { return false } - guard resolvedAccountIDs == [key.accountID] else { return false } - return accountName.caseInsensitiveCompare(otherAccountName) == .orderedSame - } -} - private extension Array where Element == UsageLimit { var sortedByLimitID: [UsageLimit] { sorted { lhs, rhs in lhs.id < rhs.id } diff --git a/Tests/ContextPanelCoreTests/ResetPrimerPlannerTests.swift b/Tests/ContextPanelCoreTests/ResetPrimerPlannerTests.swift index e826adf..7edf68b 100644 --- a/Tests/ContextPanelCoreTests/ResetPrimerPlannerTests.swift +++ b/Tests/ContextPanelCoreTests/ResetPrimerPlannerTests.swift @@ -363,7 +363,7 @@ private let now = Date(timeIntervalSinceReferenceDate: 900_000_000) #expect(plan.isEmpty) } -@Test func resetPrimerPlannerKeepsLegacyConfiguredRunStateWhenCurrentSnapshotLosesAlias() throws { +@Test func resetPrimerPlannerDoesNotKeepLegacyConfiguredRunStateWhenCurrentSnapshotLosesAlias() throws { let resetAt = now.addingTimeInterval(-10 * 60) let settings = ResetPrimerSettings( isEnabled: true, @@ -404,10 +404,10 @@ private let now = Date(timeIntervalSinceReferenceDate: 900_000_000) let plan = ResetPrimerPlanner.plan(settings: settings, snapshot: snapshot, state: state, now: now) #expect(state.records.first?.resolvedAccountIDs == ["configured-openai"]) - #expect(plan.isEmpty) + #expect(plan.due.map(\.accountID) == ["resolved-openai-v2"]) } -@Test func resetPrimerPlannerDoesNotMatchLegacyConfiguredRunStateForDifferentAccountName() throws { +@Test func resetPrimerPlannerDoesNotMatchLegacyConfiguredRunStateByAccountName() throws { let resetAt = now.addingTimeInterval(-10 * 60) let settings = ResetPrimerSettings( isEnabled: true, @@ -418,7 +418,7 @@ private let now = Date(timeIntervalSinceReferenceDate: 900_000_000) generatedAt: now, limits: [limit( accountID: "resolved-openai-b", - accountName: "OpenAI B", + accountName: "Shared Account Name", provider: .openAI, resetAt: resetAt )] @@ -433,7 +433,7 @@ private let now = Date(timeIntervalSinceReferenceDate: 900_000_000) "accountID" : "configured-openai-a", "resetAt" : "\(ISO8601DateFormatter().string(from: resetAt))" }, - "accountName" : "OpenAI A", + "accountName" : "Shared Account Name", "scheduledAt" : "\(ISO8601DateFormatter().string(from: resetAt))", "status" : "completed", "updatedAt" : "\(ISO8601DateFormatter().string(from: now))"