Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
Allows application to wire through spawning parent
elijahbenizzyf64869ccde34
diff --git a/burr/core/application.py b/burr/core/application.pyindex 6c863e80..5eb9c317 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -386,6 +386,7 @@ class Application:adapter_set: Optional[LifecycleAdapterSet] = None,builder: Optional["ApplicationBuilder"] = None,parent_pointer: Optional[burr_types.ParentPointer] = None,+ spawning_parent_pointer: Optional[burr_types.ParentPointer] = None,tracker: Optional["TrackingClient"] = None,):"""Instantiates an Application. This is an internal API -- use the builder!@@ -411,14 +412,6 @@ class Application:self._state = stateself._adapter_set = adapter_set if adapter_set is not None else LifecycleAdapterSet()self._graph = self._create_graph()- self._adapter_set.call_all_lifecycle_hooks_sync(- "post_application_create",- state=self._state,- application_graph=self._graph,- app_id=self._uid,- partition_key=self._partition_key,- parent_pointer=parent_pointer,- )# TODO -- consider adding global inputs + global input factories to the builderself._tracker = tracker@@ -432,6 +425,16 @@ class Application:),"__context": self._context_factory,}+ self._spawning_parent_pointer = spawning_parent_pointer+ self._adapter_set.call_all_lifecycle_hooks_sync(+ "post_application_create",+ state=self._state,+ application_graph=self._graph,+ app_id=self._uid,+ partition_key=self._partition_key,+ parent_pointer=parent_pointer,+ spawning_parent_pointer=spawning_parent_pointer,+ )# @telemetry.capture_function_usage # todo -- capture usage when we break this up into one that isn't called internally# This will be doable when we move sequence ID to the beginning of the function https://github.com/DAGWorks-Inc/burr/pull/73@@ -455,6 +458,7 @@ class Application:return outdef _context_factory(self, action: Action, sequence_id: int) -> ApplicationContext:+ """Helper function to create an application context, in the form of the dependency factories we inject to nodes."""return ApplicationContext(app_id=self._uid,tracker=self._tracker,@@ -1414,10 +1418,24 @@ class Application:"""Gives the parent pointer of an application (from where it was forked).This is None if it was not forked.+ Forking is the process of starting an application off of another.+:return: The parent pointer object."""return self._parent_pointer+ @property+ def spawning_parent_pointer(self) -> Optional[burr_types.ParentPointer]:+ """Gives the parent pointer of an application (from where it was spawned).+ This is None if it was not spawned.++ Spawning is the process of launching an application from within+ a step of another. This is used for recursive tracking.++ :return: The parent pointer object.+ """+ return self._spawning_parent_pointer+def _create_graph(self) -> ApplicationGraph:"""Internal-facing utility function for creating an ApplicationGraph"""all_actions = {action.name: action for action in self._actions}@@ -1570,6 +1588,9 @@ class ApplicationBuilder:self.fork_from_app_id: Optional[str] = Noneself.fork_from_partition_key: Optional[str] = Noneself.fork_from_sequence_id: Optional[int] = None+ self.spawn_from_app_id: Optional[str] = None+ self.spawn_from_partition_key: Optional[str] = None+ self.spawn_from_sequence_id: Optional[int] = Noneself.loaded_from_fork: bool = Falseself.tracker = None@@ -1707,9 +1728,7 @@ class ApplicationBuilder:def with_tracker(self,- tracker: Union[- Literal["local"], "TrackingClient" # TODO -- support async tracking- ] = "local",+ tracker: Union[Literal["local"], "TrackingClient"] = "local",project: str = "default",params: Dict[str, Any] = None,):@@ -1729,6 +1748,7 @@ class ApplicationBuilder::return: The application builder for future chaining."""# if it's a lifecycle adapter, just add it+ instantiated_tracker = trackerif isinstance(tracker, str):if params is None:params = {}@@ -1737,16 +1757,17 @@ class ApplicationBuilder:kwargs = {"project": project}kwargs.update(params)- self.lifecycle_adapters.append(LocalTrackingClient(**kwargs))+ instantiated_tracker = LocalTrackingClient(**kwargs)+ self.lifecycle_adapters.append(instantiated_tracker)else:raise ValueError(f"Tracker {tracker}:{project} not supported")else:- self.lifecycle_adapters.append(tracker)+ self.lifecycle_adapters.append(instantiated_tracker)if params is not None:raise ValueError("Params are not supported for object-specified trackers, these are already initialized!")- self.tracker = tracker+ self.tracker = instantiated_trackerreturn selfdef initialize_from(@@ -1819,6 +1840,27 @@ class ApplicationBuilder:self.lifecycle_adapters.append(persistence.PersisterHook(persister))return self+ def with_spawning_parent(+ self, app_id: str, sequence_id: int, partition_key: Optional[str] = None+ ) -> "ApplicationBuilder":+ """Sets the 'spawning' parent application that created this app.+ This is used for tracking purposes. Doing this creates a parent/child relationship.+ There can be many spawned children from a single sequence ID (just as there can be many forks of an app).++ Note the difference between this and forking. Forking allows you to create a new app+ where the old one left off. This suggests that this application is wholly contained+ within the parent application.++ :param app_id: ID of application that spawned this app+ :param sequence_id: Sequence ID of the parent app that spawned this app+ :param partition_key: Partition key of the parent app that spawned this app+ :return: The application builder for future chaining.+ """+ self.spawn_from_app_id = app_id+ self.spawn_from_sequence_id = sequence_id+ self.spawn_from_partition_key = partition_key+ return self+def _load_from_persister(self):"""Loads from the set persister and into this current object.@@ -1935,4 +1977,11 @@ class ApplicationBuilder:if self.loaded_from_forkelse None,tracker=self.tracker,+ spawning_parent_pointer=burr_types.ParentPointer(+ app_id=self.spawn_from_app_id,+ partition_key=self.spawn_from_partition_key,+ sequence_id=self.spawn_from_sequence_id,+ )+ if self.spawn_from_app_id is not None+ else None,)diff --git a/burr/lifecycle/base.py b/burr/lifecycle/base.pyindex e1b59826..e2f235af 100644--- a/burr/lifecycle/base.py+++ b/burr/lifecycle/base.py@@ -136,7 +136,8 @@ class PostApplicationCreateHook(abc.ABC):partition_key: Optional[str],state: "State",application_graph: "ApplicationGraph",- parent_pointer: burr_types.ParentPointer,+ parent_pointer: Optional[burr_types.ParentPointer],+ spawning_parent_pointer: Optional[burr_types.ParentPointer],**future_kwargs: Any,):"""Runs after an "application" object is instantiated. This is run by the Application, in its constructor,@@ -146,7 +147,8 @@ class PostApplicationCreateHook(abc.ABC)::param partition_key: Partition key of application:param state: Current state of the application:param application_graph: Application graph of the application, representing the state machine- :param parent_pointer: Parent pointer of the application+ :param parent_pointer: Forking parent pointer of the application (application that it copied from)+ :param spawning_parent_pointer: Spawning parent pointer of the application (application that it was launched from):param future_kwargs: Future keyword arguments for backwards compatibility"""passdiff --git a/burr/tracking/base.py b/burr/tracking/base.pyindex 25aa473f..cc87d792 100644--- a/burr/tracking/base.py+++ b/burr/tracking/base.py@@ -20,7 +20,8 @@ class SyncTrackingClient("""Base class for synchronous tracking clients. All tracking clients must implement from thisTODO -- create an async tracking client"""- pass+ def copy(self):+ passTrackingClient = SyncTrackingClientdiff --git a/burr/tracking/client.py b/burr/tracking/client.pyindex c3e270cb..1770cd45 100644--- a/burr/tracking/client.py+++ b/burr/tracking/client.py@@ -1,5 +1,6 @@import abcimport datetime+import fcntlimport jsonimport loggingimport os@@ -30,6 +31,7 @@ from burr.tracking.common.models import (ApplicationModel,BeginEntryModel,BeginSpanModel,+ ChildApplicationModel,EndEntryModel,EndSpanModel,PointerModel,@@ -80,14 +82,13 @@ class SyncTrackingClient(ABC,):@abc.abstractmethod- def spawn(self) -> Self:- """Spawns a tracking client with the correct parent pointer.- Also potentially logs the spawn event to the tracking system.+ def copy(self) -> Self:+ """Clones the tracking client. This is useful for forking applications.+ Note we have to copy the tracking client as it is stateful for tracking.+ We may make this called internally if it has started already,+ or make it so it carries multiple states at once.- This enables you to launch a Burr application inside another Burr application,- and have the parent application track the child application.-- :return:+ :return: a copy of the self."""pass@@ -105,6 +106,10 @@ class LocalTrackingClient(GRAPH_FILENAME = "graph.json"METADATA_FILENAME = "metadata.json"LOG_FILENAME = "log.jsonl"+ CHILDREN_FILENAME = (+ "children.jsonl" # any applications that are spawned or forked will show up here+ )+ # This is purely an optimization for bi-directional relationships using filesystems (denormalized data)DEFAULT_STORAGE_DIR = "~/.burr"def __init__(@@ -131,10 +136,86 @@ class LocalTrackingClient(f"Project: {project} is not valid. Project name cannot contain non-alphanumeric (except _ and -) characters.""We will be relaxing this restriction later but for now please rename your project!")+ self.raw_storage_dir = storage_dirself.storage_dir = LocalTrackingClient.get_storage_path(project, storage_dir)self.project_id = projectself.serde_kwargs = serde_kwargs or {}+ def _log_child_relationships(+ self,+ fork_parent_pointer_model: Optional[burr_types.ParentPointer],+ spawn_parent_pointer_model: Optional[burr_types.ParentPointer],+ app_id: str,+ ):+ """Logs a child relationship. This is special as it does not log to the main log file. Rather+ it logs within the parent directory. Note this only exists to maintain (denormalized) bidirectional+ pointers, as the filesystem is not a database, so querying is very inefficient.++ This uses fctl.flock to ensure that the file is not written to by multiple processes at the same time.+ Read may be corrupted (by the server), as it does not use the lock, but that will retry.+ """+ parent_relationships = []+ if fork_parent_pointer_model is not None:+ parent_relationships.append(+ # This effectively inverts the pointers+ # The logging call is being made from the child, so we're logging to the parent+ # We do it once for the fork_parent (if it exists), and once for the spawn_parent (if it exists)+ (+ fork_parent_pointer_model.app_id,+ ChildApplicationModel(+ child=PointerModel(+ app_id=app_id,+ sequence_id=None,+ partition_key=None, # TODO -- get partition key+ ),+ event_time=datetime.datetime.now(),+ event_type="fork",+ # this is the sequence ID at which this link occurred (from the parent)+ sequence_id=fork_parent_pointer_model.sequence_id,+ ),+ )+ )+ if spawn_parent_pointer_model is not None:+ # See the notes above+ parent_relationships.append(+ (+ spawn_parent_pointer_model.app_id,+ ChildApplicationModel(+ child=PointerModel(+ app_id=app_id,+ sequence_id=None,+ partition_key=None, # TODO -- get partition key+ ),+ event_time=datetime.datetime.now(),+ event_type="spawn_start",+ sequence_id=spawn_parent_pointer_model.sequence_id,+ ),+ )+ )+ for parent_id, child_of in parent_relationships:+ parent_path = os.path.join(self.storage_dir, parent_id)+ if not os.path.exists(parent_path):+ # This currently makes the parent directory so that it does not fail+ # If the parent directory exists we'll just use that+ os.makedirs(parent_path)+ parent_children_list_path = os.path.join(+ parent_path, LocalTrackingClient.CHILDREN_FILENAME+ )+ with open(parent_children_list_path, "a") as f:+ fileno = f.fileno()+ try:+ fcntl.flock(fileno, fcntl.LOCK_EX)+ f.write(child_of.model_dump_json() + "\n")+ finally:+ # we always want to release the lock so its not held indefinitely+ fcntl.flock(fileno, fcntl.LOCK_UN)++ def copy(self) -> "LocalTrackingClient":+ return LocalTrackingClient(+ project=self.project_id,+ storage_dir=self.raw_storage_dir,+ )+@classmethoddef get_storage_path(cls, project, storage_dir) -> str:return str(os.path.join(os.path.expanduser(storage_dir), project))@@ -239,6 +320,7 @@ class LocalTrackingClient(state: "State",application_graph: "ApplicationGraph",parent_pointer: Optional[burr_types.ParentPointer],+ spawning_parent_pointer: Optional[burr_types.ParentPointer],**future_kwargs: Any,):self._ensure_dir_structure(app_id)@@ -264,13 +346,19 @@ class LocalTrackingClient(returnmetadata = ApplicationMetadataModel(partition_key=partition_key,- parent_pointer=PointerModel.from_pointer(parent_pointer)- if parent_pointer is not None- else None,+ parent_pointer=PointerModel.from_pointer(parent_pointer),+ spawning_parent_pointer=PointerModel.from_pointer(spawning_parent_pointer),).model_dump()with open(metadata_path, "w", errors="replace") as f:json.dump(metadata, f)+ # Append to the parents of this the pointer to this, now+ self._log_child_relationships(+ parent_pointer,+ spawning_parent_pointer,+ app_id,+ )+def _append_write_line(self, model: pydantic.BaseModel):self.f.write(model.model_dump_json() + "\n")self.f.flush()@@ -354,9 +442,6 @@ class LocalTrackingClient(# TODO:return []- def spawn(self) -> Self:- raise ValueError("TODO")-def load(self, partition_key: str, app_id: Optional[str], sequence_id: Optional[int] = None, **kwargs) -> Optional[PersistedStateData]:diff --git a/burr/tracking/common/models.py b/burr/tracking/common/models.pyindex c164c614..bb926e4a 100644--- a/burr/tracking/common/models.py+++ b/burr/tracking/common/models.py@@ -1,5 +1,5 @@import datetime-from typing import Any, Dict, List, Optional+from typing import Any, Dict, List, Literal, Optionalfrom burr.common import types as burr_typesfrom burr.core import Action@@ -77,14 +77,31 @@ class PointerModel(IdentifyingModel):type: str = "pointer_data"@staticmethod- def from_pointer(pointer: burr_types.ParentPointer) -> "PointerModel":- return PointerModel(- app_id=pointer.app_id,- sequence_id=pointer.sequence_id,- partition_key=pointer.partition_key,+ def from_pointer(pointer: Optional[burr_types.ParentPointer]) -> Optional["PointerModel"]:+ return (+ PointerModel(+ app_id=pointer.app_id,+ sequence_id=pointer.sequence_id,+ partition_key=pointer.partition_key,+ )+ if pointer is not None+ else None)+class ChildApplicationModel(IdentifyingModel):+ """Stores data about a child application (either a fork or a spawned application).+ This allows us to link from parent -> child in the UI."""++ child: PointerModel+ event_time: datetime.datetime+ event_type: Literal[+ "fork", "spawn_start"+ ] # TODO -- get spawn_end working when we have interaction hooks (E.G. on app fn calls)+ sequence_id: Optional[int]+ type: str = "child_application_data"++class ApplicationModel(IdentifyingModel):"""Pydantic model that represents an application for storing/visualization in the UI"""@@ -113,9 +130,17 @@ class ApplicationMetadataModel(IdentifyingModel):partition_key: Optional[str] = Noneparent_pointer: Optional[PointerModel] = None # pointer to parent data+ spawning_parent_pointer: Optional[PointerModel] = None # pointer to spawning parent datatype: str = "application_metadata"+INPUT_FILTERLIST = {"__tracer", "__context"}+++def _filter_inputs(d: dict) -> dict:+ return {k: v for k, v in d.items() if k not in INPUT_FILTERLIST}++class BeginEntryModel(IdentifyingModel):"""Pydantic model that represents an entry for the beginning of a step"""diff --git a/burr/tracking/server/backend.py b/burr/tracking/server/backend.pyindex 22ea2773..1316279e 100644--- a/burr/tracking/server/backend.py+++ b/burr/tracking/server/backend.py@@ -8,7 +8,13 @@ import aiofiles.os as aiofilesosimport fastapifrom burr.tracking.common import models-from burr.tracking.common.models import BeginEntryModel, BeginSpanModel, EndEntryModel, EndSpanModel+from burr.tracking.common.models import (+ BeginEntryModel,+ BeginSpanModel,+ ChildApplicationModel,+ EndEntryModel,+ EndSpanModel,+)from burr.tracking.server import schemafrom burr.tracking.server.schema import ApplicationLogs, ApplicationSummary@@ -147,6 +153,7 @@ class LocalBackend(BackendBase):num_steps=await self.get_number_of_steps(log_path),tags={},parent_pointer=metadata.parent_pointer,+ spawning_parent_pointer=metadata.spawning_parent_pointer,))return out@@ -162,6 +169,7 @@ class LocalBackend(BackendBase):log_file = os.path.join(app_filepath, "log.jsonl")graph_file = os.path.join(app_filepath, "graph.json")metadata_file = os.path.join(app_filepath, "metadata.json")+ children_file = os.path.join(app_filepath, "children.jsonl")metadata = await self._load_metadata(metadata_file)if not os.path.exists(graph_file):raise fastapi.HTTPException(@@ -200,8 +208,18 @@ class LocalBackend(BackendBase):for span in spans_by_id.values():step = steps_by_sequence_id[span.begin_entry.action_sequence_id]step.spans.append(span)+ children = []+ if os.path.exists(children_file):+ async with aiofiles.open(children_file) as f:+ str_children = await f.readlines()+ children = [+ ChildApplicationModel.parse_obj(json.loads(item)) for item in str_children+ ]+return ApplicationLogs(application=schema.ApplicationModel.parse_raw(str_graph),steps=list(steps_by_sequence_id.values()),parent_pointer=metadata.parent_pointer,+ spawning_parent_pointer=metadata.spawning_parent_pointer,+ children=children,)diff --git a/burr/tracking/server/schema.py b/burr/tracking/server/schema.pyindex 8c6780bf..61989c6e 100644--- a/burr/tracking/server/schema.py+++ b/burr/tracking/server/schema.py@@ -7,6 +7,7 @@ from burr.tracking.common.models import (ApplicationModel,BeginEntryModel,BeginSpanModel,+ ChildApplicationModel,EndEntryModel,EndSpanModel,PointerModel,@@ -30,6 +31,13 @@ class ApplicationSummary(pydantic.BaseModel):num_steps: inttags: Dict[str, str]parent_pointer: Optional[PointerModel] = None+ spawning_parent_pointer: Optional[PointerModel] = None+++class ApplicationModelWithChildren(pydantic.BaseModel):+ application: ApplicationModel+ children: List[PointerModel]+ type: str = "application_with_children"class Span(pydantic.BaseModel):@@ -53,5 +61,7 @@ class ApplicationLogs(pydantic.BaseModel):we will likely be rethinking this but for now this provides for easy parsing."""application: ApplicationModel+ children: List[ChildApplicationModel]steps: List[Step]parent_pointer: Optional[PointerModel] = None+ spawning_parent_pointer: Optional[PointerModel] = Nonediff --git a/telemetry/ui/src/api/index.ts b/telemetry/ui/src/api/index.tsindex 14927baf..4969521a 100644--- a/telemetry/ui/src/api/index.ts+++ b/telemetry/ui/src/api/index.ts@@ -14,6 +14,7 @@ export type { ApplicationSummary } from './models/ApplicationSummary';export type { BeginEntryModel } from './models/BeginEntryModel';export type { BeginSpanModel } from './models/BeginSpanModel';export { ChatItem } from './models/ChatItem';+export { ChildApplicationModel } from './models/ChildApplicationModel';export type { DraftInit } from './models/DraftInit';export { EmailAssistantState } from './models/EmailAssistantState';export type { EndEntryModel } from './models/EndEntryModel';diff --git a/telemetry/ui/src/api/models/ApplicationLogs.ts b/telemetry/ui/src/api/models/ApplicationLogs.tsindex 6a1c8f1e..0194e765 100644--- a/telemetry/ui/src/api/models/ApplicationLogs.ts+++ b/telemetry/ui/src/api/models/ApplicationLogs.ts@@ -3,6 +3,7 @@/* tslint:disable *//* eslint-disable */import type { ApplicationModel } from './ApplicationModel';+import type { ChildApplicationModel } from './ChildApplicationModel';import type { PointerModel } from './PointerModel';import type { Step } from './Step';/**@@ -11,6 +12,8 @@ import type { Step } from './Step';*/export type ApplicationLogs = {application: ApplicationModel;+ children: Array<ChildApplicationModel>;steps: Array<Step>;parent_pointer?: PointerModel | null;+ spawning_parent_pointer?: PointerModel | null;};diff --git a/telemetry/ui/src/api/models/ApplicationSummary.ts b/telemetry/ui/src/api/models/ApplicationSummary.tsindex ecf2e867..6f9bdc05 100644--- a/telemetry/ui/src/api/models/ApplicationSummary.ts+++ b/telemetry/ui/src/api/models/ApplicationSummary.ts@@ -11,4 +11,5 @@ export type ApplicationSummary = {num_steps: number;tags: Record<string, string>;parent_pointer?: PointerModel | null;+ spawning_parent_pointer?: PointerModel | null;};diff --git a/telemetry/ui/src/api/models/ChildApplicationModel.ts b/telemetry/ui/src/api/models/ChildApplicationModel.tsnew file mode 100644index 00000000..f98fc169--- /dev/null+++ b/telemetry/ui/src/api/models/ChildApplicationModel.ts@@ -0,0 +1,22 @@+/* generated using openapi-typescript-codegen -- do no edit */+/* istanbul ignore file */+/* tslint:disable */+/* eslint-disable */+import type { PointerModel } from './PointerModel';+/**+ * Stores data about a child application (either a fork or a spawned application).+ * This allows us to link from parent -> child in the UI.+ */+export type ChildApplicationModel = {+ type?: string;+ child: PointerModel;+ event_time: string;+ event_type: ChildApplicationModel.event_type;+ sequence_id: number;+};+export namespace ChildApplicationModel {+ export enum event_type {+ FORK = 'fork',+ SPAWN_START = 'spawn_start'+ }+}diff --git a/telemetry/ui/src/api/models/PointerModel.ts b/telemetry/ui/src/api/models/PointerModel.tsindex d70e44e9..1fe0459b 100644--- a/telemetry/ui/src/api/models/PointerModel.ts+++ b/telemetry/ui/src/api/models/PointerModel.ts@@ -2,10 +2,14 @@/* istanbul ignore file *//* tslint:disable *//* eslint-disable */+/**+ * Stores pointers to unique identifiers for an application.+ * This is used by a few different places to, say, store parent references+ * bewteen application instances.+ */export type PointerModel = {- type?: string;- app_id: string;- sequence_id: (number | null);- partition_key: (string | null);+ type?: string;+ app_id: string;+ sequence_id: number | null;+ partition_key: string | null;};-diff --git a/telemetry/ui/src/components/common/chip.tsx b/telemetry/ui/src/components/common/chip.tsxindex 06c1aa0a..485702b6 100644--- a/telemetry/ui/src/components/common/chip.tsx+++ b/telemetry/ui/src/components/common/chip.tsx@@ -9,7 +9,8 @@ const chipColorMap = {running: 'bg-dwlightblue',demo: 'bg-yellow-400',test: 'bg-gray-800',- fork: 'bg-dwdarkblue/80'+ fork: 'bg-dwdarkblue/80',+ spawn: 'bg-purple-600'};export type ChipType = keyof typeof chipColorMap;diff --git a/telemetry/ui/src/components/routes/AppList.tsx b/telemetry/ui/src/components/routes/AppList.tsxindex d53c4574..ccede130 100644--- a/telemetry/ui/src/components/routes/AppList.tsx+++ b/telemetry/ui/src/components/routes/AppList.tsx@@ -5,9 +5,10 @@ import { ApplicationSummary, DefaultService } from '../../api';import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '../common/table';import { DateTimeDisplay } from '../common/dates';import { useState } from 'react';-import { FunnelIcon } from '@heroicons/react/24/outline';+import { FunnelIcon, MinusIcon, PlusIcon } from '@heroicons/react/24/outline';import { useNavigate } from 'react-router-dom';import { MdForkRight } from 'react-icons/md';+import { RiCornerDownRightLine } from 'react-icons/ri';const StepCountHeader = (props: {displayZeroCount: boolean;@@ -35,7 +36,115 @@ const getForkID = (app: ApplicationSummary) => {return null;}};+/**+ * Sub-application list -- handles spaned applications.+ * This contains a list of applications that correspond to a parent application.+ * It is either an individual application or more when recursively applied+ * @param props+ * @returns+ */+const AppSubList = (props: {+ app: ApplicationSummary;+ navigate: (url: string) => void;+ projectId: string;+ spawningParentMap: Map<string, ApplicationSummary[]>;+ parentHovered?: boolean;+ depth?: number;+ displayPartitionKey: boolean;+}) => {+ const [subAppsExpanded, setSubAppsExpanded] = useState(false);+ const forkID = getForkID(props.app);+ const { app } = props;+ const ExpandIcon = subAppsExpanded ? MinusIcon : PlusIcon;++ const [isHovered, setIsHovered] = useState(true);+ const isHighlighted = props.parentHovered || isHovered;+ const depth = props.depth || 0;+ return (+ <>+ <TableRow+ onMouseEnter={() => {+ setIsHovered(true);+ }}+ onMouseLeave={() => {+ setIsHovered(false);+ }}+ key={props.app.app_id}+ className={`cursor-pointer ${isHighlighted ? 'bg-gray-50' : ''}`}+ onClick={() => {+ props.navigate(`/project/${props.projectId}/${app.app_id}`);+ }}+ >+ {props.displayPartitionKey && (+ <TableCell className="text-gray-600 font-sans">{app.partition_key}</TableCell>+ )}+ <TableCell className="font-semibold text-gray-700">+ <div className="flex flex-row gap-1 items-center md:min-w-[21rem] md:max-w-none max-w-24">+ {[...Array(depth).keys()].map((i) => (+ <RiCornerDownRightLine+ key={i}+ className={`${i === depth - 1 ? 'opacity-100' : 'opacity-0'} text-lg text-gray-600`}+ ></RiCornerDownRightLine>+ ))}+ {app.app_id}+ </div>+ </TableCell>+ <TableCell>+ <DateTimeDisplay date={app.last_written} mode="long" />+ </TableCell>+ <TableCell className="z-50">+ {forkID ? (+ <MdForkRight+ className=" hover:scale-125 h-5 w-5 text-gray-600 "+ onClick={(e) => {+ props.navigate(`/project/${props.projectId}/${forkID}`);+ e.stopPropagation();+ }}+ />+ ) : (+ <></>+ )}+ </TableCell>+ <TableCell>+ {props.spawningParentMap.has(props.app.app_id) ? (+ <div className="flex flex-row items-center gap-1">+ <ExpandIcon+ className="h-4 w-4 text-gray-600 hover:scale-110 cursor-pointer"+ onClick={(e) => {+ setSubAppsExpanded(!subAppsExpanded);+ e.stopPropagation();+ }}+ />+ <span className="text-gray-600">+ ({props.spawningParentMap.get(props.app.app_id)?.length})+ </span>+ </div>+ ) : (+ <></>+ )}+ </TableCell>+ <TableCell className="text-gray-600">{app.num_steps}</TableCell>+ </TableRow>+ {props.spawningParentMap.has(props.app.app_id) && subAppsExpanded+ ? props.spawningParentMap.get(props.app.app_id)?.map((subApp) => {+ return (+ <AppSubList+ key={subApp.app_id}+ displayPartitionKey={props.displayPartitionKey}+ app={subApp}+ navigate={props.navigate}+ projectId={props.projectId}+ spawningParentMap={props.spawningParentMap}+ parentHovered={isHighlighted}+ depth={depth + 1}+ />+ );+ })+ : null}+ </>+ );+};/*** List of applications. Purely rendering a list, also sorts them.*/@@ -50,59 +159,51 @@ export const AppListTable = (props: { apps: ApplicationSummary[]; projectId: str.filter((app) => {return app.num_steps > 0 || displayZeroCount;});++ // Maps from parents -> children+ const spawningParentMap = appsToDisplay.reduce((acc, app) => {+ if (app.spawning_parent_pointer) {+ if (acc.has(app.spawning_parent_pointer.app_id)) {+ acc.get(app.spawning_parent_pointer.app_id)!.push(app);+ } else {+ acc.set(app.spawning_parent_pointer.app_id, [app]);+ }+ }+ return acc;+ }, new Map<string, ApplicationSummary[]>());++ // Display the parents no matter what+ const rootAppsToDisplay = appsCopy.filter((app) => app.spawning_parent_pointer === null);+ const anyHavePartitionKey = rootAppsToDisplay.some((app) => app.partition_key !== null);+return (- <Table>+ <Table dense={1}><TableHead><TableRow>- <TableHeader>Partition Key</TableHeader>+ {anyHavePartitionKey && <TableHeader>Partition Key</TableHeader>}<TableHeader>ID</TableHeader>- <TableHeader>First Seen</TableHeader><TableHeader>Last Run</TableHeader><TableHeader>Forked</TableHeader>+ <TableHeader>Sub Apps</TableHeader><TableHeader><StepCountHeaderdisplayZeroCount={displayZeroCount}setDisplayZeroCount={setDisplayZeroCount}/></TableHeader>- <TableHeader></TableHeader>- <TableHeader></TableHeader></TableRow></TableHead><TableBody>- {appsToDisplay.map((app) => {- const forkID = getForkID(app);+ {rootAppsToDisplay.map((app) => {return (- <TableRow+ <AppSubListkey={app.app_id}- className="hover:bg-gray-50 cursor-pointer"- onClick={() => {- navigate(`/project/${props.projectId}/${app.app_id}`);- }}- >- <TableCell className="text-gray-600 font-sans">{app.partition_key}</TableCell>- <TableCell className="font-semibold text-gray-700">{app.app_id}</TableCell>- <TableCell>- <DateTimeDisplay date={app.first_written} mode="long" />- </TableCell>- <TableCell>- <DateTimeDisplay date={app.last_written} mode="long" />- </TableCell>- <TableCell className="z-50">- {forkID ? (- <MdForkRight- className=" hover:scale-125 h-5 w-5 text-gray-600 "- onClick={(e) => {- navigate(`/project/${props.projectId}/${forkID}`);- e.stopPropagation();- }}- />- ) : (- <></>- )}- </TableCell>- <TableCell>{app.num_steps}</TableCell>- </TableRow>+ app={app}+ projectId={props.projectId}+ navigate={navigate}+ spawningParentMap={spawningParentMap}+ displayPartitionKey={anyHavePartitionKey}+ />);})}</TableBody>diff --git a/telemetry/ui/src/components/routes/app/AppView.tsx b/telemetry/ui/src/components/routes/app/AppView.tsxindex 5708d791..2ea39f45 100644--- a/telemetry/ui/src/components/routes/app/AppView.tsx+++ b/telemetry/ui/src/components/routes/app/AppView.tsx@@ -214,6 +214,8 @@ export const AppView = (props: {setMinimized={setMinimizedTable}projectId={projectId}parentPointer={data?.parent_pointer || undefined}+ spawningParentPointer={data?.spawning_parent_pointer || undefined}+ links={data.children || []}/></div>{props.orientation === 'stacked_horizontal' && (diff --git a/telemetry/ui/src/components/routes/app/StepList.tsx b/telemetry/ui/src/components/routes/app/StepList.tsxindex 1127716f..53f9c8cd 100644--- a/telemetry/ui/src/components/routes/app/StepList.tsx+++ b/telemetry/ui/src/components/routes/app/StepList.tsx@@ -1,10 +1,12 @@-import { PointerModel, Span, Step } from '../../../api';+import { ChildApplicationModel, PointerModel, Span, Step } from '../../../api';import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '../../common/table';import { DateTimeDisplay, DurationDisplay, TimeDisplay } from '../../common/dates';import { backgroundColorsForIndex, backgroundColorsForStatus } from './AppView';import { Status, getActionStatus } from '../../../utils';import { Chip } from '../../common/chip';import { useRef, useState } from 'react';+import { TbGrillFork } from 'react-icons/tb';+import {ArrowPathIcon,ChevronLeftIcon,@@ -14,9 +16,9 @@ import {} from '@heroicons/react/24/outline';import { PauseIcon } from '@heroicons/react/24/solid';+import { TiFlowChildren } from 'react-icons/ti';import { RiCornerDownRightLine } from 'react-icons/ri';-import { MdForkRight } from 'react-icons/md';import { Link } from 'react-router-dom';/**@@ -117,18 +119,25 @@ const ActionTableRow = (props: {currentSelectedIndex: number | undefined;setCurrentSelectedIndex: (index?: number) => void;numPriorIndices: number;- isExpanded: boolean;- toggleExpanded: (index: number) => void;+ isTracesExpanded: boolean;+ toggleTraceExpanded: (index: number) => void;+ isLinksExpanded: boolean;+ toggleLinksExpanded: (index: number) => void;minimized: boolean;+ links: ChildApplicationModel[];+ displaySpansCol: boolean;+ displayLinksCol: boolean;}) => {const sequenceID = props.step.step_start_log.sequence_id;const isHovered = props.currentHoverIndex === sequenceID;const spanCount = props.step.spans.length;+ const childCount = props.links.length;const shouldBeHighlighted =props.currentSelectedIndex !== undefined &&sequenceID <= props.currentSelectedIndex &&sequenceID >= props.currentSelectedIndex - props.numPriorIndices;- const ExpandIcon = props.isExpanded ? MinusIcon : PlusIcon;+ const TraceExpandIcon = props.isTracesExpanded ? MinusIcon : PlusIcon;+ const LinkExpandIcon = props.isLinksExpanded ? MinusIcon : PlusIcon;return (<CommonTableRowsequenceID={sequenceID}@@ -154,22 +163,42 @@ const ActionTableRow = (props: {end={props.step.step_end_log?.end_time}/></TableCell>- <TableCell>- {spanCount > 0 ? (- <div className="flex gap-1 items-center">- <ExpandIcon- className="h-4 w-4 text-gray-600 hover:scale-110 cursor-pointer"- onClick={(e) => {- props.toggleExpanded(sequenceID);- e.stopPropagation();- }}- />- <span className="text-gray-600">{spanCount}</span>- </div>- ) : (- <span></span>- )}- </TableCell>+ {props.displaySpansCol && (+ <TableCell>+ {spanCount > 0 ? (+ <div className="flex gap-1 items-center">+ <TraceExpandIcon+ className="h-4 w-4 text-gray-600 hover:scale-110 cursor-pointer"+ onClick={(e) => {+ props.toggleTraceExpanded(sequenceID);+ e.stopPropagation();+ }}+ />+ <span className="text-gray-600">{spanCount}</span>+ </div>+ ) : (+ <span></span>+ )}+ </TableCell>+ )}+ {props.displayLinksCol && (+ <TableCell>+ {childCount > 0 ? (+ <div className="flex gap-1 items-center">+ <LinkExpandIcon+ className="h-4 w-4 text-gray-600 hover:scale-110 cursor-pointer"+ onClick={(e) => {+ props.toggleLinksExpanded(sequenceID);+ e.stopPropagation();+ }}+ />+ <span className="text-gray-600">{childCount}</span>+ </div>+ ) : (+ <span></span>+ )}+ </TableCell>+ )}<TableCell><div className="max-w-min"><StatusChip status={getActionStatus(props.step)} />@@ -181,6 +210,73 @@ const ActionTableRow = (props: {);};+const LinkSubTable = (props: {+ step: Step;+ currentHoverIndex: number | undefined;+ setCurrentHoverIndex: (index?: number) => void;+ currentSelectedIndex: number | undefined;+ setCurrentSelectedIndex: (index?: number) => void;+ numPriorIndices: number;+ minimized: boolean;+ links: ChildApplicationModel[];+ projectId: string;+ displaySpansCol: boolean;+ displayLinksCol: boolean;+}) => {+ const sequenceID = props.step.step_start_log.sequence_id;+ const isHovered = props.currentHoverIndex === sequenceID;+ const shouldBeHighlighted =+ props.currentSelectedIndex !== undefined &&+ sequenceID <= props.currentSelectedIndex &&+ sequenceID >= props.currentSelectedIndex - props.numPriorIndices;+ const normalText = shouldBeHighlighted ? 'text-gray-100' : 'text-gray-600';+ const iconColor = shouldBeHighlighted ? 'text-gray-100' : 'text-gray-400';+ return (+ <>+ {props.links.map((subApp) => {+ const childType = subApp.event_type;+ const Icon = childType === 'fork' ? TbGrillFork : TiFlowChildren;+ return (+ <CommonTableRow+ key={`${subApp.child.app_id}-link-table-row`}+ sequenceID={sequenceID}+ isHovered={isHovered}+ shouldBeHighlighted={shouldBeHighlighted}+ currentSelectedIndex={props.currentSelectedIndex}+ step={props.step}+ setCurrentHoverIndex={props.setCurrentHoverIndex}+ setCurrentSelectedIndex={props.setCurrentSelectedIndex}+ >+ <TableCell colSpan={1}>+ <Icon className={`h-5 w-5 ${iconColor} `} />+ </TableCell>+ <TableCell colSpan={1} className={` ${normalText} w-48 min-w-48 max-w-48 truncate`}>+ <Link to={`/project/${props.projectId}/${subApp.child.app_id}`}>+ <span className="hover:underline">{subApp.child.app_id}</span>+ </Link>+ </TableCell>+ <TableCell colSpan={1} className={` ${normalText} min-w-10`}>+ <div className="flex flex-row justify-end">+ <DateTimeDisplay date={subApp.event_time} mode={'short'} />+ </div>+ </TableCell>+ <TableCell+ colSpan={1 + +!!props.displayLinksCol + +!!props.displaySpansCol}+ ></TableCell>+ <TableCell colSpan={1} className="text-gray-500">+ <Chip+ label={subApp.event_type === 'fork' ? 'forked' : 'spawned'}+ chipType={subApp.event_type === 'fork' ? 'fork' : 'spawn'}+ className="w-16 flex flex-row justify-center"+ />+ </TableCell>+ </CommonTableRow>+ );+ })}+ </>+ );+};+const TraceSubTable = (props: {spans: Span[];step: Step;@@ -190,6 +286,8 @@ const TraceSubTable = (props: {setCurrentSelectedIndex: (index?: number) => void;numPriorIndices: number;minimized: boolean;+ displaySpansCol: boolean;+ displayLinksCol: boolean;}) => {return (<>@@ -209,7 +307,7 @@ const TraceSubTable = (props: {const normalText = shouldBeHighlighted ? 'text-gray-100' : 'text-gray-400';return (<CommonTableRow- key={span.begin_entry.span_id}+ key={`${span.begin_entry.span_id}-trace-table-row`}sequenceID={sequenceID}isHovered={isHovered}shouldBeHighlighted={shouldBeHighlighted}@@ -334,36 +432,49 @@ export const StepList = (props: {setMinimized: (b: boolean) => void;projectId: string;parentPointer: PointerModel | undefined;+ spawningParentPointer: PointerModel | undefined;+ links: ChildApplicationModel[];}) => {// This is a quick way of expanding the actions all at once- const [expandedActions, setExpandedActions] = useState<number[]>([]);- const toggleExpanded = (index: number) => {- if (expandedActions.includes(index)) {- setExpandedActions(expandedActions.filter((i) => i !== index));+ const [traceExpandedActions, setTraceExpandedActions] = useState<number[]>([]);+ const toggleTraceExpandedActions = (index: number) => {+ if (traceExpandedActions.includes(index)) {+ setTraceExpandedActions(traceExpandedActions.filter((i) => i !== index));} else {- setExpandedActions([...expandedActions, index]);+ setTraceExpandedActions([...traceExpandedActions, index]);}};const [intentionExpandAll, setIntentionExpandAll] = useState(false);- // const ExpandAllIcon = intentionExpandAll ? MinusIcon : PlusIcon;- const expandAll = () => {- const allIndices = props.steps.map((step) => step.step_start_log.sequence_id);- setExpandedActions(allIndices);- };- const toggleExpandAll = () => {++ const [linksExpandedActions, setLinksExpandedActions] = useState<number[]>([]);+ const toggleExpandAllTraces = () => {if (intentionExpandAll) {- setExpandedActions([]);+ setTraceExpandedActions([]);} else {- expandAll();+ const allIndices = props.steps.map((step) => step.step_start_log.sequence_id);+ setTraceExpandedActions(allIndices);}setIntentionExpandAll(!intentionExpandAll);};- const isExpanded = (index: number) => {- return expandedActions.includes(index);+ const isLinksExpanded = (index: number) => {+ return linksExpandedActions.includes(index);+ };+ const toggleLinksExpanded = (index: number) => {+ if (isLinksExpanded(index)) {+ setLinksExpandedActions(linksExpandedActions.filter((i) => i !== index));+ } else {+ setLinksExpandedActions([...linksExpandedActions, index]);+ }};const MinimizeTableIcon = props.minimized ? ChevronRightIcon : ChevronLeftIcon;- const hasAnySpans = props.steps.some((step) => step.spans.length > 0);- // const hasParent = props.+ const displaySpansCol = props.steps.some((step) => step.spans.length > 0);+ const displayLinksCol = props.links.length > 0;+ const linksBySequenceID = props.links.reduce((acc, child) => {+ const existing = acc.get(child.sequence_id) || [];+ existing.push(child);+ acc.set(child.sequence_id, existing);+ return acc;+ }, new Map<number, ChildApplicationModel[]>());return (<Table dense={2}><TableHead className=" bg-white">@@ -379,9 +490,9 @@ export const StepList = (props: {/>{props.minimized ? (<ExpandAllButton- disabled={!hasAnySpans}+ disabled={!displaySpansCol}isExpanded={intentionExpandAll}- toggleExpandAll={toggleExpandAll}+ toggleExpandAll={toggleExpandAllTraces}/>) : (<></>@@ -404,16 +515,30 @@ export const StepList = (props: {<span className="flex justify-end">Ran</span></TableHeader><TableHeader>Duration</TableHeader>- <TableHeader>- <div className="flex flex-row items-center gap-2">- <ExpandAllButton- disabled={!hasAnySpans}- isExpanded={intentionExpandAll}- toggleExpandAll={toggleExpandAll}- />- Spans- </div>- </TableHeader>+ {displaySpansCol && (+ <TableHeader>+ <div className="flex flex-row items-center gap-2">+ <ExpandAllButton+ disabled={!displaySpansCol}+ isExpanded={intentionExpandAll}+ toggleExpandAll={toggleExpandAllTraces}+ />+ Spans+ </div>+ </TableHeader>+ )}+ {displayLinksCol && (+ <TableHeader>+ <div className="flex flex-row items-center gap-2">+ <ExpandAllButton+ disabled={!displaySpansCol}+ isExpanded={intentionExpandAll}+ toggleExpandAll={toggleExpandAllTraces}+ />+ Links+ </div>+ </TableHeader>+ )}<TableHeader><div className="flex flex-row items-center gap-2"><AutoRefreshSwitch@@ -430,20 +555,29 @@ export const StepList = (props: {{/* <div className="h-10"></div> */}<TableBody className="pt-10">{props.steps.map((step) => {+ const isTraceExpanded = traceExpandedActions.includes(step.step_start_log.sequence_id);+ const isLinksExpanded = linksExpandedActions.includes(step.step_start_log.sequence_id);+ const links = linksBySequenceID.get(step.step_start_log.sequence_id) || [];return (<><ActionTableRow+ key={`${step.step_start_log.sequence_id}-action-table-row`}step={step}currentHoverIndex={props.currentHoverIndex}setCurrentHoverIndex={props.setCurrentHoverIndex}currentSelectedIndex={props.currentSelectedIndex}setCurrentSelectedIndex={props.setCurrentSelectedIndex}numPriorIndices={props.numPriorIndices}- isExpanded={isExpanded(step.step_start_log.sequence_id)}- toggleExpanded={toggleExpanded}+ isTracesExpanded={isTraceExpanded}+ isLinksExpanded={isLinksExpanded}+ toggleTraceExpanded={toggleTraceExpandedActions}+ toggleLinksExpanded={toggleLinksExpanded}minimized={props.minimized}+ links={links}+ displaySpansCol={displaySpansCol}+ displayLinksCol={displayLinksCol}></ActionTableRow>- {isExpanded(step.step_start_log.sequence_id) && (+ {isTraceExpanded && (<TraceSubTablespans={step.spans}step={step}@@ -453,32 +587,41 @@ export const StepList = (props: {setCurrentSelectedIndex={props.setCurrentSelectedIndex}numPriorIndices={props.numPriorIndices}minimized={props.minimized}+ displaySpansCol={displaySpansCol}+ displayLinksCol={displayLinksCol}+ />+ )}+ {isLinksExpanded && (+ <LinkSubTable+ step={step}+ links={links}+ currentHoverIndex={props.currentHoverIndex}+ setCurrentHoverIndex={props.setCurrentHoverIndex}+ currentSelectedIndex={props.currentSelectedIndex}+ setCurrentSelectedIndex={props.setCurrentSelectedIndex}+ numPriorIndices={props.numPriorIndices}+ minimized={props.minimized}+ projectId={props.projectId}+ displaySpansCol={displaySpansCol}+ displayLinksCol={displayLinksCol}/>)}</>);})}{props.parentPointer ? (- <TableRow className="text-gray-500 cursor-pointer bg-gray-100">- <TableCell colSpan={1} className="items-center justify-start max-w-20">- {props.parentPointer.sequence_id}- </TableCell>- <TableCell colSpan={4} className="text-gray-500">- <div className="flex flex-row gap-2 items-center ">- <MdForkRight className="h-5 w-5 -ml-1.5" />- <Link to={`/project/${props.projectId}/${props.parentPointer.app_id}`}>- <span className="hover:underline">{props.parentPointer.app_id}</span>- </Link>- </div>- </TableCell>- <TableCell colSpan={1} className="text-gray-500">- <Chip- label={'forked'}- chipType={'fork'}- className="w-16 flex flex-row justify-center"- />- </TableCell>- </TableRow>+ <ParentLink parentPointer={props.parentPointer} projectId={props.projectId} type="fork" />+ ) : (+ <></>+ )}+ {props.spawningParentPointer ? (+ <ParentLink+ parentPointer={props.spawningParentPointer}+ projectId={props.projectId}+ type="spawn"+ displayLinksCol={displayLinksCol}+ displaySpansCol={displaySpansCol}+ />) : (<></>)}@@ -486,3 +629,39 @@ export const StepList = (props: {</Table>);};++const ParentLink = (props: {+ parentPointer: PointerModel;+ projectId: string;+ displayLinksCol?: boolean;+ displaySpansCol?: boolean;+ type: 'spawn' | 'fork';+}) => {+ const Icon = props.type === 'fork' ? TbGrillFork : TiFlowChildren;+ return (+ <TableRow className="text-gray-500 cursor-pointer bg-gray-100">+ <TableCell colSpan={1} className="items-center justify-center flex max-w-20">+ <Icon className="h-5 w-5 -ml-1.5" />+ </TableCell>+ <TableCell+ colSpan={3 + +!!props.displayLinksCol + +!!props.displaySpansCol}+ className="text-gray-500"+ >+ <div className="flex flex-row gap-1 items-center ">+ <Link to={`/project/${props.projectId}/${props.parentPointer.app_id}`}>+ <span className="hover:underline">{props.parentPointer.app_id}</span>+ </Link>+ <span>@</span>+ <span>{props.parentPointer.sequence_id}</span>+ </div>+ </TableCell>+ <TableCell colSpan={1} className="text-gray-500">+ <Chip+ label={'parent'}+ chipType={props.type}+ className="w-16 flex flex-row justify-center"+ />+ </TableCell>+ </TableRow>+ );+};diff --git a/tests/core/test_application.py b/tests/core/test_application.pyindex c51ac4b2..a08b5e8b 100644--- a/tests/core/test_application.py+++ b/tests/core/test_application.py@@ -2403,7 +2403,8 @@ def test_application_passes_context_when_declared():def test_optional_context_in_dependency_factories():"""Tests that the context is passed to the function correctly when nulled out.- TODO -- gret this to tests without instantiating an application."""+ TODO -- get this to test without instantiating an application through the builder --+ this is slightly overkill for a bit of code"""context_list = []@action(reads=["count"], writes=["count"])@@ -2431,3 +2432,42 @@ def test_optional_context_in_dependency_factories():assert (inputs["__context"].tracker.unique_id == "unique_tracker_name") # it should be the correct context+++def test_application_with_no_spawning_parent():+ """Test that the application does not have a spawning parent when it is not specified"""+ counter_action = base_counter_action.with_name("counter")+ result_action = Result("count").with_name("result")+ app = (+ ApplicationBuilder()+ .with_actions(counter_action, result_action)+ .with_transitions(("counter", "result", default))+ .with_identifiers(app_id="test123", partition_key="user123", sequence_id=5)+ .with_entrypoint("counter")+ .with_state(count=0)+ .build()+ )+ spawned_by = app.spawning_parent_pointer+ assert spawned_by is None+++def test_application_with_spawning_parent():+ """Tests that the application builder can specify a spawning+ parent and it gets wired through to the app."""+ counter_action = base_counter_action.with_name("counter")+ result_action = Result("count").with_name("result")+ app = (+ ApplicationBuilder()+ .with_actions(counter_action, result_action)+ .with_transitions(("counter", "result", default))+ .with_identifiers(app_id="test123", partition_key="user123")+ .with_spawning_parent(app_id="test123", partition_key="user123", sequence_id=5)+ .with_entrypoint("counter")+ .with_state(count=0)+ .build()+ )+ spawned_by = app.spawning_parent_pointer+ assert spawned_by is not None+ assert spawned_by.app_id == "test123"+ assert spawned_by.partition_key == "user123"+ assert spawned_by.sequence_id == 5diff --git a/tests/tracking/test_local_tracking_client.py b/tests/tracking/test_local_tracking_client.pyindex 335b974e..184e6a95 100644--- a/tests/tracking/test_local_tracking_client.py+++ b/tests/tracking/test_local_tracking_client.py@@ -16,6 +16,7 @@ from burr.tracking.common.models import (ApplicationModel,BeginEntryModel,BeginSpanModel,+ ChildApplicationModel,EndEntryModel,EndSpanModel,)@@ -31,7 +32,13 @@ def counter(state: State, __tracer: TracerFactory) -> Tuple[dict, State]:return result, state.update(**result)-def sample_application(project_name: str, log_dir: str, app_id: str, broken: bool = False):+def sample_application(+ project_name: str,+ log_dir: str,+ app_id: str,+ broken: bool = False,+ spawn_from: Tuple[Optional[str], Optional[int]] = (None, None),+):return (burr.core.ApplicationBuilder().with_state(counter=0, break_at=2 if broken else -1)@@ -43,6 +50,10 @@ def sample_application(project_name: str, log_dir: str, app_id: str, broken: boo.with_entrypoint("counter").with_tracker(project=project_name, tracker="local", params={"storage_dir": log_dir}).with_identifiers(app_id=app_id)+ .with_spawning_parent(+ app_id=spawn_from[0],+ sequence_id=spawn_from[1], # no need to test the partition key here really+ ).build())@@ -297,3 +308,49 @@ def test_multi_fork_tracking_client(tmpdir):forked_app_3.sequence_id == forked_app_2.sequence_id == 25) # this should pick up where the last one left offassert forked_app_3.parent_pointer.app_id == forked_forked_app_id+++def test_application_tracks_link_to_spawning_parent(tmpdir: str):+ """Tests that we record the parent of the spawned application in the metadata file for the spawned application."""+ app_id = str(uuid.uuid4())+ log_dir = os.path.join(tmpdir, "tracking_parent_test")+ project_name = "test_application_tracks_end_to_end_with_spawning_parent"+ # constructing this will cause the desired side-effect+ sample_application(project_name, log_dir, app_id, spawn_from=(f"spawn_{app_id}", 5))+ results_dir = os.path.join(log_dir, project_name, app_id)+ assert os.path.exists(results_dir)+ assert os.path.exists(+ metadata_output := os.path.join(results_dir, LocalTrackingClient.METADATA_FILENAME)+ )+ with open(metadata_output) as f:+ metadata = json.load(f)+ metadata_parsed = ApplicationMetadataModel.model_validate(metadata)+ assert metadata_parsed.spawning_parent_pointer.app_id == f"spawn_{app_id}"+ assert metadata_parsed.spawning_parent_pointer.sequence_id == 5+++def test_application_tracks_link_from_spawning_parent(tmpdir: str):+ """Tests that we record the child in the parent's directory when instantiated."""+ spawning_parent_app_id = str(uuid.uuid4())+ project_name = "test_application_tracks_link_from_spawning_parent"+ log_dir = os.path.join(tmpdir, "tracking_child_test")+ # creates the directory for the parent+ # technically not needed (it'll create an empty directory), but nice to have+ sample_application(project_name, log_dir, spawning_parent_app_id)+ parent_result_dir = os.path.join(log_dir, project_name, spawning_parent_app_id)+ spawned_children = [str(uuid.uuid4()), str(uuid.uuid4())]+ for child_app_id in spawned_children:+ # constructing this will cause the desired side effect -- crating the pointer to the child in the parent's directory+ sample_application(+ project_name, log_dir, child_app_id, spawn_from=(spawning_parent_app_id, 5)+ )+ assert os.path.exists(+ children_output := os.path.join(+ parent_result_dir, LocalTrackingClient.CHILDREN_FILENAME+ )+ )+ with open(children_output) as f:+ children = [json.loads(line) for line in f.readlines()]+ children_parsed = [ChildApplicationModel.model_validate(child) for child in children]+ assert set(child.child.app_id for child in children_parsed) == set(spawned_children)+ assert all(child.event_type == "spawn_start" for child in children_parsed)