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
8 changes: 8 additions & 0 deletions pm-stack/aggregator/src/aa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,14 @@ export interface AaPlanFact {
/** List-rate dollars of usage per $1 paid, when the plan meters so. */
multiplier: number | null;
note: string;
/** Typed cancellation source data (#1174): the plan is cancelled and
* its paid period ends on this ISO date. The quota-action derivation
* (buildQuotaActions) reads THESE fields — never the note prose —
* so update them with the registry when a plan changes. */
cancelled?: boolean;
periodEndsAt?: string;
/** Concrete next step while the cancelled period still has time left. */
renewalAction?: string;
}

export const AA_PLAN_FACTS: Record<string, AaPlanFact> = {
Expand Down
160 changes: 159 additions & 1 deletion pm-stack/aggregator/src/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ export function deriveToday(
// PAYG over $5 (unbilled accrual only — Q3 default), surfaces worst-first.
// ---------------------------------------------------------------------------

import { countedEstimate, type AttentionFlag, type QuotaView, type SnapshotRecord, type UsageView } from "./model.ts";
import { countedEstimate, type AttentionFlag, type Horizon, type QuotaAction, type QuotaActionKind, type QuotaView, type SnapshotRecord, type UsageView } from "./model.ts";

export const BUBBLE_PCT = 80;
export const BUBBLE_USD = 5;
Expand Down Expand Up @@ -989,6 +989,164 @@ export function buildAttentionFlags(
return flags.sort((a, b) => (rank(a.kind) === rank(b.kind) ? b.severity - a.severity : rank(a.kind) - rank(b.kind)));
}

// --- Quota actions (#1174) --------------------------------------------------
// One derivation for the expiring/cancelled, walled, and high-usage
// quota actions surfaced on /quota's strip, /today, and /attention's
// orchestrator tier. The cancellation source data is the plan-facts
// registry (AaPlanFact.cancelled/periodEndsAt, updated with the
// registry when a plan changes) — never scraped from display prose.
// Walled and high-usage state comes from the pools themselves. An
// entry exists only while its condition holds (deadline in the
// future and inside the window, pool still walled, window still hot
// with a distant reset), so entries expire and clear on their own and
// no surface can render a duplicate or stale copy.

/** Structural plan-fact input; AA_PLAN_FACTS satisfies this. */
export interface QuotaPlanFact {
cancelled?: boolean;
periodEndsAt?: string | null;
renewalAction?: string | null;
}

/** A cancelled period counts as expiring inside this many days. */
export const QUOTA_ACTION_WINDOW_DAYS = 14;
/** High-usage bar: strictly above this percent of limit. */
export const QUOTA_HIGH_USAGE_PCT = 85;
/** High-usage counts only when the reset is further out than this. */
export const QUOTA_HIGH_USAGE_RESET_MS = 24 * 3_600_000;

function planFactKeyForPool(poolId: string): string {
return poolId.replace(":", ".");
}

/**
* Every eligible quota action across the pools, worst first: expiring
* periods by nearest deadline, then walled pools, then high-usage
* windows by percent. Pure — same quota in, same actions out — so the
* three surfaces render the identical entries.
*/
export function buildQuotaActions(
quota: QuotaView,
plans: Readonly<Record<string, QuotaPlanFact>> = {},
now = Date.now(),
): QuotaAction[] {
const actions: QuotaAction[] = [];
for (const pool of quota.pools) {
const key = planFactKeyForPool(pool.id);
const fact = Object.hasOwn(plans, key) ? plans[key] : undefined; // #3056 own-key guard
const periodEnd = fact?.cancelled && fact.periodEndsAt ? Date.parse(fact.periodEndsAt) : NaN;
if (Number.isFinite(periodEnd)) {
const left = (periodEnd as number) - now;
if (left >= 0 && left <= QUOTA_ACTION_WINDOW_DAYS * 86_400_000) {
const day = String(fact?.periodEndsAt).slice(0, 10);
actions.push({
id: `${pool.id}:expiry`,
poolId: pool.id,
provider: pool.provider,
plan: pool.plan,
kind: "expiry",
title: `${pool.provider} — ${pool.plan} ends ${day}`,
detail: `Cancelled — period ends ${day}${fact?.renewalAction ? ` · ${fact.renewalAction}` : ""}`,
deadline: new Date(periodEnd as number).toISOString(),
href: "/quota",
});
}
}
if (pool.state === "walled") {
actions.push({
id: `${pool.id}:walled`,
poolId: pool.id,
provider: pool.provider,
plan: pool.plan,
kind: "walled",
title: `${pool.provider} — ${pool.plan} is walled`,
detail: pool.wallLiftsAt
? `Walled — lifts ${pool.wallLiftsAt.slice(0, 10)} · hold new sessions until then`
: "Walled with no lift date recorded — check the pool before starting sessions",
deadline: pool.wallLiftsAt ?? null,
href: "/quota",
});
}
// Eligibility before selection (Codex P2, PR #4695): a hotter window
// whose reset is near — or unrecorded — must not mask a qualifying
// cooler window, so pick the highest-usage window among those whose
// reset is distant enough to act on.
let worst: QuotaWindow | null = null;
let worstPct = 0;
let worstReset = "";
for (const w of pool.windows) {
const pct = windowPercent(w);
if (pct <= QUOTA_HIGH_USAGE_PCT || !w.resetsAt) continue;
const reset = Date.parse(w.resetsAt);
if (!Number.isFinite(reset) || reset - now <= QUOTA_HIGH_USAGE_RESET_MS) continue;
if (pct > worstPct) {
worstPct = pct;
worst = w;
Comment on lines +1077 to +1084

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check reset eligibility before selecting the highest-usage window

For a pool with multiple windows, a higher-percentage short window can mask a qualifying longer window. For example, a 5-hour window at 99% resetting in two hours and a weekly window at 92% resetting in four days produce no action: the loop selects the 5-hour window, then rejects its reset without considering the weekly window. The same happens when the highest window has no reset recorded, a shape produced by extractClinePass in quota-extract.ts. Filter for valid, distant resets before selecting the highest percentage so qualifying warnings remain visible.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 095f1d5 (task 14752, lane code-t): buildQuotaActions now filters windows by reset eligibility (percent bar + distant, recorded reset) BEFORE selecting the highest-usage one, so a hotter near-reset/unrecorded-reset window no longer masks a qualifying cooler window. Regression tests in quota-actions-1174.test.ts: "a hotter window with a near reset does not mask a qualifying cooler window" and the no-reset-recorded twin — both pass.

worstReset = w.resetsAt;
}
}
if (worst) {
actions.push({
id: `${pool.id}:high-usage`,
poolId: pool.id,
provider: pool.provider,
plan: pool.plan,
kind: "high-usage",
title: `${pool.provider} — ${pool.plan} at ${Math.round(worstPct)}%`,
detail: `${worst.name} window ${Math.round(worstPct)}% used · resets ${worstReset.slice(0, 10)} — burn it down or hold new sessions`,
deadline: worstReset,
href: "/quota",
});
}
}
const rank: Record<QuotaActionKind, number> = { expiry: 0, walled: 1, "high-usage": 2 };
return actions.sort(
(a, b) =>
rank[a.kind] - rank[b.kind] ||
(a.deadline ?? "").localeCompare(b.deadline ?? "") ||
a.id.localeCompare(b.id),
);
}

/** The /quota "Needs attention" strip rows for already-derived actions. */
export function quotaActionFlags(actions: QuotaAction[], now = Date.now()): AttentionFlag[] {
return actions.map((a) => ({
kind: "quota-action" as const,
label: a.title,
detail: a.detail,
href: a.href,
severity:
a.kind === "expiry" && a.deadline
? 1000 + (QUOTA_ACTION_WINDOW_DAYS - (Date.parse(a.deadline) - now) / 86_400_000)
: a.kind === "walled"
? 500
: 100,
}));
}

/** The /attention orchestrator-tier items for already-derived actions. */
export function quotaActionTierItems(actions: QuotaAction[], now = Date.now()): TierItem[] {
const at = new Date(now).toISOString();
return actions.map((a) => {
const left = a.deadline ? Date.parse(a.deadline) - now : NaN;
return {
id: `quota-action:${a.id}`,
kind: "task" as const,
title: a.title,
// Aggregator-synthesized like the #1148 watchdog items: derived
// live, never published by the orchestrator.
source: "watchdog" as const,
reason: a.detail,
since: at,
lastSeenAt: at,
horizon: (
Number.isFinite(left) ? (left <= 86_400_000 ? "today" : left <= 3 * 86_400_000 ? "next-3-days" : "longer") : "longer"
) as Horizon,
recommendedAction: a.detail,
};
});
}

export interface Freshness {
stale: boolean;
/** Whole days since the freshest read; null when never measured. */
Expand Down
33 changes: 32 additions & 1 deletion pm-stack/aggregator/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1389,16 +1389,47 @@ export interface ChargeRow {
/**
* One tripped bubble rule (§1.3): a metric over 80% of its limit, or
* projected unbilled PAYG over $5 (unbilled accrual only, Q3 default).
* "quota-action" entries are the eligible quota actions from
* buildQuotaActions (derive.ts, #1174) — expiry/cancellation, walled,
* and high-usage — rendered on the same strip, never a second list.
*/
export interface AttentionFlag {
kind: "over-limit" | "payg-unbilled" | "unmatched-charge" | "credential-lifecycle";
kind: "over-limit" | "payg-unbilled" | "unmatched-charge" | "credential-lifecycle" | "quota-action";
label: string;
detail: string;
href: string;
/** Percent (over-limit) or counted dollars (payg-unbilled); worst first. */
severity: number;
}

/**
* One actionable quota entry (#1174): an expiring/cancelled plan
* period, a walled pool, or a high-usage window whose reset is far
* enough away to act on. Derived once by buildQuotaActions (derive.ts)
* from the pools' typed source data — never by scraping display
* prose — and rendered identically on /quota, /today, and /attention.
* An entry exists only while its source condition holds: past
* deadlines and cleared states produce no entry, so nothing goes
* stale and nothing renders twice.
*/
export type QuotaActionKind = "expiry" | "walled" | "high-usage";

export interface QuotaAction {
/** Stable per pool + kind, so surfaces dedupe by identity. */
id: string;
poolId: string;
provider: string;
plan: string;
kind: QuotaActionKind;
/** Actionable one-liner, e.g. "OpenCode Go — Personal ends 2026-11-02". */
title: string;
/** Reason + deadline + concrete next step, when the source names one. */
detail: string;
/** ISO deadline when the source carries one (expiry date, wall lift, reset). */
deadline: string | null;
href: string;
}

/**
* The quota plane's latest-state contract (Stage 2, #1151): one entry
* per provider credential the gatherer read this tick, each window
Expand Down
26 changes: 21 additions & 5 deletions pm-stack/aggregator/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type RoadmapGrain,
type TodayView,
allRefs,
buildQuotaActions,
commentStream,
executionStrip,
freshness,
Expand Down Expand Up @@ -43,7 +44,7 @@ import { commentKindCounts, githubCommentUrl, queuedComments, type CommentKindCo
import { serviceHomeLinks, type ItemServiceLink } from "./service-links.ts";
import { activityCoverageHtml, gapsHtml, type ActivityCoverage, type GapRecord } from "./activity.ts";
import type { AuditEvent } from "./events.ts";
import { aaCaiPoints, aaPoints, summarizeAa } from "./aa.ts";
import { AA_PLAN_FACTS, aaCaiPoints, aaPoints, summarizeAa } from "./aa.ts";
import {
applyInequalityFilters,
bandIndex,
Expand Down Expand Up @@ -3438,6 +3439,7 @@ export function renderTodayBody(
queueStale = false,
): string {
const walled = quota.pools.filter((p) => p.state === "walled");
const quotaActions = buildQuotaActions(quota, AA_PLAN_FACTS);
// "-direct" display rule (#6125), judged over this view's pool set.
const poolDisplayNames = providerDisplayNames(quota.pools.map((p) => p.provider));
const displayPoolProvider = (name: string) => poolDisplayNames.get(name) ?? name;
Expand All @@ -3448,9 +3450,17 @@ export function renderTodayBody(
`<div class="banner drew"><strong>${drewTotal} item${drewTotal === 1 ? "" : "s"} in the user queue</strong> — <a href="/attention#drew">review them</a></div>`,
);
}
if (walled.length) {
// #1174: the same eligible quota actions as /quota's strip and
// /attention's orchestrator tier, derived once above — never a
// second list. Walled pools without a typed action still banner.
for (const a of quotaActions) {
alerts.push(
`<div class="banner"><strong>Walled pools:</strong> ${walled.map((p) => esc(`${displayPoolProvider(p.provider)} (${p.plan})`)).join(", ")} — <a href="/quota">quota</a></div>`,
`<div class="banner"><strong>Quota action:</strong> ${esc(a.title)} — ${esc(a.detail)} <a href="${esc(a.href)}">quota</a></div>`,
);
}
for (const p of walled.filter((p) => !quotaActions.some((a) => a.poolId === p.id))) {
alerts.push(
`<div class="banner"><strong>Walled pools:</strong> ${esc(`${displayPoolProvider(p.provider)} (${p.plan})`)} — <a href="/quota">quota</a></div>`,
);
}
const wipViolations = (today.wipLanes ?? []).flatMap((lane) => [
Expand Down Expand Up @@ -3481,13 +3491,19 @@ export function renderTodayBody(
const actions = today.actions?.length
? `<h2>Pushed back into motion</h2>${today.actions.map((a) => `<div class="card"><span class="badge">${esc(a.kind)}</span> <strong>${esc(a.target)}</strong><br>${esc(a.summary)}<br><span class="muted">${esc(fmtDate(a.at))}</span></div>`).join("")}`
: "";
const quotaObjectives = quotaActions.map((a) => ({
title: a.title,
detail: a.detail,
chip: "quota · action",
}));
const allObjectives = [...today.objectives, ...quotaObjectives];
// #6174: an objective ref comes from publisher lined-up data and is
// only sometimes a GitHub ref — tracked-goal items carry their goal id,
// and refUrl throws on those, which 500ed the whole /today render (the
// Today-page sibling of the #6149 Needs-you fix). Mirror tierItemCard:
// GitHub refs link, everything else renders as plain code.
const objectives = today.objectives.length
? today.objectives
const objectives = allObjectives.length
? allObjectives
.map(
(o, i) =>
`<div class="card obj"><div class="n">${i + 1}</div><div><strong>${esc(o.title)}</strong> <span class="badge src">${esc(o.chip)}</span>${o.detail ? `<br><span class="muted">${esc(o.detail)}</span>` : ""}${o.ref ? `<br>${isValidRef(o.ref) ? `<a href="${esc(refUrl(o.ref))}">${esc(o.ref)}</a>` : `<code>${esc(o.ref)}</code>`}` : ""}</div></div>`,
Expand Down
23 changes: 20 additions & 3 deletions pm-stack/aggregator/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
import { loadConfig } from "./config.ts";
import { itemServiceLinks } from "./service-links.ts";
import { loadFeed } from "./feed.ts";
import { AA_PLAN_FACTS } from "./aa.ts";
import { shouldFetchNeedsDrewCensus, type NeedsDrewCensus } from "./needs-drew.ts";
import {
buildHealth,
Expand All @@ -58,7 +59,7 @@ import {
import { manifestRouteFor } from "./pages.ts";
import { conflictInbox, type ConflictInbox } from "./conflicts.ts";
import { watchdogTierItems, WATCHDOG_PROBE_SERVICE_IDS } from "./watchdog.ts";
import { allRefs, buildAttention, deriveToday, filterAttentionViewByRefs, projectPace, type RoadmapGrain } from "./derive.ts";
import { allRefs, buildAttention, buildQuotaActions, deriveToday, filterAttentionViewByRefs, projectPace, quotaActionTierItems, type RoadmapGrain } from "./derive.ts";
import {
reconcileRoadmap,
roadmapDriftReport,
Expand Down Expand Up @@ -789,8 +790,19 @@ async function attentionContext(opts: { census?: boolean } = {}) {
// recomputes it on every orchestration read (#1139).
const diagnosis = source.diagnostics?.().orchestration;
const watchdog = await deriveWatchdogItems(diagnosis);
const quota = await source.quota().catch(() => null);
const view = buildAttention(feed, summary, Date.now(), diagnosis, watchdog);
// #1174: the same eligible quota actions as /quota's strip and
// /today, as orchestrator-tier task items — derived once here from
// the same pure function, so entries clear on every surface the
// moment their source condition no longer holds.
const quotaItems = quota ? quotaActionTierItems(buildQuotaActions(quota, AA_PLAN_FACTS)) : [];
if (quota) {
view.orchestratorExtra = [...view.orchestratorExtra, ...quotaItems];
}
return {
view: buildAttention(feed, summary, Date.now(), diagnosis, watchdog),
view,
quotaItems,
coverage,
orchestration: summary,
diagnosis,
Expand Down Expand Up @@ -2196,11 +2208,16 @@ async function handler(req: Request): Promise<Response> {
// HTML renders — the array shape itself is unchanged (Codex P2
// on PR #2431). #3317: include non-session needs-drew items too,
// matching the full queue rendered by the attention page.
const { view } = await attentionContext();
const { view, quotaItems } = await attentionContext();
return json([
...view.all,
...view.drewExtra.map((i) => ({ ...i, tier: "needs-drew" as const })),
...view.watchdog.map((i) => ({ ...i, tier: "needs-orchestrator" as const })),
// #1174 (Codex P2, PR #4695): the synthesized quota tasks ride along
// in the same TierItem shape — this endpoint is the machine-first
// interface the attention page advertises, so the quota tasks must
// be discoverable here, not only in the HTML orchestrator tier.
...quotaItems.map((i) => ({ ...i, tier: "needs-orchestrator" as const })),
]);
}
if (path === "/api/coverage.json") {
Expand Down
10 changes: 8 additions & 2 deletions pm-stack/aggregator/src/sources/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
refsInText,
refUrl,
} from "../model.ts";
import { buildAttentionFlags } from "../derive.ts";
import { buildAttentionFlags, buildQuotaActions, quotaActionFlags } from "../derive.ts";
import { validateRoadmapHorizon } from "../roadmap-horizon.ts";
import { gristDocUrl } from "../service-links.ts";
import { recordFromSessionEntry } from "../registry.ts";
Expand Down Expand Up @@ -1393,7 +1393,13 @@ export function fixtureSource(): Source {
return [...byRef.values()];
},
quota: async () => FIXTURE_QUOTA,
usage: async () => ({ ...FIXTURE_USAGE, attention: buildAttentionFlags(FIXTURE_QUOTA, FIXTURE_USAGE) }),
usage: async () => ({
...FIXTURE_USAGE,
attention: [
...buildAttentionFlags(FIXTURE_QUOTA, FIXTURE_USAGE),
...quotaActionFlags(buildQuotaActions(FIXTURE_QUOTA)),
],
}),
attention: async () => FIXTURE_ATTENTION,
// Fixtures carry no orchestration section: "not measured" is the
// honest fixture state for a job that only runs live (#899).
Expand Down
Loading
Loading