fix(tasks): cover syncProject wiring and allow deleting orphans after file removal

Review follow-up:

- runtime.test.js: add syncProject wiring tests with a real temp-dir
  project and real project-config runtime — asserts reconcileLoopTasks is
  driven with the discovered loops when the project path is known (task
  created, nextRunAt computed) and that plain listing is used when the
  path cannot be resolved (reconcile not called).
- service.js: DELETE on a loop-owned task is rejected with a 400 only
  while its loop file still exists on disk; once the file is gone the
  orphan task can be deleted directly instead of waiting for the next
  reconcile. Tests use real temp files for both branches.
- DOCUMENTATION.md: delete semantics updated accordingly.
- PR description refreshed for the final HEAD (test counts, reconciliation
  contract, evidence wording).
This commit is contained in:
makeittech
2026-08-06 09:49:31 +03:00
parent 59a6c1b70d
commit 9b6b90504c
4 changed files with 156 additions and 21 deletions
@@ -1,5 +1,15 @@
import { describe, expect, it } from 'vitest';
import { computeNextRunAt, expandCommandGoalObjective, formatScheduledSessionTitle, parseScheduledCommandPrompt } from './runtime.js';
import { describe, expect, it, vi } from 'vitest';
import os from 'os';
import path from 'path';
import { mkdtemp, rm, mkdir, writeFile } from 'fs/promises';
import {
computeNextRunAt,
expandCommandGoalObjective,
formatScheduledSessionTitle,
parseScheduledCommandPrompt,
createScheduledTasksRuntime,
} from './runtime.js';
import { createProjectConfigRuntime } from '../projects/project-config.js';
describe('scheduled-tasks runtime helpers', () => {
it('computes next daily run in timezone', () => {
@@ -109,3 +119,90 @@ describe('scheduled-tasks runtime helpers', () => {
.toBe('Review the requested scope.\n\nauth module');
});
});
describe('scheduled-tasks runtime syncProject wiring', () => {
const createTempProject = async () => {
const tempRoot = await mkdtemp(path.join(os.tmpdir(), 'oc-runtime-loop-'));
const repoPath = path.join(tempRoot, 'repo');
await mkdir(path.join(repoPath, '.agents', 'loops'), { recursive: true });
return {
tempRoot,
repoPath,
cleanup: async () => {
await rm(tempRoot, { recursive: true, force: true });
},
};
};
const createProjectConfig = async (tempRoot) => createProjectConfigRuntime({
fsPromises: await import('fs/promises'),
path,
projectsDirPath: path.join(tempRoot, 'config'),
createTaskID: () => 'task-fixed-id',
});
const createRuntimeDeps = (overrides = {}) => ({
buildOpenCodeUrl: () => 'http://localhost',
getOpenCodeAuthHeaders: () => ({}),
waitForOpenCodeReady: async () => {},
...overrides,
});
it('reconciles discovered loops when the project path is known', async () => {
const { tempRoot, repoPath, cleanup } = await createTempProject();
try {
await writeFile(path.join(repoPath, '.agents', 'loops', 'daily.md'), `---
name: daily
schedule: "0 9 * * *"
enabled: true
model: openai/gpt-5
---
Run daily.
`, 'utf8');
const projectConfigRuntime = await createProjectConfig(tempRoot);
const runtime = createScheduledTasksRuntime({
...createRuntimeDeps(),
projectConfigRuntime,
listProjects: async () => [{ id: 'proj', path: repoPath }],
});
await runtime.syncProject('proj');
const tasks = await projectConfigRuntime.listScheduledTasks('proj');
expect(tasks).toHaveLength(1);
expect(tasks[0].id).toBe('loop:project:daily');
expect(tasks[0].loopFile).toBe(path.join(repoPath, '.agents', 'loops', 'daily.md'));
// syncTaskSchedule computed and persisted the next run for the enabled task.
expect(tasks[0].state.nextRunAt).toBeGreaterThan(0);
} finally {
await cleanup();
}
});
it('falls back to plain listing when the project path cannot be resolved', async () => {
const { tempRoot, cleanup } = await createTempProject();
try {
const projectConfigRuntime = await createProjectConfig(tempRoot);
const reconcileSpy = vi.spyOn(projectConfigRuntime, 'reconcileLoopTasks');
const listSpy = vi.spyOn(projectConfigRuntime, 'listScheduledTasks');
const runtime = createScheduledTasksRuntime({
...createRuntimeDeps(),
projectConfigRuntime,
// Project not registered -> ensureProjectPath cannot resolve a path.
listProjects: async () => [],
});
await runtime.syncProject('proj');
expect(reconcileSpy).not.toHaveBeenCalled();
expect(listSpy).toHaveBeenCalledWith('proj');
expect(await projectConfigRuntime.listScheduledTasks('proj')).toEqual([]);
reconcileSpy.mockRestore();
listSpy.mockRestore();
} finally {
await cleanup();
}
});
});