Skip to content

use an enum to define job streams#101

Open
emilyalbini wants to merge 1 commit intoea-mvulumtytlotfrom
ea-ptqwnqpswsuv
Open

use an enum to define job streams#101
emilyalbini wants to merge 1 commit intoea-mvulumtytlotfrom
ea-ptqwnqpswsuv

Conversation

@emilyalbini
Copy link
Copy Markdown
Member

Note: This PR is stacked on top of #100.


This PR refactors how we handle job stream names, replacing the string handling we do all over the place with a common JobStream enum. My goal for this is that the compiler should guide you through its errors when adding a new job stream, rather than having to search through the codebase for places where the stream should be handled.

I chose for now to not have the server error out when an unknown stream is received, but that should be an easy change if we want to go for it.

@emilyalbini emilyalbini requested a review from jclulow April 20, 2026 08:55
Comment thread agent/src/exec.rs
Comment on lines -66 to +70
"error",
JobStream::Error,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the only occurrence of error as a job stream, and nothing else in the codebase handled it. Should this be panic instead?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The lack of handling in other places is likely just because I've never actually seen it happen in practice. It definitely should not be panic, though; that's for, e.g., kernel panic stacks that occur while your job is running.

I believe we would just have printed with generic debug-ish formatting if it had showed up in a stream, at any rate.

Comment thread agent/src/main.rs
.worker_append()
.body(vec![WorkerAppend {
stream: "agent".into(),
stream: JobStream::Agent.to_string(),
Copy link
Copy Markdown
Member Author

@emilyalbini emilyalbini Apr 20, 2026

Choose a reason for hiding this comment

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

Found another stream we don't handle anywhere else: agent seems to be only used when spawning a job fails. Maybe we should use panic instead? Or task/diag.{name} depending on what failed to spawn?

Copy link
Copy Markdown
Collaborator

@jclulow jclulow left a comment

Choose a reason for hiding this comment

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

This PR refactors how we handle job stream names, replacing the string handling we do all over the place with a common JobStream enum. My goal for this is that the compiler should guide you through its errors when adding a new job stream, rather than having to search through the codebase for places where the stream should be handled.

I'm not sure I would like to go in this direction. I recognise that using the type system has a lot of benefits for some things, but keeping the stream identifiers as strings allows easier gentle evolution, and as I've been adding bits and pieces to the system (e.g., diagnostics, background tasks) the names have started to become more hierarchical, and I don't think the enum scheme here really captures that or allows for as easy an experimental evolution.

Comment thread agent/src/exec.rs
Comment on lines -66 to +70
"error",
JobStream::Error,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The lack of handling in other places is likely just because I've never actually seen it happen in practice. It definitely should not be panic, though; that's for, e.g., kernel panic stacks that occur while your job is running.

I believe we would just have printed with generic debug-ish formatting if it had showed up in a stream, at any rate.

@emilyalbini emilyalbini force-pushed the ea-ptqwnqpswsuv branch 2 times, most recently from c8f7e71 to 7f32072 Compare May 4, 2026 09:03
@emilyalbini
Copy link
Copy Markdown
Member Author

I recognise that using the type system has a lot of benefits for some things, but keeping the stream identifiers as strings allows easier gentle evolution, and as I've been adding bits and pieces to the system (e.g., diagnostics, background tasks) the names have started to become more hierarchical, and I don't think the enum scheme here really captures that or allows for as easy an experimental evolution.

Could you share a bit more how the enum would prevent experimentation? The server doesn't reject stream names that don't parse as the enum, and all the clients handle unknown streams gracefully. Would changing the layout of the type to better match the hierarchy be better?

enum JobStream {
    Bg {
        name: String,
        stream: BgStream,
    },
    Stdout,
    Stderr,
    ...
}

enum BgStream {
    General,
    Stdout,
    Stderr,
}

@emilyalbini emilyalbini force-pushed the ea-ptqwnqpswsuv branch 2 times, most recently from a03e262 to e470570 Compare May 4, 2026 09:43
Comment thread jobsh/src/lib.rs
Comment on lines -42 to -43
} else if s.starts_with("bg.") {
"s_bgtask".into()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is an example of something that's different between the string approach, and more rigid enum thing: it's easy and concise to say "paint bg.* regardless of what it is as s_bgtask" in this particular place where we care about that. To represent this subtlety in enums you would presumably have to be able to separate JobStream::Unknown from, say, JobStream::BgUnknown, or JobStream::Background with fields including the ability to preserve the unknown stream name, etc.

It feels like a lot of additional boilerplate in the Rust code to ultimately recreate what's really going on with the strings underneath.

Comment thread jobsh/src/lib.rs
Comment on lines -14 to -17
/*
* Classes for these streams are defined in the "variety/basic/www/style.css",
* which we send along with the generated HTML output.
*/
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this comment is important to preserve, as regardless of the Rust representation of event streams you'll still need to go and add things to the CSS separately.

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.

2 participants