Skip to content

Local-first 🧠 memory for Hermes Agent that lives in your Obsidian vault and remembers project context. Nightly 😴 dream passes turn repeat corrections into confirmed preferences with measurable confidence. Adapters ship for Claude Code, Codex, and OpenClaw, with an MCP server for anything else.

TypeScript

fix(decision-models): request floor per chain attempt, recall brief wait inside the hook ceiling (#311)

Serg Eroshe5e0aba2889ea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 65908489..298e2814 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **`osb://topic/{slug}` is now `osb://rule-topic/{slug}`.** The resource that summarises the signals and the rule of a preference slug moved to its new name, and `osb://topic/{id}` serves topics; the old name still answers a slug for one minor version. Code that names the preference slug says `ruleTopic` now; the `topic` field of signals keeps its name.
- **Topics in the brain store and the human view (first part).** A topic is one area of work or one personal matter with an id that never changes: its title, description, status, goal, milestones, decisions, open questions, log, links, merges, splits and record assignments are records of the `topics` stream (`topic-create`, `topic-rename`, `topic-status`, `topic-goal`, `decision`, `open-question`, `question-resolved`, `log-entry`, `milestone`, `milestone-done`, `assign`, `unassign`, `merge`, `split`, `link`, `undo`, documented in `FORMAT.md`), folded the same way on every machine, with your records beating an agent's and an agent's beating a decision model's. A rename keeps the id and every reference, an undo voids the record it names and keeps a route you took back from being taken again, a milestone is done only when you mark it or an agent cites evidence, and a personal topic goes to the `private` partition by default. The view writer renders each shared topic to `topics/<date>-<slug>/topic.md` in a fixed section order (title, description, status, goal, milestones, decisions, open questions, the log newest first, related), moves the oldest entries over the page budget to a companion `archive.md`, shows only earned log entries, and moves the folder, your companion files included, when the topic is renamed. Your edits of a topic page become topic records with your authority, so a goal you rewrite stays even when an agent later proposes another, which then shows as an open question. Research reports and source summaries render to `reports/` and `sources/`. A note tool refuses a topic, report or source page of the view and names the tool to use instead; with the view at the vault root, your own notes in folders of those names stay writable. The view writer only ever touches a page of the exact shapes it writes, inside its view folder and never through a symbolic link; a folder at a topic page's name is reported as refused while the rest of the view renders, and a companion file the writer moves is never left in both folders, also when a sync tool replaces it mid-move or Windows refuses to remove it. Undoing an automatic route or taking a record out of a topic blocks that route whether you or an agent wrote the undo.
- **Brain store hardening.** Readers that show log events, or numbers derived from them, to a caller below local reach now withhold events of the `private` partition, and a census test fails on any new log reader that has not been classified. `brain_doctor` names each shard that reads only in part by kind, offset and repair command, and `o2b store shard quarantine|list|release` skips one bad line on this machine without touching the shard (a line of a newer format is never skipped, a pending blob only after seven days and never in `settings`). `o2b store partition leave <partition>` takes a partition off this machine on purpose, and the error that names a lost partition names the verb. A `lane-entry` record keeps the file's text in an optional `raw` field when its fields and text do not reproduce it, so a lane file written as text reads back byte for byte, also after a rollback; the format version does not change.
-- **Nine decision-model uses for topics and agent context.** With the decision-model feature on, the dream pass can refine what the topic planner does on its own (`topic_route`, `event_kind`, `human_readable`, `topic_new`, `topic_same`, `question_resolved`, `milestone_done`), and two new uses pick what an agent sees (`head_select`, `neighbour_relevant`). Every use is off until you name it in `decision_model_uses`, sends nothing when it is off, and falls back to the deterministic result on every failure. A model that acts in `enforce` writes model-authority records - a route, a derived item, a "looks answered" or "looks done, confirm" proposal, a hidden-history verdict - which your records and an agent's always beat, and `o2b topic undo` takes back like every automatic write (a hidden-history verdict is a log record that command does not undo). A private record never goes to a hosted provider unless you opt the use in with `decision_model_private_uses`; `<private>` regions never leave the machine, from the records or from the titles and texts sent beside them, and a drafted topic title is sent only when every record of its group may leave, the ones the request does not show included. The nine uses start in `shadow` and need a thresholds-store entry before `enforce` acts, and the value stored for the model that answered is the threshold the use applies. Each question names the record it asks about, a kept history note is not asked about again, the dream pass asks about the newest records first (a record dated in the future waits behind the present ones), and every use gets its share of the pass budget, `topic_same` included. An answer that changes nothing is remembered on the machine that runs the dream, also by a pass that fails later - by use, record, question version, answering model and mode, for eight weeks, without any change to the stored format - so the same record is not asked again and older records get their turn; an enforce `none` route is kept and applied again without asking. A stored threshold outside 0..1 is ignored for the built-in one, a topic head the model gave no valid answer for keeps its usual place, and two dream passes that judged the same history note at once store one verdict for it. The record-item uses ask about a record under the topic the model routed it to in the same pass, never derive a second item under an id a split moved to another topic, and `topic_same` asks about the best-scoring pairs first. The maintenance lane decides and plans with the config its caller named, the MCP dream tool's decide pass spends the call's one deadline and the lane's spends its dream task's, and a dream pass that wrote only model verdict records reports the change. The planner reads every record with its `<private>` regions removed, so a word that appears only inside a region never groups or routes a record and never reaches a drafted title or goal, a derived item or a request. No request runs past its use's share of the pass, and a question's or milestone's text, and the topic title sent beside each `event_kind` record, count toward the state budget. A merge pair only the model proposed is dropped when one of its topics was merged or closed before planning, a record taken out of its topic and routed to another in the same pass gets its derived item there, two records answering one question in one pass write one proposal, and when a merged topic shows two questions or milestones with the same id, the proposal goes to the one the model chose. A record a use writes lands in `private` when any input of its question was private - the record, its topic, an offered topic or question, an evidence record, a head field or a `<private>` region in any of them - so a "looks done" proposal judged on private evidence is no longer shared, and a history note with a `<private>` region is not asked about; a request that batches several records counts every record and every offered topic it sent for each record it writes, and a private head field the egress rule withholds does not count. A per-pass cap counts only what may leave the machine: what stays local is left out first, so the shareable records, topics, groups, pairs and heads behind it are still asked. No request starts with less than 250 ms of its use's share of the pass, or of the hook budget, left, nor with a configured request timeout or hook budget below 250 ms. A milestone's evidence dated in the future waits behind the present evidence, as records and history notes do.
+- **Nine decision-model uses for topics and agent context.** With the decision-model feature on, the dream pass can refine what the topic planner does on its own (`topic_route`, `event_kind`, `human_readable`, `topic_new`, `topic_same`, `question_resolved`, `milestone_done`), and two new uses pick what an agent sees (`head_select`, `neighbour_relevant`). Every use is off until you name it in `decision_model_uses`, sends nothing when it is off, and falls back to the deterministic result on every failure. A model that acts in `enforce` writes model-authority records - a route, a derived item, a "looks answered" or "looks done, confirm" proposal, a hidden-history verdict - which your records and an agent's always beat, and `o2b topic undo` takes back like every automatic write (a hidden-history verdict is a log record that command does not undo). A private record never goes to a hosted provider unless you opt the use in with `decision_model_private_uses`; `<private>` regions never leave the machine, from the records or from the titles and texts sent beside them, and a drafted topic title is sent only when every record of its group may leave, the ones the request does not show included. The nine uses start in `shadow` and need a thresholds-store entry before `enforce` acts, and the value stored for the model that answered is the threshold the use applies. Each question names the record it asks about, a kept history note is not asked about again, the dream pass asks about the newest records first (a record dated in the future waits behind the present ones), and every use gets its share of the pass budget, `topic_same` included. An answer that changes nothing is remembered on the machine that runs the dream, also by a pass that fails later - by use, record, question version, answering model and mode, for eight weeks, without any change to the stored format - so the same record is not asked again and older records get their turn; an enforce `none` route is kept and applied again without asking. A stored threshold outside 0..1 is ignored for the built-in one, a topic head the model gave no valid answer for keeps its usual place, and two dream passes that judged the same history note at once store one verdict for it. The record-item uses ask about a record under the topic the model routed it to in the same pass, never derive a second item under an id a split moved to another topic, and `topic_same` asks about the best-scoring pairs first. The maintenance lane decides and plans with the config its caller named, the MCP dream tool's decide pass spends the call's one deadline and the lane's spends its dream task's, and a dream pass that wrote only model verdict records reports the change. The planner reads every record with its `<private>` regions removed, so a word that appears only inside a region never groups or routes a record and never reaches a drafted title or goal, a derived item or a request. No request runs past its use's share of the pass, and a question's or milestone's text, and the topic title sent beside each `event_kind` record, count toward the state budget. A merge pair only the model proposed is dropped when one of its topics was merged or closed before planning, a record taken out of its topic and routed to another in the same pass gets its derived item there, two records answering one question in one pass write one proposal, and when a merged topic shows two questions or milestones with the same id, the proposal goes to the one the model chose. A record a use writes lands in `private` when any input of its question was private - the record, its topic, an offered topic or question, an evidence record, a head field or a `<private>` region in any of them - so a "looks done" proposal judged on private evidence is no longer shared, and a history note with a `<private>` region is not asked about; a request that batches several records counts every record and every offered topic it sent for each record it writes, and a private head field the egress rule withholds does not count. A per-pass cap counts only what may leave the machine: what stays local is left out first, so the shareable records, topics, groups, pairs and heads behind it are still asked. No request starts with less than 250 ms of its use's share of the pass, or of the hook budget, left, nor with a configured request timeout or hook budget below 250 ms; the time spent preparing the request counts, and with a provider chain the 250 ms hold for each provider asked: a provider whose even share of the request's time would be less is not asked, and its time goes to the providers before it. A milestone's evidence dated in the future waits behind the present evidence, as records and history notes do.
- **Topic heads and pinned context in the session start digest.** The SessionStart digest now shows the heads of your project's live topics (title, status, id, description, goal; at most `topic_heads_max`, default 12, within `topic_heads_max_chars`, default 1600) and the pinned context (`pinned_max_chars`, default 1200, marked as stored, agent-written text), each with a line naming what was left out and the command that shows it; each block fits its cap whole, and together they take at most half of what the injection budget leaves after the scoped rules, so your confirmed preferences always keep the other half and say when they were cut. A heads block always shows at least one head, the heads come from the store of the config the hook reads its decision settings from, and a `head_select` step that fails keeps the usual order. Without a brain store, project topics or pinned context the digest is exactly what it was.
- **Neighbour updates before each turn.** When a brain store is in use, a new prompt-time hook surfaces what other machines recorded on the topics you work on or in your project - at most five lines within 800 characters, the line naming what was cut included (shown alone when no update line is shown, also when none passed the filter but the 20-candidate cap left some out), never your own records (nor an item a model derived from one), nothing on your next prompt after you have seen it, and no old history replayed when a session starts, resumes after a day or the index is rebuilt. Each turn asks the index for what other machines wrote on topics since the last turn, so a large import of other records in between hides nothing; if more than 2000 such records arrived (on any topic or project), one line says how many were not read and the `brain_changes` call that lists them among every record since. The topics you work on come from your own newest records, however old. With `neighbour_relevant` in `enforce`, a record the model was not asked about (a private one sent to no hosted provider) is shown as it would be with the use off. It runs on every host, OpenCode included; the machine-config key `neighbour_updates_enabled` set to `false` or `0` turns it off.
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- **The recall brief survives a hung decision model under a short hook ceiling.** The `recall_inject` filter's wait now also ends at half the hook's process ceiling (`OPEN_SECOND_BRAIN_HOOK_CEILING_MS`), as the session start digest and neighbour updates already did: with a ceiling below the retrieval budget, a provider that never answers no longer lets the ceiling end the hook before it writes anything; the brief goes out without the model's filter.
- **Schema field lists no longer lose an edit made on another machine.** A schema apply on a store-only machine writes one record per entry it adds or removes (`setting-entry`, documented in `FORMAT.md`) instead of the whole list, and a list setting folds per entry, so two machines that change the same schema field before they sync keep both changes and read one list, whatever order the records arrive in. An add and a remove of the same entry resolve to your record over an agent's, then to the later one; a keyed entry such as a prefix holds one value per name, and two spellings of one token (case, spaces, quotes) are one entry. Only `brain.schema.<field>` keys take entry records, so no entry record can remove a setting that holds one value. Lists written whole by the migration or `o2b brain settings set` read as before. An agent cannot change an entry you set or removed, through an apply or through a whole-list `brain_settings` write, and an accepted write reports the list in force. A type removed on one machine and a reference to it (an alias, a prefix, a link constraint pair) added on another no longer block every later apply: the pack drops references to undeclared tokens when it is read, the same on every machine, and the next apply removes them from the records. Removing a type now also removes the link constraint pairs that name it (and a link type's constraints once none is left), removing a link type also removes the pairs of other link types that name it as source or target unless a type of that name stays declared, and an apply refuses a pair whose source or target is not declared, so a removed type no longer keeps allowing or blocking typed links of pages whose `type:` still names it. A pack merged from two applies stays attested: each apply's audit row names the records it wrote and the records rows it had seen attest, the migration's import attests what it imported, and a record no apply wrote reports the pack as modified and makes the next store-only apply refuse until you adopt it with `o2b brain schema apply --adopt-unattested`, alone or with the apply's mutations. A row attests each record in the version it saw (`<id>:<sha256>`), so another body written under an attested record's id is a finding too, and a record under `brain.schema.` for a name the pack does not read is neither part of the pack nor something to adopt. A whole-list record of a schema field whose value is no list of one-line entries (a single string or number, an object, an entry with a line break) is not applied, so it can no longer hide the list from the pack and from the adoption check. An apply reads the pack, the records that decide it and the stored entries once, so an entry another machine adds while the apply runs is neither removed by it as a repair nor attested by it, and the integrity check reads the pack and its records once the same way. On a machine that has not switched, `o2b brain schema apply --adopt-unattested` with an empty batch (`--mutation '[]'` too) is refused and leaves `_brain.yaml` as it is, and `--mutation '[]'` alone counts as no mutation. On a store-only machine, adoption alone writes no entry, the removal of a dangling reference included; an apply's removal of a dangling reference is skipped when another machine replaced that entry meanwhile, and is written as a conditional removal (`if_decided_by`) that every machine ignores once a replacement it never saw decides the entry, so a replacement that syncs in only after the removal is kept too; an attribute or frontmatter tier whose `type.field` name has spaces around its dot (`paper . year`) is one entry with `paper.year`, as the pack reads it, and is no longer removed by an unrelated apply as a dangling reference; a schema field holding only an empty list or an unset beside such a conditional removal no longer makes every settings read fail; and `o2b brain schema report` reports the pack as modified, naming the records, whenever the next apply would refuse them, also when an unaudited record left the pack as it was or removed the last entry of the only schema field, so that no pack is left.
- **The shared namespace on the brain store.** `shared_namespace` gets no store of its own: where the shared vault is store-only on a machine, it is that machine's brain store, which every store-only agent reads. The mirror now writes there when the origin record went only to the origin vault's legacy tree, and still refuses, with the reason, on a switched machine, for a store-only origin vault, with dual writes on (the record is in that store already), and when the machine has no store; these checks read the machine config the write was made with. A namespace path that is not a directory (unmounted, missing or a file) is reported as failed instead of being mirrored into this machine's store.
- **A lane entry id maps to exactly one legacy path.** A page and a JSON file of one name (`x.json.md` and `x.json`) no longer share an entry id, and a name that would not map back to itself (a file with another extension or none, a folder named like another folder's lane, such as `exact-state/`) is refused by name on a store-only machine and listed by the migration as an item it could not convert, instead of being filed under an id that names another path. A folder of your own under `Brain/` spelled otherwise than a lane name (`My Notes/`, `project_x/`) is still imported, under its lane (`my-notes`), and a rollback puts it back at its own path; two folders that would take one lane (`My Notes/` beside `my-notes/`) are listed and archived instead, and `brain_doctor` names them before a migration (`lane-folder-unimportable`) so you can rename one first. A page an earlier release's migration filed under the id of the file beside it is moved to its own id by the next dream pass on a store-only machine that holds `private`, once per machine, and again once new records arrive (or after a pass that could not read every shard), and the file gets its id back; a page removed after its repair stays removed. `brain_doctor` names any page still filed that way (`lane-id-misfiled`) with the remedy, also on a store where no machine holds `private`.
diff --git a/docs/decision-models/recall-inject.md b/docs/decision-models/recall-inject.md
index a18b4af2..b58c9062 100644
--- a/docs/decision-models/recall-inject.md
+++ b/docs/decision-models/recall-inject.md
@@ -46,8 +46,12 @@ The request gets `min(decision_model_hook_budget_ms, remaining retrieval
budget)`, where the retrieval budget is the hook's time budget (2,500 ms
unless `recall_inject_time_budget_ms` sets another value in 250..6000). The
sub-budget is passed to the provider as its timeout and is also enforced
-around the call, so it never extends the total. With no time left
-after retrieval, nothing is sent.
+around the call, so it never extends the total. The wait also ends at
+half the hook's process ceiling (`OPEN_SECOND_BRAIN_HOOK_CEILING_MS`),
+counted from the hook's start: with a ceiling set below the retrieval
+budget, a provider that never answers gives way and today's brief goes
+out without the filter, instead of the ceiling ending the hook before
+any output. With no time left after retrieval, nothing is sent.
The search the hook runs still skips the decision-model rerank kind: with
`search_rerank_kind: decision-model` the hook uses the heuristic order, so a
diff --git a/docs/decision-models/topics-and-agent-context.md b/docs/decision-models/topics-and-agent-context.md
index 08da9185..ba74bbfa 100644
--- a/docs/decision-models/topics-and-agent-context.md
+++ b/docs/decision-models/topics-and-agent-context.md
@@ -71,7 +71,11 @@ Shared mechanics:
starts when that time is less than 250 ms: a request that could not
finish might still be billed. The same 250 ms hold for the two
hook-time uses against the time their request gets, the smaller of
- `decision_model_hook_budget_ms` and what is left of the hook. A question's or
+ `decision_model_hook_budget_ms` and what is left of the hook. The time
+ spent preparing a request counts, and with a provider chain
+ (`decision_model_providers`) the 250 ms hold for each provider asked:
+ the chain splits the request's time evenly, and a provider whose share
+ would be less is not asked, its time going to the providers before it. A question's or
milestone's text counts toward `decision_model_max_state_tokens` beside
the records it is sent with.
- **Private records stay local.** A private record, private topic, private
diff --git a/hooks/neighbour-updates.ts b/hooks/neighbour-updates.ts
index 9cf933cd..f507d403 100644
--- a/hooks/neighbour-updates.ts
+++ b/hooks/neighbour-updates.ts
@@ -61,7 +61,17 @@ import { asHookPayload, readHookInput } from "./lib/stdin.ts";
import { resolveHookTarget } from "./lib/hook-vault.ts";
/** The whole lane's budget: the decision request gets the rest of it. */
-const LANE_BUDGET_MS = 1500;
+export const LANE_BUDGET_MS = 1500;
+
+/**
+ * How long the lane may still wait on the decision model, `elapsedMs`
+ * after the hook started under a ceiling of `ceilingMs`: the rest of the
+ * lane's budget, and never past half the ceiling, so a hung provider gives
+ * way to the deterministic gate while there is still time to emit.
+ */
+export function neighbourDecisionWaitMs(ceilingMs: number, elapsedMs: number): number {
+ return Math.min(Math.max(0, LANE_BUDGET_MS - elapsedMs), decisionWaitMs(ceilingMs, elapsedMs));
+}
async function main(): Promise<void> {
const ceilingMs = resolveHookCeilingMs();
@@ -192,13 +202,7 @@ async function main(): Promise<void> {
if (assigned !== undefined && assigned !== null) sessionTopics.add(assigned);
}
- // Inside the process ceiling too: a hung provider gives way to the
- // deterministic gate while there is still time to emit.
- const elapsed = Date.now() - startedAt;
- const remaining = Math.min(
- Math.max(0, LANE_BUDGET_MS - elapsed),
- decisionWaitMs(ceilingMs, elapsed),
- );
+ const remaining = neighbourDecisionWaitMs(ceilingMs, Date.now() - startedAt);
const decision = await runNeighbourRelevant({
config: resolveDecisionModelConfig({
config: discoverConfig().data,
diff --git a/hooks/recall-inject.ts b/hooks/recall-inject.ts
index 4b0ce04d..a4602917 100644
--- a/hooks/recall-inject.ts
+++ b/hooks/recall-inject.ts
@@ -91,7 +91,7 @@ import {
readRecallInjected,
recordRecallInjected,
} from "./lib/injection-ledger.ts";
-import { armProcessCeiling, resolveHookCeilingMs } from "./lib/process-ceiling.ts";
+import { armProcessCeiling, decisionWaitMs, resolveHookCeilingMs } from "./lib/process-ceiling.ts";
import { isHookStateCorrupt } from "./lib/session-state.ts";
import { asHookPayload, readHookInput } from "./lib/stdin.ts";
import { isContextEventName } from "./lib/context-events.ts";
@@ -328,9 +328,11 @@ async function main(): Promise<void> {
// ceiling armed, no payload read, no output - byte-identical to before.
if (!resolveRecallInjectEnabled()) return;
+ const startedAt = Date.now();
+ const ceilingMs = resolveHookCeilingMs();
let auditVault: string | null = null;
const disarm = armProcessCeiling({
- ceilingMs: resolveHookCeilingMs(),
+ ceilingMs,
onExpire: () => {
if (auditVault !== null) {
recordDecision(auditVault, {
@@ -375,7 +377,15 @@ async function main(): Promise<void> {
defaultRecallRetriever(configPath, vault, caps.maxNotes),
{
...caps,
- ...(decisionFilter !== undefined ? { decisionFilter } : {}),
+ ...(decisionFilter !== undefined
+ ? {
+ decisionFilter,
+ // The decision wait ends at half the ceiling: on a hung
+ // provider the brief goes out without the model's filter
+ // instead of being lost to the ceiling.
+ decisionWaitLeftMs: () => decisionWaitMs(ceilingMs, Date.now() - startedAt),
+ }
+ : {}),
...(sliceConfig.slices.length > 0
? {
slices: sliceConfig.slices,
diff --git a/openspec/changes/brain-store-and-topics/design.md b/openspec/changes/brain-store-and-topics/design.md
index 1e1bb5fa..10c56ad8 100644
--- a/openspec/changes/brain-store-and-topics/design.md
+++ b/openspec/changes/brain-store-and-topics/design.md
@@ -387,7 +387,7 @@ Three questions the store left open after phase 2, settled by the option that fi
| `topic-create` (`topic_new`) | the group members, the drafted title | any member is private (the planner's rule); the title is drafted only from text outside regions and sent only when every member may leave |
| merge proposal (`topic_same`), `head_select`, `neighbour_relevant` | topic pairs, heads, neighbour records | no store record is written; a private subject is `(withheld)` in `decision_model_call` |
- A private head field the egress rule withholds is never sent and does not count, even when its text carries a region. The table test `decide-privacy-matrix.test.ts` holds every row (74 cells: kind, which input is private, may leave or hosted; the inputs include another subject of the same batched request, that subject's own topic, and a withheld private field). *Filter before cap.* Every per-pass or per-request cap counts only what may leave: records (`event_kind`, `topic_route`, `question_resolved`), offered topics (`topic_route`), shown evidence (`milestone_done`), groups and shown members (`topic_new`), pairs (`topic_same`) and heads per request (`head_select`); what the egress rule keeps home is left out first, so shareable subjects behind it are still asked and a private subject is not even named as `(withheld)` in the call record. `neighbour_relevant` keeps its order (the hook caps before it, and a withheld candidate keeps the deterministic gate). *Minimum request time.* No request starts with less than `MIN_REQUEST_MS` (250 ms, `request-floor.ts`) of its use's share left (`mayStartRequest`, checked where the pass checked `until` and in `runPairVerdicts`) or of the hook budget (`head_select`, `neighbour_relevant`), and none whose own timeout is below it: the floor holds for the time the request really gets, the smaller of the configured request timeout (`decision_model_timeout_ms`, `decision_model_hook_budget_ms` for the hook uses) and what is left; it counts as a budget skip, and the deterministic result stands for what was not asked. The value is the floor skill attachment already keeps before its second request (`SKILLS_STAGE2_MIN_REMAINING_MS`), a twelfth of the default 3 s request timeout: a hosted round trip rarely answers in less, and a request that cannot finish may still be billed.
+ A private head field the egress rule withholds is never sent and does not count, even when its text carries a region. The table test `decide-privacy-matrix.test.ts` holds every row (74 cells: kind, which input is private, may leave or hosted; the inputs include another subject of the same batched request, that subject's own topic, and a withheld private field). *Filter before cap.* Every per-pass or per-request cap counts only what may leave: records (`event_kind`, `topic_route`, `question_resolved`), offered topics (`topic_route`), shown evidence (`milestone_done`), groups and shown members (`topic_new`), pairs (`topic_same`) and heads per request (`head_select`); what the egress rule keeps home is left out first, so shareable subjects behind it are still asked and a private subject is not even named as `(withheld)` in the call record. `neighbour_relevant` keeps its order (the hook caps before it, and a withheld candidate keeps the deterministic gate). *Minimum request time.* No request starts with less than `MIN_REQUEST_MS` (250 ms, `request-floor.ts`) of its use's share left (`mayStartRequest`, checked where the pass checked `until` and in `runPairVerdicts`) or of the hook budget (`head_select`, `neighbour_relevant`), and none whose own timeout is below it: the floor holds for the time the request really gets, the smaller of the configured request timeout (`decision_model_timeout_ms`, `decision_model_hook_budget_ms` for the hook uses) and what is left; it counts as a budget skip, and the deterministic result stands for what was not asked. The floor is enforced in `runDecision` (`requestFloor`, which every topic, pair and hook request sets): the request's time runs from the call's start, so preparing the state counts and the floor is checked again before the first attempt; and it holds for each attempt of a provider chain (`attemptTimeoutMs`): the chain splits the request's time evenly, an entry whose share would fall below the floor is not attempted, and its time goes to the entries before it. A floor skip in `runDecision` sends and records nothing. The callers' checks before a request stay as the cheap early stop that spares preparing a request that cannot start. The value is the floor skill attachment already keeps before its second request (`SKILLS_STAGE2_MIN_REMAINING_MS`), a twelfth of the default 3 s request timeout: a hosted round trip rarely answers in less, and a request that cannot finish may still be billed.
14. **After the fifth pull-request review.** *One order for every subject queue.* Records, a milestone's evidence and history notes are taken newest first, an item dated after the run behind every present one (`newestPresentFirst`, `decide-shared.ts`): a milestone's evidence keeps the order of the pass's candidates, so five future-dated records can no longer push the present evidence out of the request, and a milestone with only future-dated evidence is asked last.
diff --git a/src/core/agent-context/head-select.ts b/src/core/agent-context/head-select.ts
index 2bf5ad3d..a7f3a9d9 100644
--- a/src/core/agent-context/head-select.ts
+++ b/src/core/agent-context/head-select.ts
@@ -142,6 +142,8 @@ export async function runHeadSelect(input: {
{
config: cfg,
timeoutMs: budgetMs,
+ // The floor holds for each attempt of the chain, not only here.
+ requestFloor: true,
...(input.provider !== undefined ? { provider: input.provider } : {}),
...(input.env !== undefined ? { env: input.env } : {}),
recordDetails: (response, context) => {
diff --git a/src/core/agent-context/neighbour-relevant.ts b/src/core/agent-context/neighbour-relevant.ts
index c2898b33..5d112556 100644
--- a/src/core/agent-context/neighbour-relevant.ts
+++ b/src/core/agent-context/neighbour-relevant.ts
@@ -123,6 +123,8 @@ export async function runNeighbourRelevant(input: {
{
config: cfg,
timeoutMs: budgetMs,
+ // The floor holds for each attempt of the chain, not only here.
+ requestFloor: true,
...(input.provider !== undefined ? { provider: input.provider } : {}),
...(input.env !== undefined ? { env: input.env } : {}),
recordDetails: (response, context) => {
diff --git a/src/core/brain/recall-inject.ts b/src/core/brain/recall-inject.ts
index 99585003..08dee6d4 100644
--- a/src/core/brain/recall-inject.ts
+++ b/src/core/brain/recall-inject.ts
@@ -160,6 +160,14 @@ export interface RecallInjectOptions {
* inside what is left of {@link timeBudgetMs}.
*/
readonly decisionFilter?: RecallInjectFilter;
+ /**
+ * The longest the decision filter may still wait, read when it is
+ * consulted: the hook passes what is left of half its process ceiling
+ * (`decisionWaitMs`), so a hung provider gives way while the brief can
+ * still be emitted. The filter waits the smaller of this and what is
+ * left of {@link timeBudgetMs}; absent, only the time budget bounds it.
+ */
+ readonly decisionWaitLeftMs?: () => number;
/**
* Notes this session was already shown, as {@link recallInjectNoteKey} values
* (origin, path and line span). Absent or empty filters nothing. Applied
@@ -624,7 +632,7 @@ async function decideOverRetrieval(
);
return { ...out, injectedNotes: injectedNotesOf(kept.slice(0, out.noteCount)) };
},
- remainingMs: timeBudgetMs - (clock() - started),
+ remainingMs: filterWaitMs(timeBudgetMs - (clock() - started), options),
clock,
});
}
@@ -785,7 +793,7 @@ async function decideSliced(
slices: out.slices,
};
},
- remainingMs: caps.timeBudgetMs - (clock() - started),
+ remainingMs: filterWaitMs(caps.timeBudgetMs - (clock() - started), options),
clock,
});
}
@@ -838,6 +846,15 @@ interface FilterContext {
readonly clock: () => number;
}
+/**
+ * How long the decision filter may wait: what is left of the time budget,
+ * never past the caller's own bound ({@link RecallInjectOptions.decisionWaitLeftMs}).
+ */
+function filterWaitMs(budgetLeftMs: number, options: RecallInjectOptions): number {
+ const bound = options.decisionWaitLeftMs?.();
+ return bound === undefined ? budgetLeftMs : Math.min(budgetLeftMs, bound);
+}
+
/**
* Run the decision-model filter over today's `inject` decision. Never
* throws and never extends the time budget: with nothing left the filter
diff --git a/src/core/decision-model/pair-verdict.ts b/src/core/decision-model/pair-verdict.ts
index 3ea43a4d..46af89e8 100644
--- a/src/core/decision-model/pair-verdict.ts
+++ b/src/core/decision-model/pair-verdict.ts
@@ -272,9 +272,14 @@ export async function runPairVerdicts(
() => batch.questions,
{
config: cfg,
- // A request never runs past `until`: it gets what is left of it.
+ // A request never runs past `until`: it gets what is left of it,
+ // and with less than the minimum request time left (or per
+ // attempt of the chain) it is not sent.
...(opts.until !== undefined
- ? { timeoutMs: Math.max(1, Math.min(cfg.timeoutMs, opts.until - clock())) }
+ ? {
+ timeoutMs: Math.max(0, Math.min(cfg.timeoutMs, opts.until - clock())),
+ requestFloor: true,
+ }
: {}),
...(opts.provider !== undefined ? { provider: opts.provider } : {}),
...(opts.env !== undefined ? { env: opts.env } : {}),
diff --git a/src/core/decision-model/request-floor.ts b/src/core/decision-model/request-floor.ts
index 2eedbb50..ac8d65ca 100644
--- a/src/core/decision-model/request-floor.ts
+++ b/src/core/decision-model/request-floor.ts
@@ -3,8 +3,11 @@
* request started with only a few milliseconds of its budget left gets a
* timeout it cannot meet, and a hosted provider may still bill the
* request it received. So no request starts while less than
- * {@link MIN_REQUEST_MS} is left, nor with a timeout below it: the use stops as if its share of the
- * budget were spent, and today's result stands for what it did not ask.
+ * {@link MIN_REQUEST_MS} is left, nor with a timeout below it: the use
+ * stops as if its share of the budget were spent, and today's result
+ * stands for what it did not ask. The floor holds for each attempt of a
+ * fallback chain (`runDecision` with `requestFloor`), and the time the
+ * call spent preparing its request counts against its budget.
*/
/**
@@ -15,11 +18,25 @@
*/
export const MIN_REQUEST_MS = 250;
+/**
+ * The timeout of the next attempt of a fallback chain when `leftMs` of
+ * the call's budget is left for `entriesLeft` entries, or `null` when no
+ * attempt may start. The chain splits its one budget evenly, but never
+ * below the floor: an entry whose share would fall below it is not
+ * attempted, and its time goes to the entries that are (the earlier
+ * ones, which the configured order prefers). This is the one place the
+ * floor is computed; {@link mayStartRequest} is its single-entry case.
+ */
+export function attemptTimeoutMs(leftMs: number, entriesLeft: number): number | null {
+ const fitting = Math.min(entriesLeft, Math.floor(leftMs / MIN_REQUEST_MS));
+ return fitting < 1 ? null : Math.floor(leftMs / fitting);
+}
+
/**
* Whether a request may start at `nowMs` when none may run past `untilMs`
* and its own timeout is `timeoutMs`: the floor holds for the time the
* request really gets, the smaller of the two.
*/
export function mayStartRequest(nowMs: number, untilMs: number, timeoutMs: number): boolean {
- return Math.min(untilMs - nowMs, timeoutMs) >= MIN_REQUEST_MS;
+ return attemptTimeoutMs(Math.min(untilMs - nowMs, timeoutMs), 1) !== null;
}
diff --git a/src/core/decision-model/run.ts b/src/core/decision-model/run.ts
index e2727fb5..cf18ff1d 100644
--- a/src/core/decision-model/run.ts
+++ b/src/core/decision-model/run.ts
@@ -42,6 +42,7 @@ import type { ResolvedDecisionProvider } from "./config-types.ts";
import { noteDecisionLatency } from "./latency.ts";
import { makeEntryDecisionProvider } from "./provider.ts";
import { emitDecisionModelCall, todaySpendUsd } from "./record.ts";
+import { attemptTimeoutMs } from "./request-floor.ts";
import { estimateTokens } from "./state.ts";
/** Estimated cost of requests in flight in this process, per vault. */
@@ -114,6 +115,16 @@ export interface RunDecisionOptions<C> {
readonly secretsVault?: string | null;
/** Overrides `decision_model_timeout_ms` (e.g. a hook sub-budget). */
readonly timeoutMs?: number;
+ /**
+ * Holds the minimum request time (`request-floor.ts`, design D20) for
+ * the topic and hook uses: the budget runs from this call's start, so
+ * the time spent preparing the request counts; no attempt of the chain
+ * starts with less than the floor, and an entry whose share would fall
+ * below it is not attempted (its time goes to the entries that are).
+ * When not even the first attempt fits, nothing is built, sent or
+ * recorded and the call degrades as `budget`.
+ */
+ readonly requestFloor?: boolean;
/**
* The caller's cancellation (the search's composite hybrid deadline).
* Handed to the provider, whose transport stops the request when it
@@ -207,10 +218,17 @@ export async function runDecision<C>(
if (configured === "off" || cfg === null || cfg === undefined) return { status: "off" };
const mode = opts.modeOverride ?? configured;
const env = opts.env ?? process.env;
+ const budgetMs = opts.timeoutMs ?? cfg.timeoutMs;
+ // With the floor the budget runs from here, so preparation counts,
+ // the construction of the provider chain included.
+ const floorDeadline = opts.requestFloor === true ? Date.now() + budgetMs : null;
const chain = buildChain(cfg, use, mode, opts, env, opts.secretsVault);
if (chain.length === 0 || chain[0] === undefined) return { status: "off" };
const primary = chain[0];
const now = opts.now ?? (() => new Date());
+ const floorLeft = (): boolean =>
+ floorDeadline === null || attemptTimeoutMs(floorDeadline - Date.now(), 1) !== null;
+ if (!floorLeft()) return { status: "degraded", mode, reason: "budget" };
const record = (
attempt: ChainAttempt,
@@ -289,6 +307,8 @@ export async function runDecision<C>(
record(primary, "budget");
return { status: "degraded", mode, reason: "budget" };
}
+ // Preparation may have spent the margin above the floor.
+ if (!floorLeft()) return { status: "degraded", mode, reason: "budget" };
const ready: Extract<BuiltDecisionState<C>, { kind: "ok" }> = built;
const qs = questions(ready);
@@ -302,17 +322,23 @@ export async function runDecision<C>(
// The whole chain shares the caller's one budget (`timeout_ms`, or the
// hook sub-budget): each attempt gets an even slice of what remains, so
// a dead first server can never stretch the call past the budget, and
- // the last entry always keeps a real slice to answer in.
- const budgetMs = opts.timeoutMs ?? cfg.timeoutMs;
+ // the last entry always keeps a real slice to answer in. With the
+ // floor, no slice is below it and the budget began at the call's start.
const send = async (): Promise<DecisionRunResult<C>> => {
- const deadline = Date.now() + budgetMs;
+ const deadline = floorDeadline ?? Date.now() + budgetMs;
let last: {
readonly reason: DecisionDegradeReason;
readonly mode: Exclude<DecisionModelMode, "off">;
} | null = null;
for (const [index, attempt] of chain.entries()) {
const remaining = chain.length - index;
- const slice = Math.max(1, Math.floor((deadline - Date.now()) / remaining));
+ const left = deadline - Date.now();
+ const slice =
+ floorDeadline === null
+ ? Math.max(1, Math.floor(left / remaining))
+ : attemptTimeoutMs(left, remaining);
+ // No later entry fits either: the call ends with what it has.
+ if (slice === null) break;
const started = Date.now();
let response: DecisionResponse;
try {
@@ -366,7 +392,8 @@ export async function runDecision<C>(
return {
status: "degraded",
mode: last?.mode ?? mode,
- reason: last?.reason ?? "network",
+ // No attempt fitted the floor: a budget skip.
+ reason: last?.reason ?? (floorDeadline === null ? "network" : "budget"),
};
};
const release = reserveInFlight(gated ? cfg.vault : null, reservation);
diff --git a/src/core/topics/decide-shared.ts b/src/core/topics/decide-shared.ts
index 34bf128e..a5495627 100644
--- a/src/core/topics/decide-shared.ts
+++ b/src/core/topics/decide-shared.ts
@@ -179,10 +179,12 @@ export function holderOf(fold: TopicFold, candidate: TopicCandidate): string | n
/**
* The time one request of the running use may take: the configured
* timeout, never past {@link PassContext.until}, so a request started
- * late in a use's share cannot run past the pass's budget.
+ * late in a use's share cannot run past the pass's budget. Read after
+ * the request is prepared; `runDecision` (`requestFloor`) sends nothing
+ * when it is below the minimum request time.
*/
export function requestTimeoutMs(ctx: PassContext): number {
- return Math.max(1, Math.min(ctx.cfg.timeoutMs, ctx.until - ctx.clock()));
+ return Math.max(0, Math.min(ctx.cfg.timeoutMs, ctx.until - ctx.clock()));
}
/** The state candidate of one store record, guarded by the egress rule. */
@@ -299,6 +301,9 @@ export function requestOptions(
): Record<string, unknown> {
return {
recordDetails,
+ // The floor holds for each attempt of the chain, and the time spent
+ // preparing the request counts (`runDecision`).
+ requestFloor: true,
...(ctx.options.provider !== undefined ? { provider: ctx.options.provider } : {}),
...(ctx.options.env !== undefined ? { env: ctx.options.env } : {}),
};
diff --git a/tests/cli/brain-attest.test.ts b/tests/cli/brain-attest.test.ts
index 6d56ab58..cb12148e 100644
--- a/tests/cli/brain-attest.test.ts
+++ b/tests/cli/brain-attest.test.ts
@@ -454,10 +454,6 @@ test("json shapes: keygen paths, sign record, list rows, verify K4", async () =>
expect(verifyJson.storeDigestMatches).toBe(true);
});
-// Each of these three rungs boots two cold CLIs (bun startup, the full
-// import graph, Ed25519 keygen and sign), which the 5 s default does not
-// leave headroom for in CI; the plugin suite's hung-shim test carries the
-// same explicit bound.
test("passphrase ladder: wrap at keygen, sign from stdin in a fresh process", async () => {
append(1);
const first = await runCli(["brain", "attest", "keygen", "--vault", vault], {
@@ -477,7 +473,7 @@ test("passphrase ladder: wrap at keygen, sign from stdin in a fresh process", as
});
expect(signed.returncode).toBe(0);
expect(recordFiles()).toHaveLength(1);
-}, 30_000);
+});
test("passphrase ladder: a wrapped key with no source refuses by name", async () => {
append(1);
@@ -489,7 +485,7 @@ test("passphrase ladder: a wrapped key with no source refuses by name", async ()
expect(res.returncode).toBe(1);
expect(res.stderr).toContain("passphrase");
expect(recordFiles()).toEqual([]);
-}, 30_000);
+});
test("passphrase ladder: --passphrase-from-env with an unset variable exits two", async () => {
const res = await runCli(
@@ -517,4 +513,4 @@ test("passphrase ladder: --passphrase-from-env with a set variable wraps and sig
);
expect(signed.returncode).toBe(0);
expect(recordFiles()).toHaveLength(1);
-}, 30_000);
+});
diff --git a/tests/cli/brain-secret.test.ts b/tests/cli/brain-secret.test.ts
index e360a308..0badbf0b 100644
--- a/tests/cli/brain-secret.test.ts
+++ b/tests/cli/brain-secret.test.ts
@@ -161,7 +161,7 @@ test("unlock wraps the raw keyfile; a fresh process sees the locked refusal unti
});
expect(wrong.returncode).toBe(1);
expect(wrong.stderr).toContain("passphrase");
-}, 20000);
+});
test("a wrapped store works from a SECOND command: passphrase from env or stdin, per verb", async () => {
// The unlock holds the key for ITS process only, so every command below
@@ -239,7 +239,7 @@ test("a wrapped store works from a SECOND command: passphrase from env or stdin,
(await runCli(["brain", "secret", "list", "--vault", fresh, "--json"])).stdout,
) as { secrets: Array<{ name: string }> };
expect(listed.secrets.map((s) => s.name)).toEqual(["api-key"]);
-}, 30000);
+});
test("the well-known environment variable alone reaches a wrapped store from a fresh process", async () => {
await runCli(["brain", "secret", "set", "api-key", "--allow", "bun -e *", "--vault", vault], {
@@ -270,7 +270,7 @@ test("the well-known environment variable alone reaches a wrapped store from a f
{ env: { OPEN_SECOND_BRAIN_SECRETS_PASSPHRASE: passphrase }, subprocess: true },
);
expect(run.returncode).toBe(0);
-}, 20000);
+});
test("unwrap writes the raw keyfile back; a later command needs no passphrase; unwrap refuses when raw", async () => {
await runCli(["brain", "secret", "set", "api-key", "--allow", "bun -e *", "--vault", vault], {
@@ -309,7 +309,7 @@ test("unwrap writes the raw keyfile back; a later command needs no passphrase; u
// The wrong passphrase leaves the wrap intact (nothing was unwrapped by
// the refusals above, so re-wrap first is unnecessary: the store is raw,
// which the refusal above already proved).
-}, 20000);
+});
test("lock refuses a store that was never wrapped, creating nothing; lock after unlock reports cleared", async () => {
const lock = await runCli(["brain", "secret", "lock", "--vault", vault]);
@@ -419,7 +419,7 @@ test("export --out writes the bundle; import restores it; collisions need --repl
expect(wrong.returncode).toBe(1);
expect(wrong.stderr).toContain("passphrase");
expect(existsSync(join(empty, ".open-second-brain", "secrets", "secrets.json"))).toBe(false);
-}, 20000);
+});
describe("secret refusals and help accuracy", () => {
test("an unset --passphrase-from-env var names both ingestion routes", async () => {
diff --git a/tests/cli/install-json-shape.test.ts b/tests/cli/install-json-shape.test.ts
index 6baf1d42..ec999b5b 100644
--- a/tests/cli/install-json-shape.test.ts
+++ b/tests/cli/install-json-shape.test.ts
@@ -83,13 +83,6 @@ async function payloadOf(args: ReadonlyArray<string>): Promise<Record<string, un
return JSON.parse(r.stdout) as Record<string, unknown>;
}
-/**
- * Every case here spawns a real `o2b` child, which costs about two seconds
- * of module graph on an unloaded machine. The default five would make this
- * file fail for being slow rather than for being wrong.
- */
-const CLI_TIMEOUT_MS = 30_000;
-
/** Sorted key list, so a failure names the field rather than a count. */
function keys(value: unknown): string {
return Object.keys(value as object)
@@ -98,138 +91,102 @@ function keys(value: unknown): string {
}
describe("every install --json payload declares its schema version", () => {
- test(
- "detect",
- async () => {
- const payload = await payloadOf(["install", "--json"]);
- expect(payload["schema_version"]).toBe(INSTALL_JSON_SCHEMA_VERSION);
- },
- CLI_TIMEOUT_MS,
- );
-
- test(
- "plan",
- async () => {
- const payload = await payloadOf(["install", "--target", "cursor", "--json"]);
- expect(payload["schema_version"]).toBe(INSTALL_JSON_SCHEMA_VERSION);
- },
- CLI_TIMEOUT_MS,
- );
-
- test(
- "apply - the one mode that changes the machine, and the one that had none",
- async () => {
- const payload = await payloadOf([
- "install",
- "--target",
- "cursor",
- "--apply",
- "--yes",
- "--json",
- ]);
- expect(payload["schema_version"]).toBe(INSTALL_JSON_SCHEMA_VERSION);
- },
- CLI_TIMEOUT_MS,
- );
-
- test(
- "verify",
- async () => {
- const payload = await payloadOf(["install", "--check", "--json"]);
- expect(payload["schema_version"]).toBe(INSTALL_JSON_SCHEMA_VERSION);
- },
- CLI_TIMEOUT_MS,
- );
+ test("detect", async () => {
+ const payload = await payloadOf(["install", "--json"]);
+ expect(payload["schema_version"]).toBe(INSTALL_JSON_SCHEMA_VERSION);
+ });
+
+ test("plan", async () => {
+ const payload = await payloadOf(["install", "--target", "cursor", "--json"]);
+ expect(payload["schema_version"]).toBe(INSTALL_JSON_SCHEMA_VERSION);
+ });
+
+ test("apply - the one mode that changes the machine, and the one that had none", async () => {
+ const payload = await payloadOf([
+ "install",
+ "--target",
+ "cursor",
+ "--apply",
+ "--yes",
+ "--json",
+ ]);
+ expect(payload["schema_version"]).toBe(INSTALL_JSON_SCHEMA_VERSION);
+ });
+
+ test("verify", async () => {
+ const payload = await payloadOf(["install", "--check", "--json"]);
+ expect(payload["schema_version"]).toBe(INSTALL_JSON_SCHEMA_VERSION);
+ });
});
describe("every install --json payload names its fields", () => {
- test(
- "detect carries a target row per adapter",
- async () => {
- const payload = await payloadOf(["install", "--json"]);
- expect(keys(payload)).toBe("schema_version,targets");
- const targets = payload["targets"] as ReadonlyArray<unknown>;
- expect(targets.length).toBeGreaterThan(0);
- for (const row of targets) expect(keys(row)).toBe("config_path,notes,status,target");
- },
- CLI_TIMEOUT_MS,
- );
-
- test(
- "plan carries the plan verbatim",
- async () => {
- const payload = await payloadOf(["install", "--target", "cursor", "--json"]);
- expect(keys(payload)).toBe("plan,schema_version");
- expect(keys(payload["plan"])).toBe("postNotes,steps,target");
- // `diff` is additive and present only when the file would change;
- // a first install into an empty home always changes it.
- for (const step of (payload["plan"] as { steps: ReadonlyArray<unknown> }).steps) {
- expect(keys(step)).toBe("diff,kind,path,preview");
- }
- },
- CLI_TIMEOUT_MS,
- );
-
- test(
- "apply carries the manifest it wrote, at the top level as before",
- async () => {
- const payload = await payloadOf([
- "install",
- "--target",
- "cursor",
- "--apply",
- "--yes",
- "--json",
- ]);
- // `schema_version` is ADDED beside the result's own fields rather than
- // nesting the result under a key: an existing consumer reading
- // `.manifest` or `.steps_executed` keeps working and gains a version,
- // which a re-nesting would have broken for no benefit.
- expect(keys(payload)).toContain("manifest");
- expect(keys(payload)).toContain("steps_executed");
- expect(keys(payload)).toContain("target");
- // t_b6bcd984: the consent verdict rides the CLI receipt (not the
- // sidecar manifest), additive under schema_version 1.
- expect(keys(payload)).toContain("consent");
- },
- CLI_TIMEOUT_MS,
- );
-
- test(
- "verify carries a verify row per adapter",
- async () => {
- const payload = await payloadOf(["install", "--check", "--json"]);
- expect(keys(payload)).toBe("schema_version,targets");
- const targets = payload["targets"] as ReadonlyArray<unknown>;
- expect(targets.length).toBeGreaterThan(0);
- for (const row of targets) expect(keys(row)).toBe("details,fix_hint,status,target");
- },
- CLI_TIMEOUT_MS,
- );
+ test("detect carries a target row per adapter", async () => {
+ const payload = await payloadOf(["install", "--json"]);
+ expect(keys(payload)).toBe("schema_version,targets");
+ const targets = payload["targets"] as ReadonlyArray<unknown>;
+ expect(targets.length).toBeGreaterThan(0);
+ for (const row of targets) expect(keys(row)).toBe("config_path,notes,status,target");
+ });
+
+ test("plan carries the plan verbatim", async () => {
+ const payload = await payloadOf(["install", "--target", "cursor", "--json"]);
+ expect(keys(payload)).toBe("plan,schema_version");
+ expect(keys(payload["plan"])).toBe("postNotes,steps,target");
+ // `diff` is additive and present only when the file would change;
+ // a first install into an empty home always changes it.
+ for (const step of (payload["plan"] as { steps: ReadonlyArray<unknown> }).steps) {
+ expect(keys(step)).toBe("diff,kind,path,preview");
+ }
+ });
+
+ test("apply carries the manifest it wrote, at the top level as before", async () => {
+ const payload = await payloadOf([
+ "install",
+ "--target",
+ "cursor",
+ "--apply",
+ "--yes",
+ "--json",
+ ]);
+ // `schema_version` is ADDED beside the result's own fields rather than
+ // nesting the result under a key: an existing consumer reading
+ // `.manifest` or `.steps_executed` keeps working and gains a version,
+ // which a re-nesting would have broken for no benefit.
+ expect(keys(payload)).toContain("manifest");
+ expect(keys(payload)).toContain("steps_executed");
+ expect(keys(payload)).toContain("target");
+ // t_b6bcd984: the consent verdict rides the CLI receipt (not the
+ // sidecar manifest), additive under schema_version 1.
+ expect(keys(payload)).toContain("consent");
+ });
+
+ test("verify carries a verify row per adapter", async () => {
+ const payload = await payloadOf(["install", "--check", "--json"]);
+ expect(keys(payload)).toBe("schema_version,targets");
+ const targets = payload["targets"] as ReadonlyArray<unknown>;
+ expect(targets.length).toBeGreaterThan(0);
+ for (const row of targets) expect(keys(row)).toBe("details,fix_hint,status,target");
+ });
});
describe("the opencode V2 config shape in the apply payload", () => {
- test(
- "apply to an existing mcp.servers config records the nested owned keys",
- async () => {
- const opencodeConfig = join(home, ".config", "opencode", "opencode.json");
- mkdirSync(dirname(opencodeConfig), { recursive: true });
- writeFileSync(opencodeConfig, JSON.stringify({ mcp: { servers: {} } }, null, 2) + "\n");
- const payload = await payloadOf([
- "install",
- "--target",
- "opencode",
- "--apply",
- "--yes",
- "--json",
- ]);
- const manifest = payload["manifest"] as Record<string, unknown>;
- expect(manifest["config_path"]).toBe(opencodeConfig);
- expect(manifest["owned_keys"]).toEqual([
- "mcp.servers.open-second-brain",
- "mcp.servers.open-second-brain-writer",
- ]);
- },
- CLI_TIMEOUT_MS,
- );
+ test("apply to an existing mcp.servers config records the nested owned keys", async () => {
+ const opencodeConfig = join(home, ".config", "opencode", "opencode.json");
+ mkdirSync(dirname(opencodeConfig), { recursive: true });
+ writeFileSync(opencodeConfig, JSON.stringify({ mcp: { servers: {} } }, null, 2) + "\n");
+ const payload = await payloadOf([
+ "install",
+ "--target",
+ "opencode",
+ "--apply",
+ "--yes",
+ "--json",
+ ]);
+ const manifest = payload["manifest"] as Record<string, unknown>;
+ expect(manifest["config_path"]).toBe(opencodeConfig);
+ expect(manifest["owned_keys"]).toEqual([
+ "mcp.servers.open-second-brain",
+ "mcp.servers.open-second-brain-writer",
+ ]);
+ });
});
diff --git a/tests/cli/install-ownership-close.test.ts b/tests/cli/install-ownership-close.test.ts
index 941e9023..1999e77e 100644
--- a/tests/cli/install-ownership-close.test.ts
+++ b/tests/cli/install-ownership-close.test.ts
@@ -58,100 +58,73 @@ function envBase() {
};
}
-/**
- * Every case here spawns real `o2b` children, which cost about two seconds
- * of module graph each. The default five would make this file fail for
- * being slow rather than for being wrong.
- */
-const CLI_TIMEOUT_MS = 30_000;
-
/** The heading the composed statement always opens with. */
const CLOSE_HEADING = "Your data, and where it is:";
describe("a successful apply closes by saying what the operator owns", () => {
- test(
- "the human surface names the resolved vault and the exceptions",
- async () => {
- const r = await runCli(["install", "--target", "cursor", "--apply", "--yes"], {
- env: envBase(),
- });
- expect(r.returncode).toBe(0);
- expect(r.stdout).toContain(CLOSE_HEADING);
- expect(r.stdout).toContain(vault);
- // The counterexample is named rather than omitted.
- expect(r.stdout).toContain("opencode session spool");
- },
- CLI_TIMEOUT_MS,
- );
-
- test(
- "the machine surface carries the same value under data_ownership",
- async () => {
- const r = await runCli(["install", "--target", "cursor", "--apply", "--yes", "--json"], {
- env: envBase(),
- });
- expect(r.returncode).toBe(0);
- const payload = JSON.parse(r.stdout) as Record<string, unknown>;
- const ownership = payload["data_ownership"] as Record<string, unknown>;
- expect(ownership).toBeDefined();
- expect(ownership["vault"]).toBe(vault);
- // A verdict is always present, and it is allowed to be `undetermined`.
- expect(typeof (ownership["backing"] as Record<string, unknown>)["state"]).toBe("string");
- expect((ownership["outside_vault"] as ReadonlyArray<unknown>).length).toBeGreaterThan(0);
- },
- CLI_TIMEOUT_MS,
- );
+ test("the human surface names the resolved vault and the exceptions", async () => {
+ const r = await runCli(["install", "--target", "cursor", "--apply", "--yes"], {
+ env: envBase(),
+ });
+ expect(r.returncode).toBe(0);
+ expect(r.stdout).toContain(CLOSE_HEADING);
+ expect(r.stdout).toContain(vault);
+ // The counterexample is named rather than omitted.
+ expect(r.stdout).toContain("opencode session spool");
+ });
+
+ test("the machine surface carries the same value under data_ownership", async () => {
+ const r = await runCli(["install", "--target", "cursor", "--apply", "--yes", "--json"], {
+ env: envBase(),
+ });
+ expect(r.returncode).toBe(0);
+ const payload = JSON.parse(r.stdout) as Record<string, unknown>;
+ const ownership = payload["data_ownership"] as Record<string, unknown>;
+ expect(ownership).toBeDefined();
+ expect(ownership["vault"]).toBe(vault);
+ // A verdict is always present, and it is allowed to be `undetermined`.
+ expect(typeof (ownership["backing"] as Record<string, unknown>)["state"]).toBe("string");
+ expect((ownership["outside_vault"] as ReadonlyArray<unknown>).length).toBeGreaterThan(0);
+ });
});
describe("a verify only closes when something verified ok", () => {
- test(
- "with nothing installed the close stays silent on both surfaces",
- async () => {
- const human = await runCli(["install", "--check"], { env: envBase() });
- expect(human.returncode).toBe(0);
- expect(human.stdout).toContain("not-installed");
- expect(human.stdout).not.toContain(CLOSE_HEADING);
-
- const machine = await runCli(["install", "--check", "--json"], { env: envBase() });
- expect(machine.returncode).toBe(0);
- expect(JSON.parse(machine.stdout)["data_ownership"]).toBeUndefined();
- },
- CLI_TIMEOUT_MS,
- );
-
- test(
- "after an install, a passing check closes on both surfaces",
- async () => {
- await runCli(["install", "--target", "cursor", "--apply", "--yes"], { env: envBase() });
-
- const human = await runCli(["install", "--check", "--target", "cursor"], { env: envBase() });
- expect(human.returncode).toBe(0);
- expect(human.stdout).toContain(CLOSE_HEADING);
-
- const machine = await runCli(["install", "--check", "--target", "cursor", "--json"], {
- env: envBase(),
- });
- expect(machine.returncode).toBe(0);
- const ownership = JSON.parse(machine.stdout)["data_ownership"] as Record<string, unknown>;
- expect(ownership["vault"]).toBe(vault);
- // Three subprocess CLI runs: an apply plus both check surfaces.
- },
- CLI_TIMEOUT_MS,
- );
+ test("with nothing installed the close stays silent on both surfaces", async () => {
+ const human = await runCli(["install", "--check"], { env: envBase() });
+ expect(human.returncode).toBe(0);
+ expect(human.stdout).toContain("not-installed");
+ expect(human.stdout).not.toContain(CLOSE_HEADING);
+
+ const machine = await runCli(["install", "--check", "--json"], { env: envBase() });
+ expect(machine.returncode).toBe(0);
+ expect(JSON.parse(machine.stdout)["data_ownership"]).toBeUndefined();
+ });
+
+ test("after an install, a passing check closes on both surfaces", async () => {
+ await runCli(["install", "--target", "cursor", "--apply", "--yes"], { env: envBase() });
+
+ const human = await runCli(["install", "--check", "--target", "cursor"], { env: envBase() });
+ expect(human.returncode).toBe(0);
+ expect(human.stdout).toContain(CLOSE_HEADING);
+
+ const machine = await runCli(["install", "--check", "--target", "cursor", "--json"], {
+ env: envBase(),
+ });
+ expect(machine.returncode).toBe(0);
+ const ownership = JSON.parse(machine.stdout)["data_ownership"] as Record<string, unknown>;
+ expect(ownership["vault"]).toBe(vault);
+ // Three subprocess CLI runs: an apply plus both check surfaces.
+ });
});
describe("the onboarding checklist carries the same field", () => {
- test(
- "o2b onboarding --json exposes data_ownership beside the steps",
- async () => {
- const r = await runCli(["onboarding", "--json"], { env: envBase() });
- expect(r.returncode).toBe(0);
- const payload = JSON.parse(r.stdout) as Record<string, unknown>;
- expect(payload["steps"]).toBeDefined();
- const ownership = payload["data_ownership"] as Record<string, unknown>;
- expect(ownership["vault"]).toBe(vault);
- expect(ownership["backing"]).toBeDefined();
- },
- CLI_TIMEOUT_MS,
- );
+ test("o2b onboarding --json exposes data_ownership beside the steps", async () => {
+ const r = await runCli(["onboarding", "--json"], { env: envBase() });
+ expect(r.returncode).toBe(0);
+ const payload = JSON.parse(r.stdout) as Record<string, unknown>;
+ expect(payload["steps"]).toBeDefined();
+ const ownership = payload["data_ownership"] as Record<string, unknown>;
+ expect(ownership["vault"]).toBe(vault);
+ expect(ownership["backing"]).toBeDefined();
+ });
});
diff --git a/tests/cli/secret-unlock-nudge.test.ts b/tests/cli/secret-unlock-nudge.test.ts
index 5b22ca7b..e891859c 100644
--- a/tests/cli/secret-unlock-nudge.test.ts
+++ b/tests/cli/secret-unlock-nudge.test.ts
@@ -55,7 +55,7 @@ describe("the unlock nudge", () => {
expect(unlock.stdout).toContain("unlocked");
expect(unlock.stderr).toContain("unrecoverable");
expect(unlock.stderr).toContain("o2b brain secret export");
- }, 20000);
+ });
test("a re-unlock prints the nudge too", async () => {
await seedSecret();
@@ -68,7 +68,7 @@ describe("the unlock nudge", () => {
});
expect(again.returncode).toBe(0);
expect(again.stderr).toContain("o2b brain secret export");
- }, 20000);
+ });
test("the other custody ops are unchanged: set prints no nudge", async () => {
const set = await runCli(["brain", "secret", "set", "plain", "--vault", vault], {
@@ -76,5 +76,5 @@ describe("the unlock nudge", () => {
});
expect(set.returncode).toBe(0);
expect(set.stderr).not.toContain("o2b brain secret export");
- }, 20000);
+ });
});
diff --git a/tests/cli/store-shard.test.ts b/tests/cli/store-shard.test.ts
index a75466a7..9584c30f 100644
--- a/tests/cli/store-shard.test.ts
+++ b/tests/cli/store-shard.test.ts
@@ -360,7 +360,6 @@ describe("o2b store shard", () => {
expect(r.returncode).toBe(1);
expect((JSON.parse(r.stdout) as { reason: string }).reason).toBe("shard-unreadable");
},
- 30_000,
);
test.skipIf(CHMOD_CANNOT_DENY)(
@@ -383,7 +382,6 @@ describe("o2b store shard", () => {
const listed = await cli(["list", "--json"]);
expect((JSON.parse(listed.stdout) as { lines: unknown[] }).lines).toEqual([]);
},
- 30_000,
);
test("a corrupt line too long to hash is refused as line-too-long, with no advice to retry", async () => {
diff --git a/tests/core/agent-context/request-floor-hooks.test.ts b/tests/core/agent-context/request-floor-hooks.test.ts
index 1194a0a9..5ece7df2 100644
--- a/tests/core/agent-context/request-floor-hooks.test.ts
+++ b/tests/core/agent-context/request-floor-hooks.test.ts
@@ -2,7 +2,8 @@
* The minimum request time in the hook uses (design D20): `head_select`
* and `neighbour_relevant` start no request when less than
* `MIN_REQUEST_MS` of the hook budget is left, and keep the
- * deterministic fallback.
+ * deterministic fallback. The floor holds for each attempt of the
+ * provider chain, not only for the hook budget as a whole.
*/
import { afterEach, describe, expect, test } from "bun:test";
@@ -18,7 +19,8 @@ import { foldTopics, type TopicState } from "../../../src/core/topics/fold.ts";
import type { StoreRecord } from "../../../src/core/store/envelope.ts";
import { formatHlc } from "../../../src/core/store/hlc.ts";
import { FAKE_DECISION_KEY } from "../../helpers/fake-credentials.ts";
-import { FakeDecisionProvider } from "../../helpers/fake-decision-provider.ts";
+import { listDecisionModelCalls } from "../../../src/core/decision-model/record.ts";
+import { FakeDecisionProvider, startFakeSystemOne } from "../../helpers/fake-decision-provider.ts";
const dirs: string[] = [];
afterEach(() => {
@@ -26,6 +28,12 @@ afterEach(() => {
});
const TOPIC = "01KA0000000000000000000001";
+/**
+ * Hook time well above the floor: the budget runs from the request's
+ * start, so the few milliseconds spent preparing it count, and an exact
+ * floor would leave the request to chance.
+ */
+const ENOUGH_MS = MIN_REQUEST_MS * 4;
function config(uses: string, hookBudgetMs?: number) {
const vault = mkdtempSync(join(tmpdir(), "osb-floor-hooks-"));
@@ -49,6 +57,48 @@ function config(uses: string, hookBudgetMs?: number) {
});
}
+/**
+ * A two-entry chain: a loopback server that never answers first, one
+ * that answers second. Returns the config, its vault and both servers.
+ */
+async function hangingChain(uses: string) {
+ const vault = mkdtempSync(join(tmpdir(), "osb-floor-hooks-"));
+ mkdirSync(join(vault, "Brain"), { recursive: true });
+ dirs.push(vault);
+ const hang = await startFakeSystemOne();
+ const fast = await startFakeSystemOne();
+ hang.setReply(() => ({ hang: true }));
+ const env = {
+ O2B_TEST_FLOOR_KEY: FAKE_DECISION_KEY,
+ O2B_TEST_FLOOR_SECOND_KEY: FAKE_DECISION_KEY,
+ } as NodeJS.ProcessEnv;
+ const cfg = resolveDecisionModelConfig({
+ env,
+ config: {
+ decision_model_enabled: "true",
+ decision_model_providers: "compatible,typesafe",
+ decision_model_base_url: hang.url,
+ decision_model_id: "floor-model",
+ decision_model_env_key: "O2B_TEST_FLOOR_KEY",
+ decision_model_providers_typesafe_base_url: fast.url,
+ decision_model_providers_typesafe_id: "floor-fallback",
+ decision_model_providers_typesafe_env_key: "O2B_TEST_FLOOR_SECOND_KEY",
+ decision_model_uses: uses,
+ },
+ vault,
+ thresholdsPath: null,
+ });
+ return { cfg, env, vault, hang, fast };
+}
+
+/** Waits (bounded) until the vault holds `count` call records. */
+async function recordsLanded(vault: string, count: number): Promise<void> {
+ for (let i = 0; i < 500 && listDecisionModelCalls(vault).length < count; i++) {
+ // oxlint-disable-next-line no-await-in-loop -- polling a background write
+ await new Promise((r) => setTimeout(r, 10));
+ }
+}
+
function head(): TopicState {
const record = {
v: 1,
@@ -66,14 +116,14 @@ function head(): TopicState {
}
describe("the minimum request time in the hook uses", () => {
- test("head_select starts a request with the minimum left and none with less", async () => {
+ test("head_select starts a request with time to spare and none with less than the minimum", async () => {
const cfg = config("head_select:shadow");
const enough = new FakeDecisionProvider({});
await runHeadSelect({
config: cfg,
heads: [head()],
request: { project: "o2b", branch: null },
- remainingMs: MIN_REQUEST_MS,
+ remainingMs: ENOUGH_MS,
provider: enough,
});
expect(enough.requests).toHaveLength(1);
@@ -108,7 +158,7 @@ describe("the minimum request time in the hook uses", () => {
config: cfg,
candidates: [candidate],
prompt: "what changed in the shard writer",
- remainingMs: MIN_REQUEST_MS,
+ remainingMs: ENOUGH_MS,
provider: enough,
});
expect(enough.requests).toHaveLength(1);
@@ -158,4 +208,57 @@ describe("the minimum request time in the hook uses", () => {
});
expect(neighbours.requests).toEqual([]);
});
+
+ test("each attempt of the chain gets the floor: an entry whose share would fall below it is not asked", async () => {
+ // Room for one attempt: an even split would give each entry less
+ // than the floor, so the first gets it all and the second none.
+ const remainingMs = MIN_REQUEST_MS * 2 - 1;
+ const heads = await hangingChain("head_select:shadow");
+ try {
+ const out = await runHeadSelect({
+ config: heads.cfg,
+ heads: [head()],
+ request: { project: "o2b", branch: null },
+ remainingMs,
+ env: heads.env,
+ });
+ expect(out.applied).toBe(false);
+ await recordsLanded(heads.vault, 1);
+ expect(heads.hang.requests).toHaveLength(1);
+ expect(heads.fast.requests).toHaveLength(0);
+ } finally {
+ await heads.hang.close();
+ await heads.fast.close();
+ }
+ const neighbours = await hangingChain("neighbour_relevant:shadow");
+ try {
+ const out = await runNeighbourRelevant({
+ config: neighbours.cfg,
+ candidates: [
+ {
+ record: "rec-2",
+ kind: "log-entry",
+ topic: TOPIC,
+ topicTitle: "Shard writer",
+ text: "The shard writer refactor landed",
+ seq: 1,
+ device: "dev-a",
+ agent: "seed",
+ private: false,
+ titlePrivate: false,
+ },
+ ],
+ prompt: "what changed in the shard writer",
+ remainingMs,
+ env: neighbours.env,
+ });
+ expect(out.applied).toBe(false);
+ await recordsLanded(neighbours.vault, 1);
+ expect(neighbours.hang.requests).toHaveLength(1);
+ expect(neighbours.fast.requests).toHaveLength(0);
+ } finally {
+ await neighbours.hang.close();
+ await neighbours.fast.close();
+ }
+ });
});
diff --git a/tests/core/brain/bundle-wildcard-warning.test.ts b/tests/core/brain/bundle-wildcard-warning.test.ts
index 445cd82b..ac46daa5 100644
--- a/tests/core/brain/bundle-wildcard-warning.test.ts
+++ b/tests/core/brain/bundle-wildcard-warning.test.ts
@@ -76,7 +76,7 @@ describe("the bundle import wildcard-allowlist warning", () => {
expect(result.stderr).toContain("warning");
expect(result.stderr).toContain('"open-entry"');
expect(result.stderr).not.toContain('"narrow-entry"');
- }, 20000);
+ });
test("a non-wildcard bundle imports silently and works", async () => {
await setSecret("narrow-entry", ["bun -e *"]);
@@ -90,7 +90,7 @@ describe("the bundle import wildcard-allowlist warning", () => {
const list = await runCli(["brain", "secret", "list", "--vault", vault]);
expect(list.returncode).toBe(0);
expect(list.stdout).toContain("narrow-entry");
- }, 20000);
+ });
test("an edited (widened) bundle still refuses on its AAD, warning or not", async () => {
await setSecret("narrow-entry", ["bun -e *"]);
@@ -106,7 +106,7 @@ describe("the bundle import wildcard-allowlist warning", () => {
const result = await importBundle(bundle);
expect(result.returncode).toBe(1);
expect(result.stderr).toContain("refus");
- }, 20000);
+ });
});
/**
diff --git a/tests/core/brain/recall-inject-decision.test.ts b/tests/core/brain/recall-inject-decision.test.ts
index 9c4803eb..031e8b94 100644
--- a/tests/core/brain/recall-inject-decision.test.ts
+++ b/tests/core/brain/recall-inject-decision.test.ts
@@ -589,6 +589,28 @@ describe("abstentions and errors never trigger a request", () => {
});
});
+describe("the caller's bound on the decision wait (the hook ceiling)", () => {
+ test("with no wait left the brief goes out unfiltered and the model is not asked", async () => {
+ const vault = tempVault();
+ const provider = new FakeDecisionProvider({ answer: answerBy({ b: 0.05 }) });
+ const filter = createRecallInjectDecisionFilter({
+ config: config(vault, "enforce"),
+ vault,
+ provider,
+ });
+ const today = await decide(THREE, null);
+ const bounded = await decideRecallInject(PROMPT, retriever(THREE, {}), {
+ decisionFilter: filter!,
+ decisionWaitLeftMs: () => 0,
+ });
+ expect(bounded.kind === "inject" && today.kind === "inject").toBe(true);
+ if (bounded.kind !== "inject" || today.kind !== "inject") return;
+ expect(bounded.brief).toBe(today.brief);
+ expect(bounded.decisionModel).toMatchObject({ outcome: "degraded", degradeReason: "timeout" });
+ expect(provider.requests).toHaveLength(0);
+ });
+});
+
describe("telemetry, audit and record hold no prompt or note text", () => {
test("shadow, enforce drop and enforce abstain", async () => {
for (const [mode, injectAny] of [
diff --git a/tests/core/decision-model/providers-chain.test.ts b/tests/core/decision-model/providers-chain.test.ts
index 412b62f6..f6dd3b86 100644
--- a/tests/core/decision-model/providers-chain.test.ts
+++ b/tests/core/decision-model/providers-chain.test.ts
@@ -23,6 +23,7 @@ import {
decisionModelCheckExitCode,
renderDecisionModelCheck,
} from "../../../src/core/decision-model/diagnostics.ts";
+import { MIN_REQUEST_MS } from "../../../src/core/decision-model/request-floor.ts";
import { runDecision } from "../../../src/core/decision-model/run.ts";
import { listDecisionModelCalls } from "../../../src/core/decision-model/record.ts";
import { FAKE_DECISION_KEY, fakeCredential } from "../../helpers/fake-credentials.ts";
@@ -287,6 +288,43 @@ describe("chain fallback in runDecision", () => {
}
});
+ test("with the request floor, an entry whose share would fall below it is not attempted", async () => {
+ const vault = tempVault();
+ const hang = await startFakeSystemOne();
+ const fast = await startFakeSystemOne();
+ hang.setReply(() => ({ hang: true }));
+ try {
+ const cfg = resolveDecisionModelConfig({
+ env: CHAIN_ENV,
+ config: chainConfig({
+ decision_model_base_url: hang.url,
+ decision_model_providers_typesafe_base_url: fast.url,
+ decision_model_providers_typesafe_id: "fake-model-1.0",
+ decision_model_uses: "rerank:shadow",
+ }),
+ vault,
+ });
+ // Room for one attempt only: an even split would give each entry
+ // less than the floor. The first entry gets the whole budget, and
+ // once it hangs through it nothing is left for the second.
+ const result = await runDecision("rerank", build, QUESTIONS, {
+ config: cfg,
+ env: CHAIN_ENV,
+ timeoutMs: MIN_REQUEST_MS * 2 - 1,
+ requestFloor: true,
+ });
+ expect(result).toMatchObject({ status: "degraded", reason: "timeout" });
+ expect(hang.requests).toHaveLength(1);
+ expect(fast.requests).toHaveLength(0);
+ const records = listDecisionModelCalls(vault);
+ expect(records).toHaveLength(1);
+ expect(records[0]?.payload).toMatchObject({ provider: "compatible", outcome: "timeout" });
+ } finally {
+ await hang.close();
+ await fast.close();
+ }
+ });
+
test("an HTTP failure is an answer, not a fall-through: the next entry is not asked", async () => {
const vault = tempVault();
const refusing = await startFakeSystemOne();
diff --git a/tests/core/decision-model/request-floor.test.ts b/tests/core/decision-model/request-floor.test.ts
index 163d8004..869a3264 100644
--- a/tests/core/decision-model/request-floor.test.ts
+++ b/tests/core/decision-model/request-floor.test.ts
@@ -6,7 +6,11 @@
import { describe, expect, test } from "bun:test";
-import { MIN_REQUEST_MS, mayStartRequest } from "../../../src/core/decision-model/request-floor.ts";
+import {
+ MIN_REQUEST_MS,
+ attemptTimeoutMs,
+ mayStartRequest,
+} from "../../../src/core/decision-model/request-floor.ts";
describe("mayStartRequest", () => {
const until = 10_000;
@@ -37,3 +41,37 @@ describe("mayStartRequest", () => {
expect(MIN_REQUEST_MS).toBe(250);
});
});
+
+describe("attemptTimeoutMs: the floor for each attempt of a fallback chain", () => {
+ test("an even share while every remaining entry can get the floor", () => {
+ expect(attemptTimeoutMs(3000, 2)).toBe(1500);
+ expect(attemptTimeoutMs(MIN_REQUEST_MS * 2, 2)).toBe(MIN_REQUEST_MS);
+ });
+
+ test("an entry that would get less is not attempted: its time goes to the entries that are", () => {
+ // Two entries, room for one: the first gets the whole budget, not half.
+ expect(attemptTimeoutMs(MIN_REQUEST_MS * 2 - 1, 2)).toBe(MIN_REQUEST_MS * 2 - 1);
+ expect(attemptTimeoutMs(MIN_REQUEST_MS * 3 - 1, 3)).toBe(
+ Math.floor((MIN_REQUEST_MS * 3 - 1) / 2),
+ );
+ });
+
+ test("no attempt at all with less than the floor left", () => {
+ expect(attemptTimeoutMs(MIN_REQUEST_MS - 1, 1)).toBeNull();
+ expect(attemptTimeoutMs(MIN_REQUEST_MS - 1, 3)).toBeNull();
+ expect(attemptTimeoutMs(0, 1)).toBeNull();
+ expect(attemptTimeoutMs(-5, 1)).toBeNull();
+ });
+
+ test("never a share below the floor", () => {
+ for (let left = 0; left < MIN_REQUEST_MS * 5; left += 7) {
+ for (let entries = 1; entries <= 4; entries++) {
+ const ms = attemptTimeoutMs(left, entries);
+ if (ms !== null) {
+ expect(ms).toBeGreaterThanOrEqual(MIN_REQUEST_MS);
+ expect(ms).toBeLessThanOrEqual(left);
+ }
+ }
+ }
+ });
+});
diff --git a/tests/core/decision-model/run.test.ts b/tests/core/decision-model/run.test.ts
index 1b1f7390..85f908b3 100644
--- a/tests/core/decision-model/run.test.ts
+++ b/tests/core/decision-model/run.test.ts
@@ -7,6 +7,7 @@ import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
+import { MIN_REQUEST_MS } from "../../../src/core/decision-model/request-floor.ts";
import { runDecision } from "../../../src/core/decision-model/run.ts";
import { buildCandidateState } from "../../../src/core/decision-model/state.ts";
import {
@@ -34,6 +35,9 @@ function tempVault(): string {
return v;
}
+/** Time a state builder spends preparing in the floor test. */
+const PREPARATION_MS = 60;
+
const SECRET_TEXT = "the passage text that must never be recorded";
function build() {
@@ -423,3 +427,86 @@ describe("state builder", () => {
expect(res.kind).toBe("budget");
});
});
+
+describe("the minimum request time (requestFloor)", () => {
+ test("a budget below the floor sends nothing: a budget skip, with no record", async () => {
+ const vault = tempVault();
+ const provider = new FakeDecisionProvider();
+ let built = 0;
+ const result = await runDecision(
+ "rerank",
+ () => {
+ built++;
+ return build();
+ },
+ QUESTIONS,
+ {
+ config: activeDecisionConfig({ vault }),
+ provider,
+ timeoutMs: MIN_REQUEST_MS - 1,
+ requestFloor: true,
+ },
+ );
+ expect(result).toMatchObject({ status: "degraded", reason: "budget" });
+ expect(built).toBe(0);
+ expect(provider.requests).toHaveLength(0);
+ expect(listDecisionModelCalls(vault)).toHaveLength(0);
+ });
+
+ test("time spent building the state counts: the floor is checked again before the request", async () => {
+ const vault = tempVault();
+ const provider = new FakeDecisionProvider();
+ const result = await runDecision(
+ "rerank",
+ () => {
+ // Preparation eats the margin above the floor: what is left
+ // after it is below the floor, so no request starts.
+ Bun.sleepSync(PREPARATION_MS);
+ return build();
+ },
+ QUESTIONS,
+ {
+ config: activeDecisionConfig({ vault }),
+ provider,
+ timeoutMs: MIN_REQUEST_MS + PREPARATION_MS - 1,
+ requestFloor: true,
+ },
+ );
+ expect(result).toMatchObject({ status: "degraded", reason: "budget" });
+ expect(provider.requests).toHaveLength(0);
+ });
+
+ test("time spent building the provider chain counts too", async () => {
+ const vault = tempVault();
+ const provider = new FakeDecisionProvider();
+ // A provider list that is slow to read: the chain is built from it,
+ // and that cost belongs to the call's budget like the state's.
+ const config = Object.defineProperty(activeDecisionConfig({ vault }), "providers", {
+ get: () => {
+ Bun.sleepSync(PREPARATION_MS);
+ return undefined;
+ },
+ });
+ const result = await runDecision("rerank", build, QUESTIONS, {
+ config,
+ provider,
+ timeoutMs: MIN_REQUEST_MS + PREPARATION_MS - 1,
+ requestFloor: true,
+ });
+ expect(result).toMatchObject({ status: "degraded", reason: "budget" });
+ expect(provider.requests).toHaveLength(0);
+ });
+
+ test("with the floor met the request runs as before", async () => {
+ const vault = tempVault();
+ const provider = new FakeDecisionProvider();
+ const result = await runDecision("rerank", build, QUESTIONS, {
+ config: activeDecisionConfig({ vault }),
+ provider,
+ timeoutMs: MIN_REQUEST_MS * 4,
+ requestFloor: true,
+ });
+ expect(result.status).toBe("ok");
+ expect(provider.requests).toHaveLength(1);
+ });
+});
diff --git a/tests/core/topics/decide-human-readable.test.ts b/tests/core/topics/decide-human-readable.test.ts
index 9de6a794..b7cc2665 100644
--- a/tests/core/topics/decide-human-readable.test.ts
+++ b/tests/core/topics/decide-human-readable.test.ts
@@ -33,6 +33,8 @@ import { FakeDecisionProvider } from "../../helpers/fake-decision-provider.ts";
const T0 = Date.UTC(2026, 11, 1, 9, 0, 0);
let clock = T0;
const now = () => (clock += 1000);
+/** Does not advance on read: the decide pass's budget clock. */
+const still = (): number => clock;
let m: Machines;
let a: StoreHandle;
@@ -127,7 +129,7 @@ function asked(fake: FakeDecisionProvider): string[] {
/** One enforce pass that keeps every note, applied under the planning lock. */
async function keepAllPass(): Promise<FakeDecisionProvider> {
const fake = new FakeDecisionProvider({ answer: () => 0.9 });
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now: still });
runTopicPlanning(a, { agent: "dream", now, topicVerdicts: verdicts });
return fake;
}
@@ -141,7 +143,7 @@ describe("human_readable in the decide pass", () => {
const fake = new FakeDecisionProvider({
answer: (id) => (id.startsWith("history_") ? 0.1 : undefined),
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("off"), now });
+ const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("off"), now: still });
expect([...verdicts.hidden.keys()]).toEqual([]);
expect(fake.requests).toEqual([]);
expect(listDecisionModelCalls(vault)).toEqual([]);
@@ -161,7 +163,11 @@ describe("human_readable in the decide pass", () => {
answer: (id) => (id.startsWith("history_") ? 0.1 : undefined),
model: "hist-shadow-1",
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("shadow"),
+ now: still,
+ });
expect([...verdicts.hidden.keys()]).toEqual([]);
expect(fake.requests).toHaveLength(1);
expect(fake.requests[0]!.use).toBe("human_readable");
@@ -190,7 +196,11 @@ describe("human_readable in the decide pass", () => {
},
model: "hist-model-4",
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("enforce"),
+ now: still,
+ });
// The model read both notes; only the low-probability one is hidden.
expect(Object.keys(fake.requests[0]!.questions)).toHaveLength(2);
expect([...verdicts.hidden.keys()]).toEqual([routine]);
@@ -219,7 +229,7 @@ describe("human_readable in the decide pass", () => {
answer: (id) => (id.startsWith("history_") ? 0.5 : undefined),
}),
env: envFor("enforce"),
- now,
+ now: still,
});
expect([...low.hidden.keys()]).toEqual([]);
const timedOut = await decideTopicPlan(a, {
@@ -228,7 +238,7 @@ describe("human_readable in the decide pass", () => {
fail: "timeout",
}),
env: envFor("enforce"),
- now,
+ now: still,
});
expect([...timedOut.hidden.keys()]).toEqual([]);
expect(runTopicPlanning(a, { agent: "dream", now, topicVerdicts: low }).status).toBe("planned");
@@ -251,7 +261,7 @@ describe("human_readable in the decide pass", () => {
{ now },
);
const fake = new FakeDecisionProvider({ answer: () => 0.1 });
- await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now });
+ await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now: still });
expect(fake.requests).toEqual([]);
// The page still hides the note: the stored verdict decides.
expect(pageMentions("Routine heartbeat of the nightly compaction job")).toBe(false);
@@ -280,7 +290,7 @@ describe("human_readable in the decide pass", () => {
for (let i = 0; i < 50; i++) note(`Future dated note ${i}`, MONTH_AHEAD + i * 60_000);
note("A present note from this week", WEEK_AGO);
const fake = new FakeDecisionProvider({});
- await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now });
+ await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now: still });
const first = asked(fake);
expect(first).toHaveLength(50);
expect(first[0]).toBe("A present note from this week");
@@ -289,7 +299,7 @@ describe("human_readable in the decide pass", () => {
test("a note older than eight weeks is never asked about", async () => {
note("A very old note from the summer sprint", T0 - 10 * 7 * 24 * 60 * 60 * 1000);
const fake = new FakeDecisionProvider({});
- await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now });
+ await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now: still });
expect(fake.requests).toEqual([]);
});
});
@@ -301,7 +311,11 @@ describe("hide_below from the thresholds store", () => {
const fake = new FakeDecisionProvider({
answer: (qid) => (qid.startsWith("history_") ? 0.2 : undefined),
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("enforce"),
+ now: still,
+ });
// The built-in 0.3 would hide at 0.2; the stored 0.05 decides.
expect([...verdicts.hidden.keys()]).toEqual([]);
expect([...verdicts.kept.keys()]).toEqual([id]);
@@ -318,7 +332,7 @@ describe("history verdicts under the lock", () => {
const first = await decideTopicPlan(a, {
provider: new FakeDecisionProvider({ answer: () => 0.9 }),
env: envFor("enforce"),
- now,
+ now: still,
});
// human_readable keeps no asked entry: a note is skipped only once a
// stored verdict judges it, and none is written before the lock, so
@@ -327,7 +341,7 @@ describe("history verdicts under the lock", () => {
const second = await decideTopicPlan(a, {
provider: secondProvider,
env: envFor("enforce"),
- now,
+ now: still,
});
expect(asked(secondProvider)).toHaveLength(2);
expect(second.hidden.size).toBe(2);
diff --git a/tests/core/topics/decide-pass-budget.test.ts b/tests/core/topics/decide-pass-budget.test.ts
index 787f7ea3..55f3117c 100644
--- a/tests/core/topics/decide-pass-budget.test.ts
+++ b/tests/core/topics/decide-pass-budget.test.ts
@@ -195,11 +195,19 @@ describe("the decide pass budget", () => {
now: () => clock,
});
// topic_route gets 2000 / 7 uses, rounded up; it spends nothing on
- // this clock, so topic_same gets 2000 / 6.
- expect(timeouts).toEqual([
- { use: "topic_route", ms: Math.ceil(deadlineMs / 7) },
- { use: "topic_same", ms: Math.ceil(deadlineMs / 6) },
- ]);
+ // this clock, so topic_same gets 2000 / 6. The runner measures its
+ // own budget on the wall clock from the call's start, so a request
+ // may get a few milliseconds less than its share, never more, and
+ // never less than the minimum request time.
+ expect(timeouts.map((t) => t.use)).toEqual(["topic_route", "topic_same"]);
+ const allowance = {
+ topic_route: Math.ceil(deadlineMs / 7),
+ topic_same: Math.ceil(deadlineMs / 6),
+ };
+ for (const { use, ms } of timeouts) {
+ expect(ms).toBeGreaterThanOrEqual(MIN_REQUEST_MS);
+ expect(ms).toBeLessThanOrEqual(allowance[use as keyof typeof allowance]);
+ }
});
test("topic_same stops starting pair requests when its share is spent", async () => {
@@ -304,6 +312,9 @@ describe("the decide pass budget", () => {
return fake.requests.map((r) => r.use);
};
expect(await pass(MIN_REQUEST_MS - 1)).toEqual([]);
- expect(await pass(MIN_REQUEST_MS)).toEqual(["human_readable"]);
+ // The runner counts its own wall time from the call's start (the
+ // preparation counts), so a share of exactly the minimum may come out
+ // a millisecond short; the exact boundary is `mayStartRequest`'s test.
+ expect(await pass(MIN_REQUEST_MS * 2)).toEqual(["human_readable"]);
});
});
diff --git a/tests/core/topics/decide-privacy-matrix.test.ts b/tests/core/topics/decide-privacy-matrix.test.ts
index 4c3450da..9864f8d1 100644
--- a/tests/core/topics/decide-privacy-matrix.test.ts
+++ b/tests/core/topics/decide-privacy-matrix.test.ts
@@ -252,6 +252,12 @@ interface Kind {
* holds a second subject or whose input the egress rule withholds.
*/
readonly cells?: Partial<Record<Variant, Record<Share, Partition[]>>>;
+ /**
+ * The partitions of every decided record under a hosted provider, for
+ * the variants whose private input the egress rule withholds: the
+ * subject is not asked (`[]`), or it is asked without that input.
+ */
+ readonly hosted?: Partial<Record<Variant, Partition[]>>;
seed(variant: Variant): void;
answer(id: string, req: DecisionRequest): number | string | undefined;
}
@@ -262,6 +268,9 @@ const KINDS: ReadonlyArray<Kind> = [
use: "topic_route",
threshold: { route_min: 0.5 },
writes: TOPIC_ASSIGN_KIND,
+ // A private topic is no option and a private subject is not asked;
+ // withheld evidence leaves the shared subject to decide in shared.
+ hosted: { topic: [], subject: [], region: [], evidence: ["shared"] },
variants: [
"none",
"topic",
@@ -309,6 +318,8 @@ const KINDS: ReadonlyArray<Kind> = [
use: "event_kind",
threshold: { kind_min: 0.5 },
writes: TOPIC_DECISION_KIND,
+ // A note of a private topic still derives, into the private partition.
+ hosted: { topic: ["private"], subject: [], region: [], evidence: ["shared"] },
variants: [
"none",
"topic",
@@ -359,6 +370,7 @@ const KINDS: ReadonlyArray<Kind> = [
use: "question_resolved",
threshold: { resolved_min: 0.5 },
writes: "question-resolved",
+ hosted: { topic: [], subject: [], item: [], evidence: ["shared"] },
variants: ["none", "topic", "subject", "item", "evidence", "region", "withheld-field"],
hostedSends: ["region"],
// One request per record: no other subject rides it.
@@ -403,6 +415,7 @@ const KINDS: ReadonlyArray<Kind> = [
use: "milestone_done",
threshold: { done_min: 0.5 },
writes: "milestone-done",
+ hosted: { topic: [], item: [], evidence: [], region: [] },
variants: ["none", "topic", "item", "evidence", "region", "side-region"],
hostedSends: ["side-region"],
seed(variant) {
@@ -450,6 +463,9 @@ const KINDS: ReadonlyArray<Kind> = [
use: "topic_new",
threshold: { veto_below: 0.3 },
writes: TOPIC_CREATE_KIND,
+ // A group that holds a private record (withheld from the request)
+ // creates its topic in the private partition.
+ hosted: { subject: ["private"], region: ["private"] },
variants: ["none", "subject", "region"],
seed(variant) {
const texts = [
@@ -532,9 +548,19 @@ describe("the partition of every record a dream-time use writes", () => {
return;
}
if (share === "hosted") {
- if (variant === "none") expect(decidedIn(kind)).toEqual(["shared"]);
- if (kind.hostedSends?.includes(variant)) expect(decidedIn(kind)).toEqual(["private"]);
- if (kind.absent?.includes(variant)) expect(decidedIn(kind)).toEqual([]);
+ // Every hosted cell names its partitions: the text checks
+ // above cannot see a shared verdict that holds only private
+ // record identifiers.
+ const expected: Partition[] | undefined =
+ variant === "none"
+ ? ["shared"]
+ : kind.hostedSends?.includes(variant)
+ ? ["private"]
+ : kind.absent?.includes(variant)
+ ? []
+ : kind.hosted?.[variant];
+ expect(expected).toBeDefined();
+ expect(decidedIn(kind)).toEqual(expected!);
return;
}
const expected: Partition[] = kind.absent?.includes(variant)
diff --git a/tests/core/topics/decide-question-resolved.test.ts b/tests/core/topics/decide-question-resolved.test.ts
index 96c273db..b15af940 100644
--- a/tests/core/topics/decide-question-resolved.test.ts
+++ b/tests/core/topics/decide-question-resolved.test.ts
@@ -42,6 +42,8 @@ import { FakeDecisionProvider } from "../../helpers/fake-decision-provider.ts";
const T0 = Date.UTC(2026, 11, 1, 9, 0, 0);
let clock = T0;
const now = () => (clock += 1000);
+/** Does not advance on read: the decide pass's budget clock. */
+const still = (): number => clock;
let m: Machines;
let a: StoreHandle;
@@ -236,7 +238,11 @@ describe("question_resolved after a merge", () => {
answer: (id) => (id.startsWith("answers_") ? "Q1" : undefined),
probability: 0.9,
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("enforce"),
+ now: still,
+ });
runTopicPlanning(a, { agent: "dream", now, topicVerdicts: verdicts });
const written = readPartition(join(m.root("dev-a"), "shared"))
.flatMap((s) => s.records)
@@ -270,7 +276,7 @@ describe("question_resolved in the decide pass", () => {
...envFor("shadow"),
OPEN_SECOND_BRAIN_DECISION_MODEL_MAX_STATE_TOKENS: String(maxStateTokens),
},
- now,
+ now: still,
});
for (const request of fake.requests) {
expect(estimateTokens(request.state) + QUESTION_TOKEN_ALLOWANCE).toBeLessThanOrEqual(
@@ -287,7 +293,7 @@ describe("question_resolved in the decide pass", () => {
answer: (id) => (id.startsWith("answers_") ? "Q0" : undefined),
probability: 0.9,
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("off"), now });
+ const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("off"), now: still });
expect([...verdicts.resolutions.keys()]).toEqual([]);
expect(fake.requests).toEqual([]);
expect(listDecisionModelCalls(vault)).toEqual([]);
@@ -303,7 +309,11 @@ describe("question_resolved in the decide pass", () => {
probability: 0.9,
model: "res-shadow-1",
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("shadow"),
+ now: still,
+ });
expect([...verdicts.resolutions.keys()]).toEqual([]);
expect(fake.requests).toHaveLength(1);
expect(fake.requests[0]!.use).toBe("question_resolved");
@@ -328,7 +338,11 @@ describe("question_resolved in the decide pass", () => {
answer: (id) => (id.startsWith("answers_") ? "Q0" : undefined),
probability: 0.9,
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("enforce"),
+ now: still,
+ });
expect(verdicts.resolutions.get(record)).toMatchObject({ topic, question });
expect(verdicts.resolutions.get(second)).toMatchObject({ topic, question });
const report = runTopicPlanning(a, { agent: "dream", now, topicVerdicts: verdicts });
@@ -344,7 +358,11 @@ describe("question_resolved in the decide pass", () => {
probability: 0.9,
model: "res-model-2",
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("enforce"),
+ now: still,
+ });
expect(verdicts.resolutions.get(record)).toMatchObject({ topic, question });
const report = runTopicPlanning(a, { agent: "dream", now, topicVerdicts: verdicts });
expect(report.status === "planned" ? report.resolutions : -1).toBe(1);
@@ -381,7 +399,7 @@ describe("question_resolved in the decide pass", () => {
const timedOut = await decideTopicPlan(a, {
provider: timeoutFake,
env: envFor("enforce"),
- now,
+ now: still,
});
expect(timeoutFake.requests).toHaveLength(1);
expect([...timedOut.resolutions.keys()]).toEqual([]);
@@ -390,7 +408,7 @@ describe("question_resolved in the decide pass", () => {
answer: (id) => (id.startsWith("answers_") ? "Q0" : undefined),
probability: 0.5,
});
- const low = await decideTopicPlan(a, { provider: lowFake, env: envFor("enforce"), now });
+ const low = await decideTopicPlan(a, { provider: lowFake, env: envFor("enforce"), now: still });
expect(lowFake.requests).toHaveLength(1);
expect([...low.resolutions.keys()]).toEqual([]);
// That answer entered the record in the asked set: `none` gets a record of its own.
@@ -400,7 +418,11 @@ describe("question_resolved in the decide pass", () => {
answer: (id) => (id.startsWith("answers_") ? "none" : undefined),
probability: 0.95,
});
- const none = await decideTopicPlan(a, { provider: noneFake, env: envFor("enforce"), now });
+ const none = await decideTopicPlan(a, {
+ provider: noneFake,
+ env: envFor("enforce"),
+ now: still,
+ });
expect(noneFake.requests).toHaveLength(1);
expect(JSON.stringify(noneFake.requests[0]!.state)).toContain("still open");
expect([...none.resolutions.keys()]).toEqual([]);
@@ -426,7 +448,7 @@ describe("question_resolved in the decide pass", () => {
{ now },
);
const fake = new FakeDecisionProvider({});
- await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now });
+ await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now: still });
expect(fake.requests).toEqual([]);
void record;
});
@@ -440,7 +462,11 @@ describe("resolved_min from the thresholds store", () => {
answer: (id) => (id.startsWith("answers_") ? "Q0" : undefined),
probability: 0.9,
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("enforce"),
+ now: still,
+ });
// Asked once, in enforce: the stored value is what rejected the answer.
expect(fake.requests).toHaveLength(1);
const calls = listDecisionModelCalls(vault);
diff --git a/tests/core/topics/decide-topic-same.test.ts b/tests/core/topics/decide-topic-same.test.ts
index 29c1e28a..76961930 100644
--- a/tests/core/topics/decide-topic-same.test.ts
+++ b/tests/core/topics/decide-topic-same.test.ts
@@ -33,6 +33,8 @@ import { FakeDecisionProvider, startFakeSystemOne } from "../../helpers/fake-dec
const T0 = Date.UTC(2026, 11, 1, 9, 0, 0);
let clock = T0;
const now = () => (clock += 1000);
+/** Does not advance on read: the decide pass's budget clock. */
+const still = (): number => clock;
let m: Machines;
let a: StoreHandle;
@@ -128,7 +130,7 @@ describe("topic_same in the decide pass", () => {
answer: (id) => (id.startsWith("pair_") ? "same" : undefined),
probability: 0.9,
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("off"), now });
+ const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("off"), now: still });
expect(verdicts.sameTopics).toEqual([]);
expect(fake.requests).toEqual([]);
expect(listDecisionModelCalls(vault)).toEqual([]);
@@ -146,7 +148,11 @@ describe("topic_same in the decide pass", () => {
probability: 0.9,
model: "shadow-model-5",
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("shadow"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("shadow"),
+ now: still,
+ });
expect(verdicts.sameTopics).toEqual([]);
expect(fake.requests).toHaveLength(1);
expect(fake.requests[0]!.use).toBe("topic_same");
@@ -166,7 +172,11 @@ describe("topic_same in the decide pass", () => {
probability: 0.9,
model: "same-model-1",
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("enforce"),
+ now: still,
+ });
expect(verdicts.sameTopics).toHaveLength(3);
const report = runTopicPlanning(a, { agent: "dream", now, topicVerdicts: verdicts });
expect(report.status).toBe("planned");
@@ -193,7 +203,11 @@ describe("topic_same in the decide pass", () => {
answer: (id) => (id.startsWith("pair_") ? "same" : undefined),
probability: 0.9,
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("enforce"),
+ now: still,
+ });
expect(verdicts.sameTopics).toHaveLength(3);
// Between the decide pass and planning, the third topic is merged away.
clock += 1000;
@@ -222,7 +236,11 @@ describe("topic_same in the decide pass", () => {
probability: 0.9,
model: "diff-model-2",
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("enforce"),
+ now: still,
+ });
const report = runTopicPlanning(a, { agent: "dream", now, topicVerdicts: verdicts });
expect(report.status).toBe("planned");
if (report.status !== "planned") return;
@@ -246,7 +264,7 @@ describe("topic_same in the decide pass", () => {
probability: 0.5,
}),
env: envFor("enforce"),
- now,
+ now: still,
});
expect(low.sameTopics).toEqual([]);
const timedOut = await decideTopicPlan(a, {
@@ -256,7 +274,7 @@ describe("topic_same in the decide pass", () => {
fail: "timeout",
}),
env: envFor("enforce"),
- now,
+ now: still,
});
expect(timedOut.sameTopics).toEqual([]);
const report = runTopicPlanning(a, { agent: "dream", now, topicVerdicts: low });
@@ -284,7 +302,11 @@ describe("same_min from the thresholds store", () => {
answer: (id) => (id.startsWith("pair_") ? "same" : undefined),
probability: 0.9,
});
- const verdicts = await decideTopicPlan(a, { provider: fake, env: envFor("enforce"), now });
+ const verdicts = await decideTopicPlan(a, {
+ provider: fake,
+ env: envFor("enforce"),
+ now: still,
+ });
// The built-in 0.7 would accept 0.9; the stored 0.95 decides.
expect(verdicts.sameTopics).toEqual([]);
});
diff --git a/tests/helpers/cli-timeout.ts b/tests/helpers/cli-timeout.ts
index 665f4a84..1e6e0a22 100644
--- a/tests/helpers/cli-timeout.ts
+++ b/tests/helpers/cli-timeout.ts
@@ -1,3 +1,5 @@
+import { OS_PROBE_LIMIT_MS } from "./windows-process-probe.ts";
+
/**
* The per-test budget for a test FILE whose tests drive the real `o2b`
* command line, applied with `setDefaultTimeout(CLI_SPAWN_BUDGET_MS)` at the
@@ -39,3 +41,74 @@
* under `bun test`, `bun test <file>`, and `bun run test`.
*/
export const CLI_SPAWN_BUDGET_MS = 60_000;
+
+/** How long a killed child gets to be reaped before the failure is raised. */
+export const KILL_REAP_MS = 2_000;
+
+/**
+ * How long the output pipes get, after the child exited, to deliver what is
+ * already buffered and reach end-of-file. Past it a process the child
+ * started holds a pipe, and the result is returned without waiting for it.
+ */
+export const EXIT_DRAIN_GRACE_MS = 2_000;
+
+/** Slack for the timers and the event loop between those steps. */
+const LIMIT_MARGIN_MS = 1_000;
+
+/**
+ * What a call may still need after its per-call limit fired, or after a
+ * child that exited just before it: the Windows process probe
+ * (`OS_PROBE_LIMIT_MS`), the reap wait after the kill ({@link KILL_REAP_MS}),
+ * the drain grace after an exit ({@link EXIT_DRAIN_GRACE_MS}) and a margin.
+ * The probe and the reap follow a limit, the drain follows an exit, so one
+ * call never needs all three; adding them keeps the sum simple and safe.
+ */
+const AFTER_LIMIT_MS = OS_PROBE_LIMIT_MS + KILL_REAP_MS + EXIT_DRAIN_GRACE_MS + LIMIT_MARGIN_MS;
+
+/**
+ * The per-call limit that fits a test whose own timeout is `testTimeoutMs`:
+ * that timeout minus what the call may need after the limit
+ * (`AFTER_LIMIT_MS`, 7 s). A single-spawn test then gets the limit
+ * failure, with its evidence, before its own timeout. A test whose
+ * timeout leaves no room for a limit is refused, not given a silent one.
+ */
+export function cliChildLimitFor(testTimeoutMs: number): number {
+ const limit = testTimeoutMs - AFTER_LIMIT_MS;
+ if (limit <= 0) {
+ throw new Error(
+ `a ${testTimeoutMs} ms test timeout leaves no room for a per-call limit: ` +
+ `the failure path alone needs ${AFTER_LIMIT_MS} ms`,
+ );
+ }
+ return limit;
+}
+
+/**
+ * The longest one spawned `o2b` child may run (`runCli` with a subprocess,
+ * `tests/helpers/run-cli.ts`) before it is killed and its test fails with
+ * the command line, the elapsed time and the output so far:
+ * `cliChildLimitFor(CLI_SPAWN_BUDGET_MS)`, 60 s - 7 s = 53 s.
+ *
+ * It is a hang check, not a speed check, so it follows the test's own time
+ * budget instead of a fixed guess: a diagnostic must not lower the pass
+ * threshold. A CLI call usually costs a few seconds, but one call on the
+ * Windows runner has taken about 25 s, and a single-spawn test with the
+ * 60 s budget must still pass at that speed, as it did before the limit
+ * existed. Every file that spawns the CLI runs with
+ * {@link CLI_SPAWN_BUDGET_MS}; a file or test that has to keep a shorter
+ * timeout passes `limitMs: cliChildLimitFor(<its timeout>)`.
+ *
+ * For one spawn in a 60 s test: a hung child is killed at 53 s and the
+ * failure, with up to 2 s of Windows probe and 2 s of reap wait, is raised
+ * by about 57 s; a child that exits just before the limit returns, after
+ * at most 2 s of pipe drain, by about 55 s. Both are inside the test.
+ *
+ * A test that spawns several times shares its one test timeout between
+ * those calls, and the limit bounds each call alone. If the earlier calls
+ * took time, a later hung call reaches the test timeout before its own
+ * limit: the test then fails with bun's timeout instead of the limit's
+ * evidence. That is accepted: the limit exists to make a single hung call
+ * diagnosable, and it never fails a test that the test timeout would have
+ * passed.
+ */
+export const CLI_CHILD_LIMIT_MS = cliChildLimitFor(CLI_SPAWN_BUDGET_MS);
diff --git a/tests/helpers/run-cli-limit.test.ts b/tests/helpers/run-cli-limit.test.ts
new file mode 100644
index 00000000..4dbf7d9a
--- /dev/null
+++ b/tests/helpers/run-cli-limit.test.ts
@@ -0,0 +1,204 @@
+/**
+ * The per-call time limit of the CLI spawn helper (`run-cli.ts`): a child
+ * that never exits is killed below the test's own timeout, and the test
+ * fails with the command line, the elapsed time and what the child wrote
+ * so far, instead of a silent timeout and a "dangling process" note.
+ */
+
+import { describe, expect, setDefaultTimeout, test } from "bun:test";
+
+import {
+ CLI_CHILD_LIMIT_MS,
+ CLI_SPAWN_BUDGET_MS,
+ cliChildLimitFor,
+ EXIT_DRAIN_GRACE_MS,
+ KILL_REAP_MS,
+} from "./cli-timeout.ts";
+import { spawnWithLimit } from "./run-cli.ts";
+import { OS_PROBE_LIMIT_MS } from "./windows-process-probe.ts";
+
+setDefaultTimeout(CLI_SPAWN_BUDGET_MS);
+
+/** Long enough for a loaded machine to start bun and write two lines. */
+const LIMIT_MS = 5_000;
+
+/** How long a grandchild may take to notice its closed pipe. */
+const PIPE_CLOSE_WAIT_MS = 10_000;
+
+const HANGS = [
+ "process.stdout.write('child-started\\n');",
+ "process.stderr.write('child-note\\n');",
+ "setInterval(() => {}, 1000);",
+].join(" ");
+
+/** Writes to the stdout it inherited until the write fails, then exits. */
+const GRANDCHILD = [
+ "process.stdout.on('error', () => process.exit(7));",
+ "setInterval(() => { try { process.stdout.write('tick\\n'); } catch { process.exit(7); } }, 100);",
+ "setTimeout(() => process.exit(0), 60000);",
+].join(" ");
+
+/** Starts the grandchild on its own stdout and stderr, names it, then hangs. */
+const HANGS_WITH_GRANDCHILD = [
+ `const g = Bun.spawn([process.execPath, '-e', ${JSON.stringify(GRANDCHILD)}],`,
+ "{ stdio: ['ignore', 'inherit', 'inherit'] });",
+ "process.stderr.write('grandchild ' + g.pid + '\\n');",
+ "setInterval(() => {}, 1000);",
+].join(" ");
+
+/** Stays quiet on the stdout and stderr it inherited, then exits on its own. */
+const QUIET_GRANDCHILD = "setTimeout(() => process.exit(0), 60000);";
+
+/**
+ * Starts the quiet grandchild detached, on its own stdout and stderr (so it
+ * outlives the child and holds both pipes), names it, prints and exits 0.
+ */
+const EXITS_LEAVING_GRANDCHILD = [
+ `const g = Bun.spawn([process.execPath, '-e', ${JSON.stringify(QUIET_GRANDCHILD)}],`,
+ "{ stdio: ['ignore', 'inherit', 'inherit'], detached: true });",
+ "g.unref();",
+ "process.stderr.write('grandchild ' + g.pid + '\\n');",
+ "process.stdout.write('child-done\\n');",
+ "process.exit(0);",
+].join(" ");
+
+/** The text of one captured section (`stdout` or `stderr`) of the error. */
+function section(message: string, name: "stdout" | "stderr"): string {
+ const header = new RegExp(`^--- ${name} so far \\(\\d+ chars\\) ---$`, "m");
+ const start = header.exec(message);
+ if (start === null) return "";
+ const rest = message.slice(start.index + start[0].length + 1);
+ const next = /^--- \w+ so far \(\d+ chars\) ---$/m.exec(rest);
+ return next === null ? rest : rest.slice(0, next.index);
+}
+
+async function limitError(script: string): Promise<string> {
+ try {
+ await spawnWithLimit([process.execPath, "-e", script], {
+ env: { ...process.env } as Record<string, string>,
+ limitMs: LIMIT_MS,
+ });
+ } catch (err) {
+ expect(err).toBeInstanceOf(Error);
+ return (err as Error).message;
+ }
+ throw new Error("the child was expected to outlive the limit");
+}
+
+function alive(pid: number): boolean {
+ try {
+ process.kill(pid, 0);
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+/** Resolves true as soon as `pid` is gone, false after `maxMs`. */
+function goneWithin(pid: number, maxMs: number): Promise<boolean> {
+ return new Promise((resolve) => {
+ const startedAt = Date.now();
+ const poll = setInterval(() => {
+ const gone = !alive(pid);
+ if (gone || Date.now() - startedAt > maxMs) {
+ clearInterval(poll);
+ resolve(gone);
+ }
+ }, 100);
+ });
+}
+
+describe("spawnWithLimit", () => {
+ test("a child that never exits is killed and the failure carries the evidence", async () => {
+ const message = await limitError(HANGS);
+ expect(message).toContain(`${LIMIT_MS} ms`);
+ expect(message).toContain("-e");
+ expect(message).toContain("setInterval");
+ expect(message).toMatch(/elapsed \d+ ms/);
+ expect(message).toMatch(/^last output: \d+ ms after the spawn$/m);
+ // The command line quotes both markers, so look in the sections only.
+ expect(section(message, "stdout").trimEnd()).toBe("child-started");
+ expect(section(message, "stderr").trimEnd()).toBe("child-note");
+ });
+
+ test.skipIf(process.platform !== "win32")(
+ "on Windows the failure also names the child's state from the operating system",
+ async () => {
+ const message = await limitError(HANGS);
+ expect(message).toMatch(/^os at the limit \(/m);
+ expect(message).toMatch(/^os after the kill: /m);
+ },
+ );
+
+ test.skipIf(process.platform === "win32")(
+ "elsewhere the failure carries no operating system lines",
+ async () => {
+ const message = await limitError(HANGS);
+ expect(message).not.toMatch(/^os /m);
+ },
+ );
+
+ // Windows does not end a grandchild's write when the reader goes away
+ // the same way, and `process.kill(pid, 0)` is a POSIX probe.
+ test.skipIf(process.platform === "win32")(
+ "the output readers stop with the failure, even when a grandchild holds the pipes",
+ async () => {
+ const message = await limitError(HANGS_WITH_GRANDCHILD);
+ const pid = Number(/grandchild (\d+)/.exec(section(message, "stderr"))?.[1]);
+ expect(pid).toBeGreaterThan(0);
+ try {
+ // A closed reader breaks the grandchild's pipe, and it exits.
+ expect(await goneWithin(pid, PIPE_CLOSE_WAIT_MS)).toBe(true);
+ } finally {
+ if (alive(pid)) process.kill(pid, "SIGKILL");
+ }
+ },
+ );
+
+ test("the child's exit decides: a grandchild holding the pipes delays the result only briefly, and says so", async () => {
+ const r = await spawnWithLimit([process.execPath, "-e", EXITS_LEAVING_GRANDCHILD], {
+ env: { ...process.env } as Record<string, string>,
+ limitMs: LIMIT_MS,
+ });
+ const pid = Number(/grandchild (\d+)/.exec(r.stderr)?.[1]);
+ try {
+ expect(pid).toBeGreaterThan(0);
+ expect(r.returncode).toBe(0);
+ expect(r.stdout).toBe("child-done\n");
+ expect(r.warning).toMatch(/^stdout and stderr still open \d+ ms after the child exited/);
+ } finally {
+ if (pid > 0 && alive(pid)) process.kill(pid, "SIGKILL");
+ }
+ });
+
+ test("a child that exits returns its output and code as before", async () => {
+ const r = await spawnWithLimit(
+ [process.execPath, "-e", "console.log('out'); console.error('err'); process.exit(3)"],
+ { env: { ...process.env } as Record<string, string>, limitMs: LIMIT_MS, stdin: "" },
+ );
+ expect(r).toEqual({ stdout: "out\n", stderr: "err\n", returncode: 3 });
+ });
+});
+
+/** One `install --apply` call measured on the Windows runner. */
+const SLOW_WINDOWS_CALL_MS = 25_300;
+
+/** The latest a call with this limit can end, after a kill or after an exit. */
+function latestEnd(limitMs: number): number {
+ return Math.max(limitMs + OS_PROBE_LIMIT_MS + KILL_REAP_MS, limitMs + EXIT_DRAIN_GRACE_MS);
+}
+
+describe("the per-call limit follows the test's own timeout", () => {
+ test("the default fails a hung call inside the spawn budget, and lets a slow call pass", () => {
+ expect(latestEnd(CLI_CHILD_LIMIT_MS)).toBeLessThan(CLI_SPAWN_BUDGET_MS);
+ expect(CLI_CHILD_LIMIT_MS).toBeGreaterThan(SLOW_WINDOWS_CALL_MS);
+ });
+
+ test("a shorter timeout gets a limit that ends inside it", () => {
+ expect(latestEnd(cliChildLimitFor(30_000))).toBeLessThan(30_000);
+ });
+
+ test("a timeout with no room for the failure path is refused", () => {
+ expect(() => cliChildLimitFor(5_000)).toThrow("leaves no room for a per-call limit");
+ });
+});
diff --git a/tests/helpers/run-cli.ts b/tests/helpers/run-cli.ts
index 485a83ae..dced52d3 100644
--- a/tests/helpers/run-cli.ts
+++ b/tests/helpers/run-cli.ts
@@ -15,12 +15,20 @@ import { fileURLToPath } from "node:url";
import { main } from "../../src/cli/main.ts";
import { PARTNER_CODEGRAPH_DISABLED_ENV } from "../../src/core/config.ts";
import { STORE_ROOT_ENV } from "../../src/core/store/constants.ts";
+import { CLI_CHILD_LIMIT_MS, EXIT_DRAIN_GRACE_MS, KILL_REAP_MS } from "./cli-timeout.ts";
import { DEFAULT_STORE_ROOT_ENV } from "./test-store.ts";
+import { windowsProcessAfterKill, windowsProcessState } from "./windows-process-probe.ts";
export interface RunResult {
readonly stdout: string;
readonly stderr: string;
readonly returncode: number;
+ /**
+ * Set only when the child exited but its output pipes stayed open past a
+ * short grace period (a process it started holds them); says which and
+ * for how long. The same line goes to this process's stderr.
+ */
+ readonly warning?: string;
}
const HERE = dirname(fileURLToPath(import.meta.url));
@@ -94,6 +102,13 @@ export interface RunCliOptions {
* long-running server command, signal handling). Passing `stdin` implies it.
*/
readonly subprocess?: boolean;
+ /**
+ * The longest a spawned child may run (default {@link CLI_CHILD_LIMIT_MS}).
+ * Only the subprocess path has one; a caller whose test timeout is
+ * shorter than `CLI_SPAWN_BUDGET_MS` passes
+ * `cliChildLimitFor(<its timeout>)` (`cli-timeout.ts`).
+ */
+ readonly limitMs?: number;
}
/**
@@ -199,33 +214,212 @@ function captureWrite(sink: (s: string) => void) {
};
}
+/** A stream being read into a sink, and the way to stop reading it. */
+interface Drain {
+ readonly done: Promise<void>;
+ /** Whether end-of-file has arrived. */
+ readonly ended: () => boolean;
+ /** Stops reading and closes the pipe, so its writers see it closed. */
+ readonly cancel: () => void;
+}
+
+/** Appends a stream's decoded text to `sink` as it arrives. */
+function drain(stream: ReadableStream<Uint8Array>, sink: (s: string) => void): Drain {
+ const reader = stream.getReader();
+ const decoder = new TextDecoder();
+ let ended = false;
+ const pump = (): Promise<void> =>
+ reader.read().then((chunk) => {
+ if (chunk.done) {
+ ended = true;
+ sink(decoder.decode());
+ return undefined;
+ }
+ sink(decoder.decode(chunk.value, { stream: true }));
+ return pump();
+ });
+ return {
+ done: pump(),
+ ended: () => ended,
+ cancel: () => {
+ reader.cancel().catch(() => undefined);
+ },
+ };
+}
+
+/** Resolves `"late"` after `ms`; `clear` cancels the timer. */
+function lateAfter(ms: number): { readonly late: Promise<"late">; readonly clear: () => void } {
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const late = new Promise<"late">((done) => {
+ timer = setTimeout(() => done("late"), ms);
+ });
+ return { late, clear: () => clearTimeout(timer) };
+}
+
+export interface SpawnWithLimitOptions {
+ readonly env: Record<string, string>;
+ readonly cwd?: string;
+ /** Written to the child's stdin, then closed; absent, stdin is ignored. */
+ readonly stdin?: string;
+ readonly limitMs: number;
+}
+
+/**
+ * Runs `cmd` to completion and captures its output, but never longer than
+ * `limitMs`: a child still running then is killed, and the call fails with
+ * the command line, the elapsed time, when its last output arrived and the
+ * stdout and stderr captured so far. A child that hangs (seen on the
+ * Windows runner) thus fails its test with evidence instead of a bare test
+ * timeout. No retry.
+ *
+ * The child's exit decides, not end-of-file on its pipes: a process the
+ * child started can inherit the pipes and keep them open long after the
+ * child is gone. After the exit the pipes get {@link EXIT_DRAIN_GRACE_MS}
+ * to deliver what is buffered; a pipe still open then is closed on this
+ * side, and the result carries a `warning` (also written to this process's
+ * stderr) naming the open pipes, so a leaked handle is seen, not hidden.
+ * The readers are stopped with a limit failure too, so they never outlive
+ * the call. On Windows the failure also names the child's state from the
+ * operating system at the limit and whether it is gone after the kill
+ * (`windows-process-probe.ts`).
+ */
+export async function spawnWithLimit(
+ cmd: ReadonlyArray<string>,
+ opts: SpawnWithLimitOptions,
+): Promise<RunResult> {
+ const started = Date.now();
+ const proc = Bun.spawn([...cmd], {
+ cwd: opts.cwd ?? ROOT,
+ env: opts.env,
+ stdin: opts.stdin === undefined ? "ignore" : "pipe",
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+ let stdout = "";
+ let stderr = "";
+ let lastOutputAt: number | null = null;
+ const outputAt = (): void => {
+ lastOutputAt = Date.now();
+ };
+ const drains = [
+ drain(proc.stdout, (s) => {
+ if (s !== "") outputAt();
+ stdout += s;
+ }),
+ drain(proc.stderr, (s) => {
+ if (s !== "") outputAt();
+ stderr += s;
+ }),
+ ];
+ const stopReading = (): void => {
+ for (const d of drains) d.cancel();
+ };
+ const output = Promise.all(drains.map((d) => d.done));
+ // A reader's failure surfaces after the exit, where `output` is awaited.
+ output.catch(() => undefined);
+ const run = (async (): Promise<number> => {
+ if (opts.stdin !== undefined && proc.stdin !== undefined) {
+ proc.stdin.write(opts.stdin);
+ await proc.stdin.end();
+ }
+ return await proc.exited;
+ })();
+ const limit = lateAfter(opts.limitMs);
+ let outcome: number | "late";
+ try {
+ outcome = await Promise.race([run, limit.late]);
+ } catch (err) {
+ // A failed run (stdin delivery) must not leave the child running
+ // without its limit.
+ proc.kill("SIGKILL");
+ stopReading();
+ throw err;
+ } finally {
+ limit.clear();
+ }
+ if (outcome !== "late") return await afterExit(outcome);
+ const elapsedMs = Date.now() - started;
+ const windows = process.platform === "win32";
+ const atLimit = windows ? await windowsProcessState(proc.pid, started) : null;
+ proc.kill("SIGKILL");
+ await Promise.race([proc.exited, new Promise((r) => setTimeout(r, KILL_REAP_MS))]);
+ const afterKill = windows
+ ? windowsProcessAfterKill(proc.pid, proc.exitCode ?? proc.signalCode)
+ : null;
+ stopReading();
+ // Keep the run's late rejection (if any) from surfacing unhandled.
+ run.catch(() => undefined);
+ const lastOutput: number | null = lastOutputAt;
+ throw new Error(
+ [
+ `CLI child still running after the ${opts.limitMs} ms per-call limit: killed.`,
+ `command: ${cmd.map((part) => JSON.stringify(part)).join(" ")}`,
+ `cwd: ${opts.cwd ?? ROOT}`,
+ `pid: ${proc.pid}, elapsed ${elapsedMs} ms, exit after kill: ${proc.exitCode ?? "not reaped"}, stdin: ${opts.stdin === undefined ? "ignored" : `${opts.stdin.length} chars, closed`}`,
+ // Output that arrived after the limit (during the probe and the
+ // kill) means a slow child, not a stuck one.
+ `last output: ${lastOutput === null ? "none" : `${lastOutput - started} ms after the spawn`}`,
+ ...(atLimit === null ? [] : [atLimit]),
+ ...(afterKill === null ? [] : [afterKill]),
+ `--- stdout so far (${stdout.length} chars) ---`,
+ stdout,
+ `--- stderr so far (${stderr.length} chars) ---`,
+ stderr,
+ ].join("\n"),
+ );
+
+ /** The result once the child exited, with at most a short wait for its pipes. */
+ async function afterExit(returncode: number): Promise<RunResult> {
+ const grace = lateAfter(EXIT_DRAIN_GRACE_MS);
+ let drained: void[] | "late";
+ try {
+ drained = await Promise.race([output, grace.late]);
+ } finally {
+ grace.clear();
+ }
+ if (drained !== "late") return { stdout, stderr, returncode };
+ const open = [drains[0]?.ended() ? null : "stdout", drains[1]?.ended() ? null : "stderr"]
+ .filter((name): name is string => name !== null)
+ .join(" and ");
+ stopReading();
+ const warning =
+ `${open} still open ${EXIT_DRAIN_GRACE_MS} ms after the child exited with ${returncode}: ` +
+ "a process it started holds the pipe; the result is returned without waiting for it";
+ process.stderr.write(
+ `spawnWithLimit: ${warning}\ncommand: ${cmd.map((part) => JSON.stringify(part)).join(" ")}\n`,
+ );
+ return { stdout, stderr, returncode, warning };
+ }
+}
+
/** Fresh-process invocation - the original behavior, kept for isolation cases. */
async function runCliSubprocess(
args: ReadonlyArray<string>,
opts: RunCliOptions,
): Promise<RunResult> {
const { env, cleanupDir } = resolveEnv(opts.env ?? {});
+ let result: RunResult;
try {
- const proc = Bun.spawn(["bun", "run", CLI_ENTRY, ...args], {
- cwd: opts.cwd ?? ROOT,
+ result = await spawnWithLimit(["bun", "run", CLI_ENTRY, ...args], {
env,
- stdin: opts.stdin === undefined ? "ignore" : "pipe",
- stdout: "pipe",
- stderr: "pipe",
+ ...(opts.cwd !== undefined ? { cwd: opts.cwd } : {}),
+ ...(opts.stdin !== undefined ? { stdin: opts.stdin } : {}),
+ limitMs: opts.limitMs ?? CLI_CHILD_LIMIT_MS,
});
- if (opts.stdin !== undefined && proc.stdin !== undefined) {
- proc.stdin.write(opts.stdin);
- await proc.stdin.end();
+ } catch (err) {
+ // After a killed child (Windows keeps its files busy for a moment) a
+ // failed cleanup must not replace the evidence.
+ if (cleanupDir !== null) {
+ try {
+ rmSync(cleanupDir, { recursive: true, force: true });
+ } catch {
+ // the evidence below matters more than the temp directory
+ }
}
- const [stdout, stderr] = await Promise.all([
- new Response(proc.stdout).text(),
- new Response(proc.stderr).text(),
- ]);
- const returncode = await proc.exited;
- return { stdout, stderr, returncode };
- } finally {
- if (cleanupDir !== null) rmSync(cleanupDir, { recursive: true, force: true });
+ throw err;
}
+ if (cleanupDir !== null) rmSync(cleanupDir, { recursive: true, force: true });
+ return result;
}
/**
diff --git a/tests/helpers/windows-process-probe.ts b/tests/helpers/windows-process-probe.ts
new file mode 100644
index 00000000..059e05bf
--- /dev/null
+++ b/tests/helpers/windows-process-probe.ts
@@ -0,0 +1,92 @@
+/**
+ * What the operating system says about a CLI child that outlived its
+ * per-call limit (`spawnWithLimit` in `run-cli.ts`), on Windows only.
+ *
+ * The hangs seen on the Windows runner left no output at all, so the
+ * output cannot tell a child that never got going from one that started
+ * and then blocked. The process itself can: a child that never started
+ * has a fraction of a second of CPU time and a handful of threads, while
+ * a bun that loaded the CLI has seconds of CPU and a dozen threads or
+ * more. The thread states and wait reasons say what it is blocked on.
+ *
+ * Each probe is bounded and never throws: a probe that fails or times
+ * out says so in its line, and that is evidence too (a machine that
+ * cannot start PowerShell in time is stalled as a whole).
+ */
+
+/**
+ * The longest one PowerShell probe may take before it is abandoned. The
+ * per-call limit leaves room for it below the test's own timeout
+ * (`cliChildLimitFor` in `cli-timeout.ts`).
+ */
+export const OS_PROBE_LIMIT_MS = 2_000;
+
+function probeScript(pid: number): string {
+ return [
+ "$ErrorActionPreference = 'Stop'",
+ `try { $p = Get-Process -Id ${pid} } catch { 'no such process'; exit 0 }`,
+ "$states = ($p.Threads | Group-Object ThreadState | ForEach-Object { \"$($_.Name) $($_.Count)\" }) -join ', '",
+ "$waits = ($p.Threads | Where-Object { $_.ThreadState -eq 'Wait' } | Group-Object WaitReason | ForEach-Object { \"$($_.Name) $($_.Count)\" }) -join ', '",
+ "\"name $($p.ProcessName), cpu $([math]::Round($p.TotalProcessorTime.TotalSeconds, 2)) s, threads $($p.Threads.Count) ($states; waiting: $waits), handles $($p.HandleCount), working set $([int]($p.WorkingSet64 / 1MB)) MB, started $($p.StartTime.ToUniversalTime().ToString('o'))\"",
+ ].join("; ");
+}
+
+/**
+ * One line naming the child's state as `Get-Process` sees it now:
+ * `os at the limit (Get-Process, <ms> ms; spawned <iso>): <state>`.
+ */
+export async function windowsProcessState(pid: number, spawnedAt: number): Promise<string> {
+ const startedAt = Date.now();
+ const head = (detail: string): string =>
+ `os at the limit (Get-Process, ${detail}; spawned ${new Date(spawnedAt).toISOString()}): `;
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ try {
+ const probe = Bun.spawn(
+ // Encoded, so the script's quotes survive the Windows command line.
+ [
+ "powershell.exe",
+ "-NoProfile",
+ "-NonInteractive",
+ "-EncodedCommand",
+ Buffer.from(probeScript(pid), "utf16le").toString("base64"),
+ ],
+ { stdin: "ignore", stdout: "pipe", stderr: "pipe" },
+ );
+ const text = Promise.all([
+ new Response(probe.stdout).text(),
+ new Response(probe.stderr).text(),
+ ]);
+ const cap = new Promise<null>((done) => {
+ timer = setTimeout(() => done(null), OS_PROBE_LIMIT_MS);
+ });
+ const out = await Promise.race([text, cap]);
+ if (out === null) {
+ probe.kill();
+ return `${head(`timed out after ${OS_PROBE_LIMIT_MS} ms`)}no answer`;
+ }
+ const answer = (out[0].trim() || out[1].trim() || "no output").replace(/\s+/g, " ");
+ return `${head(`${Date.now() - startedAt} ms`)}${answer}`;
+ } catch (err) {
+ return `${head("failed")}${err instanceof Error ? err.message : String(err)}`;
+ } finally {
+ clearTimeout(timer);
+ }
+}
+
+/**
+ * Whether the operating system still has `pid`, for the line after the
+ * kill: a child bun has not reaped may still be gone. `exit` is its exit
+ * code or signal once reaped, else null.
+ */
+export function windowsProcessAfterKill(pid: number, exit: number | string | null): string {
+ if (exit !== null) return `os after the kill: exited (${exit})`;
+ try {
+ process.kill(pid, 0);
+ return "os after the kill: still present";
+ } catch (err) {
+ const code = (err as { code?: unknown }).code;
+ return code === "ESRCH"
+ ? "os after the kill: gone, not yet reaped"
+ : `os after the kill: unknown (${String(code ?? err)})`;
+ }
+}
diff --git a/tests/hooks/active-inject-topic-heads.test.ts b/tests/hooks/active-inject-topic-heads.test.ts
index 9ef5a85c..5bd7981c 100644
--- a/tests/hooks/active-inject-topic-heads.test.ts
+++ b/tests/hooks/active-inject-topic-heads.test.ts
@@ -571,12 +571,17 @@ describe("head_select at hook level", () => {
server = await startFakeSystemOne();
server.setReply(() => ({ hang: true }));
await seedThresholdEntry("head-model", "head_select");
+ // Half the ceiling leaves the hook 6 s from its start to the request,
+ // several times what a slow runner needs to get there; the budget
+ // still runs far past the ceiling.
const result = await runHook(hookPayload(), {
...decisionEnv("enforce", server.url),
OPEN_SECOND_BRAIN_DECISION_MODEL_HOOK_BUDGET_MS: "60000",
- OPEN_SECOND_BRAIN_HOOK_CEILING_MS: "1400",
+ OPEN_SECOND_BRAIN_HOOK_CEILING_MS: "12000",
});
expect(result.exit).toBe(0);
+ // The stalled provider was really asked: the wait ended, not a skip.
+ expect(server.requests).toHaveLength(1);
const context = injected(result.stdout);
expect(context).toContain("Memory body line.");
expect(context.split("\n").filter((l) => /^- .*\[active\] \(/.test(l))).toHaveLength(2);
diff --git a/tests/hooks/neighbour-decision-wait.test.ts b/tests/hooks/neighbour-decision-wait.test.ts
new file mode 100644
index 00000000..404b7802
--- /dev/null
+++ b/tests/hooks/neighbour-decision-wait.test.ts
@@ -0,0 +1,98 @@
+/**
+ * The neighbour-updates lane's decision wait, without a subprocess: the
+ * request gets the rest of the lane's budget and never more than half the
+ * hook's process ceiling, measured from the hook's start. The elapsed time
+ * is passed in, so these tests do not depend on how fast a process starts.
+ * The subprocess tests in `neighbour-updates.test.ts` prove the hook emits
+ * before its ceiling; this file proves which bound ends the wait.
+ */
+
+import { afterEach, describe, expect, test } from "bun:test";
+import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+import { LANE_BUDGET_MS, neighbourDecisionWaitMs } from "../../hooks/neighbour-updates.ts";
+import { runNeighbourRelevant } from "../../src/core/agent-context/neighbour-relevant.ts";
+import { resolveDecisionModelConfig } from "../../src/core/decision-model/config.ts";
+import { MIN_REQUEST_MS } from "../../src/core/decision-model/request-floor.ts";
+import { FAKE_DECISION_KEY } from "../helpers/fake-credentials.ts";
+import { type FakeSystemOne, startFakeSystemOne } from "../helpers/fake-decision-provider.ts";
+
+const dirs: string[] = [];
+let server: FakeSystemOne | null = null;
+afterEach(async () => {
+ for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true });
+ if (server !== null) {
+ await server.close();
+ server = null;
+ }
+});
+
+/** A ceiling whose half ends the wait before the lane's budget does. */
+const SMALL_CEILING_MS = 1400;
+
+describe("the neighbour lane's decision wait", () => {
+ test("half a small ceiling ends the wait before the lane's budget", () => {
+ expect(SMALL_CEILING_MS / 2).toBeLessThan(LANE_BUDGET_MS);
+ expect(neighbourDecisionWaitMs(SMALL_CEILING_MS, 0)).toBe(SMALL_CEILING_MS / 2);
+ expect(neighbourDecisionWaitMs(SMALL_CEILING_MS, 300)).toBe(SMALL_CEILING_MS / 2 - 300);
+ expect(neighbourDecisionWaitMs(SMALL_CEILING_MS, SMALL_CEILING_MS)).toBe(0);
+ });
+
+ test("under a large ceiling the lane's budget ends the wait", () => {
+ expect(neighbourDecisionWaitMs(10_000, 0)).toBe(LANE_BUDGET_MS);
+ expect(neighbourDecisionWaitMs(10_000, 400)).toBe(LANE_BUDGET_MS - 400);
+ expect(neighbourDecisionWaitMs(10_000, LANE_BUDGET_MS + 1)).toBe(0);
+ });
+
+ test("a provider that never answers within a configured budget past the ceiling is asked, and the deterministic gate stands at the end of the wait", async () => {
+ const vault = mkdtempSync(join(tmpdir(), "osb-neighbour-wait-"));
+ mkdirSync(join(vault, "Brain"), { recursive: true });
+ dirs.push(vault);
+ server = await startFakeSystemOne();
+ server.setReply(() => ({ hang: true }));
+ const env = { O2B_TEST_WAIT_KEY: FAKE_DECISION_KEY };
+ const config = resolveDecisionModelConfig({
+ env: env as NodeJS.ProcessEnv,
+ config: {
+ decision_model_enabled: "true",
+ decision_model_provider: "compatible",
+ decision_model_base_url: server.url,
+ decision_model_id: "nb-model",
+ decision_model_env_key: "O2B_TEST_WAIT_KEY",
+ decision_model_uses: "neighbour_relevant:enforce",
+ decision_model_hook_budget_ms: "60000",
+ },
+ vault,
+ thresholdsPath: null,
+ });
+ // The hook's start, as the lane sees it: nothing elapsed yet.
+ const remainingMs = neighbourDecisionWaitMs(SMALL_CEILING_MS, 0);
+ expect(remainingMs).toBeGreaterThanOrEqual(MIN_REQUEST_MS);
+ const out = await runNeighbourRelevant({
+ config,
+ candidates: [
+ {
+ record: "rec-2",
+ kind: "decision",
+ topic: "01KA0000000000000000000001",
+ topicTitle: "Shard writer",
+ text: "pending decision on the session topic",
+ seq: 1,
+ device: "dev-b",
+ agent: "neighbour",
+ private: false,
+ titlePrivate: false,
+ },
+ ],
+ prompt: "Work.",
+ remainingMs,
+ env,
+ });
+ // Asked once: the wait ended on its deadline, not on a skipped request.
+ expect(server.requests).toHaveLength(1);
+ expect(out.applied).toBe(false);
+ expect(out.judged.size).toBe(0);
+ });
+});
diff --git a/tests/hooks/neighbour-updates.test.ts b/tests/hooks/neighbour-updates.test.ts
index 293853e5..2d6c4c77 100644
--- a/tests/hooks/neighbour-updates.test.ts
+++ b/tests/hooks/neighbour-updates.test.ts
@@ -11,6 +11,7 @@
import { describe, expect, setDefaultTimeout, test } from "bun:test";
import { join } from "node:path";
+import { DEFAULT_HOOK_CEILING_MS } from "../../hooks/lib/process-ceiling.ts";
import { CLI_SPAWN_BUDGET_MS } from "../helpers/cli-timeout.ts";
import {
injected,
@@ -220,15 +221,17 @@ describe("neighbour_relevant at hook level", () => {
await reindex();
const server = await startFakeSystemOne();
h.server = server;
- let asks = 0;
- server.setReply(() => {
- asks += 1;
- return { hang: true };
- });
+ server.setReply(() => ({ hang: true }));
+ // At the shipped ceiling the lane's own budget ends the wait, which
+ // leaves the hook 6.5 s past it to emit. Whether the request was
+ // started depends on how fast this process got to it (the lane's
+ // budget runs from the hook's start), so this test does not count
+ // requests: `neighbour-decision-wait.test.ts` proves the wait's
+ // bounds and that a hung provider is asked, without a subprocess.
const env = {
...decisionEnv(mode, server.url),
OPEN_SECOND_BRAIN_DECISION_MODEL_HOOK_BUDGET_MS: "60000",
- OPEN_SECOND_BRAIN_HOOK_CEILING_MS: "1400",
+ OPEN_SECOND_BRAIN_HOOK_CEILING_MS: String(DEFAULT_HOOK_CEILING_MS),
};
await runHook(hookPayload("Work.", "s1"), env);
decision(
@@ -238,11 +241,8 @@ describe("neighbour_relevant at hook level", () => {
NEIGHBOUR_AGENT,
);
await reindex();
- const asksBefore = asks;
const hung = await runHook(hookPayload("Work.", "s1"), env);
expect(hung.exit).toBe(0);
- // The hanging provider was asked: the update came through the timeout path.
- expect(asks).toBeGreaterThan(asksBefore);
expect(injected(hung.stdout)).toContain("pending decision on the session topic");
},
);
diff --git a/tests/hooks/recall-inject-decision-model.test.ts b/tests/hooks/recall-inject-decision-model.test.ts
index 1d0961f7..0e641233 100644
--- a/tests/hooks/recall-inject-decision-model.test.ts
+++ b/tests/hooks/recall-inject-decision-model.test.ts
@@ -302,4 +302,32 @@ describe("recall-inject hook with the decision-model use", () => {
// The hook never waits for the slow server (process start included).
expect(r.wallMs).toBeLessThan(4_500);
}, 30_000);
+
+ test("a provider that never answers within a configured budget past the ceiling still leaves the brief", async () => {
+ const today = await baseline();
+ server.delayMs = 60_000;
+ writeConfig({
+ ...DECISION(),
+ decision_model_uses: "recall_inject:enforce",
+ decision_model_hook_budget_ms: "60000",
+ // The longest retrieval budget the setting accepts.
+ recall_inject_time_budget_ms: "6000",
+ });
+ // The retrieval budget (6 s) outlasts this ceiling (5.8 s): a wait
+ // bounded by the budget alone would let the ceiling end the process
+ // first. Half the ceiling leaves the hook 2.9 s from its start to the
+ // request, several times what a slow runner needs to get there.
+ const r = await runHook({
+ [KEY_VAR]: FAKE_DECISION_KEY,
+ OPEN_SECOND_BRAIN_HOOK_CEILING_MS: "5800",
+ });
+ expect(r.exit).toBe(0);
+ expect(r.stdout).toBe(today);
+ // The stalled provider was really asked: the wait ended, not a skip.
+ expect(server.requests).toHaveLength(1);
+ expect(lastDetails()["decision_model"]).toMatchObject({
+ outcome: "degraded",
+ degrade_reason: "timeout",
+ });
+ }, 30_000);
});