@@ -193,7 +193,8 @@ CREATE TABLE IF NOT EXISTS llm_audit_logs (
193193 status TEXT NOT NULL, -- success | error | skipped
194194 error_message TEXT,
195195 related_memory_ids TEXT, -- JSON: ids the call operated on
196- metadata TEXT -- JSON: free-form extras
196+ metadata TEXT, -- JSON: free-form extras
197+ session_key TEXT -- #254 写入准入的会话键(LLM 调用行恒 NULL)
197198);
198199CREATE INDEX IF NOT EXISTS idx_llm_audit_timestamp ON llm_audit_logs(timestamp);
199200CREATE INDEX IF NOT EXISTS idx_llm_audit_source ON llm_audit_logs(trigger_source);
@@ -608,7 +609,9 @@ function toLlmAudit(row) {
608609 status : row . status ,
609610 error_message : row . error_message ?? undefined ,
610611 related_memory_ids : parseJsonArray ( row . related_memory_ids ) ,
611- metadata
612+ metadata,
613+ // #254 写入准入的会话键;LLM 调用行恒 NULL。
614+ session_key : row . session_key ?? undefined
612615 } ;
613616}
614617
@@ -725,6 +728,13 @@ export function createStore(path) {
725728 addColumn ( "mirror_state" , "applied_generation" , "ALTER TABLE mirror_state ADD COLUMN applied_generation INTEGER NOT NULL DEFAULT 0" ) ;
726729 addColumn ( "mirror_state" , "type_status" , "ALTER TABLE mirror_state ADD COLUMN type_status TEXT" ) ;
727730
731+ // #254 写入准入(第一阶段只计量):准入决策行按会话聚合——「会话内新建了几行」
732+ // 与「同话题重复间隔」都靠它算出来,所以会话键必须是可等值查询的列,而不是塞在
733+ // metadata 里做 JSON 匹配。列可空且不带 DEFAULT(存量行零重写)。索引建在加列
734+ // 之后:老库打开时列还不存在(SCHEMA 的 CREATE TABLE 只对新建库生效)。
735+ addColumn ( "llm_audit_logs" , "session_key" , "ALTER TABLE llm_audit_logs ADD COLUMN session_key TEXT" ) ;
736+ db . exec ( "CREATE INDEX IF NOT EXISTS idx_llm_audit_session ON llm_audit_logs(session_key);" ) ;
737+
728738 // Audit peer F: a legacy DB may hold a non-integer generation/applied_generation
729739 // (pre-v0.3.9 the JS gate truncated with Math.trunc and SQLite's CHECK only
730740 // enforced >= 0). Such a value is ambiguous — it cannot map to a real applied
@@ -1767,8 +1777,8 @@ export function createStore(path) {
17671777 db . prepare (
17681778 `INSERT INTO llm_audit_logs (timestamp, trigger_source, operation_type, model_id,
17691779 input_tokens, output_tokens, total_tokens, cost_usd, duration_ms, status,
1770- error_message, related_memory_ids, metadata)
1771- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
1780+ error_message, related_memory_ids, metadata, session_key )
1781+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )`
17721782 ) . run (
17731783 entry . timestamp ?? now ,
17741784 entry . trigger_source ,
@@ -1784,33 +1794,43 @@ export function createStore(path) {
17841794 JSON . stringify ( entry . related_memory_ids ?? [ ] ) ,
17851795 entry . metadata !== undefined
17861796 ? ( typeof entry . metadata === "string" ? entry . metadata : JSON . stringify ( entry . metadata ) )
1787- : null
1797+ : null ,
1798+ entry . session_key ?? null
17881799 ) ;
17891800 return toLlmAudit ( db . prepare ( "SELECT * FROM llm_audit_logs ORDER BY id DESC LIMIT 1" ) . get ( ) ) ;
17901801 }
17911802
1792- function listLlmAudits ( { limit = 50 , offset = 0 , source } = { } ) {
1803+ function listLlmAudits ( { limit = 50 , offset = 0 , source, sessionKey } = { } ) {
17931804 const { limit : lim , offset : off } = sanitizePage ( limit , offset , 50 ) ;
17941805 const clauses = [ ] ;
17951806 const params = [ ] ;
17961807 if ( source ) {
17971808 clauses . push ( "trigger_source = ?" ) ;
17981809 params . push ( source ) ;
17991810 }
1811+ // #254 写入准入:会话内的话题回填只需要该会话的行(走 idx_llm_audit_session)。
1812+ if ( sessionKey ) {
1813+ clauses . push ( "session_key = ?" ) ;
1814+ params . push ( sessionKey ) ;
1815+ }
18001816 const where = clauses . length ? `WHERE ${ clauses . join ( " AND " ) } ` : "" ;
18011817 const rows = db . prepare (
18021818 `SELECT * FROM llm_audit_logs ${ where } ORDER BY timestamp DESC, id DESC LIMIT ? OFFSET ?`
18031819 ) . all ( ...params , lim , off ) ;
18041820 return rows . map ( toLlmAudit ) ;
18051821 }
18061822
1807- function countLlmAudits ( { source } = { } ) {
1823+ function countLlmAudits ( { source, sessionKey } = { } ) {
18081824 const clauses = [ ] ;
18091825 const params = [ ] ;
18101826 if ( source ) {
18111827 clauses . push ( "trigger_source = ?" ) ;
18121828 params . push ( source ) ;
18131829 }
1830+ if ( sessionKey ) {
1831+ clauses . push ( "session_key = ?" ) ;
1832+ params . push ( sessionKey ) ;
1833+ }
18141834 const where = clauses . length ? `WHERE ${ clauses . join ( " AND " ) } ` : "" ;
18151835 return db . prepare ( `SELECT count(*) AS c FROM llm_audit_logs ${ where } ` ) . get ( ...params ) . c ;
18161836 }
0 commit comments