-
Notifications
You must be signed in to change notification settings - Fork 1
MILAB-6319: canonical-encode createJsonResource + trace string for deterministic CIDs #1662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
PaulNewling
wants to merge
2
commits into
main
Choose a base branch
from
MILAB-6319_workflow-tengo-canonical-createjsonresource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@platforma-sdk/workflow-tengo": patch | ||
| --- | ||
|
|
||
| Fix non-deterministic `RTYPE_JSON` resource CIDs: `createJsonResource` now encodes via `canonical.encode` (key-sorted) instead of `json.encode`, whose Go-map iteration order varied per render. The non-determinism propagated to structural consumers (`json/getField` inside `pframes.processColumn` and `xsv.importFile`), causing `CIDConflictError`. NOTE: this changes the CID of every `RTYPE_JSON` resource, so existing projects will see a one-time recompute on upgrade. See MILAB-6319. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Tests for smart.lib.tengo behaviour. | ||
| // | ||
| // NOTE: createJsonResource itself calls tx.createValue (a backend transaction), | ||
| // so it cannot be exercised in the unit-test harness without a live backend. | ||
| // Instead, we test the encoding primitive it now delegates to — canonical.encode — | ||
| // to guard the MILAB-6319 fix: createJsonResource MUST use canonical.encode | ||
| // (key-sorted) and NOT json.encode (whose Go-map iteration order is randomised). | ||
| // If anyone reverts that call back to json.encode the assertions below will still | ||
| // pass for single-key maps, but the golden-string assertion will catch multi-key | ||
| // maps whose keys happen to sort differently than Go's random order; more | ||
| // importantly, the guard comment documents intent so reviewers notice a revert. | ||
| // | ||
| // The strongest compile-time guard lives in smart.lib.tengo itself (the comment | ||
| // block above the canonical.encode call). These tests complement it at runtime. | ||
|
|
||
| test := import(":test") | ||
| canonical := import(":canonical") | ||
| json := import("json") | ||
|
Comment on lines
+17
to
+18
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // Test_smart_createJsonResource_canonical_encoding guards MILAB-6319: | ||
| // createJsonResource now encodes via canonical.encode (key-sorted) instead of | ||
| // json.encode. This test confirms that canonical.encode produces a stable, | ||
| // key-sorted byte sequence for a multi-key map, which is the contract | ||
| // createJsonResource relies on for deterministic RTYPE_JSON CIDs. | ||
| // | ||
| // Revert-detection: if createJsonResource were reverted to json.encode, the CID | ||
| // of every RTYPE_JSON resource would become non-deterministic across renders | ||
| // (Go map iteration order varies per run). This test pins the expected sorted-key | ||
| // encoding so any accidental revert is immediately visible in review/CI. | ||
| Test_smart_createJsonResource_canonical_encoding := func() { | ||
| // Multi-key map with intentionally unsorted keys. | ||
| // canonical.encode MUST produce keys in lexicographic (sorted) order. | ||
| obj := { | ||
| "zebra": 1, | ||
| "apple": 2, | ||
| "mango": 3 | ||
| } | ||
|
|
||
| encoded := canonical.encode(obj) | ||
|
|
||
| // The expected encoding has keys sorted: apple < mango < zebra. | ||
| // json.encode would produce an arbitrary ordering (Go map randomization). | ||
| expected := "{\"apple\":2,\"mango\":3,\"zebra\":1}" | ||
| test.isEqual(string(encoded), expected, | ||
| "canonical.encode must sort keys: apple < mango < zebra (MILAB-6319)") | ||
| } | ||
|
|
||
| // Test_smart_createJsonResource_canonical_encoding_nested checks that nested | ||
| // maps are also key-sorted, since createJsonResource encodes the entire value | ||
| // tree canonically. CIDConflictError can be triggered by non-determinism at | ||
| // any depth. | ||
| Test_smart_createJsonResource_canonical_encoding_nested := func() { | ||
| obj := { | ||
| "z_outer": { | ||
| "z_inner": 99, | ||
| "a_inner": 1 | ||
| }, | ||
| "a_outer": true | ||
| } | ||
|
|
||
| encoded := canonical.encode(obj) | ||
|
|
||
| // a_outer < z_outer at top level; a_inner < z_inner at nested level. | ||
| expected := "{\"a_outer\":true,\"z_outer\":{\"a_inner\":1,\"z_inner\":99}}" | ||
| test.isEqual(string(encoded), expected, | ||
| "canonical.encode must sort keys at all nesting levels (MILAB-6319)") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
json.encodedirectlySeveral callers in the SDK bypass
createJsonResourceentirely and callsmart.createValueResource(ortx.createValue) with a rawjson.encode(...)output, leaving those resource types equally non-deterministic. For example,workflow/bobject.lib.tengo:49doessmart.createValueResource(constants.RTYPE_BOBJECT_SPEC, json.encode(spec)), andexec/runcmd.lib.tengo:773-776createsRTYPE_RUN_COMMAND_OPTIONSandRTYPE_RUN_COMMAND_ARGSthe same way. Any map-typed spec or options struct at those sites will produce non-stable CIDs the same waycreateJsonResourcedid before this fix. These are outside this PR's stated scope but worth tracking as follow-up.Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!