fix(run-housekeeping): gate auto knowledge_base writes on task type or department

This commit is contained in:
ZacharyZhang-NY committed 2026-04-16 22:56:16 -04:00
1 parent 1b76a51894
commit 6429c76ac7
1 file changed
+56 -2
+56 -2
View File
@@ -7,6 +7,53 @@ interface RunHousekeepingDeps {
threadIntegration: ThreadIntegrationService;
}
/**
* Task types whose successful runs produce durable lessons worth capturing
* into the knowledge base. Ad-hoc code tasks, chores, and one-off ops
* produce runtime noise that pollutes the knowledge table if auto-saved.
*/
const KNOWLEDGE_PRODUCING_TASK_TYPES: ReadonlySet<string> = new Set([
"research",
"analysis",
"planning",
"strategy",
"review",
]);
/**
* Agent departments whose output is routinely curated as corporate
* knowledge regardless of task type.
*/
const KNOWLEDGE_PRODUCING_DEPARTMENTS: ReadonlySet<string> = new Set([
"research",
"strategy",
"management",
]);
function shouldProduceKnowledge(
db: AppDatabase,
taskId: string,
agentId: string,
): boolean {
try {
const taskTypeRow = db.queryAll<{ taskType: string | null }>(
"SELECT task_type as taskType FROM tasks WHERE id = ? LIMIT 1",
taskId,
)[0];
if (taskTypeRow?.taskType && KNOWLEDGE_PRODUCING_TASK_TYPES.has(taskTypeRow.taskType)) {
return true;
}
} catch {
// Task deleted; fall through to agent check
}
try {
const agent = db.getAgent(agentId);
return Boolean(agent.department && KNOWLEDGE_PRODUCING_DEPARTMENTS.has(agent.department));
} catch {
return false;
}
}
export function createRunHousekeeping({ db, logger, threadIntegration }: RunHousekeepingDeps) {
return {
onRunCompleted(
@@ -25,8 +72,15 @@ export function createRunHousekeeping({ db, logger, threadIntegration }: RunHous
logger.error(`[run-housekeeping] threadIntegration.onRunFinished failed: ${err instanceof Error ? err.message : String(err)}`);
}
// 2. On success with a meaningful summary, create a knowledge candidate
if (status === "succeeded" && summary && summary.length > 50) {
// 2. On success with a meaningful summary, create a knowledge candidate —
// gated on the task type or the producing agent's department so
// ad-hoc runs do not flood the knowledge table with summaries.
if (
status === "succeeded" &&
summary &&
summary.length > 50 &&
shouldProduceKnowledge(db, taskId, agentId)
) {
try {
let topic = `Run ${runId.slice(0, 8)}`;
try {