refactor: centralize task runtime contracts (#199)

This commit is contained in:
Eyejoker
2026-05-30 01:56:41 +09:00
committed by GitHub
parent 1531482363
commit 37b57b20bb
14 changed files with 152 additions and 77 deletions

View File

@@ -1,4 +1,8 @@
import { Database } from 'bun:sqlite';
import {
DEFAULT_TASK_CONTEXT_MODE,
WATCH_CI_PROMPT_PREFIX,
} from 'ejclaw-runners-shared';
import {
AgentType,
@@ -70,7 +74,7 @@ export function createTaskInDatabase(
task.prompt,
task.schedule_type,
task.schedule_value,
task.context_mode || 'isolated',
task.context_mode || DEFAULT_TASK_CONTEXT_MODE,
task.next_run,
task.status,
task.created_at,
@@ -217,10 +221,10 @@ export function hasActiveCiWatcherForChatInDatabase(
const row = database
.prepare(
`SELECT 1 FROM scheduled_tasks
WHERE chat_jid = ? AND status = 'active' AND prompt LIKE '[BACKGROUND CI WATCH]%'
WHERE chat_jid = ? AND status = 'active' AND prompt LIKE ?
LIMIT 1`,
)
.get(chatJid);
.get(chatJid, `${WATCH_CI_PROMPT_PREFIX}%`);
return !!row;
}

View File

@@ -1,4 +1,5 @@
import { CronExpressionParser } from 'cron-parser';
import { normalizeTaskContextMode } from 'ejclaw-runners-shared';
import { createTask, findDuplicateCiWatcher } from '../db.js';
import { normalizeStoredAgentType } from '../db/room-registration.js';
@@ -49,10 +50,7 @@ export function handleScheduleTask(
const taskId =
data.taskId ||
`task-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
const contextMode =
data.context_mode === 'group' || data.context_mode === 'isolated'
? data.context_mode
: 'isolated';
const contextMode = normalizeTaskContextMode(data.context_mode);
const scheduledAgentType =
normalizeStoredAgentType(data.agent_type) ??
target.room.agentType ??

View File

@@ -1,43 +1,39 @@
import { describe, expect, it } from 'vitest';
import {
isBotMessageSourceKind,
inferMessageSourceKindFromBotFlag,
normalizeMessageSourceKind,
resolveInjectedMessageSourceKind,
} from './message-source.js';
import { DEFAULT_MESSAGE_SOURCE_KIND, MESSAGE_SOURCE_KINDS } from './types.js';
describe('message source helpers', () => {
it('defaults IPC injected messages to trusted human-equivalent provenance', () => {
describe('message source kind helpers', () => {
it('uses the canonical source kind list', () => {
expect(MESSAGE_SOURCE_KINDS).toEqual([
'human',
'bot',
'trusted_external_bot',
'ipc_injected_human',
'ipc_injected_bot',
]);
expect(DEFAULT_MESSAGE_SOURCE_KIND).toBe('human');
});
it('normalizes unknown source kinds to the default', () => {
expect(normalizeMessageSourceKind('trusted_external_bot')).toBe(
'trusted_external_bot',
);
expect(normalizeMessageSourceKind('unknown')).toBe('human');
});
it('derives source kinds from legacy and injected inputs', () => {
expect(inferMessageSourceKindFromBotFlag(true)).toBe('bot');
expect(inferMessageSourceKindFromBotFlag(false)).toBe('human');
expect(resolveInjectedMessageSourceKind({ treatAsHuman: true })).toBe(
'trusted_external_bot',
);
expect(
isBotMessageSourceKind(
resolveInjectedMessageSourceKind({ treatAsHuman: true }),
),
).toBe(false);
});
it('defaults non-human IPC injected messages to bot-equivalent provenance', () => {
expect(resolveInjectedMessageSourceKind({ treatAsHuman: false })).toBe(
'ipc_injected_bot',
);
expect(
isBotMessageSourceKind(
resolveInjectedMessageSourceKind({ treatAsHuman: false }),
),
).toBe(true);
});
it('honors valid explicit source kinds and normalizes invalid values', () => {
expect(
resolveInjectedMessageSourceKind({
treatAsHuman: true,
sourceKind: 'ipc_injected_human',
}),
).toBe('ipc_injected_human');
expect(normalizeMessageSourceKind('bot', 'human')).toBe('bot');
expect(normalizeMessageSourceKind('not-real', 'human')).toBe('human');
});
});

View File

@@ -1,19 +1,19 @@
import type { MessageSourceKind } from './types.js';
import {
DEFAULT_MESSAGE_SOURCE_KIND,
MESSAGE_SOURCE_KINDS,
type MessageSourceKind,
} from './types.js';
const MESSAGE_SOURCE_KINDS = new Set<MessageSourceKind>([
'human',
'bot',
'trusted_external_bot',
'ipc_injected_human',
'ipc_injected_bot',
]);
const MESSAGE_SOURCE_KIND_SET = new Set<MessageSourceKind>(
MESSAGE_SOURCE_KINDS,
);
export function normalizeMessageSourceKind(
value: unknown,
fallback: MessageSourceKind = 'human',
fallback: MessageSourceKind = DEFAULT_MESSAGE_SOURCE_KIND,
): MessageSourceKind {
return typeof value === 'string' &&
MESSAGE_SOURCE_KINDS.has(value as MessageSourceKind)
MESSAGE_SOURCE_KIND_SET.has(value as MessageSourceKind)
? (value as MessageSourceKind)
: fallback;
}
@@ -25,7 +25,7 @@ export function isBotMessageSourceKind(kind: MessageSourceKind): boolean {
export function inferMessageSourceKindFromBotFlag(
isBotMessage: boolean | number | null | undefined,
): MessageSourceKind {
return isBotMessage ? 'bot' : 'human';
return isBotMessage ? 'bot' : DEFAULT_MESSAGE_SOURCE_KIND;
}
export function resolveInjectedMessageSourceKind(args: {

View File

@@ -1,3 +1,5 @@
import type { ArbiterVerdict } from './types.js';
export type VisibleVerdict =
| 'step_done'
| 'task_done'
@@ -7,12 +9,7 @@ export type VisibleVerdict =
| 'needs_context'
| 'continue';
export type ArbiterVerdictResult =
| 'proceed'
| 'revise'
| 'reset'
| 'escalate'
| 'unknown';
export type ArbiterVerdictResult = ArbiterVerdict | 'unknown';
const VISIBLE_VERDICT_SCAN_LINE_LIMIT = 5;
@@ -81,7 +78,7 @@ export function classifyArbiterVerdict(
const normalized = verdictMatch[1].toLowerCase();
return normalized === 'continue'
? 'proceed'
: (normalized as ArbiterVerdictResult);
: (normalized as ArbiterVerdict);
}
return 'unknown';
}

View File

@@ -1,3 +1,5 @@
import { WATCH_CI_PROMPT_PREFIX } from 'ejclaw-runners-shared';
import { TIMEZONE } from './config.js';
import type { ScheduledTask } from './types.js';
import { formatElapsedKorean } from './utils.js';
@@ -8,7 +10,7 @@ export type WatcherStatusPhase =
| 'retrying'
| 'completed';
export const WATCH_CI_PREFIX = '[BACKGROUND CI WATCH]';
export const WATCH_CI_PREFIX = WATCH_CI_PROMPT_PREFIX;
export const TASK_STATUS_MESSAGE_PREFIX = '\u2063\u2063\u2063';
export const DEFAULT_WATCH_CI_MAX_DURATION_MS = 24 * 60 * 60 * 1000;

View File

@@ -4,6 +4,7 @@ import {
normalizePairedRoomRole,
normalizePairedRoomRoleOrNull,
type PairedRoomRole,
type TaskContextMode,
} from 'ejclaw-runners-shared';
import type { VisibleVerdict } from './paired-verdict.js';
@@ -208,12 +209,15 @@ export interface RegisteredGroup {
workDir?: string; // Working directory for the agent (defaults to group folder)
}
export type MessageSourceKind =
| 'human'
| 'bot'
| 'trusted_external_bot'
| 'ipc_injected_human'
| 'ipc_injected_bot';
export const MESSAGE_SOURCE_KINDS = [
'human',
'bot',
'trusted_external_bot',
'ipc_injected_human',
'ipc_injected_bot',
] as const;
export type MessageSourceKind = (typeof MESSAGE_SOURCE_KINDS)[number];
export const DEFAULT_MESSAGE_SOURCE_KIND: MessageSourceKind = 'human';
export interface NewMessage {
id: string;
@@ -242,7 +246,7 @@ export interface ScheduledTask {
prompt: string;
schedule_type: 'cron' | 'interval' | 'once';
schedule_value: string;
context_mode: 'group' | 'isolated';
context_mode: TaskContextMode;
next_run: string | null;
last_run: string | null;
last_result: string | null;

View File

@@ -1,4 +1,8 @@
import { CronExpressionParser } from 'cron-parser';
import {
DEFAULT_TASK_CONTEXT_MODE,
isTaskContextMode,
} from 'ejclaw-runners-shared';
import { TIMEZONE } from './config.js';
import { isWatchCiTask } from './task-watch-status.js';
@@ -100,10 +104,6 @@ function isScheduleType(
return value === 'cron' || value === 'interval' || value === 'once';
}
function isContextMode(value: unknown): value is ScheduledTask['context_mode'] {
return value === 'group' || value === 'isolated';
}
function isAgentType(value: unknown): value is AgentType {
return value === 'claude-code' || value === 'codex';
}
@@ -294,9 +294,9 @@ async function handleTaskCreateRoute(
return jsonResponse({ error: next.error }, { status: 400 });
}
const contextMode = isContextMode(body.contextMode)
const contextMode = isTaskContextMode(body.contextMode)
? body.contextMode
: 'isolated';
: DEFAULT_TASK_CONTEXT_MODE;
const agentType = isAgentType(body.agentType)
? body.agentType
: (room.group.agentType ?? 'claude-code');