rankyurky asked on Discord #feedback for PenguinHarness (npm @prismshadow/penguin-cli, command penguin). Every value comes from Penguin's own source (0.2.17): - Config: $PENGUIN_HOME (else ~/.penguin/data, every OS) / $PENGUIN_PROJECT_ID (else default_project) /.project_config.toml. magpie writes [providers.magpie] (base_url, client_type, api_key), one [[models]] entry per magpie model (context_window, vision, max_tokens, [models.pricing] in usd_per_mtok) and default_model in Penguin's own inline spelling. Penguin's server reads the file again when its mtime changes, so no restart; a default naming no entry is refused by Penguin, so Check fails when one is missing. - A file magpie creates is 0600 in a 0700 folder, as Penguin keeps it. Disconnect restores the user's own default_model (or none), drops a magpie vision model, and removes the file when magpie made it. Penguin's own entries, providers and command_policy rules are kept. - A sync that changes nothing writes nothing (bytes and mtime kept), so Penguin's watcher isn't woken. - Install: curl -fsSL https://penguin.ooo/install.sh | sh, or irm https://penguin.ooo/install.ps1 | iex on Windows; update: npm, or penguin update --yes for the tarball, except on Windows where update.ts refuses it. - WSL: a penguin kind with no restart notice; the WSL test now expects an empty Notice for a kind with no restart. - agentenv: PENGUIN_HOME, PENGUIN_PROJECT_ID; the project id is in NotPaths, or appdir dropped a value like "work" as a relative path. - edit.SetTOMLArrayTables: replaces every [[name]] entry with key = value (with child tables) by blocks, or removes them; blank lines between replaced entries go with them, and an unchanged result isn't written. - GUI: penguin-color.svg icon; README (en, zh-CN), docs/reference.md and docs/subsystems/agent-wiring.md. Run (worktree atca9b5048e, GOMODCACHE/GOCACHE pinned): - go test -tags nogui -timeout 45m ./internal/agent ./internal/edit ./internal/agentenv ./internal/appdir ./internal/usage: all ok but TestWSLMoreAgentsPick (penguin has no restart notice), fixed in the test; rerun -run 'TestWSL|TestPenguin|TestEveryAgent|TestFolderVars| TestEveryNotice' ok. - internal/gui: TestCoreScriptBudget, TestAssetTypesHoldAgainstTheRegistry, icon and i18n guards ok; no budget raised. - GUI Playwright, chromium and webkit: i18n-keys, getting-started, agent-cli-missing, agent-update-all, gui-ja, gui-de, gui-zh-tw: 123 tests, 123 pass. The icon renders in colour in both engines. - gofmt -l internal/ clean; go vet -tags nogui ./... with GOOS linux, windows and darwin clean. - Real Penguin 0.2.17 (npm, Node 25) in a sandbox HOME against a sandbox magpie serve and a fake upstream: penguin run -m "Say PONG" answered "PONG from fake" through magpie (usage agent "penguin", /v1/responses -> /v1/chat/completions); switching the model in magpie took effect with no Penguin restart; a no-op sync kept bytes and mtime; magpie penguin default restored the user's custom/my-own and left no magpie line; with no prior file it was created 0600 and removed on disconnect. The fixture is the file Penguin itself wrote. - Rebased on origin/maind9a8148d7and run again there: internal/agent -run 'TestPenguin|TestWSL|TestEveryAgent|TestFolderVars|TestEveryNotice| TestSync', internal/edit, internal/agentenv and the internal/gui guards ok; gofmt clean. Broken by hand, each went red: the no-op skip (mtime test), the 0600 create, the inline default_model spelling, the own-default restore, removing magpie's file, dropping the vision model, the NotPaths entry, and both SetTOMLArrayTables fixes (blank-line growth, unchanged write). Not tried: the full ./... suite (machine load ~190-200, as asked); a Windows or WSL real binary; Penguin's desktop app; a real vendor key through Penguin. Penguin sends the openai SDK's User-Agent, so the agent is named from its key, not its UA.
247 lines
7.0 KiB
Go
247 lines
7.0 KiB
Go
package edit
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
|
|
"github.com/pelletier/go-toml/v2/unstable"
|
|
)
|
|
|
|
// SetTOMLArrayTable replaces one named array entry and its child tables, or
|
|
// appends it. block is a complete array-table declaration, including its header.
|
|
// Duplicate matches, invalid input and replacements for another entry are errors.
|
|
func SetTOMLArrayTable(path, name, key, value, block string) error {
|
|
return editTOMLArrayTable(path, name, key, value, block)
|
|
}
|
|
|
|
// DelTOMLArrayTable removes just the matching entry and its child tables.
|
|
func DelTOMLArrayTable(path, name, key, value string) error {
|
|
return editTOMLArrayTable(path, name, key, value, "")
|
|
}
|
|
|
|
func editTOMLArrayTable(path, name, key, value, block string) error {
|
|
raw, err := Read(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := validateTOML(raw); err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
lines := splitLines(string(raw))
|
|
doc, err := parseTOMLFile(lines)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
spans, err := namedTOMLArray(doc, name, key, value, false)
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
var replacement []string
|
|
if block != "" {
|
|
if err := validateTOML([]byte(block)); err != nil {
|
|
return fmt.Errorf("replacement: %w", err)
|
|
}
|
|
replacement = splitLines(block)
|
|
newDoc, err := parseTOMLFile(replacement)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
matches, err := namedTOMLArray(newDoc, name, key, value, false)
|
|
if err != nil || len(matches) == 0 || len(newDoc.root.keys) > 0 {
|
|
return fmt.Errorf("replacement must declare one %s entry with %s = %q", name, key, value)
|
|
}
|
|
// All headers must belong to this entry, including child array tables.
|
|
want := tomlHeaderParts(name)
|
|
for _, table := range newDoc.tables {
|
|
parts := tomlHeaderParts(table.name)
|
|
if !tomlDescendant(parts, want) && table.from != matches[0].from {
|
|
return fmt.Errorf("replacement contains another table")
|
|
}
|
|
}
|
|
replacement = trimBlank(replacement)
|
|
}
|
|
if len(spans) == 0 {
|
|
if block == "" {
|
|
return nil
|
|
}
|
|
out := trimBlank(lines)
|
|
if len(out) > 0 {
|
|
out = append(out, "")
|
|
}
|
|
out = append(out, replacement...)
|
|
return writeTOML(path, out)
|
|
}
|
|
var out []string
|
|
at := 0
|
|
for i, span := range spans {
|
|
out = append(out, lines[at:span.from]...)
|
|
if i == 0 {
|
|
out = append(out, replacement...)
|
|
}
|
|
at = span.to
|
|
}
|
|
out = append(out, lines[at:]...)
|
|
return writeTOML(path, out)
|
|
}
|
|
|
|
// SetTOMLArrayTables replaces every entry of the array table name whose key
|
|
// is value, with its child tables, by blocks: where the first of them was,
|
|
// else at the end of the file. Entries with another value stay as they are,
|
|
// and no blocks removes the matching ones. Each block is one complete entry,
|
|
// its header included, with key = value; anything else in one is an error.
|
|
func SetTOMLArrayTables(path, name, key, value string, blocks []string) error {
|
|
raw, err := Read(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := validateTOML(raw); err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
lines := splitLines(string(raw))
|
|
doc, err := parseTOMLFile(lines)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
spans, err := namedTOMLArray(doc, name, key, value, true)
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
var replacement []string
|
|
want := tomlHeaderParts(name)
|
|
for _, block := range blocks {
|
|
if err := validateTOML([]byte(block)); err != nil {
|
|
return fmt.Errorf("replacement: %w", err)
|
|
}
|
|
bl := splitLines(block)
|
|
newDoc, err := parseTOMLFile(bl)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
matches, err := namedTOMLArray(newDoc, name, key, value, true)
|
|
if err != nil || len(matches) == 0 || len(newDoc.root.keys) > 0 || !newDoc.tables[0].array ||
|
|
!slices.Equal(tomlHeaderParts(newDoc.tables[0].name), want) {
|
|
return fmt.Errorf("replacement must declare one %s entry with %s = %q", name, key, value)
|
|
}
|
|
for i, table := range newDoc.tables {
|
|
parts := tomlHeaderParts(table.name)
|
|
if i > 0 && (slices.Equal(parts, want) || !tomlDescendant(parts, want)) {
|
|
return fmt.Errorf("replacement contains another table")
|
|
}
|
|
}
|
|
if len(replacement) > 0 {
|
|
replacement = append(replacement, "")
|
|
}
|
|
replacement = append(replacement, trimBlank(bl)...)
|
|
}
|
|
if len(spans) == 0 {
|
|
if len(replacement) == 0 {
|
|
return nil
|
|
}
|
|
out := trimBlank(lines)
|
|
if len(out) > 0 {
|
|
out = append(out, "")
|
|
}
|
|
out = append(out, replacement...)
|
|
return writeTOML(path, append(out, ""))
|
|
}
|
|
var out []string
|
|
at := 0
|
|
for i, span := range spans {
|
|
gap := lines[at:span.from]
|
|
// The blank lines between two entries replaced went with them, or
|
|
// every sync would add another.
|
|
if i == 0 || len(trimBlank(gap)) > 0 {
|
|
out = append(out, gap...)
|
|
}
|
|
if i == 0 {
|
|
if len(replacement) == 0 {
|
|
out = trimBlank(out)
|
|
}
|
|
out = append(out, replacement...)
|
|
}
|
|
at = span.to
|
|
}
|
|
out = append(out, lines[at:]...)
|
|
if joinLinesLike(out, string(raw)) == string(raw) {
|
|
return nil
|
|
}
|
|
return writeTOML(path, out)
|
|
}
|
|
|
|
type tomlArraySpan struct{ from, to int }
|
|
|
|
// namedTOMLArray are the line spans of the entries of the array table name
|
|
// whose key is value, each followed by its child tables'; more than one such
|
|
// entry is an error unless all is set.
|
|
func namedTOMLArray(doc tomlFile, name, key, value string, all bool) ([]tomlArraySpan, error) {
|
|
want := tomlHeaderParts(name)
|
|
if len(want) == 0 {
|
|
return nil, fmt.Errorf("invalid table name %q", name)
|
|
}
|
|
// An inline array cannot safely be treated as independent table blocks.
|
|
for _, kv := range doc.root.keys {
|
|
if kv.name == name {
|
|
return nil, fmt.Errorf("%s is an inline value, not an array table", name)
|
|
}
|
|
}
|
|
var spans []tomlArraySpan
|
|
for i, table := range doc.tables {
|
|
if !slices.Equal(tomlHeaderParts(table.name), want) {
|
|
continue
|
|
}
|
|
if !table.array {
|
|
return nil, fmt.Errorf("%s is not an array table", name)
|
|
}
|
|
matched := false
|
|
for _, kv := range table.keys {
|
|
if kv.name == key && kv.scalar && kv.value == value {
|
|
matched = true
|
|
}
|
|
}
|
|
if !matched {
|
|
continue
|
|
}
|
|
if len(spans) > 0 && !all {
|
|
return nil, fmt.Errorf("multiple %s entries have %s = %q", name, key, value)
|
|
}
|
|
spans = append(spans, tomlArraySpan{table.from, tomlContentEnd(table)})
|
|
for j := i + 1; j < len(doc.tables); j++ {
|
|
parts := tomlHeaderParts(doc.tables[j].name)
|
|
if slices.Equal(parts, want) {
|
|
break
|
|
}
|
|
if tomlDescendant(parts, want) {
|
|
spans = append(spans, tomlArraySpan{doc.tables[j].from, tomlContentEnd(doc.tables[j])})
|
|
}
|
|
}
|
|
}
|
|
return spans, nil
|
|
}
|
|
|
|
func tomlContentEnd(table tomlTableSpan) int {
|
|
if len(table.keys) > 0 {
|
|
return table.keys[len(table.keys)-1].to
|
|
}
|
|
return table.from + 1
|
|
}
|
|
|
|
func tomlDescendant(parts, parent []string) bool {
|
|
return len(parts) > len(parent) && slices.Equal(parts[:len(parent)], parent)
|
|
}
|
|
|
|
func tomlHeaderParts(name string) []string {
|
|
var p unstable.Parser
|
|
p.Reset([]byte("[" + name + "]"))
|
|
if !p.NextExpression() || p.Expression().Kind != unstable.Table {
|
|
return nil
|
|
}
|
|
var parts []string
|
|
for keys := p.Expression().Key(); keys.Next(); {
|
|
parts = append(parts, string(keys.Node().Data))
|
|
}
|
|
if p.NextExpression() || p.Error() != nil {
|
|
return nil
|
|
}
|
|
return parts
|
|
}
|