@magpie-community/opencode-factory-auth 0.1.22 → 0.1.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +33 -9
  2. package/index.mjs +93 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -129,27 +129,47 @@ continuation instructions; context without those phrases stays verbatim.
129
129
  Complete generated reminders for restored Read calls and omitted files are
130
130
  adapted too. Read arguments and file paths are preserved; quoted or incomplete
131
131
  reminders are left alone.
132
+ Claude Code (2.1.291 and later) also restores skills invoked before compaction
133
+ as one generated reminder. Factory refuses that fixed opening, and the restored
134
+ skill text can quote the same client phrases as tool output. A complete
135
+ reminder has its opening sentence rephrased. When its skill text quotes a
136
+ refused phrase, that text is encoded as a JSON string with explicit skill-text
137
+ decoding instructions; decoding restores the skill text exactly. Skill text
138
+ without those phrases stays verbatim inside the rephrased reminder. Pasted
139
+ fragments and incomplete reminders are left untouched.
132
140
  Claude Code can also combine reminders into a token-prefixed `system` turn
133
141
  without the reminder wrappers. The adapter handles the known metadata
134
- paragraphs in that form, including skill-list updates, while retaining
135
- numbered file contents, token markers and SessionStart hook output.
142
+ paragraphs in that form, including skill-list updates and complete
143
+ invoked-skills reminders. A complete reminder is the fixed opening sentence
144
+ followed by the complete `IMPORTANT:` paragraph; skill text then runs until
145
+ the next known attachment (a token marker, hook, restored Read, omitted-file
146
+ note, Read result, environment, model line, skill-list header, changed-file
147
+ note, agent-type list or MCP server instructions) or the end of the turn.
148
+ Numbered file contents, token markers and SessionStart hook output stay.
136
149
  The same adaptation applies when a translating gateway folds that complete
137
150
  token-prefixed block into a user message.
138
151
  Standalone skill updates can instead start with the generated skill-list
139
152
  header and end with a token marker, for example when leaving auto mode.
140
153
  That complete form receives the same adaptation while retaining the skill
141
154
  descriptions and mode-change instructions.
155
+ The skill list can also come alone as a `system` message, with only its
156
+ entries: no header, no reminder wrapper and no token marker, as text blocks
157
+ or a string. A `system` message that opens with an entry gets the same
158
+ change to the known `update-config` line; everything else in it stays.
142
159
  When runtime notifications precede the skill update, the adapter recognizes
143
160
  the complete skill-header and bullet-list paragraphs inside a token-terminated
144
161
  bundle. It preserves MCP connection errors, other notifications, mode updates
145
162
  and custom skill descriptions while adapting the known built-in description.
146
163
  After `/compact`, Claude Code also sends the files it restores and the
147
- runtime context as one turn that opens with an unwrapped restored-file note
148
- or Read call, with no token marker first. When that turn also carries the
149
- generated environment paragraph or a token marker before any hook output,
150
- each fixed paragraph (file notes, Read calls and results, environment, model
151
- line) is adapted as in a token-prefixed bundle; everything else stays
152
- byte-for-byte. Quoted, incomplete and hook-owned forms are left alone.
164
+ runtime context as one turn that opens with an unwrapped restored-file note,
165
+ Read call or complete invoked-skills reminder, with no token marker first.
166
+ A complete skills reminder is recognized even when no files were restored
167
+ and no environment or token paragraph follows it. A restored-file opening
168
+ requires the generated environment paragraph, a token marker or a complete
169
+ skills reminder before any hook output. Each fixed paragraph (file notes,
170
+ Read calls and results, skills, environment, model line) is adapted as in a
171
+ token-prefixed bundle; everything else stays byte-for-byte. Quoted,
172
+ incomplete and hook-owned forms are left alone.
153
173
 
154
174
  Factory can also refuse fixed client phrases quoted in tool results, such as
155
175
  the identity/environment definitions printed when inspecting this plugin's
@@ -177,7 +197,11 @@ Use magpie's Claude Code integration to select the Factory provider.
177
197
  magpie manages the provider-specific client settings, including capability
178
198
  and permission configuration. Fields such as `safeguards` and
179
199
  `context_management` are forwarded for Factory to validate; the request
180
- adapter preserves them along with the other request options.
200
+ adapter preserves them along with the other request options. One exception:
201
+ Factory's streaming route refuses `context_management` ("Extra inputs are
202
+ not permitted") while its non-streaming route takes it, so a streamed
203
+ request is sent without it. The server then clears nothing from the
204
+ context; Claude Code's own compaction still runs.
181
205
 
182
206
  The same metadata adaptation applies to `/messages` and
183
207
  `/messages/count_tokens`, so token counting sees the prompt used for
package/index.mjs CHANGED
@@ -658,6 +658,15 @@ const SKILL_REMINDER = /^<system-reminder>\nThe following skills are available f
658
658
  const CONFIG_SKILL_METADATA = '- update-config: Use this skill to configure the Claude Code harness via settings.json. Automated behaviors ("from now on when X", "each time X", "whenever X", "before/after X") require hooks configured in settings.json - the harness executes these, not Claude, so memory/preferences cannot fulfill them.'
659
659
  const CONFIG_SKILL_COMPAT = CONFIG_SKILL_METADATA.replace("not Claude", "not the assistant")
660
660
 
661
+ // Claude Code also sends the skill list alone as a system message: the
662
+ // entries only, no header, no <system-reminder> wrapper and no token tail,
663
+ // as text blocks or a string (#72). Only a system message that is such a
664
+ // list (it opens with an entry) changes, and in it only the known line.
665
+ function bareSkills(text) {
666
+ if (!text.startsWith("- ")) return text
667
+ return ("\n" + text).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT).slice(1)
668
+ }
669
+
661
670
  // Claude 5 also sends its runtime metadata as a string-content system
662
671
  // message, instead of user-message reminders. Preserve that role and all
663
672
  // instructions. A model switch sends a separate update without the
@@ -727,6 +736,22 @@ function announcedContext(text, start = 1) {
727
736
  // Any hook owns its file snippets. Non-startup hooks do not stop the
728
737
  // existing adaptation of model/environment metadata that follows them.
729
738
  if (HOOK_NOTIFICATION.test(hook)) quoteChangedFiles = false
739
+ // Invoked skills span several paragraphs. The refused text is the fixed
740
+ // opening plus the complete IMPORTANT paragraph; skill bodies follow
741
+ // until the next attachment this adapter already knows. A token marker
742
+ // after the reminder is not required: Claude Code emits one after a tool
743
+ // result or a user prompt, and the restored reminder usually sits among
744
+ // other attachments. Keep skill text out of the paragraph adapter,
745
+ // including on a second pass.
746
+ if (part.startsWith(INVOKED_SKILLS_OPENING + " ") || part.startsWith(INVOKED_SKILLS_COMPAT + " ")) {
747
+ let end = i + 1
748
+ while (end < parts.length && !invokedSkillsBoundary(parts[end])) end++
749
+ const wrapped = open + parts.slice(i, end).join("\n\n") + close
750
+ if (invokedSkillsMatch(wrapped)) {
751
+ parts.splice(i, end - i, invokedSkills(wrapped).slice(open.length, -close.length))
752
+ continue
753
+ }
754
+ }
730
755
  const changedFile = quoteChangedFiles ? changedFileText(part) : null
731
756
  if (changedFile !== null) {
732
757
  parts[i] = changedFile
@@ -754,19 +779,30 @@ function announcedContext(text, start = 1) {
754
779
  // opening token marker: restored-file notes, Read calls and their results,
755
780
  // the environment, the model line, token and date. Each fixed fragment is
756
781
  // refused on its own (plugins#68). Require the generated restored-file
757
- // opening and the generated environment paragraph or a token marker before
758
- // the first hook; then each paragraph is adapted as in a token-prefixed
759
- // bundle, the rest kept byte-for-byte. An adapted block no longer opens
760
- // with the generated note, so a second pass leaves it as it is.
782
+ // opening and the generated environment paragraph, a token marker or a
783
+ // complete invoked-skills reminder before the first hook. When no file was
784
+ // restored, that complete skills reminder can itself open the turn. Each
785
+ // paragraph is adapted as in a token-prefixed bundle, the rest kept
786
+ // byte-for-byte. An adapted block no longer has its generated opening,
787
+ // so a second pass leaves it as it is.
761
788
  function restoredContext(text) {
762
789
  const parts = text.split(/\n\n(?!\.\.\. \[)/)
763
790
  const result = parts[0].indexOf("\n" + READ_RESULT_HEADER)
764
791
  const opening = "<system-reminder>\n" + (result < 0 ? parts[0] : parts[0].slice(0, result)) + "\n</system-reminder>"
765
- if (!COMPACT_FILE.test(opening) && !COMPACT_READ.test(opening)) return null
766
- if (compactContext(opening) === opening) return null
767
- for (const part of parts.slice(1)) {
792
+ const file = COMPACT_FILE.test(opening) || COMPACT_READ.test(opening)
793
+ if (!file && !parts[0].startsWith(INVOKED_SKILLS_OPENING + " ")) return null
794
+ if (file && compactContext(opening) === opening) return null
795
+ for (let i = 0; i < parts.length; i++) {
796
+ const part = parts[i]
768
797
  if (HOOK_NOTIFICATION.test(part.replace(/^<system-reminder>\n/, ""))) return null
769
- if (SYSTEM_ENV_CONTEXT.test(part + "\n") || /^<total_tokens>\d+ tokens left<\/total_tokens>$/.test(part)) return announcedContext(text, 0)
798
+ if (part.startsWith(INVOKED_SKILLS_OPENING + " ")) {
799
+ let end = i + 1
800
+ while (end < parts.length && !invokedSkillsBoundary(parts[end])) end++
801
+ const wrapped = "<system-reminder>\n" + parts.slice(i, end).join("\n\n") + "\n</system-reminder>"
802
+ if (invokedSkillsMatch(wrapped)) return announcedContext(text, 0)
803
+ if (!file) return null
804
+ }
805
+ if (i > 0 && (SYSTEM_ENV_CONTEXT.test(part + "\n") || /^<total_tokens>\d+ tokens left<\/total_tokens>$/.test(part))) return announcedContext(text, 0)
770
806
  }
771
807
  return null
772
808
  }
@@ -807,6 +843,43 @@ function systemModelLine(text) {
807
843
  const INSTRUCTIONS_REMINDER = "<system-reminder>\nCodebase and user instructions are shown below"
808
844
  const GLOBAL_INSTRUCTIONS = "(user's private global instructions for all projects)"
809
845
 
846
+ // Claude Code (2.1.291 and later) restores skills invoked before compaction
847
+ // as one generated reminder. Factory refuses that fixed opening, and the restored
848
+ // skill text can quote the same client phrases tool output does. Keep the
849
+ // remaining instructions and encode only a complete reminder whose body
850
+ // quotes them. Pasted fragments and clean skill bodies stay verbatim.
851
+ const INVOKED_SKILLS_OPENING = "The following skills were invoked EARLIER in this session (before the conversation was compacted), not on the current turn."
852
+ const INVOKED_SKILLS_COMPAT = "Skills invoked earlier in this session are listed below for context."
853
+ const INVOKED_SKILLS_REMINDER = /^<system-reminder>\nThe following skills were invoked EARLIER in this session \(before the conversation was compacted\), not on the current turn\. They are shown here for context only so you remain aware of their guidelines\.\n\nIMPORTANT: Do NOT re-execute these skills or perform their one-time setup actions \(e\.g\., scheduling, creating files\) again\. Any request or argument text embedded in the skill bodies below — for example under a "## User Request" or "## Input" heading — was captured when that skill was first invoked\. It is NOT the user's current message and NOT a new request: do not act on it as if it were live\. Only continue to apply ongoing behavioral guidelines from these skills where still relevant\.\n\n([\s\S]*)\n<\/system-reminder>$/
854
+ // The next paragraph of a bundle that already belongs to another attachment.
855
+ // Skill text ends here, so a following Read, environment or hook stays its
856
+ // own paragraph. A skill body that merely quotes one of these stays inside.
857
+ function invokedSkillsBoundary(part) {
858
+ if (SYSTEM_TOKEN_CONTEXT.test(part) || part.startsWith("<total_tokens>") || HOOK_NOTIFICATION.test(part)) return true
859
+ if (part.startsWith("Called the Read tool with the following input: ")) return true
860
+ if (part.startsWith("Note: ") && part.includes(" was read before the last conversation was summarized")) return true
861
+ if (part.startsWith("Result of calling the Read tool:\n")) return true
862
+ if (SYSTEM_ENV_CONTEXT.test(part + "\n") || SYSTEM_ENV_UPDATE.test(part + "\n")) return true
863
+ if (SYSTEM_MODEL_UPDATE.test(part)) return true
864
+ if (part === "The following skills are available for use with the Skill tool:") return true
865
+ if (part.startsWith("Available agent types for the Agent tool:")) return true
866
+ if (part.startsWith("# MCP Server Instructions")) return true
867
+ if (CHANGED_FILE_HEADER.test(part)) return true
868
+ return false
869
+ }
870
+ function invokedSkillsMatch(text) {
871
+ const opening = "<system-reminder>\n" + INVOKED_SKILLS_COMPAT
872
+ const generated = text.startsWith(opening) ? "<system-reminder>\n" + INVOKED_SKILLS_OPENING + text.slice(opening.length) : text
873
+ return generated.match(INVOKED_SKILLS_REMINDER)
874
+ }
875
+ function invokedSkills(text) {
876
+ const match = invokedSkillsMatch(text)
877
+ if (!match) return text
878
+ const quoted = quotedToolText(match[1], "Skill text encoded as a JSON string. Decode the JSON string to recover the exact original text before using it:\n")
879
+ const head = text.slice(0, text.length - match[1].length - "\n</system-reminder>".length)
880
+ return head.replace(INVOKED_SKILLS_OPENING, INVOKED_SKILLS_COMPAT) + quoted + "\n</system-reminder>"
881
+ }
882
+
810
883
  // Factory returns 403 for Claude Code's fixed compaction opening, even
811
884
  // without tools or other history. Match the two generated opening sentences,
812
885
  // including the provenance prefix, without requiring a Summary label. Keep
@@ -840,6 +913,8 @@ function compactContext(text, quoteChangedFiles = true) {
840
913
  }
841
914
  const summary = piSummary(text)
842
915
  if (summary !== text) return summary
916
+ const skills = invokedSkills(text)
917
+ if (skills !== text) return skills
843
918
  const read = text.match(COMPACT_READ)
844
919
  if (read) {
845
920
  try {
@@ -975,7 +1050,7 @@ function anthropicBody(body) {
975
1050
 
976
1051
  for (const message of request.messages) {
977
1052
  if (message?.role === "system" && typeof message.content === "string") {
978
- const adapted = clientOpening(systemContext(message.content))
1053
+ const adapted = bareSkills(clientOpening(systemContext(message.content)))
979
1054
  if (adapted !== message.content) {
980
1055
  message.content = adapted
981
1056
  changed = true
@@ -988,7 +1063,7 @@ function anthropicBody(body) {
988
1063
  if (message?.role === "system" && Array.isArray(message.content)) {
989
1064
  for (const block of message.content) {
990
1065
  if (block?.type !== "text" || typeof block.text !== "string") continue
991
- const adapted = clientOpening(systemContext(block.text))
1066
+ const adapted = bareSkills(clientOpening(systemContext(block.text)))
992
1067
  if (adapted !== block.text) {
993
1068
  block.text = adapted
994
1069
  changed = true
@@ -1060,6 +1135,14 @@ function anthropicBody(body) {
1060
1135
  }
1061
1136
  }
1062
1137
  }
1138
+ // Factory's streaming route refuses context_management ("Extra inputs
1139
+ // are not permitted", #71) while its non-streaming route takes it. A
1140
+ // streamed request goes without it: the server then edits nothing out of
1141
+ // the context, and the client's own compaction still runs.
1142
+ if (request.stream === true && "context_management" in request) {
1143
+ delete request.context_management
1144
+ changed = true
1145
+ }
1063
1146
  return changed ? JSON.stringify(request) : body
1064
1147
  }
1065
1148
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",