Your always-on AI coworkers that move between text, calls, and Slack.
TypeScript
fix: contain background runner store failures (#12)
Charan Rathore3af01a4aa9ff
diff --git a/src/server/runner.ts b/src/server/runner.tsindex d56b705..e5a047e 100644--- a/src/server/runner.ts+++ b/src/server/runner.ts@@ -41,14 +41,29 @@ export class Runner {controller.abort(new Error('Run stopped because settings changed.'));}async tick() {+ try {+ await this.runTick();+ } catch {+ // Store errors must not become unhandled interval promise rejections.+ // Do not log task content, provider responses, or database details.+ console.error(+ 'Background runner tick failed; will retry on the next tick.',+ );+ }+ }+ private async runTick() {if (this.active.size) return;const claim = this.store.claim();if (!claim) return;const controller = new AbortController();this.active.set(claim.id, controller);const ownershipCheck = setInterval(() => {- if (!this.store.owns(claim))- controller.abort(new Error('Run permission or lease was revoked.'));+ try {+ if (!this.store.owns(claim))+ controller.abort(new Error('Run permission or lease was revoked.'));+ } catch {+ controller.abort(new Error('Run ownership check failed.'));+ }}, 100);const timeout = setTimeout(() =>diff --git a/tests/runner.test.ts b/tests/runner.test.tsindex d621ef8..37d4141 100644--- a/tests/runner.test.ts+++ b/tests/runner.test.ts@@ -98,3 +98,69 @@ it('holds active work for review on graceful shutdown', async () => {expect(store.claim()).toBeTruthy();store.close();});+it('survives a claim failure and runs work on the next tick', async () => {+ const store = new Store(':memory:');+ const runner = new Runner(store, { mode: 'sample', baseUrl: '' });+ const log = vi.spyOn(console, 'error').mockImplementation(() => {});+ const claim = vi.spyOn(store, 'claim').mockImplementationOnce(() => {+ throw new Error('SQLITE_BUSY');+ });+ const task = store.createTask('Read this sample');+ try {+ await expect(runner.tick()).resolves.toBeUndefined();+ expect(log).toHaveBeenCalledOnce();+ await runner.tick();+ expect(claim).toHaveBeenCalledTimes(2);+ expect(store.detail(task.id)?.runs[0].result).toBeTruthy();+ } finally {+ log.mockRestore();+ store.close();+ }+});+it('survives failure persistence errors and clears active work', async () => {+ const store = new Store(':memory:');+ const execute = vi+ .fn()+ .mockRejectedValueOnce(new Error('Provider failure'))+ .mockResolvedValueOnce({ text: 'Recovered', sources: [], memories: [] });+ const runner = new Runner(store, config, execute);+ const log = vi.spyOn(console, 'error').mockImplementation(() => {});+ const fail = vi.spyOn(store, 'fail').mockImplementationOnce(() => {+ throw new Error('SQLITE_BUSY');+ });+ store.createTask('First');+ try {+ await expect(runner.tick()).resolves.toBeUndefined();+ expect(fail).toHaveBeenCalledOnce();+ store.createTask('Second');+ await runner.tick();+ expect(execute).toHaveBeenCalledTimes(2);+ expect(log).toHaveBeenCalledOnce();+ } finally {+ log.mockRestore();+ store.close();+ }+});+it('aborts work instead of throwing from an ownership timer', async () => {+ const store = new Store(':memory:');+ store.createTask('First');+ const runner = new Runner(+ store,+ config,+ (_claim, _memories, signal) =>+ new Promise((_resolve, reject) => {+ signal.addEventListener('abort', () => reject(signal.reason), {+ once: true,+ });+ }),+ );+ vi.spyOn(store, 'owns').mockImplementationOnce(() => {+ throw new Error('Database unavailable');+ });+ try {+ await expect(runner.tick()).resolves.toBeUndefined();+ expect(store.tasks()[0].status).toBe('failed');+ } finally {+ store.close();+ }+});