feat(cron): expose run receipts and modularize execution

Return committed trigger IDs and add CLI wait and trigger-specific logs while
preserving API, CLI, transaction, deduplication and unknown-result semantics.

Move scheduling, lifecycle, settlement, queries and legacy adapters into
back/cron; remove obsolete internal import paths and circular dependencies.

Apply project formatting across the repository, cover CJS/MJS in formatting
hooks, add prettier:check and require formatting checks in AGENTS.md.

Validation: backend build; 1398 backend/frontend tests passed (17 platform
skips); 327 CLI tests passed (6 platform skips); 670 files pass format checks.
This commit is contained in:
whyour committed 2026-11-22 12:24:29 +08:00
1 parent 77ece406ac
commit c5458596c8
346 files changed
+32133 -11556

No files matched your search

+268 -65
View File
@@ -46,29 +46,64 @@ const flush = () => new Promise((resolve) => setImmediate(resolve));
const pastCursor = action('components/fileBrowser/index.tsx', 'pastCursor', {});
test('a retry at the top restores the next visible file when the first file was deleted', () => {
const row = (title, top) => ({ title, getBoundingClientRect: () => ({ top, bottom: top + 24 }) });
const row = (title, top) => ({
title,
getBoundingClientRect: () => ({ top, bottom: top + 24 }),
});
const element = {
scrollTop: 0, clientHeight: 48, getBoundingClientRect: () => ({ top: 100 }),
querySelectorAll: () => [row('gone', 100), row('survivor', 124), row('below', 148)],
querySelector: selector => selector === 'button[title="survivor"]' ? row('survivor', 364) : null,
scrollTop: 0,
clientHeight: 48,
getBoundingClientRect: () => ({ top: 100 }),
querySelectorAll: () => [
row('gone', 100),
row('survivor', 124),
row('below', 148),
],
querySelector: (selector) =>
selector === 'button[title="survivor"]' ? row('survivor', 364) : null,
};
const viewport = { current: element }, position = {};
action('components/fileBrowser/index.tsx', 'rememberPosition', { viewport, position })();
assert.deepEqual([...position.current.offsets], [['gone', 0], ['survivor', 24]]);
const viewport = { current: element },
position = {};
action('components/fileBrowser/index.tsx', 'rememberPosition', {
viewport,
position,
})();
assert.deepEqual(
[...position.current.offsets],
[
['gone', 0],
['survivor', 24],
],
);
const emptyListing = { rows: [], next: '', versions: new Map() };
action('components/fileBrowser/index.tsx', 'Entries', {
React: require('react'), emptyListing, Spin: 'i', CSS: { escape: key => key },
useRef: value => value === null ? viewport : value === undefined ? position : { current: value },
useState: value => [value, () => {}], useEffect() {}, useLayoutEffect: effect => effect(),
React: require('react'),
emptyListing,
Spin: 'i',
CSS: { escape: (key) => key },
useRef: (value) =>
value === null
? viewport
: value === undefined
? position
: { current: value },
useState: (value) => [value, () => {}],
useEffect() {},
useLayoutEffect: (effect) => effect(),
})({ source: 'logs', path: '', search: '' });
assert.equal(element.scrollTop, 240);
assert.equal(position.current, undefined);
});
test('a new execution has its own title before its log path arrives', () => {
const readableLogName = action('components/fileBrowser/name.ts', 'readableLogName', {});
const readableLogName = action(
'components/fileBrowser/name.ts',
'readableLogName',
{},
);
const title = action('pages/crontab/detail.tsx', 'instanceTitle', {
readableLogName, intl: { get: (key, { id }) => key.replace('{id}', id) },
readableLogName,
intl: { get: (key, { id }) => key.replace('{id}', id) },
});
const next = { id: 34834, status: 0, log_path: null };
const stopped = { id: 34814, status: 2, log_path: 'task/old.log' };
@@ -79,17 +114,34 @@ test('a new execution has its own title before its log path arrives', () => {
test('file rows preserve meaningful suffixes, separate the search directory and expose the full path', () => {
const React = require('react');
const names = ['async_components___init__', 'async_components_register.js', '中文任务_同步明细.log'];
const rows = names.map(title => ({ title, parent: 'workers', key: 'workers/' + title, type: 'file' }));
const names = [
'async_components___init__',
'async_components_register.js',
'中文任务_同步明细.log',
];
const rows = names.map((title) => ({
title,
parent: 'workers',
key: 'workers/' + title,
type: 'file',
}));
const emptyListing = { rows, next: '', versions: new Map() };
const entries = action('components/fileBrowser/index.tsx', 'Entries', {
React, useRef: current => ({ current }), useState: value => [value, () => {}], useEffect() {}, useLayoutEffect() {},
emptyListing, FileOutlined: 'i', Spin: 'i', Typography: { Text: 'span' }, readableLogName: value => value,
React,
useRef: (current) => ({ current }),
useState: (value) => [value, () => {}],
useEffect() {},
useLayoutEffect() {},
emptyListing,
FileOutlined: 'i',
Spin: 'i',
Typography: { Text: 'span' },
readableLogName: (value) => value,
})({ source: 'logs', path: '', search: 'work' }).props.children[0];
const tails = ['___init__', '_register.js', '_同步明细.log'];
for (const [i, button] of entries.entries()) {
const [name, parent] = button.props.children[1].props.children;
const parts = name.props.children.map(span => span.props.children);
const parts = name.props.children.map((span) => span.props.children);
assert.equal(parts.join(''), names[i]);
assert.equal(parts[1], tails[i]);
assert.equal(parent.props.children, 'workers');
@@ -101,24 +153,53 @@ test('file rows preserve meaningful suffixes, separate the search directory and
for (const remaining of [0, 2, 8]) {
test(`manual retry retains the requested depth through churn and a network error with ${remaining} files left`, async () => {
const emptyListing = { rows: [], next: '', versions: new Map() };
const listing = { current: { rows: [0, 1, 2, 3], next: '4', versions: new Map([['a', '1']]) } };
const states = [], failures = [];
let checks = 0, reads = 0, phase = 'churn';
const listing = {
current: {
rows: [0, 1, 2, 3],
next: '4',
versions: new Map([['a', '1']]),
},
};
const states = [],
failures = [];
let checks = 0,
reads = 0,
phase = 'churn';
const read = action('components/fileBrowser/index.tsx', 'read', {
pastCursor, rememberPosition() {},
listing, emptyListing, pending: {}, path: 'a', source: 'scripts', search: '', folders: false,
config: { apiPrefix: '/api/' }, setLoading() {}, setFailed: v => failures.push(v),
setListing: page => states.push(page.rows),
pastCursor,
rememberPosition() {},
listing,
emptyListing,
pending: {},
path: 'a',
source: 'scripts',
search: '',
folders: false,
config: { apiPrefix: '/api/' },
setLoading() {},
setFailed: (v) => failures.push(v),
setListing: (page) => states.push(page.rows),
request: {
async get(_url, { params }) {
reads++;
if (phase === 'offline') throw new Error('network unavailable');
const count = phase === 'churn' ? 8 : remaining;
const offset = Number(params.after);
const data = Array.from({ length: Math.min(2, count - offset) }, (_, i) => offset + i);
return { code: 200, data, next: offset + 2 < count ? String(offset + 2) : null, versions: [['a', '1']] };
const data = Array.from(
{ length: Math.min(2, count - offset) },
(_, i) => offset + i,
);
return {
code: 200,
data,
next: offset + 2 < count ? String(offset + 2) : null,
versions: [['a', '1']],
};
},
async post() {
checks++;
return { code: 200, current: phase === 'stable' };
},
async post() { checks++; return { code: 200, current: phase === 'stable' }; },
},
});
await read(listing.current);
@@ -135,7 +216,10 @@ for (const remaining of [0, 2, 8]) {
phase = 'stable';
await Promise.all([read(retry), read(retry)]);
const count = Math.min(6, remaining);
assert.deepEqual(listing.current.rows, Array.from({ length: count }, (_, i) => i));
assert.deepEqual(
listing.current.rows,
Array.from({ length: count }, (_, i) => i),
);
assert.equal(listing.current.next, remaining > count ? String(count) : '');
const completedReads = reads;
await read(retry);
@@ -149,21 +233,44 @@ for (const [source, search, boundary, cursors] of [
// DFS enters directory "a" before visiting sibling "a-".
['logs', 'needle', 'a-', ['a/1', 'a/2', 'a/3', 'a/x', 'a.', 'z']],
]) {
test(`retry traverses the old boundary after earlier inserts and deletion of the cursor (${source}, ${search || 'directory'})`, async () => {
test(`retry traverses the old boundary after earlier inserts and deletion of the cursor (${source}, ${
search || 'directory'
})`, async () => {
const emptyListing = { rows: [], next: '', versions: new Map() };
const listing = { current: { rows: ['old1', 'old2'], next: boundary, versions: new Map() } };
const listing = {
current: { rows: ['old1', 'old2'], next: boundary, versions: new Map() },
};
const failures = [];
let stable = false;
const read = action('components/fileBrowser/index.tsx', 'read', {
pastCursor, rememberPosition() {}, listing, emptyListing, pending: {}, source, path: '', search, folders: false,
config: { apiPrefix: '/api/' }, setLoading() {}, setFailed: v => failures.push(v), setListing() {},
pastCursor,
rememberPosition() {},
listing,
emptyListing,
pending: {},
source,
path: '',
search,
folders: false,
config: { apiPrefix: '/api/' },
setLoading() {},
setFailed: (v) => failures.push(v),
setListing() {},
request: {
async get(_url, { params }) {
const index = params.after === boundary ? 4 : cursors.indexOf(params.after) + 1;
const index =
params.after === boundary ? 4 : cursors.indexOf(params.after) + 1;
assert.ok(index < cursors.length);
return { code: 200, next: cursors[index], data: search && index === 3 ? [] : [cursors[index]], versions: [['', '1']] };
return {
code: 200,
next: cursors[index],
data: search && index === 3 ? [] : [cursors[index]],
versions: [['', '1']],
};
},
async post() {
return { code: 200, current: stable };
},
async post() { return { code: 200, current: stable }; },
},
});
await read(listing.current);
@@ -173,53 +280,119 @@ for (const [source, search, boundary, cursors] of [
await read(listing.current);
assert.equal(failures.at(-1), false);
assert.equal(listing.current.next, cursors[4]);
assert.deepEqual(listing.current.rows, cursors.slice(0, 5).filter((_, i) => !search || i !== 3));
assert.deepEqual(
listing.current.rows,
cursors.slice(0, 5).filter((_, i) => !search || i !== 3),
);
});
}
test('loading more rebuilds the original depth if a delayed last page missed a move into a previous directory', async () => {
const pages = [
{ data: ['c/old'], next: null, versions: [['c', '1']] },
{ data: ['a/moved', 'b/kept'], next: 'b/kept', versions: [['a', '2'], ['b', '1']] },
{
data: ['a/moved', 'b/kept'],
next: 'b/kept',
versions: [
['a', '2'],
['b', '1'],
],
},
{ data: ['c/new'], next: null, versions: [['c', '2']] },
];
const cursors = [], checks = [], commits = [], emptyListing = { rows: [], next: '', versions: new Map() };
const listing = { current: { rows: ['a/deleted', 'b/kept'], next: 'b/kept', versions: new Map([['a', '1'], ['b', '1']]) } };
const cursors = [],
checks = [],
commits = [],
emptyListing = { rows: [], next: '', versions: new Map() };
const listing = {
current: {
rows: ['a/deleted', 'b/kept'],
next: 'b/kept',
versions: new Map([
['a', '1'],
['b', '1'],
]),
},
};
const read = action('components/fileBrowser/index.tsx', 'read', {
pastCursor, rememberPosition() {},
pending: {}, listing, emptyListing, source: 'logs', path: '', search: 'match',
folders: false, config: { apiPrefix: '/api/' }, setListing: value => commits.push(value.rows),
setLoading() {}, setFailed(value) { assert.equal(value, false); },
pastCursor,
rememberPosition() {},
pending: {},
listing,
emptyListing,
source: 'logs',
path: '',
search: 'match',
folders: false,
config: { apiPrefix: '/api/' },
setListing: (value) => commits.push(value.rows),
setLoading() {},
setFailed(value) {
assert.equal(value, false);
},
request: {
async get(_url, { params }) { cursors.push(params.after); return { code: 200, ...pages.shift() }; },
async post(_url, stamps) { checks.push(Object.fromEntries(stamps)); return { code: 200, current: checks.length > 1 }; },
async get(_url, { params }) {
cursors.push(params.after);
return { code: 200, ...pages.shift() };
},
async post(_url, stamps) {
checks.push(Object.fromEntries(stamps));
return { code: 200, current: checks.length > 1 };
},
},
});
const previous = listing.current;
await read(previous);
await read(previous); // A late second click from the previous render.
assert.deepEqual(cursors, ['b/kept', '', 'b/kept']);
assert.deepEqual(checks, [{ a: '1', b: '1', c: '1' }, { a: '2', b: '1', c: '2' }]);
assert.deepEqual(checks, [
{ a: '1', b: '1', c: '1' },
{ a: '2', b: '1', c: '2' },
]);
assert.deepEqual(commits, [[], ['a/moved', 'b/kept', 'c/new']]);
});
for (const retrying of [false, true]) {
test(`abandoning a search discards its delayed validation during ${retrying ? 'manual retry' : 'initial load'}`, async () => {
const pending = {}, commits = [];
test(`abandoning a search discards its delayed validation during ${
retrying ? 'manual retry' : 'initial load'
}`, async () => {
const pending = {},
commits = [];
const emptyListing = { rows: [], next: '', versions: new Map() };
const previous = retrying ? { ...emptyListing, retryMinimum: 2 } : emptyListing;
let finish, reads = 0;
const previous = retrying
? { ...emptyListing, retryMinimum: 2 }
: emptyListing;
let finish,
reads = 0;
const read = action('components/fileBrowser/index.tsx', 'read', {
pastCursor, rememberPosition() {},
pending, listing: { current: previous }, emptyListing, source: 'scripts', path: '', search: 'old',
folders: false, config: { apiPrefix: '/api/' }, setListing: value => commits.push(value),
setLoading() {}, setFailed() {},
pastCursor,
rememberPosition() {},
pending,
listing: { current: previous },
emptyListing,
source: 'scripts',
path: '',
search: 'old',
folders: false,
config: { apiPrefix: '/api/' },
setListing: (value) => commits.push(value),
setLoading() {},
setFailed() {},
request: {
async get() {
reads++;
return { code: 200, data: [`old-${reads}.js`], next: reads === 1 ? 'more' : null, versions: [['', '1']] };
return {
code: 200,
data: [`old-${reads}.js`],
next: reads === 1 ? 'more' : null,
versions: [['', '1']],
};
},
post() {
return new Promise((resolve) => {
finish = resolve;
});
},
post() { return new Promise(resolve => { finish = resolve; }); },
},
});
const loading = read(previous);
@@ -237,11 +410,27 @@ test('directory navigation shows the current path separately and returns exactly
for (const source of ['scripts', 'logs']) {
let directory = 'parent/child';
const render = (input = '') => {
const states = [[directory, next => { directory = next; }], [input, () => {}], [input, () => {}]];
const states = [
[
directory,
(next) => {
directory = next;
},
],
[input, () => {}],
[input, () => {}],
];
return action('components/fileBrowser/index.tsx', 'FileBrowser', {
React, Entries: 'div', Spin: 'span', ArrowUpOutlined: 'i', Button: 'button',
Input: { Search: 'input' }, Typography: { Text: 'span' }, intl: { get: key => key },
useEffect() {}, useState: () => states.shift(),
React,
Entries: 'div',
Spin: 'span',
ArrowUpOutlined: 'i',
Button: 'button',
Input: { Search: 'input' },
Typography: { Text: 'span' },
intl: { get: (key) => key },
useEffect() {},
useState: () => states.shift(),
})({ source }).props.children[1];
};
assert.ok(!render('query'));
@@ -336,7 +525,7 @@ test('expanded history retains its cursor and original head during inserts and r
pending,
paging,
visibleIds,
list: { current: { scrollTo: position => scrolls.push(position) } },
list: { current: { scrollTo: (position) => scrolls.push(position) } },
pageSize: 2,
config: { apiPrefix: '/api/' },
id: 1,
@@ -356,13 +545,20 @@ test('expanded history retains its cursor and original head during inserts and r
await reading;
reading = load('older');
await load('older');
assert.deepEqual(request.calls.at(-1).config.params, { paged: true, limit: 2, before: 3 });
assert.deepEqual(request.calls.at(-1).config.params, {
paged: true,
limit: 2,
before: 3,
});
assert.equal(request.calls.length, 2);
request.calls.at(-1).reject(new Error('older request interrupted'));
await reading;
assert.equal(failure, 'older');
reading = load('refresh');
assert.deepEqual(request.calls.at(-1).config.params, { paged: true, limit: 1 });
assert.deepEqual(request.calls.at(-1).config.params, {
paged: true,
limit: 1,
});
answer([5], null);
await flush();
answer([3], null);
@@ -370,13 +566,20 @@ test('expanded history retains its cursor and original head during inserts and r
assert.equal(state.current.hasNew, true);
assert.equal(failure, 'older');
reading = load(failure);
assert.deepEqual(request.calls.at(-1).config.params, { paged: true, limit: 2, before: 3 });
assert.deepEqual(request.calls.at(-1).config.params, {
paged: true,
limit: 2,
before: 3,
});
answer([2, 1], null);
await reading;
reading = load('refresh');
answer([6], null);
await flush();
assert.deepEqual(request.calls.at(-1).config.params, { paged: true, ids: '3' });
assert.deepEqual(request.calls.at(-1).config.params, {
paged: true,
ids: '3',
});
answer([3], null);
await reading;
assert.deepEqual(