Skip to content

fix(config): keep the comments of config.yaml when a command writes it - #526

Merged
runkids merged 5 commits into
mainfrom
fix/config-keep-comments
Oct 11, 2026
Merged

runkids merged 5 commits into
mainfrom
fix/config-keep-comments

Conversation

@runkids

@runkids runkids commented Oct 11, 2026

Copy link
Copy Markdown
Owner

Why

Every command that writes config.yaml (global or project) re-encoded the in-memory struct with yaml.v3, so comments were lost. A user keeps a generated block between # BEGIN … / # END … markers and a target --mode copy --target-naming prefixed silently removed them (#525).

What

  • marshalConfig takes the bytes of the file being replaced and copies head, line and foot comments onto every key that is still present. Comments the new document already carries (raw hooks / plugins nodes) win, so nothing is duplicated. The schema directive is stripped from the copied comments because the header is written separately.
  • All five writers pass the existing file: Config.Save, ProjectConfig.SaveIn, the two legacy-target migrations on load, and the registry split.
  • Test: load a config with marker comments and a line comment, change mode, save, assert the comments and the single schema header survive.

Fixes #525

Verification

make check in the devcontainer: docs-check, fmt-check, lint, unit + integration tests all pass.

Every writer re-encoded the in-memory struct, so a command such as
`target --mode copy` dropped every comment, including marker comments
users build scripts around. marshalConfig now takes the file being
replaced and copies its comments onto the keys that are still present.

Fixes #525
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-11T12:54:38.089185Z 5488e0a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: de28e853c3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/config/format.go Outdated
Comment on lines +87 to +90
m.HeadComment = withoutSchemaDirective(m.HeadComment)
if len(m.Content) > 0 {
m.Content[0].HeadComment = withoutSchemaDirective(m.Content[0].HeadComment)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Strip the schema comment from the originally matched key

When an existing file starts in a noncanonical section order, yaml.v3 associates the schema directive with that file's first key, but sortSections has already reordered the new tree before comments are copied. Stripping only m.Content[0] therefore misses the copied directive on the old first key; for example, a file ordered as mode then sources is saved with the generated schema header at the top and another schema directive before mode, contradicting the intended single-header behavior.

Useful? React with 👍 / 👎.

Comment thread internal/config/format.go Outdated
Comment on lines +114 to +117
case old.Kind == yaml.SequenceNode && cur.Kind == yaml.SequenceNode && len(old.Content) == len(cur.Content):
for i := range old.Content {
copyComments(old.Content[i], cur.Content[i])
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve comments when sequence lengths change

When a command adds or removes an item from a sequence, this length check prevents all recursive comment copying for that sequence, including comments on entries that survived unchanged. This occurs in common paths such as project target add/remove and global or project extra removal, so saving after one of those operations still discards comments from every remaining item and its nested keys; surviving entries need to be matched independently rather than requiring equal sequence lengths.

Useful? React with 👍 / 👎.

…d lists

Codex review: the schema directive stuck to the old first key when the
file was in a noncanonical order, and a list edit dropped the comments
of every remaining item.
@runkids

runkids commented Oct 11, 2026

Copy link
Copy Markdown
Owner Author

Both fixed in the follow-up commit: the schema directive is stripped from the old document before any copying, and sequence items are matched by value (or by first key and value for - name: x items) instead of requiring equal lengths. Test TestMarshalConfig_CommentsSurviveReorderAndListEdit covers both. @codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e479db5103

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/config/format.go Outdated
Comment on lines +133 to +134
return len(a.Content) >= 2 && len(b.Content) >= 2 &&
a.Content[0].Value == b.Content[0].Value && a.Content[1].Value == b.Content[1].Value

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Match sequence mappings by their identity key

For mapping items whose first value is a collection, this comparison is not an identity check because collection nodes have an empty Value. Project targets with files, agents, or instructions are marshaled from a map that can place one of those keys before name, so multiple targets can all match the first current item; comments on later targets are then lost or copied to the wrong target during any subsequent save. Locate the name pair regardless of key order and make sequence matching one-to-one.

Useful? React with 👍 / 👎.

…registry.yaml comments too

Review follow-up: a swallowed read error would have written the config
without its comments. The three savers share one helper; registry.yaml
is the third YAML file we rewrite from a struct.
@runkids

runkids commented Oct 11, 2026

Copy link
Copy Markdown
Owner Author

Follow-up from an internal review: a read error on the existing file now fails the save instead of silently dropping its comments; the three savers (global, project, registry.yaml) share marshalConfigFile; tests split per behaviour and one added for a removed key. @codex review

Codex review: a target whose first value is a list has an empty Value,
so every such item matched the first one and took its comments.
@runkids

runkids commented Oct 11, 2026

Copy link
Copy Markdown
Owner Author

Fixed: sequence items are now identified by their name value (or first scalar pair) and matched one to one; TestMarshalConfig_ListOfMappingsMatchesByName covers targets whose first key is files. @codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5488e0a5f7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/config/format.go
func itemIdentity(n *yaml.Node) string {
switch n.Kind {
case yaml.ScalarNode:
return n.Value

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use one identity for scalar and object-form targets

When a project target changes representation, the same target cannot match because a scalar target produces claude while an object target produces name=claude. This occurs when a settings command converts - claude # comment into an object, and also when a name-only object is canonicalized back to a scalar by ProjectTargetEntry.MarshalYAML; copyComments then treats the surviving target as a new item and drops its comments. Normalize both forms to the same name-based identity.

Useful? React with 👍 / 👎.

…and object form

Codex review: `- claude` and `- name: claude` are the same target, so
they share an identity and the line comment moves onto the name.
@runkids

runkids commented Oct 11, 2026

Copy link
Copy Markdown
Owner Author

Fixed: a scalar target and an object target with the same name now share one identity, and the line comment moves onto name: since a block mapping cannot carry one. Covered by TestMarshalConfig_ScalarAndObjectTargetShareComments. Three review rounds done; merging once CI is green.

@runkids
runkids merged commit 551a154 into main Oct 11, 2026
8 checks passed
@runkids
runkids deleted the fix/config-keep-comments branch October 11, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

config.yaml loses its comments when a command writes it

1 participant