48 lines
1.7 KiB
JavaScript
48 lines
1.7 KiB
JavaScript
(function initExecutionGuard(root, factory) {
|
|
const api = factory();
|
|
if (typeof module === 'object' && module.exports) module.exports = api;
|
|
root.LTJTExecutionGuard = api;
|
|
})(typeof self !== 'undefined' ? self : globalThis, function createExecutionGuard() {
|
|
'use strict';
|
|
|
|
const TERMINAL_STATES = new Set(['completed', 'blocked', 'cancelled', 'uncertain']);
|
|
const WRITE_STATES = new Set(['write_started', 'submitted', 'uncertain', 'completed']);
|
|
|
|
function rootTaskId(taskId) {
|
|
return String(taskId || '').split('#')[0];
|
|
}
|
|
|
|
function acceptanceDecision(existing, executionId) {
|
|
const normalizedExecutionId = String(executionId || '').trim();
|
|
if (!normalizedExecutionId) {
|
|
return { accepted: false, status: 'missing_execution_id', reason: '缺少服务端执行编号。' };
|
|
}
|
|
if (!existing) return { accepted: true, status: 'accepted', reason: '' };
|
|
if (String(existing.execution_id || '') !== normalizedExecutionId) {
|
|
return { accepted: false, status: 'duplicate_blocked', reason: '同一任务出现了不同执行编号。' };
|
|
}
|
|
return {
|
|
accepted: false,
|
|
status: TERMINAL_STATES.has(existing.state) ? 'already_finished' : 'already_started',
|
|
reason: '该执行编号已经被插件接收,重复消息不会再次执行。'
|
|
};
|
|
}
|
|
|
|
function failureState(existing) {
|
|
return existing && WRITE_STATES.has(existing.state) ? 'uncertain' : 'blocked';
|
|
}
|
|
|
|
function canTransition(existing, executionId) {
|
|
return Boolean(existing) && String(existing.execution_id || '') === String(executionId || '');
|
|
}
|
|
|
|
return {
|
|
TERMINAL_STATES,
|
|
WRITE_STATES,
|
|
rootTaskId,
|
|
acceptanceDecision,
|
|
failureState,
|
|
canTransition
|
|
};
|
|
});
|