// 설명 문서: docs/specs/openwork-cli-provider.md (v2.0.0) // DLP 실패 위치만 관측한다. 원문·preview·해시·사용자 정의 키는 로그에 넣지 않는다. const scanner = require('../utils/secret-scanner'); const LIMIT = 32; function findings(text, pointer) { if (typeof text !== 'string') return []; const result = scanner.scan(text), records = []; const category = (token, index) => /https?:$/.test(text.slice(Math.max(0, index - 6), index)) ? 'url' : token.startsWith('/') || /^[A-Za-z]:[\\/]/.test(token) ? 'path' : 'token'; for (const finding of result.findings) { if (finding.type === 'high-entropy-token') { for (const match of text.matchAll(/[A-Za-z0-9+/_-]{24,}/g)) { if (scanner.shannonEntropy(match[0]) < 4) continue; records.push({ pointer, findingType: finding.type, length: match[0].length, category: category(match[0], match.index) }); if (records.length >= LIMIT) return records; } } else { const signature = scanner.SIGNATURES.find(item => item.name === finding.type), match = signature?.re.exec(text); const token = match?.[1] || match?.[0]; records.push({ pointer, findingType: finding.type, length: token ? token.length : null, category: token ? category(token, match.index) : 'token' }); } } return records.slice(0, LIMIT); } function inspect(request, prompt, originalRequest = request) { if (scanner.scan(prompt).clean) return []; const result = [], add = (value, pointer) => { if (result.length < LIMIT) result.push(...findings(value, pointer).slice(0, LIMIT - result.length)); }; // 경로는 서버가 정한 고정 구조와 배열 인덱스만 사용한다. 스키마 내부 키는 공개하지 않는다. request.messages.forEach((message, index) => { const first = result.length; add(message.content, `/messages/${index}/content`); if (['system', 'developer'].includes(message.role) && result.length > first) { const original = originalRequest.messages[index]?.content; const tokens = new Set(String(message.content).match(/[A-Za-z0-9+/_-]{24,}/g) || []); const publicStaticIds = [['openwork-cloud_execute_capability', 'public_cloud_execute'], ['openwork-cloud_search_capabilities', 'public_cloud_search']] .filter(([token]) => tokens.has(token)).map(([, id]) => id); const sectionPresence = require('./cli-system-steering').inspectSteering(typeof original === 'string' ? original : ''); for (let offset = first; offset < result.length; offset++) Object.assign(result[offset], { publicStaticIds, sectionPresence }); } message.tool_calls?.forEach((call, callIndex) => { add(call.id, `/messages/${index}/tool_calls/${callIndex}/id`); add(call.function.name, `/messages/${index}/tool_calls/${callIndex}/function/name`); add(call.function.arguments, `/messages/${index}/tool_calls/${callIndex}/function/arguments`); }); add(message.tool_call_id, `/messages/${index}/tool_call_id`); }); request.tools.forEach((tool, index) => { add(tool.function.name, `/tools/${index}/function/name`); add(tool.function.description, `/tools/${index}/function/description`); add(JSON.stringify(tool.function.parameters), `/tools/${index}/function/parameters`); }); add(JSON.stringify(request.pathAliases || []), '/pathAliases'); // 직렬화 경계/출력 계약 지침에서만 재현되는 실패도 사용자 원문 없이 구분한다. if (!result.length) add(prompt, '/'); return result; } function record(request, prompt, log = value => require('../utils/debug-log').writeDebugLog(value), originalRequest = request) { try { for (const detail of inspect(request, prompt, originalRequest)) { Promise.resolve(log('[OpenWork CLI DLP] ' + JSON.stringify({ stage: 'projection_prompt', ...detail }))).catch(() => {}); } } catch { /* 진단 실패가 기존 DLP 판정이나 정산 흐름을 변경하지 않는다. */ } } module.exports = { inspect, record };