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
2 changes: 1 addition & 1 deletion Sources/QuotaCore/WidgetPlatformDisplay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions Tests/QuotaCoreTests/QuotaCardProjectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
25 changes: 25 additions & 0 deletions Tests/WidgetParseTests/WidgetParseContractTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
85 changes: 55 additions & 30 deletions ios/CodeCapsCompanion/App/Models/CompanionQuotaModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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" {
Comment thread
swu-wise[bot] marked this conversation as resolved.
Resolved
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,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions ios/CodeCapsCompanion/Widgets/WidgetPresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Comment thread
swu-wise[bot] marked this conversation as resolved.
Resolved
let parsedReset = parseDate(from: w.resetAt)
let observed = parseDate(from: w.occurredAt)
Expand Down
Loading