-
Notifications
You must be signed in to change notification settings - Fork 10
Handle idle project runtimes #312
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
8 commits
Select commit
Hold shift + click to select a range
df4d2a5
Handle idle project runtimes
skulidropek 09c8174
Deduplicate project resource request types
skulidropek a06e2c5
Split Playwright browser runtime template
skulidropek 9e50263
Address browser runtime review feedback
skulidropek 9436a37
Handle browser volume creation failures
skulidropek 5e125ba
Harden browser image build runtime
skulidropek 4234a74
Expose browser build failure output
skulidropek fcb764a
Stabilize docker access API test
skulidropek 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
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
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,191 @@ | ||
| import { listProjectItems, readProjectRuntimeState, recordProjectRuntimeActivity } from "@effect-template/lib" | ||
| import type { ProjectItem } from "@effect-template/lib/usecases/projects" | ||
| import { Duration, Effect, Match, Schedule } from "effect" | ||
|
|
||
| import { activeAgents } from "./container-tasks-core.js" | ||
| import { readContainerTaskSnapshot } from "./container-tasks.js" | ||
| import { hasLiveProjectBrowserSession } from "./project-browser.js" | ||
| import { decideProjectIdleAction } from "./project-idle-policy.js" | ||
| import { applyProjectResourceProfile, suspendProjectRuntime } from "./project-lifecycle-resources.js" | ||
| import { loadProjectRuntimeByProject, runtimeForProject } from "./project-runtime.js" | ||
| import { hasLiveProjectSkillerSession } from "./skiller.js" | ||
| import { hasLiveProjectTerminalSession } from "./terminal-sessions.js" | ||
|
|
||
| export type ProjectAutoSuspendConfig = { | ||
| readonly enabled: boolean | ||
| readonly idleTimeoutMs: number | ||
| readonly scanIntervalMs: number | ||
| readonly throttleInteractiveIdle: boolean | ||
| readonly interactiveIdleCpuFactor: number | ||
| } | ||
|
|
||
| const minuteMs = 60_000 | ||
| const secondMs = 1_000 | ||
| const defaultIdleTimeoutMinutes = 30 | ||
| const defaultScanIntervalSeconds = 60 | ||
| const defaultInteractiveIdleCpuFactor = 0.5 | ||
|
|
||
| const parsePositiveIntegerEnv = ( | ||
| key: string, | ||
| defaultValue: number | ||
| ): number => { | ||
| const parsed = Number.parseInt(process.env[key] ?? "", 10) | ||
| return Number.isFinite(parsed) && parsed > 0 ? parsed : defaultValue | ||
| } | ||
|
|
||
| const parsePositiveFractionEnv = ( | ||
| key: string, | ||
| defaultValue: number | ||
| ): number => { | ||
| const parsed = Number(process.env[key] ?? "") | ||
| return Number.isFinite(parsed) && parsed > 0 && parsed <= 1 ? parsed : defaultValue | ||
| } | ||
|
|
||
| const parseEnabledEnv = ( | ||
| key: string, | ||
| defaultValue: boolean | ||
| ): boolean => { | ||
| const raw = process.env[key]?.trim().toLowerCase() | ||
| if (raw === undefined || raw.length === 0) { | ||
| return defaultValue | ||
| } | ||
| return raw !== "0" && raw !== "false" && raw !== "off" && raw !== "no" | ||
| } | ||
|
|
||
| export const resolveProjectAutoSuspendConfig = (): ProjectAutoSuspendConfig => ({ | ||
| enabled: parseEnabledEnv("DOCKER_GIT_AUTO_SUSPEND", true), | ||
| idleTimeoutMs: parsePositiveIntegerEnv("DOCKER_GIT_AGENT_IDLE_TIMEOUT_MINUTES", defaultIdleTimeoutMinutes) * minuteMs, | ||
| scanIntervalMs: parsePositiveIntegerEnv("DOCKER_GIT_IDLE_SCAN_INTERVAL_SECONDS", defaultScanIntervalSeconds) * secondMs, | ||
| throttleInteractiveIdle: parseEnabledEnv("DOCKER_GIT_INTERACTIVE_IDLE_THROTTLE", true), | ||
| interactiveIdleCpuFactor: parsePositiveFractionEnv( | ||
| "DOCKER_GIT_INTERACTIVE_IDLE_CPU_FACTOR", | ||
| defaultInteractiveIdleCpuFactor | ||
| ) | ||
| }) | ||
|
|
||
| const snapshotHasAgentTask = ( | ||
| project: ProjectItem | ||
| ) => | ||
| readContainerTaskSnapshot(project.projectDir, false).pipe( | ||
| Effect.map((snapshot) => | ||
| activeAgents(snapshot.agents).length > 0 || snapshot.tasks.some((task) => task.kind === "agent") | ||
| ), | ||
| Effect.catchAll(() => Effect.succeed(false)) | ||
| ) | ||
|
|
||
| const projectHasActiveAgent = ( | ||
| project: ProjectItem | ||
| ) => | ||
| snapshotHasAgentTask(project) | ||
|
|
||
| const projectHasLiveInteractiveSession = ( | ||
| project: ProjectItem, | ||
| sshSessions: number | ||
| ): boolean => | ||
| sshSessions > 0 || | ||
| hasLiveProjectTerminalSession(project.projectDir) || | ||
| hasLiveProjectBrowserSession(project.projectDir) || | ||
| hasLiveProjectSkillerSession(project.projectDir) | ||
|
|
||
| const runProjectIdleDecision = ( | ||
| project: ProjectItem, | ||
| config: ProjectAutoSuspendConfig, | ||
| running: boolean, | ||
| sshSessions: number, | ||
| startedAtEpochMs: number | null | ||
| ) => | ||
| Effect.gen(function*(_) { | ||
| const runtimeState = yield* _(readProjectRuntimeState(project.projectDir)) | ||
| const activeAgent = yield* _(projectHasActiveAgent(project)) | ||
| const liveInteractive = projectHasLiveInteractiveSession(project, sshSessions) | ||
| const decision = decideProjectIdleAction( | ||
| { | ||
| hasActiveAgent: activeAgent, | ||
| hasLiveInteractiveSession: liveInteractive, | ||
| lastAgentSeenAtEpochMs: runtimeState.lastAgentSeenAtEpochMs, | ||
| lastInteractiveSeenAtEpochMs: runtimeState.lastInteractiveSeenAtEpochMs, | ||
| resourceProfile: runtimeState.resourceProfile, | ||
| running, | ||
| startedAtEpochMs | ||
| }, | ||
| { | ||
| agentIdleTimeoutMs: config.idleTimeoutMs, | ||
| nowEpochMs: Date.now() | ||
| } | ||
| ) | ||
|
|
||
| return yield* _( | ||
| Match.value(decision).pipe( | ||
| Match.when({ _tag: "IgnoreStopped" }, () => Effect.void), | ||
| Match.when({ _tag: "KeepRunning" }, () => | ||
| activeAgent | ||
| ? recordProjectRuntimeActivity(project.projectDir, "agent").pipe(Effect.asVoid) | ||
| : Effect.void), | ||
| Match.when({ _tag: "RestoreNormalResources" }, () => | ||
| recordProjectRuntimeActivity(project.projectDir, "agent").pipe( | ||
| Effect.zipRight(applyProjectResourceProfile(project, "normal", config.interactiveIdleCpuFactor)) | ||
| )), | ||
| Match.when({ _tag: "ThrottleInteractiveIdle" }, () => | ||
| config.throttleInteractiveIdle | ||
| ? applyProjectResourceProfile( | ||
| project, | ||
| "interactive-idle-throttled", | ||
| config.interactiveIdleCpuFactor | ||
| ) | ||
| : Effect.void), | ||
| Match.when({ _tag: "SuspendIdle" }, () => | ||
| suspendProjectRuntime(project, "auto-suspend")), | ||
| Match.exhaustive | ||
| ) | ||
| ) | ||
| }).pipe( | ||
| Effect.catchAll((error) => | ||
| Effect.logWarning( | ||
| `[auto-suspend] Failed to evaluate ${project.containerName}: ${ | ||
| error instanceof Error ? error.message : String(error) | ||
| }` | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| export const scanProjectAutoSuspend = ( | ||
| config: ProjectAutoSuspendConfig | ||
| ) => | ||
| Effect.gen(function*(_) { | ||
| if (!config.enabled) { | ||
| return | ||
| } | ||
| const projects = yield* _(listProjectItems) | ||
| const runtimeByProject = yield* _(loadProjectRuntimeByProject(projects)) | ||
| yield* _( | ||
| Effect.forEach( | ||
| projects, | ||
| (project) => { | ||
| const runtime = runtimeForProject(runtimeByProject, project) | ||
| return runProjectIdleDecision( | ||
| project, | ||
| config, | ||
| runtime.running, | ||
| runtime.sshSessions, | ||
| runtime.startedAtEpochMs ?? project.lastStartedAtEpochMs | ||
| ) | ||
| }, | ||
| { concurrency: 2, discard: true } | ||
| ) | ||
| ) | ||
| }).pipe( | ||
| Effect.catchAll((error) => | ||
| Effect.logWarning( | ||
| `[auto-suspend] Scan failed: ${error instanceof Error ? error.message : String(error)}` | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| export const startProjectAutoSuspendLoop = ( | ||
| config: ProjectAutoSuspendConfig | ||
| ) => | ||
| config.enabled | ||
| ? scanProjectAutoSuspend(config).pipe( | ||
| Effect.repeat(Schedule.addDelay(Schedule.forever, () => Duration.millis(config.scanIntervalMs))) | ||
| ) | ||
| : Effect.log("docker-git auto-suspend disabled.") | ||
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.
Не считайте ошибку инспекции признаком idle-состояния.
Если
readContainerTaskSnapshot()падает,snapshotHasAgentTask()возвращаетfalse, и дальше scan может выбратьSuspendIdleдля проекта с реально работающим агентом. Здесь безопаснее пропускать решение для этого проекта и логировать warning, а не понижать состояние до “агента нет”.💡 Возможная правка
const snapshotHasAgentTask = ( project: ProjectItem ) => readContainerTaskSnapshot(project.projectDir, false).pipe( Effect.map((snapshot) => activeAgents(snapshot.agents).length > 0 || snapshot.tasks.some((task) => task.kind === "agent") - ), - Effect.catchAll(() => Effect.succeed(false)) + ) )🤖 Prompt for AI Agents