@magpie-community/opencode-factory-auth 0.1.19 → 0.1.20

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 +13 -2
  2. package/index.mjs +62 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -121,8 +121,11 @@ Claude Code's generated compaction summaries also have a fixed opening
121
121
  that Factory refuses. The adapter recognizes the two generated opening
122
122
  sentences and rephrases only the first in user strings and text blocks,
123
123
  including summaries without a `Summary:` label. The generated artifact
124
- provenance marker and note, summary body and continuation instructions stay
125
- verbatim.
124
+ provenance marker and note stay verbatim. If the summary itself quotes any
125
+ of the refused client phrases handled in tool output, the context after the
126
+ two opening sentences is encoded as a JSON string with explicit decoding
127
+ instructions. Decoding restores the exact summary, transcript path and
128
+ continuation instructions; context without those phrases stays verbatim.
126
129
  Complete generated reminders for restored Read calls and omitted files are
127
130
  adapted too. Read arguments and file paths are preserved; quoted or incomplete
128
131
  reminders are left alone.
@@ -154,6 +157,14 @@ The same lossless encoding applies to restored or `@`-attached Read file text
154
157
  after the exact `Result of calling the Read tool:` header, including complete
155
158
  reminder wrappers and announced runtime bundles. The generated header and
156
159
  wrapper stay intact. File text without a known refused phrase is unchanged.
160
+ Claude Code's complete changed-on-disk notifications receive the same handling:
161
+ only the numbered file snippet is encoded, retaining its TAB or colon line
162
+ separators, hunk separators and truncation notice. The path and instructions
163
+ about respecting the current file stay verbatim. This covers wrapped user
164
+ attachments and generated system bundles, including notifications before a
165
+ token marker. Changed-file text supplied by hooks, quoted or incomplete
166
+ notifications, and notices that omit the snippet are left alone. Model and
167
+ environment metadata after non-startup hooks retain their existing adaptation.
157
168
 
158
169
  Use magpie's Claude Code integration to select the Factory provider.
159
170
  magpie manages the provider-specific client settings, including capability
package/index.mjs CHANGED
@@ -650,6 +650,9 @@ const SYSTEM_TOKEN_CONTEXT = /(?:^|\n\n)<total_tokens>\d+ tokens left<\/total_to
650
650
  // context in the same message (#634 and subagent startup). Adapt only the
651
651
  // environment suffix; keep the hook output and tool announcement verbatim.
652
652
  const HOOK_OUTPUT = /^(?:SessionStart|SubagentStart)(?::[^\n]* hook success:| hook additional context:)/
653
+ // Other hook events can also own additional context inside a notification
654
+ // bundle. This guard only stops rewriting; it does not adapt their output.
655
+ const HOOK_NOTIFICATION = /^[A-Z][A-Za-z]*(?::[^\n]* hook success:| hook additional context:)/
653
656
  const DEFERRED_TOOLS_OPENING = 'The following deferred tools are now available via ToolSearch. Their schemas are NOT loaded — calling them directly will fail with InputValidationError. Use ToolSearch with query "select:<name>[,<name>...]" to load tool schemas before calling them:\n'
654
657
  const HOOK_CONTEXT = "\n# Environment\nYou have been invoked in the following environment:"
655
658
  const SYSTEM_TOKEN_OPENING = /^<total_tokens>\d+ tokens left<\/total_tokens>\n\n/
@@ -657,6 +660,7 @@ const SYSTEM_TOKEN_OPENING = /^<total_tokens>\d+ tokens left<\/total_tokens>\n\n
657
660
  // auto mode). Require the generated list opening and a complete token tail.
658
661
  const SYSTEM_SKILL_CONTEXT = /^The following skills are available for use with the Skill tool:\n\n- [\s\S]*\n\n<total_tokens>\d+ tokens left<\/total_tokens>$/
659
662
  function systemContext(text) {
663
+ text = changedFileContext(text)
660
664
  text = announcedSkills(text)
661
665
  if (SYSTEM_TOKEN_OPENING.test(text) || SYSTEM_SKILL_CONTEXT.test(text)) return announcedContext(text)
662
666
  if (HOOK_OUTPUT.test(text) || text.startsWith(DEFERRED_TOOLS_OPENING)) {
@@ -688,15 +692,25 @@ function announcedSkills(text) {
688
692
  // token context and no system-reminder wrappers. Only known metadata
689
693
  // paragraphs change; numbered file contents and the hook's own output stay.
690
694
  function announcedContext(text) {
691
- const parts = text.split("\n\n")
695
+ const parts = changedFileContext(text).split(/\n\n(?!\.\.\. \[)/)
692
696
  const open = "<system-reminder>\n", close = "\n</system-reminder>"
697
+ let quoteChangedFiles = true
693
698
  for (let i = 1; i < parts.length; i++) {
694
699
  const part = parts[i]
695
- if (HOOK_OUTPUT.test(part)) break
700
+ const hook = part.replace(/^<system-reminder>\n/, "")
701
+ if (HOOK_OUTPUT.test(hook)) break
702
+ // Any hook owns its file snippets. Non-startup hooks do not stop the
703
+ // existing adaptation of model/environment metadata that follows them.
704
+ if (HOOK_NOTIFICATION.test(hook)) quoteChangedFiles = false
705
+ const changedFile = quoteChangedFiles ? changedFileText(part) : null
706
+ if (changedFile !== null) {
707
+ parts[i] = changedFile
708
+ continue
709
+ }
696
710
  const result = part.indexOf("\n" + READ_RESULT_HEADER)
697
711
  const heading = result < 0 ? part : part.slice(0, result)
698
712
  const wrapped = open + heading + close
699
- const compacted = compactContext(wrapped)
713
+ const compacted = compactContext(wrapped, quoteChangedFiles)
700
714
  if (compacted !== wrapped) {
701
715
  parts[i] = compacted.slice(open.length, -close.length) + (result < 0 ? "" : "\n" + readResultText(part.slice(result + 1)))
702
716
  } else if (SYSTEM_ENV_CONTEXT.test(part + "\n")) {
@@ -749,9 +763,10 @@ const GLOBAL_INSTRUCTIONS = "(user's private global instructions for all project
749
763
  // Factory returns 403 for Claude Code's fixed compaction opening, even
750
764
  // without tools or other history. Match the two generated opening sentences,
751
765
  // including the provenance prefix, without requiring a Summary label. Keep
752
- // the provenance, summary and continuation instructions verbatim. File
753
- // reminders restored after compaction need the same narrow adaptation;
754
- // their paths and read arguments stay intact; refused file text is quoted
766
+ // the provenance verbatim. A summary that quotes refused client metadata
767
+ // needs lossless encoding too, including its transcript path and continuation
768
+ // instructions. File reminders restored after compaction need the same narrow
769
+ // adaptation; their paths and read arguments stay intact; refused file text is quoted
755
770
  // losslessly below.
756
771
  const COMPACT_OPENING = "This session is being continued from a previous conversation that ran out of context."
757
772
  const COMPACT_HEADER = COMPACT_OPENING + " The summary below covers the earlier portion of the conversation."
@@ -759,14 +774,21 @@ const COMPACT_ARTIFACT = /^<artifact-content-authored-by-others\/>\nThe summariz
759
774
  const READ_RESULT_HEADER = "Result of calling the Read tool:\n"
760
775
  const COMPACT_READ = /^<system-reminder>\nCalled the Read tool with the following input: (\{[^\n]*\})\n<\/system-reminder>$/
761
776
  const COMPACT_FILE = /^<system-reminder>\nNote: ([^\n]+) was read before the last conversation was summarized, but the contents are too large to include\. Use Read tool if you need to access it\.\n<\/system-reminder>$/
762
- function compactContext(text) {
777
+ function compactContext(text, quoteChangedFiles = true) {
763
778
  // A translating gateway can fold an announced system turn into user text.
764
779
  if (SYSTEM_TOKEN_OPENING.test(text) || SYSTEM_SKILL_CONTEXT.test(text)) return announcedContext(text)
765
780
  if (HOOK_OUTPUT.test(text) || text.startsWith(DEFERRED_TOOLS_OPENING)) return systemContext(text)
766
781
  const result = readResultText(text)
767
782
  if (result !== null) return result
783
+ const changedFile = quoteChangedFiles && text.startsWith("<system-reminder>\n") ? changedFileText(text) : null
784
+ if (changedFile !== null) return changedFile
768
785
  const prefix = text.match(COMPACT_ARTIFACT)?.[0] ?? ""
769
- if (text.startsWith(COMPACT_HEADER, prefix.length)) return prefix + "Earlier conversation context is summarized below." + text.slice(prefix.length + COMPACT_OPENING.length)
786
+ if (text.startsWith(COMPACT_HEADER, prefix.length)) {
787
+ const context = text.slice(prefix.length + COMPACT_HEADER.length)
788
+ const header = prefix + COMPACT_HEADER.replace(COMPACT_OPENING, "Earlier conversation context is summarized below.")
789
+ const quoted = quotedToolText(context, "Conversation context encoded as a JSON string. Decode the JSON string to recover the exact original context before continuing:\n")
790
+ return header + (quoted === context ? context : "\n\n" + quoted)
791
+ }
770
792
  const read = text.match(COMPACT_READ)
771
793
  if (read) {
772
794
  try {
@@ -789,13 +811,43 @@ function readResultText(text) {
789
811
  return prefix + quotedToolText(content) + suffix
790
812
  }
791
813
 
814
+ // Claude Code's edited_text_file attachment reports current file lines,
815
+ // not a unified diff. Keep its path and instructions; quote only the lines.
816
+ const CHANGED_FILE_HEADER = /^Note: [^\n]+ changed on disk since you last read it\. That's usually deliberate, so take it as the current state rather than reverting it; if the change looks wrong, say so rather than undoing it yourself — otherwise no need to call it out\. Here are the relevant changes \(shown with line numbers\):\n/
817
+ function changedFileText(text) {
818
+ const open = "<system-reminder>\n", close = "\n</system-reminder>"
819
+ const wrapped = text.startsWith(open)
820
+ if (wrapped && !text.endsWith(close)) return null
821
+ const content = wrapped ? text.slice(open.length, -close.length) : text
822
+ const header = content.match(CHANGED_FILE_HEADER)?.[0]
823
+ if (!header) return null
824
+ const lines = content.slice(header.length)
825
+ if (!/^\d+[\t:][^\n]*(?:\n(?:\.\.\.\n)?\d+[\t:][^\n]*)*(?:(?:\n\.\.\.)?\n\n\.\.\. \[\d+ lines truncated\] \.\.\.)?$/.test(lines)) return null
826
+ return (wrapped ? open : "") + header + quotedToolText(lines) + (wrapped ? close : "")
827
+ }
828
+
829
+ // System-turn notifications may start with the changed file, or put it
830
+ // among other notifications before the final token marker. Hook output is
831
+ // owned by the hook, even when it quotes a complete generated notification.
832
+ function changedFileContext(text) {
833
+ // Keep even an incomplete truncation tail with its snippet, so the
834
+ // complete-notification check can reject it rather than encode a fragment.
835
+ const parts = text.split(/\n\n(?!\.\.\. \[)/)
836
+ if (changedFileText(parts[0]) === null && !SYSTEM_TOKEN_OPENING.test(text) && !/(?:^|\n\n)<total_tokens>\d+ tokens left<\/total_tokens>$/.test(text)) return text
837
+ for (let i = 0; i < parts.length; i++) {
838
+ if (HOOK_NOTIFICATION.test(parts[i].replace(/^<system-reminder>\n/, ""))) break
839
+ parts[i] = changedFileText(parts[i]) ?? parts[i]
840
+ }
841
+ return parts.join("\n\n")
842
+ }
843
+
792
844
  // Factory also refuses these fixed client phrases when they are quoted in
793
845
  // tool output, e.g. while reading this adapter's source. Keep the original
794
846
  // text recoverable: a JSON string with explicit decoding instructions,
795
847
  // rather than deleting or rewriting the file's contents. Other output and
796
848
  // the tool's id, error/cache markers and non-text blocks stay untouched.
797
849
  const QUOTED_TOOL_PREFIX = "Tool output encoded as a JSON string. Decode the JSON string to recover the exact original text before using it:\n"
798
- function quotedToolText(text) {
850
+ function quotedToolText(text, prefix = QUOTED_TOOL_PREFIX) {
799
851
  if (![...CLAUDE_IDENTITIES].some((identity) => text.includes(identity)) &&
800
852
  !text.includes("You have been invoked in the following environment:") &&
801
853
  !text.includes("x-anthropic-billing-header: cc_version=") &&
@@ -804,7 +856,7 @@ function quotedToolText(text) {
804
856
  for (const phrase of ["You are", "You have", "x-anthropic-billing-header", "system-reminder", COMPACT_OPENING]) {
805
857
  encoded = encoded.replaceAll(phrase, "\\u" + phrase.charCodeAt(0).toString(16).padStart(4, "0") + phrase.slice(1))
806
858
  }
807
- return QUOTED_TOOL_PREFIX + encoded
859
+ return prefix + encoded
808
860
  }
809
861
 
810
862
  function anthropicBody(body) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.19",
3
+ "version": "0.1.20",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",