::nsmcp::Object ::nsmcp::Object destroy dict getdef ::nx::Object ::nx::Object ::nsmcp::Object->::nx::Object ::nsmcp::QuestionPayloadAudit ::nsmcp::QuestionPayloadAudit cleanup init write ::nsmcp::QuestionPayloadAudit->::nsmcp::Object

Class ::nsmcp::QuestionPayloadAudit

::nsmcp::QuestionPayloadAudit[i] create ... \
           [ -directory:required directory:required ] \
           [ -maxBytes (default "268435456") ] \
           [ -maxEntryBytes (default "16777216") ] \
           [ -mutex mutex ] \
           [ -retention (default "604800") ]

Defined in /usr/local/ns/tcl/nsmcp/question-payload-audit.tcl

Class Relations

  • class: ::nx::Class[i]
  • superclass: ::nsmcp::Object[i]
::nx::Class create ::nsmcp::QuestionPayloadAudit \
     -superclass ::nsmcp::Object

Methods (to be applied on instances)

  • write (scripted, public)

     <instance of nsmcp::QuestionPayloadAudit[i]> 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.

    Parameters:
    handle (required)
    Owned conversation identifier.
    turn (required)
    Owned turn identifier.
    scope (required)
    One of the documented AI handoff boundaries.
    payload (required)
    Serialized JSON body about to cross that boundary.
    label (optional)
    Bounded MCP method or tool label, when applicable.
    Returns:
    Payload byte count and SHA-256 digest.

    Testcases:
    No testcase defined.
    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}
    }