diff --git a/.agents/plugins/marketplace.json b/.agents/plugins/marketplace.json new file mode 100644 index 000000000..8a4ff0b90 --- /dev/null +++ b/.agents/plugins/marketplace.json @@ -0,0 +1,20 @@ +{ + "name": "machoswiftsection", + "interface": { + "displayName": "MachOSwiftSection" + }, + "plugins": [ + { + "name": "swift-section", + "source": { + "source": "local", + "path": "./AgentPlugins/swift-section" + }, + "policy": { + "installation": "AVAILABLE", + "authentication": "ON_INSTALL" + }, + "category": "Developer Tools" + } + ] +} diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json new file mode 100644 index 000000000..1b8e26bd2 --- /dev/null +++ b/.claude-plugin/marketplace.json @@ -0,0 +1,17 @@ +{ + "name": "machoswiftsection", + "owner": { + "name": "MxIris-Reverse-Engineering", + "url": "https://github.com/MxIris-Reverse-Engineering" + }, + "metadata": { + "description": "Agent plugins for MachOSwiftSection and its swift-section CLI." + }, + "plugins": [ + { + "name": "swift-section", + "source": "./AgentPlugins/swift-section", + "description": "Teaches coding agents to drive the swift-section CLI: Swift and Objective-C declarations, memory layout and ABI diffs recovered from a Mach-O binary or a dyld shared cache." + } + ] +} diff --git a/.github/workflows/version-check.yml b/.github/workflows/version-check.yml index 50530ba9a..8cd21f100 100644 --- a/.github/workflows/version-check.yml +++ b/.github/workflows/version-check.yml @@ -43,3 +43,23 @@ jobs: exit 1 fi echo "Found $changelog" + + - name: Verify agent plugin versions match + run: | + set -euo pipefail + value="${{ steps.version.outputs.version }}" + # Claude Code and Codex only update an installed plugin when its + # version changes, so a release that leaves these behind never + # reaches the users of the plugin. + status=0 + for manifest in AgentPlugins/swift-section/.claude-plugin/plugin.json \ + AgentPlugins/swift-section/.codex-plugin/plugin.json; do + pluginVersion=$(jq -r '.version // empty' "$manifest") + if [ "$pluginVersion" != "$value" ]; then + echo "::error file=$manifest::version is '$pluginVersion' but BundledVersion.value is '$value' — bump the plugin with the CLI." + status=1 + else + echo "$manifest: $pluginVersion" + fi + done + exit $status diff --git a/AGENTS.md b/AGENTS.md index fd671b7e0..a8fc6a5af 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -268,6 +268,17 @@ Keeping this file's module list and `Documentations/README.md`'s index in sync w + + +`AgentPlugins/swift-section/` is the agent plugin users install into Claude Code and Codex to learn this CLI; its skill (`skills/swift-section-cli/SKILL.md`, `references/objc.md`) is the only copy anywhere, so nobody else will fix it. + +- **A CLI change updates the skill in the same batch** — a new or renamed flag, a changed default, a new trap in the output. Check every flag it names still exists: `grep -ohE -- '--[a-z][a-z0-9-]+'` over the skill against the subcommands' `--help`. +- **A release sets `version` in both `plugin.json` files to `BundledVersion.value`.** Claude Code and Codex only update an installed plugin when that version changes; `version-check.yml` fails a PR to `main` that forgets. +- Validate before pushing: `claude plugin validate AgentPlugins/swift-section` and `claude plugin validate .claude-plugin/marketplace.json --strict`; for Codex, the `validate_plugin.py` of its `plugin-creator` skill. +- The directory is `AgentPlugins/`, never `plugins/`: on a case-insensitive disk that is SwiftPM's `Plugins/`. The two marketplace files are `.claude-plugin/marketplace.json` (Claude Code) and `.agents/plugins/marketplace.json` (Codex). Why and how: [0051-agent-plugin](Documentations/Evolutions/0051-agent-plugin.md). + + + ## Work in progress **GenericSpecializer** (`Sources/SwiftSpecialization/`) — interactive runtime specialization of generic types. Core implementation complete with tests. Two-step API: `makeRequest()` returns parameters and candidates, `specialize()` executes with the user's selections. Only protocol requirements need witness tables, passed in requirement order; `baseClass` / `layout` / `sameType` need validation only. Generic parameter names are derived from depth/index (A, B, A1, …) because the binary does not preserve them. diff --git a/AgentPlugins/swift-section/.claude-plugin/plugin.json b/AgentPlugins/swift-section/.claude-plugin/plugin.json new file mode 100644 index 000000000..96dba2edd --- /dev/null +++ b/AgentPlugins/swift-section/.claude-plugin/plugin.json @@ -0,0 +1,13 @@ +{ + "name": "swift-section", + "version": "0.20.0", + "description": "Teaches coding agents to drive the swift-section CLI: Swift and Objective-C declarations, memory layout and ABI diffs recovered from a Mach-O binary or a dyld shared cache.", + "author": { + "name": "MxIris-Reverse-Engineering", + "url": "https://github.com/MxIris-Reverse-Engineering" + }, + "homepage": "https://github.com/MxIris-Reverse-Engineering/MachOSwiftSection#agent-plugin", + "repository": "https://github.com/MxIris-Reverse-Engineering/MachOSwiftSection", + "license": "MIT", + "keywords": ["swift", "objective-c", "mach-o", "dyld-shared-cache", "reverse-engineering", "abi"] +} diff --git a/AgentPlugins/swift-section/.codex-plugin/plugin.json b/AgentPlugins/swift-section/.codex-plugin/plugin.json new file mode 100644 index 000000000..6d54d0886 --- /dev/null +++ b/AgentPlugins/swift-section/.codex-plugin/plugin.json @@ -0,0 +1,28 @@ +{ + "name": "swift-section", + "version": "0.20.0", + "description": "Teaches coding agents to drive the swift-section CLI: Swift and Objective-C declarations, memory layout and ABI diffs recovered from a Mach-O binary or a dyld shared cache.", + "author": { + "name": "MxIris-Reverse-Engineering", + "url": "https://github.com/MxIris-Reverse-Engineering" + }, + "homepage": "https://github.com/MxIris-Reverse-Engineering/MachOSwiftSection#agent-plugin", + "repository": "https://github.com/MxIris-Reverse-Engineering/MachOSwiftSection", + "license": "MIT", + "keywords": ["swift", "objective-c", "mach-o", "dyld-shared-cache", "reverse-engineering", "abi"], + "skills": "./skills/", + "interface": { + "displayName": "swift-section", + "shortDescription": "Swift and ObjC declarations out of a Mach-O binary", + "longDescription": "How to drive the swift-section CLI: dump and interface for Swift declarations, objc for Objective-C headers with IMP addresses, static field-offset and enum-layout comments, and diff / snapshot / evolution for comparing the API of two builds. Covers the flag spellings and output traps that produce a plausible but wrong answer. Requires the swift-section CLI (brew install swift-section).", + "developerName": "MxIris-Reverse-Engineering", + "category": "Developer Tools", + "capabilities": ["Read"], + "websiteURL": "https://github.com/MxIris-Reverse-Engineering/MachOSwiftSection", + "defaultPrompt": [ + "Dump the Swift types in this binary", + "Diff the Swift ABI of these two builds", + "Show the ObjC interface of this class" + ] + } +} diff --git a/AgentPlugins/swift-section/skills/swift-section-cli/SKILL.md b/AgentPlugins/swift-section/skills/swift-section-cli/SKILL.md new file mode 100644 index 000000000..69e664c8b --- /dev/null +++ b/AgentPlugins/swift-section/skills/swift-section-cli/SKILL.md @@ -0,0 +1,272 @@ +--- +name: swift-section-cli +description: >- + Recovering Swift and Objective-C declarations and ABI facts from a Mach-O binary or a dyld + shared cache with the `swift-section` CLI — dumping types / protocols / conformances, + generating a .swiftinterface offline, Objective-C headers with IMP addresses, diffing the API + of two builds, tracking it across OS versions, and the static field-offset / type-layout / + enum-layout comments. Read before running swift-section, and whenever a task needs type + information out of a binary without loading it into a process. Triggers on swift-section, + swift-section objc, objc-section, MachOSwiftSection, __swift5_types, swiftinterface, + ABI diff, ABI baseline. +--- + +# swift-section + +A CLI that reads Swift metadata straight out of a Mach-O file — the compiler-emitted +`__swift5_*` sections plus the symbol table — and reconstructs source-level declarations from +it. Nothing is loaded into a process and no source or `.swiftmodule` is needed, so it works on +a stripped OS framework, a third-party app binary, or an image inside a dyld shared cache. Its +`objc` subcommand does the same for the Objective-C metadata (§1). + +**Where it sits next to other reverse-engineering tools.** It is the offline, scriptable form +of the library [RuntimeViewer](https://github.com/MxIris-Reverse-Engineering/RuntimeViewer) +uses. Reach for it when the target is a file rather than something loadable, when the build is +not the one running on this machine (an archived cache, another OS version, another platform), +or when the answer must be diffed or committed. swift-section recovers *declarations, layout +and addresses*, never instruction-level behaviour: for what a method actually does, take its +address from here (`--emit-member-addresses`, or the IMP comments of `objc`) and hand that +address to a disassembler or decompiler opened on **the same build**. An address from one build +means nothing in another. + +## 0. Confirm the version before trusting a flag + +The installed binary (`brew install swift-section`, on `PATH`) is often older than the +repository, and flags are added frequently. Confirm before building a command line on one: + +```bash +swift-section --version +swift-section interface --help | grep -- --exported-only # empty ⇒ this build lacks it +``` + +Flags added comparatively late, and therefore worth checking: `--emit-header`, +`--emit-export-status`, `--exported-only`, `--jobs`, `evolution --interface`, +`--supplementary-apinotes`, and from 0.20.0 `--dependency-search-path`, +`--infer-objc-overrides` and the whole `objc` subcommand group. When one is missing, say so +rather than silently producing a weaker answer — the alternative is usually "upgrade +(`brew upgrade swift-section`), or do without that annotation". + +## 1. Pick the subcommand + +| The question | Subcommand | +|---|---| +| What Swift types / protocols / conformances does this binary contain? | `dump` | +| Give me something that reads like Swift source (or pastes into an editor) | `interface` | +| What changed in the ABI between these two builds? | `diff` | +| Freeze today's ABI so a future build can be compared without keeping the binary | `snapshot` | +| How did this API evolve across N OS versions? | `evolution` | +| Reformat the memory-layout comments the two above emit | `transformer` | +| Objective-C headers, IMP addresses, or an Objective-C API diff (0.20.0+, formerly the separate `objc-section` executable) | `objc` — read `references/objc.md` first | + +`dump` is the default subcommand: `swift-section ` is `swift-section dump `. + +**`dump` versus `interface` is not a cosmetic choice.** `dump` walks the sections one by one +and prints what each record literally says — descriptors, witness-table entries, associated +types — in binary order if asked. `interface` builds a whole declaration model first: it +indexes every symbol in the image, merges extensions, joins accessors to their vtable entries, +recovers `final` / `class` keywords, and only then prints. That model is what makes the output +readable, and it is also why `interface` costs orders of magnitude more (see §5). Start with +`dump` to find out whether the type is even there; escalate to `interface` when you need to +read it. + +## 2. Pointing it at the binary + +Three input shapes, and the option that selects each: + +```bash +# 1. A file on disk. A fat (universal) binary REQUIRES an architecture — the tool +# refuses to pick a slice and lists what it found. +swift-section dump /path/to/Foo.framework/Foo --architecture arm64e + +# 2. An image inside the running system's shared cache (frameworks are not on disk +# as separate files on modern macOS/iOS). +swift-section dump --uses-system-dyld-shared-cache --cache-image-name SwiftUICore + +# 3. An image inside a specific cache file (an archived one, a simulator runtime's). +swift-section dump --dyld-shared-cache --cache-image-path /System/Library/.../SwiftUI /path/to/dyld_shared_cache +``` + +- `-n` / `--cache-image-name` takes a **bare image name** (`SwiftUICore`); `-p` / + `--cache-image-path` takes the **install path**. Pass exactly one — both together is an + error. Prefer `-p` when a leaf name is ambiguous inside the cache (a macOS cache carries + Mac Catalyst builds under `/System/iOSSupport` with the same leaf names). +- `diff` and `evolution` read `--dyld-shared-cache` differently: it means "every input path is + a cache, extract the same `-n` image from each". They have no + `--uses-system-dyld-shared-cache`, and `snapshot` rejects it on purpose (a baseline needs a + stable recorded path). +- Neither `-n` nor `-p` does anything without one of the cache flags. +- **A standalone file's dependencies are looked for, not given.** Some facts live in another + image: a type behind a metadata accessor (an availability-conditional `some View`), a + cross-module type's layout, an Objective-C superclass (`override`, explicit selectors). By + default the tool infers paths from where the binary sits and falls back to the running + system's cache — which never offers an image of another platform. So an iOS binary read on a + macOS host needs `--dependency-search-path` (`dump`, `interface`, `snapshot`; repeatable) + naming its runtime: a Mach-O file, a cache file (`dyld_shared_cache_*` / + `dyld_sim_shared_cache_*`), or a directory used as a system root such as a simulator + runtime's `RuntimeRoot`. Named paths are consulted before the running system's cache. When + those facts are missing from an iOS binary's output, suspect this flag before the binary. + +## 3. Never redirect `interface`'s stdout — pass `-o` + +`interface` writes its progress lines to **stdout**, mixed into the product: + +``` +$ swift-section interface -a arm64e /bin/ls > out.txt # WRONG +$ head -3 out.txt +Preparing to build Swift interface... +[INFO] Extracted 0 symbol index +[INFO] Types: 0 successful, 0 failed, ... +``` + +Use `--output-path` / `-o` for anything that will be read back, diffed, or committed. The same +holds for `dump`, whose per-declaration error lines also land on stdout — a `dump` redirect +silently mixes `Error: …` lines into the product. + +`diff` / `snapshot` / `evolution` are the well-behaved ones: progress and degradation +diagnostics go to stderr, the report to stdout, so `>` is safe there. Their stderr chatter +(dropped declarations, unresolved dependencies) is **normal reporting, not failure** — judge +those runs by the exit code. + +## 4. `dump` and `interface` do not share flag spellings + +The single most common mistake. Same underlying comment, different name: + +| Comment | `dump` | `interface` | +|---|---|---| +| Field offsets | `--emit-field-offsets` | `--emit-offset-comments` (field **and** PWT offsets) | +| PWT addresses | `--emit-pwt-addresses` | folded into the above | + +Spelled the same on both: `--emit-type-layout`, `--emit-enum-layout`, `--emit-member-addresses`, +`--emit-vtable-offsets`, `--emit-expanded-field-offsets` (implies the field-offset flag), +`--emit-header`, `--emit-export-status`, `--color-scheme`, `--output-path`, +`--dependency-search-path` (§2). + +Only `dump` has: `--sections`, `--preferred-binary-order`, and the demangler option group +(`--demangle-options default|simplified|interface` plus ~22 `--enable-…` / `--disable-…` +switches). + +Only `interface` has: `--exported-only`, `--sort-members-by-offset`, +`--parse-opaque-return-type`, `--show-c-imported-types`, `--resolve-c-module-names`, +`--supplementary-apinotes`, `--infer-objc-overrides`. + +`--infer-objc-overrides` is off by default for a reason. An override whose body the optimizer +inlined into its thunk (`viewDidHide`, `encodeWithCoder:` in an OS framework) ties to no Swift +symbol; the flag attributes it anyway, to the one member whose name is the importer's spelling +of the selector. `dump` always shows such ties, marked `(selector name, no symbol evidence)`; +an interface has nowhere to say a keyword rests on a name, so it adds `override` only when +asked. + +### `--sections` has two traps + +```bash +swift-section dump --sections types,protocols /path/to/binary # ERROR: comma is not a separator +swift-section dump --sections types protocols /path/to/binary # ERROR: the path is eaten as a section +swift-section dump /path/to/binary --sections types protocols # correct — path first +swift-section dump --sections types protocols -- /path/to/binary # correct — `--` ends the list +``` + +It consumes values up to the next option, so the positional file path must not follow it. +Both wrong forms above are the ones a comma-separated habit produces, and neither fails in an +obvious way if you are skimming — read the error text. The habit is easy to pick up in this +very executable: `swift-section objc dump --sections` **is** comma-separated +(`--sections classes,protocols`). + +Also: **with no `--sections`, a section that is missing or unreadable is skipped silently** — +a binary with no Swift content produces zero output and exit status 0. Pass the section +explicitly to get the diagnosis (`Swift section __swift5_assocty not found …`). So "empty +output" never by itself means "no Swift in there"; confirm with an explicit `--sections`. + +## 5. `interface` is expensive; `dump` is not + +`interface` builds the whole image's demangled symbol index before printing anything. On a +large system framework (SwiftUI, SwiftUICore) that is tens of seconds to minutes and a lot of +resident memory. Budget for it: + +- Explore with `dump` (optionally `--sections types`) first; run `interface` once you know the + target is there. +- Never run `interface` over a whole dyld shared cache image set casually. +- `diff` and `evolution` index **every** input, so cost multiplies by the number of versions. + `--jobs N` bounds how many index at once (default: processor count); each in-flight input + holds its indexed image in memory, so lower it when memory is the constraint, not `1` for + speed's sake. +- Anything long-running belongs in a background run written to a file with `-o`, not in a + foreground command that will be waited on. + +## 6. Comment templates + +The five layout comment kinds (field offset, vtable offset, member address, type layout, enum +layout) render from `${token}` templates, and `dump` / `interface` accept the template options +directly. + +```bash +swift-section transformer templates --module field-offset # names accepted by the options +swift-section transformer tokens --module enum-layout # placeholders those templates take +``` + +- A value containing `${` is a literal template; anything else must **name** a built-in + template (matched case / space / hyphen / underscore insensitively). A name that matches + nothing is a hard error, never a silently constant comment. +- Passing any template option turns on the comment kind it formats — no separate `--emit-…` + needed. The reverse does not hold. +- `--enum-layout-style` is a whole-module preset: `detailed` (default) / `explained` (bit + ranges in words) / `standard` / `inline` / `compact`. +- `swift-section transformer config … -o comments.json` freezes a command line for reuse via + `--transformer-config`; it is the same JSON shape RuntimeViewer persists. + +## 7. `diff` / `snapshot` / `evolution` + +```bash +swift-section diff old/Foo new/Foo # change list + verdict +swift-section diff old/Foo new/Foo --interface --format unified # annotated interface +swift-section snapshot /path/Foo --label 26.0 -o foo-26.0.json # freeze a baseline +swift-section evolution 17.0.json 18.0.json /path/Foo --labels 17.0,18.0,26.0 +``` + +- `--json`, `--summary-only` and `--interface` are mutually exclusive, and `--format` only + applies with `--interface`. +- `--interface` renders from live models, so **every input must be a binary or cache** — + snapshot JSON is rejected there, though it is fine for the change-list / lineage reports. +- `evolution` needs ≥ 2 inputs **in version order, oldest first**; `--labels` is comma-separated + and must have exactly one label per input. +- `--fail-on-breaking` makes a breaking change a nonzero exit — that, not the report text, is + the CI signal. +- Compare binaries in **similar strip states**. A symbol-rich build against a stripped one + reports the same protocol requirement as a member swap, because a stripped protocol diffs by + witness-table slot rather than by symbol. + +## 8. `--resolve-c-module-names` (interface, macOS only) + +Turns `__C.NSString` into `Foundation.NSString`. Needs macOS 13+ and an Xcode installation, and +the first run per SDK generates module interfaces through sourcekitd — slow once, cached after. +Attribution against a non-macOS binary is limited (it warns and degrades rather than failing). +`--supplementary-apinotes` only does anything alongside it. + +## 9. Reading the output: things that look like bugs and are not + +- **`// not exported`** is an export-trie fact, not an access-level claim. A missing entry + means "not statically callable from outside this image", which is not the same as `internal`. +- **A type member printed `static`** may have been `class` in the source: the four + descriptor-less `class` spellings are ABI-identical to `static`, so the printer picks the + spelling that is always legal. +- **`Field offset: unknown ()`** is the layout engine declining to guess, and the + reason names what it could not resolve. It is distinct from the flag being off (which emits + nothing at all). +- **A generic type printed with unresolved fields** is expected when no arguments were + supplied: an unconstrained `T` has no layout until it is bound. Class-bound parameters and + parameter metatypes do resolve unspecialized. +- **`pwtslot:` records in a protocol diff** are the fallback view used when requirement symbols + are stripped — see the strip-state warning in §7. +- **Enum-layout patterns marked "not resolved offline"** mean only the extra-inhabitant *index* + is derivable statically; the concrete bytes need the live metadata. + +## 10. Where the details live + +The repository is [MachOSwiftSection](https://github.com/MxIris-Reverse-Engineering/MachOSwiftSection). + +| Need | Read | +|---|---| +| Worked examples of every subcommand and flag | its `README.md`, section "swift-section CLI Tool" | +| How to read an enum-layout comment | `Documentations/SwiftEnumLayout.md` (`_zh` companion) | +| Writing supplementary `.apinotes` for a private framework | `Documentations/SupplementaryTypeMappings.md` | +| The `objc` subcommands in full, including the snapshot / diff / evolution workflow | `Documentations/ObjCCommandLine.md` (`_zh` companion); the traps are in `references/objc.md` beside this file | +| What each flag does, authoritatively | `swift-section --help` (and `swift-section objc --help`) — generated from the code, so it cannot drift | diff --git a/AgentPlugins/swift-section/skills/swift-section-cli/references/objc.md b/AgentPlugins/swift-section/skills/swift-section-cli/references/objc.md new file mode 100644 index 000000000..306205f78 --- /dev/null +++ b/AgentPlugins/swift-section/skills/swift-section-cli/references/objc.md @@ -0,0 +1,86 @@ +# `swift-section objc` + +Reads Objective-C metadata straight out of a Mach-O file or an image inside a dyld shared +cache — any architecture, platform or OS version, nothing loaded into a process — and renders +headers from it. The Objective-C libraries underneath (MachOObjCSection) are the ones +RuntimeViewer renders Objective-C with, so its headers and address comments read like a +RuntimeViewer export. + +**It needs `swift-section` 0.20.0 or later.** These commands used to ship as a separate +`objc-section` executable; 0.20.0 moved them into `swift-section` with the same options, +output and exit codes, and the standalone tool is retired. Anything written for +`objc-section ` works as `swift-section objc `. An older install has no +`objc` subcommand — `swift-section objc --help` fails — so upgrade it with +`brew upgrade swift-section`. + +## The usual commands + +```bash +# One class with its method and accessor addresses, from a cache file on disk +swift-section objc interface NSTextInputContext \ + /path/to/dyld_shared_cache_arm64e --dyld-shared-cache -n AppKit \ + --emit-method-imp-addresses --emit-property-accessor-addresses --emit-ivar-offsets + +# The same from the running system's cache — no path +swift-section objc interface NSTextInputContext --uses-system-dyld-shared-cache -n AppKit \ + --emit-method-imp-addresses --emit-property-accessor-addresses + +# A standalone binary; a universal one needs -a, and without it the error lists the slices +swift-section objc interface SomeClass /path/to/Binary -a arm64e --emit-method-imp-addresses + +# A whole image into one header. -o does not create directories. +swift-section objc dump /path/to/dyld_shared_cache_arm64e --dyld-shared-cache -n AppKit \ + --emit-method-imp-addresses --emit-property-accessor-addresses \ + --emit-ivar-offsets --emit-property-attributes \ + -o AppKit.h +``` + +- The loading options are spelled exactly as in the Swift subcommands: `--dyld-shared-cache` + (the path is a cache), `--uses-system-dyld-shared-cache` (no path), `-n` / + `--cache-image-name`, `-p` / `--cache-image-path` (exact install path, when a leaf name is + ambiguous), `-a` / `--architecture`. Pass a cache's main file, never a `.01` / + `.dylddata` shard — the shards are found automatically. +- **Every address and offset comment is off by default** — a bare `interface` prints no + addresses. `--emit-method-imp-addresses` appends `// IMP: 0x…` to each method, + `--emit-property-accessor-addresses` appends `// getter IMP: … // setter IMP: …` to each + property. +- IMP addresses are unslid virtual addresses — what a disassembler shows for the same file or + cache. They are only meaningful in that exact build. +- `interface` looks a name up in classes, protocols, categories, structs and unions, in that + order, and takes the first hit; `--kind` picks one when a name is both a class and a struct. + A category is named `ClassName(CategoryName)`. +- `-f` / `--filter ` narrows `dump` to names containing the text, case-insensitively. +- stdout carries only declarations; indexing progress goes to stderr, and only with `-v`. + Unlike the Swift `interface`, redirecting stdout is safe here. +- `swift-section --version` prints the version to record beside a generated header. + +## Traps + +- **`--sections` is comma-separated here** (`--sections classes,protocols`) — the opposite of + the Swift `dump` in the same executable, where a comma is an error and the values are + space-separated. +- **An empty `dump` still exits 0.** The reason goes to stderr only: + `no Objective-C metadata found in `, `no found in `, or + `--filter '' matched none of the declarations in `. Read stderr before + concluding that a class is not there. `interface` is the opposite: a name it cannot find is + exit 1 with `Error: No class, protocol, category, struct or union named '' …`. +- **A standalone file's superclass chain stops at its own boundary.** Outside a shared cache, a + superclass defined in another binary is not followed, so `--strip-overrides` removes almost + nothing and inherited members such as `init` stay listed. Inside one cache the chain crosses + images normally. When you need the full chain, read the image inside its cache. +- **The ivar offsets of pure Swift classes (`_TtC…` names) are unreliable** — file and + in-process readings disagree, and some ivars are dropped. Take Swift layout from the Swift + subcommands (`--emit-field-offsets`). +- **An API diff cannot tell public from private.** Objective-C has no access control, so a + renamed private selector is reported as API-breaking just like a public one. A pure change + of an ivar's offset is deliberately not reported at all. +- **Snapshots do not cross format versions.** A snapshot JSON carries a `formatVersion`; any + other version is rejected with `Unsupported ObjC API snapshot format version …`, and the fix + is to regenerate the baseline with the current tool. + +## Where the details live + +`Documentations/ObjCCommandLine.md` in the MachOSwiftSection repository covers all five +subcommands — including the `snapshot` / `diff` / `evolution` trio for comparing the +Objective-C API of two builds — the ten generation switches and the comment templates. +`swift-section objc --help` is generated from the code and cannot drift. diff --git a/Documentations/Evolutions/0051-agent-plugin.md b/Documentations/Evolutions/0051-agent-plugin.md new file mode 100644 index 000000000..6d5d0cc8b --- /dev/null +++ b/Documentations/Evolutions/0051-agent-plugin.md @@ -0,0 +1,75 @@ +# 0051 - `swift-section` 的 agent 插件:教 agent 用 CLI 的 skill 随仓库发布 + +- **状态**: Implemented +- **作者**: JH +- **创建日期**: 2026-09-27 +- **最后更新**: 2026-09-27 +- **所属愿景**: 无 +- **关联提案**: [0036-objc-subcommands](0036-objc-subcommands.md)(`objc-section` 并入 `swift-section objc`;插件里的 `references/objc.md` 讲的就是它) +- **实现分支 / PR**: `feature/agent-plugin`,直接合入 `main`(只加插件与文档,不动库代码;合入后再把 `main` 合回 `next`) +- **配套文档**: README「Agent Plugin」一节(安装方法);`AGENTS.md` 里「you are changing the swift-section CLI」一段(维护规则) + +## 摘要 + +教 coding agent 用 `swift-section` 的那份 skill 此前只存在于维护者本机的全局配置里,别人装不到, +CLI 一改参数它也没人同步。本提案把它搬进本仓库,做成一个 Claude Code 与 Codex 都能直接从 +GitHub 安装的插件 `swift-section`,并把「CLI 改了就改 skill、发版就改插件版本号」写进 +`AGENTS.md`,版本号一致性交给 CI 把关。 + +## 方案 + +**布局**: + +``` +.claude-plugin/marketplace.json Claude Code 在仓库根找 marketplace 的固定位置 +.agents/plugins/marketplace.json Codex 在仓库根找 marketplace 的固定位置 +AgentPlugins/swift-section/ +├── .claude-plugin/plugin.json +├── .codex-plugin/plugin.json +└── skills/swift-section-cli/ + ├── SKILL.md + └── references/objc.md +``` + +- 两个工具各有自己的 marketplace 与 manifest 格式,互不识别,但都能指向同一个插件目录, + `skills/` 只有一份。Codex 的 marketplace 里 `./AgentPlugins/…` 以 `.agents/` 所在目录 + (即仓库根)为基准解析,与 Claude Code 一致。 +- **目录叫 `AgentPlugins/` 而不是两个工具示例里的 `plugins/`**:仓库根已有 SwiftPM 的 + `Plugins/`(`RegenerateBaselinesPlugin`),macOS 默认文件系统不区分大小写,两者会是同一个 + 目录。 +- 仓库打包约 30 MB,两个工具默认整仓克隆也能接受,不强制 sparse checkout。 + +**skill 内容**:以维护者本机那份为底,做了三类改动—— + +1. 删掉只在本机成立的内容(指向本机其他 skill 的交叉引用)。「何时用它、何时交给反汇编器」 + 改写成通用说法:地址从这里拿,交给**同一构建**上打开的反汇编器。 +2. `objc` 子命令的用法与坑原本写在本机另一个逆向 skill 里,整体搬进 `references/objc.md`, + 本机路径换成通用写法;在原有四条坑之外补上 `ObjCCommandLine.md` 里 diff 相关的两条 + (不分公私、快照格式版本)。 +3. 补上 0.20.0 新增而原 skill 没写的 `--dependency-search-path` 与 `--infer-objc-overrides`。 + +skill 里出现的每个 `--flag` 都与已安装的 0.20.0 各子命令 `--help` 核对过,无一缺失。 + +**版本与更新**:`plugin.json` 的 `version` 跟 CLI 的 `BundledVersion.value` 走,起始 0.20.0。 +两个工具都只在这个版本号变化时才给已安装的用户更新,所以发版忘改就等于用户永远停在旧 skill。 +为此 `version-check.yml`(在合入 `main` 的 PR 上跑)新增一步:两个 `plugin.json` 的版本号 +必须等于 `BundledVersion.value`,否则报错;`Version.swift` 顶部的发版提示同步加了这一条。 + +**校验**:`claude plugin validate`(插件与 `--strict` 的 marketplace)、Codex `plugin-creator` +的 `validate_plugin.py` 与 skill 的 `quick_validate.py` 均通过;另在一个临时 `CODEX_HOME` 里 +实际执行了「添加 marketplace → 列出 → 安装」,skill 落进了插件缓存。Claude Code 侧未做实装测试 +(`claude plugin` 命令行会写入用户的真实配置),只依赖官方校验器。 + +**未经询问取的假设**:插件名 `swift-section`、marketplace 名 `machoswiftsection`、skill 名沿用 +`swift-section-cli`(在 Claude Code 里显示为 `swift-section:swift-section-cli`);不附带仓库里的 +`swift-section-mcp` 服务器,那是另一件事。 + +## 决策日志 + +| 日期 | 决定 | 理由 | +|------|------|------| +| 2026-09-27 | 把本机维护的 `swift-section-cli` skill 搬进本仓库,做成插件分发 | 维护者提出:交给仓库维护、让用户自己装,本机不再维护一份 | +| 2026-09-27 | 直接 PR 进 `main`,不等下个版本 | 维护者选择。只加插件与文档、不动库代码;skill 描述的正是 `main` 上已发布的 0.20.0;两个工具都跟踪默认分支,合入即可装 | +| 2026-09-27 | 同一插件目录同时放两个工具的 manifest | 两边格式互不识别但都支持相对路径指向同一目录,skill 只维护一份 | +| 2026-09-27 | 插件版本号由 CI 核对,而不只写进 `AGENTS.md` | 忘改的后果是用户静默停在旧版本、无人察觉;`version-check.yml` 本来就在发版 PR 上核对版本号,加一步成本最低 | +| 2026-09-27 | Implemented | 编号在落地 commit 里取:fetch 后 `main` / `next` 上最大为 0050 | diff --git a/Documentations/Evolutions/README.md b/Documentations/Evolutions/README.md index 994669d4a..cde492b38 100644 --- a/Documentations/Evolutions/README.md +++ b/Documentations/Evolutions/README.md @@ -59,4 +59,5 @@ | [0048](0048-objc-member-selector-recovery.md) | 从 ObjC 方法表还原每个 `@objc` 成员:strip 后的 `@objc`、显式 selector 与 category 成员 | Implemented | | [0049](0049-objc-ancestor-dependency-closure.md) | ObjC 祖先链走依赖闭包:独立文件上的父类与 category 目标类按名字在依赖镜像里解析 | Implemented | | [0050](0050-symbolic-mangling-symbol-index.md) | `_symbolic` 符号索引:被符号引用的对象 → 编译器写下的完整名字 | Implemented | +| [0051](0051-agent-plugin.md) | `swift-section` 的 agent 插件:教 agent 用 CLI 的 skill 随仓库发布,Claude Code 与 Codex 直接安装 | Implemented | | draft | [静态布局引擎读取 accessor thunk 背后的字段类型](draft-static-layout-through-accessor-thunks.md) | Draft | \ No newline at end of file diff --git a/Documentations/Internal/ProjectEvolutionLog.md b/Documentations/Internal/ProjectEvolutionLog.md index 7ca323c19..0d311a26a 100644 --- a/Documentations/Internal/ProjectEvolutionLog.md +++ b/Documentations/Internal/ProjectEvolutionLog.md @@ -1982,6 +1982,16 @@ - **关联文档**:[Changelogs/0.20.0.md](../../Changelogs/0.20.0.md);[ReviewAdjudications A48–A51](ReviewAdjudications.md);[发版审查记录](../../Roadmaps/2026-09-26-release-0.20.0-review-findings.md);[FixtureTestingAndContinuousIntegration.md](FixtureTestingAndContinuousIntegration.md);[AccessorThunkResolutionExplained.md](AccessorThunkResolutionExplained.md)(改名后的测试)。 - **对应版本**:0.20.0。 +## 61. `swift-section` 的 agent 插件 + +- **时间段**:2026-09-27(单日)。 +- **动机**:教 coding agent 用 `swift-section` 的 skill 只存在于维护者本机的全局配置里——别人装不到,CLI 改了参数也不会有人同步它。 +- **关键决策**:做成 Claude Code 与 Codex 都能从 GitHub 直接安装的插件,两边的 marketplace 各放在各自的固定位置、指向同一个插件目录,skill 只有一份。目录叫 `AgentPlugins/` 而不是 `plugins/`,因为后者在不区分大小写的磁盘上就是 SwiftPM 的 `Plugins/`。插件版本号跟 CLI 版本走,两个工具只在版本号变化时才更新已安装的插件,所以由 `version-check.yml` 在合入 `main` 的 PR 上核对,而不只靠 `AGENTS.md` 里的提醒。skill 搬家时去掉了本机专属的交叉引用,把 `objc` 子命令的用法与坑一并收进 `references/objc.md`,补上 0.20.0 的 `--dependency-search-path` 与 `--infer-objc-overrides`。直接 PR 进 `main`,不等下个版本:只加插件与文档,不动库代码。 +- **落地模块**:`AgentPlugins/swift-section/`、`.claude-plugin/marketplace.json`、`.agents/plugins/marketplace.json`、`.github/workflows/version-check.yml`、`Sources/swift-section/Version.swift`(仅注释)、`AGENTS.md`、`README.md`。 +- **验证**:Claude Code 的 `claude plugin validate`(marketplace 用 `--strict`)、Codex 的 `validate_plugin.py` 与 `quick_validate.py` 均通过;临时 `CODEX_HOME` 里实际装上了插件;skill 中出现的每个 `--flag` 都在 0.20.0 的 `--help` 里;CI 新增的版本核对在版本一致时通过、不一致时对两个文件各报一条错误。 +- **关联文档**:[0051-agent-plugin](../Evolutions/0051-agent-plugin.md);README「Agent Plugin」。 +- **对应版本**:0.20.0(插件版本 0.20.0)。 + ## 维护约定 1. **每个非平凡批次结束时必须在本文追加/更新一节**(新工作弧新增一节;延续既有弧则在该节 diff --git a/README.md b/README.md index c458aa72e..49b11f622 100644 --- a/README.md +++ b/README.md @@ -650,6 +650,32 @@ The contracts that neither the signatures nor `--help` show — how file mode tr superclass chain, why pure-Swift classes' ivar records do not line up, and the rest — are in [Objective-C Command Line](Documentations/ObjCCommandLine.md). +## Agent Plugin + +The `swift-section` plugin teaches coding agents to drive the CLI: which subcommand answers +which question, the flag spellings that differ between `dump` and `interface`, and the output +traps that produce a plausible but wrong answer. It is a skill only — install the CLI itself +separately (see [Installation](#installation)). + +**Claude Code** — inside a session: + +``` +/plugin marketplace add MxIris-Reverse-Engineering/MachOSwiftSection +/plugin install swift-section@machoswiftsection +``` + +**Codex**: + +```bash +codex plugin marketplace add MxIris-Reverse-Engineering/MachOSwiftSection +codex plugin add swift-section@machoswiftsection +``` + +The plugin's version follows the CLI's, and an installed plugin only picks up a new release +when that version changes. To pull one in: `claude plugin marketplace update machoswiftsection` +then `claude plugin update swift-section@machoswiftsection` for Claude Code, +`codex plugin marketplace upgrade machoswiftsection` for Codex. + ## Documentation - [Swift Enum Memory Layout](Documentations/SwiftEnumLayout.md) ([中文](Documentations/SwiftEnumLayout_zh.md)) — how Swift lays out enums, and how to read the `--emit-enum-layout` comments. diff --git a/Sources/swift-section/Version.swift b/Sources/swift-section/Version.swift index 61eaa2bb6..29a3bf255 100644 --- a/Sources/swift-section/Version.swift +++ b/Sources/swift-section/Version.swift @@ -1,5 +1,7 @@ // Single source of truth for the CLI version. -// When bumping: also add Changelogs/.md, then tag the release with the same string. +// When bumping: also add Changelogs/.md, set the same version in both +// AgentPlugins/swift-section/{.claude-plugin,.codex-plugin}/plugin.json, then tag the +// release with the same string. // Verified by .github/workflows/version-check.yml (PR) and .github/workflows/release.yml (tag). enum BundledVersion { static let value = "0.20.0"