@hginsights/hgi 0.2.0-rc.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,12 +6,12 @@ Every failure has its own exit code, so a script can branch without parsing Engl
6
6
  |---|---|---|
7
7
  | 0 | | success |
8
8
  | 1 | `internal_error` | a bug in `hgi`. Never used for a server response or a usage error |
9
- | 2 | `invalid_input` | bad flags or usage; invalid JSON; input that fails the tool's schema; unknown tool; unreadable `--input @file` or `--ids @file`; `--out` refusals (exists, symlink, protected path, `--force` on `call`); config-dir or `HGI_LEDGER` workspace guard (`workspace_guard`); a bad `--label` (`bad_label`), `hgi usage --since` (`bad_since`) or `--by` (`bad_by`); any `--ids` refusal (see [below](#partial-output-from---ids)). Nothing is sent |
9
+ | 2 | `invalid_input` | bad flags or usage; invalid JSON; input that fails the tool's schema; unknown tool; unreadable `--input @file` or `--ids @file`; `--out` refusals (exists, symlink, protected path, `--force` on `call`); config-dir or `HGI_LEDGER` workspace guard (`workspace_guard`); a bad `--label` (`bad_label`), `hgi usage --since` (`bad_since`) or `--by` (`bad_by`); any `--ids` refusal (see [below](#partial-output-from---ids)); any `--all-pages` refusal, `not_pageable` among them (see [below](#partial-output-from---all-pages)). Nothing is sent |
10
10
  | 3 | `tool_error` | the tool returned `isError` (including server-side invalid arguments, which the server reports in-band), a JSON-RPC error, or a response `hgi` could not parse after the call was sent. With `--ids`: at least one batch call failed (every row is printed first) |
11
11
  | 4 | `login_required`, `login_expired`, `phoenix_account_required` | no credentials; a second 401 after one refresh; the refresh token was rejected (`invalid_grant`). `phoenix_account_required`: the server says the signed-in identity has no Phoenix account; sign up, or sign in with `hgi auth login` as an account that has one |
12
- | 5 | `rate_limited` | 429 after bounded retries (MCP calls); a 429 from the userinfo or revoke endpoint exits at once |
12
+ | 5 | `rate_limited` | 429 after bounded retries (MCP calls: 3 by default, `--retries` to change; see [Retries](#retries)), or a `Retry-After` longer than what is left of the retry budget; `details.attempts` says how many times the request was sent. A 429 from the userinfo or revoke endpoint exits at once |
13
13
  | 6 | `credit_limit_exceeded`, `user_credit_limit_exceeded` | the server refused the call: your organization's credit allowance (`credit_limit_exceeded`) or your personal credit budget (`user_credit_limit_exceeded`) is used up. Retrying will not help; ask an admin, or wait for the next billing period |
14
- | 7 | `server_unreachable`, `governance_unavailable` | `governance_unavailable`: the server could not check your budget, so nothing ran (`details.retryable: true`, `details.outcome_unknown: false`); retry shortly. `hgi` never retries it itself. `server_unreachable`: DNS failure, refused connection, timeout, HTTP 5xx, an unexpected redirect from the MCP endpoint, the server shedding load after retries, or the server choosing an MCP protocol version this `hgi` does not speak (`details.reason` is `unsupported_protocol_version`; upgrade `hgi`) |
14
+ | 7 | `server_unreachable`, `governance_unavailable` | `governance_unavailable`: the server could not check your budget, so nothing ran (`details.retryable: true`, `details.outcome_unknown: false`); retry shortly. `hgi call --retries <n>` re-sends it up to n times itself (then `details.attempts` is set); without `--retries`, and always on `hgi run`, it is not retried. `server_unreachable`: DNS failure, refused connection, timeout, HTTP 5xx, an unexpected redirect from the MCP endpoint, the server shedding load after retries (`details.attempts`), or the server choosing an MCP protocol version this `hgi` does not speak (`details.reason` is `unsupported_protocol_version`; upgrade `hgi`) |
15
15
  | 8 | `oauth_error` | discovery or issuer mismatch, state or redirect mismatch, consent denied, a rejected or reused sign-in code, a token-endpoint failure, an unexpected redirect on an auth request |
16
16
  | 9 | `network_proxy_tls` | TLS verification failure or proxy failure; the message names the cause (for example `DEPTH_ZERO_SELF_SIGNED_CERT`) |
17
17
  | 10 | `wrong_verb` | `hgi call` on a state-changing tool, or `hgi run` on a read-only one. Nothing is sent |
@@ -23,7 +23,7 @@ Every failure has its own exit code, so a script can branch without parsing Engl
23
23
 
24
24
  ## The error body
25
25
 
26
- When stderr is not a terminal (or you pass `--json-errors`), errors are one JSON object on stderr and nothing on stdout. There are two exceptions: exit 12 with `details.tool_ran: true`, where the tool already ran and its result is printed on stdout, and `hgi call --ids`, where the rows printed before the error stay on stdout ([below](#partial-output-from---ids)):
26
+ When stderr is not a terminal (or you pass `--json-errors`), errors are one JSON object on stderr and nothing on stdout. There are three exceptions: exit 12 with `details.tool_ran: true`, where the tool already ran and its result is printed on stdout; `hgi call --ids`, where the rows printed before the error stay on stdout ([below](#partial-output-from---ids)); and `hgi call --all-pages` with `details.partial_result: true`, where a later page failed and the merged result of the pages before it is printed where a full result would go, stdout or `--out` ([below](#partial-output-from---all-pages)):
27
27
 
28
28
  ```json
29
29
  {"schema":1,"error":{"code":"credit_limit_exceeded","exit_code":6,"message":"...","hint":"...","details":{"tool":"company_enrich"}}}
@@ -33,15 +33,17 @@ When stderr is not a terminal (or you pass `--json-errors`), errors are one JSON
33
33
  - `code` and `exit_code` are stable. `message` and `hint` are for humans.
34
34
  - `details` carries machine-readable context where it exists:
35
35
  - `retry_after_seconds` on `rate_limited`
36
+ - `attempts` when retries ran out (`rate_limited`, an exhausted load-shed or refused connection, `governance_unavailable` on `hgi call --retries`): how many times the failing request was sent, 1 + its retries
36
37
  - `outcome_unknown` on `tools/call` failures (see below)
37
38
  - `expected_verb` on `wrong_verb`
38
- - `reason` on `invalid_input` and `oauth_error` (for example `unknown_tool`, `schema`, `state_mismatch`)
39
+ - `reason` on `invalid_input` and `oauth_error` (for example `unknown_tool`, `schema`, `state_mismatch`, `bad_retries`, `bad_retry_budget`)
39
40
  - `cause` on `server_unreachable` and `network_proxy_tls` (a Node error code)
40
41
  - `location_host` on an unexpected redirect
41
42
  - `tool` and `server_error_code` on every error from a tool result (`server_error_code` is the server's raw `_meta.errorCode`, when it sent one; an unknown one stays `tool_error`)
42
43
  - `retryable` on `governance_unavailable`: nothing ran, and retrying later is safe
43
44
  - `budget_write_failed: true` on a server refusal (exit 3, 4, 6, 7 or 11) whose spend could not be recorded under a run cap; the refusal is still the error to act on
44
45
  - `spent`, `max` and `budget_id` on `budget_exceeded`
46
+ - `partial_result: true` and `pages_fetched` (the pages requested, the failed one included; a re-sent page counts once) when `--all-pages` stopped after page 1; `out_write_failed: true` when the partial result could not be written to `--out` and went to stdout instead
45
47
  - `kind` on `local_state_error`: `credentials_corrupt`, `credentials_unsafe`, `lock_timeout`, `lock_compromised`, `write_failed`, `budget_corrupt`, `budget_write_failed`, `ledger_unsafe` or `read_failed`; with `tool_ran` when the tool already ran
46
48
 
47
49
  On a terminal, errors are plain text with a hint.
@@ -71,10 +73,54 @@ Exit 2 refusals specific to `--ids`, each before anything is sent (`details.reas
71
73
  | `unusable_max_items` | the tool declares a `maxItems` that is not a positive whole number, so it cannot be batched safely |
72
74
  | `schema` | a batch fails the tool's input schema; `details.batch` says which. Every batch is checked before the first is sent |
73
75
 
76
+ ## Partial output from `--all-pages`
77
+
78
+ `hgi call --all-pages` fetches pages one at a time and prints one merged result ([output formats](output-formats.md#hgi-call---all-pages)). How a run ends:
79
+
80
+ - **0**: paging finished. Parts that stopped at `--max-pages` or could not be tied to one input are marked `truncated: true` (`truncated_reason` `max_pages` or `unmatched`); that is not an error.
81
+ - **A failure on page 1** is exactly the failure of `hgi call` without `--all-pages`: nothing on stdout.
82
+ - **A failure on any later page** (tool error, out of credits, rate limited or `governance_unavailable` after its retries, unreachable, the run cap, a follow-up that fails the input schema, any code) stops all paging; nothing is retried. The merged result of the pages so far is written where a full result would go (stdout, or `--out`; if `--out` fails it goes to stdout and `details.out_write_failed` is `true`), with every part that did not finish marked `truncated: true, truncated_reason: "<error.code>"`. `hgi` then exits with the failing call's own code, and `details` adds `partial_result: true` and `pages_fetched`. Keep what was printed: it was billed.
83
+ - A later page that is not an object, or has no rows array where the schema says one is, is exit 3 `tool_error` with `details.reason: "unexpected_response"`, handled as above.
84
+ - **12 `budget_write_failed` with `details.tool_ran: true`**: a page ran but its spend could not be recorded under a run cap. On page 1 this is exactly the single-call case (page 1 on stdout, no paging fields). On a later page, paging stops and the merged result so far is on stdout (even with `--out`), with `partial_result` and `pages_fetched`. Do not re-run it.
85
+
86
+ Exit 2 refusals specific to `--all-pages`, each before anything is sent (`details.reason`):
87
+
88
+ | `reason` | When |
89
+ |---|---|
90
+ | `not_pageable` | the tool's schemas don't show it can be paged safely: no integer `offset` input, or no output object with exactly one rows array plus `has_more`/`hasMore` or a total (`details.tool`) |
91
+ | `max_pages_without_all_pages` | `--max-pages` without `--all-pages` |
92
+ | `bad_max_pages` | `--max-pages` is not a whole number of at least 1 |
93
+ | `offset_with_all_pages` | `--input` already has `offset`; `--all-pages` sets it |
94
+ | `all_pages_with_ids` | `--all-pages` together with `--ids` |
95
+
74
96
  ## `outcome_unknown`
75
97
 
76
98
  `hgi` retries only failures that happen **before the server runs the tool**: a 401 (after a refresh), a 429, the server's 503 "shedding load" response, and a refused or unresolvable connection. If a connection drops, times out, a proxy resets it, or a 5xx arrives **after** a `tools/call` was sent, it is not retried, because `hgi run` could start the work twice. The error then has `"outcome_unknown": true` and a hint to check state first. This marker is added to every network-class or unclassified failure once the call was sent, so it can appear with exit 1, 7 or 9. An exhausted load-shed retry has `"outcome_unknown": false`: nothing ran and retrying is safe.
77
99
 
100
+ A failure with `"outcome_unknown": true` is never retried, whatever `--retries` says. Neither is a TLS or proxy failure (exit 9: waiting does not fix it) or any exit 2, 3, 4, 6, 8, 10, 11, 12 or 13.
101
+
102
+ ### Retries
103
+
104
+ What `hgi call` and `hgi run` retry, all before the tool runs:
105
+
106
+ | `code` in the retry line | What | Default limit | Wait |
107
+ |---|---|---|---|
108
+ | `rate_limited` | a 429 | 3, shared with `load_shed` | `Retry-After`, else 2, 4, 8 … s (at most 30) plus up to 1 s |
109
+ | `load_shed` | the server's 503 "shedding load" (503 + `Retry-After` + JSON-RPC `-32000`) | 3, shared with `rate_limited` | `Retry-After` |
110
+ | `not_established` | a connection that was refused or did not resolve before the request left; on requests other than `tools/call`, also other network failures and 502/503/504 | 2 | 0.5 s × attempt |
111
+ | `governance_unavailable` | the server could not check the budget (an in-band refusal raised before the tool runs) | not retried | full jitter: random in `[0, min(2^attempt, 30)]` s |
112
+
113
+ `rate_limited` and `load_shed` waits get up to 0.25 s of extra jitter, which the budget check does not count. Every wait is bounded by a **retry budget per request** (each `--ids` batch call is its own request): a wait longer than what is left of it ends the retries at once with the final error. The 401 → refresh → retry-once path is separate and is not counted.
114
+
115
+ - `--retries <n>` (or `HGI_RETRIES`; the flag wins), a whole number 0–20: each class above may retry up to n times, each on its own counter. `0` turns every retry off, built-in ones included. On `hgi call` it also re-sends `governance_unavailable` up to n times; `hgi run` never does (exit 7, `retryable: true`, and you decide).
116
+ - `--retry-budget <seconds>` (or `HGI_RETRY_BUDGET`; the flag wins), a positive number up to 3600: the per-request budget. Default 600 when retries are set, else 60. Without `--retries` it raises the budget and keeps the built-in counts.
117
+ - A bad value exits 2 `invalid_input` (`details.reason` `bad_retries` or `bad_retry_budget`) before credentials are read or anything is sent.
118
+ - `--timeout` bounds each attempt, not the waits between them.
119
+ - Each `governance_unavailable` attempt that came back is recorded like a single call: one credit-ledger row (`status: "tool_error"`) and, under a [run cap](#run-cap), its reported cost. The cap is checked again right before every re-send; when it is reached, the re-send is not sent and the command exits 13. Refused attempts are not billed, so `credit_cost` is the last attempt's.
120
+ - Every retry prints one line on stderr before its wait ([output formats](output-formats.md#retry-lines)).
121
+
122
+ `governance_unavailable` is the one in-band refusal `hgi` re-sends, because the server raises it before the tool runs (see `CONTRIBUTING.md` → Ground rules), and only on the read-only verb.
123
+
78
124
  ## Credit cost
79
125
 
80
126
  Success prints the tool result on stdout and the credit cost on stderr (`credit_cost: 3`, or `{"credit_cost":3}` when stderr is not a terminal). Tools that do not report a cost (external, aggregated tools) show `unknown` / `null`, never `0`.
@@ -86,6 +132,7 @@ Success prints the tool result on stdout and the credit cost on stderr (`credit_
86
132
  - The cap is **client-side only, on top of the server's limits**. The organization and personal limits still apply, and are enforced by the server.
87
133
  - It covers `hgi call` only. `hgi run` has no `--max-credits`, ignores `HGI_MAX_CREDITS`, and is neither checked nor counted.
88
134
  - Before each capped call, `hgi` refuses with exit 13 when the budget has already spent its cap, and sends nothing. After the call, it adds the cost the server reported (`_meta.creditCost`), for error results too. A tool that reports no cost counts as 0 and is counted in `unknown_cost_calls`.
135
+ - With `--all-pages`, every page is a capped call: checked before it is sent and counted after it. When the cap is reached partway, the next page is not sent, the pages so far are printed, and `hgi` exits 13 with `details.partial_result: true` ([above](#partial-output-from---all-pages)).
89
136
  - With `--ids`, every batch call is a capped call: checked before it is sent and counted after it. When the cap is reached partway, the run stops with exit 13 and the rows so far stay printed, with `rows_emitted` and `calls_made` as in [Partial output from `--ids`](#partial-output-from---ids). When a batch's spend cannot be recorded, its rows are printed first (to `--out` when given), then the run stops with exit 12 `budget_write_failed` and `details.tool_ran: true`.
90
137
  - The server gives no price before a call, so a run can overshoot its cap by the calls running at the same time: one call when calls run one after another, more when they run in parallel.
91
138
  - Spend is recorded only when a cap is set. It lives in `~/.config/hgi/budgets/` (see [security](security.md)); a file not updated for 7 days is deleted on the next budget write.
@@ -39,10 +39,26 @@ Results are never truncated. A large result prints complete in every format. Use
39
39
  `--meta` replaces the bare result on stdout with an envelope:
40
40
 
41
41
  ```json
42
- {"result": {...}, "credit_cost": 2, "mcp_version": "v2"}
42
+ {"result": {...}, "credit_cost": 2, "mcp_version": "v2", "attempts": 1}
43
43
  ```
44
44
 
45
- `--select` trims the `result` inside the envelope, so `--meta --select name` gives `{"result": {"name": ...}, "credit_cost": ..., "mcp_version": ...}`.
45
+ `attempts` is how many times the `tools/call` was sent: 1, plus every [retry](exit-codes.md#retries) of it. `--select` trims the `result` inside the envelope, so `--meta --select name` gives `{"result": {"name": ...}, "credit_cost": ..., "mcp_version": ..., "attempts": ...}`.
46
+
47
+ ## Retry lines
48
+
49
+ Every retry writes one line on stderr before its wait, with or without `--retries`. On a terminal:
50
+
51
+ ```
52
+ hgi: retry 2/6: rate_limited, waiting 12s
53
+ ```
54
+
55
+ Otherwise (stderr not a terminal, or `--json-errors`), the same as one JSON object:
56
+
57
+ ```json
58
+ {"retry":{"attempt":2,"max":6,"code":"rate_limited","wait_seconds":12}}
59
+ ```
60
+
61
+ `code` is `rate_limited`, `load_shed`, `not_established` or `governance_unavailable`. `max` is that class's limit (`--retries`, else the built-in 3 or 2). `wait_seconds` is the wait that follows, a number rounded to one decimal place. The lines go to stderr only, so stdout stays the result.
46
62
 
47
63
  ## `hgi call --ids` (batches)
48
64
 
@@ -58,10 +74,46 @@ With `--ids`, `hgi call` prints **one row per input line, in input order**, inst
58
74
  - `error.code` is `not_found` (the server left the input out), `unexpected_response` (the response has no array to match against), or the code of the failed batch call (for example `tool_error`). Error messages are redacted like every other `hgi` error.
59
75
  - **The default format is JSONL, on a terminal or not.** JSONL rows stream as each batch returns. `-f json` prints one array of rows; `-f csv|yaml|table` format the list of rows. Those, and `--out`, are written once when the run ends (or, on a fatal stop, with the rows so far).
60
76
  - `--select` trims each row's `result`; `input` and `error` are always kept.
61
- - **`--meta` does not wrap stdout** with `--ids`. stdout stays pure rows, and `--meta` adds one summary object on stderr at the end: `{"calls", "rows", "row_errors", "credit_cost", "calls_without_cost"}`. `credit_cost` sums the costs the calls reported; `calls_without_cost` counts calls that reported none, so the sum is a lower bound.
77
+ - **`--meta` does not wrap stdout** with `--ids`. stdout stays pure rows, and `--meta` adds one summary object on stderr at the end: `{"calls", "retries", "rows", "row_errors", "credit_cost", "calls_without_cost"}`. `calls` counts batch calls, one per batch however many times it was sent; `retries` is the total of re-sends across them. `credit_cost` sums the costs the calls reported (for a re-sent batch, its last attempt's); `calls_without_cost` counts calls that reported none, so the sum is a lower bound.
62
78
  - The credit line on stderr is printed once, with the total, at the end (and before a fatal error).
63
79
  - On a terminal, progress (`hgi: batch 3/40`) goes to stderr. It is never printed when stderr is not a terminal.
64
80
 
81
+ ## `hgi call --all-pages`
82
+
83
+ `--all-pages` fetches every page of an offset-paged tool, one call at a time, and prints **one merged result in the server's shape**, as if the server had returned every row at once. `--max-pages <n>` caps the pages per page object (default 10, page 1 included). **Each extra page is a separate, billed call.**
84
+
85
+ Which tools can be paged is read from the tool's schemas, never its name:
86
+
87
+ - the input schema has an `offset` property of type `integer`;
88
+ - the output has **page objects**: the result itself if it qualifies, otherwise each item of its one top-level array whose items qualify (`companies[]` on batch tools). A page object has exactly one array-of-objects property (its rows), plus `has_more` or `hasMore`, a total, or both;
89
+ - the **total** is a numeric `total`, `total_count` or `totalCount`; otherwise the one numeric `total_*` property whose `_`-separated name has the rows property's name as a whole segment (`installs` → `total_installs_count`). None, or more than one, means no total. `count` is never a total;
90
+ - the **page size** is the `limit` or `max_results` value in `--input`, else that property's schema `default`, else unknown. A tool declaring both has no known page size.
91
+
92
+ Anything else is refused before anything is sent (exit 2, `details.reason: "not_pageable"`).
93
+
94
+ **Stop rule**, per page object, after each page: stop when `has_more`/`hasMore` is `false`, the page has no rows, the page has fewer rows than a known page size, the rows so far reach the total, or `--max-pages` pages were fetched for it. `has_more: true` alone never keeps paging past a short page, an empty page or the total. Otherwise the next page is the original input plus `offset` = rows so far.
95
+
96
+ **Batch tools.** Page 1 is the call as given. Follow-ups go only for companies that are not finished, because `offset` applies to every company in a call:
97
+
98
+ - with two or more values across `hg_ids` and `domains`, one call per company: the original input with `hg_ids`/`domains` replaced by that company's value as you wrote it (`{"hg_ids": ["<id>"], "offset": n}`), other filters kept. Entries are matched like `--ids` rows: `company_id`, else `requested_hg_id`, else `organization_id` for ids (lowercased, leading zeros removed), `company_domain` for domains (lowercased, no scheme, `www.` or path);
99
+ - otherwise, when page 1 returned exactly one entry (the company the input named, if it named one), the original input plus `offset`; every follow-up must again return exactly that one company;
100
+ - an entry that can't be tied to exactly one input, a page 1 with several entries and nothing to split on, or a follow-up that doesn't return exactly that one company, is not followed up and is marked `unmatched`. hgi never guesses which company a page belongs to.
101
+
102
+ **The merged result.** Rows are appended to each page object's rows array. Its other fields (`has_more`, counts) are those of the last page fetched for it; for batch tools the top level is page 1's. Each page object gains:
103
+
104
+ - `pages_fetched`: the pages that contributed to it;
105
+ - `truncated: true` and `truncated_reason` when it did not finish: `max_pages`, `unmatched`, or the `error.code` of the later page that failed (see [exit codes](exit-codes.md#partial-output-from---all-pages)).
106
+
107
+ `--select`, `-f` and `--out` apply to the merged result, also when a later page failed and a partial result is printed. The credit line on stderr is printed once, with the sum of the reported costs (`null`/`unknown` when no page reported one). With `--meta` the envelope adds `pages_fetched` (the pages requested, the failed one included: one per page however many times it was sent), `retries` (the re-sends across all pages, replacing a single call's `attempts`) and `calls_without_cost` (pages that reported no cost, so `credit_cost`, the sum, is a lower bound; a re-sent page counts its last attempt's cost):
108
+
109
+ ```json
110
+ {"result": {...}, "credit_cost": 6, "mcp_version": "v2", "pages_fetched": 3, "retries": 0, "calls_without_cost": 0}
111
+ ```
112
+
113
+ With `--retries`, each page gets the same retries as a single `hgi call`, `governance_unavailable` re-sends included; a page whose retries run out stops paging like any later-page failure, with `details.attempts`.
114
+
115
+ Without `--all-pages`, `--meta` is unchanged. `--all-pages` is not available with `--ids` or on `hgi run`.
116
+
65
117
  ## `hgi tools list`
66
118
 
67
119
  `hgi tools list --json` prints `{base_url, mcp_version, server, fetched_at, from_cache, count, tools: [...]}`. Each tool has `name`, `verb` (`call` or `run`), `read_only` (`true`, `false` or `null` when the server gave no annotation), `description`, `required`, `parameters` and the full `input_schema`. The list is cached for 15 minutes per server, organization and user; `--refresh` asks the server. The served MCP version comes from the server (`X-MCP-Version`).
package/docs/security.md CHANGED
@@ -40,11 +40,11 @@
40
40
 
41
41
  - `hgi call` only runs tools the server marks `readOnlyHint: true`, decided against a fresh tool list on every invocation. `hgi run` runs everything else, including tools with no annotations. There is no `--yes` flag, so a host permission rule on `hgi call *` approves only lookups.
42
42
  - `--out <file>` never overwrites an existing file on `hgi call`, never follows symlinks, and refuses paths inside hgi's config and cache directories. `--input @file` and `--ids @file` refuse hgi's own credential and cache files (including through symlinks and hard links), and read only regular files. An allowlisted agent can still write a **new** file or send any other file it can read to HG Insights as tool input; scope your allowlist accordingly.
43
- - Credit cost is reported after each call (with `--ids`, once for the whole run) and recorded in the local [credit ledger](#the-credit-ledger), one line per call (with `--ids`, one per batch call). Credits are billed to your organization. `--ids` sends batches one at a time and stops at the first fatal error, so a run that hits a credit limit or an expired sign-in does not keep sending.
43
+ - Credit cost is reported after each call (with `--ids` or `--all-pages`, once for the whole run, as the sum) and recorded in the local [credit ledger](#the-credit-ledger), one line per call (with `--ids`, one per batch call; with `--all-pages`, one per page), and one per attempt when `hgi call --retries` re-sends a `governance_unavailable` refusal (each attempt also counts against a run cap). Credits are billed to your organization. `--ids` sends batches one at a time and `--all-pages` sends pages one at a time; both stop at the first fatal error, so a run that hits a credit limit or an expired sign-in does not keep sending. Each extra page of `--all-pages` is a separate, billed call, capped by `--max-pages` (default 10 per page object).
44
44
 
45
45
  ## The credit ledger
46
46
 
47
- `hgi call` and `hgi run` append one line per tool call that got a result back to a local ledger, which `hgi usage` summarizes. It is the one local write a lookup (`hgi call`) makes.
47
+ `hgi call` and `hgi run` append one line per tool call that got a result back (a re-sent `governance_unavailable` refusal counts once per attempt) to a local ledger, which `hgi usage` summarizes. It is the one local write a lookup (`hgi call`) makes.
48
48
 
49
49
  - **What it holds:** the time, the server host, `call`/`run`, the tool name, your `--label`, the credit cost, `HGI_BUDGET_ID`, the MCP version and `ok`/`tool_error`. Never the tool input, the result, your user or organization, or any token: every text field is passed through the same redaction as error output, and the server-supplied MCP version is kept only when it is a short plain version string. See [output formats](output-formats.md#the-ledger-record) for the record format.
50
50
  - **Where:** `~/.config/hgi/ledger.jsonl` (or `$XDG_CONFIG_HOME/hgi`, `HGI_CONFIG_DIR`). `HGI_LEDGER=<path>` moves it; a path inside a git working tree (as given, or after following symlinks) is refused before anything is sent. If you keep `~/.config` in a dotfiles repository, exclude `hgi/` from it: the ledger records which tools you used and when.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@hginsights/hgi",
3
- "version": "0.2.0-rc.1",
3
+ "version": "0.2.0",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@hginsights/hgi",
9
- "version": "0.2.0-rc.1",
9
+ "version": "0.2.0",
10
10
  "license": "MIT",
11
11
  "os": [
12
12
  "darwin",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hginsights/hgi",
3
- "version": "0.2.0-rc.1",
3
+ "version": "0.2.0",
4
4
  "description": "Command-line client for the HG Insights MCP server, built for AI coding agents and GTM engineers.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -38,6 +38,7 @@ hgi call company_enrich --input '{"domain":"acme.com"}' # lookup, JSON on stdout
38
38
  hgi call search_companies --input '{"query":"saas"}' --select name,domain -f csv
39
39
  hgi call <tool> --input '{"limit":100}' --out result.json # big result to a new file
40
40
  hgi call <tool> --ids @hg_ids.txt --input '{...}' # a whole list: one JSONL row per line
41
+ hgi call <tool> --all-pages --input '{...}' # every page of an offset-paged tool, merged
41
42
  hgi run <tool> --input '{...}' # only after the user approves
42
43
  ```
43
44
 
@@ -50,6 +51,16 @@ hgi run <tool> --input '{...}' # only after the user a
50
51
  - A `{"warning":"ledger_not_updated",...}` line on stderr means the call still succeeded; do not re-run it.
51
52
  - For more than one company, use `--ids @file` (one HG id or domain per line; `--ids -` for stdin; `--ids-column <name>` for a CSV; `--batch-size <n>` to send fewer per call than the tool's limit) instead of looping. hgi batches by the tool's limit, sends batches one at a time, and prints `{"input", "result"}` or `{"input", "error"}` rows as JSONL in input order. Exit 3 means a batch failed (its rows carry the error, the rest are fine); any other non-zero exit stopped the run, and `details.rows_emitted` / `calls_made` say how far it got: keep the rows already printed and resume from there.
52
53
 
54
+ ### Paging (`--all-pages`)
55
+
56
+ For every row of an offset-paged tool, use `hgi call <tool> --all-pages` instead of an `offset` loop. hgi pages only tools whose schemas show it is safe (an integer `offset` input and a rows array with `has_more` or a total); anything else is refused with exit 2 `not_pageable`, nothing sent. Don't pass `offset` yourself.
57
+
58
+ - **Each extra page is a separate, billed call.** For batch tools (`hg_ids`/`domains`), follow-ups go one call per unfinished company.
59
+ - It stops on `has_more: false`, an empty or short page, or the total reached, never on `has_more: true` alone (the server can say `true` after the last row). `--max-pages <n>` caps pages per page object (default 10, page 1 included).
60
+ - The result keeps the server's shape. Each page object (the result, or each `companies[]` entry) gets `pages_fetched`; one cut short says `truncated: true` and `truncated_reason`: `max_pages`, `unmatched` (a batch entry hgi could not tie to one input, so it never guesses) or the code of the call that failed. Report truncated parts as incomplete.
61
+ - One credit line at the end with the sum. `--meta` adds `pages_fetched` (pages requested), `retries` (re-sends) and `calls_without_cost`.
62
+ - Not with `--ids` (exit 2 `all_pages_with_ids`): page one company list at a time.
63
+
53
64
  ## Recipes
54
65
 
55
66
  These recipes name tools as examples. Before each one:
@@ -59,7 +70,7 @@ These recipes name tools as examples. Before each one:
59
70
  - **Keep batches stable.** The server caches identical inputs for 30 days (7 for search), and a cached call costs nothing (observed). Sort ids and keep the same batches, so a re-run is free. Changing one id in a batch makes the whole batch a new input.
60
71
  - **Missing is not zero.** No installs, no intent, or a company left out of a batch response means unknown, not zero. Record it as unknown; don't score the account down.
61
72
  - **Show a plan first** for more than one batch (Rule 7).
62
- - Paging and batching are manual for now: a hand-written page loop and sorted id batches, as below.
73
+ - Batching is manual for now: sorted id batches, as below. Paging is `--all-pages` where the tool allows it (`not_pageable` otherwise).
63
74
 
64
75
  ### Match an account list
65
76
 
@@ -82,24 +93,16 @@ hgi call company_firmographic --input '{"hg_ids":["<id1>","<id2>"]}' --select co
82
93
  **Batch:** read `maxItems` on `hg_ids` in `company_technographic`'s `input_schema`, and the maximum of `max_results` and `offset`. `company_intent` takes one company per call.
83
94
  **Order:**
84
95
 
85
- 1. **Fit:** `company_technographic` on a sorted batch of `hg_ids`, filtered (`vendor_names`, `product_names` or `category_ids`) so pages stay small. Page with `offset`. Stop on a short page (fewer rows than `max_results`), once `total_installs_count` is reached, or at the schema's `offset` maximum. Don't stop on `has_more` alone: it can be wrong at page boundaries. At the `offset` maximum, report the result as partial and narrow the filter.
96
+ 1. **Fit:** `company_technographic` on a batch of `hg_ids`, filtered (`vendor_names`, `product_names` or `category_ids`) so pages stay small, with `--all-pages` (above). It pages each company on its own and stops on a short page or `total_installs_count`, not on `has_more` alone (it can be wrong at page boundaries). A company with `truncated: true` is partial: say so, and narrow the filter rather than raising `--max-pages`.
86
97
  2. **Topics:** resolve curated topic names to ids with `list_intent_topics`.
87
98
  3. **Intent:** `company_intent` on the company's web domain (`company_domain`), not only its HQ id: intent is observed on web domains. In one run, Accor had 0 topics on its HQ id and 18 on `accor.com` (observed; counts change over time). Check `company.id` and `company.name` in the response: a domain can resolve to a subsidiary.
88
99
  4. **Baseline:** large companies are active on about 70% of the ~20,000-topic catalogue (observed), so a raw topic count measures size, not interest. Compare the curated topics (`topic_ids`) with the company's own unfiltered summary. A summary with `topic_ids` counts only those topics. `fields:["summary"]` without a topic filter rejects `granularity` (422). When you request `fields:["topics"]`, pass `granularity:"global"`: the default is one row per state, which counts a topic several times.
89
100
 
90
101
  ```bash
91
- # technographics page loop (needs jq); page size and offset maximum come from the schema
92
- dir=$(mktemp -d) # a fresh directory per run, so concurrent runs never share files
93
- schema=$(hgi tools list --json | jq '.tools[] | select(.name=="company_technographic") | .input_schema.properties')
94
- max=$(jq '.max_results.maximum' <<<"$schema"); maxoff=$(jq '.offset.maximum' <<<"$schema")
95
- ids='["<id1>","<id2>"]'; off=0 # one sorted batch of at most hg_ids.maxItems ids
96
- while [ "$ids" != "[]" ]; do
97
- if [ "$off" -gt "$maxoff" ]; then echo "partial: offset maximum reached for $ids" >&2; break; fi
98
- hgi call company_technographic --input "{\"hg_ids\":$ids,\"vendor_names\":[\"<vendor>\"],\"max_results\":$max,\"offset\":$off}" -f json > "$dir/tech-$off.json" || break
99
- # next page only for companies that filled this page and are short of their stated total, sorted
100
- ids=$(jq -c --argjson max "$max" --argjson off "$off" '[.companies[] | select(.installs_count == $max and $off + $max < .total_installs_count) | .company_id] | sort' "$dir/tech-$off.json")
101
- off=$((off + max))
102
- done
102
+ # every install per company, paged safely; max_results up to the schema's maximum keeps the call count down
103
+ hgi call company_technographic --all-pages --max-pages 5 \
104
+ --input '{"hg_ids":["<id1>","<id2>"],"vendor_names":["<vendor>"],"max_results":<schema maximum>}' -f json > tech.json
105
+ # exit 0 with truncated companies = capped; non-zero with details.partial_result = stopped early, tech.json still holds what was paid for
103
106
  hgi call company_intent --input '{"company_domain":"accor.com"}'
104
107
  hgi call company_intent --input '{"company_domain":"accor.com","fields":["summary"],"topic_ids":["<topic id>"]}'
105
108
  ```
@@ -113,7 +116,7 @@ hgi call company_intent --input '{"company_domain":"accor.com","fields":["summar
113
116
  1. Use the `hg_id`s from the matching recipe, sorted.
114
117
  2. To size one area, resolve its category ids first with `get_product_category`, as `company_spend`'s description says. Don't pass guessed `category_names`: a name that matches nothing returns 0 rows without an error.
115
118
  3. One `company_spend` call per batch of `hg_ids`, with `category_ids` set in that same call, not filtered afterwards.
116
- 4. Page with `max_results` and `offset` only when `all_count` exceeds the rows returned, with the same stop rule as technographics.
119
+ 4. `company_spend` is not pageable with `--all-pages` (its per-company spend has no rows array). Page with `max_results` and `offset` by hand only when `all_count` exceeds the rows returned; stop on a short page or once `all_count` is reached, never on a has-more flag alone.
117
120
  5. Rows are per country, and categories form a tree through `category_parent_id` (Total IT → Total External IT → Software, …). Sum across countries, never a parent with its children.
118
121
 
119
122
  ```bash
@@ -152,15 +155,16 @@ Not null, but easy to misread:
152
155
  | 3 | the tool reported an error | read the message; do not blindly retry |
153
156
  | 4 | not signed in, or login expired | ask the user to run `hgi auth login` |
154
157
  | 4 `phoenix_account_required` | the signed-in identity has no Phoenix account | stop; tell the user to sign up, or to sign in with `hgi auth login` as an account that has one |
155
- | 5 | rate limited | wait `retry_after_seconds` from the error, then retry |
158
+ | 5 | rate limited (after hgi's own retries) | wait `retry_after_seconds` from the error, then retry. For a long batch of lookups, pass `--retries 6` instead of writing your own retry loop |
156
159
  | 6 | out of credits: `credit_limit_exceeded` (the organization) or `user_credit_limit_exceeded` (the user's personal budget) | stop; tell the user which one, and to ask an admin. Retrying will not help |
157
160
  | 7 | server unreachable | retry later. If `outcome_unknown` is `true`, check state before re-running a `run` |
158
- | 7 `governance_unavailable` | the server could not check the budget; nothing ran (`retryable: true`) | wait a little, then retry once; hgi never retries it itself |
161
+ | 7 `governance_unavailable` | the server could not check the budget; nothing ran (`retryable: true`) | wait a little, then retry once. `hgi call --retries <n>` retries it itself; `hgi run` never does |
159
162
  | 8 | OAuth or sign-in failure | ask the user to run `hgi auth login` again |
160
163
  | 9 | proxy or TLS problem | the error names the cause (see `NODE_EXTRA_CA_CERTS`, `HTTPS_PROXY`). After a `run`, check `outcome_unknown` before retrying |
161
164
  | 10 | wrong verb | use the verb the error names |
162
165
  | 11 | forbidden, or `tool_not_enabled_for_team` | the account, organization or team lacks access to this tool; stop and tell the user to ask an admin |
163
166
  | 12 | local credential or file problem | follow the hint. If `details.tool_ran` is `true`, the tool already ran and its result is on stdout: do not re-run it |
167
+ | any, with `details.partial_result: true` | `--all-pages` stopped on a later page; the pages fetched so far are on stdout (or `--out`), unfinished parts marked `truncated` | keep them, they were billed; then act on the code as its own row says. Don't re-fetch pages you already have |
164
168
  | 13 `budget_exceeded` | the run spent its `--max-credits` cap; nothing more was sent (with `--ids`, keep the rows already printed) | stop; tell the user what was spent (`details.spent` of `details.max`). Only the user raises the cap or runs `hgi budget reset` |
165
169
 
166
170
  Errors are JSON on stderr when it is not a terminal: `{"schema":1,"error":{"code","exit_code","message","hint","details"}}`. Branch on `error.code`, not on the message.