stackdeck-mcp-writes 0.2.0

Safe writes for MCP servers: idempotency keys, conflict checks, in-flight locks, dry-run previews, and write receipts. Zero dependencies.
Documentation
local key = KEYS[1]
local token = ARGV[1]
local max_safe = 9007199254740991

local function uint(value)
  if not value or not string.match(value, '^%d+$') then return nil end
  local number = tonumber(value)
  if number and number <= max_safe then return number end
end

local function text(number)
  return string.format('%.0f', number)
end

local function store_error()
  return redis.error_reply('ERR malformed mcp-writes record or response')
end

local kind = redis.call('TYPE', key).ok
if kind ~= 'none' and kind ~= 'hash' then return store_error() end
local record = {}
if kind == 'hash' then
  local fields = redis.call('HGETALL', key)
  for index = 1, #fields, 2 do record[fields[index]] = fields[index + 1] end
  local created = uint(record.createdAtMs)
  if not created or not record.inputHash or record.inputHash == ''
    or not record.claimToken or record.claimToken == '' then
    return store_error()
  end
  if record.status == 'in_progress' then
    if not uint(record.leaseExpiresAtMs) or uint(record.leaseExpiresAtMs) <= created
      or record.completedAtMs or record.expiresAtMs or record.retainUntilMs
      or record.receiptJson or record.replayUnavailable then return store_error() end
  elseif record.status == 'succeeded' then
    local completed = uint(record.completedAtMs)
    local expires = uint(record.expiresAtMs)
    local retain = uint(record.retainUntilMs)
    if not completed or not expires or not retain or expires <= completed or retain < expires
      or record.leaseExpiresAtMs
      or (record.replayUnavailable ~= '0' and record.replayUnavailable ~= '1')
      or (record.replayUnavailable == '0' and (not record.receiptJson or record.receiptJson == ''))
      or (record.replayUnavailable == '1' and record.receiptJson) then
      return store_error()
    end
  else
    return store_error()
  end
end

if action == 'release' then
  if record.status ~= 'in_progress' or record.claimToken ~= token then return 0 end
  return redis.call('DEL', key)
end

local now
if ARGV[2] == '' then
  local clock = redis.call('TIME')
  now = tonumber(clock[1]) * 1000 + math.floor(tonumber(clock[2]) / 1000)
else
  now = uint(ARGV[2])
end
local duration = uint(ARGV[3])
if not now or not duration or duration == 0 or now + duration > max_safe
  or not token or token == '' then
  return redis.error_reply('ERR invalid mcp-writes timestamp or duration')
end
local deadline = now + duration

if action == 'claim' then
  local hash = ARGV[4]
  if not hash or hash == '' then return store_error() end
  if record.status == 'succeeded' and (record.replayUnavailable == '1' or now < uint(record.retainUntilMs)) then
    return {'existing', text(now), record.inputHash, record.createdAtMs,
      record.completedAtMs, record.expiresAtMs, record.retainUntilMs,
      record.replayUnavailable, record.receiptJson or ''}
  end
  if record.status == 'in_progress' then
    return {'in_progress', text(now), record.inputHash, record.createdAtMs,
      record.claimToken, record.leaseExpiresAtMs}
  end
  if kind == 'hash' then redis.call('DEL', key) end
  redis.call('HSET', key, 'status', 'in_progress', 'claimToken', token,
    'inputHash', hash, 'createdAtMs', text(now), 'leaseExpiresAtMs', text(deadline))
  return {'claimed', '0'}
end

if action == 'extend' then
  if record.status ~= 'in_progress' or record.claimToken ~= token
    or now >= uint(record.leaseExpiresAtMs) then return 0 end
  redis.call('HSET', key, 'leaseExpiresAtMs', text(math.max(deadline, uint(record.leaseExpiresAtMs))))
  return 1
end

if action == 'complete' then
  local retain = uint(ARGV[4])
  local marker = ARGV[5]
  local receipt = ARGV[6]
  if not retain or retain < duration or now + retain > max_safe
    or (marker ~= '0' and marker ~= '1')
    or (marker == '0' and (not receipt or receipt == '')) then return store_error() end
  if record.claimToken ~= token then return 0 end
  if record.status == 'succeeded' then return 1 end
  redis.call('HSET', key, 'status', 'succeeded', 'completedAtMs', text(now),
    'expiresAtMs', text(deadline), 'retainUntilMs', text(now + retain), 'replayUnavailable', marker)
  redis.call('HDEL', key, 'leaseExpiresAtMs')
  if marker == '0' then
    redis.call('HSET', key, 'receiptJson', receipt)
    redis.call('PEXPIRE', key, text(retain))
  else
    redis.call('PERSIST', key)
  end
  return 1
end

return store_error()