Welcome to the background project! In this first chapter, we are going to explore the foundation of running tasks remotely: the Remote Session Model.
Imagine you have a heavy task you want to run, like a complex build process or a data migration that takes an hour. You don't want this running on your local machine, slowing down your editor, or stopping if you close your laptop. You want to "teleport" this task to a remote server.
But here is the problem: Once you send the task away, how do you keep track of it?
We need a digital "receipt" or a "handle" that represents that remote process on our local machine. That is exactly what the Remote Session Model is.
Think of the Remote Session Model as a Dry Cleaning Ticket.
In our code, this "ticket" is a TypeScript object that holds all the essential information about the background task.
Let's look at the actual definition of this ticket. It is defined as BackgroundRemoteSession.
Here is the simplified structure of our session model.
// File: remote/remoteSession.ts
export type BackgroundRemoteSession = {
id: string // The unique ticket number
command: string // What we are doing (e.g., "npm install")
startTime: number // When we dropped it off
// ... status and logs below
}
Just like a laundry ticket might be stamped "Washing," "Pressing," or "Done," our session has a specific set of statuses.
// File: remote/remoteSession.ts
export type BackgroundRemoteSession = {
// ... previous fields
status: 'starting' | 'running' | 'completed' | 'failed' | 'killed'
log: SDKMessage[] // The output text from the terminal
// ... other metadata
}
Before we can create this "ticket," the system needs to verify that the shop is open and we are allowed to use it.
BackgroundRemoteSession object (the ticket).The code doesn't just define the type; it also includes a function to check if we can create a model. This is called Eligibility.
While we will cover the logic of checking these rules in detail in Session Eligibility Gatekeeper, it is important to see the entry point here.
The function checkBackgroundRemoteSessionEligibility acts as the bouncer. It returns a list of reasons why a session cannot be created. If the list is empty, we are good to go.
// File: remote/remoteSession.ts
export async function checkBackgroundRemoteSessionEligibility({
skipBundle = false,
}: { skipBundle?: boolean } = {}) {
const errors: BackgroundRemoteSessionPrecondition[] = []
// ... Logic to check policies, login, and git repo ...
return errors // Returns empty array [] if eligible
}
This function coordinates with several other components to make its decision:
We will dive into how these specific checks work in the next chapter.
In this chapter, we learned:
id, status, and log.Now that we understand what the "ticket" looks like, we need to understand the security guard checking us at the door.
Next Chapter: Session Eligibility Gatekeeper
Generated by Code IQ