Class ::nsmcp::QuestionPayloadAudit (public)
::nx::Class ::nsmcp::QuestionPayloadAudit
Defined in /usr/local/ns/tcl/nsmcp/question-payload-audit.tcl
- Testcases:
- No testcase defined.
Source code: :property directory:required :property {maxBytes 268435456} :property {maxEntryBytes 16777216} :property {retention 604800} :variable mutex :method init {} { if {[file pathtype ${:directory}] ne "absolute" || ${:maxBytes}<1048576 || ${:maxEntryBytes}<1024 || ${:maxEntryBytes}>${:maxBytes} || ${:retention}<60 || ${:retention}>2592000} { error "Invalid question payload audit limits or directory" } if {![file exists ${:directory}]} { file mkdir ${:directory} file attributes ${:directory} -permissions 0700 } file lstat ${:directory} state if {$state(type) ne "directory" || ([file attributes ${:directory} -permissions]&0077)!=0} { error "Question payload audit directory must be a private directory (0700)" } set :mutex [ns_mutex create "nsmcp:payload-audit:[ns_info server]"] :cleanup } :method cleanup {} { # Called under the audit mutex after initialization. Only audit-owned # hexadecimal conversation directories and known file suffixes qualify. set cutoff [expr {[clock seconds]-${:retention}}] set used 0 foreach directory [glob -nocomplain -directory ${:directory} *] { if {![regexp {^[0-9a-f]{64}$} [file tail $directory]]} {continue} file lstat $directory state if {$state(type) ne "directory"} {continue} foreach path [glob -nocomplain -directory $directory *.payload] { file lstat $path entry if {$entry(type) ne "file"} {continue} if {$entry(mtime)<$cutoff} { file delete $path file delete -force [file rootname $path].json } else { incr used $entry(size) } } if {![llength [glob -nocomplain -directory $directory *]]} { file delete $directory } } return $used } :public method write {handle turn scope payload {label {}}} { # # Persist exact UTF-8 payload bytes before the backend handoff. # # A write or quota failure prevents that handoff. The metadata is separate # so the .payload file can be compared byte-for-byte with a captured body. # @param handle Owned conversation identifier. # @param turn Owned turn identifier. # @param scope One of the documented AI handoff boundaries. # @param payload Serialized JSON body about to cross that boundary. # @param label Bounded MCP method or tool label, when applicable. # @return Payload byte count and SHA-256 digest. # if {![regexp {^[0-9a-f]{64}$} $handle] || ![regexp {^[0-9a-f]{32}$} $turn] || $scope ni {chat-request codex-thread-start codex-turn-start codex-mcp-response} || ![regexp {^[A-Za-z0-9_./-]{0,96}$} $label]} { error "Invalid question payload audit metadata" } set bytes [encoding convertto utf-8 $payload] set size [string length $bytes] if {$size>${:maxEntryBytes}} { error "Question payload audit entry exceeds configured byte limit" } ns_mutex lock ${:mutex} try { set used [:cleanup] if {$used+$size>${:maxBytes}} { error "Question payload audit byte limit reached" } set directory [file join ${:directory} $handle] if {![file exists $directory]} { file mkdir $directory file attributes $directory -permissions 0700 } file lstat $directory state if {$state(type) ne "directory" || ([file attributes $directory -permissions]&0077)!=0} { error "Question payload audit conversation directory is not private" } set name "[clock milliseconds]-[ns_crypto::randombytes -encoding hex 8]" set destination [file join $directory $name.payload] set channel [file tempfile temporary [file join $directory pending-]] try { file attributes $temporary -permissions 0600 fconfigure $channel -translation binary -encoding binary puts -nonewline $channel $bytes close $channel set channel {} file rename $temporary $destination set temporary {} set digest [ns_crypto::md string -digest sha256 -binary -encoding hex -- $bytes] set metadata [ns_json value -- [list {} object [list server string [ns_info server] conversation string $handle turn string $turn capturedAt number [clock seconds] scope string $scope label string $label contentType string application/json payloadBytes number $size sha256 string $digest]]] set meta [open [file rootname $destination].json {WRONLY CREAT EXCL} 0600] try { fconfigure $meta -encoding utf-8 -translation lf puts $meta $metadata } finally { close $meta } } on error {message options} { if {$channel ne {}} {catch {close $channel}} if {$temporary ne {}} {catch {file delete $temporary}} catch {file delete $destination} return -options $options $message } return [dict create bytes $size sha256 $digest] } finally { ns_mutex unlock ${:mutex} } }XQL Not present: Generic, PostgreSQL, Oracle
![[i]](/resources/acs-subsite/ZoomIn16.gif)