Багрепорты todo_service: проверка (CI-баг и запись без проекта приняты в approved, токены - обсуждение); масштаб лимита только при полном покрытии окна; промт khb-todo
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -150,7 +150,7 @@ describe("промты в docs модуля (p-*.md)", () => {
|
||||
|
||||
it("todo через khb-todo (proposed): блок инструмента, указатели в промтах и события вместо старого слежения Control", () => {
|
||||
const tool = loadFragment("p-todo-tool.md", "Todo: khb-todo", { person_id: "PERSON-1" }, "proposed");
|
||||
for (const part of ["khb-todo list", "khb-todo create", "khb-todo snooze", "khb-todo control", "khb-todo stale", "khb-todo keep", "close-batch", "PERSON-1", "queued", "багрепорт"]) expect(tool, part).toContain(part);
|
||||
for (const part of ["khb-todo list", "khb-todo create", "khb-todo snooze", "khb-todo control", "khb-todo stale", "khb-todo keep", "close-batch", "PERSON-1", "queued", "вне списка проектов"]) expect(tool, part).toContain(part);
|
||||
for (const name of ["p-manager-dialog.md", "p-project-channel.md", "p-ceo-assistant.md"]) expect(loadPrompt(name, { media_dir: "/m" }, "proposed"), name).toContain("`khb-todo`");
|
||||
const events = loadFragment("p-chat-events.md", "События todo", { events: "- control_done: x" }, "proposed");
|
||||
for (const part of ["control_done", "review_due", "write_failed", "- control_done: x"]) expect(events, part).toContain(part);
|
||||
|
||||
@@ -122,12 +122,16 @@ d("учёт токенов в БД (временная БД)", () => {
|
||||
}) as unknown as typeof fetch;
|
||||
const notes: string[] = [];
|
||||
const poller = new LimitsPoller(db.pool, log, async (t) => { notes.push(t); }, { intervalMs: 0, credentialsPath: cred, fetchFn });
|
||||
// 4 млн «взвешенных» токенов за недельное окно при 40% -> лимит недели 10 млн
|
||||
await db.pool.query("insert into agent_usage (op, input_tokens, output_tokens) values ('x', 3000000, 1000000)");
|
||||
// учёт моложе недельного окна: масштаб в токенах не оцениваем (иначе занижение), проценты пишем
|
||||
await poller.tick();
|
||||
let g = (await db.pool.query("select * from globals")).rows[0];
|
||||
expect(seen[0]).toBe("Bearer tok-test");
|
||||
expect(Number(g.limit_week_used_pct)).toBe(40);
|
||||
expect(g.limit_week_tokens_est).toBeNull();
|
||||
// учёт покрывает окно: 4 млн «взвешенных» токенов при 40% -> лимит недели 10 млн
|
||||
await db.pool.query("insert into agent_usage (ts, op, input_tokens, output_tokens) values (now() - interval '8 days', 'x', 0, 0), (now(), 'x', 3000000, 1000000)");
|
||||
await poller.tick();
|
||||
g = (await db.pool.query("select * from globals")).rows[0];
|
||||
expect(seen[0]).toBe("Bearer tok-test");
|
||||
expect(Number(g.limit_week_tokens_est)).toBeGreaterThanOrEqual(10_000_000);
|
||||
expect(Number(g.limit_day_tokens_est)).toBe(Math.round(Number(g.limit_week_tokens_est) / 7));
|
||||
expect(Number(g.limit_month_tokens_est)).toBe(Math.round((Number(g.limit_week_tokens_est) * 30) / 7));
|
||||
|
||||
@@ -215,11 +215,16 @@ export class LimitsPoller {
|
||||
return t;
|
||||
}
|
||||
|
||||
private async weighted(since: Date): Promise<number> {
|
||||
/** Взвешенные токены окна; null, если учёт не покрывает окно целиком (самая старая запись позже начала окна + 5% окна): иначе масштаб занижался бы. */
|
||||
private async weighted(since: Date, windowMs: number): Promise<number | null> {
|
||||
const { rows } = await this.pool.query(
|
||||
"select coalesce(sum(input_tokens + output_tokens + cache_creation_tokens + cache_read_tokens / 10.0), 0) as w from agent_usage where ts >= $1",
|
||||
"select min(ts) as first, coalesce(sum(input_tokens + output_tokens + cache_creation_tokens + cache_read_tokens / 10.0), 0) as w from agent_usage where ts >= $1",
|
||||
[since],
|
||||
);
|
||||
const first = rows[0].first ? new Date(rows[0].first).getTime() : null;
|
||||
const { rows: [o] } = await this.pool.query("select min(ts) as oldest from agent_usage");
|
||||
const oldest = o.oldest ? new Date(o.oldest).getTime() : null;
|
||||
if (first === null || oldest === null || oldest > since.getTime() + windowMs * 0.05) return null;
|
||||
return Number(rows[0].w);
|
||||
}
|
||||
|
||||
@@ -233,10 +238,10 @@ export class LimitsPoller {
|
||||
}
|
||||
if (!res.ok) throw new Error(`usage HTTP ${res.status}`);
|
||||
const s = parseLimits(await res.json());
|
||||
const week = s.weekResetsAt ? await this.weighted(new Date(s.weekResetsAt.getTime() - 7 * 86400_000)) : 0;
|
||||
const sess = s.sessionResetsAt ? await this.weighted(new Date(s.sessionResetsAt.getTime() - 5 * 3600_000)) : 0;
|
||||
const weekEst = estimateWindowTokens(week, s.weekPct);
|
||||
const sessEst = estimateWindowTokens(sess, s.sessionPct);
|
||||
const week = s.weekResetsAt ? await this.weighted(new Date(s.weekResetsAt.getTime() - 7 * 86400_000), 7 * 86400_000) : null;
|
||||
const sess = s.sessionResetsAt ? await this.weighted(new Date(s.sessionResetsAt.getTime() - 5 * 3600_000), 5 * 3600_000) : null;
|
||||
const weekEst = week === null ? null : estimateWindowTokens(week, s.weekPct);
|
||||
const sessEst = sess === null ? null : estimateWindowTokens(sess, s.sessionPct);
|
||||
const { rows: [g] } = await this.pool.query(
|
||||
`select limit_week_resets_at, limit_session_resets_at, limit_week_alerted_pct, limit_session_alerted_pct from globals where id`,
|
||||
);
|
||||
@@ -257,8 +262,7 @@ export class LimitsPoller {
|
||||
}
|
||||
await this.pool.query(
|
||||
`update globals set limits_checked_at = now(), limit_session_used_pct = $1, limit_session_resets_at = $2, limit_week_used_pct = $3, limit_week_resets_at = $4,
|
||||
limit_week_tokens_est = coalesce($5::bigint, limit_week_tokens_est), limit_day_tokens_est = coalesce($6::bigint, limit_day_tokens_est),
|
||||
limit_month_tokens_est = coalesce($7::bigint, limit_month_tokens_est), limit_5h_tokens_est = coalesce($8::bigint, limit_5h_tokens_est),
|
||||
limit_week_tokens_est = $5::bigint, limit_day_tokens_est = $6::bigint, limit_month_tokens_est = $7::bigint, limit_5h_tokens_est = $8::bigint,
|
||||
extra_usage_limit_cents = $9, extra_usage_used_cents = $10, limit_week_alerted_pct = $11, limit_session_alerted_pct = $12, updated_at = now() where id`,
|
||||
[
|
||||
s.sessionPct, s.sessionResetsAt, s.weekPct, s.weekResetsAt, weekEst, weekEst === null ? null : Math.round(weekEst / 7), weekEst === null ? null : Math.round((weekEst * 30) / 7), sessEst,
|
||||
|
||||
Reference in New Issue
Block a user