Skip to content

feat(chat): ask before a job the remaining AI limit covers only in part (W2-03) - #1994

Merged
gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-partial-limit
Nov 3, 2026
Merged

gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-partial-limit

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

CHAT-04 item 17a: before a job in several like parts, Chat checks that the remaining AI limit covers all of them, and asks first when it covers only some. Behind CHAT_AGENTIC. Stacked on #1993 (feature/chat-shortcut-playbooks): merge it first. Until then, this PR's diff also shows W2-09's commit. No migration. Plan: Chat waves plan, W2-03.

Server

  • fitChatUnits(orgId, { modelId, perUnit, units, ownKey, agentRunId }) in chat-budget.js counts how many of the job's parts the AI budget left covers. Each part is sized as one write step's worst case (stepWorstCaseUsd).
  • What is left is the budget, purchased credits included, minus the period's spend and holds. For an agent run's chat, the run's own cap counts too (CHAT-27 item 21).
  • It answers a count, never dollars, and holds nothing: each step is still held on its own.
  • Every part fits on the workspace's own key, without a budget (self-hosted), or for a model priced at nothing.
  • New internal endpoint POST /api/internal/chat/budget/fit (Zod: 1–100 parts).

Engine

  • A new engine tool, plan_parts, offered while writing. Before work in several like parts (drafts, pages, tasks), the model calls it once with how many parts (2–50) and what they are.
  • The limit covers all of them: the work goes on.
  • It covers none: the run pauses at the AI limit, as it already does (paused_cap).
  • It covers some: a question card asks "Your remaining AI limit covers about {k} of the {n} {unit}." with three options:
    • Do the first {k} now (Recommended; also taken when the card is skipped);
    • Get more AI credits: the run pauses at the AI limit, where the credits are sold;
    • Cancel: the run stops.
  • With no one there to ask (an agent's run, or a run no session is behind), the first {k} are done and filed, and the run ends paused at the AI limit.
  • A skill's run (a /skill or a shortcut's playbook) is checked as it starts, before anything is read. If the limit can't cover one write step, the run pauses at the AI limit with nothing spent.
  • Nothing is checked on the workspace's own key. If the check gets no answer, the job goes on and each step is still held as before.
  • The logic is in the new lib/chat/partial-limit.ts; engine.ts only gets thin hooks. The card is the existing question card, with new words in en.json.

Help: help/chat/chat.md gets "When only part of a job fits" (still approved: false).

Related issue

None. Chat waves plan, W2-03 (CHAT-04 item 17a).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Unit tests:

  • Server: chat-budget.test.js (fitChatUnits: room, purchased credits, an agent run's cap, own key) and internal-chat-budget.test.js (/fit).
  • Web: partial-limit.test.ts, budget.test.ts, and engine.test.ts:
    • offers the parts check while writing, and goes on when every part fits;
    • pauses at the AI limit when none fit, with no more model steps;
    • asks when some fit: Do the first k now (Recommended), Get more AI credits, Cancel;
    • goes on with the first parts when the card is skipped;
    • pauses on Get more AI credits and stops on Cancel;
    • with no one there, does what fits, files it and stops at the AI limit.

Manually (CHAT_AGENTIC=true, web and API server running, a cloud workspace whose remaining AI limit covers only a few write steps):

  1. Ask "Write 5 drafts, one for each of our top prompts." Chat asks "Your remaining AI limit covers about {k} of the 5 drafts." with the three options.
  2. Pick Do the first {k} now: Chat makes {k} drafts and says in one line which are left.
  3. Ask again and pick Get more AI credits: the run pauses at the AI limit. Ask again and pick Cancel: the run stops.
  4. With the limit used up, send /report: the run pauses at the AI limit before any step reads data.
  5. On the workspace's own AI key, the same request gets no card.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

CI passed on this branch (9bdd514), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41411448922): schema is up to date; web format:check, lint, typecheck, all tests and yarn build pass; server lint, format:check and all tests pass.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits November 2, 2026 09:22
…-09)

Each / menu shortcut (CHAT-08) now has a hidden playbook in skills/
(metadata.hidden: "true"): its allowed reads, default scope, output,
question rules and next moves, checked by the same format, scan and CI as
the library skills. The menu gains /start, /optimize, /opportunities and
/audit; "Create a report on" moves to /new-report, as /report is the
Weekly report skill's.

The composer sends shortcutId with the message while its trigger words
stay (rewritten, it drops), and keeps it on Retry. Home chips whose job
is a shortcut's send its id; chips for the focus areas' skills ask with
the skill's /name. The server takes the id only for a message that is
still that shortcut's, writes chat_messages.shortcut_id, loads the
playbook first as one of the run's three skill bodies, offers only the
reads in its data plan, and tells the model in one line what the plan
leaves out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rt (W2-03)

Before work in several like parts (drafts, pages, tasks), the model calls
the engine's plan_parts with how many; the server counts how many the AI
limit left covers (fitChatUnits and POST /api/internal/chat/budget/fit,
each part one write step's worst case, an agent run's own cap included):
a count, never dollars.

All covered, the work goes on. None, the run pauses at the AI limit as
before. Some, a question card asks "Your remaining AI limit covers about
{k} of the {n} ...": Do the first {k} now (Recommended), Get more AI
credits (pauses at the limit) or Cancel (stops). With no one there (an
agent's or a scheduled run), the first {k} are done and filed and the run
stops at the AI limit as it ends. A skill's run is checked as it starts,
before anything is read. Nothing is checked on the workspace's own key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@gkhngyk

gkhngyk commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

One issue: every new cap path calls pauseAtCap(rc, 'month'), including when the agent run's own cap is the limit that is short. fitChatUnits takes min(month room, run-cap room) but returns only fits, so the engine can't tell which limit binds. For a triggered run that hits its own cap (e.g. a 1% cap, 7 of 10 drafts), agent-driver then sees capHit !== 'run' and pauses the whole agent until the month resets instead of ending the run as "Stopped at its limit". Please return which limit binds from the fit endpoint and pause with 'run' when it is the run cap, as modelStep already does.

…heck hits

The fit check (fitChatUnits) took the smaller of what is left of the
month's AI limit and what an agent run's own cap leaves, but only returned
the count. The engine then paused every new cap path (a skill's run that
can't start, a job none of whose parts fit, Get more AI credits, and the
parts done with no one there) at the month's limit. For a triggered run
stopped by its own cap, the agent driver read capHit 'month' and paused
the whole agent until the month resets instead of ending the run as
"Stopped at its limit".

The server now returns which limit binds ('run' when the run's cap leaves
no more than the month, as reserveChatStep's run_cap), the route passes it
on, and the web client reads it (the month's when absent). The engine
pauses with that limit, keeping it on the parts card and on limitStop, as
modelStep already does for run_cap refusals.
# Conflicts:
#	help/chat/chat.md
#	server/src/lib/chat-budget.test.js
#	server/src/routes/internal-chat-budget.js
#	server/src/routes/internal-chat-budget.test.js
#	web/messages/en.json
#	web/src/lib/chat/engine.test.ts
#	web/src/lib/chat/engine.ts

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@gkhngyk
gkhngyk merged commit 14fb3ce into ansvisor:main Nov 3, 2026
5 checks passed
cihan-gyk added a commit to cihan-gyk/ansvisor that referenced this pull request Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants