Skip to content

feat(agent): hold and evaluate 2.x policies in the package broker - #2229

Draft
Benoît Cortier (CBenoit) wants to merge 17 commits into
claude/broker-v2-base-7702d2from
claude/broker-v2-policy-7702d2
Draft

Benoît Cortier (CBenoit) wants to merge 17 commits into
claude/broker-v2-base-7702d2from
claude/broker-v2-policy-7702d2

Conversation

@CBenoit

@CBenoit Benoît Cortier (CBenoit) commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Temporary git dependencies, each of which must become a crates.io version before this merges:

  • now-policy, now-policy-api and now-policy-server-template: [patch.crates-io] in the root Cargo.toml, on Devolutions/now-libraries at 5b23b1a616ca97590d4d6b9e610cd07772d30453, for the 2.0 package broker API types.
  • now-policy-eval: a dependency of crates/now-package-broker, same repository at 25e75110c067239e44e42f005abbf566f32f803a, for the 2.x policy reader and evaluator.

Builds on #2224, #2225 and #2226: the base branch is master with those three merged.

The package broker holds a policy file in format 2.x and decides requests against it with the now-policy-eval evaluator.
Nothing serves the 2.0 request encoding yet: this change gives the policy store what the /v2/ endpoints need, and the endpoints themselves follow in the pull requests stacked on this one.

The store observes the policy file once and publishes two views of that observation.
The 1.x endpoints keep the snapshot they read today.
The 2.0 view is the policy management state of CONTRACT.wire.md: Active with the accepted document and its text, Missing, or Invalid with the policy errors of the 2.x reader or the reason the storage failed.
A configured path whose form the broker does not read is UnsupportedPath for the 2.0 view, and the 1.x endpoints report it as before.

A replacement of the policy goes through one store operation that answers the outcomes in the order of the contract's error list: a stale store token, a policy that cannot be written, an invalid document, a Revision that is not higher than the accepted one for the same Id, a write that failed, a written file the broker does not hold, and the replacement.
The accepted document is written exactly as sent, and every outcome that reaches the store's audit is recorded once.

A request the policy allows is still refused, as not runnable, when it would run a Pre or Post command with administrator rights, including when audit mode is what allowed it.

The 1.x policy endpoints never write over a file in format 2.x.
The file is told from the errors of the 2.x reader, and from its top-level PolicyFormatVersion members when the reader stops on a repeated key before it reads the format version.
A file the 2.x reader finds malformed or too large stays writable for them, so a broken 1.x file can still be repaired.

No conversion from a 1.x policy is defined, so the store refuses each one: with a 1.x file in place the 2.0 view is Invalid with Unconvertible, and the 1.x endpoints keep enforcing the file.
The refusal is one function that returns either a 2.x document with the differences it reports, or a refusal, so a conversion replaces it without touching the store.

Scope item Outcome Evidence
Load 2.x policies Done policy_store::document_tests: valid_document_file_is_active_with_its_text, invalid_document_file_is_invalid_with_its_errors, missing_file_is_missing, file_changed_by_someone_else_is_the_accepted_policy_after_a_reload; v2::policy: accepted_policy_keeps_its_text_and_identity, leading_byte_order_mark_stays_in_the_document_text
Evaluate with now-policy-eval Done v2::policy: evaluation_decides_against_the_document, evaluation_uses_the_given_time, request_that_is_not_well_formed_gets_no_decision
Refuse a replacement with the same Id unless its Revision is higher Done v2::policy::revision_is_refused_only_for_the_same_id_and_no_higher_revision; document_tests::revision_that_is_not_higher_for_the_same_id_is_refused_before_writing, invalid_document_is_refused_with_its_errors_before_its_revision_is_compared
Replacement outcomes in the contract's order Done document_tests: stale_store_token_is_decided_before_writability, read_only_policy_is_not_writable_whatever_the_document, write_that_fails_leaves_the_policy_as_it_was, written_document_the_broker_does_not_hold_is_an_activation_failure, only_one_of_two_replacements_with_the_same_store_token_succeeds, accepted_replacement_holds_the_document_exactly_as_sent
Never run Pre or Post commands for a request that runs elevated, audit mode included Done at the decision; the operation side is in the next pull request v2::policy: allowed_elevated_request_with_commands_is_not_runnable, request_allowed_by_audit_mode_is_not_runnable_either, denied_request_is_not_reported_as_not_runnable
1.x endpoints and a 2.x file Done: they never write over it document_tests: earlier_endpoints_write_no_file_the_reader_tells_is_in_format_2, format_1_write_never_replaces_a_format_2_file
Policy path of a form the broker does not read Done: UnsupportedPath for the 2.0 view policy_store::windows tests of the observation of such a path
Convert a 1.x policy Stopped: no rule is defined, so every 1.x policy is refused and reported as Unconvertible document_tests: format_1_file_is_unconvertible_while_the_earlier_endpoints_still_read_it, converted_format_1_policy_is_active_with_the_text_of_its_file (the slot, with a test conversion), conversion_that_is_no_valid_document_is_unconvertible

Issue: DGW-499
Changelog: ignore

🤖 Generated with Claude Code

The package broker reads 2.x policies and evaluates requests with `now-policy-eval`.
The crate is not published yet, so the dependency is pinned to a git revision until it is.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The 2.0 request and response types live in `now_policy_api::v2`, which no published version carries yet.
The now-policy crates are patched to one git revision until they are published, so the Agent and the package
broker keep sharing one copy of the 1.x types.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
An accepted policy keeps the text it was read from next to the parsed document, because the management
state reports that text and a replacement compares identities without reading the file again.
`Metadata.Id` and `Metadata.Revision` are read from the valid document, since the evaluator does not expose them.

The evaluation time goes through the timestamp the broker writes in its responses, because the evaluator
reads its time from text only.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The policy store now reads the policy file once for both request
encodings.
Beside the 1.x snapshot it holds what the 2.0 endpoints report: the
policy management state of the wire contract, and the accepted policy
when the file is a valid 2.x document.
A replacement of the 2.0 encoding writes the document text unchanged as
the whole file through the transactions the 1.x writes use, decides its
outcome in the order the wire contract gives, and succeeds only when the
file read back holds exactly the document that was sent.

A file in format 2.x is read-only for the 1.x management endpoints,
because their reader reports it as invalid and a repair would overwrite
it.
A file the 1.x reader accepts stays exactly as it was for those
endpoints, and is `Unconvertible` for the 2.0 endpoints: no conversion
is defined, so the one function a conversion would live in refuses
every policy.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A second, lenient JSON parser decided whether the policy file is in
format 2.x, and it disagreed with the reader of that format.
A 2.x document holding a number that parser refuses was not told as
2.x, so a 1.x repair could overwrite it, and a document repeating
`PolicyFormatVersion` was told as 2.x although the reader calls it
malformed.
The store now tells the format from the errors the reader reports, in
the code that exists only for the 1.x endpoints.

A replacement of the 2.0 encoding fails before it records an attempt or
touches the file when no policy management state can carry the policy
path, so nothing can fail once the outcome is decided.
Its success audit record names the 1.x policy it replaced when the file
held one, because the 1.x endpoints enforced that policy.
The storage observes the written file with the source the store reads
the path from.

A 1.x policy refused for the 2.0 endpoints is logged once for each
change of the file, so the refusal is reported beyond the policy
management state.

Tests now pin how each storage failure and read-only reason maps to the
`InvalidReason` and `ReadOnlyReason` of the wire contract, and that
recovery accepts a published replacement that is a 2.x document.
The content check of a recovered replacement is its own function, so a
test reaches it without a file secured for the broker, which only an
elevated process can create.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ersion

The 2.x reader reports a repeated key before it reads the format
version, and reports nothing else.
A file whose only `PolicyFormatVersion` is `"2.0.0"` and that repeats a
key elsewhere is a 2.x file the errors of the reader cannot tell from a
1.x file that repeats a key, so a 1.x repair could overwrite a policy
that belongs to the 2.0 endpoints.

The rule that tells a 2.x file changes for that one case.
When the reader reports a repeated key other than the format version,
the top-level `PolicyFormatVersion` members of the document decide: the
file is in format 2.x when there is at least one and each of them is the
string `"2.0.0"`.
They are read without interpreting any other value, so a number of any
magnitude does not prevent it.
A 1.x file that repeats a key stays repairable, and so does a file that
gives its format version twice, whatever the two values.
Every other case is still decided from the errors of the reader.
A malformed or too large 2.x file stays writable for the 1.x endpoints,
because nothing reads a format version from it.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…d path

The policy management state of the 2.0 endpoints has one reason for a
path whose form or extension the broker does not read or write, and
keeps `InsecureStorage` and `UnsafePath` for a path or location that
fails the storage security checks.
A relative path, a path ending with a separator, a path with a `.` or
`..` component and a path that names no file were reported with the
security reasons, as the 1.x management state reports them.
The 2.0 state now gives `UnsupportedPath` for both the invalid reason
and the read-only reason of such a path, like a path with an extension
the broker does not read.
The 1.x management state is unchanged.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The `Path` of the 2.0 policy management state is the configured path,
not the canonical path of the observation, which the 1.x management
state shows.
The success audit record of a replacement can name the 1.x policy the
replacement took the place of.
A 1.x policy refused for the 2.0 endpoints is logged each time the
store builds a view, which a change of the security or the write
capability of unchanged bytes also causes.
The observation a document write returns is read with the configuration
source of the store, a property no test checks.

The test of the content check of a recovered replacement gains a 1.x
policy that parses and fails validation, and compares the refusal, so
each case reaches the branch it is meant for.
The observation doubles of the storage tests move next to the test
module, out of the production items.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… version

Whether a file that repeats a key is in format 2.x depended on which key
the 2.x reader reports first.
A file giving `"2.0.0"` twice as its `PolicyFormatVersion` was not told
as a 2.x file, while the same file with a repeated key of another object
between the two was.

The rule is now the same for every repeated key.
When the reader reports a repeated key, whatever the key, the top-level
`PolicyFormatVersion` members of the document decide: the file is in
format 2.x when there is at least one and each of them is the string
`"2.0.0"`.
A file that gives that format version twice is in format 2.x, so the 1.x
management endpoints do not write it.
A file that gives two different format versions is not, and a 1.x file
that repeats a key stays repairable.
Every other case is still decided from the errors of the reader.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… decided

The wire contract decides each of these cases, and a broker that got
them wrong passed every test:

- A stale store token sent while the file is not watched is
  `StaleStoreToken`, not `PolicyNotWritable`.
- A file that became read-only after its state was read is
  `PolicyNotWritable`, decided from the file as it is now.
- A file someone else changed before a write that then fails for
  another reason is `StaleStoreToken`, and the change is reported.
- A file that holds another valid document after the write is
  `ActivationFailed`, not a successful replacement.
- A file the broker starts watching is no longer read-only for
  `MonitoringUnavailable`.

The table of documents with one defect compared the code and pointer of
four of the ten errors the policy contract has a reader report.
It now has a document too large, a repeated key, a missing key,
conflicting keys, a value of the wrong type and a repeated rule id.

The table of files the 1.x endpoints do not write gains a 2.x document
that misses a required key other than its format version, which a
reader taking any missing key for a missing format version would hand
to the 1.x endpoints.

The storage double can refuse writes after it was built, and can fail a
write over a file someone else changed without telling of the change.

Issue: DGW-499

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The 2.0 view of a path of unsupported form is set right beside the code
that builds the 1.x view of it, and no test of the storage decided what
the 1.x endpoints report for such a path.
A test now pins the state, the write capability and the read-only
reason they get for each rule that refuses a form.

The path meant to name no file ended with a separator, so the rule for
a trailing separator refused it and the rule for a missing file name was
never reached.
A share root reaches that rule, which its own test now shows.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…nd nesting

The members that decide the format of a file that repeats a key are read
by another parser than the 2.x reader, so the two must read a key, a
value and a nested value alike.
The table now holds a format version key and a format version value
spelled with a JSON escape, and a value nested to the deepest level the
reader accepts, each alone and beside a repeated key, and a value nested
one level deeper.
The two agree on every row, so the rule is unchanged.

The fixtures that rewrite the text of a document now fail when the text
they look for is not there exactly once.
A fixture that returns its input unchanged leaves a row that no longer
tests its case, and the row still passes where a valid document is
expected.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The documentation of `observe` gave `UnsupportedFormat` as the read-only
reason of every path of unsupported form or extension.
The 1.x endpoints get `UnsafePath` for a form and `UnsupportedFormat`
for an extension, and the 2.0 endpoints get `UnsupportedPath` for both.

The read-only reason of a file the store stopped watching is marked as
a value no test decides in a flow the broker has: the state of an
unwatched file gets its reason elsewhere, and the test of the mapping
uses a storage that gives the reason for a watched file.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Copilot AI adaptive review requested due to automatic review settings December 1, 2026 03:19
@CBenoit

Copy link
Copy Markdown
Member Author

Intent drift audit: the broker's 2.x policy side below the HTTP routes

Audited against the policies folder of Devolutions/now-libraries at 77307f5106d9764d03a420d22fd8377c99432af5: CONTRACT.wire.md (38 rows), CONTRACT.policy.md (12 rows), INTENT.md (5 rows), INTENT.wire.md (2 rows), INTENT.legacy.md (5 rows).
Revision audited: e6093c06c, as the diff from 983f00e59.
Code audited: v2/policy.rs and policy_store/** of crates/now-package-broker/src/; every path below is relative to that folder, and every line number is a line at e6093c06c.
All audited code is new in this diff, so every verdict is "introduced" unless a row says otherwise.

Result: 0 violates, 2 partial, 1 cannot verify, 5 not yet realised, 54 conforms (62 rows).

A statement ID is the section anchor plus the ordinal of the statement in its section; a table counts as one statement, and a letter names a clause or a table row.

ID Statement Verdict Evidence Note
INTENT.legacy.md#format-1-1 The broker converts a 1.x policy in memory when it loads it, refuses any policy it cannot convert exactly, and reports the refusal. partial (known, reported) policy_store/format_1.rs:30-32, policy_store/document.rs:129-135, policy_store/document.rs:198-206, policy_store/document_tests.rs:435, policy_store/document_tests.rs:470 No conversion is defined: every 1.x policy is refused, reported as Invalid / Unconvertible and logged once per observed change, and the 1.x endpoints keep enforcing it. See Partials.
CONTRACT.wire.md#management-state-2a "State is Active when the broker holds an accepted policy read from the policy file, Missing when no file exists at Path, and Invalid otherwise" partial (judgement) policy_store/windows.rs:1323-1352, policy_store/windows.rs:1391-1415 before 1524-1558, policy_store/mod.rs:396-421, test policy_store/windows.rs:5036 Missing is reported only when the storage reached the file lookup. Three states are Invalid whether or not a file exists. See Partials.
CONTRACT.wire.md#management-state-8 The InvalidReason table: ReadFailed "cannot be opened or read", InsecureStorage "or its location fails the broker's storage security checks" cannot verify (judgement) policy_store/windows.rs:1364-1380, policy_store/windows.rs:1959-1965, policy_store/document.rs:48 An existing file under a directory that fails the security verification is ReadFailed, with the ReadOnlyReason UnsafePath. Whether ReadFailed "applies" to a file the broker declines to open decides this; if it does, the choice is implementation-defined (#management-state-11). The other rows conform: policy_store/document.rs:48-51, policy_store/document.rs:128, policy_store/document.rs:134, policy_store/windows.rs:1348-1351, tests policy_store/document_tests.rs:492, :417, :435, :623.
CONTRACT.policy.md#evaluation-1 The broker evaluates each request once, before running it, against the one policy it holds, at its current time. not yet realised v2/policy.rs:75-84, policy_store/mod.rs:321-323 The store holds one accepted policy and evaluate takes the time from its caller; no production code calls either at this revision.
CONTRACT.policy.md#evaluation-3c The broker "runs no request while it holds no accepted policy". not yet realised policy_store/mod.rs:321-323, policy_store/document.rs:145-150 accepted_policy() is None unless the state is Active; nothing that runs a request reads it yet.
CONTRACT.wire.md#not-runnable-2 The error response carries the decision, and the broker runs nothing for the request. not yet realised v2/policy.rs:127-131 The predicate has no caller outside its tests.
CONTRACT.wire.md#not-runnable-3 An evaluation request and an execution request get the same NotRunnable outcome. not yet realised v2/policy.rs:127-131 The predicate is a pure function of the request and its decision, which is what the statement needs once both endpoints call it.
INTENT.md#broker-limits-2 An allowed request that asks for one runs nothing, and that refusal is not a decision. not yet realised v2/policy.rs:122-131 Same as above.
CONTRACT.wire.md#def-accepted-policy-1 The accepted policy is the one policy the broker holds and evaluates package requests with. conforms policy_store/mod.rs:211, policy_store/mod.rs:321-323, v2/policy.rs:15-21 One DocumentView per snapshot.
CONTRACT.wire.md#policy-identity-1 A policy identity has Id and Revision, copied from the policy's Metadata. conforms v2/policy.rs:67-72, v2/policy.rs:156-164, test v2/policy.rs:238
CONTRACT.wire.md#not-runnable-1 An allowed request whose OptIns has PrePostCommands and whose Elevation is Elevated is NotRunnable, whatever the decision's Audited. conforms v2/policy.rs:127-131, tests v2/policy.rs:363, :378, :397, :411 A denied request is not NotRunnable, as CONTRACT.wire.md#evaluate orders it.
CONTRACT.wire.md#errors-1a Management is present when Code is StaleStoreToken, PolicyNotWritable, RevisionNotHigher or ActivationFailed, and is the state after the request. conforms policy_store/mod.rs:232-241, policy_store/mod.rs:721-723, :727-729, :745, :794-796, test policy_store/document_tests.rs:656-670 Store side only: the outcome carries the state, the response is not built here.
CONTRACT.wire.md#errors-1b Errors is a non-empty list when Code is InvalidPolicy. conforms policy_store/mod.rs:732-737, v2/policy.rs:26-41, test policy_store/document_tests.rs:742 The reader returns at least one error for a document it refuses (now-policy-eval src/document.rs:141-190 at the pinned revision).
CONTRACT.wire.md#management-state-1a State is always present; Path is always present, a non-empty string of at most 32767 UTF-16 code units. conforms policy_store/mod.rs:294, policy_store/mod.rs:329-331, policy_store/document.rs:155-159, test policy_store/document_tests.rs:400 A path no state can carry gives no state at all (ValueError). See Not covered.
CONTRACT.wire.md#management-state-1b, #management-state-3 StoreToken is always present, 1 to 512 characters of the token alphabet, first one a letter or digit. conforms policy_store/mod.rs:333-337, policy_store/windows.rs:2009-2011, policy_store/mod.rs:978-988 Every token is a hyphenated version 4 UUID minted by the store.
CONTRACT.wire.md#management-state-1c ReadOnlyReason is present when a replacement cannot write the policy. conforms policy_store/document.rs:160-164, policy_store/mod.rs:696-710, policy_store/mod.rs:726-730, tests policy_store/document_tests.rs:532, :709, :726 A replacement is PolicyNotWritable exactly when the state it carries has a reason.
CONTRACT.wire.md#management-state-1d Document is present when State is Active. conforms policy_store/document.rs:170-175, tests policy_store/document_tests.rs:400, :457
CONTRACT.wire.md#management-state-1e InvalidReason is present when State is Invalid; Errors, non-empty, when it is InvalidDocument. conforms policy_store/document.rs:176-184, policy_store/document.rs:105-106, test policy_store/document_tests.rs:417
CONTRACT.wire.md#management-state-2b The broker holds no accepted policy unless State is Active. conforms policy_store/document.rs:145-150, policy_store/document.rs:170-174 Both read the same DocumentState.
CONTRACT.wire.md#management-state-4 Each content of the policy file, and its absence, has its own store token; two states carry the same token only when the broker observed no change between them. conforms policy_store/mod.rs:978-984, policy_store/mod.rs:836-850, policy_store/mod.rs:799-804, tests policy_store/document_tests.rs:638, :674 A new fingerprint gets a random token, and a successful replacement gets one even if the storage reports the old fingerprint.
CONTRACT.wire.md#management-state-5 Document is the file's bytes decoded as UTF-8, a leading byte order mark becomes U+FEFF, and a replaced document comes back exactly as sent. conforms v2/policy.rs:40-44, policy_store/document.rs:124-127, tests v2/policy.rs:252, policy_store/document_tests.rs:780-831
CONTRACT.wire.md#management-state-6 When the accepted policy was converted from an earlier format, Document holds the file's text in that format. conforms policy_store/document.rs:129-133, test policy_store/document_tests.rs:593 Reachable only with a test converter, because the production conversion refuses everything.
CONTRACT.wire.md#management-state-9 The ReadOnlyReason table is a closed set, each value with its meaning. conforms policy_store/document.rs:75-89, policy_store/document.rs:160-164, policy_store/windows.rs:1348-1351, test policy_store/document_tests.rs:532 See Not covered for a file that cannot be read and for the time before the watcher is ready.
CONTRACT.wire.md#management-state-10 A state whose InvalidReason is MonitoringUnavailable has the ReadOnlyReason MonitoringUnavailable. conforms policy_store/mod.rs:396-421, policy_store/document.rs:51, policy_store/document.rs:78, policy_store/document.rs:160-164, test policy_store/document_tests.rs:623
CONTRACT.wire.md#policy-errors-1 A policy error has Code, an error code of the policy contract, and Pointer, a JSON Pointer, possibly empty. conforms v2/policy.rs:133-153 The match is exhaustive, so a new reader code fails the build.
CONTRACT.wire.md#validation-2 The document is the UTF-8 encoding of the string, so the size limit and the byte order mark of the policy contract apply to it. conforms v2/policy.rs:115-120, v2/policy.rs:27-29, policy_store/mod.rs:732 The bytes go to the reader whole and unchanged.
CONTRACT.wire.md#validation-4 Errors is the list the policy contract has a reader report, empty exactly when the document is valid. conforms v2/policy.rs:115-120, tests v2/policy.rs:265, :271
CONTRACT.wire.md#validation-5 Validation reads and changes nothing the broker stores. conforms v2/policy.rs:115-120 A free function of the bytes; it takes no store.
CONTRACT.wire.md#replacement-2 The current store token is that of the policy file as it is when the broker decides the request, or when it writes the file. conforms policy_store/mod.rs:693-694, policy_store/mod.rs:712-724, policy_store/mod.rs:748-753 The file is observed under the writer lock and the write is conditional on that observation. An unwatched file is not observed: see Not covered.
CONTRACT.wire.md#replacement-3.1 StaleStoreToken, when ExpectedStoreToken is not the current store token. conforms policy_store/mod.rs:704-707, policy_store/mod.rs:720-724, tests policy_store/document_tests.rs:656, :674
CONTRACT.wire.md#replacement-3.2 PolicyNotWritable, when the current state has a ReadOnlyReason. conforms policy_store/mod.rs:708-709, policy_store/mod.rs:726-730, tests policy_store/document_tests.rs:709, :726
CONTRACT.wire.md#replacement-3.3 InvalidPolicy, when the document is not valid, with its errors as validation gives them. conforms policy_store/mod.rs:732-738, test policy_store/document_tests.rs:742 Same function as validation (AcceptedPolicy::from_bytes).
CONTRACT.wire.md#replacement-3.4 RevisionNotHigher, when the broker holds an accepted policy with the document's Id and a Revision that is not lower. conforms policy_store/mod.rs:740-746, v2/policy.rs:89-91, tests policy_store/document_tests.rs:760, v2/policy.rs:434
CONTRACT.wire.md#replacement-3.5 PersistenceFailed, when the broker failed to write the policy file. conforms policy_store/mod.rs:756-775, test policy_store/document_tests.rs:835 Carries no state, as #errors-1 has it.
CONTRACT.wire.md#replacement-3.6 ActivationFailed, when the broker wrote the file and does not hold the document as its accepted policy. conforms policy_store/mod.rs:776-797, tests policy_store/document_tests.rs:869, :888 What the file holds after the write decides, whatever failed after the publication.
CONTRACT.wire.md#replacement-3 (order) The outcomes apply in that order. conforms policy_store/mod.rs:720, :726, :732, :743, :748, :787, tests policy_store/document_tests.rs:693, :709, :742, :760
CONTRACT.wire.md#replacement-4 A file that changes after the token check and before the write makes the request StaleStoreToken, and the broker never writes over that change. conforms policy_store/mod.rs:756-772, policy_store/mod.rs:138-167, policy_store/windows.rs:2068-2083, policy_store/windows.rs:2277-2385, policy_store/windows.rs:3213-3222, policy_store/windows.rs:3278-3295, test policy_store/document_tests.rs:851 By reading for the real storage: no test writes a real file. UNTESTED: at policy_store/mod.rs:131-137, policy_store/windows.rs:2064-2067 and :3210-3212 state it.
CONTRACT.wire.md#replacement-5 A document with another Id may have any Revision, and so may any document while the broker holds no accepted policy. conforms v2/policy.rs:89-91, policy_store/mod.rs:742-743, test policy_store/document_tests.rs:780-831
CONTRACT.wire.md#replacement-6 On success the broker writes the UTF-8 encoding of Document as the whole file, unchanged, and holds it as its accepted policy before writing the response. conforms policy_store/mod.rs:748-753, policy_store/windows.rs:3331-3375, policy_store/mod.rs:787-805, test policy_store/document_tests.rs:780-831 The published file is compared byte for byte, and the snapshot is replaced before the outcome returns.
CONTRACT.wire.md#replacement-7 The replacement response's Management is Active, its Document equals the request's, and its StoreToken differs from ExpectedStoreToken. conforms policy_store/mod.rs:787-805, policy_store/mod.rs:816, test policy_store/document_tests.rs:822-829 The token is a fresh random UUID.
CONTRACT.wire.md#replacement-8 A package request evaluated after the response is evaluated against the new policy. conforms policy_store/mod.rs:805, policy_store/mod.rs:321-323 Store side: accepted_policy() reads the snapshot the replacement stored.
CONTRACT.wire.md#replacement-9 Of several replacements with the same ExpectedStoreToken, at most one succeeds, and no other writes the file. conforms policy_store/mod.rs:693, policy_store/mod.rs:799-805, test policy_store/document_tests.rs:904 The writer lock is held from the observation to the outcome.
CONTRACT.policy.md#json-2, #json-3 A document is at most 4 MiB, counting a byte order mark, and a reader ignores one leading byte order mark. conforms v2/policy.rs:27-29, policy_store/windows.rs:1748-1761, policy_store/windows.rs:1824-1830, test v2/policy.rs:252 The file is read whole and the reader applies the limit; the broker sets no other limit.
CONTRACT.policy.md#document-keys-2 The broker never changes a document it accepts. conforms policy_store/mod.rs:748-753, policy_store/windows.rs:3331-3375, v2/policy.rs:43-48, test policy_store/document_tests.rs:780-831
CONTRACT.policy.md#format-version-4 A reader refuses a format version it does not implement rather than reading the document partially. conforms v2/policy.rs:27-29, policy_store/document.rs:128-135 No accepted policy comes from a file the 2.x reader refuses, other than through a conversion.
CONTRACT.policy.md#validity-window-3 Outside the validity window every request is denied, and the broker does not fall back to another policy. conforms v2/policy.rs:75-84, policy_store/mod.rs:211, test v2/policy.rs:322 The store holds no second policy to fall back to.
CONTRACT.policy.md#errors-1 The table is the closed set of errors a reader reports for a document. conforms v2/policy.rs:133-153, v2/policy.rs:34-39, policy_store/document.rs:59-64 The broker adds no code; the two places where it writes MalformedDocument itself are stated unreachable, the second under an UNTESTED: comment.
CONTRACT.policy.md#errors-3 Every validation error carries its code and a JSON Pointer to where it is. conforms v2/policy.rs:149-152, test v2/policy.rs:271 Pointers pass through from the reader unchanged.
CONTRACT.policy.md#request-input-2, #request-input-3 A package request that is not well-formed gets no decision; an evaluator given one reports it and returns no decision. conforms v2/policy.rs:83, v2/policy.rs:95-101, test v2/policy.rs:342
CONTRACT.policy.md#def-decision-2 Evaluating a well-formed request against a valid policy at an evaluation time always gives a decision. conforms v2/policy.rs:75-84, test v2/policy.rs:304 For every time a timestamp of the contract expresses. See Not covered for TimeOutOfRange.
CONTRACT.policy.md#evaluation-2 A decision depends only on the policy, the request and the evaluation time. conforms v2/policy.rs:75-84 evaluate passes exactly those three to the evaluator.
CONTRACT.policy.md#evaluation-3a, #evaluation-3b The broker accepts every valid document, refuses every invalid document whose PolicyFormatVersion is "2.0.0", and reports its errors. conforms v2/policy.rs:27-49, policy_store/windows.rs:1824-1830, policy_store/document.rs:124-128, policy_store/mod.rs:732-738, tests policy_store/document_tests.rs:400, :417 The identity is read a second time with another JSON reader (v2/policy.rs:156-164); a valid document has no construct the two read differently, and a disagreement would refuse the document, not accept it.
INTENT.md#ownership-1, #ownership-2 The policy specification owns the meaning of a policy and the broker implements it, through a Rust crate the broker depends on. conforms v2/policy.rs:28, v2/policy.rs:83, crates/now-package-broker/Cargo.toml:33 The broker has no reading or evaluation rule of its own for 2.x.
INTENT.md#ownership-9 A decision depends only on the policy and the request, never on what a given broker version does with the options. conforms v2/policy.rs:75-84
INTENT.md#fail-closed-1 Outside the validity dates, every request is denied, with no fallback. conforms v2/policy.rs:75-84, test v2/policy.rs:322
INTENT.md#broker-limits-1 The broker never runs pre/post commands for a request that runs elevated, whatever the policy grants and even in audit mode. conforms v2/policy.rs:127-131, tests v2/policy.rs:363, :378 The rule only; what enforces it is not in this change (#broker-limits-2 above).
INTENT.wire.md#replacement-1 A policy that replaces the active policy with the same Id has a higher Revision, so that a stale push cannot silently undo a newer policy. conforms v2/policy.rs:89-91, policy_store/mod.rs:742-746, test policy_store/document_tests.rs:760 Holds whenever the 2.0 side holds an accepted policy; a 1.x file is the exception described under INTENT.legacy.md#format-1-1.
INTENT.wire.md#replacement-2 A policy with another Id may replace it. conforms v2/policy.rs:89-91, test policy_store/document_tests.rs:788-793
INTENT.legacy.md preamble 1 The current policy format and its code paths stay clean, and nothing in them exists only to keep an earlier format working. conforms (judgement) v2/policy.rs:1-164, policy_store/format_1.rs:1-117 The 2.x reader and evaluator hold nothing for 1.x. The store, which both encodings share, holds 1.x-only code outside format_1.rs: policy_store/document.rs:36, :129-135, :198-221, policy_store/mod.rs:819-834, policy_store/windows.rs:1838-1845, :1923-1927.
INTENT.legacy.md preamble 2 Each earlier format is read in its own code path, apart from the current reader and evaluator, so that removing it deletes that code without changing them. conforms v2/policy.rs:27-49, policy_store/document.rs:129-135, policy_store/format_1.rs:25-32 Removing 1.x changes the store, not v2/policy.rs or the evaluator.
INTENT.legacy.md#format-1-2 Refusing is acceptable even though a refused policy leaves the broker refusing every request until the policy is replaced. conforms policy_store/document.rs:134, test policy_store/document_tests.rs:801-806 The 2.0 side holds no accepted policy for a refused file, and a replacement over a 1.x file succeeds.
INTENT.legacy.md#format-1-3, #format-1-4 A 1.x policy that uses a pre-release setting is refused; a priority ordering converts only when exact analysis allows it, otherwise the policy is refused. conforms policy_store/format_1.rs:30-32 Trivially: every 1.x policy is refused.

Outside this change

Listed once, not audited statement by statement.

  • CONTRACT.wire.md: #general, #values, #json, #transport, #endpoints, #authentication, the codes, statuses and order of #errors, #health, #capabilities, #package-request, #opt-ins, #equal-requests, #decision, #evaluate, #execute, #bypass, #bypass-reading, #operations, #status, #cancel, #event-channel, #frames, #frame-order, #management (the response), the request tables of #validation and #replacement, #validation-3 (body limit), #replacement-10 (what a client does).
  • CONTRACT.policy.md: everything the reader and the evaluator decide by themselves, which the pinned now-policy-eval implements and this change only calls: #text-comparison, #patterns, the rest of #json, #timestamps, #document-keys-1, #format-version-1 to -3, #metadata, #validity-window-1 and -2, #trusted-sources to #opt-ins, #errors-2, #errors-4, #errors-5, #reading to #missing-version, #applicability to #decision, #properties to #risk-guidance, and #evaluation-4.
  • INTENT.md: #vocabulary to #bypass-token, the rest of #ownership, #rules, #trusted-sources, #decisions, the rest of #fail-closed, #broker-limits-3, #remote-operations, #warnings.
  • INTENT.wire.md: #opt-ins, #targeting, #fields, #bypass, #encodings.
  • INTENT.legacy.md: #format-1-5 to -7 (editors, deprecation, authoring), #wire-1.

Known items, one line each

  1. A 1.x policy file is not converted: partial against INTENT.legacy.md#format-1-1, as the first row has it; CONTRACT.wire.md#management-state itself conforms (Invalid / Unconvertible).
  2. Two mismatches in the published now_policy_api::v2 types left as ignored tests: outside this change; no ignored test exists under v2/policy.rs or policy_store/** at e6093c06c.
  3. An Update under Scoop or Windows PowerShell ends Failed without a step: outside this change (CONTRACT.wire.md#operations, CONTRACT.policy.md#managers).
  4. The bypass endpoint: outside this change (CONTRACT.wire.md#bypass).

Partials

INTENT.legacy.md#format-1-1: no 1.x policy converts.
Required: convert a 1.x policy in memory when it converts exactly, refuse the others, report the refusal.
The code refuses every 1.x policy (policy_store/format_1.rs:30-32) and reports each refusal (policy_store/document.rs:199-205), so the refusing and reporting half is met and the converting half is not.
One consequence is not in the known report: while a 1.x file is in place the 2.0 side holds no accepted policy, so a replacement with the same Id and a lower Revision succeeds (policy_store/mod.rs:742-743, case "file in an earlier format" at policy_store/document_tests.rs:801-806).
CONTRACT.wire.md#replacement-5 allows it, and INTENT.wire.md#replacement-1 reads against it if the 1.x policy counts as the active policy.
Smallest fix: define the conversion behind format_1::convert, which the store already reads as a 2.x document; the revision rule then follows, because a converted policy carries its Id and Revision.

CONTRACT.wire.md#management-state-2a: Invalid where no file exists.
Required: Missing when no file exists at Path, Invalid only otherwise.
The code reports Invalid without knowing whether a file exists in three cases:

  • a path whose form or extension the broker does not read is Invalid / UnsupportedPath before anything is looked up (policy_store/windows.rs:1323-1352, test policy_store/windows.rs:5036);
  • a failure while checking the directory is Invalid / ReadFailed or InsecureStorage before the file lookup (policy_store/windows.rs:1391-1415, lookup at :1524-1558);
  • a file that cannot be watched is Invalid / MonitoringUnavailable whatever the storage last saw (policy_store/mod.rs:396-421).

The InvalidReason table describes each of these by the path or the watch, not by a file, which supports the code's reading; the sentence in #management-state-2 does not.
A directory that cannot be verified at all does report Missing for an absent file (policy_store/windows.rs:1938-1958), so the code is not uniform either.
Smallest fix: one sentence in the contract saying which of Missing and an InvalidReason of the path, location or watch wins; or, in the code, a lookup of the file before each of the three returns.

Cannot verify

CONTRACT.wire.md#management-state-8: ReadFailed for a file under an unverified directory.
observe_leaf_under_unverifiable_directory reports an existing file as unreadable (policy_store/windows.rs:1959-1965), which becomes ReadFailed (policy_store/document.rs:48), while its own comment gives the cause as a directory that "could not be trusted" and the same state has the ReadOnlyReason UnsafePath.
InsecureStorage is the row that names a location failing the security checks.
It conforms if ReadFailed also applies to a file the broker declines to open, and is a wrong reason otherwise; the contract does not say.
Fix if wanted: give that return the document reason InsecureStorage, as policy_store/windows.rs:1348-1351 already overrides the reason of an unsupported path.

Not yet realised

AcceptedPolicy::evaluate, validate, is_not_runnable, PolicyStore::policy_management and PolicyStore::replace_document have no production caller at e6093c06c; replace_document says so itself (policy_store/mod.rs:677-680), and accepted_policy is read only to audit an external change (policy_store/mod.rs:864).
The five rows above wait for the endpoints, and so does every wire-level statement of #management, #validation and #replacement, whose store side is what this audit covers.

Not covered by intent

Behaviour of the audited code that no governing statement decides.

  • A policy path that is empty or longer than 32767 UTF-16 code units: policy_management() and replace_document() return a ValueError and no state (policy_store/mod.rs:294, :329-331, :689, test policy_store/document_tests.rs:1119). Path is the configured path through Path::display(), so a path that is not valid Unicode is reported with replacement characters.
  • A replacement of a file the broker does not watch is decided against the published state without reading the file (policy_store/mod.rs:696-710): a client that sends the published token gets PolicyNotWritable even if the file changed on disk. Nothing is written either way.
  • While the watcher is starting, a state can be Active with the ReadOnlyReason MonitoringUnavailable (policy_store/mod.rs:299, policy_store/document.rs:160-164), although the table words that reason as "cannot watch".
  • A file or directory the broker cannot read has the ReadOnlyReason UnsafePath (for example policy_store/windows.rs:1391-1397); the closed set has no value for "cannot read".
  • An evaluation time outside the years 0001 to 9999 gives EvaluationError::TimeOutOfRange and no decision (v2/policy.rs:78-81, test v2/policy.rs:353). How the evaluation time of a leap second converts was not checked.
  • The 1.x endpoints never write a file the 2.x reader tells is in format 2.x, and may write one that is malformed or too large (policy_store/format_1.rs:37-75, tests policy_store/document_tests.rs:922, :1039). INTENT.legacy.md does not say what the earlier endpoints may do to a 2.x file.
  • Two mappings exist for storage reasons the storage never gives, each under an UNTESTED: comment that says the contract has no value for it (policy_store/document.rs:55-64, :80-84).
  • The audit records of a replacement (audit.rs, policy_store/mod.rs:705-814).

Other notes

  • UNTESTED: comments on audited statements: policy_store/mod.rs:131-137, policy_store/windows.rs:2064-2067, policy_store/windows.rs:3210-3212 (the real-storage write behind #replacement-4 and #replacement-6), policy_store/document.rs:55-58 and :80-83. Every replacement test runs over a storage double (policy_store/document_tests.rs:35-198).
  • Repository rule, not a contract statement: the TEMPORARY: comments at Cargo.toml:36-37 and crates/now-package-broker/Cargo.toml:31-32 say the git dependencies must go "before this change merges", which names a work stage in committed text, and the dependencies they mark are still git revisions at e6093c06c.
  • The conformance of now-policy-eval to CONTRACT.policy.md was not audited; the broker's rows above assume the reader and evaluator at the pinned revision do what the contract says.

The four commits after e6093c06c on this branch change tests, documentation comments and one UNTESTED: comment only, so the line numbers above are those of e6093c06c.

Note

LLM-assisted content (no human feedback).

… a test decides

The arm that maps `ManagementDisabled` has a test, so its comment no longer marks it as untested and says instead why no client reads its value.
The fixture that adds an unknown key now fails when the text it replaces is not there exactly once, like the other fixtures that rewrite a document.
The rows that quote the policy contract cite the revision the quotes come from.

Issue: DGW-499
Changelog: ignore

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@CBenoit

Copy link
Copy Markdown
Member Author

Evidence, deviations and open points

Lines changed against the base branch (approximate split): production +1230 −48, tests +1868, other +17 −6.

Checks run on d3cd6103d

  • cargo +nightly fmt --all -- --check: pass.
  • cargo clippy -p now-package-broker --all-targets -- -D warnings: pass. The workspace-wide cargo clippy --workspace --all-targets -- -D warnings passes on the top of the stack, which contains this branch.
  • cargo test -p now-package-broker: 874 passed, 2 ignored (both ignored tests exist on the base).
  • ci/check-oracle-markers.ps1: pass on 47fec3e51; the commit after it changes no marker.
  • Some policy-store tests return early in a process that is not elevated, so a run without elevation says nothing about them.

UNTESTED: properties added

  • crates/now-package-broker/src/policy_store/document.rs:55: the storage never fails with these reasons before it read the content, so no test decides what this arm reports, and the wire contract has no InvalidReason for it. A state from this arm would tell a client the file is an invalid document without having read it. When the storage starts failing with one of these reasons, give it its own arm and a contract value.
  • crates/now-package-broker/src/policy_store/document.rs:82: the storage never gives PathNotConfigured, because the broker always reads a path, so no test decides what this arm reports, and the wire contract has no ReadOnlyReason for a broker without a path. A state from this arm would name a reason that is not why the file cannot be written. When the storage starts giving this reason, decide its contract value before relying on this arm.
  • crates/now-package-broker/src/policy_store/mod.rs:131: a missing file is created without ever replacing one that appeared since, any other file is replaced through the handle its observation retained, and the returned observation is read after the write, with the configuration source of the store, which decides how the directory of the file is verified. No test runs it, because writing needs a directory secured for the broker, and the storage doubles ignore the source. When changing it, check the first three against how create and replace write a 1.x policy, and that the source reaches windows::observe unchanged.
  • crates/now-package-broker/src/policy_store/windows.rs:2065: this publication goes through the same transaction as atomic_replace, so a file that changed since the observation is never written over and an interruption stays recoverable. No test runs it, because it needs a directory secured for the broker. When changing it, check that it still differs from atomic_replace only in how the published file is verified.
  • crates/now-package-broker/src/policy_store/windows.rs:3211: this publication never overwrites an existing destination, exactly as atomic_create. No test runs it, because it needs a directory secured for the broker. When changing it, check that it still differs from atomic_create only in how the published file is verified.

Every replacement test runs over a storage double; the real-storage write is covered only by the last three comments above.

Stopped or deferred

  • Conversion of a 1.x policy. No rule is defined, so policy_store::format_1::convert refuses every policy. It returns either a 2.x document with the differences it reports, or a refusal, and the store reads the returned document exactly as it reads a 2.x file, so a conversion only replaces that function.
  • Consequence of the above. With a 1.x file in place the 2.0 side holds no accepted policy, so a 2.x replacement with the Id of the 1.x policy is accepted whatever its Revision.
  • ReadFailed under a directory that fails the security check. The directory verification returns one untyped error for "could not read the security information" and for "the directory is not secure", so the 2.0 state reports ReadFailed for both, where InsecureStorage reads better for the second. Separating them needs a typed error from policy_security.rs, which this change does not touch.
  • Missing against Invalid. For a path of unsupported form or extension, a directory check that fails, and a file that cannot be watched, the state is Invalid with its reason without looking whether a file exists. CONTRACT.wire.md §management-state says Missing "when no file exists at Path"; the InvalidReason table describes these reasons by the path or the watch. The broker follows the table.
  • A 2.x file the 2.x reader finds malformed or too large is not told as 2.x, so a 1.x repair write can replace it. Every valid 2.x document and every invalid one whose errors are about values is protected.

Deviations from the conventions

  • The description starts with the list of temporary git dependencies and carries the scope table, so it holds more than a squash commit body would.
  • Changelog: ignore: nothing a user can observe changes until the /v2/ endpoints exist. Drop the footer if this should appear in the changelog on its own.
  • The TEMPORARY: comments on the git dependencies say they must go "before this change merges", which names a stage of the work in committed text; they are there to be removed with the dependencies.

Review and pruning

  • Review: the last review of the store's code approved it; a separate review of the test and documentation commits after it asked for one comment to change, which d3cd6103d does. Left from it, not blocking: the case tables are for loops, as the crate's older tables are, because the crate has no rstest dev-dependency; and the documentation on ConfiguredPathError in policy_store/windows.rs, which predates this change, says the path form check is never applied to the default path, while observe applies it to every source.

  • Dependencies: the only additions are the git dependencies listed in the description. now-policy-eval brings no dependency of its own; the lockfile goes from 933 to 934 packages.

  • Tests: 47 tests in scope, none removed or merged. Mutants came from three sources: 22 written from the contract without sight of the tests, 22 written after reading them, and cargo-mutants on format_1.rs, document.rs and v2/policy.rs (52 mutants: 34 caught, 3 missed, 15 unviable). Three added tests and new rows in four tables kill every survivor but three, which are left: nothing states which failure of a 2.0 replacement is recorded as stale_conflict in the audit; one mutant is equivalent (the message of an error the code discards); one changes only the text of EvaluationError. cargo-mutants was not run on policy_store/mod.rs, audit.rs and policy_store/windows.rs, which only the hand-written mutants cover.

Note

LLM-assisted content (no human feedback).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A race in policy_management() can return a stale active document and token alongside MonitoringUnavailable, violating the contract’s requirement for a consistent management state.

7 open issues
What changed in this PR

Extends the Windows Agent package broker’s shared policy store to hold and evaluate 2.x policies while preserving the existing 1.x endpoints.
The 2.0 endpoints themselves will follow separately.

Changes:

  • Adds 2.x policy parsing, evaluation, identity checks, and elevated-command restrictions.
  • Adds version-specific store views, ordered replacement checks, exact-text persistence, recovery, and audit outcomes.
  • Adds policy/store tests and temporary pinned policy-library dependencies.
File Description
crates/​now-package-broker/​src/​v2/​wire/​mod.rs Adds a placeholder for 2.0 wire types.
crates/​now-package-broker/​src/​v2/​policy.rs Implements accepted policies, evaluation, and request restrictions.
crates/​now-package-broker/​src/​v2/​mod.rs Declares the versioned broker modules.
crates/​now-package-broker/​src/​v2/​execution.rs Adds a placeholder for 2.0 execution support.
crates/​now-package-broker/​src/​policy_store/​windows.rs Adds dual-format observation, document persistence, and recovery support.
crates/​now-package-broker/​src/​policy_store/​mod.rs Publishes 2.0 management state and handles document replacement.
crates/​now-package-broker/​src/​policy_store/​format_1.rs Isolates legacy conversion refusal and 2.x format detection.
crates/​now-package-broker/​src/​policy_store/​document.rs Maps observations into 2.0 policy management views.
crates/​now-package-broker/​src/​policy_store/​document_tests.rs Tests management state, replacement outcomes, compatibility, and auditing.
crates/​now-package-broker/​src/​lib.rs Exposes the Windows-only v2 module.
crates/​now-package-broker/​src/​audit.rs Adds document-replacement audit outcome helpers.
crates/​now-package-broker/​Cargo.toml Adds the temporary pinned evaluator dependency.
Cargo.toml Temporarily patches policy libraries to the required revision.
Cargo.lock Records the evaluator and patched policy dependencies.

Review effort: Max (adaptive)

snapshot.document.management(
path,
store_token,
self.watched.load(std::sync::atomic::Ordering::Acquire),
Comment on lines +190 to +191
/// The request a policy replacement of the 2.0 encoding arrives as.
const DOCUMENT_INTENT: &str = "POST /v2/policy";
let management_of = |snapshot: &Snapshot| self.management_of(snapshot, document_path.clone());
let audit = begin_audit();

let monitoring = self.writer.lock().await;
Comment on lines +1882 to +1885
return invalid_with(
validation::DiskFailureReason::MalformedContent,
hosting_dir,
unread_by_format_1,
canonical_path: path.to_owned(),
hosting_dir: None,
retained_target: None,
document: DocumentObservation::of_storage_failure(reason, read_only_reason),
Comment on lines +334 to +335
// The store mints every store token as a hyphenated UUID: 36 ASCII hexadecimal digits and hyphens, the first
// of them a digit.
Comment on lines +6 to +8
pub mod execution;
pub mod policy;
pub mod wire;

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

V2 policy replacements still log attempts and cancellations as v1 requests while outcomes identify v2, producing inconsistent audit trails that cannot be correlated by intent.

7 open issues
5 previously missed issues

In code that hasn't changed since last review

Medium severity V2 audit attempts and cancellations use the v1 intent

crates/​now-package-broker/​src/​audit.rs:687

Every v2 replacement currently starts with a PUT /v1/policy audit attempt because WriteAudit::begin_with_recorder hard-codes the v1 intent; the cancellation fallback does too. These outcome helpers instead use POST /v2/policy, so one request produces inconsistent audit records. Add a v2-aware initializer, retain its intent in WriteAuditState, and use it for attempts, outcomes, and cancellation.

Medium severity Publish refreshed observations before revision-conflict exits

crates/​now-package-broker/​src/​policy_store/​mod.rs:746

If an atomicity probe recovers, the fresh observation can become writable without changing its fingerprint or store token. A same-revision replacement then returns RevisionNotHigher with the old ReadOnlyReason, and the cached management state stays stale. Publish the observation before non-writing validation/revision exits and build the revision-conflict response from the refreshed snapshot.

Medium severity Report directory security failures as InsecureStorage

crates/​now-package-broker/​src/​policy_store/​windows.rs:2002

When directory security verification rejects an existing, readable policy's location, observe_leaf_under_unverifiable_directory labels it Unreadable, so this reports ReadFailed. The wire contract's “Policy management state” assigns failed storage-security checks to InsecureStorage, so clients receive the wrong diagnosis. Preserve the 1.x diagnostics but report Unusable(InsecureStorage) for the 2.0 view when the leaf metadata check succeeds, keeping actual metadata/read failures as ReadFailed.

Low severity Add required contract links to policy documentation

crates/​now-package-broker/​src/​policy_store/​mod.rs:228

These docs and policy_management at line 327 refer to the wire contract without linking it; the same issue occurs in v2/policy.rs:26,99 for errors and timestamps. STYLE.md:312-316 requires reference-style links and verbatim section titles, as demonstrated in server/mod.rs:58-60. Link “Policy replacement” and “Policy management state” in the pinned CONTRACT.wire.md, and “Validation errors” and “Timestamps” in the pinned CONTRACT.policy.md.

Low severity Factor duplicated transaction bodies into non-generic cores

crates/​now-package-broker/​src/​policy_store/​windows.rs:2285

conditional_replace now generates a separate copy of the roughly 100-line transaction body for each callback/result type; create_verified has the same pattern. STYLE.md (“Avoid monomorphization”) calls for a thin generic wrapper over a non-generic core. Keep the typed return value in the wrapper and use a dyn callback for the shared transaction body.

Review effort: Max (adaptive)

…est without a source

The API types at the new revision read a closed-set value from a string only, refuse a timestamp of a leap second, and check the members of a status response, a policy identity and a policy error.
The evaluator at its new revision takes a request whose source is optional, which a bypass request may leave out, and reads such a request the way the broker executes it.

The two pins differ because the two crates are changed on separate branches of the repository; the evaluator has no dependency of its own, so no type exists twice.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Copilot AI adaptive review requested due to automatic review settings December 1, 2026 18:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

V2 policy replacements log attempts and cancellation fallbacks as v1 requests, producing inconsistent audit trails and breaking endpoint-based correlation of attempts with outcomes.

7 open issues
3 previously missed issues

In code that hasn't changed since last review

Medium severity Keep the replacement intent consistent across audit events

crates/​now-package-broker/​src/​audit.rs:687

WriteAudit::begin_with_recorder and its drop fallback still use PUT /v1/policy, while these document outcomes use POST /v2/policy. A document replacement therefore has mismatched attempt/outcome records, and cancellation is attributed to v1. Add a v2-aware constructor, retain its intent in WriteAuditState for all events, and assert the intent in the document audit tests.

Medium severity Report insecure storage instead of unreadable files

crates/​now-package-broker/​src/​policy_store/​document.rs:48

When a custom policy directory fails a security check (for example, because it is user-writable), observe_leaf_under_unverifiable_directory reports an existing file as DiskFailureReason::Unreadable. This exposes ReadFailed even though the broker refused to read an insecure location, which CONTRACT.wire.md §management-state classifies as InsecureStorage. Preserve the security-check failure in the 2.0 observation while keeping the existing 1.x diagnostic if needed.

Medium severity Test overlapping replacements with forced lock contention

crates/​now-package-broker/​src/​policy_store/​document_tests.rs:1036

This does not exercise overlapping replacements. tokio::join! polls both futures on the same task, and replace_document has no suspension point after acquiring an uncontended writer lock, so the first replacement finishes before the second is polled. The assertions would therefore still pass without request serialization. Please use separately spawned tasks on a multithreaded runtime with a gated storage double to force contention, then verify that the second request becomes stale without publishing.

Review effort: Max (adaptive)

…icy-7702d2

Both sides patch `now-policy`, `now-policy-api` and
`now-policy-server-template` to a now-libraries revision. The patches
stay at the revision that carries the 2.0 package broker API types,
which also carries `ErrorCode::BrokerBusy`.

Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Copilot AI adaptive review requested due to automatic review settings December 2, 2026 09:25
@CBenoit

Copy link
Copy Markdown
Member Author

Merged master up the stack (merge commits only); head 89e0bd494.

master patches now-policy, now-policy-api and now-policy-server-template to the now-libraries revision that adds ErrorCode::BrokerBusy. The patches stay at 5b23b1a616ca97590d4d6b9e610cd07772d30453, which carries the 2.0 API types and that error code, so the list at the top of the description is unchanged.

Note

LLM-assisted content (no human feedback).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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


let document_path = self.document_path.clone()?;
let management_of = |snapshot: &Snapshot| self.management_of(snapshot, document_path.clone());
let audit = begin_audit();
Comment on lines +732 to +746
let candidate = match AcceptedPolicy::from_bytes(document.as_bytes()) {
Ok(candidate) => candidate,
Err(errors) => {
audit.document_failed_at(&path, write, FailureReason::InvalidPolicy);
return Ok(DocumentReplacement::InvalidPolicy(errors));
}
};

// The observation has the fingerprint of the published state, so the policy that state holds is the one the
// file holds now.
let held = previous.document.accepted();
if held.is_some_and(|held| held.refuses_revision_of(&candidate)) {
audit.document_failed_at(&path, write, FailureReason::RevisionConflict);
return Ok(DocumentReplacement::RevisionNotHigher(management_of(&previous)));
}
Comment on lines +1035 to +1036
let (first_outcome, second_outcome) =
tokio::join!(replace(&store, &expected, &first), replace(&store, &expected, &second));
Comment on lines +3 to +4
//! The store reads a converted document exactly as it reads the text of a 2.x policy file, so a conversion only has to
//! produce that text.
}]
};
let document = core::str::from_utf8(bytes).map_err(|_| unreadable())?;
let (id, revision) = read_identity(document).ok_or_else(unreadable)?;

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants