-
Notifications
You must be signed in to change notification settings - Fork 0
feat!: add validation for AppendSessionConfig
#225
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
Conversation
279783a to
9558e03
Compare
|
@greptileai review this draft |
Greptile SummaryAdded validation to Key changes:
Note: As discussed in the previous thread, validation can still be bypassed by directly mutating the public fields after construction, but this is an intentional design choice by the maintainers. Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant User
participant AppendSessionConfig
participant ValidationError
participant AppendSession
User->>AppendSessionConfig: new()
AppendSessionConfig-->>User: config (with defaults)
User->>AppendSessionConfig: with_max_inflight_bytes(bytes)
alt bytes < 1 MiB
AppendSessionConfig->>ValidationError: create error
ValidationError-->>User: Err(ValidationError)
else bytes >= 1 MiB
AppendSessionConfig-->>User: Ok(config)
end
User->>AppendSessionConfig: with_max_inflight_batches(batches)
alt batches < 1
AppendSessionConfig->>ValidationError: create error
ValidationError-->>User: Err(ValidationError)
else batches >= 1
AppendSessionConfig-->>User: Ok(config)
end
User->>AppendSession: new(client, stream, config)
AppendSession-->>User: session
|
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.
1 file reviewed, 1 comment
| pub struct AppendSessionConfig { | ||
| /// Limit on total metered bytes of unacknowledged [AppendInput]s held in memory. | ||
| /// | ||
| /// **Note:** It must be at least `1MiB`. | ||
| /// | ||
| /// Defaults to `10MiB`. | ||
| pub max_inflight_bytes: u32, | ||
| /// Limit on number of unacknowledged [AppendInput]s held in memory. | ||
| /// | ||
| /// Defaults to `None`. | ||
| /// **Note:** It must be at least `1`. | ||
| /// | ||
| /// Defaults to no limit. | ||
| pub max_inflight_batches: Option<u32>, | ||
| } |
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.
logic: validation can be bypassed by directly mutating public fields
the fields max_inflight_bytes and max_inflight_batches are public, allowing users to bypass validation after construction:
let mut config = AppendSessionConfig::new();
config.max_inflight_bytes = 100; // bypasses 1 MiB minimum!
config.max_inflight_batches = Some(0); // bypasses minimum of 1!consider making these fields private and adding getter methods, or add validation at the point of use in AppendSession::new
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/session/append.rs
Line: 90:103
Comment:
**logic:** validation can be bypassed by directly mutating public fields
the fields `max_inflight_bytes` and `max_inflight_batches` are public, allowing users to bypass validation after construction:
```rust
let mut config = AppendSessionConfig::new();
config.max_inflight_bytes = 100; // bypasses 1 MiB minimum!
config.max_inflight_batches = Some(0); // bypasses minimum of 1!
```
consider making these fields private and adding getter methods, or add validation at the point of use in `AppendSession::new`
How can I resolve this? If you propose a fix, please make it concise.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.
yeah i'm aware of the possibility of this misuse, not just for this type but for few other types. the choice i made was to just prevent direct struct inits, and let constructors and builders be the only way to construct instances. private fields and providing accessors for em is something that i'm not leaning towards at the moment.
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.
looks like readonly crate might fit the bill. will check that out.
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.
one big downside w/ readonly crate is that the readonly fields are not getting shown in IDE's autocomplete options.
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.
That's a valid concern with the readonly crate - losing IDE autocomplete for read-only fields definitely hurts developer experience.
A few alternative approaches you could consider:
-
Runtime validation in
AppendSession::new()- Keep the fields public but add validation where the config is actually used, as you mentioned in your original comment. This preserves the current API while catching invalid values at the point of use. -
Builder pattern with private fields - Make fields private and provide a builder that validates on build, but this changes the API more significantly.
-
Custom
readonlyimplementation - Create your own read-only wrapper that's more IDE-friendly, though that's probably overkill for this use case.
Given your preference to avoid private fields + accessors and the IDE issue with readonly, the runtime validation approach in AppendSession::new() might be the most practical solution. It catches the misuse where it actually matters (when the session is created) while preserving the current ergonomics.
No description provided.