prefix . SA_ORCH_DB_PREFIX . 'actor_handoff'; } /** * Record (or update) the committer for an actor-authored comment. * Idempotent on (kind, comment_id) — re-recording replaces the prior row. * * Returns true on success, false on invalid input or DB failure. */ public static function record_committer( string $kind, int $comment_id, int $committed_by_user_id ): bool { if ( $comment_id <= 0 || $committed_by_user_id <= 0 ) { return false; } $kind = sanitize_key( $kind ); if ( $kind === '' ) return false; global $wpdb; $tbl = self::table(); // Replace prior row, if any. Keeps the table thin. $wpdb->delete( $tbl, [ 'comment_kind' => $kind, 'comment_id' => $comment_id, ] ); $ok = $wpdb->insert( $tbl, [ 'comment_kind' => $kind, 'comment_id' => $comment_id, 'committed_by_user_id' => $committed_by_user_id, 'recorded_at' => current_time( 'mysql', true ), ] ); return $ok !== false; } /** * Get the recorded committer for a comment, or null if none recorded. */ public static function get_committer( string $kind, int $comment_id ): ?int { if ( $comment_id <= 0 ) return null; $kind = sanitize_key( $kind ); if ( $kind === '' ) return null; global $wpdb; $row = $wpdb->get_var( $wpdb->prepare( "SELECT committed_by_user_id FROM " . self::table() . " WHERE comment_kind = %s AND comment_id = %d ORDER BY id DESC LIMIT 1", $kind, $comment_id ) ); return $row !== null ? (int) $row : null; } /** * Detect operator mode for a comment. Returns one of the MODE_* constants. * Never lies about uncertainty — returns 'unknown' rather than guessing. */ public static function detect_mode( string $kind, int $comment_id ): string { $committer = self::get_committer( $kind, $comment_id ); if ( $committer === null ) { return self::MODE_UNKNOWN; } /** * Filter that signals whether an LLM token row exists for this commit. * Returns: true (token row found → AI mode), * false (no token row → human mode), * null (unknown — token log not yet integrated, default). * * Board #16 will register a callback that consults the token log. */ $token_present = apply_filters( 'sa_orch_handoff_has_llm_token', null, $kind, $comment_id ); if ( $token_present === null ) return self::MODE_UNKNOWN; if ( $token_present === true ) return self::MODE_AI; return self::MODE_HUMAN; } }