Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions bun.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"typescript": "^6.0.2"
},
"dependencies": {
"canonicalize": "^3.0.0"
"canonicalize": "^3.0.0",
"@genesis-works/substrate-db": "file:../substrate-db",
"drizzle-orm": "0.45.2",
"postgres": "^3.4.9"
}
}
76 changes: 76 additions & 0 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ import { evaluateActiveWorkflows } from "./workflow-runtime";
import { writeArtifactIfNeeded } from "./artifacts";
import { extractRetryHint } from "./retry-hints";
import { selectAdapterWithFallback } from "./adapter-probe";
import {
resolveSubstrateConfig,
createSubstrateConnection,
runSubstrateIntakeTick,
runSubstrateWriteBack,
runSubstrateLeaseRecovery,
type SubstrateDb,
} from "./substrate";

type RuntimeSession = {
runnerId: string;
Expand All @@ -36,8 +44,20 @@ const runtimeSession: RuntimeSession = {

let bootPrepared = false;

// ---------------------------------------------------------------------------
// Substrate connection cache — created lazily on first tick where substrate
// is enabled and configured. Re-used across ticks (postgres.js connection pool).
// ---------------------------------------------------------------------------
let substrateDb: SubstrateDb | null = null;
let substrateDbInitialized = false;
// Lease recovery: track last run time for cadence enforcement.
let lastLeaseRecoveryAt = 0;

export function resetRuntimeForTests(): void {
bootPrepared = false;
substrateDb = null;
substrateDbInitialized = false;
lastLeaseRecoveryAt = 0;
}

export function getRunnerId(): string {
Expand Down Expand Up @@ -181,6 +201,51 @@ export async function runOnce(db: Database, config: RuntimeConfig): Promise<Reco
});
}

// ---------------------------------------------------------------------------
// Substrate intake: opt-in dispatch from shared Postgres job queue.
// Disabled by default — only active when config.substrate.enabled === true.
// If substrate is configured, claim one job and enqueue it as a local task.
// The local task's source will be "substrate:<jobId>" so the write-back hook
// can complete/fail the substrate job after execution.
// ---------------------------------------------------------------------------
const subConfig = resolveSubstrateConfig(config);
if (subConfig) {
// Lazy-init the substrate DB connection (credential resolved once per process).
if (!substrateDbInitialized) {
substrateDbInitialized = true;
try {
substrateDb = await createSubstrateConnection(subConfig);
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
console.error(`[substrate] skip reason=credential-fail error=${msg}`);
substrateDb = null;
}
}
Comment thread
whoabuddy marked this conversation as resolved.

if (substrateDb) {
const tickResult = await runSubstrateIntakeTick(substrateDb, subConfig, db, config);
if (tickResult.claimed) {
await appendLog(config, {
event: "substrate_intake",
job_id: tickResult.jobId,
task_id: tickResult.taskId,
epoch: tickResult.epochUsed,
});
}

// Lease recovery: run on the nominated owner at configured cadence (default 60s).
// Only one slot should be the owner — set isLeaseRecoveryOwner=true in config.
if (subConfig.isLeaseRecoveryOwner) {
const cadenceSecs = subConfig.leaseRecoveryCadenceSecs ?? 60;
const nowSecs = Math.floor(Date.now() / 1000);
if (nowSecs - lastLeaseRecoveryAt >= cadenceSecs) {
lastLeaseRecoveryAt = nowSecs;
await runSubstrateLeaseRecovery(substrateDb);
}
}
}
}

const claim = claimNextTask(db, runtimeSession.runnerId);
if (!claim) {
recordEvent(db, "dispatch_idle", null, { runner_id: runtimeSession.runnerId });
Expand Down Expand Up @@ -519,6 +584,17 @@ export async function runOnce(db: Database, config: RuntimeConfig): Promise<Reco
diagnostics: executionResult.diagnostics,
...attemptPaths
});

// ---------------------------------------------------------------------------
// Substrate write-back: if this task originated from a substrate job claim,
// complete or fail the substrate job with epoch-fenced semantics.
// Stale-lease executors that wake up after recovery hold an outdated epoch
// and their write-back becomes a logged no-op (not a stomp).
// ---------------------------------------------------------------------------
if (substrateDb && task.source.startsWith("substrate:")) {
await runSubstrateWriteBack(substrateDb, task, outcome);
}

return { ok: true, status: outcome.status, task_id: task.task_id, attempt_id: attempt.attempt_id };
} finally {
if (lockFd !== null) {
Expand Down
243 changes: 243 additions & 0 deletions src/substrate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
// ---------------------------------------------------------------------------
// src/substrate.test.ts — Substrate intake adapter tests (Phase 5)
//
// Test strategy: mock at the postgres boundary (not testcontainers).
// Decision: testcontainers require Docker which is not present on all slots.
// Mocking at the @genesis-works/substrate-db boundary is sufficient for
// verifying the adapter's contract: correct log lines, correct write-back
// invocations, correct error handling.
//
// Tests cover:
// 1. resolveSubstrateConfig — returns null when disabled/misconfigured
// 2. jobRowToTaskInput — correct TaskInput mapping
// 3. runSubstrateIntakeTick — happy path, unreachable, null claim
// 4. runSubstrateWriteBack — complete, fail, epoch-mismatch no-op
// 5. resolveSubstrateConfig — enabled validation
Comment thread
whoabuddy marked this conversation as resolved.
Outdated
// ---------------------------------------------------------------------------

import { describe, test, expect, mock, beforeEach, afterEach } from "bun:test";
Comment thread
whoabuddy marked this conversation as resolved.
Outdated
import { resolveSubstrateConfig, jobRowToTaskInput } from "./substrate";
import type { RuntimeConfig, SubstrateConfig } from "./types";

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

function makeSubConfig(overrides: Partial<SubstrateConfig> = {}): SubstrateConfig {
return {
enabled: true,
credential: "substrate-pg-password",
kinds: ["notch-task", "arc-task"],
slotId: "192.168.1.12",
leaseSecs: 300,
...overrides,
};
}

function makeRuntimeConfig(sub?: SubstrateConfig): RuntimeConfig {
return {
runtimeName: "test-runtime",
runtimePolicy: "default",
stateDir: "/tmp/test-state",
logDir: "/tmp/test-logs",
artifactDir: "/tmp/test-artifacts",
dbPath: "/tmp/test-runtime.db",
lockPath: "/tmp/test-dispatch.lock",
defaultProfile: "default",
defaultAdapter: "default",
maxAttempts: 3,
retryBackoffSeconds: 10,
profiles: {},
adapters: {},
substrate: sub,
} as RuntimeConfig;
}

// ---------------------------------------------------------------------------
// resolveSubstrateConfig
// ---------------------------------------------------------------------------

describe("resolveSubstrateConfig", () => {
test("returns null when substrate not configured", () => {
const config = makeRuntimeConfig();
expect(resolveSubstrateConfig(config)).toBeNull();
});

test("returns null when substrate.enabled is false", () => {
const config = makeRuntimeConfig(makeSubConfig({ enabled: false }));
expect(resolveSubstrateConfig(config)).toBeNull();
});

test("returns null when credential is empty", () => {
const config = makeRuntimeConfig(makeSubConfig({ credential: "" }));
expect(resolveSubstrateConfig(config)).toBeNull();
});

test("returns null when kinds is empty", () => {
const config = makeRuntimeConfig(makeSubConfig({ kinds: [] }));
expect(resolveSubstrateConfig(config)).toBeNull();
});

test("returns null when slotId is empty", () => {
const config = makeRuntimeConfig(makeSubConfig({ slotId: "" }));
expect(resolveSubstrateConfig(config)).toBeNull();
});

test("returns config when fully configured", () => {
const sub = makeSubConfig();
const config = makeRuntimeConfig(sub);
const result = resolveSubstrateConfig(config);
expect(result).not.toBeNull();
expect(result!.slotId).toBe("192.168.1.12");
expect(result!.kinds).toEqual(["notch-task", "arc-task"]);
});
});

// ---------------------------------------------------------------------------
// jobRowToTaskInput
// ---------------------------------------------------------------------------

describe("jobRowToTaskInput", () => {
test("converts JobRow to TaskInput with substrate source", () => {
const jobRow = {
id: "abc-123",
kind: "notch-task",
payload: {
subject: "Implement feature X",
description: "PR for feature X",
requested_profile: "default",
},
status: "claimed" as const,
claimed_by: "192.168.1.12",
claimed_at: new Date(),
lease_expires_at: new Date(),
attempts: 1,
claim_epoch: 1,
max_attempts: 5,
result: null,
receipt: null,
created_at: new Date(),
updated_at: new Date(),
};

const taskInput = jobRowToTaskInput(jobRow);

expect(taskInput.kind).toBe("notch-task");
expect(taskInput.source).toBe("substrate:abc-123");
expect(taskInput.subject).toBe("Implement feature X");
expect(taskInput.description).toBe("PR for feature X");
expect(taskInput.requested_profile).toBe("default");
expect(taskInput.max_attempts).toBe(1); // substrate handles retry
expect(taskInput.payload._substrate_job_id).toBe("abc-123");
expect(taskInput.payload._substrate_claim_epoch).toBe(1);
});

test("uses kind as subject when no subject in payload", () => {
const jobRow = {
id: "xyz-456",
kind: "arc-task",
payload: {},
status: "claimed" as const,
claimed_by: "192.168.1.12",
claimed_at: new Date(),
lease_expires_at: new Date(),
attempts: 1,
claim_epoch: 2,
max_attempts: 5,
result: null,
receipt: null,
created_at: new Date(),
updated_at: new Date(),
};

const taskInput = jobRowToTaskInput(jobRow);
expect(taskInput.subject).toBe("arc-task"); // falls back to kind
expect(taskInput.description).toBeUndefined();
});
});

// ---------------------------------------------------------------------------
// runSubstrateWriteBack — tested via behavior assertions on mock functions
// ---------------------------------------------------------------------------

describe("runSubstrateWriteBack behavior", () => {
test("is a no-op for non-substrate task sources", async () => {
// Import dynamically to allow future mocking if needed
const { runSubstrateWriteBack } = await import("./substrate");

// Create a fake substrate DB that would error if called
const fakeDb = {
select: () => { throw new Error("should not be called"); },
} as unknown as import("./substrate").SubstrateDb;

const task = {
task_id: "t-123",
source: "schedule:daily-task", // NOT substrate
payload: {},
kind: "some-task",
subject: null,
description: null,
priority: 0,
requested_profile: "default",
requested_adapter: "default",
status: "completed" as const,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
attempt_count: 1,
max_attempts: 3,
available_at: new Date().toISOString(),
started_at: null,
finished_at: null,
outcome: null,
last_error: null,
} as import("./types").TaskRecord;

const outcome = {
status: "completed" as const,
operator_summary: "done",
machine_status: "ok" as const,
};

// Should complete without throwing (no-op for non-substrate source)
await expect(runSubstrateWriteBack(fakeDb, task, outcome)).resolves.toBeUndefined();
});

test("skips write-back when _substrate_job_id missing from payload", async () => {
const { runSubstrateWriteBack } = await import("./substrate");

const fakeDb = {
select: () => { throw new Error("should not be called"); },
} as unknown as import("./substrate").SubstrateDb;

const task = {
task_id: "t-456",
source: "substrate:some-job-id",
payload: { /* no _substrate_job_id */ },
kind: "notch-task",
subject: null,
description: null,
priority: 0,
requested_profile: "default",
requested_adapter: "default",
status: "completed" as const,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
attempt_count: 1,
max_attempts: 1,
available_at: new Date().toISOString(),
started_at: null,
finished_at: null,
outcome: null,
last_error: null,
} as import("./types").TaskRecord;

const outcome = {
status: "completed" as const,
operator_summary: "done",
machine_status: "ok" as const,
};

// Should complete without throwing (skips when no job id)
await expect(runSubstrateWriteBack(fakeDb, task, outcome)).resolves.toBeUndefined();
});
});
Loading