Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/reference-pool-growth-status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@schlessera/brain-ui-sdk": minor
---

**Reference pool growth status (#3515).** `brain-native-store pool growth <config>` and the native manager client's `poolGrowth()` report residual growth on the host-only reference pool from the same simple-quota owner charges as admission. They report each worktree's and each changed refresh's charge against its chain's cold acquisition, and the sources retained per published repository and revision across views. A flag fires strictly above the ruled thresholds: 25 % for a worktree, 0.75× for a refresh, and more than two retained sources. Each flag carries the measured bytes and its threshold.

The status is read-only, reports no forecast savings or freed capacity, and reads missing accounting as `unavailable`, never zero. Sealing now records each root's charge, so the basis survives release. Background deduplication is rejected, as recorded in the pool decision.
49 changes: 49 additions & 0 deletions api-report/ui-server.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,54 @@ signatures (every public export and the types it is made of)
}
NativePoolCreatePurpose (packages/ui-sdk/src/server/native-brain-manager-protocol.ts)
export type NativePoolCreatePurpose = Exclude<NativePoolRecord["purpose"], "checkpoint">;
NativePoolGrowth (packages/ui-sdk/src/server/native-brain-manager-protocol.ts)
export interface NativePoolGrowth {
basis: "btrfs-space-info-disk-used+simple-quota-owner";
state: "available" | "unavailable";
reason: string | null;
generation: string | null;
thresholds: {
worktreeRatio: number;
refreshRatio: number;
retainedSources: number;
};
worktrees: NativePoolGrowthComparison[];
refreshes: NativePoolGrowthComparison[];
sources: NativePoolGrowthSources[];
flags: NativePoolGrowthFlag[];
omitted: number;
}
NativePoolGrowthComparison (packages/ui-sdk/src/server/native-brain-manager-protocol.ts)
export interface NativePoolGrowthComparison {
id: string;
from: string;
cold: string | null;
ownerBytes: number | null;
basisBytes: number | null;
thresholdBytes: number | null;
state: "available" | "unavailable";
flagged: boolean;
}
NativePoolGrowthFlag (packages/ui-sdk/src/server/native-brain-manager-protocol.ts)
export interface NativePoolGrowthFlag {
kind: "worktree" | "refresh" | "retained-sources";
subject: string;
observedBytes: number | null;
thresholdBytes: number | null;
threshold: number;
observedCount: number | null;
}
NativePoolGrowthSources (packages/ui-sdk/src/server/native-brain-manager-protocol.ts)
export interface NativePoolGrowthSources {
name: string;
roots: {
id: string;
view: string;
ownerBytes: number | null;
}[];
ownerBytes: number | null;
flagged: boolean;
}
NativePoolRecord (packages/ui-sdk/src/server/native-brain-manager-protocol.ts)
export interface NativePoolRecord {
id: string;
Expand Down Expand Up @@ -2502,6 +2550,7 @@ signatures (every public export and the types it is made of)
poolCheckpoint: (id: string, from: string, signal?: AbortSignal) => Promise<NativePoolCheckpoint>;
poolCreate: (id: string, purpose: NativePoolCreatePurpose, from: null | string, allowanceBytes: number, signal?: AbortSignal) => Promise<NativePoolRecord>;
poolDelta: (from: string, to: string, signal?: AbortSignal) => Promise<NativeDeltaStream>;
poolGrowth: (signal?: AbortSignal) => Promise<NativePoolGrowth>;
poolListCheckpoint: (id: string, path: string, expectedWorkerUid: number, signal?: AbortSignal) => Promise<NativeCheckpointMetadata[]>;
poolPark: (id: string, signal?: AbortSignal) => Promise<NativePoolRecord>;
poolPublish: (id: string, view: string, name: string, signal?: AbortSignal) => Promise<NativePoolRecord>;
Expand Down
47 changes: 45 additions & 2 deletions docs/decisions/reference-pool-accounting.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ actual allocated disk. That decision left the mechanism to implementation.
This record names the mechanism, the primary sources it relies on and the
native observations that qualified it. The pool is managed by the existing
host-only native manager
(`createNativeReferencePool`, `packages/ui-sdk/src/server/native-reference-pool.ts:273-988`).
(`createNativeReferencePool`, `packages/ui-sdk/src/server/native-reference-pool.ts:380-1129`).
No consumer uses it yet.

## A separate volume, not another subvolume of the brain store
Expand Down Expand Up @@ -224,7 +224,7 @@ directory its intent records. Sealing also makes the root's top directory
`roots/` and `views/` join the mandatory worker masks. A worker sees reference
source only through a view bound for it, and other domains' views stay
invisible. `poolView` (#2432) opens a view's directory before its first
publication (`function viewOpen`, `packages/ui-sdk/src/server/native-reference-pool.ts:954-957`),
publication (`function viewOpen`, `packages/ui-sdk/src/server/native-reference-pool.ts:1095-1098`),
so a worker that binds it at its start sees the domain's first publication on a
later lookup ([agent tools](managed-worktrees.md#agent-tools)).

Expand Down Expand Up @@ -269,6 +269,49 @@ directory itself. `access/` joins the mandatory worker masks. Census accepts a
writable worktree only in the directory its intent records, and measurement
adopts a grant or park interrupted between its rename and its record.

## Background deduplication is rejected; residual growth is reported (#3515)

Preventive sharing stays the only mechanism: acquisition snapshots, worktree
snapshots and reflink copies. No duperemove, bees or other extent-rewriting
tool runs on the pool. The [measured investigation](../plans/repository-storage-dedupe.md)
found three reasons, each a conflict with a rule above:

- **Cross-root owner re-attribution.** Deduplicating between two roots moves
the destination's data onto the source root's simple-quota charge. A
released source then stays `deleted` with that charge and is never
reclaimed while the destination lives.
- **Sealed-root modification.** Deduplicating into a sealed, read-only root
succeeds without refusal and leaves its recorded owner charge stale.
- **False checkpoint deltas.** Deduplicating inside a worktree between two
checkpoints makes the checkpoint delta report content edits that never
happened, and frees nothing while the older checkpoint lives.

Residual growth is reported instead, by `brain-native-store pool growth`
(`referencePoolGrowth`, `packages/ui-sdk/src/server/native-reference-pool.ts:266-338`).
It reads one stable observation and the same owner charges as admission, with
no reconciliation, so it commits, deletes, resets and reserves nothing. Every
source chain starts at a cold acquisition, a source with no `from`. The charge
recorded when that root was sealed is the basis for the worktrees and refreshes
built on the chain, because a refresh holds only the extents it changed and a
released root keeps only what is still shared. The ruled thresholds are
`REFERENCE_POOL_GROWTH_THRESHOLDS`, from the #3513 baseline of +1.6 % for two
worktrees and 0.39x for a changed refresh. Each flag fires only strictly above
its threshold:

- a worktree charged above 25 % of its source's cold acquisition;
- a changed refresh charged above 0.75x its chain's cold acquisition;
- more than two retained sources published under one name across views. The
name hashes the canonical repository and revision (`referenceSourceName`).

A basis that cannot be read reads as `unavailable`, never as zero. That covers
a broken chain and a released cold acquisition sealed before the seal-time
charge was recorded. A root with no recorded owner charge reports null bytes,
and so does any retained-source sum that includes it. Evidence that admission would refuse, other than
capacity, makes the whole status unavailable. So does a published name that
does not resolve to a recorded sealed source in its own view. The status
reports measured bytes and thresholds only, never forecast savings or freed
capacity.

## Native qualification

`bun scripts/prove-reference-pool.ts` runs `reference-pool-fixture.py` inside
Expand Down
11 changes: 11 additions & 0 deletions docs/integration-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ same-commit update of the index or relevant authoritative component. How they ar

The reasoning is in [decisions/contract-versioning.md](decisions/contract-versioning.md).

## Reference pool growth status (additive, 0.45.0, #3515)

The host-only `brain-native-store pool` projection gains a read-only `growth`
action, the native manager client gains `poolGrowth()`, and the protocol gains
`NativePoolGrowth`. They report each worktree's and each changed refresh's
owner charge against its chain's cold acquisition, and retained sources per
published name across views. Flags apply the ruled thresholds and carry
measured bytes only. See
[the package API](integration-contract/package-api.md#reference-pool-growth-status-additive-0450-3515).
This ships in a minor; no breaking ruling is required.

## Turn data trees on the managed volume (additive, 0.45.0, #2911)

The native manager client and the lease owner gain `prepareTurnTree(id,
Expand Down
54 changes: 54 additions & 0 deletions docs/integration-contract/package-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4409,6 +4409,60 @@ No call accepts a host path, UID or mount. Worker masks now include the pool's
The mechanism is recorded in the
[pool decision](../decisions/reference-pool-accounting.md#worktree-checkpoints-and-access-generations-2430).

<a id="reference-pool-growth-status-additive-0450-3515"></a>

### Reference pool growth status (additive, 0.45.0, #3515)

`brain-native-store pool growth <config>` prints the same envelope as `status`,
with `action: "growth"` and `growth`, and the same exit codes. The native
manager client gains `poolGrowth()`, which takes no identity, path or selector
and returns `NativePoolGrowth`:

- `basis` is `btrfs-space-info-disk-used+simple-quota-owner`. `state` is
`available` or `unavailable`. `reason` says why the whole status is
unavailable; `generation` is the observation's filesystem generation.
- `thresholds` are `{ worktreeRatio: 0.25, refreshRatio: 0.75, retainedSources: 2 }`,
exported as `REFERENCE_POOL_GROWTH_THRESHOLDS`.
- `worktrees` (writable or sealed) and `refreshes` (sealed sources with a
`from`) are `NativePoolGrowthComparison`s:
- `id`, `from`, and `cold`, the cold acquisition at the root of the chain;
- `ownerBytes`, null when the accounting has no charge for the root;
- `basisBytes`, that acquisition's charge when it was sealed;
- `thresholdBytes`, `basisBytes` times the ratio, rounded down;
- `state`, and `flagged`, true when `ownerBytes` is strictly above
`thresholdBytes`.

A basis or charge that cannot be read gives `state: "unavailable"` and null
bytes, never zero, and is never flagged.
- `sources` are `NativePoolGrowthSources`, one per published name: `name`,
`roots` (`id`, `view`, `ownerBytes`), their summed `ownerBytes`, and
`flagged` when more than `retainedSources` views publish the name. A root
with no recorded charge has `ownerBytes: null`, and so does the sum: never
zero. The count flag still applies.
- `flags` are `NativePoolGrowthFlag`s: `kind` (`worktree`, `refresh` or
`retained-sources`), `subject` (root id or name), `observedBytes` (null only
for a retained-sources sum that includes an unknown charge),
`thresholdBytes` (null for the count), `threshold`, and `observedCount`
(null for a ratio).
- `omitted` counts comparisons, groups or flags left out to keep one reply
within the 64 KiB control bound. Unflagged detail goes first.

The status is read-only. It neither reconciles, deletes, resets nor reserves
anything, and nothing in it forecasts savings or freed capacity. It is
unavailable in these cases:

- the pool refuses for any reason other than `capacity`;
- the owner accounting is unreadable;
- a view holds a non-link entry;
- a published name resolves to anything but a recorded sealed source in its
own view, such as a path, another view or a stale identity.

The manager records each root's charge when it is sealed, so the basis
survives the root's release. Roots sealed before this release carry none. A
released one reads as unavailable, while a live sealed one uses its current,
unchanging charge. The rationale is in
[the pool decision](../decisions/reference-pool-accounting.md#background-deduplication-is-rejected-residual-growth-is-reported-3515).

<a id="reference-and-worktree-tools-additive-0440-2432"></a>

### Reference and worktree tools (additive, 0.44.0, #2432)
Expand Down
4 changes: 3 additions & 1 deletion packages/ui-sdk/src/bin/brain-native-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { nativeCheckpointId, NativeBrainManagerError } from "../server/native-br

const usage = `Usage: brain-native-store /absolute/root-owned-config.json
brain-native-store pool status /absolute/root-owned-config.json
brain-native-store pool growth /absolute/root-owned-config.json
brain-native-store pool recovery [--after <id>] /absolute/root-owned-config.json
brain-native-store pool release <id> /absolute/root-owned-config.json`;
function loadConfig(path: string): NativeBrainManagerConfig {
Expand Down Expand Up @@ -36,13 +37,14 @@ async function pool(args: string[]): Promise<number> {
const action = args[0], config = args.at(-1);
const after = action === "recovery" && args.length === 4 && args[1] === "--after" ? args[2]! : null;
const id = action === "release" && args.length === 3 ? args[1]! : null;
const shaped = (action === "status" && args.length === 2) || (action === "recovery" && (args.length === 2 || after !== null)) || id !== null;
const shaped = ((action === "status" || action === "growth") && args.length === 2) || (action === "recovery" && (args.length === 2 || after !== null)) || id !== null;
if (!shaped || !config || (after !== null && !nativeCheckpointId.safeParse(after).success) || (id !== null && !nativeCheckpointId.safeParse(id).success)) {
console.error(usage); return 2;
}
const fixed = loadConfig(config), client = createNativeBrainManagerClient({ socketPath: fixed.socketPath, timeoutMs: fixed.operationTimeoutMs });
try {
const result = action === "status" ? { action, status: await client.poolStatus() }
: action === "growth" ? { action, growth: await client.poolGrowth() }
: action === "recovery" ? { action, ...(await client.listPoolRecovery(after)) }
: { action, record: await client.poolRelease(id!) };
console.log(JSON.stringify({ schema_version: 1, ok: true, ...result }));
Expand Down
4 changes: 3 additions & 1 deletion packages/ui-sdk/src/server/native-brain-manager-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { consumeNativeStageAuthority, consumeNativeCheckpointRead, nativeCheckpo
import { nativeCheckpointId } from "./native-brain-manager-protocol.js";
import { NativeBrainManagerError, nativeManagerRequest, NATIVE_CONTROL_BYTES, NATIVE_DATA_CHUNK_BYTES, NATIVE_MANAGER_OPERATION_TIMEOUT_MS, NATIVE_MANAGER_MAX_DELTA_BYTES,
type NativeCheckpoint, type NativeManagerErrorCode, type NativeManagerReady, type NativeManagerRequest, type NativeRecoveryRecord, type NativeStage, type NativeStageChange, type NativeTurnTree,
type NativePoolAccess, type NativePoolCheckpoint, type NativePoolCreatePurpose, type NativePoolRecord, type NativePoolStatus } from "./native-brain-manager-protocol.js";
type NativePoolAccess, type NativePoolCheckpoint, type NativePoolCreatePurpose, type NativePoolGrowth, type NativePoolRecord, type NativePoolStatus } from "./native-brain-manager-protocol.js";

export interface NativeDeltaStream {
stream: ReadableStream<Uint8Array>;
Expand Down Expand Up @@ -293,6 +293,8 @@ export function createNativeBrainManagerClient(input: { socketPath: string; time
};
},
poolStatus: async (signal?: AbortSignal): Promise<NativePoolStatus> => control({ operation: "poolStatus" }, signal),
/** Read-only residual-growth status (#3515); reports measured charges and thresholds only. */
poolGrowth: async (signal?: AbortSignal): Promise<NativePoolGrowth> => control({ operation: "poolGrowth" }, signal),
poolCreate: async (id: string, purpose: NativePoolCreatePurpose, from: string | null, allowanceBytes: number, signal?: AbortSignal): Promise<NativePoolRecord> =>
control({ operation: "poolCreate", id, purpose, from, allowanceBytes }, signal),
poolSeal: async (id: string, signal?: AbortSignal): Promise<NativePoolRecord> => control({ operation: "poolSeal", id }, signal),
Expand Down
61 changes: 61 additions & 0 deletions packages/ui-sdk/src/server/native-brain-manager-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const nativeManagerRequest = z.discriminatedUnion("operation", [
// Reference pool (#2428). Identities are caller-generated UUIDs; the manager
// alone chooses paths, qgroups and limits within its configured allowance.
z.object({ operation: z.literal("poolStatus") }).strict(),
// Read-only residual-growth status (#3515) over the same observation and owner charges.
z.object({ operation: z.literal("poolGrowth") }).strict(),
z.object({ operation: z.literal("poolCreate"), id: nativeCheckpointId, purpose: z.enum(["source", "worktree", "staging"]),
from: nativeCheckpointId.nullable(), allowanceBytes: z.number().int().positive() }).strict(),
z.object({ operation: z.literal("poolSeal"), id: nativeCheckpointId }).strict(),
Expand Down Expand Up @@ -213,6 +215,65 @@ export interface NativePoolStatus {
counts: Record<NativePoolState, number>;
identities: number;
}
/**
* One measured comparison of the residual-growth status (#3515). `basisBytes`
* is the charge recorded when the chain's cold acquisition was sealed; null
* means the accounting needed for it is missing, never that it is zero.
*/
export interface NativePoolGrowthComparison {
id: string;
/** Sealed source this root was snapshotted from. */
from: string;
/** Cold acquisition at the root of `from`'s chain: the one with no source. */
cold: string | null;
/** The root's owner charge; null when the accounting has no charge for it, never zero. */
ownerBytes: number | null;
basisBytes: number | null;
/** `basisBytes` times the ratio threshold; null when the basis is unavailable. */
thresholdBytes: number | null;
state: "available" | "unavailable";
flagged: boolean;
}
/** Retained sources published under one name (one repository and revision) across views. */
export interface NativePoolGrowthSources {
name: string;
/** `ownerBytes` is null when the accounting has no charge for that root, never zero. */
roots: { id: string; view: string; ownerBytes: number | null }[];
/** Summed charge; null when any root's charge is missing. */
ownerBytes: number | null;
flagged: boolean;
}
export interface NativePoolGrowthFlag {
kind: "worktree" | "refresh" | "retained-sources";
/** Root identity, or the publication name for retained sources. */
subject: string;
/** Measured charge; null only for a retained-sources count whose summed charge is unknown. */
observedBytes: number | null;
/** Byte threshold for a ratio flag; null for the count flag. */
thresholdBytes: number | null;
/** The ruled threshold: a ratio, or the retained-source count. */
threshold: number;
/** Retained-source count; null for a ratio flag. */
observedCount: number | null;
}
/**
* Read-only residual-growth status (#3515). It reports measured owner charges
* and the ruled thresholds only: never forecast savings or freed capacity.
*/
export interface NativePoolGrowth {
basis: "btrfs-space-info-disk-used+simple-quota-owner";
state: "available" | "unavailable";
/** Why the whole status is unavailable; comparisons and flags are then empty. */
reason: string | null;
generation: string | null;
thresholds: { worktreeRatio: number; refreshRatio: number; retainedSources: number };
worktrees: NativePoolGrowthComparison[];
refreshes: NativePoolGrowthComparison[];
sources: NativePoolGrowthSources[];
flags: NativePoolGrowthFlag[];
/** Comparisons and source groups left out to stay within the control bound, unflagged ones first. */
omitted: number;
}
/** The privileged helper inspects only its bounded upper layer after the worker exits. */
export interface NativeStageChange { pathHex: string; kind: "entry" | "whiteout" | "opaque" }
export const NATIVE_CONTROL_BYTES = 64 * 1024;
Expand Down
Loading
Loading