Skip to content

Added the net package operations to the --simulate-json change set - #41

Open
djbclark wants to merge 6 commits into
simulate-json-filesfrom
simulate-json-packages
Open

djbclark wants to merge 6 commits into
simulate-json-filesfrom
simulate-json-packages

Conversation

@djbclark

@djbclark djbclark commented Sep 30, 2026 •

Copy link
Copy Markdown
Owner

Fork-internal PR for CI; not an upstream submission. PR 2 of the three-PR plan on #3, stacked on PR 1 (#40, base simulate-json-files); the first four commits are #39, #44, #45 and #40 as pushed. Two commits of its own: a refactor that makes the package records store the package fields instead of a formatted message, so the existing install/remove reduction can feed more than one renderer, and the feature, which adds a packages array to the --simulate-json document. Each entry has operation (install or remove), name, and version and architecture when the policy named them. The array always carries the install/remove reduction, under --simulate=manifest as well. Unit tests: 21, all passing; the acceptance test records a real package operation through a fake package module shipped beside the policy. Adversary security review: three Low findings, none blocking; the one in this PR's own test fixture (an unquoted command substitution in simulate_json.cf.module) is fixed, and the two in pre-existing upstream code (the CSV op code is checked only by asserts; the name-_-arch map key is not injective) are recorded for a fork follow-up, not changed here.


ELI5 — auditable without C

You know shell and a little Python. Two facts.

What the code is for. cf-agent --simulate doesn't install or remove packages. It writes one CSV line per package action it would take, op,name,version,architecture, and at the end reduces that log to the net result: a package installed and then removed in the same run is no change, a newer version replaces an older one, and so on. Until now that net result was only printed as prose. --simulate-json=<file> (PR 1) writes the file changes as JSON; this PR adds the packages to the same document.

Fact 1: the refactor changes no output. Before, each surviving record stored the finished sentence (Package 'foo [1.0]' would be installed). Now it stores the three fields and the sentence is formatted when printed:

# before: record = {"msg": "Package 'foo [1.0]' would be installed", "version": "1.0"}
# after:  record = {"name": "foo", "arch": "", "version": "1.0"}
#         print(message(op, record))      # same sentence as before

The reduction rules are untouched; the five unit tests from #39, #44 and #45 that read the printed sentences still pass unchanged.

Fact 2: the JSON is the same reduction. The packages array is built from the same two maps the prose uses, then sorted by name and architecture so the order is stable:

"packages": [
  {"name": "simulate-json-pkg", "operation": "install"}
]

version and architecture are present only when known; empty strings are never written.

How you'd catch me lying, without reading C. The unit test feeds 15 small CSV logs through the JSON writer and checks each one's net result, including the corner cases: an install cancelled by a matching removal, a removal of another version that keeps the install, a second unversioned removal that keeps the versioned one, latest beating any version, and a malformed line being skipped. A second test checks the sort order over three records. The acceptance test 29_simulate_mode/simulate_json.cf runs a real --simulate=manifest with a one-package policy and a fake package module (simulate_json.cf.module) and diffs the whole JSON document, packages included, against simulate_json.cf.expected.

🤖 Generated with Claude Code

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approved. Cursor Bugbot was not present on this head after the initial check poll, so that signal was skipped; no applicable approval policy required human review. No reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

RecordPkgOperationInChroot() wrote the architecture before the version,
the reverse of the order its readers expect.

Ticket: none
Changelog: Title
Signed-off-by: Daniel JB Clark <djbclark@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@djbclark
djbclark force-pushed the simulate-json-files branch from d5cc2c4 to f8342de Compare October 9, 2026 03:09
djbclark and others added 4 commits October 8, 2026 23:57
A recorded removal with a version was only reported when it cancelled a
recorded installation of the same version.

Ticket: none
Changelog: Title
Signed-off-by: Daniel JB Clark <djbclark@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…talls

DiffPkgOperations() compared the versions of two recorded installations
of the same package without checking that both were set, which
PkgVersionIsGreater() asserts. The diff report now keeps the record the
way the manifest report does: a versioned record over an unversioned
one, otherwise the higher version, otherwise the first one recorded.

Ticket: none
Changelog: Title
Signed-off-by: Daniel JB Clark <djbclark@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
With --simulate-json=FILE, cf-agent also writes the files and renames of
a --simulate run to FILE as one JSON document, together with whether the
run fell back to the failsafe policy or was aborted. Under manifest-full
the JSON lists only the changed files, not the kept ones.

Ticket: none
Changelog: Title
Signed-off-by: Daniel JB Clark <djbclark@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
PkgOperationRecord now stores the package fields and the simulate modes
format their messages when printing, so that the reduction can be reused
by other renderers.

Ticket: none
Changelog: None
Signed-off-by: Daniel JB Clark <djbclark@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@djbclark
djbclark force-pushed the simulate-json-packages branch 2 times, most recently from 15d4380 to 312c466 Compare October 9, 2026 04:20
The "packages" array lists each package that a --simulate run would
install or remove, with its version and architecture when known. It
always carries that install/remove reduction, under --simulate=manifest
as well.

Ticket: none
Changelog: Title
Signed-off-by: Daniel JB Clark <djbclark@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant