openvibers/openvibe.codes Commits
Route coding tasks to harness offers through the SDK placement planner (#6)
Changes
nnew file mode 100644@@@ -0,0 +1,83 @@++'use strict';++++/**++ * Harness placement: each harness × agent pair from the catalog (server/domain/harnesses.js) becomes one++ * openvibe-sdk/placement Offer, and the SDK planner picks where a coding task runs. Pure: no I/O.++ */++const contracts = require('openvibe-contracts');++const { plan } = require('openvibe-sdk/placement');++++// Every coding harness offers edit and review until task capabilities reach harness-offer@1.++const BASE_CAPABILITIES = ['edit', 'review'];++const HARNESS_CAPABILITIES = ['host_access', 'mcp', 'long_autonomy', 'resume'];++++const TASKS = Object.freeze({++ edit: ['edit'],++ review: ['review'],++ long: ['edit', 'long_autonomy'],++ resume: ['resume'],++});++++function toOffer(harness, agent, opts = {}) {++ const caps = harness.capabilities || {};++ const limits = harness.limits || {};++ const health = typeof opts.health === 'string' ? { status: opts.health } : opts.health || { status: 'up' };++ const offer = {++ offer_id: `${harness.id}:${agent.id || agent.model}`,++ kind: 'provider',++ provider: agent.provider || harness.provider,++ region: 'global',++ trust: opts.trust || 'external',++ capabilities: [...BASE_CAPABILITIES, ...HARNESS_CAPABILITIES.filter((name) => caps[name] === true)],++ capacity: { workers: { harness: limits.max_concurrent_runs } },++ health,++ pricing: { model: 'per-operation', unit: 'token', marginal_usd_per_unit: agent.price_per_1k_tokens.fresh_usd / 1000 },++ // Not an SDK Offer field: the planner ignores it; kept for callers sizing a run.++ max_context_tokens: (agent.context_limits && agent.context_limits.input_tokens) ?? limits.max_context_tokens,++ };++ if (opts.latencyMs != null) offer.latency_ms = { run_p95: opts.latencyMs };++ return offer;++}++++function requirementsFor(task, extra = {}) {++ if (!Object.prototype.hasOwnProperty.call(TASKS, task)) {++ const error = new Error(`unknown harness task: ${task}`);++ error.code = 'harness.task_unknown';++ throw error;++ }++ const req = {++ kind: `harness.${task}`,++ mobility: 'job',++ latency_class: 'interactive',++ objective: 'balanced',++ ...extra,++ capabilities: [...new Set([...TASKS[task], ...(extra.capabilities || [])])],++ };++ if (typeof req.trust === 'string') req.trust = [req.trust];++ return req;++}++++function route({ task, extra = {}, harnesses, opts = {}, now = Date.now() }) {++ const req = requirementsFor(task, extra);++ const offers = harnesses.list().flatMap((harness) => harnesses.agents(harness.id).map((agent) => {++ const id = `${harness.id}:${agent.id || agent.model}`;++ return toOffer(harness, agent, opts[id] || {});++ }));++ const result = plan(req, offers, { now });++ // placement-result@1 types `selected` as a string, so a plan with no eligible candidate cannot validate.++ if (result.selected !== null) {++ const check = contracts.validate('platform.placement-result@1', JSON.parse(JSON.stringify(result)));++ if (!check.valid) {++ const error = new Error(`invalid placement result: ${JSON.stringify(check.errors)}`);++ error.code = 'harness.placement_invalid';++ throw error;++ }++ }++ return {++ selected: result.selected,++ reasons: result.reasons,++ candidates: result.candidates.map((c) => ({ id: c.id, included: c.eligible, reason: c.excluded_because || null })),++ };++}++++module.exports = { TASKS, toOffer, requirementsFor, route };nnew file mode 100644@@@ -0,0 +1,114 @@++'use strict';++++const assert = require('assert');++const fs = require('fs');++const path = require('path');++const { createHarnesses } = require('../server/domain/harnesses');++const { TASKS, toOffer, requirementsFor, route } = require('../server/domain/harness-placement');++const { check, done } = require('./helpers/boot');++++const catalogPath = path.join(__dirname, '..', 'server', 'data', 'harness-offers.json');++const rows = JSON.parse(fs.readFileSync(catalogPath, 'utf8'));++const now = Date.parse('2026-10-02T12:00:00Z');++++function stub(list) {++ return { list: () => list, agents: (id) => (list.find((row) => row.id === id) || {}).agents || [] };++}++++function priced(row, freshUsd) {++ const agent = { ...row.agents[0], price_per_1k_tokens: { ...row.agents[0].price_per_1k_tokens, fresh_usd: freshUsd } };++ return { ...row, agents: [agent] };++}++++(async () => {++ await check('toOffer maps a seed harness and agent field by field', async () => {++ const harness = rows.find((row) => row.id === 'command-code');++ const agent = {++ ...rows[0].agents[0],++ id: 'command-code-test',++ harness: 'command-code',++ provider: 'example',++ context_limits: { input_tokens: 100000, output_tokens: 8000 },++ price_per_1k_tokens: { fresh_usd: 0.003, cached_usd: 0.0003, output_usd: 0.015 },++ };++ assert.deepStrictEqual(toOffer(harness, agent, { trust: 'partner', health: 'degraded', latencyMs: 900 }), {++ offer_id: 'command-code:command-code-test',++ kind: 'provider',++ provider: 'example',++ region: 'global',++ trust: 'partner',++ capabilities: ['edit', 'review', 'host_access', 'mcp', 'resume'],++ capacity: { workers: { harness: 2 } },++ health: { status: 'degraded' },++ pricing: { model: 'per-operation', unit: 'token', marginal_usd_per_unit: 0.003 / 1000 },++ max_context_tokens: 100000,++ latency_ms: { run_p95: 900 },++ });++++ const deepseek = rows.find((row) => row.id === 'deepseek');++ const offer = toOffer(deepseek, deepseek.agents[0]);++ assert.strictEqual(offer.offer_id, 'deepseek:deepseek-chat');++ assert.deepStrictEqual(offer.capabilities, ['edit', 'review']);++ assert.strictEqual(offer.trust, 'external');++ assert.deepStrictEqual(offer.health, { status: 'up' });++ assert.strictEqual('latency_ms' in offer, false);++ assert.strictEqual(offer.max_context_tokens, 64000);++ assert.strictEqual(toOffer(deepseek, { ...deepseek.agents[0], id: undefined }).offer_id, `deepseek:${deepseek.agents[0].model}`);++ });++++ await check('requirementsFor maps each task and merges extras', async () => {++ assert.deepStrictEqual(requirementsFor('long').capabilities, TASKS.long);++ const req = requirementsFor('edit', { trust: 'first-party', capabilities: ['mcp'] });++ assert.deepStrictEqual(req.capabilities, ['edit', 'mcp']);++ assert.deepStrictEqual(req.trust, ['first-party']);++ assert.strictEqual(req.objective, 'balanced');++ });++++ await check('a review task selects a review-capable offer', async () => {++ const harnesses = createHarnesses({ catalogPath });++ const r = route({ task: 'review', harnesses, now });++ assert.ok(r.selected);++ const [harnessId] = r.selected.split(':');++ assert.ok(toOffer(harnesses.get(harnessId), harnesses.agents(harnessId)[0]).capabilities.includes('review'));++ assert.ok(r.reasons.length);++ assert.deepStrictEqual(r.candidates.map((c) => c.id), ['claude-code:claude-code-sonnet', 'codex:codex-default', 'deepseek:deepseek-chat']);++ });++++ await check('an offer whose health is down is excluded with a reason', async () => {++ const harnesses = createHarnesses({ catalogPath });++ const r = route({ task: 'edit', harnesses, now, opts: { 'claude-code:claude-code-sonnet': { health: 'down' } } });++ assert.deepStrictEqual(r.candidates.find((c) => c.id === 'claude-code:claude-code-sonnet'), { id: 'claude-code:claude-code-sonnet', included: false, reason: 'health down' });++ assert.notStrictEqual(r.selected, 'claude-code:claude-code-sonnet');++ });++++ await check('first-party trust excludes every external offer', async () => {++ const harnesses = createHarnesses({ catalogPath });++ const r = route({ task: 'edit', extra: { trust: 'first-party' }, harnesses, now, opts: { 'codex:codex-default': { trust: 'first-party' } } });++ assert.strictEqual(r.selected, 'codex:codex-default');++ for (const c of r.candidates.filter((c) => c.id !== 'codex:codex-default')) {++ assert.deepStrictEqual(c, { id: c.id, included: false, reason: 'workload requires first-party trust' });++ }++ const none = route({ task: 'edit', extra: { trust: ['first-party'] }, harnesses, now });++ assert.strictEqual(none.selected, null);++ assert.ok(none.candidates.every((c) => !c.included));++ });++++ await check('with equal latency the cheaper eligible offer wins', async () => {++ const [claude, codex] = rows;++ const harnesses = stub([priced(claude, 0.015), priced(codex, 0.003)]);++ const opts = { 'claude-code:claude-code-sonnet': { latencyMs: 500 }, 'codex:codex-default': { latencyMs: 500 } };++ const r = route({ task: 'edit', harnesses, opts, now });++ assert.strictEqual(r.selected, 'codex:codex-default');++ assert.ok(r.candidates.every((c) => c.included));++ const flipped = route({ task: 'edit', harnesses: stub([priced(claude, 0.001), priced(codex, 0.003)]), opts, now });++ assert.strictEqual(flipped.selected, 'claude-code:claude-code-sonnet');++ });++++ await check('an unknown task throws harness.task_unknown', async () => {++ assert.throws(() => requirementsFor('deploy'), (error) => error.code === 'harness.task_unknown');++ assert.throws(() => requirementsFor('toString'), (error) => error.code === 'harness.task_unknown');++ assert.throws(() => route({ task: 'deploy', harnesses: createHarnesses({ catalogPath }), now }), (error) => error.code === 'harness.task_unknown');++ });++++ done();++})();
new file mode 100644
new file mode 100644
@@ -0,0 +1,83 @@
@@ -0,0 +1,83 @@
+'use strict';
+
+/**
+ * Harness placement: each harness × agent pair from the catalog (server/domain/harnesses.js) becomes one
+ * openvibe-sdk/placement Offer, and the SDK planner picks where a coding task runs. Pure: no I/O.
+ */
+const contracts = require('openvibe-contracts');
+const { plan } = require('openvibe-sdk/placement');
+
+// Every coding harness offers edit and review until task capabilities reach harness-offer@1.
+const BASE_CAPABILITIES = ['edit', 'review'];
+const HARNESS_CAPABILITIES = ['host_access', 'mcp', 'long_autonomy', 'resume'];
+
+const TASKS = Object.freeze({
+ edit: ['edit'],
+ review: ['review'],
+ long: ['edit', 'long_autonomy'],
+ resume: ['resume'],
+});
+
+function toOffer(harness, agent, opts = {}) {
+ const caps = harness.capabilities || {};
+ const limits = harness.limits || {};
+ const health = typeof opts.health === 'string' ? { status: opts.health } : opts.health || { status: 'up' };
+ const offer = {
+ offer_id: `${harness.id}:${agent.id || agent.model}`,
+ kind: 'provider',
+ provider: agent.provider || harness.provider,
+ region: 'global',
+ trust: opts.trust || 'external',
+ capabilities: [...BASE_CAPABILITIES, ...HARNESS_CAPABILITIES.filter((name) => caps[name] === true)],
+ capacity: { workers: { harness: limits.max_concurrent_runs } },
+ health,
+ pricing: { model: 'per-operation', unit: 'token', marginal_usd_per_unit: agent.price_per_1k_tokens.fresh_usd / 1000 },
+ // Not an SDK Offer field: the planner ignores it; kept for callers sizing a run.
+ max_context_tokens: (agent.context_limits && agent.context_limits.input_tokens) ?? limits.max_context_tokens,
+ };
+ if (opts.latencyMs != null) offer.latency_ms = { run_p95: opts.latencyMs };
+ return offer;
+}
+
+function requirementsFor(task, extra = {}) {
+ if (!Object.prototype.hasOwnProperty.call(TASKS, task)) {
+ const error = new Error(`unknown harness task: ${task}`);
+ error.code = 'harness.task_unknown';
+ throw error;
+ }
+ const req = {
+ kind: `harness.${task}`,
+ mobility: 'job',
+ latency_class: 'interactive',
+ objective: 'balanced',
+ ...extra,
+ capabilities: [...new Set([...TASKS[task], ...(extra.capabilities || [])])],
+ };
+ if (typeof req.trust === 'string') req.trust = [req.trust];
+ return req;
+}
+
+function route({ task, extra = {}, harnesses, opts = {}, now = Date.now() }) {
+ const req = requirementsFor(task, extra);
+ const offers = harnesses.list().flatMap((harness) => harnesses.agents(harness.id).map((agent) => {
+ const id = `${harness.id}:${agent.id || agent.model}`;
+ return toOffer(harness, agent, opts[id] || {});
+ }));
+ const result = plan(req, offers, { now });
+ // placement-result@1 types `selected` as a string, so a plan with no eligible candidate cannot validate.
+ if (result.selected !== null) {
+ const check = contracts.validate('platform.placement-result@1', JSON.parse(JSON.stringify(result)));
+ if (!check.valid) {
+ const error = new Error(`invalid placement result: ${JSON.stringify(check.errors)}`);
+ error.code = 'harness.placement_invalid';
+ throw error;
+ }
+ }
+ return {
+ selected: result.selected,
+ reasons: result.reasons,
+ candidates: result.candidates.map((c) => ({ id: c.id, included: c.eligible, reason: c.excluded_because || null })),
+ };
+}
+
+module.exports = { TASKS, toOffer, requirementsFor, route };
new file mode 100644
new file mode 100644
@@ -0,0 +1,114 @@
@@ -0,0 +1,114 @@
+'use strict';
+
+const assert = require('assert');
+const fs = require('fs');
+const path = require('path');
+const { createHarnesses } = require('../server/domain/harnesses');
+const { TASKS, toOffer, requirementsFor, route } = require('../server/domain/harness-placement');
+const { check, done } = require('./helpers/boot');
+
+const catalogPath = path.join(__dirname, '..', 'server', 'data', 'harness-offers.json');
+const rows = JSON.parse(fs.readFileSync(catalogPath, 'utf8'));
+const now = Date.parse('2026-10-02T12:00:00Z');
+
+function stub(list) {
+ return { list: () => list, agents: (id) => (list.find((row) => row.id === id) || {}).agents || [] };
+}
+
+function priced(row, freshUsd) {
+ const agent = { ...row.agents[0], price_per_1k_tokens: { ...row.agents[0].price_per_1k_tokens, fresh_usd: freshUsd } };
+ return { ...row, agents: [agent] };
+}
+
+(async () => {
+ await check('toOffer maps a seed harness and agent field by field', async () => {
+ const harness = rows.find((row) => row.id === 'command-code');
+ const agent = {
+ ...rows[0].agents[0],
+ id: 'command-code-test',
+ harness: 'command-code',
+ provider: 'example',
+ context_limits: { input_tokens: 100000, output_tokens: 8000 },
+ price_per_1k_tokens: { fresh_usd: 0.003, cached_usd: 0.0003, output_usd: 0.015 },
+ };
+ assert.deepStrictEqual(toOffer(harness, agent, { trust: 'partner', health: 'degraded', latencyMs: 900 }), {
+ offer_id: 'command-code:command-code-test',
+ kind: 'provider',
+ provider: 'example',
+ region: 'global',
+ trust: 'partner',
+ capabilities: ['edit', 'review', 'host_access', 'mcp', 'resume'],
+ capacity: { workers: { harness: 2 } },
+ health: { status: 'degraded' },
+ pricing: { model: 'per-operation', unit: 'token', marginal_usd_per_unit: 0.003 / 1000 },
+ max_context_tokens: 100000,
+ latency_ms: { run_p95: 900 },
+ });
+
+ const deepseek = rows.find((row) => row.id === 'deepseek');
+ const offer = toOffer(deepseek, deepseek.agents[0]);
+ assert.strictEqual(offer.offer_id, 'deepseek:deepseek-chat');
+ assert.deepStrictEqual(offer.capabilities, ['edit', 'review']);
+ assert.strictEqual(offer.trust, 'external');
+ assert.deepStrictEqual(offer.health, { status: 'up' });
+ assert.strictEqual('latency_ms' in offer, false);
+ assert.strictEqual(offer.max_context_tokens, 64000);
+ assert.strictEqual(toOffer(deepseek, { ...deepseek.agents[0], id: undefined }).offer_id, `deepseek:${deepseek.agents[0].model}`);
+ });
+
+ await check('requirementsFor maps each task and merges extras', async () => {
+ assert.deepStrictEqual(requirementsFor('long').capabilities, TASKS.long);
+ const req = requirementsFor('edit', { trust: 'first-party', capabilities: ['mcp'] });
+ assert.deepStrictEqual(req.capabilities, ['edit', 'mcp']);
+ assert.deepStrictEqual(req.trust, ['first-party']);
+ assert.strictEqual(req.objective, 'balanced');
+ });
+
+ await check('a review task selects a review-capable offer', async () => {
+ const harnesses = createHarnesses({ catalogPath });
+ const r = route({ task: 'review', harnesses, now });
+ assert.ok(r.selected);
+ const [harnessId] = r.selected.split(':');
+ assert.ok(toOffer(harnesses.get(harnessId), harnesses.agents(harnessId)[0]).capabilities.includes('review'));
+ assert.ok(r.reasons.length);
+ assert.deepStrictEqual(r.candidates.map((c) => c.id), ['claude-code:claude-code-sonnet', 'codex:codex-default', 'deepseek:deepseek-chat']);
+ });
+
+ await check('an offer whose health is down is excluded with a reason', async () => {
+ const harnesses = createHarnesses({ catalogPath });
+ const r = route({ task: 'edit', harnesses, now, opts: { 'claude-code:claude-code-sonnet': { health: 'down' } } });
+ assert.deepStrictEqual(r.candidates.find((c) => c.id === 'claude-code:claude-code-sonnet'), { id: 'claude-code:claude-code-sonnet', included: false, reason: 'health down' });
+ assert.notStrictEqual(r.selected, 'claude-code:claude-code-sonnet');
+ });
+
+ await check('first-party trust excludes every external offer', async () => {
+ const harnesses = createHarnesses({ catalogPath });
+ const r = route({ task: 'edit', extra: { trust: 'first-party' }, harnesses, now, opts: { 'codex:codex-default': { trust: 'first-party' } } });
+ assert.strictEqual(r.selected, 'codex:codex-default');
+ for (const c of r.candidates.filter((c) => c.id !== 'codex:codex-default')) {
+ assert.deepStrictEqual(c, { id: c.id, included: false, reason: 'workload requires first-party trust' });
+ }
+ const none = route({ task: 'edit', extra: { trust: ['first-party'] }, harnesses, now });
+ assert.strictEqual(none.selected, null);
+ assert.ok(none.candidates.every((c) => !c.included));
+ });
+
+ await check('with equal latency the cheaper eligible offer wins', async () => {
+ const [claude, codex] = rows;
+ const harnesses = stub([priced(claude, 0.015), priced(codex, 0.003)]);
+ const opts = { 'claude-code:claude-code-sonnet': { latencyMs: 500 }, 'codex:codex-default': { latencyMs: 500 } };
+ const r = route({ task: 'edit', harnesses, opts, now });
+ assert.strictEqual(r.selected, 'codex:codex-default');
+ assert.ok(r.candidates.every((c) => c.included));
+ const flipped = route({ task: 'edit', harnesses: stub([priced(claude, 0.001), priced(codex, 0.003)]), opts, now });
+ assert.strictEqual(flipped.selected, 'claude-code:claude-code-sonnet');
+ });
+
+ await check('an unknown task throws harness.task_unknown', async () => {
+ assert.throws(() => requirementsFor('deploy'), (error) => error.code === 'harness.task_unknown');
+ assert.throws(() => requirementsFor('toString'), (error) => error.code === 'harness.task_unknown');
+ assert.throws(() => route({ task: 'deploy', harnesses: createHarnesses({ catalogPath }), now }), (error) => error.code === 'harness.task_unknown');
+ });
+
+ done();
+})();