Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions runtime/flybot-workspace-web/features/agents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ export default async function agents(host) {
const binding = await endpoints['personal-binding']()
if (!binding) {
let bootstrap
try { bootstrap = loadPersonalAgentBootstrap() } catch {
try { bootstrap = loadPersonalAgentBootstrap(host.env) } catch {
throw new Error('Personal agent bootstrap configuration is unreadable')
}
if (bootstrap) {
Expand All @@ -1674,7 +1674,7 @@ export default async function agents(host) {
const timer = setTimeout(async () => {
if (binding) {
try {
const bootstrap = loadPersonalAgentBootstrap()
const bootstrap = loadPersonalAgentBootstrap(host.env)
if (bootstrap?.instance_id && bootstrap.instance_id === (host.identity?.routingInstanceId || host.identity?.instanceId)) {
await rebrandBuiltinAssistant(bootstrap, binding)
}
Expand Down
9 changes: 6 additions & 3 deletions runtime/flybot-workspace-web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ export function apply(ctx, config = {}) {
if (!ctx?.connection?.fetch?.register || !ctx?.effect) {
throw new Error('flybot-workspace-web: dsh connection fetch registry is required')
}
const dataDir = config.dataDir ?? process.env.DSH_HOME ?? '/data'
const identity = loadIdentity({ env: process.env, dataDir })
// config.env lets a caller (tests, embedders) run the plugin against an explicit
// environment instead of whatever the host process happens to export.
const env = config.env ?? process.env
const dataDir = config.dataDir ?? env.DSH_HOME ?? '/data'
const identity = loadIdentity({ env, dataDir })
const pagesDir = config.pagesDir ?? join(HERE, 'pages')
const pageSettings = Promise.withResolvers()
const chatDirectory = Promise.withResolvers()
const features = loadFeatures({
dir: config.featuresDir ?? join(HERE, 'features'), identity, dataDir,
dir: config.featuresDir ?? join(HERE, 'features'), identity, dataDir, env,
onRegistered(name, table, loaded) {
if (name === 'settings') pageSettings.resolve(table['settings/get'])
if (name === 'teams' && CHAT_DIRECTORY_FEATURES.every(feature => loaded.includes(feature))) {
Expand Down
7 changes: 4 additions & 3 deletions runtime/flybot-workspace-web/src/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export const OP_NAME = /^[a-z][a-zA-Z0-9-]{0,63}$/
export const CHAT_DIRECTORY_FEATURES = ['chat', 'agents', 'personal', 'teams']
const startupFeatures = ['settings', ...CHAT_DIRECTORY_FEATURES]

export function featureHost({ name, identity, dataDir, log = () => {}, table = {} }) {
export function featureHost({ name, identity, dataDir, env = process.env, log = () => {}, table = {} }) {
const host = Object.freeze({
name,
identity,
dataDir,
env,
featureDir() {
const dir = join(dataDir, 'flybot-workspace', 'features', name)
mkdirSync(dir, { recursive: true, mode: 0o700 })
Expand Down Expand Up @@ -64,7 +65,7 @@ export function registerFeature(table, name, spec) {

// Load every features/<name>/index.js. A feature that fails to load is reported and
// skipped; the others and the core keep working.
export async function loadFeatures({ dir, identity, dataDir, log = console.error, onRegistered = () => {} }) {
export async function loadFeatures({ dir, identity, dataDir, env = process.env, log = console.error, onRegistered = () => {} }) {
const table = {}
const loaded = []
const failed = []
Expand All @@ -82,7 +83,7 @@ export async function loadFeatures({ dir, identity, dataDir, log = console.error
const mod = await import(pathToFileURL(join(dir, name, 'index.js')).href)
const factory = mod.default
if (typeof factory !== 'function') throw new Error('default export must be a factory function')
const host = featureHost({ name, identity, dataDir, log, table })
const host = featureHost({ name, identity, dataDir, env, log, table })
registerFeature(table, name, await factory(host))
loaded.push(name)
onRegistered(name, table, loaded)
Expand Down
3 changes: 2 additions & 1 deletion runtime/flybot-workspace-web/test/agent-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createAgents } from '../features/agents/index.js'
import { createAgentApi } from '../features/agent-api/index.js'
import { loadFeatures } from '../src/features.js'
import { nativeAgentFixture } from './native-agent-fixture.js'
import { TEST_ENV } from './helpers.js'

const HERE = dirname(fileURLToPath(import.meta.url))
const W = 'a'.repeat(32)
Expand Down Expand Up @@ -90,7 +91,7 @@ test('a DS-Bot failure is recorded on the invocation', async () => {

test('agent-api loads through the extension point next to agents', async () => {
process.env.FLYBOT_WORKSPACE_AUTOMATIONS_CLOCK = '0'
const { table, loaded } = await loadFeatures({ dir: join(HERE, '../features'), identity: { instanceId: INSTANCE }, dataDir: mkdtempSync(join(tmpdir(), 'ot-api-load-')), log: () => {} })
const { table, loaded } = await loadFeatures({ dir: join(HERE, '../features'), identity: { instanceId: INSTANCE }, dataDir: mkdtempSync(join(tmpdir(), 'ot-api-load-')), env: TEST_ENV, log: () => {} })
assert.ok(loaded.includes('agent-api') && loaded.includes('agents'))
assert.ok(table['agent-api/invoke'])
})
Expand Down
3 changes: 2 additions & 1 deletion runtime/flybot-workspace-web/test/agents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createAgents, composePrompt, CATEGORIES } from '../features/agents/inde
import { loadFeatures } from '../src/features.js'
import { asksToSetUpTeam } from '../features/agents/team-creation.js'
import { nativeAgentFixture } from './native-agent-fixture.js'
import { TEST_ENV } from './helpers.js'

const HERE = dirname(fileURLToPath(import.meta.url))
const W = 'a'.repeat(32)
Expand Down Expand Up @@ -312,7 +313,7 @@ test('legacy import is idempotent; persona header order matches the server', asy

test('agents load through the extension point', async () => {
process.env.FLYBOT_WORKSPACE_AUTOMATIONS_CLOCK = '0'
const { table, loaded } = await loadFeatures({ dir: join(HERE, '../features'), identity: { instanceId: 'e'.repeat(32) }, dataDir: mkdtempSync(join(tmpdir(), 'ot-agents-load-')), log: () => {} })
const { table, loaded } = await loadFeatures({ dir: join(HERE, '../features'), identity: { instanceId: 'e'.repeat(32) }, dataDir: mkdtempSync(join(tmpdir(), 'ot-agents-load-')), env: TEST_ENV, log: () => {} })
assert.ok(loaded.includes('agents'))
assert.ok(table['agents/turn'] && table['agents/send'] && table['agents/conversation-create'])
})
Expand Down
3 changes: 2 additions & 1 deletion runtime/flybot-workspace-web/test/automations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createAutomations, dsBotTurn } from '../features/automations/automation
import { TEMPLATES } from '../features/automations/templates.js'
import { featureHost, loadFeatures, registerFeature } from '../src/features.js'
import artifactsFeature from '../features/artifacts/index.js'
import { TEST_ENV } from './helpers.js'
import { attachRuntime, detachRuntime } from '../src/host.js'

const HERE = dirname(fileURLToPath(import.meta.url))
Expand Down Expand Up @@ -1206,7 +1207,7 @@ test('legacy import is idempotent and never re-fires the already-run slot', () =
test('the feature loads through the extension point and runs turns via the DS-Bot chatTurn service', async () => {
process.env.FLYBOT_WORKSPACE_AUTOMATIONS_CLOCK = '0'
const data = mkdtempSync(join(tmpdir(), 'ot-auto-load-'))
const { table, loaded } = await loadFeatures({ dir: join(HERE, '../features'), identity: { instanceId: 'e'.repeat(32) }, dataDir: data, log: () => {} })
const { table, loaded } = await loadFeatures({ dir: join(HERE, '../features'), identity: { instanceId: 'e'.repeat(32) }, dataDir: data, env: TEST_ENV, log: () => {} })
assert.ok(loaded.includes('automations'))
const made = await table['automations/create']({ workspace_id: W, title: 'X', template_id: 'custom', prompt: 'p', schedule: { kind: 'now', time_zone: 'UTC' }, participants: ['alice'], idempotency_key: 'l'.repeat(16) })
assert.equal(made.status, 201)
Expand Down
3 changes: 2 additions & 1 deletion runtime/flybot-workspace-web/test/chat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import { createAgents } from '../features/agents/index.js'
import { loadFeatures } from '../src/features.js'
import { attachRuntime, detachRuntime } from '../src/host.js'
import { tokens, rank } from '../features/chat/recall.js'
import { TEST_ENV } from './helpers.js'

const dir = join(fileURLToPath(new URL('.', import.meta.url)), '..', 'features')
const identity = { instanceId: 'd'.repeat(32) }

async function load(data = mkdtempSync(join(tmpdir(), 'ot-chat-'))) {
const { table, failed } = await loadFeatures({ dir, identity, dataDir: data, log: () => {} })
const { table, failed } = await loadFeatures({ dir, identity, dataDir: data, env: TEST_ENV, log: () => {} })
assert.deepEqual(failed, [])
return table
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/flybot-workspace-web/test/features.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { PAGE_PATH } from '../src/pages.js'
import { loadFeatures, registerFeature } from '../src/features.js'
import { attachRuntime, detachRuntime, runtime } from '../src/host.js'
import { createDispatch, LEGACY_PATH } from '../src/dispatch.js'
import { deliverBundle, fakeCtx, volume } from './helpers.js'
import { deliverBundle, fakeCtx, TEST_ENV, volume } from './helpers.js'

const identity = { instanceId: 'c'.repeat(32) }

Expand All @@ -32,7 +32,7 @@ test('features register namespaced endpoints; a broken feature is skipped, other
})
const data = mkdtempSync(join(tmpdir(), 'ot-data-'))
const logs = []
const { table, loaded, failed } = await loadFeatures({ dir, identity, dataDir: data, log: (m) => logs.push(m) })
const { table, loaded, failed } = await loadFeatures({ dir, identity, dataDir: data, env: TEST_ENV, log: (m) => logs.push(m) })
assert.deepEqual(loaded, ['alpha'])
assert.deepEqual(failed.map((f) => f.name).sort(), ['Bad_Name', 'broken', 'nofactory'])
assert.deepEqual(Object.keys(table), ['alpha/ping'])
Expand All @@ -56,7 +56,7 @@ test('native documents include saved appearance without waiting for another feat
effect: fn => fn(),
}
const pagesDir = fileURLToPath(new URL('../../../flybot_workspace/static/', import.meta.url))
const {features} = apply(ctx, {pagesDir, featuresDir:dir, dataDir:coordination})
const {features} = apply(ctx, {pagesDir, featuresDir:dir, dataDir:coordination, env: TEST_ENV})
try {
await latch.entered.promise
const response = await Promise.race([
Expand Down Expand Up @@ -106,7 +106,7 @@ test('native Chat serves the migrated owner directory while unrelated features i
deliverBundle(vol, 'agents.bundle', JSON.stringify({format:'flybot-workspace-dsbot-import', version:1, feature:'agents', tables, manifest}))

const ctx = fakeCtx()
const {features} = apply(ctx, {dataDir:vol.data, featuresDir:dir})
const {features} = apply(ctx, {dataDir:vol.data, featuresDir:dir, env: TEST_ENV})
const fetch = (query, method = 'GET') => ctx.routes.get(LEGACY_PATH).fetch(new Request(`http://bot${LEGACY_PATH}?` +
new URLSearchParams({feature:'teams', path:'/v1/workspaces', query}), {method, headers:{'x-ot-owner-username':'directory-owner'}}))
const query = 'scope=personal&include=chat'
Expand Down
17 changes: 13 additions & 4 deletions runtime/flybot-workspace-web/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ export function volume() {
return { root, pages, data }
}

export async function boot(vol = volume()) {
// The environment these tests run the plugin with: an explicit object, not the
// developer's shell. The key paths point at files that cannot exist, because an
// unset or empty value falls back to the host's /run/secrets defaults inside
// lib/b4/seal.js and flybot-image/agent-bootstrap.mjs.
export const TEST_ENV = Object.freeze({
FLYBOT_WORKSPACE_RUNTIME_KEY_FILE: '/nonexistent/flybot-workspace-runtime-key.json',
FLYBOT_WORKSPACE_SEAL_KEY_FILE: '/nonexistent/flybot-workspace-seal.key',
})

export async function boot(vol = volume(), { env = TEST_ENV } = {}) {
const ctx = fakeCtx()
const result = apply(ctx, { pagesDir: vol.pages, dataDir: vol.data })
const result = apply(ctx, { pagesDir: vol.pages, dataDir: vol.data, env })
const features = await result.features
const api = ctx.routes.get(API_PATH)
async function endpoint(name, payload) {
Expand All @@ -57,9 +66,9 @@ export function legacyBundles(features) {
return JSON.parse(run.stdout)
}

export function deliverBundle(vol, name, text) {
export function deliverBundle(vol, name, text, env = TEST_ENV) {
const dir = join(vol.data, 'flybot-workspace', 'import')
const {routingInstanceId, instanceId} = loadIdentity({env:process.env, dataDir:vol.data})
const {routingInstanceId, instanceId} = loadIdentity({env, dataDir:vol.data})
const bundle = JSON.parse(text)
bundle.instance_id ??= routingInstanceId ?? instanceId
mkdirSync(dir, { recursive: true })
Expand Down
4 changes: 3 additions & 1 deletion runtime/flybot-workspace-web/test/model-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ test('seal round trip, purpose binding, corrupt blobs refused', () => {
test('seal key: private volume fallback is created once and reused', () => {
const dir = tmp()
const file = join(dir, 'k', 'seal.key')
const env = { FLYBOT_WORKSPACE_SEAL_KEY_FILE: '' }
// A path that cannot exist means "no operator key"; an empty string would be
// falsy in lib/b4/seal.js and fall back to the host's /run/secrets key file.
const env = { FLYBOT_WORKSPACE_SEAL_KEY_FILE: join(dir, 'k', 'no-operator.key') }
const a = loadSealKey({ env, fallbackFile: file })
assert.equal(statSync(file).mode & 0o777, 0o600)
assert.deepEqual(loadSealKey({ env, fallbackFile: file }), a)
Expand Down
4 changes: 2 additions & 2 deletions runtime/flybot-workspace-web/test/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'node:assert/strict'
import test from 'node:test'
import { join } from 'node:path'
import { DatabaseSync } from 'node:sqlite'
import { boot, bootMemory, deliverBundle, key, volume, WS } from './helpers.js'
import { boot, bootMemory, deliverBundle, key, TEST_ENV, volume, WS } from './helpers.js'
import { digest } from '../featurekit/http.js'
import { setSharedBotSocial } from '../lib/c/bot-social.js'
import { loadIdentity } from '../src/identity.js'
Expand Down Expand Up @@ -75,7 +75,7 @@ test('message search runs in DS-Bot over this owner\'s own conversations', async

test('target-bound imported history is searchable with stable source IDs, workspace scoping and keyset paging', async () => {
const vol = volume()
const owner = loadIdentity({env: process.env, dataDir: vol.data})
const owner = loadIdentity({env: TEST_ENV, dataDir: vol.data})
const instance = owner.routingInstanceId ?? owner.instanceId
const otherWorkspace = 'b'.repeat(32)
const agent = '1'.repeat(32)
Expand Down
3 changes: 2 additions & 1 deletion runtime/flybot-workspace-web/test/team-runs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createTeamRuns, stages } from '../features/team-runs/index.js'
import { teamRunWorkKey } from '../features/shares/team-run-source.js'
import artifacts from '../features/artifacts/index.js'
import { featureHost, loadFeatures, registerFeature } from '../src/features.js'
import { TEST_ENV } from './helpers.js'

const HERE = dirname(fileURLToPath(import.meta.url))
const W = 'a'.repeat(32)
Expand Down Expand Up @@ -449,7 +450,7 @@ test('workspace membership is checked on reads, writes, and outstanding step dis

test('team-runs loads through the extension point', async () => {
process.env.FLYBOT_WORKSPACE_AUTOMATIONS_CLOCK = '0'
const { table, loaded } = await loadFeatures({ dir: join(HERE, '../features'), identity: { instanceId: 'e'.repeat(32) }, dataDir: mkdtempSync(join(tmpdir(), 'ot-tr-load-')), log: () => {} })
const { table, loaded } = await loadFeatures({ dir: join(HERE, '../features'), identity: { instanceId: 'e'.repeat(32) }, dataDir: mkdtempSync(join(tmpdir(), 'ot-tr-load-')), env: TEST_ENV, log: () => {} })
assert.ok(loaded.includes('team-runs'))
assert.ok(table['team-runs/create'] && table['team-runs/retry'] && table['team-runs/v1'] && table['team-runs/step-artifact'])
})
Loading