diff --git a/api/endpoints/kb_intent_generator.php b/api/endpoints/kb_intent_generator.php index 064c868..1bc4c26 100644 --- a/api/endpoints/kb_intent_generator.php +++ b/api/endpoints/kb_intent_generator.php @@ -69,24 +69,24 @@ function normalize_pattern(string $pat): string { return '/' . $pat . '/i'; } -/* ── daily guard: skip if already ran within last 20 hours ── */ +/* ── run guard: skip if ran within last 4 hours ── */ /* Set JARVIS_FORCE_RUN=1 (env) or pass --force (argv) to bypass */ $lastRun = JarvisDB::single( "SELECT updated_at FROM kb_facts WHERE category='kb_generator' AND fact_key='last_run'" ); $forceRun = !empty(getenv('JARVIS_FORCE_RUN')) || (isset($argv[1]) && $argv[1] === '--force'); if (!$forceRun && $lastRun && (time() - strtotime($lastRun['updated_at'])) < 14400) { - log_line('Skipping – ran within last 20 hours (next run tomorrow 3am). Use --force to override.'); + log_line('Skipping – ran within last 4 hours. Use --force to override.'); exit(0); } -if ($forceRun) log_line('Force-run flag set — bypassing 20-hour guard.'); +if ($forceRun) log_line('Force-run flag set — bypassing 4-hour guard.'); log_line('Starting daily KB intent generation run.'); /* ── load active topics from database ── */ $BATCHES = JarvisDB::query( - "SELECT topic_id AS id, category, topic_name AS topic, description AS `desc` - FROM kb_generator_topics WHERE active=1 ORDER BY id ASC" + "SELECT t.topic_id AS id, t.category, t.topic_name AS topic, t.description AS `desc` + FROM kb_generator_topics t WHERE t.active=1 ORDER BY t.id ASC" ); if (empty($BATCHES)) { log_line('ERROR: No active topics in kb_generator_topics table. Add topics via the JARVIS admin panel.'); diff --git a/public_html/admin/index.php b/public_html/admin/index.php index 37837bf..c858e63 100644 --- a/public_html/admin/index.php +++ b/public_html/admin/index.php @@ -641,20 +641,26 @@ if ($action) { $name = trim($_POST['topic_name'] ?? ''); $desc = trim($_POST['description'] ?? ''); $act = (int)!empty($_POST['active']); - if (!$tid||!$cat||!$name||!$desc) bad('All fields required'); - if ($id) { - JarvisDB::execute( - 'UPDATE kb_generator_topics SET topic_id=?,category=?,topic_name=?,description=?,active=?,updated_at=NOW() WHERE id=?', - [$tid,$cat,$name,$desc,$act,$id] - ); - j(['ok'=>true,'msg'=>'Topic updated']); - } else { - JarvisDB::execute( - 'INSERT INTO kb_generator_topics (topic_id,category,topic_name,description,active) VALUES (?,?,?,?,?)', - [$tid,$cat,$name,$desc,$act] - ); - j(['ok'=>true,'msg'=>'Topic created']); + if (!$tid || !preg_match('/[a-z0-9]/', $tid) || !$cat || !$name || !$desc) + bad('All fields required — topic_id must contain at least one letter or digit'); + try { + if ($id) { + JarvisDB::execute( + 'UPDATE kb_generator_topics SET topic_id=?,category=?,topic_name=?,description=?,active=?,updated_at=NOW() WHERE id=?', + [$tid,$cat,$name,$desc,$act,$id] + ); + } else { + JarvisDB::execute( + 'INSERT INTO kb_generator_topics (topic_id,category,topic_name,description,active) VALUES (?,?,?,?,?)', + [$tid,$cat,$name,$desc,$act] + ); + } + } catch (\PDOException $e) { + bad(strpos($e->getMessage(), 'Duplicate entry') !== false + ? 'topic_id already in use — choose a different slug' + : 'Database error'); } + j(['ok'=>true,'msg'=> $id ? 'Topic updated' : 'Topic created']); case 'kb_topic_delete': $id = (int)($_POST['id'] ?? 0); if (!$id) bad('Missing id'); @@ -2557,16 +2563,16 @@ function tmFilter() {
${rows.map(t => `