Files
ruflo/plugins/ruflo-console/hooks/data/plugin-catalog.ts
T
ea25a05abe fix(ruflo-console): Windows writes go through the host filesystem (#4433) (#4434)
* test(console): fixture file system accepts Windows drive paths

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(console): Windows project folders are project folders (ADRs, export, catalog, mission cost)

Guards that accepted only a path starting with "/" treated a Windows session
folder (C:\Users\x\proj) as "no project folder is known". Adds hooks/data/paths.ts
(pure string helpers: isAbsolutePath, joinPath, samePath, plus hasParentSegment,
normalizePath, below, trimTrailing, isWindowsPath) and uses it at the absolute-path
guards in adr, event-export, wf-file, plugin-catalog, mission-cost (and the ledger
install path), wf-worktrees, skills-lab, wf-drill-io, facts, ap-envelope and
register. Each caller keeps its own ".." refusal; POSIX paths take the same code
path as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(console): protected autopilot folders match ignoring case on Windows paths

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(console): find the home folder on Windows (sessions, plugin health, What's new)

Implements homeOf function that checks HOME → USERPROFILE → HOMEDRIVE+HOMEPATH
with comprehensive test coverage. Fixes state.home initialization on Windows
where HOME is unset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(console): disk writes work on Windows through the host file API

Windows has none of uname, dd, sh, install, mkdir or cp in Claude Code's process
environment, so every console write (Events, Timeline, autopilot journal, ADRs,
exports, .gitignore) failed there. New write flavor host-fs, chosen on win32 (no
uname is run), carries the enumerated writes through $.fs.read/write/exists via
writeVia. Linux GNU and macOS/BSD argv are unchanged (pinned by test).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(console): autopilot kill flag can be cleared on Windows; document host-fs create window

Windows has no delete, so clearKill rewrites the KILL flag with a fixed marker and
killSeen treats exactly that content as not set (host-fs only); stopNow rewrites
the flag empty so a second stop is seen. POSIX rm/stat behaviour is unchanged.
write-via and wf-file now say host-fs creates are check-then-write (no O_EXCL).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(console): no Linux-only process probe on Windows; clear probe timers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(console): a hive started from the console shows at once

When a start's CLI run exited 0 but its verify fails (the CLI can return
before its state file lands), wait 1 s via host.after, read fresh and verify
once more before reporting the "expected ..." text. No retry when the run
failed; exactly one retry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(console): pin Windows containment, parent-segment and UNC rules in paths.ts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(console): workspace keys equal across drive-letter case and trailing separator

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(console): autopilot start is refused on Windows until atomic appends and Windows-aware path checks exist

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(console): 0.42.2 changelog and version

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(console): read home variables by literal name so the module loads

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* post-merge cleanup: finish #4036 integration

The Option B merge commit (4313013cb) landed cleanly against upstream/main
but a batch of merge-resolution edits in the working tree were not persisted
into the commit itself. This commit finishes the integration:

Path-helper consolidation (abs-path.ts → paths.ts):
- hooks/data/wf-drill-io.ts: add missing paths.ts import for below,
  hasParentSegment, isAbsolutePath, samePath, trimTrailing (fixes 4
  wf-drill-read.spec.ts failures that threw `ReferenceError: isAbsolutePath
  is not defined` at runtime).
- hooks/adr.ts: migrate from ./data/abs-path to ./data/paths, pulling in
  joinPath which is needed at lines 89/94.
- hooks/register.ts: drop the rootedPath import from the deleted
  abs-path.ts; inline the equivalent isAbsolutePath+joinPath expression.
- hooks/data/event-export.ts, hooks/data/wf-file.ts: drop the duplicate
  isAbsolutePath import from the deleted ./abs-path (the main-side wider
  './paths' import already covers it).

VERIFY_RETRY_MS lifecycle (ADR-450 interaction with Marius' feature):
- hooks/runner.ts: extend the retry condition — `verified === 'n/a' &&
  spec.verify !== undefined` also retries, so a snapshot that hasn't
  loaded yet (null, not "no verifier") gets the one-second re-check.

Tests brought in line with the new timing / platform guards:
- tests/tui-actions.spec.ts: settle(600) → settle(1200) at the point the
  runner's VERIFY_RETRY_MS one-second re-check has to resolve before the
  verdict is drawn.
- tests/windows-drive-path-sites.spec.ts: fsWithDir made separator-agnostic
  via a normSeps helper, since joinPath now uses the base path's own
  separator (`\` on a Windows base).

Version bump:
- .claude-plugin/plugin.json, hooks/version.ts, scripts/smoke.sh: 0.44.7
  → 0.44.8.
- CHANGELOG.md: prepend 0.44.8 entry crediting #4036 and @socrates6969.

Verified: full console suite 3400 total · 3362 pass · 15 fail · 23 skipped.
Every one of the 15 remaining fails is pre-existing on main or a
platform/env limitation (Windows chmod, fake-timer timing, codex not
installed) — zero merge-caused failures remain.

Co-Authored-By: RuFlo <ruv@ruv.net>

---------

Co-authored-by: Marius Carlsson <109006081+socrates6969@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Mario Jauvin <marioja@users.noreply.github.com>
Co-authored-by: r <r@x>
2026-10-23 14:04:03 -04:00

171 lines
8.3 KiB
TypeScript

/**
* The ruflo marketplace as a catalog: every plugin its clone lists, with the skills, agents, commands, MCP config and
* mod (function hooks) each ships, read from the clone on disk. An external plugin (listed by a git source, maintained in
* another repository) has no copy in the clone: it is read from its install path in `installed_plugins.json` when it is
* installed. Read-only, bounded, nothing run: a file over the cap, a name that is not a plain word and a path with `..`
* are skipped, and every read may be refused.
*/
import type { ReaderFs } from './files'
import { plain } from './parse'
import { hasParentSegment, isAbsolutePath, joinPath } from './paths'
export type CatalogPlugin = {
name: string
description: string
version: string | null
/** Folder of the plugin inside the clone, or of the installed copy of an external one (absolute); '' when there is neither. */
dir: string
skills: string[]
agents: string[]
commands: string[]
hasMcp: boolean
/** The plugin.json userConfig keys: the options Settings can edit. */
options: string[]
/** A `hooks/register.ts` makes it a mod: function hooks run in the engine. */
isMod: boolean
/** Set for an external plugin: the repository, folder and ref the marketplace pins it to. */
external?: string
}
const NAME = /^[A-Za-z0-9._-]{1,80}$/
const PLUGINS_MAX = 200
const LIST_MAX = 200
const DOC_BYTES = 120_000
/** Lines of a SKILL.md or command file the Result panel shows. */
export const DOC_LINES = 40
const recordOf = (value: unknown): Record<string, unknown> | null => (typeof value === 'object' && value !== null && !Array.isArray(value) ? (value as Record<string, unknown>) : null)
const safeSource = (source: unknown): string | null =>
typeof source === 'string' && source.startsWith('./') && !source.split('/').includes('..') && source.length < 200 ? source.slice(2).replace(/\/+$/, '') : null
async function namesIn(fs: ReaderFs, dir: string, kind: 'dir' | 'md'): Promise<string[]> {
const entries = await fs.list(dir).catch(() => [])
return entries
.flatMap(entry => {
const isDir = entry.kind === undefined ? !entry.name.includes('.') : entry.kind === 'directory' || entry.kind === 'dir'
const name = kind === 'md' ? entry.name.replace(/\.md$/, '') : entry.name
return (kind === 'md' ? entry.name.endsWith('.md') && !isDir : isDir) && NAME.test(name) ? [name] : []
})
.sort()
.slice(0, LIST_MAX)
}
/** An external source (`git-subdir`, `github`, `git`, `url`) as one plain line, or null for anything else. */
function externalOf(source: unknown): string | null {
const record = recordOf(source)
const at = (value: unknown) => (typeof value === 'string' ? value : '')
if (record === null || !['git-subdir', 'github', 'git', 'url'].includes(at(record.source))) return null
const where = [at(record.url) || at(record.repo), at(record.path)].filter(Boolean).join(' ')
const ref = at(record.ref) || at(record.sha).slice(0, 12)
return where === '' ? null : plain(`${where}${ref === '' ? '' : ` @ ${ref}`}`, 200)
}
const exists = async (fs: ReaderFs, path: string): Promise<boolean> => (await fs.stat(path).catch(() => undefined)) !== undefined
async function pluginOf(fs: ReaderFs, location: string, entry: Record<string, unknown>, installed: ReadonlyMap<string, string>): Promise<CatalogPlugin | null> {
const name = entry.name
const source = safeSource(entry.source)
const external = source === null ? externalOf(entry.source) : null
if (typeof name !== 'string' || !NAME.test(name) || (source === null && external === null)) return null
const dir = source !== null ? `${location}/${source}` : (installed.get(name) ?? '')
if (dir === '') {
return { name, options: [], description: plain(typeof entry.description === 'string' ? entry.description : '', 400), version: null, dir, skills: [], agents: [], commands: [], hasMcp: false, isMod: false, ...(external !== null && { external }) }
}
const manifest = await fs.read(`${dir}/.claude-plugin/plugin.json`).then(
text => recordOf(JSON.parse(text)),
() => null,
)
const [skills, agents, commands, hasMcp, isMod] = await Promise.all([namesIn(fs, `${dir}/skills`, 'dir'), namesIn(fs, `${dir}/agents`, 'md'), namesIn(fs, `${dir}/commands`, 'md'), exists(fs, `${dir}/.mcp.json`), exists(fs, `${dir}/hooks/register.ts`)])
const version = typeof manifest?.version === 'string' ? plain(manifest.version, 30) : typeof entry.version === 'string' ? plain(entry.version, 30) : null
const options = Object.keys(recordOf(manifest?.userConfig) ?? {}).filter(key => NAME.test(key)).slice(0, 60)
return { name, options, description: plain(typeof entry.description === 'string' ? entry.description : typeof manifest?.description === 'string' ? manifest.description : '', 400), version, dir, skills, agents, commands, hasMcp, isMod, ...(external !== null && { external }) }
}
/**
* Every plugin the marketplace clone at `location` lists, or null when its manifest cannot be read. `installed` maps a plugin name to
* its install path (absolute, already checked by the caller): an external plugin is read from there.
*/
export async function readCatalog(fs: ReaderFs, location: string, installed: ReadonlyMap<string, string> = new Map()): Promise<CatalogPlugin[] | null> {
if (!location.startsWith('/') || location.split('/').includes('..')) return null
const raw = await fs.read(joinPath(location, '.claude-plugin', 'marketplace.json')).catch(() => null)
let list: unknown[] = []
try {
const plugins = raw === null ? null : recordOf(JSON.parse(raw))?.plugins
if (!Array.isArray(plugins)) return null
list = plugins.slice(0, PLUGINS_MAX)
} catch {
return null
}
const found: CatalogPlugin[] = []
// Eight at a time: forty-odd plugins are a few hundred small reads, none of which should crowd the engine's own.
for (let at = 0; at < list.length; at += 8) {
const batch = await Promise.all(
list.slice(at, at + 8).map(async entry => {
const record = recordOf(entry)
return record === null ? null : pluginOf(fs, location, record, installed).catch(() => null)
}),
)
for (const plugin of batch) if (plugin !== null) found.push(plugin)
}
return found.sort((a, b) => a.name.localeCompare(b.name))
}
/** A skill, agent or command file's first lines (and its frontmatter description), or null when it cannot be read. */
export async function readDoc(fs: ReaderFs, plugin: CatalogPlugin, kind: 'skill' | 'agent' | 'command', name: string): Promise<{ description: string; lines: string[] } | null> {
if (!NAME.test(name) || plugin.dir === '') return null
const path = kind === 'skill' ? `${plugin.dir}/skills/${name}/SKILL.md` : `${plugin.dir}/${kind === 'agent' ? 'agents' : 'commands'}/${name}.md`
const stat = await fs.stat(path).catch(() => undefined)
if (stat === undefined || (stat.size ?? 0) > DOC_BYTES) return null
// A link, or anything but a plain file, is never read: its target may be outside the plugin and its text would be drawn and handed to the model (#3817).
if (stat.isLink === true || (stat.kind !== undefined && stat.kind !== 'file')) return null
const text = await fs.read(path).catch(() => null)
if (text === null) return null
const lines = text.split('\n').map(line => plain(line, 200))
const description = lines.find(line => /^description:/i.test(line))?.replace(/^description:\s*/i, '').replace(/^["']|["']$/g, '') ?? ''
return { description: plain(description, 200), lines: lines.slice(0, DOC_LINES) }
}
export type CatalogMode = 'all' | 'installed' | 'missing' | 'mods' | 'skills' | 'agents'
/** The plugins a mode and a filter word keep: the word matches a name, a description, or any skill/agent/command name. */
export function visible(all: readonly CatalogPlugin[], installed: ReadonlySet<string>, mode: CatalogMode, word: string): CatalogPlugin[] {
const needle = word.trim().toLowerCase()
return all.filter(plugin => {
if (mode === 'installed' && !installed.has(plugin.name)) return false
if (mode === 'missing' && installed.has(plugin.name)) return false
if (mode === 'mods' && !plugin.isMod) return false
if (mode === 'skills' && plugin.skills.length === 0) return false
if (mode === 'agents' && plugin.agents.length === 0) return false
return needle === '' || [plugin.name, plugin.description, ...plugin.skills, ...plugin.agents, ...plugin.commands].some(text => text.toLowerCase().includes(needle))
})
}