diff --git a/Sources/QuotaCore/WidgetPlatformDisplay.swift b/Sources/QuotaCore/WidgetPlatformDisplay.swift index a44bf6f9..eeb1c71b 100644 --- a/Sources/QuotaCore/WidgetPlatformDisplay.swift +++ b/Sources/QuotaCore/WidgetPlatformDisplay.swift @@ -225,7 +225,7 @@ public enum QuotaCardProjection { public var isQuota: Bool { let lower = label.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() - guard !id.lowercased().hasSuffix(":plan-metadata"), lower != "plan details" else { return false } + guard !id.lowercased().contains("plan-metadata"), lower != "plan details" else { return false } guard !QuotaCapRule.isSupplementaryVideoQuota(providerKey: providerKey, modelId: modelId, modelType: modelType, label: label), !QuotaCapRule.isScopedPoolAllowance(providerKey: providerKey, id: id, modelId: modelId, diff --git a/Tests/QuotaCoreTests/QuotaCardProjectionTests.swift b/Tests/QuotaCoreTests/QuotaCardProjectionTests.swift index 746e1646..9321f15c 100644 --- a/Tests/QuotaCoreTests/QuotaCardProjectionTests.swift +++ b/Tests/QuotaCoreTests/QuotaCardProjectionTests.swift @@ -23,6 +23,18 @@ final class QuotaCardProjectionTests: XCTestCase { } } + func testJsonShapedMetadataIdIsNotAQuotaReading() { + // Producer-scoped window ids are serialized JSON, so a metadata id + // ends in `"]`, not in the bare ":plan-metadata" token (qa3 wire + // fixture): match the marker anywhere in the id (Kody, PR #388). + let jsonId = "[\"producer\",\"4f938669-72f9-4386-8808-6408a031ab4d\",\"minimax:plan-metadata\"]" + let metadata = row(jsonId, "minimax", nil, 100, label: "Usage notes") + XCTAssertFalse(metadata.isQuota) + let quota = row("short", "minimax", "5h", 92) + let rows = [metadata, quota] + XCTAssertEqual(QuotaCardProjection.selectedIndices(in: rows, now: now).map { rows[$0].id }, ["short"]) + } + func testPrimaryModelIdsDoNotCreateDuplicateAllowancesButFableIsDistinct() { let rows = [row("unknown", "anthropic", "7d", nil), row("short", "anthropic", "5h", 80), row("weekly", "anthropic", "7d", 0), diff --git a/Tests/WidgetParseTests/WidgetParseContractTests.swift b/Tests/WidgetParseTests/WidgetParseContractTests.swift index 2d690d9b..dfeb5c8a 100644 --- a/Tests/WidgetParseTests/WidgetParseContractTests.swift +++ b/Tests/WidgetParseTests/WidgetParseContractTests.swift @@ -178,4 +178,29 @@ final class WidgetParseContractTests: XCTestCase { let minimax = parsed.first { $0.providerKey == "minimax" } XCTAssertTrue(minimax?.windows.isEmpty ?? true, "a JSON-shaped metadata id is a caption, not a window") } + + /// The phone's intake is the widget's contract twin: a JSON-shaped + /// plan-metadata id that carries a reset date must never become a phone + /// window either (the reading-less drop only catches figure-less, + /// reset-less rows). Drives the phone's real intake predicate. + @MainActor + func testPhoneIntakeDropsJsonMetadataIds() throws { + let stamp = ISO8601DateFormatter().string(from: Date()) + let reset = ISO8601DateFormatter().string(from: Date().addingTimeInterval(3600)) + let codingPlan = "[\"producer\",\"Mac.Jays.Services\",\"minimax:coding-plan\"]" + let metadataJsonId = "[\"producer\",\"4f938669-72f9-4386-8808-6408a031ab4d\",\"minimax:plan-metadata\"]" + let payload = [ + wire(codingPlan, "minimax", "5h", 78, "Coding plan (all models)", stamp: stamp, reset: reset), + wire(metadataJsonId, "minimax", nil, nil, "MiniMax quota", stamp: stamp, reset: reset), + ] + let decoder = JSONDecoder() + var newestDeadCopy: Date? = nil + let admitted = try payload.map { + try decoder.decode(CompanionQuotaModel.WireRawWindow.self, from: JSONSerialization.data(withJSONObject: $0)) + }.filter { + CompanionQuotaModel.admitsToIntake($0, providerKey: "minimax", ownInstanceId: nil, + now: Date(), newestDeadCopyObservedAt: &newestDeadCopy) + } + XCTAssertEqual(admitted.map(\.id), [codingPlan]) + } } diff --git a/ios/CodeCapsCompanion/App/Models/CompanionQuotaModel.swift b/ios/CodeCapsCompanion/App/Models/CompanionQuotaModel.swift index 22187726..c22618bd 100644 --- a/ios/CodeCapsCompanion/App/Models/CompanionQuotaModel.swift +++ b/ios/CodeCapsCompanion/App/Models/CompanionQuotaModel.swift @@ -1580,7 +1580,9 @@ public final class CompanionQuotaModel: ObservableObject { let ext: String } - private struct WireRawWindow: Decodable { + // Internal rather than private so the mirrored package tests can feed + // wire rows to the intake predicate (WidgetParseContractTests). + struct WireRawWindow: Decodable { let id: String let provider: String let providerKey: String? @@ -2107,6 +2109,55 @@ public final class CompanionQuotaModel: ObservableObject { return CompanionDateFormatter.date(from: wire.occurredAt) } + /// The phone's wire-intake rule, lifted out of `buildPlatformSection` + /// unchanged so the mirrored package tests can exercise it directly: a + /// scoped pool or detail-less model limit is not the plan's quota (the + /// shared gate), a plan-metadata id or caption is not a window, a + /// reading-less status note is not a window, a dead copy is not a + /// reading, and this device's own window always survives. + static func admitsToIntake(_ w: WireRawWindow, providerKey: String, + ownInstanceId: String?, now: Date, + newestDeadCopyObservedAt: inout Date?) -> Bool { + let reading = QuotaCardProjection.Reading(id: w.id, providerKey: providerKey, label: w.label, + window: w.window, modelId: w.modelId, modelType: w.modelType, + remainingPercent: w.remainingUnknown == true ? nil : w.remainingPercent, + absoluteRemaining: w.absoluteRemaining, + resetAt: CompanionDateFormatter.date(from: w.resetAt), reportedAt: Self.reportedAt(from: w), + source: w.source, via: w.via) + guard reading.isQuota else { return false } + let lowId = w.id.lowercased() + let lowLabel = w.label.lowercased() + // Producer-scoped window ids are serialized JSON, so they end in + // `"]`, not in the bare token: match the metadata marker anywhere + // in the id, the same shape the widget's intake uses (PR #387). + if lowId.contains("plan-metadata") || lowLabel == "plan details" { + return false + } + if (w.remainingUnknown == true || lowLabel.contains("plan details")) + && w.remainingPercent == nil + && CompanionDateFormatter.date(from: w.resetAt) == nil + && !QuotaWindowVocabulary.isExtraAllowanceLabel(w.label) { + return false + } + if QuotaWindowVocabulary.isOwnDeviceWindow(id: w.id, + source: w.source, + machine: w.machine, + producerInstanceId: w.producerInstanceId, + ownInstanceId: ownInstanceId) { + return true + } + let observedAt = CompanionDateFormatter.date(from: w.occurredAt) + if QuotaFreshnessRule.isDeadCopy(observedAt: observedAt, + resetAt: CompanionDateFormatter.date(from: w.resetAt), + now: now) { + if let observedAt, observedAt > newestDeadCopyObservedAt ?? .distantPast { + newestDeadCopyObservedAt = observedAt + } + return false + } + return true + } + private func buildPlatformSection( id: String, providerKey: String, @@ -2156,35 +2207,9 @@ public final class CompanionQuotaModel: ObservableObject { // observation time. It only judges copies: this Mac's own reading // keeps its row, so an expired one greys out instead of vanishing. let liveWindows = rawWindows.filter { w in - guard reading(w).isQuota else { return false } - let lowId = w.id.lowercased() - let lowLabel = w.label.lowercased() - if lowId.hasSuffix(":plan-metadata") || lowLabel == "plan details" { - return false - } - if (w.remainingUnknown == true || lowLabel.contains("plan details")) - && w.remainingPercent == nil - && CompanionDateFormatter.date(from: w.resetAt) == nil - && !QuotaWindowVocabulary.isExtraAllowanceLabel(w.label) { - return false - } - if QuotaWindowVocabulary.isOwnDeviceWindow(id: w.id, - source: w.source, - machine: w.machine, - producerInstanceId: w.producerInstanceId, - ownInstanceId: payloadProducerInstanceId) { - return true - } - let observedAt = CompanionDateFormatter.date(from: w.occurredAt) - if QuotaFreshnessRule.isDeadCopy(observedAt: observedAt, - resetAt: CompanionDateFormatter.date(from: w.resetAt), - now: now) { - if let observedAt, observedAt > newestDeadCopyObservedAt ?? .distantPast { - newestDeadCopyObservedAt = observedAt - } - return false - } - return true + Self.admitsToIntake(w, providerKey: providerKey, + ownInstanceId: payloadProducerInstanceId, + now: now, newestDeadCopyObservedAt: &newestDeadCopyObservedAt) } /// Whether a window's reading is recent enough to believe, by the diff --git a/ios/CodeCapsCompanion/Widgets/WidgetPresentation.swift b/ios/CodeCapsCompanion/Widgets/WidgetPresentation.swift index 0befe3f5..fe74b227 100644 --- a/ios/CodeCapsCompanion/Widgets/WidgetPresentation.swift +++ b/ios/CodeCapsCompanion/Widgets/WidgetPresentation.swift @@ -1033,8 +1033,7 @@ public enum WidgetPresentation { // were carried it would take a bar slot beside the plan's own // meters (native probe, PR #367). let trimmedLabel = w.label.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() - guard !w.id.lowercased().hasSuffix(":plan-metadata"), - trimmedLabel != "plan details", + guard trimmedLabel != "plan details", !(trimmedLabel == "plan" && w.remainingPercent == nil) else { continue } let parsedReset = parseDate(from: w.resetAt) let observed = parseDate(from: w.occurredAt)