Skip to content
← Public packages

@kody/kody-factory

Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.

FollowFork

Files

cursor-launch.ts

147 lines · 3.5 KB · TypeScript
import { createAgent } from 'kody:@kody/cursor/agents'
import { agentUrlOf, githubUrl } from './shared.ts'
import { recordLaunch, upsertCiDedupe } from './storage-port.ts'
import type { SpawnOrPreviewInput, SpawnOrPreviewResult } from './run-core.ts'

/**
 * Cursor launcher for automations that still use Cloud Agents
 * (legacy-reaper, keep-tests-tight, prod-ci-failure).
 */
export async function launchOrPreviewCursor(input: SpawnOrPreviewInput): Promise<SpawnOrPreviewResult> {
	const spawnedAt = new Date().toISOString()
	const meta = {
		outcome: input.outcome ?? null,
		files: input.files && input.files.length > 0 ? JSON.stringify(input.files) : null,
	}
	if (input.skipped) {
		if (!input.dryRun) {
			await recordLaunch({
				...meta,
				automation: input.automation,
				repo: input.repo,
				branch: input.branch,
				head_sha: input.headSha,
				agent_id: null,
				agent_url: null,
				spawned_at: spawnedAt,
				dry_run: 0,
				skipped: input.skipped,
			})
		}
		return {
			ok: true as const,
			automation: input.automation,
			repo: input.repo,
			branch: input.branch,
			dryRun: input.dryRun,
			skipped: input.skipped,
			prompt: input.prompt,
			promptChars: input.prompt.length,
			model: input.model,
			wouldSpawn: false,
		}
	}

	if (input.dryRun) {
		await recordLaunch({
			...meta,
			automation: input.automation,
			repo: input.repo,
			branch: input.branch,
			head_sha: input.headSha,
			agent_id: null,
			agent_url: null,
			spawned_at: spawnedAt,
			dry_run: 1,
			skipped: null,
		})
		return {
			ok: true as const,
			automation: input.automation,
			repo: input.repo,
			branch: input.branch,
			dryRun: true,
			skipped: null,
			prompt: input.prompt,
			promptChars: input.prompt.length,
			model: input.model,
			wouldSpawn: true,
			repository: githubUrl(input.repo),
			ref: input.branch,
		}
	}

	try {
		const created = await createAgent({
			prompt: { text: input.prompt },
			model: input.model,
			name: `${input.automation} ${input.repo}`.slice(0, 60),
			repository: githubUrl(input.repo),
			ref: input.branch,
		})
		const agent =
			(created as { agent?: { id?: string; url?: string } })?.agent ??
			(created as { id?: string; url?: string })
		const agentId = agent?.id || null
		const agentUrl = agentUrlOf(agent)
		await recordLaunch({
			...meta,
			automation: input.automation,
			repo: input.repo,
			branch: input.branch,
			head_sha: input.headSha,
			agent_id: agentId,
			agent_url: agentUrl,
			spawned_at: spawnedAt,
			dry_run: 0,
			skipped: null,
		})
		if (input.headSha) {
			await upsertCiDedupe({
				repo: input.repo,
				head_sha: input.headSha,
				agent_id: agentId,
				agent_url: agentUrl,
			})
		}
		return {
			ok: true as const,
			automation: input.automation,
			repo: input.repo,
			branch: input.branch,
			dryRun: false,
			skipped: null,
			spawned: true,
			agentId,
			agentUrl,
			promptChars: input.prompt.length,
			model: input.model,
		}
	} catch (error) {
		const message = error instanceof Error ? error.message : String(error)
		await recordLaunch({
			...meta,
			automation: input.automation,
			repo: input.repo,
			branch: input.branch,
			head_sha: input.headSha,
			agent_id: null,
			agent_url: null,
			spawned_at: spawnedAt,
			dry_run: 0,
			skipped: `spawn-failed:${message.slice(0, 180)}`,
			outcome: 'spawn-failed',
		})
		return {
			ok: false as const,
			automation: input.automation,
			repo: input.repo,
			branch: input.branch,
			dryRun: false,
			error: message,
			promptChars: input.prompt.length,
			model: input.model,
		}
	}
}