Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 2 additions & 31 deletions Sources/ContextPanelCore/ResetPrimerPlanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 }
Expand Down
10 changes: 5 additions & 5 deletions Tests/ContextPanelCoreTests/ResetPrimerPlannerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
)]
Expand All @@ -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))"
Expand Down