scratchtool

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 12 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

scratchtool

scratchtool provides a session-scoped key/value scratchpad for ds4go.ToolLoop: small, durable working memory for the model that is neither the chat log nor the user's project tree.

A pad is a flat map of key → text blob, persisted one file per key under $DS4_DIR/scratch/<session>/. The model lists keys, reads a value (optionally only its first or last bytes), replaces a value, appends to a value, and deletes a key. There are no directories, nested keys, or search; a queryable store would go behind this same API.

Usage

Register the full tool set:

package main

import (
	"log"

	ds4go "github.com/NimbleMarkets/ds4go"
	"github.com/NimbleMarkets/ds4go/scratchtool"
)

func main() {
	store, err := scratchtool.New(scratchtool.Config{Session: "mytask"})
	if err != nil {
		log.Fatal(err)
	}
	defer store.Close()

	reg := ds4go.NewToolRegistry()
	if err := store.Register(reg); err != nil {
		log.Fatal(err)
	}

	// Use reg with ds4go.ToolLoop. Optionally prepend
	// scratchtool.SystemHint to the system prompt.
}

Register read-only access to an existing pad:

store, err := scratchtool.New(scratchtool.Config{
	Session:  "mytask",
	ReadOnly: true,
})
if err != nil {
	return err
}
defer store.Close()

reg := ds4go.NewToolRegistry()
if err := store.RegisterReadOnly(reg); err != nil {
	return err
}

Tool Sets

Register registers:

Tool Purpose
scratch_list List keys with sizes and modification times.
scratch_get Read one value, optionally only its head or tail bytes.
scratch_set Replace one value, creating the key if missing.
scratch_append Append to one value, creating the key if missing.
scratch_delete Delete one key.

RegisterReadOnly registers only scratch_list and scratch_get. When Config.ReadOnly is set, Register returns an error instead of advertising write tools, so a registered tool is always usable.

Keys and Limits

Keys are flat names validated on every call:

  • 1–64 characters from [a-z0-9._-]
  • no leading . or -, no ..
  • index.json is reserved

Suggested starter keys (not enforced): plan, findings, open, user, session.

Limits are enforced before every write, and exceeding one returns an ERROR: ... observation naming the limit:

Limit Default
MaxKeys 32
MaxValueBytes 16 KiB
MaxTotalBytes 128 KiB

Persistence

Each key is one file in the session directory, written atomically: a temp file in the same directory renamed over the destination, so a mid-write failure never leaves a truncated value. Per-key file I/O goes through an os.Root handle confined to the session directory, and reads reject symlinks and other non-regular files, so a planted symlink can neither leak an outside file into the model's context nor be copied into the pad. A Store is safe for concurrent use within one process. There is no cross-process locking: two CLIs sharing a session name can race each other's writes.

Routine tool failures — an unknown key, a bad key name, an exceeded limit — are returned to the model as ERROR: ... tool results, so a ToolLoop run continues and the model can correct itself. Only context cancellation aborts the loop.

Available Tools

scratch_list

List keys, one per line: key, size in bytes, last-modified time (UTC RFC 3339). An empty pad returns OK: no keys.

Arguments:

{}

Output:

findings  2081  2026-09-19T15:04:02Z
plan  412  2026-09-19T15:02:11Z
scratch_get

Read one value. head returns only the first N bytes, tail only the last N bytes (bytes, not runes); set at most one. A header line precedes the value:

key=plan bytes=412 truncated=false

Arguments:

{
  "key": "plan",
  "head": 0,
  "tail": 0
}
scratch_set

Replace one value, creating the key if missing.

Arguments:

{
  "key": "plan",
  "value": "..."
}
scratch_append

Append to one value, creating the key if missing. Limits apply to the combined size.

Arguments:

{
  "key": "findings",
  "value": "\n- foo"
}
scratch_delete

Delete one key. Deleting a missing key is an error, not a silent success.

Arguments:

{
  "key": "plan"
}

Configuration

Field Description
Dir Session directory. Defaults to DefaultDir(Session): $DS4_DIR/scratch/<session>/.
Session Pad namespace. Defaults to "default"; must pass the same rules as pad keys.
MaxKeys Maximum number of keys. Defaults to 32.
MaxValueBytes Maximum bytes per value. Defaults to 16 KiB.
MaxTotalBytes Maximum bytes across all values. Defaults to 128 KiB.
ReadOnly Makes Register fail; use RegisterReadOnly.

Documentation

Overview

Package scratchtool provides a session-scoped key/value scratchpad tool for ds4go tool loops.

A scratchpad is small, durable working memory for the model that is neither the chat log nor the user's project tree: a flat map of key → text blob, persisted one file per key under $DS4_DIR/scratch/<session>/. The model lists keys, reads a value (optionally head or tail bytes), replaces a value, appends to a value, and deletes a key.

Keys are flat names of 1–64 characters from [a-z0-9._-]; there are no directories or nested paths. Limits (MaxKeys, MaxValueBytes, MaxTotalBytes) are enforced before every write. Routine failures are returned to the model as "ERROR: ..." tool results so a tool loop can continue; only context cancellation aborts the loop.

Writes are atomic (temp file renamed over the destination) and a Store is safe for concurrent use within one process. Per-key file I/O is confined to the session directory through an os.Root handle, and reads reject symlinks and other non-regular files. Two processes sharing a session are not coordinated and can race each other's writes.

A typical setup registers the scratch tools alongside other tools:

store, err := scratchtool.New(scratchtool.Config{Session: "mytask"})
if err != nil {
	return err
}
defer store.Close()

reg := ds4go.NewToolRegistry()
if err := store.Register(reg); err != nil {
	return err
}
system := scratchtool.SystemHint + "\n\n" + mySystemPrompt

A read-only registration advertises only scratch_list and scratch_get:

store, err := scratchtool.New(scratchtool.Config{
	Session:  "mytask",
	ReadOnly: true,
})
if err != nil {
	return err
}
defer store.Close()

if err := store.RegisterReadOnly(reg); err != nil {
	return err
}

Index

Constants

View Source
const SystemHint = "" /* 163-byte string literal not displayed */

SystemHint is an optional line callers can prepend to the system prompt when the scratch tools are registered.

Variables

This section is empty.

Functions

func DefaultDir

func DefaultDir(session string) string

DefaultDir returns the default session directory for a scratchpad session: the "scratch/<session>" subdirectory of the ds4go data directory (DS4_DIR, default ~/.ds4). An empty session means "default".

Types

type Config

type Config struct {
	// Dir is the session directory. Empty → DefaultDir(Session).
	Dir string
	// Session names the pad namespace. Empty → "default".
	// Must pass the same key rules as pad keys.
	Session string
	// MaxKeys default 32.
	MaxKeys int
	// MaxValueBytes default 16KiB.
	MaxValueBytes int
	// MaxTotalBytes default 128KiB.
	MaxTotalBytes int
	// ReadOnly registers get/list only.
	ReadOnly bool
}

Config controls scratchpad storage and limits.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is one session's key/value scratchpad, persisted one file per key.

func New

func New(cfg Config) (*Store, error)

New creates the session directory if needed and returns a Store for it.

The session name must pass the same rules as pad keys, so a session can never escape the scratch base directory.

func (*Store) Close

func (s *Store) Close() error

Close releases the session-directory handle. Every mutation is already durable via write-temp-and-rename, so Close flushes nothing.

func (*Store) Register

func (s *Store) Register(reg *ds4.ToolRegistry) error

Register adds scratch_list, scratch_get, scratch_set, scratch_append, and scratch_delete to reg. It fails when Config.ReadOnly is set, so registered tools are always usable; use RegisterReadOnly instead.

func (*Store) RegisterReadOnly

func (s *Store) RegisterReadOnly(reg *ds4.ToolRegistry) error

RegisterReadOnly adds scratch_list and scratch_get to reg.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL