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

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 +7 -0
  2. package/index.mjs +58 -7
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -143,6 +143,13 @@ When runtime notifications precede the skill update, the adapter recognizes
143
143
  the complete skill-header and bullet-list paragraphs inside a token-terminated
144
144
  bundle. It preserves MCP connection errors, other notifications, mode updates
145
145
  and custom skill descriptions while adapting the known built-in description.
146
+ 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.
146
153
 
147
154
  Factory can also refuse fixed client phrases quoted in tool results, such as
148
155
  the identity/environment definitions printed when inspecting this plugin's
package/index.mjs CHANGED
@@ -286,20 +286,43 @@ function clientOpening(text) {
286
286
  return typeof text === "string" ? text.replace(PI_OPENING, (_, at) => at + PI_COMPAT) : text
287
287
  }
288
288
 
289
+ // pi turns a compaction, and a branch it came back from, into a user
290
+ // message of one text: a fixed sentence, then the summary in <summary> tags
291
+ // (pi-coding-agent 1.0.4 and 1.1.0, dist/core/messages.js:
292
+ // COMPACTION_SUMMARY_PREFIX and BRANCH_SUMMARY_PREFIX). Factory refuses the
293
+ // compaction sentence whole (yetone/magpie#1316): after a compaction every
294
+ // turn was 403, and the reporter's replays passed with the sentence removed,
295
+ // reworded or only half of it. The branch sentence is pi's other fixed
296
+ // summary opening and is reworded the same way. Only a text that is pi's
297
+ // whole summary, sentence first and </summary> last, changes; the summary
298
+ // itself goes on as it is.
299
+ const PI_SUMMARIES = [
300
+ ["The conversation history before this point was compacted into the following summary:\n\n<summary>\n", "Earlier conversation context is summarized below:\n\n<summary>\n"],
301
+ ["The following is a summary of a branch that this conversation came back from:\n\n<summary>\n", "Summary of an earlier conversation branch:\n\n<summary>\n"],
302
+ ]
303
+ function piSummary(text) {
304
+ if (typeof text !== "string" || !text.endsWith("</summary>")) return text
305
+ for (const [from, to] of PI_SUMMARIES) if (text.startsWith(from)) return to + text.slice(from.length)
306
+ return text
307
+ }
308
+
289
309
  // promptOpenings adapts clientOpening in the system and developer messages
290
- // of msgs (chat completions' messages, Responses' input), their string
291
- // content or text parts: false when nothing changed.
310
+ // of msgs (chat completions' messages, Responses' input), and piSummary in
311
+ // their user messages, their string content or text parts: false when
312
+ // nothing changed.
292
313
  function promptOpenings(msgs) {
293
314
  let changed = false
294
315
  for (const m of Array.isArray(msgs) ? msgs : []) {
295
- if (!m || typeof m !== "object" || (m.role !== "system" && m.role !== "developer")) continue
316
+ if (!m || typeof m !== "object") continue
317
+ const adapt = m.role === "system" || m.role === "developer" ? clientOpening : m.role === "user" ? piSummary : null
318
+ if (!adapt) continue
296
319
  if (typeof m.content === "string") {
297
- const adapted = clientOpening(m.content)
320
+ const adapted = adapt(m.content)
298
321
  if (adapted !== m.content) (m.content = adapted), (changed = true)
299
322
  } else if (Array.isArray(m.content)) {
300
323
  for (const p of m.content) {
301
324
  if (!p || typeof p !== "object" || typeof p.text !== "string") continue
302
- const adapted = clientOpening(p.text)
325
+ const adapted = adapt(p.text)
303
326
  if (adapted !== p.text) (p.text = adapted), (changed = true)
304
327
  }
305
328
  }
@@ -662,6 +685,8 @@ const SYSTEM_SKILL_CONTEXT = /^The following skills are available for use with t
662
685
  function systemContext(text) {
663
686
  text = changedFileContext(text)
664
687
  text = announcedSkills(text)
688
+ const restored = restoredContext(text)
689
+ if (restored !== null) return restored
665
690
  if (SYSTEM_TOKEN_OPENING.test(text) || SYSTEM_SKILL_CONTEXT.test(text)) return announcedContext(text)
666
691
  if (HOOK_OUTPUT.test(text) || text.startsWith(DEFERRED_TOOLS_OPENING)) {
667
692
  const at = text.indexOf(HOOK_CONTEXT)
@@ -691,11 +716,11 @@ function announcedSkills(text) {
691
716
  // Claude Code also announces several reminders as one system turn, with
692
717
  // token context and no system-reminder wrappers. Only known metadata
693
718
  // paragraphs change; numbered file contents and the hook's own output stay.
694
- function announcedContext(text) {
719
+ function announcedContext(text, start = 1) {
695
720
  const parts = changedFileContext(text).split(/\n\n(?!\.\.\. \[)/)
696
721
  const open = "<system-reminder>\n", close = "\n</system-reminder>"
697
722
  let quoteChangedFiles = true
698
- for (let i = 1; i < parts.length; i++) {
723
+ for (let i = start; i < parts.length; i++) {
699
724
  const part = parts[i]
700
725
  const hook = part.replace(/^<system-reminder>\n/, "")
701
726
  if (HOOK_OUTPUT.test(hook)) break
@@ -724,6 +749,28 @@ function announcedContext(text) {
724
749
  return parts.join("\n\n")
725
750
  }
726
751
 
752
+ // After /compact, Claude Code (2.1.280) sends the files it restores and the
753
+ // session's runtime context as one turn without reminder wrappers or an
754
+ // opening token marker: restored-file notes, Read calls and their results,
755
+ // the environment, the model line, token and date. Each fixed fragment is
756
+ // 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.
761
+ function restoredContext(text) {
762
+ const parts = text.split(/\n\n(?!\.\.\. \[)/)
763
+ const result = parts[0].indexOf("\n" + READ_RESULT_HEADER)
764
+ 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)) {
768
+ 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)
770
+ }
771
+ return null
772
+ }
773
+
727
774
  function generatedContext(text) {
728
775
  const environment = SYSTEM_ENV_CONTEXT.test(text)
729
776
  if (!environment && !((SYSTEM_MODEL_UPDATE.test(text) || SYSTEM_ENV_UPDATE.test(text)) && SYSTEM_TOKEN_CONTEXT.test(text))) return text
@@ -778,6 +825,8 @@ function compactContext(text, quoteChangedFiles = true) {
778
825
  // A translating gateway can fold an announced system turn into user text.
779
826
  if (SYSTEM_TOKEN_OPENING.test(text) || SYSTEM_SKILL_CONTEXT.test(text)) return announcedContext(text)
780
827
  if (HOOK_OUTPUT.test(text) || text.startsWith(DEFERRED_TOOLS_OPENING)) return systemContext(text)
828
+ const restored = restoredContext(text)
829
+ if (restored !== null) return restored
781
830
  const result = readResultText(text)
782
831
  if (result !== null) return result
783
832
  const changedFile = quoteChangedFiles && text.startsWith("<system-reminder>\n") ? changedFileText(text) : null
@@ -789,6 +838,8 @@ function compactContext(text, quoteChangedFiles = true) {
789
838
  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
839
  return header + (quoted === context ? context : "\n\n" + quoted)
791
840
  }
841
+ const summary = piSummary(text)
842
+ if (summary !== text) return summary
792
843
  const read = text.match(COMPACT_READ)
793
844
  if (read) {
794
845
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.20",
3
+ "version": "0.1.22",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",