Repository navigation
feat(activity): record authored source locations for calls and outputs #2535
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9a97103
feat(activity): record where authored source made each operation call…
lazybean 24be8e7
fix(activity): keep locations inside Saved Functions written without …
lazybean 6516b17
fix(activity): use a unique migration version for record locations
lazybean ec0ba8b
refactor(v8): preserve authored source when linking Saved Functions
lazybean 7067229
test(v8): preserve locations before inline default exports
lazybean 177db7d
fix(activity): order the record locations migration after Universal S…
lazybean e0f8894
test(run): build Dispatch Fence fixtures with the recorded location f…
lazybean c6927d1
fix(activity): order the record locations migration after the search …
lazybean 466112d
test(run): pass no authored location for scripted fan-out calls
lazybean 21a9793
refactor(run): end a terminated Run's fence loop through the Error co…
lazybean 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
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,179 @@ | ||
| //! Recorded authored source locations survive a server restart and reach Reef | ||
| //! through the server's own private gRPC listener. | ||
| #![allow( | ||
| clippy::unwrap_used, | ||
| reason = "fixture setup fails the restart test if it cannot complete" | ||
| )] | ||
|
|
||
| use std::{fs, sync::Arc}; | ||
|
|
||
| use coral_app::ServerBuilder; | ||
| use coral_grpc::coral::management::v1::{ | ||
| self as wire, activity_service_client::ActivityServiceClient, | ||
| activity_source_location::Document, | ||
| }; | ||
| use coral_workspace::{ | ||
| AuthenticatedPrincipal, ChangeContext, TraceId, WorkspaceDisplayName, WorkspaceModule, | ||
| local_organization_owner, | ||
| }; | ||
| use tempfile::TempDir; | ||
|
|
||
| const SAVED_FUNCTION: &str = | ||
| "export default function forecast(city: string) {\n console.info(city);\n}"; | ||
| const PROGRAM: &str = "console.log(\"l\");\nawait coral.emit({ step: 1 });\ncoral.functions.report.forecast(\"Oslo\");"; | ||
|
|
||
| /// Accepts one Run in the server's database and waits for it to succeed. | ||
| async fn record_run( | ||
| database: Arc<coral_db::Database>, | ||
| ) -> (coral_workspace::WorkspaceId, coral_run::RunId) { | ||
| let owner = local_organization_owner(); | ||
| let context = ChangeContext::new(owner.clone(), 1, TraceId::current_or_new()); | ||
| let workspaces = WorkspaceModule::new(database.clone()); | ||
| let workspace = workspaces | ||
| .create(&context, WorkspaceDisplayName::parse("Locations").unwrap()) | ||
| .await | ||
| .unwrap(); | ||
| let workspace = workspace.id().clone(); | ||
| let principal = AuthenticatedPrincipal::user(owner); | ||
| coral_functions::SavedFunctionModule::new( | ||
| database.clone(), | ||
| workspaces.clone(), | ||
| coral_run::IsolateLimits::default(), | ||
| ) | ||
| .unwrap() | ||
| .save( | ||
| &coral_functions::SavedFunctionChangeContext::new( | ||
| principal.clone(), | ||
| 1, | ||
| TraceId::current_or_new(), | ||
| ), | ||
| &workspace, | ||
| coral_functions::SavedFunctionSave::new( | ||
| coral_functions::SavedFunctionPath::parse("report.forecast").unwrap(), | ||
| coral_functions::SavedFunctionDescription::parse("Forecast one city").unwrap(), | ||
| coral_functions::SavedFunctionSource::parse(SAVED_FUNCTION).unwrap(), | ||
| ), | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
| let providers = coral_provider::ProviderModule::new( | ||
| coral_outbound::DestinationPolicy::default(), | ||
| database.clone(), | ||
| workspaces.clone(), | ||
| coral_catalog::BuiltinCatalog::running().unwrap(), | ||
| ); | ||
| let runs = coral_run::RunModule::new( | ||
| coral_outbound::DestinationPolicy::default(), | ||
| database, | ||
| workspaces, | ||
| providers, | ||
| coral_run::RuntimeLimits::default(), | ||
| ) | ||
| .unwrap(); | ||
| let run = runs | ||
| .start( | ||
| &principal, | ||
| &workspace, | ||
| coral_run::RunSubmission::new( | ||
| coral_run::ProgramSource::parse(PROGRAM).unwrap(), | ||
| coral_run::RunTimeout::from_millis(60_000).unwrap(), | ||
| ), | ||
| ) | ||
| .await | ||
| .unwrap() | ||
| .run_id() | ||
| .clone(); | ||
| let settled = runs | ||
| .observe( | ||
| &principal, | ||
| &workspace, | ||
| &run, | ||
| &coral_run::RunObservation::initial(std::time::Duration::from_secs(30)).unwrap(), | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
| assert_eq!(settled.state(), coral_run::RunState::Succeeded); | ||
| (workspace, run) | ||
| } | ||
|
|
||
| fn reef<T>(message: T) -> tonic::Request<T> { | ||
| let mut request = tonic::Request::new(message); | ||
| request | ||
| .metadata_mut() | ||
| .insert("coral-audience", "reef".parse().unwrap()); | ||
| request | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn recorded_locations_survive_a_restart_on_the_private_listener() { | ||
| let temp = TempDir::new().unwrap(); | ||
| fs::write( | ||
| temp.path().join("config.toml"), | ||
| "[listeners.web]\npublic_url = 'http://127.0.0.1:5173'\n", | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let first = ServerBuilder::new() | ||
| .with_config_dir(temp.path()) | ||
| .start() | ||
| .await | ||
| .unwrap(); | ||
| let (workspace, run) = record_run(first.database()).await; | ||
| first.shutdown().await.unwrap(); | ||
|
|
||
| let second = ServerBuilder::new() | ||
| .with_config_dir(temp.path()) | ||
| .start() | ||
| .await | ||
| .unwrap(); | ||
| let mut client = ActivityServiceClient::connect(second.endpoint_uri().to_owned()) | ||
| .await | ||
| .unwrap(); | ||
| let run_name = format!("workspaces/{workspace}/runs/{run}"); | ||
| let detail = client | ||
| .inspect_run(reef(wire::InspectRunRequest { | ||
| name: run_name, | ||
| page_size: 100, | ||
| ..Default::default() | ||
| })) | ||
| .await | ||
| .unwrap() | ||
| .into_inner(); | ||
| let [snapshot] = detail.linked_functions.as_slice() else { | ||
| panic!("one linked snapshot") | ||
| }; | ||
| let at = |document: Document, line, column| { | ||
| Some(wire::ActivitySourceLocation { | ||
| document: Some(document), | ||
| line, | ||
| column, | ||
| }) | ||
| }; | ||
| let program = |line, column| at(Document::Program(wire::ActivityEmpty {}), line, column); | ||
| let in_snapshot = |line, column| { | ||
| at( | ||
| Document::SavedFunction(snapshot.saved_function.clone()), | ||
| line, | ||
| column, | ||
| ) | ||
| }; | ||
| let outputs = detail | ||
| .events | ||
| .iter() | ||
| .filter(|event| { | ||
| matches!( | ||
| event.payload, | ||
| Some( | ||
| wire::activity_run_event::Payload::ProgramEmitted(_) | ||
| | wire::activity_run_event::Payload::ProgramLogged(_) | ||
| ) | ||
| ) | ||
| }) | ||
| .map(|event| event.source_location.clone()) | ||
| .collect::<Vec<_>>(); | ||
| assert_eq!( | ||
| outputs, | ||
| vec![program(1, 9), program(2, 13), in_snapshot(2, 13)] | ||
| ); | ||
| second.shutdown().await.unwrap(); | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
crates/coral-db/migrations/0037_record_source_locations.sql
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,39 @@ | ||
| -- Where accepted authored source made each Operation call, Emission and Log. | ||
| -- Earlier records keep NULL: their location is unavailable. A Saved Function | ||
| -- location names the Run's retained snapshot, never the current definition. | ||
| ALTER TABLE operation_invocations ADD COLUMN source_document TEXT; | ||
| ALTER TABLE operation_invocations ADD COLUMN source_saved_function_id TEXT; | ||
| ALTER TABLE operation_invocations ADD COLUMN source_line BIGINT; | ||
| ALTER TABLE operation_invocations ADD COLUMN source_column BIGINT | ||
| CONSTRAINT operation_invocations_source_location_consistent | ||
| CHECK (COALESCE( | ||
| (source_document IS NULL AND source_saved_function_id IS NULL | ||
| AND source_line IS NULL AND source_column IS NULL) | ||
| OR | ||
| (source_document = 'program' AND source_saved_function_id IS NULL | ||
| AND source_line > 0 AND source_column > 0) | ||
| OR | ||
| (source_document = 'saved_function' AND LENGTH(source_saved_function_id) > 0 | ||
| AND source_line > 0 AND source_column > 0), | ||
| FALSE | ||
| )); | ||
|
|
||
| ALTER TABLE program_run_events ADD COLUMN source_document TEXT; | ||
| ALTER TABLE program_run_events ADD COLUMN source_saved_function_id TEXT; | ||
| ALTER TABLE program_run_events ADD COLUMN source_line BIGINT; | ||
| ALTER TABLE program_run_events ADD COLUMN source_column BIGINT | ||
| CONSTRAINT program_run_events_source_location_consistent | ||
| CHECK (COALESCE( | ||
| (source_document IS NULL AND source_saved_function_id IS NULL | ||
| AND source_line IS NULL AND source_column IS NULL) | ||
| OR | ||
| (event_type IN ('program.emitted', 'program.logged') | ||
| AND ( | ||
| (source_document = 'program' AND source_saved_function_id IS NULL | ||
| AND source_line > 0 AND source_column > 0) | ||
| OR | ||
| (source_document = 'saved_function' AND LENGTH(source_saved_function_id) > 0 | ||
| AND source_line > 0 AND source_column > 0) | ||
| )), | ||
| FALSE | ||
| )); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
P3: The restart assertions validate only internal consistency: the SavedFunction document in each event is built from the same response's
snapshot.saved_function, so a lost or placeholder snapshot identity on both sides would still pass. Since the PR's contract is preserving exact snapshot identity across restart, pin the returned snapshot against the recorded source:assert_eq!(snapshot.source, SAVED_FUNCTION);(and optionally checksnapshot.path == "coral.functions.report.forecast").Prompt for AI agents