Skip to content

Commit 9301b86

Browse files
roodboiclaude
andcommitted
fix(native): keep policy refusal reasons past the storage deadline and exclude only carrier proofs
- A carrier boundary now keeps a refusal's own observed reason (an issued helper policy boundary or a classified command cause) even when it is caught after the carrier deadline. `storage-budget` is reported only for a deadline-shaped refusal (timeout, budget, lifetime mark or no observed cause) whose carrier deadline expired and was not cancelled. - A recorded storage command that replaces an admission refusal with its fixed command refusal now carries the source's issued boundary, so a helper policy refusal observed during admission is no longer relabeled `storage-budget` at `storage-helper-start`. - The start deadline's storage exclusion now wraps only each carrier proof (carrier creation and witness verification). The guard's freshness checks and saved-state reads consume the start budget. - The exclusion-cap comment states that the uncapped sum is finite, (5B+3) x V x S, but can reach hours. Refs HACK-1361 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 616407a commit 9301b86

9 files changed

Lines changed: 218 additions & 44 deletions

‎docs/cli.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ The two variables bound different things:
297297

298298
When a carrier budget or the whole-phase cap runs out, the command reports the
299299
closed effect reason `storage-budget` in `error.detail.native_compose_effect_refusal`,
300-
and the human summary names `HACK_NATIVE_STORAGE_TIMEOUT_MS`. Exhaustion leaves the
300+
and the human summary names `HACK_NATIVE_STORAGE_TIMEOUT_MS`. A refusal with its own
301+
cause, such as a helper policy refusal, keeps that reason even when it is caught after
302+
the carrier deadline; raising the storage budget does not address it. Exhaustion leaves the
301303
same retained, uncertain state as any other storage refusal; inspect saved state
302304
before retrying.
303305

‎docs/reference/native-compose-commands.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,11 @@ expectation admission/write, journal, enrollment read, provision, root discovery
347347
seed, verification, closing root proof, completion write and publication. Carrier
348348
transport additionally distinguishes target, create, policy, start, response and
349349
cleanup; policy reasons identify shape, command, labels, host policy, mount
350-
cardinality, program/storage mounts and policy stability. A refusal that finds
351-
its carrier's storage budget exhausted, and not cancelled, reports
352-
`storage-budget`, which names `HACK_NATIVE_STORAGE_TIMEOUT_MS`; a start phase
350+
cardinality, program/storage mounts and policy stability. A policy refusal keeps
351+
its policy reason even when it is caught after the carrier's deadline. A timeout,
352+
budget or otherwise unclassified refusal that finds its carrier's storage budget
353+
exhausted, and not cancelled, reports `storage-budget`, which names
354+
`HACK_NATIVE_STORAGE_TIMEOUT_MS`; a start phase
353355
that spends its whole-phase storage cap reports the same reason at
354356
`guard-storage`. Unknown errors
355357
remain `unclassified`. Diagnostics contain no paths, resource names, helper replies,

‎docs/reference/native-compose-storage-xattr-carrier.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ Budgets are layered and all finite:
409409
1,000–600,000 ms). It is never derived from `HACK_COMPOSE_STARTUP_TIMEOUT_MS`,
410410
so a short start budget cannot starve a helper proof and a long one does not
411411
lengthen it.
412-
- In a native `up` or `restart` start phase, guard proof time is excluded from
412+
- In a native `up` or `restart` start phase, guard proof time (each carrier's
413+
creation and witness verification, not the guard's surrounding freshness
414+
checks and saved-state reads) is excluded from
413415
the start deadline but capped in total at 16 × the storage budget or 1 hour,
414416
whichever is smaller. A guard proof that would start after the cap is spent is
415417
refused before it runs, and one that ends past it refuses as `storage-budget`.
@@ -424,10 +426,13 @@ Budgets are layered and all finite:
424426
(`performance.now`), so a wall-clock step cannot shorten or extend it.
425427
Exhaustion refuses as `probe-budget`.
426428

427-
A refusal that finds its carrier's own deadline passed (and the carrier not
428-
cancelled) reports the closed effect reason `storage-budget`; a probe clipped to
429-
the remaining budget times out exactly at that end. Cancellation keeps
430-
`probe-cancel`. The reason changes no check, retention or recovery rule.
429+
A timeout, budget or otherwise unclassified refusal that finds its carrier's own
430+
deadline passed (and the carrier not cancelled) reports the closed effect reason
431+
`storage-budget`; a probe clipped to the remaining budget times out exactly at
432+
that end. A refusal with its own observed reason, such as a helper policy
433+
refusal, keeps that reason even when it is caught after the deadline, including
434+
when a recorded command replaces it with its fixed command refusal. Cancellation
435+
keeps `probe-cancel`. The reason changes no check, retention or recovery rule.
431436

432437
Only the original in-memory writer can advance its exact sequence. Publication
433438
failure or deadline permanently invalidates that writer and retains its private

‎src/lib/compose-startup-budget.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ export function resolveNativeStorageTimeoutMs(
104104
/**
105105
* Whole-phase cap on storage time excluded from one `up`/`restart` start
106106
* deadline. Each guard storage proof is bounded per carrier by
107-
* `HACK_NATIVE_STORAGE_TIMEOUT_MS` (S), but a phase runs up to (5B+3) guard
108-
* proofs for B builds, each over every enrolled volume, so the sum alone is
109-
* unbounded in practice. The excluded total may therefore not exceed
107+
* `HACK_NATIVE_STORAGE_TIMEOUT_MS` (S), and a phase runs up to (5B+3) guard
108+
* proofs for B builds, each with one carrier per enrolled volume (V). Without
109+
* a cap the excluded total is finite, at most (5B+3) x V x S, but it grows with
110+
* the build and volume counts and can reach hours: 4 builds and 3 volumes
111+
* allow 69 carriers, about 1.7 hours at the 90 s default and 11.5 hours at the
112+
* 600 s maximum. The excluded total may therefore not exceed
110113
* {@link NATIVE_STORAGE_EXCLUSION_MULTIPLE} x S or
111114
* {@link MAX_NATIVE_STORAGE_EXCLUSION_MS}, whichever is smaller: 24 minutes at
112115
* the 90 s default and 1 hour from S = 225 s up. Ordinary proofs take a small
@@ -147,8 +150,11 @@ export class NativeStorageTotalBudgetError extends Error {
147150
* `HACK_COMPOSE_STARTUP_TIMEOUT_MS`. Storage proofs that pre-spawn guards run
148151
* inside this phase are bounded by their own `HACK_NATIVE_STORAGE_TIMEOUT_MS`
149152
* carriers instead: their monotonic elapsed time extends this deadline rather
150-
* than consuming it. Overlapping exclusions count once, a rejected proof still
151-
* counts as excluded time, and the deadline never moves earlier.
153+
* than consuming it. Only each carrier proof (carrier creation and witness
154+
* verification) is excluded; a guard's freshness checks and saved-state reads
155+
* around it consume the start budget. Overlapping exclusions count once, a
156+
* rejected proof still counts as excluded time, and the deadline never moves
157+
* earlier.
152158
*
153159
* The excluded total is capped by
154160
* {@link nativeComposeStorageExclusionCapMs}: the deadline moves at most that

‎src/lib/native-compose-command-storage.ts‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ async function volumeNames(signal: AbortSignal): Promise<readonly string[]> {
130130
export type NativeComposeCommandStorageVerifyOptions = {
131131
/** Only when nothing but read-only checks ran since this lease's previous proof. */
132132
readonly adjacentProof?: true;
133+
/**
134+
* Wraps each enrolled volume's carrier proof (fresh carrier creation and its
135+
* witness verification), which its `HACK_NATIVE_STORAGE_TIMEOUT_MS` carrier
136+
* deadline bounds. The surrounding freshness checks and saved-state reads stay
137+
* outside it. A native `up`/`restart` start deadline passes its storage
138+
* exclusion here, so only bounded carrier time extends that deadline.
139+
*/
140+
readonly excludeProof?: <T>(proof: () => Promise<T>) => Promise<T>;
133141
};
134142

135143
/** Command admission owns no helper process. Each proof gets a fresh finite carrier;
@@ -236,6 +244,8 @@ export async function prepareNativeComposeCommandStorage(opts: {
236244
if (before.storageWitnessesPending) {
237245
throw new NativeComposeGenerationError("E_NATIVE_COMPOSE_UNCERTAIN");
238246
}
247+
const excludeProof =
248+
options.excludeProof ?? (<T>(proof: () => Promise<T>) => proof());
239249
for (const entry of before.storageWitnesses ?? []) {
240250
if (
241251
entry.state !== "enrolled" ||
@@ -244,16 +254,18 @@ export async function prepareNativeComposeCommandStorage(opts: {
244254
) {
245255
return refuse();
246256
}
247-
await verifyNativeComposeStorageXattrWitness({
248-
authority: mutation.materialAuthority,
249-
generation,
250-
engineId,
251-
reference: entry.reference,
252-
carrier: await carrier(),
253-
...(options.adjacentProof === true
254-
? { adjacentProof: true as const }
255-
: {}),
256-
});
257+
await excludeProof(async () =>
258+
verifyNativeComposeStorageXattrWitness({
259+
authority: mutation.materialAuthority,
260+
generation,
261+
engineId,
262+
reference: entry.reference,
263+
carrier: await carrier(),
264+
...(options.adjacentProof === true
265+
? { adjacentProof: true as const }
266+
: {}),
267+
})
268+
);
257269
}
258270
const after = await store.loadCurrent();
259271
if (

‎src/lib/native-compose-command.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,12 +1727,13 @@ async function executePreparedGeneration(opts: {
17271727
? () => {
17281728
const adjacentProof = adjacentGuardProof;
17291729
adjacentGuardProof = false;
1730-
return start.excludeStorage(() =>
1731-
storage.verify(
1732-
generation,
1733-
adjacentProof ? { adjacentProof } : {}
1734-
)
1735-
);
1730+
// Only each carrier proof is excluded; the guard's
1731+
// freshness checks and saved-state reads consume the
1732+
// start budget like any other start-phase work.
1733+
return storage.verify(generation, {
1734+
...(adjacentProof ? { adjacentProof } : {}),
1735+
excludeProof: start.excludeStorage,
1736+
});
17361737
}
17371738
: undefined,
17381739
}),

‎src/lib/native-compose-storage-witness-docker.ts‎

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
markNativeComposeStorageBudgetRefusal,
1717
type NativeComposeEffectRefusal,
1818
nativeComposeEffectReason,
19+
nativeComposeEffectRefusal,
1920
retainNativeComposeEffectRefusal,
2021
} from "./native-compose-effect-diagnostics.ts";
2122
import {
@@ -932,14 +933,21 @@ type StorageCommandRefusal = "probe-timeout" | "probe-cancel" | "probe-budget";
932933
const storageCommandRefusals = new WeakMap<object, StorageCommandRefusal>();
933934
/** The same fixed refusal, keeping only its closed command cause (a timed-out or
934935
* cancelled child, or an exhausted publication/settlement budget) for the effect
935-
* diagnostic. It grants nothing and changes no check, deadline or retention. */
936-
function refuseCommand(reason: StorageCommandRefusal | undefined): never {
936+
* diagnostic. A replaced `source` refusal keeps its issued owner boundary (for
937+
* example a helper policy refusal observed during admission), so a later carrier
938+
* boundary cannot relabel it. It grants nothing and changes no check, deadline or
939+
* retention. */
940+
function refuseCommand(
941+
reason: StorageCommandRefusal | undefined,
942+
source?: unknown
943+
): never {
937944
const error = new Error(
938945
"Native storage xattr proof refused; values omitted."
939946
);
940947
if (reason) {
941948
storageCommandRefusals.set(error, reason);
942949
}
950+
copyNativeComposeEffectRefusal(source, error);
943951
throw error;
944952
}
945953
function storageCommandRefusal(
@@ -964,12 +972,24 @@ export function nativeComposeStorageCarrierReason(
964972
return storageCommandRefusal(error) ?? nativeComposeEffectReason(error);
965973
}
966974
/**
967-
* Effect reason at a carrier boundary of one storage carrier. When its own
968-
* deadline, the named `HACK_NATIVE_STORAGE_TIMEOUT_MS` storage budget, is
969-
* exhausted and the carrier was not cancelled, the refusal names that budget:
970-
* every probe and command inside it is clipped to the remaining budget, so its
971-
* timeout or budget refusal fires exactly at that end. Otherwise it keeps the
972-
* classified command cause. Diagnostics only: no check, deadline or retention changes.
975+
* Reasons an expired carrier deadline itself can produce: every probe and
976+
* command inside a carrier is clipped to the remaining budget (a timeout or
977+
* budget refusal), its lifetime check marks `storage-budget`, and a refusal
978+
* with no observed cause stays `unclassified`. Any other reason, such as a
979+
* helper policy refusal, was observed independently of the deadline.
980+
*/
981+
const deadlineReasons: ReadonlySet<NativeComposeEffectRefusal["reason"]> =
982+
new Set(["probe-timeout", "probe-budget", "storage-budget", "unclassified"]);
983+
/**
984+
* Effect reason at a carrier boundary of one storage carrier. A refusal that
985+
* already carries its own observed reason (an issued boundary such as a helper
986+
* policy refusal, or a classified command cause) keeps it even when it is caught
987+
* after the deadline, so an operator is not sent to raise the storage budget for
988+
* a policy failure. Only a deadline-shaped refusal (timeout, budget, lifetime
989+
* mark or no observed cause) whose carrier deadline, the named
990+
* `HACK_NATIVE_STORAGE_TIMEOUT_MS` storage budget, has expired and whose carrier
991+
* was not cancelled names that budget. Diagnostics only: no check, deadline or
992+
* retention changes.
973993
*/
974994
export function nativeComposeStorageCarrierBudgetReason(opts: {
975995
readonly error: unknown;
@@ -978,10 +998,19 @@ export function nativeComposeStorageCarrierBudgetReason(opts: {
978998
readonly now?: () => number;
979999
}): NativeComposeEffectRefusal["reason"] {
9801000
const now = opts.now ?? Date.now;
981-
if (!opts.signal.aborted && now() >= opts.deadline) {
1001+
const issued = nativeComposeEffectRefusal(opts.error)?.reason;
1002+
const observed =
1003+
issued && issued !== "unclassified"
1004+
? issued
1005+
: nativeComposeStorageCarrierReason(opts.error);
1006+
if (
1007+
deadlineReasons.has(observed) &&
1008+
!opts.signal.aborted &&
1009+
now() >= opts.deadline
1010+
) {
9821011
return "storage-budget";
9831012
}
984-
return nativeComposeStorageCarrierReason(opts.error);
1013+
return observed;
9851014
}
9861015

9871016
export async function runNativeComposeStorageDockerCommand(supplied: {
@@ -1040,6 +1069,8 @@ export async function runNativeComposeStorageDockerCommand(supplied: {
10401069
let armed: NativeComposeStorageArmedCommand | null = null;
10411070
let publicationRefused = false;
10421071
let publicationCause: StorageCommandRefusal | undefined;
1072+
// The first refusal that stopped publication; only its issued boundary is kept.
1073+
let publicationSource: unknown;
10431074
const cancellation = new AbortController();
10441075
try {
10451076
await opts.assertAdmitted();
@@ -1058,7 +1089,7 @@ export async function runNativeComposeStorageDockerCommand(supplied: {
10581089
return refuseCommand("probe-budget");
10591090
}
10601091
if (publicationRefused) {
1061-
return refuseCommand(publicationCause);
1092+
return refuseCommand(publicationCause, publicationSource);
10621093
}
10631094
if (
10641095
!(recordingSettlement || opts.cleanup) &&
@@ -1100,11 +1131,12 @@ export async function runNativeComposeStorageDockerCommand(supplied: {
11001131
} catch (error) {
11011132
publicationRefused = true;
11021133
publicationCause ??= storageCommandRefusal(error);
1134+
publicationSource ??= error;
11031135
if (record) {
11041136
invalidateNativeComposeStorageCommandOwner(record.owner);
11051137
}
11061138
cancellation.abort();
1107-
return refuseCommand(publicationCause);
1139+
return refuseCommand(publicationCause, publicationSource);
11081140
} finally {
11091141
if (timer !== undefined) {
11101142
clearTimeout(timer);
@@ -1252,6 +1284,7 @@ export async function runNativeComposeStorageDockerCommand(supplied: {
12521284
} catch (error) {
12531285
publicationRefused = true;
12541286
publicationCause ??= storageCommandRefusal(error);
1287+
publicationSource ??= error;
12551288
invalidateNativeComposeStorageCommandOwner(record.owner);
12561289
cancellation.abort();
12571290
}
@@ -1268,7 +1301,7 @@ export async function runNativeComposeStorageDockerCommand(supplied: {
12681301
settled = true;
12691302
if (record) {
12701303
if (publicationRefused) {
1271-
return refuseCommand(publicationCause);
1304+
return refuseCommand(publicationCause, publicationSource);
12721305
}
12731306
if (!(armed && exit)) {
12741307
return refuse();
@@ -1311,7 +1344,7 @@ export async function runNativeComposeStorageDockerCommand(supplied: {
13111344
} catch (error) {
13121345
if (opts.originalCommand) {
13131346
invalidateNativeComposeStorageCommandOwner(opts.originalCommand.owner);
1314-
return refuseCommand(storageCommandRefusal(error));
1347+
return refuseCommand(storageCommandRefusal(error), error);
13151348
}
13161349
throw error;
13171350
} finally {

‎tests/native-compose-storage-command-record.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import {
1717
} from "node:fs/promises";
1818
import { tmpdir } from "node:os";
1919
import { join } from "node:path";
20+
import {
21+
nativeComposeEffectRefusal,
22+
retainNativeComposeEffectRefusal,
23+
} from "../src/lib/native-compose-effect-diagnostics.ts";
2024
import {
2125
holdDirectory,
2226
writeExclusive,
@@ -32,6 +36,7 @@ import {
3236
settleNativeComposeStorageCommand,
3337
} from "../src/lib/native-compose-storage-command-record.ts";
3438
import {
39+
nativeComposeStorageCarrierBudgetReason,
3540
nativeComposeStorageCarrierReason,
3641
runNativeComposeStorageDockerCommand,
3742
} from "../src/lib/native-compose-storage-witness-docker.ts";
@@ -529,6 +534,70 @@ test(
529534
}),
530535
30_000
531536
);
537+
test.each([
538+
["before arming", 1],
539+
["after arming", 2],
540+
["after the stopped child is published", 3],
541+
] as const)(
542+
"a helper policy refusal %s keeps its own reason after the carrier deadline",
543+
(_, failingCall) =>
544+
ownedCase(async () => {
545+
const f = await fixture(),
546+
marker = join(f.root, "effect"),
547+
signal = new AbortController().signal,
548+
deadline = Date.now() + 5000;
549+
let calls = 0;
550+
try {
551+
const caught = await runNativeComposeStorageDockerCommand({
552+
context: { signal, deadline },
553+
files: f.files,
554+
args: ["/bin/sh", "-c", 'printf complete > "$1"', "owned", marker],
555+
timeoutMs: 5000,
556+
beforeSpawn: () => {},
557+
// The carrier's admission re-inspects the helper; the chosen call
558+
// observes a policy refusal with its own issued boundary.
559+
assertAdmitted: async () => {
560+
calls += 1;
561+
if (calls === failingCall) {
562+
const policy = new Error(
563+
"Native storage xattr proof refused; values omitted."
564+
);
565+
retainNativeComposeEffectRefusal(policy, {
566+
stage: "storage-helper-policy",
567+
reason: "helper-host-policy",
568+
});
569+
throw policy;
570+
}
571+
},
572+
originalCommand: { owner: f.owner, kind: "create", carrier: null },
573+
}).catch((error: unknown) => error);
574+
expect(caught).toBeInstanceOf(Error);
575+
expect((caught as Error).message).toContain("values omitted");
576+
expect(calls).toBe(failingCall);
577+
expect(existsSync(marker)).toBe(false);
578+
// The replaced command refusal keeps the first issued boundary.
579+
expect(nativeComposeEffectRefusal(caught)).toEqual({
580+
stage: "storage-helper-policy",
581+
reason: "helper-host-policy",
582+
});
583+
// Caught after the carrier deadline (fake clock), it still names the
584+
// policy failure, never the storage budget.
585+
expect(
586+
nativeComposeStorageCarrierBudgetReason({
587+
error: caught,
588+
signal,
589+
deadline,
590+
now: () => deadline + 1,
591+
})
592+
).toBe("helper-host-policy");
593+
} finally {
594+
if (!nativeComposeStorageCommandOwnerConfirmed(f.owner)) {
595+
retained.add(f.root);
596+
}
597+
}
598+
})(),
599+
30_000
600+
);
532601
test(
533602
"failed arm directory sync prevents the target spawn rather than proving no-spawn from a record",
534603
ownedCase(async () => {

0 commit comments

Comments
 (0)