Repository navigation
Ig 029 general profiles and particle beams - #41
Conversation
…ile which takes in an instance of a `MathExpression` to produce the density profile
…`fbpic.lpa_utils.bunch` module
…aser/beam, or a laser/beam on its own; defers requiring a simulation length for approval of existing PR
…ralDensityProfile` to `ExpressionDensityProfile`; simplify `SUBCLASS` identifiers
|
@claude , please review this PR. |
|
Claude finished @joshmann-inversionsemi's task in 3m 2s —— View job PR Review: General profiles & particle beams
Overall this is a well-structured, carefully-documented PR — the 🔴 1.
|
…reverted once a simulation length parameter is added
|
@claude , I have addressed each of your concerns. Please provide an updated review of this PR. |
|
Claude finished @joshmann-inversionsemi's task in 1m 15s —— View job Updated PR review: general profiles and particle beams
You fixed both blocking issues from my last review, so I no longer see anything blocking. The remaining items below are small cleanups. ✅ #1
|
| Attack | Result |
|---|---|
Access a non-allowlisted attribute of np, numpy, sp, scipy, mt or math (np.load, np.ndarray.tofile, np.ctypeslib…, scipy.io…) |
Rejected: the path isn't in _ALLOWED_SYMBOLS, and the proxy doesn't have it anyway |
Reach attributes through call results or subscripts (np.array([z]).tofile, z[0].x) |
Rejected: _attribute_path returns None |
Use dunder attributes (z.__class__, np.sin.__class__) |
Rejected: the path isn't allowlisted |
Call anything that isn't an exact allowlisted path (z(...), a subscripted callable) |
Rejected by the _ALLOWED_CALLABLES membership check |
Lambda, comprehension, walrus, */** unpacking, strings, IfExp, BoolOp |
All rejected by the closed node allowlist |
Change the shared _ALLOWED_NAMESPACES |
Not possible: eval mode has no assignment, setattr isn't reachable, and __builtins__ is empty |
The ufunc and math sets contain only pure numerical functions, and the parametrised rejection test covers the vectors I reported before. Good fix.
✅ #2 laser-only and beam-only crash: resolved by deferring the feature
Raising whenever there is no density profile (075d5cc) means z_extent can no longer return (None, None) into setup_simulation or calculate_mean_group_beta. That's reasonable until #37 lands. Three follow-ups:
- Unreachable code. The
if not (self.lasers or self.beams): ...block ininversion_fbpic/lib/simulation.py:411-419now sits after an unconditionalraise, so it never runs. Either delete it and keep the TODO comment, or reorder it so it's clearly the code to restore after Ig 022 exploration campaign 1 #37. - Missing space in the error message. The two adjacent string literals at
simulation.py:404-405and:408-409join into"...simulation extent.Provide a density profile." - Stale PR description. It still lists "Permit laser-only and beam-only simulations" as a bullet. Update it so reviewers and the changelog don't assume the feature ships with this PR.
✅ Minor notes from last time: resolved
super().__attrs_post_init__()is now called in both beam subclasses. The base class has a no-op hook, and a test asserts the call order.- The validation-only
_density_function(...)call now has a comment. - The "index 7 is the particle charge" comment was added to the demo.
- I take the new
calculate_mean_group_beta()behaviour (max across lasers) as intentional, sincetest_mean_group_beta_uses_all_laser_wavelengthspins it.
🟡 New, low-severity (optional)
out=lets an expression overwrite its input arrays. All ufuncs are allowlisted and any identifier keyword is accepted. So an expression likenp.multiply(z, 0, out=z)passes validation and changes thez/rarrays FBPIC passes into the density function. That is a correctness hazard rather than a security one. Rejecting theoutkeyword in theast.keywordbranch of_validate_expression(math_expression.py:317) closes it cheaply.- Unbounded expressions can hang a run. Something like
9**9**9**9(Python big-int power) ornp.arange(1e12)validates and then hangs or runs out of memory when the profile is evaluated. A config author can already exhaust resources through grid sizes, so this isn't a new trust boundary. It's worth a sentence in theMathExpressiondocstring in case configs ever come from less-trusted sources. - Missing constants.
np.infandnp.nanaren't exposed. They come up in density cutoffs, for examplenp.where(z < z0, np.nan, …). Consider adding them next tonp.eandnp.piatmath_expression.py:174.
Bash(python -m pytest:*) to the action's --allowedTools would let me run the suite next time.
• IG-029-general-profiles-and-particle-beams
…u`) as a constant, add "nan" and "inf" (`np.nan`, `np.inf`)
|
Addressed each of Claude's concerns. Ready for human review! |
…es-and-particle-beams
|
Claude finished @joshmann-inversionsemi's task in 1m 21s —— View job Review of the #40 merge (
|
|
Now that #37 will be merged soon, I will want to take more time to merge those changes here; hold off on reviewing, all. |
|
#37 is now merged! |
…es-and-particle-beams
…h` is set and other elements are included
@claude , please review this updated PR. |
|
Claude finished @joshmann-inversionsemi's task in 1m 49s —— View job Review of the updated PR (#37 merge,
|
…y present Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fix: update verbiage for error messages regarding simulation elements
refactor: rename tests to more accurate reflect their target coverage
… git directory, and extend hint
|
Addressed each of your concerns, ready for final review from you @claude . |
|
Claude finished @joshmann-inversionsemi's task in 49s —— View job Final review of PR #41
You've addressed everything from my last review, and I found nothing blocking. From my side this is ready to merge. Two optional notes are at the end. ✅ Laser-only crash: fixed (
|
|
Updated the Ready for another review, @moellep ! |
moellep
left a comment
There was a problem hiding this comment.
Looks good - thanks for the changes!
Makes the additions to
inversion-fbpic:MathExpression(basic Python operators viaast, as well asnumpyandscipymethods)ExpressionDensityProfilewhich takes aMathExpressionas the density profile_ParticleBeamclass for PWFA (or other) simulations, coveringfbpc.lpa_utils.bunch; includes aExpressionParticleBeamclass which takes in aMathExpressionA couple notes:
For the purposes of permitting users to use general methods to describe density and beam profiles, a limited
MathExpressionwas preferred over an alternative native Python method + pickling scheme. Pickling would prevent the method from being readable or modifiable in its serialized form. It also brings potential security issues alla arbitrary code execution. I think we could and should extend support for this scheme, either with additional security measures or an insistent warning to the user, for the case that more complex logic is desired for the generation of these profiles. In the meantime, theMathExpressionscheme should cover most practical cases.A gap in the logic for determining a sufficient element configuration for a running simulation, where lacking a_DensityProfileis permitted, was left. In PR #37 , asimulation_lengthparameter was introduced. Once PR #37 is merged, this parameter is sufficient to replace the otherwise required_DensityProfileclass. Presently valid simulations will still be valid whether this PR or PR #37 is merged first.Simulations require at least one driver (a beam or a laser), but
_DensityProfiles are optional ifsimulation_lengthis set.